1
2
3
4
5
6
7
8
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
12#include <log.h>
13#include <memalign.h>
14#include <usb.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
17#include <dm/uclass-internal.h>
18
19extern bool usb_started;
20static bool asynch_allowed;
21
22struct usb_uclass_priv {
23 int companion_device_count;
24};
25
26int usb_lock_async(struct usb_device *udev, int lock)
27{
28 struct udevice *bus = udev->controller_dev;
29 struct dm_usb_ops *ops = usb_get_ops(bus);
30
31 if (!ops->lock_async)
32 return -ENOSYS;
33
34 return ops->lock_async(bus, lock);
35}
36
37int usb_disable_asynch(int disable)
38{
39 int old_value = asynch_allowed;
40
41 asynch_allowed = !disable;
42 return old_value;
43}
44
45int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
46 int length, int interval, bool nonblock)
47{
48 struct udevice *bus = udev->controller_dev;
49 struct dm_usb_ops *ops = usb_get_ops(bus);
50
51 if (!ops->interrupt)
52 return -ENOSYS;
53
54 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
55 nonblock);
56}
57
58int submit_control_msg(struct usb_device *udev, unsigned long pipe,
59 void *buffer, int length, struct devrequest *setup)
60{
61 struct udevice *bus = udev->controller_dev;
62 struct dm_usb_ops *ops = usb_get_ops(bus);
63 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
64 int err;
65
66 if (!ops->control)
67 return -ENOSYS;
68
69 err = ops->control(bus, udev, pipe, buffer, length, setup);
70 if (setup->request == USB_REQ_SET_FEATURE &&
71 setup->requesttype == USB_RT_PORT &&
72 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
73 err == -ENXIO) {
74
75 uc_priv->companion_device_count++;
76 }
77
78 return err;
79}
80
81int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
82 int length)
83{
84 struct udevice *bus = udev->controller_dev;
85 struct dm_usb_ops *ops = usb_get_ops(bus);
86
87 if (!ops->bulk)
88 return -ENOSYS;
89
90 return ops->bulk(bus, udev, pipe, buffer, length);
91}
92
93struct int_queue *create_int_queue(struct usb_device *udev,
94 unsigned long pipe, int queuesize, int elementsize,
95 void *buffer, int interval)
96{
97 struct udevice *bus = udev->controller_dev;
98 struct dm_usb_ops *ops = usb_get_ops(bus);
99
100 if (!ops->create_int_queue)
101 return NULL;
102
103 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
104 buffer, interval);
105}
106
107void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
108{
109 struct udevice *bus = udev->controller_dev;
110 struct dm_usb_ops *ops = usb_get_ops(bus);
111
112 if (!ops->poll_int_queue)
113 return NULL;
114
115 return ops->poll_int_queue(bus, udev, queue);
116}
117
118int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
119{
120 struct udevice *bus = udev->controller_dev;
121 struct dm_usb_ops *ops = usb_get_ops(bus);
122
123 if (!ops->destroy_int_queue)
124 return -ENOSYS;
125
126 return ops->destroy_int_queue(bus, udev, queue);
127}
128
129int usb_alloc_device(struct usb_device *udev)
130{
131 struct udevice *bus = udev->controller_dev;
132 struct dm_usb_ops *ops = usb_get_ops(bus);
133
134
135 if (!ops->alloc_device)
136 return 0;
137
138 return ops->alloc_device(bus, udev);
139}
140
141int usb_reset_root_port(struct usb_device *udev)
142{
143 struct udevice *bus = udev->controller_dev;
144 struct dm_usb_ops *ops = usb_get_ops(bus);
145
146 if (!ops->reset_root_port)
147 return -ENOSYS;
148
149 return ops->reset_root_port(bus, udev);
150}
151
152int usb_update_hub_device(struct usb_device *udev)
153{
154 struct udevice *bus = udev->controller_dev;
155 struct dm_usb_ops *ops = usb_get_ops(bus);
156
157 if (!ops->update_hub_device)
158 return -ENOSYS;
159
160 return ops->update_hub_device(bus, udev);
161}
162
163int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
164{
165 struct udevice *bus = udev->controller_dev;
166 struct dm_usb_ops *ops = usb_get_ops(bus);
167
168 if (!ops->get_max_xfer_size)
169 return -ENOSYS;
170
171 return ops->get_max_xfer_size(bus, size);
172}
173
174int usb_stop(void)
175{
176 struct udevice *bus;
177 struct udevice *rh;
178 struct uclass *uc;
179 struct usb_uclass_priv *uc_priv;
180 int err = 0, ret;
181
182
183 ret = uclass_get(UCLASS_USB, &uc);
184 if (ret)
185 return ret;
186
187 uc_priv = uc->priv;
188
189 uclass_foreach_dev(bus, uc) {
190 ret = device_remove(bus, DM_REMOVE_NORMAL);
191 if (ret && !err)
192 err = ret;
193
194
195 device_find_first_child(bus, &rh);
196 if (rh) {
197
198
199
200
201 ret = device_unbind(rh);
202 if (ret && !err)
203 err = ret;
204 }
205 }
206
207#ifdef CONFIG_USB_STORAGE
208 usb_stor_reset();
209#endif
210 uc_priv->companion_device_count = 0;
211 usb_started = 0;
212
213 return err;
214}
215
216static void usb_scan_bus(struct udevice *bus, bool recurse)
217{
218 struct usb_bus_priv *priv;
219 struct udevice *dev;
220 int ret;
221
222 priv = dev_get_uclass_priv(bus);
223
224 assert(recurse);
225
226 printf("scanning bus %s for devices... ", bus->name);
227 debug("\n");
228 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
229 if (ret)
230 printf("failed, error %d\n", ret);
231 else if (priv->next_addr == 0)
232 printf("No USB Device found\n");
233 else
234 printf("%d USB Device(s) found\n", priv->next_addr);
235}
236
237static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
238{
239 uclass_foreach_dev(bus, uc) {
240 struct udevice *dev, *next;
241
242 if (!device_active(bus))
243 continue;
244 device_foreach_child_safe(dev, next, bus) {
245 if (!device_active(dev))
246 device_unbind(dev);
247 }
248 }
249}
250
251int usb_init(void)
252{
253 int controllers_initialized = 0;
254 struct usb_uclass_priv *uc_priv;
255 struct usb_bus_priv *priv;
256 struct udevice *bus;
257 struct uclass *uc;
258 int ret;
259
260 asynch_allowed = 1;
261
262 ret = uclass_get(UCLASS_USB, &uc);
263 if (ret)
264 return ret;
265
266 uc_priv = uc->priv;
267
268 uclass_foreach_dev(bus, uc) {
269
270 printf("Bus %s: ", bus->name);
271
272#ifdef CONFIG_SANDBOX
273
274
275
276
277
278
279 ret = dm_scan_fdt_dev(bus);
280 if (ret) {
281 printf("Sandbox USB device scan failed (%d)\n", ret);
282 continue;
283 }
284#endif
285
286 ret = device_probe(bus);
287 if (ret == -ENODEV) {
288 puts("Port not available.\n");
289 controllers_initialized++;
290 continue;
291 }
292
293 if (ret) {
294 printf("probe failed, error %d\n", ret);
295 continue;
296 }
297 controllers_initialized++;
298 usb_started = true;
299 }
300
301
302
303
304
305 uclass_foreach_dev(bus, uc) {
306 if (!device_active(bus))
307 continue;
308
309 priv = dev_get_uclass_priv(bus);
310 if (!priv->companion)
311 usb_scan_bus(bus, true);
312 }
313
314
315
316
317
318
319 if (uc_priv->companion_device_count) {
320 uclass_foreach_dev(bus, uc) {
321 if (!device_active(bus))
322 continue;
323
324 priv = dev_get_uclass_priv(bus);
325 if (priv->companion)
326 usb_scan_bus(bus, true);
327 }
328 }
329
330 debug("scan end\n");
331
332
333 remove_inactive_children(uc, bus);
334
335 ret = uclass_get(UCLASS_USB_HUB, &uc);
336 if (ret)
337 return ret;
338 remove_inactive_children(uc, bus);
339
340
341 if (controllers_initialized == 0)
342 printf("No working controllers found\n");
343
344 return usb_started ? 0 : -1;
345}
346
347
348
349
350
351
352
353
354#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
355static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
356{
357 struct usb_device *udev;
358 struct udevice *dev;
359
360 if (!device_active(parent))
361 return NULL;
362 udev = dev_get_parent_priv(parent);
363 if (udev->devnum == devnum)
364 return udev;
365
366 for (device_find_first_child(parent, &dev);
367 dev;
368 device_find_next_child(&dev)) {
369 udev = find_child_devnum(dev, devnum);
370 if (udev)
371 return udev;
372 }
373
374 return NULL;
375}
376
377struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
378{
379 struct udevice *dev;
380 int devnum = index + 1;
381
382 device_find_first_child(bus, &dev);
383 if (!dev)
384 return NULL;
385
386 return find_child_devnum(dev, devnum);
387}
388#endif
389
390int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
391{
392 struct usb_platdata *plat;
393 struct udevice *dev;
394 int ret;
395
396
397 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
398 if (ret)
399 return ret;
400 ret = device_remove(dev, DM_REMOVE_NORMAL);
401 if (ret)
402 return ret;
403
404 plat = dev_get_platdata(dev);
405 plat->init_type = USB_INIT_DEVICE;
406 ret = device_probe(dev);
407 if (ret)
408 return ret;
409 *ctlrp = dev_get_priv(dev);
410
411 return 0;
412}
413
414int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
415{
416 struct udevice *dev;
417 int ret;
418
419
420 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
421 if (ret)
422 return ret;
423 ret = device_remove(dev, DM_REMOVE_NORMAL);
424 if (ret)
425 return ret;
426
427 *ctlrp = NULL;
428
429 return 0;
430}
431
432
433static int usb_match_device(const struct usb_device_descriptor *desc,
434 const struct usb_device_id *id)
435{
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
437 id->idVendor != desc->idVendor)
438 return 0;
439
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
441 id->idProduct != desc->idProduct)
442 return 0;
443
444
445
446 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
447 (id->bcdDevice_lo > desc->bcdDevice))
448 return 0;
449
450 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
451 (id->bcdDevice_hi < desc->bcdDevice))
452 return 0;
453
454 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
455 (id->bDeviceClass != desc->bDeviceClass))
456 return 0;
457
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
459 (id->bDeviceSubClass != desc->bDeviceSubClass))
460 return 0;
461
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
463 (id->bDeviceProtocol != desc->bDeviceProtocol))
464 return 0;
465
466 return 1;
467}
468
469
470static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
471 const struct usb_interface_descriptor *int_desc,
472 const struct usb_device_id *id)
473{
474
475
476
477 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
478 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
479 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
480 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
481 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
482 USB_DEVICE_ID_MATCH_INT_NUMBER)))
483 return 0;
484
485 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
486 (id->bInterfaceClass != int_desc->bInterfaceClass))
487 return 0;
488
489 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
490 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
491 return 0;
492
493 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
494 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
495 return 0;
496
497 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
498 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
499 return 0;
500
501 return 1;
502}
503
504
505static int usb_match_one_id(struct usb_device_descriptor *desc,
506 struct usb_interface_descriptor *int_desc,
507 const struct usb_device_id *id)
508{
509 if (!usb_match_device(desc, id))
510 return 0;
511
512 return usb_match_one_id_intf(desc, int_desc, id);
513}
514
515static ofnode usb_get_ofnode(struct udevice *hub, int port)
516{
517 ofnode node;
518 u32 reg;
519
520 if (!dev_has_of_node(hub))
521 return ofnode_null();
522
523
524
525
526
527
528
529
530 if (!port)
531 return dev_ofnode(hub);
532
533 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
534 if (ofnode_read_u32(node, "reg", ®))
535 continue;
536
537 if (reg == port)
538 return node;
539 }
540
541 return ofnode_null();
542}
543
544
545
546
547
548
549static int usb_find_and_bind_driver(struct udevice *parent,
550 struct usb_device_descriptor *desc,
551 struct usb_interface_descriptor *iface,
552 int bus_seq, int devnum, int port,
553 struct udevice **devp)
554{
555 struct usb_driver_entry *start, *entry;
556 int n_ents;
557 int ret;
558 char name[30], *str;
559 ofnode node = usb_get_ofnode(parent, port);
560
561 *devp = NULL;
562 debug("%s: Searching for driver\n", __func__);
563 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
564 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
565 for (entry = start; entry != start + n_ents; entry++) {
566 const struct usb_device_id *id;
567 struct udevice *dev;
568 const struct driver *drv;
569 struct usb_dev_platdata *plat;
570
571 for (id = entry->match; id->match_flags; id++) {
572 if (!usb_match_one_id(desc, iface, id))
573 continue;
574
575 drv = entry->driver;
576
577
578
579
580
581
582
583
584 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
585 node, &dev);
586 if (ret)
587 goto error;
588 debug("%s: Match found: %s\n", __func__, drv->name);
589 dev->driver_data = id->driver_info;
590 plat = dev_get_parent_platdata(dev);
591 plat->id = *id;
592 *devp = dev;
593 return 0;
594 }
595 }
596
597
598 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
599 str = strdup(name);
600 if (!str)
601 return -ENOMEM;
602 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
603
604error:
605 debug("%s: No match found: %d\n", __func__, ret);
606 return ret;
607}
608
609
610
611
612
613
614static int usb_find_child(struct udevice *parent,
615 struct usb_device_descriptor *desc,
616 struct usb_interface_descriptor *iface,
617 struct udevice **devp)
618{
619 struct udevice *dev;
620
621 *devp = NULL;
622 for (device_find_first_child(parent, &dev);
623 dev;
624 device_find_next_child(&dev)) {
625 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
626
627
628 if (device_active(dev))
629 continue;
630 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
631 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
632 if (usb_match_one_id(desc, iface, &plat->id)) {
633 *devp = dev;
634 return 0;
635 }
636 }
637
638 return -ENOENT;
639}
640
641int usb_scan_device(struct udevice *parent, int port,
642 enum usb_device_speed speed, struct udevice **devp)
643{
644 struct udevice *dev;
645 bool created = false;
646 struct usb_dev_platdata *plat;
647 struct usb_bus_priv *priv;
648 struct usb_device *parent_udev;
649 int ret;
650 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
651 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
652
653 *devp = NULL;
654 memset(udev, '\0', sizeof(*udev));
655 udev->controller_dev = usb_get_bus(parent);
656 priv = dev_get_uclass_priv(udev->controller_dev);
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686 udev->dev = parent;
687 udev->speed = speed;
688 udev->devnum = priv->next_addr + 1;
689 udev->portnr = port;
690 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
691 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
692 dev_get_parent_priv(parent) : NULL;
693 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
694 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
695 if (ret)
696 return ret;
697 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
698 debug("** usb_find_child returns %d\n", ret);
699 if (ret) {
700 if (ret != -ENOENT)
701 return ret;
702 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
703 iface,
704 udev->controller_dev->seq,
705 udev->devnum, port, &dev);
706 if (ret)
707 return ret;
708 created = true;
709 }
710 plat = dev_get_parent_platdata(dev);
711 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
712 plat->devnum = udev->devnum;
713 plat->udev = udev;
714 priv->next_addr++;
715 ret = device_probe(dev);
716 if (ret) {
717 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
718 priv->next_addr--;
719 if (created)
720 device_unbind(dev);
721 return ret;
722 }
723 *devp = dev;
724
725 return 0;
726}
727
728
729
730
731int usb_detect_change(void)
732{
733 struct udevice *hub;
734 struct uclass *uc;
735 int change = 0;
736 int ret;
737
738 ret = uclass_get(UCLASS_USB_HUB, &uc);
739 if (ret)
740 return ret;
741
742 uclass_foreach_dev(hub, uc) {
743 struct usb_device *udev;
744 struct udevice *dev;
745
746 if (!device_active(hub))
747 continue;
748 for (device_find_first_child(hub, &dev);
749 dev;
750 device_find_next_child(&dev)) {
751 struct usb_port_status status;
752
753 if (!device_active(dev))
754 continue;
755
756 udev = dev_get_parent_priv(dev);
757 if (usb_get_port_status(udev, udev->portnr, &status)
758 < 0)
759
760 continue;
761
762 if (le16_to_cpu(status.wPortChange) &
763 USB_PORT_STAT_C_CONNECTION)
764 change++;
765 }
766 }
767
768 return change;
769}
770
771static int usb_child_post_bind(struct udevice *dev)
772{
773 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
774 int val;
775
776 if (!dev_of_valid(dev))
777 return 0;
778
779
780 val = dev_read_u32_default(dev, "usb,device-class", -1);
781 if (val != -1) {
782 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
783 plat->id.bDeviceClass = val;
784 }
785 val = dev_read_u32_default(dev, "usb,interface-class", -1);
786 if (val != -1) {
787 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
788 plat->id.bInterfaceClass = val;
789 }
790
791 return 0;
792}
793
794struct udevice *usb_get_bus(struct udevice *dev)
795{
796 struct udevice *bus;
797
798 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
799 bus = bus->parent;
800 if (!bus) {
801
802 assert(bus);
803 debug("USB HUB '%s' does not have a controller\n", dev->name);
804 }
805
806 return bus;
807}
808
809int usb_child_pre_probe(struct udevice *dev)
810{
811 struct usb_device *udev = dev_get_parent_priv(dev);
812 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
813 int ret;
814
815 if (plat->udev) {
816
817
818
819
820
821 *udev = *(plat->udev);
822
823 plat->udev = NULL;
824 udev->dev = dev;
825 } else {
826
827
828
829
830
831 udev->dev = dev;
832 udev->controller_dev = usb_get_bus(dev);
833 udev->devnum = plat->devnum;
834
835
836
837
838
839 ret = usb_select_config(udev);
840 if (ret)
841 return ret;
842 }
843
844 return 0;
845}
846
847UCLASS_DRIVER(usb) = {
848 .id = UCLASS_USB,
849 .name = "usb",
850 .flags = DM_UC_FLAG_SEQ_ALIAS,
851 .post_bind = dm_scan_fdt_dev,
852 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
853 .per_child_auto_alloc_size = sizeof(struct usb_device),
854 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
855 .child_post_bind = usb_child_post_bind,
856 .child_pre_probe = usb_child_pre_probe,
857 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
858};
859
860UCLASS_DRIVER(usb_dev_generic) = {
861 .id = UCLASS_USB_DEV_GENERIC,
862 .name = "usb_dev_generic",
863};
864
865U_BOOT_DRIVER(usb_dev_generic_drv) = {
866 .id = UCLASS_USB_DEV_GENERIC,
867 .name = "usb_dev_generic_drv",
868};
869