usb.c 29 KB

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