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