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