]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_usb.c
Merge git://git.denx.de/u-boot-dm
[people/ms/u-boot.git] / common / cmd_usb.c
CommitLineData
e887afc9
WD
1/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
6a1b206d
SG
5 * Adapted for U-Boot driver model
6 * (C) Copyright 2015 Google, Inc
7 *
e887afc9
WD
8 * Most of this source has been derived from the Linux USB
9 * project.
10 *
1a459660 11 * SPDX-License-Identifier: GPL-2.0+
e887afc9
WD
12 */
13
14#include <common.h>
15#include <command.h>
6a1b206d 16#include <dm.h>
414eec35 17#include <asm/byteorder.h>
b2fb47f1 18#include <asm/unaligned.h>
735dd97b 19#include <part.h>
e887afc9 20#include <usb.h>
e887afc9 21
1968e615 22#ifdef CONFIG_USB_STORAGE
d0ff51ba 23static int usb_stor_curr_dev = -1; /* current device */
1968e615 24#endif
89d48367
SG
25#ifdef CONFIG_USB_HOST_ETHER
26static int usb_ether_curr_dev = -1; /* current ethernet device */
27#endif
e887afc9 28
a2663ea4 29/* some display routines (info command) */
088f1b19 30static char *usb_get_class_desc(unsigned char dclass)
e887afc9 31{
de39f8c1
MT
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 "";
e887afc9
WD
53 }
54}
55
088f1b19
KP
56static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
57 unsigned char proto)
e887afc9 58{
de39f8c1
MT
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");
e887afc9 68 break;
de39f8c1
MT
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;
e887afc9
WD
84 }
85 break;
de39f8c1
MT
86 default:
87 printf("reserved");
88 break;
89 }
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;
115 }
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");
e887afc9
WD
126 break;
127 default:
de39f8c1
MT
128 printf("reserved");
129 break;
130 }
131 break;
132 default:
133 printf("%s", usb_get_class_desc(dclass));
134 break;
e887afc9
WD
135 }
136}
137
088f1b19 138static void usb_display_string(struct usb_device *dev, int index)
e887afc9 139{
f5766139
PS
140 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
141
de39f8c1
MT
142 if (index != 0) {
143 if (usb_string(dev, index, &buffer[0], 256) > 0)
144 printf("String: \"%s\"", buffer);
e887afc9
WD
145 }
146}
147
088f1b19 148static void usb_display_desc(struct usb_device *dev)
e887afc9 149{
de39f8c1
MT
150 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
151 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
8f8bd565 152 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
de39f8c1
MT
153 (dev->descriptor.bcdUSB>>8) & 0xff,
154 dev->descriptor.bcdUSB & 0xff);
155
156 if (strlen(dev->mf) || strlen(dev->prod) ||
157 strlen(dev->serial))
158 printf(" - %s %s %s\n", dev->mf, dev->prod,
159 dev->serial);
e887afc9
WD
160 if (dev->descriptor.bDeviceClass) {
161 printf(" - Class: ");
de39f8c1
MT
162 usb_display_class_sub(dev->descriptor.bDeviceClass,
163 dev->descriptor.bDeviceSubClass,
164 dev->descriptor.bDeviceProtocol);
e887afc9 165 printf("\n");
de39f8c1
MT
166 } else {
167 printf(" - Class: (from Interface) %s\n",
168 usb_get_class_desc(
8f8bd565 169 dev->config.if_desc[0].desc.bInterfaceClass));
e887afc9 170 }
de39f8c1
MT
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);
e887afc9
WD
178 }
179
180}
181
c60795f4 182static void usb_display_conf_desc(struct usb_config_descriptor *config,
088f1b19 183 struct usb_device *dev)
e887afc9 184{
de39f8c1
MT
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 " : "",
8f8bd565 189 config->bMaxPower*2);
e887afc9
WD
190 if (config->iConfiguration) {
191 printf(" - ");
de39f8c1 192 usb_display_string(dev, config->iConfiguration);
e887afc9
WD
193 printf("\n");
194 }
195}
196
088f1b19
KP
197static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
198 struct usb_device *dev)
e887afc9 199{
de39f8c1
MT
200 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
201 printf(" - Alternate Setting %d, Endpoints: %d\n",
202 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
e887afc9 203 printf(" - Class ");
de39f8c1
MT
204 usb_display_class_sub(ifdesc->bInterfaceClass,
205 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
e887afc9
WD
206 printf("\n");
207 if (ifdesc->iInterface) {
208 printf(" - ");
de39f8c1 209 usb_display_string(dev, ifdesc->iInterface);
e887afc9
WD
210 printf("\n");
211 }
212}
213
088f1b19 214static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
e887afc9 215{
de39f8c1
MT
216 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
217 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
218 switch ((epdesc->bmAttributes & 0x03)) {
219 case 0:
220 printf("Control");
221 break;
222 case 1:
223 printf("Isochronous");
224 break;
225 case 2:
226 printf("Bulk");
227 break;
228 case 3:
229 printf("Interrupt");
230 break;
e887afc9 231 }
b2fb47f1 232 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
de39f8c1
MT
233 if ((epdesc->bmAttributes & 0x03) == 0x3)
234 printf(" Interval %dms", epdesc->bInterval);
e887afc9
WD
235 printf("\n");
236}
237
238/* main routine to diasplay the configs, interfaces and endpoints */
088f1b19 239static void usb_display_config(struct usb_device *dev)
e887afc9 240{
8f8bd565
TR
241 struct usb_config *config;
242 struct usb_interface *ifdesc;
e887afc9 243 struct usb_endpoint_descriptor *epdesc;
de39f8c1
MT
244 int i, ii;
245
246 config = &dev->config;
8f8bd565 247 usb_display_conf_desc(&config->desc, dev);
de39f8c1
MT
248 for (i = 0; i < config->no_of_if; i++) {
249 ifdesc = &config->if_desc[i];
8f8bd565 250 usb_display_if_desc(&ifdesc->desc, dev);
de39f8c1
MT
251 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
252 epdesc = &ifdesc->ep_desc[ii];
e887afc9
WD
253 usb_display_ep_desc(epdesc);
254 }
255 }
256 printf("\n");
257}
258
6a1b206d
SG
259/*
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.
263 */
7d9aa8fd
JW
264static struct usb_device *usb_find_device(int devnum)
265{
6a1b206d
SG
266#ifdef CONFIG_DM_USB
267 struct usb_device *udev;
268 struct udevice *hub;
269 struct uclass *uc;
270 int ret;
271
272 /* Device addresses start at 1 */
273 devnum++;
274 ret = uclass_get(UCLASS_USB_HUB, &uc);
275 if (ret)
276 return NULL;
277
278 uclass_foreach_dev(hub, uc) {
279 struct udevice *dev;
280
281 if (!device_active(hub))
282 continue;
283 udev = dev_get_parentdata(hub);
284 if (udev->devnum == devnum)
285 return udev;
286
287 for (device_find_first_child(hub, &dev);
288 dev;
289 device_find_next_child(&dev)) {
290 if (!device_active(hub))
291 continue;
292
293 udev = dev_get_parentdata(dev);
294 if (udev->devnum == devnum)
295 return udev;
296 }
297 }
298#else
cad4291c 299 struct usb_device *udev;
7d9aa8fd
JW
300 int d;
301
302 for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291c
SG
303 udev = usb_get_dev_index(d);
304 if (udev == NULL)
7d9aa8fd 305 return NULL;
cad4291c
SG
306 if (udev->devnum == devnum)
307 return udev;
7d9aa8fd 308 }
6a1b206d 309#endif
7d9aa8fd
JW
310
311 return NULL;
312}
313
f1c1f540
SR
314static inline char *portspeed(int speed)
315{
6497c667
VG
316 char *speed_str;
317
318 switch (speed) {
319 case USB_SPEED_SUPER:
320 speed_str = "5 Gb/s";
321 break;
322 case USB_SPEED_HIGH:
323 speed_str = "480 Mb/s";
324 break;
325 case USB_SPEED_LOW:
326 speed_str = "1.5 Mb/s";
327 break;
328 default:
329 speed_str = "12 Mb/s";
330 break;
331 }
332
333 return speed_str;
f1c1f540
SR
334}
335
e887afc9 336/* shows the device tree recursively */
088f1b19 337static void usb_show_tree_graph(struct usb_device *dev, char *pre)
e887afc9 338{
6a1b206d 339 int index;
10f4dd78 340 int has_child, last_child;
e887afc9 341
de39f8c1
MT
342 index = strlen(pre);
343 printf(" %s", pre);
6a1b206d
SG
344#ifdef CONFIG_DM_USB
345 has_child = device_has_active_children(dev->dev);
346#else
e887afc9 347 /* check if the device has connected children */
6a1b206d
SG
348 int i;
349
de39f8c1
MT
350 has_child = 0;
351 for (i = 0; i < dev->maxchild; i++) {
352 if (dev->children[i] != NULL)
353 has_child = 1;
e887afc9 354 }
6a1b206d 355#endif
e887afc9 356 /* check if we are the last one */
6a1b206d
SG
357#ifdef CONFIG_DM_USB
358 last_child = device_is_last_sibling(dev->dev);
359#else
cad4291c 360 last_child = (dev->parent != NULL);
6a1b206d 361#endif
cad4291c 362 if (last_child) {
6a1b206d 363#ifndef CONFIG_DM_USB
de39f8c1 364 for (i = 0; i < dev->parent->maxchild; i++) {
e887afc9 365 /* search for children */
de39f8c1
MT
366 if (dev->parent->children[i] == dev) {
367 /* found our pointer, see if we have a
368 * little sister
369 */
de39f8c1
MT
370 while (i++ < dev->parent->maxchild) {
371 if (dev->parent->children[i] != NULL) {
e887afc9 372 /* found a sister */
de39f8c1 373 last_child = 0;
e887afc9
WD
374 break;
375 } /* if */
376 } /* while */
377 } /* device found */
378 } /* for all children of the parent */
6a1b206d 379#endif
e887afc9
WD
380 printf("\b+-");
381 /* correct last child */
cad4291c 382 if (last_child && index)
de39f8c1 383 pre[index-1] = ' ';
e887afc9
WD
384 } /* if not root hub */
385 else
386 printf(" ");
de39f8c1
MT
387 printf("%d ", dev->devnum);
388 pre[index++] = ' ';
389 pre[index++] = has_child ? '|' : ' ';
390 pre[index] = 0;
391 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
8f8bd565 392 dev->config.if_desc[0].desc.bInterfaceClass),
f1c1f540 393 portspeed(dev->speed),
8f8bd565 394 dev->config.desc.bMaxPower * 2);
de39f8c1
MT
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);
6a1b206d
SG
398#ifdef CONFIG_DM_USB
399 struct udevice *child;
400
401 for (device_find_first_child(dev->dev, &child);
402 child;
403 device_find_next_child(&child)) {
404 struct usb_device *udev;
405
406 if (!device_active(child))
407 continue;
408
409 udev = dev_get_parentdata(child);
410
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);
414 pre[index] = 0;
415 }
416 }
417#else
de39f8c1
MT
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);
422 pre[index] = 0;
e887afc9
WD
423 }
424 }
425 }
6a1b206d 426#endif
e887afc9
WD
427}
428
429/* main routine for the tree command */
088f1b19 430static void usb_show_tree(struct usb_device *dev)
e887afc9
WD
431{
432 char preamble[32];
433
cad4291c 434 memset(preamble, '\0', sizeof(preamble));
de39f8c1 435 usb_show_tree_graph(dev, &preamble[0]);
e887afc9
WD
436}
437
7d9aa8fd
JW
438static int usb_test(struct usb_device *dev, int port, char* arg)
439{
440 int mode;
441
442 if (port > dev->maxchild) {
443 printf("Device is no hub or does not have %d ports.\n", port);
444 return 1;
445 }
446
447 switch (arg[0]) {
448 case 'J':
449 case 'j':
450 printf("Setting Test_J mode");
451 mode = USB_TEST_MODE_J;
452 break;
453 case 'K':
454 case 'k':
455 printf("Setting Test_K mode");
456 mode = USB_TEST_MODE_K;
457 break;
458 case 'S':
459 case 's':
460 printf("Setting Test_SE0_NAK mode");
461 mode = USB_TEST_MODE_SE0_NAK;
462 break;
463 case 'P':
464 case 'p':
465 printf("Setting Test_Packet mode");
466 mode = USB_TEST_MODE_PACKET;
467 break;
468 case 'F':
469 case 'f':
470 printf("Setting Test_Force_Enable mode");
471 mode = USB_TEST_MODE_FORCE_ENABLE;
472 break;
473 default:
474 printf("Unrecognized test mode: %s\nAvailable modes: "
475 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
476 return 1;
477 }
478
479 if (port)
480 printf(" on downstream facing port %d...\n", port);
481 else
482 printf(" on upstream facing port...\n");
483
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,
487 (mode << 8) | port,
488 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
489 printf("Error during SET_FEATURE.\n");
490 return 1;
491 } else {
492 printf("Test mode successfully set. Use 'usb start' "
493 "to return to normal operation.\n");
494 return 0;
495 }
496}
497
e887afc9 498
e887afc9
WD
499/******************************************************************************
500 * usb boot command intepreter. Derived from diskboot
501 */
502#ifdef CONFIG_USB_STORAGE
088f1b19 503static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9 504{
7405a133 505 return common_diskboot(cmdtp, "usb", argc, argv);
e887afc9
WD
506}
507#endif /* CONFIG_USB_STORAGE */
508
8a8a2257 509static int do_usb_stop_keyboard(int force)
6e78c74f
HG
510{
511#ifdef CONFIG_USB_KEYBOARD
8a8a2257 512 if (usb_kbd_deregister(force) != 0) {
6e78c74f
HG
513 printf("USB not stopped: usbkbd still using USB\n");
514 return 1;
515 }
516#endif
517 return 0;
518}
e887afc9 519
b5072264
HG
520static void do_usb_start(void)
521{
522 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
523
524 if (usb_init() < 0)
525 return;
526
6a1b206d
SG
527 /* Driver model will probe the devices as they are found */
528#ifndef CONFIG_DM_USB
b5072264
HG
529#ifdef CONFIG_USB_STORAGE
530 /* try to recognize storage devices immediately */
531 usb_stor_curr_dev = usb_stor_scan(1);
532#endif
6a1b206d 533#endif
b5072264
HG
534#ifdef CONFIG_USB_HOST_ETHER
535 /* try to recognize ethernet devices immediately */
536 usb_ether_curr_dev = usb_host_eth_scan(1);
537#endif
538#ifdef CONFIG_USB_KEYBOARD
539 drv_usb_kbd_init();
540#endif
541}
542
6a1b206d
SG
543#ifdef CONFIG_DM_USB
544static void show_info(struct udevice *dev)
545{
546 struct udevice *child;
547 struct usb_device *udev;
548
549 udev = dev_get_parentdata(dev);
550 usb_display_desc(udev);
551 usb_display_config(udev);
552 for (device_find_first_child(dev, &child);
553 child;
554 device_find_next_child(&child)) {
555 if (device_active(child))
556 show_info(child);
557 }
558}
559
560static int usb_device_info(void)
561{
562 struct udevice *bus;
563
564 for (uclass_first_device(UCLASS_USB, &bus);
565 bus;
566 uclass_next_device(&bus)) {
567 struct udevice *hub;
568
569 device_find_first_child(bus, &hub);
570 if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
571 device_active(hub)) {
572 show_info(hub);
573 }
574 }
575
576 return 0;
577}
578#endif
579
de39f8c1 580/******************************************************************************
e887afc9
WD
581 * usb command intepreter
582 */
088f1b19 583static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9 584{
cad4291c 585 struct usb_device *udev = NULL;
e887afc9 586 int i;
e51aae38 587 extern char usb_started;
1968e615 588#ifdef CONFIG_USB_STORAGE
e887afc9 589 block_dev_desc_t *stor_dev;
1968e615 590#endif
e887afc9 591
47e26b1b 592 if (argc < 2)
4c12eeb8 593 return CMD_RET_USAGE;
65d34254 594
b5072264
HG
595 if (strncmp(argv[1], "start", 5) == 0) {
596 if (usb_started)
597 return 0; /* Already started */
598 printf("starting USB...\n");
599 do_usb_start();
600 return 0;
601 }
602
603 if (strncmp(argv[1], "reset", 5) == 0) {
604 printf("resetting USB...\n");
8a8a2257 605 if (do_usb_stop_keyboard(1) != 0)
6e78c74f 606 return 1;
e887afc9 607 usb_stop();
b5072264 608 do_usb_start();
e887afc9
WD
609 return 0;
610 }
de39f8c1 611 if (strncmp(argv[1], "stop", 4) == 0) {
6e78c74f 612 if (argc != 2)
de39f8c1 613 console_assign(stdin, "serial");
8a8a2257 614 if (do_usb_stop_keyboard(0) != 0)
6e78c74f 615 return 1;
e887afc9
WD
616 printf("stopping USB..\n");
617 usb_stop();
618 return 0;
619 }
e51aae38
BS
620 if (!usb_started) {
621 printf("USB is stopped. Please issue 'usb start' first.\n");
622 return 1;
623 }
de39f8c1 624 if (strncmp(argv[1], "tree", 4) == 0) {
93c2582f 625 puts("USB device tree:\n");
6a1b206d
SG
626#ifdef CONFIG_DM_USB
627 struct udevice *bus;
628
629 for (uclass_first_device(UCLASS_USB, &bus);
630 bus;
631 uclass_next_device(&bus)) {
632 struct usb_device *udev;
633 struct udevice *hub;
634
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);
639 usb_show_tree(udev);
640 }
641 }
642#else
93c2582f 643 for (i = 0; i < USB_MAX_DEVICE; i++) {
cad4291c
SG
644 udev = usb_get_dev_index(i);
645 if (udev == NULL)
93c2582f 646 break;
cad4291c
SG
647 if (udev->parent == NULL)
648 usb_show_tree(udev);
93c2582f 649 }
6a1b206d 650#endif
e887afc9
WD
651 return 0;
652 }
de39f8c1 653 if (strncmp(argv[1], "inf", 3) == 0) {
de39f8c1 654 if (argc == 2) {
6a1b206d
SG
655#ifdef CONFIG_DM_USB
656 usb_device_info();
657#else
658 int d;
de39f8c1 659 for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291c
SG
660 udev = usb_get_dev_index(d);
661 if (udev == NULL)
e887afc9 662 break;
cad4291c
SG
663 usb_display_desc(udev);
664 usb_display_config(udev);
e887afc9 665 }
6a1b206d 666#endif
e887afc9 667 return 0;
de39f8c1 668 } else {
6a1b206d
SG
669 /*
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.
673 */
7d9aa8fd 674 i = simple_strtoul(argv[2], NULL, 10);
de39f8c1 675 printf("config for device %d\n", i);
cad4291c
SG
676 udev = usb_find_device(i);
677 if (udev == NULL) {
6052cbab 678 printf("*** No device available ***\n");
e887afc9 679 return 0;
de39f8c1 680 } else {
cad4291c
SG
681 usb_display_desc(udev);
682 usb_display_config(udev);
e887afc9
WD
683 }
684 }
685 return 0;
686 }
7d9aa8fd
JW
687 if (strncmp(argv[1], "test", 4) == 0) {
688 if (argc < 5)
689 return CMD_RET_USAGE;
690 i = simple_strtoul(argv[2], NULL, 10);
cad4291c
SG
691 udev = usb_find_device(i);
692 if (udev == NULL) {
7d9aa8fd
JW
693 printf("Device %d does not exist.\n", i);
694 return 1;
695 }
696 i = simple_strtoul(argv[3], NULL, 10);
cad4291c 697 return usb_test(udev, i, argv[4]);
7d9aa8fd 698 }
e887afc9 699#ifdef CONFIG_USB_STORAGE
de39f8c1 700 if (strncmp(argv[1], "stor", 4) == 0)
f6b44e0e 701 return usb_stor_info();
9c998aa8 702
de39f8c1 703 if (strncmp(argv[1], "part", 4) == 0) {
d4b5f3fa 704 int devno, ok = 0;
de39f8c1 705 if (argc == 2) {
aaad108b 706 for (devno = 0; ; ++devno) {
de39f8c1 707 stor_dev = usb_stor_get_dev(devno);
aaad108b
KH
708 if (stor_dev == NULL)
709 break;
de39f8c1 710 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
d4b5f3fa
CE
711 ok++;
712 if (devno)
713 printf("\n");
060f2853 714 debug("print_part of %x\n", devno);
d4b5f3fa
CE
715 print_part(stor_dev);
716 }
717 }
de39f8c1
MT
718 } else {
719 devno = simple_strtoul(argv[2], NULL, 16);
720 stor_dev = usb_stor_get_dev(devno);
aaad108b
KH
721 if (stor_dev != NULL &&
722 stor_dev->type != DEV_TYPE_UNKNOWN) {
e887afc9 723 ok++;
060f2853 724 debug("print_part of %x\n", devno);
e887afc9
WD
725 print_part(stor_dev);
726 }
727 }
728 if (!ok) {
729 printf("\nno USB devices available\n");
730 return 1;
731 }
732 return 0;
733 }
de39f8c1
MT
734 if (strcmp(argv[1], "read") == 0) {
735 if (usb_stor_curr_dev < 0) {
e887afc9
WD
736 printf("no current device selected\n");
737 return 1;
738 }
de39f8c1 739 if (argc == 5) {
e887afc9
WD
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);
743 unsigned long n;
de39f8c1
MT
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,
748 (ulong *)addr);
749 printf("%ld blocks read: %s\n", n,
750 (n == cnt) ? "OK" : "ERROR");
751 if (n == cnt)
e887afc9
WD
752 return 0;
753 return 1;
754 }
755 }
127e1084
MJ
756 if (strcmp(argv[1], "write") == 0) {
757 if (usb_stor_curr_dev < 0) {
758 printf("no current device selected\n");
759 return 1;
760 }
761 if (argc == 5) {
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);
765 unsigned long n;
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,
770 (ulong *)addr);
771 printf("%ld blocks write: %s\n", n,
772 (n == cnt) ? "OK" : "ERROR");
773 if (n == cnt)
774 return 0;
775 return 1;
776 }
777 }
9c998aa8
WD
778 if (strncmp(argv[1], "dev", 3) == 0) {
779 if (argc == 3) {
e887afc9 780 int dev = (int)simple_strtoul(argv[2], NULL, 10);
de39f8c1 781 printf("\nUSB device %d: ", dev);
aaad108b
KH
782 stor_dev = usb_stor_get_dev(dev);
783 if (stor_dev == NULL) {
e887afc9
WD
784 printf("unknown device\n");
785 return 1;
786 }
de39f8c1 787 printf("\n Device %d: ", dev);
e887afc9 788 dev_print(stor_dev);
de39f8c1 789 if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9 790 return 1;
e887afc9
WD
791 usb_stor_curr_dev = dev;
792 printf("... is now current device\n");
793 return 0;
de39f8c1
MT
794 } else {
795 printf("\nUSB device %d: ", usb_stor_curr_dev);
796 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
e887afc9 797 dev_print(stor_dev);
de39f8c1 798 if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9 799 return 1;
e887afc9
WD
800 return 0;
801 }
802 return 0;
803 }
804#endif /* CONFIG_USB_STORAGE */
4c12eeb8 805 return CMD_RET_USAGE;
e887afc9
WD
806}
807
0d498393
WD
808U_BOOT_CMD(
809 usb, 5, 1, do_usb,
2fb2604d 810 "USB sub-system",
1af9f963
VPP
811 "start - start (scan) USB controller\n"
812 "usb reset - reset (rescan) USB controller\n"
b3813a22
VPP
813 "usb stop [f] - stop USB [f]=force stop\n"
814 "usb tree - show USB device tree\n"
5cf91d6b 815 "usb info [dev] - show available USB devices\n"
7d9aa8fd
JW
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
b3813a22 820 "usb storage - show details of USB storage devices\n"
342717f7 821 "usb dev [dev] - show or set current USB storage device\n"
de39f8c1 822 "usb part [dev] - print partition table of one or all USB storage"
7d9aa8fd 823 " devices\n"
5cf91d6b 824 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
842404ea 825 " to memory address `addr'\n"
127e1084
MJ
826 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
827 " from memory address `addr'"
7d9aa8fd 828#endif /* CONFIG_USB_STORAGE */
8bde7f77
WD
829);
830
831
7d9aa8fd 832#ifdef CONFIG_USB_STORAGE
0d498393
WD
833U_BOOT_CMD(
834 usbboot, 3, 1, do_usbboot,
2fb2604d 835 "boot from USB device",
a89c33db 836 "loadAddr dev:part"
8bde7f77 837);
7d9aa8fd 838#endif /* CONFIG_USB_STORAGE */