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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* #define VERBOSE_DEBUG */
22
23 #include <linux/kallsyms.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/device.h>
27
28 #include <linux/usb/composite.h>
29
30
31 /*
32 * The code in this file is utility code, used to build a gadget driver
33 * from one or more "function" drivers, one or more "configuration"
34 * objects, and a "usb_composite_driver" by gluing them together along
35 * with the relevant device-wide data.
36 */
37
38 /* big enough to hold our biggest descriptor */
39 #define USB_BUFSIZ 512
40
41 static struct usb_composite_driver *composite;
42
43 /* Some systems will need runtime overrides for the product identifers
44 * published in the device descriptor, either numbers or strings or both.
45 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
46 */
47
48 static ushort idVendor;
49 module_param(idVendor, ushort, 0);
50 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
51
52 static ushort idProduct;
53 module_param(idProduct, ushort, 0);
54 MODULE_PARM_DESC(idProduct, "USB Product ID");
55
56 static ushort bcdDevice;
57 module_param(bcdDevice, ushort, 0);
58 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
59
60 static char *iManufacturer;
61 module_param(iManufacturer, charp, 0);
62 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
63
64 static char *iProduct;
65 module_param(iProduct, charp, 0);
66 MODULE_PARM_DESC(iProduct, "USB Product string");
67
68 static char *iSerialNumber;
69 module_param(iSerialNumber, charp, 0);
70 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
71
72 /*-------------------------------------------------------------------------*/
73
74 /**
75 * usb_add_function() - add a function to a configuration
76 * @config: the configuration
77 * @function: the function being added
78 * Context: single threaded during gadget setup
79 *
80 * After initialization, each configuration must have one or more
81 * functions added to it. Adding a function involves calling its @bind()
82 * method to allocate resources such as interface and string identifiers
83 * and endpoints.
84 *
85 * This function returns the value of the function's bind(), which is
86 * zero for success else a negative errno value.
87 */
usb_add_function(struct usb_configuration * config,struct usb_function * function)88 int __init usb_add_function(struct usb_configuration *config,
89 struct usb_function *function)
90 {
91 int value = -EINVAL;
92
93 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
94 function->name, function,
95 config->label, config);
96
97 if (!function->set_alt || !function->disable)
98 goto done;
99
100 function->config = config;
101 list_add_tail(&function->list, &config->functions);
102
103 /* REVISIT *require* function->bind? */
104 if (function->bind) {
105 value = function->bind(config, function);
106 if (value < 0) {
107 list_del(&function->list);
108 function->config = NULL;
109 }
110 } else
111 value = 0;
112
113 /* We allow configurations that don't work at both speeds.
114 * If we run into a lowspeed Linux system, treat it the same
115 * as full speed ... it's the function drivers that will need
116 * to avoid bulk and ISO transfers.
117 */
118 if (!config->fullspeed && function->descriptors)
119 config->fullspeed = true;
120 if (!config->highspeed && function->hs_descriptors)
121 config->highspeed = true;
122
123 done:
124 if (value)
125 DBG(config->cdev, "adding '%s'/%p --> %d\n",
126 function->name, function, value);
127 return value;
128 }
129
130 /**
131 * usb_function_deactivate - prevent function and gadget enumeration
132 * @function: the function that isn't yet ready to respond
133 *
134 * Blocks response of the gadget driver to host enumeration by
135 * preventing the data line pullup from being activated. This is
136 * normally called during @bind() processing to change from the
137 * initial "ready to respond" state, or when a required resource
138 * becomes available.
139 *
140 * For example, drivers that serve as a passthrough to a userspace
141 * daemon can block enumeration unless that daemon (such as an OBEX,
142 * MTP, or print server) is ready to handle host requests.
143 *
144 * Not all systems support software control of their USB peripheral
145 * data pullups.
146 *
147 * Returns zero on success, else negative errno.
148 */
usb_function_deactivate(struct usb_function * function)149 int usb_function_deactivate(struct usb_function *function)
150 {
151 struct usb_composite_dev *cdev = function->config->cdev;
152 int status = 0;
153
154 spin_lock(&cdev->lock);
155
156 if (cdev->deactivations == 0)
157 status = usb_gadget_disconnect(cdev->gadget);
158 if (status == 0)
159 cdev->deactivations++;
160
161 spin_unlock(&cdev->lock);
162 return status;
163 }
164
165 /**
166 * usb_function_activate - allow function and gadget enumeration
167 * @function: function on which usb_function_activate() was called
168 *
169 * Reverses effect of usb_function_deactivate(). If no more functions
170 * are delaying their activation, the gadget driver will respond to
171 * host enumeration procedures.
172 *
173 * Returns zero on success, else negative errno.
174 */
usb_function_activate(struct usb_function * function)175 int usb_function_activate(struct usb_function *function)
176 {
177 struct usb_composite_dev *cdev = function->config->cdev;
178 int status = 0;
179
180 spin_lock(&cdev->lock);
181
182 if (WARN_ON(cdev->deactivations == 0))
183 status = -EINVAL;
184 else {
185 cdev->deactivations--;
186 if (cdev->deactivations == 0)
187 status = usb_gadget_connect(cdev->gadget);
188 }
189
190 spin_unlock(&cdev->lock);
191 return status;
192 }
193
194 /**
195 * usb_interface_id() - allocate an unused interface ID
196 * @config: configuration associated with the interface
197 * @function: function handling the interface
198 * Context: single threaded during gadget setup
199 *
200 * usb_interface_id() is called from usb_function.bind() callbacks to
201 * allocate new interface IDs. The function driver will then store that
202 * ID in interface, association, CDC union, and other descriptors. It
203 * will also handle any control requests targetted at that interface,
204 * particularly changing its altsetting via set_alt(). There may
205 * also be class-specific or vendor-specific requests to handle.
206 *
207 * All interface identifier should be allocated using this routine, to
208 * ensure that for example different functions don't wrongly assign
209 * different meanings to the same identifier. Note that since interface
210 * identifers are configuration-specific, functions used in more than
211 * one configuration (or more than once in a given configuration) need
212 * multiple versions of the relevant descriptors.
213 *
214 * Returns the interface ID which was allocated; or -ENODEV if no
215 * more interface IDs can be allocated.
216 */
usb_interface_id(struct usb_configuration * config,struct usb_function * function)217 int __init usb_interface_id(struct usb_configuration *config,
218 struct usb_function *function)
219 {
220 unsigned id = config->next_interface_id;
221
222 if (id < MAX_CONFIG_INTERFACES) {
223 config->interface[id] = function;
224 config->next_interface_id = id + 1;
225 return id;
226 }
227 return -ENODEV;
228 }
229
config_buf(struct usb_configuration * config,enum usb_device_speed speed,void * buf,u8 type)230 static int config_buf(struct usb_configuration *config,
231 enum usb_device_speed speed, void *buf, u8 type)
232 {
233 struct usb_config_descriptor *c = buf;
234 void *next = buf + USB_DT_CONFIG_SIZE;
235 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
236 struct usb_function *f;
237 int status;
238 int interfaceCount = 0;
239
240 /* write the config descriptor */
241 c = buf;
242 c->bLength = USB_DT_CONFIG_SIZE;
243 c->bDescriptorType = type;
244 /* wTotalLength is written later */
245 c->bNumInterfaces = config->next_interface_id;
246 c->bConfigurationValue = config->bConfigurationValue;
247 c->iConfiguration = config->iConfiguration;
248 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
249 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
250
251 /* There may be e.g. OTG descriptors */
252 if (config->descriptors) {
253 status = usb_descriptor_fillbuf(next, len,
254 config->descriptors);
255 if (status < 0)
256 return status;
257 len -= status;
258 next += status;
259 }
260
261 /* add each function's descriptors */
262 list_for_each_entry(f, &config->functions, list) {
263 struct usb_descriptor_header **descriptors;
264
265 if (speed == USB_SPEED_HIGH)
266 descriptors = f->hs_descriptors;
267 else
268 descriptors = f->descriptors;
269 if (!descriptors || descriptors[0] == NULL) {
270 for (; f != config->interface[interfaceCount];) {
271 interfaceCount++;
272 c->bNumInterfaces--;
273 }
274 continue;
275 }
276 for (; f != config->interface[interfaceCount];)
277 interfaceCount++;
278
279 status = usb_descriptor_fillbuf(next, len,
280 (const struct usb_descriptor_header **) descriptors);
281 if (status < 0)
282 return status;
283 len -= status;
284 next += status;
285 }
286
287 len = next - buf;
288 c->wTotalLength = cpu_to_le16(len);
289 return len;
290 }
291
config_desc(struct usb_composite_dev * cdev,unsigned w_value)292 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
293 {
294 struct usb_gadget *gadget = cdev->gadget;
295 struct usb_configuration *c;
296 u8 type = w_value >> 8;
297 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
298
299 if (gadget_is_dualspeed(gadget)) {
300 int hs = 0;
301
302 if (gadget->speed == USB_SPEED_HIGH)
303 hs = 1;
304 if (type == USB_DT_OTHER_SPEED_CONFIG)
305 hs = !hs;
306 if (hs)
307 speed = USB_SPEED_HIGH;
308
309 }
310
311 /* This is a lookup by config *INDEX* */
312 w_value &= 0xff;
313 list_for_each_entry(c, &cdev->configs, list) {
314 /* ignore configs that won't work at this speed */
315 if (speed == USB_SPEED_HIGH) {
316 if (!c->highspeed)
317 continue;
318 } else {
319 if (!c->fullspeed)
320 continue;
321 }
322 if (w_value == 0)
323 return config_buf(c, speed, cdev->req->buf, type);
324 w_value--;
325 }
326 return -EINVAL;
327 }
328
count_configs(struct usb_composite_dev * cdev,unsigned type)329 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
330 {
331 struct usb_gadget *gadget = cdev->gadget;
332 struct usb_configuration *c;
333 unsigned count = 0;
334 int hs = 0;
335
336 if (gadget_is_dualspeed(gadget)) {
337 if (gadget->speed == USB_SPEED_HIGH)
338 hs = 1;
339 if (type == USB_DT_DEVICE_QUALIFIER)
340 hs = !hs;
341 }
342 list_for_each_entry(c, &cdev->configs, list) {
343 /* ignore configs that won't work at this speed */
344 if (hs) {
345 if (!c->highspeed)
346 continue;
347 } else {
348 if (!c->fullspeed)
349 continue;
350 }
351 count++;
352 }
353 return count;
354 }
355
device_qual(struct usb_composite_dev * cdev)356 static void device_qual(struct usb_composite_dev *cdev)
357 {
358 struct usb_qualifier_descriptor *qual = cdev->req->buf;
359
360 qual->bLength = sizeof(*qual);
361 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
362 /* POLICY: same bcdUSB and device type info at both speeds */
363 qual->bcdUSB = cdev->desc.bcdUSB;
364 qual->bDeviceClass = cdev->desc.bDeviceClass;
365 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
366 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
367 /* ASSUME same EP0 fifo size at both speeds */
368 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
369 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
370 qual->bRESERVED = 0;
371 }
372
373 /*-------------------------------------------------------------------------*/
374
reset_config(struct usb_composite_dev * cdev)375 static void reset_config(struct usb_composite_dev *cdev)
376 {
377 struct usb_function *f;
378
379 DBG(cdev, "reset config\n");
380
381 list_for_each_entry(f, &cdev->config->functions, list) {
382 if (f->disable)
383 f->disable(f);
384 }
385 cdev->config = NULL;
386 }
387
set_config(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl,unsigned number)388 static int set_config(struct usb_composite_dev *cdev,
389 const struct usb_ctrlrequest *ctrl, unsigned number)
390 {
391 struct usb_gadget *gadget = cdev->gadget;
392 struct usb_configuration *c = NULL;
393 int result = -EINVAL;
394 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
395 int tmp;
396
397 if (cdev->config)
398 reset_config(cdev);
399
400 if (number) {
401 list_for_each_entry(c, &cdev->configs, list) {
402 if (c->bConfigurationValue == number) {
403 result = 0;
404 break;
405 }
406 }
407 if (result < 0)
408 goto done;
409 } else
410 result = 0;
411
412 INFO(cdev, "%s speed config #%d: %s\n",
413 ({ char *speed;
414 switch (gadget->speed) {
415 case USB_SPEED_LOW: speed = "low"; break;
416 case USB_SPEED_FULL: speed = "full"; break;
417 case USB_SPEED_HIGH: speed = "high"; break;
418 default: speed = "?"; break;
419 } ; speed; }), number, c ? c->label : "unconfigured");
420
421 if (!c)
422 goto done;
423
424 cdev->config = c;
425
426 /* Initialize all interfaces by setting them to altsetting zero. */
427 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
428 struct usb_function *f = c->interface[tmp];
429
430 if (!f)
431 break;
432
433 result = f->set_alt(f, tmp, 0);
434 if (result < 0) {
435 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
436 tmp, f->name, f, result);
437
438 reset_config(cdev);
439 goto done;
440 }
441 }
442
443 /* when we return, be sure our power usage is valid */
444 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
445 done:
446 usb_gadget_vbus_draw(gadget, power);
447 return result;
448 }
449
450 /**
451 * usb_add_config() - add a configuration to a device.
452 * @cdev: wraps the USB gadget
453 * @config: the configuration, with bConfigurationValue assigned
454 * Context: single threaded during gadget setup
455 *
456 * One of the main tasks of a composite driver's bind() routine is to
457 * add each of the configurations it supports, using this routine.
458 *
459 * This function returns the value of the configuration's bind(), which
460 * is zero for success else a negative errno value. Binding configurations
461 * assigns global resources including string IDs, and per-configuration
462 * resources such as interface IDs and endpoints.
463 */
usb_add_config(struct usb_composite_dev * cdev,struct usb_configuration * config)464 int __init usb_add_config(struct usb_composite_dev *cdev,
465 struct usb_configuration *config)
466 {
467 int status = -EINVAL;
468 struct usb_configuration *c;
469
470 DBG(cdev, "adding config #%u '%s'/%p\n",
471 config->bConfigurationValue,
472 config->label, config);
473
474 if (!config->bConfigurationValue || !config->bind)
475 goto done;
476
477 /* Prevent duplicate configuration identifiers */
478 list_for_each_entry(c, &cdev->configs, list) {
479 if (c->bConfigurationValue == config->bConfigurationValue) {
480 status = -EBUSY;
481 goto done;
482 }
483 }
484
485 config->cdev = cdev;
486 list_add_tail(&config->list, &cdev->configs);
487
488 INIT_LIST_HEAD(&config->functions);
489 config->next_interface_id = 0;
490
491 status = config->bind(config);
492 if (status < 0) {
493 list_del(&config->list);
494 config->cdev = NULL;
495 } else {
496 unsigned i;
497
498 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
499 config->bConfigurationValue, config,
500 config->highspeed ? " high" : "",
501 config->fullspeed
502 ? (gadget_is_dualspeed(cdev->gadget)
503 ? " full"
504 : " full/low")
505 : "");
506
507 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
508 struct usb_function *f = config->interface[i];
509
510 if (!f)
511 continue;
512 DBG(cdev, " interface %d = %s/%p\n",
513 i, f->name, f);
514 }
515 }
516
517 /* set_alt(), or next config->bind(), sets up
518 * ep->driver_data as needed.
519 */
520 usb_ep_autoconfig_reset(cdev->gadget);
521
522 done:
523 if (status)
524 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
525 config->bConfigurationValue, status);
526 return status;
527 }
528
529 /*-------------------------------------------------------------------------*/
530
531 /* We support strings in multiple languages ... string descriptor zero
532 * says which languages are supported. The typical case will be that
533 * only one language (probably English) is used, with I18N handled on
534 * the host side.
535 */
536
collect_langs(struct usb_gadget_strings ** sp,__le16 * buf)537 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
538 {
539 const struct usb_gadget_strings *s;
540 u16 language;
541 __le16 *tmp;
542
543 while (*sp) {
544 s = *sp;
545 language = cpu_to_le16(s->language);
546 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
547 if (*tmp == language)
548 goto repeat;
549 }
550 *tmp++ = language;
551 repeat:
552 sp++;
553 }
554 }
555
lookup_string(struct usb_gadget_strings ** sp,void * buf,u16 language,int id)556 static int lookup_string(
557 struct usb_gadget_strings **sp,
558 void *buf,
559 u16 language,
560 int id
561 )
562 {
563 struct usb_gadget_strings *s;
564 int value;
565
566 while (*sp) {
567 s = *sp++;
568 if (s->language != language)
569 continue;
570 value = usb_gadget_get_string(s, id, buf);
571 if (value > 0)
572 return value;
573 }
574 return -EINVAL;
575 }
576
get_string(struct usb_composite_dev * cdev,void * buf,u16 language,int id)577 static int get_string(struct usb_composite_dev *cdev,
578 void *buf, u16 language, int id)
579 {
580 struct usb_configuration *c;
581 struct usb_function *f;
582 int len;
583
584 /* Yes, not only is USB's I18N support probably more than most
585 * folk will ever care about ... also, it's all supported here.
586 * (Except for UTF8 support for Unicode's "Astral Planes".)
587 */
588
589 /* 0 == report all available language codes */
590 if (id == 0) {
591 struct usb_string_descriptor *s = buf;
592 struct usb_gadget_strings **sp;
593
594 memset(s, 0, 256);
595 s->bDescriptorType = USB_DT_STRING;
596
597 sp = composite->strings;
598 if (sp)
599 collect_langs(sp, s->wData);
600
601 list_for_each_entry(c, &cdev->configs, list) {
602 sp = c->strings;
603 if (sp)
604 collect_langs(sp, s->wData);
605
606 list_for_each_entry(f, &c->functions, list) {
607 sp = f->strings;
608 if (sp)
609 collect_langs(sp, s->wData);
610 }
611 }
612
613 for (len = 0; s->wData[len] && len <= 126; len++)
614 continue;
615 if (!len)
616 return -EINVAL;
617
618 s->bLength = 2 * (len + 1);
619 return s->bLength;
620 }
621
622 /* Otherwise, look up and return a specified string. String IDs
623 * are device-scoped, so we look up each string table we're told
624 * about. These lookups are infrequent; simpler-is-better here.
625 */
626 if (composite->strings) {
627 len = lookup_string(composite->strings, buf, language, id);
628 if (len > 0)
629 return len;
630 }
631 list_for_each_entry(c, &cdev->configs, list) {
632 if (c->strings) {
633 len = lookup_string(c->strings, buf, language, id);
634 if (len > 0)
635 return len;
636 }
637 list_for_each_entry(f, &c->functions, list) {
638 if (!f->strings)
639 continue;
640 len = lookup_string(f->strings, buf, language, id);
641 if (len > 0)
642 return len;
643 }
644 }
645 return -EINVAL;
646 }
647
648 /**
649 * usb_string_id() - allocate an unused string ID
650 * @cdev: the device whose string descriptor IDs are being allocated
651 * Context: single threaded during gadget setup
652 *
653 * @usb_string_id() is called from bind() callbacks to allocate
654 * string IDs. Drivers for functions, configurations, or gadgets will
655 * then store that ID in the appropriate descriptors and string table.
656 *
657 * All string identifier should be allocated using this routine, to
658 * ensure that for example different functions don't wrongly assign
659 * different meanings to the same identifier.
660 */
usb_string_id(struct usb_composite_dev * cdev)661 int __init usb_string_id(struct usb_composite_dev *cdev)
662 {
663 if (cdev->next_string_id < 254) {
664 /* string id 0 is reserved */
665 cdev->next_string_id++;
666 return cdev->next_string_id;
667 }
668 return -ENODEV;
669 }
670
671 /*-------------------------------------------------------------------------*/
672
composite_setup_complete(struct usb_ep * ep,struct usb_request * req)673 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
674 {
675 if (req->status || req->actual != req->length)
676 DBG((struct usb_composite_dev *) ep->driver_data,
677 "setup complete --> %d, %d/%d\n",
678 req->status, req->actual, req->length);
679 }
680
681 /*
682 * The setup() callback implements all the ep0 functionality that's
683 * not handled lower down, in hardware or the hardware driver(like
684 * device and endpoint feature flags, and their status). It's all
685 * housekeeping for the gadget function we're implementing. Most of
686 * the work is in config and function specific setup.
687 */
688 static int
composite_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)689 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
690 {
691 struct usb_composite_dev *cdev = get_gadget_data(gadget);
692 struct usb_request *req = cdev->req;
693 int value = -EOPNOTSUPP;
694 u16 w_index = le16_to_cpu(ctrl->wIndex);
695 u8 intf = w_index & 0xFF;
696 u16 w_value = le16_to_cpu(ctrl->wValue);
697 u16 w_length = le16_to_cpu(ctrl->wLength);
698 struct usb_function *f = NULL;
699
700 /* partial re-init of the response message; the function or the
701 * gadget might need to intercept e.g. a control-OUT completion
702 * when we delegate to it.
703 */
704 req->zero = 0;
705 req->complete = composite_setup_complete;
706 req->length = USB_BUFSIZ;
707 gadget->ep0->driver_data = cdev;
708
709 switch (ctrl->bRequest) {
710
711 /* we handle all standard USB descriptors */
712 case USB_REQ_GET_DESCRIPTOR:
713 if (ctrl->bRequestType != USB_DIR_IN)
714 goto unknown;
715 switch (w_value >> 8) {
716
717 case USB_DT_DEVICE:
718 cdev->desc.bNumConfigurations =
719 count_configs(cdev, USB_DT_DEVICE);
720 value = min(w_length, (u16) sizeof cdev->desc);
721 memcpy(req->buf, &cdev->desc, value);
722 break;
723 case USB_DT_DEVICE_QUALIFIER:
724 if (!gadget_is_dualspeed(gadget))
725 break;
726 device_qual(cdev);
727 value = min_t(int, w_length,
728 sizeof(struct usb_qualifier_descriptor));
729 break;
730 case USB_DT_OTHER_SPEED_CONFIG:
731 if (!gadget_is_dualspeed(gadget))
732 break;
733 /* FALLTHROUGH */
734 case USB_DT_CONFIG:
735 value = config_desc(cdev, w_value);
736 if (value >= 0)
737 value = min(w_length, (u16) value);
738 break;
739 case USB_DT_STRING:
740 value = get_string(cdev, req->buf,
741 w_index, w_value & 0xff);
742 if (value >= 0)
743 value = min(w_length, (u16) value);
744 break;
745 }
746 break;
747
748 /* any number of configs can work */
749 case USB_REQ_SET_CONFIGURATION:
750 if (ctrl->bRequestType != 0)
751 goto unknown;
752 if (gadget_is_otg(gadget)) {
753 if (gadget->a_hnp_support)
754 DBG(cdev, "HNP available\n");
755 else if (gadget->a_alt_hnp_support)
756 DBG(cdev, "HNP on another port\n");
757 else
758 VDBG(cdev, "HNP inactive\n");
759 }
760 spin_lock(&cdev->lock);
761 value = set_config(cdev, ctrl, w_value);
762 spin_unlock(&cdev->lock);
763 break;
764 case USB_REQ_GET_CONFIGURATION:
765 if (ctrl->bRequestType != USB_DIR_IN)
766 goto unknown;
767 if (cdev->config) {
768 *(u8 *)req->buf = cdev->config->bConfigurationValue;
769 value = min(w_length, (u16) 1);
770 } else
771 *(u8 *)req->buf = 0;
772 break;
773
774 /* function drivers must handle get/set altsetting; if there's
775 * no get() method, we know only altsetting zero works.
776 */
777 case USB_REQ_SET_INTERFACE:
778 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
779 goto unknown;
780 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
781 break;
782 f = cdev->config->interface[intf];
783 if (!f)
784 break;
785 if (w_value && !f->set_alt)
786 break;
787 value = f->set_alt(f, w_index, w_value);
788 break;
789 case USB_REQ_GET_INTERFACE:
790 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
791 goto unknown;
792 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
793 break;
794 f = cdev->config->interface[intf];
795 if (!f)
796 break;
797 /* lots of interfaces only need altsetting zero... */
798 value = f->get_alt ? f->get_alt(f, w_index) : 0;
799 if (value < 0)
800 break;
801 *((u8 *)req->buf) = value;
802 value = min(w_length, (u16) 1);
803 break;
804 default:
805 unknown:
806 VDBG(cdev,
807 "non-core control req%02x.%02x v%04x i%04x l%d\n",
808 ctrl->bRequestType, ctrl->bRequest,
809 w_value, w_index, w_length);
810
811 /* functions always handle their interfaces ... punt other
812 * recipients (endpoint, other, WUSB, ...) to the current
813 * configuration code.
814 *
815 * REVISIT it could make sense to let the composite device
816 * take such requests too, if that's ever needed: to work
817 * in config 0, etc.
818 */
819 if ((ctrl->bRequestType & USB_RECIP_MASK)
820 == USB_RECIP_INTERFACE) {
821 if (cdev->config == NULL)
822 return value;
823
824 f = cdev->config->interface[intf];
825 if (f && f->setup)
826 value = f->setup(f, ctrl);
827 else
828 f = NULL;
829 }
830 if (value < 0 && !f) {
831 struct usb_configuration *c;
832
833 c = cdev->config;
834 if (c && c->setup)
835 value = c->setup(c, ctrl);
836 }
837
838 goto done;
839 }
840
841 /* respond with data transfer before status phase? */
842 if (value >= 0) {
843 req->length = value;
844 req->zero = value < w_length;
845 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
846 if (value < 0) {
847 DBG(cdev, "ep_queue --> %d\n", value);
848 req->status = 0;
849 composite_setup_complete(gadget->ep0, req);
850 }
851 }
852
853 done:
854 /* device either stalls (value < 0) or reports success */
855 return value;
856 }
857
composite_disconnect(struct usb_gadget * gadget)858 static void composite_disconnect(struct usb_gadget *gadget)
859 {
860 struct usb_composite_dev *cdev = get_gadget_data(gadget);
861 unsigned long flags;
862
863 /* REVISIT: should we have config and device level
864 * disconnect callbacks?
865 */
866 spin_lock_irqsave(&cdev->lock, flags);
867 if (cdev->config)
868 reset_config(cdev);
869 spin_unlock_irqrestore(&cdev->lock, flags);
870 }
871
872 /*-------------------------------------------------------------------------*/
873
874 static void /* __init_or_exit */
composite_unbind(struct usb_gadget * gadget)875 composite_unbind(struct usb_gadget *gadget)
876 {
877 struct usb_composite_dev *cdev = get_gadget_data(gadget);
878
879 /* composite_disconnect() must already have been called
880 * by the underlying peripheral controller driver!
881 * so there's no i/o concurrency that could affect the
882 * state protected by cdev->lock.
883 */
884 WARN_ON(cdev->config);
885
886 while (!list_empty(&cdev->configs)) {
887 struct usb_configuration *c;
888
889 c = list_first_entry(&cdev->configs,
890 struct usb_configuration, list);
891 while (!list_empty(&c->functions)) {
892 struct usb_function *f;
893
894 f = list_first_entry(&c->functions,
895 struct usb_function, list);
896 list_del(&f->list);
897 if (f->unbind) {
898 DBG(cdev, "unbind function '%s'/%p\n",
899 f->name, f);
900 f->unbind(c, f);
901 /* may free memory for "f" */
902 }
903 }
904 list_del(&c->list);
905 if (c->unbind) {
906 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
907 c->unbind(c);
908 /* may free memory for "c" */
909 }
910 }
911 if (composite->unbind)
912 composite->unbind(cdev);
913
914 if (cdev->req) {
915 kfree(cdev->req->buf);
916 usb_ep_free_request(gadget->ep0, cdev->req);
917 }
918 kfree(cdev);
919 set_gadget_data(gadget, NULL);
920 composite = NULL;
921 }
922
923 static void __init
string_override_one(struct usb_gadget_strings * tab,u8 id,const char * s)924 string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
925 {
926 struct usb_string *str = tab->strings;
927
928 for (str = tab->strings; str->s; str++) {
929 if (str->id == id) {
930 str->s = s;
931 return;
932 }
933 }
934 }
935
936 static void __init
string_override(struct usb_gadget_strings ** tab,u8 id,const char * s)937 string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
938 {
939 while (*tab) {
940 string_override_one(*tab, id, s);
941 tab++;
942 }
943 }
944
composite_bind(struct usb_gadget * gadget)945 static int __init composite_bind(struct usb_gadget *gadget)
946 {
947 struct usb_composite_dev *cdev;
948 int status = -ENOMEM;
949
950 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
951 if (!cdev)
952 return status;
953
954 spin_lock_init(&cdev->lock);
955 cdev->gadget = gadget;
956 set_gadget_data(gadget, cdev);
957 INIT_LIST_HEAD(&cdev->configs);
958
959 /* preallocate control response and buffer */
960 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
961 if (!cdev->req)
962 goto fail;
963 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
964 if (!cdev->req->buf)
965 goto fail;
966 cdev->req->complete = composite_setup_complete;
967 gadget->ep0->driver_data = cdev;
968
969 cdev->bufsiz = USB_BUFSIZ;
970 cdev->driver = composite;
971
972 usb_gadget_set_selfpowered(gadget);
973
974 /* interface and string IDs start at zero via kzalloc.
975 * we force endpoints to start unassigned; few controller
976 * drivers will zero ep->driver_data.
977 */
978 usb_ep_autoconfig_reset(cdev->gadget);
979
980 /* composite gadget needs to assign strings for whole device (like
981 * serial number), register function drivers, potentially update
982 * power state and consumption, etc
983 */
984 status = composite->bind(cdev);
985 if (status < 0)
986 goto fail;
987
988 cdev->desc = *composite->dev;
989 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
990
991 /* standardized runtime overrides for device ID data */
992 if (idVendor)
993 cdev->desc.idVendor = cpu_to_le16(idVendor);
994 if (idProduct)
995 cdev->desc.idProduct = cpu_to_le16(idProduct);
996 if (bcdDevice)
997 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
998
999 /* strings can't be assigned before bind() allocates the
1000 * releavnt identifiers
1001 */
1002 if (cdev->desc.iManufacturer && iManufacturer)
1003 string_override(composite->strings,
1004 cdev->desc.iManufacturer, iManufacturer);
1005 if (cdev->desc.iProduct && iProduct)
1006 string_override(composite->strings,
1007 cdev->desc.iProduct, iProduct);
1008 if (cdev->desc.iSerialNumber && iSerialNumber)
1009 string_override(composite->strings,
1010 cdev->desc.iSerialNumber, iSerialNumber);
1011
1012 INFO(cdev, "%s ready\n", composite->name);
1013 return 0;
1014
1015 fail:
1016 composite_unbind(gadget);
1017 return status;
1018 }
1019
1020 /*-------------------------------------------------------------------------*/
1021
1022 static void
composite_suspend(struct usb_gadget * gadget)1023 composite_suspend(struct usb_gadget *gadget)
1024 {
1025 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1026 struct usb_function *f;
1027
1028 /* REVISIT: should we have config and device level
1029 * suspend/resume callbacks?
1030 */
1031 DBG(cdev, "suspend\n");
1032 if (cdev->config) {
1033 list_for_each_entry(f, &cdev->config->functions, list) {
1034 if (f->suspend)
1035 f->suspend(f);
1036 }
1037 }
1038 }
1039
1040 static void
composite_resume(struct usb_gadget * gadget)1041 composite_resume(struct usb_gadget *gadget)
1042 {
1043 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1044 struct usb_function *f;
1045
1046 /* REVISIT: should we have config and device level
1047 * suspend/resume callbacks?
1048 */
1049 DBG(cdev, "resume\n");
1050 if (cdev->config) {
1051 list_for_each_entry(f, &cdev->config->functions, list) {
1052 if (f->resume)
1053 f->resume(f);
1054 }
1055 }
1056 }
1057
1058 /*-------------------------------------------------------------------------*/
1059
1060 static struct usb_gadget_driver composite_driver = {
1061 .speed = USB_SPEED_HIGH,
1062
1063 .bind = composite_bind,
1064 .unbind = __exit_p(composite_unbind),
1065
1066 .setup = composite_setup,
1067 .disconnect = composite_disconnect,
1068
1069 .suspend = composite_suspend,
1070 .resume = composite_resume,
1071
1072 .driver = {
1073 .owner = THIS_MODULE,
1074 },
1075 };
1076
1077 /**
1078 * usb_composite_register() - register a composite driver
1079 * @driver: the driver to register
1080 * Context: single threaded during gadget setup
1081 *
1082 * This function is used to register drivers using the composite driver
1083 * framework. The return value is zero, or a negative errno value.
1084 * Those values normally come from the driver's @bind method, which does
1085 * all the work of setting up the driver to match the hardware.
1086 *
1087 * On successful return, the gadget is ready to respond to requests from
1088 * the host, unless one of its components invokes usb_gadget_disconnect()
1089 * while it was binding. That would usually be done in order to wait for
1090 * some userspace participation.
1091 */
usb_composite_register(struct usb_composite_driver * driver)1092 int __init usb_composite_register(struct usb_composite_driver *driver)
1093 {
1094 if (!driver || !driver->dev || !driver->bind || composite)
1095 return -EINVAL;
1096
1097 if (!driver->name)
1098 driver->name = "composite";
1099 composite_driver.function = (char *) driver->name;
1100 composite_driver.driver.name = driver->name;
1101 composite = driver;
1102
1103 return usb_gadget_register_driver(&composite_driver);
1104 }
1105
1106 /**
1107 * usb_composite_unregister() - unregister a composite driver
1108 * @driver: the driver to unregister
1109 *
1110 * This function is used to unregister drivers using the composite
1111 * driver framework.
1112 */
usb_composite_unregister(struct usb_composite_driver * driver)1113 void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1114 {
1115 if (composite != driver)
1116 return;
1117 usb_gadget_unregister_driver(&composite_driver);
1118 }
1119