usb.c 28 KB

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