cmd_usb.c 16 KB

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