1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * usb/gadget/config.c -- simplify building config descriptors
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2003 David Brownell
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/errno.h>
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/list.h>
13*4882a593Smuzhiyun #include <linux/string.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/usb/ch9.h>
17*4882a593Smuzhiyun #include <linux/usb/gadget.h>
18*4882a593Smuzhiyun #include <linux/usb/composite.h>
19*4882a593Smuzhiyun #include <linux/usb/otg.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /**
22*4882a593Smuzhiyun * usb_descriptor_fillbuf - fill buffer with descriptors
23*4882a593Smuzhiyun * @buf: Buffer to be filled
24*4882a593Smuzhiyun * @buflen: Size of buf
25*4882a593Smuzhiyun * @src: Array of descriptor pointers, terminated by null pointer.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * Copies descriptors into the buffer, returning the length or a
28*4882a593Smuzhiyun * negative error code if they can't all be copied. Useful when
29*4882a593Smuzhiyun * assembling descriptors for an associated set of interfaces used
30*4882a593Smuzhiyun * as part of configuring a composite device; or in other cases where
31*4882a593Smuzhiyun * sets of descriptors need to be marshaled.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun int
usb_descriptor_fillbuf(void * buf,unsigned buflen,const struct usb_descriptor_header ** src)34*4882a593Smuzhiyun usb_descriptor_fillbuf(void *buf, unsigned buflen,
35*4882a593Smuzhiyun const struct usb_descriptor_header **src)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun u8 *dest = buf;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun if (!src)
40*4882a593Smuzhiyun return -EINVAL;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* fill buffer from src[] until null descriptor ptr */
43*4882a593Smuzhiyun for (; NULL != *src; src++) {
44*4882a593Smuzhiyun unsigned len = (*src)->bLength;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun if (len > buflen)
47*4882a593Smuzhiyun return -EINVAL;
48*4882a593Smuzhiyun memcpy(dest, *src, len);
49*4882a593Smuzhiyun buflen -= len;
50*4882a593Smuzhiyun dest += len;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun return dest - (u8 *)buf;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_descriptor_fillbuf);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun * usb_gadget_config_buf - builts a complete configuration descriptor
58*4882a593Smuzhiyun * @config: Header for the descriptor, including characteristics such
59*4882a593Smuzhiyun * as power requirements and number of interfaces.
60*4882a593Smuzhiyun * @desc: Null-terminated vector of pointers to the descriptors (interface,
61*4882a593Smuzhiyun * endpoint, etc) defining all functions in this device configuration.
62*4882a593Smuzhiyun * @buf: Buffer for the resulting configuration descriptor.
63*4882a593Smuzhiyun * @length: Length of buffer. If this is not big enough to hold the
64*4882a593Smuzhiyun * entire configuration descriptor, an error code will be returned.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * This copies descriptors into the response buffer, building a descriptor
67*4882a593Smuzhiyun * for that configuration. It returns the buffer length or a negative
68*4882a593Smuzhiyun * status code. The config.wTotalLength field is set to match the length
69*4882a593Smuzhiyun * of the result, but other descriptor fields (including power usage and
70*4882a593Smuzhiyun * interface count) must be set by the caller.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * Gadget drivers could use this when constructing a config descriptor
73*4882a593Smuzhiyun * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
74*4882a593Smuzhiyun * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
75*4882a593Smuzhiyun */
usb_gadget_config_buf(const struct usb_config_descriptor * config,void * buf,unsigned length,const struct usb_descriptor_header ** desc)76*4882a593Smuzhiyun int usb_gadget_config_buf(
77*4882a593Smuzhiyun const struct usb_config_descriptor *config,
78*4882a593Smuzhiyun void *buf,
79*4882a593Smuzhiyun unsigned length,
80*4882a593Smuzhiyun const struct usb_descriptor_header **desc
81*4882a593Smuzhiyun )
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct usb_config_descriptor *cp = buf;
84*4882a593Smuzhiyun int len;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* config descriptor first */
87*4882a593Smuzhiyun if (length < USB_DT_CONFIG_SIZE || !desc)
88*4882a593Smuzhiyun return -EINVAL;
89*4882a593Smuzhiyun *cp = *config;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* then interface/endpoint/class/vendor/... */
92*4882a593Smuzhiyun len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
93*4882a593Smuzhiyun length - USB_DT_CONFIG_SIZE, desc);
94*4882a593Smuzhiyun if (len < 0)
95*4882a593Smuzhiyun return len;
96*4882a593Smuzhiyun len += USB_DT_CONFIG_SIZE;
97*4882a593Smuzhiyun if (len > 0xffff)
98*4882a593Smuzhiyun return -EINVAL;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* patch up the config descriptor */
101*4882a593Smuzhiyun cp->bLength = USB_DT_CONFIG_SIZE;
102*4882a593Smuzhiyun cp->bDescriptorType = USB_DT_CONFIG;
103*4882a593Smuzhiyun cp->wTotalLength = cpu_to_le16(len);
104*4882a593Smuzhiyun cp->bmAttributes |= USB_CONFIG_ATT_ONE;
105*4882a593Smuzhiyun return len;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_gadget_config_buf);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun * usb_copy_descriptors - copy a vector of USB descriptors
111*4882a593Smuzhiyun * @src: null-terminated vector to copy
112*4882a593Smuzhiyun * Context: initialization code, which may sleep
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * This makes a copy of a vector of USB descriptors. Its primary use
115*4882a593Smuzhiyun * is to support usb_function objects which can have multiple copies,
116*4882a593Smuzhiyun * each needing different descriptors. Functions may have static
117*4882a593Smuzhiyun * tables of descriptors, which are used as templates and customized
118*4882a593Smuzhiyun * with identifiers (for interfaces, strings, endpoints, and more)
119*4882a593Smuzhiyun * as needed by a given function instance.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun struct usb_descriptor_header **
usb_copy_descriptors(struct usb_descriptor_header ** src)122*4882a593Smuzhiyun usb_copy_descriptors(struct usb_descriptor_header **src)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun struct usb_descriptor_header **tmp;
125*4882a593Smuzhiyun unsigned bytes;
126*4882a593Smuzhiyun unsigned n_desc;
127*4882a593Smuzhiyun void *mem;
128*4882a593Smuzhiyun struct usb_descriptor_header **ret;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* count descriptors and their sizes; then add vector size */
131*4882a593Smuzhiyun for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
132*4882a593Smuzhiyun bytes += (*tmp)->bLength;
133*4882a593Smuzhiyun bytes += (n_desc + 1) * sizeof(*tmp);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun mem = kmalloc(bytes, GFP_KERNEL);
136*4882a593Smuzhiyun if (!mem)
137*4882a593Smuzhiyun return NULL;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* fill in pointers starting at "tmp",
140*4882a593Smuzhiyun * to descriptors copied starting at "mem";
141*4882a593Smuzhiyun * and return "ret"
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun tmp = mem;
144*4882a593Smuzhiyun ret = mem;
145*4882a593Smuzhiyun mem += (n_desc + 1) * sizeof(*tmp);
146*4882a593Smuzhiyun while (*src) {
147*4882a593Smuzhiyun memcpy(mem, *src, (*src)->bLength);
148*4882a593Smuzhiyun *tmp = mem;
149*4882a593Smuzhiyun tmp++;
150*4882a593Smuzhiyun mem += (*src)->bLength;
151*4882a593Smuzhiyun src++;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun *tmp = NULL;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return ret;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_copy_descriptors);
158*4882a593Smuzhiyun
usb_assign_descriptors(struct usb_function * f,struct usb_descriptor_header ** fs,struct usb_descriptor_header ** hs,struct usb_descriptor_header ** ss,struct usb_descriptor_header ** ssp)159*4882a593Smuzhiyun int usb_assign_descriptors(struct usb_function *f,
160*4882a593Smuzhiyun struct usb_descriptor_header **fs,
161*4882a593Smuzhiyun struct usb_descriptor_header **hs,
162*4882a593Smuzhiyun struct usb_descriptor_header **ss,
163*4882a593Smuzhiyun struct usb_descriptor_header **ssp)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct usb_gadget *g = f->config->cdev->gadget;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* super-speed-plus descriptor falls back to super-speed one,
168*4882a593Smuzhiyun * if such a descriptor was provided, thus avoiding a NULL
169*4882a593Smuzhiyun * pointer dereference if a 5gbps capable gadget is used with
170*4882a593Smuzhiyun * a 10gbps capable config (device port + cable + host port)
171*4882a593Smuzhiyun */
172*4882a593Smuzhiyun if (!ssp)
173*4882a593Smuzhiyun ssp = ss;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun if (fs) {
176*4882a593Smuzhiyun f->fs_descriptors = usb_copy_descriptors(fs);
177*4882a593Smuzhiyun if (!f->fs_descriptors)
178*4882a593Smuzhiyun goto err;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun if (hs && gadget_is_dualspeed(g)) {
181*4882a593Smuzhiyun f->hs_descriptors = usb_copy_descriptors(hs);
182*4882a593Smuzhiyun if (!f->hs_descriptors)
183*4882a593Smuzhiyun goto err;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun if (ss && gadget_is_superspeed(g)) {
186*4882a593Smuzhiyun f->ss_descriptors = usb_copy_descriptors(ss);
187*4882a593Smuzhiyun if (!f->ss_descriptors)
188*4882a593Smuzhiyun goto err;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun if (ssp && gadget_is_superspeed_plus(g)) {
191*4882a593Smuzhiyun f->ssp_descriptors = usb_copy_descriptors(ssp);
192*4882a593Smuzhiyun if (!f->ssp_descriptors)
193*4882a593Smuzhiyun goto err;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun return 0;
196*4882a593Smuzhiyun err:
197*4882a593Smuzhiyun usb_free_all_descriptors(f);
198*4882a593Smuzhiyun return -ENOMEM;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_assign_descriptors);
201*4882a593Smuzhiyun
usb_free_all_descriptors(struct usb_function * f)202*4882a593Smuzhiyun void usb_free_all_descriptors(struct usb_function *f)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun usb_free_descriptors(f->fs_descriptors);
205*4882a593Smuzhiyun f->fs_descriptors = NULL;
206*4882a593Smuzhiyun usb_free_descriptors(f->hs_descriptors);
207*4882a593Smuzhiyun f->hs_descriptors = NULL;
208*4882a593Smuzhiyun usb_free_descriptors(f->ss_descriptors);
209*4882a593Smuzhiyun f->ss_descriptors = NULL;
210*4882a593Smuzhiyun usb_free_descriptors(f->ssp_descriptors);
211*4882a593Smuzhiyun f->ssp_descriptors = NULL;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
214*4882a593Smuzhiyun
usb_otg_descriptor_alloc(struct usb_gadget * gadget)215*4882a593Smuzhiyun struct usb_descriptor_header *usb_otg_descriptor_alloc(
216*4882a593Smuzhiyun struct usb_gadget *gadget)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun struct usb_descriptor_header *otg_desc;
219*4882a593Smuzhiyun unsigned length = 0;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
222*4882a593Smuzhiyun length = sizeof(struct usb_otg20_descriptor);
223*4882a593Smuzhiyun else
224*4882a593Smuzhiyun length = sizeof(struct usb_otg_descriptor);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun otg_desc = kzalloc(length, GFP_KERNEL);
227*4882a593Smuzhiyun return otg_desc;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
230*4882a593Smuzhiyun
usb_otg_descriptor_init(struct usb_gadget * gadget,struct usb_descriptor_header * otg_desc)231*4882a593Smuzhiyun int usb_otg_descriptor_init(struct usb_gadget *gadget,
232*4882a593Smuzhiyun struct usb_descriptor_header *otg_desc)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun struct usb_otg_descriptor *otg1x_desc;
235*4882a593Smuzhiyun struct usb_otg20_descriptor *otg20_desc;
236*4882a593Smuzhiyun struct usb_otg_caps *otg_caps = gadget->otg_caps;
237*4882a593Smuzhiyun u8 otg_attributes = 0;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (!otg_desc)
240*4882a593Smuzhiyun return -EINVAL;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (otg_caps && otg_caps->otg_rev) {
243*4882a593Smuzhiyun if (otg_caps->hnp_support)
244*4882a593Smuzhiyun otg_attributes |= USB_OTG_HNP;
245*4882a593Smuzhiyun if (otg_caps->srp_support)
246*4882a593Smuzhiyun otg_attributes |= USB_OTG_SRP;
247*4882a593Smuzhiyun if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
248*4882a593Smuzhiyun otg_attributes |= USB_OTG_ADP;
249*4882a593Smuzhiyun } else {
250*4882a593Smuzhiyun otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
254*4882a593Smuzhiyun otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
255*4882a593Smuzhiyun otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
256*4882a593Smuzhiyun otg20_desc->bDescriptorType = USB_DT_OTG;
257*4882a593Smuzhiyun otg20_desc->bmAttributes = otg_attributes;
258*4882a593Smuzhiyun otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
259*4882a593Smuzhiyun } else {
260*4882a593Smuzhiyun otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
261*4882a593Smuzhiyun otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
262*4882a593Smuzhiyun otg1x_desc->bDescriptorType = USB_DT_OTG;
263*4882a593Smuzhiyun otg1x_desc->bmAttributes = otg_attributes;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun return 0;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);
269