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