]> Git Repo - J-u-boot.git/blame - common/cmd_usb.c
spl: nand: remove code duplication
[J-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>
cf92e05c 17#include <memalign.h>
414eec35 18#include <asm/byteorder.h>
b2fb47f1 19#include <asm/unaligned.h>
735dd97b 20#include <part.h>
e887afc9 21#include <usb.h>
e887afc9 22
1968e615 23#ifdef CONFIG_USB_STORAGE
d0ff51ba 24static int usb_stor_curr_dev = -1; /* current device */
1968e615 25#endif
c8c2797c 26#if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
69559093 27static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
89d48367 28#endif
e887afc9 29
a2663ea4 30/* some display routines (info command) */
088f1b19 31static char *usb_get_class_desc(unsigned char dclass)
e887afc9 32{
de39f8c1
MT
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 "";
e887afc9
WD
54 }
55}
56
088f1b19
KP
57static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
58 unsigned char proto)
e887afc9 59{
de39f8c1
MT
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");
e887afc9 69 break;
de39f8c1
MT
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;
e887afc9
WD
85 }
86 break;
de39f8c1
MT
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");
e887afc9
WD
127 break;
128 default:
de39f8c1
MT
129 printf("reserved");
130 break;
131 }
132 break;
133 default:
134 printf("%s", usb_get_class_desc(dclass));
135 break;
e887afc9
WD
136 }
137}
138
088f1b19 139static void usb_display_string(struct usb_device *dev, int index)
e887afc9 140{
f5766139
PS
141 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
142
de39f8c1
MT
143 if (index != 0) {
144 if (usb_string(dev, index, &buffer[0], 256) > 0)
145 printf("String: \"%s\"", buffer);
e887afc9
WD
146 }
147}
148
088f1b19 149static void usb_display_desc(struct usb_device *dev)
e887afc9 150{
de39f8c1
MT
151 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
152 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
8f8bd565 153 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
de39f8c1
MT
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);
e887afc9
WD
161 if (dev->descriptor.bDeviceClass) {
162 printf(" - Class: ");
de39f8c1
MT
163 usb_display_class_sub(dev->descriptor.bDeviceClass,
164 dev->descriptor.bDeviceSubClass,
165 dev->descriptor.bDeviceProtocol);
e887afc9 166 printf("\n");
de39f8c1
MT
167 } else {
168 printf(" - Class: (from Interface) %s\n",
169 usb_get_class_desc(
8f8bd565 170 dev->config.if_desc[0].desc.bInterfaceClass));
e887afc9 171 }
de39f8c1
MT
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);
e887afc9
WD
179 }
180
181}
182
c60795f4 183static void usb_display_conf_desc(struct usb_config_descriptor *config,
088f1b19 184 struct usb_device *dev)
e887afc9 185{
de39f8c1
MT
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 " : "",
8f8bd565 190 config->bMaxPower*2);
e887afc9
WD
191 if (config->iConfiguration) {
192 printf(" - ");
de39f8c1 193 usb_display_string(dev, config->iConfiguration);
e887afc9
WD
194 printf("\n");
195 }
196}
197
088f1b19
KP
198static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
199 struct usb_device *dev)
e887afc9 200{
de39f8c1
MT
201 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
202 printf(" - Alternate Setting %d, Endpoints: %d\n",
203 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
e887afc9 204 printf(" - Class ");
de39f8c1
MT
205 usb_display_class_sub(ifdesc->bInterfaceClass,
206 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
e887afc9
WD
207 printf("\n");
208 if (ifdesc->iInterface) {
209 printf(" - ");
de39f8c1 210 usb_display_string(dev, ifdesc->iInterface);
e887afc9
WD
211 printf("\n");
212 }
213}
214
088f1b19 215static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
e887afc9 216{
de39f8c1
MT
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;
e887afc9 232 }
b2fb47f1 233 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
de39f8c1
MT
234 if ((epdesc->bmAttributes & 0x03) == 0x3)
235 printf(" Interval %dms", epdesc->bInterval);
e887afc9
WD
236 printf("\n");
237}
238
239/* main routine to diasplay the configs, interfaces and endpoints */
088f1b19 240static void usb_display_config(struct usb_device *dev)
e887afc9 241{
8f8bd565
TR
242 struct usb_config *config;
243 struct usb_interface *ifdesc;
e887afc9 244 struct usb_endpoint_descriptor *epdesc;
de39f8c1
MT
245 int i, ii;
246
247 config = &dev->config;
8f8bd565 248 usb_display_conf_desc(&config->desc, dev);
de39f8c1
MT
249 for (i = 0; i < config->no_of_if; i++) {
250 ifdesc = &config->if_desc[i];
8f8bd565 251 usb_display_if_desc(&ifdesc->desc, dev);
de39f8c1
MT
252 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
253 epdesc = &ifdesc->ep_desc[ii];
e887afc9
WD
254 usb_display_ep_desc(epdesc);
255 }
256 }
257 printf("\n");
258}
259
6a1b206d
SG
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([email protected]): Add a way to specify the controller/bus.
264 */
7d9aa8fd
JW
265static struct usb_device *usb_find_device(int devnum)
266{
6a1b206d
SG
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;
bcbe3d15 284 udev = dev_get_parent_priv(hub);
6a1b206d
SG
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
bcbe3d15 294 udev = dev_get_parent_priv(dev);
6a1b206d
SG
295 if (udev->devnum == devnum)
296 return udev;
297 }
298 }
299#else
cad4291c 300 struct usb_device *udev;
7d9aa8fd
JW
301 int d;
302
303 for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291c
SG
304 udev = usb_get_dev_index(d);
305 if (udev == NULL)
7d9aa8fd 306 return NULL;
cad4291c
SG
307 if (udev->devnum == devnum)
308 return udev;
7d9aa8fd 309 }
6a1b206d 310#endif
7d9aa8fd
JW
311
312 return NULL;
313}
314
f1c1f540
SR
315static inline char *portspeed(int speed)
316{
6497c667
VG
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;
f1c1f540
SR
335}
336
e887afc9 337/* shows the device tree recursively */
088f1b19 338static void usb_show_tree_graph(struct usb_device *dev, char *pre)
e887afc9 339{
6a1b206d 340 int index;
10f4dd78 341 int has_child, last_child;
e887afc9 342
de39f8c1
MT
343 index = strlen(pre);
344 printf(" %s", pre);
6a1b206d
SG
345#ifdef CONFIG_DM_USB
346 has_child = device_has_active_children(dev->dev);
347#else
e887afc9 348 /* check if the device has connected children */
6a1b206d
SG
349 int i;
350
de39f8c1
MT
351 has_child = 0;
352 for (i = 0; i < dev->maxchild; i++) {
353 if (dev->children[i] != NULL)
354 has_child = 1;
e887afc9 355 }
6a1b206d 356#endif
e887afc9 357 /* check if we are the last one */
6a1b206d 358#ifdef CONFIG_DM_USB
c27b3290
HG
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);
6a1b206d 362#else
c27b3290
HG
363 if (dev->parent != NULL) { /* not root? */
364 last_child = 1;
de39f8c1 365 for (i = 0; i < dev->parent->maxchild; i++) {
e887afc9 366 /* search for children */
de39f8c1
MT
367 if (dev->parent->children[i] == dev) {
368 /* found our pointer, see if we have a
369 * little sister
370 */
de39f8c1
MT
371 while (i++ < dev->parent->maxchild) {
372 if (dev->parent->children[i] != NULL) {
e887afc9 373 /* found a sister */
de39f8c1 374 last_child = 0;
e887afc9
WD
375 break;
376 } /* if */
377 } /* while */
378 } /* device found */
379 } /* for all children of the parent */
6a1b206d 380#endif
e887afc9
WD
381 printf("\b+-");
382 /* correct last child */
cad4291c 383 if (last_child && index)
de39f8c1 384 pre[index-1] = ' ';
e887afc9
WD
385 } /* if not root hub */
386 else
387 printf(" ");
de39f8c1
MT
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(
8f8bd565 393 dev->config.if_desc[0].desc.bInterfaceClass),
f1c1f540 394 portspeed(dev->speed),
8f8bd565 395 dev->config.desc.bMaxPower * 2);
de39f8c1
MT
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);
6a1b206d
SG
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
bcbe3d15 410 udev = dev_get_parent_priv(child);
6a1b206d
SG
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
de39f8c1
MT
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;
e887afc9
WD
424 }
425 }
426 }
6a1b206d 427#endif
e887afc9
WD
428}
429
430/* main routine for the tree command */
088f1b19 431static void usb_show_tree(struct usb_device *dev)
e887afc9
WD
432{
433 char preamble[32];
434
cad4291c 435 memset(preamble, '\0', sizeof(preamble));
de39f8c1 436 usb_show_tree_graph(dev, &preamble[0]);
e887afc9
WD
437}
438
7d9aa8fd
JW
439static 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
e887afc9 499
e887afc9
WD
500/******************************************************************************
501 * usb boot command intepreter. Derived from diskboot
502 */
503#ifdef CONFIG_USB_STORAGE
088f1b19 504static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9 505{
7405a133 506 return common_diskboot(cmdtp, "usb", argc, argv);
e887afc9
WD
507}
508#endif /* CONFIG_USB_STORAGE */
509
8a8a2257 510static int do_usb_stop_keyboard(int force)
6e78c74f
HG
511{
512#ifdef CONFIG_USB_KEYBOARD
8a8a2257 513 if (usb_kbd_deregister(force) != 0) {
6e78c74f
HG
514 printf("USB not stopped: usbkbd still using USB\n");
515 return 1;
516 }
517#endif
518 return 0;
519}
e887afc9 520
b5072264
HG
521static 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
6a1b206d
SG
528 /* Driver model will probe the devices as they are found */
529#ifndef CONFIG_DM_USB
b5072264
HG
530#ifdef CONFIG_USB_STORAGE
531 /* try to recognize storage devices immediately */
532 usb_stor_curr_dev = usb_stor_scan(1);
533#endif
389f1856 534#endif
b5072264 535#ifdef CONFIG_USB_HOST_ETHER
69559093 536# ifdef CONFIG_DM_ETH
389f1856
MZ
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
b5072264
HG
541 /* try to recognize ethernet devices immediately */
542 usb_ether_curr_dev = usb_host_eth_scan(1);
389f1856 543# endif
69559093 544#endif
b5072264
HG
545#ifdef CONFIG_USB_KEYBOARD
546 drv_usb_kbd_init();
547#endif
548}
549
6a1b206d
SG
550#ifdef CONFIG_DM_USB
551static void show_info(struct udevice *dev)
552{
553 struct udevice *child;
554 struct usb_device *udev;
555
bcbe3d15 556 udev = dev_get_parent_priv(dev);
6a1b206d
SG
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
567static 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
de39f8c1 587/******************************************************************************
e887afc9
WD
588 * usb command intepreter
589 */
088f1b19 590static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9 591{
cad4291c 592 struct usb_device *udev = NULL;
e887afc9 593 int i;
e51aae38 594 extern char usb_started;
1968e615 595#ifdef CONFIG_USB_STORAGE
e887afc9 596 block_dev_desc_t *stor_dev;
1968e615 597#endif
e887afc9 598
47e26b1b 599 if (argc < 2)
4c12eeb8 600 return CMD_RET_USAGE;
65d34254 601
b5072264
HG
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");
8a8a2257 612 if (do_usb_stop_keyboard(1) != 0)
6e78c74f 613 return 1;
e887afc9 614 usb_stop();
b5072264 615 do_usb_start();
e887afc9
WD
616 return 0;
617 }
de39f8c1 618 if (strncmp(argv[1], "stop", 4) == 0) {
6e78c74f 619 if (argc != 2)
de39f8c1 620 console_assign(stdin, "serial");
8a8a2257 621 if (do_usb_stop_keyboard(0) != 0)
6e78c74f 622 return 1;
e887afc9
WD
623 printf("stopping USB..\n");
624 usb_stop();
625 return 0;
626 }
e51aae38
BS
627 if (!usb_started) {
628 printf("USB is stopped. Please issue 'usb start' first.\n");
629 return 1;
630 }
de39f8c1 631 if (strncmp(argv[1], "tree", 4) == 0) {
93c2582f 632 puts("USB device tree:\n");
6a1b206d
SG
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;
fd1bd21b 640 struct udevice *dev;
6a1b206d 641
fd1bd21b
HG
642 device_find_first_child(bus, &dev);
643 if (dev && device_active(dev)) {
bcbe3d15 644 udev = dev_get_parent_priv(dev);
6a1b206d
SG
645 usb_show_tree(udev);
646 }
647 }
648#else
93c2582f 649 for (i = 0; i < USB_MAX_DEVICE; i++) {
cad4291c
SG
650 udev = usb_get_dev_index(i);
651 if (udev == NULL)
93c2582f 652 break;
cad4291c
SG
653 if (udev->parent == NULL)
654 usb_show_tree(udev);
93c2582f 655 }
6a1b206d 656#endif
e887afc9
WD
657 return 0;
658 }
de39f8c1 659 if (strncmp(argv[1], "inf", 3) == 0) {
de39f8c1 660 if (argc == 2) {
6a1b206d
SG
661#ifdef CONFIG_DM_USB
662 usb_device_info();
663#else
664 int d;
de39f8c1 665 for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291c
SG
666 udev = usb_get_dev_index(d);
667 if (udev == NULL)
e887afc9 668 break;
cad4291c
SG
669 usb_display_desc(udev);
670 usb_display_config(udev);
e887afc9 671 }
6a1b206d 672#endif
e887afc9 673 return 0;
de39f8c1 674 } else {
6a1b206d
SG
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 */
7d9aa8fd 680 i = simple_strtoul(argv[2], NULL, 10);
de39f8c1 681 printf("config for device %d\n", i);
cad4291c
SG
682 udev = usb_find_device(i);
683 if (udev == NULL) {
6052cbab 684 printf("*** No device available ***\n");
e887afc9 685 return 0;
de39f8c1 686 } else {
cad4291c
SG
687 usb_display_desc(udev);
688 usb_display_config(udev);
e887afc9
WD
689 }
690 }
691 return 0;
692 }
7d9aa8fd
JW
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);
cad4291c
SG
697 udev = usb_find_device(i);
698 if (udev == NULL) {
7d9aa8fd
JW
699 printf("Device %d does not exist.\n", i);
700 return 1;
701 }
702 i = simple_strtoul(argv[3], NULL, 10);
cad4291c 703 return usb_test(udev, i, argv[4]);
7d9aa8fd 704 }
e887afc9 705#ifdef CONFIG_USB_STORAGE
de39f8c1 706 if (strncmp(argv[1], "stor", 4) == 0)
f6b44e0e 707 return usb_stor_info();
9c998aa8 708
de39f8c1 709 if (strncmp(argv[1], "part", 4) == 0) {
d4b5f3fa 710 int devno, ok = 0;
de39f8c1 711 if (argc == 2) {
aaad108b 712 for (devno = 0; ; ++devno) {
de39f8c1 713 stor_dev = usb_stor_get_dev(devno);
aaad108b
KH
714 if (stor_dev == NULL)
715 break;
de39f8c1 716 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
d4b5f3fa
CE
717 ok++;
718 if (devno)
719 printf("\n");
060f2853 720 debug("print_part of %x\n", devno);
d4b5f3fa
CE
721 print_part(stor_dev);
722 }
723 }
de39f8c1
MT
724 } else {
725 devno = simple_strtoul(argv[2], NULL, 16);
726 stor_dev = usb_stor_get_dev(devno);
aaad108b
KH
727 if (stor_dev != NULL &&
728 stor_dev->type != DEV_TYPE_UNKNOWN) {
e887afc9 729 ok++;
060f2853 730 debug("print_part of %x\n", devno);
e887afc9
WD
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 }
de39f8c1
MT
740 if (strcmp(argv[1], "read") == 0) {
741 if (usb_stor_curr_dev < 0) {
e887afc9
WD
742 printf("no current device selected\n");
743 return 1;
744 }
de39f8c1 745 if (argc == 5) {
e887afc9
WD
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;
de39f8c1
MT
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)
e887afc9
WD
758 return 0;
759 return 1;
760 }
761 }
127e1084
MJ
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 }
9c998aa8
WD
784 if (strncmp(argv[1], "dev", 3) == 0) {
785 if (argc == 3) {
e887afc9 786 int dev = (int)simple_strtoul(argv[2], NULL, 10);
de39f8c1 787 printf("\nUSB device %d: ", dev);
aaad108b
KH
788 stor_dev = usb_stor_get_dev(dev);
789 if (stor_dev == NULL) {
e887afc9
WD
790 printf("unknown device\n");
791 return 1;
792 }
de39f8c1 793 printf("\n Device %d: ", dev);
e887afc9 794 dev_print(stor_dev);
de39f8c1 795 if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9 796 return 1;
e887afc9
WD
797 usb_stor_curr_dev = dev;
798 printf("... is now current device\n");
799 return 0;
de39f8c1
MT
800 } else {
801 printf("\nUSB device %d: ", usb_stor_curr_dev);
802 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
e887afc9 803 dev_print(stor_dev);
de39f8c1 804 if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9 805 return 1;
e887afc9
WD
806 return 0;
807 }
808 return 0;
809 }
810#endif /* CONFIG_USB_STORAGE */
4c12eeb8 811 return CMD_RET_USAGE;
e887afc9
WD
812}
813
0d498393
WD
814U_BOOT_CMD(
815 usb, 5, 1, do_usb,
2fb2604d 816 "USB sub-system",
1af9f963
VPP
817 "start - start (scan) USB controller\n"
818 "usb reset - reset (rescan) USB controller\n"
b3813a22
VPP
819 "usb stop [f] - stop USB [f]=force stop\n"
820 "usb tree - show USB device tree\n"
5cf91d6b 821 "usb info [dev] - show available USB devices\n"
7d9aa8fd
JW
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
b3813a22 826 "usb storage - show details of USB storage devices\n"
342717f7 827 "usb dev [dev] - show or set current USB storage device\n"
de39f8c1 828 "usb part [dev] - print partition table of one or all USB storage"
7d9aa8fd 829 " devices\n"
5cf91d6b 830 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
842404ea 831 " to memory address `addr'\n"
127e1084
MJ
832 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
833 " from memory address `addr'"
7d9aa8fd 834#endif /* CONFIG_USB_STORAGE */
8bde7f77
WD
835);
836
837
7d9aa8fd 838#ifdef CONFIG_USB_STORAGE
0d498393
WD
839U_BOOT_CMD(
840 usbboot, 3, 1, do_usbboot,
2fb2604d 841 "boot from USB device",
a89c33db 842 "loadAddr dev:part"
8bde7f77 843);
7d9aa8fd 844#endif /* CONFIG_USB_STORAGE */
This page took 0.503682 seconds and 4 git commands to generate.