3 * Denis Peter, MPL AG Switzerland
5 * Most of this source has been derived from the Linux USB
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <asm/byteorder.h>
34 #ifdef CONFIG_USB_STORAGE
35 static int usb_stor_curr_dev = -1; /* current device */
37 #ifdef CONFIG_USB_HOST_ETHER
38 static int usb_ether_curr_dev = -1; /* current ethernet device */
41 /* some display routines (info command) */
42 char *usb_get_class_desc(unsigned char dclass)
45 case USB_CLASS_PER_INTERFACE:
46 return "See Interface";
50 return "Communication";
52 return "Human Interface";
53 case USB_CLASS_PRINTER:
55 case USB_CLASS_MASS_STORAGE:
56 return "Mass Storage";
61 case USB_CLASS_VENDOR_SPEC:
62 return "Vendor specific";
68 void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
72 case USB_CLASS_PER_INTERFACE:
73 printf("See Interface");
76 printf("Human Interface, Subclass: ");
78 case USB_SUB_HID_NONE:
81 case USB_SUB_HID_BOOT:
84 case USB_PROT_HID_NONE:
87 case USB_PROT_HID_KEYBOARD:
90 case USB_PROT_HID_MOUSE:
103 case USB_CLASS_MASS_STORAGE:
104 printf("Mass Storage, ");
110 printf("SFF-8020i (ATAPI)");
113 printf("QIC-157 (Tape)");
122 printf("Transp. SCSI");
131 printf("Command/Bulk");
134 printf("Command/Bulk/Int");
145 printf("%s", usb_get_class_desc(dclass));
150 void usb_display_string(struct usb_device *dev, int index)
154 if (usb_string(dev, index, &buffer[0], 256) > 0)
155 printf("String: \"%s\"", buffer);
159 void usb_display_desc(struct usb_device *dev)
161 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
162 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
163 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
164 (dev->descriptor.bcdUSB>>8) & 0xff,
165 dev->descriptor.bcdUSB & 0xff);
167 if (strlen(dev->mf) || strlen(dev->prod) ||
169 printf(" - %s %s %s\n", dev->mf, dev->prod,
171 if (dev->descriptor.bDeviceClass) {
172 printf(" - Class: ");
173 usb_display_class_sub(dev->descriptor.bDeviceClass,
174 dev->descriptor.bDeviceSubClass,
175 dev->descriptor.bDeviceProtocol);
178 printf(" - Class: (from Interface) %s\n",
180 dev->config.if_desc[0].desc.bInterfaceClass));
182 printf(" - PacketSize: %d Configurations: %d\n",
183 dev->descriptor.bMaxPacketSize0,
184 dev->descriptor.bNumConfigurations);
185 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
186 dev->descriptor.idVendor, dev->descriptor.idProduct,
187 (dev->descriptor.bcdDevice>>8) & 0xff,
188 dev->descriptor.bcdDevice & 0xff);
193 void usb_display_conf_desc(struct usb_configuration_descriptor *config,
194 struct usb_device *dev)
196 printf(" Configuration: %d\n", config->bConfigurationValue);
197 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
198 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
199 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
200 config->bMaxPower*2);
201 if (config->iConfiguration) {
203 usb_display_string(dev, config->iConfiguration);
208 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
209 struct usb_device *dev)
211 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
212 printf(" - Alternate Setting %d, Endpoints: %d\n",
213 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
215 usb_display_class_sub(ifdesc->bInterfaceClass,
216 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
218 if (ifdesc->iInterface) {
220 usb_display_string(dev, ifdesc->iInterface);
225 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
227 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
228 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
229 switch ((epdesc->bmAttributes & 0x03)) {
234 printf("Isochronous");
243 printf(" MaxPacket %d", epdesc->wMaxPacketSize);
244 if ((epdesc->bmAttributes & 0x03) == 0x3)
245 printf(" Interval %dms", epdesc->bInterval);
249 /* main routine to diasplay the configs, interfaces and endpoints */
250 void usb_display_config(struct usb_device *dev)
252 struct usb_config *config;
253 struct usb_interface *ifdesc;
254 struct usb_endpoint_descriptor *epdesc;
257 config = &dev->config;
258 usb_display_conf_desc(&config->desc, dev);
259 for (i = 0; i < config->no_of_if; i++) {
260 ifdesc = &config->if_desc[i];
261 usb_display_if_desc(&ifdesc->desc, dev);
262 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
263 epdesc = &ifdesc->ep_desc[ii];
264 usb_display_ep_desc(epdesc);
270 static inline char *portspeed(int speed)
272 if (speed == USB_SPEED_HIGH)
274 else if (speed == USB_SPEED_LOW)
280 /* shows the device tree recursively */
281 void usb_show_tree_graph(struct usb_device *dev, char *pre)
284 int has_child, last_child;
288 /* check if the device has connected children */
290 for (i = 0; i < dev->maxchild; i++) {
291 if (dev->children[i] != NULL)
294 /* check if we are the last one */
296 if (dev->parent != NULL) {
297 for (i = 0; i < dev->parent->maxchild; i++) {
298 /* search for children */
299 if (dev->parent->children[i] == dev) {
300 /* found our pointer, see if we have a
303 while (i++ < dev->parent->maxchild) {
304 if (dev->parent->children[i] != NULL) {
311 } /* for all children of the parent */
313 /* correct last child */
316 } /* if not root hub */
319 printf("%d ", dev->devnum);
321 pre[index++] = has_child ? '|' : ' ';
323 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
324 dev->config.if_desc[0].desc.bInterfaceClass),
325 portspeed(dev->speed),
326 dev->config.desc.bMaxPower * 2);
327 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
328 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
329 printf(" %s\n", pre);
330 if (dev->maxchild > 0) {
331 for (i = 0; i < dev->maxchild; i++) {
332 if (dev->children[i] != NULL) {
333 usb_show_tree_graph(dev->children[i], pre);
340 /* main routine for the tree command */
341 void usb_show_tree(struct usb_device *dev)
345 memset(preamble, 0, 32);
346 usb_show_tree_graph(dev, &preamble[0]);
350 /******************************************************************************
351 * usb boot command intepreter. Derived from diskboot
353 #ifdef CONFIG_USB_STORAGE
354 int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
356 char *boot_device = NULL;
360 disk_partition_t info;
362 block_dev_desc_t *stor_dev;
363 #if defined(CONFIG_FIT)
364 const void *fit_hdr = NULL;
369 addr = CONFIG_SYS_LOAD_ADDR;
370 boot_device = getenv("bootdevice");
373 addr = simple_strtoul(argv[1], NULL, 16);
374 boot_device = getenv("bootdevice");
377 addr = simple_strtoul(argv[1], NULL, 16);
378 boot_device = argv[2];
381 return cmd_usage(cmdtp);
385 puts("\n** No boot device **\n");
389 dev = simple_strtoul(boot_device, &ep, 16);
390 stor_dev = usb_stor_get_dev(dev);
391 if (stor_dev == NULL || stor_dev->type == DEV_TYPE_UNKNOWN) {
392 printf("\n** Device %d not available\n", dev);
395 if (stor_dev->block_read == NULL) {
396 printf("storage device not initialized. Use usb scan\n");
401 puts("\n** Invalid boot device, use `dev[:part]' **\n");
404 part = simple_strtoul(++ep, NULL, 16);
407 if (get_partition_info(stor_dev, part, &info)) {
408 /* try to boot raw .... */
409 strncpy((char *)&info.type[0], BOOT_PART_TYPE,
410 sizeof(BOOT_PART_TYPE));
411 strncpy((char *)&info.name[0], "Raw", 4);
415 printf("error reading partinfo...try to boot raw\n");
417 if ((strncmp((char *)info.type, BOOT_PART_TYPE,
418 sizeof(info.type)) != 0) &&
419 (strncmp((char *)info.type, BOOT_PART_COMP,
420 sizeof(info.type)) != 0)) {
421 printf("\n** Invalid partition type \"%.32s\""
422 " (expect \"" BOOT_PART_TYPE "\")\n",
426 printf("\nLoading from USB device %d, partition %d: "
427 "Name: %.32s Type: %.32s\n",
428 dev, part, info.name, info.type);
430 debug("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
431 info.start, info.size, info.blksz);
433 if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
434 printf("** Read error on %d:%d\n", dev, part);
438 switch (genimg_get_format((void *)addr)) {
439 case IMAGE_FORMAT_LEGACY:
440 hdr = (image_header_t *)addr;
442 if (!image_check_hcrc(hdr)) {
443 puts("\n** Bad Header Checksum **\n");
447 image_print_contents(hdr);
449 cnt = image_get_image_size(hdr);
451 #if defined(CONFIG_FIT)
452 case IMAGE_FORMAT_FIT:
453 fit_hdr = (const void *)addr;
454 puts("Fit image detected...\n");
456 cnt = fit_get_size(fit_hdr);
460 puts("** Unknown image type\n");
464 cnt += info.blksz - 1;
468 if (stor_dev->block_read(dev, info.start+1, cnt,
469 (ulong *)(addr+info.blksz)) != cnt) {
470 printf("\n** Read error on %d:%d\n", dev, part);
474 #if defined(CONFIG_FIT)
475 /* This cannot be done earlier, we need complete FIT image in RAM
478 if (genimg_get_format((void *)addr) == IMAGE_FORMAT_FIT) {
479 if (!fit_check_format(fit_hdr)) {
480 puts("** Bad FIT image format\n");
483 fit_print_contents(fit_hdr);
487 /* Loading ok, update default load address */
490 flush_cache(addr, (cnt+1)*info.blksz);
492 return bootm_maybe_autostart(cmdtp, argv[0]);
494 #endif /* CONFIG_USB_STORAGE */
497 /******************************************************************************
498 * usb command intepreter
500 int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
504 struct usb_device *dev = NULL;
505 extern char usb_started;
506 #ifdef CONFIG_USB_STORAGE
507 block_dev_desc_t *stor_dev;
511 return cmd_usage(cmdtp);
513 if ((strncmp(argv[1], "reset", 5) == 0) ||
514 (strncmp(argv[1], "start", 5) == 0)) {
516 printf("(Re)start USB...\n");
519 #ifdef CONFIG_USB_STORAGE
520 /* try to recognize storage devices immediately */
521 usb_stor_curr_dev = usb_stor_scan(1);
523 #ifdef CONFIG_USB_HOST_ETHER
524 /* try to recognize ethernet devices immediately */
525 usb_ether_curr_dev = usb_host_eth_scan(1);
530 if (strncmp(argv[1], "stop", 4) == 0) {
531 #ifdef CONFIG_USB_KEYBOARD
533 if (usb_kbd_deregister() != 0) {
534 printf("USB not stopped: usbkbd still"
539 /* forced stop, switch console in to serial */
540 console_assign(stdin, "serial");
541 usb_kbd_deregister();
544 printf("stopping USB..\n");
549 printf("USB is stopped. Please issue 'usb start' first.\n");
552 if (strncmp(argv[1], "tree", 4) == 0) {
553 printf("\nDevice Tree:\n");
554 usb_show_tree(usb_get_dev_index(0));
557 if (strncmp(argv[1], "inf", 3) == 0) {
560 for (d = 0; d < USB_MAX_DEVICE; d++) {
561 dev = usb_get_dev_index(d);
564 usb_display_desc(dev);
565 usb_display_config(dev);
571 i = simple_strtoul(argv[2], NULL, 16);
572 printf("config for device %d\n", i);
573 for (d = 0; d < USB_MAX_DEVICE; d++) {
574 dev = usb_get_dev_index(d);
577 if (dev->devnum == i)
581 printf("*** No device available ***\n");
584 usb_display_desc(dev);
585 usb_display_config(dev);
590 #ifdef CONFIG_USB_STORAGE
591 if (strncmp(argv[1], "stor", 4) == 0)
592 return usb_stor_info();
594 if (strncmp(argv[1], "part", 4) == 0) {
597 for (devno = 0; ; ++devno) {
598 stor_dev = usb_stor_get_dev(devno);
599 if (stor_dev == NULL)
601 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
605 debug("print_part of %x\n", devno);
606 print_part(stor_dev);
610 devno = simple_strtoul(argv[2], NULL, 16);
611 stor_dev = usb_stor_get_dev(devno);
612 if (stor_dev != NULL &&
613 stor_dev->type != DEV_TYPE_UNKNOWN) {
615 debug("print_part of %x\n", devno);
616 print_part(stor_dev);
620 printf("\nno USB devices available\n");
625 if (strcmp(argv[1], "read") == 0) {
626 if (usb_stor_curr_dev < 0) {
627 printf("no current device selected\n");
631 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
632 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
633 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
635 printf("\nUSB read: device %d block # %ld, count %ld"
636 " ... ", usb_stor_curr_dev, blk, cnt);
637 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
638 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
640 printf("%ld blocks read: %s\n", n,
641 (n == cnt) ? "OK" : "ERROR");
647 if (strcmp(argv[1], "write") == 0) {
648 if (usb_stor_curr_dev < 0) {
649 printf("no current device selected\n");
653 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
654 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
655 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
657 printf("\nUSB write: device %d block # %ld, count %ld"
658 " ... ", usb_stor_curr_dev, blk, cnt);
659 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
660 n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
662 printf("%ld blocks write: %s\n", n,
663 (n == cnt) ? "OK" : "ERROR");
669 if (strncmp(argv[1], "dev", 3) == 0) {
671 int dev = (int)simple_strtoul(argv[2], NULL, 10);
672 printf("\nUSB device %d: ", dev);
673 stor_dev = usb_stor_get_dev(dev);
674 if (stor_dev == NULL) {
675 printf("unknown device\n");
678 printf("\n Device %d: ", dev);
680 if (stor_dev->type == DEV_TYPE_UNKNOWN)
682 usb_stor_curr_dev = dev;
683 printf("... is now current device\n");
686 printf("\nUSB device %d: ", usb_stor_curr_dev);
687 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
689 if (stor_dev->type == DEV_TYPE_UNKNOWN)
695 #endif /* CONFIG_USB_STORAGE */
696 return cmd_usage(cmdtp);
699 #ifdef CONFIG_USB_STORAGE
703 "start - start (scan) USB controller\n"
704 "usb reset - reset (rescan) USB controller\n"
705 "usb stop [f] - stop USB [f]=force stop\n"
706 "usb tree - show USB device tree\n"
707 "usb info [dev] - show available USB devices\n"
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"
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'"
720 usbboot, 3, 1, do_usbboot,
721 "boot from USB device",
729 "start - start (scan) USB controller\n"
730 "usb reset - reset (rescan) USB controller\n"
731 "usb tree - show USB device tree\n"
732 "usb info [dev] - show available USB devices"