usb.c 29 KB

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