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+
17 #include <asm/byteorder.h>
18 #include <asm/unaligned.h>
22 #ifdef CONFIG_USB_STORAGE
23 static int usb_stor_curr_dev
= -1; /* current device */
25 #ifdef CONFIG_USB_HOST_ETHER
26 static int usb_ether_curr_dev
= -1; /* current ethernet device */
29 /* some display routines (info command) */
30 static char *usb_get_class_desc(unsigned char dclass
)
33 case USB_CLASS_PER_INTERFACE
:
34 return "See Interface";
38 return "Communication";
40 return "Human Interface";
41 case USB_CLASS_PRINTER
:
43 case USB_CLASS_MASS_STORAGE
:
44 return "Mass Storage";
49 case USB_CLASS_VENDOR_SPEC
:
50 return "Vendor specific";
56 static void usb_display_class_sub(unsigned char dclass
, unsigned char subclass
,
60 case USB_CLASS_PER_INTERFACE
:
61 printf("See Interface");
64 printf("Human Interface, Subclass: ");
66 case USB_SUB_HID_NONE
:
69 case USB_SUB_HID_BOOT
:
72 case USB_PROT_HID_NONE
:
75 case USB_PROT_HID_KEYBOARD
:
78 case USB_PROT_HID_MOUSE
:
91 case USB_CLASS_MASS_STORAGE
:
92 printf("Mass Storage, ");
98 printf("SFF-8020i (ATAPI)");
101 printf("QIC-157 (Tape)");
110 printf("Transp. SCSI");
119 printf("Command/Bulk");
122 printf("Command/Bulk/Int");
133 printf("%s", usb_get_class_desc(dclass
));
138 static void usb_display_string(struct usb_device
*dev
, int index
)
140 ALLOC_CACHE_ALIGN_BUFFER(char, buffer
, 256);
143 if (usb_string(dev
, index
, &buffer
[0], 256) > 0)
144 printf("String: \"%s\"", buffer
);
148 static void usb_display_desc(struct usb_device
*dev
)
150 if (dev
->descriptor
.bDescriptorType
== USB_DT_DEVICE
) {
151 printf("%d: %s, USB Revision %x.%x\n", dev
->devnum
,
152 usb_get_class_desc(dev
->config
.if_desc
[0].desc
.bInterfaceClass
),
153 (dev
->descriptor
.bcdUSB
>>8) & 0xff,
154 dev
->descriptor
.bcdUSB
& 0xff);
156 if (strlen(dev
->mf
) || strlen(dev
->prod
) ||
158 printf(" - %s %s %s\n", dev
->mf
, dev
->prod
,
160 if (dev
->descriptor
.bDeviceClass
) {
161 printf(" - Class: ");
162 usb_display_class_sub(dev
->descriptor
.bDeviceClass
,
163 dev
->descriptor
.bDeviceSubClass
,
164 dev
->descriptor
.bDeviceProtocol
);
167 printf(" - Class: (from Interface) %s\n",
169 dev
->config
.if_desc
[0].desc
.bInterfaceClass
));
171 printf(" - PacketSize: %d Configurations: %d\n",
172 dev
->descriptor
.bMaxPacketSize0
,
173 dev
->descriptor
.bNumConfigurations
);
174 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
175 dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
,
176 (dev
->descriptor
.bcdDevice
>>8) & 0xff,
177 dev
->descriptor
.bcdDevice
& 0xff);
182 static void usb_display_conf_desc(struct usb_config_descriptor
*config
,
183 struct usb_device
*dev
)
185 printf(" Configuration: %d\n", config
->bConfigurationValue
);
186 printf(" - Interfaces: %d %s%s%dmA\n", config
->bNumInterfaces
,
187 (config
->bmAttributes
& 0x40) ? "Self Powered " : "Bus Powered ",
188 (config
->bmAttributes
& 0x20) ? "Remote Wakeup " : "",
189 config
->bMaxPower
*2);
190 if (config
->iConfiguration
) {
192 usb_display_string(dev
, config
->iConfiguration
);
197 static void usb_display_if_desc(struct usb_interface_descriptor
*ifdesc
,
198 struct usb_device
*dev
)
200 printf(" Interface: %d\n", ifdesc
->bInterfaceNumber
);
201 printf(" - Alternate Setting %d, Endpoints: %d\n",
202 ifdesc
->bAlternateSetting
, ifdesc
->bNumEndpoints
);
204 usb_display_class_sub(ifdesc
->bInterfaceClass
,
205 ifdesc
->bInterfaceSubClass
, ifdesc
->bInterfaceProtocol
);
207 if (ifdesc
->iInterface
) {
209 usb_display_string(dev
, ifdesc
->iInterface
);
214 static void usb_display_ep_desc(struct usb_endpoint_descriptor
*epdesc
)
216 printf(" - Endpoint %d %s ", epdesc
->bEndpointAddress
& 0xf,
217 (epdesc
->bEndpointAddress
& 0x80) ? "In" : "Out");
218 switch ((epdesc
->bmAttributes
& 0x03)) {
223 printf("Isochronous");
232 printf(" MaxPacket %d", get_unaligned(&epdesc
->wMaxPacketSize
));
233 if ((epdesc
->bmAttributes
& 0x03) == 0x3)
234 printf(" Interval %dms", epdesc
->bInterval
);
238 /* main routine to diasplay the configs, interfaces and endpoints */
239 static void usb_display_config(struct usb_device
*dev
)
241 struct usb_config
*config
;
242 struct usb_interface
*ifdesc
;
243 struct usb_endpoint_descriptor
*epdesc
;
246 config
= &dev
->config
;
247 usb_display_conf_desc(&config
->desc
, dev
);
248 for (i
= 0; i
< config
->no_of_if
; i
++) {
249 ifdesc
= &config
->if_desc
[i
];
250 usb_display_if_desc(&ifdesc
->desc
, dev
);
251 for (ii
= 0; ii
< ifdesc
->no_of_ep
; ii
++) {
252 epdesc
= &ifdesc
->ep_desc
[ii
];
253 usb_display_ep_desc(epdesc
);
260 * With driver model this isn't right since we can have multiple controllers
261 * and the device numbering starts at 1 on each bus.
262 * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
264 static struct usb_device
*usb_find_device(int devnum
)
267 struct usb_device
*udev
;
272 /* Device addresses start at 1 */
274 ret
= uclass_get(UCLASS_USB_HUB
, &uc
);
278 uclass_foreach_dev(hub
, uc
) {
281 if (!device_active(hub
))
283 udev
= dev_get_parentdata(hub
);
284 if (udev
->devnum
== devnum
)
287 for (device_find_first_child(hub
, &dev
);
289 device_find_next_child(&dev
)) {
290 if (!device_active(hub
))
293 udev
= dev_get_parentdata(dev
);
294 if (udev
->devnum
== devnum
)
299 struct usb_device
*udev
;
302 for (d
= 0; d
< USB_MAX_DEVICE
; d
++) {
303 udev
= usb_get_dev_index(d
);
306 if (udev
->devnum
== devnum
)
314 static inline char *portspeed(int speed
)
319 case USB_SPEED_SUPER
:
320 speed_str
= "5 Gb/s";
323 speed_str
= "480 Mb/s";
326 speed_str
= "1.5 Mb/s";
329 speed_str
= "12 Mb/s";
336 /* shows the device tree recursively */
337 static void usb_show_tree_graph(struct usb_device
*dev
, char *pre
)
340 int has_child
, last_child
;
345 has_child
= device_has_active_children(dev
->dev
);
347 /* check if the device has connected children */
351 for (i
= 0; i
< dev
->maxchild
; i
++) {
352 if (dev
->children
[i
] != NULL
)
356 /* check if we are the last one */
358 last_child
= device_is_last_sibling(dev
->dev
);
360 last_child
= (dev
->parent
!= NULL
);
363 #ifndef CONFIG_DM_USB
364 for (i
= 0; i
< dev
->parent
->maxchild
; i
++) {
365 /* search for children */
366 if (dev
->parent
->children
[i
] == dev
) {
367 /* found our pointer, see if we have a
370 while (i
++ < dev
->parent
->maxchild
) {
371 if (dev
->parent
->children
[i
] != NULL
) {
378 } /* for all children of the parent */
381 /* correct last child */
382 if (last_child
&& index
)
384 } /* if not root hub */
387 printf("%d ", dev
->devnum
);
389 pre
[index
++] = has_child
? '|' : ' ';
391 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
392 dev
->config
.if_desc
[0].desc
.bInterfaceClass
),
393 portspeed(dev
->speed
),
394 dev
->config
.desc
.bMaxPower
* 2);
395 if (strlen(dev
->mf
) || strlen(dev
->prod
) || strlen(dev
->serial
))
396 printf(" %s %s %s %s\n", pre
, dev
->mf
, dev
->prod
, dev
->serial
);
397 printf(" %s\n", pre
);
399 struct udevice
*child
;
401 for (device_find_first_child(dev
->dev
, &child
);
403 device_find_next_child(&child
)) {
404 struct usb_device
*udev
;
406 if (!device_active(child
))
409 udev
= dev_get_parentdata(child
);
411 /* Ignore emulators, we only want real devices */
412 if (device_get_uclass_id(child
) != UCLASS_USB_EMUL
) {
413 usb_show_tree_graph(udev
, pre
);
418 if (dev
->maxchild
> 0) {
419 for (i
= 0; i
< dev
->maxchild
; i
++) {
420 if (dev
->children
[i
] != NULL
) {
421 usb_show_tree_graph(dev
->children
[i
], pre
);
429 /* main routine for the tree command */
430 static void usb_show_tree(struct usb_device
*dev
)
434 memset(preamble
, '\0', sizeof(preamble
));
435 usb_show_tree_graph(dev
, &preamble
[0]);
438 static int usb_test(struct usb_device
*dev
, int port
, char* arg
)
442 if (port
> dev
->maxchild
) {
443 printf("Device is no hub or does not have %d ports.\n", port
);
450 printf("Setting Test_J mode");
451 mode
= USB_TEST_MODE_J
;
455 printf("Setting Test_K mode");
456 mode
= USB_TEST_MODE_K
;
460 printf("Setting Test_SE0_NAK mode");
461 mode
= USB_TEST_MODE_SE0_NAK
;
465 printf("Setting Test_Packet mode");
466 mode
= USB_TEST_MODE_PACKET
;
470 printf("Setting Test_Force_Enable mode");
471 mode
= USB_TEST_MODE_FORCE_ENABLE
;
474 printf("Unrecognized test mode: %s\nAvailable modes: "
475 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg
);
480 printf(" on downstream facing port %d...\n", port
);
482 printf(" on upstream facing port...\n");
484 if (usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), USB_REQ_SET_FEATURE
,
485 port
? USB_RT_PORT
: USB_RECIP_DEVICE
,
486 port
? USB_PORT_FEAT_TEST
: USB_FEAT_TEST
,
488 NULL
, 0, USB_CNTL_TIMEOUT
) == -1) {
489 printf("Error during SET_FEATURE.\n");
492 printf("Test mode successfully set. Use 'usb start' "
493 "to return to normal operation.\n");
499 /******************************************************************************
500 * usb boot command intepreter. Derived from diskboot
502 #ifdef CONFIG_USB_STORAGE
503 static int do_usbboot(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
505 return common_diskboot(cmdtp
, "usb", argc
, argv
);
507 #endif /* CONFIG_USB_STORAGE */
509 static int do_usb_stop_keyboard(int force
)
511 #ifdef CONFIG_USB_KEYBOARD
512 if (usb_kbd_deregister(force
) != 0) {
513 printf("USB not stopped: usbkbd still using USB\n");
520 static void do_usb_start(void)
522 bootstage_mark_name(BOOTSTAGE_ID_USB_START
, "usb_start");
527 /* Driver model will probe the devices as they are found */
528 #ifndef CONFIG_DM_USB
529 #ifdef CONFIG_USB_STORAGE
530 /* try to recognize storage devices immediately */
531 usb_stor_curr_dev
= usb_stor_scan(1);
534 #ifdef CONFIG_USB_HOST_ETHER
535 /* try to recognize ethernet devices immediately */
536 usb_ether_curr_dev
= usb_host_eth_scan(1);
538 #ifdef CONFIG_USB_KEYBOARD
544 static void show_info(struct udevice
*dev
)
546 struct udevice
*child
;
547 struct usb_device
*udev
;
549 udev
= dev_get_parentdata(dev
);
550 usb_display_desc(udev
);
551 usb_display_config(udev
);
552 for (device_find_first_child(dev
, &child
);
554 device_find_next_child(&child
)) {
555 if (device_active(child
))
560 static int usb_device_info(void)
564 for (uclass_first_device(UCLASS_USB
, &bus
);
566 uclass_next_device(&bus
)) {
569 device_find_first_child(bus
, &hub
);
570 if (device_get_uclass_id(hub
) == UCLASS_USB_HUB
&&
571 device_active(hub
)) {
580 /******************************************************************************
581 * usb command intepreter
583 static int do_usb(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
585 struct usb_device
*udev
= NULL
;
587 extern char usb_started
;
588 #ifdef CONFIG_USB_STORAGE
589 block_dev_desc_t
*stor_dev
;
593 return CMD_RET_USAGE
;
595 if (strncmp(argv
[1], "start", 5) == 0) {
597 return 0; /* Already started */
598 printf("starting USB...\n");
603 if (strncmp(argv
[1], "reset", 5) == 0) {
604 printf("resetting USB...\n");
605 if (do_usb_stop_keyboard(1) != 0)
611 if (strncmp(argv
[1], "stop", 4) == 0) {
613 console_assign(stdin
, "serial");
614 if (do_usb_stop_keyboard(0) != 0)
616 printf("stopping USB..\n");
621 printf("USB is stopped. Please issue 'usb start' first.\n");
624 if (strncmp(argv
[1], "tree", 4) == 0) {
625 puts("USB device tree:\n");
629 for (uclass_first_device(UCLASS_USB
, &bus
);
631 uclass_next_device(&bus
)) {
632 struct usb_device
*udev
;
635 device_find_first_child(bus
, &hub
);
636 if (device_get_uclass_id(hub
) == UCLASS_USB_HUB
&&
637 device_active(hub
)) {
638 udev
= dev_get_parentdata(hub
);
643 for (i
= 0; i
< USB_MAX_DEVICE
; i
++) {
644 udev
= usb_get_dev_index(i
);
647 if (udev
->parent
== NULL
)
653 if (strncmp(argv
[1], "inf", 3) == 0) {
659 for (d
= 0; d
< USB_MAX_DEVICE
; d
++) {
660 udev
= usb_get_dev_index(d
);
663 usb_display_desc(udev
);
664 usb_display_config(udev
);
670 * With driver model this isn't right since we can
671 * have multiple controllers and the device numbering
672 * starts at 1 on each bus.
674 i
= simple_strtoul(argv
[2], NULL
, 10);
675 printf("config for device %d\n", i
);
676 udev
= usb_find_device(i
);
678 printf("*** No device available ***\n");
681 usb_display_desc(udev
);
682 usb_display_config(udev
);
687 if (strncmp(argv
[1], "test", 4) == 0) {
689 return CMD_RET_USAGE
;
690 i
= simple_strtoul(argv
[2], NULL
, 10);
691 udev
= usb_find_device(i
);
693 printf("Device %d does not exist.\n", i
);
696 i
= simple_strtoul(argv
[3], NULL
, 10);
697 return usb_test(udev
, i
, argv
[4]);
699 #ifdef CONFIG_USB_STORAGE
700 if (strncmp(argv
[1], "stor", 4) == 0)
701 return usb_stor_info();
703 if (strncmp(argv
[1], "part", 4) == 0) {
706 for (devno
= 0; ; ++devno
) {
707 stor_dev
= usb_stor_get_dev(devno
);
708 if (stor_dev
== NULL
)
710 if (stor_dev
->type
!= DEV_TYPE_UNKNOWN
) {
714 debug("print_part of %x\n", devno
);
715 print_part(stor_dev
);
719 devno
= simple_strtoul(argv
[2], NULL
, 16);
720 stor_dev
= usb_stor_get_dev(devno
);
721 if (stor_dev
!= NULL
&&
722 stor_dev
->type
!= DEV_TYPE_UNKNOWN
) {
724 debug("print_part of %x\n", devno
);
725 print_part(stor_dev
);
729 printf("\nno USB devices available\n");
734 if (strcmp(argv
[1], "read") == 0) {
735 if (usb_stor_curr_dev
< 0) {
736 printf("no current device selected\n");
740 unsigned long addr
= simple_strtoul(argv
[2], NULL
, 16);
741 unsigned long blk
= simple_strtoul(argv
[3], NULL
, 16);
742 unsigned long cnt
= simple_strtoul(argv
[4], NULL
, 16);
744 printf("\nUSB read: device %d block # %ld, count %ld"
745 " ... ", usb_stor_curr_dev
, blk
, cnt
);
746 stor_dev
= usb_stor_get_dev(usb_stor_curr_dev
);
747 n
= stor_dev
->block_read(usb_stor_curr_dev
, blk
, cnt
,
749 printf("%ld blocks read: %s\n", n
,
750 (n
== cnt
) ? "OK" : "ERROR");
756 if (strcmp(argv
[1], "write") == 0) {
757 if (usb_stor_curr_dev
< 0) {
758 printf("no current device selected\n");
762 unsigned long addr
= simple_strtoul(argv
[2], NULL
, 16);
763 unsigned long blk
= simple_strtoul(argv
[3], NULL
, 16);
764 unsigned long cnt
= simple_strtoul(argv
[4], NULL
, 16);
766 printf("\nUSB write: device %d block # %ld, count %ld"
767 " ... ", usb_stor_curr_dev
, blk
, cnt
);
768 stor_dev
= usb_stor_get_dev(usb_stor_curr_dev
);
769 n
= stor_dev
->block_write(usb_stor_curr_dev
, blk
, cnt
,
771 printf("%ld blocks write: %s\n", n
,
772 (n
== cnt
) ? "OK" : "ERROR");
778 if (strncmp(argv
[1], "dev", 3) == 0) {
780 int dev
= (int)simple_strtoul(argv
[2], NULL
, 10);
781 printf("\nUSB device %d: ", dev
);
782 stor_dev
= usb_stor_get_dev(dev
);
783 if (stor_dev
== NULL
) {
784 printf("unknown device\n");
787 printf("\n Device %d: ", dev
);
789 if (stor_dev
->type
== DEV_TYPE_UNKNOWN
)
791 usb_stor_curr_dev
= dev
;
792 printf("... is now current device\n");
795 printf("\nUSB device %d: ", usb_stor_curr_dev
);
796 stor_dev
= usb_stor_get_dev(usb_stor_curr_dev
);
798 if (stor_dev
->type
== DEV_TYPE_UNKNOWN
)
804 #endif /* CONFIG_USB_STORAGE */
805 return CMD_RET_USAGE
;
811 "start - start (scan) USB controller\n"
812 "usb reset - reset (rescan) USB controller\n"
813 "usb stop [f] - stop USB [f]=force stop\n"
814 "usb tree - show USB device tree\n"
815 "usb info [dev] - show available USB devices\n"
816 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
817 " (specify port 0 to indicate the device's upstream port)\n"
818 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
819 #ifdef CONFIG_USB_STORAGE
820 "usb storage - show details of USB storage devices\n"
821 "usb dev [dev] - show or set current USB storage device\n"
822 "usb part [dev] - print partition table of one or all USB storage"
824 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
825 " to memory address `addr'\n"
826 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
827 " from memory address `addr'"
828 #endif /* CONFIG_USB_STORAGE */
832 #ifdef CONFIG_USB_STORAGE
834 usbboot
, 3, 1, do_usbboot
,
835 "boot from USB device",
838 #endif /* CONFIG_USB_STORAGE */