cmd_usb.c 20 KB

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