musb_hcd.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * Mentor USB OTG Core host controller driver.
  3. *
  4. * Copyright (c) 2008 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. *
  21. * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
  22. */
  23. #include <common.h>
  24. #include "musb_hcd.h"
  25. /* MSC control transfers */
  26. #define USB_MSC_BBB_RESET 0xFF
  27. #define USB_MSC_BBB_GET_MAX_LUN 0xFE
  28. /* Endpoint configuration information */
  29. static const struct musb_epinfo epinfo[3] = {
  30. {MUSB_BULK_EP, 1, 512}, /* EP1 - Bluk Out - 512 Bytes */
  31. {MUSB_BULK_EP, 0, 512}, /* EP1 - Bluk In - 512 Bytes */
  32. {MUSB_INTR_EP, 0, 64} /* EP2 - Interrupt IN - 64 Bytes */
  33. };
  34. /* --- Virtual Root Hub ---------------------------------------------------- */
  35. #ifdef MUSB_NO_MULTIPOINT
  36. static int rh_devnum;
  37. static u32 port_status;
  38. /* Device descriptor */
  39. static const u8 root_hub_dev_des[] = {
  40. 0x12, /* __u8 bLength; */
  41. 0x01, /* __u8 bDescriptorType; Device */
  42. 0x00, /* __u16 bcdUSB; v1.1 */
  43. 0x02,
  44. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  45. 0x00, /* __u8 bDeviceSubClass; */
  46. 0x00, /* __u8 bDeviceProtocol; */
  47. 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
  48. 0x00, /* __u16 idVendor; */
  49. 0x00,
  50. 0x00, /* __u16 idProduct; */
  51. 0x00,
  52. 0x00, /* __u16 bcdDevice; */
  53. 0x00,
  54. 0x00, /* __u8 iManufacturer; */
  55. 0x01, /* __u8 iProduct; */
  56. 0x00, /* __u8 iSerialNumber; */
  57. 0x01 /* __u8 bNumConfigurations; */
  58. };
  59. /* Configuration descriptor */
  60. static const u8 root_hub_config_des[] = {
  61. 0x09, /* __u8 bLength; */
  62. 0x02, /* __u8 bDescriptorType; Configuration */
  63. 0x19, /* __u16 wTotalLength; */
  64. 0x00,
  65. 0x01, /* __u8 bNumInterfaces; */
  66. 0x01, /* __u8 bConfigurationValue; */
  67. 0x00, /* __u8 iConfiguration; */
  68. 0x40, /* __u8 bmAttributes;
  69. Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
  70. 0x00, /* __u8 MaxPower; */
  71. /* interface */
  72. 0x09, /* __u8 if_bLength; */
  73. 0x04, /* __u8 if_bDescriptorType; Interface */
  74. 0x00, /* __u8 if_bInterfaceNumber; */
  75. 0x00, /* __u8 if_bAlternateSetting; */
  76. 0x01, /* __u8 if_bNumEndpoints; */
  77. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  78. 0x00, /* __u8 if_bInterfaceSubClass; */
  79. 0x00, /* __u8 if_bInterfaceProtocol; */
  80. 0x00, /* __u8 if_iInterface; */
  81. /* endpoint */
  82. 0x07, /* __u8 ep_bLength; */
  83. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  84. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  85. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  86. 0x00, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
  87. 0x02,
  88. 0xff /* __u8 ep_bInterval; 255 ms */
  89. };
  90. static const unsigned char root_hub_str_index0[] = {
  91. 0x04, /* __u8 bLength; */
  92. 0x03, /* __u8 bDescriptorType; String-descriptor */
  93. 0x09, /* __u8 lang ID */
  94. 0x04, /* __u8 lang ID */
  95. };
  96. static const unsigned char root_hub_str_index1[] = {
  97. 0x1c, /* __u8 bLength; */
  98. 0x03, /* __u8 bDescriptorType; String-descriptor */
  99. 'M', /* __u8 Unicode */
  100. 0, /* __u8 Unicode */
  101. 'U', /* __u8 Unicode */
  102. 0, /* __u8 Unicode */
  103. 'S', /* __u8 Unicode */
  104. 0, /* __u8 Unicode */
  105. 'B', /* __u8 Unicode */
  106. 0, /* __u8 Unicode */
  107. ' ', /* __u8 Unicode */
  108. 0, /* __u8 Unicode */
  109. 'R', /* __u8 Unicode */
  110. 0, /* __u8 Unicode */
  111. 'o', /* __u8 Unicode */
  112. 0, /* __u8 Unicode */
  113. 'o', /* __u8 Unicode */
  114. 0, /* __u8 Unicode */
  115. 't', /* __u8 Unicode */
  116. 0, /* __u8 Unicode */
  117. ' ', /* __u8 Unicode */
  118. 0, /* __u8 Unicode */
  119. 'H', /* __u8 Unicode */
  120. 0, /* __u8 Unicode */
  121. 'u', /* __u8 Unicode */
  122. 0, /* __u8 Unicode */
  123. 'b', /* __u8 Unicode */
  124. 0, /* __u8 Unicode */
  125. };
  126. #endif
  127. /*
  128. * This function writes the data toggle value.
  129. */
  130. static void write_toggle(struct usb_device *dev, u8 ep, u8 dir_out)
  131. {
  132. u16 toggle = usb_gettoggle(dev, ep, dir_out);
  133. u16 csr;
  134. if (dir_out) {
  135. csr = readw(&musbr->txcsr);
  136. if (!toggle) {
  137. if (csr & MUSB_TXCSR_MODE)
  138. csr = MUSB_TXCSR_CLRDATATOG;
  139. else
  140. csr = 0;
  141. writew(csr, &musbr->txcsr);
  142. } else {
  143. csr |= MUSB_TXCSR_H_WR_DATATOGGLE;
  144. writew(csr, &musbr->txcsr);
  145. csr |= (toggle << MUSB_TXCSR_H_DATATOGGLE_SHIFT);
  146. writew(csr, &musbr->txcsr);
  147. }
  148. } else {
  149. if (!toggle) {
  150. csr = readw(&musbr->txcsr);
  151. if (csr & MUSB_TXCSR_MODE)
  152. csr = MUSB_RXCSR_CLRDATATOG;
  153. else
  154. csr = 0;
  155. writew(csr, &musbr->rxcsr);
  156. } else {
  157. csr = readw(&musbr->rxcsr);
  158. csr |= MUSB_RXCSR_H_WR_DATATOGGLE;
  159. writew(csr, &musbr->rxcsr);
  160. csr |= (toggle << MUSB_S_RXCSR_H_DATATOGGLE);
  161. writew(csr, &musbr->rxcsr);
  162. }
  163. }
  164. }
  165. /*
  166. * This function checks if RxStall has occured on the endpoint. If a RxStall
  167. * has occured, the RxStall is cleared and 1 is returned. If RxStall has
  168. * not occured, 0 is returned.
  169. */
  170. static u8 check_stall(u8 ep, u8 dir_out)
  171. {
  172. u16 csr;
  173. /* For endpoint 0 */
  174. if (!ep) {
  175. csr = readw(&musbr->txcsr);
  176. if (csr & MUSB_CSR0_H_RXSTALL) {
  177. csr &= ~MUSB_CSR0_H_RXSTALL;
  178. writew(csr, &musbr->txcsr);
  179. return 1;
  180. }
  181. } else { /* For non-ep0 */
  182. if (dir_out) { /* is it tx ep */
  183. csr = readw(&musbr->txcsr);
  184. if (csr & MUSB_TXCSR_H_RXSTALL) {
  185. csr &= ~MUSB_TXCSR_H_RXSTALL;
  186. writew(csr, &musbr->txcsr);
  187. return 1;
  188. }
  189. } else { /* is it rx ep */
  190. csr = readw(&musbr->rxcsr);
  191. if (csr & MUSB_RXCSR_H_RXSTALL) {
  192. csr &= ~MUSB_RXCSR_H_RXSTALL;
  193. writew(csr, &musbr->rxcsr);
  194. return 1;
  195. }
  196. }
  197. }
  198. return 0;
  199. }
  200. /*
  201. * waits until ep0 is ready. Returns 0 if ep is ready, -1 for timeout
  202. * error and -2 for stall.
  203. */
  204. static int wait_until_ep0_ready(struct usb_device *dev, u32 bit_mask)
  205. {
  206. u16 csr;
  207. int result = 1;
  208. int timeout = CONFIG_MUSB_TIMEOUT;
  209. while (result > 0) {
  210. csr = readw(&musbr->txcsr);
  211. if (csr & MUSB_CSR0_H_ERROR) {
  212. csr &= ~MUSB_CSR0_H_ERROR;
  213. writew(csr, &musbr->txcsr);
  214. dev->status = USB_ST_CRC_ERR;
  215. result = -1;
  216. break;
  217. }
  218. switch (bit_mask) {
  219. case MUSB_CSR0_TXPKTRDY:
  220. if (!(csr & MUSB_CSR0_TXPKTRDY)) {
  221. if (check_stall(MUSB_CONTROL_EP, 0)) {
  222. dev->status = USB_ST_STALLED;
  223. result = -2;
  224. } else
  225. result = 0;
  226. }
  227. break;
  228. case MUSB_CSR0_RXPKTRDY:
  229. if (check_stall(MUSB_CONTROL_EP, 0)) {
  230. dev->status = USB_ST_STALLED;
  231. result = -2;
  232. } else
  233. if (csr & MUSB_CSR0_RXPKTRDY)
  234. result = 0;
  235. break;
  236. case MUSB_CSR0_H_REQPKT:
  237. if (!(csr & MUSB_CSR0_H_REQPKT)) {
  238. if (check_stall(MUSB_CONTROL_EP, 0)) {
  239. dev->status = USB_ST_STALLED;
  240. result = -2;
  241. } else
  242. result = 0;
  243. }
  244. break;
  245. }
  246. /* Check the timeout */
  247. if (--timeout)
  248. udelay(1);
  249. else {
  250. dev->status = USB_ST_CRC_ERR;
  251. result = -1;
  252. break;
  253. }
  254. }
  255. return result;
  256. }
  257. /*
  258. * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
  259. */
  260. static u8 wait_until_txep_ready(struct usb_device *dev, u8 ep)
  261. {
  262. u16 csr;
  263. int timeout = CONFIG_MUSB_TIMEOUT;
  264. do {
  265. if (check_stall(ep, 1)) {
  266. dev->status = USB_ST_STALLED;
  267. return 0;
  268. }
  269. csr = readw(&musbr->txcsr);
  270. if (csr & MUSB_TXCSR_H_ERROR) {
  271. dev->status = USB_ST_CRC_ERR;
  272. return 0;
  273. }
  274. /* Check the timeout */
  275. if (--timeout)
  276. udelay(1);
  277. else {
  278. dev->status = USB_ST_CRC_ERR;
  279. return -1;
  280. }
  281. } while (csr & MUSB_TXCSR_TXPKTRDY);
  282. return 1;
  283. }
  284. /*
  285. * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
  286. */
  287. static u8 wait_until_rxep_ready(struct usb_device *dev, u8 ep)
  288. {
  289. u16 csr;
  290. int timeout = CONFIG_MUSB_TIMEOUT;
  291. do {
  292. if (check_stall(ep, 0)) {
  293. dev->status = USB_ST_STALLED;
  294. return 0;
  295. }
  296. csr = readw(&musbr->rxcsr);
  297. if (csr & MUSB_RXCSR_H_ERROR) {
  298. dev->status = USB_ST_CRC_ERR;
  299. return 0;
  300. }
  301. /* Check the timeout */
  302. if (--timeout)
  303. udelay(1);
  304. else {
  305. dev->status = USB_ST_CRC_ERR;
  306. return -1;
  307. }
  308. } while (!(csr & MUSB_RXCSR_RXPKTRDY));
  309. return 1;
  310. }
  311. /*
  312. * This function performs the setup phase of the control transfer
  313. */
  314. static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
  315. {
  316. int result;
  317. u16 csr;
  318. /* write the control request to ep0 fifo */
  319. write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
  320. /* enable transfer of setup packet */
  321. csr = readw(&musbr->txcsr);
  322. csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
  323. writew(csr, &musbr->txcsr);
  324. /* wait until the setup packet is transmitted */
  325. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  326. dev->act_len = 0;
  327. return result;
  328. }
  329. /*
  330. * This function handles the control transfer in data phase
  331. */
  332. static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
  333. {
  334. u16 csr;
  335. u32 rxlen = 0;
  336. u32 nextlen = 0;
  337. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  338. u8 *rxbuff = (u8 *)buffer;
  339. u8 rxedlength;
  340. int result;
  341. while (rxlen < len) {
  342. /* Determine the next read length */
  343. nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
  344. /* Set the ReqPkt bit */
  345. csr = readw(&musbr->txcsr);
  346. writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
  347. result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
  348. if (result < 0)
  349. return result;
  350. /* Actual number of bytes received by usb */
  351. rxedlength = readb(&musbr->rxcount);
  352. /* Read the data from the RxFIFO */
  353. read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
  354. /* Clear the RxPktRdy Bit */
  355. csr = readw(&musbr->txcsr);
  356. csr &= ~MUSB_CSR0_RXPKTRDY;
  357. writew(csr, &musbr->txcsr);
  358. /* short packet? */
  359. if (rxedlength != nextlen) {
  360. dev->act_len += rxedlength;
  361. break;
  362. }
  363. rxlen += nextlen;
  364. dev->act_len = rxlen;
  365. }
  366. return 0;
  367. }
  368. /*
  369. * This function handles the control transfer out data phase
  370. */
  371. static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
  372. {
  373. u16 csr;
  374. u32 txlen = 0;
  375. u32 nextlen = 0;
  376. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  377. u8 *txbuff = (u8 *)buffer;
  378. int result = 0;
  379. while (txlen < len) {
  380. /* Determine the next write length */
  381. nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
  382. /* Load the data to send in FIFO */
  383. write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
  384. /* Set TXPKTRDY bit */
  385. csr = readw(&musbr->txcsr);
  386. writew(csr | MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY,
  387. &musbr->txcsr);
  388. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  389. if (result < 0)
  390. break;
  391. txlen += nextlen;
  392. dev->act_len = txlen;
  393. }
  394. return result;
  395. }
  396. /*
  397. * This function handles the control transfer out status phase
  398. */
  399. static int ctrlreq_out_status_phase(struct usb_device *dev)
  400. {
  401. u16 csr;
  402. int result;
  403. /* Set the StatusPkt bit */
  404. csr = readw(&musbr->txcsr);
  405. csr |= (MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY |
  406. MUSB_CSR0_H_STATUSPKT);
  407. writew(csr, &musbr->txcsr);
  408. /* Wait until TXPKTRDY bit is cleared */
  409. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  410. return result;
  411. }
  412. /*
  413. * This function handles the control transfer in status phase
  414. */
  415. static int ctrlreq_in_status_phase(struct usb_device *dev)
  416. {
  417. u16 csr;
  418. int result;
  419. /* Set the StatusPkt bit and ReqPkt bit */
  420. csr = MUSB_CSR0_H_DIS_PING | MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
  421. writew(csr, &musbr->txcsr);
  422. result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
  423. /* clear StatusPkt bit and RxPktRdy bit */
  424. csr = readw(&musbr->txcsr);
  425. csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
  426. writew(csr, &musbr->txcsr);
  427. return result;
  428. }
  429. /*
  430. * determines the speed of the device (High/Full/Slow)
  431. */
  432. static u8 get_dev_speed(struct usb_device *dev)
  433. {
  434. return (dev->speed & USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
  435. ((dev->speed & USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
  436. MUSB_TYPE_SPEED_FULL);
  437. }
  438. /*
  439. * configure the hub address and the port address.
  440. */
  441. static void config_hub_port(struct usb_device *dev, u8 ep)
  442. {
  443. u8 chid;
  444. u8 hub;
  445. /* Find out the nearest parent which is high speed */
  446. while (dev->parent->parent != NULL)
  447. if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
  448. dev = dev->parent;
  449. else
  450. break;
  451. /* determine the port address at that hub */
  452. hub = dev->parent->devnum;
  453. for (chid = 0; chid < USB_MAXCHILDREN; chid++)
  454. if (dev->parent->children[chid] == dev)
  455. break;
  456. #ifndef MUSB_NO_MULTIPOINT
  457. /* configure the hub address and the port address */
  458. writeb(hub, &musbr->tar[ep].txhubaddr);
  459. writeb((chid + 1), &musbr->tar[ep].txhubport);
  460. writeb(hub, &musbr->tar[ep].rxhubaddr);
  461. writeb((chid + 1), &musbr->tar[ep].rxhubport);
  462. #endif
  463. }
  464. #ifdef MUSB_NO_MULTIPOINT
  465. static void musb_port_reset(int do_reset)
  466. {
  467. u8 power = readb(&musbr->power);
  468. if (do_reset) {
  469. power &= 0xf0;
  470. writeb(power | MUSB_POWER_RESET, &musbr->power);
  471. port_status |= USB_PORT_STAT_RESET;
  472. port_status &= ~USB_PORT_STAT_ENABLE;
  473. udelay(30000);
  474. } else {
  475. writeb(power & ~MUSB_POWER_RESET, &musbr->power);
  476. power = readb(&musbr->power);
  477. if (power & MUSB_POWER_HSMODE)
  478. port_status |= USB_PORT_STAT_HIGH_SPEED;
  479. port_status &= ~(USB_PORT_STAT_RESET | (USB_PORT_STAT_C_CONNECTION << 16));
  480. port_status |= USB_PORT_STAT_ENABLE
  481. | (USB_PORT_STAT_C_RESET << 16)
  482. | (USB_PORT_STAT_C_ENABLE << 16);
  483. }
  484. }
  485. /*
  486. * root hub control
  487. */
  488. static int musb_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  489. void *buffer, int transfer_len,
  490. struct devrequest *cmd)
  491. {
  492. int leni = transfer_len;
  493. int len = 0;
  494. int stat = 0;
  495. u32 datab[4];
  496. const u8 *data_buf = (u8 *) datab;
  497. u16 bmRType_bReq;
  498. u16 wValue;
  499. u16 wIndex;
  500. u16 wLength;
  501. u16 int_usb;
  502. if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
  503. debug("Root-Hub submit IRQ: NOT implemented\n");
  504. return 0;
  505. }
  506. bmRType_bReq = cmd->requesttype | (cmd->request << 8);
  507. wValue = swap_16(cmd->value);
  508. wIndex = swap_16(cmd->index);
  509. wLength = swap_16(cmd->length);
  510. debug("--- HUB ----------------------------------------\n");
  511. debug("submit rh urb, req=%x val=%#x index=%#x len=%d\n",
  512. bmRType_bReq, wValue, wIndex, wLength);
  513. debug("------------------------------------------------\n");
  514. switch (bmRType_bReq) {
  515. case RH_GET_STATUS:
  516. debug("RH_GET_STATUS\n");
  517. *(__u16 *) data_buf = swap_16(1);
  518. len = 2;
  519. break;
  520. case RH_GET_STATUS | RH_INTERFACE:
  521. debug("RH_GET_STATUS | RH_INTERFACE\n");
  522. *(__u16 *) data_buf = swap_16(0);
  523. len = 2;
  524. break;
  525. case RH_GET_STATUS | RH_ENDPOINT:
  526. debug("RH_GET_STATUS | RH_ENDPOINT\n");
  527. *(__u16 *) data_buf = swap_16(0);
  528. len = 2;
  529. break;
  530. case RH_GET_STATUS | RH_CLASS:
  531. debug("RH_GET_STATUS | RH_CLASS\n");
  532. *(__u32 *) data_buf = swap_32(0);
  533. len = 4;
  534. break;
  535. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  536. debug("RH_GET_STATUS | RH_OTHER | RH_CLASS\n");
  537. int_usb = readw(&musbr->intrusb);
  538. if (int_usb & MUSB_INTR_CONNECT) {
  539. port_status |= USB_PORT_STAT_CONNECTION
  540. | (USB_PORT_STAT_C_CONNECTION << 16);
  541. port_status |= USB_PORT_STAT_HIGH_SPEED
  542. | USB_PORT_STAT_ENABLE;
  543. }
  544. if (port_status & USB_PORT_STAT_RESET)
  545. musb_port_reset(0);
  546. *(__u32 *) data_buf = swap_32(port_status);
  547. len = 4;
  548. break;
  549. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  550. debug("RH_CLEAR_FEATURE | RH_ENDPOINT\n");
  551. switch (wValue) {
  552. case RH_ENDPOINT_STALL:
  553. debug("C_HUB_ENDPOINT_STALL\n");
  554. len = 0;
  555. break;
  556. }
  557. port_status &= ~(1 << wValue);
  558. break;
  559. case RH_CLEAR_FEATURE | RH_CLASS:
  560. debug("RH_CLEAR_FEATURE | RH_CLASS\n");
  561. switch (wValue) {
  562. case RH_C_HUB_LOCAL_POWER:
  563. debug("C_HUB_LOCAL_POWER\n");
  564. len = 0;
  565. break;
  566. case RH_C_HUB_OVER_CURRENT:
  567. debug("C_HUB_OVER_CURRENT\n");
  568. len = 0;
  569. break;
  570. }
  571. port_status &= ~(1 << wValue);
  572. break;
  573. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  574. debug("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS\n");
  575. switch (wValue) {
  576. case RH_PORT_ENABLE:
  577. len = 0;
  578. break;
  579. case RH_PORT_SUSPEND:
  580. len = 0;
  581. break;
  582. case RH_PORT_POWER:
  583. len = 0;
  584. break;
  585. case RH_C_PORT_CONNECTION:
  586. len = 0;
  587. break;
  588. case RH_C_PORT_ENABLE:
  589. len = 0;
  590. break;
  591. case RH_C_PORT_SUSPEND:
  592. len = 0;
  593. break;
  594. case RH_C_PORT_OVER_CURRENT:
  595. len = 0;
  596. break;
  597. case RH_C_PORT_RESET:
  598. len = 0;
  599. break;
  600. default:
  601. debug("invalid wValue\n");
  602. stat = USB_ST_STALLED;
  603. }
  604. port_status &= ~(1 << wValue);
  605. break;
  606. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  607. debug("RH_SET_FEATURE | RH_OTHER | RH_CLASS\n");
  608. switch (wValue) {
  609. case RH_PORT_SUSPEND:
  610. len = 0;
  611. break;
  612. case RH_PORT_RESET:
  613. musb_port_reset(1);
  614. len = 0;
  615. break;
  616. case RH_PORT_POWER:
  617. len = 0;
  618. break;
  619. case RH_PORT_ENABLE:
  620. len = 0;
  621. break;
  622. default:
  623. debug("invalid wValue\n");
  624. stat = USB_ST_STALLED;
  625. }
  626. port_status |= 1 << wValue;
  627. break;
  628. case RH_SET_ADDRESS:
  629. debug("RH_SET_ADDRESS\n");
  630. rh_devnum = wValue;
  631. len = 0;
  632. break;
  633. case RH_GET_DESCRIPTOR:
  634. debug("RH_GET_DESCRIPTOR: %x, %d\n", wValue, wLength);
  635. switch (wValue) {
  636. case (USB_DT_DEVICE << 8): /* device descriptor */
  637. len = min_t(unsigned int,
  638. leni, min_t(unsigned int,
  639. sizeof(root_hub_dev_des),
  640. wLength));
  641. data_buf = root_hub_dev_des;
  642. break;
  643. case (USB_DT_CONFIG << 8): /* configuration descriptor */
  644. len = min_t(unsigned int,
  645. leni, min_t(unsigned int,
  646. sizeof(root_hub_config_des),
  647. wLength));
  648. data_buf = root_hub_config_des;
  649. break;
  650. case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
  651. len = min_t(unsigned int,
  652. leni, min_t(unsigned int,
  653. sizeof(root_hub_str_index0),
  654. wLength));
  655. data_buf = root_hub_str_index0;
  656. break;
  657. case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
  658. len = min_t(unsigned int,
  659. leni, min_t(unsigned int,
  660. sizeof(root_hub_str_index1),
  661. wLength));
  662. data_buf = root_hub_str_index1;
  663. break;
  664. default:
  665. debug("invalid wValue\n");
  666. stat = USB_ST_STALLED;
  667. }
  668. break;
  669. case RH_GET_DESCRIPTOR | RH_CLASS: {
  670. u8 *_data_buf = (u8 *) datab;
  671. debug("RH_GET_DESCRIPTOR | RH_CLASS\n");
  672. _data_buf[0] = 0x09; /* min length; */
  673. _data_buf[1] = 0x29;
  674. _data_buf[2] = 0x1; /* 1 port */
  675. _data_buf[3] = 0x01; /* per-port power switching */
  676. _data_buf[3] |= 0x10; /* no overcurrent reporting */
  677. /* Corresponds to data_buf[4-7] */
  678. _data_buf[4] = 0;
  679. _data_buf[5] = 5;
  680. _data_buf[6] = 0;
  681. _data_buf[7] = 0x02;
  682. _data_buf[8] = 0xff;
  683. len = min_t(unsigned int, leni,
  684. min_t(unsigned int, data_buf[0], wLength));
  685. break;
  686. }
  687. case RH_GET_CONFIGURATION:
  688. debug("RH_GET_CONFIGURATION\n");
  689. *(__u8 *) data_buf = 0x01;
  690. len = 1;
  691. break;
  692. case RH_SET_CONFIGURATION:
  693. debug("RH_SET_CONFIGURATION\n");
  694. len = 0;
  695. break;
  696. default:
  697. debug("*** *** *** unsupported root hub command *** *** ***\n");
  698. stat = USB_ST_STALLED;
  699. }
  700. len = min_t(int, len, leni);
  701. if (buffer != data_buf)
  702. memcpy(buffer, data_buf, len);
  703. dev->act_len = len;
  704. dev->status = stat;
  705. debug("dev act_len %d, status %d\n", dev->act_len, dev->status);
  706. return stat;
  707. }
  708. static void musb_rh_init(void)
  709. {
  710. rh_devnum = 0;
  711. port_status = 0;
  712. }
  713. #else
  714. static void musb_rh_init(void) {}
  715. #endif
  716. /*
  717. * do a control transfer
  718. */
  719. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  720. int len, struct devrequest *setup)
  721. {
  722. int devnum = usb_pipedevice(pipe);
  723. u8 devspeed;
  724. #ifdef MUSB_NO_MULTIPOINT
  725. /* Control message is for the HUB? */
  726. if (devnum == rh_devnum) {
  727. int stat = musb_submit_rh_msg(dev, pipe, buffer, len, setup);
  728. if (stat)
  729. return stat;
  730. }
  731. #endif
  732. /* select control endpoint */
  733. writeb(MUSB_CONTROL_EP, &musbr->index);
  734. readw(&musbr->txcsr);
  735. #ifndef MUSB_NO_MULTIPOINT
  736. /* target addr and (for multipoint) hub addr/port */
  737. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  738. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  739. #endif
  740. /* configure the hub address and the port number as required */
  741. devspeed = get_dev_speed(dev);
  742. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  743. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  744. config_hub_port(dev, MUSB_CONTROL_EP);
  745. writeb(devspeed << 6, &musbr->txtype);
  746. } else {
  747. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  748. #ifndef MUSB_NO_MULTIPOINT
  749. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  750. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  751. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  752. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  753. #endif
  754. }
  755. /* Control transfer setup phase */
  756. if (ctrlreq_setup_phase(dev, setup) < 0)
  757. return 0;
  758. switch (setup->request) {
  759. case USB_REQ_GET_DESCRIPTOR:
  760. case USB_REQ_GET_CONFIGURATION:
  761. case USB_REQ_GET_INTERFACE:
  762. case USB_REQ_GET_STATUS:
  763. case USB_MSC_BBB_GET_MAX_LUN:
  764. /* control transfer in-data-phase */
  765. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  766. return 0;
  767. /* control transfer out-status-phase */
  768. if (ctrlreq_out_status_phase(dev) < 0)
  769. return 0;
  770. break;
  771. case USB_REQ_SET_ADDRESS:
  772. case USB_REQ_SET_CONFIGURATION:
  773. case USB_REQ_SET_FEATURE:
  774. case USB_REQ_SET_INTERFACE:
  775. case USB_REQ_CLEAR_FEATURE:
  776. case USB_MSC_BBB_RESET:
  777. /* control transfer in status phase */
  778. if (ctrlreq_in_status_phase(dev) < 0)
  779. return 0;
  780. break;
  781. case USB_REQ_SET_DESCRIPTOR:
  782. /* control transfer out data phase */
  783. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  784. return 0;
  785. /* control transfer in status phase */
  786. if (ctrlreq_in_status_phase(dev) < 0)
  787. return 0;
  788. break;
  789. default:
  790. /* unhandled control transfer */
  791. return -1;
  792. }
  793. dev->status = 0;
  794. dev->act_len = len;
  795. #ifdef MUSB_NO_MULTIPOINT
  796. /* Set device address to USB_FADDR register */
  797. if (setup->request == USB_REQ_SET_ADDRESS)
  798. writeb(dev->devnum, &musbr->faddr);
  799. #endif
  800. return len;
  801. }
  802. /*
  803. * do a bulk transfer
  804. */
  805. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  806. void *buffer, int len)
  807. {
  808. int dir_out = usb_pipeout(pipe);
  809. int ep = usb_pipeendpoint(pipe);
  810. #ifndef MUSB_NO_MULTIPOINT
  811. int devnum = usb_pipedevice(pipe);
  812. #endif
  813. u8 type;
  814. u16 csr;
  815. u32 txlen = 0;
  816. u32 nextlen = 0;
  817. u8 devspeed;
  818. /* select bulk endpoint */
  819. writeb(MUSB_BULK_EP, &musbr->index);
  820. #ifndef MUSB_NO_MULTIPOINT
  821. /* write the address of the device */
  822. if (dir_out)
  823. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  824. else
  825. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  826. #endif
  827. /* configure the hub address and the port number as required */
  828. devspeed = get_dev_speed(dev);
  829. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  830. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  831. /*
  832. * MUSB is in high speed and the destination device is full
  833. * speed device. So configure the hub address and port
  834. * address registers.
  835. */
  836. config_hub_port(dev, MUSB_BULK_EP);
  837. } else {
  838. #ifndef MUSB_NO_MULTIPOINT
  839. if (dir_out) {
  840. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  841. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  842. } else {
  843. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  844. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  845. }
  846. #endif
  847. devspeed = musb_cfg.musb_speed;
  848. }
  849. /* Write the saved toggle bit value */
  850. write_toggle(dev, ep, dir_out);
  851. if (dir_out) { /* bulk-out transfer */
  852. /* Program the TxType register */
  853. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  854. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  855. (ep & MUSB_TYPE_REMOTE_END);
  856. writeb(type, &musbr->txtype);
  857. /* Write maximum packet size to the TxMaxp register */
  858. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  859. while (txlen < len) {
  860. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  861. (len-txlen) : dev->epmaxpacketout[ep];
  862. #ifdef CONFIG_USB_BLACKFIN
  863. /* Set the transfer data size */
  864. writew(nextlen, &musbr->txcount);
  865. #endif
  866. /* Write the data to the FIFO */
  867. write_fifo(MUSB_BULK_EP, nextlen,
  868. (void *)(((u8 *)buffer) + txlen));
  869. /* Set the TxPktRdy bit */
  870. csr = readw(&musbr->txcsr);
  871. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  872. /* Wait until the TxPktRdy bit is cleared */
  873. if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
  874. readw(&musbr->txcsr);
  875. usb_settoggle(dev, ep, dir_out,
  876. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  877. dev->act_len = txlen;
  878. return 0;
  879. }
  880. txlen += nextlen;
  881. }
  882. /* Keep a copy of the data toggle bit */
  883. csr = readw(&musbr->txcsr);
  884. usb_settoggle(dev, ep, dir_out,
  885. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  886. } else { /* bulk-in transfer */
  887. /* Write the saved toggle bit value */
  888. write_toggle(dev, ep, dir_out);
  889. /* Program the RxType register */
  890. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  891. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  892. (ep & MUSB_TYPE_REMOTE_END);
  893. writeb(type, &musbr->rxtype);
  894. /* Write the maximum packet size to the RxMaxp register */
  895. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  896. while (txlen < len) {
  897. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  898. (len-txlen) : dev->epmaxpacketin[ep];
  899. /* Set the ReqPkt bit */
  900. csr = readw(&musbr->rxcsr);
  901. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  902. /* Wait until the RxPktRdy bit is set */
  903. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  904. csr = readw(&musbr->rxcsr);
  905. usb_settoggle(dev, ep, dir_out,
  906. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  907. csr &= ~MUSB_RXCSR_RXPKTRDY;
  908. writew(csr, &musbr->rxcsr);
  909. dev->act_len = txlen;
  910. return 0;
  911. }
  912. /* Read the data from the FIFO */
  913. read_fifo(MUSB_BULK_EP, nextlen,
  914. (void *)(((u8 *)buffer) + txlen));
  915. /* Clear the RxPktRdy bit */
  916. csr = readw(&musbr->rxcsr);
  917. csr &= ~MUSB_RXCSR_RXPKTRDY;
  918. writew(csr, &musbr->rxcsr);
  919. txlen += nextlen;
  920. }
  921. /* Keep a copy of the data toggle bit */
  922. csr = readw(&musbr->rxcsr);
  923. usb_settoggle(dev, ep, dir_out,
  924. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  925. }
  926. /* bulk transfer is complete */
  927. dev->status = 0;
  928. dev->act_len = len;
  929. return 0;
  930. }
  931. /*
  932. * This function initializes the usb controller module.
  933. */
  934. int usb_lowlevel_init(void)
  935. {
  936. u8 power;
  937. u32 timeout;
  938. musb_rh_init();
  939. if (musb_platform_init() == -1)
  940. return -1;
  941. /* Configure all the endpoint FIFO's and start usb controller */
  942. musbr = musb_cfg.regs;
  943. musb_configure_ep(&epinfo[0],
  944. sizeof(epinfo) / sizeof(struct musb_epinfo));
  945. musb_start();
  946. /*
  947. * Wait until musb is enabled in host mode with a timeout. There
  948. * should be a usb device connected.
  949. */
  950. timeout = musb_cfg.timeout;
  951. while (timeout--)
  952. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  953. break;
  954. /* if musb core is not in host mode, then return */
  955. if (!timeout)
  956. return -1;
  957. /* start usb bus reset */
  958. power = readb(&musbr->power);
  959. writeb(power | MUSB_POWER_RESET, &musbr->power);
  960. /* After initiating a usb reset, wait for about 20ms to 30ms */
  961. udelay(30000);
  962. /* stop usb bus reset */
  963. power = readb(&musbr->power);
  964. power &= ~MUSB_POWER_RESET;
  965. writeb(power, &musbr->power);
  966. /* Determine if the connected device is a high/full/low speed device */
  967. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  968. MUSB_TYPE_SPEED_HIGH :
  969. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  970. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  971. return 0;
  972. }
  973. /*
  974. * This function stops the operation of the davinci usb module.
  975. */
  976. int usb_lowlevel_stop(void)
  977. {
  978. /* Reset the USB module */
  979. musb_platform_deinit();
  980. writeb(0, &musbr->devctl);
  981. return 0;
  982. }
  983. /*
  984. * This function supports usb interrupt transfers. Currently, usb interrupt
  985. * transfers are not supported.
  986. */
  987. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  988. void *buffer, int len, int interval)
  989. {
  990. int dir_out = usb_pipeout(pipe);
  991. int ep = usb_pipeendpoint(pipe);
  992. #ifndef MUSB_NO_MULTIPOINT
  993. int devnum = usb_pipedevice(pipe);
  994. #endif
  995. u8 type;
  996. u16 csr;
  997. u32 txlen = 0;
  998. u32 nextlen = 0;
  999. u8 devspeed;
  1000. /* select interrupt endpoint */
  1001. writeb(MUSB_INTR_EP, &musbr->index);
  1002. #ifndef MUSB_NO_MULTIPOINT
  1003. /* write the address of the device */
  1004. if (dir_out)
  1005. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  1006. else
  1007. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  1008. #endif
  1009. /* configure the hub address and the port number as required */
  1010. devspeed = get_dev_speed(dev);
  1011. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  1012. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  1013. /*
  1014. * MUSB is in high speed and the destination device is full
  1015. * speed device. So configure the hub address and port
  1016. * address registers.
  1017. */
  1018. config_hub_port(dev, MUSB_INTR_EP);
  1019. } else {
  1020. #ifndef MUSB_NO_MULTIPOINT
  1021. if (dir_out) {
  1022. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  1023. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  1024. } else {
  1025. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  1026. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  1027. }
  1028. #endif
  1029. devspeed = musb_cfg.musb_speed;
  1030. }
  1031. /* Write the saved toggle bit value */
  1032. write_toggle(dev, ep, dir_out);
  1033. if (!dir_out) { /* intrrupt-in transfer */
  1034. /* Write the saved toggle bit value */
  1035. write_toggle(dev, ep, dir_out);
  1036. writeb(interval, &musbr->rxinterval);
  1037. /* Program the RxType register */
  1038. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  1039. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  1040. (ep & MUSB_TYPE_REMOTE_END);
  1041. writeb(type, &musbr->rxtype);
  1042. /* Write the maximum packet size to the RxMaxp register */
  1043. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  1044. while (txlen < len) {
  1045. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  1046. (len-txlen) : dev->epmaxpacketin[ep];
  1047. /* Set the ReqPkt bit */
  1048. csr = readw(&musbr->rxcsr);
  1049. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  1050. /* Wait until the RxPktRdy bit is set */
  1051. if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
  1052. csr = readw(&musbr->rxcsr);
  1053. usb_settoggle(dev, ep, dir_out,
  1054. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1055. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1056. writew(csr, &musbr->rxcsr);
  1057. dev->act_len = txlen;
  1058. return 0;
  1059. }
  1060. /* Read the data from the FIFO */
  1061. read_fifo(MUSB_INTR_EP, nextlen,
  1062. (void *)(((u8 *)buffer) + txlen));
  1063. /* Clear the RxPktRdy bit */
  1064. csr = readw(&musbr->rxcsr);
  1065. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1066. writew(csr, &musbr->rxcsr);
  1067. txlen += nextlen;
  1068. }
  1069. /* Keep a copy of the data toggle bit */
  1070. csr = readw(&musbr->rxcsr);
  1071. usb_settoggle(dev, ep, dir_out,
  1072. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1073. }
  1074. /* interrupt transfer is complete */
  1075. dev->irq_status = 0;
  1076. dev->irq_act_len = len;
  1077. dev->irq_handle(dev);
  1078. dev->status = 0;
  1079. dev->act_len = len;
  1080. return 0;
  1081. }
  1082. #ifdef CONFIG_SYS_USB_EVENT_POLL
  1083. /*
  1084. * This function polls for USB keyboard data.
  1085. */
  1086. void usb_event_poll()
  1087. {
  1088. struct stdio_dev *dev;
  1089. struct usb_device *usb_kbd_dev;
  1090. struct usb_interface *iface;
  1091. struct usb_endpoint_descriptor *ep;
  1092. int pipe;
  1093. int maxp;
  1094. /* Get the pointer to USB Keyboard device pointer */
  1095. dev = stdio_get_by_name("usbkbd");
  1096. usb_kbd_dev = (struct usb_device *)dev->priv;
  1097. iface = &usb_kbd_dev->config.if_desc[0];
  1098. ep = &iface->ep_desc[0];
  1099. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  1100. /* Submit a interrupt transfer request */
  1101. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  1102. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  1103. maxp > 8 ? 8 : maxp, ep->bInterval);
  1104. }
  1105. #endif /* CONFIG_SYS_USB_EVENT_POLL */