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 512
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 __init 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 __init 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 cdev->config = NULL;
378}
379
380static int set_config(struct usb_composite_dev *cdev,
381 const struct usb_ctrlrequest *ctrl, unsigned number)
382{
383 struct usb_gadget *gadget = cdev->gadget;
384 struct usb_configuration *c = NULL;
385 int result = -EINVAL;
386 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
387 int tmp;
388
389 if (cdev->config)
390 reset_config(cdev);
391
392 if (number) {
393 list_for_each_entry(c, &cdev->configs, list) {
394 if (c->bConfigurationValue == number) {
395 result = 0;
396 break;
397 }
398 }
399 if (result < 0)
400 goto done;
401 } else
402 result = 0;
403
404 INFO(cdev, "%s speed config #%d: %s\n",
405 ({ char *speed;
406 switch (gadget->speed) {
407 case USB_SPEED_LOW: speed = "low"; break;
408 case USB_SPEED_FULL: speed = "full"; break;
409 case USB_SPEED_HIGH: speed = "high"; break;
410 default: speed = "?"; break;
411 } ; speed; }), number, c ? c->label : "unconfigured");
412
413 if (!c)
414 goto done;
415
416 cdev->config = c;
417
418
419 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
420 struct usb_function *f = c->interface[tmp];
421
422 if (!f)
423 break;
424
425 result = f->set_alt(f, tmp, 0);
426 if (result < 0) {
427 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
428 tmp, f->name, f, result);
429
430 reset_config(cdev);
431 goto done;
432 }
433 }
434
435
436 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
437done:
438 usb_gadget_vbus_draw(gadget, power);
439 return result;
440}
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456int __init usb_add_config(struct usb_composite_dev *cdev,
457 struct usb_configuration *config)
458{
459 int status = -EINVAL;
460 struct usb_configuration *c;
461
462 DBG(cdev, "adding config #%u '%s'/%p\n",
463 config->bConfigurationValue,
464 config->label, config);
465
466 if (!config->bConfigurationValue || !config->bind)
467 goto done;
468
469
470 list_for_each_entry(c, &cdev->configs, list) {
471 if (c->bConfigurationValue == config->bConfigurationValue) {
472 status = -EBUSY;
473 goto done;
474 }
475 }
476
477 config->cdev = cdev;
478 list_add_tail(&config->list, &cdev->configs);
479
480 INIT_LIST_HEAD(&config->functions);
481 config->next_interface_id = 0;
482
483 status = config->bind(config);
484 if (status < 0) {
485 list_del(&config->list);
486 config->cdev = NULL;
487 } else {
488 unsigned i;
489
490 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
491 config->bConfigurationValue, config,
492 config->highspeed ? " high" : "",
493 config->fullspeed
494 ? (gadget_is_dualspeed(cdev->gadget)
495 ? " full"
496 : " full/low")
497 : "");
498
499 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
500 struct usb_function *f = config->interface[i];
501
502 if (!f)
503 continue;
504 DBG(cdev, " interface %d = %s/%p\n",
505 i, f->name, f);
506 }
507 }
508
509
510
511
512 usb_ep_autoconfig_reset(cdev->gadget);
513
514done:
515 if (status)
516 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
517 config->bConfigurationValue, status);
518 return status;
519}
520
521
522
523
524
525
526
527
528
529static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
530{
531 const struct usb_gadget_strings *s;
532 u16 language;
533 __le16 *tmp;
534
535 while (*sp) {
536 s = *sp;
537 language = cpu_to_le16(s->language);
538 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
539 if (*tmp == language)
540 goto repeat;
541 }
542 *tmp++ = language;
543repeat:
544 sp++;
545 }
546}
547
548static int lookup_string(
549 struct usb_gadget_strings **sp,
550 void *buf,
551 u16 language,
552 int id
553)
554{
555 struct usb_gadget_strings *s;
556 int value;
557
558 while (*sp) {
559 s = *sp++;
560 if (s->language != language)
561 continue;
562 value = usb_gadget_get_string(s, id, buf);
563 if (value > 0)
564 return value;
565 }
566 return -EINVAL;
567}
568
569static int get_string(struct usb_composite_dev *cdev,
570 void *buf, u16 language, int id)
571{
572 struct usb_configuration *c;
573 struct usb_function *f;
574 int len;
575
576
577
578
579
580
581
582 if (id == 0) {
583 struct usb_string_descriptor *s = buf;
584 struct usb_gadget_strings **sp;
585
586 memset(s, 0, 256);
587 s->bDescriptorType = USB_DT_STRING;
588
589 sp = composite->strings;
590 if (sp)
591 collect_langs(sp, s->wData);
592
593 list_for_each_entry(c, &cdev->configs, list) {
594 sp = c->strings;
595 if (sp)
596 collect_langs(sp, s->wData);
597
598 list_for_each_entry(f, &c->functions, list) {
599 sp = f->strings;
600 if (sp)
601 collect_langs(sp, s->wData);
602 }
603 }
604
605 for (len = 0; len <= 126 && s->wData[len]; len++)
606 continue;
607 if (!len)
608 return -EINVAL;
609
610 s->bLength = 2 * (len + 1);
611 return s->bLength;
612 }
613
614
615
616
617
618 if (composite->strings) {
619 len = lookup_string(composite->strings, buf, language, id);
620 if (len > 0)
621 return len;
622 }
623 list_for_each_entry(c, &cdev->configs, list) {
624 if (c->strings) {
625 len = lookup_string(c->strings, buf, language, id);
626 if (len > 0)
627 return len;
628 }
629 list_for_each_entry(f, &c->functions, list) {
630 if (!f->strings)
631 continue;
632 len = lookup_string(f->strings, buf, language, id);
633 if (len > 0)
634 return len;
635 }
636 }
637 return -EINVAL;
638}
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653int __init usb_string_id(struct usb_composite_dev *cdev)
654{
655 if (cdev->next_string_id < 254) {
656
657 cdev->next_string_id++;
658 return cdev->next_string_id;
659 }
660 return -ENODEV;
661}
662
663
664
665static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
666{
667 if (req->status || req->actual != req->length)
668 DBG((struct usb_composite_dev *) ep->driver_data,
669 "setup complete --> %d, %d/%d\n",
670 req->status, req->actual, req->length);
671}
672
673
674
675
676
677
678
679
680static int
681composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
682{
683 struct usb_composite_dev *cdev = get_gadget_data(gadget);
684 struct usb_request *req = cdev->req;
685 int value = -EOPNOTSUPP;
686 u16 w_index = le16_to_cpu(ctrl->wIndex);
687 u8 intf = w_index & 0xFF;
688 u16 w_value = le16_to_cpu(ctrl->wValue);
689 u16 w_length = le16_to_cpu(ctrl->wLength);
690 struct usb_function *f = NULL;
691
692
693
694
695
696 req->zero = 0;
697 req->complete = composite_setup_complete;
698 req->length = USB_BUFSIZ;
699 gadget->ep0->driver_data = cdev;
700
701 switch (ctrl->bRequest) {
702
703
704 case USB_REQ_GET_DESCRIPTOR:
705 if (ctrl->bRequestType != USB_DIR_IN)
706 goto unknown;
707 switch (w_value >> 8) {
708
709 case USB_DT_DEVICE:
710 cdev->desc.bNumConfigurations =
711 count_configs(cdev, USB_DT_DEVICE);
712 value = min(w_length, (u16) sizeof cdev->desc);
713 memcpy(req->buf, &cdev->desc, value);
714 break;
715 case USB_DT_DEVICE_QUALIFIER:
716 if (!gadget_is_dualspeed(gadget))
717 break;
718 device_qual(cdev);
719 value = min_t(int, w_length,
720 sizeof(struct usb_qualifier_descriptor));
721 break;
722 case USB_DT_OTHER_SPEED_CONFIG:
723 if (!gadget_is_dualspeed(gadget))
724 break;
725
726 case USB_DT_CONFIG:
727 value = config_desc(cdev, w_value);
728 if (value >= 0)
729 value = min(w_length, (u16) value);
730 break;
731 case USB_DT_STRING:
732 value = get_string(cdev, req->buf,
733 w_index, w_value & 0xff);
734 if (value >= 0)
735 value = min(w_length, (u16) value);
736 break;
737 }
738 break;
739
740
741 case USB_REQ_SET_CONFIGURATION:
742 if (ctrl->bRequestType != 0)
743 goto unknown;
744 if (gadget_is_otg(gadget)) {
745 if (gadget->a_hnp_support)
746 DBG(cdev, "HNP available\n");
747 else if (gadget->a_alt_hnp_support)
748 DBG(cdev, "HNP on another port\n");
749 else
750 VDBG(cdev, "HNP inactive\n");
751 }
752 spin_lock(&cdev->lock);
753 value = set_config(cdev, ctrl, w_value);
754 spin_unlock(&cdev->lock);
755 break;
756 case USB_REQ_GET_CONFIGURATION:
757 if (ctrl->bRequestType != USB_DIR_IN)
758 goto unknown;
759 if (cdev->config)
760 *(u8 *)req->buf = cdev->config->bConfigurationValue;
761 else
762 *(u8 *)req->buf = 0;
763 value = min(w_length, (u16) 1);
764 break;
765
766
767
768
769 case USB_REQ_SET_INTERFACE:
770 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
771 goto unknown;
772 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
773 break;
774 f = cdev->config->interface[intf];
775 if (!f)
776 break;
777 if (w_value && !f->set_alt)
778 break;
779 value = f->set_alt(f, w_index, w_value);
780 break;
781 case USB_REQ_GET_INTERFACE:
782 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
783 goto unknown;
784 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
785 break;
786 f = cdev->config->interface[intf];
787 if (!f)
788 break;
789
790 value = f->get_alt ? f->get_alt(f, w_index) : 0;
791 if (value < 0)
792 break;
793 *((u8 *)req->buf) = value;
794 value = min(w_length, (u16) 1);
795 break;
796 default:
797unknown:
798 VDBG(cdev,
799 "non-core control req%02x.%02x v%04x i%04x l%d\n",
800 ctrl->bRequestType, ctrl->bRequest,
801 w_value, w_index, w_length);
802
803
804
805
806
807
808
809
810
811 if ((ctrl->bRequestType & USB_RECIP_MASK)
812 == USB_RECIP_INTERFACE) {
813 f = cdev->config->interface[intf];
814 if (f && f->setup)
815 value = f->setup(f, ctrl);
816 else
817 f = NULL;
818 }
819 if (value < 0 && !f) {
820 struct usb_configuration *c;
821
822 c = cdev->config;
823 if (c && c->setup)
824 value = c->setup(c, ctrl);
825 }
826
827 goto done;
828 }
829
830
831 if (value >= 0) {
832 req->length = value;
833 req->zero = value < w_length;
834 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
835 if (value < 0) {
836 DBG(cdev, "ep_queue --> %d\n", value);
837 req->status = 0;
838 composite_setup_complete(gadget->ep0, req);
839 }
840 }
841
842done:
843
844 return value;
845}
846
847static void composite_disconnect(struct usb_gadget *gadget)
848{
849 struct usb_composite_dev *cdev = get_gadget_data(gadget);
850 unsigned long flags;
851
852
853
854
855 spin_lock_irqsave(&cdev->lock, flags);
856 if (cdev->config)
857 reset_config(cdev);
858 spin_unlock_irqrestore(&cdev->lock, flags);
859}
860
861
862
863static void
864composite_unbind(struct usb_gadget *gadget)
865{
866 struct usb_composite_dev *cdev = get_gadget_data(gadget);
867
868
869
870
871
872
873 WARN_ON(cdev->config);
874
875 while (!list_empty(&cdev->configs)) {
876 struct usb_configuration *c;
877
878 c = list_first_entry(&cdev->configs,
879 struct usb_configuration, list);
880 while (!list_empty(&c->functions)) {
881 struct usb_function *f;
882
883 f = list_first_entry(&c->functions,
884 struct usb_function, list);
885 list_del(&f->list);
886 if (f->unbind) {
887 DBG(cdev, "unbind function '%s'/%p\n",
888 f->name, f);
889 f->unbind(c, f);
890
891 }
892 }
893 list_del(&c->list);
894 if (c->unbind) {
895 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
896 c->unbind(c);
897
898 }
899 }
900 if (composite->unbind)
901 composite->unbind(cdev);
902
903 if (cdev->req) {
904 kfree(cdev->req->buf);
905 usb_ep_free_request(gadget->ep0, cdev->req);
906 }
907 kfree(cdev);
908 set_gadget_data(gadget, NULL);
909 composite = NULL;
910}
911
912static void __init
913string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
914{
915 struct usb_string *str = tab->strings;
916
917 for (str = tab->strings; str->s; str++) {
918 if (str->id == id) {
919 str->s = s;
920 return;
921 }
922 }
923}
924
925static void __init
926string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
927{
928 while (*tab) {
929 string_override_one(*tab, id, s);
930 tab++;
931 }
932}
933
934static int __init composite_bind(struct usb_gadget *gadget)
935{
936 struct usb_composite_dev *cdev;
937 int status = -ENOMEM;
938
939 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
940 if (!cdev)
941 return status;
942
943 spin_lock_init(&cdev->lock);
944 cdev->gadget = gadget;
945 set_gadget_data(gadget, cdev);
946 INIT_LIST_HEAD(&cdev->configs);
947
948
949 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
950 if (!cdev->req)
951 goto fail;
952 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
953 if (!cdev->req->buf)
954 goto fail;
955 cdev->req->complete = composite_setup_complete;
956 gadget->ep0->driver_data = cdev;
957
958 cdev->bufsiz = USB_BUFSIZ;
959 cdev->driver = composite;
960
961 usb_gadget_set_selfpowered(gadget);
962
963
964
965
966
967 usb_ep_autoconfig_reset(cdev->gadget);
968
969
970
971
972
973 status = composite->bind(cdev);
974 if (status < 0)
975 goto fail;
976
977 cdev->desc = *composite->dev;
978 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
979
980
981 if (idVendor)
982 cdev->desc.idVendor = cpu_to_le16(idVendor);
983 if (idProduct)
984 cdev->desc.idProduct = cpu_to_le16(idProduct);
985 if (bcdDevice)
986 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
987
988
989
990
991 if (cdev->desc.iManufacturer && iManufacturer)
992 string_override(composite->strings,
993 cdev->desc.iManufacturer, iManufacturer);
994 if (cdev->desc.iProduct && iProduct)
995 string_override(composite->strings,
996 cdev->desc.iProduct, iProduct);
997 if (cdev->desc.iSerialNumber && iSerialNumber)
998 string_override(composite->strings,
999 cdev->desc.iSerialNumber, iSerialNumber);
1000
1001 INFO(cdev, "%s ready\n", composite->name);
1002 return 0;
1003
1004fail:
1005 composite_unbind(gadget);
1006 return status;
1007}
1008
1009
1010
1011static void
1012composite_suspend(struct usb_gadget *gadget)
1013{
1014 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1015 struct usb_function *f;
1016
1017
1018
1019
1020 DBG(cdev, "suspend\n");
1021 if (cdev->config) {
1022 list_for_each_entry(f, &cdev->config->functions, list) {
1023 if (f->suspend)
1024 f->suspend(f);
1025 }
1026 }
1027 if (composite->suspend)
1028 composite->suspend(cdev);
1029}
1030
1031static void
1032composite_resume(struct usb_gadget *gadget)
1033{
1034 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1035 struct usb_function *f;
1036
1037
1038
1039
1040 DBG(cdev, "resume\n");
1041 if (composite->resume)
1042 composite->resume(cdev);
1043 if (cdev->config) {
1044 list_for_each_entry(f, &cdev->config->functions, list) {
1045 if (f->resume)
1046 f->resume(f);
1047 }
1048 }
1049}
1050
1051
1052
1053static struct usb_gadget_driver composite_driver = {
1054 .speed = USB_SPEED_HIGH,
1055
1056 .bind = composite_bind,
1057 .unbind = __exit_p(composite_unbind),
1058
1059 .setup = composite_setup,
1060 .disconnect = composite_disconnect,
1061
1062 .suspend = composite_suspend,
1063 .resume = composite_resume,
1064
1065 .driver = {
1066 .owner = THIS_MODULE,
1067 },
1068};
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085int __init usb_composite_register(struct usb_composite_driver *driver)
1086{
1087 if (!driver || !driver->dev || !driver->bind || composite)
1088 return -EINVAL;
1089
1090 if (!driver->name)
1091 driver->name = "composite";
1092 composite_driver.function = (char *) driver->name;
1093 composite_driver.driver.name = driver->name;
1094 composite = driver;
1095
1096 return usb_gadget_register_driver(&composite_driver);
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1107{
1108 if (composite != driver)
1109 return;
1110 usb_gadget_unregister_driver(&composite_driver);
1111}
1112