Merge tag 'tpm-master-28012025' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[u-boot.git] / cmd / usb.c
blob13a2996c1f007c322884d2f93d36d1512f5fc8f6
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2001
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
10 * project.
13 #include <blk.h>
14 #include <bootstage.h>
15 #include <command.h>
16 #include <console.h>
17 #include <dm.h>
18 #include <dm/uclass-internal.h>
19 #include <memalign.h>
20 #include <asm/byteorder.h>
21 #include <asm/unaligned.h>
22 #include <part.h>
23 #include <usb.h>
25 #ifdef CONFIG_USB_STORAGE
26 static int usb_stor_curr_dev = -1; /* current device */
27 #endif
29 /* some display routines (info command) */
30 static char *usb_get_class_desc(unsigned char dclass)
32 switch (dclass) {
33 case USB_CLASS_PER_INTERFACE:
34 return "See Interface";
35 case USB_CLASS_AUDIO:
36 return "Audio";
37 case USB_CLASS_COMM:
38 return "Communication";
39 case USB_CLASS_HID:
40 return "Human Interface";
41 case USB_CLASS_PRINTER:
42 return "Printer";
43 case USB_CLASS_MASS_STORAGE:
44 return "Mass Storage";
45 case USB_CLASS_HUB:
46 return "Hub";
47 case USB_CLASS_DATA:
48 return "CDC Data";
49 case USB_CLASS_VENDOR_SPEC:
50 return "Vendor specific";
51 default:
52 return "";
56 static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
57 unsigned char proto)
59 switch (dclass) {
60 case USB_CLASS_PER_INTERFACE:
61 printf("See Interface");
62 break;
63 case USB_CLASS_HID:
64 printf("Human Interface, Subclass: ");
65 switch (subclass) {
66 case USB_SUB_HID_NONE:
67 printf("None");
68 break;
69 case USB_SUB_HID_BOOT:
70 printf("Boot ");
71 switch (proto) {
72 case USB_PROT_HID_NONE:
73 printf("None");
74 break;
75 case USB_PROT_HID_KEYBOARD:
76 printf("Keyboard");
77 break;
78 case USB_PROT_HID_MOUSE:
79 printf("Mouse");
80 break;
81 default:
82 printf("reserved");
83 break;
85 break;
86 default:
87 printf("reserved");
88 break;
90 break;
91 case USB_CLASS_MASS_STORAGE:
92 printf("Mass Storage, ");
93 switch (subclass) {
94 case US_SC_RBC:
95 printf("RBC ");
96 break;
97 case US_SC_8020:
98 printf("SFF-8020i (ATAPI)");
99 break;
100 case US_SC_QIC:
101 printf("QIC-157 (Tape)");
102 break;
103 case US_SC_UFI:
104 printf("UFI");
105 break;
106 case US_SC_8070:
107 printf("SFF-8070");
108 break;
109 case US_SC_SCSI:
110 printf("Transp. SCSI");
111 break;
112 default:
113 printf("reserved");
114 break;
116 printf(", ");
117 switch (proto) {
118 case US_PR_CB:
119 printf("Command/Bulk");
120 break;
121 case US_PR_CBI:
122 printf("Command/Bulk/Int");
123 break;
124 case US_PR_BULK:
125 printf("Bulk only");
126 break;
127 default:
128 printf("reserved");
129 break;
131 break;
132 default:
133 printf("%s", usb_get_class_desc(dclass));
134 break;
138 static void usb_display_string(struct usb_device *dev, int index)
140 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
142 if (index != 0) {
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 uint packet_size = dev->descriptor.bMaxPacketSize0;
152 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
153 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
154 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
155 (dev->descriptor.bcdUSB>>8) & 0xff,
156 dev->descriptor.bcdUSB & 0xff);
158 if (strlen(dev->mf) || strlen(dev->prod) ||
159 strlen(dev->serial))
160 printf(" - %s %s %s\n", dev->mf, dev->prod,
161 dev->serial);
162 if (dev->descriptor.bDeviceClass) {
163 printf(" - Class: ");
164 usb_display_class_sub(dev->descriptor.bDeviceClass,
165 dev->descriptor.bDeviceSubClass,
166 dev->descriptor.bDeviceProtocol);
167 printf("\n");
168 } else {
169 printf(" - Class: (from Interface) %s\n",
170 usb_get_class_desc(
171 dev->config.if_desc[0].desc.bInterfaceClass));
173 if (dev->descriptor.bcdUSB >= cpu_to_le16(0x0300))
174 packet_size = 1 << packet_size;
175 printf(" - PacketSize: %d Configurations: %d\n",
176 packet_size, dev->descriptor.bNumConfigurations);
177 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
178 dev->descriptor.idVendor, dev->descriptor.idProduct,
179 (dev->descriptor.bcdDevice>>8) & 0xff,
180 dev->descriptor.bcdDevice & 0xff);
185 static void usb_display_conf_desc(struct usb_config_descriptor *config,
186 struct usb_device *dev)
188 printf(" Configuration: %d\n", config->bConfigurationValue);
189 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
190 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
191 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
192 config->bMaxPower*2);
193 if (config->iConfiguration) {
194 printf(" - ");
195 usb_display_string(dev, config->iConfiguration);
196 printf("\n");
200 static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
201 struct usb_device *dev)
203 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
204 printf(" - Alternate Setting %d, Endpoints: %d\n",
205 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
206 printf(" - Class ");
207 usb_display_class_sub(ifdesc->bInterfaceClass,
208 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
209 printf("\n");
210 if (ifdesc->iInterface) {
211 printf(" - ");
212 usb_display_string(dev, ifdesc->iInterface);
213 printf("\n");
217 static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
219 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
220 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
221 switch ((epdesc->bmAttributes & 0x03)) {
222 case 0:
223 printf("Control");
224 break;
225 case 1:
226 printf("Isochronous");
227 break;
228 case 2:
229 printf("Bulk");
230 break;
231 case 3:
232 printf("Interrupt");
233 break;
235 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
236 if ((epdesc->bmAttributes & 0x03) == 0x3)
237 printf(" Interval %dms", epdesc->bInterval);
238 printf("\n");
241 /* main routine to diasplay the configs, interfaces and endpoints */
242 static void usb_display_config(struct usb_device *dev)
244 struct usb_config *config;
245 struct usb_interface *ifdesc;
246 struct usb_endpoint_descriptor *epdesc;
247 int i, ii;
249 config = &dev->config;
250 usb_display_conf_desc(&config->desc, dev);
251 for (i = 0; i < config->no_of_if; i++) {
252 ifdesc = &config->if_desc[i];
253 usb_display_if_desc(&ifdesc->desc, dev);
254 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
255 epdesc = &ifdesc->ep_desc[ii];
256 usb_display_ep_desc(epdesc);
259 printf("\n");
263 * With driver model this isn't right since we can have multiple controllers
264 * and the device numbering starts at 1 on each bus.
265 * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
267 static struct usb_device *usb_find_device(int devnum)
269 #ifdef CONFIG_DM_USB
270 struct usb_device *udev;
271 struct udevice *hub;
272 struct uclass *uc;
273 int ret;
275 /* Device addresses start at 1 */
276 devnum++;
277 ret = uclass_get(UCLASS_USB_HUB, &uc);
278 if (ret)
279 return NULL;
281 uclass_foreach_dev(hub, uc) {
282 struct udevice *dev;
284 if (!device_active(hub))
285 continue;
286 udev = dev_get_parent_priv(hub);
287 if (udev->devnum == devnum)
288 return udev;
290 for (device_find_first_child(hub, &dev);
291 dev;
292 device_find_next_child(&dev)) {
293 if (!device_active(hub))
294 continue;
296 udev = dev_get_parent_priv(dev);
297 if (udev->devnum == devnum)
298 return udev;
301 #else
302 struct usb_device *udev;
303 int d;
305 for (d = 0; d < USB_MAX_DEVICE; d++) {
306 udev = usb_get_dev_index(d);
307 if (udev == NULL)
308 return NULL;
309 if (udev->devnum == devnum)
310 return udev;
312 #endif
314 return NULL;
317 static inline const char *portspeed(int speed)
319 switch (speed) {
320 case USB_SPEED_SUPER:
321 return "5 Gb/s";
322 case USB_SPEED_HIGH:
323 return "480 Mb/s";
324 case USB_SPEED_LOW:
325 return "1.5 Mb/s";
326 default:
327 return "12 Mb/s";
331 /* shows the device tree recursively */
332 static void usb_show_tree_graph(struct usb_device *dev, char *pre)
334 int index;
335 int has_child, last_child;
337 index = strlen(pre);
338 printf(" %s", pre);
339 #ifdef CONFIG_DM_USB
340 has_child = device_has_active_children(dev->dev);
341 if (device_get_uclass_id(dev->dev) == UCLASS_MASS_STORAGE) {
342 struct udevice *child;
344 for (device_find_first_child(dev->dev, &child);
345 child;
346 device_find_next_child(&child)) {
347 if (device_get_uclass_id(child) == UCLASS_BLK)
348 has_child = 0;
351 #else
352 /* check if the device has connected children */
353 int i;
355 has_child = 0;
356 for (i = 0; i < dev->maxchild; i++) {
357 if (dev->children[i] != NULL)
358 has_child = 1;
360 #endif
361 /* check if we are the last one */
362 #ifdef CONFIG_DM_USB
363 /* Not the root of the usb tree? */
364 if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
365 last_child = device_is_last_sibling(dev->dev);
366 #else
367 if (dev->parent != NULL) { /* not root? */
368 last_child = 1;
369 for (i = 0; i < dev->parent->maxchild; i++) {
370 /* search for children */
371 if (dev->parent->children[i] == dev) {
372 /* found our pointer, see if we have a
373 * little sister
375 while (i++ < dev->parent->maxchild) {
376 if (dev->parent->children[i] != NULL) {
377 /* found a sister */
378 last_child = 0;
379 break;
380 } /* if */
381 } /* while */
382 } /* device found */
383 } /* for all children of the parent */
384 #endif
385 printf("\b+-");
386 /* correct last child */
387 if (last_child && index)
388 pre[index-1] = ' ';
389 } /* if not root hub */
390 else
391 printf(" ");
392 printf("%d ", dev->devnum);
393 pre[index++] = ' ';
394 pre[index++] = has_child ? '|' : ' ';
395 pre[index] = 0;
396 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
397 dev->config.if_desc[0].desc.bInterfaceClass),
398 portspeed(dev->speed),
399 dev->config.desc.bMaxPower * 2);
400 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
401 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
402 printf(" %s\n", pre);
403 #ifdef CONFIG_DM_USB
404 struct udevice *child;
406 for (device_find_first_child(dev->dev, &child);
407 child;
408 device_find_next_child(&child)) {
409 struct usb_device *udev;
411 if (!device_active(child))
412 continue;
414 udev = dev_get_parent_priv(child);
417 * Ignore emulators and block child devices, we only want
418 * real devices
420 if (udev &&
421 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
422 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
423 (device_get_uclass_id(child) != UCLASS_BLK)) {
424 usb_show_tree_graph(udev, pre);
425 pre[index] = 0;
428 #else
429 if (dev->maxchild > 0) {
430 for (i = 0; i < dev->maxchild; i++) {
431 if (dev->children[i] != NULL) {
432 usb_show_tree_graph(dev->children[i], pre);
433 pre[index] = 0;
437 #endif
440 /* main routine for the tree command */
441 static void usb_show_subtree(struct usb_device *dev)
443 char preamble[32];
445 memset(preamble, '\0', sizeof(preamble));
446 usb_show_tree_graph(dev, &preamble[0]);
449 #ifdef CONFIG_DM_USB
450 typedef void (*usb_dev_func_t)(struct usb_device *udev);
452 static void usb_for_each_root_dev(usb_dev_func_t func)
454 struct udevice *bus;
456 for (uclass_find_first_device(UCLASS_USB, &bus);
457 bus;
458 uclass_find_next_device(&bus)) {
459 struct usb_device *udev;
460 struct udevice *dev;
462 if (!device_active(bus))
463 continue;
465 device_find_first_child(bus, &dev);
466 if (dev && device_active(dev)) {
467 udev = dev_get_parent_priv(dev);
468 func(udev);
472 #endif
474 void usb_show_tree(void)
476 #ifdef CONFIG_DM_USB
477 usb_for_each_root_dev(usb_show_subtree);
478 #else
479 struct usb_device *udev;
480 int i;
482 for (i = 0; i < USB_MAX_DEVICE; i++) {
483 udev = usb_get_dev_index(i);
484 if (udev == NULL)
485 break;
486 if (udev->parent == NULL)
487 usb_show_subtree(udev);
489 #endif
492 static int usb_test(struct usb_device *dev, int port, char* arg)
494 int mode;
496 if (port > dev->maxchild) {
497 printf("Device is no hub or does not have %d ports.\n", port);
498 return 1;
501 switch (arg[0]) {
502 case 'J':
503 case 'j':
504 printf("Setting Test_J mode");
505 mode = USB_TEST_MODE_J;
506 break;
507 case 'K':
508 case 'k':
509 printf("Setting Test_K mode");
510 mode = USB_TEST_MODE_K;
511 break;
512 case 'S':
513 case 's':
514 printf("Setting Test_SE0_NAK mode");
515 mode = USB_TEST_MODE_SE0_NAK;
516 break;
517 case 'P':
518 case 'p':
519 printf("Setting Test_Packet mode");
520 mode = USB_TEST_MODE_PACKET;
521 break;
522 case 'F':
523 case 'f':
524 printf("Setting Test_Force_Enable mode");
525 mode = USB_TEST_MODE_FORCE_ENABLE;
526 break;
527 default:
528 printf("Unrecognized test mode: %s\nAvailable modes: "
529 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
530 return 1;
533 if (port)
534 printf(" on downstream facing port %d...\n", port);
535 else
536 printf(" on upstream facing port...\n");
538 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
539 port ? USB_RT_PORT : USB_RECIP_DEVICE,
540 port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
541 (mode << 8) | port,
542 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
543 printf("Error during SET_FEATURE.\n");
544 return 1;
545 } else {
546 printf("Test mode successfully set. Use 'usb start' "
547 "to return to normal operation.\n");
548 return 0;
552 /******************************************************************************
553 * usb boot command intepreter. Derived from diskboot
555 #ifdef CONFIG_USB_STORAGE
556 static int do_usbboot(struct cmd_tbl *cmdtp, int flag, int argc,
557 char *const argv[])
559 return common_diskboot(cmdtp, "usb", argc, argv);
561 #endif /* CONFIG_USB_STORAGE */
563 static void do_usb_start(void)
565 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
567 if (usb_init() < 0)
568 return;
570 /* Driver model will probe the devices as they are found */
571 # ifdef CONFIG_USB_STORAGE
572 /* try to recognize storage devices immediately */
573 usb_stor_curr_dev = usb_stor_scan(1);
574 # endif
577 #ifdef CONFIG_DM_USB
578 static void usb_show_info(struct usb_device *udev)
580 struct udevice *child;
582 usb_display_desc(udev);
583 usb_display_config(udev);
584 for (device_find_first_child(udev->dev, &child);
585 child;
586 device_find_next_child(&child)) {
587 if (device_active(child) &&
588 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
589 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
590 (device_get_uclass_id(child) != UCLASS_BLK)) {
591 udev = dev_get_parent_priv(child);
592 if (udev)
593 usb_show_info(udev);
597 #endif
599 /******************************************************************************
600 * usb command intepreter
602 static int do_usb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
604 struct usb_device *udev = NULL;
605 int i;
607 if (argc < 2)
608 return CMD_RET_USAGE;
610 if (strncmp(argv[1], "start", 5) == 0) {
611 if (usb_started)
612 return 0; /* Already started */
613 printf("starting USB...\n");
614 do_usb_start();
615 return 0;
618 if (strncmp(argv[1], "reset", 5) == 0) {
619 printf("resetting USB...\n");
620 usb_stop();
621 do_usb_start();
622 return 0;
624 if (strncmp(argv[1], "stop", 4) == 0) {
625 if (argc != 2)
626 console_assign(stdin, "serial");
627 printf("stopping USB..\n");
628 usb_stop();
629 return 0;
631 if (!usb_started) {
632 printf("USB is stopped. Please issue 'usb start' first.\n");
633 return 1;
635 if (strncmp(argv[1], "tree", 4) == 0) {
636 puts("USB device tree:\n");
637 usb_show_tree();
638 return 0;
640 if (strncmp(argv[1], "inf", 3) == 0) {
641 if (argc == 2) {
642 #ifdef CONFIG_DM_USB
643 usb_for_each_root_dev(usb_show_info);
644 #else
645 int d;
646 for (d = 0; d < USB_MAX_DEVICE; d++) {
647 udev = usb_get_dev_index(d);
648 if (udev == NULL)
649 break;
650 usb_display_desc(udev);
651 usb_display_config(udev);
653 #endif
654 return 0;
655 } else {
657 * With driver model this isn't right since we can
658 * have multiple controllers and the device numbering
659 * starts at 1 on each bus.
661 i = dectoul(argv[2], NULL);
662 printf("config for device %d\n", i);
663 udev = usb_find_device(i);
664 if (udev == NULL) {
665 printf("*** No device available ***\n");
666 return 0;
667 } else {
668 usb_display_desc(udev);
669 usb_display_config(udev);
672 return 0;
674 if (strncmp(argv[1], "test", 4) == 0) {
675 if (argc < 5)
676 return CMD_RET_USAGE;
677 i = dectoul(argv[2], NULL);
678 udev = usb_find_device(i);
679 if (udev == NULL) {
680 printf("Device %d does not exist.\n", i);
681 return 1;
683 i = dectoul(argv[3], NULL);
684 return usb_test(udev, i, argv[4]);
686 #ifdef CONFIG_USB_STORAGE
687 if (strncmp(argv[1], "stor", 4) == 0)
688 return usb_stor_info();
690 return blk_common_cmd(argc, argv, UCLASS_USB, &usb_stor_curr_dev);
691 #else
692 return CMD_RET_USAGE;
693 #endif /* CONFIG_USB_STORAGE */
696 U_BOOT_CMD(
697 usb, 5, 1, do_usb,
698 "USB sub-system",
699 "start - start (scan) USB controller\n"
700 "usb reset - reset (rescan) USB controller\n"
701 "usb stop [f] - stop USB [f]=force stop\n"
702 "usb tree - show USB device tree\n"
703 "usb info [dev] - show available USB devices\n"
704 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
705 " (specify port 0 to indicate the device's upstream port)\n"
706 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
707 #ifdef CONFIG_USB_STORAGE
708 "usb storage - show details of USB storage devices\n"
709 "usb dev [dev] - show or set current USB storage device\n"
710 "usb part [dev] - print partition table of one or all USB storage"
711 " devices\n"
712 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
713 " to memory address `addr'\n"
714 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
715 " from memory address `addr'"
716 #endif /* CONFIG_USB_STORAGE */
719 #ifdef CONFIG_USB_STORAGE
720 U_BOOT_CMD(
721 usbboot, 3, 1, do_usbboot,
722 "boot from USB device",
723 "loadAddr dev:part"
725 #endif /* CONFIG_USB_STORAGE */