cmd_usb.c 16 KB

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