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