usb.c 29 KB

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