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>
33 #if defined(CONFIG_CMD_USB)
37 #ifdef CONFIG_USB_STORAGE
38 static int usb_stor_curr_dev=-1; /* current 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,unsigned char proto)
71 case USB_CLASS_PER_INTERFACE:
72 printf("See Interface");
75 printf("Human Interface, Subclass: ");
77 case USB_SUB_HID_NONE:
80 case USB_SUB_HID_BOOT:
83 case USB_PROT_HID_NONE:
86 case USB_PROT_HID_KEYBOARD:
89 case USB_PROT_HID_MOUSE:
100 case USB_CLASS_MASS_STORAGE:
101 printf("Mass Storage, ");
107 printf("SFF-8020i (ATAPI)");
110 printf("QIC-157 (Tape)");
119 printf("Transp. SCSI");
128 printf("Command/Bulk");
131 printf("Command/Bulk/Int");
141 printf("%s",usb_get_class_desc(dclass));
145 void usb_display_string(struct usb_device *dev,int index)
149 if (usb_string(dev,index,&buffer[0],256)>0);
150 printf("String: \"%s\"",buffer);
154 void usb_display_desc(struct usb_device *dev)
156 if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
157 printf("%d: %s, USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
158 (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
159 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
160 printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
161 if (dev->descriptor.bDeviceClass) {
162 printf(" - Class: ");
163 usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
167 printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
169 printf(" - PacketSize: %d Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
170 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);
175 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
177 printf(" Configuration: %d\n",config->bConfigurationValue);
178 printf(" - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
179 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
180 if (config->iConfiguration) {
182 usb_display_string(dev,config->iConfiguration);
187 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
189 printf(" Interface: %d\n",ifdesc->bInterfaceNumber);
190 printf(" - Alternate Setting %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
192 usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
194 if (ifdesc->iInterface) {
196 usb_display_string(dev,ifdesc->iInterface);
201 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
203 printf(" - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
204 switch((epdesc->bmAttributes & 0x03))
206 case 0: printf("Control"); break;
207 case 1: printf("Isochronous"); break;
208 case 2: printf("Bulk"); break;
209 case 3: printf("Interrupt"); break;
211 printf(" MaxPacket %d",epdesc->wMaxPacketSize);
212 if ((epdesc->bmAttributes & 0x03)==0x3)
213 printf(" Interval %dms",epdesc->bInterval);
217 /* main routine to diasplay the configs, interfaces and endpoints */
218 void usb_display_config(struct usb_device *dev)
220 struct usb_config_descriptor *config;
221 struct usb_interface_descriptor *ifdesc;
222 struct usb_endpoint_descriptor *epdesc;
225 config= &dev->config;
226 usb_display_conf_desc(config,dev);
227 for(i=0;i<config->no_of_if;i++) {
228 ifdesc= &config->if_desc[i];
229 usb_display_if_desc(ifdesc,dev);
230 for(ii=0;ii<ifdesc->no_of_ep;ii++) {
231 epdesc= &ifdesc->ep_desc[ii];
232 usb_display_ep_desc(epdesc);
238 /* shows the device tree recursively */
239 void usb_show_tree_graph(struct usb_device *dev,char *pre)
242 int has_child,last_child,port;
246 /* check if the device has connected children */
248 for(i=0;i<dev->maxchild;i++) {
249 if (dev->children[i]!=NULL)
252 /* check if we are the last one */
254 if (dev->parent!=NULL) {
255 for(i=0;i<dev->parent->maxchild;i++) {
256 /* search for children */
257 if (dev->parent->children[i]==dev) {
258 /* found our pointer, see if we have a little sister */
260 while(i++<dev->parent->maxchild) {
261 if (dev->parent->children[i]!=NULL) {
268 } /* for all children of the parent */
270 /* correct last child */
274 } /* if not root hub */
277 printf("%d ",dev->devnum);
279 pre[index++]= has_child ? '|' : ' ';
281 printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
282 dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
283 if (strlen(dev->mf) ||
286 printf(" %s %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
288 if (dev->maxchild>0) {
289 for(i=0;i<dev->maxchild;i++) {
290 if (dev->children[i]!=NULL) {
291 usb_show_tree_graph(dev->children[i],pre);
298 /* main routine for the tree command */
299 void usb_show_tree(struct usb_device *dev)
303 memset(preamble,0,32);
304 usb_show_tree_graph(dev,&preamble[0]);
308 /******************************************************************************
309 * usb boot command intepreter. Derived from diskboot
311 #ifdef CONFIG_USB_STORAGE
312 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
314 char *boot_device = NULL;
316 int dev, part=1, rcode;
317 ulong addr, cnt, checksum;
318 disk_partition_t info;
320 block_dev_desc_t *stor_dev;
325 addr = CFG_LOAD_ADDR;
326 boot_device = getenv ("bootdevice");
329 addr = simple_strtoul(argv[1], NULL, 16);
330 boot_device = getenv ("bootdevice");
333 addr = simple_strtoul(argv[1], NULL, 16);
334 boot_device = argv[2];
337 printf ("Usage:\n%s\n", cmdtp->usage);
342 puts ("\n** No boot device **\n");
346 dev = simple_strtoul(boot_device, &ep, 16);
347 stor_dev=usb_stor_get_dev(dev);
348 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
349 printf ("\n** Device %d not available\n", dev);
352 if (stor_dev->block_read==NULL) {
353 printf("storage device not initialized. Use usb scan\n");
358 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
361 part = simple_strtoul(++ep, NULL, 16);
364 if (get_partition_info (stor_dev, part, &info)) {
365 /* try to boot raw .... */
366 strncpy((char *)&info.type[0], BOOT_PART_TYPE, sizeof(BOOT_PART_TYPE));
367 strncpy((char *)&info.name[0], "Raw", 4);
371 printf("error reading partinfo...try to boot raw\n");
373 if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
374 (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
375 printf ("\n** Invalid partition type \"%.32s\""
376 " (expect \"" BOOT_PART_TYPE "\")\n",
380 printf ("\nLoading from USB device %d, partition %d: "
381 "Name: %.32s Type: %.32s\n",
382 dev, part, info.name, info.type);
384 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
385 info.start, info.size, info.blksz);
387 if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
388 printf ("** Read error on %d:%d\n", dev, part);
392 hdr = (image_header_t *)addr;
394 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
395 printf("\n** Bad Magic Number **\n");
399 checksum = ntohl(hdr->ih_hcrc);
402 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
403 puts ("\n** Bad Header Checksum **\n");
406 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
408 print_image_hdr (hdr);
410 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
411 cnt += info.blksz - 1;
415 if (stor_dev->block_read (dev, info.start+1, cnt,
416 (ulong *)(addr+info.blksz)) != cnt) {
417 printf ("\n** Read error on %d:%d\n", dev, part);
420 /* Loading ok, update default load address */
423 flush_cache (addr, (cnt+1)*info.blksz);
425 /* Check if we should attempt an auto-start */
426 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
428 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
429 local_args[0] = argv[0];
430 local_args[1] = NULL;
431 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
432 rcode=do_bootm (cmdtp, 0, 1, local_args);
437 #endif /* CONFIG_USB_STORAGE */
440 /*********************************************************************************
441 * usb command intepreter
443 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
447 struct usb_device *dev = NULL;
448 extern char usb_started;
449 #ifdef CONFIG_USB_STORAGE
450 block_dev_desc_t *stor_dev;
453 if ((strncmp(argv[1], "reset", 5) == 0) ||
454 (strncmp(argv[1], "start", 5) == 0)){
456 printf("(Re)start USB...\n");
458 #ifdef CONFIG_USB_STORAGE
459 /* try to recognize storage devices immediately */
461 usb_stor_curr_dev = usb_stor_scan(1);
465 if (strncmp(argv[1],"stop",4) == 0) {
466 #ifdef CONFIG_USB_KEYBOARD
468 if (usb_kbd_deregister()!=0) {
469 printf("USB not stopped: usbkbd still using USB\n");
473 else { /* forced stop, switch console in to serial */
474 console_assign(stdin,"serial");
475 usb_kbd_deregister();
478 printf("stopping USB..\n");
483 printf("USB is stopped. Please issue 'usb start' first.\n");
486 if (strncmp(argv[1],"tree",4) == 0) {
487 printf("\nDevice Tree:\n");
488 usb_show_tree(usb_get_dev_index(0));
491 if (strncmp(argv[1],"inf",3) == 0) {
494 for(d=0;d<USB_MAX_DEVICE;d++) {
495 dev=usb_get_dev_index(d);
498 usb_display_desc(dev);
499 usb_display_config(dev);
506 i=simple_strtoul(argv[2], NULL, 16);
507 printf("config for device %d\n",i);
508 for(d=0;d<USB_MAX_DEVICE;d++) {
509 dev=usb_get_dev_index(d);
516 printf("*** NO Device avaiable ***\n");
520 usb_display_desc(dev);
521 usb_display_config(dev);
526 #ifdef CONFIG_USB_STORAGE
527 if (strncmp(argv[1], "scan", 4) == 0) {
528 printf(" NOTE: this command is obsolete and will be phased out\n");
529 printf(" please use 'usb storage' for USB storage devices information\n\n");
534 if (strncmp(argv[1], "stor", 4) == 0) {
539 if (strncmp(argv[1],"part",4) == 0) {
541 for (ok=0, devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
542 stor_dev=usb_stor_get_dev(devno);
543 if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
547 printf("print_part of %x\n",devno);
548 print_part(stor_dev);
552 printf("\nno USB devices available\n");
557 if (strcmp(argv[1],"read") == 0) {
558 if (usb_stor_curr_dev<0) {
559 printf("no current device selected\n");
563 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
564 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
565 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
567 printf ("\nUSB read: device %d block # %ld, count %ld ... ",
568 usb_stor_curr_dev, blk, cnt);
569 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
570 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
571 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
577 if (strncmp(argv[1], "dev", 3) == 0) {
579 int dev = (int)simple_strtoul(argv[2], NULL, 10);
580 printf ("\nUSB device %d: ", dev);
581 if (dev >= USB_MAX_STOR_DEV) {
582 printf("unknown device\n");
585 printf ("\n Device %d: ", dev);
586 stor_dev=usb_stor_get_dev(dev);
588 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
591 usb_stor_curr_dev = dev;
592 printf("... is now current device\n");
596 printf ("\nUSB device %d: ", usb_stor_curr_dev);
597 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
599 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
606 #endif /* CONFIG_USB_STORAGE */
607 printf ("Usage:\n%s\n", cmdtp->usage);
615 #if defined(CONFIG_CMD_USB)
617 #ifdef CONFIG_USB_STORAGE
620 "usb - USB sub-system\n",
621 "reset - reset (rescan) USB controller\n"
622 "usb stop [f] - stop USB [f]=force stop\n"
623 "usb tree - show USB device tree\n"
624 "usb info [dev] - show available USB devices\n"
625 "usb storage - show details of USB storage devices\n"
626 "usb dev [dev] - show or set current USB storage device\n"
627 "usb part [dev] - print partition table of one or all USB storage devices\n"
628 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
629 " to memory address `addr'\n"
634 usbboot, 3, 1, do_usbboot,
635 "usbboot - boot from USB device\n",
636 "loadAddr dev:part\n"
642 "usb - USB sub-system\n",
643 "reset - reset (rescan) USB controller\n"
644 "usb tree - show USB device tree\n"
645 "usb info [dev] - show available USB devices\n"