]>
git.ipfire.org Git - u-boot.git/blob - common/cmd_usb.c
3 * Denis Peter, MPL AG Switzerland
5 * Most of this source has been derived from the Linux USB
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
31 #if (CONFIG_COMMANDS & CFG_CMD_USB)
39 #define CMD_USB_PRINTF(fmt,args...) printf (fmt ,##args)
41 #define CMD_USB_PRINTF(fmt,args...)
43 static int usb_stor_curr_dev
=-1; /* current device */
45 /* somme display routines (info command) */
46 char * usb_get_class_desc(unsigned char dclass
)
49 case USB_CLASS_PER_INTERFACE
:
50 return("See Interface");
54 return("Communication");
56 return("Human Interface");
57 case USB_CLASS_PRINTER
:
59 case USB_CLASS_MASS_STORAGE
:
60 return("Mass Storage");
65 case USB_CLASS_VENDOR_SPEC
:
66 return("Vendor specific");
72 void usb_display_class_sub(unsigned char dclass
,unsigned char subclass
,unsigned char proto
)
75 case USB_CLASS_PER_INTERFACE
:
76 printf("See Interface");
79 printf("Human Interface, Subclass: ");
81 case USB_SUB_HID_NONE
:
84 case USB_SUB_HID_BOOT
:
87 case USB_PROT_HID_NONE
:
90 case USB_PROT_HID_KEYBOARD
:
93 case USB_PROT_HID_MOUSE
:
104 case USB_CLASS_MASS_STORAGE
:
105 printf("Mass Storage, ");
111 printf("SFF-8020i (ATAPI)");
114 printf("QIC-157 (Tape)");
123 printf("Transp. SCSI");
132 printf("Command/Bulk");
135 printf("Command/Bulk/Int");
145 printf("%s",usb_get_class_desc(dclass
));
149 void usb_display_string(struct usb_device
*dev
,int index
)
153 if (usb_string(dev
,index
,&buffer
[0],256)>0);
154 printf("String: \"%s\"",buffer
);
158 void usb_display_desc(struct usb_device
*dev
)
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
);
171 printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev
->config
.if_desc
[0].bInterfaceClass
));
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);
179 void usb_display_conf_desc(struct usb_config_descriptor
*config
,struct usb_device
*dev
)
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
) {
186 usb_display_string(dev
,config
->iConfiguration
);
191 void usb_display_if_desc(struct usb_interface_descriptor
*ifdesc
,struct usb_device
*dev
)
193 printf(" Interface: %d\n",ifdesc
->bInterfaceNumber
);
194 printf(" - Alternate Settings %d, Endpoints: %d\n",ifdesc
->bAlternateSetting
,ifdesc
->bNumEndpoints
);
196 usb_display_class_sub(ifdesc
->bInterfaceClass
,ifdesc
->bInterfaceSubClass
,ifdesc
->bInterfaceProtocol
);
198 if (ifdesc
->iInterface
) {
200 usb_display_string(dev
,ifdesc
->iInterface
);
205 void usb_display_ep_desc(struct usb_endpoint_descriptor
*epdesc
)
207 printf(" - Endpoint %d %s ",epdesc
->bEndpointAddress
& 0xf,(epdesc
->bEndpointAddress
& 0x80) ? "In" : "Out");
208 switch((epdesc
->bmAttributes
& 0x03))
210 case 0: printf("Control"); break;
211 case 1: printf("Isochronous"); break;
212 case 2: printf("Bulk"); break;
213 case 3: printf("Interrupt"); break;
215 printf(" MaxPacket %d",epdesc
->wMaxPacketSize
);
216 if ((epdesc
->bmAttributes
& 0x03)==0x3)
217 printf(" Interval %dms",epdesc
->bInterval
);
221 /* main routine to diasplay the configs, interfaces and endpoints */
222 void usb_display_config(struct usb_device
*dev
)
224 struct usb_config_descriptor
*config
;
225 struct usb_interface_descriptor
*ifdesc
;
226 struct usb_endpoint_descriptor
*epdesc
;
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
);
242 /* shows the device tree recursively */
243 void usb_show_tree_graph(struct usb_device
*dev
,char *pre
)
246 int has_child
,last_child
,port
;
250 /* check if the device has connected children */
252 for(i
=0;i
<dev
->maxchild
;i
++) {
253 if (dev
->children
[i
]!=NULL
)
256 /* check if we are the last one */
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 */
264 while(i
++<dev
->parent
->maxchild
) {
265 if (dev
->parent
->children
[i
]!=NULL
) {
272 } /* for all children of the parent */
274 /* correct last child */
278 } /* if not root hub */
281 printf("%d ",dev
->devnum
);
283 pre
[index
++]= has_child
? '|' : ' ';
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
) ||
290 printf(" %s %s %s %s\n",pre
,dev
->mf
,dev
->prod
,dev
->serial
);
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
);
302 /* main routine for the tree command */
303 void usb_show_tree(struct usb_device
*dev
)
307 memset(preamble
,0,32);
308 usb_show_tree_graph(dev
,&preamble
[0]);
312 /******************************************************************************
313 * usb boot command intepreter. Derived from diskboot
315 #ifdef CONFIG_USB_STORAGE
316 int do_usbboot (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
318 char *boot_device
= NULL
;
320 int dev
, part
=0, rcode
;
323 disk_partition_t info
;
325 block_dev_desc_t
*stor_dev
;
330 addr
= CFG_LOAD_ADDR
;
331 boot_device
= getenv ("bootdevice");
334 addr
= simple_strtoul(argv
[1], NULL
, 16);
335 boot_device
= getenv ("bootdevice");
338 addr
= simple_strtoul(argv
[1], NULL
, 16);
339 boot_device
= argv
[2];
342 printf ("Usage:\n%s\n", cmdtp
->usage
);
347 puts ("\n** No boot device **\n");
351 dev
= simple_strtoul(boot_device
, &ep
, 16);
352 stor_dev
=usb_stor_get_dev(dev
);
353 if (stor_dev
->type
== DEV_TYPE_UNKNOWN
) {
354 printf ("\n** Device %d not available\n", dev
);
357 if (stor_dev
->block_read
==NULL
) {
358 printf("storage device not initialized. Use usb scan\n");
363 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
366 part
= simple_strtoul(++ep
, NULL
, 16);
369 if (get_partition_info (stor_dev
, part
, &info
)) {
370 /* try to boot raw .... */
371 strncpy(&info
.type
[0], BOOT_PART_TYPE
, sizeof(BOOT_PART_TYPE
));
372 strncpy(&info
.name
[0], "Raw", 4);
376 printf("error reading partinfo...try to boot raw\n");
378 if ((strncmp(info
.type
, BOOT_PART_TYPE
, sizeof(info
.type
)) != 0) &&
379 (strncmp(info
.type
, BOOT_PART_COMP
, sizeof(info
.type
)) != 0)) {
380 printf ("\n** Invalid partition type \"%.32s\""
381 " (expect \"" BOOT_PART_TYPE
"\")\n",
385 printf ("\nLoading from USB device %d, partition %d: "
386 "Name: %.32s Type: %.32s\n",
387 dev
, part
, info
.name
, info
.type
);
389 printf ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
390 info
.start
, info
.size
, info
.blksz
);
392 if (stor_dev
->block_read(dev
, info
.start
, 1, (ulong
*)addr
) != 1) {
393 printf ("** Read error on %d:%d\n", dev
, part
);
397 hdr
= (image_header_t
*)addr
;
399 if (hdr
->ih_magic
== IH_MAGIC
) {
400 print_image_hdr (hdr
);
401 cnt
= (hdr
->ih_size
+ sizeof(image_header_t
));
402 cnt
+= info
.blksz
- 1;
406 printf("\n** Bad Magic Number **\n");
410 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
);
415 /* Loading ok, update default load address */
418 flush_cache (addr
, (cnt
+1)*info
.blksz
);
420 /* Check if we should attempt an auto-start */
421 if (((ep
= getenv("autostart")) != NULL
) && (strcmp(ep
,"yes") == 0)) {
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
);
432 #endif /* CONFIG_USB_STORAGE */
435 /*********************************************************************************
436 * usb command intepreter
438 int do_usb (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
442 struct usb_device
*dev
= NULL
;
443 block_dev_desc_t
*stor_dev
;
445 if ((strncmp(argv
[1],"reset",5) == 0) ||
446 (strncmp(argv
[1],"start",5) == 0)){
448 printf("(Re)start USB...\n");
452 if (strncmp(argv
[1],"stop",4) == 0) {
453 #ifdef CONFIG_USB_KEYBOARD
455 if (usb_kbd_deregister()!=0) {
456 printf("USB not stopped: usbkbd still using USB\n");
460 else { /* forced stop, switch console in to serial */
461 console_assign(stdin
,"serial");
462 usb_kbd_deregister();
465 printf("stopping USB..\n");
469 if (strncmp(argv
[1],"tree",4) == 0) {
470 printf("\nDevice Tree:\n");
471 usb_show_tree(usb_get_dev_index(0));
474 if (strncmp(argv
[1],"inf",3) == 0) {
477 for(d
=0;d
<USB_MAX_DEVICE
;d
++) {
478 dev
=usb_get_dev_index(d
);
481 usb_display_desc(dev
);
482 usb_display_config(dev
);
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
);
499 printf("*** NO Device avaiable ***\n");
503 usb_display_desc(dev
);
504 usb_display_config(dev
);
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");
519 if (strncmp(argv
[1],"part",4) == 0) {
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
) {
527 printf("print_part of %x\n",devno
);
528 print_part(stor_dev
);
532 printf("\nno USB devices available\n");
537 if (strcmp(argv
[1],"read") == 0) {
538 if (usb_stor_curr_dev
<0) {
539 printf("no current device selected\n");
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);
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");
557 if (strcmp(argv
[1],"dev") == 0) {
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");
565 printf ("\n Device %d: ", dev
);
566 stor_dev
=usb_stor_get_dev(dev
);
568 if (stor_dev
->type
== DEV_TYPE_UNKNOWN
) {
571 usb_stor_curr_dev
= dev
;
572 printf("... is now current device\n");
576 printf ("\nUSB device %d: ", usb_stor_curr_dev
);
577 stor_dev
=usb_stor_get_dev(usb_stor_curr_dev
);
579 if (stor_dev
->type
== DEV_TYPE_UNKNOWN
) {
586 #endif /* CONFIG_USB_STORAGE */
587 printf ("Usage:\n%s\n", cmdtp
->usage
);
592 #endif /* (CONFIG_COMMANDS & CFG_CMD_USB) */
595 #if (CONFIG_COMMANDS & CFG_CMD_USB)
597 #ifdef CONFIG_USB_STORAGE
598 cmd_tbl_t
U_BOOT_CMD(USB
) = MK_CMD_ENTRY(
600 "usb - USB sub-system\n",
601 "reset - reset (rescan) USB controller\n"
602 "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"
613 cmd_tbl_t
U_BOOT_CMD(USBBOOT
) = MK_CMD_ENTRY(
614 "usbboot", 3, 1, do_usbboot
,
615 "usbboot - boot from USB device\n",
616 "loadAddr dev:part\n"
620 cmd_tbl_t
U_BOOT_CMD(USB
) = MK_CMD_ENTRY(
622 "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"