1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/kallsyms.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/device.h>
27#include <linux/utsname.h>
28
29#include <linux/usb/composite.h>
30
31
32
33
34
35
36
37
38
39
40#define USB_BUFSIZ 1024
41
42static struct usb_composite_driver *composite;
43static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
44
45
46
47
48
49
50static ushort idVendor;
51module_param(idVendor, ushort, 0);
52MODULE_PARM_DESC(idVendor, "USB Vendor ID");
53
54static ushort idProduct;
55module_param(idProduct, ushort, 0);
56MODULE_PARM_DESC(idProduct, "USB Product ID");
57
58static ushort bcdDevice;
59module_param(bcdDevice, ushort, 0);
60MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
61
62static char *iManufacturer;
63module_param(iManufacturer, charp, 0);
64MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
65
66static char *iProduct;
67module_param(iProduct, charp, 0);
68MODULE_PARM_DESC(iProduct, "USB Product string");
69
70static char *iSerialNumber;
71module_param(iSerialNumber, charp, 0);
72MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
73
74static char composite_manufacturer[50];
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92int usb_add_function(struct usb_configuration *config,
93 struct usb_function *function)
94{
95 int value = -EINVAL;
96
97 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
98 function->name, function,
99 config->label, config);
100
101 if (!function->set_alt || !function->disable)
102 goto done;
103
104 function->config = config;
105 list_add_tail(&function->list, &config->functions);
106
107
108 if (function->bind) {
109 value = function->bind(config, function);
110 if (value < 0) {
111 list_del(&function->list);
112 function->config = NULL;
113 }
114 } else
115 value = 0;
116
117
118
119
120
121
122 if (!config->fullspeed && function->descriptors)
123 config->fullspeed = true;
124 if (!config->highspeed && function->hs_descriptors)
125 config->highspeed = true;
126
127done:
128 if (value)
129 DBG(config->cdev, "adding '%s'/%p --> %d\n",
130 function->name, function, value);
131 return value;
132}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153int usb_function_deactivate(struct usb_function *function)
154{
155 struct usb_composite_dev *cdev = function->config->cdev;
156 unsigned long flags;
157 int status = 0;
158
159 spin_lock_irqsave(&cdev->lock, flags);
160
161 if (cdev->deactivations == 0)
162 status = usb_gadget_disconnect(cdev->gadget);
163 if (status == 0)
164 cdev->deactivations++;
165
166 spin_unlock_irqrestore(&cdev->lock, flags);
167 return status;
168}
169
170
171
172
173
174
175
176
177
178
179
180int usb_function_activate(struct usb_function *function)
181{
182 struct usb_composite_dev *cdev = function->config->cdev;
183 int status = 0;
184
185 spin_lock(&cdev->lock);
186
187 if (WARN_ON(cdev->deactivations == 0))
188 status = -EINVAL;
189 else {
190 cdev->deactivations--;
191 if (cdev->deactivations == 0)
192 status = usb_gadget_connect(cdev->gadget);
193 }
194
195 spin_unlock(&cdev->lock);
196 return status;
197}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222int usb_interface_id(struct usb_configuration *config,
223 struct usb_function *function)
224{
225 unsigned id = config->next_interface_id;
226
227 if (id < MAX_CONFIG_INTERFACES) {
228 config->interface[id] = function;
229 config->next_interface_id = id + 1;
230 return id;
231 }
232 return -ENODEV;
233}
234
235static int config_buf(struct usb_configuration *config,
236 enum usb_device_speed speed, void *buf, u8 type)
237{
238 struct usb_config_descriptor *c = buf;
239 void *next = buf + USB_DT_CONFIG_SIZE;
240 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
241 struct usb_function *f;
242 int status;
243
244
245 c = buf;
246 c->bLength = USB_DT_CONFIG_SIZE;
247 c->bDescriptorType = type;
248
249 c->bNumInterfaces = config->next_interface_id;
250 c->bConfigurationValue = config->bConfigurationValue;
251 c->iConfiguration = config->iConfiguration;
252 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
253 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
254
255
256 if (config->descriptors) {
257 status = usb_descriptor_fillbuf(next, len,
258 config->descriptors);
259 if (status < 0)
260 return status;
261 len -= status;
262 next += status;
263 }
264
265
266 list_for_each_entry(f, &config->functions, list) {
267 struct usb_descriptor_header **descriptors;
268
269 if (speed == USB_SPEED_HIGH)
270 descriptors = f->hs_descriptors;
271 else
272 descriptors = f->descriptors;
273 if (!descriptors)
274 continue;
275 status = usb_descriptor_fillbuf(next, len,
276 (const struct usb_descriptor_header **) descriptors);
277 if (status < 0)
278 return status;
279 len -= status;
280 next += status;
281 }
282
283 len = next - buf;
284 c->wTotalLength = cpu_to_le16(len);
285 return len;
286}
287
288static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
289{
290 struct usb_gadget *gadget = cdev->gadget;
291 struct usb_configuration *c;
292 u8 type = w_value >> 8;
293 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
294
295 if (gadget_is_dualspeed(gadget)) {
296 int hs = 0;
297
298 if (gadget->speed == USB_SPEED_HIGH)
299 hs = 1;
300 if (type == USB_DT_OTHER_SPEED_CONFIG)
301 hs = !hs;
302 if (hs)
303 speed = USB_SPEED_HIGH;
304
305 }
306
307
308 w_value &= 0xff;
309 list_for_each_entry(c, &cdev->configs, list) {
310
311 if (speed == USB_SPEED_HIGH) {
312 if (!c->highspeed)
313 continue;
314 } else {
315 if (!c->fullspeed)
316 continue;
317 }
318 if (w_value == 0)
319 return config_buf(c, speed, cdev->req->buf, type);
320 w_value--;
321 }
322 return -EINVAL;
323}
324
325static int count_configs(struct usb_composite_dev *cdev, unsigned type)
326{
327 struct usb_gadget *gadget = cdev->gadget;
328 struct usb_configuration *c;
329 unsigned count = 0;
330 int hs = 0;
331
332 if (gadget_is_dualspeed(gadget)) {
333 if (gadget->speed == USB_SPEED_HIGH)
334 hs = 1;
335 if (type == USB_DT_DEVICE_QUALIFIER)
336 hs = !hs;
337 }
338 list_for_each_entry(c, &cdev->configs, list) {
339
340 if (hs) {
341 if (!c->highspeed)
342 continue;
343 } else {
344 if (!c->fullspeed)
345 continue;
346 }
347 count++;
348 }
349 return count;
350}
351
352static void device_qual(struct usb_composite_dev *cdev)
353{
354 struct usb_qualifier_descriptor *qual = cdev->req->buf;
355
356 qual->bLength = sizeof(*qual);
357 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
358
359 qual->bcdUSB = cdev->desc.bcdUSB;
360 qual->bDeviceClass = cdev->desc.bDeviceClass;
361 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
362 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
363
364 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
365 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
366 qual->bRESERVED = 0;
367}
368
369
370
371static void reset_config(struct usb_composite_dev *cdev)
372{
373 struct usb_function *f;
374
375 DBG(cdev, "reset config\n");
376
377 list_for_each_entry(f, &cdev->config->functions, list) {
378 if (f->disable)
379 f->disable(f);
380
381 bitmap_zero(f->endpoints, 32);
382 }
383 cdev->config = NULL;
384}
385
386static int set_config(struct usb_composite_dev *cdev,
387 const struct usb_ctrlrequest *ctrl, unsigned number)
388{
389 struct usb_gadget *gadget = cdev->gadget;
390 struct usb_configuration *c = NULL;
391 int result = -EINVAL;
392 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
393 int tmp;
394
395 if (cdev->config)
396 reset_config(cdev);
397
398 if (number) {
399 list_for_each_entry(c, &cdev->configs, list) {
400 if (c->bConfigurationValue == number) {
401 result = 0;
402 break;
403 }
404 }
405 if (result < 0)
406 goto done;
407 } else
408 result = 0;
409
410 INFO(cdev, "%s speed config #%d: %s\n",
411 ({ char *speed;
412 switch (gadget->speed) {
413 case USB_SPEED_LOW: speed = "low"; break;
414 case USB_SPEED_FULL: speed = "full"; break;
415 case USB_SPEED_HIGH: speed = "high"; break;
416 default: speed = "?"; break;
417 } ; speed; }), number, c ? c->label : "unconfigured");
418
419 if (!c)
420 goto done;
421
422 cdev->config = c;
423
424
425 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
426 struct usb_function *f = c->interface[tmp];
427 struct usb_descriptor_header **descriptors;
428
429 if (!f)
430 break;
431
432
433
434
435
436
437
438 if (gadget->speed == USB_SPEED_HIGH)
439 descriptors = f->hs_descriptors;
440 else
441 descriptors = f->descriptors;
442
443 for (; *descriptors; ++descriptors) {
444 struct usb_endpoint_descriptor *ep;
445 int addr;
446
447 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
448 continue;
449
450 ep = (struct usb_endpoint_descriptor *)*descriptors;
451 addr = ((ep->bEndpointAddress & 0x80) >> 3)
452 | (ep->bEndpointAddress & 0x0f);
453 set_bit(addr, f->endpoints);
454 }
455
456 result = f->set_alt(f, tmp, 0);
457 if (result < 0) {
458 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
459 tmp, f->name, f, result);
460
461 reset_config(cdev);
462 goto done;
463 }
464
465 if (result == USB_GADGET_DELAYED_STATUS) {
466 DBG(cdev,
467 "%s: interface %d (%s) requested delayed status\n",
468 __func__, tmp, f->name);
469 cdev->delayed_status++;
470 DBG(cdev, "delayed_status count %d\n",
471 cdev->delayed_status);
472 }
473 }
474
475
476 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
477done:
478 usb_gadget_vbus_draw(gadget, power);
479 if (result >= 0 && cdev->delayed_status)
480 result = USB_GADGET_DELAYED_STATUS;
481 return result;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499int usb_add_config(struct usb_composite_dev *cdev,
500 struct usb_configuration *config,
501 int (*bind)(struct usb_configuration *))
502{
503 int status = -EINVAL;
504 struct usb_configuration *c;
505
506 DBG(cdev, "adding config #%u '%s'/%p\n",
507 config->bConfigurationValue,
508 config->label, config);
509
510 if (!config->bConfigurationValue || !bind)
511 goto done;
512
513
514 list_for_each_entry(c, &cdev->configs, list) {
515 if (c->bConfigurationValue == config->bConfigurationValue) {
516 status = -EBUSY;
517 goto done;
518 }
519 }
520
521 config->cdev = cdev;
522 list_add_tail(&config->list, &cdev->configs);
523
524 INIT_LIST_HEAD(&config->functions);
525 config->next_interface_id = 0;
526
527 status = bind(config);
528 if (status < 0) {
529 list_del(&config->list);
530 config->cdev = NULL;
531 } else {
532 unsigned i;
533
534 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
535 config->bConfigurationValue, config,
536 config->highspeed ? " high" : "",
537 config->fullspeed
538 ? (gadget_is_dualspeed(cdev->gadget)
539 ? " full"
540 : " full/low")
541 : "");
542
543 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
544 struct usb_function *f = config->interface[i];
545
546 if (!f)
547 continue;
548 DBG(cdev, " interface %d = %s/%p\n",
549 i, f->name, f);
550 }
551 }
552
553
554
555
556 usb_ep_autoconfig_reset(cdev->gadget);
557
558done:
559 if (status)
560 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
561 config->bConfigurationValue, status);
562 return status;
563}
564
565
566
567
568
569
570
571
572
573static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
574{
575 const struct usb_gadget_strings *s;
576 u16 language;
577 __le16 *tmp;
578
579 while (*sp) {
580 s = *sp;
581 language = cpu_to_le16(s->language);
582 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
583 if (*tmp == language)
584 goto repeat;
585 }
586 *tmp++ = language;
587repeat:
588 sp++;
589 }
590}
591
592static int lookup_string(
593 struct usb_gadget_strings **sp,
594 void *buf,
595 u16 language,
596 int id
597)
598{
599 struct usb_gadget_strings *s;
600 int value;
601
602 while (*sp) {
603 s = *sp++;
604 if (s->language != language)
605 continue;
606 value = usb_gadget_get_string(s, id, buf);
607 if (value > 0)
608 return value;
609 }
610 return -EINVAL;
611}
612
613static int get_string(struct usb_composite_dev *cdev,
614 void *buf, u16 language, int id)
615{
616 struct usb_configuration *c;
617 struct usb_function *f;
618 int len;
619 const char *str;
620
621
622
623
624
625
626
627 if (id == 0) {
628 struct usb_string_descriptor *s = buf;
629 struct usb_gadget_strings **sp;
630
631 memset(s, 0, 256);
632 s->bDescriptorType = USB_DT_STRING;
633
634 sp = composite->strings;
635 if (sp)
636 collect_langs(sp, s->wData);
637
638 list_for_each_entry(c, &cdev->configs, list) {
639 sp = c->strings;
640 if (sp)
641 collect_langs(sp, s->wData);
642
643 list_for_each_entry(f, &c->functions, list) {
644 sp = f->strings;
645 if (sp)
646 collect_langs(sp, s->wData);
647 }
648 }
649
650 for (len = 0; len <= 126 && s->wData[len]; len++)
651 continue;
652 if (!len)
653 return -EINVAL;
654
655 s->bLength = 2 * (len + 1);
656 return s->bLength;
657 }
658
659
660
661
662 if (cdev->manufacturer_override == id)
663 str = iManufacturer ?: composite->iManufacturer ?:
664 composite_manufacturer;
665 else if (cdev->product_override == id)
666 str = iProduct ?: composite->iProduct;
667 else if (cdev->serial_override == id)
668 str = iSerialNumber;
669 else
670 str = NULL;
671 if (str) {
672 struct usb_gadget_strings strings = {
673 .language = language,
674 .strings = &(struct usb_string) { 0xff, str }
675 };
676 return usb_gadget_get_string(&strings, 0xff, buf);
677 }
678
679
680
681
682
683 if (composite->strings) {
684 len = lookup_string(composite->strings, buf, language, id);
685 if (len > 0)
686 return len;
687 }
688 list_for_each_entry(c, &cdev->configs, list) {
689 if (c->strings) {
690 len = lookup_string(c->strings, buf, language, id);
691 if (len > 0)
692 return len;
693 }
694 list_for_each_entry(f, &c->functions, list) {
695 if (!f->strings)
696 continue;
697 len = lookup_string(f->strings, buf, language, id);
698 if (len > 0)
699 return len;
700 }
701 }
702 return -EINVAL;
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719int usb_string_id(struct usb_composite_dev *cdev)
720{
721 if (cdev->next_string_id < 254) {
722
723
724
725 cdev->next_string_id++;
726 return cdev->next_string_id;
727 }
728 return -ENODEV;
729}
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
748{
749 int next = cdev->next_string_id;
750
751 for (; str->s; ++str) {
752 if (unlikely(next >= 254))
753 return -ENODEV;
754 str->id = ++next;
755 }
756
757 cdev->next_string_id = next;
758
759 return 0;
760}
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
782{
783 unsigned next = c->next_string_id;
784 if (unlikely(n > 254 || (unsigned)next + n > 254))
785 return -ENODEV;
786 c->next_string_id += n;
787 return next + 1;
788}
789
790
791
792
793static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
794{
795 if (req->status || req->actual != req->length)
796 DBG((struct usb_composite_dev *) ep->driver_data,
797 "setup complete --> %d, %d/%d\n",
798 req->status, req->actual, req->length);
799}
800
801
802
803
804
805
806
807
808static int
809composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
810{
811 struct usb_composite_dev *cdev = get_gadget_data(gadget);
812 struct usb_request *req = cdev->req;
813 int value = -EOPNOTSUPP;
814 u16 w_index = le16_to_cpu(ctrl->wIndex);
815 u8 intf = w_index & 0xFF;
816 u16 w_value = le16_to_cpu(ctrl->wValue);
817 u16 w_length = le16_to_cpu(ctrl->wLength);
818 struct usb_function *f = NULL;
819 u8 endp;
820
821
822
823
824
825 req->zero = 0;
826 req->complete = composite_setup_complete;
827 req->length = 0;
828 gadget->ep0->driver_data = cdev;
829
830 switch (ctrl->bRequest) {
831
832
833 case USB_REQ_GET_DESCRIPTOR:
834 if (ctrl->bRequestType != USB_DIR_IN)
835 goto unknown;
836 switch (w_value >> 8) {
837
838 case USB_DT_DEVICE:
839 cdev->desc.bNumConfigurations =
840 count_configs(cdev, USB_DT_DEVICE);
841 value = min(w_length, (u16) sizeof cdev->desc);
842 memcpy(req->buf, &cdev->desc, value);
843 break;
844 case USB_DT_DEVICE_QUALIFIER:
845 if (!gadget_is_dualspeed(gadget))
846 break;
847 device_qual(cdev);
848 value = min_t(int, w_length,
849 sizeof(struct usb_qualifier_descriptor));
850 break;
851 case USB_DT_OTHER_SPEED_CONFIG:
852 if (!gadget_is_dualspeed(gadget))
853 break;
854
855 case USB_DT_CONFIG:
856 value = config_desc(cdev, w_value);
857 if (value >= 0)
858 value = min(w_length, (u16) value);
859 break;
860 case USB_DT_STRING:
861 value = get_string(cdev, req->buf,
862 w_index, w_value & 0xff);
863 if (value >= 0)
864 value = min(w_length, (u16) value);
865 break;
866 }
867 break;
868
869
870 case USB_REQ_SET_CONFIGURATION:
871 if (ctrl->bRequestType != 0)
872 goto unknown;
873 if (gadget_is_otg(gadget)) {
874 if (gadget->a_hnp_support)
875 DBG(cdev, "HNP available\n");
876 else if (gadget->a_alt_hnp_support)
877 DBG(cdev, "HNP on another port\n");
878 else
879 VDBG(cdev, "HNP inactive\n");
880 }
881 spin_lock(&cdev->lock);
882 value = set_config(cdev, ctrl, w_value);
883 spin_unlock(&cdev->lock);
884 break;
885 case USB_REQ_GET_CONFIGURATION:
886 if (ctrl->bRequestType != USB_DIR_IN)
887 goto unknown;
888 if (cdev->config)
889 *(u8 *)req->buf = cdev->config->bConfigurationValue;
890 else
891 *(u8 *)req->buf = 0;
892 value = min(w_length, (u16) 1);
893 break;
894
895
896
897
898 case USB_REQ_SET_INTERFACE:
899 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
900 goto unknown;
901 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
902 break;
903 f = cdev->config->interface[intf];
904 if (!f)
905 break;
906 if (w_value && !f->set_alt)
907 break;
908 value = f->set_alt(f, w_index, w_value);
909 if (value == USB_GADGET_DELAYED_STATUS) {
910 DBG(cdev,
911 "%s: interface %d (%s) requested delayed status\n",
912 __func__, intf, f->name);
913 cdev->delayed_status++;
914 DBG(cdev, "delayed_status count %d\n",
915 cdev->delayed_status);
916 }
917 break;
918 case USB_REQ_GET_INTERFACE:
919 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
920 goto unknown;
921 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
922 break;
923 f = cdev->config->interface[intf];
924 if (!f)
925 break;
926
927 value = f->get_alt ? f->get_alt(f, w_index) : 0;
928 if (value < 0)
929 break;
930 *((u8 *)req->buf) = value;
931 value = min(w_length, (u16) 1);
932 break;
933 default:
934unknown:
935 VDBG(cdev,
936 "non-core control req%02x.%02x v%04x i%04x l%d\n",
937 ctrl->bRequestType, ctrl->bRequest,
938 w_value, w_index, w_length);
939
940
941
942
943
944
945
946
947
948 switch (ctrl->bRequestType & USB_RECIP_MASK) {
949 case USB_RECIP_INTERFACE:
950 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
951 break;
952 f = cdev->config->interface[intf];
953 break;
954
955 case USB_RECIP_ENDPOINT:
956 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
957 list_for_each_entry(f, &cdev->config->functions, list) {
958 if (test_bit(endp, f->endpoints))
959 break;
960 }
961 if (&f->list == &cdev->config->functions)
962 f = NULL;
963 break;
964 }
965
966 if (f && f->setup)
967 value = f->setup(f, ctrl);
968 else {
969 struct usb_configuration *c;
970
971 c = cdev->config;
972 if (c && c->setup)
973 value = c->setup(c, ctrl);
974 }
975
976 goto done;
977 }
978
979
980 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
981 req->length = value;
982 req->zero = value < w_length;
983 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
984 if (value < 0) {
985 DBG(cdev, "ep_queue --> %d\n", value);
986 req->status = 0;
987 composite_setup_complete(gadget->ep0, req);
988 }
989 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
990 WARN(cdev,
991 "%s: Delayed status not supported for w_length != 0",
992 __func__);
993 }
994
995done:
996
997 return value;
998}
999
1000static void composite_disconnect(struct usb_gadget *gadget)
1001{
1002 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1003 unsigned long flags;
1004
1005
1006
1007
1008 spin_lock_irqsave(&cdev->lock, flags);
1009 if (cdev->config)
1010 reset_config(cdev);
1011 if (composite->disconnect)
1012 composite->disconnect(cdev);
1013 spin_unlock_irqrestore(&cdev->lock, flags);
1014}
1015
1016
1017
1018static ssize_t composite_show_suspended(struct device *dev,
1019 struct device_attribute *attr,
1020 char *buf)
1021{
1022 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1023 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1024
1025 return sprintf(buf, "%d\n", cdev->suspended);
1026}
1027
1028static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1029
1030static void
1031composite_unbind(struct usb_gadget *gadget)
1032{
1033 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1034
1035
1036
1037
1038
1039
1040 WARN_ON(cdev->config);
1041
1042 while (!list_empty(&cdev->configs)) {
1043 struct usb_configuration *c;
1044
1045 c = list_first_entry(&cdev->configs,
1046 struct usb_configuration, list);
1047 while (!list_empty(&c->functions)) {
1048 struct usb_function *f;
1049
1050 f = list_first_entry(&c->functions,
1051 struct usb_function, list);
1052 list_del(&f->list);
1053 if (f->unbind) {
1054 DBG(cdev, "unbind function '%s'/%p\n",
1055 f->name, f);
1056 f->unbind(c, f);
1057
1058 }
1059 }
1060 list_del(&c->list);
1061 if (c->unbind) {
1062 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1063 c->unbind(c);
1064
1065 }
1066 }
1067 if (composite->unbind)
1068 composite->unbind(cdev);
1069
1070 if (cdev->req) {
1071 kfree(cdev->req->buf);
1072 usb_ep_free_request(gadget->ep0, cdev->req);
1073 }
1074 device_remove_file(&gadget->dev, &dev_attr_suspended);
1075 kfree(cdev);
1076 set_gadget_data(gadget, NULL);
1077 composite = NULL;
1078}
1079
1080static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1081{
1082 if (!*desc) {
1083 int ret = usb_string_id(cdev);
1084 if (unlikely(ret < 0))
1085 WARNING(cdev, "failed to override string ID\n");
1086 else
1087 *desc = ret;
1088 }
1089
1090 return *desc;
1091}
1092
1093static int composite_bind(struct usb_gadget *gadget)
1094{
1095 struct usb_composite_dev *cdev;
1096 int status = -ENOMEM;
1097
1098 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1099 if (!cdev)
1100 return status;
1101
1102 spin_lock_init(&cdev->lock);
1103 cdev->gadget = gadget;
1104 set_gadget_data(gadget, cdev);
1105 INIT_LIST_HEAD(&cdev->configs);
1106
1107
1108 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1109 if (!cdev->req)
1110 goto fail;
1111 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1112 if (!cdev->req->buf)
1113 goto fail;
1114 cdev->req->complete = composite_setup_complete;
1115 gadget->ep0->driver_data = cdev;
1116
1117 cdev->bufsiz = USB_BUFSIZ;
1118 cdev->driver = composite;
1119
1120
1121
1122
1123
1124
1125 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1126 usb_gadget_set_selfpowered(gadget);
1127
1128
1129
1130
1131
1132 usb_ep_autoconfig_reset(cdev->gadget);
1133
1134
1135
1136
1137
1138 status = composite_gadget_bind(cdev);
1139 if (status < 0)
1140 goto fail;
1141
1142 cdev->desc = *composite->dev;
1143 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1144
1145
1146 if (idVendor)
1147 cdev->desc.idVendor = cpu_to_le16(idVendor);
1148 if (idProduct)
1149 cdev->desc.idProduct = cpu_to_le16(idProduct);
1150 if (bcdDevice)
1151 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1152
1153
1154 if (iManufacturer || !cdev->desc.iManufacturer) {
1155 if (!iManufacturer && !composite->iManufacturer &&
1156 !*composite_manufacturer)
1157 snprintf(composite_manufacturer,
1158 sizeof composite_manufacturer,
1159 "%s %s with %s",
1160 init_utsname()->sysname,
1161 init_utsname()->release,
1162 gadget->name);
1163
1164 cdev->manufacturer_override =
1165 override_id(cdev, &cdev->desc.iManufacturer);
1166 }
1167
1168 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1169 cdev->product_override =
1170 override_id(cdev, &cdev->desc.iProduct);
1171
1172 if (iSerialNumber)
1173 cdev->serial_override =
1174 override_id(cdev, &cdev->desc.iSerialNumber);
1175
1176
1177 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1178 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1179
1180
1181 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1182 if (status)
1183 goto fail;
1184
1185 INFO(cdev, "%s ready\n", composite->name);
1186 return 0;
1187
1188fail:
1189 composite_unbind(gadget);
1190 return status;
1191}
1192
1193
1194
1195static void
1196composite_suspend(struct usb_gadget *gadget)
1197{
1198 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1199 struct usb_function *f;
1200
1201
1202
1203
1204 DBG(cdev, "suspend\n");
1205 if (cdev->config) {
1206 list_for_each_entry(f, &cdev->config->functions, list) {
1207 if (f->suspend)
1208 f->suspend(f);
1209 }
1210 }
1211 if (composite->suspend)
1212 composite->suspend(cdev);
1213
1214 cdev->suspended = 1;
1215
1216 usb_gadget_vbus_draw(gadget, 2);
1217}
1218
1219static void
1220composite_resume(struct usb_gadget *gadget)
1221{
1222 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1223 struct usb_function *f;
1224 u8 maxpower;
1225
1226
1227
1228
1229 DBG(cdev, "resume\n");
1230 if (composite->resume)
1231 composite->resume(cdev);
1232 if (cdev->config) {
1233 list_for_each_entry(f, &cdev->config->functions, list) {
1234 if (f->resume)
1235 f->resume(f);
1236 }
1237
1238 maxpower = cdev->config->bMaxPower;
1239
1240 usb_gadget_vbus_draw(gadget, maxpower ?
1241 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1242 }
1243
1244 cdev->suspended = 0;
1245}
1246
1247
1248
1249static struct usb_gadget_driver composite_driver = {
1250 .speed = USB_SPEED_HIGH,
1251
1252 .unbind = composite_unbind,
1253
1254 .setup = composite_setup,
1255 .disconnect = composite_disconnect,
1256
1257 .suspend = composite_suspend,
1258 .resume = composite_resume,
1259
1260 .driver = {
1261 .owner = THIS_MODULE,
1262 },
1263};
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284int usb_composite_probe(struct usb_composite_driver *driver,
1285 int (*bind)(struct usb_composite_dev *cdev))
1286{
1287 if (!driver || !driver->dev || !bind || composite)
1288 return -EINVAL;
1289
1290 if (!driver->name)
1291 driver->name = "composite";
1292 if (!driver->iProduct)
1293 driver->iProduct = driver->name;
1294 composite_driver.function = (char *) driver->name;
1295 composite_driver.driver.name = driver->name;
1296 composite = driver;
1297 composite_gadget_bind = bind;
1298
1299 return usb_gadget_probe_driver(&composite_driver, composite_bind);
1300}
1301
1302
1303
1304
1305
1306
1307
1308
1309void usb_composite_unregister(struct usb_composite_driver *driver)
1310{
1311 if (composite != driver)
1312 return;
1313 usb_gadget_unregister_driver(&composite_driver);
1314}
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1327{
1328 int value;
1329 struct usb_request *req = cdev->req;
1330 unsigned long flags;
1331
1332 DBG(cdev, "%s\n", __func__);
1333 spin_lock_irqsave(&cdev->lock, flags);
1334
1335 if (cdev->delayed_status == 0) {
1336 WARN(cdev, "%s: Unexpected call\n", __func__);
1337
1338 } else if (--cdev->delayed_status == 0) {
1339 DBG(cdev, "%s: Completing delayed status\n", __func__);
1340 req->length = 0;
1341 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1342 if (value < 0) {
1343 DBG(cdev, "ep_queue --> %d\n", value);
1344 req->status = 0;
1345 composite_setup_complete(cdev->gadget->ep0, req);
1346 }
1347 }
1348
1349 spin_unlock_irqrestore(&cdev->lock, flags);
1350}
1351
1352