cmd_usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * Adapted for U-Boot driver model
  6. * (C) Copyright 2015 Google, Inc
  7. *
  8. * Most of this source has been derived from the Linux USB
  9. * project.
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <console.h>
  16. #include <dm.h>
  17. #include <memalign.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/unaligned.h>
  20. #include <part.h>
  21. #include <usb.h>
  22. #ifdef CONFIG_USB_STORAGE
  23. static int usb_stor_curr_dev = -1; /* current device */
  24. #endif
  25. #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
  26. static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
  27. #endif
  28. /* some display routines (info command) */
  29. static char *usb_get_class_desc(unsigned char dclass)
  30. {
  31. switch (dclass) {
  32. case USB_CLASS_PER_INTERFACE:
  33. return "See Interface";
  34. case USB_CLASS_AUDIO:
  35. return "Audio";
  36. case USB_CLASS_COMM:
  37. return "Communication";
  38. case USB_CLASS_HID:
  39. return "Human Interface";
  40. case USB_CLASS_PRINTER:
  41. return "Printer";
  42. case USB_CLASS_MASS_STORAGE:
  43. return "Mass Storage";
  44. case USB_CLASS_HUB:
  45. return "Hub";
  46. case USB_CLASS_DATA:
  47. return "CDC Data";
  48. case USB_CLASS_VENDOR_SPEC:
  49. return "Vendor specific";
  50. default:
  51. return "";
  52. }
  53. }
  54. static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
  55. unsigned char proto)
  56. {
  57. switch (dclass) {
  58. case USB_CLASS_PER_INTERFACE:
  59. printf("See Interface");
  60. break;
  61. case USB_CLASS_HID:
  62. printf("Human Interface, Subclass: ");
  63. switch (subclass) {
  64. case USB_SUB_HID_NONE:
  65. printf("None");
  66. break;
  67. case USB_SUB_HID_BOOT:
  68. printf("Boot ");
  69. switch (proto) {
  70. case USB_PROT_HID_NONE:
  71. printf("None");
  72. break;
  73. case USB_PROT_HID_KEYBOARD:
  74. printf("Keyboard");
  75. break;
  76. case USB_PROT_HID_MOUSE:
  77. printf("Mouse");
  78. break;
  79. default:
  80. printf("reserved");
  81. break;
  82. }
  83. break;
  84. default:
  85. printf("reserved");
  86. break;
  87. }
  88. break;
  89. case USB_CLASS_MASS_STORAGE:
  90. printf("Mass Storage, ");
  91. switch (subclass) {
  92. case US_SC_RBC:
  93. printf("RBC ");
  94. break;
  95. case US_SC_8020:
  96. printf("SFF-8020i (ATAPI)");
  97. break;
  98. case US_SC_QIC:
  99. printf("QIC-157 (Tape)");
  100. break;
  101. case US_SC_UFI:
  102. printf("UFI");
  103. break;
  104. case US_SC_8070:
  105. printf("SFF-8070");
  106. break;
  107. case US_SC_SCSI:
  108. printf("Transp. SCSI");
  109. break;
  110. default:
  111. printf("reserved");
  112. break;
  113. }
  114. printf(", ");
  115. switch (proto) {
  116. case US_PR_CB:
  117. printf("Command/Bulk");
  118. break;
  119. case US_PR_CBI:
  120. printf("Command/Bulk/Int");
  121. break;
  122. case US_PR_BULK:
  123. printf("Bulk only");
  124. break;
  125. default:
  126. printf("reserved");
  127. break;
  128. }
  129. break;
  130. default:
  131. printf("%s", usb_get_class_desc(dclass));
  132. break;
  133. }
  134. }
  135. static void usb_display_string(struct usb_device *dev, int index)
  136. {
  137. ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
  138. if (index != 0) {
  139. if (usb_string(dev, index, &buffer[0], 256) > 0)
  140. printf("String: \"%s\"", buffer);
  141. }
  142. }
  143. static void usb_display_desc(struct usb_device *dev)
  144. {
  145. if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
  146. printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
  147. usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
  148. (dev->descriptor.bcdUSB>>8) & 0xff,
  149. dev->descriptor.bcdUSB & 0xff);
  150. if (strlen(dev->mf) || strlen(dev->prod) ||
  151. strlen(dev->serial))
  152. printf(" - %s %s %s\n", dev->mf, dev->prod,
  153. dev->serial);
  154. if (dev->descriptor.bDeviceClass) {
  155. printf(" - Class: ");
  156. usb_display_class_sub(dev->descriptor.bDeviceClass,
  157. dev->descriptor.bDeviceSubClass,
  158. dev->descriptor.bDeviceProtocol);
  159. printf("\n");
  160. } else {
  161. printf(" - Class: (from Interface) %s\n",
  162. usb_get_class_desc(
  163. dev->config.if_desc[0].desc.bInterfaceClass));
  164. }
  165. printf(" - PacketSize: %d Configurations: %d\n",
  166. dev->descriptor.bMaxPacketSize0,
  167. dev->descriptor.bNumConfigurations);
  168. printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
  169. dev->descriptor.idVendor, dev->descriptor.idProduct,
  170. (dev->descriptor.bcdDevice>>8) & 0xff,
  171. dev->descriptor.bcdDevice & 0xff);
  172. }
  173. }
  174. static void usb_display_conf_desc(struct usb_config_descriptor *config,
  175. struct usb_device *dev)
  176. {
  177. printf(" Configuration: %d\n", config->bConfigurationValue);
  178. printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
  179. (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
  180. (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
  181. config->bMaxPower*2);
  182. if (config->iConfiguration) {
  183. printf(" - ");
  184. usb_display_string(dev, config->iConfiguration);
  185. printf("\n");
  186. }
  187. }
  188. static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
  189. struct usb_device *dev)
  190. {
  191. printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
  192. printf(" - Alternate Setting %d, Endpoints: %d\n",
  193. ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
  194. printf(" - Class ");
  195. usb_display_class_sub(ifdesc->bInterfaceClass,
  196. ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
  197. printf("\n");
  198. if (ifdesc->iInterface) {
  199. printf(" - ");
  200. usb_display_string(dev, ifdesc->iInterface);
  201. printf("\n");
  202. }
  203. }
  204. static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
  205. {
  206. printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
  207. (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
  208. switch ((epdesc->bmAttributes & 0x03)) {
  209. case 0:
  210. printf("Control");
  211. break;
  212. case 1:
  213. printf("Isochronous");
  214. break;
  215. case 2:
  216. printf("Bulk");
  217. break;
  218. case 3:
  219. printf("Interrupt");
  220. break;
  221. }
  222. printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
  223. if ((epdesc->bmAttributes & 0x03) == 0x3)
  224. printf(" Interval %dms", epdesc->bInterval);
  225. printf("\n");
  226. }
  227. /* main routine to diasplay the configs, interfaces and endpoints */
  228. static void usb_display_config(struct usb_device *dev)
  229. {
  230. struct usb_config *config;
  231. struct usb_interface *ifdesc;
  232. struct usb_endpoint_descriptor *epdesc;
  233. int i, ii;
  234. config = &dev->config;
  235. usb_display_conf_desc(&config->desc, dev);
  236. for (i = 0; i < config->no_of_if; i++) {
  237. ifdesc = &config->if_desc[i];
  238. usb_display_if_desc(&ifdesc->desc, dev);
  239. for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
  240. epdesc = &ifdesc->ep_desc[ii];
  241. usb_display_ep_desc(epdesc);
  242. }
  243. }
  244. printf("\n");
  245. }
  246. /*
  247. * With driver model this isn't right since we can have multiple controllers
  248. * and the device numbering starts at 1 on each bus.
  249. * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
  250. */
  251. static struct usb_device *usb_find_device(int devnum)
  252. {
  253. #ifdef CONFIG_DM_USB
  254. struct usb_device *udev;
  255. struct udevice *hub;
  256. struct uclass *uc;
  257. int ret;
  258. /* Device addresses start at 1 */
  259. devnum++;
  260. ret = uclass_get(UCLASS_USB_HUB, &uc);
  261. if (ret)
  262. return NULL;
  263. uclass_foreach_dev(hub, uc) {
  264. struct udevice *dev;
  265. if (!device_active(hub))
  266. continue;
  267. udev = dev_get_parent_priv(hub);
  268. if (udev->devnum == devnum)
  269. return udev;
  270. for (device_find_first_child(hub, &dev);
  271. dev;
  272. device_find_next_child(&dev)) {
  273. if (!device_active(hub))
  274. continue;
  275. udev = dev_get_parent_priv(dev);
  276. if (udev->devnum == devnum)
  277. return udev;
  278. }
  279. }
  280. #else
  281. struct usb_device *udev;
  282. int d;
  283. for (d = 0; d < USB_MAX_DEVICE; d++) {
  284. udev = usb_get_dev_index(d);
  285. if (udev == NULL)
  286. return NULL;
  287. if (udev->devnum == devnum)
  288. return udev;
  289. }
  290. #endif
  291. return NULL;
  292. }
  293. static inline char *portspeed(int speed)
  294. {
  295. char *speed_str;
  296. switch (speed) {
  297. case USB_SPEED_SUPER:
  298. speed_str = "5 Gb/s";
  299. break;
  300. case USB_SPEED_HIGH:
  301. speed_str = "480 Mb/s";
  302. break;
  303. case USB_SPEED_LOW:
  304. speed_str = "1.5 Mb/s";
  305. break;
  306. default:
  307. speed_str = "12 Mb/s";
  308. break;
  309. }
  310. return speed_str;
  311. }
  312. /* shows the device tree recursively */
  313. static void usb_show_tree_graph(struct usb_device *dev, char *pre)
  314. {
  315. int index;
  316. int has_child, last_child;
  317. index = strlen(pre);
  318. printf(" %s", pre);
  319. #ifdef CONFIG_DM_USB
  320. has_child = device_has_active_children(dev->dev);
  321. #else
  322. /* check if the device has connected children */
  323. int i;
  324. has_child = 0;
  325. for (i = 0; i < dev->maxchild; i++) {
  326. if (dev->children[i] != NULL)
  327. has_child = 1;
  328. }
  329. #endif
  330. /* check if we are the last one */
  331. #ifdef CONFIG_DM_USB
  332. /* Not the root of the usb tree? */
  333. if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
  334. last_child = device_is_last_sibling(dev->dev);
  335. #else
  336. if (dev->parent != NULL) { /* not root? */
  337. last_child = 1;
  338. for (i = 0; i < dev->parent->maxchild; i++) {
  339. /* search for children */
  340. if (dev->parent->children[i] == dev) {
  341. /* found our pointer, see if we have a
  342. * little sister
  343. */
  344. while (i++ < dev->parent->maxchild) {
  345. if (dev->parent->children[i] != NULL) {
  346. /* found a sister */
  347. last_child = 0;
  348. break;
  349. } /* if */
  350. } /* while */
  351. } /* device found */
  352. } /* for all children of the parent */
  353. #endif
  354. printf("\b+-");
  355. /* correct last child */
  356. if (last_child && index)
  357. pre[index-1] = ' ';
  358. } /* if not root hub */
  359. else
  360. printf(" ");
  361. printf("%d ", dev->devnum);
  362. pre[index++] = ' ';
  363. pre[index++] = has_child ? '|' : ' ';
  364. pre[index] = 0;
  365. printf(" %s (%s, %dmA)\n", usb_get_class_desc(
  366. dev->config.if_desc[0].desc.bInterfaceClass),
  367. portspeed(dev->speed),
  368. dev->config.desc.bMaxPower * 2);
  369. if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
  370. printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
  371. printf(" %s\n", pre);
  372. #ifdef CONFIG_DM_USB
  373. struct udevice *child;
  374. for (device_find_first_child(dev->dev, &child);
  375. child;
  376. device_find_next_child(&child)) {
  377. struct usb_device *udev;
  378. if (!device_active(child))
  379. continue;
  380. udev = dev_get_parent_priv(child);
  381. /* Ignore emulators, we only want real devices */
  382. if (device_get_uclass_id(child) != UCLASS_USB_EMUL) {
  383. usb_show_tree_graph(udev, pre);
  384. pre[index] = 0;
  385. }
  386. }
  387. #else
  388. if (dev->maxchild > 0) {
  389. for (i = 0; i < dev->maxchild; i++) {
  390. if (dev->children[i] != NULL) {
  391. usb_show_tree_graph(dev->children[i], pre);
  392. pre[index] = 0;
  393. }
  394. }
  395. }
  396. #endif
  397. }
  398. /* main routine for the tree command */
  399. static void usb_show_subtree(struct usb_device *dev)
  400. {
  401. char preamble[32];
  402. memset(preamble, '\0', sizeof(preamble));
  403. usb_show_tree_graph(dev, &preamble[0]);
  404. }
  405. void usb_show_tree(void)
  406. {
  407. #ifdef CONFIG_DM_USB
  408. struct udevice *bus;
  409. for (uclass_first_device(UCLASS_USB, &bus);
  410. bus;
  411. uclass_next_device(&bus)) {
  412. struct usb_device *udev;
  413. struct udevice *dev;
  414. device_find_first_child(bus, &dev);
  415. if (dev && device_active(dev)) {
  416. udev = dev_get_parent_priv(dev);
  417. usb_show_subtree(udev);
  418. }
  419. }
  420. #else
  421. struct usb_device *udev;
  422. int i;
  423. for (i = 0; i < USB_MAX_DEVICE; i++) {
  424. udev = usb_get_dev_index(i);
  425. if (udev == NULL)
  426. break;
  427. if (udev->parent == NULL)
  428. usb_show_subtree(udev);
  429. }
  430. #endif
  431. }
  432. static int usb_test(struct usb_device *dev, int port, char* arg)
  433. {
  434. int mode;
  435. if (port > dev->maxchild) {
  436. printf("Device is no hub or does not have %d ports.\n", port);
  437. return 1;
  438. }
  439. switch (arg[0]) {
  440. case 'J':
  441. case 'j':
  442. printf("Setting Test_J mode");
  443. mode = USB_TEST_MODE_J;
  444. break;
  445. case 'K':
  446. case 'k':
  447. printf("Setting Test_K mode");
  448. mode = USB_TEST_MODE_K;
  449. break;
  450. case 'S':
  451. case 's':
  452. printf("Setting Test_SE0_NAK mode");
  453. mode = USB_TEST_MODE_SE0_NAK;
  454. break;
  455. case 'P':
  456. case 'p':
  457. printf("Setting Test_Packet mode");
  458. mode = USB_TEST_MODE_PACKET;
  459. break;
  460. case 'F':
  461. case 'f':
  462. printf("Setting Test_Force_Enable mode");
  463. mode = USB_TEST_MODE_FORCE_ENABLE;
  464. break;
  465. default:
  466. printf("Unrecognized test mode: %s\nAvailable modes: "
  467. "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
  468. return 1;
  469. }
  470. if (port)
  471. printf(" on downstream facing port %d...\n", port);
  472. else
  473. printf(" on upstream facing port...\n");
  474. if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
  475. port ? USB_RT_PORT : USB_RECIP_DEVICE,
  476. port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
  477. (mode << 8) | port,
  478. NULL, 0, USB_CNTL_TIMEOUT) == -1) {
  479. printf("Error during SET_FEATURE.\n");
  480. return 1;
  481. } else {
  482. printf("Test mode successfully set. Use 'usb start' "
  483. "to return to normal operation.\n");
  484. return 0;
  485. }
  486. }
  487. /******************************************************************************
  488. * usb boot command intepreter. Derived from diskboot
  489. */
  490. #ifdef CONFIG_USB_STORAGE
  491. static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  492. {
  493. return common_diskboot(cmdtp, "usb", argc, argv);
  494. }
  495. #endif /* CONFIG_USB_STORAGE */
  496. static int do_usb_stop_keyboard(int force)
  497. {
  498. #ifdef CONFIG_USB_KEYBOARD
  499. if (usb_kbd_deregister(force) != 0) {
  500. printf("USB not stopped: usbkbd still using USB\n");
  501. return 1;
  502. }
  503. #endif
  504. return 0;
  505. }
  506. static void do_usb_start(void)
  507. {
  508. bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
  509. if (usb_init() < 0)
  510. return;
  511. /* Driver model will probe the devices as they are found */
  512. #ifndef CONFIG_DM_USB
  513. # ifdef CONFIG_USB_STORAGE
  514. /* try to recognize storage devices immediately */
  515. usb_stor_curr_dev = usb_stor_scan(1);
  516. # endif
  517. # ifdef CONFIG_USB_KEYBOARD
  518. drv_usb_kbd_init();
  519. # endif
  520. #endif /* !CONFIG_DM_USB */
  521. #ifdef CONFIG_USB_HOST_ETHER
  522. # ifdef CONFIG_DM_ETH
  523. # ifndef CONFIG_DM_USB
  524. # error "You must use CONFIG_DM_USB if you want to use CONFIG_USB_HOST_ETHER with CONFIG_DM_ETH"
  525. # endif
  526. # else
  527. /* try to recognize ethernet devices immediately */
  528. usb_ether_curr_dev = usb_host_eth_scan(1);
  529. # endif
  530. #endif
  531. }
  532. #ifdef CONFIG_DM_USB
  533. static void show_info(struct udevice *dev)
  534. {
  535. struct udevice *child;
  536. struct usb_device *udev;
  537. udev = dev_get_parent_priv(dev);
  538. usb_display_desc(udev);
  539. usb_display_config(udev);
  540. for (device_find_first_child(dev, &child);
  541. child;
  542. device_find_next_child(&child)) {
  543. if (device_active(child))
  544. show_info(child);
  545. }
  546. }
  547. static int usb_device_info(void)
  548. {
  549. struct udevice *bus;
  550. for (uclass_first_device(UCLASS_USB, &bus);
  551. bus;
  552. uclass_next_device(&bus)) {
  553. struct udevice *hub;
  554. device_find_first_child(bus, &hub);
  555. if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
  556. device_active(hub)) {
  557. show_info(hub);
  558. }
  559. }
  560. return 0;
  561. }
  562. #endif
  563. /******************************************************************************
  564. * usb command intepreter
  565. */
  566. static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  567. {
  568. struct usb_device *udev = NULL;
  569. int i;
  570. extern char usb_started;
  571. #ifdef CONFIG_USB_STORAGE
  572. block_dev_desc_t *stor_dev;
  573. #endif
  574. if (argc < 2)
  575. return CMD_RET_USAGE;
  576. if (strncmp(argv[1], "start", 5) == 0) {
  577. if (usb_started)
  578. return 0; /* Already started */
  579. printf("starting USB...\n");
  580. do_usb_start();
  581. return 0;
  582. }
  583. if (strncmp(argv[1], "reset", 5) == 0) {
  584. printf("resetting USB...\n");
  585. if (do_usb_stop_keyboard(1) != 0)
  586. return 1;
  587. usb_stop();
  588. do_usb_start();
  589. return 0;
  590. }
  591. if (strncmp(argv[1], "stop", 4) == 0) {
  592. if (argc != 2)
  593. console_assign(stdin, "serial");
  594. if (do_usb_stop_keyboard(0) != 0)
  595. return 1;
  596. printf("stopping USB..\n");
  597. usb_stop();
  598. return 0;
  599. }
  600. if (!usb_started) {
  601. printf("USB is stopped. Please issue 'usb start' first.\n");
  602. return 1;
  603. }
  604. if (strncmp(argv[1], "tree", 4) == 0) {
  605. puts("USB device tree:\n");
  606. usb_show_tree();
  607. return 0;
  608. }
  609. if (strncmp(argv[1], "inf", 3) == 0) {
  610. if (argc == 2) {
  611. #ifdef CONFIG_DM_USB
  612. usb_device_info();
  613. #else
  614. int d;
  615. for (d = 0; d < USB_MAX_DEVICE; d++) {
  616. udev = usb_get_dev_index(d);
  617. if (udev == NULL)
  618. break;
  619. usb_display_desc(udev);
  620. usb_display_config(udev);
  621. }
  622. #endif
  623. return 0;
  624. } else {
  625. /*
  626. * With driver model this isn't right since we can
  627. * have multiple controllers and the device numbering
  628. * starts at 1 on each bus.
  629. */
  630. i = simple_strtoul(argv[2], NULL, 10);
  631. printf("config for device %d\n", i);
  632. udev = usb_find_device(i);
  633. if (udev == NULL) {
  634. printf("*** No device available ***\n");
  635. return 0;
  636. } else {
  637. usb_display_desc(udev);
  638. usb_display_config(udev);
  639. }
  640. }
  641. return 0;
  642. }
  643. if (strncmp(argv[1], "test", 4) == 0) {
  644. if (argc < 5)
  645. return CMD_RET_USAGE;
  646. i = simple_strtoul(argv[2], NULL, 10);
  647. udev = usb_find_device(i);
  648. if (udev == NULL) {
  649. printf("Device %d does not exist.\n", i);
  650. return 1;
  651. }
  652. i = simple_strtoul(argv[3], NULL, 10);
  653. return usb_test(udev, i, argv[4]);
  654. }
  655. #ifdef CONFIG_USB_STORAGE
  656. if (strncmp(argv[1], "stor", 4) == 0)
  657. return usb_stor_info();
  658. if (strncmp(argv[1], "part", 4) == 0) {
  659. int devno, ok = 0;
  660. if (argc == 2) {
  661. for (devno = 0; ; ++devno) {
  662. stor_dev = usb_stor_get_dev(devno);
  663. if (stor_dev == NULL)
  664. break;
  665. if (stor_dev->type != DEV_TYPE_UNKNOWN) {
  666. ok++;
  667. if (devno)
  668. printf("\n");
  669. debug("print_part of %x\n", devno);
  670. print_part(stor_dev);
  671. }
  672. }
  673. } else {
  674. devno = simple_strtoul(argv[2], NULL, 16);
  675. stor_dev = usb_stor_get_dev(devno);
  676. if (stor_dev != NULL &&
  677. stor_dev->type != DEV_TYPE_UNKNOWN) {
  678. ok++;
  679. debug("print_part of %x\n", devno);
  680. print_part(stor_dev);
  681. }
  682. }
  683. if (!ok) {
  684. printf("\nno USB devices available\n");
  685. return 1;
  686. }
  687. return 0;
  688. }
  689. if (strcmp(argv[1], "read") == 0) {
  690. if (usb_stor_curr_dev < 0) {
  691. printf("no current device selected\n");
  692. return 1;
  693. }
  694. if (argc == 5) {
  695. unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  696. unsigned long blk = simple_strtoul(argv[3], NULL, 16);
  697. unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
  698. unsigned long n;
  699. printf("\nUSB read: device %d block # %ld, count %ld"
  700. " ... ", usb_stor_curr_dev, blk, cnt);
  701. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  702. n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
  703. (ulong *)addr);
  704. printf("%ld blocks read: %s\n", n,
  705. (n == cnt) ? "OK" : "ERROR");
  706. if (n == cnt)
  707. return 0;
  708. return 1;
  709. }
  710. }
  711. if (strcmp(argv[1], "write") == 0) {
  712. if (usb_stor_curr_dev < 0) {
  713. printf("no current device selected\n");
  714. return 1;
  715. }
  716. if (argc == 5) {
  717. unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  718. unsigned long blk = simple_strtoul(argv[3], NULL, 16);
  719. unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
  720. unsigned long n;
  721. printf("\nUSB write: device %d block # %ld, count %ld"
  722. " ... ", usb_stor_curr_dev, blk, cnt);
  723. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  724. n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
  725. (ulong *)addr);
  726. printf("%ld blocks write: %s\n", n,
  727. (n == cnt) ? "OK" : "ERROR");
  728. if (n == cnt)
  729. return 0;
  730. return 1;
  731. }
  732. }
  733. if (strncmp(argv[1], "dev", 3) == 0) {
  734. if (argc == 3) {
  735. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  736. printf("\nUSB device %d: ", dev);
  737. stor_dev = usb_stor_get_dev(dev);
  738. if (stor_dev == NULL) {
  739. printf("unknown device\n");
  740. return 1;
  741. }
  742. printf("\n Device %d: ", dev);
  743. dev_print(stor_dev);
  744. if (stor_dev->type == DEV_TYPE_UNKNOWN)
  745. return 1;
  746. usb_stor_curr_dev = dev;
  747. printf("... is now current device\n");
  748. return 0;
  749. } else {
  750. printf("\nUSB device %d: ", usb_stor_curr_dev);
  751. stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  752. dev_print(stor_dev);
  753. if (stor_dev->type == DEV_TYPE_UNKNOWN)
  754. return 1;
  755. return 0;
  756. }
  757. return 0;
  758. }
  759. #endif /* CONFIG_USB_STORAGE */
  760. return CMD_RET_USAGE;
  761. }
  762. U_BOOT_CMD(
  763. usb, 5, 1, do_usb,
  764. "USB sub-system",
  765. "start - start (scan) USB controller\n"
  766. "usb reset - reset (rescan) USB controller\n"
  767. "usb stop [f] - stop USB [f]=force stop\n"
  768. "usb tree - show USB device tree\n"
  769. "usb info [dev] - show available USB devices\n"
  770. "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
  771. " (specify port 0 to indicate the device's upstream port)\n"
  772. " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
  773. #ifdef CONFIG_USB_STORAGE
  774. "usb storage - show details of USB storage devices\n"
  775. "usb dev [dev] - show or set current USB storage device\n"
  776. "usb part [dev] - print partition table of one or all USB storage"
  777. " devices\n"
  778. "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
  779. " to memory address `addr'\n"
  780. "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
  781. " from memory address `addr'"
  782. #endif /* CONFIG_USB_STORAGE */
  783. );
  784. #ifdef CONFIG_USB_STORAGE
  785. U_BOOT_CMD(
  786. usbboot, 3, 1, do_usbboot,
  787. "boot from USB device",
  788. "loadAddr dev:part"
  789. );
  790. #endif /* CONFIG_USB_STORAGE */