1 // SPDX-License-Identifier: GPL-2.0+
4 * Denis Peter, MPL AG Switzerland
6 * Adapted for U-Boot driver model
7 * (C) Copyright 2015 Google, Inc
9 * Most of this source has been derived from the Linux USB
15 #include <bootstage.h>
19 #include <dm/uclass-internal.h>
21 #include <asm/byteorder.h>
22 #include <asm/unaligned.h>
26 #ifdef CONFIG_USB_STORAGE
27 static int usb_stor_curr_dev = -1; /* current device */
29 #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
30 static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
33 /* some display routines (info command) */
34 static char *usb_get_class_desc(unsigned char dclass)
37 case USB_CLASS_PER_INTERFACE:
38 return "See Interface";
42 return "Communication";
44 return "Human Interface";
45 case USB_CLASS_PRINTER:
47 case USB_CLASS_MASS_STORAGE:
48 return "Mass Storage";
53 case USB_CLASS_VENDOR_SPEC:
54 return "Vendor specific";
60 static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
64 case USB_CLASS_PER_INTERFACE:
65 printf("See Interface");
68 printf("Human Interface, Subclass: ");
70 case USB_SUB_HID_NONE:
73 case USB_SUB_HID_BOOT:
76 case USB_PROT_HID_NONE:
79 case USB_PROT_HID_KEYBOARD:
82 case USB_PROT_HID_MOUSE:
95 case USB_CLASS_MASS_STORAGE:
96 printf("Mass Storage, ");
102 printf("SFF-8020i (ATAPI)");
105 printf("QIC-157 (Tape)");
114 printf("Transp. SCSI");
123 printf("Command/Bulk");
126 printf("Command/Bulk/Int");
137 printf("%s", usb_get_class_desc(dclass));
142 static void usb_display_string(struct usb_device *dev, int index)
144 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
147 if (usb_string(dev, index, &buffer[0], 256) > 0)
148 printf("String: \"%s\"", buffer);
152 static void usb_display_desc(struct usb_device *dev)
154 uint packet_size = dev->descriptor.bMaxPacketSize0;
156 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
157 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
158 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
159 (dev->descriptor.bcdUSB>>8) & 0xff,
160 dev->descriptor.bcdUSB & 0xff);
162 if (strlen(dev->mf) || strlen(dev->prod) ||
164 printf(" - %s %s %s\n", dev->mf, dev->prod,
166 if (dev->descriptor.bDeviceClass) {
167 printf(" - Class: ");
168 usb_display_class_sub(dev->descriptor.bDeviceClass,
169 dev->descriptor.bDeviceSubClass,
170 dev->descriptor.bDeviceProtocol);
173 printf(" - Class: (from Interface) %s\n",
175 dev->config.if_desc[0].desc.bInterfaceClass));
177 if (dev->descriptor.bcdUSB >= cpu_to_le16(0x0300))
178 packet_size = 1 << packet_size;
179 printf(" - PacketSize: %d Configurations: %d\n",
180 packet_size, dev->descriptor.bNumConfigurations);
181 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
182 dev->descriptor.idVendor, dev->descriptor.idProduct,
183 (dev->descriptor.bcdDevice>>8) & 0xff,
184 dev->descriptor.bcdDevice & 0xff);
189 static void usb_display_conf_desc(struct usb_config_descriptor *config,
190 struct usb_device *dev)
192 printf(" Configuration: %d\n", config->bConfigurationValue);
193 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
194 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
195 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
196 config->bMaxPower*2);
197 if (config->iConfiguration) {
199 usb_display_string(dev, config->iConfiguration);
204 static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
205 struct usb_device *dev)
207 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
208 printf(" - Alternate Setting %d, Endpoints: %d\n",
209 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
211 usb_display_class_sub(ifdesc->bInterfaceClass,
212 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
214 if (ifdesc->iInterface) {
216 usb_display_string(dev, ifdesc->iInterface);
221 static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
223 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
224 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
225 switch ((epdesc->bmAttributes & 0x03)) {
230 printf("Isochronous");
239 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
240 if ((epdesc->bmAttributes & 0x03) == 0x3)
241 printf(" Interval %dms", epdesc->bInterval);
245 /* main routine to diasplay the configs, interfaces and endpoints */
246 static void usb_display_config(struct usb_device *dev)
248 struct usb_config *config;
249 struct usb_interface *ifdesc;
250 struct usb_endpoint_descriptor *epdesc;
253 config = &dev->config;
254 usb_display_conf_desc(&config->desc, dev);
255 for (i = 0; i < config->no_of_if; i++) {
256 ifdesc = &config->if_desc[i];
257 usb_display_if_desc(&ifdesc->desc, dev);
258 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
259 epdesc = &ifdesc->ep_desc[ii];
260 usb_display_ep_desc(epdesc);
267 * With driver model this isn't right since we can have multiple controllers
268 * and the device numbering starts at 1 on each bus.
271 static struct usb_device *usb_find_device(int devnum)
274 struct usb_device *udev;
279 /* Device addresses start at 1 */
281 ret = uclass_get(UCLASS_USB_HUB, &uc);
285 uclass_foreach_dev(hub, uc) {
288 if (!device_active(hub))
290 udev = dev_get_parent_priv(hub);
291 if (udev->devnum == devnum)
294 for (device_find_first_child(hub, &dev);
296 device_find_next_child(&dev)) {
297 if (!device_active(hub))
300 udev = dev_get_parent_priv(dev);
301 if (udev->devnum == devnum)
306 struct usb_device *udev;
309 for (d = 0; d < USB_MAX_DEVICE; d++) {
310 udev = usb_get_dev_index(d);
313 if (udev->devnum == devnum)
321 static inline const char *portspeed(int speed)
324 case USB_SPEED_SUPER:
335 /* shows the device tree recursively */
336 static void usb_show_tree_graph(struct usb_device *dev, char *pre)
339 int has_child, last_child;
344 has_child = device_has_active_children(dev->dev);
345 if (device_get_uclass_id(dev->dev) == UCLASS_MASS_STORAGE) {
346 struct udevice *child;
348 for (device_find_first_child(dev->dev, &child);
350 device_find_next_child(&child)) {
351 if (device_get_uclass_id(child) == UCLASS_BLK)
356 /* check if the device has connected children */
360 for (i = 0; i < dev->maxchild; i++) {
361 if (dev->children[i] != NULL)
365 /* check if we are the last one */
367 /* Not the root of the usb tree? */
368 if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
369 last_child = device_is_last_sibling(dev->dev);
371 if (dev->parent != NULL) { /* not root? */
373 for (i = 0; i < dev->parent->maxchild; i++) {
374 /* search for children */
375 if (dev->parent->children[i] == dev) {
376 /* found our pointer, see if we have a
379 while (i++ < dev->parent->maxchild) {
380 if (dev->parent->children[i] != NULL) {
387 } /* for all children of the parent */
390 /* correct last child */
391 if (last_child && index)
393 } /* if not root hub */
396 printf("%d ", dev->devnum);
398 pre[index++] = has_child ? '|' : ' ';
400 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
401 dev->config.if_desc[0].desc.bInterfaceClass),
402 portspeed(dev->speed),
403 dev->config.desc.bMaxPower * 2);
404 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
405 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
406 printf(" %s\n", pre);
408 struct udevice *child;
410 for (device_find_first_child(dev->dev, &child);
412 device_find_next_child(&child)) {
413 struct usb_device *udev;
415 if (!device_active(child))
418 udev = dev_get_parent_priv(child);
421 * Ignore emulators and block child devices, we only want
425 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
426 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
427 (device_get_uclass_id(child) != UCLASS_BLK)) {
428 usb_show_tree_graph(udev, pre);
433 if (dev->maxchild > 0) {
434 for (i = 0; i < dev->maxchild; i++) {
435 if (dev->children[i] != NULL) {
436 usb_show_tree_graph(dev->children[i], pre);
444 /* main routine for the tree command */
445 static void usb_show_subtree(struct usb_device *dev)
449 memset(preamble, '\0', sizeof(preamble));
450 usb_show_tree_graph(dev, &preamble[0]);
454 typedef void (*usb_dev_func_t)(struct usb_device *udev);
456 static void usb_for_each_root_dev(usb_dev_func_t func)
460 for (uclass_find_first_device(UCLASS_USB, &bus);
462 uclass_find_next_device(&bus)) {
463 struct usb_device *udev;
466 if (!device_active(bus))
469 device_find_first_child(bus, &dev);
470 if (dev && device_active(dev)) {
471 udev = dev_get_parent_priv(dev);
478 void usb_show_tree(void)
481 usb_for_each_root_dev(usb_show_subtree);
483 struct usb_device *udev;
486 for (i = 0; i < USB_MAX_DEVICE; i++) {
487 udev = usb_get_dev_index(i);
490 if (udev->parent == NULL)
491 usb_show_subtree(udev);
496 static int usb_test(struct usb_device *dev, int port, char* arg)
500 if (port > dev->maxchild) {
501 printf("Device is no hub or does not have %d ports.\n", port);
508 printf("Setting Test_J mode");
509 mode = USB_TEST_MODE_J;
513 printf("Setting Test_K mode");
514 mode = USB_TEST_MODE_K;
518 printf("Setting Test_SE0_NAK mode");
519 mode = USB_TEST_MODE_SE0_NAK;
523 printf("Setting Test_Packet mode");
524 mode = USB_TEST_MODE_PACKET;
528 printf("Setting Test_Force_Enable mode");
529 mode = USB_TEST_MODE_FORCE_ENABLE;
532 printf("Unrecognized test mode: %s\nAvailable modes: "
533 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
538 printf(" on downstream facing port %d...\n", port);
540 printf(" on upstream facing port...\n");
542 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
543 port ? USB_RT_PORT : USB_RECIP_DEVICE,
544 port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
546 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
547 printf("Error during SET_FEATURE.\n");
550 printf("Test mode successfully set. Use 'usb start' "
551 "to return to normal operation.\n");
557 /******************************************************************************
558 * usb boot command intepreter. Derived from diskboot
560 #ifdef CONFIG_USB_STORAGE
561 static int do_usbboot(struct cmd_tbl *cmdtp, int flag, int argc,
564 return common_diskboot(cmdtp, "usb", argc, argv);
566 #endif /* CONFIG_USB_STORAGE */
568 static int do_usb_stop_keyboard(int force)
570 #if !defined CONFIG_DM_USB && defined CONFIG_USB_KEYBOARD
571 if (usb_kbd_deregister(force) != 0) {
572 printf("USB not stopped: usbkbd still using USB\n");
579 static void do_usb_start(void)
581 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
586 /* Driver model will probe the devices as they are found */
587 # ifdef CONFIG_USB_STORAGE
588 /* try to recognize storage devices immediately */
589 usb_stor_curr_dev = usb_stor_scan(1);
591 #ifndef CONFIG_DM_USB
592 # ifdef CONFIG_USB_KEYBOARD
595 #endif /* !CONFIG_DM_USB */
599 static void usb_show_info(struct usb_device *udev)
601 struct udevice *child;
603 usb_display_desc(udev);
604 usb_display_config(udev);
605 for (device_find_first_child(udev->dev, &child);
607 device_find_next_child(&child)) {
608 if (device_active(child) &&
609 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
610 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
611 (device_get_uclass_id(child) != UCLASS_BLK)) {
612 udev = dev_get_parent_priv(child);
620 /******************************************************************************
621 * usb command intepreter
623 static int do_usb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
625 struct usb_device *udev = NULL;
629 return CMD_RET_USAGE;
631 if (strncmp(argv[1], "start", 5) == 0) {
633 return 0; /* Already started */
634 printf("starting USB...\n");
639 if (strncmp(argv[1], "reset", 5) == 0) {
640 printf("resetting USB...\n");
641 if (do_usb_stop_keyboard(1) != 0)
647 if (strncmp(argv[1], "stop", 4) == 0) {
649 console_assign(stdin, "serial");
650 if (do_usb_stop_keyboard(0) != 0)
652 printf("stopping USB..\n");
657 printf("USB is stopped. Please issue 'usb start' first.\n");
660 if (strncmp(argv[1], "tree", 4) == 0) {
661 puts("USB device tree:\n");
665 if (strncmp(argv[1], "inf", 3) == 0) {
668 usb_for_each_root_dev(usb_show_info);
671 for (d = 0; d < USB_MAX_DEVICE; d++) {
672 udev = usb_get_dev_index(d);
675 usb_display_desc(udev);
676 usb_display_config(udev);
682 * With driver model this isn't right since we can
683 * have multiple controllers and the device numbering
684 * starts at 1 on each bus.
686 i = dectoul(argv[2], NULL);
687 printf("config for device %d\n", i);
688 udev = usb_find_device(i);
690 printf("*** No device available ***\n");
693 usb_display_desc(udev);
694 usb_display_config(udev);
699 if (strncmp(argv[1], "test", 4) == 0) {
701 return CMD_RET_USAGE;
702 i = dectoul(argv[2], NULL);
703 udev = usb_find_device(i);
705 printf("Device %d does not exist.\n", i);
708 i = dectoul(argv[3], NULL);
709 return usb_test(udev, i, argv[4]);
711 #ifdef CONFIG_USB_STORAGE
712 if (strncmp(argv[1], "stor", 4) == 0)
713 return usb_stor_info();
715 return blk_common_cmd(argc, argv, UCLASS_USB, &usb_stor_curr_dev);
717 return CMD_RET_USAGE;
718 #endif /* CONFIG_USB_STORAGE */
724 "start - start (scan) USB controller\n"
725 "usb reset - reset (rescan) USB controller\n"
726 "usb stop [f] - stop USB [f]=force stop\n"
727 "usb tree - show USB device tree\n"
728 "usb info [dev] - show available USB devices\n"
729 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
730 " (specify port 0 to indicate the device's upstream port)\n"
731 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
732 #ifdef CONFIG_USB_STORAGE
733 "usb storage - show details of USB storage devices\n"
734 "usb dev [dev] - show or set current USB storage device\n"
735 "usb part [dev] - print partition table of one or all USB storage"
737 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
738 " to memory address `addr'\n"
739 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
740 " from memory address `addr'"
741 #endif /* CONFIG_USB_STORAGE */
745 #ifdef CONFIG_USB_STORAGE
747 usbboot, 3, 1, do_usbboot,
748 "boot from USB device",
751 #endif /* CONFIG_USB_STORAGE */