usb.c 34 KB

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