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