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