usb: gadget: remove string override from struct usb_composite_driver
[deliverable/linux.git] / drivers / usb / gadget / composite.c
CommitLineData
40982be5
DB
1/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
40982be5
DB
10 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
6eb0de82 17#include <linux/module.h>
40982be5 18#include <linux/device.h>
ad1a8102 19#include <linux/utsname.h>
40982be5
DB
20
21#include <linux/usb/composite.h>
bdb64d72 22#include <asm/unaligned.h>
40982be5
DB
23
24/*
25 * The code in this file is utility code, used to build a gadget driver
26 * from one or more "function" drivers, one or more "configuration"
27 * objects, and a "usb_composite_driver" by gluing them together along
28 * with the relevant device-wide data.
29 */
30
ad1a8102
MN
31static char composite_manufacturer[50];
32
40982be5 33/*-------------------------------------------------------------------------*/
48767a4e
TB
34/**
35 * next_ep_desc() - advance to the next EP descriptor
36 * @t: currect pointer within descriptor array
37 *
38 * Return: next EP descriptor or NULL
39 *
40 * Iterate over @t until either EP descriptor found or
41 * NULL (that indicates end of list) encountered
42 */
43static struct usb_descriptor_header**
44next_ep_desc(struct usb_descriptor_header **t)
45{
46 for (; *t; t++) {
47 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
48 return t;
49 }
50 return NULL;
51}
52
53/*
54 * for_each_ep_desc()- iterate over endpoint descriptors in the
55 * descriptors list
56 * @start: pointer within descriptor array.
57 * @ep_desc: endpoint descriptor to use as the loop cursor
58 */
59#define for_each_ep_desc(start, ep_desc) \
60 for (ep_desc = next_ep_desc(start); \
61 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
62
63/**
64 * config_ep_by_speed() - configures the given endpoint
65 * according to gadget speed.
66 * @g: pointer to the gadget
67 * @f: usb function
68 * @_ep: the endpoint to configure
69 *
70 * Return: error code, 0 on success
71 *
72 * This function chooses the right descriptors for a given
73 * endpoint according to gadget speed and saves it in the
74 * endpoint desc field. If the endpoint already has a descriptor
75 * assigned to it - overwrites it with currently corresponding
76 * descriptor. The endpoint maxpacket field is updated according
77 * to the chosen descriptor.
78 * Note: the supplied function should hold all the descriptors
79 * for supported speeds
80 */
81int config_ep_by_speed(struct usb_gadget *g,
82 struct usb_function *f,
83 struct usb_ep *_ep)
84{
b785ea7c 85 struct usb_composite_dev *cdev = get_gadget_data(g);
48767a4e
TB
86 struct usb_endpoint_descriptor *chosen_desc = NULL;
87 struct usb_descriptor_header **speed_desc = NULL;
88
bdb64d72
TB
89 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
90 int want_comp_desc = 0;
91
48767a4e
TB
92 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
93
94 if (!g || !f || !_ep)
95 return -EIO;
96
97 /* select desired speed */
98 switch (g->speed) {
bdb64d72
TB
99 case USB_SPEED_SUPER:
100 if (gadget_is_superspeed(g)) {
101 speed_desc = f->ss_descriptors;
102 want_comp_desc = 1;
103 break;
104 }
105 /* else: Fall trough */
48767a4e
TB
106 case USB_SPEED_HIGH:
107 if (gadget_is_dualspeed(g)) {
108 speed_desc = f->hs_descriptors;
109 break;
110 }
111 /* else: fall through */
112 default:
113 speed_desc = f->descriptors;
114 }
115 /* find descriptors */
116 for_each_ep_desc(speed_desc, d_spd) {
117 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
118 if (chosen_desc->bEndpointAddress == _ep->address)
119 goto ep_found;
120 }
121 return -EIO;
122
123ep_found:
124 /* commit results */
29cc8897 125 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
48767a4e 126 _ep->desc = chosen_desc;
bdb64d72
TB
127 _ep->comp_desc = NULL;
128 _ep->maxburst = 0;
129 _ep->mult = 0;
130 if (!want_comp_desc)
131 return 0;
48767a4e 132
bdb64d72
TB
133 /*
134 * Companion descriptor should follow EP descriptor
135 * USB 3.0 spec, #9.6.7
136 */
137 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
138 if (!comp_desc ||
139 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
140 return -EIO;
141 _ep->comp_desc = comp_desc;
142 if (g->speed == USB_SPEED_SUPER) {
143 switch (usb_endpoint_type(_ep->desc)) {
bdb64d72
TB
144 case USB_ENDPOINT_XFER_ISOC:
145 /* mult: bits 1:0 of bmAttributes */
146 _ep->mult = comp_desc->bmAttributes & 0x3;
9e878a6b
PZ
147 case USB_ENDPOINT_XFER_BULK:
148 case USB_ENDPOINT_XFER_INT:
b785ea7c 149 _ep->maxburst = comp_desc->bMaxBurst + 1;
bdb64d72
TB
150 break;
151 default:
b785ea7c
FB
152 if (comp_desc->bMaxBurst != 0)
153 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
154 _ep->maxburst = 1;
bdb64d72
TB
155 break;
156 }
157 }
48767a4e
TB
158 return 0;
159}
40982be5
DB
160
161/**
162 * usb_add_function() - add a function to a configuration
163 * @config: the configuration
164 * @function: the function being added
165 * Context: single threaded during gadget setup
166 *
167 * After initialization, each configuration must have one or more
168 * functions added to it. Adding a function involves calling its @bind()
169 * method to allocate resources such as interface and string identifiers
170 * and endpoints.
171 *
172 * This function returns the value of the function's bind(), which is
173 * zero for success else a negative errno value.
174 */
28824b18 175int usb_add_function(struct usb_configuration *config,
40982be5
DB
176 struct usb_function *function)
177{
178 int value = -EINVAL;
179
180 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
181 function->name, function,
182 config->label, config);
183
184 if (!function->set_alt || !function->disable)
185 goto done;
186
187 function->config = config;
188 list_add_tail(&function->list, &config->functions);
189
190 /* REVISIT *require* function->bind? */
191 if (function->bind) {
192 value = function->bind(config, function);
193 if (value < 0) {
194 list_del(&function->list);
195 function->config = NULL;
196 }
197 } else
198 value = 0;
199
200 /* We allow configurations that don't work at both speeds.
201 * If we run into a lowspeed Linux system, treat it the same
202 * as full speed ... it's the function drivers that will need
203 * to avoid bulk and ISO transfers.
204 */
205 if (!config->fullspeed && function->descriptors)
206 config->fullspeed = true;
207 if (!config->highspeed && function->hs_descriptors)
208 config->highspeed = true;
bdb64d72
TB
209 if (!config->superspeed && function->ss_descriptors)
210 config->superspeed = true;
40982be5
DB
211
212done:
213 if (value)
214 DBG(config->cdev, "adding '%s'/%p --> %d\n",
215 function->name, function, value);
216 return value;
217}
218
60beed95
DB
219/**
220 * usb_function_deactivate - prevent function and gadget enumeration
221 * @function: the function that isn't yet ready to respond
222 *
223 * Blocks response of the gadget driver to host enumeration by
224 * preventing the data line pullup from being activated. This is
225 * normally called during @bind() processing to change from the
226 * initial "ready to respond" state, or when a required resource
227 * becomes available.
228 *
229 * For example, drivers that serve as a passthrough to a userspace
230 * daemon can block enumeration unless that daemon (such as an OBEX,
231 * MTP, or print server) is ready to handle host requests.
232 *
233 * Not all systems support software control of their USB peripheral
234 * data pullups.
235 *
236 * Returns zero on success, else negative errno.
237 */
238int usb_function_deactivate(struct usb_function *function)
239{
240 struct usb_composite_dev *cdev = function->config->cdev;
b2bdf3a7 241 unsigned long flags;
60beed95
DB
242 int status = 0;
243
b2bdf3a7 244 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
245
246 if (cdev->deactivations == 0)
247 status = usb_gadget_disconnect(cdev->gadget);
248 if (status == 0)
249 cdev->deactivations++;
250
b2bdf3a7 251 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
252 return status;
253}
254
255/**
256 * usb_function_activate - allow function and gadget enumeration
257 * @function: function on which usb_function_activate() was called
258 *
259 * Reverses effect of usb_function_deactivate(). If no more functions
260 * are delaying their activation, the gadget driver will respond to
261 * host enumeration procedures.
262 *
263 * Returns zero on success, else negative errno.
264 */
265int usb_function_activate(struct usb_function *function)
266{
267 struct usb_composite_dev *cdev = function->config->cdev;
4fefe9f6 268 unsigned long flags;
60beed95
DB
269 int status = 0;
270
4fefe9f6 271 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
272
273 if (WARN_ON(cdev->deactivations == 0))
274 status = -EINVAL;
275 else {
276 cdev->deactivations--;
277 if (cdev->deactivations == 0)
278 status = usb_gadget_connect(cdev->gadget);
279 }
280
4fefe9f6 281 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
282 return status;
283}
284
40982be5
DB
285/**
286 * usb_interface_id() - allocate an unused interface ID
287 * @config: configuration associated with the interface
288 * @function: function handling the interface
289 * Context: single threaded during gadget setup
290 *
291 * usb_interface_id() is called from usb_function.bind() callbacks to
292 * allocate new interface IDs. The function driver will then store that
293 * ID in interface, association, CDC union, and other descriptors. It
25985edc 294 * will also handle any control requests targeted at that interface,
40982be5
DB
295 * particularly changing its altsetting via set_alt(). There may
296 * also be class-specific or vendor-specific requests to handle.
297 *
298 * All interface identifier should be allocated using this routine, to
299 * ensure that for example different functions don't wrongly assign
300 * different meanings to the same identifier. Note that since interface
25985edc 301 * identifiers are configuration-specific, functions used in more than
40982be5
DB
302 * one configuration (or more than once in a given configuration) need
303 * multiple versions of the relevant descriptors.
304 *
305 * Returns the interface ID which was allocated; or -ENODEV if no
306 * more interface IDs can be allocated.
307 */
28824b18 308int usb_interface_id(struct usb_configuration *config,
40982be5
DB
309 struct usb_function *function)
310{
311 unsigned id = config->next_interface_id;
312
313 if (id < MAX_CONFIG_INTERFACES) {
314 config->interface[id] = function;
315 config->next_interface_id = id + 1;
316 return id;
317 }
318 return -ENODEV;
319}
320
321static int config_buf(struct usb_configuration *config,
322 enum usb_device_speed speed, void *buf, u8 type)
323{
324 struct usb_config_descriptor *c = buf;
325 void *next = buf + USB_DT_CONFIG_SIZE;
e13f17ff 326 int len;
40982be5
DB
327 struct usb_function *f;
328 int status;
329
e13f17ff 330 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
40982be5
DB
331 /* write the config descriptor */
332 c = buf;
333 c->bLength = USB_DT_CONFIG_SIZE;
334 c->bDescriptorType = type;
335 /* wTotalLength is written later */
336 c->bNumInterfaces = config->next_interface_id;
337 c->bConfigurationValue = config->bConfigurationValue;
338 c->iConfiguration = config->iConfiguration;
339 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
36e893d2 340 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
40982be5
DB
341
342 /* There may be e.g. OTG descriptors */
343 if (config->descriptors) {
344 status = usb_descriptor_fillbuf(next, len,
345 config->descriptors);
346 if (status < 0)
347 return status;
348 len -= status;
349 next += status;
350 }
351
352 /* add each function's descriptors */
353 list_for_each_entry(f, &config->functions, list) {
354 struct usb_descriptor_header **descriptors;
355
bdb64d72
TB
356 switch (speed) {
357 case USB_SPEED_SUPER:
358 descriptors = f->ss_descriptors;
359 break;
360 case USB_SPEED_HIGH:
40982be5 361 descriptors = f->hs_descriptors;
bdb64d72
TB
362 break;
363 default:
40982be5 364 descriptors = f->descriptors;
bdb64d72
TB
365 }
366
40982be5
DB
367 if (!descriptors)
368 continue;
369 status = usb_descriptor_fillbuf(next, len,
370 (const struct usb_descriptor_header **) descriptors);
371 if (status < 0)
372 return status;
373 len -= status;
374 next += status;
375 }
376
377 len = next - buf;
378 c->wTotalLength = cpu_to_le16(len);
379 return len;
380}
381
382static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
383{
384 struct usb_gadget *gadget = cdev->gadget;
385 struct usb_configuration *c;
386 u8 type = w_value >> 8;
387 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
388
bdb64d72
TB
389 if (gadget->speed == USB_SPEED_SUPER)
390 speed = gadget->speed;
391 else if (gadget_is_dualspeed(gadget)) {
392 int hs = 0;
40982be5
DB
393 if (gadget->speed == USB_SPEED_HIGH)
394 hs = 1;
395 if (type == USB_DT_OTHER_SPEED_CONFIG)
396 hs = !hs;
397 if (hs)
398 speed = USB_SPEED_HIGH;
399
400 }
401
402 /* This is a lookup by config *INDEX* */
403 w_value &= 0xff;
404 list_for_each_entry(c, &cdev->configs, list) {
405 /* ignore configs that won't work at this speed */
bdb64d72
TB
406 switch (speed) {
407 case USB_SPEED_SUPER:
408 if (!c->superspeed)
409 continue;
410 break;
411 case USB_SPEED_HIGH:
40982be5
DB
412 if (!c->highspeed)
413 continue;
bdb64d72
TB
414 break;
415 default:
40982be5
DB
416 if (!c->fullspeed)
417 continue;
418 }
bdb64d72 419
40982be5
DB
420 if (w_value == 0)
421 return config_buf(c, speed, cdev->req->buf, type);
422 w_value--;
423 }
424 return -EINVAL;
425}
426
427static int count_configs(struct usb_composite_dev *cdev, unsigned type)
428{
429 struct usb_gadget *gadget = cdev->gadget;
430 struct usb_configuration *c;
431 unsigned count = 0;
432 int hs = 0;
bdb64d72 433 int ss = 0;
40982be5
DB
434
435 if (gadget_is_dualspeed(gadget)) {
436 if (gadget->speed == USB_SPEED_HIGH)
437 hs = 1;
bdb64d72
TB
438 if (gadget->speed == USB_SPEED_SUPER)
439 ss = 1;
40982be5
DB
440 if (type == USB_DT_DEVICE_QUALIFIER)
441 hs = !hs;
442 }
443 list_for_each_entry(c, &cdev->configs, list) {
444 /* ignore configs that won't work at this speed */
bdb64d72
TB
445 if (ss) {
446 if (!c->superspeed)
447 continue;
448 } else if (hs) {
40982be5
DB
449 if (!c->highspeed)
450 continue;
451 } else {
452 if (!c->fullspeed)
453 continue;
454 }
455 count++;
456 }
457 return count;
458}
459
bdb64d72
TB
460/**
461 * bos_desc() - prepares the BOS descriptor.
462 * @cdev: pointer to usb_composite device to generate the bos
463 * descriptor for
464 *
465 * This function generates the BOS (Binary Device Object)
466 * descriptor and its device capabilities descriptors. The BOS
467 * descriptor should be supported by a SuperSpeed device.
468 */
469static int bos_desc(struct usb_composite_dev *cdev)
470{
471 struct usb_ext_cap_descriptor *usb_ext;
472 struct usb_ss_cap_descriptor *ss_cap;
473 struct usb_dcd_config_params dcd_config_params;
474 struct usb_bos_descriptor *bos = cdev->req->buf;
475
476 bos->bLength = USB_DT_BOS_SIZE;
477 bos->bDescriptorType = USB_DT_BOS;
478
479 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
480 bos->bNumDeviceCaps = 0;
481
482 /*
483 * A SuperSpeed device shall include the USB2.0 extension descriptor
484 * and shall support LPM when operating in USB2.0 HS mode.
485 */
486 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
487 bos->bNumDeviceCaps++;
488 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
489 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
490 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
491 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
492 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
493
494 /*
495 * The Superspeed USB Capability descriptor shall be implemented by all
496 * SuperSpeed devices.
497 */
498 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
499 bos->bNumDeviceCaps++;
500 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
501 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
502 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
503 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
504 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
505 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
506 USB_FULL_SPEED_OPERATION |
507 USB_HIGH_SPEED_OPERATION |
508 USB_5GBPS_OPERATION);
509 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
510
511 /* Get Controller configuration */
512 if (cdev->gadget->ops->get_config_params)
513 cdev->gadget->ops->get_config_params(&dcd_config_params);
514 else {
089b837a 515 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
bdb64d72 516 dcd_config_params.bU2DevExitLat =
089b837a 517 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
bdb64d72
TB
518 }
519 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
520 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
521
522 return le16_to_cpu(bos->wTotalLength);
523}
524
40982be5
DB
525static void device_qual(struct usb_composite_dev *cdev)
526{
527 struct usb_qualifier_descriptor *qual = cdev->req->buf;
528
529 qual->bLength = sizeof(*qual);
530 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
531 /* POLICY: same bcdUSB and device type info at both speeds */
532 qual->bcdUSB = cdev->desc.bcdUSB;
533 qual->bDeviceClass = cdev->desc.bDeviceClass;
534 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
535 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
536 /* ASSUME same EP0 fifo size at both speeds */
765f5b83 537 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
40982be5 538 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
c24f4227 539 qual->bRESERVED = 0;
40982be5
DB
540}
541
542/*-------------------------------------------------------------------------*/
543
544static void reset_config(struct usb_composite_dev *cdev)
545{
546 struct usb_function *f;
547
548 DBG(cdev, "reset config\n");
549
550 list_for_each_entry(f, &cdev->config->functions, list) {
551 if (f->disable)
552 f->disable(f);
5242658d
LP
553
554 bitmap_zero(f->endpoints, 32);
40982be5
DB
555 }
556 cdev->config = NULL;
557}
558
559static int set_config(struct usb_composite_dev *cdev,
560 const struct usb_ctrlrequest *ctrl, unsigned number)
561{
562 struct usb_gadget *gadget = cdev->gadget;
563 struct usb_configuration *c = NULL;
564 int result = -EINVAL;
565 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
566 int tmp;
567
40982be5
DB
568 if (number) {
569 list_for_each_entry(c, &cdev->configs, list) {
570 if (c->bConfigurationValue == number) {
bdb64d72
TB
571 /*
572 * We disable the FDs of the previous
573 * configuration only if the new configuration
574 * is a valid one
575 */
576 if (cdev->config)
577 reset_config(cdev);
40982be5
DB
578 result = 0;
579 break;
580 }
581 }
582 if (result < 0)
583 goto done;
bdb64d72
TB
584 } else { /* Zero configuration value - need to reset the config */
585 if (cdev->config)
586 reset_config(cdev);
40982be5 587 result = 0;
bdb64d72 588 }
40982be5 589
e538dfda
MN
590 INFO(cdev, "%s config #%d: %s\n",
591 usb_speed_string(gadget->speed),
592 number, c ? c->label : "unconfigured");
40982be5
DB
593
594 if (!c)
595 goto done;
596
597 cdev->config = c;
598
599 /* Initialize all interfaces by setting them to altsetting zero. */
600 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
601 struct usb_function *f = c->interface[tmp];
5242658d 602 struct usb_descriptor_header **descriptors;
40982be5
DB
603
604 if (!f)
605 break;
606
5242658d
LP
607 /*
608 * Record which endpoints are used by the function. This is used
609 * to dispatch control requests targeted at that endpoint to the
610 * function's setup callback instead of the current
611 * configuration's setup callback.
612 */
bdb64d72
TB
613 switch (gadget->speed) {
614 case USB_SPEED_SUPER:
615 descriptors = f->ss_descriptors;
616 break;
617 case USB_SPEED_HIGH:
5242658d 618 descriptors = f->hs_descriptors;
bdb64d72
TB
619 break;
620 default:
5242658d 621 descriptors = f->descriptors;
bdb64d72 622 }
5242658d
LP
623
624 for (; *descriptors; ++descriptors) {
625 struct usb_endpoint_descriptor *ep;
626 int addr;
627
628 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
629 continue;
630
631 ep = (struct usb_endpoint_descriptor *)*descriptors;
632 addr = ((ep->bEndpointAddress & 0x80) >> 3)
633 | (ep->bEndpointAddress & 0x0f);
634 set_bit(addr, f->endpoints);
635 }
636
40982be5
DB
637 result = f->set_alt(f, tmp, 0);
638 if (result < 0) {
639 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
640 tmp, f->name, f, result);
641
642 reset_config(cdev);
643 goto done;
644 }
1b9ba000
RQ
645
646 if (result == USB_GADGET_DELAYED_STATUS) {
647 DBG(cdev,
648 "%s: interface %d (%s) requested delayed status\n",
649 __func__, tmp, f->name);
650 cdev->delayed_status++;
651 DBG(cdev, "delayed_status count %d\n",
652 cdev->delayed_status);
653 }
40982be5
DB
654 }
655
656 /* when we return, be sure our power usage is valid */
36e893d2 657 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
40982be5
DB
658done:
659 usb_gadget_vbus_draw(gadget, power);
1b9ba000
RQ
660 if (result >= 0 && cdev->delayed_status)
661 result = USB_GADGET_DELAYED_STATUS;
40982be5
DB
662 return result;
663}
664
665/**
666 * usb_add_config() - add a configuration to a device.
667 * @cdev: wraps the USB gadget
668 * @config: the configuration, with bConfigurationValue assigned
c9bfff9c 669 * @bind: the configuration's bind function
40982be5
DB
670 * Context: single threaded during gadget setup
671 *
c9bfff9c 672 * One of the main tasks of a composite @bind() routine is to
40982be5
DB
673 * add each of the configurations it supports, using this routine.
674 *
c9bfff9c 675 * This function returns the value of the configuration's @bind(), which
40982be5
DB
676 * is zero for success else a negative errno value. Binding configurations
677 * assigns global resources including string IDs, and per-configuration
678 * resources such as interface IDs and endpoints.
679 */
28824b18 680int usb_add_config(struct usb_composite_dev *cdev,
c9bfff9c
UKK
681 struct usb_configuration *config,
682 int (*bind)(struct usb_configuration *))
40982be5
DB
683{
684 int status = -EINVAL;
685 struct usb_configuration *c;
686
687 DBG(cdev, "adding config #%u '%s'/%p\n",
688 config->bConfigurationValue,
689 config->label, config);
690
c9bfff9c 691 if (!config->bConfigurationValue || !bind)
40982be5
DB
692 goto done;
693
694 /* Prevent duplicate configuration identifiers */
695 list_for_each_entry(c, &cdev->configs, list) {
696 if (c->bConfigurationValue == config->bConfigurationValue) {
697 status = -EBUSY;
698 goto done;
699 }
700 }
701
702 config->cdev = cdev;
703 list_add_tail(&config->list, &cdev->configs);
704
705 INIT_LIST_HEAD(&config->functions);
706 config->next_interface_id = 0;
02e8161e 707 memset(config->interface, 0, sizeof(config->interface));
40982be5 708
c9bfff9c 709 status = bind(config);
40982be5 710 if (status < 0) {
124ef389
YO
711 while (!list_empty(&config->functions)) {
712 struct usb_function *f;
713
714 f = list_first_entry(&config->functions,
715 struct usb_function, list);
716 list_del(&f->list);
717 if (f->unbind) {
718 DBG(cdev, "unbind function '%s'/%p\n",
719 f->name, f);
720 f->unbind(config, f);
721 /* may free memory for "f" */
722 }
723 }
40982be5
DB
724 list_del(&config->list);
725 config->cdev = NULL;
726 } else {
727 unsigned i;
728
bdb64d72 729 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
40982be5 730 config->bConfigurationValue, config,
bdb64d72 731 config->superspeed ? " super" : "",
40982be5
DB
732 config->highspeed ? " high" : "",
733 config->fullspeed
734 ? (gadget_is_dualspeed(cdev->gadget)
735 ? " full"
736 : " full/low")
737 : "");
738
739 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
740 struct usb_function *f = config->interface[i];
741
742 if (!f)
743 continue;
744 DBG(cdev, " interface %d = %s/%p\n",
745 i, f->name, f);
746 }
747 }
748
c9bfff9c 749 /* set_alt(), or next bind(), sets up
40982be5
DB
750 * ep->driver_data as needed.
751 */
752 usb_ep_autoconfig_reset(cdev->gadget);
753
754done:
755 if (status)
756 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
757 config->bConfigurationValue, status);
758 return status;
759}
760
51cce6fc
BG
761static void remove_config(struct usb_composite_dev *cdev,
762 struct usb_configuration *config)
763{
764 while (!list_empty(&config->functions)) {
765 struct usb_function *f;
766
767 f = list_first_entry(&config->functions,
768 struct usb_function, list);
769 list_del(&f->list);
770 if (f->unbind) {
771 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
772 f->unbind(config, f);
773 /* may free memory for "f" */
774 }
775 }
776 list_del(&config->list);
777 if (config->unbind) {
778 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
779 config->unbind(config);
780 /* may free memory for "c" */
781 }
782}
783
784/**
785 * usb_remove_config() - remove a configuration from a device.
786 * @cdev: wraps the USB gadget
787 * @config: the configuration
788 *
789 * Drivers must call usb_gadget_disconnect before calling this function
790 * to disconnect the device from the host and make sure the host will not
791 * try to enumerate the device while we are changing the config list.
792 */
793void usb_remove_config(struct usb_composite_dev *cdev,
794 struct usb_configuration *config)
795{
796 unsigned long flags;
797
798 spin_lock_irqsave(&cdev->lock, flags);
799
800 if (cdev->config == config)
801 reset_config(cdev);
802
803 spin_unlock_irqrestore(&cdev->lock, flags);
804
805 remove_config(cdev, config);
806}
807
40982be5
DB
808/*-------------------------------------------------------------------------*/
809
810/* We support strings in multiple languages ... string descriptor zero
811 * says which languages are supported. The typical case will be that
812 * only one language (probably English) is used, with I18N handled on
813 * the host side.
814 */
815
816static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
817{
818 const struct usb_gadget_strings *s;
20c5e74c 819 __le16 language;
40982be5
DB
820 __le16 *tmp;
821
822 while (*sp) {
823 s = *sp;
824 language = cpu_to_le16(s->language);
825 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
826 if (*tmp == language)
827 goto repeat;
828 }
829 *tmp++ = language;
830repeat:
831 sp++;
832 }
833}
834
835static int lookup_string(
836 struct usb_gadget_strings **sp,
837 void *buf,
838 u16 language,
839 int id
840)
841{
842 struct usb_gadget_strings *s;
843 int value;
844
845 while (*sp) {
846 s = *sp++;
847 if (s->language != language)
848 continue;
849 value = usb_gadget_get_string(s, id, buf);
850 if (value > 0)
851 return value;
852 }
853 return -EINVAL;
854}
855
856static int get_string(struct usb_composite_dev *cdev,
857 void *buf, u16 language, int id)
858{
ffe0b335 859 struct usb_composite_driver *composite = cdev->driver;
40982be5
DB
860 struct usb_configuration *c;
861 struct usb_function *f;
862 int len;
ad1a8102 863 const char *str;
40982be5
DB
864
865 /* Yes, not only is USB's I18N support probably more than most
866 * folk will ever care about ... also, it's all supported here.
867 * (Except for UTF8 support for Unicode's "Astral Planes".)
868 */
869
870 /* 0 == report all available language codes */
871 if (id == 0) {
872 struct usb_string_descriptor *s = buf;
873 struct usb_gadget_strings **sp;
874
875 memset(s, 0, 256);
876 s->bDescriptorType = USB_DT_STRING;
877
878 sp = composite->strings;
879 if (sp)
880 collect_langs(sp, s->wData);
881
882 list_for_each_entry(c, &cdev->configs, list) {
883 sp = c->strings;
884 if (sp)
885 collect_langs(sp, s->wData);
886
887 list_for_each_entry(f, &c->functions, list) {
888 sp = f->strings;
889 if (sp)
890 collect_langs(sp, s->wData);
891 }
892 }
893
417b57b3 894 for (len = 0; len <= 126 && s->wData[len]; len++)
40982be5
DB
895 continue;
896 if (!len)
897 return -EINVAL;
898
899 s->bLength = 2 * (len + 1);
900 return s->bLength;
901 }
902
ad1a8102
MN
903 /* Otherwise, look up and return a specified string. First
904 * check if the string has not been overridden.
905 */
906 if (cdev->manufacturer_override == id)
d33f74fc 907 str = composite_manufacturer;
ad1a8102
MN
908 else
909 str = NULL;
910 if (str) {
911 struct usb_gadget_strings strings = {
912 .language = language,
913 .strings = &(struct usb_string) { 0xff, str }
914 };
915 return usb_gadget_get_string(&strings, 0xff, buf);
916 }
917
918 /* String IDs are device-scoped, so we look up each string
919 * table we're told about. These lookups are infrequent;
920 * simpler-is-better here.
40982be5
DB
921 */
922 if (composite->strings) {
923 len = lookup_string(composite->strings, buf, language, id);
924 if (len > 0)
925 return len;
926 }
927 list_for_each_entry(c, &cdev->configs, list) {
928 if (c->strings) {
929 len = lookup_string(c->strings, buf, language, id);
930 if (len > 0)
931 return len;
932 }
933 list_for_each_entry(f, &c->functions, list) {
934 if (!f->strings)
935 continue;
936 len = lookup_string(f->strings, buf, language, id);
937 if (len > 0)
938 return len;
939 }
940 }
941 return -EINVAL;
942}
943
944/**
945 * usb_string_id() - allocate an unused string ID
946 * @cdev: the device whose string descriptor IDs are being allocated
947 * Context: single threaded during gadget setup
948 *
949 * @usb_string_id() is called from bind() callbacks to allocate
950 * string IDs. Drivers for functions, configurations, or gadgets will
951 * then store that ID in the appropriate descriptors and string table.
952 *
f2adc4f8
MN
953 * All string identifier should be allocated using this,
954 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
955 * that for example different functions don't wrongly assign different
956 * meanings to the same identifier.
40982be5 957 */
28824b18 958int usb_string_id(struct usb_composite_dev *cdev)
40982be5
DB
959{
960 if (cdev->next_string_id < 254) {
f2adc4f8
MN
961 /* string id 0 is reserved by USB spec for list of
962 * supported languages */
963 /* 255 reserved as well? -- mina86 */
40982be5
DB
964 cdev->next_string_id++;
965 return cdev->next_string_id;
966 }
967 return -ENODEV;
968}
969
f2adc4f8
MN
970/**
971 * usb_string_ids() - allocate unused string IDs in batch
972 * @cdev: the device whose string descriptor IDs are being allocated
973 * @str: an array of usb_string objects to assign numbers to
974 * Context: single threaded during gadget setup
975 *
976 * @usb_string_ids() is called from bind() callbacks to allocate
977 * string IDs. Drivers for functions, configurations, or gadgets will
978 * then copy IDs from the string table to the appropriate descriptors
979 * and string table for other languages.
980 *
981 * All string identifier should be allocated using this,
982 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
983 * example different functions don't wrongly assign different meanings
984 * to the same identifier.
985 */
986int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
987{
988 int next = cdev->next_string_id;
989
990 for (; str->s; ++str) {
991 if (unlikely(next >= 254))
992 return -ENODEV;
993 str->id = ++next;
994 }
995
996 cdev->next_string_id = next;
997
998 return 0;
999}
1000
1001/**
1002 * usb_string_ids_n() - allocate unused string IDs in batch
d187abb9 1003 * @c: the device whose string descriptor IDs are being allocated
f2adc4f8
MN
1004 * @n: number of string IDs to allocate
1005 * Context: single threaded during gadget setup
1006 *
1007 * Returns the first requested ID. This ID and next @n-1 IDs are now
d187abb9 1008 * valid IDs. At least provided that @n is non-zero because if it
f2adc4f8
MN
1009 * is, returns last requested ID which is now very useful information.
1010 *
1011 * @usb_string_ids_n() is called from bind() callbacks to allocate
1012 * string IDs. Drivers for functions, configurations, or gadgets will
1013 * then store that ID in the appropriate descriptors and string table.
1014 *
1015 * All string identifier should be allocated using this,
1016 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1017 * example different functions don't wrongly assign different meanings
1018 * to the same identifier.
1019 */
1020int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1021{
1022 unsigned next = c->next_string_id;
1023 if (unlikely(n > 254 || (unsigned)next + n > 254))
1024 return -ENODEV;
1025 c->next_string_id += n;
1026 return next + 1;
1027}
1028
1029
40982be5
DB
1030/*-------------------------------------------------------------------------*/
1031
1032static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1033{
1034 if (req->status || req->actual != req->length)
1035 DBG((struct usb_composite_dev *) ep->driver_data,
1036 "setup complete --> %d, %d/%d\n",
1037 req->status, req->actual, req->length);
1038}
1039
1040/*
1041 * The setup() callback implements all the ep0 functionality that's
1042 * not handled lower down, in hardware or the hardware driver(like
1043 * device and endpoint feature flags, and their status). It's all
1044 * housekeeping for the gadget function we're implementing. Most of
1045 * the work is in config and function specific setup.
1046 */
1047static int
1048composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1049{
1050 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1051 struct usb_request *req = cdev->req;
1052 int value = -EOPNOTSUPP;
bdb64d72 1053 int status = 0;
40982be5 1054 u16 w_index = le16_to_cpu(ctrl->wIndex);
08889517 1055 u8 intf = w_index & 0xFF;
40982be5
DB
1056 u16 w_value = le16_to_cpu(ctrl->wValue);
1057 u16 w_length = le16_to_cpu(ctrl->wLength);
1058 struct usb_function *f = NULL;
5242658d 1059 u8 endp;
40982be5
DB
1060
1061 /* partial re-init of the response message; the function or the
1062 * gadget might need to intercept e.g. a control-OUT completion
1063 * when we delegate to it.
1064 */
1065 req->zero = 0;
1066 req->complete = composite_setup_complete;
2edb11cb 1067 req->length = 0;
40982be5
DB
1068 gadget->ep0->driver_data = cdev;
1069
1070 switch (ctrl->bRequest) {
1071
1072 /* we handle all standard USB descriptors */
1073 case USB_REQ_GET_DESCRIPTOR:
1074 if (ctrl->bRequestType != USB_DIR_IN)
1075 goto unknown;
1076 switch (w_value >> 8) {
1077
1078 case USB_DT_DEVICE:
1079 cdev->desc.bNumConfigurations =
1080 count_configs(cdev, USB_DT_DEVICE);
bdb64d72
TB
1081 cdev->desc.bMaxPacketSize0 =
1082 cdev->gadget->ep0->maxpacket;
1083 if (gadget_is_superspeed(gadget)) {
a8f21156 1084 if (gadget->speed >= USB_SPEED_SUPER) {
bdb64d72 1085 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
a8f21156
SAS
1086 cdev->desc.bMaxPacketSize0 = 9;
1087 } else {
bdb64d72 1088 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
a8f21156 1089 }
bdb64d72
TB
1090 }
1091
40982be5
DB
1092 value = min(w_length, (u16) sizeof cdev->desc);
1093 memcpy(req->buf, &cdev->desc, value);
1094 break;
1095 case USB_DT_DEVICE_QUALIFIER:
bdb64d72
TB
1096 if (!gadget_is_dualspeed(gadget) ||
1097 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1098 break;
1099 device_qual(cdev);
1100 value = min_t(int, w_length,
1101 sizeof(struct usb_qualifier_descriptor));
1102 break;
1103 case USB_DT_OTHER_SPEED_CONFIG:
bdb64d72
TB
1104 if (!gadget_is_dualspeed(gadget) ||
1105 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1106 break;
1107 /* FALLTHROUGH */
1108 case USB_DT_CONFIG:
1109 value = config_desc(cdev, w_value);
1110 if (value >= 0)
1111 value = min(w_length, (u16) value);
1112 break;
1113 case USB_DT_STRING:
1114 value = get_string(cdev, req->buf,
1115 w_index, w_value & 0xff);
1116 if (value >= 0)
1117 value = min(w_length, (u16) value);
1118 break;
bdb64d72
TB
1119 case USB_DT_BOS:
1120 if (gadget_is_superspeed(gadget)) {
1121 value = bos_desc(cdev);
1122 value = min(w_length, (u16) value);
1123 }
1124 break;
40982be5
DB
1125 }
1126 break;
1127
1128 /* any number of configs can work */
1129 case USB_REQ_SET_CONFIGURATION:
1130 if (ctrl->bRequestType != 0)
1131 goto unknown;
1132 if (gadget_is_otg(gadget)) {
1133 if (gadget->a_hnp_support)
1134 DBG(cdev, "HNP available\n");
1135 else if (gadget->a_alt_hnp_support)
1136 DBG(cdev, "HNP on another port\n");
1137 else
1138 VDBG(cdev, "HNP inactive\n");
1139 }
1140 spin_lock(&cdev->lock);
1141 value = set_config(cdev, ctrl, w_value);
1142 spin_unlock(&cdev->lock);
1143 break;
1144 case USB_REQ_GET_CONFIGURATION:
1145 if (ctrl->bRequestType != USB_DIR_IN)
1146 goto unknown;
1147 if (cdev->config)
1148 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1149 else
1150 *(u8 *)req->buf = 0;
1151 value = min(w_length, (u16) 1);
1152 break;
1153
1154 /* function drivers must handle get/set altsetting; if there's
1155 * no get() method, we know only altsetting zero works.
1156 */
1157 case USB_REQ_SET_INTERFACE:
1158 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1159 goto unknown;
ff085de7 1160 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1161 break;
08889517 1162 f = cdev->config->interface[intf];
40982be5
DB
1163 if (!f)
1164 break;
dd4dff8b 1165 if (w_value && !f->set_alt)
40982be5
DB
1166 break;
1167 value = f->set_alt(f, w_index, w_value);
1b9ba000
RQ
1168 if (value == USB_GADGET_DELAYED_STATUS) {
1169 DBG(cdev,
1170 "%s: interface %d (%s) requested delayed status\n",
1171 __func__, intf, f->name);
1172 cdev->delayed_status++;
1173 DBG(cdev, "delayed_status count %d\n",
1174 cdev->delayed_status);
1175 }
40982be5
DB
1176 break;
1177 case USB_REQ_GET_INTERFACE:
1178 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1179 goto unknown;
ff085de7 1180 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1181 break;
08889517 1182 f = cdev->config->interface[intf];
40982be5
DB
1183 if (!f)
1184 break;
1185 /* lots of interfaces only need altsetting zero... */
1186 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1187 if (value < 0)
1188 break;
1189 *((u8 *)req->buf) = value;
1190 value = min(w_length, (u16) 1);
1191 break;
bdb64d72
TB
1192
1193 /*
1194 * USB 3.0 additions:
1195 * Function driver should handle get_status request. If such cb
1196 * wasn't supplied we respond with default value = 0
1197 * Note: function driver should supply such cb only for the first
1198 * interface of the function
1199 */
1200 case USB_REQ_GET_STATUS:
1201 if (!gadget_is_superspeed(gadget))
1202 goto unknown;
1203 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1204 goto unknown;
1205 value = 2; /* This is the length of the get_status reply */
1206 put_unaligned_le16(0, req->buf);
1207 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1208 break;
1209 f = cdev->config->interface[intf];
1210 if (!f)
1211 break;
1212 status = f->get_status ? f->get_status(f) : 0;
1213 if (status < 0)
1214 break;
1215 put_unaligned_le16(status & 0x0000ffff, req->buf);
1216 break;
1217 /*
1218 * Function drivers should handle SetFeature/ClearFeature
1219 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1220 * only for the first interface of the function
1221 */
1222 case USB_REQ_CLEAR_FEATURE:
1223 case USB_REQ_SET_FEATURE:
1224 if (!gadget_is_superspeed(gadget))
1225 goto unknown;
1226 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1227 goto unknown;
1228 switch (w_value) {
1229 case USB_INTRF_FUNC_SUSPEND:
1230 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1231 break;
1232 f = cdev->config->interface[intf];
1233 if (!f)
1234 break;
1235 value = 0;
1236 if (f->func_suspend)
1237 value = f->func_suspend(f, w_index >> 8);
1238 if (value < 0) {
1239 ERROR(cdev,
1240 "func_suspend() returned error %d\n",
1241 value);
1242 value = 0;
1243 }
1244 break;
1245 }
1246 break;
40982be5
DB
1247 default:
1248unknown:
1249 VDBG(cdev,
1250 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1251 ctrl->bRequestType, ctrl->bRequest,
1252 w_value, w_index, w_length);
1253
5242658d
LP
1254 /* functions always handle their interfaces and endpoints...
1255 * punt other recipients (other, WUSB, ...) to the current
40982be5
DB
1256 * configuration code.
1257 *
1258 * REVISIT it could make sense to let the composite device
1259 * take such requests too, if that's ever needed: to work
1260 * in config 0, etc.
1261 */
5242658d
LP
1262 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1263 case USB_RECIP_INTERFACE:
ff085de7 1264 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
3c47eb06
MM
1265 break;
1266 f = cdev->config->interface[intf];
5242658d
LP
1267 break;
1268
1269 case USB_RECIP_ENDPOINT:
1270 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1271 list_for_each_entry(f, &cdev->config->functions, list) {
1272 if (test_bit(endp, f->endpoints))
1273 break;
1274 }
1275 if (&f->list == &cdev->config->functions)
40982be5 1276 f = NULL;
5242658d 1277 break;
40982be5 1278 }
5242658d
LP
1279
1280 if (f && f->setup)
1281 value = f->setup(f, ctrl);
1282 else {
40982be5
DB
1283 struct usb_configuration *c;
1284
1285 c = cdev->config;
1286 if (c && c->setup)
1287 value = c->setup(c, ctrl);
1288 }
1289
1290 goto done;
1291 }
1292
1293 /* respond with data transfer before status phase? */
1b9ba000 1294 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
40982be5
DB
1295 req->length = value;
1296 req->zero = value < w_length;
1297 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1298 if (value < 0) {
1299 DBG(cdev, "ep_queue --> %d\n", value);
1300 req->status = 0;
1301 composite_setup_complete(gadget->ep0, req);
1302 }
1b9ba000
RQ
1303 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1304 WARN(cdev,
1305 "%s: Delayed status not supported for w_length != 0",
1306 __func__);
40982be5
DB
1307 }
1308
1309done:
1310 /* device either stalls (value < 0) or reports success */
1311 return value;
1312}
1313
1314static void composite_disconnect(struct usb_gadget *gadget)
1315{
1316 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1317 unsigned long flags;
1318
1319 /* REVISIT: should we have config and device level
1320 * disconnect callbacks?
1321 */
1322 spin_lock_irqsave(&cdev->lock, flags);
1323 if (cdev->config)
1324 reset_config(cdev);
ffe0b335
SAS
1325 if (cdev->driver->disconnect)
1326 cdev->driver->disconnect(cdev);
40982be5
DB
1327 spin_unlock_irqrestore(&cdev->lock, flags);
1328}
1329
1330/*-------------------------------------------------------------------------*/
1331
f48cf80f
FC
1332static ssize_t composite_show_suspended(struct device *dev,
1333 struct device_attribute *attr,
1334 char *buf)
1335{
1336 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1337 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1338
1339 return sprintf(buf, "%d\n", cdev->suspended);
1340}
1341
1342static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1343
28824b18 1344static void
40982be5
DB
1345composite_unbind(struct usb_gadget *gadget)
1346{
1347 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1348
1349 /* composite_disconnect() must already have been called
1350 * by the underlying peripheral controller driver!
1351 * so there's no i/o concurrency that could affect the
1352 * state protected by cdev->lock.
1353 */
1354 WARN_ON(cdev->config);
1355
1356 while (!list_empty(&cdev->configs)) {
1357 struct usb_configuration *c;
40982be5
DB
1358 c = list_first_entry(&cdev->configs,
1359 struct usb_configuration, list);
51cce6fc 1360 remove_config(cdev, c);
40982be5 1361 }
ffe0b335
SAS
1362 if (cdev->driver->unbind)
1363 cdev->driver->unbind(cdev);
40982be5
DB
1364
1365 if (cdev->req) {
1366 kfree(cdev->req->buf);
1367 usb_ep_free_request(gadget->ep0, cdev->req);
1368 }
daba5803 1369 device_remove_file(&gadget->dev, &dev_attr_suspended);
40982be5
DB
1370 kfree(cdev);
1371 set_gadget_data(gadget, NULL);
40982be5
DB
1372}
1373
ad1a8102 1374static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
40982be5 1375{
ad1a8102
MN
1376 if (!*desc) {
1377 int ret = usb_string_id(cdev);
1378 if (unlikely(ret < 0))
1379 WARNING(cdev, "failed to override string ID\n");
1380 else
1381 *desc = ret;
40982be5 1382 }
40982be5 1383
ad1a8102 1384 return *desc;
40982be5
DB
1385}
1386
7d16e8d3
SAS
1387static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1388 const struct usb_device_descriptor *old)
1389{
1390 __le16 idVendor;
1391 __le16 idProduct;
1392 __le16 bcdDevice;
1cf0d264 1393 u8 iSerialNumber;
03de9bf6 1394 u8 iManufacturer;
2d35ee47 1395 u8 iProduct;
7d16e8d3
SAS
1396
1397 /*
1398 * these variables may have been set in
1399 * usb_composite_overwrite_options()
1400 */
1401 idVendor = new->idVendor;
1402 idProduct = new->idProduct;
1403 bcdDevice = new->bcdDevice;
1cf0d264 1404 iSerialNumber = new->iSerialNumber;
03de9bf6 1405 iManufacturer = new->iManufacturer;
2d35ee47 1406 iProduct = new->iProduct;
7d16e8d3
SAS
1407
1408 *new = *old;
1409 if (idVendor)
1410 new->idVendor = idVendor;
1411 if (idProduct)
1412 new->idProduct = idProduct;
1413 if (bcdDevice)
1414 new->bcdDevice = bcdDevice;
1cf0d264
SAS
1415 if (iSerialNumber)
1416 new->iSerialNumber = iSerialNumber;
03de9bf6
SAS
1417 if (iManufacturer)
1418 new->iManufacturer = iManufacturer;
2d35ee47
SAS
1419 if (iProduct)
1420 new->iProduct = iProduct;
7d16e8d3
SAS
1421}
1422
ffe0b335
SAS
1423static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1424{
1425 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1426}
1427
1428static int composite_bind(struct usb_gadget *gadget,
1429 struct usb_gadget_driver *gdriver)
40982be5
DB
1430{
1431 struct usb_composite_dev *cdev;
ffe0b335 1432 struct usb_composite_driver *composite = to_cdriver(gdriver);
40982be5
DB
1433 int status = -ENOMEM;
1434
1435 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1436 if (!cdev)
1437 return status;
1438
1439 spin_lock_init(&cdev->lock);
1440 cdev->gadget = gadget;
1441 set_gadget_data(gadget, cdev);
1442 INIT_LIST_HEAD(&cdev->configs);
1443
1444 /* preallocate control response and buffer */
1445 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1446 if (!cdev->req)
1447 goto fail;
e13f17ff 1448 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
40982be5
DB
1449 if (!cdev->req->buf)
1450 goto fail;
1451 cdev->req->complete = composite_setup_complete;
1452 gadget->ep0->driver_data = cdev;
1453
40982be5
DB
1454 cdev->driver = composite;
1455
37b5801e
PM
1456 /*
1457 * As per USB compliance update, a device that is actively drawing
1458 * more than 100mA from USB must report itself as bus-powered in
1459 * the GetStatus(DEVICE) call.
1460 */
1461 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1462 usb_gadget_set_selfpowered(gadget);
40982be5
DB
1463
1464 /* interface and string IDs start at zero via kzalloc.
1465 * we force endpoints to start unassigned; few controller
1466 * drivers will zero ep->driver_data.
1467 */
1468 usb_ep_autoconfig_reset(cdev->gadget);
1469
1470 /* composite gadget needs to assign strings for whole device (like
1471 * serial number), register function drivers, potentially update
1472 * power state and consumption, etc
1473 */
fac3a43e 1474 status = composite->bind(cdev);
40982be5
DB
1475 if (status < 0)
1476 goto fail;
1477
7d16e8d3 1478 update_unchanged_dev_desc(&cdev->desc, composite->dev);
dbb442b8 1479
78bff3c6 1480 /* string overrides */
03de9bf6 1481 if (!cdev->desc.iManufacturer) {
d33f74fc
SAS
1482 snprintf(composite_manufacturer,
1483 sizeof composite_manufacturer,
1484 "%s %s with %s",
1485 init_utsname()->sysname,
1486 init_utsname()->release,
1487 gadget->name);
ad1a8102
MN
1488
1489 cdev->manufacturer_override =
1490 override_id(cdev, &cdev->desc.iManufacturer);
1491 }
1492
ad1a8102
MN
1493 /* has userspace failed to provide a serial number? */
1494 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1495 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
40982be5 1496
ad1a8102 1497 /* finish up */
f48cf80f
FC
1498 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1499 if (status)
1500 goto fail;
1501
40982be5
DB
1502 INFO(cdev, "%s ready\n", composite->name);
1503 return 0;
1504
1505fail:
1506 composite_unbind(gadget);
1507 return status;
1508}
1509
1510/*-------------------------------------------------------------------------*/
1511
1512static void
1513composite_suspend(struct usb_gadget *gadget)
1514{
1515 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1516 struct usb_function *f;
1517
8942939a 1518 /* REVISIT: should we have config level
40982be5
DB
1519 * suspend/resume callbacks?
1520 */
1521 DBG(cdev, "suspend\n");
1522 if (cdev->config) {
1523 list_for_each_entry(f, &cdev->config->functions, list) {
1524 if (f->suspend)
1525 f->suspend(f);
1526 }
1527 }
ffe0b335
SAS
1528 if (cdev->driver->suspend)
1529 cdev->driver->suspend(cdev);
f48cf80f
FC
1530
1531 cdev->suspended = 1;
b23f2f94
HW
1532
1533 usb_gadget_vbus_draw(gadget, 2);
40982be5
DB
1534}
1535
1536static void
1537composite_resume(struct usb_gadget *gadget)
1538{
1539 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1540 struct usb_function *f;
b23f2f94 1541 u8 maxpower;
40982be5 1542
8942939a 1543 /* REVISIT: should we have config level
40982be5
DB
1544 * suspend/resume callbacks?
1545 */
1546 DBG(cdev, "resume\n");
ffe0b335
SAS
1547 if (cdev->driver->resume)
1548 cdev->driver->resume(cdev);
40982be5
DB
1549 if (cdev->config) {
1550 list_for_each_entry(f, &cdev->config->functions, list) {
1551 if (f->resume)
1552 f->resume(f);
1553 }
b23f2f94
HW
1554
1555 maxpower = cdev->config->bMaxPower;
1556
1557 usb_gadget_vbus_draw(gadget, maxpower ?
1558 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
40982be5 1559 }
f48cf80f
FC
1560
1561 cdev->suspended = 0;
40982be5
DB
1562}
1563
1564/*-------------------------------------------------------------------------*/
1565
ffe0b335 1566static const struct usb_gadget_driver composite_driver_template = {
93952956 1567 .bind = composite_bind,
915c8bef 1568 .unbind = composite_unbind,
40982be5
DB
1569
1570 .setup = composite_setup,
1571 .disconnect = composite_disconnect,
1572
1573 .suspend = composite_suspend,
1574 .resume = composite_resume,
1575
1576 .driver = {
1577 .owner = THIS_MODULE,
1578 },
1579};
1580
1581/**
07a18bd7 1582 * usb_composite_probe() - register a composite driver
40982be5 1583 * @driver: the driver to register
07a18bd7
MN
1584 * @bind: the callback used to allocate resources that are shared across the
1585 * whole device, such as string IDs, and add its configurations using
1586 * @usb_add_config(). This may fail by returning a negative errno
1587 * value; it should return zero on successful initialization.
40982be5
DB
1588 * Context: single threaded during gadget setup
1589 *
1590 * This function is used to register drivers using the composite driver
1591 * framework. The return value is zero, or a negative errno value.
1592 * Those values normally come from the driver's @bind method, which does
1593 * all the work of setting up the driver to match the hardware.
1594 *
1595 * On successful return, the gadget is ready to respond to requests from
1596 * the host, unless one of its components invokes usb_gadget_disconnect()
1597 * while it was binding. That would usually be done in order to wait for
1598 * some userspace participation.
1599 */
03e42bd5 1600int usb_composite_probe(struct usb_composite_driver *driver)
40982be5 1601{
ffe0b335
SAS
1602 struct usb_gadget_driver *gadget_driver;
1603
1604 if (!driver || !driver->dev || !driver->bind)
40982be5
DB
1605 return -EINVAL;
1606
1607 if (!driver->name)
1608 driver->name = "composite";
40982be5 1609
ffe0b335
SAS
1610 driver->gadget_driver = composite_driver_template;
1611 gadget_driver = &driver->gadget_driver;
1612
1613 gadget_driver->function = (char *) driver->name;
1614 gadget_driver->driver.name = driver->name;
1615 gadget_driver->max_speed = driver->max_speed;
1616
1617 return usb_gadget_probe_driver(gadget_driver);
40982be5
DB
1618}
1619
1620/**
1621 * usb_composite_unregister() - unregister a composite driver
1622 * @driver: the driver to unregister
1623 *
1624 * This function is used to unregister drivers using the composite
1625 * driver framework.
1626 */
28824b18 1627void usb_composite_unregister(struct usb_composite_driver *driver)
40982be5 1628{
ffe0b335 1629 usb_gadget_unregister_driver(&driver->gadget_driver);
40982be5 1630}
1b9ba000
RQ
1631
1632/**
1633 * usb_composite_setup_continue() - Continue with the control transfer
1634 * @cdev: the composite device who's control transfer was kept waiting
1635 *
1636 * This function must be called by the USB function driver to continue
1637 * with the control transfer's data/status stage in case it had requested to
1638 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1639 * can request the composite framework to delay the setup request's data/status
1640 * stages by returning USB_GADGET_DELAYED_STATUS.
1641 */
1642void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1643{
1644 int value;
1645 struct usb_request *req = cdev->req;
1646 unsigned long flags;
1647
1648 DBG(cdev, "%s\n", __func__);
1649 spin_lock_irqsave(&cdev->lock, flags);
1650
1651 if (cdev->delayed_status == 0) {
1652 WARN(cdev, "%s: Unexpected call\n", __func__);
1653
1654 } else if (--cdev->delayed_status == 0) {
1655 DBG(cdev, "%s: Completing delayed status\n", __func__);
1656 req->length = 0;
1657 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1658 if (value < 0) {
1659 DBG(cdev, "ep_queue --> %d\n", value);
1660 req->status = 0;
1661 composite_setup_complete(cdev->gadget->ep0, req);
1662 }
1663 }
1664
1665 spin_unlock_irqrestore(&cdev->lock, flags);
1666}
1667
7d16e8d3
SAS
1668void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1669 struct usb_composite_overwrite *covr)
1670{
1671 struct usb_device_descriptor *desc = &cdev->desc;
1cf0d264
SAS
1672 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1673 struct usb_string *dev_str = gstr->strings;
7d16e8d3
SAS
1674
1675 if (covr->idVendor)
1676 desc->idVendor = cpu_to_le16(covr->idVendor);
1677
1678 if (covr->idProduct)
1679 desc->idProduct = cpu_to_le16(covr->idProduct);
1680
1681 if (covr->bcdDevice)
1682 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
1cf0d264
SAS
1683
1684 if (covr->serial_number) {
1685 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
1686 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
1687 }
03de9bf6
SAS
1688 if (covr->manufacturer) {
1689 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1690 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
1691 }
2d35ee47
SAS
1692
1693 if (covr->product) {
1694 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
1695 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
1696 }
7d16e8d3 1697}
This page took 0.429936 seconds and 5 git commands to generate.