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 */
38 /* some display routines (info command) */
39 char * usb_get_class_desc(unsigned char dclass)
42 case USB_CLASS_PER_INTERFACE:
43 return("See Interface");
47 return("Communication");
49 return("Human Interface");
50 case USB_CLASS_PRINTER:
52 case USB_CLASS_MASS_STORAGE:
53 return("Mass Storage");
58 case USB_CLASS_VENDOR_SPEC:
59 return("Vendor specific");
65 void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
68 case USB_CLASS_PER_INTERFACE:
69 printf("See Interface");
72 printf("Human Interface, Subclass: ");
74 case USB_SUB_HID_NONE:
77 case USB_SUB_HID_BOOT:
80 case USB_PROT_HID_NONE:
83 case USB_PROT_HID_KEYBOARD:
86 case USB_PROT_HID_MOUSE:
97 case USB_CLASS_MASS_STORAGE:
98 printf("Mass Storage, ");
104 printf("SFF-8020i (ATAPI)");
107 printf("QIC-157 (Tape)");
116 printf("Transp. SCSI");
125 printf("Command/Bulk");
128 printf("Command/Bulk/Int");
138 printf("%s",usb_get_class_desc(dclass));
142 void usb_display_string(struct usb_device *dev,int index)
146 if (usb_string(dev,index,&buffer[0],256)>0);
147 printf("String: \"%s\"",buffer);
151 void usb_display_desc(struct usb_device *dev)
153 if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
154 printf("%d: %s, USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
155 (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
156 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
157 printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
158 if (dev->descriptor.bDeviceClass) {
159 printf(" - Class: ");
160 usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
164 printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
166 printf(" - PacketSize: %d Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
167 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",dev->descriptor.idVendor,dev->descriptor.idProduct,(dev->descriptor.bcdDevice>>8) & 0xff,dev->descriptor.bcdDevice & 0xff);
172 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
174 printf(" Configuration: %d\n",config->bConfigurationValue);
175 printf(" - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
176 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
177 if (config->iConfiguration) {
179 usb_display_string(dev,config->iConfiguration);
184 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
186 printf(" Interface: %d\n",ifdesc->bInterfaceNumber);
187 printf(" - Alternate Setting %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
189 usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
191 if (ifdesc->iInterface) {
193 usb_display_string(dev,ifdesc->iInterface);
198 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
200 printf(" - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
201 switch((epdesc->bmAttributes & 0x03))
203 case 0: printf("Control"); break;
204 case 1: printf("Isochronous"); break;
205 case 2: printf("Bulk"); break;
206 case 3: printf("Interrupt"); break;
208 printf(" MaxPacket %d",epdesc->wMaxPacketSize);
209 if ((epdesc->bmAttributes & 0x03)==0x3)
210 printf(" Interval %dms",epdesc->bInterval);
214 /* main routine to diasplay the configs, interfaces and endpoints */
215 void usb_display_config(struct usb_device *dev)
217 struct usb_config_descriptor *config;
218 struct usb_interface_descriptor *ifdesc;
219 struct usb_endpoint_descriptor *epdesc;
222 config= &dev->config;
223 usb_display_conf_desc(config,dev);
224 for(i=0;i<config->no_of_if;i++) {
225 ifdesc= &config->if_desc[i];
226 usb_display_if_desc(ifdesc,dev);
227 for(ii=0;ii<ifdesc->no_of_ep;ii++) {
228 epdesc= &ifdesc->ep_desc[ii];
229 usb_display_ep_desc(epdesc);
235 /* shows the device tree recursively */
236 void usb_show_tree_graph(struct usb_device *dev,char *pre)
239 int has_child,last_child,port;
243 /* check if the device has connected children */
245 for(i=0;i<dev->maxchild;i++) {
246 if (dev->children[i]!=NULL)
249 /* check if we are the last one */
251 if (dev->parent!=NULL) {
252 for(i=0;i<dev->parent->maxchild;i++) {
253 /* search for children */
254 if (dev->parent->children[i]==dev) {
255 /* found our pointer, see if we have a little sister */
257 while(i++<dev->parent->maxchild) {
258 if (dev->parent->children[i]!=NULL) {
265 } /* for all children of the parent */
267 /* correct last child */
271 } /* if not root hub */
274 printf("%d ",dev->devnum);
276 pre[index++]= has_child ? '|' : ' ';
278 printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
279 dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
280 if (strlen(dev->mf) ||
283 printf(" %s %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
285 if (dev->maxchild>0) {
286 for(i=0;i<dev->maxchild;i++) {
287 if (dev->children[i]!=NULL) {
288 usb_show_tree_graph(dev->children[i],pre);
295 /* main routine for the tree command */
296 void usb_show_tree(struct usb_device *dev)
300 memset(preamble,0,32);
301 usb_show_tree_graph(dev,&preamble[0]);
305 /******************************************************************************
306 * usb boot command intepreter. Derived from diskboot
308 #ifdef CONFIG_USB_STORAGE
309 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
311 char *boot_device = NULL;
313 int dev, part=1, rcode;
315 disk_partition_t info;
317 block_dev_desc_t *stor_dev;
318 #if defined(CONFIG_FIT)
324 addr = CFG_LOAD_ADDR;
325 boot_device = getenv ("bootdevice");
328 addr = simple_strtoul(argv[1], NULL, 16);
329 boot_device = getenv ("bootdevice");
332 addr = simple_strtoul(argv[1], NULL, 16);
333 boot_device = argv[2];
336 printf ("Usage:\n%s\n", cmdtp->usage);
341 puts ("\n** No boot device **\n");
345 dev = simple_strtoul(boot_device, &ep, 16);
346 stor_dev=usb_stor_get_dev(dev);
347 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
348 printf ("\n** Device %d not available\n", dev);
351 if (stor_dev->block_read==NULL) {
352 printf("storage device not initialized. Use usb scan\n");
357 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
360 part = simple_strtoul(++ep, NULL, 16);
363 if (get_partition_info (stor_dev, part, &info)) {
364 /* try to boot raw .... */
365 strncpy((char *)&info.type[0], BOOT_PART_TYPE, sizeof(BOOT_PART_TYPE));
366 strncpy((char *)&info.name[0], "Raw", 4);
370 printf("error reading partinfo...try to boot raw\n");
372 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
373 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
374 printf ("\n** Invalid partition type \"%.32s\""
375 " (expect \"" BOOT_PART_TYPE "\")\n",
379 printf ("\nLoading from USB device %d, partition %d: "
380 "Name: %.32s Type: %.32s\n",
381 dev, part, info.name, info.type);
383 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
384 info.start, info.size, info.blksz);
386 if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
387 printf ("** Read error on %d:%d\n", dev, part);
391 switch (genimg_get_format ((void *)addr)) {
392 case IMAGE_FORMAT_LEGACY:
393 hdr = (image_header_t *)addr;
395 if (!image_check_hcrc (hdr)) {
396 puts ("\n** Bad Header Checksum **\n");
400 image_print_contents (hdr);
402 cnt = image_get_image_size (hdr);
404 #if defined(CONFIG_FIT)
405 case IMAGE_FORMAT_FIT:
406 fit_hdr = (const void *)addr;
407 if (!fit_check_format (fit_hdr)) {
408 puts ("** Bad FIT image format\n");
411 puts ("Fit image detected...\n");
413 cnt = fit_get_size (fit_hdr);
417 puts ("** Unknown image type\n");
421 cnt += info.blksz - 1;
425 if (stor_dev->block_read (dev, info.start+1, cnt,
426 (ulong *)(addr+info.blksz)) != cnt) {
427 printf ("\n** Read error on %d:%d\n", dev, part);
431 #if defined(CONFIG_FIT)
432 /* This cannot be done earlier, we need complete FIT image in RAM first */
433 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
434 fit_print_contents ((const void *)addr);
437 /* Loading ok, update default load address */
440 flush_cache (addr, (cnt+1)*info.blksz);
442 /* Check if we should attempt an auto-start */
443 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
445 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
446 local_args[0] = argv[0];
447 local_args[1] = NULL;
448 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
449 rcode=do_bootm (cmdtp, 0, 1, local_args);
454 #endif /* CONFIG_USB_STORAGE */
457 /*********************************************************************************
458 * usb command intepreter
460 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
464 struct usb_device *dev = NULL;
465 extern char usb_started;
466 #ifdef CONFIG_USB_STORAGE
467 block_dev_desc_t *stor_dev;
470 if ((strncmp(argv[1], "reset", 5) == 0) ||
471 (strncmp(argv[1], "start", 5) == 0)){
473 printf("(Re)start USB...\n");
475 #ifdef CONFIG_USB_STORAGE
476 /* try to recognize storage devices immediately */
478 usb_stor_curr_dev = usb_stor_scan(1);
482 if (strncmp(argv[1],"stop",4) == 0) {
483 #ifdef CONFIG_USB_KEYBOARD
485 if (usb_kbd_deregister()!=0) {
486 printf("USB not stopped: usbkbd still using USB\n");
490 else { /* forced stop, switch console in to serial */
491 console_assign(stdin,"serial");
492 usb_kbd_deregister();
495 printf("stopping USB..\n");
500 printf("USB is stopped. Please issue 'usb start' first.\n");
503 if (strncmp(argv[1],"tree",4) == 0) {
504 printf("\nDevice Tree:\n");
505 usb_show_tree(usb_get_dev_index(0));
508 if (strncmp(argv[1],"inf",3) == 0) {
511 for(d=0;d<USB_MAX_DEVICE;d++) {
512 dev=usb_get_dev_index(d);
515 usb_display_desc(dev);
516 usb_display_config(dev);
523 i=simple_strtoul(argv[2], NULL, 16);
524 printf("config for device %d\n",i);
525 for(d=0;d<USB_MAX_DEVICE;d++) {
526 dev=usb_get_dev_index(d);
533 printf("*** NO Device avaiable ***\n");
537 usb_display_desc(dev);
538 usb_display_config(dev);
543 #ifdef CONFIG_USB_STORAGE
544 if (strncmp(argv[1], "scan", 4) == 0) {
545 printf(" NOTE: this command is obsolete and will be phased out\n");
546 printf(" please use 'usb storage' for USB storage devices information\n\n");
551 if (strncmp(argv[1], "stor", 4) == 0) {
552 return usb_stor_info();
555 if (strncmp(argv[1],"part",4) == 0) {
557 for (ok=0, devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
558 stor_dev=usb_stor_get_dev(devno);
559 if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
563 printf("print_part of %x\n",devno);
564 print_part(stor_dev);
568 printf("\nno USB devices available\n");
573 if (strcmp(argv[1],"read") == 0) {
574 if (usb_stor_curr_dev<0) {
575 printf("no current device selected\n");
579 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
580 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
581 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
583 printf ("\nUSB read: device %d block # %ld, count %ld ... ",
584 usb_stor_curr_dev, blk, cnt);
585 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
586 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
587 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
593 if (strncmp(argv[1], "dev", 3) == 0) {
595 int dev = (int)simple_strtoul(argv[2], NULL, 10);
596 printf ("\nUSB device %d: ", dev);
597 if (dev >= USB_MAX_STOR_DEV) {
598 printf("unknown device\n");
601 printf ("\n Device %d: ", dev);
602 stor_dev=usb_stor_get_dev(dev);
604 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
607 usb_stor_curr_dev = dev;
608 printf("... is now current device\n");
612 printf ("\nUSB device %d: ", usb_stor_curr_dev);
613 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
615 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
622 #endif /* CONFIG_USB_STORAGE */
623 printf ("Usage:\n%s\n", cmdtp->usage);
627 #ifdef CONFIG_USB_STORAGE
630 "usb - USB sub-system\n",
631 "reset - reset (rescan) USB controller\n"
632 "usb stop [f] - stop USB [f]=force stop\n"
633 "usb tree - show USB device tree\n"
634 "usb info [dev] - show available USB devices\n"
635 "usb storage - show details of USB storage devices\n"
636 "usb dev [dev] - show or set current USB storage device\n"
637 "usb part [dev] - print partition table of one or all USB storage devices\n"
638 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
639 " to memory address `addr'\n"
644 usbboot, 3, 1, do_usbboot,
645 "usbboot - boot from USB device\n",
646 "loadAddr dev:part\n"
652 "usb - USB sub-system\n",
653 "reset - reset (rescan) USB controller\n"
654 "usb tree - show USB device tree\n"
655 "usb info [dev] - show available USB devices\n"