blob: a0c28dbe598f1ee00374673498ab646f0497999c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02002/*
3 * composite.c - infrastructure for Composite USB Gadgets
4 *
5 * Copyright (C) 2006-2008 David Brownell
Bin Menga1875592016-02-05 19:30:11 -08006 * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02007 */
8#undef DEBUG
9
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070011#include <dm/devres.h>
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020012#include <linux/bitops.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060013#include <linux/bug.h>
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020014#include <linux/usb/composite.h>
Li Juna764c942021-01-25 21:43:49 +080015#include "u_os_desc.h"
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020016
17#define USB_BUFSIZ 4096
18
Simon Goldschmidt616ebd82019-11-21 22:15:22 +010019/* Helper type for accessing packed u16 pointers */
20typedef struct { __le16 val; } __packed __le16_packed;
21
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020022static struct usb_composite_driver *composite;
23
Simon Goldschmidt616ebd82019-11-21 22:15:22 +010024static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
25{
26 var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
27}
28
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020029/**
Li Jun1c7aacb2021-01-25 21:43:46 +080030 * struct usb_os_string - represents OS String to be reported by a gadget
31 * @bLength: total length of the entire descritor, always 0x12
32 * @bDescriptorType: USB_DT_STRING
33 * @qwSignature: the OS String proper
34 * @bMS_VendorCode: code used by the host for subsequent requests
35 * @bPad: not used, must be zero
36 */
37struct usb_os_string {
38 __u8 bLength;
39 __u8 bDescriptorType;
40 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
41 __u8 bMS_VendorCode;
42 __u8 bPad;
43} __packed;
44
45/**
Lukasz Majewski7010f5b2012-05-02 17:47:02 +020046 * usb_add_function() - add a function to a configuration
47 * @config: the configuration
48 * @function: the function being added
49 * Context: single threaded during gadget setup
50 *
51 * After initialization, each configuration must have one or more
52 * functions added to it. Adding a function involves calling its @bind()
53 * method to allocate resources such as interface and string identifiers
54 * and endpoints.
55 *
56 * This function returns the value of the function's bind(), which is
57 * zero for success else a negative errno value.
58 */
59int usb_add_function(struct usb_configuration *config,
60 struct usb_function *function)
61{
62 int value = -EINVAL;
63
64 debug("adding '%s'/%p to config '%s'/%p\n",
65 function->name, function,
66 config->label, config);
67
68 if (!function->set_alt || !function->disable)
69 goto done;
70
71 function->config = config;
72 list_add_tail(&function->list, &config->functions);
73
74 if (function->bind) {
75 value = function->bind(config, function);
76 if (value < 0) {
77 list_del(&function->list);
78 function->config = NULL;
79 }
80 } else
81 value = 0;
82
83 if (!config->fullspeed && function->descriptors)
84 config->fullspeed = 1;
85 if (!config->highspeed && function->hs_descriptors)
86 config->highspeed = 1;
87
88done:
89 if (value)
90 debug("adding '%s'/%p --> %d\n",
91 function->name, function, value);
92 return value;
93}
94
95/**
96 * usb_function_deactivate - prevent function and gadget enumeration
97 * @function: the function that isn't yet ready to respond
98 *
99 * Blocks response of the gadget driver to host enumeration by
100 * preventing the data line pullup from being activated. This is
101 * normally called during @bind() processing to change from the
102 * initial "ready to respond" state, or when a required resource
103 * becomes available.
104 *
105 * For example, drivers that serve as a passthrough to a userspace
106 * daemon can block enumeration unless that daemon (such as an OBEX,
107 * MTP, or print server) is ready to handle host requests.
108 *
109 * Not all systems support software control of their USB peripheral
110 * data pullups.
111 *
112 * Returns zero on success, else negative errno.
113 */
114int usb_function_deactivate(struct usb_function *function)
115{
116 struct usb_composite_dev *cdev = function->config->cdev;
117 int status = 0;
118
119 if (cdev->deactivations == 0)
120 status = usb_gadget_disconnect(cdev->gadget);
121 if (status == 0)
122 cdev->deactivations++;
123
124 return status;
125}
126
127/**
128 * usb_function_activate - allow function and gadget enumeration
129 * @function: function on which usb_function_activate() was called
130 *
131 * Reverses effect of usb_function_deactivate(). If no more functions
132 * are delaying their activation, the gadget driver will respond to
133 * host enumeration procedures.
134 *
135 * Returns zero on success, else negative errno.
136 */
137int usb_function_activate(struct usb_function *function)
138{
139 struct usb_composite_dev *cdev = function->config->cdev;
140 int status = 0;
141
142 if (cdev->deactivations == 0)
143 status = -EINVAL;
144 else {
145 cdev->deactivations--;
146 if (cdev->deactivations == 0)
147 status = usb_gadget_connect(cdev->gadget);
148 }
149
150 return status;
151}
152
153/**
154 * usb_interface_id() - allocate an unused interface ID
155 * @config: configuration associated with the interface
156 * @function: function handling the interface
157 * Context: single threaded during gadget setup
158 *
159 * usb_interface_id() is called from usb_function.bind() callbacks to
160 * allocate new interface IDs. The function driver will then store that
161 * ID in interface, association, CDC union, and other descriptors. It
162 * will also handle any control requests targetted at that interface,
163 * particularly changing its altsetting via set_alt(). There may
164 * also be class-specific or vendor-specific requests to handle.
165 *
166 * All interface identifier should be allocated using this routine, to
167 * ensure that for example different functions don't wrongly assign
168 * different meanings to the same identifier. Note that since interface
169 * identifers are configuration-specific, functions used in more than
170 * one configuration (or more than once in a given configuration) need
171 * multiple versions of the relevant descriptors.
172 *
173 * Returns the interface ID which was allocated; or -ENODEV if no
174 * more interface IDs can be allocated.
175 */
176int usb_interface_id(struct usb_configuration *config,
177 struct usb_function *function)
178{
179 unsigned char id = config->next_interface_id;
180
181 if (id < MAX_CONFIG_INTERFACES) {
182 config->interface[id] = function;
183 config->next_interface_id = id + 1;
184 return id;
185 }
186 return -ENODEV;
187}
188
189static int config_buf(struct usb_configuration *config,
190 enum usb_device_speed speed, void *buf, u8 type)
191{
192 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
193 void *next = buf + USB_DT_CONFIG_SIZE;
194 struct usb_descriptor_header **descriptors;
Heinrich Schuchardtfa9da8e2018-03-18 13:05:58 +0100195 struct usb_config_descriptor *c;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200196 int status;
197 struct usb_function *f;
198
199 /* write the config descriptor */
200 c = buf;
201 c->bLength = USB_DT_CONFIG_SIZE;
202 c->bDescriptorType = type;
203
204 c->bNumInterfaces = config->next_interface_id;
205 c->bConfigurationValue = config->bConfigurationValue;
206 c->iConfiguration = config->iConfiguration;
207 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
208 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
209
210 /* There may be e.g. OTG descriptors */
211 if (config->descriptors) {
212 status = usb_descriptor_fillbuf(next, len,
213 config->descriptors);
214 if (status < 0)
215 return status;
216 len -= status;
217 next += status;
218 }
219
220 /* add each function's descriptors */
221 list_for_each_entry(f, &config->functions, list) {
222 if (speed == USB_SPEED_HIGH)
223 descriptors = f->hs_descriptors;
224 else
225 descriptors = f->descriptors;
226 if (!descriptors)
227 continue;
228 status = usb_descriptor_fillbuf(next, len,
229 (const struct usb_descriptor_header **) descriptors);
230 if (status < 0)
231 return status;
232 len -= status;
233 next += status;
234 }
235
236 len = next - buf;
237 c->wTotalLength = cpu_to_le16(len);
238 return len;
239}
240
241static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
242{
243 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
244 struct usb_gadget *gadget = cdev->gadget;
245 u8 type = w_value >> 8;
246 int hs = 0;
247 struct usb_configuration *c;
Li Juna764c942021-01-25 21:43:49 +0800248 struct list_head *pos;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200249
250 if (gadget_is_dualspeed(gadget)) {
251 if (gadget->speed == USB_SPEED_HIGH)
252 hs = 1;
253 if (type == USB_DT_OTHER_SPEED_CONFIG)
254 hs = !hs;
255 if (hs)
256 speed = USB_SPEED_HIGH;
257 }
258
259 w_value &= 0xff;
Li Juna764c942021-01-25 21:43:49 +0800260
261 pos = &cdev->configs;
262 c = cdev->os_desc_config;
263 if (c)
264 goto check_config;
265
266 while ((pos = pos->next) != &cdev->configs) {
267 c = list_entry(pos, typeof(*c), list);
268
269 /* skip OS Descriptors config which is handled separately */
270 if (c == cdev->os_desc_config)
271 continue;
272
273check_config:
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200274 if (speed == USB_SPEED_HIGH) {
275 if (!c->highspeed)
276 continue;
277 } else {
278 if (!c->fullspeed)
279 continue;
280 }
281 if (w_value == 0)
282 return config_buf(c, speed, cdev->req->buf, type);
283 w_value--;
284 }
285 return -EINVAL;
286}
287
288static int count_configs(struct usb_composite_dev *cdev, unsigned type)
289{
290 struct usb_gadget *gadget = cdev->gadget;
291 unsigned count = 0;
292 int hs = 0;
293 struct usb_configuration *c;
294
295 if (gadget_is_dualspeed(gadget)) {
296 if (gadget->speed == USB_SPEED_HIGH)
297 hs = 1;
298 if (type == USB_DT_DEVICE_QUALIFIER)
299 hs = !hs;
300 }
301 list_for_each_entry(c, &cdev->configs, list) {
302 /* ignore configs that won't work at this speed */
303 if (hs) {
304 if (!c->highspeed)
305 continue;
306 } else {
307 if (!c->fullspeed)
308 continue;
309 }
310 count++;
311 }
312 return count;
313}
314
315static void device_qual(struct usb_composite_dev *cdev)
316{
317 struct usb_qualifier_descriptor *qual = cdev->req->buf;
318
319 qual->bLength = sizeof(*qual);
320 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
321 /* POLICY: same bcdUSB and device type info at both speeds */
322 qual->bcdUSB = cdev->desc.bcdUSB;
323 qual->bDeviceClass = cdev->desc.bDeviceClass;
324 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
325 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
326 /* ASSUME same EP0 fifo size at both speeds */
Kishon Vijay Abraham I04afd5b2015-02-23 18:40:17 +0530327 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200328 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
329 qual->bRESERVED = 0;
330}
331
332static void reset_config(struct usb_composite_dev *cdev)
333{
334 struct usb_function *f;
335
336 debug("%s:\n", __func__);
337
338 list_for_each_entry(f, &cdev->config->functions, list) {
339 if (f->disable)
340 f->disable(f);
341
342 bitmap_zero(f->endpoints, 32);
343 }
344 cdev->config = NULL;
345}
346
347static int set_config(struct usb_composite_dev *cdev,
348 const struct usb_ctrlrequest *ctrl, unsigned number)
349{
350 struct usb_gadget *gadget = cdev->gadget;
351 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
352 struct usb_descriptor_header **descriptors;
353 int result = -EINVAL;
354 struct usb_endpoint_descriptor *ep;
355 struct usb_configuration *c = NULL;
356 int addr;
357 int tmp;
358 struct usb_function *f;
359
360 if (cdev->config)
361 reset_config(cdev);
362
363 if (number) {
364 list_for_each_entry(c, &cdev->configs, list) {
365 if (c->bConfigurationValue == number) {
366 result = 0;
367 break;
368 }
369 }
370 if (result < 0)
371 goto done;
372 } else
373 result = 0;
374
375 debug("%s: %s speed config #%d: %s\n", __func__,
376 ({ char *speed;
377 switch (gadget->speed) {
378 case USB_SPEED_LOW:
379 speed = "low";
380 break;
381 case USB_SPEED_FULL:
382 speed = "full";
383 break;
384 case USB_SPEED_HIGH:
385 speed = "high";
386 break;
387 default:
388 speed = "?";
389 break;
390 };
391 speed;
392 }), number, c ? c->label : "unconfigured");
393
394 if (!c)
395 goto done;
396
397 cdev->config = c;
398
399 /* Initialize all interfaces by setting them to altsetting zero. */
400 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
401 f = c->interface[tmp];
402 if (!f)
403 break;
404
405 /*
406 * Record which endpoints are used by the function. This is used
407 * to dispatch control requests targeted at that endpoint to the
408 * function's setup callback instead of the current
409 * configuration's setup callback.
410 */
411 if (gadget->speed == USB_SPEED_HIGH)
412 descriptors = f->hs_descriptors;
413 else
414 descriptors = f->descriptors;
415
416 for (; *descriptors; ++descriptors) {
417 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
418 continue;
419
420 ep = (struct usb_endpoint_descriptor *)*descriptors;
421 addr = ((ep->bEndpointAddress & 0x80) >> 3)
422 | (ep->bEndpointAddress & 0x0f);
Bryan O'Donoghue31dd8ef2018-04-30 15:56:10 +0100423 generic_set_bit(addr, f->endpoints);
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200424 }
425
426 result = f->set_alt(f, tmp, 0);
427 if (result < 0) {
428 debug("interface %d (%s/%p) alt 0 --> %d\n",
429 tmp, f->name, f, result);
430
431 reset_config(cdev);
432 goto done;
433 }
434 }
435
436 /* when we return, be sure our power usage is valid */
437 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
438done:
439 usb_gadget_vbus_draw(gadget, power);
440 return result;
441}
442
443/**
444 * usb_add_config() - add a configuration to a device.
445 * @cdev: wraps the USB gadget
446 * @config: the configuration, with bConfigurationValue assigned
447 * Context: single threaded during gadget setup
448 *
449 * One of the main tasks of a composite driver's bind() routine is to
450 * add each of the configurations it supports, using this routine.
451 *
452 * This function returns the value of the configuration's bind(), which
453 * is zero for success else a negative errno value. Binding configurations
454 * assigns global resources including string IDs, and per-configuration
455 * resources such as interface IDs and endpoints.
456 */
457int usb_add_config(struct usb_composite_dev *cdev,
458 struct usb_configuration *config)
459{
460 int status = -EINVAL;
461 struct usb_configuration *c;
462 struct usb_function *f;
463 unsigned int i;
464
465 debug("%s: adding config #%u '%s'/%p\n", __func__,
466 config->bConfigurationValue,
467 config->label, config);
468
469 if (!config->bConfigurationValue || !config->bind)
470 goto done;
471
472 /* Prevent duplicate configuration identifiers */
473 list_for_each_entry(c, &cdev->configs, list) {
474 if (c->bConfigurationValue == config->bConfigurationValue) {
475 status = -EBUSY;
476 goto done;
477 }
478 }
479
480 config->cdev = cdev;
481 list_add_tail(&config->list, &cdev->configs);
482
483 INIT_LIST_HEAD(&config->functions);
484 config->next_interface_id = 0;
485
486 status = config->bind(config);
487 if (status < 0) {
488 list_del(&config->list);
489 config->cdev = NULL;
490 } else {
491 debug("cfg %d/%p speeds:%s%s\n",
492 config->bConfigurationValue, config,
493 config->highspeed ? " high" : "",
494 config->fullspeed
495 ? (gadget_is_dualspeed(cdev->gadget)
496 ? " full"
497 : " full/low")
498 : "");
499
500 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
501 f = config->interface[i];
502 if (!f)
503 continue;
504 debug("%s: interface %d = %s/%p\n",
505 __func__, i, f->name, f);
506 }
507 }
508
509 usb_ep_autoconfig_reset(cdev->gadget);
510
511done:
512 if (status)
513 debug("added config '%s'/%u --> %d\n", config->label,
514 config->bConfigurationValue, status);
515 return status;
516}
517
518/*
519 * We support strings in multiple languages ... string descriptor zero
520 * says which languages are supported. The typical case will be that
521 * only one language (probably English) is used, with I18N handled on
522 * the host side.
523 */
524
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100525static void collect_langs(struct usb_gadget_strings **sp, void *buf)
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200526{
527 const struct usb_gadget_strings *s;
528 u16 language;
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100529 __le16_packed *tmp;
530 __le16_packed *end = (buf + 252);
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200531
532 while (*sp) {
533 s = *sp;
534 language = cpu_to_le16(s->language);
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100535 for (tmp = buf; tmp->val && tmp < end; tmp++) {
536 if (tmp->val == language)
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200537 goto repeat;
538 }
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100539 tmp->val = language;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200540repeat:
541 sp++;
542 }
543}
544
545static int lookup_string(
546 struct usb_gadget_strings **sp,
547 void *buf,
548 u16 language,
549 int id
550)
551{
552 int value;
553 struct usb_gadget_strings *s;
554
555 while (*sp) {
556 s = *sp++;
557 if (s->language != language)
558 continue;
559 value = usb_gadget_get_string(s, id, buf);
560 if (value > 0)
561 return value;
562 }
563 return -EINVAL;
564}
565
566static int get_string(struct usb_composite_dev *cdev,
567 void *buf, u16 language, int id)
568{
569 struct usb_string_descriptor *s = buf;
570 struct usb_gadget_strings **sp;
571 int len;
572 struct usb_configuration *c;
573 struct usb_function *f;
574
575 /*
576 * Yes, not only is USB's I18N support probably more than most
577 * folk will ever care about ... also, it's all supported here.
578 * (Except for UTF8 support for Unicode's "Astral Planes".)
579 */
580
581 /* 0 == report all available language codes */
582 if (id == 0) {
583 memset(s, 0, 256);
584 s->bDescriptorType = USB_DT_STRING;
585
586 sp = composite->strings;
587 if (sp)
588 collect_langs(sp, s->wData);
589
590 list_for_each_entry(c, &cdev->configs, list) {
591 sp = c->strings;
592 if (sp)
593 collect_langs(sp, s->wData);
594
595 list_for_each_entry(f, &c->functions, list) {
596 sp = f->strings;
597 if (sp)
598 collect_langs(sp, s->wData);
599 }
600 }
601
602 for (len = 0; len <= 126 && s->wData[len]; len++)
603 continue;
604 if (!len)
605 return -EINVAL;
606
607 s->bLength = 2 * (len + 1);
608 return s->bLength;
609 }
610
Li Jun1c7aacb2021-01-25 21:43:46 +0800611 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
612 struct usb_os_string *b = buf;
613 b->bLength = sizeof(*b);
614 b->bDescriptorType = USB_DT_STRING;
615 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
616 b->bMS_VendorCode = cdev->b_vendor_code;
617 b->bPad = 0;
618 return sizeof(*b);
619 }
620
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200621 /*
622 * Otherwise, look up and return a specified string. String IDs
623 * are device-scoped, so we look up each string table we're told
624 * about. These lookups are infrequent; simpler-is-better here.
625 */
626 if (composite->strings) {
627 len = lookup_string(composite->strings, buf, language, id);
628 if (len > 0)
629 return len;
630 }
631 list_for_each_entry(c, &cdev->configs, list) {
632 if (c->strings) {
633 len = lookup_string(c->strings, buf, language, id);
634 if (len > 0)
635 return len;
636 }
637 list_for_each_entry(f, &c->functions, list) {
638 if (!f->strings)
639 continue;
640 len = lookup_string(f->strings, buf, language, id);
641 if (len > 0)
642 return len;
643 }
644 }
645 return -EINVAL;
646}
647
648/**
649 * usb_string_id() - allocate an unused string ID
650 * @cdev: the device whose string descriptor IDs are being allocated
651 * Context: single threaded during gadget setup
652 *
653 * @usb_string_id() is called from bind() callbacks to allocate
654 * string IDs. Drivers for functions, configurations, or gadgets will
655 * then store that ID in the appropriate descriptors and string table.
656 *
657 * All string identifier should be allocated using this,
658 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
659 * that for example different functions don't wrongly assign different
660 * meanings to the same identifier.
661 */
662int usb_string_id(struct usb_composite_dev *cdev)
663{
664 if (cdev->next_string_id < 254) {
665 /*
666 * string id 0 is reserved by USB spec for list of
667 * supported languages
668 * 255 reserved as well? -- mina86
669 */
670 cdev->next_string_id++;
671 return cdev->next_string_id;
672 }
673 return -ENODEV;
674}
675
676/**
677 * usb_string_ids() - allocate unused string IDs in batch
678 * @cdev: the device whose string descriptor IDs are being allocated
679 * @str: an array of usb_string objects to assign numbers to
680 * Context: single threaded during gadget setup
681 *
682 * @usb_string_ids() is called from bind() callbacks to allocate
683 * string IDs. Drivers for functions, configurations, or gadgets will
684 * then copy IDs from the string table to the appropriate descriptors
685 * and string table for other languages.
686 *
687 * All string identifier should be allocated using this,
688 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
689 * example different functions don't wrongly assign different meanings
690 * to the same identifier.
691 */
692int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
693{
694 u8 next = cdev->next_string_id;
695
696 for (; str->s; ++str) {
697 if (next >= 254)
698 return -ENODEV;
699 str->id = ++next;
700 }
701
702 cdev->next_string_id = next;
703
704 return 0;
705}
706
707/**
708 * usb_string_ids_n() - allocate unused string IDs in batch
709 * @c: the device whose string descriptor IDs are being allocated
710 * @n: number of string IDs to allocate
711 * Context: single threaded during gadget setup
712 *
713 * Returns the first requested ID. This ID and next @n-1 IDs are now
714 * valid IDs. At least provided that @n is non-zero because if it
715 * is, returns last requested ID which is now very useful information.
716 *
717 * @usb_string_ids_n() is called from bind() callbacks to allocate
718 * string IDs. Drivers for functions, configurations, or gadgets will
719 * then store that ID in the appropriate descriptors and string table.
720 *
721 * All string identifier should be allocated using this,
722 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
723 * example different functions don't wrongly assign different meanings
724 * to the same identifier.
725 */
726int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
727{
728 u8 next = c->next_string_id;
729
730 if (n > 254 || next + n > 254)
731 return -ENODEV;
732
733 c->next_string_id += n;
734 return next + 1;
735}
736
737static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
738{
739 if (req->status || req->actual != req->length)
740 debug("%s: setup complete --> %d, %d/%d\n", __func__,
741 req->status, req->actual, req->length);
742}
743
T Karthik Reddyf69257b2019-10-14 14:52:50 +0200744static int bos_desc(struct usb_composite_dev *cdev)
745{
746 struct usb_ext_cap_descriptor *usb_ext;
747 struct usb_bos_descriptor *bos = cdev->req->buf;
748
749 bos->bLength = USB_DT_BOS_SIZE;
750 bos->bDescriptorType = USB_DT_BOS;
751
752 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
753 bos->bNumDeviceCaps = 0;
754
755 /*
756 * A SuperSpeed device shall include the USB2.0 extension descriptor
757 * and shall support LPM when operating in USB2.0 HS mode.
758 */
759 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
760 bos->bNumDeviceCaps++;
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100761 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
762 USB_DT_USB_EXT_CAP_SIZE);
T Karthik Reddyf69257b2019-10-14 14:52:50 +0200763 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
764 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
765 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
766 usb_ext->bmAttributes =
767 cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
768
769 /*
770 * The Superspeed USB Capability descriptor shall be implemented
771 * by all SuperSpeed devices.
772 */
773 if (gadget_is_superspeed(cdev->gadget)) {
774 struct usb_ss_cap_descriptor *ss_cap;
775
776 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
777 bos->bNumDeviceCaps++;
Simon Goldschmidt616ebd82019-11-21 22:15:22 +0100778 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
779 USB_DT_USB_SS_CAP_SIZE);
T Karthik Reddyf69257b2019-10-14 14:52:50 +0200780 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
781 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
782 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
783 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
784 ss_cap->wSpeedSupported =
785 cpu_to_le16(USB_LOW_SPEED_OPERATION |
786 USB_FULL_SPEED_OPERATION |
787 USB_HIGH_SPEED_OPERATION |
788 USB_5GBPS_OPERATION);
789 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
790 ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
791 ss_cap->bU2DevExitLat =
792 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
793 }
794 return le16_to_cpu(bos->wTotalLength);
795}
796
Li Juna764c942021-01-25 21:43:49 +0800797static int count_ext_compat(struct usb_configuration *c)
798{
799 int i, res;
800
801 res = 0;
802 for (i = 0; i < c->next_interface_id; ++i) {
803 struct usb_function *f;
804 int j;
805
806 f = c->interface[i];
807 for (j = 0; j < f->os_desc_n; ++j) {
808 struct usb_os_desc *d;
809
810 if (i != f->os_desc_table[j].if_id)
811 continue;
812 d = f->os_desc_table[j].os_desc;
813 if (d && d->ext_compat_id)
814 ++res;
815 }
816 }
817 BUG_ON(res > 255);
818 return res;
819}
820
821static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
822{
823 int i, count;
824
825 count = 16;
826 for (i = 0; i < c->next_interface_id; ++i) {
827 struct usb_function *f;
828 int j;
829
830 f = c->interface[i];
831 for (j = 0; j < f->os_desc_n; ++j) {
832 struct usb_os_desc *d;
833
834 if (i != f->os_desc_table[j].if_id)
835 continue;
836 d = f->os_desc_table[j].os_desc;
837 if (d && d->ext_compat_id) {
838 *buf++ = i;
839 *buf++ = 0x01;
840 memcpy(buf, d->ext_compat_id, 16);
841 buf += 22;
842 } else {
843 ++buf;
844 *buf = 0x01;
845 buf += 23;
846 }
847 count += 24;
848 if (count >= 4096)
849 return;
850 }
851 }
852}
853
854static int count_ext_prop(struct usb_configuration *c, int interface)
855{
856 struct usb_function *f;
857 int j;
858
859 f = c->interface[interface];
860 for (j = 0; j < f->os_desc_n; ++j) {
861 struct usb_os_desc *d;
862
863 if (interface != f->os_desc_table[j].if_id)
864 continue;
865 d = f->os_desc_table[j].os_desc;
866 if (d && d->ext_compat_id)
867 return d->ext_prop_count;
868 }
869 return 0;
870}
871
872static int len_ext_prop(struct usb_configuration *c, int interface)
873{
874 struct usb_function *f;
875 struct usb_os_desc *d;
876 int j, res;
877
878 res = 10; /* header length */
879 f = c->interface[interface];
880 for (j = 0; j < f->os_desc_n; ++j) {
881 if (interface != f->os_desc_table[j].if_id)
882 continue;
883 d = f->os_desc_table[j].os_desc;
884 if (d)
885 return min(res + d->ext_prop_len, 4096);
886 }
887 return res;
888}
889
890static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
891{
892 struct usb_function *f;
893 struct usb_os_desc *d;
894 struct usb_os_desc_ext_prop *ext_prop;
895 int j, count, n, ret;
896 u8 *start = buf;
897
898 f = c->interface[interface];
899 for (j = 0; j < f->os_desc_n; ++j) {
900 if (interface != f->os_desc_table[j].if_id)
901 continue;
902 d = f->os_desc_table[j].os_desc;
903 if (d)
904 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
905 /* 4kB minus header length */
906 n = buf - start;
907 if (n >= 4086)
908 return 0;
909
910 count = ext_prop->data_len +
911 ext_prop->name_len + 14;
912 if (count > 4086 - n)
913 return -EINVAL;
914 usb_ext_prop_put_size(buf, count);
915 usb_ext_prop_put_type(buf, ext_prop->type);
916 ret = usb_ext_prop_put_name(buf, ext_prop->name,
917 ext_prop->name_len);
918 if (ret < 0)
919 return ret;
920 switch (ext_prop->type) {
921 case USB_EXT_PROP_UNICODE:
922 case USB_EXT_PROP_UNICODE_ENV:
923 case USB_EXT_PROP_UNICODE_LINK:
924 usb_ext_prop_put_unicode(buf, ret,
925 ext_prop->data,
926 ext_prop->data_len);
927 break;
928 case USB_EXT_PROP_BINARY:
929 usb_ext_prop_put_binary(buf, ret,
930 ext_prop->data,
931 ext_prop->data_len);
932 break;
933 case USB_EXT_PROP_LE32:
934 /* not implemented */
935 case USB_EXT_PROP_BE32:
936 /* not implemented */
937 default:
938 return -EINVAL;
939 }
940 buf += count;
941 }
942 }
943
944 return 0;
945}
946
Lukasz Majewski7010f5b2012-05-02 17:47:02 +0200947/*
948 * The setup() callback implements all the ep0 functionality that's
949 * not handled lower down, in hardware or the hardware driver(like
950 * device and endpoint feature flags, and their status). It's all
951 * housekeeping for the gadget function we're implementing. Most of
952 * the work is in config and function specific setup.
953 */
954static int
955composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
956{
957 u16 w_length = le16_to_cpu(ctrl->wLength);
958 u16 w_index = le16_to_cpu(ctrl->wIndex);
959 u16 w_value = le16_to_cpu(ctrl->wValue);
960 struct usb_composite_dev *cdev = get_gadget_data(gadget);
961 u8 intf = w_index & 0xFF;
962 int value = -EOPNOTSUPP;
963 struct usb_request *req = cdev->req;
964 struct usb_function *f = NULL;
965 int standard;
966 u8 endp;
967 struct usb_configuration *c;
968
969 /*
970 * partial re-init of the response message; the function or the
971 * gadget might need to intercept e.g. a control-OUT completion
972 * when we delegate to it.
973 */
974 req->zero = 0;
975 req->complete = composite_setup_complete;
976 req->length = USB_BUFSIZ;
977 gadget->ep0->driver_data = cdev;
978 standard = (ctrl->bRequestType & USB_TYPE_MASK)
979 == USB_TYPE_STANDARD;
980 if (!standard)
981 goto unknown;
982
983 switch (ctrl->bRequest) {
984
985 /* we handle all standard USB descriptors */
986 case USB_REQ_GET_DESCRIPTOR:
987 if (ctrl->bRequestType != USB_DIR_IN)
988 goto unknown;
989 switch (w_value >> 8) {
990
991 case USB_DT_DEVICE:
992 cdev->desc.bNumConfigurations =
993 count_configs(cdev, USB_DT_DEVICE);
Siva Durga Prasad Paladugu771e7652018-12-13 15:16:36 +0530994
995 /*
996 * If the speed is Super speed, then the supported
997 * max packet size is 512 and it should be sent as
998 * exponent of 2. So, 9(2^9=512) should be filled in
999 * bMaxPacketSize0. Also fill USB version as 3.0
1000 * if speed is Super speed.
1001 */
1002 if (cdev->gadget->speed == USB_SPEED_SUPER) {
1003 cdev->desc.bMaxPacketSize0 = 9;
1004 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
1005 } else {
1006 cdev->desc.bMaxPacketSize0 =
1007 cdev->gadget->ep0->maxpacket;
1008 }
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001009 value = min(w_length, (u16) sizeof cdev->desc);
1010 memcpy(req->buf, &cdev->desc, value);
1011 break;
1012 case USB_DT_DEVICE_QUALIFIER:
1013 if (!gadget_is_dualspeed(gadget))
1014 break;
1015 device_qual(cdev);
Masahiro Yamadab4141192014-11-07 03:03:31 +09001016 value = min_t(int, w_length,
1017 sizeof(struct usb_qualifier_descriptor));
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001018 break;
1019 case USB_DT_OTHER_SPEED_CONFIG:
1020 if (!gadget_is_dualspeed(gadget))
1021 break;
1022
1023 case USB_DT_CONFIG:
1024 value = config_desc(cdev, w_value);
1025 if (value >= 0)
1026 value = min(w_length, (u16) value);
1027 break;
1028 case USB_DT_STRING:
1029 value = get_string(cdev, req->buf,
1030 w_index, w_value & 0xff);
1031 if (value >= 0)
1032 value = min(w_length, (u16) value);
1033 break;
Stefan Roese87ed6b12015-01-09 14:54:55 +01001034 case USB_DT_BOS:
T Karthik Reddyf69257b2019-10-14 14:52:50 +02001035 if (gadget_is_superspeed(cdev->gadget))
1036 value = bos_desc(cdev);
1037 if (value >= 0)
1038 value = min(w_length, (u16)value);
Stefan Roese87ed6b12015-01-09 14:54:55 +01001039 break;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001040 default:
1041 goto unknown;
1042 }
1043 break;
1044
1045 /* any number of configs can work */
1046 case USB_REQ_SET_CONFIGURATION:
1047 if (ctrl->bRequestType != 0)
1048 goto unknown;
1049 if (gadget_is_otg(gadget)) {
1050 if (gadget->a_hnp_support)
1051 debug("HNP available\n");
1052 else if (gadget->a_alt_hnp_support)
1053 debug("HNP on another port\n");
1054 else
1055 debug("HNP inactive\n");
1056 }
1057
1058 value = set_config(cdev, ctrl, w_value);
1059 break;
1060 case USB_REQ_GET_CONFIGURATION:
1061 if (ctrl->bRequestType != USB_DIR_IN)
1062 goto unknown;
1063 if (cdev->config)
1064 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1065 else
1066 *(u8 *)req->buf = 0;
1067 value = min(w_length, (u16) 1);
1068 break;
1069
1070 /*
1071 * function drivers must handle get/set altsetting; if there's
1072 * no get() method, we know only altsetting zero works.
1073 */
1074 case USB_REQ_SET_INTERFACE:
1075 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1076 goto unknown;
1077 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1078 break;
1079 f = cdev->config->interface[intf];
1080 if (!f)
1081 break;
1082 if (w_value && !f->set_alt)
1083 break;
1084 value = f->set_alt(f, w_index, w_value);
1085 break;
1086 case USB_REQ_GET_INTERFACE:
1087 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1088 goto unknown;
1089 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1090 break;
1091 f = cdev->config->interface[intf];
1092 if (!f)
1093 break;
1094 /* lots of interfaces only need altsetting zero... */
1095 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1096 if (value < 0)
1097 break;
1098 *((u8 *)req->buf) = value;
1099 value = min(w_length, (u16) 1);
1100 break;
1101 default:
1102unknown:
Li Juna764c942021-01-25 21:43:49 +08001103 /*
1104 * OS descriptors handling
1105 */
1106 if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string &&
1107 cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1108 ctrl->bRequest == cdev->b_vendor_code) {
1109 struct usb_configuration *os_desc_cfg;
1110 u8 *buf;
1111 int interface;
1112 int count = 0;
1113
1114 buf = req->buf;
1115 os_desc_cfg = cdev->os_desc_config;
1116 memset(buf, 0, w_length);
1117 buf[5] = 0x01;
1118 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1119 case USB_RECIP_DEVICE:
1120 if (w_index != 0x4 || (w_value >> 8))
1121 break;
1122 buf[6] = w_index;
1123 if (w_length == 0x10) {
1124 /* Number of ext compat interfaces */
1125 count = count_ext_compat(os_desc_cfg);
1126 buf[8] = count;
1127 count *= 24; /* 24 B/ext compat desc */
1128 count += 16; /* header */
1129 put_unaligned_le32(count, buf);
1130 value = w_length;
1131 } else {
1132 /* "extended compatibility ID"s */
1133 count = count_ext_compat(os_desc_cfg);
1134 buf[8] = count;
1135 count *= 24; /* 24 B/ext compat desc */
1136 count += 16; /* header */
1137 put_unaligned_le32(count, buf);
1138 buf += 16;
1139 fill_ext_compat(os_desc_cfg, buf);
1140 value = w_length;
1141 }
1142 break;
1143 case USB_RECIP_INTERFACE:
1144 if (w_index != 0x5 || (w_value >> 8))
1145 break;
1146 interface = w_value & 0xFF;
1147 buf[6] = w_index;
1148 if (w_length == 0x0A) {
1149 count = count_ext_prop(os_desc_cfg,
1150 interface);
1151 put_unaligned_le16(count, buf + 8);
1152 count = len_ext_prop(os_desc_cfg,
1153 interface);
1154 put_unaligned_le32(count, buf);
1155
1156 value = w_length;
1157 } else {
1158 count = count_ext_prop(os_desc_cfg,
1159 interface);
1160 put_unaligned_le16(count, buf + 8);
1161 count = len_ext_prop(os_desc_cfg,
1162 interface);
1163 put_unaligned_le32(count, buf);
1164 buf += 10;
1165 value = fill_ext_prop(os_desc_cfg,
1166 interface, buf);
1167 if (value < 0)
1168 return value;
1169
1170 value = w_length;
1171 }
1172 break;
1173 }
1174
1175 if (value >= 0) {
1176 req->length = value;
1177 req->zero = value < w_length;
1178 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1179 if (value < 0) {
1180 debug("ep_queue --> %d\n", value);
1181 req->status = 0;
1182 composite_setup_complete(gadget->ep0, req);
1183 }
1184 }
1185 return value;
1186 }
1187
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001188 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
1189 ctrl->bRequestType, ctrl->bRequest,
1190 w_value, w_index, w_length);
1191
Christophe Kerellod57ed4d2018-03-15 09:34:17 +01001192 if (!cdev->config)
1193 goto done;
1194
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001195 /*
1196 * functions always handle their interfaces and endpoints...
1197 * punt other recipients (other, WUSB, ...) to the current
1198 * configuration code.
1199 */
1200 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1201 case USB_RECIP_INTERFACE:
1202 f = cdev->config->interface[intf];
1203 break;
1204
1205 case USB_RECIP_ENDPOINT:
1206 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1207 list_for_each_entry(f, &cdev->config->functions, list) {
1208 if (test_bit(endp, f->endpoints))
1209 break;
1210 }
1211 if (&f->list == &cdev->config->functions)
1212 f = NULL;
1213 break;
Lukasz Majewskif7b41622013-03-01 15:30:18 +01001214 /*
1215 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
1216 * for non-standard request (w_value = 0x21,
1217 * bRequest = GET_DESCRIPTOR in this case).
1218 * When only one interface is registered (as it is done now),
1219 * then this request shall be handled as it was requested for
1220 * interface.
1221 *
1222 * In the below code it is checked if only one interface is
1223 * present and proper function for it is extracted. Due to that
1224 * function's setup (f->setup) is called to handle this
1225 * special non-standard request.
1226 */
1227 case USB_RECIP_DEVICE:
1228 debug("cdev->config->next_interface_id: %d intf: %d\n",
1229 cdev->config->next_interface_id, intf);
1230 if (cdev->config->next_interface_id == 1)
1231 f = cdev->config->interface[intf];
1232 break;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001233 }
1234
1235 if (f && f->setup)
1236 value = f->setup(f, ctrl);
1237 else {
1238 c = cdev->config;
Christophe Kerellod57ed4d2018-03-15 09:34:17 +01001239 if (c->setup)
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001240 value = c->setup(c, ctrl);
1241 }
1242
1243 goto done;
1244 }
1245
1246 /* respond with data transfer before status phase? */
1247 if (value >= 0) {
1248 req->length = value;
1249 req->zero = value < w_length;
1250 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1251 if (value < 0) {
1252 debug("ep_queue --> %d\n", value);
1253 req->status = 0;
1254 composite_setup_complete(gadget->ep0, req);
1255 }
1256 }
1257
1258done:
1259 /* device either stalls (value < 0) or reports success */
1260 return value;
1261}
1262
1263static void composite_disconnect(struct usb_gadget *gadget)
1264{
1265 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1266
1267 if (cdev->config)
1268 reset_config(cdev);
1269 if (composite->disconnect)
1270 composite->disconnect(cdev);
1271}
1272
1273static void composite_unbind(struct usb_gadget *gadget)
1274{
1275 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1276 struct usb_configuration *c;
1277 struct usb_function *f;
1278
1279 /*
1280 * composite_disconnect() must already have been called
1281 * by the underlying peripheral controller driver!
1282 * so there's no i/o concurrency that could affect the
1283 * state protected by cdev->lock.
1284 */
Simon Glass1058eec2019-12-29 21:19:12 -07001285#ifdef __UBOOT__
1286 assert_noisy(!cdev->config);
1287#else
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001288 BUG_ON(cdev->config);
Simon Glass1058eec2019-12-29 21:19:12 -07001289#endif
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001290
1291 while (!list_empty(&cdev->configs)) {
1292 c = list_first_entry(&cdev->configs,
1293 struct usb_configuration, list);
1294 while (!list_empty(&c->functions)) {
1295 f = list_first_entry(&c->functions,
1296 struct usb_function, list);
1297 list_del(&f->list);
1298 if (f->unbind) {
1299 debug("unbind function '%s'/%p\n",
1300 f->name, f);
1301 f->unbind(c, f);
1302 }
1303 }
1304 list_del(&c->list);
1305 if (c->unbind) {
1306 debug("unbind config '%s'/%p\n", c->label, c);
1307 c->unbind(c);
1308 }
Stephen Warren44bfb432015-09-04 22:03:42 -06001309 free(c);
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001310 }
1311 if (composite->unbind)
1312 composite->unbind(cdev);
1313
1314 if (cdev->req) {
1315 kfree(cdev->req->buf);
1316 usb_ep_free_request(gadget->ep0, cdev->req);
1317 }
1318 kfree(cdev);
1319 set_gadget_data(gadget, NULL);
1320
1321 composite = NULL;
1322}
1323
1324static int composite_bind(struct usb_gadget *gadget)
1325{
1326 int status = -ENOMEM;
1327 struct usb_composite_dev *cdev;
1328
1329 cdev = calloc(sizeof *cdev, 1);
1330 if (!cdev)
1331 return status;
1332
1333 cdev->gadget = gadget;
1334 set_gadget_data(gadget, cdev);
1335 INIT_LIST_HEAD(&cdev->configs);
1336
1337 /* preallocate control response and buffer */
1338 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1339 if (!cdev->req)
1340 goto fail;
1341 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1342 if (!cdev->req->buf)
1343 goto fail;
1344 cdev->req->complete = composite_setup_complete;
1345 gadget->ep0->driver_data = cdev;
1346
1347 cdev->bufsiz = USB_BUFSIZ;
1348 cdev->driver = composite;
1349
1350 usb_gadget_set_selfpowered(gadget);
1351 usb_ep_autoconfig_reset(cdev->gadget);
1352
1353 status = composite->bind(cdev);
1354 if (status < 0)
1355 goto fail;
1356
Piotr Wilczek12595e92013-04-10 14:07:51 +02001357 memcpy(&cdev->desc, composite->dev,
1358 sizeof(struct usb_device_descriptor));
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001359 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1360
1361 debug("%s: ready\n", composite->name);
1362 return 0;
1363
1364fail:
1365 composite_unbind(gadget);
1366 return status;
1367}
1368
1369static void
1370composite_suspend(struct usb_gadget *gadget)
1371{
1372 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1373 struct usb_function *f;
1374
1375 debug("%s: suspend\n", __func__);
1376 if (cdev->config) {
1377 list_for_each_entry(f, &cdev->config->functions, list) {
1378 if (f->suspend)
1379 f->suspend(f);
1380 }
1381 }
1382 if (composite->suspend)
1383 composite->suspend(cdev);
1384
1385 cdev->suspended = 1;
1386}
1387
1388static void
1389composite_resume(struct usb_gadget *gadget)
1390{
1391 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1392 struct usb_function *f;
1393
1394 debug("%s: resume\n", __func__);
1395 if (composite->resume)
1396 composite->resume(cdev);
1397 if (cdev->config) {
1398 list_for_each_entry(f, &cdev->config->functions, list) {
1399 if (f->resume)
1400 f->resume(f);
1401 }
1402 }
1403
1404 cdev->suspended = 0;
1405}
1406
1407static struct usb_gadget_driver composite_driver = {
1408 .speed = USB_SPEED_HIGH,
1409
1410 .bind = composite_bind,
1411 .unbind = composite_unbind,
1412
1413 .setup = composite_setup,
Lukasz Majewski6d691732015-03-03 17:32:07 +01001414 .reset = composite_disconnect,
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001415 .disconnect = composite_disconnect,
1416
1417 .suspend = composite_suspend,
1418 .resume = composite_resume,
1419};
1420
1421/**
1422 * usb_composite_register() - register a composite driver
1423 * @driver: the driver to register
1424 * Context: single threaded during gadget setup
1425 *
1426 * This function is used to register drivers using the composite driver
1427 * framework. The return value is zero, or a negative errno value.
1428 * Those values normally come from the driver's @bind method, which does
1429 * all the work of setting up the driver to match the hardware.
1430 *
1431 * On successful return, the gadget is ready to respond to requests from
1432 * the host, unless one of its components invokes usb_gadget_disconnect()
1433 * while it was binding. That would usually be done in order to wait for
1434 * some userspace participation.
1435 */
1436int usb_composite_register(struct usb_composite_driver *driver)
1437{
Sam Protsenko8038f6d2016-02-16 19:59:19 +02001438 int res;
1439
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001440 if (!driver || !driver->dev || !driver->bind || composite)
1441 return -EINVAL;
1442
1443 if (!driver->name)
1444 driver->name = "composite";
1445 composite = driver;
1446
Sam Protsenko8038f6d2016-02-16 19:59:19 +02001447 res = usb_gadget_register_driver(&composite_driver);
1448 if (res != 0)
1449 composite = NULL;
1450
1451 return res;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001452}
1453
1454/**
1455 * usb_composite_unregister() - unregister a composite driver
1456 * @driver: the driver to unregister
1457 *
1458 * This function is used to unregister drivers using the composite
1459 * driver framework.
1460 */
1461void usb_composite_unregister(struct usb_composite_driver *driver)
1462{
1463 if (composite != driver)
1464 return;
1465 usb_gadget_unregister_driver(&composite_driver);
Heiko Schocherc67b0e42013-06-04 11:21:32 +02001466 composite = NULL;
Lukasz Majewski7010f5b2012-05-02 17:47:02 +02001467}