blob: e41b1d964ea52073901ef4f284285d006850bdb6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02002/*
3 * usb/gadget/config.c -- simplify building config descriptors
4 *
5 * Copyright (C) 2003 David Brownell
6 *
Bin Meng75574052016-02-05 19:30:11 -08007 * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
Remy Bohmerdf063442009-07-29 18:18:43 +02008 * Remy Bohmer <linux@bohmer.net>
9 */
10
Troy Kisky719f1a72013-09-11 18:24:48 +080011#include <asm/unaligned.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020013#include <linux/list.h>
14#include <linux/string.h>
15
16#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18
Remy Bohmerdf063442009-07-29 18:18:43 +020019/**
20 * usb_descriptor_fillbuf - fill buffer with descriptors
21 * @buf: Buffer to be filled
22 * @buflen: Size of buf
23 * @src: Array of descriptor pointers, terminated by null pointer.
24 *
25 * Copies descriptors into the buffer, returning the length or a
26 * negative error code if they can't all be copied. Useful when
27 * assembling descriptors for an associated set of interfaces used
28 * as part of configuring a composite device; or in other cases where
29 * sets of descriptors need to be marshaled.
30 */
31int
32usb_descriptor_fillbuf(void *buf, unsigned buflen,
33 const struct usb_descriptor_header **src)
34{
35 u8 *dest = buf;
36
37 if (!src)
38 return -EINVAL;
39
40 /* fill buffer from src[] until null descriptor ptr */
41 for (; NULL != *src; src++) {
42 unsigned len = (*src)->bLength;
43
44 if (len > buflen)
45 return -EINVAL;
46 memcpy(dest, *src, len);
47 buflen -= len;
48 dest += len;
49 }
50 return dest - (u8 *)buf;
51}
52
Remy Bohmerdf063442009-07-29 18:18:43 +020053/**
54 * usb_gadget_config_buf - builts a complete configuration descriptor
55 * @config: Header for the descriptor, including characteristics such
56 * as power requirements and number of interfaces.
57 * @desc: Null-terminated vector of pointers to the descriptors (interface,
58 * endpoint, etc) defining all functions in this device configuration.
59 * @buf: Buffer for the resulting configuration descriptor.
60 * @length: Length of buffer. If this is not big enough to hold the
61 * entire configuration descriptor, an error code will be returned.
62 *
63 * This copies descriptors into the response buffer, building a descriptor
64 * for that configuration. It returns the buffer length or a negative
65 * status code. The config.wTotalLength field is set to match the length
66 * of the result, but other descriptor fields (including power usage and
67 * interface count) must be set by the caller.
68 *
69 * Gadget drivers could use this when constructing a config descriptor
70 * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
71 * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
72 */
73int usb_gadget_config_buf(
74 const struct usb_config_descriptor *config,
75 void *buf,
76 unsigned length,
77 const struct usb_descriptor_header **desc
78)
79{
80 struct usb_config_descriptor *cp = buf;
81 int len;
82
83 /* config descriptor first */
84 if (length < USB_DT_CONFIG_SIZE || !desc)
85 return -EINVAL;
Troy Kisky719f1a72013-09-11 18:24:48 +080086 /* config need not be aligned */
87 memcpy(cp, config, sizeof(*cp));
Remy Bohmerdf063442009-07-29 18:18:43 +020088
89 /* then interface/endpoint/class/vendor/... */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040090 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
Remy Bohmerdf063442009-07-29 18:18:43 +020091 length - USB_DT_CONFIG_SIZE, desc);
92 if (len < 0)
93 return len;
94 len += USB_DT_CONFIG_SIZE;
95 if (len > 0xffff)
96 return -EINVAL;
97
98 /* patch up the config descriptor */
99 cp->bLength = USB_DT_CONFIG_SIZE;
100 cp->bDescriptorType = USB_DT_CONFIG;
Troy Kisky719f1a72013-09-11 18:24:48 +0800101 put_unaligned_le16(len, &cp->wTotalLength);
Remy Bohmerdf063442009-07-29 18:18:43 +0200102 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
103 return len;
104}