usb.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. ifno = -1;
  310. epno = -1;
  311. curr_if_num = -1;
  312. dev->configno = cfgno;
  313. head = (struct usb_descriptor_header *) &buffer[0];
  314. if (head->bDescriptorType != USB_DT_CONFIG) {
  315. printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
  316. head->bDescriptorType);
  317. return -1;
  318. }
  319. memcpy(&dev->config, buffer, buffer[0]);
  320. le16_to_cpus(&(dev->config.desc.wTotalLength));
  321. dev->config.no_of_if = 0;
  322. index = dev->config.desc.bLength;
  323. /* Ok the first entry must be a configuration entry,
  324. * now process the others */
  325. head = (struct usb_descriptor_header *) &buffer[index];
  326. while (index + 1 < dev->config.desc.wTotalLength) {
  327. switch (head->bDescriptorType) {
  328. case USB_DT_INTERFACE:
  329. if (((struct usb_interface_descriptor *) \
  330. &buffer[index])->bInterfaceNumber != curr_if_num) {
  331. /* this is a new interface, copy new desc */
  332. ifno = dev->config.no_of_if;
  333. dev->config.no_of_if++;
  334. memcpy(&dev->config.if_desc[ifno],
  335. &buffer[index], buffer[index]);
  336. dev->config.if_desc[ifno].no_of_ep = 0;
  337. dev->config.if_desc[ifno].num_altsetting = 1;
  338. curr_if_num =
  339. dev->config.if_desc[ifno].desc.bInterfaceNumber;
  340. } else {
  341. /* found alternate setting for the interface */
  342. dev->config.if_desc[ifno].num_altsetting++;
  343. }
  344. break;
  345. case USB_DT_ENDPOINT:
  346. epno = dev->config.if_desc[ifno].no_of_ep;
  347. /* found an endpoint */
  348. dev->config.if_desc[ifno].no_of_ep++;
  349. memcpy(&dev->config.if_desc[ifno].ep_desc[epno],
  350. &buffer[index], buffer[index]);
  351. ep_wMaxPacketSize = get_unaligned(&dev->config.\
  352. if_desc[ifno].\
  353. ep_desc[epno].\
  354. wMaxPacketSize);
  355. put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
  356. &dev->config.\
  357. if_desc[ifno].\
  358. ep_desc[epno].\
  359. wMaxPacketSize);
  360. debug("if %d, ep %d\n", ifno, epno);
  361. break;
  362. default:
  363. if (head->bLength == 0)
  364. return 1;
  365. debug("unknown Description Type : %x\n",
  366. head->bDescriptorType);
  367. #ifdef DEBUG
  368. {
  369. unsigned char *ch = (unsigned char *)head;
  370. int i;
  371. for (i = 0; i < head->bLength; i++)
  372. debug("%02X ", *ch++);
  373. debug("\n\n\n");
  374. }
  375. #endif
  376. break;
  377. }
  378. index += head->bLength;
  379. head = (struct usb_descriptor_header *)&buffer[index];
  380. }
  381. return 1;
  382. }
  383. /***********************************************************************
  384. * Clears an endpoint
  385. * endp: endpoint number in bits 0-3;
  386. * direction flag in bit 7 (1 = IN, 0 = OUT)
  387. */
  388. int usb_clear_halt(struct usb_device *dev, int pipe)
  389. {
  390. int result;
  391. int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
  392. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  393. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  394. endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
  395. /* don't clear if failed */
  396. if (result < 0)
  397. return result;
  398. /*
  399. * NOTE: we do not get status and verify reset was successful
  400. * as some devices are reported to lock up upon this check..
  401. */
  402. usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  403. /* toggle is reset on clear */
  404. usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
  405. return 0;
  406. }
  407. /**********************************************************************
  408. * get_descriptor type
  409. */
  410. static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  411. unsigned char index, void *buf, int size)
  412. {
  413. int res;
  414. res = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  415. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  416. (type << 8) + index, 0,
  417. buf, size, USB_CNTL_TIMEOUT);
  418. return res;
  419. }
  420. /**********************************************************************
  421. * gets configuration cfgno and store it in the buffer
  422. */
  423. int usb_get_configuration_no(struct usb_device *dev,
  424. unsigned char *buffer, int cfgno)
  425. {
  426. int result;
  427. unsigned int tmp;
  428. struct usb_config_descriptor *config;
  429. config = (struct usb_config_descriptor *)&buffer[0];
  430. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
  431. if (result < 9) {
  432. if (result < 0)
  433. printf("unable to get descriptor, error %lX\n",
  434. dev->status);
  435. else
  436. printf("config descriptor too short " \
  437. "(expected %i, got %i)\n", 9, result);
  438. return -1;
  439. }
  440. tmp = le16_to_cpu(config->wTotalLength);
  441. if (tmp > USB_BUFSIZ) {
  442. printf("usb_get_configuration_no: failed to get " \
  443. "descriptor - too long: %d\n", tmp);
  444. return -1;
  445. }
  446. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, tmp);
  447. debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result, tmp);
  448. return result;
  449. }
  450. /********************************************************************
  451. * set address of a device to the value in dev->devnum.
  452. * This can only be done by addressing the device via the default address (0)
  453. */
  454. static int usb_set_address(struct usb_device *dev)
  455. {
  456. int res;
  457. debug("set address %d\n", dev->devnum);
  458. res = usb_control_msg(dev, usb_snddefctrl(dev),
  459. USB_REQ_SET_ADDRESS, 0,
  460. (dev->devnum), 0,
  461. NULL, 0, USB_CNTL_TIMEOUT);
  462. return res;
  463. }
  464. /********************************************************************
  465. * set interface number to interface
  466. */
  467. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  468. {
  469. struct usb_interface *if_face = NULL;
  470. int ret, i;
  471. for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
  472. if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
  473. if_face = &dev->config.if_desc[i];
  474. break;
  475. }
  476. }
  477. if (!if_face) {
  478. printf("selecting invalid interface %d", interface);
  479. return -1;
  480. }
  481. /*
  482. * We should return now for devices with only one alternate setting.
  483. * According to 9.4.10 of the Universal Serial Bus Specification
  484. * Revision 2.0 such devices can return with a STALL. This results in
  485. * some USB sticks timeouting during initialization and then being
  486. * unusable in U-Boot.
  487. */
  488. if (if_face->num_altsetting == 1)
  489. return 0;
  490. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  491. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  492. alternate, interface, NULL, 0,
  493. USB_CNTL_TIMEOUT * 5);
  494. if (ret < 0)
  495. return ret;
  496. return 0;
  497. }
  498. /********************************************************************
  499. * set configuration number to configuration
  500. */
  501. static int usb_set_configuration(struct usb_device *dev, int configuration)
  502. {
  503. int res;
  504. debug("set configuration %d\n", configuration);
  505. /* set setup command */
  506. res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  507. USB_REQ_SET_CONFIGURATION, 0,
  508. configuration, 0,
  509. NULL, 0, USB_CNTL_TIMEOUT);
  510. if (res == 0) {
  511. dev->toggle[0] = 0;
  512. dev->toggle[1] = 0;
  513. return 0;
  514. } else
  515. return -1;
  516. }
  517. /********************************************************************
  518. * set protocol to protocol
  519. */
  520. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
  521. {
  522. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  523. USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  524. protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  525. }
  526. /********************************************************************
  527. * set idle
  528. */
  529. int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
  530. {
  531. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  532. USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  533. (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  534. }
  535. /********************************************************************
  536. * get report
  537. */
  538. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  539. unsigned char id, void *buf, int size)
  540. {
  541. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  542. USB_REQ_GET_REPORT,
  543. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  544. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  545. }
  546. /********************************************************************
  547. * get class descriptor
  548. */
  549. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  550. unsigned char type, unsigned char id, void *buf, int size)
  551. {
  552. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  553. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  554. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  555. }
  556. /********************************************************************
  557. * get string index in buffer
  558. */
  559. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  560. unsigned char index, void *buf, int size)
  561. {
  562. int i;
  563. int result;
  564. for (i = 0; i < 3; ++i) {
  565. /* some devices are flaky */
  566. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  567. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  568. (USB_DT_STRING << 8) + index, langid, buf, size,
  569. USB_CNTL_TIMEOUT);
  570. if (result > 0)
  571. break;
  572. }
  573. return result;
  574. }
  575. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  576. {
  577. int newlength, oldlength = *length;
  578. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  579. if (!isprint(buf[newlength]) || buf[newlength + 1])
  580. break;
  581. if (newlength > 2) {
  582. buf[0] = newlength;
  583. *length = newlength;
  584. }
  585. }
  586. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  587. unsigned int index, unsigned char *buf)
  588. {
  589. int rc;
  590. /* Try to read the string descriptor by asking for the maximum
  591. * possible number of bytes */
  592. rc = usb_get_string(dev, langid, index, buf, 255);
  593. /* If that failed try to read the descriptor length, then
  594. * ask for just that many bytes */
  595. if (rc < 2) {
  596. rc = usb_get_string(dev, langid, index, buf, 2);
  597. if (rc == 2)
  598. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  599. }
  600. if (rc >= 2) {
  601. if (!buf[0] && !buf[1])
  602. usb_try_string_workarounds(buf, &rc);
  603. /* There might be extra junk at the end of the descriptor */
  604. if (buf[0] < rc)
  605. rc = buf[0];
  606. rc = rc - (rc & 1); /* force a multiple of two */
  607. }
  608. if (rc < 2)
  609. rc = -1;
  610. return rc;
  611. }
  612. /********************************************************************
  613. * usb_string:
  614. * Get string index and translate it to ascii.
  615. * returns string length (> 0) or error (< 0)
  616. */
  617. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  618. {
  619. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
  620. unsigned char *tbuf;
  621. int err;
  622. unsigned int u, idx;
  623. if (size <= 0 || !buf || !index)
  624. return -1;
  625. buf[0] = 0;
  626. tbuf = &mybuf[0];
  627. /* get langid for strings if it's not yet known */
  628. if (!dev->have_langid) {
  629. err = usb_string_sub(dev, 0, 0, tbuf);
  630. if (err < 0) {
  631. debug("error getting string descriptor 0 " \
  632. "(error=%lx)\n", dev->status);
  633. return -1;
  634. } else if (tbuf[0] < 4) {
  635. debug("string descriptor 0 too short\n");
  636. return -1;
  637. } else {
  638. dev->have_langid = -1;
  639. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  640. /* always use the first langid listed */
  641. debug("USB device number %d default " \
  642. "language ID 0x%x\n",
  643. dev->devnum, dev->string_langid);
  644. }
  645. }
  646. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  647. if (err < 0)
  648. return err;
  649. size--; /* leave room for trailing NULL char in output buffer */
  650. for (idx = 0, u = 2; u < err; u += 2) {
  651. if (idx >= size)
  652. break;
  653. if (tbuf[u+1]) /* high byte */
  654. buf[idx++] = '?'; /* non-ASCII character */
  655. else
  656. buf[idx++] = tbuf[u];
  657. }
  658. buf[idx] = 0;
  659. err = idx;
  660. return err;
  661. }
  662. /********************************************************************
  663. * USB device handling:
  664. * the USB device are static allocated [USB_MAX_DEVICE].
  665. */
  666. /* returns a pointer to the device with the index [index].
  667. * if the device is not assigned (dev->devnum==-1) returns NULL
  668. */
  669. struct usb_device *usb_get_dev_index(int index)
  670. {
  671. if (usb_dev[index].devnum == -1)
  672. return NULL;
  673. else
  674. return &usb_dev[index];
  675. }
  676. /* returns a pointer of a new device structure or NULL, if
  677. * no device struct is available
  678. */
  679. struct usb_device *usb_alloc_new_device(void *controller)
  680. {
  681. int i;
  682. debug("New Device %d\n", dev_index);
  683. if (dev_index == USB_MAX_DEVICE) {
  684. printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
  685. return NULL;
  686. }
  687. /* default Address is 0, real addresses start with 1 */
  688. usb_dev[dev_index].devnum = dev_index + 1;
  689. usb_dev[dev_index].maxchild = 0;
  690. for (i = 0; i < USB_MAXCHILDREN; i++)
  691. usb_dev[dev_index].children[i] = NULL;
  692. usb_dev[dev_index].parent = NULL;
  693. usb_dev[dev_index].controller = controller;
  694. dev_index++;
  695. return &usb_dev[dev_index - 1];
  696. }
  697. /*
  698. * Free the newly created device node.
  699. * Called in error cases where configuring a newly attached
  700. * device fails for some reason.
  701. */
  702. void usb_free_device(void)
  703. {
  704. dev_index--;
  705. debug("Freeing device node: %d\n", dev_index);
  706. memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
  707. usb_dev[dev_index].devnum = -1;
  708. }
  709. /*
  710. * By the time we get here, the device has gotten a new device ID
  711. * and is in the default state. We need to identify the thing and
  712. * get the ball rolling..
  713. *
  714. * Returns 0 for success, != 0 for error.
  715. */
  716. int usb_new_device(struct usb_device *dev)
  717. {
  718. int addr, err;
  719. int tmp;
  720. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
  721. /* We still haven't set the Address yet */
  722. addr = dev->devnum;
  723. dev->devnum = 0;
  724. #ifdef CONFIG_LEGACY_USB_INIT_SEQ
  725. /* this is the old and known way of initializing devices, it is
  726. * different than what Windows and Linux are doing. Windows and Linux
  727. * both retrieve 64 bytes while reading the device descriptor
  728. * Several USB stick devices report ERR: CTL_TIMEOUT, caused by an
  729. * invalid header while reading 8 bytes as device descriptor. */
  730. dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */
  731. dev->maxpacketsize = PACKET_SIZE_8;
  732. dev->epmaxpacketin[0] = 8;
  733. dev->epmaxpacketout[0] = 8;
  734. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, tmpbuf, 8);
  735. if (err < 8) {
  736. printf("\n USB device not responding, " \
  737. "giving up (status=%lX)\n", dev->status);
  738. return 1;
  739. }
  740. memcpy(&dev->descriptor, tmpbuf, 8);
  741. #else
  742. /* This is a Windows scheme of initialization sequence, with double
  743. * reset of the device (Linux uses the same sequence)
  744. * Some equipment is said to work only with such init sequence; this
  745. * patch is based on the work by Alan Stern:
  746. * http://sourceforge.net/mailarchive/forum.php?
  747. * thread_id=5729457&forum_id=5398
  748. */
  749. struct usb_device_descriptor *desc;
  750. int port = -1;
  751. struct usb_device *parent = dev->parent;
  752. unsigned short portstatus;
  753. /* send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
  754. * only 18 bytes long, this will terminate with a short packet. But if
  755. * the maxpacket size is 8 or 16 the device may be waiting to transmit
  756. * some more, or keeps on retransmitting the 8 byte header. */
  757. desc = (struct usb_device_descriptor *)tmpbuf;
  758. dev->descriptor.bMaxPacketSize0 = 64; /* Start off at 64 bytes */
  759. /* Default to 64 byte max packet size */
  760. dev->maxpacketsize = PACKET_SIZE_64;
  761. dev->epmaxpacketin[0] = 64;
  762. dev->epmaxpacketout[0] = 64;
  763. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, 64);
  764. if (err < 0) {
  765. debug("usb_new_device: usb_get_descriptor() failed\n");
  766. return 1;
  767. }
  768. dev->descriptor.bMaxPacketSize0 = desc->bMaxPacketSize0;
  769. /* find the port number we're at */
  770. if (parent) {
  771. int j;
  772. for (j = 0; j < parent->maxchild; j++) {
  773. if (parent->children[j] == dev) {
  774. port = j;
  775. break;
  776. }
  777. }
  778. if (port < 0) {
  779. printf("usb_new_device:cannot locate device's port.\n");
  780. return 1;
  781. }
  782. /* reset the port for the second time */
  783. err = hub_port_reset(dev->parent, port, &portstatus);
  784. if (err < 0) {
  785. printf("\n Couldn't reset port %i\n", port);
  786. return 1;
  787. }
  788. }
  789. #endif
  790. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  791. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  792. switch (dev->descriptor.bMaxPacketSize0) {
  793. case 8:
  794. dev->maxpacketsize = PACKET_SIZE_8;
  795. break;
  796. case 16:
  797. dev->maxpacketsize = PACKET_SIZE_16;
  798. break;
  799. case 32:
  800. dev->maxpacketsize = PACKET_SIZE_32;
  801. break;
  802. case 64:
  803. dev->maxpacketsize = PACKET_SIZE_64;
  804. break;
  805. }
  806. dev->devnum = addr;
  807. err = usb_set_address(dev); /* set address */
  808. if (err < 0) {
  809. printf("\n USB device not accepting new address " \
  810. "(error=%lX)\n", dev->status);
  811. return 1;
  812. }
  813. mdelay(10); /* Let the SET_ADDRESS settle */
  814. tmp = sizeof(dev->descriptor);
  815. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  816. tmpbuf, sizeof(dev->descriptor));
  817. if (err < tmp) {
  818. if (err < 0)
  819. printf("unable to get device descriptor (error=%d)\n",
  820. err);
  821. else
  822. printf("USB device descriptor short read " \
  823. "(expected %i, got %i)\n", tmp, err);
  824. return 1;
  825. }
  826. memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
  827. /* correct le values */
  828. le16_to_cpus(&dev->descriptor.bcdUSB);
  829. le16_to_cpus(&dev->descriptor.idVendor);
  830. le16_to_cpus(&dev->descriptor.idProduct);
  831. le16_to_cpus(&dev->descriptor.bcdDevice);
  832. /* only support for one config for now */
  833. err = usb_get_configuration_no(dev, tmpbuf, 0);
  834. if (err < 0) {
  835. printf("usb_new_device: Cannot read configuration, " \
  836. "skipping device %04x:%04x\n",
  837. dev->descriptor.idVendor, dev->descriptor.idProduct);
  838. return -1;
  839. }
  840. usb_parse_config(dev, tmpbuf, 0);
  841. usb_set_maxpacket(dev);
  842. /* we set the default configuration here */
  843. if (usb_set_configuration(dev, dev->config.desc.bConfigurationValue)) {
  844. printf("failed to set default configuration " \
  845. "len %d, status %lX\n", dev->act_len, dev->status);
  846. return -1;
  847. }
  848. debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  849. dev->descriptor.iManufacturer, dev->descriptor.iProduct,
  850. dev->descriptor.iSerialNumber);
  851. memset(dev->mf, 0, sizeof(dev->mf));
  852. memset(dev->prod, 0, sizeof(dev->prod));
  853. memset(dev->serial, 0, sizeof(dev->serial));
  854. if (dev->descriptor.iManufacturer)
  855. usb_string(dev, dev->descriptor.iManufacturer,
  856. dev->mf, sizeof(dev->mf));
  857. if (dev->descriptor.iProduct)
  858. usb_string(dev, dev->descriptor.iProduct,
  859. dev->prod, sizeof(dev->prod));
  860. if (dev->descriptor.iSerialNumber)
  861. usb_string(dev, dev->descriptor.iSerialNumber,
  862. dev->serial, sizeof(dev->serial));
  863. debug("Manufacturer %s\n", dev->mf);
  864. debug("Product %s\n", dev->prod);
  865. debug("SerialNumber %s\n", dev->serial);
  866. /* now prode if the device is a hub */
  867. usb_hub_probe(dev, 0);
  868. return 0;
  869. }
  870. /* EOF */