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,
31 #if (CONFIG_COMMANDS & CFG_CMD_USB)
35 static int usb_stor_curr_dev=-1; /* current device */
37 /* some display routines (info command) */
38 char * usb_get_class_desc(unsigned char dclass)
41 case USB_CLASS_PER_INTERFACE:
42 return("See Interface");
46 return("Communication");
48 return("Human Interface");
49 case USB_CLASS_PRINTER:
51 case USB_CLASS_MASS_STORAGE:
52 return("Mass Storage");
57 case USB_CLASS_VENDOR_SPEC:
58 return("Vendor specific");
64 void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
67 case USB_CLASS_PER_INTERFACE:
68 printf("See Interface");
71 printf("Human Interface, Subclass: ");
73 case USB_SUB_HID_NONE:
76 case USB_SUB_HID_BOOT:
79 case USB_PROT_HID_NONE:
82 case USB_PROT_HID_KEYBOARD:
85 case USB_PROT_HID_MOUSE:
96 case USB_CLASS_MASS_STORAGE:
97 printf("Mass Storage, ");
103 printf("SFF-8020i (ATAPI)");
106 printf("QIC-157 (Tape)");
115 printf("Transp. SCSI");
124 printf("Command/Bulk");
127 printf("Command/Bulk/Int");
137 printf("%s",usb_get_class_desc(dclass));
141 void usb_display_string(struct usb_device *dev,int index)
145 if (usb_string(dev,index,&buffer[0],256)>0);
146 printf("String: \"%s\"",buffer);
150 void usb_display_desc(struct usb_device *dev)
152 if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
153 printf("%d: %s, USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
154 (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
155 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
156 printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
157 if (dev->descriptor.bDeviceClass) {
158 printf(" - Class: ");
159 usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
163 printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
165 printf(" - PacketSize: %d Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
166 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);
171 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
173 printf(" Configuration: %d\n",config->bConfigurationValue);
174 printf(" - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
175 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
176 if (config->iConfiguration) {
178 usb_display_string(dev,config->iConfiguration);
183 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
185 printf(" Interface: %d\n",ifdesc->bInterfaceNumber);
186 printf(" - Alternate Settings %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
188 usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
190 if (ifdesc->iInterface) {
192 usb_display_string(dev,ifdesc->iInterface);
197 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
199 printf(" - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
200 switch((epdesc->bmAttributes & 0x03))
202 case 0: printf("Control"); break;
203 case 1: printf("Isochronous"); break;
204 case 2: printf("Bulk"); break;
205 case 3: printf("Interrupt"); break;
207 printf(" MaxPacket %d",epdesc->wMaxPacketSize);
208 if ((epdesc->bmAttributes & 0x03)==0x3)
209 printf(" Interval %dms",epdesc->bInterval);
213 /* main routine to diasplay the configs, interfaces and endpoints */
214 void usb_display_config(struct usb_device *dev)
216 struct usb_config_descriptor *config;
217 struct usb_interface_descriptor *ifdesc;
218 struct usb_endpoint_descriptor *epdesc;
221 config= &dev->config;
222 usb_display_conf_desc(config,dev);
223 for(i=0;i<config->no_of_if;i++) {
224 ifdesc= &config->if_desc[i];
225 usb_display_if_desc(ifdesc,dev);
226 for(ii=0;ii<ifdesc->no_of_ep;ii++) {
227 epdesc= &ifdesc->ep_desc[ii];
228 usb_display_ep_desc(epdesc);
234 /* shows the device tree recursively */
235 void usb_show_tree_graph(struct usb_device *dev,char *pre)
238 int has_child,last_child,port;
242 /* check if the device has connected children */
244 for(i=0;i<dev->maxchild;i++) {
245 if (dev->children[i]!=NULL)
248 /* check if we are the last one */
250 if (dev->parent!=NULL) {
251 for(i=0;i<dev->parent->maxchild;i++) {
252 /* search for children */
253 if (dev->parent->children[i]==dev) {
254 /* found our pointer, see if we have a little sister */
256 while(i++<dev->parent->maxchild) {
257 if (dev->parent->children[i]!=NULL) {
264 } /* for all children of the parent */
266 /* correct last child */
270 } /* if not root hub */
273 printf("%d ",dev->devnum);
275 pre[index++]= has_child ? '|' : ' ';
277 printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
278 dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
279 if (strlen(dev->mf) ||
282 printf(" %s %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
284 if (dev->maxchild>0) {
285 for(i=0;i<dev->maxchild;i++) {
286 if (dev->children[i]!=NULL) {
287 usb_show_tree_graph(dev->children[i],pre);
294 /* main routine for the tree command */
295 void usb_show_tree(struct usb_device *dev)
299 memset(preamble,0,32);
300 usb_show_tree_graph(dev,&preamble[0]);
304 /******************************************************************************
305 * usb boot command intepreter. Derived from diskboot
307 #ifdef CONFIG_USB_STORAGE
308 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
310 char *boot_device = NULL;
312 int dev, part=1, rcode;
313 ulong addr, cnt, checksum;
314 disk_partition_t info;
316 block_dev_desc_t *stor_dev;
321 addr = CFG_LOAD_ADDR;
322 boot_device = getenv ("bootdevice");
325 addr = simple_strtoul(argv[1], NULL, 16);
326 boot_device = getenv ("bootdevice");
329 addr = simple_strtoul(argv[1], NULL, 16);
330 boot_device = argv[2];
333 printf ("Usage:\n%s\n", cmdtp->usage);
338 puts ("\n** No boot device **\n");
342 dev = simple_strtoul(boot_device, &ep, 16);
343 stor_dev=usb_stor_get_dev(dev);
344 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
345 printf ("\n** Device %d not available\n", dev);
348 if (stor_dev->block_read==NULL) {
349 printf("storage device not initialized. Use usb scan\n");
354 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
357 part = simple_strtoul(++ep, NULL, 16);
360 if (get_partition_info (stor_dev, part, &info)) {
361 /* try to boot raw .... */
362 strncpy(&info.type[0], BOOT_PART_TYPE, sizeof(BOOT_PART_TYPE));
363 strncpy(&info.name[0], "Raw", 4);
367 printf("error reading partinfo...try to boot raw\n");
369 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
370 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
371 printf ("\n** Invalid partition type \"%.32s\""
372 " (expect \"" BOOT_PART_TYPE "\")\n",
376 printf ("\nLoading from USB device %d, partition %d: "
377 "Name: %.32s Type: %.32s\n",
378 dev, part, info.name, info.type);
380 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
381 info.start, info.size, info.blksz);
383 if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
384 printf ("** Read error on %d:%d\n", dev, part);
388 hdr = (image_header_t *)addr;
390 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
391 printf("\n** Bad Magic Number **\n");
395 checksum = ntohl(hdr->ih_hcrc);
398 if (crc32 (0, (char *)hdr, sizeof(image_header_t)) != checksum) {
399 puts ("\n** Bad Header Checksum **\n");
402 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
404 print_image_hdr (hdr);
406 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
407 cnt += info.blksz - 1;
411 if (stor_dev->block_read (dev, info.start+1, cnt,
412 (ulong *)(addr+info.blksz)) != cnt) {
413 printf ("\n** Read error on %d:%d\n", dev, part);
416 /* Loading ok, update default load address */
419 flush_cache (addr, (cnt+1)*info.blksz);
421 /* Check if we should attempt an auto-start */
422 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
424 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
425 local_args[0] = argv[0];
426 local_args[1] = NULL;
427 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
428 rcode=do_bootm (cmdtp, 0, 1, local_args);
433 #endif /* CONFIG_USB_STORAGE */
436 /*********************************************************************************
437 * usb command intepreter
439 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
443 struct usb_device *dev = NULL;
444 block_dev_desc_t *stor_dev;
446 if ((strncmp(argv[1],"reset",5) == 0) ||
447 (strncmp(argv[1],"start",5) == 0)){
449 printf("(Re)start USB...\n");
453 if (strncmp(argv[1],"stop",4) == 0) {
454 #ifdef CONFIG_USB_KEYBOARD
456 if (usb_kbd_deregister()!=0) {
457 printf("USB not stopped: usbkbd still using USB\n");
461 else { /* forced stop, switch console in to serial */
462 console_assign(stdin,"serial");
463 usb_kbd_deregister();
466 printf("stopping USB..\n");
470 if (strncmp(argv[1],"tree",4) == 0) {
471 printf("\nDevice Tree:\n");
472 usb_show_tree(usb_get_dev_index(0));
475 if (strncmp(argv[1],"inf",3) == 0) {
478 for(d=0;d<USB_MAX_DEVICE;d++) {
479 dev=usb_get_dev_index(d);
482 usb_display_desc(dev);
483 usb_display_config(dev);
490 i=simple_strtoul(argv[2], NULL, 16);
491 printf("config for device %d\n",i);
492 for(d=0;d<USB_MAX_DEVICE;d++) {
493 dev=usb_get_dev_index(d);
500 printf("*** NO Device avaiable ***\n");
504 usb_display_desc(dev);
505 usb_display_config(dev);
510 #ifdef CONFIG_USB_STORAGE
511 if (strncmp(argv[1],"scan",4) == 0) {
512 printf("Scan for storage device:\n");
513 usb_stor_curr_dev=usb_stor_scan(1);
514 if (usb_stor_curr_dev==-1) {
515 printf("No device found. Not initialized?\n");
520 if (strncmp(argv[1],"part",4) == 0) {
522 for (ok=0, devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
523 stor_dev=usb_stor_get_dev(devno);
524 if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
528 printf("print_part of %x\n",devno);
529 print_part(stor_dev);
533 printf("\nno USB devices available\n");
538 if (strcmp(argv[1],"read") == 0) {
539 if (usb_stor_curr_dev<0) {
540 printf("no current device selected\n");
544 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
545 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
546 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
548 printf ("\nUSB read: device %d block # %ld, count %ld ... ",
549 usb_stor_curr_dev, blk, cnt);
550 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
551 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
552 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
558 if (strcmp(argv[1],"dev") == 0) {
560 int dev = (int)simple_strtoul(argv[2], NULL, 10);
561 printf ("\nUSB device %d: ", dev);
562 if (dev >= USB_MAX_STOR_DEV) {
563 printf("unknown device\n");
566 printf ("\n Device %d: ", dev);
567 stor_dev=usb_stor_get_dev(dev);
569 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
572 usb_stor_curr_dev = dev;
573 printf("... is now current device\n");
577 printf ("\nUSB device %d: ", usb_stor_curr_dev);
578 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
580 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
587 #endif /* CONFIG_USB_STORAGE */
588 printf ("Usage:\n%s\n", cmdtp->usage);
593 #endif /* (CONFIG_COMMANDS & CFG_CMD_USB) */
596 #if (CONFIG_COMMANDS & CFG_CMD_USB)
598 #ifdef CONFIG_USB_STORAGE
601 "usb - USB sub-system\n",
602 "reset - reset (rescan) USB controller\n"
603 "usb stop [f] - stop USB [f]=force stop\n"
604 "usb tree - show USB device tree\n"
605 "usb info [dev] - show available USB devices\n"
606 "usb scan - (re-)scan USB bus for storage devices\n"
607 "usb device [dev] - show or set current USB storage device\n"
608 "usb part [dev] - print partition table of one or all USB storage devices\n"
609 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
610 " to memory address `addr'\n"
615 usbboot, 3, 1, do_usbboot,
616 "usbboot - boot from USB device\n",
617 "loadAddr dev:part\n"
623 "usb - USB sub-system\n",
624 "reset - reset (rescan) USB controller\n"
625 "usb tree - show USB device tree\n"
626 "usb info [dev] - show available USB devices\n"