]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_usb.c
rename CFG_ macros to CONFIG_SYS
[karo-tx-uboot.git] / 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 #include <asm/byteorder.h>
31 #include <part.h>
32 #include <usb.h>
33
34 #ifdef CONFIG_USB_STORAGE
35 static int usb_stor_curr_dev = -1; /* current device */
36 #endif
37
38 /* some display routines (info command) */
39 char * usb_get_class_desc(unsigned char dclass)
40 {
41         switch(dclass) {
42                 case USB_CLASS_PER_INTERFACE:
43                         return("See Interface");
44                 case USB_CLASS_AUDIO:
45                         return("Audio");
46                 case USB_CLASS_COMM:
47                         return("Communication");
48                 case USB_CLASS_HID:
49                         return("Human Interface");
50                 case USB_CLASS_PRINTER:
51                         return("Printer");
52                 case USB_CLASS_MASS_STORAGE:
53                         return("Mass Storage");
54                 case USB_CLASS_HUB:
55                         return("Hub");
56                 case USB_CLASS_DATA:
57                         return("CDC Data");
58                 case USB_CLASS_VENDOR_SPEC:
59                         return("Vendor specific");
60                 default :
61                         return("");
62         }
63 }
64
65 void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
66 {
67         switch(dclass) {
68                 case USB_CLASS_PER_INTERFACE:
69                         printf("See Interface");
70                         break;
71                 case USB_CLASS_HID:
72                         printf("Human Interface, Subclass: ");
73                         switch(subclass) {
74                                 case USB_SUB_HID_NONE:
75                                         printf("None");
76                                         break;
77                                 case USB_SUB_HID_BOOT:
78                                         printf("Boot ");
79                                         switch(proto) {
80                                                 case USB_PROT_HID_NONE:
81                                                         printf("None");
82                                                         break;
83                                                 case USB_PROT_HID_KEYBOARD:
84                                                         printf("Keyboard");
85                                                         break;
86                                                 case USB_PROT_HID_MOUSE:
87                                                         printf("Mouse");
88                                                         break;
89                                                 default:
90                                                         printf("reserved");
91                                         }
92                                         break;
93                                 default:
94                                         printf("reserved");
95                         }
96                         break;
97                 case USB_CLASS_MASS_STORAGE:
98                         printf("Mass Storage, ");
99                         switch(subclass) {
100                                 case US_SC_RBC:
101                                         printf("RBC ");
102                                         break;
103                                 case US_SC_8020:
104                                         printf("SFF-8020i (ATAPI)");
105                                         break;
106                                 case US_SC_QIC:
107                                         printf("QIC-157 (Tape)");
108                                         break;
109                                 case US_SC_UFI:
110                                         printf("UFI");
111                                         break;
112                                 case US_SC_8070:
113                                         printf("SFF-8070");
114                                         break;
115                                 case US_SC_SCSI:
116                                         printf("Transp. SCSI");
117                                         break;
118                                 default:
119                                         printf("reserved");
120                                         break;
121                         }
122                         printf(", ");
123                         switch(proto) {
124                                 case US_PR_CB:
125                                         printf("Command/Bulk");
126                                         break;
127                                 case US_PR_CBI:
128                                         printf("Command/Bulk/Int");
129                                         break;
130                                 case US_PR_BULK:
131                                         printf("Bulk only");
132                                         break;
133                                 default:
134                                         printf("reserved");
135                         }
136                         break;
137                 default:
138                         printf("%s",usb_get_class_desc(dclass));
139         }
140 }
141
142 void usb_display_string(struct usb_device *dev,int index)
143 {
144         char buffer[256];
145         if (index!=0) {
146                 if (usb_string(dev,index,&buffer[0],256)>0);
147                         printf("String: \"%s\"",buffer);
148         }
149 }
150
151 void usb_display_desc(struct usb_device *dev)
152 {
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);
161                         printf("\n");
162                 }
163                 else {
164                         printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
165                 }
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);
168         }
169
170 }
171
172 void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
173 {
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) {
178                 printf("   - ");
179                 usb_display_string(dev,config->iConfiguration);
180                 printf("\n");
181         }
182 }
183
184 void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
185 {
186         printf("     Interface: %d\n",ifdesc->bInterfaceNumber);
187         printf("     - Alternate Setting %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
188         printf("     - Class ");
189         usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
190         printf("\n");
191         if (ifdesc->iInterface) {
192                 printf("     - ");
193                 usb_display_string(dev,ifdesc->iInterface);
194                 printf("\n");
195         }
196 }
197
198 void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
199 {
200         printf("     - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
201         switch((epdesc->bmAttributes & 0x03))
202         {
203                 case 0: printf("Control"); break;
204                 case 1: printf("Isochronous"); break;
205                 case 2: printf("Bulk"); break;
206                 case 3: printf("Interrupt"); break;
207         }
208         printf(" MaxPacket %d",epdesc->wMaxPacketSize);
209         if ((epdesc->bmAttributes & 0x03)==0x3)
210                 printf(" Interval %dms",epdesc->bInterval);
211         printf("\n");
212 }
213
214 /* main routine to diasplay the configs, interfaces and endpoints */
215 void usb_display_config(struct usb_device *dev)
216 {
217         struct usb_config_descriptor *config;
218         struct usb_interface_descriptor *ifdesc;
219         struct usb_endpoint_descriptor *epdesc;
220         int i,ii;
221
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);
230                 }
231         }
232         printf("\n");
233 }
234
235 /* shows the device tree recursively */
236 void usb_show_tree_graph(struct usb_device *dev,char *pre)
237 {
238         int i,index;
239         int has_child,last_child,port;
240
241         index=strlen(pre);
242         printf(" %s",pre);
243         /* check if the device has connected children */
244         has_child=0;
245         for(i=0;i<dev->maxchild;i++) {
246                 if (dev->children[i]!=NULL)
247                         has_child=1;
248         }
249         /* check if we are the last one */
250         last_child=1;
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 */
256                                 port=i;
257                                 while(i++<dev->parent->maxchild) {
258                                         if (dev->parent->children[i]!=NULL) {
259                                                 /* found a sister */
260                                                 last_child=0;
261                                                 break;
262                                         } /* if */
263                                 } /* while */
264                         } /* device found */
265                 } /* for all children of the parent */
266                 printf("\b+-");
267                 /* correct last child */
268                 if (last_child) {
269                         pre[index-1]=' ';
270                 }
271         } /* if not root hub */
272         else
273                 printf(" ");
274         printf("%d ",dev->devnum);
275         pre[index++]=' ';
276         pre[index++]= has_child ? '|' : ' ';
277         pre[index]=0;
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) ||
281            strlen(dev->prod) ||
282            strlen(dev->serial))
283                 printf(" %s  %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
284         printf(" %s\n",pre);
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);
289                                 pre[index]=0;
290                         }
291                 }
292         }
293 }
294
295 /* main routine for the tree command */
296 void usb_show_tree(struct usb_device *dev)
297 {
298         char preamble[32];
299
300         memset(preamble,0,32);
301         usb_show_tree_graph(dev,&preamble[0]);
302 }
303
304
305 /******************************************************************************
306  * usb boot command intepreter. Derived from diskboot
307  */
308 #ifdef CONFIG_USB_STORAGE
309 int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
310 {
311         char *boot_device = NULL;
312         char *ep;
313         int dev, part=1, rcode;
314         ulong addr, cnt;
315         disk_partition_t info;
316         image_header_t *hdr;
317         block_dev_desc_t *stor_dev;
318 #if defined(CONFIG_FIT)
319         const void *fit_hdr = NULL;
320 #endif
321
322         switch (argc) {
323         case 1:
324                 addr = CONFIG_SYS_LOAD_ADDR;
325                 boot_device = getenv ("bootdevice");
326                 break;
327         case 2:
328                 addr = simple_strtoul(argv[1], NULL, 16);
329                 boot_device = getenv ("bootdevice");
330                 break;
331         case 3:
332                 addr = simple_strtoul(argv[1], NULL, 16);
333                 boot_device = argv[2];
334                 break;
335         default:
336                 printf ("Usage:\n%s\n", cmdtp->usage);
337                 return 1;
338         }
339
340         if (!boot_device) {
341                 puts ("\n** No boot device **\n");
342                 return 1;
343         }
344
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);
349                 return 1;
350         }
351         if (stor_dev->block_read==NULL) {
352                 printf("storage device not initialized. Use usb scan\n");
353                 return 1;
354         }
355         if (*ep) {
356                 if (*ep != ':') {
357                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
358                         return 1;
359                 }
360                 part = simple_strtoul(++ep, NULL, 16);
361         }
362
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);
367                 info.start=0;
368                 info.blksz=0x200;
369                 info.size=2880;
370                 printf("error reading partinfo...try to boot raw\n");
371         }
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",
376                         info.type);
377                 return 1;
378         }
379         printf ("\nLoading from USB device %d, partition %d: "
380                 "Name: %.32s  Type: %.32s\n",
381                 dev, part, info.name, info.type);
382
383         debug ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
384                 info.start, info.size, info.blksz);
385
386         if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
387                 printf ("** Read error on %d:%d\n", dev, part);
388                 return 1;
389         }
390
391         switch (genimg_get_format ((void *)addr)) {
392         case IMAGE_FORMAT_LEGACY:
393                 hdr = (image_header_t *)addr;
394
395                 if (!image_check_hcrc (hdr)) {
396                         puts ("\n** Bad Header Checksum **\n");
397                         return 1;
398                 }
399
400                 image_print_contents (hdr);
401
402                 cnt = image_get_image_size (hdr);
403                 break;
404 #if defined(CONFIG_FIT)
405         case IMAGE_FORMAT_FIT:
406                 fit_hdr = (const void *)addr;
407                 puts ("Fit image detected...\n");
408
409                 cnt = fit_get_size (fit_hdr);
410                 break;
411 #endif
412         default:
413                 puts ("** Unknown image type\n");
414                 return 1;
415         }
416
417         cnt += info.blksz - 1;
418         cnt /= info.blksz;
419         cnt -= 1;
420
421         if (stor_dev->block_read (dev, info.start+1, cnt,
422                       (ulong *)(addr+info.blksz)) != cnt) {
423                 printf ("\n** Read error on %d:%d\n", dev, part);
424                 return 1;
425         }
426
427 #if defined(CONFIG_FIT)
428         /* This cannot be done earlier, we need complete FIT image in RAM first */
429         if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
430                 if (!fit_check_format (fit_hdr)) {
431                         puts ("** Bad FIT image format\n");
432                         return 1;
433                 }
434                 fit_print_contents (fit_hdr);
435         }
436 #endif
437
438         /* Loading ok, update default load address */
439         load_addr = addr;
440
441         flush_cache (addr, (cnt+1)*info.blksz);
442
443         /* Check if we should attempt an auto-start */
444         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
445                 char *local_args[2];
446                 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
447                 local_args[0] = argv[0];
448                 local_args[1] = NULL;
449                 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
450                 rcode=do_bootm (cmdtp, 0, 1, local_args);
451                 return rcode;
452         }
453         return 0;
454 }
455 #endif /* CONFIG_USB_STORAGE */
456
457
458 /*********************************************************************************
459  * usb command intepreter
460  */
461 int do_usb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
462 {
463
464         int i;
465         struct usb_device *dev = NULL;
466         extern char usb_started;
467 #ifdef CONFIG_USB_STORAGE
468         block_dev_desc_t *stor_dev;
469 #endif
470
471         if ((strncmp(argv[1], "reset", 5) == 0) ||
472                  (strncmp(argv[1], "start", 5) == 0)){
473                 usb_stop();
474                 printf("(Re)start USB...\n");
475                 i = usb_init();
476 #ifdef CONFIG_USB_STORAGE
477                 /* try to recognize storage devices immediately */
478                 if (i >= 0)
479                         usb_stor_curr_dev = usb_stor_scan(1);
480 #endif
481                 return 0;
482         }
483         if (strncmp(argv[1],"stop",4) == 0) {
484 #ifdef CONFIG_USB_KEYBOARD
485                 if (argc==2) {
486                         if (usb_kbd_deregister()!=0) {
487                                 printf("USB not stopped: usbkbd still using USB\n");
488                                 return 1;
489                         }
490                 }
491                 else { /* forced stop, switch console in to serial */
492                         console_assign(stdin,"serial");
493                         usb_kbd_deregister();
494                 }
495 #endif
496                 printf("stopping USB..\n");
497                 usb_stop();
498                 return 0;
499         }
500         if (!usb_started) {
501                 printf("USB is stopped. Please issue 'usb start' first.\n");
502                 return 1;
503         }
504         if (strncmp(argv[1],"tree",4) == 0) {
505                 printf("\nDevice Tree:\n");
506                 usb_show_tree(usb_get_dev_index(0));
507                 return 0;
508         }
509         if (strncmp(argv[1],"inf",3) == 0) {
510                 int d;
511                 if (argc==2) {
512                         for(d=0;d<USB_MAX_DEVICE;d++) {
513                                 dev=usb_get_dev_index(d);
514                                 if (dev==NULL)
515                                         break;
516                                 usb_display_desc(dev);
517                                 usb_display_config(dev);
518                         }
519                         return 0;
520                 }
521                 else {
522                         int d;
523
524                         i=simple_strtoul(argv[2], NULL, 16);
525                         printf("config for device %d\n",i);
526                         for(d=0;d<USB_MAX_DEVICE;d++) {
527                                 dev=usb_get_dev_index(d);
528                                 if (dev==NULL)
529                                         break;
530                                 if (dev->devnum==i)
531                                         break;
532                         }
533                         if (dev==NULL) {
534                                 printf("*** NO Device avaiable ***\n");
535                                 return 0;
536                         }
537                         else {
538                                 usb_display_desc(dev);
539                                 usb_display_config(dev);
540                         }
541                 }
542                 return 0;
543         }
544 #ifdef CONFIG_USB_STORAGE
545         if (strncmp(argv[1], "scan", 4) == 0) {
546                 printf("  NOTE: this command is obsolete and will be phased out\n");
547                 printf("  please use 'usb storage' for USB storage devices information\n\n");
548                 usb_stor_info();
549                 return 0;
550         }
551
552         if (strncmp(argv[1], "stor", 4) == 0) {
553                 return usb_stor_info();
554         }
555
556         if (strncmp(argv[1],"part",4) == 0) {
557                 int devno, ok = 0;
558                 if (argc==2) {
559                         for (devno=0; devno<USB_MAX_STOR_DEV; ++devno) {
560                                 stor_dev=usb_stor_get_dev(devno);
561                                 if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
562                                         ok++;
563                                         if (devno)
564                                                 printf("\n");
565                                         printf("print_part of %x\n",devno);
566                                         print_part(stor_dev);
567                                 }
568                         }
569                 }
570                 else {
571                         devno=simple_strtoul(argv[2], NULL, 16);
572                         stor_dev=usb_stor_get_dev(devno);
573                         if (stor_dev->type!=DEV_TYPE_UNKNOWN) {
574                                 ok++;
575                                 printf("print_part of %x\n",devno);
576                                 print_part(stor_dev);
577                         }
578                 }
579                 if (!ok) {
580                         printf("\nno USB devices available\n");
581                         return 1;
582                 }
583                 return 0;
584         }
585         if (strcmp(argv[1],"read") == 0) {
586                 if (usb_stor_curr_dev<0) {
587                         printf("no current device selected\n");
588                         return 1;
589                 }
590                 if (argc==5) {
591                         unsigned long addr = simple_strtoul(argv[2], NULL, 16);
592                         unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
593                         unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
594                         unsigned long n;
595                         printf ("\nUSB read: device %d block # %ld, count %ld ... ",
596                                         usb_stor_curr_dev, blk, cnt);
597                         stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
598                         n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt, (ulong *)addr);
599                         printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
600                         if (n==cnt)
601                                 return 0;
602                         return 1;
603                 }
604         }
605         if (strncmp(argv[1], "dev", 3) == 0) {
606                 if (argc == 3) {
607                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
608                         printf ("\nUSB device %d: ", dev);
609                         if (dev >= USB_MAX_STOR_DEV) {
610                                 printf("unknown device\n");
611                                 return 1;
612                         }
613                         printf ("\n    Device %d: ", dev);
614                         stor_dev=usb_stor_get_dev(dev);
615                         dev_print(stor_dev);
616                         if (stor_dev->type == DEV_TYPE_UNKNOWN) {
617                                 return 1;
618                         }
619                         usb_stor_curr_dev = dev;
620                         printf("... is now current device\n");
621                         return 0;
622                 }
623                 else {
624                         printf ("\nUSB device %d: ", usb_stor_curr_dev);
625                         stor_dev=usb_stor_get_dev(usb_stor_curr_dev);
626                         dev_print(stor_dev);
627                         if (stor_dev->type == DEV_TYPE_UNKNOWN) {
628                                 return 1;
629                         }
630                         return 0;
631                 }
632                 return 0;
633         }
634 #endif /* CONFIG_USB_STORAGE */
635         printf ("Usage:\n%s\n", cmdtp->usage);
636         return 1;
637 }
638
639 #ifdef CONFIG_USB_STORAGE
640 U_BOOT_CMD(
641         usb,    5,      1,      do_usb,
642         "usb     - USB sub-system\n",
643         "reset - reset (rescan) USB controller\n"
644         "usb stop [f]  - stop USB [f]=force stop\n"
645         "usb tree  - show USB device tree\n"
646         "usb info [dev] - show available USB devices\n"
647         "usb storage  - show details of USB storage devices\n"
648         "usb dev [dev] - show or set current USB storage device\n"
649         "usb part [dev] - print partition table of one or all USB storage devices\n"
650         "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
651         "    to memory address `addr'\n"
652 );
653
654
655 U_BOOT_CMD(
656         usbboot,        3,      1,      do_usbboot,
657         "usbboot - boot from USB device\n",
658         "loadAddr dev:part\n"
659 );
660
661 #else
662 U_BOOT_CMD(
663         usb,    5,      1,      do_usb,
664         "usb     - USB sub-system\n",
665         "reset - reset (rescan) USB controller\n"
666         "usb  tree  - show USB device tree\n"
667         "usb  info [dev] - show available USB devices\n"
668 );
669 #endif