Add support for LZ4 decompression algorithm
[oweals/u-boot.git] / common / cmd_usb.c
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland
4  *
5  * Adapted for U-Boot driver model
6  * (C) Copyright 2015 Google, Inc
7  *
8  * Most of this source has been derived from the Linux USB
9  * project.
10  *
11  * SPDX-License-Identifier:     GPL-2.0+
12  */
13
14 #include <common.h>
15 #include <command.h>
16 #include <dm.h>
17 #include <memalign.h>
18 #include <asm/byteorder.h>
19 #include <asm/unaligned.h>
20 #include <part.h>
21 #include <usb.h>
22
23 #ifdef CONFIG_USB_STORAGE
24 static int usb_stor_curr_dev = -1; /* current device */
25 #endif
26 #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
27 static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
28 #endif
29
30 /* some display routines (info command) */
31 static char *usb_get_class_desc(unsigned char dclass)
32 {
33         switch (dclass) {
34         case USB_CLASS_PER_INTERFACE:
35                 return "See Interface";
36         case USB_CLASS_AUDIO:
37                 return "Audio";
38         case USB_CLASS_COMM:
39                 return "Communication";
40         case USB_CLASS_HID:
41                 return "Human Interface";
42         case USB_CLASS_PRINTER:
43                 return "Printer";
44         case USB_CLASS_MASS_STORAGE:
45                 return "Mass Storage";
46         case USB_CLASS_HUB:
47                 return "Hub";
48         case USB_CLASS_DATA:
49                 return "CDC Data";
50         case USB_CLASS_VENDOR_SPEC:
51                 return "Vendor specific";
52         default:
53                 return "";
54         }
55 }
56
57 static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
58                                   unsigned char proto)
59 {
60         switch (dclass) {
61         case USB_CLASS_PER_INTERFACE:
62                 printf("See Interface");
63                 break;
64         case USB_CLASS_HID:
65                 printf("Human Interface, Subclass: ");
66                 switch (subclass) {
67                 case USB_SUB_HID_NONE:
68                         printf("None");
69                         break;
70                 case USB_SUB_HID_BOOT:
71                         printf("Boot ");
72                         switch (proto) {
73                         case USB_PROT_HID_NONE:
74                                 printf("None");
75                                 break;
76                         case USB_PROT_HID_KEYBOARD:
77                                 printf("Keyboard");
78                                 break;
79                         case USB_PROT_HID_MOUSE:
80                                 printf("Mouse");
81                                 break;
82                         default:
83                                 printf("reserved");
84                                 break;
85                         }
86                         break;
87                 default:
88                         printf("reserved");
89                         break;
90                 }
91                 break;
92         case USB_CLASS_MASS_STORAGE:
93                 printf("Mass Storage, ");
94                 switch (subclass) {
95                 case US_SC_RBC:
96                         printf("RBC ");
97                         break;
98                 case US_SC_8020:
99                         printf("SFF-8020i (ATAPI)");
100                         break;
101                 case US_SC_QIC:
102                         printf("QIC-157 (Tape)");
103                         break;
104                 case US_SC_UFI:
105                         printf("UFI");
106                         break;
107                 case US_SC_8070:
108                         printf("SFF-8070");
109                         break;
110                 case US_SC_SCSI:
111                         printf("Transp. SCSI");
112                         break;
113                 default:
114                         printf("reserved");
115                         break;
116                 }
117                 printf(", ");
118                 switch (proto) {
119                 case US_PR_CB:
120                         printf("Command/Bulk");
121                         break;
122                 case US_PR_CBI:
123                         printf("Command/Bulk/Int");
124                         break;
125                 case US_PR_BULK:
126                         printf("Bulk only");
127                         break;
128                 default:
129                         printf("reserved");
130                         break;
131                 }
132                 break;
133         default:
134                 printf("%s", usb_get_class_desc(dclass));
135                 break;
136         }
137 }
138
139 static void usb_display_string(struct usb_device *dev, int index)
140 {
141         ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
142
143         if (index != 0) {
144                 if (usb_string(dev, index, &buffer[0], 256) > 0)
145                         printf("String: \"%s\"", buffer);
146         }
147 }
148
149 static void usb_display_desc(struct usb_device *dev)
150 {
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);
156
157                 if (strlen(dev->mf) || strlen(dev->prod) ||
158                     strlen(dev->serial))
159                         printf(" - %s %s %s\n", dev->mf, dev->prod,
160                                 dev->serial);
161                 if (dev->descriptor.bDeviceClass) {
162                         printf(" - Class: ");
163                         usb_display_class_sub(dev->descriptor.bDeviceClass,
164                                               dev->descriptor.bDeviceSubClass,
165                                               dev->descriptor.bDeviceProtocol);
166                         printf("\n");
167                 } else {
168                         printf(" - Class: (from Interface) %s\n",
169                                usb_get_class_desc(
170                                 dev->config.if_desc[0].desc.bInterfaceClass));
171                 }
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);
179         }
180
181 }
182
183 static void usb_display_conf_desc(struct usb_config_descriptor *config,
184                                   struct usb_device *dev)
185 {
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) {
192                 printf("   - ");
193                 usb_display_string(dev, config->iConfiguration);
194                 printf("\n");
195         }
196 }
197
198 static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
199                                 struct usb_device *dev)
200 {
201         printf("     Interface: %d\n", ifdesc->bInterfaceNumber);
202         printf("     - Alternate Setting %d, Endpoints: %d\n",
203                 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
204         printf("     - Class ");
205         usb_display_class_sub(ifdesc->bInterfaceClass,
206                 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
207         printf("\n");
208         if (ifdesc->iInterface) {
209                 printf("     - ");
210                 usb_display_string(dev, ifdesc->iInterface);
211                 printf("\n");
212         }
213 }
214
215 static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
216 {
217         printf("     - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
218                 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
219         switch ((epdesc->bmAttributes & 0x03)) {
220         case 0:
221                 printf("Control");
222                 break;
223         case 1:
224                 printf("Isochronous");
225                 break;
226         case 2:
227                 printf("Bulk");
228                 break;
229         case 3:
230                 printf("Interrupt");
231                 break;
232         }
233         printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
234         if ((epdesc->bmAttributes & 0x03) == 0x3)
235                 printf(" Interval %dms", epdesc->bInterval);
236         printf("\n");
237 }
238
239 /* main routine to diasplay the configs, interfaces and endpoints */
240 static void usb_display_config(struct usb_device *dev)
241 {
242         struct usb_config *config;
243         struct usb_interface *ifdesc;
244         struct usb_endpoint_descriptor *epdesc;
245         int i, ii;
246
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);
255                 }
256         }
257         printf("\n");
258 }
259
260 /*
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.
264  */
265 static struct usb_device *usb_find_device(int devnum)
266 {
267 #ifdef CONFIG_DM_USB
268         struct usb_device *udev;
269         struct udevice *hub;
270         struct uclass *uc;
271         int ret;
272
273         /* Device addresses start at 1 */
274         devnum++;
275         ret = uclass_get(UCLASS_USB_HUB, &uc);
276         if (ret)
277                 return NULL;
278
279         uclass_foreach_dev(hub, uc) {
280                 struct udevice *dev;
281
282                 if (!device_active(hub))
283                         continue;
284                 udev = dev_get_parentdata(hub);
285                 if (udev->devnum == devnum)
286                         return udev;
287
288                 for (device_find_first_child(hub, &dev);
289                      dev;
290                      device_find_next_child(&dev)) {
291                         if (!device_active(hub))
292                                 continue;
293
294                         udev = dev_get_parentdata(dev);
295                         if (udev->devnum == devnum)
296                                 return udev;
297                 }
298         }
299 #else
300         struct usb_device *udev;
301         int d;
302
303         for (d = 0; d < USB_MAX_DEVICE; d++) {
304                 udev = usb_get_dev_index(d);
305                 if (udev == NULL)
306                         return NULL;
307                 if (udev->devnum == devnum)
308                         return udev;
309         }
310 #endif
311
312         return NULL;
313 }
314
315 static inline char *portspeed(int speed)
316 {
317         char *speed_str;
318
319         switch (speed) {
320         case USB_SPEED_SUPER:
321                 speed_str = "5 Gb/s";
322                 break;
323         case USB_SPEED_HIGH:
324                 speed_str = "480 Mb/s";
325                 break;
326         case USB_SPEED_LOW:
327                 speed_str = "1.5 Mb/s";
328                 break;
329         default:
330                 speed_str = "12 Mb/s";
331                 break;
332         }
333
334         return speed_str;
335 }
336
337 /* shows the device tree recursively */
338 static void usb_show_tree_graph(struct usb_device *dev, char *pre)
339 {
340         int index;
341         int has_child, last_child;
342
343         index = strlen(pre);
344         printf(" %s", pre);
345 #ifdef CONFIG_DM_USB
346         has_child = device_has_active_children(dev->dev);
347 #else
348         /* check if the device has connected children */
349         int i;
350
351         has_child = 0;
352         for (i = 0; i < dev->maxchild; i++) {
353                 if (dev->children[i] != NULL)
354                         has_child = 1;
355         }
356 #endif
357         /* check if we are the last one */
358 #ifdef CONFIG_DM_USB
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);
362 #else
363         if (dev->parent != NULL) { /* not root? */
364                 last_child = 1;
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
369                                  * little sister
370                                  */
371                                 while (i++ < dev->parent->maxchild) {
372                                         if (dev->parent->children[i] != NULL) {
373                                                 /* found a sister */
374                                                 last_child = 0;
375                                                 break;
376                                         } /* if */
377                                 } /* while */
378                         } /* device found */
379                 } /* for all children of the parent */
380 #endif
381                 printf("\b+-");
382                 /* correct last child */
383                 if (last_child && index)
384                         pre[index-1] = ' ';
385         } /* if not root hub */
386         else
387                 printf(" ");
388         printf("%d ", dev->devnum);
389         pre[index++] = ' ';
390         pre[index++] = has_child ? '|' : ' ';
391         pre[index] = 0;
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);
399 #ifdef CONFIG_DM_USB
400         struct udevice *child;
401
402         for (device_find_first_child(dev->dev, &child);
403              child;
404              device_find_next_child(&child)) {
405                 struct usb_device *udev;
406
407                 if (!device_active(child))
408                         continue;
409
410                 udev = dev_get_parentdata(child);
411
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);
415                         pre[index] = 0;
416                 }
417         }
418 #else
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);
423                                 pre[index] = 0;
424                         }
425                 }
426         }
427 #endif
428 }
429
430 /* main routine for the tree command */
431 static void usb_show_tree(struct usb_device *dev)
432 {
433         char preamble[32];
434
435         memset(preamble, '\0', sizeof(preamble));
436         usb_show_tree_graph(dev, &preamble[0]);
437 }
438
439 static int usb_test(struct usb_device *dev, int port, char* arg)
440 {
441         int mode;
442
443         if (port > dev->maxchild) {
444                 printf("Device is no hub or does not have %d ports.\n", port);
445                 return 1;
446         }
447
448         switch (arg[0]) {
449         case 'J':
450         case 'j':
451                 printf("Setting Test_J mode");
452                 mode = USB_TEST_MODE_J;
453                 break;
454         case 'K':
455         case 'k':
456                 printf("Setting Test_K mode");
457                 mode = USB_TEST_MODE_K;
458                 break;
459         case 'S':
460         case 's':
461                 printf("Setting Test_SE0_NAK mode");
462                 mode = USB_TEST_MODE_SE0_NAK;
463                 break;
464         case 'P':
465         case 'p':
466                 printf("Setting Test_Packet mode");
467                 mode = USB_TEST_MODE_PACKET;
468                 break;
469         case 'F':
470         case 'f':
471                 printf("Setting Test_Force_Enable mode");
472                 mode = USB_TEST_MODE_FORCE_ENABLE;
473                 break;
474         default:
475                 printf("Unrecognized test mode: %s\nAvailable modes: "
476                        "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
477                 return 1;
478         }
479
480         if (port)
481                 printf(" on downstream facing port %d...\n", port);
482         else
483                 printf(" on upstream facing port...\n");
484
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,
488                             (mode << 8) | port,
489                             NULL, 0, USB_CNTL_TIMEOUT) == -1) {
490                 printf("Error during SET_FEATURE.\n");
491                 return 1;
492         } else {
493                 printf("Test mode successfully set. Use 'usb start' "
494                        "to return to normal operation.\n");
495                 return 0;
496         }
497 }
498
499
500 /******************************************************************************
501  * usb boot command intepreter. Derived from diskboot
502  */
503 #ifdef CONFIG_USB_STORAGE
504 static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
505 {
506         return common_diskboot(cmdtp, "usb", argc, argv);
507 }
508 #endif /* CONFIG_USB_STORAGE */
509
510 static int do_usb_stop_keyboard(int force)
511 {
512 #ifdef CONFIG_USB_KEYBOARD
513         if (usb_kbd_deregister(force) != 0) {
514                 printf("USB not stopped: usbkbd still using USB\n");
515                 return 1;
516         }
517 #endif
518         return 0;
519 }
520
521 static void do_usb_start(void)
522 {
523         bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
524
525         if (usb_init() < 0)
526                 return;
527
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);
533 #endif
534 #endif
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"
539 #  endif
540 # else
541         /* try to recognize ethernet devices immediately */
542         usb_ether_curr_dev = usb_host_eth_scan(1);
543 # endif
544 #endif
545 #ifdef CONFIG_USB_KEYBOARD
546         drv_usb_kbd_init();
547 #endif
548 }
549
550 #ifdef CONFIG_DM_USB
551 static void show_info(struct udevice *dev)
552 {
553         struct udevice *child;
554         struct usb_device *udev;
555
556         udev = dev_get_parentdata(dev);
557         usb_display_desc(udev);
558         usb_display_config(udev);
559         for (device_find_first_child(dev, &child);
560              child;
561              device_find_next_child(&child)) {
562                 if (device_active(child))
563                         show_info(child);
564         }
565 }
566
567 static int usb_device_info(void)
568 {
569         struct udevice *bus;
570
571         for (uclass_first_device(UCLASS_USB, &bus);
572              bus;
573              uclass_next_device(&bus)) {
574                 struct udevice *hub;
575
576                 device_find_first_child(bus, &hub);
577                 if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
578                     device_active(hub)) {
579                         show_info(hub);
580                 }
581         }
582
583         return 0;
584 }
585 #endif
586
587 /******************************************************************************
588  * usb command intepreter
589  */
590 static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
591 {
592         struct usb_device *udev = NULL;
593         int i;
594         extern char usb_started;
595 #ifdef CONFIG_USB_STORAGE
596         block_dev_desc_t *stor_dev;
597 #endif
598
599         if (argc < 2)
600                 return CMD_RET_USAGE;
601
602         if (strncmp(argv[1], "start", 5) == 0) {
603                 if (usb_started)
604                         return 0; /* Already started */
605                 printf("starting USB...\n");
606                 do_usb_start();
607                 return 0;
608         }
609
610         if (strncmp(argv[1], "reset", 5) == 0) {
611                 printf("resetting USB...\n");
612                 if (do_usb_stop_keyboard(1) != 0)
613                         return 1;
614                 usb_stop();
615                 do_usb_start();
616                 return 0;
617         }
618         if (strncmp(argv[1], "stop", 4) == 0) {
619                 if (argc != 2)
620                         console_assign(stdin, "serial");
621                 if (do_usb_stop_keyboard(0) != 0)
622                         return 1;
623                 printf("stopping USB..\n");
624                 usb_stop();
625                 return 0;
626         }
627         if (!usb_started) {
628                 printf("USB is stopped. Please issue 'usb start' first.\n");
629                 return 1;
630         }
631         if (strncmp(argv[1], "tree", 4) == 0) {
632                 puts("USB device tree:\n");
633 #ifdef CONFIG_DM_USB
634                 struct udevice *bus;
635
636                 for (uclass_first_device(UCLASS_USB, &bus);
637                      bus;
638                      uclass_next_device(&bus)) {
639                         struct usb_device *udev;
640                         struct udevice *dev;
641
642                         device_find_first_child(bus, &dev);
643                         if (dev && device_active(dev)) {
644                                 udev = dev_get_parentdata(dev);
645                                 usb_show_tree(udev);
646                         }
647                 }
648 #else
649                 for (i = 0; i < USB_MAX_DEVICE; i++) {
650                         udev = usb_get_dev_index(i);
651                         if (udev == NULL)
652                                 break;
653                         if (udev->parent == NULL)
654                                 usb_show_tree(udev);
655                 }
656 #endif
657                 return 0;
658         }
659         if (strncmp(argv[1], "inf", 3) == 0) {
660                 if (argc == 2) {
661 #ifdef CONFIG_DM_USB
662                         usb_device_info();
663 #else
664                         int d;
665                         for (d = 0; d < USB_MAX_DEVICE; d++) {
666                                 udev = usb_get_dev_index(d);
667                                 if (udev == NULL)
668                                         break;
669                                 usb_display_desc(udev);
670                                 usb_display_config(udev);
671                         }
672 #endif
673                         return 0;
674                 } else {
675                         /*
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.
679                          */
680                         i = simple_strtoul(argv[2], NULL, 10);
681                         printf("config for device %d\n", i);
682                         udev = usb_find_device(i);
683                         if (udev == NULL) {
684                                 printf("*** No device available ***\n");
685                                 return 0;
686                         } else {
687                                 usb_display_desc(udev);
688                                 usb_display_config(udev);
689                         }
690                 }
691                 return 0;
692         }
693         if (strncmp(argv[1], "test", 4) == 0) {
694                 if (argc < 5)
695                         return CMD_RET_USAGE;
696                 i = simple_strtoul(argv[2], NULL, 10);
697                 udev = usb_find_device(i);
698                 if (udev == NULL) {
699                         printf("Device %d does not exist.\n", i);
700                         return 1;
701                 }
702                 i = simple_strtoul(argv[3], NULL, 10);
703                 return usb_test(udev, i, argv[4]);
704         }
705 #ifdef CONFIG_USB_STORAGE
706         if (strncmp(argv[1], "stor", 4) == 0)
707                 return usb_stor_info();
708
709         if (strncmp(argv[1], "part", 4) == 0) {
710                 int devno, ok = 0;
711                 if (argc == 2) {
712                         for (devno = 0; ; ++devno) {
713                                 stor_dev = usb_stor_get_dev(devno);
714                                 if (stor_dev == NULL)
715                                         break;
716                                 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
717                                         ok++;
718                                         if (devno)
719                                                 printf("\n");
720                                         debug("print_part of %x\n", devno);
721                                         print_part(stor_dev);
722                                 }
723                         }
724                 } else {
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) {
729                                 ok++;
730                                 debug("print_part of %x\n", devno);
731                                 print_part(stor_dev);
732                         }
733                 }
734                 if (!ok) {
735                         printf("\nno USB devices available\n");
736                         return 1;
737                 }
738                 return 0;
739         }
740         if (strcmp(argv[1], "read") == 0) {
741                 if (usb_stor_curr_dev < 0) {
742                         printf("no current device selected\n");
743                         return 1;
744                 }
745                 if (argc == 5) {
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);
749                         unsigned long n;
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,
754                                                  (ulong *)addr);
755                         printf("%ld blocks read: %s\n", n,
756                                 (n == cnt) ? "OK" : "ERROR");
757                         if (n == cnt)
758                                 return 0;
759                         return 1;
760                 }
761         }
762         if (strcmp(argv[1], "write") == 0) {
763                 if (usb_stor_curr_dev < 0) {
764                         printf("no current device selected\n");
765                         return 1;
766                 }
767                 if (argc == 5) {
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);
771                         unsigned long n;
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,
776                                                 (ulong *)addr);
777                         printf("%ld blocks write: %s\n", n,
778                                 (n == cnt) ? "OK" : "ERROR");
779                         if (n == cnt)
780                                 return 0;
781                         return 1;
782                 }
783         }
784         if (strncmp(argv[1], "dev", 3) == 0) {
785                 if (argc == 3) {
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");
791                                 return 1;
792                         }
793                         printf("\n    Device %d: ", dev);
794                         dev_print(stor_dev);
795                         if (stor_dev->type == DEV_TYPE_UNKNOWN)
796                                 return 1;
797                         usb_stor_curr_dev = dev;
798                         printf("... is now current device\n");
799                         return 0;
800                 } else {
801                         printf("\nUSB device %d: ", usb_stor_curr_dev);
802                         stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
803                         dev_print(stor_dev);
804                         if (stor_dev->type == DEV_TYPE_UNKNOWN)
805                                 return 1;
806                         return 0;
807                 }
808                 return 0;
809         }
810 #endif /* CONFIG_USB_STORAGE */
811         return CMD_RET_USAGE;
812 }
813
814 U_BOOT_CMD(
815         usb,    5,      1,      do_usb,
816         "USB sub-system",
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"
829         "    devices\n"
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 */
835 );
836
837
838 #ifdef CONFIG_USB_STORAGE
839 U_BOOT_CMD(
840         usbboot,        3,      1,      do_usbboot,
841         "boot from USB device",
842         "loadAddr dev:part"
843 );
844 #endif /* CONFIG_USB_STORAGE */