cmd_usb.c 20 KB

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