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