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