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