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 #ifndef CONFIG_SUPPORT_USBPLUG
842 if (!strncmp(cdev->driver->name,
843 "rkusb_ums_dnl", 13))
844 cdev->desc.bcdUSB = cpu_to_le16(0x0301);
845 else
846 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
847 #else
848 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
849 #endif
850 cdev->desc.bMaxPacketSize0 = 9;
851 } else {
852 cdev->desc.bMaxPacketSize0 =
853 cdev->gadget->ep0->maxpacket;
854 }
855
856 value = min(w_length, (u16) sizeof cdev->desc);
857 memcpy(req->buf, &cdev->desc, value);
858 break;
859 case USB_DT_DEVICE_QUALIFIER:
860 if (!gadget_is_dualspeed(gadget))
861 break;
862 device_qual(cdev);
863 value = min_t(int, w_length,
864 sizeof(struct usb_qualifier_descriptor));
865 break;
866 case USB_DT_OTHER_SPEED_CONFIG:
867 if (!gadget_is_dualspeed(gadget))
868 break;
869
870 case USB_DT_CONFIG:
871 value = config_desc(cdev, w_value);
872 if (value >= 0)
873 value = min(w_length, (u16) value);
874 break;
875 case USB_DT_STRING:
876 value = get_string(cdev, req->buf,
877 w_index, w_value & 0xff);
878 if (value >= 0)
879 value = min(w_length, (u16) value);
880 break;
881 case USB_DT_BOS:
882 /* HACK: only for rockusb command.
883 * Rockchip upgrade tool use bcdUSB (0x0201) field
884 * distinguishing maskrom or loader device at present.
885 * Unfortunately, it conflict with Windows 8 and beyond
886 * which request BOS descriptor in this case that bcdUSB
887 * is set to 0x0201.
888 */
889 if (gadget_is_superspeed(gadget) ||
890 !strncmp(cdev->driver->name, "rkusb_ums_dnl", 13)) {
891 value = bos_desc(cdev);
892 value = min(w_length, (u16) value);
893 }
894
895 /*
896 * The USB compliance test (USB 2.0 Command Verifier)
897 * issues this request. We should not run into the
898 * default path here. But return for now until
899 * the superspeed support is added.
900 */
901 break;
902 default:
903 goto unknown;
904 }
905 break;
906
907 /* any number of configs can work */
908 case USB_REQ_SET_CONFIGURATION:
909 if (ctrl->bRequestType != 0)
910 goto unknown;
911 if (gadget_is_otg(gadget)) {
912 if (gadget->a_hnp_support)
913 debug("HNP available\n");
914 else if (gadget->a_alt_hnp_support)
915 debug("HNP on another port\n");
916 else
917 debug("HNP inactive\n");
918 }
919
920 value = set_config(cdev, ctrl, w_value);
921 break;
922 case USB_REQ_GET_CONFIGURATION:
923 if (ctrl->bRequestType != USB_DIR_IN)
924 goto unknown;
925 if (cdev->config)
926 *(u8 *)req->buf = cdev->config->bConfigurationValue;
927 else
928 *(u8 *)req->buf = 0;
929 value = min(w_length, (u16) 1);
930 break;
931
932 /*
933 * function drivers must handle get/set altsetting; if there's
934 * no get() method, we know only altsetting zero works.
935 */
936 case USB_REQ_SET_INTERFACE:
937 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
938 goto unknown;
939 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
940 break;
941 f = cdev->config->interface[intf];
942 if (!f)
943 break;
944 if (w_value && !f->set_alt)
945 break;
946 value = f->set_alt(f, w_index, w_value);
947 break;
948 case USB_REQ_GET_INTERFACE:
949 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
950 goto unknown;
951 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
952 break;
953 f = cdev->config->interface[intf];
954 if (!f)
955 break;
956 /* lots of interfaces only need altsetting zero... */
957 value = f->get_alt ? f->get_alt(f, w_index) : 0;
958 if (value < 0)
959 break;
960 *((u8 *)req->buf) = value;
961 value = min(w_length, (u16) 1);
962 break;
963 default:
964 unknown:
965 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
966 ctrl->bRequestType, ctrl->bRequest,
967 w_value, w_index, w_length);
968
969 if (!cdev->config)
970 goto done;
971
972 /*
973 * functions always handle their interfaces and endpoints...
974 * punt other recipients (other, WUSB, ...) to the current
975 * configuration code.
976 */
977 switch (ctrl->bRequestType & USB_RECIP_MASK) {
978 case USB_RECIP_INTERFACE:
979 if (!cdev->config)
980 break;
981 f = cdev->config->interface[intf];
982 break;
983
984 case USB_RECIP_ENDPOINT:
985 if (!cdev->config)
986 break;
987 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
988 list_for_each_entry(f, &cdev->config->functions, list) {
989 if (test_bit(endp, f->endpoints))
990 break;
991 }
992 if (&f->list == &cdev->config->functions)
993 f = NULL;
994 break;
995 /*
996 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
997 * for non-standard request (w_value = 0x21,
998 * bRequest = GET_DESCRIPTOR in this case).
999 * When only one interface is registered (as it is done now),
1000 * then this request shall be handled as it was requested for
1001 * interface.
1002 *
1003 * In the below code it is checked if only one interface is
1004 * present and proper function for it is extracted. Due to that
1005 * function's setup (f->setup) is called to handle this
1006 * special non-standard request.
1007 */
1008 case USB_RECIP_DEVICE:
1009 if (cdev->config) {
1010 debug("cdev->config->next_interface_id: %d intf: %d\n",
1011 cdev->config->next_interface_id, intf);
1012 if (cdev->config->next_interface_id == 1)
1013 f = cdev->config->interface[intf];
1014 }
1015 break;
1016 }
1017
1018 if (f && f->setup)
1019 value = f->setup(f, ctrl);
1020 else {
1021 c = cdev->config;
1022 if (c->setup)
1023 value = c->setup(c, ctrl);
1024 }
1025
1026 goto done;
1027 }
1028
1029 /* respond with data transfer before status phase? */
1030 if (value >= 0) {
1031 req->length = value;
1032 req->zero = value < w_length;
1033 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1034 if (value < 0) {
1035 debug("ep_queue --> %d\n", value);
1036 req->status = 0;
1037 composite_setup_complete(gadget->ep0, req);
1038 }
1039 }
1040
1041 done:
1042 /* device either stalls (value < 0) or reports success */
1043 return value;
1044 }
1045
composite_disconnect(struct usb_gadget * gadget)1046 static void composite_disconnect(struct usb_gadget *gadget)
1047 {
1048 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1049
1050 if (cdev->config)
1051 reset_config(cdev);
1052 if (composite->disconnect)
1053 composite->disconnect(cdev);
1054 }
1055
composite_unbind(struct usb_gadget * gadget)1056 static void composite_unbind(struct usb_gadget *gadget)
1057 {
1058 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1059 struct usb_configuration *c;
1060 struct usb_function *f;
1061
1062 /*
1063 * composite_disconnect() must already have been called
1064 * by the underlying peripheral controller driver!
1065 * so there's no i/o concurrency that could affect the
1066 * state protected by cdev->lock.
1067 */
1068 BUG_ON(cdev->config);
1069
1070 while (!list_empty(&cdev->configs)) {
1071 c = list_first_entry(&cdev->configs,
1072 struct usb_configuration, list);
1073 while (!list_empty(&c->functions)) {
1074 f = list_first_entry(&c->functions,
1075 struct usb_function, list);
1076 list_del(&f->list);
1077 if (f->unbind) {
1078 debug("unbind function '%s'/%p\n",
1079 f->name, f);
1080 f->unbind(c, f);
1081 }
1082 }
1083 list_del(&c->list);
1084 if (c->unbind) {
1085 debug("unbind config '%s'/%p\n", c->label, c);
1086 c->unbind(c);
1087 }
1088 free(c);
1089 }
1090 if (composite->unbind)
1091 composite->unbind(cdev);
1092
1093 if (cdev->req) {
1094 kfree(cdev->req->buf);
1095 usb_ep_free_request(gadget->ep0, cdev->req);
1096 }
1097 kfree(cdev);
1098 set_gadget_data(gadget, NULL);
1099
1100 composite = NULL;
1101 }
1102
composite_bind(struct usb_gadget * gadget)1103 static int composite_bind(struct usb_gadget *gadget)
1104 {
1105 int status = -ENOMEM;
1106 struct usb_composite_dev *cdev;
1107
1108 cdev = calloc(sizeof *cdev, 1);
1109 if (!cdev)
1110 return status;
1111
1112 cdev->gadget = gadget;
1113 set_gadget_data(gadget, cdev);
1114 INIT_LIST_HEAD(&cdev->configs);
1115
1116 /* preallocate control response and buffer */
1117 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1118 if (!cdev->req)
1119 goto fail;
1120 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1121 if (!cdev->req->buf)
1122 goto fail;
1123 cdev->req->complete = composite_setup_complete;
1124 gadget->ep0->driver_data = cdev;
1125
1126 cdev->bufsiz = USB_BUFSIZ;
1127 cdev->driver = composite;
1128
1129 usb_gadget_set_selfpowered(gadget);
1130 usb_ep_autoconfig_reset(cdev->gadget);
1131
1132 status = composite->bind(cdev);
1133 if (status < 0)
1134 goto fail;
1135
1136 memcpy(&cdev->desc, composite->dev,
1137 sizeof(struct usb_device_descriptor));
1138 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1139
1140 debug("%s: ready\n", composite->name);
1141 return 0;
1142
1143 fail:
1144 composite_unbind(gadget);
1145 return status;
1146 }
1147
1148 static void
composite_suspend(struct usb_gadget * gadget)1149 composite_suspend(struct usb_gadget *gadget)
1150 {
1151 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1152 struct usb_function *f;
1153
1154 debug("%s: suspend\n", __func__);
1155 if (cdev->config) {
1156 list_for_each_entry(f, &cdev->config->functions, list) {
1157 if (f->suspend)
1158 f->suspend(f);
1159 }
1160 }
1161 if (composite->suspend)
1162 composite->suspend(cdev);
1163
1164 cdev->suspended = 1;
1165 }
1166
1167 static void
composite_resume(struct usb_gadget * gadget)1168 composite_resume(struct usb_gadget *gadget)
1169 {
1170 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1171 struct usb_function *f;
1172
1173 debug("%s: resume\n", __func__);
1174 if (composite->resume)
1175 composite->resume(cdev);
1176 if (cdev->config) {
1177 list_for_each_entry(f, &cdev->config->functions, list) {
1178 if (f->resume)
1179 f->resume(f);
1180 }
1181 }
1182
1183 cdev->suspended = 0;
1184 }
1185
1186 static struct usb_gadget_driver composite_driver = {
1187 .speed = USB_SPEED_HIGH,
1188
1189 .bind = composite_bind,
1190 .unbind = composite_unbind,
1191
1192 .setup = composite_setup,
1193 .reset = composite_disconnect,
1194 .disconnect = composite_disconnect,
1195
1196 .suspend = composite_suspend,
1197 .resume = composite_resume,
1198 };
1199
1200 /**
1201 * usb_composite_register() - register a composite driver
1202 * @driver: the driver to register
1203 * Context: single threaded during gadget setup
1204 *
1205 * This function is used to register drivers using the composite driver
1206 * framework. The return value is zero, or a negative errno value.
1207 * Those values normally come from the driver's @bind method, which does
1208 * all the work of setting up the driver to match the hardware.
1209 *
1210 * On successful return, the gadget is ready to respond to requests from
1211 * the host, unless one of its components invokes usb_gadget_disconnect()
1212 * while it was binding. That would usually be done in order to wait for
1213 * some userspace participation.
1214 */
usb_composite_register(struct usb_composite_driver * driver)1215 int usb_composite_register(struct usb_composite_driver *driver)
1216 {
1217 int res;
1218
1219 if (!driver || !driver->dev || !driver->bind || composite)
1220 return -EINVAL;
1221
1222 if (!driver->name)
1223 driver->name = "composite";
1224 composite = driver;
1225
1226 res = usb_gadget_register_driver(&composite_driver);
1227 if (res != 0)
1228 composite = NULL;
1229
1230 return res;
1231 }
1232
1233 /**
1234 * usb_composite_unregister() - unregister a composite driver
1235 * @driver: the driver to unregister
1236 *
1237 * This function is used to unregister drivers using the composite
1238 * driver framework.
1239 */
usb_composite_unregister(struct usb_composite_driver * driver)1240 void usb_composite_unregister(struct usb_composite_driver *driver)
1241 {
1242 if (composite != driver)
1243 return;
1244 usb_gadget_unregister_driver(&composite_driver);
1245 composite = NULL;
1246 }
1247