3 * Denis Peter, MPL AG Switzerland
5 * Adapted for U-Boot driver model
6 * (C) Copyright 2015 Google, Inc
8 * Most of this source has been derived from the Linux USB
11 * SPDX-License-Identifier: GPL-2.0+
18 #include <asm/byteorder.h>
19 #include <asm/unaligned.h>
23 #ifdef CONFIG_USB_STORAGE
24 static int usb_stor_curr_dev = -1; /* current device */
26 #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
27 static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
30 /* some display routines (info command) */
31 static char *usb_get_class_desc(unsigned char dclass)
34 case USB_CLASS_PER_INTERFACE:
35 return "See Interface";
39 return "Communication";
41 return "Human Interface";
42 case USB_CLASS_PRINTER:
44 case USB_CLASS_MASS_STORAGE:
45 return "Mass Storage";
50 case USB_CLASS_VENDOR_SPEC:
51 return "Vendor specific";
57 static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
61 case USB_CLASS_PER_INTERFACE:
62 printf("See Interface");
65 printf("Human Interface, Subclass: ");
67 case USB_SUB_HID_NONE:
70 case USB_SUB_HID_BOOT:
73 case USB_PROT_HID_NONE:
76 case USB_PROT_HID_KEYBOARD:
79 case USB_PROT_HID_MOUSE:
92 case USB_CLASS_MASS_STORAGE:
93 printf("Mass Storage, ");
99 printf("SFF-8020i (ATAPI)");
102 printf("QIC-157 (Tape)");
111 printf("Transp. SCSI");
120 printf("Command/Bulk");
123 printf("Command/Bulk/Int");
134 printf("%s", usb_get_class_desc(dclass));
139 static void usb_display_string(struct usb_device *dev, int index)
141 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
144 if (usb_string(dev, index, &buffer[0], 256) > 0)
145 printf("String: \"%s\"", buffer);
149 static void usb_display_desc(struct usb_device *dev)
151 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
152 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
153 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
154 (dev->descriptor.bcdUSB>>8) & 0xff,
155 dev->descriptor.bcdUSB & 0xff);
157 if (strlen(dev->mf) || strlen(dev->prod) ||
159 printf(" - %s %s %s\n", dev->mf, dev->prod,
161 if (dev->descriptor.bDeviceClass) {
162 printf(" - Class: ");
163 usb_display_class_sub(dev->descriptor.bDeviceClass,
164 dev->descriptor.bDeviceSubClass,
165 dev->descriptor.bDeviceProtocol);
168 printf(" - Class: (from Interface) %s\n",
170 dev->config.if_desc[0].desc.bInterfaceClass));
172 printf(" - PacketSize: %d Configurations: %d\n",
173 dev->descriptor.bMaxPacketSize0,
174 dev->descriptor.bNumConfigurations);
175 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
176 dev->descriptor.idVendor, dev->descriptor.idProduct,
177 (dev->descriptor.bcdDevice>>8) & 0xff,
178 dev->descriptor.bcdDevice & 0xff);
183 static void usb_display_conf_desc(struct usb_config_descriptor *config,
184 struct usb_device *dev)
186 printf(" Configuration: %d\n", config->bConfigurationValue);
187 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
188 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
189 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
190 config->bMaxPower*2);
191 if (config->iConfiguration) {
193 usb_display_string(dev, config->iConfiguration);
198 static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
199 struct usb_device *dev)
201 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
202 printf(" - Alternate Setting %d, Endpoints: %d\n",
203 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
205 usb_display_class_sub(ifdesc->bInterfaceClass,
206 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
208 if (ifdesc->iInterface) {
210 usb_display_string(dev, ifdesc->iInterface);
215 static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
217 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
218 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
219 switch ((epdesc->bmAttributes & 0x03)) {
224 printf("Isochronous");
233 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
234 if ((epdesc->bmAttributes & 0x03) == 0x3)
235 printf(" Interval %dms", epdesc->bInterval);
239 /* main routine to diasplay the configs, interfaces and endpoints */
240 static void usb_display_config(struct usb_device *dev)
242 struct usb_config *config;
243 struct usb_interface *ifdesc;
244 struct usb_endpoint_descriptor *epdesc;
247 config = &dev->config;
248 usb_display_conf_desc(&config->desc, dev);
249 for (i = 0; i < config->no_of_if; i++) {
250 ifdesc = &config->if_desc[i];
251 usb_display_if_desc(&ifdesc->desc, dev);
252 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
253 epdesc = &ifdesc->ep_desc[ii];
254 usb_display_ep_desc(epdesc);
261 * With driver model this isn't right since we can have multiple controllers
262 * and the device numbering starts at 1 on each bus.
263 * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
265 static struct usb_device *usb_find_device(int devnum)
268 struct usb_device *udev;
273 /* Device addresses start at 1 */
275 ret = uclass_get(UCLASS_USB_HUB, &uc);
279 uclass_foreach_dev(hub, uc) {
282 if (!device_active(hub))
284 udev = dev_get_parentdata(hub);
285 if (udev->devnum == devnum)
288 for (device_find_first_child(hub, &dev);
290 device_find_next_child(&dev)) {
291 if (!device_active(hub))
294 udev = dev_get_parentdata(dev);
295 if (udev->devnum == devnum)
300 struct usb_device *udev;
303 for (d = 0; d < USB_MAX_DEVICE; d++) {
304 udev = usb_get_dev_index(d);
307 if (udev->devnum == devnum)
315 static inline char *portspeed(int speed)
320 case USB_SPEED_SUPER:
321 speed_str = "5 Gb/s";
324 speed_str = "480 Mb/s";
327 speed_str = "1.5 Mb/s";
330 speed_str = "12 Mb/s";
337 /* shows the device tree recursively */
338 static void usb_show_tree_graph(struct usb_device *dev, char *pre)
341 int has_child, last_child;
346 has_child = device_has_active_children(dev->dev);
348 /* check if the device has connected children */
352 for (i = 0; i < dev->maxchild; i++) {
353 if (dev->children[i] != NULL)
357 /* check if we are the last one */
359 /* Not the root of the usb tree? */
360 if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
361 last_child = device_is_last_sibling(dev->dev);
363 if (dev->parent != NULL) { /* not root? */
365 for (i = 0; i < dev->parent->maxchild; i++) {
366 /* search for children */
367 if (dev->parent->children[i] == dev) {
368 /* found our pointer, see if we have a
371 while (i++ < dev->parent->maxchild) {
372 if (dev->parent->children[i] != NULL) {
379 } /* for all children of the parent */
382 /* correct last child */
383 if (last_child && index)
385 } /* if not root hub */
388 printf("%d ", dev->devnum);
390 pre[index++] = has_child ? '|' : ' ';
392 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
393 dev->config.if_desc[0].desc.bInterfaceClass),
394 portspeed(dev->speed),
395 dev->config.desc.bMaxPower * 2);
396 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
397 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
398 printf(" %s\n", pre);
400 struct udevice *child;
402 for (device_find_first_child(dev->dev, &child);
404 device_find_next_child(&child)) {
405 struct usb_device *udev;
407 if (!device_active(child))
410 udev = dev_get_parentdata(child);
412 /* Ignore emulators, we only want real devices */
413 if (device_get_uclass_id(child) != UCLASS_USB_EMUL) {
414 usb_show_tree_graph(udev, pre);
419 if (dev->maxchild > 0) {
420 for (i = 0; i < dev->maxchild; i++) {
421 if (dev->children[i] != NULL) {
422 usb_show_tree_graph(dev->children[i], pre);
430 /* main routine for the tree command */
431 static void usb_show_tree(struct usb_device *dev)
435 memset(preamble, '\0', sizeof(preamble));
436 usb_show_tree_graph(dev, &preamble[0]);
439 static int usb_test(struct usb_device *dev, int port, char* arg)
443 if (port > dev->maxchild) {
444 printf("Device is no hub or does not have %d ports.\n", port);
451 printf("Setting Test_J mode");
452 mode = USB_TEST_MODE_J;
456 printf("Setting Test_K mode");
457 mode = USB_TEST_MODE_K;
461 printf("Setting Test_SE0_NAK mode");
462 mode = USB_TEST_MODE_SE0_NAK;
466 printf("Setting Test_Packet mode");
467 mode = USB_TEST_MODE_PACKET;
471 printf("Setting Test_Force_Enable mode");
472 mode = USB_TEST_MODE_FORCE_ENABLE;
475 printf("Unrecognized test mode: %s\nAvailable modes: "
476 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
481 printf(" on downstream facing port %d...\n", port);
483 printf(" on upstream facing port...\n");
485 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
486 port ? USB_RT_PORT : USB_RECIP_DEVICE,
487 port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
489 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
490 printf("Error during SET_FEATURE.\n");
493 printf("Test mode successfully set. Use 'usb start' "
494 "to return to normal operation.\n");
500 /******************************************************************************
501 * usb boot command intepreter. Derived from diskboot
503 #ifdef CONFIG_USB_STORAGE
504 static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
506 return common_diskboot(cmdtp, "usb", argc, argv);
508 #endif /* CONFIG_USB_STORAGE */
510 static int do_usb_stop_keyboard(int force)
512 #ifdef CONFIG_USB_KEYBOARD
513 if (usb_kbd_deregister(force) != 0) {
514 printf("USB not stopped: usbkbd still using USB\n");
521 static void do_usb_start(void)
523 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
528 /* Driver model will probe the devices as they are found */
529 #ifndef CONFIG_DM_USB
530 #ifdef CONFIG_USB_STORAGE
531 /* try to recognize storage devices immediately */
532 usb_stor_curr_dev = usb_stor_scan(1);
535 #ifdef CONFIG_USB_HOST_ETHER
536 # ifdef CONFIG_DM_ETH
537 # ifndef CONFIG_DM_USB
538 # error "You must use CONFIG_DM_USB if you want to use CONFIG_USB_HOST_ETHER with CONFIG_DM_ETH"
541 /* try to recognize ethernet devices immediately */
542 usb_ether_curr_dev = usb_host_eth_scan(1);
545 #ifdef CONFIG_USB_KEYBOARD
551 static void show_info(struct udevice *dev)
553 struct udevice *child;
554 struct usb_device *udev;
556 udev = dev_get_parentdata(dev);
557 usb_display_desc(udev);
558 usb_display_config(udev);
559 for (device_find_first_child(dev, &child);
561 device_find_next_child(&child)) {
562 if (device_active(child))
567 static int usb_device_info(void)
571 for (uclass_first_device(UCLASS_USB, &bus);
573 uclass_next_device(&bus)) {
576 device_find_first_child(bus, &hub);
577 if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
578 device_active(hub)) {
587 /******************************************************************************
588 * usb command intepreter
590 static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
592 struct usb_device *udev = NULL;
594 extern char usb_started;
595 #ifdef CONFIG_USB_STORAGE
596 block_dev_desc_t *stor_dev;
600 return CMD_RET_USAGE;
602 if (strncmp(argv[1], "start", 5) == 0) {
604 return 0; /* Already started */
605 printf("starting USB...\n");
610 if (strncmp(argv[1], "reset", 5) == 0) {
611 printf("resetting USB...\n");
612 if (do_usb_stop_keyboard(1) != 0)
618 if (strncmp(argv[1], "stop", 4) == 0) {
620 console_assign(stdin, "serial");
621 if (do_usb_stop_keyboard(0) != 0)
623 printf("stopping USB..\n");
628 printf("USB is stopped. Please issue 'usb start' first.\n");
631 if (strncmp(argv[1], "tree", 4) == 0) {
632 puts("USB device tree:\n");
636 for (uclass_first_device(UCLASS_USB, &bus);
638 uclass_next_device(&bus)) {
639 struct usb_device *udev;
642 device_find_first_child(bus, &dev);
643 if (dev && device_active(dev)) {
644 udev = dev_get_parentdata(dev);
649 for (i = 0; i < USB_MAX_DEVICE; i++) {
650 udev = usb_get_dev_index(i);
653 if (udev->parent == NULL)
659 if (strncmp(argv[1], "inf", 3) == 0) {
665 for (d = 0; d < USB_MAX_DEVICE; d++) {
666 udev = usb_get_dev_index(d);
669 usb_display_desc(udev);
670 usb_display_config(udev);
676 * With driver model this isn't right since we can
677 * have multiple controllers and the device numbering
678 * starts at 1 on each bus.
680 i = simple_strtoul(argv[2], NULL, 10);
681 printf("config for device %d\n", i);
682 udev = usb_find_device(i);
684 printf("*** No device available ***\n");
687 usb_display_desc(udev);
688 usb_display_config(udev);
693 if (strncmp(argv[1], "test", 4) == 0) {
695 return CMD_RET_USAGE;
696 i = simple_strtoul(argv[2], NULL, 10);
697 udev = usb_find_device(i);
699 printf("Device %d does not exist.\n", i);
702 i = simple_strtoul(argv[3], NULL, 10);
703 return usb_test(udev, i, argv[4]);
705 #ifdef CONFIG_USB_STORAGE
706 if (strncmp(argv[1], "stor", 4) == 0)
707 return usb_stor_info();
709 if (strncmp(argv[1], "part", 4) == 0) {
712 for (devno = 0; ; ++devno) {
713 stor_dev = usb_stor_get_dev(devno);
714 if (stor_dev == NULL)
716 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
720 debug("print_part of %x\n", devno);
721 print_part(stor_dev);
725 devno = simple_strtoul(argv[2], NULL, 16);
726 stor_dev = usb_stor_get_dev(devno);
727 if (stor_dev != NULL &&
728 stor_dev->type != DEV_TYPE_UNKNOWN) {
730 debug("print_part of %x\n", devno);
731 print_part(stor_dev);
735 printf("\nno USB devices available\n");
740 if (strcmp(argv[1], "read") == 0) {
741 if (usb_stor_curr_dev < 0) {
742 printf("no current device selected\n");
746 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
747 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
748 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
750 printf("\nUSB read: device %d block # %ld, count %ld"
751 " ... ", usb_stor_curr_dev, blk, cnt);
752 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
753 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
755 printf("%ld blocks read: %s\n", n,
756 (n == cnt) ? "OK" : "ERROR");
762 if (strcmp(argv[1], "write") == 0) {
763 if (usb_stor_curr_dev < 0) {
764 printf("no current device selected\n");
768 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
769 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
770 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
772 printf("\nUSB write: device %d block # %ld, count %ld"
773 " ... ", usb_stor_curr_dev, blk, cnt);
774 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
775 n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
777 printf("%ld blocks write: %s\n", n,
778 (n == cnt) ? "OK" : "ERROR");
784 if (strncmp(argv[1], "dev", 3) == 0) {
786 int dev = (int)simple_strtoul(argv[2], NULL, 10);
787 printf("\nUSB device %d: ", dev);
788 stor_dev = usb_stor_get_dev(dev);
789 if (stor_dev == NULL) {
790 printf("unknown device\n");
793 printf("\n Device %d: ", dev);
795 if (stor_dev->type == DEV_TYPE_UNKNOWN)
797 usb_stor_curr_dev = dev;
798 printf("... is now current device\n");
801 printf("\nUSB device %d: ", usb_stor_curr_dev);
802 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
804 if (stor_dev->type == DEV_TYPE_UNKNOWN)
810 #endif /* CONFIG_USB_STORAGE */
811 return CMD_RET_USAGE;
817 "start - start (scan) USB controller\n"
818 "usb reset - reset (rescan) USB controller\n"
819 "usb stop [f] - stop USB [f]=force stop\n"
820 "usb tree - show USB device tree\n"
821 "usb info [dev] - show available USB devices\n"
822 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
823 " (specify port 0 to indicate the device's upstream port)\n"
824 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
825 #ifdef CONFIG_USB_STORAGE
826 "usb storage - show details of USB storage devices\n"
827 "usb dev [dev] - show or set current USB storage device\n"
828 "usb part [dev] - print partition table of one or all USB storage"
830 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
831 " to memory address `addr'\n"
832 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
833 " from memory address `addr'"
834 #endif /* CONFIG_USB_STORAGE */
838 #ifdef CONFIG_USB_STORAGE
840 usbboot, 3, 1, do_usbboot,
841 "boot from USB device",
844 #endif /* CONFIG_USB_STORAGE */