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