]> git.sur5r.net Git - u-boot/blob - common/cmd_usb.c
* Patches by David Müller, 14 Nov 2003:
[u-boot] / common / cmd_usb.c
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland
4  *
5  * Most of this source has been derived from the Linux USB
6  * project.
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
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.
15  *
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.
20  *
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,
24  * MA 02111-1307 USA
25  *
26  */
27
28 #include <common.h>
29 #include <command.h>
30
31 #if (CONFIG_COMMANDS & CFG_CMD_USB)
32
33 #include <usb.h>
34
35 #undef  CMD_USB_DEBUG
36
37 #ifdef  CMD_USB_DEBUG
38 #define CMD_USB_PRINTF(fmt,args...)     printf (fmt ,##args)
39 #else
40 #define CMD_USB_PRINTF(fmt,args...)
41 #endif
42 static int usb_stor_curr_dev=-1; /* current device */
43
44 /* some display routines (info command) */
45 char * usb_get_class_desc(unsigned char dclass)
46 {
47         switch(dclass) {
48                 case USB_CLASS_PER_INTERFACE:
49                         return("See Interface");
50                 case USB_CLASS_AUDIO:
51                         return("Audio");
52                 case USB_CLASS_COMM:
53                         return("Communication");
54                 case USB_CLASS_HID:
55                         return("Human Interface");
56                 case USB_CLASS_PRINTER:
57                         return("Printer");
58                 case USB_CLASS_MASS_STORAGE:
59                         return("Mass Storage");
60                 case USB_CLASS_HUB:
61                         return("Hub");
62                 case USB_CLASS_DATA:
63                         return("CDC Data");
64                 case USB_CLASS_VENDOR_SPEC:
65                         return("Vendor specific");
66                 default :
67                         return("");
68         }
69 }
70
71 void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
72 {
73         switch(dclass) {
74                 case USB_CLASS_PER_INTERFACE:
75                         printf("See Interface");
76                         break;
77                 case USB_CLASS_HID:
78                         printf("Human Interface, Subclass: ");
79                         switch(subclass) {
80                                 case USB_SUB_HID_NONE:
81                                         printf("None");
82                                         break;
83                                 case USB_SUB_HID_BOOT:
84                                         printf("Boot ");
85                                         switch(proto) {
86                                                 case USB_PROT_HID_NONE:
87                                                         printf("None");
88                                                         break;
89                                                 case USB_PROT_HID_KEYBOARD:
90                                                         printf("Keyboard");
91                                                         break;
92                                                 case USB_PROT_HID_MOUSE:
93                                                         printf("Mouse");
94                                                         break;
95                                                 default:
96                                                         printf("reserved");
97                                         }
98                                         break;
99                                 default:
100                                         printf("reserved");
101                         }
102                         break;
103                 case USB_CLASS_MASS_STORAGE:
104                         printf("Mass Storage, ");
105                         switch(subclass) {
106                                 case US_SC_RBC:
107                                         printf("RBC ");
108                                         break;
109                                 case US_SC_8020:
110                                         printf("SFF-8020i (ATAPI)");
111                                         break;
112                                 case US_SC_QIC:
113                                         printf("QIC-157 (Tape)");
114                                         break;
115                                 case US_SC_UFI:
116                                         printf("UFI");
117                                         break;
118                                 case US_SC_8070:
119                                         printf("SFF-8070");
120                                         break;
121                                 case US_SC_SCSI:
122                                         printf("Transp. SCSI");
123                                         break;
124                                 default:
125                                         printf("reserved");
126                                         break;
127                         }
128                         printf(", ");
129                         switch(proto) {
130                                 case US_PR_CB:
131                                         printf("Command/Bulk");
132                                         break;
133                                 case US_PR_CBI:
134                                         printf("Command/Bulk/Int");
135                                         break;
136                                 case US_PR_BULK:
137                                         printf("Bulk only");
138                                         break;
139                                 default:
140                                         printf("reserved");
141                         }
142                         break;
143                 default:
144                         printf("%s",usb_get_class_desc(dclass));
145         }
146 }
147
148 void usb_display_string(struct usb_device *dev,int index)
149 {
150         char buffer[256];
151         if (index!=0) {
152                 if (usb_string(dev,index,&buffer[0],256)>0);
153                         printf("String: \"%s\"",buffer);
154         }
155 }
156
157 void usb_display_desc(struct usb_device *dev)
158 {
159         if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
160                 printf("%d: %s,  USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
161                         (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
162                 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
163                         printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
164                 if (dev->descriptor.bDeviceClass) {
165                         printf(" - Class: ");
166                         usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
167                         printf("\n");
168                 }
169                 else {
170                         printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
171                 }
172                 printf(" - PacketSize: %d  Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
173                 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         }
175
176 }
177
178 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
179 {
180         printf("   Configuration: %d\n",config->bConfigurationValue);
181         printf("   - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
182         (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
183         if (config->iConfiguration) {
184                 printf("   - ");
185                 usb_display_string(dev,config->iConfiguration);
186                 printf("\n");
187         }
188 }
189
190 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
191 {
192         printf("     Interface: %d\n",ifdesc->bInterfaceNumber);
193         printf("     - Alternate Settings %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
194         printf("     - Class ");
195         usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
196         printf("\n");
197         if (ifdesc->iInterface) {
198                 printf("     - ");
199                 usb_display_string(dev,ifdesc->iInterface);
200                 printf("\n");
201         }
202 }
203
204 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
205 {
206         printf("     - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
207         switch((epdesc->bmAttributes & 0x03))
208         {
209                 case 0: printf("Control"); break;
210                 case 1: printf("Isochronous"); break;
211                 case 2: printf("Bulk"); break;
212                 case 3: printf("Interrupt"); break;
213         }
214         printf(" MaxPacket %d",epdesc->wMaxPacketSize);
215         if ((epdesc->bmAttributes & 0x03)==0x3)
216                 printf(" Interval %dms",epdesc->bInterval);
217         printf("\n");
218 }
219
220 /* main routine to diasplay the configs, interfaces and endpoints */
221 void usb_display_config(struct usb_device *dev)
222 {
223         struct usb_config_descriptor *config;
224         struct usb_interface_descriptor *ifdesc;
225         struct usb_endpoint_descriptor *epdesc;
226         int i,ii;
227
228         config= &dev->config;
229         usb_display_conf_desc(config,dev);
230         for(i=0;i<config->no_of_if;i++) {
231                 ifdesc= &config->if_desc[i];
232                 usb_display_if_desc(ifdesc,dev);
233                 for(ii=0;ii<ifdesc->no_of_ep;ii++) {
234                         epdesc= &ifdesc->ep_desc[ii];
235                         usb_display_ep_desc(epdesc);
236                 }
237         }
238         printf("\n");
239 }
240
241 /* shows the device tree recursively */
242 void usb_show_tree_graph(struct usb_device *dev,char *pre)
243 {
244         int i,index;
245         int has_child,last_child,port;
246
247         index=strlen(pre);
248         printf(" %s",pre);
249         /* check if the device has connected children */
250         has_child=0;
251         for(i=0;i<dev->maxchild;i++) {
252                 if (dev->children[i]!=NULL)
253                         has_child=1;
254         }
255         /* check if we are the last one */
256         last_child=1;
257         if (dev->parent!=NULL) {
258                 for(i=0;i<dev->parent->maxchild;i++) {
259                         /* search for children */
260                         if (dev->parent->children[i]==dev) {
261                                 /* found our pointer, see if we have a little sister */
262                                 port=i;
263                                 while(i++<dev->parent->maxchild) {
264                                         if (dev->parent->children[i]!=NULL) {
265                                                 /* found a sister */
266                                                 last_child=0;
267                                                 break;
268                                         } /* if */
269                                 } /* while */
270                         } /* device found */
271                 } /* for all children of the parent */
272                 printf("\b+-");
273                 /* correct last child */
274                 if (last_child) {
275                         pre[index-1]=' ';
276                 }
277         } /* if not root hub */
278         else
279                 printf(" ");
280         printf("%d ",dev->devnum);
281         pre[index++]=' ';
282         pre[index++]= has_child ? '|' : ' ';
283         pre[index]=0;
284         printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
285                 dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
286         if (strlen(dev->mf) ||
287            strlen(dev->prod) ||
288            strlen(dev->serial))
289                 printf(" %s  %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
290         printf(" %s\n",pre);
291         if (dev->maxchild>0) {
292                 for(i=0;i<dev->maxchild;i++) {
293                         if (dev->children[i]!=NULL) {
294                                 usb_show_tree_graph(dev->children[i],pre);
295                                 pre[index]=0;
296                         }
297                 }
298         }
299 }
300
301 /* main routine for the tree command */
302 void usb_show_tree(struct usb_device *dev)
303 {
304         char preamble[32];
305
306         memset(preamble,0,32);
307         usb_show_tree_graph(dev,&preamble[0]);
308 }
309
310
311 /******************************************************************************
312  * usb boot command intepreter. Derived from diskboot
313  */
314 #ifdef CONFIG_USB_STORAGE
315 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
316 {
317         char *boot_device = NULL;
318         char *ep;
319         int dev, part=0, rcode;
320         ulong cnt;
321         ulong addr;
322         disk_partition_t info;
323         image_header_t *hdr;
324         block_dev_desc_t *stor_dev;
325
326
327         switch (argc) {
328         case 1:
329                 addr = CFG_LOAD_ADDR;
330                 boot_device = getenv ("bootdevice");
331                 break;
332         case 2:
333                 addr = simple_strtoul(argv[1], NULL, 16);
334                 boot_device = getenv ("bootdevice");
335                 break;
336         case 3:
337                 addr = simple_strtoul(argv[1], NULL, 16);
338                 boot_device = argv[2];
339                 break;
340         default:
341                 printf ("Usage:\n%s\n", cmdtp->usage);
342                 return 1;
343         }
344
345         if (!boot_device) {
346                 puts ("\n** No boot device **\n");
347                 return 1;
348         }
349
350         dev = simple_strtoul(boot_device, &ep, 16);
351         stor_dev=usb_stor_get_dev(dev);
352         if (stor_dev->type == DEV_TYPE_UNKNOWN) {
353                 printf ("\n** Device %d not available\n", dev);
354                 return 1;
355         }
356         if (stor_dev->block_read==NULL) {
357                 printf("storage device not initialized. Use usb scan\n");
358                 return 1;
359         }
360         if (*ep) {
361                 if (*ep != ':') {
362                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
363                         return 1;
364                 }
365                 part = simple_strtoul(++ep, NULL, 16);
366         }
367
368         if (get_partition_info (stor_dev, part, &info)) {
369                 /* try to boot raw .... */
370                 strncpy(&info.type[0], BOOT_PART_TYPE, sizeof(BOOT_PART_TYPE));
371                 strncpy(&info.name[0], "Raw", 4);
372                 info.start=0;
373                 info.blksz=0x200;
374                 info.size=2880;
375                 printf("error reading partinfo...try to boot raw\n");
376         }
377         if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
378             (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
379                 printf ("\n** Invalid partition type \"%.32s\""
380                         " (expect \"" BOOT_PART_TYPE "\")\n",
381                         info.type);
382                 return 1;
383         }
384         printf ("\nLoading from USB device %d, partition %d: "
385                 "Name: %.32s  Type: %.32s\n",
386                 dev, part, info.name, info.type);
387
388         printf ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
389                 info.start, info.size, info.blksz);
390
391         if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
392                 printf ("** Read error on %d:%d\n", dev, part);
393                 return 1;
394         }
395
396         hdr = (image_header_t *)addr;
397
398         if (hdr->ih_magic == IH_MAGIC) {
399                 print_image_hdr (hdr);
400                 cnt = (hdr->ih_size + sizeof(image_header_t));
401                 cnt += info.blksz - 1;
402                 cnt /= info.blksz;
403                 cnt -= 1;
404         } else {
405                 printf("\n** Bad Magic Number **\n");
406                 return 1;
407         }
408
409         if (stor_dev->block_read (dev, info.start+1, cnt,
410                       (ulong *)(addr+info.blksz)) != cnt) {
411                 printf ("\n** Read error on %d:%d\n", dev, part);
412                 return 1;
413         }
414         /* Loading ok, update default load address */
415         load_addr = addr;
416
417         flush_cache (addr, (cnt+1)*info.blksz);
418
419         /* Check if we should attempt an auto-start */
420         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
421                 char *local_args[2];
422                 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
423                 local_args[0] = argv[0];
424                 local_args[1] = NULL;
425                 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
426                 rcode=do_bootm (cmdtp, 0, 1, local_args);
427                 return rcode;
428         }
429         return 0;
430 }
431 #endif /* CONFIG_USB_STORAGE */
432
433
434 /*********************************************************************************
435  * usb command intepreter
436  */
437 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
438 {
439
440         int i;
441         struct usb_device *dev = NULL;
442         block_dev_desc_t *stor_dev;
443
444         if ((strncmp(argv[1],"reset",5) == 0) ||
445                  (strncmp(argv[1],"start",5) == 0)){
446                 usb_stop();
447                 printf("(Re)start USB...\n");
448                 usb_init();
449                 return 0;
450         }
451         if (strncmp(argv[1],"stop",4) == 0) {
452 #ifdef CONFIG_USB_KEYBOARD
453                 if (argc==2) {
454                         if (usb_kbd_deregister()!=0) {
455                                 printf("USB not stopped: usbkbd still using USB\n");
456                                 return 1;
457                         }
458                 }
459                 else { /* forced stop, switch console in to serial */
460                         console_assign(stdin,"serial");
461                         usb_kbd_deregister();
462                 }
463 #endif
464                 printf("stopping USB..\n");
465                 usb_stop();
466                 return 0;
467         }
468         if (strncmp(argv[1],"tree",4) == 0) {
469                 printf("\nDevice Tree:\n");
470                 usb_show_tree(usb_get_dev_index(0));
471                 return 0;
472         }
473         if (strncmp(argv[1],"inf",3) == 0) {
474                 int d;
475                 if (argc==2) {
476                         for(d=0;d<USB_MAX_DEVICE;d++) {
477                                 dev=usb_get_dev_index(d);
478                                 if (dev==NULL)
479                                         break;
480                                 usb_display_desc(dev);
481                                 usb_display_config(dev);
482                         }
483                         return 0;
484                 }
485                 else {
486                         int d;
487
488                         i=simple_strtoul(argv[2], NULL, 16);
489                         printf("config for device %d\n",i);
490                         for(d=0;d<USB_MAX_DEVICE;d++) {
491                                 dev=usb_get_dev_index(d);
492                                 if (dev==NULL)
493                                         break;
494                                 if (dev->devnum==i)
495                                         break;
496                         }
497                         if (dev==NULL) {
498                                 printf("*** NO Device avaiable ***\n");
499                                 return 0;
500                         }
501                         else {
502                                 usb_display_desc(dev);
503                                 usb_display_config(dev);
504                         }
505                 }
506                 return 0;
507         }
508 #ifdef CONFIG_USB_STORAGE
509         if (strncmp(argv[1],"scan",4) == 0) {
510                 printf("Scan for storage device:\n");
511                 usb_stor_curr_dev=usb_stor_scan(1);
512                 if (usb_stor_curr_dev==-1) {
513                         printf("No device found. Not initialized?\n");
514                         return 1;
515                 }
516                 return 0;
517         }
518         if (strncmp(argv[1],"part",4) == 0) {
519                 int devno, ok;
520                 for (ok=0, devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
521                         stor_dev=usb_stor_get_dev(devno);
522                         if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
523                                 ok++;
524                                 if (devno)
525                                         printf("\n");
526                                 printf("print_part of %x\n",devno);
527                                 print_part(stor_dev);
528                         }
529                 }
530                 if (!ok) {
531                         printf("\nno USB devices available\n");
532                         return 1;
533                 }
534                 return 0;
535         }
536         if (strcmp(argv[1],"read") == 0) {
537                 if (usb_stor_curr_dev<0) {
538                         printf("no current device selected\n");
539                         return 1;
540                 }
541                 if (argc==5) {
542                         unsigned long addr = simple_strtoul(argv[2], NULL, 16);
543                         unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
544                         unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
545                         unsigned long n;
546                         printf ("\nUSB read: device %d block # %ld, count %ld ... ",
547                                         usb_stor_curr_dev, blk, cnt);
548                         stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
549                         n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
550                         printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
551                         if (n==cnt)
552                                 return 0;
553                         return 1;
554                 }
555         }
556         if (strcmp(argv[1],"dev") == 0) {
557                 if (argc==3) {
558                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
559                         printf ("\nUSB device %d: ", dev);
560                         if (dev >= USB_MAX_STOR_DEV) {
561                                 printf("unknown device\n");
562                                 return 1;
563                         }
564                         printf ("\n    Device %d: ", dev);
565                         stor_dev=usb_stor_get_dev(dev);
566                         dev_print(stor_dev);
567                         if (stor_dev->type == DEV_TYPE_UNKNOWN) {
568                                 return 1;
569                         }
570                         usb_stor_curr_dev = dev;
571                         printf("... is now current device\n");
572                         return 0;
573                 }
574                 else {
575                         printf ("\nUSB device %d: ", usb_stor_curr_dev);
576                         stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
577                         dev_print(stor_dev);
578                         if (stor_dev->type == DEV_TYPE_UNKNOWN) {
579                                 return 1;
580                         }
581                         return 0;
582                 }
583                 return 0;
584         }
585 #endif /* CONFIG_USB_STORAGE */
586         printf ("Usage:\n%s\n", cmdtp->usage);
587         return 1;
588 }
589
590
591 #endif /* (CONFIG_COMMANDS & CFG_CMD_USB) */
592
593
594 #if (CONFIG_COMMANDS & CFG_CMD_USB)
595
596 #ifdef CONFIG_USB_STORAGE
597 U_BOOT_CMD(
598         usb,    5,      1,      do_usb,
599         "usb     - USB sub-system\n",
600         "reset - reset (rescan) USB controller\n"
601         "usb  stop [f]  - stop USB [f]=force stop\n"
602         "usb  tree  - show USB device tree\n"
603         "usb  info [dev] - show available USB devices\n"
604         "usb  scan  - (re-)scan USB bus for storage devices\n"
605         "usb  device [dev] - show or set current USB storage device\n"
606         "usb  part [dev] - print partition table of one or all USB storage devices\n"
607         "usb  read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
608         "     to memory address `addr'\n"
609 );
610
611
612 U_BOOT_CMD(
613         usbboot,        3,      1,      do_usbboot,
614         "usbboot - boot from USB device\n",
615         "loadAddr dev:part\n"
616 );
617
618 #else
619 U_BOOT_CMD(
620         usb,    5,      1,      do_usb,
621         "usb     - USB sub-system\n",
622         "reset - reset (rescan) USB controller\n"
623         "usb  tree  - show USB device tree\n"
624         "usb  info [dev] - show available USB devices\n"
625 );
626 #endif
627 #endif