usb.c 27 KB

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