usb.c 28 KB

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