usb.c 20 KB

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