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>
32 #if (CONFIG_COMMANDS & CFG_CMD_USB)
36 #ifdef CONFIG_USB_STORAGE
37 static int usb_stor_curr_dev=-1; /* current device */
40 /* some display routines (info command) */
41 char * usb_get_class_desc(unsigned char dclass)
44 case USB_CLASS_PER_INTERFACE:
45 return("See Interface");
49 return("Communication");
51 return("Human Interface");
52 case USB_CLASS_PRINTER:
54 case USB_CLASS_MASS_STORAGE:
55 return("Mass Storage");
60 case USB_CLASS_VENDOR_SPEC:
61 return("Vendor specific");
67 void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
70 case USB_CLASS_PER_INTERFACE:
71 printf("See Interface");
74 printf("Human Interface, Subclass: ");
76 case USB_SUB_HID_NONE:
79 case USB_SUB_HID_BOOT:
82 case USB_PROT_HID_NONE:
85 case USB_PROT_HID_KEYBOARD:
88 case USB_PROT_HID_MOUSE:
99 case USB_CLASS_MASS_STORAGE:
100 printf("Mass Storage, ");
106 printf("SFF-8020i (ATAPI)");
109 printf("QIC-157 (Tape)");
118 printf("Transp. SCSI");
127 printf("Command/Bulk");
130 printf("Command/Bulk/Int");
140 printf("%s",usb_get_class_desc(dclass));
144 void usb_display_string(struct usb_device *dev,int index)
148 if (usb_string(dev,index,&buffer[0],256)>0);
149 printf("String: \"%s\"",buffer);
153 void usb_display_desc(struct usb_device *dev)
155 if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
156 printf("%d: %s, USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
157 (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
158 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
159 printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
160 if (dev->descriptor.bDeviceClass) {
161 printf(" - Class: ");
162 usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
166 printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
168 printf(" - PacketSize: %d Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
169 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);
174 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
176 printf(" Configuration: %d\n",config->bConfigurationValue);
177 printf(" - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
178 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
179 if (config->iConfiguration) {
181 usb_display_string(dev,config->iConfiguration);
186 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
188 printf(" Interface: %d\n",ifdesc->bInterfaceNumber);
189 printf(" - Alternate Settings %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
191 usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
193 if (ifdesc->iInterface) {
195 usb_display_string(dev,ifdesc->iInterface);
200 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
202 printf(" - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
203 switch((epdesc->bmAttributes & 0x03))
205 case 0: printf("Control"); break;
206 case 1: printf("Isochronous"); break;
207 case 2: printf("Bulk"); break;
208 case 3: printf("Interrupt"); break;
210 printf(" MaxPacket %d",epdesc->wMaxPacketSize);
211 if ((epdesc->bmAttributes & 0x03)==0x3)
212 printf(" Interval %dms",epdesc->bInterval);
216 /* main routine to diasplay the configs, interfaces and endpoints */
217 void usb_display_config(struct usb_device *dev)
219 struct usb_config_descriptor *config;
220 struct usb_interface_descriptor *ifdesc;
221 struct usb_endpoint_descriptor *epdesc;
224 config= &dev->config;
225 usb_display_conf_desc(config,dev);
226 for(i=0;i<config->no_of_if;i++) {
227 ifdesc= &config->if_desc[i];
228 usb_display_if_desc(ifdesc,dev);
229 for(ii=0;ii<ifdesc->no_of_ep;ii++) {
230 epdesc= &ifdesc->ep_desc[ii];
231 usb_display_ep_desc(epdesc);
237 /* shows the device tree recursively */
238 void usb_show_tree_graph(struct usb_device *dev,char *pre)
241 int has_child,last_child,port;
245 /* check if the device has connected children */
247 for(i=0;i<dev->maxchild;i++) {
248 if (dev->children[i]!=NULL)
251 /* check if we are the last one */
253 if (dev->parent!=NULL) {
254 for(i=0;i<dev->parent->maxchild;i++) {
255 /* search for children */
256 if (dev->parent->children[i]==dev) {
257 /* found our pointer, see if we have a little sister */
259 while(i++<dev->parent->maxchild) {
260 if (dev->parent->children[i]!=NULL) {
267 } /* for all children of the parent */
269 /* correct last child */
273 } /* if not root hub */
276 printf("%d ",dev->devnum);
278 pre[index++]= has_child ? '|' : ' ';
280 printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
281 dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
282 if (strlen(dev->mf) ||
285 printf(" %s %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
287 if (dev->maxchild>0) {
288 for(i=0;i<dev->maxchild;i++) {
289 if (dev->children[i]!=NULL) {
290 usb_show_tree_graph(dev->children[i],pre);
297 /* main routine for the tree command */
298 void usb_show_tree(struct usb_device *dev)
302 memset(preamble,0,32);
303 usb_show_tree_graph(dev,&preamble[0]);
307 /******************************************************************************
308 * usb boot command intepreter. Derived from diskboot
310 #ifdef CONFIG_USB_STORAGE
311 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
313 char *boot_device = NULL;
315 int dev, part=1, rcode;
316 ulong addr, cnt, checksum;
317 disk_partition_t info;
319 block_dev_desc_t *stor_dev;
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 hdr = (image_header_t *)addr;
393 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
394 printf("\n** Bad Magic Number **\n");
398 checksum = ntohl(hdr->ih_hcrc);
401 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
402 puts ("\n** Bad Header Checksum **\n");
405 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
407 print_image_hdr (hdr);
409 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
410 cnt += info.blksz - 1;
414 if (stor_dev->block_read (dev, info.start+1, cnt,
415 (ulong *)(addr+info.blksz)) != cnt) {
416 printf ("\n** Read error on %d:%d\n", dev, part);
419 /* Loading ok, update default load address */
422 flush_cache (addr, (cnt+1)*info.blksz);
424 /* Check if we should attempt an auto-start */
425 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
427 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
428 local_args[0] = argv[0];
429 local_args[1] = NULL;
430 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
431 rcode=do_bootm (cmdtp, 0, 1, local_args);
436 #endif /* CONFIG_USB_STORAGE */
439 /*********************************************************************************
440 * usb command intepreter
442 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
446 struct usb_device *dev = NULL;
447 #ifdef CONFIG_USB_STORAGE
448 block_dev_desc_t *stor_dev;
451 if ((strncmp(argv[1], "reset", 5) == 0) ||
452 (strncmp(argv[1], "start", 5) == 0)){
454 printf("(Re)start USB...\n");
456 #ifdef CONFIG_USB_STORAGE
457 /* try to recognize storage devices immediately */
459 usb_stor_curr_dev = usb_stor_scan(1);
463 if (strncmp(argv[1],"stop",4) == 0) {
464 #ifdef CONFIG_USB_KEYBOARD
466 if (usb_kbd_deregister()!=0) {
467 printf("USB not stopped: usbkbd still using USB\n");
471 else { /* forced stop, switch console in to serial */
472 console_assign(stdin,"serial");
473 usb_kbd_deregister();
476 printf("stopping USB..\n");
480 if (strncmp(argv[1],"tree",4) == 0) {
481 printf("\nDevice Tree:\n");
482 usb_show_tree(usb_get_dev_index(0));
485 if (strncmp(argv[1],"inf",3) == 0) {
488 for(d=0;d<USB_MAX_DEVICE;d++) {
489 dev=usb_get_dev_index(d);
492 usb_display_desc(dev);
493 usb_display_config(dev);
500 i=simple_strtoul(argv[2], NULL, 16);
501 printf("config for device %d\n",i);
502 for(d=0;d<USB_MAX_DEVICE;d++) {
503 dev=usb_get_dev_index(d);
510 printf("*** NO Device avaiable ***\n");
514 usb_display_desc(dev);
515 usb_display_config(dev);
520 #ifdef CONFIG_USB_STORAGE
521 if (strncmp(argv[1], "scan", 4) == 0) {
522 printf(" NOTE: this command is obsolete and will be phased out\n");
523 printf(" please use 'usb storage' for USB storage devices information\n\n");
528 if (strncmp(argv[1], "stor", 4) == 0) {
533 if (strncmp(argv[1],"part",4) == 0) {
535 for (ok=0, devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
536 stor_dev=usb_stor_get_dev(devno);
537 if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
541 printf("print_part of %x\n",devno);
542 print_part(stor_dev);
546 printf("\nno USB devices available\n");
551 if (strcmp(argv[1],"read") == 0) {
552 if (usb_stor_curr_dev<0) {
553 printf("no current device selected\n");
557 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
558 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
559 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
561 printf ("\nUSB read: device %d block # %ld, count %ld ... ",
562 usb_stor_curr_dev, blk, cnt);
563 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
564 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
565 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
571 if (strncmp(argv[1], "dev", 3) == 0) {
573 int dev = (int)simple_strtoul(argv[2], NULL, 10);
574 printf ("\nUSB device %d: ", dev);
575 if (dev >= USB_MAX_STOR_DEV) {
576 printf("unknown device\n");
579 printf ("\n Device %d: ", dev);
580 stor_dev=usb_stor_get_dev(dev);
582 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
585 usb_stor_curr_dev = dev;
586 printf("... is now current device\n");
590 printf ("\nUSB device %d: ", usb_stor_curr_dev);
591 stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
593 if (stor_dev->type == DEV_TYPE_UNKNOWN) {
600 #endif /* CONFIG_USB_STORAGE */
601 printf ("Usage:\n%s\n", cmdtp->usage);
606 #endif /* (CONFIG_COMMANDS & CFG_CMD_USB) */
609 #if (CONFIG_COMMANDS & CFG_CMD_USB)
611 #ifdef CONFIG_USB_STORAGE
614 "usb - USB sub-system\n",
615 "reset - reset (rescan) USB controller\n"
616 "usb stop [f] - stop USB [f]=force stop\n"
617 "usb tree - show USB device tree\n"
618 "usb info [dev] - show available USB devices\n"
619 "usb storage - show details of USB storage devices\n"
620 "usb dev [dev] - show or set current USB storage device\n"
621 "usb part [dev] - print partition table of one or all USB storage devices\n"
622 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
623 " to memory address `addr'\n"
628 usbboot, 3, 1, do_usbboot,
629 "usbboot - boot from USB device\n",
630 "loadAddr dev:part\n"
636 "usb - USB sub-system\n",
637 "reset - reset (rescan) USB controller\n"
638 "usb tree - show USB device tree\n"
639 "usb info [dev] - show available USB devices\n"