cmd_usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include <cmd_disk.h>
  32. #undef CMD_USB_DEBUG
  33. #ifdef CMD_USB_DEBUG
  34. #define CMD_USB_PRINTF(fmt,args...) printf (fmt ,##args)
  35. #else
  36. #define CMD_USB_PRINTF(fmt,args...)
  37. #endif
  38. static int usb_stor_curr_dev=-1; /* current device */
  39. /* somme display routines (info command) */
  40. char * usb_get_class_desc(unsigned char dclass)
  41. {
  42. switch(dclass) {
  43. case USB_CLASS_PER_INTERFACE:
  44. return("See Interface");
  45. case USB_CLASS_AUDIO:
  46. return("Audio");
  47. case USB_CLASS_COMM:
  48. return("Communication");
  49. case USB_CLASS_HID:
  50. return("Human Interface");
  51. case USB_CLASS_PRINTER:
  52. return("Printer");
  53. case USB_CLASS_MASS_STORAGE:
  54. return("Mass Storage");
  55. case USB_CLASS_HUB:
  56. return("Hub");
  57. case USB_CLASS_DATA:
  58. return("CDC Data");
  59. case USB_CLASS_VENDOR_SPEC:
  60. return("Vendor specific");
  61. default :
  62. return("");
  63. }
  64. }
  65. void usb_display_class_sub(unsigned char dclass,unsigned char subclass,unsigned char proto)
  66. {
  67. switch(dclass) {
  68. case USB_CLASS_PER_INTERFACE:
  69. printf("See Interface");
  70. break;
  71. case USB_CLASS_HID:
  72. printf("Human Interface, Subclass: ");
  73. switch(subclass) {
  74. case USB_SUB_HID_NONE:
  75. printf("None");
  76. break;
  77. case USB_SUB_HID_BOOT:
  78. printf("Boot ");
  79. switch(proto) {
  80. case USB_PROT_HID_NONE:
  81. printf("None");
  82. break;
  83. case USB_PROT_HID_KEYBOARD:
  84. printf("Keyboard");
  85. break;
  86. case USB_PROT_HID_MOUSE:
  87. printf("Mouse");
  88. break;
  89. default:
  90. printf("reserved");
  91. }
  92. break;
  93. default:
  94. printf("reserved");
  95. }
  96. break;
  97. case USB_CLASS_MASS_STORAGE:
  98. printf("Mass Storage, ");
  99. switch(subclass) {
  100. case US_SC_RBC:
  101. printf("RBC ");
  102. break;
  103. case US_SC_8020:
  104. printf("SFF-8020i (ATAPI)");
  105. break;
  106. case US_SC_QIC:
  107. printf("QIC-157 (Tape)");
  108. break;
  109. case US_SC_UFI:
  110. printf("UFI");
  111. break;
  112. case US_SC_8070:
  113. printf("SFF-8070");
  114. break;
  115. case US_SC_SCSI:
  116. printf("Transp. SCSI");
  117. break;
  118. default:
  119. printf("reserved");
  120. break;
  121. }
  122. printf(", ");
  123. switch(proto) {
  124. case US_PR_CB:
  125. printf("Command/Bulk");
  126. break;
  127. case US_PR_CBI:
  128. printf("Command/Bulk/Int");
  129. break;
  130. case US_PR_BULK:
  131. printf("Bulk only");
  132. break;
  133. default:
  134. printf("reserved");
  135. }
  136. break;
  137. default:
  138. printf("%s",usb_get_class_desc(dclass));
  139. }
  140. }
  141. void 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. void usb_display_desc(struct usb_device *dev)
  150. {
  151. if (dev->descriptor.bDescriptorType==USB_DT_DEVICE) {
  152. printf("%d: %s, USB Revision %x.%x\n",dev->devnum,usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
  153. (dev->descriptor.bcdUSB>>8) & 0xff,dev->descriptor.bcdUSB & 0xff);
  154. if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
  155. printf(" - %s %s %s\n",dev->mf,dev->prod,dev->serial);
  156. if (dev->descriptor.bDeviceClass) {
  157. printf(" - Class: ");
  158. usb_display_class_sub(dev->descriptor.bDeviceClass,dev->descriptor.bDeviceSubClass,dev->descriptor.bDeviceProtocol);
  159. printf("\n");
  160. }
  161. else {
  162. printf(" - Class: (from Interface) %s\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass));
  163. }
  164. printf(" - PacketSize: %d Configurations: %d\n",dev->descriptor.bMaxPacketSize0,dev->descriptor.bNumConfigurations);
  165. 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);
  166. }
  167. }
  168. void usb_display_conf_desc(struct usb_config_descriptor *config,struct usb_device *dev)
  169. {
  170. printf(" Configuration: %d\n",config->bConfigurationValue);
  171. printf(" - Interfaces: %d %s%s%dmA\n",config->bNumInterfaces,(config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
  172. (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",config->MaxPower*2);
  173. if (config->iConfiguration) {
  174. printf(" - ");
  175. usb_display_string(dev,config->iConfiguration);
  176. printf("\n");
  177. }
  178. }
  179. void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,struct usb_device *dev)
  180. {
  181. printf(" Interface: %d\n",ifdesc->bInterfaceNumber);
  182. printf(" - Alternate Settings %d, Endpoints: %d\n",ifdesc->bAlternateSetting,ifdesc->bNumEndpoints);
  183. printf(" - Class ");
  184. usb_display_class_sub(ifdesc->bInterfaceClass,ifdesc->bInterfaceSubClass,ifdesc->bInterfaceProtocol);
  185. printf("\n");
  186. if (ifdesc->iInterface) {
  187. printf(" - ");
  188. usb_display_string(dev,ifdesc->iInterface);
  189. printf("\n");
  190. }
  191. }
  192. void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
  193. {
  194. printf(" - Endpoint %d %s ",epdesc->bEndpointAddress & 0xf,(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
  195. switch((epdesc->bmAttributes & 0x03))
  196. {
  197. case 0: printf("Control"); break;
  198. case 1: printf("Isochronous"); break;
  199. case 2: printf("Bulk"); break;
  200. case 3: printf("Interrupt"); break;
  201. }
  202. printf(" MaxPacket %d",epdesc->wMaxPacketSize);
  203. if ((epdesc->bmAttributes & 0x03)==0x3)
  204. printf(" Interval %dms",epdesc->bInterval);
  205. printf("\n");
  206. }
  207. /* main routine to diasplay the configs, interfaces and endpoints */
  208. void usb_display_config(struct usb_device *dev)
  209. {
  210. struct usb_config_descriptor *config;
  211. struct usb_interface_descriptor *ifdesc;
  212. struct usb_endpoint_descriptor *epdesc;
  213. int i,ii;
  214. config= &dev->config;
  215. usb_display_conf_desc(config,dev);
  216. for(i=0;i<config->no_of_if;i++) {
  217. ifdesc= &config->if_desc[i];
  218. usb_display_if_desc(ifdesc,dev);
  219. for(ii=0;ii<ifdesc->no_of_ep;ii++) {
  220. epdesc= &ifdesc->ep_desc[ii];
  221. usb_display_ep_desc(epdesc);
  222. }
  223. }
  224. printf("\n");
  225. }
  226. /* shows the device tree recursively */
  227. void usb_show_tree_graph(struct usb_device *dev,char *pre)
  228. {
  229. int i,index;
  230. int has_child,last_child,port;
  231. index=strlen(pre);
  232. printf(" %s",pre);
  233. /* check if the device has connected children */
  234. has_child=0;
  235. for(i=0;i<dev->maxchild;i++) {
  236. if (dev->children[i]!=NULL)
  237. has_child=1;
  238. }
  239. /* check if we are the last one */
  240. last_child=1;
  241. if (dev->parent!=NULL) {
  242. for(i=0;i<dev->parent->maxchild;i++) {
  243. /* search for children */
  244. if (dev->parent->children[i]==dev) {
  245. /* found our pointer, see if we have a little sister */
  246. port=i;
  247. while(i++<dev->parent->maxchild) {
  248. if (dev->parent->children[i]!=NULL) {
  249. /* found a sister */
  250. last_child=0;
  251. break;
  252. } /* if */
  253. } /* while */
  254. } /* device found */
  255. } /* for all children of the parent */
  256. printf("\b+-");
  257. /* correct last child */
  258. if (last_child) {
  259. pre[index-1]=' ';
  260. }
  261. } /* if not root hub */
  262. else
  263. printf(" ");
  264. printf("%d ",dev->devnum);
  265. pre[index++]=' ';
  266. pre[index++]= has_child ? '|' : ' ';
  267. pre[index]=0;
  268. printf(" %s (%s, %dmA)\n",usb_get_class_desc(dev->config.if_desc[0].bInterfaceClass),
  269. dev->slow ? "1.5MBit/s" : "12MBit/s",dev->config.MaxPower * 2);
  270. if (strlen(dev->mf) ||
  271. strlen(dev->prod) ||
  272. strlen(dev->serial))
  273. printf(" %s %s %s %s\n",pre,dev->mf,dev->prod,dev->serial);
  274. printf(" %s\n",pre);
  275. if (dev->maxchild>0) {
  276. for(i=0;i<dev->maxchild;i++) {
  277. if (dev->children[i]!=NULL) {
  278. usb_show_tree_graph(dev->children[i],pre);
  279. pre[index]=0;
  280. }
  281. }
  282. }
  283. }
  284. /* main routine for the tree command */
  285. void usb_show_tree(struct usb_device *dev)
  286. {
  287. char preamble[32];
  288. memset(preamble,0,32);
  289. usb_show_tree_graph(dev,&preamble[0]);
  290. }
  291. /******************************************************************************
  292. * usb boot command intepreter. Derived from diskboot
  293. */
  294. #ifdef CONFIG_USB_STORAGE
  295. int do_usbboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  296. {
  297. char *boot_device = NULL;
  298. char *ep;
  299. int dev, part=0, rcode;
  300. ulong cnt;
  301. ulong addr;
  302. disk_partition_t info;
  303. image_header_t *hdr;
  304. block_dev_desc_t *stor_dev;
  305. switch (argc) {
  306. case 1:
  307. addr = CFG_LOAD_ADDR;
  308. boot_device = getenv ("bootdevice");
  309. break;
  310. case 2:
  311. addr = simple_strtoul(argv[1], NULL, 16);
  312. boot_device = getenv ("bootdevice");
  313. break;
  314. case 3:
  315. addr = simple_strtoul(argv[1], NULL, 16);
  316. boot_device = argv[2];
  317. break;
  318. default:
  319. printf ("Usage:\n%s\n", cmdtp->usage);
  320. return 1;
  321. }
  322. if (!boot_device) {
  323. puts ("\n** No boot device **\n");
  324. return 1;
  325. }
  326. dev = simple_strtoul(boot_device, &ep, 16);
  327. stor_dev=usb_stor_get_dev(dev);
  328. if (stor_dev->type == DEV_TYPE_UNKNOWN) {
  329. printf ("\n** Device %d not available\n", dev);
  330. return 1;
  331. }
  332. if (stor_dev->block_read==NULL) {
  333. printf("storage device not initialized. Use usb scan\n");
  334. return 1;
  335. }
  336. if (*ep) {
  337. if (*ep != ':') {
  338. puts ("\n** Invalid boot device, use `dev[:part]' **\n");
  339. return 1;
  340. }
  341. part = simple_strtoul(++ep, NULL, 16);
  342. }
  343. if (get_partition_info (stor_dev, part, &info)) {
  344. /* try to boot raw .... */
  345. strncpy(&info.type[0], BOOT_PART_TYPE, sizeof(BOOT_PART_TYPE));
  346. strncpy(&info.name[0], "Raw", 4);
  347. info.start=0;
  348. info.blksz=0x200;
  349. info.size=2880;
  350. printf("error reading partinfo...try to boot raw\n");
  351. }
  352. if (strncmp(info.type, BOOT_PART_TYPE, 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) */