usb: gadget: push iProduct into gadgets
[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)
03de9bf6 907 str = composite->iManufacturer ?: composite_manufacturer;
ad1a8102 908 else if (cdev->product_override == id)
2d35ee47 909 str = composite->iProduct;
ad1a8102 910 else if (cdev->serial_override == id)
1cf0d264 911 str = composite->iSerialNumber;
ad1a8102
MN
912 else
913 str = NULL;
914 if (str) {
915 struct usb_gadget_strings strings = {
916 .language = language,
917 .strings = &(struct usb_string) { 0xff, str }
918 };
919 return usb_gadget_get_string(&strings, 0xff, buf);
920 }
921
922 /* String IDs are device-scoped, so we look up each string
923 * table we're told about. These lookups are infrequent;
924 * simpler-is-better here.
40982be5
DB
925 */
926 if (composite->strings) {
927 len = lookup_string(composite->strings, buf, language, id);
928 if (len > 0)
929 return len;
930 }
931 list_for_each_entry(c, &cdev->configs, list) {
932 if (c->strings) {
933 len = lookup_string(c->strings, buf, language, id);
934 if (len > 0)
935 return len;
936 }
937 list_for_each_entry(f, &c->functions, list) {
938 if (!f->strings)
939 continue;
940 len = lookup_string(f->strings, buf, language, id);
941 if (len > 0)
942 return len;
943 }
944 }
945 return -EINVAL;
946}
947
948/**
949 * usb_string_id() - allocate an unused string ID
950 * @cdev: the device whose string descriptor IDs are being allocated
951 * Context: single threaded during gadget setup
952 *
953 * @usb_string_id() is called from bind() callbacks to allocate
954 * string IDs. Drivers for functions, configurations, or gadgets will
955 * then store that ID in the appropriate descriptors and string table.
956 *
f2adc4f8
MN
957 * All string identifier should be allocated using this,
958 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
959 * that for example different functions don't wrongly assign different
960 * meanings to the same identifier.
40982be5 961 */
28824b18 962int usb_string_id(struct usb_composite_dev *cdev)
40982be5
DB
963{
964 if (cdev->next_string_id < 254) {
f2adc4f8
MN
965 /* string id 0 is reserved by USB spec for list of
966 * supported languages */
967 /* 255 reserved as well? -- mina86 */
40982be5
DB
968 cdev->next_string_id++;
969 return cdev->next_string_id;
970 }
971 return -ENODEV;
972}
973
f2adc4f8
MN
974/**
975 * usb_string_ids() - allocate unused string IDs in batch
976 * @cdev: the device whose string descriptor IDs are being allocated
977 * @str: an array of usb_string objects to assign numbers to
978 * Context: single threaded during gadget setup
979 *
980 * @usb_string_ids() is called from bind() callbacks to allocate
981 * string IDs. Drivers for functions, configurations, or gadgets will
982 * then copy IDs from the string table to the appropriate descriptors
983 * and string table for other languages.
984 *
985 * All string identifier should be allocated using this,
986 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
987 * example different functions don't wrongly assign different meanings
988 * to the same identifier.
989 */
990int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
991{
992 int next = cdev->next_string_id;
993
994 for (; str->s; ++str) {
995 if (unlikely(next >= 254))
996 return -ENODEV;
997 str->id = ++next;
998 }
999
1000 cdev->next_string_id = next;
1001
1002 return 0;
1003}
1004
1005/**
1006 * usb_string_ids_n() - allocate unused string IDs in batch
d187abb9 1007 * @c: the device whose string descriptor IDs are being allocated
f2adc4f8
MN
1008 * @n: number of string IDs to allocate
1009 * Context: single threaded during gadget setup
1010 *
1011 * Returns the first requested ID. This ID and next @n-1 IDs are now
d187abb9 1012 * valid IDs. At least provided that @n is non-zero because if it
f2adc4f8
MN
1013 * is, returns last requested ID which is now very useful information.
1014 *
1015 * @usb_string_ids_n() is called from bind() callbacks to allocate
1016 * string IDs. Drivers for functions, configurations, or gadgets will
1017 * then store that ID in the appropriate descriptors and string table.
1018 *
1019 * All string identifier should be allocated using this,
1020 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1021 * example different functions don't wrongly assign different meanings
1022 * to the same identifier.
1023 */
1024int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1025{
1026 unsigned next = c->next_string_id;
1027 if (unlikely(n > 254 || (unsigned)next + n > 254))
1028 return -ENODEV;
1029 c->next_string_id += n;
1030 return next + 1;
1031}
1032
1033
40982be5
DB
1034/*-------------------------------------------------------------------------*/
1035
1036static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1037{
1038 if (req->status || req->actual != req->length)
1039 DBG((struct usb_composite_dev *) ep->driver_data,
1040 "setup complete --> %d, %d/%d\n",
1041 req->status, req->actual, req->length);
1042}
1043
1044/*
1045 * The setup() callback implements all the ep0 functionality that's
1046 * not handled lower down, in hardware or the hardware driver(like
1047 * device and endpoint feature flags, and their status). It's all
1048 * housekeeping for the gadget function we're implementing. Most of
1049 * the work is in config and function specific setup.
1050 */
1051static int
1052composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1053{
1054 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1055 struct usb_request *req = cdev->req;
1056 int value = -EOPNOTSUPP;
bdb64d72 1057 int status = 0;
40982be5 1058 u16 w_index = le16_to_cpu(ctrl->wIndex);
08889517 1059 u8 intf = w_index & 0xFF;
40982be5
DB
1060 u16 w_value = le16_to_cpu(ctrl->wValue);
1061 u16 w_length = le16_to_cpu(ctrl->wLength);
1062 struct usb_function *f = NULL;
5242658d 1063 u8 endp;
40982be5
DB
1064
1065 /* partial re-init of the response message; the function or the
1066 * gadget might need to intercept e.g. a control-OUT completion
1067 * when we delegate to it.
1068 */
1069 req->zero = 0;
1070 req->complete = composite_setup_complete;
2edb11cb 1071 req->length = 0;
40982be5
DB
1072 gadget->ep0->driver_data = cdev;
1073
1074 switch (ctrl->bRequest) {
1075
1076 /* we handle all standard USB descriptors */
1077 case USB_REQ_GET_DESCRIPTOR:
1078 if (ctrl->bRequestType != USB_DIR_IN)
1079 goto unknown;
1080 switch (w_value >> 8) {
1081
1082 case USB_DT_DEVICE:
1083 cdev->desc.bNumConfigurations =
1084 count_configs(cdev, USB_DT_DEVICE);
bdb64d72
TB
1085 cdev->desc.bMaxPacketSize0 =
1086 cdev->gadget->ep0->maxpacket;
1087 if (gadget_is_superspeed(gadget)) {
a8f21156 1088 if (gadget->speed >= USB_SPEED_SUPER) {
bdb64d72 1089 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
a8f21156
SAS
1090 cdev->desc.bMaxPacketSize0 = 9;
1091 } else {
bdb64d72 1092 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
a8f21156 1093 }
bdb64d72
TB
1094 }
1095
40982be5
DB
1096 value = min(w_length, (u16) sizeof cdev->desc);
1097 memcpy(req->buf, &cdev->desc, value);
1098 break;
1099 case USB_DT_DEVICE_QUALIFIER:
bdb64d72
TB
1100 if (!gadget_is_dualspeed(gadget) ||
1101 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1102 break;
1103 device_qual(cdev);
1104 value = min_t(int, w_length,
1105 sizeof(struct usb_qualifier_descriptor));
1106 break;
1107 case USB_DT_OTHER_SPEED_CONFIG:
bdb64d72
TB
1108 if (!gadget_is_dualspeed(gadget) ||
1109 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1110 break;
1111 /* FALLTHROUGH */
1112 case USB_DT_CONFIG:
1113 value = config_desc(cdev, w_value);
1114 if (value >= 0)
1115 value = min(w_length, (u16) value);
1116 break;
1117 case USB_DT_STRING:
1118 value = get_string(cdev, req->buf,
1119 w_index, w_value & 0xff);
1120 if (value >= 0)
1121 value = min(w_length, (u16) value);
1122 break;
bdb64d72
TB
1123 case USB_DT_BOS:
1124 if (gadget_is_superspeed(gadget)) {
1125 value = bos_desc(cdev);
1126 value = min(w_length, (u16) value);
1127 }
1128 break;
40982be5
DB
1129 }
1130 break;
1131
1132 /* any number of configs can work */
1133 case USB_REQ_SET_CONFIGURATION:
1134 if (ctrl->bRequestType != 0)
1135 goto unknown;
1136 if (gadget_is_otg(gadget)) {
1137 if (gadget->a_hnp_support)
1138 DBG(cdev, "HNP available\n");
1139 else if (gadget->a_alt_hnp_support)
1140 DBG(cdev, "HNP on another port\n");
1141 else
1142 VDBG(cdev, "HNP inactive\n");
1143 }
1144 spin_lock(&cdev->lock);
1145 value = set_config(cdev, ctrl, w_value);
1146 spin_unlock(&cdev->lock);
1147 break;
1148 case USB_REQ_GET_CONFIGURATION:
1149 if (ctrl->bRequestType != USB_DIR_IN)
1150 goto unknown;
1151 if (cdev->config)
1152 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1153 else
1154 *(u8 *)req->buf = 0;
1155 value = min(w_length, (u16) 1);
1156 break;
1157
1158 /* function drivers must handle get/set altsetting; if there's
1159 * no get() method, we know only altsetting zero works.
1160 */
1161 case USB_REQ_SET_INTERFACE:
1162 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1163 goto unknown;
ff085de7 1164 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1165 break;
08889517 1166 f = cdev->config->interface[intf];
40982be5
DB
1167 if (!f)
1168 break;
dd4dff8b 1169 if (w_value && !f->set_alt)
40982be5
DB
1170 break;
1171 value = f->set_alt(f, w_index, w_value);
1b9ba000
RQ
1172 if (value == USB_GADGET_DELAYED_STATUS) {
1173 DBG(cdev,
1174 "%s: interface %d (%s) requested delayed status\n",
1175 __func__, intf, f->name);
1176 cdev->delayed_status++;
1177 DBG(cdev, "delayed_status count %d\n",
1178 cdev->delayed_status);
1179 }
40982be5
DB
1180 break;
1181 case USB_REQ_GET_INTERFACE:
1182 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1183 goto unknown;
ff085de7 1184 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1185 break;
08889517 1186 f = cdev->config->interface[intf];
40982be5
DB
1187 if (!f)
1188 break;
1189 /* lots of interfaces only need altsetting zero... */
1190 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1191 if (value < 0)
1192 break;
1193 *((u8 *)req->buf) = value;
1194 value = min(w_length, (u16) 1);
1195 break;
bdb64d72
TB
1196
1197 /*
1198 * USB 3.0 additions:
1199 * Function driver should handle get_status request. If such cb
1200 * wasn't supplied we respond with default value = 0
1201 * Note: function driver should supply such cb only for the first
1202 * interface of the function
1203 */
1204 case USB_REQ_GET_STATUS:
1205 if (!gadget_is_superspeed(gadget))
1206 goto unknown;
1207 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1208 goto unknown;
1209 value = 2; /* This is the length of the get_status reply */
1210 put_unaligned_le16(0, req->buf);
1211 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1212 break;
1213 f = cdev->config->interface[intf];
1214 if (!f)
1215 break;
1216 status = f->get_status ? f->get_status(f) : 0;
1217 if (status < 0)
1218 break;
1219 put_unaligned_le16(status & 0x0000ffff, req->buf);
1220 break;
1221 /*
1222 * Function drivers should handle SetFeature/ClearFeature
1223 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1224 * only for the first interface of the function
1225 */
1226 case USB_REQ_CLEAR_FEATURE:
1227 case USB_REQ_SET_FEATURE:
1228 if (!gadget_is_superspeed(gadget))
1229 goto unknown;
1230 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1231 goto unknown;
1232 switch (w_value) {
1233 case USB_INTRF_FUNC_SUSPEND:
1234 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1235 break;
1236 f = cdev->config->interface[intf];
1237 if (!f)
1238 break;
1239 value = 0;
1240 if (f->func_suspend)
1241 value = f->func_suspend(f, w_index >> 8);
1242 if (value < 0) {
1243 ERROR(cdev,
1244 "func_suspend() returned error %d\n",
1245 value);
1246 value = 0;
1247 }
1248 break;
1249 }
1250 break;
40982be5
DB
1251 default:
1252unknown:
1253 VDBG(cdev,
1254 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1255 ctrl->bRequestType, ctrl->bRequest,
1256 w_value, w_index, w_length);
1257
5242658d
LP
1258 /* functions always handle their interfaces and endpoints...
1259 * punt other recipients (other, WUSB, ...) to the current
40982be5
DB
1260 * configuration code.
1261 *
1262 * REVISIT it could make sense to let the composite device
1263 * take such requests too, if that's ever needed: to work
1264 * in config 0, etc.
1265 */
5242658d
LP
1266 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1267 case USB_RECIP_INTERFACE:
ff085de7 1268 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
3c47eb06
MM
1269 break;
1270 f = cdev->config->interface[intf];
5242658d
LP
1271 break;
1272
1273 case USB_RECIP_ENDPOINT:
1274 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1275 list_for_each_entry(f, &cdev->config->functions, list) {
1276 if (test_bit(endp, f->endpoints))
1277 break;
1278 }
1279 if (&f->list == &cdev->config->functions)
40982be5 1280 f = NULL;
5242658d 1281 break;
40982be5 1282 }
5242658d
LP
1283
1284 if (f && f->setup)
1285 value = f->setup(f, ctrl);
1286 else {
40982be5
DB
1287 struct usb_configuration *c;
1288
1289 c = cdev->config;
1290 if (c && c->setup)
1291 value = c->setup(c, ctrl);
1292 }
1293
1294 goto done;
1295 }
1296
1297 /* respond with data transfer before status phase? */
1b9ba000 1298 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
40982be5
DB
1299 req->length = value;
1300 req->zero = value < w_length;
1301 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1302 if (value < 0) {
1303 DBG(cdev, "ep_queue --> %d\n", value);
1304 req->status = 0;
1305 composite_setup_complete(gadget->ep0, req);
1306 }
1b9ba000
RQ
1307 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1308 WARN(cdev,
1309 "%s: Delayed status not supported for w_length != 0",
1310 __func__);
40982be5
DB
1311 }
1312
1313done:
1314 /* device either stalls (value < 0) or reports success */
1315 return value;
1316}
1317
1318static void composite_disconnect(struct usb_gadget *gadget)
1319{
1320 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1321 unsigned long flags;
1322
1323 /* REVISIT: should we have config and device level
1324 * disconnect callbacks?
1325 */
1326 spin_lock_irqsave(&cdev->lock, flags);
1327 if (cdev->config)
1328 reset_config(cdev);
ffe0b335
SAS
1329 if (cdev->driver->disconnect)
1330 cdev->driver->disconnect(cdev);
40982be5
DB
1331 spin_unlock_irqrestore(&cdev->lock, flags);
1332}
1333
1334/*-------------------------------------------------------------------------*/
1335
f48cf80f
FC
1336static ssize_t composite_show_suspended(struct device *dev,
1337 struct device_attribute *attr,
1338 char *buf)
1339{
1340 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1341 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1342
1343 return sprintf(buf, "%d\n", cdev->suspended);
1344}
1345
1346static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1347
28824b18 1348static void
40982be5
DB
1349composite_unbind(struct usb_gadget *gadget)
1350{
1351 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1352
1353 /* composite_disconnect() must already have been called
1354 * by the underlying peripheral controller driver!
1355 * so there's no i/o concurrency that could affect the
1356 * state protected by cdev->lock.
1357 */
1358 WARN_ON(cdev->config);
1359
1360 while (!list_empty(&cdev->configs)) {
1361 struct usb_configuration *c;
40982be5
DB
1362 c = list_first_entry(&cdev->configs,
1363 struct usb_configuration, list);
51cce6fc 1364 remove_config(cdev, c);
40982be5 1365 }
ffe0b335
SAS
1366 if (cdev->driver->unbind)
1367 cdev->driver->unbind(cdev);
40982be5
DB
1368
1369 if (cdev->req) {
1370 kfree(cdev->req->buf);
1371 usb_ep_free_request(gadget->ep0, cdev->req);
1372 }
daba5803 1373 device_remove_file(&gadget->dev, &dev_attr_suspended);
40982be5
DB
1374 kfree(cdev);
1375 set_gadget_data(gadget, NULL);
40982be5
DB
1376}
1377
ad1a8102 1378static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
40982be5 1379{
ad1a8102
MN
1380 if (!*desc) {
1381 int ret = usb_string_id(cdev);
1382 if (unlikely(ret < 0))
1383 WARNING(cdev, "failed to override string ID\n");
1384 else
1385 *desc = ret;
40982be5 1386 }
40982be5 1387
ad1a8102 1388 return *desc;
40982be5
DB
1389}
1390
7d16e8d3
SAS
1391static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1392 const struct usb_device_descriptor *old)
1393{
1394 __le16 idVendor;
1395 __le16 idProduct;
1396 __le16 bcdDevice;
1cf0d264 1397 u8 iSerialNumber;
03de9bf6 1398 u8 iManufacturer;
2d35ee47 1399 u8 iProduct;
7d16e8d3
SAS
1400
1401 /*
1402 * these variables may have been set in
1403 * usb_composite_overwrite_options()
1404 */
1405 idVendor = new->idVendor;
1406 idProduct = new->idProduct;
1407 bcdDevice = new->bcdDevice;
1cf0d264 1408 iSerialNumber = new->iSerialNumber;
03de9bf6 1409 iManufacturer = new->iManufacturer;
2d35ee47 1410 iProduct = new->iProduct;
7d16e8d3
SAS
1411
1412 *new = *old;
1413 if (idVendor)
1414 new->idVendor = idVendor;
1415 if (idProduct)
1416 new->idProduct = idProduct;
1417 if (bcdDevice)
1418 new->bcdDevice = bcdDevice;
1cf0d264
SAS
1419 if (iSerialNumber)
1420 new->iSerialNumber = iSerialNumber;
03de9bf6
SAS
1421 if (iManufacturer)
1422 new->iManufacturer = iManufacturer;
2d35ee47
SAS
1423 if (iProduct)
1424 new->iProduct = iProduct;
7d16e8d3
SAS
1425}
1426
ffe0b335
SAS
1427static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1428{
1429 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1430}
1431
1432static int composite_bind(struct usb_gadget *gadget,
1433 struct usb_gadget_driver *gdriver)
40982be5
DB
1434{
1435 struct usb_composite_dev *cdev;
ffe0b335 1436 struct usb_composite_driver *composite = to_cdriver(gdriver);
40982be5
DB
1437 int status = -ENOMEM;
1438
1439 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1440 if (!cdev)
1441 return status;
1442
1443 spin_lock_init(&cdev->lock);
1444 cdev->gadget = gadget;
1445 set_gadget_data(gadget, cdev);
1446 INIT_LIST_HEAD(&cdev->configs);
1447
1448 /* preallocate control response and buffer */
1449 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1450 if (!cdev->req)
1451 goto fail;
e13f17ff 1452 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
40982be5
DB
1453 if (!cdev->req->buf)
1454 goto fail;
1455 cdev->req->complete = composite_setup_complete;
1456 gadget->ep0->driver_data = cdev;
1457
40982be5
DB
1458 cdev->driver = composite;
1459
37b5801e
PM
1460 /*
1461 * As per USB compliance update, a device that is actively drawing
1462 * more than 100mA from USB must report itself as bus-powered in
1463 * the GetStatus(DEVICE) call.
1464 */
1465 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1466 usb_gadget_set_selfpowered(gadget);
40982be5
DB
1467
1468 /* interface and string IDs start at zero via kzalloc.
1469 * we force endpoints to start unassigned; few controller
1470 * drivers will zero ep->driver_data.
1471 */
1472 usb_ep_autoconfig_reset(cdev->gadget);
1473
1474 /* composite gadget needs to assign strings for whole device (like
1475 * serial number), register function drivers, potentially update
1476 * power state and consumption, etc
1477 */
fac3a43e 1478 status = composite->bind(cdev);
40982be5
DB
1479 if (status < 0)
1480 goto fail;
1481
7d16e8d3 1482 update_unchanged_dev_desc(&cdev->desc, composite->dev);
dbb442b8 1483
78bff3c6 1484 /* string overrides */
03de9bf6
SAS
1485 if (!cdev->desc.iManufacturer) {
1486 if (!composite->iManufacturer)
ad1a8102
MN
1487 snprintf(composite_manufacturer,
1488 sizeof composite_manufacturer,
1489 "%s %s with %s",
1490 init_utsname()->sysname,
1491 init_utsname()->release,
1492 gadget->name);
1493
1494 cdev->manufacturer_override =
1495 override_id(cdev, &cdev->desc.iManufacturer);
1496 }
1497
2d35ee47 1498 if (!cdev->desc.iProduct && composite->iProduct)
ad1a8102
MN
1499 cdev->product_override =
1500 override_id(cdev, &cdev->desc.iProduct);
1501
1cf0d264 1502 if (composite->iSerialNumber)
ad1a8102
MN
1503 cdev->serial_override =
1504 override_id(cdev, &cdev->desc.iSerialNumber);
1505
1506 /* has userspace failed to provide a serial number? */
1507 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1508 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
40982be5 1509
ad1a8102 1510 /* finish up */
f48cf80f
FC
1511 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1512 if (status)
1513 goto fail;
1514
40982be5
DB
1515 INFO(cdev, "%s ready\n", composite->name);
1516 return 0;
1517
1518fail:
1519 composite_unbind(gadget);
1520 return status;
1521}
1522
1523/*-------------------------------------------------------------------------*/
1524
1525static void
1526composite_suspend(struct usb_gadget *gadget)
1527{
1528 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1529 struct usb_function *f;
1530
8942939a 1531 /* REVISIT: should we have config level
40982be5
DB
1532 * suspend/resume callbacks?
1533 */
1534 DBG(cdev, "suspend\n");
1535 if (cdev->config) {
1536 list_for_each_entry(f, &cdev->config->functions, list) {
1537 if (f->suspend)
1538 f->suspend(f);
1539 }
1540 }
ffe0b335
SAS
1541 if (cdev->driver->suspend)
1542 cdev->driver->suspend(cdev);
f48cf80f
FC
1543
1544 cdev->suspended = 1;
b23f2f94
HW
1545
1546 usb_gadget_vbus_draw(gadget, 2);
40982be5
DB
1547}
1548
1549static void
1550composite_resume(struct usb_gadget *gadget)
1551{
1552 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1553 struct usb_function *f;
b23f2f94 1554 u8 maxpower;
40982be5 1555
8942939a 1556 /* REVISIT: should we have config level
40982be5
DB
1557 * suspend/resume callbacks?
1558 */
1559 DBG(cdev, "resume\n");
ffe0b335
SAS
1560 if (cdev->driver->resume)
1561 cdev->driver->resume(cdev);
40982be5
DB
1562 if (cdev->config) {
1563 list_for_each_entry(f, &cdev->config->functions, list) {
1564 if (f->resume)
1565 f->resume(f);
1566 }
b23f2f94
HW
1567
1568 maxpower = cdev->config->bMaxPower;
1569
1570 usb_gadget_vbus_draw(gadget, maxpower ?
1571 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
40982be5 1572 }
f48cf80f
FC
1573
1574 cdev->suspended = 0;
40982be5
DB
1575}
1576
1577/*-------------------------------------------------------------------------*/
1578
ffe0b335 1579static const struct usb_gadget_driver composite_driver_template = {
93952956 1580 .bind = composite_bind,
915c8bef 1581 .unbind = composite_unbind,
40982be5
DB
1582
1583 .setup = composite_setup,
1584 .disconnect = composite_disconnect,
1585
1586 .suspend = composite_suspend,
1587 .resume = composite_resume,
1588
1589 .driver = {
1590 .owner = THIS_MODULE,
1591 },
1592};
1593
1594/**
07a18bd7 1595 * usb_composite_probe() - register a composite driver
40982be5 1596 * @driver: the driver to register
07a18bd7
MN
1597 * @bind: the callback used to allocate resources that are shared across the
1598 * whole device, such as string IDs, and add its configurations using
1599 * @usb_add_config(). This may fail by returning a negative errno
1600 * value; it should return zero on successful initialization.
40982be5
DB
1601 * Context: single threaded during gadget setup
1602 *
1603 * This function is used to register drivers using the composite driver
1604 * framework. The return value is zero, or a negative errno value.
1605 * Those values normally come from the driver's @bind method, which does
1606 * all the work of setting up the driver to match the hardware.
1607 *
1608 * On successful return, the gadget is ready to respond to requests from
1609 * the host, unless one of its components invokes usb_gadget_disconnect()
1610 * while it was binding. That would usually be done in order to wait for
1611 * some userspace participation.
1612 */
03e42bd5 1613int usb_composite_probe(struct usb_composite_driver *driver)
40982be5 1614{
ffe0b335
SAS
1615 struct usb_gadget_driver *gadget_driver;
1616
1617 if (!driver || !driver->dev || !driver->bind)
40982be5
DB
1618 return -EINVAL;
1619
1620 if (!driver->name)
1621 driver->name = "composite";
05c3eebd
JB
1622 if (!driver->iProduct)
1623 driver->iProduct = driver->name;
40982be5 1624
ffe0b335
SAS
1625 driver->gadget_driver = composite_driver_template;
1626 gadget_driver = &driver->gadget_driver;
1627
1628 gadget_driver->function = (char *) driver->name;
1629 gadget_driver->driver.name = driver->name;
1630 gadget_driver->max_speed = driver->max_speed;
1631
1632 return usb_gadget_probe_driver(gadget_driver);
40982be5
DB
1633}
1634
1635/**
1636 * usb_composite_unregister() - unregister a composite driver
1637 * @driver: the driver to unregister
1638 *
1639 * This function is used to unregister drivers using the composite
1640 * driver framework.
1641 */
28824b18 1642void usb_composite_unregister(struct usb_composite_driver *driver)
40982be5 1643{
ffe0b335 1644 usb_gadget_unregister_driver(&driver->gadget_driver);
40982be5 1645}
1b9ba000
RQ
1646
1647/**
1648 * usb_composite_setup_continue() - Continue with the control transfer
1649 * @cdev: the composite device who's control transfer was kept waiting
1650 *
1651 * This function must be called by the USB function driver to continue
1652 * with the control transfer's data/status stage in case it had requested to
1653 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1654 * can request the composite framework to delay the setup request's data/status
1655 * stages by returning USB_GADGET_DELAYED_STATUS.
1656 */
1657void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1658{
1659 int value;
1660 struct usb_request *req = cdev->req;
1661 unsigned long flags;
1662
1663 DBG(cdev, "%s\n", __func__);
1664 spin_lock_irqsave(&cdev->lock, flags);
1665
1666 if (cdev->delayed_status == 0) {
1667 WARN(cdev, "%s: Unexpected call\n", __func__);
1668
1669 } else if (--cdev->delayed_status == 0) {
1670 DBG(cdev, "%s: Completing delayed status\n", __func__);
1671 req->length = 0;
1672 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1673 if (value < 0) {
1674 DBG(cdev, "ep_queue --> %d\n", value);
1675 req->status = 0;
1676 composite_setup_complete(cdev->gadget->ep0, req);
1677 }
1678 }
1679
1680 spin_unlock_irqrestore(&cdev->lock, flags);
1681}
1682
7d16e8d3
SAS
1683void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1684 struct usb_composite_overwrite *covr)
1685{
1686 struct usb_device_descriptor *desc = &cdev->desc;
1cf0d264
SAS
1687 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1688 struct usb_string *dev_str = gstr->strings;
7d16e8d3
SAS
1689
1690 if (covr->idVendor)
1691 desc->idVendor = cpu_to_le16(covr->idVendor);
1692
1693 if (covr->idProduct)
1694 desc->idProduct = cpu_to_le16(covr->idProduct);
1695
1696 if (covr->bcdDevice)
1697 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
1cf0d264
SAS
1698
1699 if (covr->serial_number) {
1700 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
1701 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
1702 }
03de9bf6
SAS
1703 if (covr->manufacturer) {
1704 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1705 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
1706 }
2d35ee47
SAS
1707
1708 if (covr->product) {
1709 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
1710 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
1711 }
7d16e8d3 1712}
This page took 0.845325 seconds and 5 git commands to generate.