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