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