usb.c 27 KB

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