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