usb.c 27 KB

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