1
2
3
4
5
6
7
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/list.h>
13#include <linux/err.h>
14#include <linux/dma-mapping.h>
15#include <linux/sched/task_stack.h>
16#include <linux/workqueue.h>
17
18#include <linux/usb/ch9.h>
19#include <linux/usb/gadget.h>
20#include <linux/usb.h>
21
22#include "trace.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36struct usb_udc {
37 struct usb_gadget_driver *driver;
38 struct usb_gadget *gadget;
39 struct device dev;
40 struct list_head list;
41 bool vbus;
42};
43
44static struct class *udc_class;
45static LIST_HEAD(udc_list);
46static LIST_HEAD(gadget_driver_pending_list);
47static DEFINE_MUTEX(udc_lock);
48
49static int udc_bind_to_driver(struct usb_udc *udc,
50 struct usb_gadget_driver *driver);
51
52
53
54
55
56
57
58
59
60
61
62void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
63 unsigned maxpacket_limit)
64{
65 ep->maxpacket_limit = maxpacket_limit;
66 ep->maxpacket = maxpacket_limit;
67
68 trace_usb_ep_set_maxpacket_limit(ep, 0);
69}
70EXPORT_SYMBOL_GPL(usb_ep_set_maxpacket_limit);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92int usb_ep_enable(struct usb_ep *ep)
93{
94 int ret = 0;
95
96 if (ep->enabled)
97 goto out;
98
99 ret = ep->ops->enable(ep, ep->desc);
100 if (ret)
101 goto out;
102
103 ep->enabled = true;
104
105out:
106 trace_usb_ep_enable(ep, ret);
107
108 return ret;
109}
110EXPORT_SYMBOL_GPL(usb_ep_enable);
111
112
113
114
115
116
117
118
119
120
121
122
123
124int usb_ep_disable(struct usb_ep *ep)
125{
126 int ret = 0;
127
128 if (!ep->enabled)
129 goto out;
130
131 ret = ep->ops->disable(ep);
132 if (ret)
133 goto out;
134
135 ep->enabled = false;
136
137out:
138 trace_usb_ep_disable(ep, ret);
139
140 return ret;
141}
142EXPORT_SYMBOL_GPL(usb_ep_disable);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
159 gfp_t gfp_flags)
160{
161 struct usb_request *req = NULL;
162
163 req = ep->ops->alloc_request(ep, gfp_flags);
164
165 trace_usb_ep_alloc_request(ep, req, req ? 0 : -ENOMEM);
166
167 return req;
168}
169EXPORT_SYMBOL_GPL(usb_ep_alloc_request);
170
171
172
173
174
175
176
177
178
179
180void usb_ep_free_request(struct usb_ep *ep,
181 struct usb_request *req)
182{
183 trace_usb_ep_free_request(ep, req, 0);
184 ep->ops->free_request(ep, req);
185}
186EXPORT_SYMBOL_GPL(usb_ep_free_request);
187
188
189
190
191
192
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254int usb_ep_queue(struct usb_ep *ep,
255 struct usb_request *req, gfp_t gfp_flags)
256{
257 int ret = 0;
258
259 if (WARN_ON_ONCE(!ep->enabled && ep->address)) {
260 ret = -ESHUTDOWN;
261 goto out;
262 }
263
264 ret = ep->ops->queue(ep, req, gfp_flags);
265
266out:
267 trace_usb_ep_queue(ep, req, ret);
268
269 return ret;
270}
271EXPORT_SYMBOL_GPL(usb_ep_queue);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
289{
290 int ret;
291
292 ret = ep->ops->dequeue(ep, req);
293 trace_usb_ep_dequeue(ep, req, ret);
294
295 return ret;
296}
297EXPORT_SYMBOL_GPL(usb_ep_dequeue);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320int usb_ep_set_halt(struct usb_ep *ep)
321{
322 int ret;
323
324 ret = ep->ops->set_halt(ep, 1);
325 trace_usb_ep_set_halt(ep, ret);
326
327 return ret;
328}
329EXPORT_SYMBOL_GPL(usb_ep_set_halt);
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344int usb_ep_clear_halt(struct usb_ep *ep)
345{
346 int ret;
347
348 ret = ep->ops->set_halt(ep, 0);
349 trace_usb_ep_clear_halt(ep, ret);
350
351 return ret;
352}
353EXPORT_SYMBOL_GPL(usb_ep_clear_halt);
354
355
356
357
358
359
360
361
362
363
364
365int usb_ep_set_wedge(struct usb_ep *ep)
366{
367 int ret;
368
369 if (ep->ops->set_wedge)
370 ret = ep->ops->set_wedge(ep);
371 else
372 ret = ep->ops->set_halt(ep, 1);
373
374 trace_usb_ep_set_wedge(ep, ret);
375
376 return ret;
377}
378EXPORT_SYMBOL_GPL(usb_ep_set_wedge);
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395int usb_ep_fifo_status(struct usb_ep *ep)
396{
397 int ret;
398
399 if (ep->ops->fifo_status)
400 ret = ep->ops->fifo_status(ep);
401 else
402 ret = -EOPNOTSUPP;
403
404 trace_usb_ep_fifo_status(ep, ret);
405
406 return ret;
407}
408EXPORT_SYMBOL_GPL(usb_ep_fifo_status);
409
410
411
412
413
414
415
416
417
418
419void usb_ep_fifo_flush(struct usb_ep *ep)
420{
421 if (ep->ops->fifo_flush)
422 ep->ops->fifo_flush(ep);
423
424 trace_usb_ep_fifo_flush(ep, 0);
425}
426EXPORT_SYMBOL_GPL(usb_ep_fifo_flush);
427
428
429
430
431
432
433
434
435
436
437int usb_gadget_frame_number(struct usb_gadget *gadget)
438{
439 int ret;
440
441 ret = gadget->ops->get_frame(gadget);
442
443 trace_usb_gadget_frame_number(gadget, ret);
444
445 return ret;
446}
447EXPORT_SYMBOL_GPL(usb_gadget_frame_number);
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462int usb_gadget_wakeup(struct usb_gadget *gadget)
463{
464 int ret = 0;
465
466 if (!gadget->ops->wakeup) {
467 ret = -EOPNOTSUPP;
468 goto out;
469 }
470
471 ret = gadget->ops->wakeup(gadget);
472
473out:
474 trace_usb_gadget_wakeup(gadget, ret);
475
476 return ret;
477}
478EXPORT_SYMBOL_GPL(usb_gadget_wakeup);
479
480
481
482
483
484
485
486
487
488
489int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
490{
491 int ret = 0;
492
493 if (!gadget->ops->set_selfpowered) {
494 ret = -EOPNOTSUPP;
495 goto out;
496 }
497
498 ret = gadget->ops->set_selfpowered(gadget, 1);
499
500out:
501 trace_usb_gadget_set_selfpowered(gadget, ret);
502
503 return ret;
504}
505EXPORT_SYMBOL_GPL(usb_gadget_set_selfpowered);
506
507
508
509
510
511
512
513
514
515
516
517int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
518{
519 int ret = 0;
520
521 if (!gadget->ops->set_selfpowered) {
522 ret = -EOPNOTSUPP;
523 goto out;
524 }
525
526 ret = gadget->ops->set_selfpowered(gadget, 0);
527
528out:
529 trace_usb_gadget_clear_selfpowered(gadget, ret);
530
531 return ret;
532}
533EXPORT_SYMBOL_GPL(usb_gadget_clear_selfpowered);
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548int usb_gadget_vbus_connect(struct usb_gadget *gadget)
549{
550 int ret = 0;
551
552 if (!gadget->ops->vbus_session) {
553 ret = -EOPNOTSUPP;
554 goto out;
555 }
556
557 ret = gadget->ops->vbus_session(gadget, 1);
558
559out:
560 trace_usb_gadget_vbus_connect(gadget, ret);
561
562 return ret;
563}
564EXPORT_SYMBOL_GPL(usb_gadget_vbus_connect);
565
566
567
568
569
570
571
572
573
574
575
576
577
578int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
579{
580 int ret = 0;
581
582 if (!gadget->ops->vbus_draw) {
583 ret = -EOPNOTSUPP;
584 goto out;
585 }
586
587 ret = gadget->ops->vbus_draw(gadget, mA);
588 if (!ret)
589 gadget->mA = mA;
590
591out:
592 trace_usb_gadget_vbus_draw(gadget, ret);
593
594 return ret;
595}
596EXPORT_SYMBOL_GPL(usb_gadget_vbus_draw);
597
598
599
600
601
602
603
604
605
606
607
608
609int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
610{
611 int ret = 0;
612
613 if (!gadget->ops->vbus_session) {
614 ret = -EOPNOTSUPP;
615 goto out;
616 }
617
618 ret = gadget->ops->vbus_session(gadget, 0);
619
620out:
621 trace_usb_gadget_vbus_disconnect(gadget, ret);
622
623 return ret;
624}
625EXPORT_SYMBOL_GPL(usb_gadget_vbus_disconnect);
626
627
628
629
630
631
632
633
634
635
636
637
638int usb_gadget_connect(struct usb_gadget *gadget)
639{
640 int ret = 0;
641
642 if (!gadget->ops->pullup) {
643 ret = -EOPNOTSUPP;
644 goto out;
645 }
646
647 if (gadget->deactivated) {
648
649
650
651
652 gadget->connected = true;
653 goto out;
654 }
655
656 ret = gadget->ops->pullup(gadget, 1);
657 if (!ret)
658 gadget->connected = 1;
659
660out:
661 trace_usb_gadget_connect(gadget, ret);
662
663 return ret;
664}
665EXPORT_SYMBOL_GPL(usb_gadget_connect);
666
667
668
669
670
671
672
673
674
675
676
677int usb_gadget_disconnect(struct usb_gadget *gadget)
678{
679 int ret = 0;
680
681 if (!gadget->ops->pullup) {
682 ret = -EOPNOTSUPP;
683 goto out;
684 }
685
686 if (gadget->deactivated) {
687
688
689
690
691 gadget->connected = false;
692 goto out;
693 }
694
695 ret = gadget->ops->pullup(gadget, 0);
696 if (!ret)
697 gadget->connected = 0;
698
699out:
700 trace_usb_gadget_disconnect(gadget, ret);
701
702 return ret;
703}
704EXPORT_SYMBOL_GPL(usb_gadget_disconnect);
705
706
707
708
709
710
711
712
713
714
715
716
717int usb_gadget_deactivate(struct usb_gadget *gadget)
718{
719 int ret = 0;
720
721 if (gadget->deactivated)
722 goto out;
723
724 if (gadget->connected) {
725 ret = usb_gadget_disconnect(gadget);
726 if (ret)
727 goto out;
728
729
730
731
732
733 gadget->connected = true;
734 }
735 gadget->deactivated = true;
736
737out:
738 trace_usb_gadget_deactivate(gadget, ret);
739
740 return ret;
741}
742EXPORT_SYMBOL_GPL(usb_gadget_deactivate);
743
744
745
746
747
748
749
750
751
752
753int usb_gadget_activate(struct usb_gadget *gadget)
754{
755 int ret = 0;
756
757 if (!gadget->deactivated)
758 goto out;
759
760 gadget->deactivated = false;
761
762
763
764
765
766 if (gadget->connected)
767 ret = usb_gadget_connect(gadget);
768
769out:
770 trace_usb_gadget_activate(gadget, ret);
771
772 return ret;
773}
774EXPORT_SYMBOL_GPL(usb_gadget_activate);
775
776
777
778#ifdef CONFIG_HAS_DMA
779
780int usb_gadget_map_request_by_dev(struct device *dev,
781 struct usb_request *req, int is_in)
782{
783 if (req->length == 0)
784 return 0;
785
786 if (req->num_sgs) {
787 int mapped;
788
789 mapped = dma_map_sg(dev, req->sg, req->num_sgs,
790 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
791 if (mapped == 0) {
792 dev_err(dev, "failed to map SGs\n");
793 return -EFAULT;
794 }
795
796 req->num_mapped_sgs = mapped;
797 } else {
798 if (is_vmalloc_addr(req->buf)) {
799 dev_err(dev, "buffer is not dma capable\n");
800 return -EFAULT;
801 } else if (object_is_on_stack(req->buf)) {
802 dev_err(dev, "buffer is on stack\n");
803 return -EFAULT;
804 }
805
806 req->dma = dma_map_single(dev, req->buf, req->length,
807 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
808
809 if (dma_mapping_error(dev, req->dma)) {
810 dev_err(dev, "failed to map buffer\n");
811 return -EFAULT;
812 }
813
814 req->dma_mapped = 1;
815 }
816
817 return 0;
818}
819EXPORT_SYMBOL_GPL(usb_gadget_map_request_by_dev);
820
821int usb_gadget_map_request(struct usb_gadget *gadget,
822 struct usb_request *req, int is_in)
823{
824 return usb_gadget_map_request_by_dev(gadget->dev.parent, req, is_in);
825}
826EXPORT_SYMBOL_GPL(usb_gadget_map_request);
827
828void usb_gadget_unmap_request_by_dev(struct device *dev,
829 struct usb_request *req, int is_in)
830{
831 if (req->length == 0)
832 return;
833
834 if (req->num_mapped_sgs) {
835 dma_unmap_sg(dev, req->sg, req->num_sgs,
836 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
837
838 req->num_mapped_sgs = 0;
839 } else if (req->dma_mapped) {
840 dma_unmap_single(dev, req->dma, req->length,
841 is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
842 req->dma_mapped = 0;
843 }
844}
845EXPORT_SYMBOL_GPL(usb_gadget_unmap_request_by_dev);
846
847void usb_gadget_unmap_request(struct usb_gadget *gadget,
848 struct usb_request *req, int is_in)
849{
850 usb_gadget_unmap_request_by_dev(gadget->dev.parent, req, is_in);
851}
852EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
853
854#endif
855
856
857
858
859
860
861
862
863
864
865void usb_gadget_giveback_request(struct usb_ep *ep,
866 struct usb_request *req)
867{
868 if (likely(req->status == 0))
869 usb_led_activity(USB_LED_EVENT_GADGET);
870
871 trace_usb_gadget_giveback_request(ep, req, 0);
872
873 req->complete(ep, req);
874}
875EXPORT_SYMBOL_GPL(usb_gadget_giveback_request);
876
877
878
879
880
881
882
883
884
885struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g, const char *name)
886{
887 struct usb_ep *ep;
888
889 gadget_for_each_ep(ep, g) {
890 if (!strcmp(ep->name, name))
891 return ep;
892 }
893
894 return NULL;
895}
896EXPORT_SYMBOL_GPL(gadget_find_ep_by_name);
897
898
899
900int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
901 struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
902 struct usb_ss_ep_comp_descriptor *ep_comp)
903{
904 u8 type;
905 u16 max;
906 int num_req_streams = 0;
907
908
909 if (ep->claimed)
910 return 0;
911
912 type = usb_endpoint_type(desc);
913 max = usb_endpoint_maxp(desc);
914
915 if (usb_endpoint_dir_in(desc) && !ep->caps.dir_in)
916 return 0;
917 if (usb_endpoint_dir_out(desc) && !ep->caps.dir_out)
918 return 0;
919
920 if (max > ep->maxpacket_limit)
921 return 0;
922
923
924 if (!gadget_is_dualspeed(gadget) && usb_endpoint_maxp_mult(desc) > 1)
925 return 0;
926
927 switch (type) {
928 case USB_ENDPOINT_XFER_CONTROL:
929
930 return 0;
931 case USB_ENDPOINT_XFER_ISOC:
932 if (!ep->caps.type_iso)
933 return 0;
934
935 if (!gadget_is_dualspeed(gadget) && max > 1023)
936 return 0;
937 break;
938 case USB_ENDPOINT_XFER_BULK:
939 if (!ep->caps.type_bulk)
940 return 0;
941 if (ep_comp && gadget_is_superspeed(gadget)) {
942
943
944
945
946 num_req_streams = ep_comp->bmAttributes & 0x1f;
947 if (num_req_streams > ep->max_streams)
948 return 0;
949 }
950 break;
951 case USB_ENDPOINT_XFER_INT:
952
953
954
955 if (!ep->caps.type_int && !ep->caps.type_bulk)
956 return 0;
957
958 if (!gadget_is_dualspeed(gadget) && max > 64)
959 return 0;
960 break;
961 }
962
963 return 1;
964}
965EXPORT_SYMBOL_GPL(usb_gadget_ep_match_desc);
966
967
968
969static void usb_gadget_state_work(struct work_struct *work)
970{
971 struct usb_gadget *gadget = work_to_gadget(work);
972 struct usb_udc *udc = gadget->udc;
973
974 if (udc)
975 sysfs_notify(&udc->dev.kobj, NULL, "state");
976}
977
978void usb_gadget_set_state(struct usb_gadget *gadget,
979 enum usb_device_state state)
980{
981 gadget->state = state;
982 schedule_work(&gadget->work);
983}
984EXPORT_SYMBOL_GPL(usb_gadget_set_state);
985
986
987
988static void usb_udc_connect_control(struct usb_udc *udc)
989{
990 if (udc->vbus)
991 usb_gadget_connect(udc->gadget);
992 else
993 usb_gadget_disconnect(udc->gadget);
994}
995
996
997
998
999
1000
1001
1002
1003
1004
1005void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status)
1006{
1007 struct usb_udc *udc = gadget->udc;
1008
1009 if (udc) {
1010 udc->vbus = status;
1011 usb_udc_connect_control(udc);
1012 }
1013}
1014EXPORT_SYMBOL_GPL(usb_udc_vbus_handler);
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025void usb_gadget_udc_reset(struct usb_gadget *gadget,
1026 struct usb_gadget_driver *driver)
1027{
1028 driver->reset(gadget);
1029 usb_gadget_set_state(gadget, USB_STATE_DEFAULT);
1030}
1031EXPORT_SYMBOL_GPL(usb_gadget_udc_reset);
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046static inline int usb_gadget_udc_start(struct usb_udc *udc)
1047{
1048 return udc->gadget->ops->udc_start(udc->gadget, udc->driver);
1049}
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063static inline void usb_gadget_udc_stop(struct usb_udc *udc)
1064{
1065 udc->gadget->ops->udc_stop(udc->gadget);
1066}
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078static inline void usb_gadget_udc_set_speed(struct usb_udc *udc,
1079 enum usb_device_speed speed)
1080{
1081 if (udc->gadget->ops->udc_set_speed) {
1082 enum usb_device_speed s;
1083
1084 s = min(speed, udc->gadget->max_speed);
1085 udc->gadget->ops->udc_set_speed(udc->gadget, s);
1086 }
1087}
1088
1089
1090
1091
1092
1093
1094
1095
1096static void usb_udc_release(struct device *dev)
1097{
1098 struct usb_udc *udc;
1099
1100 udc = container_of(dev, struct usb_udc, dev);
1101 dev_dbg(dev, "releasing '%s'\n", dev_name(dev));
1102 kfree(udc);
1103}
1104
1105static const struct attribute_group *usb_udc_attr_groups[];
1106
1107static void usb_udc_nop_release(struct device *dev)
1108{
1109 dev_vdbg(dev, "%s\n", __func__);
1110}
1111
1112
1113static int check_pending_gadget_drivers(struct usb_udc *udc)
1114{
1115 struct usb_gadget_driver *driver;
1116 int ret = 0;
1117
1118 list_for_each_entry(driver, &gadget_driver_pending_list, pending)
1119 if (!driver->udc_name || strcmp(driver->udc_name,
1120 dev_name(&udc->dev)) == 0) {
1121 ret = udc_bind_to_driver(udc, driver);
1122 if (ret != -EPROBE_DEFER)
1123 list_del(&driver->pending);
1124 break;
1125 }
1126
1127 return ret;
1128}
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
1141 void (*release)(struct device *dev))
1142{
1143 struct usb_udc *udc;
1144 int ret = -ENOMEM;
1145
1146 dev_set_name(&gadget->dev, "gadget");
1147 INIT_WORK(&gadget->work, usb_gadget_state_work);
1148 gadget->dev.parent = parent;
1149
1150 if (release)
1151 gadget->dev.release = release;
1152 else
1153 gadget->dev.release = usb_udc_nop_release;
1154
1155 device_initialize(&gadget->dev);
1156
1157 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
1158 if (!udc)
1159 goto err_put_gadget;
1160
1161 device_initialize(&udc->dev);
1162 udc->dev.release = usb_udc_release;
1163 udc->dev.class = udc_class;
1164 udc->dev.groups = usb_udc_attr_groups;
1165 udc->dev.parent = parent;
1166 ret = dev_set_name(&udc->dev, "%s", kobject_name(&parent->kobj));
1167 if (ret)
1168 goto err_put_udc;
1169
1170 ret = device_add(&gadget->dev);
1171 if (ret)
1172 goto err_put_udc;
1173
1174 udc->gadget = gadget;
1175 gadget->udc = udc;
1176
1177 mutex_lock(&udc_lock);
1178 list_add_tail(&udc->list, &udc_list);
1179
1180 ret = device_add(&udc->dev);
1181 if (ret)
1182 goto err_unlist_udc;
1183
1184 usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED);
1185 udc->vbus = true;
1186
1187
1188 ret = check_pending_gadget_drivers(udc);
1189 if (ret)
1190 goto err_del_udc;
1191
1192 mutex_unlock(&udc_lock);
1193
1194 return 0;
1195
1196 err_del_udc:
1197 device_del(&udc->dev);
1198
1199 err_unlist_udc:
1200 list_del(&udc->list);
1201 mutex_unlock(&udc_lock);
1202
1203 device_del(&gadget->dev);
1204
1205 err_put_udc:
1206 put_device(&udc->dev);
1207
1208 err_put_gadget:
1209 put_device(&gadget->dev);
1210 return ret;
1211}
1212EXPORT_SYMBOL_GPL(usb_add_gadget_udc_release);
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226char *usb_get_gadget_udc_name(void)
1227{
1228 struct usb_udc *udc;
1229 char *name = NULL;
1230
1231
1232 mutex_lock(&udc_lock);
1233 list_for_each_entry(udc, &udc_list, list) {
1234 if (!udc->driver) {
1235 name = kstrdup(udc->gadget->name, GFP_KERNEL);
1236 break;
1237 }
1238 }
1239 mutex_unlock(&udc_lock);
1240 return name;
1241}
1242EXPORT_SYMBOL_GPL(usb_get_gadget_udc_name);
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget)
1253{
1254 return usb_add_gadget_udc_release(parent, gadget, NULL);
1255}
1256EXPORT_SYMBOL_GPL(usb_add_gadget_udc);
1257
1258static void usb_gadget_remove_driver(struct usb_udc *udc)
1259{
1260 dev_dbg(&udc->dev, "unregistering UDC driver [%s]\n",
1261 udc->driver->function);
1262
1263 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1264
1265 usb_gadget_disconnect(udc->gadget);
1266 udc->driver->disconnect(udc->gadget);
1267 udc->driver->unbind(udc->gadget);
1268 usb_gadget_udc_stop(udc);
1269
1270 udc->driver = NULL;
1271 udc->dev.driver = NULL;
1272 udc->gadget->dev.driver = NULL;
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282void usb_del_gadget_udc(struct usb_gadget *gadget)
1283{
1284 struct usb_udc *udc = gadget->udc;
1285
1286 if (!udc)
1287 return;
1288
1289 dev_vdbg(gadget->dev.parent, "unregistering gadget\n");
1290
1291 mutex_lock(&udc_lock);
1292 list_del(&udc->list);
1293
1294 if (udc->driver) {
1295 struct usb_gadget_driver *driver = udc->driver;
1296
1297 usb_gadget_remove_driver(udc);
1298 list_add(&driver->pending, &gadget_driver_pending_list);
1299 }
1300 mutex_unlock(&udc_lock);
1301
1302 kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
1303 flush_work(&gadget->work);
1304 device_unregister(&udc->dev);
1305 device_unregister(&gadget->dev);
1306 memset(&gadget->dev, 0x00, sizeof(gadget->dev));
1307}
1308EXPORT_SYMBOL_GPL(usb_del_gadget_udc);
1309
1310
1311
1312static int udc_bind_to_driver(struct usb_udc *udc, struct usb_gadget_driver *driver)
1313{
1314 int ret;
1315
1316 dev_dbg(&udc->dev, "registering UDC driver [%s]\n",
1317 driver->function);
1318
1319 udc->driver = driver;
1320 udc->dev.driver = &driver->driver;
1321 udc->gadget->dev.driver = &driver->driver;
1322
1323 usb_gadget_udc_set_speed(udc, driver->max_speed);
1324
1325 ret = driver->bind(udc->gadget, driver);
1326 if (ret)
1327 goto err1;
1328 ret = usb_gadget_udc_start(udc);
1329 if (ret) {
1330 driver->unbind(udc->gadget);
1331 goto err1;
1332 }
1333 usb_udc_connect_control(udc);
1334
1335 kobject_uevent(&udc->dev.kobj, KOBJ_CHANGE);
1336 return 0;
1337err1:
1338 if (ret != -EISNAM)
1339 dev_err(&udc->dev, "failed to start %s: %d\n",
1340 udc->driver->function, ret);
1341 udc->driver = NULL;
1342 udc->dev.driver = NULL;
1343 udc->gadget->dev.driver = NULL;
1344 return ret;
1345}
1346
1347int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
1348{
1349 struct usb_udc *udc = NULL;
1350 int ret = -ENODEV;
1351
1352 if (!driver || !driver->bind || !driver->setup)
1353 return -EINVAL;
1354
1355 mutex_lock(&udc_lock);
1356 if (driver->udc_name) {
1357 list_for_each_entry(udc, &udc_list, list) {
1358 ret = strcmp(driver->udc_name, dev_name(&udc->dev));
1359 if (!ret)
1360 break;
1361 }
1362 if (ret)
1363 ret = -ENODEV;
1364 else if (udc->driver)
1365 ret = -EBUSY;
1366 else
1367 goto found;
1368 } else {
1369 list_for_each_entry(udc, &udc_list, list) {
1370
1371 if (!udc->driver)
1372 goto found;
1373 }
1374 }
1375
1376 if (!driver->match_existing_only) {
1377 list_add_tail(&driver->pending, &gadget_driver_pending_list);
1378 pr_info("udc-core: couldn't find an available UDC - added [%s] to list of pending drivers\n",
1379 driver->function);
1380 ret = 0;
1381 }
1382
1383 mutex_unlock(&udc_lock);
1384 return ret;
1385found:
1386 ret = udc_bind_to_driver(udc, driver);
1387 mutex_unlock(&udc_lock);
1388 return ret;
1389}
1390EXPORT_SYMBOL_GPL(usb_gadget_probe_driver);
1391
1392int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1393{
1394 struct usb_udc *udc = NULL;
1395 int ret = -ENODEV;
1396
1397 if (!driver || !driver->unbind)
1398 return -EINVAL;
1399
1400 mutex_lock(&udc_lock);
1401 list_for_each_entry(udc, &udc_list, list) {
1402 if (udc->driver == driver) {
1403 usb_gadget_remove_driver(udc);
1404 usb_gadget_set_state(udc->gadget,
1405 USB_STATE_NOTATTACHED);
1406
1407
1408 check_pending_gadget_drivers(udc);
1409
1410
1411
1412
1413 ret = 0;
1414 break;
1415 }
1416 }
1417
1418 if (ret) {
1419 list_del(&driver->pending);
1420 ret = 0;
1421 }
1422 mutex_unlock(&udc_lock);
1423 return ret;
1424}
1425EXPORT_SYMBOL_GPL(usb_gadget_unregister_driver);
1426
1427
1428
1429static ssize_t srp_store(struct device *dev,
1430 struct device_attribute *attr, const char *buf, size_t n)
1431{
1432 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1433
1434 if (sysfs_streq(buf, "1"))
1435 usb_gadget_wakeup(udc->gadget);
1436
1437 return n;
1438}
1439static DEVICE_ATTR_WO(srp);
1440
1441static ssize_t soft_connect_store(struct device *dev,
1442 struct device_attribute *attr, const char *buf, size_t n)
1443{
1444 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1445
1446 if (!udc->driver) {
1447 dev_err(dev, "soft-connect without a gadget driver\n");
1448 return -EOPNOTSUPP;
1449 }
1450
1451 if (sysfs_streq(buf, "connect")) {
1452 usb_gadget_udc_start(udc);
1453 usb_gadget_connect(udc->gadget);
1454 } else if (sysfs_streq(buf, "disconnect")) {
1455 usb_gadget_disconnect(udc->gadget);
1456 udc->driver->disconnect(udc->gadget);
1457 usb_gadget_udc_stop(udc);
1458 } else {
1459 dev_err(dev, "unsupported command '%s'\n", buf);
1460 return -EINVAL;
1461 }
1462
1463 return n;
1464}
1465static DEVICE_ATTR_WO(soft_connect);
1466
1467static ssize_t state_show(struct device *dev, struct device_attribute *attr,
1468 char *buf)
1469{
1470 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1471 struct usb_gadget *gadget = udc->gadget;
1472
1473 return sprintf(buf, "%s\n", usb_state_string(gadget->state));
1474}
1475static DEVICE_ATTR_RO(state);
1476
1477static ssize_t function_show(struct device *dev, struct device_attribute *attr,
1478 char *buf)
1479{
1480 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1481 struct usb_gadget_driver *drv = udc->driver;
1482
1483 if (!drv || !drv->function)
1484 return 0;
1485 return scnprintf(buf, PAGE_SIZE, "%s\n", drv->function);
1486}
1487static DEVICE_ATTR_RO(function);
1488
1489#define USB_UDC_SPEED_ATTR(name, param) \
1490ssize_t name##_show(struct device *dev, \
1491 struct device_attribute *attr, char *buf) \
1492{ \
1493 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1494 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1495 usb_speed_string(udc->gadget->param)); \
1496} \
1497static DEVICE_ATTR_RO(name)
1498
1499static USB_UDC_SPEED_ATTR(current_speed, speed);
1500static USB_UDC_SPEED_ATTR(maximum_speed, max_speed);
1501
1502#define USB_UDC_ATTR(name) \
1503ssize_t name##_show(struct device *dev, \
1504 struct device_attribute *attr, char *buf) \
1505{ \
1506 struct usb_udc *udc = container_of(dev, struct usb_udc, dev); \
1507 struct usb_gadget *gadget = udc->gadget; \
1508 \
1509 return scnprintf(buf, PAGE_SIZE, "%d\n", gadget->name); \
1510} \
1511static DEVICE_ATTR_RO(name)
1512
1513static USB_UDC_ATTR(is_otg);
1514static USB_UDC_ATTR(is_a_peripheral);
1515static USB_UDC_ATTR(b_hnp_enable);
1516static USB_UDC_ATTR(a_hnp_support);
1517static USB_UDC_ATTR(a_alt_hnp_support);
1518static USB_UDC_ATTR(is_selfpowered);
1519
1520static struct attribute *usb_udc_attrs[] = {
1521 &dev_attr_srp.attr,
1522 &dev_attr_soft_connect.attr,
1523 &dev_attr_state.attr,
1524 &dev_attr_function.attr,
1525 &dev_attr_current_speed.attr,
1526 &dev_attr_maximum_speed.attr,
1527
1528 &dev_attr_is_otg.attr,
1529 &dev_attr_is_a_peripheral.attr,
1530 &dev_attr_b_hnp_enable.attr,
1531 &dev_attr_a_hnp_support.attr,
1532 &dev_attr_a_alt_hnp_support.attr,
1533 &dev_attr_is_selfpowered.attr,
1534 NULL,
1535};
1536
1537static const struct attribute_group usb_udc_attr_group = {
1538 .attrs = usb_udc_attrs,
1539};
1540
1541static const struct attribute_group *usb_udc_attr_groups[] = {
1542 &usb_udc_attr_group,
1543 NULL,
1544};
1545
1546static int usb_udc_uevent(struct device *dev, struct kobj_uevent_env *env)
1547{
1548 struct usb_udc *udc = container_of(dev, struct usb_udc, dev);
1549 int ret;
1550
1551 ret = add_uevent_var(env, "USB_UDC_NAME=%s", udc->gadget->name);
1552 if (ret) {
1553 dev_err(dev, "failed to add uevent USB_UDC_NAME\n");
1554 return ret;
1555 }
1556
1557 if (udc->driver) {
1558 ret = add_uevent_var(env, "USB_UDC_DRIVER=%s",
1559 udc->driver->function);
1560 if (ret) {
1561 dev_err(dev, "failed to add uevent USB_UDC_DRIVER\n");
1562 return ret;
1563 }
1564 }
1565
1566 return 0;
1567}
1568
1569static int __init usb_udc_init(void)
1570{
1571 udc_class = class_create(THIS_MODULE, "udc");
1572 if (IS_ERR(udc_class)) {
1573 pr_err("failed to create udc class --> %ld\n",
1574 PTR_ERR(udc_class));
1575 return PTR_ERR(udc_class);
1576 }
1577
1578 udc_class->dev_uevent = usb_udc_uevent;
1579 return 0;
1580}
1581subsys_initcall(usb_udc_init);
1582
1583static void __exit usb_udc_exit(void)
1584{
1585 class_destroy(udc_class);
1586}
1587module_exit(usb_udc_exit);
1588
1589MODULE_DESCRIPTION("UDC Framework");
1590MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1591MODULE_LICENSE("GPL v2");
1592