1 /*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 /* #define VERBOSE_DEBUG */
13
14 #include <linux/kallsyms.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/utsname.h>
20
21 #include <linux/usb/composite.h>
22 #include <asm/unaligned.h>
23
24 #include "u_os_desc.h"
25
26 /**
27 * struct usb_os_string - represents OS String to be reported by a gadget
28 * @bLength: total length of the entire descritor, always 0x12
29 * @bDescriptorType: USB_DT_STRING
30 * @qwSignature: the OS String proper
31 * @bMS_VendorCode: code used by the host for subsequent requests
32 * @bPad: not used, must be zero
33 */
34 struct usb_os_string {
35 __u8 bLength;
36 __u8 bDescriptorType;
37 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
38 __u8 bMS_VendorCode;
39 __u8 bPad;
40 } __packed;
41
42 /*
43 * The code in this file is utility code, used to build a gadget driver
44 * from one or more "function" drivers, one or more "configuration"
45 * objects, and a "usb_composite_driver" by gluing them together along
46 * with the relevant device-wide data.
47 */
48
get_containers_gs(struct usb_gadget_string_container * uc)49 static struct usb_gadget_strings **get_containers_gs(
50 struct usb_gadget_string_container *uc)
51 {
52 return (struct usb_gadget_strings **)uc->stash;
53 }
54
55 /**
56 * next_ep_desc() - advance to the next EP descriptor
57 * @t: currect pointer within descriptor array
58 *
59 * Return: next EP descriptor or NULL
60 *
61 * Iterate over @t until either EP descriptor found or
62 * NULL (that indicates end of list) encountered
63 */
64 static struct usb_descriptor_header**
next_ep_desc(struct usb_descriptor_header ** t)65 next_ep_desc(struct usb_descriptor_header **t)
66 {
67 for (; *t; t++) {
68 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
69 return t;
70 }
71 return NULL;
72 }
73
74 /*
75 * for_each_ep_desc()- iterate over endpoint descriptors in the
76 * descriptors list
77 * @start: pointer within descriptor array.
78 * @ep_desc: endpoint descriptor to use as the loop cursor
79 */
80 #define for_each_ep_desc(start, ep_desc) \
81 for (ep_desc = next_ep_desc(start); \
82 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
83
84 /**
85 * config_ep_by_speed() - configures the given endpoint
86 * according to gadget speed.
87 * @g: pointer to the gadget
88 * @f: usb function
89 * @_ep: the endpoint to configure
90 *
91 * Return: error code, 0 on success
92 *
93 * This function chooses the right descriptors for a given
94 * endpoint according to gadget speed and saves it in the
95 * endpoint desc field. If the endpoint already has a descriptor
96 * assigned to it - overwrites it with currently corresponding
97 * descriptor. The endpoint maxpacket field is updated according
98 * to the chosen descriptor.
99 * Note: the supplied function should hold all the descriptors
100 * for supported speeds
101 */
config_ep_by_speed(struct usb_gadget * g,struct usb_function * f,struct usb_ep * _ep)102 int config_ep_by_speed(struct usb_gadget *g,
103 struct usb_function *f,
104 struct usb_ep *_ep)
105 {
106 struct usb_endpoint_descriptor *chosen_desc = NULL;
107 struct usb_descriptor_header **speed_desc = NULL;
108
109 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
110 int want_comp_desc = 0;
111
112 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
113
114 if (!g || !f || !_ep)
115 return -EIO;
116
117 /* select desired speed */
118 switch (g->speed) {
119 case USB_SPEED_SUPER:
120 if (gadget_is_superspeed(g)) {
121 speed_desc = f->ss_descriptors;
122 want_comp_desc = 1;
123 break;
124 }
125 /* else: Fall trough */
126 case USB_SPEED_HIGH:
127 if (gadget_is_dualspeed(g)) {
128 speed_desc = f->hs_descriptors;
129 break;
130 }
131 /* else: fall through */
132 default:
133 speed_desc = f->fs_descriptors;
134 }
135 /* find descriptors */
136 for_each_ep_desc(speed_desc, d_spd) {
137 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
138 if (chosen_desc->bEndpointAddress == _ep->address)
139 goto ep_found;
140 }
141 return -EIO;
142
143 ep_found:
144 /* commit results */
145 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
146 _ep->desc = chosen_desc;
147 _ep->comp_desc = NULL;
148 _ep->maxburst = 0;
149 _ep->mult = 0;
150 if (!want_comp_desc)
151 return 0;
152
153 /*
154 * Companion descriptor should follow EP descriptor
155 * USB 3.0 spec, #9.6.7
156 */
157 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
158 if (!comp_desc ||
159 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
160 return -EIO;
161 _ep->comp_desc = comp_desc;
162 if (g->speed == USB_SPEED_SUPER) {
163 switch (usb_endpoint_type(_ep->desc)) {
164 case USB_ENDPOINT_XFER_ISOC:
165 /* mult: bits 1:0 of bmAttributes */
166 _ep->mult = comp_desc->bmAttributes & 0x3;
167 case USB_ENDPOINT_XFER_BULK:
168 case USB_ENDPOINT_XFER_INT:
169 _ep->maxburst = comp_desc->bMaxBurst + 1;
170 break;
171 default:
172 if (comp_desc->bMaxBurst != 0) {
173 struct usb_composite_dev *cdev;
174
175 cdev = get_gadget_data(g);
176 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
177 }
178 _ep->maxburst = 1;
179 break;
180 }
181 }
182 return 0;
183 }
184 EXPORT_SYMBOL_GPL(config_ep_by_speed);
185
186 /**
187 * usb_add_function() - add a function to a configuration
188 * @config: the configuration
189 * @function: the function being added
190 * Context: single threaded during gadget setup
191 *
192 * After initialization, each configuration must have one or more
193 * functions added to it. Adding a function involves calling its @bind()
194 * method to allocate resources such as interface and string identifiers
195 * and endpoints.
196 *
197 * This function returns the value of the function's bind(), which is
198 * zero for success else a negative errno value.
199 */
usb_add_function(struct usb_configuration * config,struct usb_function * function)200 int usb_add_function(struct usb_configuration *config,
201 struct usb_function *function)
202 {
203 int value = -EINVAL;
204
205 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
206 function->name, function,
207 config->label, config);
208
209 if (!function->set_alt || !function->disable)
210 goto done;
211
212 function->config = config;
213 list_add_tail(&function->list, &config->functions);
214
215 /* REVISIT *require* function->bind? */
216 if (function->bind) {
217 value = function->bind(config, function);
218 if (value < 0) {
219 list_del(&function->list);
220 function->config = NULL;
221 }
222 } else
223 value = 0;
224
225 /* We allow configurations that don't work at both speeds.
226 * If we run into a lowspeed Linux system, treat it the same
227 * as full speed ... it's the function drivers that will need
228 * to avoid bulk and ISO transfers.
229 */
230 if (!config->fullspeed && function->fs_descriptors)
231 config->fullspeed = true;
232 if (!config->highspeed && function->hs_descriptors)
233 config->highspeed = true;
234 if (!config->superspeed && function->ss_descriptors)
235 config->superspeed = true;
236
237 done:
238 if (value)
239 DBG(config->cdev, "adding '%s'/%p --> %d\n",
240 function->name, function, value);
241 return value;
242 }
243 EXPORT_SYMBOL_GPL(usb_add_function);
244
usb_remove_function(struct usb_configuration * c,struct usb_function * f)245 void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
246 {
247 if (f->disable)
248 f->disable(f);
249
250 bitmap_zero(f->endpoints, 32);
251 list_del(&f->list);
252 if (f->unbind)
253 f->unbind(c, f);
254 }
255 EXPORT_SYMBOL_GPL(usb_remove_function);
256
257 /**
258 * usb_function_deactivate - prevent function and gadget enumeration
259 * @function: the function that isn't yet ready to respond
260 *
261 * Blocks response of the gadget driver to host enumeration by
262 * preventing the data line pullup from being activated. This is
263 * normally called during @bind() processing to change from the
264 * initial "ready to respond" state, or when a required resource
265 * becomes available.
266 *
267 * For example, drivers that serve as a passthrough to a userspace
268 * daemon can block enumeration unless that daemon (such as an OBEX,
269 * MTP, or print server) is ready to handle host requests.
270 *
271 * Not all systems support software control of their USB peripheral
272 * data pullups.
273 *
274 * Returns zero on success, else negative errno.
275 */
usb_function_deactivate(struct usb_function * function)276 int usb_function_deactivate(struct usb_function *function)
277 {
278 struct usb_composite_dev *cdev = function->config->cdev;
279 unsigned long flags;
280 int status = 0;
281
282 spin_lock_irqsave(&cdev->lock, flags);
283
284 if (cdev->deactivations == 0)
285 status = usb_gadget_disconnect(cdev->gadget);
286 if (status == 0)
287 cdev->deactivations++;
288
289 spin_unlock_irqrestore(&cdev->lock, flags);
290 return status;
291 }
292 EXPORT_SYMBOL_GPL(usb_function_deactivate);
293
294 /**
295 * usb_function_activate - allow function and gadget enumeration
296 * @function: function on which usb_function_activate() was called
297 *
298 * Reverses effect of usb_function_deactivate(). If no more functions
299 * are delaying their activation, the gadget driver will respond to
300 * host enumeration procedures.
301 *
302 * Returns zero on success, else negative errno.
303 */
usb_function_activate(struct usb_function * function)304 int usb_function_activate(struct usb_function *function)
305 {
306 struct usb_composite_dev *cdev = function->config->cdev;
307 unsigned long flags;
308 int status = 0;
309
310 spin_lock_irqsave(&cdev->lock, flags);
311
312 if (WARN_ON(cdev->deactivations == 0))
313 status = -EINVAL;
314 else {
315 cdev->deactivations--;
316 if (cdev->deactivations == 0)
317 status = usb_gadget_connect(cdev->gadget);
318 }
319
320 spin_unlock_irqrestore(&cdev->lock, flags);
321 return status;
322 }
323 EXPORT_SYMBOL_GPL(usb_function_activate);
324
325 /**
326 * usb_interface_id() - allocate an unused interface ID
327 * @config: configuration associated with the interface
328 * @function: function handling the interface
329 * Context: single threaded during gadget setup
330 *
331 * usb_interface_id() is called from usb_function.bind() callbacks to
332 * allocate new interface IDs. The function driver will then store that
333 * ID in interface, association, CDC union, and other descriptors. It
334 * will also handle any control requests targeted at that interface,
335 * particularly changing its altsetting via set_alt(). There may
336 * also be class-specific or vendor-specific requests to handle.
337 *
338 * All interface identifier should be allocated using this routine, to
339 * ensure that for example different functions don't wrongly assign
340 * different meanings to the same identifier. Note that since interface
341 * identifiers are configuration-specific, functions used in more than
342 * one configuration (or more than once in a given configuration) need
343 * multiple versions of the relevant descriptors.
344 *
345 * Returns the interface ID which was allocated; or -ENODEV if no
346 * more interface IDs can be allocated.
347 */
usb_interface_id(struct usb_configuration * config,struct usb_function * function)348 int usb_interface_id(struct usb_configuration *config,
349 struct usb_function *function)
350 {
351 unsigned id = config->next_interface_id;
352
353 if (id < MAX_CONFIG_INTERFACES) {
354 config->interface[id] = function;
355 config->next_interface_id = id + 1;
356 return id;
357 }
358 return -ENODEV;
359 }
360 EXPORT_SYMBOL_GPL(usb_interface_id);
361
encode_bMaxPower(enum usb_device_speed speed,struct usb_configuration * c)362 static u8 encode_bMaxPower(enum usb_device_speed speed,
363 struct usb_configuration *c)
364 {
365 unsigned val;
366
367 if (c->MaxPower)
368 val = c->MaxPower;
369 else
370 val = CONFIG_USB_GADGET_VBUS_DRAW;
371 if (!val)
372 return 0;
373 switch (speed) {
374 case USB_SPEED_SUPER:
375 return DIV_ROUND_UP(val, 8);
376 default:
377 return DIV_ROUND_UP(val, 2);
378 }
379 }
380
config_buf(struct usb_configuration * config,enum usb_device_speed speed,void * buf,u8 type)381 static int config_buf(struct usb_configuration *config,
382 enum usb_device_speed speed, void *buf, u8 type)
383 {
384 struct usb_config_descriptor *c = buf;
385 void *next = buf + USB_DT_CONFIG_SIZE;
386 int len;
387 struct usb_function *f;
388 int status;
389
390 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
391 /* write the config descriptor */
392 c = buf;
393 c->bLength = USB_DT_CONFIG_SIZE;
394 c->bDescriptorType = type;
395 /* wTotalLength is written later */
396 c->bNumInterfaces = config->next_interface_id;
397 c->bConfigurationValue = config->bConfigurationValue;
398 c->iConfiguration = config->iConfiguration;
399 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
400 c->bMaxPower = encode_bMaxPower(speed, config);
401
402 /* There may be e.g. OTG descriptors */
403 if (config->descriptors) {
404 status = usb_descriptor_fillbuf(next, len,
405 config->descriptors);
406 if (status < 0)
407 return status;
408 len -= status;
409 next += status;
410 }
411
412 /* add each function's descriptors */
413 list_for_each_entry(f, &config->functions, list) {
414 struct usb_descriptor_header **descriptors;
415
416 switch (speed) {
417 case USB_SPEED_SUPER:
418 descriptors = f->ss_descriptors;
419 break;
420 case USB_SPEED_HIGH:
421 descriptors = f->hs_descriptors;
422 break;
423 default:
424 descriptors = f->fs_descriptors;
425 }
426
427 if (!descriptors)
428 continue;
429 status = usb_descriptor_fillbuf(next, len,
430 (const struct usb_descriptor_header **) descriptors);
431 if (status < 0)
432 return status;
433 len -= status;
434 next += status;
435 }
436
437 len = next - buf;
438 c->wTotalLength = cpu_to_le16(len);
439 return len;
440 }
441
config_desc(struct usb_composite_dev * cdev,unsigned w_value)442 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
443 {
444 struct usb_gadget *gadget = cdev->gadget;
445 struct usb_configuration *c;
446 struct list_head *pos;
447 u8 type = w_value >> 8;
448 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
449
450 if (gadget->speed == USB_SPEED_SUPER)
451 speed = gadget->speed;
452 else if (gadget_is_dualspeed(gadget)) {
453 int hs = 0;
454 if (gadget->speed == USB_SPEED_HIGH)
455 hs = 1;
456 if (type == USB_DT_OTHER_SPEED_CONFIG)
457 hs = !hs;
458 if (hs)
459 speed = USB_SPEED_HIGH;
460
461 }
462
463 /* This is a lookup by config *INDEX* */
464 w_value &= 0xff;
465
466 pos = &cdev->configs;
467 c = cdev->os_desc_config;
468 if (c)
469 goto check_config;
470
471 while ((pos = pos->next) != &cdev->configs) {
472 c = list_entry(pos, typeof(*c), list);
473
474 /* skip OS Descriptors config which is handled separately */
475 if (c == cdev->os_desc_config)
476 continue;
477
478 check_config:
479 /* ignore configs that won't work at this speed */
480 switch (speed) {
481 case USB_SPEED_SUPER:
482 if (!c->superspeed)
483 continue;
484 break;
485 case USB_SPEED_HIGH:
486 if (!c->highspeed)
487 continue;
488 break;
489 default:
490 if (!c->fullspeed)
491 continue;
492 }
493
494 if (w_value == 0)
495 return config_buf(c, speed, cdev->req->buf, type);
496 w_value--;
497 }
498 return -EINVAL;
499 }
500
count_configs(struct usb_composite_dev * cdev,unsigned type)501 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
502 {
503 struct usb_gadget *gadget = cdev->gadget;
504 struct usb_configuration *c;
505 unsigned count = 0;
506 int hs = 0;
507 int ss = 0;
508
509 if (gadget_is_dualspeed(gadget)) {
510 if (gadget->speed == USB_SPEED_HIGH)
511 hs = 1;
512 if (gadget->speed == USB_SPEED_SUPER)
513 ss = 1;
514 if (type == USB_DT_DEVICE_QUALIFIER)
515 hs = !hs;
516 }
517 list_for_each_entry(c, &cdev->configs, list) {
518 /* ignore configs that won't work at this speed */
519 if (ss) {
520 if (!c->superspeed)
521 continue;
522 } else if (hs) {
523 if (!c->highspeed)
524 continue;
525 } else {
526 if (!c->fullspeed)
527 continue;
528 }
529 count++;
530 }
531 return count;
532 }
533
534 /**
535 * bos_desc() - prepares the BOS descriptor.
536 * @cdev: pointer to usb_composite device to generate the bos
537 * descriptor for
538 *
539 * This function generates the BOS (Binary Device Object)
540 * descriptor and its device capabilities descriptors. The BOS
541 * descriptor should be supported by a SuperSpeed device.
542 */
bos_desc(struct usb_composite_dev * cdev)543 static int bos_desc(struct usb_composite_dev *cdev)
544 {
545 struct usb_ext_cap_descriptor *usb_ext;
546 struct usb_ss_cap_descriptor *ss_cap;
547 struct usb_dcd_config_params dcd_config_params;
548 struct usb_bos_descriptor *bos = cdev->req->buf;
549
550 bos->bLength = USB_DT_BOS_SIZE;
551 bos->bDescriptorType = USB_DT_BOS;
552
553 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
554 bos->bNumDeviceCaps = 0;
555
556 /*
557 * A SuperSpeed device shall include the USB2.0 extension descriptor
558 * and shall support LPM when operating in USB2.0 HS mode.
559 */
560 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
561 bos->bNumDeviceCaps++;
562 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
563 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
564 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
565 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
566 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
567
568 /*
569 * The Superspeed USB Capability descriptor shall be implemented by all
570 * SuperSpeed devices.
571 */
572 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
573 bos->bNumDeviceCaps++;
574 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
575 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
576 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
577 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
578 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
579 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
580 USB_FULL_SPEED_OPERATION |
581 USB_HIGH_SPEED_OPERATION |
582 USB_5GBPS_OPERATION);
583 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
584
585 /* Get Controller configuration */
586 if (cdev->gadget->ops->get_config_params)
587 cdev->gadget->ops->get_config_params(&dcd_config_params);
588 else {
589 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
590 dcd_config_params.bU2DevExitLat =
591 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
592 }
593 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
594 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
595
596 return le16_to_cpu(bos->wTotalLength);
597 }
598
device_qual(struct usb_composite_dev * cdev)599 static void device_qual(struct usb_composite_dev *cdev)
600 {
601 struct usb_qualifier_descriptor *qual = cdev->req->buf;
602
603 qual->bLength = sizeof(*qual);
604 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
605 /* POLICY: same bcdUSB and device type info at both speeds */
606 qual->bcdUSB = cdev->desc.bcdUSB;
607 qual->bDeviceClass = cdev->desc.bDeviceClass;
608 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
609 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
610 /* ASSUME same EP0 fifo size at both speeds */
611 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
612 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
613 qual->bRESERVED = 0;
614 }
615
616 /*-------------------------------------------------------------------------*/
617
reset_config(struct usb_composite_dev * cdev)618 static void reset_config(struct usb_composite_dev *cdev)
619 {
620 struct usb_function *f;
621
622 DBG(cdev, "reset config\n");
623
624 list_for_each_entry(f, &cdev->config->functions, list) {
625 if (f->disable)
626 f->disable(f);
627
628 bitmap_zero(f->endpoints, 32);
629 }
630 cdev->config = NULL;
631 cdev->delayed_status = 0;
632 }
633
set_config(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl,unsigned number)634 static int set_config(struct usb_composite_dev *cdev,
635 const struct usb_ctrlrequest *ctrl, unsigned number)
636 {
637 struct usb_gadget *gadget = cdev->gadget;
638 struct usb_configuration *c = NULL;
639 int result = -EINVAL;
640 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
641 int tmp;
642
643 if (number) {
644 list_for_each_entry(c, &cdev->configs, list) {
645 if (c->bConfigurationValue == number) {
646 /*
647 * We disable the FDs of the previous
648 * configuration only if the new configuration
649 * is a valid one
650 */
651 if (cdev->config)
652 reset_config(cdev);
653 result = 0;
654 break;
655 }
656 }
657 if (result < 0)
658 goto done;
659 } else { /* Zero configuration value - need to reset the config */
660 if (cdev->config)
661 reset_config(cdev);
662 result = 0;
663 }
664
665 INFO(cdev, "%s config #%d: %s\n",
666 usb_speed_string(gadget->speed),
667 number, c ? c->label : "unconfigured");
668
669 if (!c)
670 goto done;
671
672 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
673 cdev->config = c;
674
675 /* Initialize all interfaces by setting them to altsetting zero. */
676 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
677 struct usb_function *f = c->interface[tmp];
678 struct usb_descriptor_header **descriptors;
679
680 if (!f)
681 break;
682
683 /*
684 * Record which endpoints are used by the function. This is used
685 * to dispatch control requests targeted at that endpoint to the
686 * function's setup callback instead of the current
687 * configuration's setup callback.
688 */
689 switch (gadget->speed) {
690 case USB_SPEED_SUPER:
691 descriptors = f->ss_descriptors;
692 break;
693 case USB_SPEED_HIGH:
694 descriptors = f->hs_descriptors;
695 break;
696 default:
697 descriptors = f->fs_descriptors;
698 }
699
700 for (; *descriptors; ++descriptors) {
701 struct usb_endpoint_descriptor *ep;
702 int addr;
703
704 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
705 continue;
706
707 ep = (struct usb_endpoint_descriptor *)*descriptors;
708 addr = ((ep->bEndpointAddress & 0x80) >> 3)
709 | (ep->bEndpointAddress & 0x0f);
710 set_bit(addr, f->endpoints);
711 }
712
713 result = f->set_alt(f, tmp, 0);
714 if (result < 0) {
715 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
716 tmp, f->name, f, result);
717
718 reset_config(cdev);
719 goto done;
720 }
721
722 if (result == USB_GADGET_DELAYED_STATUS) {
723 DBG(cdev,
724 "%s: interface %d (%s) requested delayed status\n",
725 __func__, tmp, f->name);
726 cdev->delayed_status++;
727 DBG(cdev, "delayed_status count %d\n",
728 cdev->delayed_status);
729 }
730 }
731
732 /* when we return, be sure our power usage is valid */
733 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
734 done:
735 usb_gadget_vbus_draw(gadget, power);
736 if (result >= 0 && cdev->delayed_status)
737 result = USB_GADGET_DELAYED_STATUS;
738 return result;
739 }
740
usb_add_config_only(struct usb_composite_dev * cdev,struct usb_configuration * config)741 int usb_add_config_only(struct usb_composite_dev *cdev,
742 struct usb_configuration *config)
743 {
744 struct usb_configuration *c;
745
746 if (!config->bConfigurationValue)
747 return -EINVAL;
748
749 /* Prevent duplicate configuration identifiers */
750 list_for_each_entry(c, &cdev->configs, list) {
751 if (c->bConfigurationValue == config->bConfigurationValue)
752 return -EBUSY;
753 }
754
755 config->cdev = cdev;
756 list_add_tail(&config->list, &cdev->configs);
757
758 INIT_LIST_HEAD(&config->functions);
759 config->next_interface_id = 0;
760 memset(config->interface, 0, sizeof(config->interface));
761
762 return 0;
763 }
764 EXPORT_SYMBOL_GPL(usb_add_config_only);
765
766 /**
767 * usb_add_config() - add a configuration to a device.
768 * @cdev: wraps the USB gadget
769 * @config: the configuration, with bConfigurationValue assigned
770 * @bind: the configuration's bind function
771 * Context: single threaded during gadget setup
772 *
773 * One of the main tasks of a composite @bind() routine is to
774 * add each of the configurations it supports, using this routine.
775 *
776 * This function returns the value of the configuration's @bind(), which
777 * is zero for success else a negative errno value. Binding configurations
778 * assigns global resources including string IDs, and per-configuration
779 * resources such as interface IDs and endpoints.
780 */
usb_add_config(struct usb_composite_dev * cdev,struct usb_configuration * config,int (* bind)(struct usb_configuration *))781 int usb_add_config(struct usb_composite_dev *cdev,
782 struct usb_configuration *config,
783 int (*bind)(struct usb_configuration *))
784 {
785 int status = -EINVAL;
786
787 if (!bind)
788 goto done;
789
790 DBG(cdev, "adding config #%u '%s'/%p\n",
791 config->bConfigurationValue,
792 config->label, config);
793
794 status = usb_add_config_only(cdev, config);
795 if (status)
796 goto done;
797
798 status = bind(config);
799 if (status < 0) {
800 while (!list_empty(&config->functions)) {
801 struct usb_function *f;
802
803 f = list_first_entry(&config->functions,
804 struct usb_function, list);
805 list_del(&f->list);
806 if (f->unbind) {
807 DBG(cdev, "unbind function '%s'/%p\n",
808 f->name, f);
809 f->unbind(config, f);
810 /* may free memory for "f" */
811 }
812 }
813 list_del(&config->list);
814 config->cdev = NULL;
815 } else {
816 unsigned i;
817
818 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
819 config->bConfigurationValue, config,
820 config->superspeed ? " super" : "",
821 config->highspeed ? " high" : "",
822 config->fullspeed
823 ? (gadget_is_dualspeed(cdev->gadget)
824 ? " full"
825 : " full/low")
826 : "");
827
828 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
829 struct usb_function *f = config->interface[i];
830
831 if (!f)
832 continue;
833 DBG(cdev, " interface %d = %s/%p\n",
834 i, f->name, f);
835 }
836 }
837
838 /* set_alt(), or next bind(), sets up
839 * ep->driver_data as needed.
840 */
841 usb_ep_autoconfig_reset(cdev->gadget);
842
843 done:
844 if (status)
845 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
846 config->bConfigurationValue, status);
847 return status;
848 }
849 EXPORT_SYMBOL_GPL(usb_add_config);
850
remove_config(struct usb_composite_dev * cdev,struct usb_configuration * config)851 static void remove_config(struct usb_composite_dev *cdev,
852 struct usb_configuration *config)
853 {
854 while (!list_empty(&config->functions)) {
855 struct usb_function *f;
856
857 f = list_first_entry(&config->functions,
858 struct usb_function, list);
859 list_del(&f->list);
860 if (f->unbind) {
861 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
862 f->unbind(config, f);
863 /* may free memory for "f" */
864 }
865 }
866 list_del(&config->list);
867 if (config->unbind) {
868 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
869 config->unbind(config);
870 /* may free memory for "c" */
871 }
872 }
873
874 /**
875 * usb_remove_config() - remove a configuration from a device.
876 * @cdev: wraps the USB gadget
877 * @config: the configuration
878 *
879 * Drivers must call usb_gadget_disconnect before calling this function
880 * to disconnect the device from the host and make sure the host will not
881 * try to enumerate the device while we are changing the config list.
882 */
usb_remove_config(struct usb_composite_dev * cdev,struct usb_configuration * config)883 void usb_remove_config(struct usb_composite_dev *cdev,
884 struct usb_configuration *config)
885 {
886 unsigned long flags;
887
888 spin_lock_irqsave(&cdev->lock, flags);
889
890 if (cdev->config == config)
891 reset_config(cdev);
892
893 spin_unlock_irqrestore(&cdev->lock, flags);
894
895 remove_config(cdev, config);
896 }
897
898 /*-------------------------------------------------------------------------*/
899
900 /* We support strings in multiple languages ... string descriptor zero
901 * says which languages are supported. The typical case will be that
902 * only one language (probably English) is used, with I18N handled on
903 * the host side.
904 */
905
collect_langs(struct usb_gadget_strings ** sp,__le16 * buf)906 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
907 {
908 const struct usb_gadget_strings *s;
909 __le16 language;
910 __le16 *tmp;
911
912 while (*sp) {
913 s = *sp;
914 language = cpu_to_le16(s->language);
915 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
916 if (*tmp == language)
917 goto repeat;
918 }
919 *tmp++ = language;
920 repeat:
921 sp++;
922 }
923 }
924
lookup_string(struct usb_gadget_strings ** sp,void * buf,u16 language,int id)925 static int lookup_string(
926 struct usb_gadget_strings **sp,
927 void *buf,
928 u16 language,
929 int id
930 )
931 {
932 struct usb_gadget_strings *s;
933 int value;
934
935 while (*sp) {
936 s = *sp++;
937 if (s->language != language)
938 continue;
939 value = usb_gadget_get_string(s, id, buf);
940 if (value > 0)
941 return value;
942 }
943 return -EINVAL;
944 }
945
get_string(struct usb_composite_dev * cdev,void * buf,u16 language,int id)946 static int get_string(struct usb_composite_dev *cdev,
947 void *buf, u16 language, int id)
948 {
949 struct usb_composite_driver *composite = cdev->driver;
950 struct usb_gadget_string_container *uc;
951 struct usb_configuration *c;
952 struct usb_function *f;
953 int len;
954
955 /* Yes, not only is USB's I18N support probably more than most
956 * folk will ever care about ... also, it's all supported here.
957 * (Except for UTF8 support for Unicode's "Astral Planes".)
958 */
959
960 /* 0 == report all available language codes */
961 if (id == 0) {
962 struct usb_string_descriptor *s = buf;
963 struct usb_gadget_strings **sp;
964
965 memset(s, 0, 256);
966 s->bDescriptorType = USB_DT_STRING;
967
968 sp = composite->strings;
969 if (sp)
970 collect_langs(sp, s->wData);
971
972 list_for_each_entry(c, &cdev->configs, list) {
973 sp = c->strings;
974 if (sp)
975 collect_langs(sp, s->wData);
976
977 list_for_each_entry(f, &c->functions, list) {
978 sp = f->strings;
979 if (sp)
980 collect_langs(sp, s->wData);
981 }
982 }
983 list_for_each_entry(uc, &cdev->gstrings, list) {
984 struct usb_gadget_strings **sp;
985
986 sp = get_containers_gs(uc);
987 collect_langs(sp, s->wData);
988 }
989
990 for (len = 0; len <= 126 && s->wData[len]; len++)
991 continue;
992 if (!len)
993 return -EINVAL;
994
995 s->bLength = 2 * (len + 1);
996 return s->bLength;
997 }
998
999 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1000 struct usb_os_string *b = buf;
1001 b->bLength = sizeof(*b);
1002 b->bDescriptorType = USB_DT_STRING;
1003 compiletime_assert(
1004 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1005 "qwSignature size must be equal to qw_sign");
1006 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1007 b->bMS_VendorCode = cdev->b_vendor_code;
1008 b->bPad = 0;
1009 return sizeof(*b);
1010 }
1011
1012 list_for_each_entry(uc, &cdev->gstrings, list) {
1013 struct usb_gadget_strings **sp;
1014
1015 sp = get_containers_gs(uc);
1016 len = lookup_string(sp, buf, language, id);
1017 if (len > 0)
1018 return len;
1019 }
1020
1021 /* String IDs are device-scoped, so we look up each string
1022 * table we're told about. These lookups are infrequent;
1023 * simpler-is-better here.
1024 */
1025 if (composite->strings) {
1026 len = lookup_string(composite->strings, buf, language, id);
1027 if (len > 0)
1028 return len;
1029 }
1030 list_for_each_entry(c, &cdev->configs, list) {
1031 if (c->strings) {
1032 len = lookup_string(c->strings, buf, language, id);
1033 if (len > 0)
1034 return len;
1035 }
1036 list_for_each_entry(f, &c->functions, list) {
1037 if (!f->strings)
1038 continue;
1039 len = lookup_string(f->strings, buf, language, id);
1040 if (len > 0)
1041 return len;
1042 }
1043 }
1044 return -EINVAL;
1045 }
1046
1047 /**
1048 * usb_string_id() - allocate an unused string ID
1049 * @cdev: the device whose string descriptor IDs are being allocated
1050 * Context: single threaded during gadget setup
1051 *
1052 * @usb_string_id() is called from bind() callbacks to allocate
1053 * string IDs. Drivers for functions, configurations, or gadgets will
1054 * then store that ID in the appropriate descriptors and string table.
1055 *
1056 * All string identifier should be allocated using this,
1057 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1058 * that for example different functions don't wrongly assign different
1059 * meanings to the same identifier.
1060 */
usb_string_id(struct usb_composite_dev * cdev)1061 int usb_string_id(struct usb_composite_dev *cdev)
1062 {
1063 if (cdev->next_string_id < 254) {
1064 /* string id 0 is reserved by USB spec for list of
1065 * supported languages */
1066 /* 255 reserved as well? -- mina86 */
1067 cdev->next_string_id++;
1068 return cdev->next_string_id;
1069 }
1070 return -ENODEV;
1071 }
1072 EXPORT_SYMBOL_GPL(usb_string_id);
1073
1074 /**
1075 * usb_string_ids() - allocate unused string IDs in batch
1076 * @cdev: the device whose string descriptor IDs are being allocated
1077 * @str: an array of usb_string objects to assign numbers to
1078 * Context: single threaded during gadget setup
1079 *
1080 * @usb_string_ids() is called from bind() callbacks to allocate
1081 * string IDs. Drivers for functions, configurations, or gadgets will
1082 * then copy IDs from the string table to the appropriate descriptors
1083 * and string table for other languages.
1084 *
1085 * All string identifier should be allocated using this,
1086 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1087 * example different functions don't wrongly assign different meanings
1088 * to the same identifier.
1089 */
usb_string_ids_tab(struct usb_composite_dev * cdev,struct usb_string * str)1090 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1091 {
1092 int next = cdev->next_string_id;
1093
1094 for (; str->s; ++str) {
1095 if (unlikely(next >= 254))
1096 return -ENODEV;
1097 str->id = ++next;
1098 }
1099
1100 cdev->next_string_id = next;
1101
1102 return 0;
1103 }
1104 EXPORT_SYMBOL_GPL(usb_string_ids_tab);
1105
copy_gadget_strings(struct usb_gadget_strings ** sp,unsigned n_gstrings,unsigned n_strings)1106 static struct usb_gadget_string_container *copy_gadget_strings(
1107 struct usb_gadget_strings **sp, unsigned n_gstrings,
1108 unsigned n_strings)
1109 {
1110 struct usb_gadget_string_container *uc;
1111 struct usb_gadget_strings **gs_array;
1112 struct usb_gadget_strings *gs;
1113 struct usb_string *s;
1114 unsigned mem;
1115 unsigned n_gs;
1116 unsigned n_s;
1117 void *stash;
1118
1119 mem = sizeof(*uc);
1120 mem += sizeof(void *) * (n_gstrings + 1);
1121 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1122 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1123 uc = kmalloc(mem, GFP_KERNEL);
1124 if (!uc)
1125 return ERR_PTR(-ENOMEM);
1126 gs_array = get_containers_gs(uc);
1127 stash = uc->stash;
1128 stash += sizeof(void *) * (n_gstrings + 1);
1129 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1130 struct usb_string *org_s;
1131
1132 gs_array[n_gs] = stash;
1133 gs = gs_array[n_gs];
1134 stash += sizeof(struct usb_gadget_strings);
1135 gs->language = sp[n_gs]->language;
1136 gs->strings = stash;
1137 org_s = sp[n_gs]->strings;
1138
1139 for (n_s = 0; n_s < n_strings; n_s++) {
1140 s = stash;
1141 stash += sizeof(struct usb_string);
1142 if (org_s->s)
1143 s->s = org_s->s;
1144 else
1145 s->s = "";
1146 org_s++;
1147 }
1148 s = stash;
1149 s->s = NULL;
1150 stash += sizeof(struct usb_string);
1151
1152 }
1153 gs_array[n_gs] = NULL;
1154 return uc;
1155 }
1156
1157 /**
1158 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1159 * @cdev: the device whose string descriptor IDs are being allocated
1160 * and attached.
1161 * @sp: an array of usb_gadget_strings to attach.
1162 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1163 *
1164 * This function will create a deep copy of usb_gadget_strings and usb_string
1165 * and attach it to the cdev. The actual string (usb_string.s) will not be
1166 * copied but only a referenced will be made. The struct usb_gadget_strings
1167 * array may contain multiple languges and should be NULL terminated.
1168 * The ->language pointer of each struct usb_gadget_strings has to contain the
1169 * same amount of entries.
1170 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1171 * usb_string entry of es-ES containts the translation of the first usb_string
1172 * entry of en-US. Therefore both entries become the same id assign.
1173 */
usb_gstrings_attach(struct usb_composite_dev * cdev,struct usb_gadget_strings ** sp,unsigned n_strings)1174 struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1175 struct usb_gadget_strings **sp, unsigned n_strings)
1176 {
1177 struct usb_gadget_string_container *uc;
1178 struct usb_gadget_strings **n_gs;
1179 unsigned n_gstrings = 0;
1180 unsigned i;
1181 int ret;
1182
1183 for (i = 0; sp[i]; i++)
1184 n_gstrings++;
1185
1186 if (!n_gstrings)
1187 return ERR_PTR(-EINVAL);
1188
1189 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1190 if (IS_ERR(uc))
1191 return ERR_CAST(uc);
1192
1193 n_gs = get_containers_gs(uc);
1194 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1195 if (ret)
1196 goto err;
1197
1198 for (i = 1; i < n_gstrings; i++) {
1199 struct usb_string *m_s;
1200 struct usb_string *s;
1201 unsigned n;
1202
1203 m_s = n_gs[0]->strings;
1204 s = n_gs[i]->strings;
1205 for (n = 0; n < n_strings; n++) {
1206 s->id = m_s->id;
1207 s++;
1208 m_s++;
1209 }
1210 }
1211 list_add_tail(&uc->list, &cdev->gstrings);
1212 return n_gs[0]->strings;
1213 err:
1214 kfree(uc);
1215 return ERR_PTR(ret);
1216 }
1217 EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1218
1219 /**
1220 * usb_string_ids_n() - allocate unused string IDs in batch
1221 * @c: the device whose string descriptor IDs are being allocated
1222 * @n: number of string IDs to allocate
1223 * Context: single threaded during gadget setup
1224 *
1225 * Returns the first requested ID. This ID and next @n-1 IDs are now
1226 * valid IDs. At least provided that @n is non-zero because if it
1227 * is, returns last requested ID which is now very useful information.
1228 *
1229 * @usb_string_ids_n() is called from bind() callbacks to allocate
1230 * string IDs. Drivers for functions, configurations, or gadgets will
1231 * then store that ID in the appropriate descriptors and string table.
1232 *
1233 * All string identifier should be allocated using this,
1234 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1235 * example different functions don't wrongly assign different meanings
1236 * to the same identifier.
1237 */
usb_string_ids_n(struct usb_composite_dev * c,unsigned n)1238 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1239 {
1240 unsigned next = c->next_string_id;
1241 if (unlikely(n > 254 || (unsigned)next + n > 254))
1242 return -ENODEV;
1243 c->next_string_id += n;
1244 return next + 1;
1245 }
1246 EXPORT_SYMBOL_GPL(usb_string_ids_n);
1247
1248 /*-------------------------------------------------------------------------*/
1249
composite_setup_complete(struct usb_ep * ep,struct usb_request * req)1250 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1251 {
1252 if (req->status || req->actual != req->length)
1253 DBG((struct usb_composite_dev *) ep->driver_data,
1254 "setup complete --> %d, %d/%d\n",
1255 req->status, req->actual, req->length);
1256 }
1257
count_ext_compat(struct usb_configuration * c)1258 static int count_ext_compat(struct usb_configuration *c)
1259 {
1260 int i, res;
1261
1262 res = 0;
1263 for (i = 0; i < c->next_interface_id; ++i) {
1264 struct usb_function *f;
1265 int j;
1266
1267 f = c->interface[i];
1268 for (j = 0; j < f->os_desc_n; ++j) {
1269 struct usb_os_desc *d;
1270
1271 if (i != f->os_desc_table[j].if_id)
1272 continue;
1273 d = f->os_desc_table[j].os_desc;
1274 if (d && d->ext_compat_id)
1275 ++res;
1276 }
1277 }
1278 BUG_ON(res > 255);
1279 return res;
1280 }
1281
fill_ext_compat(struct usb_configuration * c,u8 * buf)1282 static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1283 {
1284 int i, count;
1285
1286 count = 16;
1287 for (i = 0; i < c->next_interface_id; ++i) {
1288 struct usb_function *f;
1289 int j;
1290
1291 f = c->interface[i];
1292 for (j = 0; j < f->os_desc_n; ++j) {
1293 struct usb_os_desc *d;
1294
1295 if (i != f->os_desc_table[j].if_id)
1296 continue;
1297 d = f->os_desc_table[j].os_desc;
1298 if (d && d->ext_compat_id) {
1299 *buf++ = i;
1300 *buf++ = 0x01;
1301 memcpy(buf, d->ext_compat_id, 16);
1302 buf += 22;
1303 } else {
1304 ++buf;
1305 *buf = 0x01;
1306 buf += 23;
1307 }
1308 count += 24;
1309 if (count >= 4096)
1310 return;
1311 }
1312 }
1313 }
1314
count_ext_prop(struct usb_configuration * c,int interface)1315 static int count_ext_prop(struct usb_configuration *c, int interface)
1316 {
1317 struct usb_function *f;
1318 int j;
1319
1320 f = c->interface[interface];
1321 for (j = 0; j < f->os_desc_n; ++j) {
1322 struct usb_os_desc *d;
1323
1324 if (interface != f->os_desc_table[j].if_id)
1325 continue;
1326 d = f->os_desc_table[j].os_desc;
1327 if (d && d->ext_compat_id)
1328 return d->ext_prop_count;
1329 }
1330 return 0;
1331 }
1332
len_ext_prop(struct usb_configuration * c,int interface)1333 static int len_ext_prop(struct usb_configuration *c, int interface)
1334 {
1335 struct usb_function *f;
1336 struct usb_os_desc *d;
1337 int j, res;
1338
1339 res = 10; /* header length */
1340 f = c->interface[interface];
1341 for (j = 0; j < f->os_desc_n; ++j) {
1342 if (interface != f->os_desc_table[j].if_id)
1343 continue;
1344 d = f->os_desc_table[j].os_desc;
1345 if (d)
1346 return min(res + d->ext_prop_len, 4096);
1347 }
1348 return res;
1349 }
1350
fill_ext_prop(struct usb_configuration * c,int interface,u8 * buf)1351 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1352 {
1353 struct usb_function *f;
1354 struct usb_os_desc *d;
1355 struct usb_os_desc_ext_prop *ext_prop;
1356 int j, count, n, ret;
1357 u8 *start = buf;
1358
1359 f = c->interface[interface];
1360 for (j = 0; j < f->os_desc_n; ++j) {
1361 if (interface != f->os_desc_table[j].if_id)
1362 continue;
1363 d = f->os_desc_table[j].os_desc;
1364 if (d)
1365 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1366 /* 4kB minus header length */
1367 n = buf - start;
1368 if (n >= 4086)
1369 return 0;
1370
1371 count = ext_prop->data_len +
1372 ext_prop->name_len + 14;
1373 if (count > 4086 - n)
1374 return -EINVAL;
1375 usb_ext_prop_put_size(buf, count);
1376 usb_ext_prop_put_type(buf, ext_prop->type);
1377 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1378 ext_prop->name_len);
1379 if (ret < 0)
1380 return ret;
1381 switch (ext_prop->type) {
1382 case USB_EXT_PROP_UNICODE:
1383 case USB_EXT_PROP_UNICODE_ENV:
1384 case USB_EXT_PROP_UNICODE_LINK:
1385 usb_ext_prop_put_unicode(buf, ret,
1386 ext_prop->data,
1387 ext_prop->data_len);
1388 break;
1389 case USB_EXT_PROP_BINARY:
1390 usb_ext_prop_put_binary(buf, ret,
1391 ext_prop->data,
1392 ext_prop->data_len);
1393 break;
1394 case USB_EXT_PROP_LE32:
1395 /* not implemented */
1396 case USB_EXT_PROP_BE32:
1397 /* not implemented */
1398 default:
1399 return -EINVAL;
1400 }
1401 buf += count;
1402 }
1403 }
1404
1405 return 0;
1406 }
1407
1408 /*
1409 * The setup() callback implements all the ep0 functionality that's
1410 * not handled lower down, in hardware or the hardware driver(like
1411 * device and endpoint feature flags, and their status). It's all
1412 * housekeeping for the gadget function we're implementing. Most of
1413 * the work is in config and function specific setup.
1414 */
1415 int
composite_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)1416 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1417 {
1418 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1419 struct usb_request *req = cdev->req;
1420 int value = -EOPNOTSUPP;
1421 int status = 0;
1422 u16 w_index = le16_to_cpu(ctrl->wIndex);
1423 u8 intf = w_index & 0xFF;
1424 u16 w_value = le16_to_cpu(ctrl->wValue);
1425 u16 w_length = le16_to_cpu(ctrl->wLength);
1426 struct usb_function *f = NULL;
1427 u8 endp;
1428
1429 /* partial re-init of the response message; the function or the
1430 * gadget might need to intercept e.g. a control-OUT completion
1431 * when we delegate to it.
1432 */
1433 req->zero = 0;
1434 req->complete = composite_setup_complete;
1435 req->length = 0;
1436 gadget->ep0->driver_data = cdev;
1437
1438 switch (ctrl->bRequest) {
1439
1440 /* we handle all standard USB descriptors */
1441 case USB_REQ_GET_DESCRIPTOR:
1442 if (ctrl->bRequestType != USB_DIR_IN)
1443 goto unknown;
1444 switch (w_value >> 8) {
1445
1446 case USB_DT_DEVICE:
1447 cdev->desc.bNumConfigurations =
1448 count_configs(cdev, USB_DT_DEVICE);
1449 cdev->desc.bMaxPacketSize0 =
1450 cdev->gadget->ep0->maxpacket;
1451 if (gadget_is_superspeed(gadget)) {
1452 if (gadget->speed >= USB_SPEED_SUPER) {
1453 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
1454 cdev->desc.bMaxPacketSize0 = 9;
1455 } else {
1456 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1457 }
1458 }
1459
1460 value = min(w_length, (u16) sizeof cdev->desc);
1461 memcpy(req->buf, &cdev->desc, value);
1462 break;
1463 case USB_DT_DEVICE_QUALIFIER:
1464 if (!gadget_is_dualspeed(gadget) ||
1465 gadget->speed >= USB_SPEED_SUPER)
1466 break;
1467 device_qual(cdev);
1468 value = min_t(int, w_length,
1469 sizeof(struct usb_qualifier_descriptor));
1470 break;
1471 case USB_DT_OTHER_SPEED_CONFIG:
1472 if (!gadget_is_dualspeed(gadget) ||
1473 gadget->speed >= USB_SPEED_SUPER)
1474 break;
1475 /* FALLTHROUGH */
1476 case USB_DT_CONFIG:
1477 value = config_desc(cdev, w_value);
1478 if (value >= 0)
1479 value = min(w_length, (u16) value);
1480 break;
1481 case USB_DT_STRING:
1482 value = get_string(cdev, req->buf,
1483 w_index, w_value & 0xff);
1484 if (value >= 0)
1485 value = min(w_length, (u16) value);
1486 break;
1487 case USB_DT_BOS:
1488 if (gadget_is_superspeed(gadget)) {
1489 value = bos_desc(cdev);
1490 value = min(w_length, (u16) value);
1491 }
1492 break;
1493 }
1494 break;
1495
1496 /* any number of configs can work */
1497 case USB_REQ_SET_CONFIGURATION:
1498 if (ctrl->bRequestType != 0)
1499 goto unknown;
1500 if (gadget_is_otg(gadget)) {
1501 if (gadget->a_hnp_support)
1502 DBG(cdev, "HNP available\n");
1503 else if (gadget->a_alt_hnp_support)
1504 DBG(cdev, "HNP on another port\n");
1505 else
1506 VDBG(cdev, "HNP inactive\n");
1507 }
1508 spin_lock(&cdev->lock);
1509 value = set_config(cdev, ctrl, w_value);
1510 spin_unlock(&cdev->lock);
1511 break;
1512 case USB_REQ_GET_CONFIGURATION:
1513 if (ctrl->bRequestType != USB_DIR_IN)
1514 goto unknown;
1515 if (cdev->config)
1516 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1517 else
1518 *(u8 *)req->buf = 0;
1519 value = min(w_length, (u16) 1);
1520 break;
1521
1522 /* function drivers must handle get/set altsetting; if there's
1523 * no get() method, we know only altsetting zero works.
1524 */
1525 case USB_REQ_SET_INTERFACE:
1526 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1527 goto unknown;
1528 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1529 break;
1530 f = cdev->config->interface[intf];
1531 if (!f)
1532 break;
1533 if (w_value && !f->set_alt)
1534 break;
1535 value = f->set_alt(f, w_index, w_value);
1536 if (value == USB_GADGET_DELAYED_STATUS) {
1537 DBG(cdev,
1538 "%s: interface %d (%s) requested delayed status\n",
1539 __func__, intf, f->name);
1540 cdev->delayed_status++;
1541 DBG(cdev, "delayed_status count %d\n",
1542 cdev->delayed_status);
1543 }
1544 break;
1545 case USB_REQ_GET_INTERFACE:
1546 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1547 goto unknown;
1548 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1549 break;
1550 f = cdev->config->interface[intf];
1551 if (!f)
1552 break;
1553 /* lots of interfaces only need altsetting zero... */
1554 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1555 if (value < 0)
1556 break;
1557 *((u8 *)req->buf) = value;
1558 value = min(w_length, (u16) 1);
1559 break;
1560
1561 /*
1562 * USB 3.0 additions:
1563 * Function driver should handle get_status request. If such cb
1564 * wasn't supplied we respond with default value = 0
1565 * Note: function driver should supply such cb only for the first
1566 * interface of the function
1567 */
1568 case USB_REQ_GET_STATUS:
1569 if (!gadget_is_superspeed(gadget))
1570 goto unknown;
1571 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1572 goto unknown;
1573 value = 2; /* This is the length of the get_status reply */
1574 put_unaligned_le16(0, req->buf);
1575 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1576 break;
1577 f = cdev->config->interface[intf];
1578 if (!f)
1579 break;
1580 status = f->get_status ? f->get_status(f) : 0;
1581 if (status < 0)
1582 break;
1583 put_unaligned_le16(status & 0x0000ffff, req->buf);
1584 break;
1585 /*
1586 * Function drivers should handle SetFeature/ClearFeature
1587 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1588 * only for the first interface of the function
1589 */
1590 case USB_REQ_CLEAR_FEATURE:
1591 case USB_REQ_SET_FEATURE:
1592 if (!gadget_is_superspeed(gadget))
1593 goto unknown;
1594 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1595 goto unknown;
1596 switch (w_value) {
1597 case USB_INTRF_FUNC_SUSPEND:
1598 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1599 break;
1600 f = cdev->config->interface[intf];
1601 if (!f)
1602 break;
1603 value = 0;
1604 if (f->func_suspend)
1605 value = f->func_suspend(f, w_index >> 8);
1606 if (value < 0) {
1607 ERROR(cdev,
1608 "func_suspend() returned error %d\n",
1609 value);
1610 value = 0;
1611 }
1612 break;
1613 }
1614 break;
1615 default:
1616 unknown:
1617 /*
1618 * OS descriptors handling
1619 */
1620 if (cdev->use_os_string && cdev->os_desc_config &&
1621 (ctrl->bRequest & USB_TYPE_VENDOR) &&
1622 ctrl->bRequest == cdev->b_vendor_code) {
1623 struct usb_request *req;
1624 struct usb_configuration *os_desc_cfg;
1625 u8 *buf;
1626 int interface;
1627 int count = 0;
1628
1629 req = cdev->os_desc_req;
1630 req->complete = composite_setup_complete;
1631 buf = req->buf;
1632 os_desc_cfg = cdev->os_desc_config;
1633 memset(buf, 0, w_length);
1634 buf[5] = 0x01;
1635 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1636 case USB_RECIP_DEVICE:
1637 if (w_index != 0x4 || (w_value >> 8))
1638 break;
1639 buf[6] = w_index;
1640 if (w_length == 0x10) {
1641 /* Number of ext compat interfaces */
1642 count = count_ext_compat(os_desc_cfg);
1643 buf[8] = count;
1644 count *= 24; /* 24 B/ext compat desc */
1645 count += 16; /* header */
1646 put_unaligned_le32(count, buf);
1647 value = w_length;
1648 } else {
1649 /* "extended compatibility ID"s */
1650 count = count_ext_compat(os_desc_cfg);
1651 buf[8] = count;
1652 count *= 24; /* 24 B/ext compat desc */
1653 count += 16; /* header */
1654 put_unaligned_le32(count, buf);
1655 buf += 16;
1656 fill_ext_compat(os_desc_cfg, buf);
1657 value = w_length;
1658 }
1659 break;
1660 case USB_RECIP_INTERFACE:
1661 if (w_index != 0x5 || (w_value >> 8))
1662 break;
1663 interface = w_value & 0xFF;
1664 buf[6] = w_index;
1665 if (w_length == 0x0A) {
1666 count = count_ext_prop(os_desc_cfg,
1667 interface);
1668 put_unaligned_le16(count, buf + 8);
1669 count = len_ext_prop(os_desc_cfg,
1670 interface);
1671 put_unaligned_le32(count, buf);
1672
1673 value = w_length;
1674 } else {
1675 count = count_ext_prop(os_desc_cfg,
1676 interface);
1677 put_unaligned_le16(count, buf + 8);
1678 count = len_ext_prop(os_desc_cfg,
1679 interface);
1680 put_unaligned_le32(count, buf);
1681 buf += 10;
1682 value = fill_ext_prop(os_desc_cfg,
1683 interface, buf);
1684 if (value < 0)
1685 return value;
1686
1687 value = w_length;
1688 }
1689 break;
1690 }
1691 req->length = value;
1692 req->zero = value < w_length;
1693 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1694 if (value < 0) {
1695 DBG(cdev, "ep_queue --> %d\n", value);
1696 req->status = 0;
1697 composite_setup_complete(gadget->ep0, req);
1698 }
1699 return value;
1700 }
1701
1702 VDBG(cdev,
1703 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1704 ctrl->bRequestType, ctrl->bRequest,
1705 w_value, w_index, w_length);
1706
1707 /* functions always handle their interfaces and endpoints...
1708 * punt other recipients (other, WUSB, ...) to the current
1709 * configuration code.
1710 *
1711 * REVISIT it could make sense to let the composite device
1712 * take such requests too, if that's ever needed: to work
1713 * in config 0, etc.
1714 */
1715 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1716 case USB_RECIP_INTERFACE:
1717 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1718 break;
1719 f = cdev->config->interface[intf];
1720 break;
1721
1722 case USB_RECIP_ENDPOINT:
1723 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1724 list_for_each_entry(f, &cdev->config->functions, list) {
1725 if (test_bit(endp, f->endpoints))
1726 break;
1727 }
1728 if (&f->list == &cdev->config->functions)
1729 f = NULL;
1730 break;
1731 }
1732
1733 if (f && f->setup)
1734 value = f->setup(f, ctrl);
1735 else {
1736 struct usb_configuration *c;
1737
1738 c = cdev->config;
1739 if (!c)
1740 goto done;
1741
1742 /* try current config's setup */
1743 if (c->setup) {
1744 value = c->setup(c, ctrl);
1745 goto done;
1746 }
1747
1748 /* try the only function in the current config */
1749 if (!list_is_singular(&c->functions))
1750 goto done;
1751 f = list_first_entry(&c->functions, struct usb_function,
1752 list);
1753 if (f->setup)
1754 value = f->setup(f, ctrl);
1755 }
1756
1757 goto done;
1758 }
1759
1760 /* respond with data transfer before status phase? */
1761 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
1762 req->length = value;
1763 req->zero = value < w_length;
1764 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1765 if (value < 0) {
1766 DBG(cdev, "ep_queue --> %d\n", value);
1767 req->status = 0;
1768 composite_setup_complete(gadget->ep0, req);
1769 }
1770 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1771 WARN(cdev,
1772 "%s: Delayed status not supported for w_length != 0",
1773 __func__);
1774 }
1775
1776 done:
1777 /* device either stalls (value < 0) or reports success */
1778 return value;
1779 }
1780
composite_disconnect(struct usb_gadget * gadget)1781 void composite_disconnect(struct usb_gadget *gadget)
1782 {
1783 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1784 unsigned long flags;
1785
1786 if (cdev == NULL) {
1787 WARN(1, "%s: Calling disconnect on a Gadget that is \
1788 not connected\n", __func__);
1789 return;
1790 }
1791
1792 /* REVISIT: should we have config and device level
1793 * disconnect callbacks?
1794 */
1795 spin_lock_irqsave(&cdev->lock, flags);
1796 if (cdev->config)
1797 reset_config(cdev);
1798 if (cdev->driver->disconnect)
1799 cdev->driver->disconnect(cdev);
1800 spin_unlock_irqrestore(&cdev->lock, flags);
1801 }
1802
1803 /*-------------------------------------------------------------------------*/
1804
suspended_show(struct device * dev,struct device_attribute * attr,char * buf)1805 static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1806 char *buf)
1807 {
1808 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1809 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1810
1811 return sprintf(buf, "%d\n", cdev->suspended);
1812 }
1813 static DEVICE_ATTR_RO(suspended);
1814
__composite_unbind(struct usb_gadget * gadget,bool unbind_driver)1815 static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
1816 {
1817 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1818 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1819 struct usb_string *dev_str = gstr->strings;
1820
1821 /* composite_disconnect() must already have been called
1822 * by the underlying peripheral controller driver!
1823 * so there's no i/o concurrency that could affect the
1824 * state protected by cdev->lock.
1825 */
1826 WARN_ON(cdev->config);
1827
1828 while (!list_empty(&cdev->configs)) {
1829 struct usb_configuration *c;
1830 c = list_first_entry(&cdev->configs,
1831 struct usb_configuration, list);
1832 remove_config(cdev, c);
1833 }
1834 if (cdev->driver->unbind && unbind_driver)
1835 cdev->driver->unbind(cdev);
1836
1837 composite_dev_cleanup(cdev);
1838
1839 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
1840 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
1841
1842 kfree(cdev->def_manufacturer);
1843 kfree(cdev);
1844 set_gadget_data(gadget, NULL);
1845 }
1846
composite_unbind(struct usb_gadget * gadget)1847 static void composite_unbind(struct usb_gadget *gadget)
1848 {
1849 __composite_unbind(gadget, true);
1850 }
1851
update_unchanged_dev_desc(struct usb_device_descriptor * new,const struct usb_device_descriptor * old)1852 static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1853 const struct usb_device_descriptor *old)
1854 {
1855 __le16 idVendor;
1856 __le16 idProduct;
1857 __le16 bcdDevice;
1858 u8 iSerialNumber;
1859 u8 iManufacturer;
1860 u8 iProduct;
1861
1862 /*
1863 * these variables may have been set in
1864 * usb_composite_overwrite_options()
1865 */
1866 idVendor = new->idVendor;
1867 idProduct = new->idProduct;
1868 bcdDevice = new->bcdDevice;
1869 iSerialNumber = new->iSerialNumber;
1870 iManufacturer = new->iManufacturer;
1871 iProduct = new->iProduct;
1872
1873 *new = *old;
1874 if (idVendor)
1875 new->idVendor = idVendor;
1876 if (idProduct)
1877 new->idProduct = idProduct;
1878 if (bcdDevice)
1879 new->bcdDevice = bcdDevice;
1880 else
1881 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
1882 if (iSerialNumber)
1883 new->iSerialNumber = iSerialNumber;
1884 if (iManufacturer)
1885 new->iManufacturer = iManufacturer;
1886 if (iProduct)
1887 new->iProduct = iProduct;
1888 }
1889
composite_dev_prepare(struct usb_composite_driver * composite,struct usb_composite_dev * cdev)1890 int composite_dev_prepare(struct usb_composite_driver *composite,
1891 struct usb_composite_dev *cdev)
1892 {
1893 struct usb_gadget *gadget = cdev->gadget;
1894 int ret = -ENOMEM;
1895
1896 /* preallocate control response and buffer */
1897 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1898 if (!cdev->req)
1899 return -ENOMEM;
1900
1901 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
1902 if (!cdev->req->buf)
1903 goto fail;
1904
1905 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
1906 if (ret)
1907 goto fail_dev;
1908
1909 cdev->req->complete = composite_setup_complete;
1910 gadget->ep0->driver_data = cdev;
1911
1912 cdev->driver = composite;
1913
1914 /*
1915 * As per USB compliance update, a device that is actively drawing
1916 * more than 100mA from USB must report itself as bus-powered in
1917 * the GetStatus(DEVICE) call.
1918 */
1919 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1920 usb_gadget_set_selfpowered(gadget);
1921
1922 /* interface and string IDs start at zero via kzalloc.
1923 * we force endpoints to start unassigned; few controller
1924 * drivers will zero ep->driver_data.
1925 */
1926 usb_ep_autoconfig_reset(gadget);
1927 return 0;
1928 fail_dev:
1929 kfree(cdev->req->buf);
1930 fail:
1931 usb_ep_free_request(gadget->ep0, cdev->req);
1932 cdev->req = NULL;
1933 return ret;
1934 }
1935
composite_os_desc_req_prepare(struct usb_composite_dev * cdev,struct usb_ep * ep0)1936 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
1937 struct usb_ep *ep0)
1938 {
1939 int ret = 0;
1940
1941 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
1942 if (!cdev->os_desc_req) {
1943 ret = PTR_ERR(cdev->os_desc_req);
1944 goto end;
1945 }
1946
1947 /* OS feature descriptor length <= 4kB */
1948 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
1949 if (!cdev->os_desc_req->buf) {
1950 ret = PTR_ERR(cdev->os_desc_req->buf);
1951 kfree(cdev->os_desc_req);
1952 goto end;
1953 }
1954 cdev->os_desc_req->complete = composite_setup_complete;
1955 end:
1956 return ret;
1957 }
1958
composite_dev_cleanup(struct usb_composite_dev * cdev)1959 void composite_dev_cleanup(struct usb_composite_dev *cdev)
1960 {
1961 struct usb_gadget_string_container *uc, *tmp;
1962
1963 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
1964 list_del(&uc->list);
1965 kfree(uc);
1966 }
1967 if (cdev->os_desc_req) {
1968 kfree(cdev->os_desc_req->buf);
1969 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
1970 }
1971 if (cdev->req) {
1972 kfree(cdev->req->buf);
1973 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
1974 }
1975 cdev->next_string_id = 0;
1976 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
1977 }
1978
composite_bind(struct usb_gadget * gadget,struct usb_gadget_driver * gdriver)1979 static int composite_bind(struct usb_gadget *gadget,
1980 struct usb_gadget_driver *gdriver)
1981 {
1982 struct usb_composite_dev *cdev;
1983 struct usb_composite_driver *composite = to_cdriver(gdriver);
1984 int status = -ENOMEM;
1985
1986 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1987 if (!cdev)
1988 return status;
1989
1990 spin_lock_init(&cdev->lock);
1991 cdev->gadget = gadget;
1992 set_gadget_data(gadget, cdev);
1993 INIT_LIST_HEAD(&cdev->configs);
1994 INIT_LIST_HEAD(&cdev->gstrings);
1995
1996 status = composite_dev_prepare(composite, cdev);
1997 if (status)
1998 goto fail;
1999
2000 /* composite gadget needs to assign strings for whole device (like
2001 * serial number), register function drivers, potentially update
2002 * power state and consumption, etc
2003 */
2004 status = composite->bind(cdev);
2005 if (status < 0)
2006 goto fail;
2007
2008 if (cdev->use_os_string) {
2009 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2010 if (status)
2011 goto fail;
2012 }
2013
2014 update_unchanged_dev_desc(&cdev->desc, composite->dev);
2015
2016 /* has userspace failed to provide a serial number? */
2017 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2018 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2019
2020 INFO(cdev, "%s ready\n", composite->name);
2021 return 0;
2022
2023 fail:
2024 __composite_unbind(gadget, false);
2025 return status;
2026 }
2027
2028 /*-------------------------------------------------------------------------*/
2029
2030 static void
composite_suspend(struct usb_gadget * gadget)2031 composite_suspend(struct usb_gadget *gadget)
2032 {
2033 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2034 struct usb_function *f;
2035
2036 /* REVISIT: should we have config level
2037 * suspend/resume callbacks?
2038 */
2039 DBG(cdev, "suspend\n");
2040 if (cdev->config) {
2041 list_for_each_entry(f, &cdev->config->functions, list) {
2042 if (f->suspend)
2043 f->suspend(f);
2044 }
2045 }
2046 if (cdev->driver->suspend)
2047 cdev->driver->suspend(cdev);
2048
2049 cdev->suspended = 1;
2050
2051 usb_gadget_vbus_draw(gadget, 2);
2052 }
2053
2054 static void
composite_resume(struct usb_gadget * gadget)2055 composite_resume(struct usb_gadget *gadget)
2056 {
2057 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2058 struct usb_function *f;
2059 u16 maxpower;
2060
2061 /* REVISIT: should we have config level
2062 * suspend/resume callbacks?
2063 */
2064 DBG(cdev, "resume\n");
2065 if (cdev->driver->resume)
2066 cdev->driver->resume(cdev);
2067 if (cdev->config) {
2068 list_for_each_entry(f, &cdev->config->functions, list) {
2069 if (f->resume)
2070 f->resume(f);
2071 }
2072
2073 maxpower = cdev->config->MaxPower;
2074
2075 usb_gadget_vbus_draw(gadget, maxpower ?
2076 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
2077 }
2078
2079 cdev->suspended = 0;
2080 }
2081
2082 /*-------------------------------------------------------------------------*/
2083
2084 static const struct usb_gadget_driver composite_driver_template = {
2085 .bind = composite_bind,
2086 .unbind = composite_unbind,
2087
2088 .setup = composite_setup,
2089 .reset = composite_disconnect,
2090 .disconnect = composite_disconnect,
2091
2092 .suspend = composite_suspend,
2093 .resume = composite_resume,
2094
2095 .driver = {
2096 .owner = THIS_MODULE,
2097 },
2098 };
2099
2100 /**
2101 * usb_composite_probe() - register a composite driver
2102 * @driver: the driver to register
2103 *
2104 * Context: single threaded during gadget setup
2105 *
2106 * This function is used to register drivers using the composite driver
2107 * framework. The return value is zero, or a negative errno value.
2108 * Those values normally come from the driver's @bind method, which does
2109 * all the work of setting up the driver to match the hardware.
2110 *
2111 * On successful return, the gadget is ready to respond to requests from
2112 * the host, unless one of its components invokes usb_gadget_disconnect()
2113 * while it was binding. That would usually be done in order to wait for
2114 * some userspace participation.
2115 */
usb_composite_probe(struct usb_composite_driver * driver)2116 int usb_composite_probe(struct usb_composite_driver *driver)
2117 {
2118 struct usb_gadget_driver *gadget_driver;
2119
2120 if (!driver || !driver->dev || !driver->bind)
2121 return -EINVAL;
2122
2123 if (!driver->name)
2124 driver->name = "composite";
2125
2126 driver->gadget_driver = composite_driver_template;
2127 gadget_driver = &driver->gadget_driver;
2128
2129 gadget_driver->function = (char *) driver->name;
2130 gadget_driver->driver.name = driver->name;
2131 gadget_driver->max_speed = driver->max_speed;
2132
2133 return usb_gadget_probe_driver(gadget_driver);
2134 }
2135 EXPORT_SYMBOL_GPL(usb_composite_probe);
2136
2137 /**
2138 * usb_composite_unregister() - unregister a composite driver
2139 * @driver: the driver to unregister
2140 *
2141 * This function is used to unregister drivers using the composite
2142 * driver framework.
2143 */
usb_composite_unregister(struct usb_composite_driver * driver)2144 void usb_composite_unregister(struct usb_composite_driver *driver)
2145 {
2146 usb_gadget_unregister_driver(&driver->gadget_driver);
2147 }
2148 EXPORT_SYMBOL_GPL(usb_composite_unregister);
2149
2150 /**
2151 * usb_composite_setup_continue() - Continue with the control transfer
2152 * @cdev: the composite device who's control transfer was kept waiting
2153 *
2154 * This function must be called by the USB function driver to continue
2155 * with the control transfer's data/status stage in case it had requested to
2156 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2157 * can request the composite framework to delay the setup request's data/status
2158 * stages by returning USB_GADGET_DELAYED_STATUS.
2159 */
usb_composite_setup_continue(struct usb_composite_dev * cdev)2160 void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2161 {
2162 int value;
2163 struct usb_request *req = cdev->req;
2164 unsigned long flags;
2165
2166 DBG(cdev, "%s\n", __func__);
2167 spin_lock_irqsave(&cdev->lock, flags);
2168
2169 if (cdev->delayed_status == 0) {
2170 WARN(cdev, "%s: Unexpected call\n", __func__);
2171
2172 } else if (--cdev->delayed_status == 0) {
2173 DBG(cdev, "%s: Completing delayed status\n", __func__);
2174 req->length = 0;
2175 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
2176 if (value < 0) {
2177 DBG(cdev, "ep_queue --> %d\n", value);
2178 req->status = 0;
2179 composite_setup_complete(cdev->gadget->ep0, req);
2180 }
2181 }
2182
2183 spin_unlock_irqrestore(&cdev->lock, flags);
2184 }
2185 EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
2186
composite_default_mfr(struct usb_gadget * gadget)2187 static char *composite_default_mfr(struct usb_gadget *gadget)
2188 {
2189 char *mfr;
2190 int len;
2191
2192 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2193 init_utsname()->release, gadget->name);
2194 len++;
2195 mfr = kmalloc(len, GFP_KERNEL);
2196 if (!mfr)
2197 return NULL;
2198 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2199 init_utsname()->release, gadget->name);
2200 return mfr;
2201 }
2202
usb_composite_overwrite_options(struct usb_composite_dev * cdev,struct usb_composite_overwrite * covr)2203 void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2204 struct usb_composite_overwrite *covr)
2205 {
2206 struct usb_device_descriptor *desc = &cdev->desc;
2207 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2208 struct usb_string *dev_str = gstr->strings;
2209
2210 if (covr->idVendor)
2211 desc->idVendor = cpu_to_le16(covr->idVendor);
2212
2213 if (covr->idProduct)
2214 desc->idProduct = cpu_to_le16(covr->idProduct);
2215
2216 if (covr->bcdDevice)
2217 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
2218
2219 if (covr->serial_number) {
2220 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2221 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2222 }
2223 if (covr->manufacturer) {
2224 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2225 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
2226
2227 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2228 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2229 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2230 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
2231 }
2232
2233 if (covr->product) {
2234 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2235 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2236 }
2237 }
2238 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
2239
2240 MODULE_LICENSE("GPL");
2241 MODULE_AUTHOR("David Brownell");
2242