musb_hcd.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. u16 csr;
  724. u8 devspeed;
  725. #ifdef MUSB_NO_MULTIPOINT
  726. /* Control message is for the HUB? */
  727. if (devnum == rh_devnum) {
  728. int stat = musb_submit_rh_msg(dev, pipe, buffer, len, setup);
  729. if (stat)
  730. return stat;
  731. }
  732. #endif
  733. /* select control endpoint */
  734. writeb(MUSB_CONTROL_EP, &musbr->index);
  735. csr = readw(&musbr->txcsr);
  736. #ifndef MUSB_NO_MULTIPOINT
  737. /* target addr and (for multipoint) hub addr/port */
  738. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  739. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  740. #endif
  741. /* configure the hub address and the port number as required */
  742. devspeed = get_dev_speed(dev);
  743. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  744. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  745. config_hub_port(dev, MUSB_CONTROL_EP);
  746. writeb(devspeed << 6, &musbr->txtype);
  747. } else {
  748. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  749. #ifndef MUSB_NO_MULTIPOINT
  750. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  751. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  752. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  753. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  754. #endif
  755. }
  756. /* Control transfer setup phase */
  757. if (ctrlreq_setup_phase(dev, setup) < 0)
  758. return 0;
  759. switch (setup->request) {
  760. case USB_REQ_GET_DESCRIPTOR:
  761. case USB_REQ_GET_CONFIGURATION:
  762. case USB_REQ_GET_INTERFACE:
  763. case USB_REQ_GET_STATUS:
  764. case USB_MSC_BBB_GET_MAX_LUN:
  765. /* control transfer in-data-phase */
  766. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  767. return 0;
  768. /* control transfer out-status-phase */
  769. if (ctrlreq_out_status_phase(dev) < 0)
  770. return 0;
  771. break;
  772. case USB_REQ_SET_ADDRESS:
  773. case USB_REQ_SET_CONFIGURATION:
  774. case USB_REQ_SET_FEATURE:
  775. case USB_REQ_SET_INTERFACE:
  776. case USB_REQ_CLEAR_FEATURE:
  777. case USB_MSC_BBB_RESET:
  778. /* control transfer in status phase */
  779. if (ctrlreq_in_status_phase(dev) < 0)
  780. return 0;
  781. break;
  782. case USB_REQ_SET_DESCRIPTOR:
  783. /* control transfer out data phase */
  784. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  785. return 0;
  786. /* control transfer in status phase */
  787. if (ctrlreq_in_status_phase(dev) < 0)
  788. return 0;
  789. break;
  790. default:
  791. /* unhandled control transfer */
  792. return -1;
  793. }
  794. dev->status = 0;
  795. dev->act_len = len;
  796. #ifdef MUSB_NO_MULTIPOINT
  797. /* Set device address to USB_FADDR register */
  798. if (setup->request == USB_REQ_SET_ADDRESS)
  799. writeb(dev->devnum, &musbr->faddr);
  800. #endif
  801. return len;
  802. }
  803. /*
  804. * do a bulk transfer
  805. */
  806. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  807. void *buffer, int len)
  808. {
  809. int dir_out = usb_pipeout(pipe);
  810. int ep = usb_pipeendpoint(pipe);
  811. #ifndef MUSB_NO_MULTIPOINT
  812. int devnum = usb_pipedevice(pipe);
  813. #endif
  814. u8 type;
  815. u16 csr;
  816. u32 txlen = 0;
  817. u32 nextlen = 0;
  818. u8 devspeed;
  819. /* select bulk endpoint */
  820. writeb(MUSB_BULK_EP, &musbr->index);
  821. #ifndef MUSB_NO_MULTIPOINT
  822. /* write the address of the device */
  823. if (dir_out)
  824. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  825. else
  826. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  827. #endif
  828. /* configure the hub address and the port number as required */
  829. devspeed = get_dev_speed(dev);
  830. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  831. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  832. /*
  833. * MUSB is in high speed and the destination device is full
  834. * speed device. So configure the hub address and port
  835. * address registers.
  836. */
  837. config_hub_port(dev, MUSB_BULK_EP);
  838. } else {
  839. #ifndef MUSB_NO_MULTIPOINT
  840. if (dir_out) {
  841. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  842. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  843. } else {
  844. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  845. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  846. }
  847. #endif
  848. devspeed = musb_cfg.musb_speed;
  849. }
  850. /* Write the saved toggle bit value */
  851. write_toggle(dev, ep, dir_out);
  852. if (dir_out) { /* bulk-out transfer */
  853. /* Program the TxType register */
  854. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  855. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  856. (ep & MUSB_TYPE_REMOTE_END);
  857. writeb(type, &musbr->txtype);
  858. /* Write maximum packet size to the TxMaxp register */
  859. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  860. while (txlen < len) {
  861. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  862. (len-txlen) : dev->epmaxpacketout[ep];
  863. #ifdef CONFIG_USB_BLACKFIN
  864. /* Set the transfer data size */
  865. writew(nextlen, &musbr->txcount);
  866. #endif
  867. /* Write the data to the FIFO */
  868. write_fifo(MUSB_BULK_EP, nextlen,
  869. (void *)(((u8 *)buffer) + txlen));
  870. /* Set the TxPktRdy bit */
  871. csr = readw(&musbr->txcsr);
  872. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  873. /* Wait until the TxPktRdy bit is cleared */
  874. if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
  875. readw(&musbr->txcsr);
  876. usb_settoggle(dev, ep, dir_out,
  877. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  878. dev->act_len = txlen;
  879. return 0;
  880. }
  881. txlen += nextlen;
  882. }
  883. /* Keep a copy of the data toggle bit */
  884. csr = readw(&musbr->txcsr);
  885. usb_settoggle(dev, ep, dir_out,
  886. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  887. } else { /* bulk-in transfer */
  888. /* Write the saved toggle bit value */
  889. write_toggle(dev, ep, dir_out);
  890. /* Program the RxType register */
  891. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  892. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  893. (ep & MUSB_TYPE_REMOTE_END);
  894. writeb(type, &musbr->rxtype);
  895. /* Write the maximum packet size to the RxMaxp register */
  896. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  897. while (txlen < len) {
  898. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  899. (len-txlen) : dev->epmaxpacketin[ep];
  900. /* Set the ReqPkt bit */
  901. csr = readw(&musbr->rxcsr);
  902. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  903. /* Wait until the RxPktRdy bit is set */
  904. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  905. csr = readw(&musbr->rxcsr);
  906. usb_settoggle(dev, ep, dir_out,
  907. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  908. csr &= ~MUSB_RXCSR_RXPKTRDY;
  909. writew(csr, &musbr->rxcsr);
  910. dev->act_len = txlen;
  911. return 0;
  912. }
  913. /* Read the data from the FIFO */
  914. read_fifo(MUSB_BULK_EP, nextlen,
  915. (void *)(((u8 *)buffer) + txlen));
  916. /* Clear the RxPktRdy bit */
  917. csr = readw(&musbr->rxcsr);
  918. csr &= ~MUSB_RXCSR_RXPKTRDY;
  919. writew(csr, &musbr->rxcsr);
  920. txlen += nextlen;
  921. }
  922. /* Keep a copy of the data toggle bit */
  923. csr = readw(&musbr->rxcsr);
  924. usb_settoggle(dev, ep, dir_out,
  925. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  926. }
  927. /* bulk transfer is complete */
  928. dev->status = 0;
  929. dev->act_len = len;
  930. return 0;
  931. }
  932. /*
  933. * This function initializes the usb controller module.
  934. */
  935. int usb_lowlevel_init(void)
  936. {
  937. u8 power;
  938. u32 timeout;
  939. musb_rh_init();
  940. if (musb_platform_init() == -1)
  941. return -1;
  942. /* Configure all the endpoint FIFO's and start usb controller */
  943. musbr = musb_cfg.regs;
  944. musb_configure_ep(&epinfo[0],
  945. sizeof(epinfo) / sizeof(struct musb_epinfo));
  946. musb_start();
  947. /*
  948. * Wait until musb is enabled in host mode with a timeout. There
  949. * should be a usb device connected.
  950. */
  951. timeout = musb_cfg.timeout;
  952. while (timeout--)
  953. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  954. break;
  955. /* if musb core is not in host mode, then return */
  956. if (!timeout)
  957. return -1;
  958. /* start usb bus reset */
  959. power = readb(&musbr->power);
  960. writeb(power | MUSB_POWER_RESET, &musbr->power);
  961. /* After initiating a usb reset, wait for about 20ms to 30ms */
  962. udelay(30000);
  963. /* stop usb bus reset */
  964. power = readb(&musbr->power);
  965. power &= ~MUSB_POWER_RESET;
  966. writeb(power, &musbr->power);
  967. /* Determine if the connected device is a high/full/low speed device */
  968. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  969. MUSB_TYPE_SPEED_HIGH :
  970. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  971. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  972. return 0;
  973. }
  974. /*
  975. * This function stops the operation of the davinci usb module.
  976. */
  977. int usb_lowlevel_stop(void)
  978. {
  979. /* Reset the USB module */
  980. musb_platform_deinit();
  981. writeb(0, &musbr->devctl);
  982. return 0;
  983. }
  984. /*
  985. * This function supports usb interrupt transfers. Currently, usb interrupt
  986. * transfers are not supported.
  987. */
  988. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  989. void *buffer, int len, int interval)
  990. {
  991. int dir_out = usb_pipeout(pipe);
  992. int ep = usb_pipeendpoint(pipe);
  993. #ifndef MUSB_NO_MULTIPOINT
  994. int devnum = usb_pipedevice(pipe);
  995. #endif
  996. u8 type;
  997. u16 csr;
  998. u32 txlen = 0;
  999. u32 nextlen = 0;
  1000. u8 devspeed;
  1001. /* select interrupt endpoint */
  1002. writeb(MUSB_INTR_EP, &musbr->index);
  1003. #ifndef MUSB_NO_MULTIPOINT
  1004. /* write the address of the device */
  1005. if (dir_out)
  1006. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  1007. else
  1008. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  1009. #endif
  1010. /* configure the hub address and the port number as required */
  1011. devspeed = get_dev_speed(dev);
  1012. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  1013. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  1014. /*
  1015. * MUSB is in high speed and the destination device is full
  1016. * speed device. So configure the hub address and port
  1017. * address registers.
  1018. */
  1019. config_hub_port(dev, MUSB_INTR_EP);
  1020. } else {
  1021. #ifndef MUSB_NO_MULTIPOINT
  1022. if (dir_out) {
  1023. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  1024. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  1025. } else {
  1026. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  1027. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  1028. }
  1029. #endif
  1030. devspeed = musb_cfg.musb_speed;
  1031. }
  1032. /* Write the saved toggle bit value */
  1033. write_toggle(dev, ep, dir_out);
  1034. if (!dir_out) { /* intrrupt-in transfer */
  1035. /* Write the saved toggle bit value */
  1036. write_toggle(dev, ep, dir_out);
  1037. writeb(interval, &musbr->rxinterval);
  1038. /* Program the RxType register */
  1039. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  1040. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  1041. (ep & MUSB_TYPE_REMOTE_END);
  1042. writeb(type, &musbr->rxtype);
  1043. /* Write the maximum packet size to the RxMaxp register */
  1044. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  1045. while (txlen < len) {
  1046. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  1047. (len-txlen) : dev->epmaxpacketin[ep];
  1048. /* Set the ReqPkt bit */
  1049. csr = readw(&musbr->rxcsr);
  1050. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  1051. /* Wait until the RxPktRdy bit is set */
  1052. if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
  1053. csr = readw(&musbr->rxcsr);
  1054. usb_settoggle(dev, ep, dir_out,
  1055. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1056. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1057. writew(csr, &musbr->rxcsr);
  1058. dev->act_len = txlen;
  1059. return 0;
  1060. }
  1061. /* Read the data from the FIFO */
  1062. read_fifo(MUSB_INTR_EP, nextlen,
  1063. (void *)(((u8 *)buffer) + txlen));
  1064. /* Clear the RxPktRdy bit */
  1065. csr = readw(&musbr->rxcsr);
  1066. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1067. writew(csr, &musbr->rxcsr);
  1068. txlen += nextlen;
  1069. }
  1070. /* Keep a copy of the data toggle bit */
  1071. csr = readw(&musbr->rxcsr);
  1072. usb_settoggle(dev, ep, dir_out,
  1073. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1074. }
  1075. /* interrupt transfer is complete */
  1076. dev->irq_status = 0;
  1077. dev->irq_act_len = len;
  1078. dev->irq_handle(dev);
  1079. dev->status = 0;
  1080. dev->act_len = len;
  1081. return 0;
  1082. }
  1083. #ifdef CONFIG_SYS_USB_EVENT_POLL
  1084. /*
  1085. * This function polls for USB keyboard data.
  1086. */
  1087. void usb_event_poll()
  1088. {
  1089. struct stdio_dev *dev;
  1090. struct usb_device *usb_kbd_dev;
  1091. struct usb_interface *iface;
  1092. struct usb_endpoint_descriptor *ep;
  1093. int pipe;
  1094. int maxp;
  1095. /* Get the pointer to USB Keyboard device pointer */
  1096. dev = stdio_get_by_name("usbkbd");
  1097. usb_kbd_dev = (struct usb_device *)dev->priv;
  1098. iface = &usb_kbd_dev->config.if_desc[0];
  1099. ep = &iface->ep_desc[0];
  1100. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  1101. /* Submit a interrupt transfer request */
  1102. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  1103. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  1104. maxp > 8 ? 8 : maxp, ep->bInterval);
  1105. }
  1106. #endif /* CONFIG_SYS_USB_EVENT_POLL */