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