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