usb.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Most of this source has been derived from the Linux USB
  4. * project:
  5. * (C) Copyright Linus Torvalds 1999
  6. * (C) Copyright Johannes Erdfelt 1999-2001
  7. * (C) Copyright Andreas Gal 1999
  8. * (C) Copyright Gregory P. Smith 1999
  9. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  10. * (C) Copyright Randy Dunlap 2000
  11. * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
  12. * (C) Copyright Yggdrasil Computing, Inc. 2000
  13. * (usb_device_id matching changes by Adam J. Richter)
  14. *
  15. * Adapted for U-Boot:
  16. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  17. */
  18. /*
  19. * How it works:
  20. *
  21. * Since this is a bootloader, the devices will not be automatic
  22. * (re)configured on hotplug, but after a restart of the USB the
  23. * device should work.
  24. *
  25. * For each transfer (except "Interrupt") we wait for completion.
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <dm.h>
  30. #include <log.h>
  31. #include <malloc.h>
  32. #include <memalign.h>
  33. #include <asm/processor.h>
  34. #include <linux/compiler.h>
  35. #include <linux/ctype.h>
  36. #include <asm/byteorder.h>
  37. #include <asm/unaligned.h>
  38. #include <errno.h>
  39. #include <usb.h>
  40. #include <linux/delay.h>
  41. #define USB_BUFSIZ 512
  42. static int asynch_allowed;
  43. bool usb_started; /* flag for the started/stopped USB status */
  44. #if !CONFIG_IS_ENABLED(DM_USB)
  45. static struct usb_device usb_dev[USB_MAX_DEVICE];
  46. static int dev_index;
  47. /***************************************************************************
  48. * Init USB Device
  49. */
  50. int usb_init(void)
  51. {
  52. void *ctrl;
  53. struct usb_device *dev;
  54. int i, start_index = 0;
  55. int controllers_initialized = 0;
  56. int ret;
  57. dev_index = 0;
  58. asynch_allowed = 1;
  59. usb_hub_reset();
  60. /* first make all devices unknown */
  61. for (i = 0; i < USB_MAX_DEVICE; i++) {
  62. memset(&usb_dev[i], 0, sizeof(struct usb_device));
  63. usb_dev[i].devnum = -1;
  64. }
  65. /* init low_level USB */
  66. for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  67. /* init low_level USB */
  68. printf("USB%d: ", i);
  69. ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl);
  70. if (ret == -ENODEV) { /* No such device. */
  71. puts("Port not available.\n");
  72. controllers_initialized++;
  73. continue;
  74. }
  75. if (ret) { /* Other error. */
  76. puts("lowlevel init failed\n");
  77. continue;
  78. }
  79. /*
  80. * lowlevel init is OK, now scan the bus for devices
  81. * i.e. search HUBs and configure them
  82. */
  83. controllers_initialized++;
  84. start_index = dev_index;
  85. printf("scanning bus %d for devices... ", i);
  86. ret = usb_alloc_new_device(ctrl, &dev);
  87. if (ret)
  88. break;
  89. /*
  90. * device 0 is always present
  91. * (root hub, so let it analyze)
  92. */
  93. ret = usb_new_device(dev);
  94. if (ret)
  95. usb_free_device(dev->controller);
  96. if (start_index == dev_index) {
  97. puts("No USB Device found\n");
  98. continue;
  99. } else {
  100. printf("%d USB Device(s) found\n",
  101. dev_index - start_index);
  102. }
  103. usb_started = 1;
  104. }
  105. debug("scan end\n");
  106. /* if we were not able to find at least one working bus, bail out */
  107. if (controllers_initialized == 0)
  108. puts("USB error: all controllers failed lowlevel init\n");
  109. return usb_started ? 0 : -ENODEV;
  110. }
  111. /******************************************************************************
  112. * Stop USB this stops the LowLevel Part and deregisters USB devices.
  113. */
  114. int usb_stop(void)
  115. {
  116. int i;
  117. if (usb_started) {
  118. asynch_allowed = 1;
  119. usb_started = 0;
  120. usb_hub_reset();
  121. for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  122. if (usb_lowlevel_stop(i))
  123. printf("failed to stop USB controller %d\n", i);
  124. }
  125. }
  126. return 0;
  127. }
  128. /******************************************************************************
  129. * Detect if a USB device has been plugged or unplugged.
  130. */
  131. int usb_detect_change(void)
  132. {
  133. int i, j;
  134. int change = 0;
  135. for (j = 0; j < USB_MAX_DEVICE; j++) {
  136. for (i = 0; i < usb_dev[j].maxchild; i++) {
  137. struct usb_port_status status;
  138. if (usb_get_port_status(&usb_dev[j], i + 1,
  139. &status) < 0)
  140. /* USB request failed */
  141. continue;
  142. if (le16_to_cpu(status.wPortChange) &
  143. USB_PORT_STAT_C_CONNECTION)
  144. change++;
  145. }
  146. }
  147. return change;
  148. }
  149. /* Lock or unlock async schedule on the controller */
  150. __weak int usb_lock_async(struct usb_device *dev, int lock)
  151. {
  152. return 0;
  153. }
  154. /*
  155. * disables the asynch behaviour of the control message. This is used for data
  156. * transfers that uses the exclusiv access to the control and bulk messages.
  157. * Returns the old value so it can be restored later.
  158. */
  159. int usb_disable_asynch(int disable)
  160. {
  161. int old_value = asynch_allowed;
  162. asynch_allowed = !disable;
  163. return old_value;
  164. }
  165. #endif /* !CONFIG_IS_ENABLED(DM_USB) */
  166. /*-------------------------------------------------------------------
  167. * Message wrappers.
  168. *
  169. */
  170. /*
  171. * submits an Interrupt Message. Some drivers may implement non-blocking
  172. * polling: when non-block is true and the device is not responding return
  173. * -EAGAIN instead of waiting for device to respond.
  174. */
  175. int usb_int_msg(struct usb_device *dev, unsigned long pipe,
  176. void *buffer, int transfer_len, int interval, bool nonblock)
  177. {
  178. return submit_int_msg(dev, pipe, buffer, transfer_len, interval,
  179. nonblock);
  180. }
  181. /*
  182. * submits a control message and waits for comletion (at least timeout * 1ms)
  183. * If timeout is 0, we don't wait for completion (used as example to set and
  184. * clear keyboards LEDs). For data transfers, (storage transfers) we don't
  185. * allow control messages with 0 timeout, by previousely resetting the flag
  186. * asynch_allowed (usb_disable_asynch(1)).
  187. * returns the transferred length if OK or -1 if error. The transferred length
  188. * and the current status are stored in the dev->act_len and dev->status.
  189. */
  190. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  191. unsigned char request, unsigned char requesttype,
  192. unsigned short value, unsigned short index,
  193. void *data, unsigned short size, int timeout)
  194. {
  195. ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
  196. int err;
  197. if ((timeout == 0) && (!asynch_allowed)) {
  198. /* request for a asynch control pipe is not allowed */
  199. return -EINVAL;
  200. }
  201. /* set setup command */
  202. setup_packet->requesttype = requesttype;
  203. setup_packet->request = request;
  204. setup_packet->value = cpu_to_le16(value);
  205. setup_packet->index = cpu_to_le16(index);
  206. setup_packet->length = cpu_to_le16(size);
  207. debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
  208. "value 0x%X index 0x%X length 0x%X\n",
  209. request, requesttype, value, index, size);
  210. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  211. err = submit_control_msg(dev, pipe, data, size, setup_packet);
  212. if (err < 0)
  213. return err;
  214. if (timeout == 0)
  215. return (int)size;
  216. /*
  217. * Wait for status to update until timeout expires, USB driver
  218. * interrupt handler may set the status when the USB operation has
  219. * been completed.
  220. */
  221. while (timeout--) {
  222. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
  223. break;
  224. mdelay(1);
  225. }
  226. if (dev->status)
  227. return -1;
  228. return dev->act_len;
  229. }
  230. /*-------------------------------------------------------------------
  231. * submits bulk message, and waits for completion. returns 0 if Ok or
  232. * negative if Error.
  233. * synchronous behavior
  234. */
  235. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  236. void *data, int len, int *actual_length, int timeout)
  237. {
  238. if (len < 0)
  239. return -EINVAL;
  240. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  241. if (submit_bulk_msg(dev, pipe, data, len) < 0)
  242. return -EIO;
  243. while (timeout--) {
  244. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
  245. break;
  246. mdelay(1);
  247. }
  248. *actual_length = dev->act_len;
  249. if (dev->status == 0)
  250. return 0;
  251. else
  252. return -EIO;
  253. }
  254. /*-------------------------------------------------------------------
  255. * Max Packet stuff
  256. */
  257. /*
  258. * returns the max packet size, depending on the pipe direction and
  259. * the configurations values
  260. */
  261. int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
  262. {
  263. /* direction is out -> use emaxpacket out */
  264. if ((pipe & USB_DIR_IN) == 0)
  265. return dev->epmaxpacketout[((pipe>>15) & 0xf)];
  266. else
  267. return dev->epmaxpacketin[((pipe>>15) & 0xf)];
  268. }
  269. /*
  270. * The routine usb_set_maxpacket_ep() is extracted from the loop of routine
  271. * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
  272. * when it is inlined in 1 single routine. What happens is that the register r3
  273. * is used as loop-count 'i', but gets overwritten later on.
  274. * This is clearly a compiler bug, but it is easier to workaround it here than
  275. * to update the compiler (Occurs with at least several GCC 4.{1,2},x
  276. * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
  277. *
  278. * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5.
  279. */
  280. static void noinline
  281. usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
  282. {
  283. int b;
  284. struct usb_endpoint_descriptor *ep;
  285. u16 ep_wMaxPacketSize;
  286. ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx];
  287. b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  288. ep_wMaxPacketSize = get_unaligned(&ep->wMaxPacketSize);
  289. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  290. USB_ENDPOINT_XFER_CONTROL) {
  291. /* Control => bidirectional */
  292. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  293. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  294. debug("##Control EP epmaxpacketout/in[%d] = %d\n",
  295. b, dev->epmaxpacketin[b]);
  296. } else {
  297. if ((ep->bEndpointAddress & 0x80) == 0) {
  298. /* OUT Endpoint */
  299. if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) {
  300. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  301. debug("##EP epmaxpacketout[%d] = %d\n",
  302. b, dev->epmaxpacketout[b]);
  303. }
  304. } else {
  305. /* IN Endpoint */
  306. if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) {
  307. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  308. debug("##EP epmaxpacketin[%d] = %d\n",
  309. b, dev->epmaxpacketin[b]);
  310. }
  311. } /* if out */
  312. } /* if control */
  313. }
  314. /*
  315. * set the max packed value of all endpoints in the given configuration
  316. */
  317. static int usb_set_maxpacket(struct usb_device *dev)
  318. {
  319. int i, ii;
  320. for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
  321. for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
  322. usb_set_maxpacket_ep(dev, i, ii);
  323. return 0;
  324. }
  325. /*******************************************************************************
  326. * Parse the config, located in buffer, and fills the dev->config structure.
  327. * Note that all little/big endian swapping are done automatically.
  328. * (wTotalLength has already been swapped and sanitized when it was read.)
  329. */
  330. static int usb_parse_config(struct usb_device *dev,
  331. unsigned char *buffer, int cfgno)
  332. {
  333. struct usb_descriptor_header *head;
  334. int index, ifno, epno, curr_if_num;
  335. u16 ep_wMaxPacketSize;
  336. struct usb_interface *if_desc = NULL;
  337. ifno = -1;
  338. epno = -1;
  339. curr_if_num = -1;
  340. dev->configno = cfgno;
  341. head = (struct usb_descriptor_header *) &buffer[0];
  342. if (head->bDescriptorType != USB_DT_CONFIG) {
  343. printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
  344. head->bDescriptorType);
  345. return -EINVAL;
  346. }
  347. if (head->bLength != USB_DT_CONFIG_SIZE) {
  348. printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength);
  349. return -EINVAL;
  350. }
  351. memcpy(&dev->config, head, USB_DT_CONFIG_SIZE);
  352. dev->config.no_of_if = 0;
  353. index = dev->config.desc.bLength;
  354. /* Ok the first entry must be a configuration entry,
  355. * now process the others */
  356. head = (struct usb_descriptor_header *) &buffer[index];
  357. while (index + 1 < dev->config.desc.wTotalLength && head->bLength) {
  358. switch (head->bDescriptorType) {
  359. case USB_DT_INTERFACE:
  360. if (head->bLength != USB_DT_INTERFACE_SIZE) {
  361. printf("ERROR: Invalid USB IF length (%d)\n",
  362. head->bLength);
  363. break;
  364. }
  365. if (index + USB_DT_INTERFACE_SIZE >
  366. dev->config.desc.wTotalLength) {
  367. puts("USB IF descriptor overflowed buffer!\n");
  368. break;
  369. }
  370. if (((struct usb_interface_descriptor *) \
  371. head)->bInterfaceNumber != curr_if_num) {
  372. /* this is a new interface, copy new desc */
  373. ifno = dev->config.no_of_if;
  374. if (ifno >= USB_MAXINTERFACES) {
  375. puts("Too many USB interfaces!\n");
  376. /* try to go on with what we have */
  377. return -EINVAL;
  378. }
  379. if_desc = &dev->config.if_desc[ifno];
  380. dev->config.no_of_if++;
  381. memcpy(if_desc, head,
  382. USB_DT_INTERFACE_SIZE);
  383. if_desc->no_of_ep = 0;
  384. if_desc->num_altsetting = 1;
  385. curr_if_num =
  386. if_desc->desc.bInterfaceNumber;
  387. } else {
  388. /* found alternate setting for the interface */
  389. if (ifno >= 0) {
  390. if_desc = &dev->config.if_desc[ifno];
  391. if_desc->num_altsetting++;
  392. }
  393. }
  394. break;
  395. case USB_DT_ENDPOINT:
  396. if (head->bLength != USB_DT_ENDPOINT_SIZE &&
  397. head->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) {
  398. printf("ERROR: Invalid USB EP length (%d)\n",
  399. head->bLength);
  400. break;
  401. }
  402. if (index + head->bLength >
  403. dev->config.desc.wTotalLength) {
  404. puts("USB EP descriptor overflowed buffer!\n");
  405. break;
  406. }
  407. if (ifno < 0) {
  408. puts("Endpoint descriptor out of order!\n");
  409. break;
  410. }
  411. epno = dev->config.if_desc[ifno].no_of_ep;
  412. if_desc = &dev->config.if_desc[ifno];
  413. if (epno >= USB_MAXENDPOINTS) {
  414. printf("Interface %d has too many endpoints!\n",
  415. if_desc->desc.bInterfaceNumber);
  416. return -EINVAL;
  417. }
  418. /* found an endpoint */
  419. if_desc->no_of_ep++;
  420. memcpy(&if_desc->ep_desc[epno], head,
  421. USB_DT_ENDPOINT_SIZE);
  422. ep_wMaxPacketSize = get_unaligned(&dev->config.\
  423. if_desc[ifno].\
  424. ep_desc[epno].\
  425. wMaxPacketSize);
  426. put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
  427. &dev->config.\
  428. if_desc[ifno].\
  429. ep_desc[epno].\
  430. wMaxPacketSize);
  431. debug("if %d, ep %d\n", ifno, epno);
  432. break;
  433. case USB_DT_SS_ENDPOINT_COMP:
  434. if (head->bLength != USB_DT_SS_EP_COMP_SIZE) {
  435. printf("ERROR: Invalid USB EPC length (%d)\n",
  436. head->bLength);
  437. break;
  438. }
  439. if (index + USB_DT_SS_EP_COMP_SIZE >
  440. dev->config.desc.wTotalLength) {
  441. puts("USB EPC descriptor overflowed buffer!\n");
  442. break;
  443. }
  444. if (ifno < 0 || epno < 0) {
  445. puts("EPC descriptor out of order!\n");
  446. break;
  447. }
  448. if_desc = &dev->config.if_desc[ifno];
  449. memcpy(&if_desc->ss_ep_comp_desc[epno], head,
  450. USB_DT_SS_EP_COMP_SIZE);
  451. break;
  452. default:
  453. if (head->bLength == 0)
  454. return -EINVAL;
  455. debug("unknown Description Type : %x\n",
  456. head->bDescriptorType);
  457. #ifdef DEBUG
  458. {
  459. unsigned char *ch = (unsigned char *)head;
  460. int i;
  461. for (i = 0; i < head->bLength; i++)
  462. debug("%02X ", *ch++);
  463. debug("\n\n\n");
  464. }
  465. #endif
  466. break;
  467. }
  468. index += head->bLength;
  469. head = (struct usb_descriptor_header *)&buffer[index];
  470. }
  471. return 0;
  472. }
  473. /***********************************************************************
  474. * Clears an endpoint
  475. * endp: endpoint number in bits 0-3;
  476. * direction flag in bit 7 (1 = IN, 0 = OUT)
  477. */
  478. int usb_clear_halt(struct usb_device *dev, int pipe)
  479. {
  480. int result;
  481. int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
  482. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  483. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  484. endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
  485. /* don't clear if failed */
  486. if (result < 0)
  487. return result;
  488. /*
  489. * NOTE: we do not get status and verify reset was successful
  490. * as some devices are reported to lock up upon this check..
  491. */
  492. usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  493. /* toggle is reset on clear */
  494. usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
  495. return 0;
  496. }
  497. /**********************************************************************
  498. * get_descriptor type
  499. */
  500. static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  501. unsigned char index, void *buf, int size)
  502. {
  503. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  504. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  505. (type << 8) + index, 0, buf, size,
  506. USB_CNTL_TIMEOUT);
  507. }
  508. /**********************************************************************
  509. * gets len of configuration cfgno
  510. */
  511. int usb_get_configuration_len(struct usb_device *dev, int cfgno)
  512. {
  513. int result;
  514. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 9);
  515. struct usb_config_descriptor *config;
  516. config = (struct usb_config_descriptor *)&buffer[0];
  517. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
  518. if (result < 9) {
  519. if (result < 0)
  520. printf("unable to get descriptor, error %lX\n",
  521. dev->status);
  522. else
  523. printf("config descriptor too short " \
  524. "(expected %i, got %i)\n", 9, result);
  525. return -EIO;
  526. }
  527. return le16_to_cpu(config->wTotalLength);
  528. }
  529. /**********************************************************************
  530. * gets configuration cfgno and store it in the buffer
  531. */
  532. int usb_get_configuration_no(struct usb_device *dev, int cfgno,
  533. unsigned char *buffer, int length)
  534. {
  535. int result;
  536. struct usb_config_descriptor *config;
  537. config = (struct usb_config_descriptor *)&buffer[0];
  538. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length);
  539. debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result,
  540. le16_to_cpu(config->wTotalLength));
  541. config->wTotalLength = result; /* validated, with CPU byte order */
  542. return result;
  543. }
  544. /********************************************************************
  545. * set address of a device to the value in dev->devnum.
  546. * This can only be done by addressing the device via the default address (0)
  547. */
  548. static int usb_set_address(struct usb_device *dev)
  549. {
  550. debug("set address %d\n", dev->devnum);
  551. return usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
  552. 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT);
  553. }
  554. /********************************************************************
  555. * set interface number to interface
  556. */
  557. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  558. {
  559. struct usb_interface *if_face = NULL;
  560. int ret, i;
  561. for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
  562. if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
  563. if_face = &dev->config.if_desc[i];
  564. break;
  565. }
  566. }
  567. if (!if_face) {
  568. printf("selecting invalid interface %d", interface);
  569. return -EINVAL;
  570. }
  571. /*
  572. * We should return now for devices with only one alternate setting.
  573. * According to 9.4.10 of the Universal Serial Bus Specification
  574. * Revision 2.0 such devices can return with a STALL. This results in
  575. * some USB sticks timeouting during initialization and then being
  576. * unusable in U-Boot.
  577. */
  578. if (if_face->num_altsetting == 1)
  579. return 0;
  580. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  581. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  582. alternate, interface, NULL, 0,
  583. USB_CNTL_TIMEOUT * 5);
  584. if (ret < 0)
  585. return ret;
  586. return 0;
  587. }
  588. /********************************************************************
  589. * set configuration number to configuration
  590. */
  591. static int usb_set_configuration(struct usb_device *dev, int configuration)
  592. {
  593. int res;
  594. debug("set configuration %d\n", configuration);
  595. /* set setup command */
  596. res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  597. USB_REQ_SET_CONFIGURATION, 0,
  598. configuration, 0,
  599. NULL, 0, USB_CNTL_TIMEOUT);
  600. if (res == 0) {
  601. dev->toggle[0] = 0;
  602. dev->toggle[1] = 0;
  603. return 0;
  604. } else
  605. return -EIO;
  606. }
  607. /********************************************************************
  608. * set protocol to protocol
  609. */
  610. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
  611. {
  612. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  613. USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  614. protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  615. }
  616. /********************************************************************
  617. * set idle
  618. */
  619. int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
  620. {
  621. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  622. USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  623. (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  624. }
  625. /********************************************************************
  626. * get report
  627. */
  628. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  629. unsigned char id, void *buf, int size)
  630. {
  631. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  632. USB_REQ_GET_REPORT,
  633. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  634. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  635. }
  636. /********************************************************************
  637. * get class descriptor
  638. */
  639. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  640. unsigned char type, unsigned char id, void *buf, int size)
  641. {
  642. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  643. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  644. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  645. }
  646. /********************************************************************
  647. * get string index in buffer
  648. */
  649. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  650. unsigned char index, void *buf, int size)
  651. {
  652. int i;
  653. int result;
  654. for (i = 0; i < 3; ++i) {
  655. /* some devices are flaky */
  656. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  657. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  658. (USB_DT_STRING << 8) + index, langid, buf, size,
  659. USB_CNTL_TIMEOUT);
  660. if (result > 0)
  661. break;
  662. }
  663. return result;
  664. }
  665. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  666. {
  667. int newlength, oldlength = *length;
  668. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  669. if (!isprint(buf[newlength]) || buf[newlength + 1])
  670. break;
  671. if (newlength > 2) {
  672. buf[0] = newlength;
  673. *length = newlength;
  674. }
  675. }
  676. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  677. unsigned int index, unsigned char *buf)
  678. {
  679. int rc;
  680. /* Try to read the string descriptor by asking for the maximum
  681. * possible number of bytes */
  682. rc = usb_get_string(dev, langid, index, buf, 255);
  683. /* If that failed try to read the descriptor length, then
  684. * ask for just that many bytes */
  685. if (rc < 2) {
  686. rc = usb_get_string(dev, langid, index, buf, 2);
  687. if (rc == 2)
  688. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  689. }
  690. if (rc >= 2) {
  691. if (!buf[0] && !buf[1])
  692. usb_try_string_workarounds(buf, &rc);
  693. /* There might be extra junk at the end of the descriptor */
  694. if (buf[0] < rc)
  695. rc = buf[0];
  696. rc = rc - (rc & 1); /* force a multiple of two */
  697. }
  698. if (rc < 2)
  699. rc = -EINVAL;
  700. return rc;
  701. }
  702. /********************************************************************
  703. * usb_string:
  704. * Get string index and translate it to ascii.
  705. * returns string length (> 0) or error (< 0)
  706. */
  707. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  708. {
  709. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
  710. unsigned char *tbuf;
  711. int err;
  712. unsigned int u, idx;
  713. if (size <= 0 || !buf || !index)
  714. return -EINVAL;
  715. buf[0] = 0;
  716. tbuf = &mybuf[0];
  717. /* get langid for strings if it's not yet known */
  718. if (!dev->have_langid) {
  719. err = usb_string_sub(dev, 0, 0, tbuf);
  720. if (err < 0) {
  721. debug("error getting string descriptor 0 " \
  722. "(error=%lx)\n", dev->status);
  723. return -EIO;
  724. } else if (tbuf[0] < 4) {
  725. debug("string descriptor 0 too short\n");
  726. return -EIO;
  727. } else {
  728. dev->have_langid = -1;
  729. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  730. /* always use the first langid listed */
  731. debug("USB device number %d default " \
  732. "language ID 0x%x\n",
  733. dev->devnum, dev->string_langid);
  734. }
  735. }
  736. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  737. if (err < 0)
  738. return err;
  739. size--; /* leave room for trailing NULL char in output buffer */
  740. for (idx = 0, u = 2; u < err; u += 2) {
  741. if (idx >= size)
  742. break;
  743. if (tbuf[u+1]) /* high byte */
  744. buf[idx++] = '?'; /* non-ASCII character */
  745. else
  746. buf[idx++] = tbuf[u];
  747. }
  748. buf[idx] = 0;
  749. err = idx;
  750. return err;
  751. }
  752. /********************************************************************
  753. * USB device handling:
  754. * the USB device are static allocated [USB_MAX_DEVICE].
  755. */
  756. #if !CONFIG_IS_ENABLED(DM_USB)
  757. /* returns a pointer to the device with the index [index].
  758. * if the device is not assigned (dev->devnum==-1) returns NULL
  759. */
  760. struct usb_device *usb_get_dev_index(int index)
  761. {
  762. if (usb_dev[index].devnum == -1)
  763. return NULL;
  764. else
  765. return &usb_dev[index];
  766. }
  767. int usb_alloc_new_device(struct udevice *controller, struct usb_device **devp)
  768. {
  769. int i;
  770. debug("New Device %d\n", dev_index);
  771. if (dev_index == USB_MAX_DEVICE) {
  772. printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
  773. return -ENOSPC;
  774. }
  775. /* default Address is 0, real addresses start with 1 */
  776. usb_dev[dev_index].devnum = dev_index + 1;
  777. usb_dev[dev_index].maxchild = 0;
  778. for (i = 0; i < USB_MAXCHILDREN; i++)
  779. usb_dev[dev_index].children[i] = NULL;
  780. usb_dev[dev_index].parent = NULL;
  781. usb_dev[dev_index].controller = controller;
  782. dev_index++;
  783. *devp = &usb_dev[dev_index - 1];
  784. return 0;
  785. }
  786. /*
  787. * Free the newly created device node.
  788. * Called in error cases where configuring a newly attached
  789. * device fails for some reason.
  790. */
  791. void usb_free_device(struct udevice *controller)
  792. {
  793. dev_index--;
  794. debug("Freeing device node: %d\n", dev_index);
  795. memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
  796. usb_dev[dev_index].devnum = -1;
  797. }
  798. /*
  799. * XHCI issues Enable Slot command and thereafter
  800. * allocates device contexts. Provide a weak alias
  801. * function for the purpose, so that XHCI overrides it
  802. * and EHCI/OHCI just work out of the box.
  803. */
  804. __weak int usb_alloc_device(struct usb_device *udev)
  805. {
  806. return 0;
  807. }
  808. #endif /* !CONFIG_IS_ENABLED(DM_USB) */
  809. static int usb_hub_port_reset(struct usb_device *dev, struct usb_device *hub)
  810. {
  811. if (!hub)
  812. usb_reset_root_port(dev);
  813. return 0;
  814. }
  815. static int get_descriptor_len(struct usb_device *dev, int len, int expect_len)
  816. {
  817. __maybe_unused struct usb_device_descriptor *desc;
  818. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
  819. int err;
  820. desc = (struct usb_device_descriptor *)tmpbuf;
  821. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, len);
  822. if (err < expect_len) {
  823. if (err < 0) {
  824. printf("unable to get device descriptor (error=%d)\n",
  825. err);
  826. return err;
  827. } else {
  828. printf("USB device descriptor short read (expected %i, got %i)\n",
  829. expect_len, err);
  830. return -EIO;
  831. }
  832. }
  833. memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
  834. return 0;
  835. }
  836. static int usb_setup_descriptor(struct usb_device *dev, bool do_read)
  837. {
  838. /*
  839. * This is a Windows scheme of initialization sequence, with double
  840. * reset of the device (Linux uses the same sequence)
  841. * Some equipment is said to work only with such init sequence; this
  842. * patch is based on the work by Alan Stern:
  843. * http://sourceforge.net/mailarchive/forum.php?
  844. * thread_id=5729457&forum_id=5398
  845. */
  846. /*
  847. * send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
  848. * only 18 bytes long, this will terminate with a short packet. But if
  849. * the maxpacket size is 8 or 16 the device may be waiting to transmit
  850. * some more, or keeps on retransmitting the 8 byte header.
  851. */
  852. if (dev->speed == USB_SPEED_LOW) {
  853. dev->descriptor.bMaxPacketSize0 = 8;
  854. dev->maxpacketsize = PACKET_SIZE_8;
  855. } else {
  856. dev->descriptor.bMaxPacketSize0 = 64;
  857. dev->maxpacketsize = PACKET_SIZE_64;
  858. }
  859. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  860. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  861. if (do_read && dev->speed == USB_SPEED_FULL) {
  862. int err;
  863. /*
  864. * Validate we've received only at least 8 bytes, not that
  865. * we've received the entire descriptor. The reasoning is:
  866. * - The code only uses fields in the first 8 bytes, so
  867. * that's all we need to have fetched at this stage.
  868. * - The smallest maxpacket size is 8 bytes. Before we know
  869. * the actual maxpacket the device uses, the USB controller
  870. * may only accept a single packet. Consequently we are only
  871. * guaranteed to receive 1 packet (at least 8 bytes) even in
  872. * a non-error case.
  873. *
  874. * At least the DWC2 controller needs to be programmed with
  875. * the number of packets in addition to the number of bytes.
  876. * A request for 64 bytes of data with the maxpacket guessed
  877. * as 64 (above) yields a request for 1 packet.
  878. */
  879. err = get_descriptor_len(dev, 64, 8);
  880. if (err)
  881. return err;
  882. /*
  883. * Logitech Unifying Receiver 046d:c52b bcdDevice 12.10 seems
  884. * sensitive about the first Get Descriptor request. If there
  885. * are any other requests in the same microframe, the device
  886. * reports bogus data, first of the descriptor parts is not
  887. * sent to the host. Wait over one microframe duration here
  888. * (1mS for USB 1.x , 125uS for USB 2.0) to avoid triggering
  889. * the issue.
  890. */
  891. mdelay(1);
  892. }
  893. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  894. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  895. switch (dev->descriptor.bMaxPacketSize0) {
  896. case 8:
  897. dev->maxpacketsize = PACKET_SIZE_8;
  898. break;
  899. case 16:
  900. dev->maxpacketsize = PACKET_SIZE_16;
  901. break;
  902. case 32:
  903. dev->maxpacketsize = PACKET_SIZE_32;
  904. break;
  905. case 64:
  906. dev->maxpacketsize = PACKET_SIZE_64;
  907. break;
  908. default:
  909. printf("%s: invalid max packet size\n", __func__);
  910. return -EIO;
  911. }
  912. return 0;
  913. }
  914. static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read,
  915. struct usb_device *parent)
  916. {
  917. int err;
  918. /*
  919. * Allocate usb 3.0 device context.
  920. * USB 3.0 (xHCI) protocol tries to allocate device slot
  921. * and related data structures first. This call does that.
  922. * Refer to sec 4.3.2 in xHCI spec rev1.0
  923. */
  924. err = usb_alloc_device(dev);
  925. if (err) {
  926. printf("Cannot allocate device context to get SLOT_ID\n");
  927. return err;
  928. }
  929. err = usb_setup_descriptor(dev, do_read);
  930. if (err)
  931. return err;
  932. err = usb_hub_port_reset(dev, parent);
  933. if (err)
  934. return err;
  935. dev->devnum = addr;
  936. err = usb_set_address(dev); /* set address */
  937. if (err < 0) {
  938. printf("\n USB device not accepting new address " \
  939. "(error=%lX)\n", dev->status);
  940. return err;
  941. }
  942. mdelay(10); /* Let the SET_ADDRESS settle */
  943. /*
  944. * If we haven't read device descriptor before, read it here
  945. * after device is assigned an address. This is only applicable
  946. * to xHCI so far.
  947. */
  948. if (!do_read) {
  949. err = usb_setup_descriptor(dev, true);
  950. if (err)
  951. return err;
  952. }
  953. return 0;
  954. }
  955. int usb_select_config(struct usb_device *dev)
  956. {
  957. unsigned char *tmpbuf = NULL;
  958. int retry = 5;
  959. int err;
  960. err = get_descriptor_len(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE);
  961. if (err)
  962. return err;
  963. /* correct le values */
  964. le16_to_cpus(&dev->descriptor.bcdUSB);
  965. le16_to_cpus(&dev->descriptor.idVendor);
  966. le16_to_cpus(&dev->descriptor.idProduct);
  967. le16_to_cpus(&dev->descriptor.bcdDevice);
  968. do {
  969. /*
  970. * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive
  971. * about this first Get Descriptor request. If there are any other
  972. * requests in the first microframe, the stick crashes. Wait about
  973. * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0).
  974. */
  975. mdelay(1);
  976. /* only support for one config for now */
  977. err = usb_get_configuration_len(dev, 0);
  978. if (err >= 0) {
  979. tmpbuf = (unsigned char *)malloc_cache_aligned(err);
  980. if (!tmpbuf)
  981. err = -ENOMEM;
  982. else
  983. err = usb_get_configuration_no(dev, 0, tmpbuf, err);
  984. }
  985. if (err < 0) {
  986. if (tmpbuf) {
  987. free(tmpbuf);
  988. tmpbuf = NULL;
  989. }
  990. }
  991. } while ((err < 0) && retry--);
  992. if (err < 0) {
  993. printf("usb_new_device: Cannot read configuration, " \
  994. "skipping device %04x:%04x\n",
  995. dev->descriptor.idVendor, dev->descriptor.idProduct);
  996. if (tmpbuf)
  997. free(tmpbuf);
  998. return err;
  999. }
  1000. usb_parse_config(dev, tmpbuf, 0);
  1001. free(tmpbuf);
  1002. usb_set_maxpacket(dev);
  1003. /*
  1004. * we set the default configuration here
  1005. * This seems premature. If the driver wants a different configuration
  1006. * it will need to select itself.
  1007. */
  1008. err = usb_set_configuration(dev, dev->config.desc.bConfigurationValue);
  1009. if (err < 0) {
  1010. printf("failed to set default configuration " \
  1011. "len %d, status %lX\n", dev->act_len, dev->status);
  1012. return err;
  1013. }
  1014. /*
  1015. * Wait until the Set Configuration request gets processed by the
  1016. * device. This is required by at least SanDisk Cruzer Pop USB 2.0
  1017. * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller.
  1018. */
  1019. mdelay(10);
  1020. debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  1021. dev->descriptor.iManufacturer, dev->descriptor.iProduct,
  1022. dev->descriptor.iSerialNumber);
  1023. memset(dev->mf, 0, sizeof(dev->mf));
  1024. memset(dev->prod, 0, sizeof(dev->prod));
  1025. memset(dev->serial, 0, sizeof(dev->serial));
  1026. if (dev->descriptor.iManufacturer)
  1027. usb_string(dev, dev->descriptor.iManufacturer,
  1028. dev->mf, sizeof(dev->mf));
  1029. if (dev->descriptor.iProduct)
  1030. usb_string(dev, dev->descriptor.iProduct,
  1031. dev->prod, sizeof(dev->prod));
  1032. if (dev->descriptor.iSerialNumber)
  1033. usb_string(dev, dev->descriptor.iSerialNumber,
  1034. dev->serial, sizeof(dev->serial));
  1035. debug("Manufacturer %s\n", dev->mf);
  1036. debug("Product %s\n", dev->prod);
  1037. debug("SerialNumber %s\n", dev->serial);
  1038. return 0;
  1039. }
  1040. int usb_setup_device(struct usb_device *dev, bool do_read,
  1041. struct usb_device *parent)
  1042. {
  1043. int addr;
  1044. int ret;
  1045. /* We still haven't set the Address yet */
  1046. addr = dev->devnum;
  1047. dev->devnum = 0;
  1048. ret = usb_prepare_device(dev, addr, do_read, parent);
  1049. if (ret)
  1050. return ret;
  1051. ret = usb_select_config(dev);
  1052. return ret;
  1053. }
  1054. #if !CONFIG_IS_ENABLED(DM_USB)
  1055. /*
  1056. * By the time we get here, the device has gotten a new device ID
  1057. * and is in the default state. We need to identify the thing and
  1058. * get the ball rolling..
  1059. *
  1060. * Returns 0 for success, != 0 for error.
  1061. */
  1062. int usb_new_device(struct usb_device *dev)
  1063. {
  1064. bool do_read = true;
  1065. int err;
  1066. /*
  1067. * XHCI needs to issue a Address device command to setup
  1068. * proper device context structures, before it can interact
  1069. * with the device. So a get_descriptor will fail before any
  1070. * of that is done for XHCI unlike EHCI.
  1071. */
  1072. #ifdef CONFIG_USB_XHCI_HCD
  1073. do_read = false;
  1074. #endif
  1075. err = usb_setup_device(dev, do_read, dev->parent);
  1076. if (err)
  1077. return err;
  1078. /* Now probe if the device is a hub */
  1079. err = usb_hub_probe(dev, 0);
  1080. if (err < 0)
  1081. return err;
  1082. return 0;
  1083. }
  1084. #endif
  1085. __weak
  1086. int board_usb_init(int index, enum usb_init_type init)
  1087. {
  1088. return 0;
  1089. }
  1090. __weak
  1091. int board_usb_cleanup(int index, enum usb_init_type init)
  1092. {
  1093. return 0;
  1094. }
  1095. bool usb_device_has_child_on_port(struct usb_device *parent, int port)
  1096. {
  1097. #if CONFIG_IS_ENABLED(DM_USB)
  1098. return false;
  1099. #else
  1100. return parent->children[port] != NULL;
  1101. #endif
  1102. }
  1103. #if CONFIG_IS_ENABLED(DM_USB)
  1104. void usb_find_usb2_hub_address_port(struct usb_device *udev,
  1105. uint8_t *hub_address, uint8_t *hub_port)
  1106. {
  1107. struct udevice *parent;
  1108. struct usb_device *uparent, *ttdev;
  1109. /*
  1110. * When called from usb-uclass.c: usb_scan_device() udev->dev points
  1111. * to the parent udevice, not the actual udevice belonging to the
  1112. * udev as the device is not instantiated yet. So when searching
  1113. * for the first usb-2 parent start with udev->dev not
  1114. * udev->dev->parent .
  1115. */
  1116. ttdev = udev;
  1117. parent = udev->dev;
  1118. uparent = dev_get_parent_priv(parent);
  1119. while (uparent->speed != USB_SPEED_HIGH) {
  1120. struct udevice *dev = parent;
  1121. if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) {
  1122. printf("Error: Cannot find high speed parent of usb-1 device\n");
  1123. *hub_address = 0;
  1124. *hub_port = 0;
  1125. return;
  1126. }
  1127. ttdev = dev_get_parent_priv(dev);
  1128. parent = dev->parent;
  1129. uparent = dev_get_parent_priv(parent);
  1130. }
  1131. *hub_address = uparent->devnum;
  1132. *hub_port = ttdev->portnr;
  1133. }
  1134. #else
  1135. void usb_find_usb2_hub_address_port(struct usb_device *udev,
  1136. uint8_t *hub_address, uint8_t *hub_port)
  1137. {
  1138. /* Find out the nearest parent which is high speed */
  1139. while (udev->parent->parent != NULL)
  1140. if (udev->parent->speed != USB_SPEED_HIGH) {
  1141. udev = udev->parent;
  1142. } else {
  1143. *hub_address = udev->parent->devnum;
  1144. *hub_port = udev->portnr;
  1145. return;
  1146. }
  1147. printf("Error: Cannot find high speed parent of usb-1 device\n");
  1148. *hub_address = 0;
  1149. *hub_port = 0;
  1150. }
  1151. #endif
  1152. /* EOF */