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