1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * composite.c - infrastructure for Composite USB Gadgets
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2006-2008 David Brownell
5*4882a593Smuzhiyun * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun #undef DEBUG
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/bitops.h>
12*4882a593Smuzhiyun #include <linux/usb/composite.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define USB_BUFSIZ 4096
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /* Helper type for accessing packed u16 pointers */
17*4882a593Smuzhiyun typedef struct { __le16 val; } __packed __le16_packed;
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun static struct usb_composite_driver *composite;
20*4882a593Smuzhiyun
le16_add_cpu_packed(__le16_packed * var,u16 val)21*4882a593Smuzhiyun static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /**
27*4882a593Smuzhiyun * usb_add_function() - add a function to a configuration
28*4882a593Smuzhiyun * @config: the configuration
29*4882a593Smuzhiyun * @function: the function being added
30*4882a593Smuzhiyun * Context: single threaded during gadget setup
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * After initialization, each configuration must have one or more
33*4882a593Smuzhiyun * functions added to it. Adding a function involves calling its @bind()
34*4882a593Smuzhiyun * method to allocate resources such as interface and string identifiers
35*4882a593Smuzhiyun * and endpoints.
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * This function returns the value of the function's bind(), which is
38*4882a593Smuzhiyun * zero for success else a negative errno value.
39*4882a593Smuzhiyun */
usb_add_function(struct usb_configuration * config,struct usb_function * function)40*4882a593Smuzhiyun int usb_add_function(struct usb_configuration *config,
41*4882a593Smuzhiyun struct usb_function *function)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun int value = -EINVAL;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun debug("adding '%s'/%p to config '%s'/%p\n",
46*4882a593Smuzhiyun function->name, function,
47*4882a593Smuzhiyun config->label, config);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (!function->set_alt || !function->disable)
50*4882a593Smuzhiyun goto done;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun function->config = config;
53*4882a593Smuzhiyun list_add_tail(&function->list, &config->functions);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (function->bind) {
56*4882a593Smuzhiyun value = function->bind(config, function);
57*4882a593Smuzhiyun if (value < 0) {
58*4882a593Smuzhiyun list_del(&function->list);
59*4882a593Smuzhiyun function->config = NULL;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun } else
62*4882a593Smuzhiyun value = 0;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun if (!config->fullspeed && function->descriptors)
65*4882a593Smuzhiyun config->fullspeed = 1;
66*4882a593Smuzhiyun if (!config->highspeed && function->hs_descriptors)
67*4882a593Smuzhiyun config->highspeed = 1;
68*4882a593Smuzhiyun if (!config->superspeed && function->ss_descriptors)
69*4882a593Smuzhiyun config->superspeed = 1;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun done:
72*4882a593Smuzhiyun if (value)
73*4882a593Smuzhiyun debug("adding '%s'/%p --> %d\n",
74*4882a593Smuzhiyun function->name, function, value);
75*4882a593Smuzhiyun return value;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun * usb_function_deactivate - prevent function and gadget enumeration
80*4882a593Smuzhiyun * @function: the function that isn't yet ready to respond
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * Blocks response of the gadget driver to host enumeration by
83*4882a593Smuzhiyun * preventing the data line pullup from being activated. This is
84*4882a593Smuzhiyun * normally called during @bind() processing to change from the
85*4882a593Smuzhiyun * initial "ready to respond" state, or when a required resource
86*4882a593Smuzhiyun * becomes available.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * For example, drivers that serve as a passthrough to a userspace
89*4882a593Smuzhiyun * daemon can block enumeration unless that daemon (such as an OBEX,
90*4882a593Smuzhiyun * MTP, or print server) is ready to handle host requests.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * Not all systems support software control of their USB peripheral
93*4882a593Smuzhiyun * data pullups.
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * Returns zero on success, else negative errno.
96*4882a593Smuzhiyun */
usb_function_deactivate(struct usb_function * function)97*4882a593Smuzhiyun int usb_function_deactivate(struct usb_function *function)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun struct usb_composite_dev *cdev = function->config->cdev;
100*4882a593Smuzhiyun int status = 0;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (cdev->deactivations == 0)
103*4882a593Smuzhiyun status = usb_gadget_disconnect(cdev->gadget);
104*4882a593Smuzhiyun if (status == 0)
105*4882a593Smuzhiyun cdev->deactivations++;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun return status;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun * usb_function_activate - allow function and gadget enumeration
112*4882a593Smuzhiyun * @function: function on which usb_function_activate() was called
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * Reverses effect of usb_function_deactivate(). If no more functions
115*4882a593Smuzhiyun * are delaying their activation, the gadget driver will respond to
116*4882a593Smuzhiyun * host enumeration procedures.
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * Returns zero on success, else negative errno.
119*4882a593Smuzhiyun */
usb_function_activate(struct usb_function * function)120*4882a593Smuzhiyun int usb_function_activate(struct usb_function *function)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun struct usb_composite_dev *cdev = function->config->cdev;
123*4882a593Smuzhiyun int status = 0;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (cdev->deactivations == 0)
126*4882a593Smuzhiyun status = -EINVAL;
127*4882a593Smuzhiyun else {
128*4882a593Smuzhiyun cdev->deactivations--;
129*4882a593Smuzhiyun if (cdev->deactivations == 0)
130*4882a593Smuzhiyun status = usb_gadget_connect(cdev->gadget);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun return status;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun * usb_interface_id() - allocate an unused interface ID
138*4882a593Smuzhiyun * @config: configuration associated with the interface
139*4882a593Smuzhiyun * @function: function handling the interface
140*4882a593Smuzhiyun * Context: single threaded during gadget setup
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * usb_interface_id() is called from usb_function.bind() callbacks to
143*4882a593Smuzhiyun * allocate new interface IDs. The function driver will then store that
144*4882a593Smuzhiyun * ID in interface, association, CDC union, and other descriptors. It
145*4882a593Smuzhiyun * will also handle any control requests targetted at that interface,
146*4882a593Smuzhiyun * particularly changing its altsetting via set_alt(). There may
147*4882a593Smuzhiyun * also be class-specific or vendor-specific requests to handle.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * All interface identifier should be allocated using this routine, to
150*4882a593Smuzhiyun * ensure that for example different functions don't wrongly assign
151*4882a593Smuzhiyun * different meanings to the same identifier. Note that since interface
152*4882a593Smuzhiyun * identifers are configuration-specific, functions used in more than
153*4882a593Smuzhiyun * one configuration (or more than once in a given configuration) need
154*4882a593Smuzhiyun * multiple versions of the relevant descriptors.
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * Returns the interface ID which was allocated; or -ENODEV if no
157*4882a593Smuzhiyun * more interface IDs can be allocated.
158*4882a593Smuzhiyun */
usb_interface_id(struct usb_configuration * config,struct usb_function * function)159*4882a593Smuzhiyun int usb_interface_id(struct usb_configuration *config,
160*4882a593Smuzhiyun struct usb_function *function)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun unsigned char id = config->next_interface_id;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (id < MAX_CONFIG_INTERFACES) {
165*4882a593Smuzhiyun config->interface[id] = function;
166*4882a593Smuzhiyun config->next_interface_id = id + 1;
167*4882a593Smuzhiyun return id;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun return -ENODEV;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
config_buf(struct usb_configuration * config,enum usb_device_speed speed,void * buf,u8 type)172*4882a593Smuzhiyun static int config_buf(struct usb_configuration *config,
173*4882a593Smuzhiyun enum usb_device_speed speed, void *buf, u8 type)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
176*4882a593Smuzhiyun void *next = buf + USB_DT_CONFIG_SIZE;
177*4882a593Smuzhiyun struct usb_descriptor_header **descriptors;
178*4882a593Smuzhiyun struct usb_config_descriptor *c;
179*4882a593Smuzhiyun int status;
180*4882a593Smuzhiyun struct usb_function *f;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* write the config descriptor */
183*4882a593Smuzhiyun c = buf;
184*4882a593Smuzhiyun c->bLength = USB_DT_CONFIG_SIZE;
185*4882a593Smuzhiyun c->bDescriptorType = type;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun c->bNumInterfaces = config->next_interface_id;
188*4882a593Smuzhiyun c->bConfigurationValue = config->bConfigurationValue;
189*4882a593Smuzhiyun c->iConfiguration = config->iConfiguration;
190*4882a593Smuzhiyun c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
191*4882a593Smuzhiyun c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* There may be e.g. OTG descriptors */
194*4882a593Smuzhiyun if (config->descriptors) {
195*4882a593Smuzhiyun status = usb_descriptor_fillbuf(next, len,
196*4882a593Smuzhiyun config->descriptors);
197*4882a593Smuzhiyun if (status < 0)
198*4882a593Smuzhiyun return status;
199*4882a593Smuzhiyun len -= status;
200*4882a593Smuzhiyun next += status;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* add each function's descriptors */
204*4882a593Smuzhiyun list_for_each_entry(f, &config->functions, list) {
205*4882a593Smuzhiyun switch (speed) {
206*4882a593Smuzhiyun case USB_SPEED_SUPER:
207*4882a593Smuzhiyun descriptors = f->ss_descriptors;
208*4882a593Smuzhiyun break;
209*4882a593Smuzhiyun case USB_SPEED_HIGH:
210*4882a593Smuzhiyun descriptors = f->hs_descriptors;
211*4882a593Smuzhiyun break;
212*4882a593Smuzhiyun default:
213*4882a593Smuzhiyun descriptors = f->descriptors;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (!descriptors)
217*4882a593Smuzhiyun continue;
218*4882a593Smuzhiyun status = usb_descriptor_fillbuf(next, len,
219*4882a593Smuzhiyun (const struct usb_descriptor_header **) descriptors);
220*4882a593Smuzhiyun if (status < 0)
221*4882a593Smuzhiyun return status;
222*4882a593Smuzhiyun len -= status;
223*4882a593Smuzhiyun next += status;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun len = next - buf;
227*4882a593Smuzhiyun c->wTotalLength = cpu_to_le16(len);
228*4882a593Smuzhiyun return len;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
config_desc(struct usb_composite_dev * cdev,unsigned w_value)231*4882a593Smuzhiyun static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun enum usb_device_speed speed = USB_SPEED_UNKNOWN;
234*4882a593Smuzhiyun struct usb_gadget *gadget = cdev->gadget;
235*4882a593Smuzhiyun u8 type = w_value >> 8;
236*4882a593Smuzhiyun int hs = 0;
237*4882a593Smuzhiyun struct usb_configuration *c;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (gadget->speed == USB_SPEED_SUPER)
240*4882a593Smuzhiyun speed = gadget->speed;
241*4882a593Smuzhiyun else if (gadget_is_dualspeed(gadget)) {
242*4882a593Smuzhiyun if (gadget->speed == USB_SPEED_HIGH)
243*4882a593Smuzhiyun hs = 1;
244*4882a593Smuzhiyun if (type == USB_DT_OTHER_SPEED_CONFIG)
245*4882a593Smuzhiyun hs = !hs;
246*4882a593Smuzhiyun if (hs)
247*4882a593Smuzhiyun speed = USB_SPEED_HIGH;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun w_value &= 0xff;
251*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
252*4882a593Smuzhiyun switch (speed) {
253*4882a593Smuzhiyun case USB_SPEED_SUPER:
254*4882a593Smuzhiyun if (!c->superspeed)
255*4882a593Smuzhiyun continue;
256*4882a593Smuzhiyun break;
257*4882a593Smuzhiyun case USB_SPEED_HIGH:
258*4882a593Smuzhiyun if (!c->highspeed)
259*4882a593Smuzhiyun continue;
260*4882a593Smuzhiyun break;
261*4882a593Smuzhiyun default:
262*4882a593Smuzhiyun if (!c->fullspeed)
263*4882a593Smuzhiyun continue;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (w_value == 0)
267*4882a593Smuzhiyun return config_buf(c, speed, cdev->req->buf, type);
268*4882a593Smuzhiyun w_value--;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun return -EINVAL;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
count_configs(struct usb_composite_dev * cdev,unsigned type)273*4882a593Smuzhiyun static int count_configs(struct usb_composite_dev *cdev, unsigned type)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun struct usb_gadget *gadget = cdev->gadget;
276*4882a593Smuzhiyun unsigned count = 0;
277*4882a593Smuzhiyun int hs = 0;
278*4882a593Smuzhiyun struct usb_configuration *c;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (gadget_is_dualspeed(gadget)) {
281*4882a593Smuzhiyun if (gadget->speed == USB_SPEED_HIGH)
282*4882a593Smuzhiyun hs = 1;
283*4882a593Smuzhiyun if (type == USB_DT_DEVICE_QUALIFIER)
284*4882a593Smuzhiyun hs = !hs;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
287*4882a593Smuzhiyun /* ignore configs that won't work at this speed */
288*4882a593Smuzhiyun if (hs) {
289*4882a593Smuzhiyun if (!c->highspeed)
290*4882a593Smuzhiyun continue;
291*4882a593Smuzhiyun } else {
292*4882a593Smuzhiyun if (!c->fullspeed)
293*4882a593Smuzhiyun continue;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun count++;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun return count;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
device_qual(struct usb_composite_dev * cdev)300*4882a593Smuzhiyun static void device_qual(struct usb_composite_dev *cdev)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct usb_qualifier_descriptor *qual = cdev->req->buf;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun qual->bLength = sizeof(*qual);
305*4882a593Smuzhiyun qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
306*4882a593Smuzhiyun /* POLICY: same bcdUSB and device type info at both speeds */
307*4882a593Smuzhiyun qual->bcdUSB = cdev->desc.bcdUSB;
308*4882a593Smuzhiyun qual->bDeviceClass = cdev->desc.bDeviceClass;
309*4882a593Smuzhiyun qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
310*4882a593Smuzhiyun qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
311*4882a593Smuzhiyun /* ASSUME same EP0 fifo size at both speeds */
312*4882a593Smuzhiyun qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
313*4882a593Smuzhiyun qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
314*4882a593Smuzhiyun qual->bRESERVED = 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
reset_config(struct usb_composite_dev * cdev)317*4882a593Smuzhiyun static void reset_config(struct usb_composite_dev *cdev)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun struct usb_function *f;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun debug("%s:\n", __func__);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun list_for_each_entry(f, &cdev->config->functions, list) {
324*4882a593Smuzhiyun if (f->disable)
325*4882a593Smuzhiyun f->disable(f);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun bitmap_zero(f->endpoints, 32);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun cdev->config = NULL;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
set_config(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl,unsigned number)332*4882a593Smuzhiyun static int set_config(struct usb_composite_dev *cdev,
333*4882a593Smuzhiyun const struct usb_ctrlrequest *ctrl, unsigned number)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun struct usb_gadget *gadget = cdev->gadget;
336*4882a593Smuzhiyun unsigned power = gadget_is_otg(gadget) ? 8 : 100;
337*4882a593Smuzhiyun struct usb_descriptor_header **descriptors;
338*4882a593Smuzhiyun int result = -EINVAL;
339*4882a593Smuzhiyun struct usb_endpoint_descriptor *ep;
340*4882a593Smuzhiyun struct usb_configuration *c = NULL;
341*4882a593Smuzhiyun int addr;
342*4882a593Smuzhiyun int tmp;
343*4882a593Smuzhiyun struct usb_function *f;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if (cdev->config)
346*4882a593Smuzhiyun reset_config(cdev);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (number) {
349*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
350*4882a593Smuzhiyun if (c->bConfigurationValue == number) {
351*4882a593Smuzhiyun result = 0;
352*4882a593Smuzhiyun break;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun if (result < 0)
356*4882a593Smuzhiyun goto done;
357*4882a593Smuzhiyun } else
358*4882a593Smuzhiyun result = 0;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun debug("%s: %s speed config #%d: %s\n", __func__,
361*4882a593Smuzhiyun ({ char *speed;
362*4882a593Smuzhiyun switch (gadget->speed) {
363*4882a593Smuzhiyun case USB_SPEED_LOW:
364*4882a593Smuzhiyun speed = "low";
365*4882a593Smuzhiyun break;
366*4882a593Smuzhiyun case USB_SPEED_FULL:
367*4882a593Smuzhiyun speed = "full";
368*4882a593Smuzhiyun break;
369*4882a593Smuzhiyun case USB_SPEED_HIGH:
370*4882a593Smuzhiyun speed = "high";
371*4882a593Smuzhiyun break;
372*4882a593Smuzhiyun case USB_SPEED_SUPER:
373*4882a593Smuzhiyun speed = "super";
374*4882a593Smuzhiyun break;
375*4882a593Smuzhiyun default:
376*4882a593Smuzhiyun speed = "?";
377*4882a593Smuzhiyun break;
378*4882a593Smuzhiyun };
379*4882a593Smuzhiyun speed;
380*4882a593Smuzhiyun }), number, c ? c->label : "unconfigured");
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun if (!c)
383*4882a593Smuzhiyun goto done;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun cdev->config = c;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /* Initialize all interfaces by setting them to altsetting zero. */
388*4882a593Smuzhiyun for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
389*4882a593Smuzhiyun f = c->interface[tmp];
390*4882a593Smuzhiyun if (!f)
391*4882a593Smuzhiyun break;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun * Record which endpoints are used by the function. This is used
395*4882a593Smuzhiyun * to dispatch control requests targeted at that endpoint to the
396*4882a593Smuzhiyun * function's setup callback instead of the current
397*4882a593Smuzhiyun * configuration's setup callback.
398*4882a593Smuzhiyun */
399*4882a593Smuzhiyun switch (gadget->speed) {
400*4882a593Smuzhiyun case USB_SPEED_SUPER:
401*4882a593Smuzhiyun descriptors = f->ss_descriptors;
402*4882a593Smuzhiyun break;
403*4882a593Smuzhiyun case USB_SPEED_HIGH:
404*4882a593Smuzhiyun descriptors = f->hs_descriptors;
405*4882a593Smuzhiyun break;
406*4882a593Smuzhiyun default:
407*4882a593Smuzhiyun descriptors = f->descriptors;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun for (; *descriptors; ++descriptors) {
411*4882a593Smuzhiyun if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
412*4882a593Smuzhiyun continue;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun ep = (struct usb_endpoint_descriptor *)*descriptors;
415*4882a593Smuzhiyun addr = ((ep->bEndpointAddress & 0x80) >> 3)
416*4882a593Smuzhiyun | (ep->bEndpointAddress & 0x0f);
417*4882a593Smuzhiyun generic_set_bit(addr, f->endpoints);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun result = f->set_alt(f, tmp, 0);
421*4882a593Smuzhiyun if (result < 0) {
422*4882a593Smuzhiyun debug("interface %d (%s/%p) alt 0 --> %d\n",
423*4882a593Smuzhiyun tmp, f->name, f, result);
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun reset_config(cdev);
426*4882a593Smuzhiyun goto done;
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* when we return, be sure our power usage is valid */
431*4882a593Smuzhiyun power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
432*4882a593Smuzhiyun done:
433*4882a593Smuzhiyun usb_gadget_vbus_draw(gadget, power);
434*4882a593Smuzhiyun return result;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /**
438*4882a593Smuzhiyun * usb_add_config() - add a configuration to a device.
439*4882a593Smuzhiyun * @cdev: wraps the USB gadget
440*4882a593Smuzhiyun * @config: the configuration, with bConfigurationValue assigned
441*4882a593Smuzhiyun * Context: single threaded during gadget setup
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * One of the main tasks of a composite driver's bind() routine is to
444*4882a593Smuzhiyun * add each of the configurations it supports, using this routine.
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * This function returns the value of the configuration's bind(), which
447*4882a593Smuzhiyun * is zero for success else a negative errno value. Binding configurations
448*4882a593Smuzhiyun * assigns global resources including string IDs, and per-configuration
449*4882a593Smuzhiyun * resources such as interface IDs and endpoints.
450*4882a593Smuzhiyun */
usb_add_config(struct usb_composite_dev * cdev,struct usb_configuration * config)451*4882a593Smuzhiyun int usb_add_config(struct usb_composite_dev *cdev,
452*4882a593Smuzhiyun struct usb_configuration *config)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun int status = -EINVAL;
455*4882a593Smuzhiyun struct usb_configuration *c;
456*4882a593Smuzhiyun struct usb_function *f;
457*4882a593Smuzhiyun unsigned int i;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun debug("%s: adding config #%u '%s'/%p\n", __func__,
460*4882a593Smuzhiyun config->bConfigurationValue,
461*4882a593Smuzhiyun config->label, config);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (!config->bConfigurationValue || !config->bind)
464*4882a593Smuzhiyun goto done;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /* Prevent duplicate configuration identifiers */
467*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
468*4882a593Smuzhiyun if (c->bConfigurationValue == config->bConfigurationValue) {
469*4882a593Smuzhiyun status = -EBUSY;
470*4882a593Smuzhiyun goto done;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun config->cdev = cdev;
475*4882a593Smuzhiyun list_add_tail(&config->list, &cdev->configs);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun INIT_LIST_HEAD(&config->functions);
478*4882a593Smuzhiyun config->next_interface_id = 0;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun status = config->bind(config);
481*4882a593Smuzhiyun if (status < 0) {
482*4882a593Smuzhiyun list_del(&config->list);
483*4882a593Smuzhiyun config->cdev = NULL;
484*4882a593Smuzhiyun } else {
485*4882a593Smuzhiyun debug("cfg %d/%p speeds:%s%s%s\n",
486*4882a593Smuzhiyun config->bConfigurationValue, config,
487*4882a593Smuzhiyun config->superspeed ? " super" : "",
488*4882a593Smuzhiyun config->highspeed ? " high" : "",
489*4882a593Smuzhiyun config->fullspeed ?
490*4882a593Smuzhiyun (gadget_is_dualspeed(cdev->gadget) ?
491*4882a593Smuzhiyun " full" : " full/low") : "");
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
494*4882a593Smuzhiyun f = config->interface[i];
495*4882a593Smuzhiyun if (!f)
496*4882a593Smuzhiyun continue;
497*4882a593Smuzhiyun debug("%s: interface %d = %s/%p\n",
498*4882a593Smuzhiyun __func__, i, f->name, f);
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun usb_ep_autoconfig_reset(cdev->gadget);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun done:
505*4882a593Smuzhiyun if (status)
506*4882a593Smuzhiyun debug("added config '%s'/%u --> %d\n", config->label,
507*4882a593Smuzhiyun config->bConfigurationValue, status);
508*4882a593Smuzhiyun return status;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /*
512*4882a593Smuzhiyun * We support strings in multiple languages ... string descriptor zero
513*4882a593Smuzhiyun * says which languages are supported. The typical case will be that
514*4882a593Smuzhiyun * only one language (probably English) is used, with I18N handled on
515*4882a593Smuzhiyun * the host side.
516*4882a593Smuzhiyun */
517*4882a593Smuzhiyun
collect_langs(struct usb_gadget_strings ** sp,void * buf)518*4882a593Smuzhiyun static void collect_langs(struct usb_gadget_strings **sp, void *buf)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun const struct usb_gadget_strings *s;
521*4882a593Smuzhiyun u16 language;
522*4882a593Smuzhiyun __le16_packed *tmp;
523*4882a593Smuzhiyun __le16_packed *end = (buf + 252);
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun while (*sp) {
526*4882a593Smuzhiyun s = *sp;
527*4882a593Smuzhiyun language = cpu_to_le16(s->language);
528*4882a593Smuzhiyun for (tmp = buf; tmp->val && tmp < end; tmp++) {
529*4882a593Smuzhiyun if (tmp->val == language)
530*4882a593Smuzhiyun goto repeat;
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun tmp->val = language;
533*4882a593Smuzhiyun repeat:
534*4882a593Smuzhiyun sp++;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
lookup_string(struct usb_gadget_strings ** sp,void * buf,u16 language,int id)538*4882a593Smuzhiyun static int lookup_string(
539*4882a593Smuzhiyun struct usb_gadget_strings **sp,
540*4882a593Smuzhiyun void *buf,
541*4882a593Smuzhiyun u16 language,
542*4882a593Smuzhiyun int id
543*4882a593Smuzhiyun )
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun int value;
546*4882a593Smuzhiyun struct usb_gadget_strings *s;
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun while (*sp) {
549*4882a593Smuzhiyun s = *sp++;
550*4882a593Smuzhiyun if (s->language != language)
551*4882a593Smuzhiyun continue;
552*4882a593Smuzhiyun value = usb_gadget_get_string(s, id, buf);
553*4882a593Smuzhiyun if (value > 0)
554*4882a593Smuzhiyun return value;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun return -EINVAL;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
get_string(struct usb_composite_dev * cdev,void * buf,u16 language,int id)559*4882a593Smuzhiyun static int get_string(struct usb_composite_dev *cdev,
560*4882a593Smuzhiyun void *buf, u16 language, int id)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun struct usb_string_descriptor *s = buf;
563*4882a593Smuzhiyun struct usb_gadget_strings **sp;
564*4882a593Smuzhiyun int len;
565*4882a593Smuzhiyun struct usb_configuration *c;
566*4882a593Smuzhiyun struct usb_function *f;
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun * Yes, not only is USB's I18N support probably more than most
570*4882a593Smuzhiyun * folk will ever care about ... also, it's all supported here.
571*4882a593Smuzhiyun * (Except for UTF8 support for Unicode's "Astral Planes".)
572*4882a593Smuzhiyun */
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun /* 0 == report all available language codes */
575*4882a593Smuzhiyun if (id == 0) {
576*4882a593Smuzhiyun memset(s, 0, 256);
577*4882a593Smuzhiyun s->bDescriptorType = USB_DT_STRING;
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun sp = composite->strings;
580*4882a593Smuzhiyun if (sp)
581*4882a593Smuzhiyun collect_langs(sp, s->wData);
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
584*4882a593Smuzhiyun sp = c->strings;
585*4882a593Smuzhiyun if (sp)
586*4882a593Smuzhiyun collect_langs(sp, s->wData);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun list_for_each_entry(f, &c->functions, list) {
589*4882a593Smuzhiyun sp = f->strings;
590*4882a593Smuzhiyun if (sp)
591*4882a593Smuzhiyun collect_langs(sp, s->wData);
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun for (len = 0; len <= 126 && s->wData[len]; len++)
596*4882a593Smuzhiyun continue;
597*4882a593Smuzhiyun if (!len)
598*4882a593Smuzhiyun return -EINVAL;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun s->bLength = 2 * (len + 1);
601*4882a593Smuzhiyun return s->bLength;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /*
605*4882a593Smuzhiyun * Otherwise, look up and return a specified string. String IDs
606*4882a593Smuzhiyun * are device-scoped, so we look up each string table we're told
607*4882a593Smuzhiyun * about. These lookups are infrequent; simpler-is-better here.
608*4882a593Smuzhiyun */
609*4882a593Smuzhiyun if (composite->strings) {
610*4882a593Smuzhiyun len = lookup_string(composite->strings, buf, language, id);
611*4882a593Smuzhiyun if (len > 0)
612*4882a593Smuzhiyun return len;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun list_for_each_entry(c, &cdev->configs, list) {
615*4882a593Smuzhiyun if (c->strings) {
616*4882a593Smuzhiyun len = lookup_string(c->strings, buf, language, id);
617*4882a593Smuzhiyun if (len > 0)
618*4882a593Smuzhiyun return len;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun list_for_each_entry(f, &c->functions, list) {
621*4882a593Smuzhiyun if (!f->strings)
622*4882a593Smuzhiyun continue;
623*4882a593Smuzhiyun len = lookup_string(f->strings, buf, language, id);
624*4882a593Smuzhiyun if (len > 0)
625*4882a593Smuzhiyun return len;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun return -EINVAL;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /**
632*4882a593Smuzhiyun * usb_string_id() - allocate an unused string ID
633*4882a593Smuzhiyun * @cdev: the device whose string descriptor IDs are being allocated
634*4882a593Smuzhiyun * Context: single threaded during gadget setup
635*4882a593Smuzhiyun *
636*4882a593Smuzhiyun * @usb_string_id() is called from bind() callbacks to allocate
637*4882a593Smuzhiyun * string IDs. Drivers for functions, configurations, or gadgets will
638*4882a593Smuzhiyun * then store that ID in the appropriate descriptors and string table.
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * All string identifier should be allocated using this,
641*4882a593Smuzhiyun * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
642*4882a593Smuzhiyun * that for example different functions don't wrongly assign different
643*4882a593Smuzhiyun * meanings to the same identifier.
644*4882a593Smuzhiyun */
usb_string_id(struct usb_composite_dev * cdev)645*4882a593Smuzhiyun int usb_string_id(struct usb_composite_dev *cdev)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun if (cdev->next_string_id < 254) {
648*4882a593Smuzhiyun /*
649*4882a593Smuzhiyun * string id 0 is reserved by USB spec for list of
650*4882a593Smuzhiyun * supported languages
651*4882a593Smuzhiyun * 255 reserved as well? -- mina86
652*4882a593Smuzhiyun */
653*4882a593Smuzhiyun cdev->next_string_id++;
654*4882a593Smuzhiyun return cdev->next_string_id;
655*4882a593Smuzhiyun }
656*4882a593Smuzhiyun return -ENODEV;
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /**
660*4882a593Smuzhiyun * usb_string_ids() - allocate unused string IDs in batch
661*4882a593Smuzhiyun * @cdev: the device whose string descriptor IDs are being allocated
662*4882a593Smuzhiyun * @str: an array of usb_string objects to assign numbers to
663*4882a593Smuzhiyun * Context: single threaded during gadget setup
664*4882a593Smuzhiyun *
665*4882a593Smuzhiyun * @usb_string_ids() is called from bind() callbacks to allocate
666*4882a593Smuzhiyun * string IDs. Drivers for functions, configurations, or gadgets will
667*4882a593Smuzhiyun * then copy IDs from the string table to the appropriate descriptors
668*4882a593Smuzhiyun * and string table for other languages.
669*4882a593Smuzhiyun *
670*4882a593Smuzhiyun * All string identifier should be allocated using this,
671*4882a593Smuzhiyun * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
672*4882a593Smuzhiyun * example different functions don't wrongly assign different meanings
673*4882a593Smuzhiyun * to the same identifier.
674*4882a593Smuzhiyun */
usb_string_ids_tab(struct usb_composite_dev * cdev,struct usb_string * str)675*4882a593Smuzhiyun int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun u8 next = cdev->next_string_id;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun for (; str->s; ++str) {
680*4882a593Smuzhiyun if (next >= 254)
681*4882a593Smuzhiyun return -ENODEV;
682*4882a593Smuzhiyun str->id = ++next;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun cdev->next_string_id = next;
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun return 0;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /**
691*4882a593Smuzhiyun * usb_string_ids_n() - allocate unused string IDs in batch
692*4882a593Smuzhiyun * @c: the device whose string descriptor IDs are being allocated
693*4882a593Smuzhiyun * @n: number of string IDs to allocate
694*4882a593Smuzhiyun * Context: single threaded during gadget setup
695*4882a593Smuzhiyun *
696*4882a593Smuzhiyun * Returns the first requested ID. This ID and next @n-1 IDs are now
697*4882a593Smuzhiyun * valid IDs. At least provided that @n is non-zero because if it
698*4882a593Smuzhiyun * is, returns last requested ID which is now very useful information.
699*4882a593Smuzhiyun *
700*4882a593Smuzhiyun * @usb_string_ids_n() is called from bind() callbacks to allocate
701*4882a593Smuzhiyun * string IDs. Drivers for functions, configurations, or gadgets will
702*4882a593Smuzhiyun * then store that ID in the appropriate descriptors and string table.
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * All string identifier should be allocated using this,
705*4882a593Smuzhiyun * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
706*4882a593Smuzhiyun * example different functions don't wrongly assign different meanings
707*4882a593Smuzhiyun * to the same identifier.
708*4882a593Smuzhiyun */
usb_string_ids_n(struct usb_composite_dev * c,unsigned n)709*4882a593Smuzhiyun int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun u8 next = c->next_string_id;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun if (n > 254 || next + n > 254)
714*4882a593Smuzhiyun return -ENODEV;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun c->next_string_id += n;
717*4882a593Smuzhiyun return next + 1;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
composite_setup_complete(struct usb_ep * ep,struct usb_request * req)720*4882a593Smuzhiyun static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun if (req->status || req->actual != req->length)
723*4882a593Smuzhiyun debug("%s: setup complete --> %d, %d/%d\n", __func__,
724*4882a593Smuzhiyun req->status, req->actual, req->length);
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun
bos_desc(struct usb_composite_dev * cdev)727*4882a593Smuzhiyun static int bos_desc(struct usb_composite_dev *cdev)
728*4882a593Smuzhiyun {
729*4882a593Smuzhiyun struct usb_dev_cap_header *cap;
730*4882a593Smuzhiyun struct usb_ext_cap_descriptor *usb_ext;
731*4882a593Smuzhiyun struct usb_ss_cap_descriptor *ss_cap;
732*4882a593Smuzhiyun struct usb_bos_descriptor *bos = cdev->req->buf;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun bos->bLength = USB_DT_BOS_SIZE;
735*4882a593Smuzhiyun bos->bDescriptorType = USB_DT_BOS;
736*4882a593Smuzhiyun bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
737*4882a593Smuzhiyun bos->bNumDeviceCaps = 0;
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun if (cdev->gadget->speed < USB_SPEED_SUPER) {
740*4882a593Smuzhiyun /* For rockusb with bcdUSB (0x0201) */
741*4882a593Smuzhiyun cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
742*4882a593Smuzhiyun bos->bNumDeviceCaps++;
743*4882a593Smuzhiyun bos->wTotalLength = cpu_to_le16(bos->wTotalLength +
744*4882a593Smuzhiyun sizeof(*cap));
745*4882a593Smuzhiyun cap->bLength = sizeof(*cap);
746*4882a593Smuzhiyun cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
747*4882a593Smuzhiyun cap->bDevCapabilityType = 0;
748*4882a593Smuzhiyun } else {
749*4882a593Smuzhiyun /*
750*4882a593Smuzhiyun * A SuperSpeed device shall include the USB2.0
751*4882a593Smuzhiyun * extension descriptor and shall support LPM when
752*4882a593Smuzhiyun * operating in USB2.0 HS mode.
753*4882a593Smuzhiyun */
754*4882a593Smuzhiyun usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
755*4882a593Smuzhiyun bos->bNumDeviceCaps++;
756*4882a593Smuzhiyun le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
757*4882a593Smuzhiyun USB_DT_USB_EXT_CAP_SIZE);
758*4882a593Smuzhiyun usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
759*4882a593Smuzhiyun usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
760*4882a593Smuzhiyun usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
761*4882a593Smuzhiyun usb_ext->bmAttributes = USB_LPM_SUPPORT;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /*
764*4882a593Smuzhiyun * The Superspeed USB Capability descriptor shall be
765*4882a593Smuzhiyun * implemented by all SuperSpeed devices.
766*4882a593Smuzhiyun */
767*4882a593Smuzhiyun ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
768*4882a593Smuzhiyun bos->bNumDeviceCaps++;
769*4882a593Smuzhiyun le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
770*4882a593Smuzhiyun USB_DT_USB_SS_CAP_SIZE);
771*4882a593Smuzhiyun ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
772*4882a593Smuzhiyun ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
773*4882a593Smuzhiyun ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
774*4882a593Smuzhiyun ss_cap->bmAttributes = 0; /* LTM is not supported yet */
775*4882a593Smuzhiyun ss_cap->wSpeedSupported = cpu_to_le16(USB_FULL_SPEED_OPERATION |
776*4882a593Smuzhiyun USB_HIGH_SPEED_OPERATION |
777*4882a593Smuzhiyun USB_5GBPS_OPERATION);
778*4882a593Smuzhiyun ss_cap->bFunctionalitySupport = USB_FULL_SPEED_OPERATION;
779*4882a593Smuzhiyun ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
780*4882a593Smuzhiyun ss_cap->bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun return le16_to_cpu(bos->wTotalLength);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun /*
787*4882a593Smuzhiyun * The setup() callback implements all the ep0 functionality that's
788*4882a593Smuzhiyun * not handled lower down, in hardware or the hardware driver(like
789*4882a593Smuzhiyun * device and endpoint feature flags, and their status). It's all
790*4882a593Smuzhiyun * housekeeping for the gadget function we're implementing. Most of
791*4882a593Smuzhiyun * the work is in config and function specific setup.
792*4882a593Smuzhiyun */
793*4882a593Smuzhiyun static int
composite_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)794*4882a593Smuzhiyun composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun u16 w_length = le16_to_cpu(ctrl->wLength);
797*4882a593Smuzhiyun u16 w_index = le16_to_cpu(ctrl->wIndex);
798*4882a593Smuzhiyun u16 w_value = le16_to_cpu(ctrl->wValue);
799*4882a593Smuzhiyun struct usb_composite_dev *cdev = get_gadget_data(gadget);
800*4882a593Smuzhiyun u8 intf = w_index & 0xFF;
801*4882a593Smuzhiyun int value = -EOPNOTSUPP;
802*4882a593Smuzhiyun struct usb_request *req = cdev->req;
803*4882a593Smuzhiyun struct usb_function *f = NULL;
804*4882a593Smuzhiyun int standard;
805*4882a593Smuzhiyun u8 endp;
806*4882a593Smuzhiyun struct usb_configuration *c;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun /*
809*4882a593Smuzhiyun * partial re-init of the response message; the function or the
810*4882a593Smuzhiyun * gadget might need to intercept e.g. a control-OUT completion
811*4882a593Smuzhiyun * when we delegate to it.
812*4882a593Smuzhiyun */
813*4882a593Smuzhiyun req->zero = 0;
814*4882a593Smuzhiyun req->complete = composite_setup_complete;
815*4882a593Smuzhiyun req->length = USB_BUFSIZ;
816*4882a593Smuzhiyun gadget->ep0->driver_data = cdev;
817*4882a593Smuzhiyun standard = (ctrl->bRequestType & USB_TYPE_MASK)
818*4882a593Smuzhiyun == USB_TYPE_STANDARD;
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun if (!standard)
821*4882a593Smuzhiyun goto unknown;
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun switch (ctrl->bRequest) {
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /* we handle all standard USB descriptors */
826*4882a593Smuzhiyun case USB_REQ_GET_DESCRIPTOR:
827*4882a593Smuzhiyun if (ctrl->bRequestType != USB_DIR_IN)
828*4882a593Smuzhiyun goto unknown;
829*4882a593Smuzhiyun switch (w_value >> 8) {
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun case USB_DT_DEVICE:
832*4882a593Smuzhiyun cdev->desc.bNumConfigurations =
833*4882a593Smuzhiyun count_configs(cdev, USB_DT_DEVICE);
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun if (gadget_is_superspeed(gadget) &&
836*4882a593Smuzhiyun gadget->speed >= USB_SPEED_SUPER) {
837*4882a593Smuzhiyun /*
838*4882a593Smuzhiyun * bcdUSB should be 0x0300 for superspeed,
839*4882a593Smuzhiyun * but we change it to 0x0301 for rockusb.
840*4882a593Smuzhiyun */
841*4882a593Smuzhiyun if (!strncmp(cdev->driver->name,
842*4882a593Smuzhiyun "rkusb_ums_dnl", 13))
843*4882a593Smuzhiyun cdev->desc.bcdUSB = cpu_to_le16(0x0301);
844*4882a593Smuzhiyun else
845*4882a593Smuzhiyun cdev->desc.bcdUSB = cpu_to_le16(0x0300);
846*4882a593Smuzhiyun cdev->desc.bMaxPacketSize0 = 9;
847*4882a593Smuzhiyun } else {
848*4882a593Smuzhiyun cdev->desc.bMaxPacketSize0 =
849*4882a593Smuzhiyun cdev->gadget->ep0->maxpacket;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun value = min(w_length, (u16) sizeof cdev->desc);
853*4882a593Smuzhiyun memcpy(req->buf, &cdev->desc, value);
854*4882a593Smuzhiyun break;
855*4882a593Smuzhiyun case USB_DT_DEVICE_QUALIFIER:
856*4882a593Smuzhiyun if (!gadget_is_dualspeed(gadget))
857*4882a593Smuzhiyun break;
858*4882a593Smuzhiyun device_qual(cdev);
859*4882a593Smuzhiyun value = min_t(int, w_length,
860*4882a593Smuzhiyun sizeof(struct usb_qualifier_descriptor));
861*4882a593Smuzhiyun break;
862*4882a593Smuzhiyun case USB_DT_OTHER_SPEED_CONFIG:
863*4882a593Smuzhiyun if (!gadget_is_dualspeed(gadget))
864*4882a593Smuzhiyun break;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun case USB_DT_CONFIG:
867*4882a593Smuzhiyun value = config_desc(cdev, w_value);
868*4882a593Smuzhiyun if (value >= 0)
869*4882a593Smuzhiyun value = min(w_length, (u16) value);
870*4882a593Smuzhiyun break;
871*4882a593Smuzhiyun case USB_DT_STRING:
872*4882a593Smuzhiyun value = get_string(cdev, req->buf,
873*4882a593Smuzhiyun w_index, w_value & 0xff);
874*4882a593Smuzhiyun if (value >= 0)
875*4882a593Smuzhiyun value = min(w_length, (u16) value);
876*4882a593Smuzhiyun break;
877*4882a593Smuzhiyun case USB_DT_BOS:
878*4882a593Smuzhiyun /* HACK: only for rockusb command.
879*4882a593Smuzhiyun * Rockchip upgrade tool use bcdUSB (0x0201) field
880*4882a593Smuzhiyun * distinguishing maskrom or loader device at present.
881*4882a593Smuzhiyun * Unfortunately, it conflict with Windows 8 and beyond
882*4882a593Smuzhiyun * which request BOS descriptor in this case that bcdUSB
883*4882a593Smuzhiyun * is set to 0x0201.
884*4882a593Smuzhiyun */
885*4882a593Smuzhiyun if (gadget_is_superspeed(gadget) ||
886*4882a593Smuzhiyun !strncmp(cdev->driver->name, "rkusb_ums_dnl", 13)) {
887*4882a593Smuzhiyun value = bos_desc(cdev);
888*4882a593Smuzhiyun value = min(w_length, (u16) value);
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun /*
892*4882a593Smuzhiyun * The USB compliance test (USB 2.0 Command Verifier)
893*4882a593Smuzhiyun * issues this request. We should not run into the
894*4882a593Smuzhiyun * default path here. But return for now until
895*4882a593Smuzhiyun * the superspeed support is added.
896*4882a593Smuzhiyun */
897*4882a593Smuzhiyun break;
898*4882a593Smuzhiyun default:
899*4882a593Smuzhiyun goto unknown;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun break;
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun /* any number of configs can work */
904*4882a593Smuzhiyun case USB_REQ_SET_CONFIGURATION:
905*4882a593Smuzhiyun if (ctrl->bRequestType != 0)
906*4882a593Smuzhiyun goto unknown;
907*4882a593Smuzhiyun if (gadget_is_otg(gadget)) {
908*4882a593Smuzhiyun if (gadget->a_hnp_support)
909*4882a593Smuzhiyun debug("HNP available\n");
910*4882a593Smuzhiyun else if (gadget->a_alt_hnp_support)
911*4882a593Smuzhiyun debug("HNP on another port\n");
912*4882a593Smuzhiyun else
913*4882a593Smuzhiyun debug("HNP inactive\n");
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun value = set_config(cdev, ctrl, w_value);
917*4882a593Smuzhiyun break;
918*4882a593Smuzhiyun case USB_REQ_GET_CONFIGURATION:
919*4882a593Smuzhiyun if (ctrl->bRequestType != USB_DIR_IN)
920*4882a593Smuzhiyun goto unknown;
921*4882a593Smuzhiyun if (cdev->config)
922*4882a593Smuzhiyun *(u8 *)req->buf = cdev->config->bConfigurationValue;
923*4882a593Smuzhiyun else
924*4882a593Smuzhiyun *(u8 *)req->buf = 0;
925*4882a593Smuzhiyun value = min(w_length, (u16) 1);
926*4882a593Smuzhiyun break;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /*
929*4882a593Smuzhiyun * function drivers must handle get/set altsetting; if there's
930*4882a593Smuzhiyun * no get() method, we know only altsetting zero works.
931*4882a593Smuzhiyun */
932*4882a593Smuzhiyun case USB_REQ_SET_INTERFACE:
933*4882a593Smuzhiyun if (ctrl->bRequestType != USB_RECIP_INTERFACE)
934*4882a593Smuzhiyun goto unknown;
935*4882a593Smuzhiyun if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
936*4882a593Smuzhiyun break;
937*4882a593Smuzhiyun f = cdev->config->interface[intf];
938*4882a593Smuzhiyun if (!f)
939*4882a593Smuzhiyun break;
940*4882a593Smuzhiyun if (w_value && !f->set_alt)
941*4882a593Smuzhiyun break;
942*4882a593Smuzhiyun value = f->set_alt(f, w_index, w_value);
943*4882a593Smuzhiyun break;
944*4882a593Smuzhiyun case USB_REQ_GET_INTERFACE:
945*4882a593Smuzhiyun if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
946*4882a593Smuzhiyun goto unknown;
947*4882a593Smuzhiyun if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
948*4882a593Smuzhiyun break;
949*4882a593Smuzhiyun f = cdev->config->interface[intf];
950*4882a593Smuzhiyun if (!f)
951*4882a593Smuzhiyun break;
952*4882a593Smuzhiyun /* lots of interfaces only need altsetting zero... */
953*4882a593Smuzhiyun value = f->get_alt ? f->get_alt(f, w_index) : 0;
954*4882a593Smuzhiyun if (value < 0)
955*4882a593Smuzhiyun break;
956*4882a593Smuzhiyun *((u8 *)req->buf) = value;
957*4882a593Smuzhiyun value = min(w_length, (u16) 1);
958*4882a593Smuzhiyun break;
959*4882a593Smuzhiyun default:
960*4882a593Smuzhiyun unknown:
961*4882a593Smuzhiyun debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
962*4882a593Smuzhiyun ctrl->bRequestType, ctrl->bRequest,
963*4882a593Smuzhiyun w_value, w_index, w_length);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun if (!cdev->config)
966*4882a593Smuzhiyun goto done;
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun /*
969*4882a593Smuzhiyun * functions always handle their interfaces and endpoints...
970*4882a593Smuzhiyun * punt other recipients (other, WUSB, ...) to the current
971*4882a593Smuzhiyun * configuration code.
972*4882a593Smuzhiyun */
973*4882a593Smuzhiyun switch (ctrl->bRequestType & USB_RECIP_MASK) {
974*4882a593Smuzhiyun case USB_RECIP_INTERFACE:
975*4882a593Smuzhiyun if (!cdev->config)
976*4882a593Smuzhiyun break;
977*4882a593Smuzhiyun f = cdev->config->interface[intf];
978*4882a593Smuzhiyun break;
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun case USB_RECIP_ENDPOINT:
981*4882a593Smuzhiyun if (!cdev->config)
982*4882a593Smuzhiyun break;
983*4882a593Smuzhiyun endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
984*4882a593Smuzhiyun list_for_each_entry(f, &cdev->config->functions, list) {
985*4882a593Smuzhiyun if (test_bit(endp, f->endpoints))
986*4882a593Smuzhiyun break;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun if (&f->list == &cdev->config->functions)
989*4882a593Smuzhiyun f = NULL;
990*4882a593Smuzhiyun break;
991*4882a593Smuzhiyun /*
992*4882a593Smuzhiyun * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
993*4882a593Smuzhiyun * for non-standard request (w_value = 0x21,
994*4882a593Smuzhiyun * bRequest = GET_DESCRIPTOR in this case).
995*4882a593Smuzhiyun * When only one interface is registered (as it is done now),
996*4882a593Smuzhiyun * then this request shall be handled as it was requested for
997*4882a593Smuzhiyun * interface.
998*4882a593Smuzhiyun *
999*4882a593Smuzhiyun * In the below code it is checked if only one interface is
1000*4882a593Smuzhiyun * present and proper function for it is extracted. Due to that
1001*4882a593Smuzhiyun * function's setup (f->setup) is called to handle this
1002*4882a593Smuzhiyun * special non-standard request.
1003*4882a593Smuzhiyun */
1004*4882a593Smuzhiyun case USB_RECIP_DEVICE:
1005*4882a593Smuzhiyun if (cdev->config) {
1006*4882a593Smuzhiyun debug("cdev->config->next_interface_id: %d intf: %d\n",
1007*4882a593Smuzhiyun cdev->config->next_interface_id, intf);
1008*4882a593Smuzhiyun if (cdev->config->next_interface_id == 1)
1009*4882a593Smuzhiyun f = cdev->config->interface[intf];
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun break;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun if (f && f->setup)
1015*4882a593Smuzhiyun value = f->setup(f, ctrl);
1016*4882a593Smuzhiyun else {
1017*4882a593Smuzhiyun c = cdev->config;
1018*4882a593Smuzhiyun if (c->setup)
1019*4882a593Smuzhiyun value = c->setup(c, ctrl);
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun goto done;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun /* respond with data transfer before status phase? */
1026*4882a593Smuzhiyun if (value >= 0) {
1027*4882a593Smuzhiyun req->length = value;
1028*4882a593Smuzhiyun req->zero = value < w_length;
1029*4882a593Smuzhiyun value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1030*4882a593Smuzhiyun if (value < 0) {
1031*4882a593Smuzhiyun debug("ep_queue --> %d\n", value);
1032*4882a593Smuzhiyun req->status = 0;
1033*4882a593Smuzhiyun composite_setup_complete(gadget->ep0, req);
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun done:
1038*4882a593Smuzhiyun /* device either stalls (value < 0) or reports success */
1039*4882a593Smuzhiyun return value;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun
composite_disconnect(struct usb_gadget * gadget)1042*4882a593Smuzhiyun static void composite_disconnect(struct usb_gadget *gadget)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun struct usb_composite_dev *cdev = get_gadget_data(gadget);
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun if (cdev->config)
1047*4882a593Smuzhiyun reset_config(cdev);
1048*4882a593Smuzhiyun if (composite->disconnect)
1049*4882a593Smuzhiyun composite->disconnect(cdev);
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun
composite_unbind(struct usb_gadget * gadget)1052*4882a593Smuzhiyun static void composite_unbind(struct usb_gadget *gadget)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun struct usb_composite_dev *cdev = get_gadget_data(gadget);
1055*4882a593Smuzhiyun struct usb_configuration *c;
1056*4882a593Smuzhiyun struct usb_function *f;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun /*
1059*4882a593Smuzhiyun * composite_disconnect() must already have been called
1060*4882a593Smuzhiyun * by the underlying peripheral controller driver!
1061*4882a593Smuzhiyun * so there's no i/o concurrency that could affect the
1062*4882a593Smuzhiyun * state protected by cdev->lock.
1063*4882a593Smuzhiyun */
1064*4882a593Smuzhiyun BUG_ON(cdev->config);
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun while (!list_empty(&cdev->configs)) {
1067*4882a593Smuzhiyun c = list_first_entry(&cdev->configs,
1068*4882a593Smuzhiyun struct usb_configuration, list);
1069*4882a593Smuzhiyun while (!list_empty(&c->functions)) {
1070*4882a593Smuzhiyun f = list_first_entry(&c->functions,
1071*4882a593Smuzhiyun struct usb_function, list);
1072*4882a593Smuzhiyun list_del(&f->list);
1073*4882a593Smuzhiyun if (f->unbind) {
1074*4882a593Smuzhiyun debug("unbind function '%s'/%p\n",
1075*4882a593Smuzhiyun f->name, f);
1076*4882a593Smuzhiyun f->unbind(c, f);
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun list_del(&c->list);
1080*4882a593Smuzhiyun if (c->unbind) {
1081*4882a593Smuzhiyun debug("unbind config '%s'/%p\n", c->label, c);
1082*4882a593Smuzhiyun c->unbind(c);
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun free(c);
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun if (composite->unbind)
1087*4882a593Smuzhiyun composite->unbind(cdev);
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun if (cdev->req) {
1090*4882a593Smuzhiyun kfree(cdev->req->buf);
1091*4882a593Smuzhiyun usb_ep_free_request(gadget->ep0, cdev->req);
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun kfree(cdev);
1094*4882a593Smuzhiyun set_gadget_data(gadget, NULL);
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun composite = NULL;
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun
composite_bind(struct usb_gadget * gadget)1099*4882a593Smuzhiyun static int composite_bind(struct usb_gadget *gadget)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun int status = -ENOMEM;
1102*4882a593Smuzhiyun struct usb_composite_dev *cdev;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun cdev = calloc(sizeof *cdev, 1);
1105*4882a593Smuzhiyun if (!cdev)
1106*4882a593Smuzhiyun return status;
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun cdev->gadget = gadget;
1109*4882a593Smuzhiyun set_gadget_data(gadget, cdev);
1110*4882a593Smuzhiyun INIT_LIST_HEAD(&cdev->configs);
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun /* preallocate control response and buffer */
1113*4882a593Smuzhiyun cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1114*4882a593Smuzhiyun if (!cdev->req)
1115*4882a593Smuzhiyun goto fail;
1116*4882a593Smuzhiyun cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1117*4882a593Smuzhiyun if (!cdev->req->buf)
1118*4882a593Smuzhiyun goto fail;
1119*4882a593Smuzhiyun cdev->req->complete = composite_setup_complete;
1120*4882a593Smuzhiyun gadget->ep0->driver_data = cdev;
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun cdev->bufsiz = USB_BUFSIZ;
1123*4882a593Smuzhiyun cdev->driver = composite;
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun usb_gadget_set_selfpowered(gadget);
1126*4882a593Smuzhiyun usb_ep_autoconfig_reset(cdev->gadget);
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun status = composite->bind(cdev);
1129*4882a593Smuzhiyun if (status < 0)
1130*4882a593Smuzhiyun goto fail;
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun memcpy(&cdev->desc, composite->dev,
1133*4882a593Smuzhiyun sizeof(struct usb_device_descriptor));
1134*4882a593Smuzhiyun cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun debug("%s: ready\n", composite->name);
1137*4882a593Smuzhiyun return 0;
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun fail:
1140*4882a593Smuzhiyun composite_unbind(gadget);
1141*4882a593Smuzhiyun return status;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun static void
composite_suspend(struct usb_gadget * gadget)1145*4882a593Smuzhiyun composite_suspend(struct usb_gadget *gadget)
1146*4882a593Smuzhiyun {
1147*4882a593Smuzhiyun struct usb_composite_dev *cdev = get_gadget_data(gadget);
1148*4882a593Smuzhiyun struct usb_function *f;
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun debug("%s: suspend\n", __func__);
1151*4882a593Smuzhiyun if (cdev->config) {
1152*4882a593Smuzhiyun list_for_each_entry(f, &cdev->config->functions, list) {
1153*4882a593Smuzhiyun if (f->suspend)
1154*4882a593Smuzhiyun f->suspend(f);
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun if (composite->suspend)
1158*4882a593Smuzhiyun composite->suspend(cdev);
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun cdev->suspended = 1;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun static void
composite_resume(struct usb_gadget * gadget)1164*4882a593Smuzhiyun composite_resume(struct usb_gadget *gadget)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun struct usb_composite_dev *cdev = get_gadget_data(gadget);
1167*4882a593Smuzhiyun struct usb_function *f;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun debug("%s: resume\n", __func__);
1170*4882a593Smuzhiyun if (composite->resume)
1171*4882a593Smuzhiyun composite->resume(cdev);
1172*4882a593Smuzhiyun if (cdev->config) {
1173*4882a593Smuzhiyun list_for_each_entry(f, &cdev->config->functions, list) {
1174*4882a593Smuzhiyun if (f->resume)
1175*4882a593Smuzhiyun f->resume(f);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun cdev->suspended = 0;
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun static struct usb_gadget_driver composite_driver = {
1183*4882a593Smuzhiyun .speed = USB_SPEED_HIGH,
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun .bind = composite_bind,
1186*4882a593Smuzhiyun .unbind = composite_unbind,
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun .setup = composite_setup,
1189*4882a593Smuzhiyun .reset = composite_disconnect,
1190*4882a593Smuzhiyun .disconnect = composite_disconnect,
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun .suspend = composite_suspend,
1193*4882a593Smuzhiyun .resume = composite_resume,
1194*4882a593Smuzhiyun };
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun /**
1197*4882a593Smuzhiyun * usb_composite_register() - register a composite driver
1198*4882a593Smuzhiyun * @driver: the driver to register
1199*4882a593Smuzhiyun * Context: single threaded during gadget setup
1200*4882a593Smuzhiyun *
1201*4882a593Smuzhiyun * This function is used to register drivers using the composite driver
1202*4882a593Smuzhiyun * framework. The return value is zero, or a negative errno value.
1203*4882a593Smuzhiyun * Those values normally come from the driver's @bind method, which does
1204*4882a593Smuzhiyun * all the work of setting up the driver to match the hardware.
1205*4882a593Smuzhiyun *
1206*4882a593Smuzhiyun * On successful return, the gadget is ready to respond to requests from
1207*4882a593Smuzhiyun * the host, unless one of its components invokes usb_gadget_disconnect()
1208*4882a593Smuzhiyun * while it was binding. That would usually be done in order to wait for
1209*4882a593Smuzhiyun * some userspace participation.
1210*4882a593Smuzhiyun */
usb_composite_register(struct usb_composite_driver * driver)1211*4882a593Smuzhiyun int usb_composite_register(struct usb_composite_driver *driver)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun int res;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun if (!driver || !driver->dev || !driver->bind || composite)
1216*4882a593Smuzhiyun return -EINVAL;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun if (!driver->name)
1219*4882a593Smuzhiyun driver->name = "composite";
1220*4882a593Smuzhiyun composite = driver;
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun res = usb_gadget_register_driver(&composite_driver);
1223*4882a593Smuzhiyun if (res != 0)
1224*4882a593Smuzhiyun composite = NULL;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun return res;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun /**
1230*4882a593Smuzhiyun * usb_composite_unregister() - unregister a composite driver
1231*4882a593Smuzhiyun * @driver: the driver to unregister
1232*4882a593Smuzhiyun *
1233*4882a593Smuzhiyun * This function is used to unregister drivers using the composite
1234*4882a593Smuzhiyun * driver framework.
1235*4882a593Smuzhiyun */
usb_composite_unregister(struct usb_composite_driver * driver)1236*4882a593Smuzhiyun void usb_composite_unregister(struct usb_composite_driver *driver)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun if (composite != driver)
1239*4882a593Smuzhiyun return;
1240*4882a593Smuzhiyun usb_gadget_unregister_driver(&composite_driver);
1241*4882a593Smuzhiyun composite = NULL;
1242*4882a593Smuzhiyun }
1243