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