usb.c 27 KB

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