Firmware for MNT ZZ9000 graphics and ARM coprocessor card for Amiga computers.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb.c 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. // SPDX-License-Identifier: GPL-2.0+
  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. /*
  19. * How it works:
  20. *
  21. * Since this is a bootloader, the devices will not be automatic
  22. * (re)configured on hotplug, but after a restart of the USB the
  23. * device should work.
  24. *
  25. * For each transfer (except "Interrupt") we wait for completion.
  26. */
  27. #include "memalign.h"
  28. #include <ctype.h>
  29. #include <asm/byteorder.h>
  30. #include <errno.h>
  31. #include "usb.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34. static inline void *malloc_cache_aligned(size_t size)
  35. {
  36. return memalign(ARCH_DMA_MINALIGN, ALIGN(size, ARCH_DMA_MINALIGN));
  37. }
  38. #define USB_BUFSIZ 512
  39. static int asynch_allowed;
  40. char usb_started; /* flag for the started/stopped USB status */
  41. static struct usb_device usb_dev[USB_MAX_DEVICE];
  42. static int dev_index;
  43. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  44. /***************************************************************************
  45. * Init USB Device
  46. */
  47. int usb_init(void)
  48. {
  49. void *ctrl;
  50. struct usb_device *dev;
  51. int i, start_index = 0;
  52. int controllers_initialized = 0;
  53. int ret;
  54. dev_index = 0;
  55. asynch_allowed = 1;
  56. usb_hub_reset();
  57. /* first make all devices unknown */
  58. for (i = 0; i < USB_MAX_DEVICE; i++) {
  59. memset(&usb_dev[i], 0, sizeof(struct usb_device));
  60. usb_dev[i].devnum = -1;
  61. }
  62. /* init low_level USB */
  63. for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  64. /* init low_level USB */
  65. printf("USB%d: ", i);
  66. ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl);
  67. if (ret == -ENODEV) { /* No such device. */
  68. puts("Port not available.\n");
  69. controllers_initialized++;
  70. continue;
  71. }
  72. if (ret) { /* Other error. */
  73. puts("lowlevel init failed\n");
  74. continue;
  75. }
  76. /*
  77. * lowlevel init is OK, now scan the bus for devices
  78. * i.e. search HUBs and configure them
  79. */
  80. controllers_initialized++;
  81. start_index = dev_index;
  82. printf("[usb] scanning bus %d for devices... ", i);
  83. ret = usb_alloc_new_device(ctrl, &dev);
  84. if (ret)
  85. break;
  86. /*
  87. * device 0 is always present
  88. * (root hub, so let it analyze)
  89. */
  90. ret = usb_new_device(dev);
  91. if (ret)
  92. usb_free_device(dev->controller);
  93. if (start_index == dev_index) {
  94. puts("[usb] No USB Device found\n");
  95. continue;
  96. } else {
  97. printf("[usb] %d USB Device(s) found\n",
  98. dev_index - start_index);
  99. }
  100. usb_started = 1;
  101. }
  102. /* if we were not able to find at least one working bus, bail out */
  103. if (controllers_initialized == 0)
  104. puts("USB error: all controllers failed lowlevel init\n");
  105. return usb_started ? 0 : -ENODEV;
  106. }
  107. /******************************************************************************
  108. * Stop USB this stops the LowLevel Part and deregisters USB devices.
  109. */
  110. int usb_stop(void)
  111. {
  112. int i;
  113. if (usb_started) {
  114. asynch_allowed = 1;
  115. usb_started = 0;
  116. usb_hub_reset();
  117. for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  118. if (usb_lowlevel_stop(i))
  119. printf("failed to stop USB controller %d\n", i);
  120. }
  121. }
  122. return 0;
  123. }
  124. /******************************************************************************
  125. * Detect if a USB device has been plugged or unplugged.
  126. */
  127. int usb_detect_change(void)
  128. {
  129. int i, j;
  130. int change = 0;
  131. for (j = 0; j < USB_MAX_DEVICE; j++) {
  132. for (i = 0; i < usb_dev[j].maxchild; i++) {
  133. struct usb_port_status status;
  134. if (usb_get_port_status(&usb_dev[j], i + 1,
  135. &status) < 0)
  136. /* USB request failed */
  137. continue;
  138. if (le16_to_cpu(status.wPortChange) &
  139. USB_PORT_STAT_C_CONNECTION)
  140. change++;
  141. }
  142. }
  143. return change;
  144. }
  145. /*
  146. * disables the asynch behaviour of the control message. This is used for data
  147. * transfers that uses the exclusiv access to the control and bulk messages.
  148. * Returns the old value so it can be restored later.
  149. */
  150. int usb_disable_asynch(int disable)
  151. {
  152. int old_value = asynch_allowed;
  153. asynch_allowed = !disable;
  154. return old_value;
  155. }
  156. /*-------------------------------------------------------------------
  157. * Message wrappers.
  158. *
  159. */
  160. /*
  161. * submits an Interrupt Message
  162. */
  163. int usb_int_msg(struct usb_device *dev, unsigned long pipe,
  164. void *buffer, int transfer_len, int interval)
  165. {
  166. return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
  167. }
  168. /*
  169. * submits a control message and waits for completion (at least timeout * 1ms)
  170. * If timeout is 0, we don't wait for completion (used as example to set and
  171. * clear keyboards LEDs). For data transfers, (storage transfers) we don't
  172. * allow control messages with 0 timeout, by previously resetting the flag
  173. * asynch_allowed (usb_disable_asynch(1)).
  174. * returns the transferred length if OK or -1 if error. The transferred length
  175. * and the current status are stored in the dev->act_len and dev->status.
  176. */
  177. int usb_control_msg(struct usb_device *dev, unsigned int pipe,
  178. unsigned char request, unsigned char requesttype,
  179. unsigned short value, unsigned short index,
  180. void *data, unsigned short size, int timeout)
  181. {
  182. ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
  183. int err;
  184. if ((timeout == 0) && (!asynch_allowed)) {
  185. /* request for a asynch control pipe is not allowed */
  186. return -EINVAL;
  187. }
  188. /* set setup command */
  189. setup_packet->requesttype = requesttype;
  190. setup_packet->request = request;
  191. setup_packet->value = cpu_to_le16(value);
  192. setup_packet->index = cpu_to_le16(index);
  193. setup_packet->length = cpu_to_le16(size);
  194. // FIXME debug option
  195. //printf("[usb] usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
  196. // "value 0x%X index 0x%X length 0x%X\n",
  197. // request, requesttype, value, index, size);
  198. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  199. err = submit_control_msg(dev, pipe, data, size, setup_packet);
  200. if (err < 0)
  201. return err;
  202. if (timeout == 0)
  203. return (int)size;
  204. /*
  205. * Wait for status to update until timeout expires, USB driver
  206. * interrupt handler may set the status when the USB operation has
  207. * been completed.
  208. */
  209. while (timeout--) {
  210. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC)) {
  211. break;
  212. }
  213. mdelay(1);
  214. }
  215. if (timeout==0) {
  216. printf("[usb] usb_control_msg timeout!\n");
  217. }
  218. if (dev->status)
  219. return -1;
  220. return dev->act_len;
  221. }
  222. /*-------------------------------------------------------------------
  223. * submits bulk message, and waits for completion. returns 0 if Ok or
  224. * negative if Error.
  225. * synchronous behavior
  226. */
  227. int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
  228. void *data, int len, int *actual_length, int timeout)
  229. {
  230. if (len < 0)
  231. return -EINVAL;
  232. dev->status = USB_ST_NOT_PROC; /*not yet processed */
  233. if (submit_bulk_msg(dev, pipe, data, len) < 0)
  234. return -EIO;
  235. while (timeout--) {
  236. if (!((volatile unsigned long)dev->status & USB_ST_NOT_PROC))
  237. break;
  238. mdelay(1);
  239. }
  240. *actual_length = dev->act_len;
  241. if (dev->status == 0)
  242. return 0;
  243. else
  244. return -EIO;
  245. }
  246. /*-------------------------------------------------------------------
  247. * Max Packet stuff
  248. */
  249. /*
  250. * returns the max packet size, depending on the pipe direction and
  251. * the configurations values
  252. */
  253. int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
  254. {
  255. /* direction is out -> use emaxpacket out */
  256. if ((pipe & USB_DIR_IN) == 0)
  257. return dev->epmaxpacketout[((pipe>>15) & 0xf)];
  258. else
  259. return dev->epmaxpacketin[((pipe>>15) & 0xf)];
  260. }
  261. // FIXME: LOL
  262. /*
  263. * The routine usb_set_maxpacket_ep() is extracted from the loop of routine
  264. * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
  265. * when it is inlined in 1 single routine. What happens is that the register r3
  266. * is used as loop-count 'i', but gets overwritten later on.
  267. * This is clearly a compiler bug, but it is easier to workaround it here than
  268. * to update the compiler (Occurs with at least several GCC 4.{1,2},x
  269. * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
  270. *
  271. * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5.
  272. */
  273. static void usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
  274. {
  275. int b;
  276. struct usb_endpoint_descriptor *ep;
  277. u16 ep_wMaxPacketSize;
  278. ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx];
  279. b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  280. ep_wMaxPacketSize = get_unaligned(&ep->wMaxPacketSize);
  281. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  282. USB_ENDPOINT_XFER_CONTROL) {
  283. /* Control => bidirectional */
  284. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  285. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  286. printf("##Control EP epmaxpacketout/in[%d] = %d\n",
  287. b, dev->epmaxpacketin[b]);
  288. } else {
  289. if ((ep->bEndpointAddress & 0x80) == 0) {
  290. /* OUT Endpoint */
  291. if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) {
  292. dev->epmaxpacketout[b] = ep_wMaxPacketSize;
  293. //printf("##EP epmaxpacketout[%d] = %d\n",
  294. // b, dev->epmaxpacketout[b]);
  295. }
  296. } else {
  297. /* IN Endpoint */
  298. if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) {
  299. dev->epmaxpacketin[b] = ep_wMaxPacketSize;
  300. //printf("##EP epmaxpacketin[%d] = %d\n",
  301. // b, dev->epmaxpacketin[b]);
  302. }
  303. } /* if out */
  304. } /* if control */
  305. }
  306. /*
  307. * set the max packed value of all endpoints in the given configuration
  308. */
  309. static int usb_set_maxpacket(struct usb_device *dev)
  310. {
  311. int i, ii;
  312. for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
  313. for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
  314. usb_set_maxpacket_ep(dev, i, ii);
  315. return 0;
  316. }
  317. /*******************************************************************************
  318. * Parse the config, located in buffer, and fills the dev->config structure.
  319. * Note that all little/big endian swapping are done automatically.
  320. * (wTotalLength has already been swapped and sanitized when it was read.)
  321. */
  322. static int usb_parse_config(struct usb_device *dev,
  323. unsigned char *buffer, int cfgno)
  324. {
  325. struct usb_descriptor_header *head;
  326. int index, ifno, epno, curr_if_num;
  327. u16 ep_wMaxPacketSize;
  328. struct usb_interface *if_desc = NULL;
  329. ifno = -1;
  330. epno = -1;
  331. curr_if_num = -1;
  332. dev->configno = cfgno;
  333. head = (struct usb_descriptor_header *) &buffer[0];
  334. if (head->bDescriptorType != USB_DT_CONFIG) {
  335. printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
  336. head->bDescriptorType);
  337. return -EINVAL;
  338. }
  339. if (head->bLength != USB_DT_CONFIG_SIZE) {
  340. printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength);
  341. return -EINVAL;
  342. }
  343. memcpy(&dev->config, head, USB_DT_CONFIG_SIZE);
  344. dev->config.no_of_if = 0;
  345. index = dev->config.desc.bLength;
  346. /* Ok the first entry must be a configuration entry,
  347. * now process the others */
  348. head = (struct usb_descriptor_header *) &buffer[index];
  349. while (index + 1 < dev->config.desc.wTotalLength && head->bLength) {
  350. switch (head->bDescriptorType) {
  351. case USB_DT_INTERFACE:
  352. if (head->bLength != USB_DT_INTERFACE_SIZE) {
  353. printf("ERROR: Invalid USB IF length (%d)\n",
  354. head->bLength);
  355. break;
  356. }
  357. if (index + USB_DT_INTERFACE_SIZE >
  358. dev->config.desc.wTotalLength) {
  359. puts("USB IF descriptor overflowed buffer!\n");
  360. break;
  361. }
  362. if (((struct usb_interface_descriptor *) \
  363. head)->bInterfaceNumber != curr_if_num) {
  364. /* this is a new interface, copy new desc */
  365. ifno = dev->config.no_of_if;
  366. if (ifno >= USB_MAXINTERFACES) {
  367. puts("Too many USB interfaces!\n");
  368. /* try to go on with what we have */
  369. return -EINVAL;
  370. }
  371. if_desc = &dev->config.if_desc[ifno];
  372. dev->config.no_of_if++;
  373. memcpy(if_desc, head,
  374. USB_DT_INTERFACE_SIZE);
  375. if_desc->no_of_ep = 0;
  376. if_desc->num_altsetting = 1;
  377. curr_if_num =
  378. if_desc->desc.bInterfaceNumber;
  379. } else {
  380. /* found alternate setting for the interface */
  381. if (ifno >= 0) {
  382. if_desc = &dev->config.if_desc[ifno];
  383. if_desc->num_altsetting++;
  384. }
  385. }
  386. break;
  387. case USB_DT_ENDPOINT:
  388. if (head->bLength != USB_DT_ENDPOINT_SIZE &&
  389. head->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) {
  390. printf("ERROR: Invalid USB EP length (%d)\n",
  391. head->bLength);
  392. break;
  393. }
  394. if (index + head->bLength >
  395. dev->config.desc.wTotalLength) {
  396. puts("USB EP descriptor overflowed buffer!\n");
  397. break;
  398. }
  399. if (ifno < 0) {
  400. puts("Endpoint descriptor out of order!\n");
  401. break;
  402. }
  403. epno = dev->config.if_desc[ifno].no_of_ep;
  404. if_desc = &dev->config.if_desc[ifno];
  405. if (epno >= USB_MAXENDPOINTS) {
  406. printf("Interface %d has too many endpoints!\n",
  407. if_desc->desc.bInterfaceNumber);
  408. return -EINVAL;
  409. }
  410. /* found an endpoint */
  411. if_desc->no_of_ep++;
  412. memcpy(&if_desc->ep_desc[epno], head,
  413. USB_DT_ENDPOINT_SIZE);
  414. ep_wMaxPacketSize = get_unaligned(&dev->config.\
  415. if_desc[ifno].\
  416. ep_desc[epno].\
  417. wMaxPacketSize);
  418. put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
  419. &dev->config.\
  420. if_desc[ifno].\
  421. ep_desc[epno].\
  422. wMaxPacketSize);
  423. //printf("if %d, ep %d\n", ifno, epno);
  424. break;
  425. case USB_DT_SS_ENDPOINT_COMP:
  426. if (head->bLength != USB_DT_SS_EP_COMP_SIZE) {
  427. printf("ERROR: Invalid USB EPC length (%d)\n",
  428. head->bLength);
  429. break;
  430. }
  431. if (index + USB_DT_SS_EP_COMP_SIZE >
  432. dev->config.desc.wTotalLength) {
  433. puts("USB EPC descriptor overflowed buffer!\n");
  434. break;
  435. }
  436. if (ifno < 0 || epno < 0) {
  437. puts("EPC descriptor out of order!\n");
  438. break;
  439. }
  440. if_desc = &dev->config.if_desc[ifno];
  441. memcpy(&if_desc->ss_ep_comp_desc[epno], head,
  442. USB_DT_SS_EP_COMP_SIZE);
  443. break;
  444. default:
  445. if (head->bLength == 0)
  446. return -EINVAL;
  447. printf("unknown Description Type : %x\n",
  448. head->bDescriptorType);
  449. #ifdef DEBUG
  450. {
  451. unsigned char *ch = (unsigned char *)head;
  452. int i;
  453. for (i = 0; i < head->bLength; i++)
  454. printf("%02X ", *ch++);
  455. printf("\n\n\n");
  456. }
  457. #endif
  458. break;
  459. }
  460. index += head->bLength;
  461. head = (struct usb_descriptor_header *)&buffer[index];
  462. }
  463. return 0;
  464. }
  465. /***********************************************************************
  466. * Clears an endpoint
  467. * endp: endpoint number in bits 0-3;
  468. * direction flag in bit 7 (1 = IN, 0 = OUT)
  469. */
  470. int usb_clear_halt(struct usb_device *dev, int pipe)
  471. {
  472. int result;
  473. int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
  474. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  475. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  476. endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
  477. /* don't clear if failed */
  478. if (result < 0)
  479. return result;
  480. /*
  481. * NOTE: we do not get status and verify reset was successful
  482. * as some devices are reported to lock up upon this check..
  483. */
  484. usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  485. /* toggle is reset on clear */
  486. usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
  487. return 0;
  488. }
  489. /**********************************************************************
  490. * get_descriptor type
  491. */
  492. static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
  493. unsigned char index, void *buf, int size)
  494. {
  495. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  496. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  497. (type << 8) + index, 0, buf, size,
  498. USB_CNTL_TIMEOUT);
  499. }
  500. /**********************************************************************
  501. * gets len of configuration cfgno
  502. */
  503. int usb_get_configuration_len(struct usb_device *dev, int cfgno)
  504. {
  505. int result;
  506. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 9);
  507. struct usb_config_descriptor *config;
  508. config = (struct usb_config_descriptor *)&buffer[0];
  509. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
  510. if (result < 9) {
  511. if (result < 0)
  512. printf("unable to get descriptor, error %lX\n",
  513. dev->status);
  514. else
  515. printf("config descriptor too short " \
  516. "(expected %i, got %i)\n", 9, result);
  517. return -EIO;
  518. }
  519. return le16_to_cpu(config->wTotalLength);
  520. }
  521. /**********************************************************************
  522. * gets configuration cfgno and store it in the buffer
  523. */
  524. int usb_get_configuration_no(struct usb_device *dev, int cfgno,
  525. unsigned char *buffer, int length)
  526. {
  527. int result;
  528. struct usb_config_descriptor *config;
  529. config = (struct usb_config_descriptor *)&buffer[0];
  530. result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length);
  531. //printf("get_conf_no %d Result %d, wLength %d\n", cfgno, result,
  532. // le16_to_cpu(config->wTotalLength));
  533. config->wTotalLength = result; /* validated, with CPU byte order */
  534. return result;
  535. }
  536. /********************************************************************
  537. * set address of a device to the value in dev->devnum.
  538. * This can only be done by addressing the device via the default address (0)
  539. */
  540. static int usb_set_address(struct usb_device *dev)
  541. {
  542. printf("[usb] set address %d\n", dev->devnum);
  543. return usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
  544. 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT);
  545. }
  546. /********************************************************************
  547. * set interface number to interface
  548. */
  549. int usb_set_interface(struct usb_device *dev, int interface, int alternate)
  550. {
  551. struct usb_interface *if_face = NULL;
  552. int ret, i;
  553. for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
  554. if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
  555. if_face = &dev->config.if_desc[i];
  556. break;
  557. }
  558. }
  559. if (!if_face) {
  560. printf("selecting invalid interface %d", interface);
  561. return -EINVAL;
  562. }
  563. /*
  564. * We should return now for devices with only one alternate setting.
  565. * According to 9.4.10 of the Universal Serial Bus Specification
  566. * Revision 2.0 such devices can return with a STALL. This results in
  567. * some USB sticks timeouting during initialization and then being
  568. * unusable in U-Boot.
  569. */
  570. if (if_face->num_altsetting == 1)
  571. return 0;
  572. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  573. USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
  574. alternate, interface, NULL, 0,
  575. USB_CNTL_TIMEOUT * 5);
  576. if (ret < 0)
  577. return ret;
  578. return 0;
  579. }
  580. /********************************************************************
  581. * set configuration number to configuration
  582. */
  583. static int usb_set_configuration(struct usb_device *dev, int configuration)
  584. {
  585. int res;
  586. printf("[usb] set configuration %d\n", configuration);
  587. /* set setup command */
  588. res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  589. USB_REQ_SET_CONFIGURATION, 0,
  590. configuration, 0,
  591. NULL, 0, USB_CNTL_TIMEOUT);
  592. if (res == 0) {
  593. dev->toggle[0] = 0;
  594. dev->toggle[1] = 0;
  595. return 0;
  596. } else
  597. return -EIO;
  598. }
  599. /********************************************************************
  600. * set protocol to protocol
  601. */
  602. int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol)
  603. {
  604. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  605. USB_REQ_SET_PROTOCOL, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  606. protocol, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  607. }
  608. /********************************************************************
  609. * set idle
  610. */
  611. int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id)
  612. {
  613. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  614. USB_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  615. (duration << 8) | report_id, ifnum, NULL, 0, USB_CNTL_TIMEOUT);
  616. }
  617. /********************************************************************
  618. * get report
  619. */
  620. int usb_get_report(struct usb_device *dev, int ifnum, unsigned char type,
  621. unsigned char id, void *buf, int size)
  622. {
  623. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  624. USB_REQ_GET_REPORT,
  625. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  626. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  627. }
  628. /********************************************************************
  629. * get class descriptor
  630. */
  631. int usb_get_class_descriptor(struct usb_device *dev, int ifnum,
  632. unsigned char type, unsigned char id, void *buf, int size)
  633. {
  634. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  635. USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
  636. (type << 8) + id, ifnum, buf, size, USB_CNTL_TIMEOUT);
  637. }
  638. /********************************************************************
  639. * get string index in buffer
  640. */
  641. static int usb_get_string(struct usb_device *dev, unsigned short langid,
  642. unsigned char index, void *buf, int size)
  643. {
  644. int i;
  645. int result;
  646. for (i = 0; i < 3; ++i) {
  647. /* some devices are flaky */
  648. result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  649. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  650. (USB_DT_STRING << 8) + index, langid, buf, size,
  651. USB_CNTL_TIMEOUT);
  652. if (result > 0)
  653. break;
  654. }
  655. return result;
  656. }
  657. static void usb_try_string_workarounds(unsigned char *buf, int *length)
  658. {
  659. int newlength, oldlength = *length;
  660. for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
  661. if (!isprint(buf[newlength]) || buf[newlength + 1])
  662. break;
  663. if (newlength > 2) {
  664. buf[0] = newlength;
  665. *length = newlength;
  666. }
  667. }
  668. static int usb_string_sub(struct usb_device *dev, unsigned int langid,
  669. unsigned int index, unsigned char *buf)
  670. {
  671. int rc;
  672. /* Try to read the string descriptor by asking for the maximum
  673. * possible number of bytes */
  674. rc = usb_get_string(dev, langid, index, buf, 255);
  675. /* If that failed try to read the descriptor length, then
  676. * ask for just that many bytes */
  677. if (rc < 2) {
  678. rc = usb_get_string(dev, langid, index, buf, 2);
  679. if (rc == 2)
  680. rc = usb_get_string(dev, langid, index, buf, buf[0]);
  681. }
  682. if (rc >= 2) {
  683. if (!buf[0] && !buf[1])
  684. usb_try_string_workarounds(buf, &rc);
  685. /* There might be extra junk at the end of the descriptor */
  686. if (buf[0] < rc)
  687. rc = buf[0];
  688. rc = rc - (rc & 1); /* force a multiple of two */
  689. }
  690. if (rc < 2)
  691. rc = -EINVAL;
  692. return rc;
  693. }
  694. /********************************************************************
  695. * usb_string:
  696. * Get string index and translate it to ascii.
  697. * returns string length (> 0) or error (< 0)
  698. */
  699. int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
  700. {
  701. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
  702. unsigned char *tbuf;
  703. int err;
  704. unsigned int u, idx;
  705. if (size <= 0 || !buf || !index)
  706. return -EINVAL;
  707. buf[0] = 0;
  708. tbuf = &mybuf[0];
  709. /* get langid for strings if it's not yet known */
  710. if (!dev->have_langid) {
  711. err = usb_string_sub(dev, 0, 0, tbuf);
  712. if (err < 0) {
  713. printf("error getting string descriptor 0 " \
  714. "(error=%lx)\n", dev->status);
  715. return -EIO;
  716. } else if (tbuf[0] < 4) {
  717. printf("string descriptor 0 too short\n");
  718. return -EIO;
  719. } else {
  720. dev->have_langid = -1;
  721. dev->string_langid = tbuf[2] | (tbuf[3] << 8);
  722. /* always use the first langid listed */
  723. printf("[usb] USB device number %d default " \
  724. "language ID 0x%x\n",
  725. dev->devnum, dev->string_langid);
  726. }
  727. }
  728. err = usb_string_sub(dev, dev->string_langid, index, tbuf);
  729. if (err < 0)
  730. return err;
  731. size--; /* leave room for trailing NULL char in output buffer */
  732. for (idx = 0, u = 2; u < err; u += 2) {
  733. if (idx >= size)
  734. break;
  735. if (tbuf[u+1]) /* high byte */
  736. buf[idx++] = '?'; /* non-ASCII character */
  737. else
  738. buf[idx++] = tbuf[u];
  739. }
  740. buf[idx] = 0;
  741. err = idx;
  742. return err;
  743. }
  744. /********************************************************************
  745. * USB device handling:
  746. * the USB device are static allocated [USB_MAX_DEVICE].
  747. */
  748. /* returns a pointer to the device with the index [index].
  749. * if the device is not assigned (dev->devnum==-1) returns NULL
  750. */
  751. struct usb_device *usb_get_dev_index(int index)
  752. {
  753. if (usb_dev[index].devnum == -1)
  754. return NULL;
  755. else
  756. return &usb_dev[index];
  757. }
  758. int usb_alloc_new_device(void *controller, struct usb_device **devp)
  759. {
  760. int i;
  761. printf("[usb] new device %d\n", dev_index);
  762. if (dev_index == USB_MAX_DEVICE) {
  763. printf("ERROR, too many USB Devices, max=%d\n", USB_MAX_DEVICE);
  764. return -ENOSPC;
  765. }
  766. /* default Address is 0, real addresses start with 1 */
  767. usb_dev[dev_index].devnum = dev_index + 1;
  768. usb_dev[dev_index].maxchild = 0;
  769. for (i = 0; i < USB_MAXCHILDREN; i++)
  770. usb_dev[dev_index].children[i] = NULL;
  771. usb_dev[dev_index].parent = NULL;
  772. usb_dev[dev_index].controller = controller;
  773. dev_index++;
  774. *devp = &usb_dev[dev_index - 1];
  775. return 0;
  776. }
  777. /*
  778. * Free the newly created device node.
  779. * Called in error cases where configuring a newly attached
  780. * device fails for some reason.
  781. */
  782. void usb_free_device(void *controller)
  783. {
  784. dev_index--;
  785. printf("Freeing device node: %d\n", dev_index);
  786. memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
  787. usb_dev[dev_index].devnum = -1;
  788. }
  789. /*
  790. * XHCI issues Enable Slot command and thereafter
  791. * allocates device contexts. Provide a weak alias
  792. * function for the purpose, so that XHCI overrides it
  793. * and EHCI/OHCI just work out of the box.
  794. */
  795. int usb_alloc_device(struct usb_device *udev)
  796. {
  797. return 0;
  798. }
  799. static int usb_hub_port_reset(struct usb_device *dev, struct usb_device *hub)
  800. {
  801. if (!hub)
  802. usb_reset_root_port(dev);
  803. return 0;
  804. }
  805. static int get_descriptor_len(struct usb_device *dev, int len, int expect_len)
  806. {
  807. struct usb_device_descriptor *desc;
  808. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
  809. int err;
  810. desc = (struct usb_device_descriptor *)tmpbuf;
  811. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, len);
  812. if (err < expect_len) {
  813. if (err < 0) {
  814. printf("unable to get device descriptor (error=%d)\n",
  815. err);
  816. return err;
  817. } else {
  818. printf("USB device descriptor short read (expected %i, got %i)\n",
  819. expect_len, err);
  820. return -EIO;
  821. }
  822. }
  823. memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
  824. return 0;
  825. }
  826. static int usb_setup_descriptor(struct usb_device *dev, bool do_read)
  827. {
  828. /*
  829. * This is a Windows scheme of initialization sequence, with double
  830. * reset of the device (Linux uses the same sequence)
  831. * Some equipment is said to work only with such init sequence; this
  832. * patch is based on the work by Alan Stern:
  833. * http://sourceforge.net/mailarchive/forum.php?
  834. * thread_id=5729457&forum_id=5398
  835. */
  836. /*
  837. * send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
  838. * only 18 bytes long, this will terminate with a short packet. But if
  839. * the maxpacket size is 8 or 16 the device may be waiting to transmit
  840. * some more, or keeps on retransmitting the 8 byte header.
  841. */
  842. if (dev->speed == USB_SPEED_LOW) {
  843. dev->descriptor.bMaxPacketSize0 = 8;
  844. dev->maxpacketsize = PACKET_SIZE_8;
  845. } else {
  846. dev->descriptor.bMaxPacketSize0 = 64;
  847. dev->maxpacketsize = PACKET_SIZE_64;
  848. }
  849. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  850. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  851. if (do_read && dev->speed == USB_SPEED_FULL) {
  852. int err;
  853. /*
  854. * Validate we've received only at least 8 bytes, not that
  855. * we've received the entire descriptor. The reasoning is:
  856. * - The code only uses fields in the first 8 bytes, so
  857. * that's all we need to have fetched at this stage.
  858. * - The smallest maxpacket size is 8 bytes. Before we know
  859. * the actual maxpacket the device uses, the USB controller
  860. * may only accept a single packet. Consequently we are only
  861. * guaranteed to receive 1 packet (at least 8 bytes) even in
  862. * a non-error case.
  863. *
  864. * At least the DWC2 controller needs to be programmed with
  865. * the number of packets in addition to the number of bytes.
  866. * A request for 64 bytes of data with the maxpacket guessed
  867. * as 64 (above) yields a request for 1 packet.
  868. */
  869. err = get_descriptor_len(dev, 64, 8);
  870. if (err)
  871. return err;
  872. }
  873. dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
  874. dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
  875. switch (dev->descriptor.bMaxPacketSize0) {
  876. case 8:
  877. dev->maxpacketsize = PACKET_SIZE_8;
  878. break;
  879. case 16:
  880. dev->maxpacketsize = PACKET_SIZE_16;
  881. break;
  882. case 32:
  883. dev->maxpacketsize = PACKET_SIZE_32;
  884. break;
  885. case 64:
  886. dev->maxpacketsize = PACKET_SIZE_64;
  887. break;
  888. default:
  889. printf("%s: invalid max packet size\n", __func__);
  890. return -EIO;
  891. }
  892. return 0;
  893. }
  894. static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read,
  895. struct usb_device *parent)
  896. {
  897. int err;
  898. /*
  899. * Allocate usb 3.0 device context.
  900. * USB 3.0 (xHCI) protocol tries to allocate device slot
  901. * and related data structures first. This call does that.
  902. * Refer to sec 4.3.2 in xHCI spec rev1.0
  903. */
  904. err = usb_alloc_device(dev);
  905. if (err) {
  906. printf("Cannot allocate device context to get SLOT_ID\n");
  907. return err;
  908. }
  909. err = usb_setup_descriptor(dev, do_read);
  910. if (err)
  911. return err;
  912. err = usb_hub_port_reset(dev, parent);
  913. if (err)
  914. return err;
  915. dev->devnum = addr;
  916. err = usb_set_address(dev); /* set address */
  917. if (err < 0) {
  918. printf("\n USB device not accepting new address " \
  919. "(error=%lX)\n", dev->status);
  920. return err;
  921. }
  922. mdelay(10); /* Let the SET_ADDRESS settle */
  923. /*
  924. * If we haven't read device descriptor before, read it here
  925. * after device is assigned an address. This is only applicable
  926. * to xHCI so far.
  927. */
  928. if (!do_read) {
  929. err = usb_setup_descriptor(dev, true);
  930. if (err)
  931. return err;
  932. }
  933. return 0;
  934. }
  935. int usb_select_config(struct usb_device *dev)
  936. {
  937. unsigned char *tmpbuf = NULL;
  938. int err;
  939. err = get_descriptor_len(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE);
  940. if (err)
  941. return err;
  942. /* correct le values */
  943. le16_to_cpus(&dev->descriptor.bcdUSB);
  944. le16_to_cpus(&dev->descriptor.idVendor);
  945. le16_to_cpus(&dev->descriptor.idProduct);
  946. le16_to_cpus(&dev->descriptor.bcdDevice);
  947. /*
  948. * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive
  949. * about this first Get Descriptor request. If there are any other
  950. * requests in the first microframe, the stick crashes. Wait about
  951. * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0).
  952. */
  953. mdelay(1);
  954. /* only support for one config for now */
  955. err = usb_get_configuration_len(dev, 0);
  956. if (err >= 0) {
  957. tmpbuf = (unsigned char *)malloc_cache_aligned(err);
  958. if (!tmpbuf)
  959. err = -ENOMEM;
  960. else
  961. err = usb_get_configuration_no(dev, 0, tmpbuf, err);
  962. }
  963. if (err < 0) {
  964. printf("usb_new_device: Cannot read configuration, " \
  965. "skipping device %04x:%04x\n",
  966. dev->descriptor.idVendor, dev->descriptor.idProduct);
  967. free(tmpbuf);
  968. return err;
  969. }
  970. usb_parse_config(dev, tmpbuf, 0);
  971. free(tmpbuf);
  972. usb_set_maxpacket(dev);
  973. /*
  974. * we set the default configuration here
  975. * This seems premature. If the driver wants a different configuration
  976. * it will need to select itself.
  977. */
  978. err = usb_set_configuration(dev, dev->config.desc.bConfigurationValue);
  979. if (err < 0) {
  980. printf("failed to set default configuration " \
  981. "len %d, status %lX\n", dev->act_len, dev->status);
  982. return err;
  983. }
  984. /*
  985. * Wait until the Set Configuration request gets processed by the
  986. * device. This is required by at least SanDisk Cruzer Pop USB 2.0
  987. * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller.
  988. */
  989. mdelay(10);
  990. /*printf("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  991. dev->descriptor.iManufacturer, dev->descriptor.iProduct,
  992. dev->descriptor.iSerialNumber);*/
  993. memset(dev->mf, 0, sizeof(dev->mf));
  994. memset(dev->prod, 0, sizeof(dev->prod));
  995. memset(dev->serial, 0, sizeof(dev->serial));
  996. if (dev->descriptor.iManufacturer)
  997. usb_string(dev, dev->descriptor.iManufacturer,
  998. dev->mf, sizeof(dev->mf));
  999. if (dev->descriptor.iProduct)
  1000. usb_string(dev, dev->descriptor.iProduct,
  1001. dev->prod, sizeof(dev->prod));
  1002. if (dev->descriptor.iSerialNumber)
  1003. usb_string(dev, dev->descriptor.iSerialNumber,
  1004. dev->serial, sizeof(dev->serial));
  1005. printf("[usb] Manufacturer %s\n", dev->mf);
  1006. printf("[usb] Product %s\n", dev->prod);
  1007. printf("[usb] SerialNumber %s\n", dev->serial);
  1008. return 0;
  1009. }
  1010. int usb_setup_device(struct usb_device *dev, bool do_read,
  1011. struct usb_device *parent)
  1012. {
  1013. int addr;
  1014. int ret;
  1015. /* We still haven't set the Address yet */
  1016. addr = dev->devnum;
  1017. dev->devnum = 0;
  1018. ret = usb_prepare_device(dev, addr, do_read, parent);
  1019. if (ret)
  1020. return ret;
  1021. ret = usb_select_config(dev);
  1022. return ret;
  1023. }
  1024. /*
  1025. * By the time we get here, the device has gotten a new device ID
  1026. * and is in the default state. We need to identify the thing and
  1027. * get the ball rolling..
  1028. *
  1029. * Returns 0 for success, != 0 for error.
  1030. */
  1031. int usb_new_device(struct usb_device *dev)
  1032. {
  1033. bool do_read = true;
  1034. int err;
  1035. err = usb_setup_device(dev, do_read, dev->parent);
  1036. if (err)
  1037. return err;
  1038. /* Now probe if the device is a hub */
  1039. err = usb_hub_probe(dev, 0);
  1040. if (err < 0)
  1041. return err;
  1042. return 0;
  1043. }
  1044. int board_usb_init(int index, enum usb_init_type init)
  1045. {
  1046. return 0;
  1047. }
  1048. int board_usb_cleanup(int index, enum usb_init_type init)
  1049. {
  1050. return 0;
  1051. }
  1052. bool usb_device_has_child_on_port(struct usb_device *parent, int port)
  1053. {
  1054. return parent->children[port] != NULL;
  1055. }
  1056. void usb_find_usb2_hub_address_port(struct usb_device *udev,
  1057. uint8_t *hub_address, uint8_t *hub_port)
  1058. {
  1059. /* Find out the nearest parent which is high speed */
  1060. while (udev->parent->parent != NULL)
  1061. if (udev->parent->speed != USB_SPEED_HIGH) {
  1062. udev = udev->parent;
  1063. } else {
  1064. *hub_address = udev->parent->devnum;
  1065. *hub_port = udev->portnr;
  1066. return;
  1067. }
  1068. printf("Error: Cannot find high speed parent of usb-1 device\n");
  1069. *hub_address = 0;
  1070. *hub_port = 0;
  1071. }
  1072. /* EOF */