musb_gadget_ep0.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * MUSB OTG peripheral driver ep0 handling
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  26. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  28. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  29. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #include <linux/kernel.h>
  36. #include <linux/list.h>
  37. #include <linux/timer.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/device.h>
  40. #include <linux/interrupt.h>
  41. #include "musb_core.h"
  42. /* ep0 is always musb->endpoints[0].ep_in */
  43. #define next_ep0_request(musb) next_in_request(&(musb)->endpoints[0])
  44. /*
  45. * locking note: we use only the controller lock, for simpler correctness.
  46. * It's always held with IRQs blocked.
  47. *
  48. * It protects the ep0 request queue as well as ep0_state, not just the
  49. * controller and indexed registers. And that lock stays held unless it
  50. * needs to be dropped to allow reentering this driver ... like upcalls to
  51. * the gadget driver, or adjusting endpoint halt status.
  52. */
  53. static char *decode_ep0stage(u8 stage)
  54. {
  55. switch (stage) {
  56. case MUSB_EP0_STAGE_IDLE: return "idle";
  57. case MUSB_EP0_STAGE_SETUP: return "setup";
  58. case MUSB_EP0_STAGE_TX: return "in";
  59. case MUSB_EP0_STAGE_RX: return "out";
  60. case MUSB_EP0_STAGE_ACKWAIT: return "wait";
  61. case MUSB_EP0_STAGE_STATUSIN: return "in/status";
  62. case MUSB_EP0_STAGE_STATUSOUT: return "out/status";
  63. default: return "?";
  64. }
  65. }
  66. /* handle a standard GET_STATUS request
  67. * Context: caller holds controller lock
  68. */
  69. static int service_tx_status_request(
  70. struct musb *musb,
  71. const struct usb_ctrlrequest *ctrlrequest)
  72. {
  73. void __iomem *mbase = musb->mregs;
  74. int handled = 1;
  75. u8 result[2], epnum = 0;
  76. const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
  77. result[1] = 0;
  78. switch (recip) {
  79. case USB_RECIP_DEVICE:
  80. result[0] = musb->g.is_selfpowered << USB_DEVICE_SELF_POWERED;
  81. result[0] |= musb->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  82. if (musb->g.is_otg) {
  83. result[0] |= musb->g.b_hnp_enable
  84. << USB_DEVICE_B_HNP_ENABLE;
  85. result[0] |= musb->g.a_alt_hnp_support
  86. << USB_DEVICE_A_ALT_HNP_SUPPORT;
  87. result[0] |= musb->g.a_hnp_support
  88. << USB_DEVICE_A_HNP_SUPPORT;
  89. }
  90. break;
  91. case USB_RECIP_INTERFACE:
  92. result[0] = 0;
  93. break;
  94. case USB_RECIP_ENDPOINT: {
  95. int is_in;
  96. struct musb_ep *ep;
  97. u16 tmp;
  98. void __iomem *regs;
  99. epnum = (u8) ctrlrequest->wIndex;
  100. if (!epnum) {
  101. result[0] = 0;
  102. break;
  103. }
  104. is_in = epnum & USB_DIR_IN;
  105. epnum &= 0x0f;
  106. if (epnum >= MUSB_C_NUM_EPS) {
  107. handled = -EINVAL;
  108. break;
  109. }
  110. if (is_in)
  111. ep = &musb->endpoints[epnum].ep_in;
  112. else
  113. ep = &musb->endpoints[epnum].ep_out;
  114. regs = musb->endpoints[epnum].regs;
  115. if (!ep->desc) {
  116. handled = -EINVAL;
  117. break;
  118. }
  119. musb_ep_select(mbase, epnum);
  120. if (is_in)
  121. tmp = musb_readw(regs, MUSB_TXCSR)
  122. & MUSB_TXCSR_P_SENDSTALL;
  123. else
  124. tmp = musb_readw(regs, MUSB_RXCSR)
  125. & MUSB_RXCSR_P_SENDSTALL;
  126. musb_ep_select(mbase, 0);
  127. result[0] = tmp ? 1 : 0;
  128. } break;
  129. default:
  130. /* class, vendor, etc ... delegate */
  131. handled = 0;
  132. break;
  133. }
  134. /* fill up the fifo; caller updates csr0 */
  135. if (handled > 0) {
  136. u16 len = le16_to_cpu(ctrlrequest->wLength);
  137. if (len > 2)
  138. len = 2;
  139. musb_write_fifo(&musb->endpoints[0], len, result);
  140. }
  141. return handled;
  142. }
  143. /*
  144. * handle a control-IN request, the end0 buffer contains the current request
  145. * that is supposed to be a standard control request. Assumes the fifo to
  146. * be at least 2 bytes long.
  147. *
  148. * @return 0 if the request was NOT HANDLED,
  149. * < 0 when error
  150. * > 0 when the request is processed
  151. *
  152. * Context: caller holds controller lock
  153. */
  154. static int
  155. service_in_request(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
  156. {
  157. int handled = 0; /* not handled */
  158. if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
  159. == USB_TYPE_STANDARD) {
  160. switch (ctrlrequest->bRequest) {
  161. case USB_REQ_GET_STATUS:
  162. handled = service_tx_status_request(musb,
  163. ctrlrequest);
  164. break;
  165. /* case USB_REQ_SYNC_FRAME: */
  166. default:
  167. break;
  168. }
  169. }
  170. return handled;
  171. }
  172. /*
  173. * Context: caller holds controller lock
  174. */
  175. static void musb_g_ep0_giveback(struct musb *musb, struct usb_request *req)
  176. {
  177. musb_g_giveback(&musb->endpoints[0].ep_in, req, 0);
  178. }
  179. /*
  180. * Tries to start B-device HNP negotiation if enabled via sysfs
  181. */
  182. static inline void musb_try_b_hnp_enable(struct musb *musb)
  183. {
  184. void __iomem *mbase = musb->mregs;
  185. u8 devctl;
  186. dev_dbg(musb->controller, "HNP: Setting HR\n");
  187. devctl = musb_readb(mbase, MUSB_DEVCTL);
  188. musb_writeb(mbase, MUSB_DEVCTL, devctl | MUSB_DEVCTL_HR);
  189. }
  190. /*
  191. * Handle all control requests with no DATA stage, including standard
  192. * requests such as:
  193. * USB_REQ_SET_CONFIGURATION, USB_REQ_SET_INTERFACE, unrecognized
  194. * always delegated to the gadget driver
  195. * USB_REQ_SET_ADDRESS, USB_REQ_CLEAR_FEATURE, USB_REQ_SET_FEATURE
  196. * always handled here, except for class/vendor/... features
  197. *
  198. * Context: caller holds controller lock
  199. */
  200. static int
  201. service_zero_data_request(struct musb *musb,
  202. struct usb_ctrlrequest *ctrlrequest)
  203. __releases(musb->lock)
  204. __acquires(musb->lock)
  205. {
  206. int handled = -EINVAL;
  207. void __iomem *mbase = musb->mregs;
  208. const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
  209. /* the gadget driver handles everything except what we MUST handle */
  210. if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
  211. == USB_TYPE_STANDARD) {
  212. switch (ctrlrequest->bRequest) {
  213. case USB_REQ_SET_ADDRESS:
  214. /* change it after the status stage */
  215. musb->set_address = true;
  216. musb->address = (u8) (ctrlrequest->wValue & 0x7f);
  217. handled = 1;
  218. break;
  219. case USB_REQ_CLEAR_FEATURE:
  220. switch (recip) {
  221. case USB_RECIP_DEVICE:
  222. if (ctrlrequest->wValue
  223. != USB_DEVICE_REMOTE_WAKEUP)
  224. break;
  225. musb->may_wakeup = 0;
  226. handled = 1;
  227. break;
  228. case USB_RECIP_INTERFACE:
  229. break;
  230. case USB_RECIP_ENDPOINT:{
  231. const u8 epnum =
  232. ctrlrequest->wIndex & 0x0f;
  233. struct musb_ep *musb_ep;
  234. struct musb_hw_ep *ep;
  235. struct musb_request *request;
  236. void __iomem *regs;
  237. int is_in;
  238. u16 csr;
  239. if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
  240. ctrlrequest->wValue != USB_ENDPOINT_HALT)
  241. break;
  242. ep = musb->endpoints + epnum;
  243. regs = ep->regs;
  244. is_in = ctrlrequest->wIndex & USB_DIR_IN;
  245. if (is_in)
  246. musb_ep = &ep->ep_in;
  247. else
  248. musb_ep = &ep->ep_out;
  249. if (!musb_ep->desc)
  250. break;
  251. handled = 1;
  252. /* Ignore request if endpoint is wedged */
  253. if (musb_ep->wedged)
  254. break;
  255. musb_ep_select(mbase, epnum);
  256. if (is_in) {
  257. csr = musb_readw(regs, MUSB_TXCSR);
  258. csr |= MUSB_TXCSR_CLRDATATOG |
  259. MUSB_TXCSR_P_WZC_BITS;
  260. csr &= ~(MUSB_TXCSR_P_SENDSTALL |
  261. MUSB_TXCSR_P_SENTSTALL |
  262. MUSB_TXCSR_TXPKTRDY);
  263. musb_writew(regs, MUSB_TXCSR, csr);
  264. } else {
  265. csr = musb_readw(regs, MUSB_RXCSR);
  266. csr |= MUSB_RXCSR_CLRDATATOG |
  267. MUSB_RXCSR_P_WZC_BITS;
  268. csr &= ~(MUSB_RXCSR_P_SENDSTALL |
  269. MUSB_RXCSR_P_SENTSTALL);
  270. musb_writew(regs, MUSB_RXCSR, csr);
  271. }
  272. /* Maybe start the first request in the queue */
  273. request = next_request(musb_ep);
  274. if (!musb_ep->busy && request) {
  275. dev_dbg(musb->controller, "restarting the request\n");
  276. musb_ep_restart(musb, request);
  277. }
  278. /* select ep0 again */
  279. musb_ep_select(mbase, 0);
  280. } break;
  281. default:
  282. /* class, vendor, etc ... delegate */
  283. handled = 0;
  284. break;
  285. }
  286. break;
  287. case USB_REQ_SET_FEATURE:
  288. switch (recip) {
  289. case USB_RECIP_DEVICE:
  290. handled = 1;
  291. switch (ctrlrequest->wValue) {
  292. case USB_DEVICE_REMOTE_WAKEUP:
  293. musb->may_wakeup = 1;
  294. break;
  295. case USB_DEVICE_TEST_MODE:
  296. if (musb->g.speed != USB_SPEED_HIGH)
  297. goto stall;
  298. if (ctrlrequest->wIndex & 0xff)
  299. goto stall;
  300. switch (ctrlrequest->wIndex >> 8) {
  301. case 1:
  302. pr_debug("TEST_J\n");
  303. /* TEST_J */
  304. musb->test_mode_nr =
  305. MUSB_TEST_J;
  306. break;
  307. case 2:
  308. /* TEST_K */
  309. pr_debug("TEST_K\n");
  310. musb->test_mode_nr =
  311. MUSB_TEST_K;
  312. break;
  313. case 3:
  314. /* TEST_SE0_NAK */
  315. pr_debug("TEST_SE0_NAK\n");
  316. musb->test_mode_nr =
  317. MUSB_TEST_SE0_NAK;
  318. break;
  319. case 4:
  320. /* TEST_PACKET */
  321. pr_debug("TEST_PACKET\n");
  322. musb->test_mode_nr =
  323. MUSB_TEST_PACKET;
  324. break;
  325. case 0xc0:
  326. /* TEST_FORCE_HS */
  327. pr_debug("TEST_FORCE_HS\n");
  328. musb->test_mode_nr =
  329. MUSB_TEST_FORCE_HS;
  330. break;
  331. case 0xc1:
  332. /* TEST_FORCE_FS */
  333. pr_debug("TEST_FORCE_FS\n");
  334. musb->test_mode_nr =
  335. MUSB_TEST_FORCE_FS;
  336. break;
  337. case 0xc2:
  338. /* TEST_FIFO_ACCESS */
  339. pr_debug("TEST_FIFO_ACCESS\n");
  340. musb->test_mode_nr =
  341. MUSB_TEST_FIFO_ACCESS;
  342. break;
  343. case 0xc3:
  344. /* TEST_FORCE_HOST */
  345. pr_debug("TEST_FORCE_HOST\n");
  346. musb->test_mode_nr =
  347. MUSB_TEST_FORCE_HOST;
  348. break;
  349. default:
  350. goto stall;
  351. }
  352. /* enter test mode after irq */
  353. if (handled > 0)
  354. musb->test_mode = true;
  355. break;
  356. case USB_DEVICE_B_HNP_ENABLE:
  357. if (!musb->g.is_otg)
  358. goto stall;
  359. musb->g.b_hnp_enable = 1;
  360. musb_try_b_hnp_enable(musb);
  361. break;
  362. case USB_DEVICE_A_HNP_SUPPORT:
  363. if (!musb->g.is_otg)
  364. goto stall;
  365. musb->g.a_hnp_support = 1;
  366. break;
  367. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  368. if (!musb->g.is_otg)
  369. goto stall;
  370. musb->g.a_alt_hnp_support = 1;
  371. break;
  372. case USB_DEVICE_DEBUG_MODE:
  373. handled = 0;
  374. break;
  375. stall:
  376. default:
  377. handled = -EINVAL;
  378. break;
  379. }
  380. break;
  381. case USB_RECIP_INTERFACE:
  382. break;
  383. case USB_RECIP_ENDPOINT:{
  384. const u8 epnum =
  385. ctrlrequest->wIndex & 0x0f;
  386. struct musb_ep *musb_ep;
  387. struct musb_hw_ep *ep;
  388. void __iomem *regs;
  389. int is_in;
  390. u16 csr;
  391. if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
  392. ctrlrequest->wValue != USB_ENDPOINT_HALT)
  393. break;
  394. ep = musb->endpoints + epnum;
  395. regs = ep->regs;
  396. is_in = ctrlrequest->wIndex & USB_DIR_IN;
  397. if (is_in)
  398. musb_ep = &ep->ep_in;
  399. else
  400. musb_ep = &ep->ep_out;
  401. if (!musb_ep->desc)
  402. break;
  403. musb_ep_select(mbase, epnum);
  404. if (is_in) {
  405. csr = musb_readw(regs, MUSB_TXCSR);
  406. if (csr & MUSB_TXCSR_FIFONOTEMPTY)
  407. csr |= MUSB_TXCSR_FLUSHFIFO;
  408. csr |= MUSB_TXCSR_P_SENDSTALL
  409. | MUSB_TXCSR_CLRDATATOG
  410. | MUSB_TXCSR_P_WZC_BITS;
  411. musb_writew(regs, MUSB_TXCSR, csr);
  412. } else {
  413. csr = musb_readw(regs, MUSB_RXCSR);
  414. csr |= MUSB_RXCSR_P_SENDSTALL
  415. | MUSB_RXCSR_FLUSHFIFO
  416. | MUSB_RXCSR_CLRDATATOG
  417. | MUSB_RXCSR_P_WZC_BITS;
  418. musb_writew(regs, MUSB_RXCSR, csr);
  419. }
  420. /* select ep0 again */
  421. musb_ep_select(mbase, 0);
  422. handled = 1;
  423. } break;
  424. default:
  425. /* class, vendor, etc ... delegate */
  426. handled = 0;
  427. break;
  428. }
  429. break;
  430. default:
  431. /* delegate SET_CONFIGURATION, etc */
  432. handled = 0;
  433. }
  434. } else
  435. handled = 0;
  436. return handled;
  437. }
  438. /* we have an ep0out data packet
  439. * Context: caller holds controller lock
  440. */
  441. static void ep0_rxstate(struct musb *musb)
  442. {
  443. void __iomem *regs = musb->control_ep->regs;
  444. struct musb_request *request;
  445. struct usb_request *req;
  446. u16 count, csr;
  447. request = next_ep0_request(musb);
  448. req = &request->request;
  449. /* read packet and ack; or stall because of gadget driver bug:
  450. * should have provided the rx buffer before setup() returned.
  451. */
  452. if (req) {
  453. void *buf = req->buf + req->actual;
  454. unsigned len = req->length - req->actual;
  455. /* read the buffer */
  456. count = musb_readb(regs, MUSB_COUNT0);
  457. if (count > len) {
  458. req->status = -EOVERFLOW;
  459. count = len;
  460. }
  461. if (count > 0) {
  462. musb_read_fifo(&musb->endpoints[0], count, buf);
  463. req->actual += count;
  464. }
  465. csr = MUSB_CSR0_P_SVDRXPKTRDY;
  466. if (count < 64 || req->actual == req->length) {
  467. musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
  468. csr |= MUSB_CSR0_P_DATAEND;
  469. } else
  470. req = NULL;
  471. } else
  472. csr = MUSB_CSR0_P_SVDRXPKTRDY | MUSB_CSR0_P_SENDSTALL;
  473. /* Completion handler may choose to stall, e.g. because the
  474. * message just received holds invalid data.
  475. */
  476. if (req) {
  477. musb->ackpend = csr;
  478. musb_g_ep0_giveback(musb, req);
  479. if (!musb->ackpend)
  480. return;
  481. musb->ackpend = 0;
  482. }
  483. musb_ep_select(musb->mregs, 0);
  484. musb_writew(regs, MUSB_CSR0, csr);
  485. }
  486. /*
  487. * transmitting to the host (IN), this code might be called from IRQ
  488. * and from kernel thread.
  489. *
  490. * Context: caller holds controller lock
  491. */
  492. static void ep0_txstate(struct musb *musb)
  493. {
  494. void __iomem *regs = musb->control_ep->regs;
  495. struct musb_request *req = next_ep0_request(musb);
  496. struct usb_request *request;
  497. u16 csr = MUSB_CSR0_TXPKTRDY;
  498. u8 *fifo_src;
  499. u8 fifo_count;
  500. if (!req) {
  501. /* WARN_ON(1); */
  502. dev_dbg(musb->controller, "odd; csr0 %04x\n", musb_readw(regs, MUSB_CSR0));
  503. return;
  504. }
  505. request = &req->request;
  506. /* load the data */
  507. fifo_src = (u8 *) request->buf + request->actual;
  508. fifo_count = min((unsigned) MUSB_EP0_FIFOSIZE,
  509. request->length - request->actual);
  510. musb_write_fifo(&musb->endpoints[0], fifo_count, fifo_src);
  511. request->actual += fifo_count;
  512. /* update the flags */
  513. if (fifo_count < MUSB_MAX_END0_PACKET
  514. || (request->actual == request->length
  515. && !request->zero)) {
  516. musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
  517. csr |= MUSB_CSR0_P_DATAEND;
  518. } else
  519. request = NULL;
  520. /* report completions as soon as the fifo's loaded; there's no
  521. * win in waiting till this last packet gets acked. (other than
  522. * very precise fault reporting, needed by USB TMC; possible with
  523. * this hardware, but not usable from portable gadget drivers.)
  524. */
  525. if (request) {
  526. musb->ackpend = csr;
  527. musb_g_ep0_giveback(musb, request);
  528. if (!musb->ackpend)
  529. return;
  530. musb->ackpend = 0;
  531. }
  532. /* send it out, triggering a "txpktrdy cleared" irq */
  533. musb_ep_select(musb->mregs, 0);
  534. musb_writew(regs, MUSB_CSR0, csr);
  535. }
  536. /*
  537. * Read a SETUP packet (struct usb_ctrlrequest) from the hardware.
  538. * Fields are left in USB byte-order.
  539. *
  540. * Context: caller holds controller lock.
  541. */
  542. static void
  543. musb_read_setup(struct musb *musb, struct usb_ctrlrequest *req)
  544. {
  545. struct musb_request *r;
  546. void __iomem *regs = musb->control_ep->regs;
  547. musb_read_fifo(&musb->endpoints[0], sizeof *req, (u8 *)req);
  548. /* NOTE: earlier 2.6 versions changed setup packets to host
  549. * order, but now USB packets always stay in USB byte order.
  550. */
  551. dev_dbg(musb->controller, "SETUP req%02x.%02x v%04x i%04x l%d\n",
  552. req->bRequestType,
  553. req->bRequest,
  554. le16_to_cpu(req->wValue),
  555. le16_to_cpu(req->wIndex),
  556. le16_to_cpu(req->wLength));
  557. /* clean up any leftover transfers */
  558. r = next_ep0_request(musb);
  559. if (r)
  560. musb_g_ep0_giveback(musb, &r->request);
  561. /* For zero-data requests we want to delay the STATUS stage to
  562. * avoid SETUPEND errors. If we read data (OUT), delay accepting
  563. * packets until there's a buffer to store them in.
  564. *
  565. * If we write data, the controller acts happier if we enable
  566. * the TX FIFO right away, and give the controller a moment
  567. * to switch modes...
  568. */
  569. musb->set_address = false;
  570. musb->ackpend = MUSB_CSR0_P_SVDRXPKTRDY;
  571. if (req->wLength == 0) {
  572. if (req->bRequestType & USB_DIR_IN)
  573. musb->ackpend |= MUSB_CSR0_TXPKTRDY;
  574. musb->ep0_state = MUSB_EP0_STAGE_ACKWAIT;
  575. } else if (req->bRequestType & USB_DIR_IN) {
  576. musb->ep0_state = MUSB_EP0_STAGE_TX;
  577. musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDRXPKTRDY);
  578. while ((musb_readw(regs, MUSB_CSR0)
  579. & MUSB_CSR0_RXPKTRDY) != 0)
  580. cpu_relax();
  581. musb->ackpend = 0;
  582. } else
  583. musb->ep0_state = MUSB_EP0_STAGE_RX;
  584. }
  585. static int
  586. forward_to_driver(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
  587. __releases(musb->lock)
  588. __acquires(musb->lock)
  589. {
  590. int retval;
  591. if (!musb->gadget_driver)
  592. return -EOPNOTSUPP;
  593. spin_unlock(&musb->lock);
  594. retval = musb->gadget_driver->setup(&musb->g, ctrlrequest);
  595. spin_lock(&musb->lock);
  596. return retval;
  597. }
  598. /*
  599. * Handle peripheral ep0 interrupt
  600. *
  601. * Context: irq handler; we won't re-enter the driver that way.
  602. */
  603. irqreturn_t musb_g_ep0_irq(struct musb *musb)
  604. {
  605. u16 csr;
  606. u16 len;
  607. void __iomem *mbase = musb->mregs;
  608. void __iomem *regs = musb->endpoints[0].regs;
  609. irqreturn_t retval = IRQ_NONE;
  610. musb_ep_select(mbase, 0); /* select ep0 */
  611. csr = musb_readw(regs, MUSB_CSR0);
  612. len = musb_readb(regs, MUSB_COUNT0);
  613. dev_dbg(musb->controller, "csr %04x, count %d, ep0stage %s\n",
  614. csr, len, decode_ep0stage(musb->ep0_state));
  615. if (csr & MUSB_CSR0_P_DATAEND) {
  616. /*
  617. * If DATAEND is set we should not call the callback,
  618. * hence the status stage is not complete.
  619. */
  620. return IRQ_HANDLED;
  621. }
  622. /* I sent a stall.. need to acknowledge it now.. */
  623. if (csr & MUSB_CSR0_P_SENTSTALL) {
  624. musb_writew(regs, MUSB_CSR0,
  625. csr & ~MUSB_CSR0_P_SENTSTALL);
  626. retval = IRQ_HANDLED;
  627. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  628. csr = musb_readw(regs, MUSB_CSR0);
  629. }
  630. /* request ended "early" */
  631. if (csr & MUSB_CSR0_P_SETUPEND) {
  632. musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDSETUPEND);
  633. retval = IRQ_HANDLED;
  634. /* Transition into the early status phase */
  635. switch (musb->ep0_state) {
  636. case MUSB_EP0_STAGE_TX:
  637. musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
  638. break;
  639. case MUSB_EP0_STAGE_RX:
  640. musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
  641. break;
  642. default:
  643. ERR("SetupEnd came in a wrong ep0stage %s\n",
  644. decode_ep0stage(musb->ep0_state));
  645. }
  646. csr = musb_readw(regs, MUSB_CSR0);
  647. /* NOTE: request may need completion */
  648. }
  649. /* docs from Mentor only describe tx, rx, and idle/setup states.
  650. * we need to handle nuances around status stages, and also the
  651. * case where status and setup stages come back-to-back ...
  652. */
  653. switch (musb->ep0_state) {
  654. case MUSB_EP0_STAGE_TX:
  655. /* irq on clearing txpktrdy */
  656. if ((csr & MUSB_CSR0_TXPKTRDY) == 0) {
  657. ep0_txstate(musb);
  658. retval = IRQ_HANDLED;
  659. }
  660. break;
  661. case MUSB_EP0_STAGE_RX:
  662. /* irq on set rxpktrdy */
  663. if (csr & MUSB_CSR0_RXPKTRDY) {
  664. ep0_rxstate(musb);
  665. retval = IRQ_HANDLED;
  666. }
  667. break;
  668. case MUSB_EP0_STAGE_STATUSIN:
  669. /* end of sequence #2 (OUT/RX state) or #3 (no data) */
  670. /* update address (if needed) only @ the end of the
  671. * status phase per usb spec, which also guarantees
  672. * we get 10 msec to receive this irq... until this
  673. * is done we won't see the next packet.
  674. */
  675. if (musb->set_address) {
  676. musb->set_address = false;
  677. musb_writeb(mbase, MUSB_FADDR, musb->address);
  678. }
  679. /* enter test mode if needed (exit by reset) */
  680. else if (musb->test_mode) {
  681. dev_dbg(musb->controller, "entering TESTMODE\n");
  682. if (MUSB_TEST_PACKET == musb->test_mode_nr)
  683. musb_load_testpacket(musb);
  684. musb_writeb(mbase, MUSB_TESTMODE,
  685. musb->test_mode_nr);
  686. }
  687. /* FALLTHROUGH */
  688. case MUSB_EP0_STAGE_STATUSOUT:
  689. /* end of sequence #1: write to host (TX state) */
  690. {
  691. struct musb_request *req;
  692. req = next_ep0_request(musb);
  693. if (req)
  694. musb_g_ep0_giveback(musb, &req->request);
  695. }
  696. /*
  697. * In case when several interrupts can get coalesced,
  698. * check to see if we've already received a SETUP packet...
  699. */
  700. if (csr & MUSB_CSR0_RXPKTRDY)
  701. goto setup;
  702. retval = IRQ_HANDLED;
  703. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  704. break;
  705. case MUSB_EP0_STAGE_IDLE:
  706. /*
  707. * This state is typically (but not always) indiscernible
  708. * from the status states since the corresponding interrupts
  709. * tend to happen within too little period of time (with only
  710. * a zero-length packet in between) and so get coalesced...
  711. */
  712. retval = IRQ_HANDLED;
  713. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  714. /* FALLTHROUGH */
  715. case MUSB_EP0_STAGE_SETUP:
  716. setup:
  717. if (csr & MUSB_CSR0_RXPKTRDY) {
  718. struct usb_ctrlrequest setup;
  719. int handled = 0;
  720. if (len != 8) {
  721. ERR("SETUP packet len %d != 8 ?\n", len);
  722. break;
  723. }
  724. musb_read_setup(musb, &setup);
  725. retval = IRQ_HANDLED;
  726. /* sometimes the RESET won't be reported */
  727. if (unlikely(musb->g.speed == USB_SPEED_UNKNOWN)) {
  728. u8 power;
  729. printk(KERN_NOTICE "%s: peripheral reset "
  730. "irq lost!\n",
  731. musb_driver_name);
  732. power = musb_readb(mbase, MUSB_POWER);
  733. musb->g.speed = (power & MUSB_POWER_HSMODE)
  734. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  735. }
  736. switch (musb->ep0_state) {
  737. /* sequence #3 (no data stage), includes requests
  738. * we can't forward (notably SET_ADDRESS and the
  739. * device/endpoint feature set/clear operations)
  740. * plus SET_CONFIGURATION and others we must
  741. */
  742. case MUSB_EP0_STAGE_ACKWAIT:
  743. handled = service_zero_data_request(
  744. musb, &setup);
  745. /*
  746. * We're expecting no data in any case, so
  747. * always set the DATAEND bit -- doing this
  748. * here helps avoid SetupEnd interrupt coming
  749. * in the idle stage when we're stalling...
  750. */
  751. musb->ackpend |= MUSB_CSR0_P_DATAEND;
  752. /* status stage might be immediate */
  753. if (handled > 0)
  754. musb->ep0_state =
  755. MUSB_EP0_STAGE_STATUSIN;
  756. break;
  757. /* sequence #1 (IN to host), includes GET_STATUS
  758. * requests that we can't forward, GET_DESCRIPTOR
  759. * and others that we must
  760. */
  761. case MUSB_EP0_STAGE_TX:
  762. handled = service_in_request(musb, &setup);
  763. if (handled > 0) {
  764. musb->ackpend = MUSB_CSR0_TXPKTRDY
  765. | MUSB_CSR0_P_DATAEND;
  766. musb->ep0_state =
  767. MUSB_EP0_STAGE_STATUSOUT;
  768. }
  769. break;
  770. /* sequence #2 (OUT from host), always forward */
  771. default: /* MUSB_EP0_STAGE_RX */
  772. break;
  773. }
  774. dev_dbg(musb->controller, "handled %d, csr %04x, ep0stage %s\n",
  775. handled, csr,
  776. decode_ep0stage(musb->ep0_state));
  777. /* unless we need to delegate this to the gadget
  778. * driver, we know how to wrap this up: csr0 has
  779. * not yet been written.
  780. */
  781. if (handled < 0)
  782. goto stall;
  783. else if (handled > 0)
  784. goto finish;
  785. handled = forward_to_driver(musb, &setup);
  786. if (handled < 0) {
  787. musb_ep_select(mbase, 0);
  788. stall:
  789. dev_dbg(musb->controller, "stall (%d)\n", handled);
  790. musb->ackpend |= MUSB_CSR0_P_SENDSTALL;
  791. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  792. finish:
  793. musb_writew(regs, MUSB_CSR0,
  794. musb->ackpend);
  795. musb->ackpend = 0;
  796. }
  797. }
  798. break;
  799. case MUSB_EP0_STAGE_ACKWAIT:
  800. /* This should not happen. But happens with tusb6010 with
  801. * g_file_storage and high speed. Do nothing.
  802. */
  803. retval = IRQ_HANDLED;
  804. break;
  805. default:
  806. /* "can't happen" */
  807. WARN_ON(1);
  808. musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SENDSTALL);
  809. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  810. break;
  811. }
  812. return retval;
  813. }
  814. static int
  815. musb_g_ep0_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *desc)
  816. {
  817. /* always enabled */
  818. return -EINVAL;
  819. }
  820. static int musb_g_ep0_disable(struct usb_ep *e)
  821. {
  822. /* always enabled */
  823. return -EINVAL;
  824. }
  825. static int
  826. musb_g_ep0_queue(struct usb_ep *e, struct usb_request *r, gfp_t gfp_flags)
  827. {
  828. struct musb_ep *ep;
  829. struct musb_request *req;
  830. struct musb *musb;
  831. int status;
  832. unsigned long lockflags;
  833. void __iomem *regs;
  834. if (!e || !r)
  835. return -EINVAL;
  836. ep = to_musb_ep(e);
  837. musb = ep->musb;
  838. regs = musb->control_ep->regs;
  839. req = to_musb_request(r);
  840. req->musb = musb;
  841. req->request.actual = 0;
  842. req->request.status = -EINPROGRESS;
  843. req->tx = ep->is_in;
  844. spin_lock_irqsave(&musb->lock, lockflags);
  845. if (!list_empty(&ep->req_list)) {
  846. status = -EBUSY;
  847. goto cleanup;
  848. }
  849. switch (musb->ep0_state) {
  850. case MUSB_EP0_STAGE_RX: /* control-OUT data */
  851. case MUSB_EP0_STAGE_TX: /* control-IN data */
  852. case MUSB_EP0_STAGE_ACKWAIT: /* zero-length data */
  853. status = 0;
  854. break;
  855. default:
  856. dev_dbg(musb->controller, "ep0 request queued in state %d\n",
  857. musb->ep0_state);
  858. status = -EINVAL;
  859. goto cleanup;
  860. }
  861. /* add request to the list */
  862. list_add_tail(&req->list, &ep->req_list);
  863. dev_dbg(musb->controller, "queue to %s (%s), length=%d\n",
  864. ep->name, ep->is_in ? "IN/TX" : "OUT/RX",
  865. req->request.length);
  866. musb_ep_select(musb->mregs, 0);
  867. /* sequence #1, IN ... start writing the data */
  868. if (musb->ep0_state == MUSB_EP0_STAGE_TX)
  869. ep0_txstate(musb);
  870. /* sequence #3, no-data ... issue IN status */
  871. else if (musb->ep0_state == MUSB_EP0_STAGE_ACKWAIT) {
  872. if (req->request.length)
  873. status = -EINVAL;
  874. else {
  875. musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
  876. musb_writew(regs, MUSB_CSR0,
  877. musb->ackpend | MUSB_CSR0_P_DATAEND);
  878. musb->ackpend = 0;
  879. musb_g_ep0_giveback(ep->musb, r);
  880. }
  881. /* else for sequence #2 (OUT), caller provides a buffer
  882. * before the next packet arrives. deferred responses
  883. * (after SETUP is acked) are racey.
  884. */
  885. } else if (musb->ackpend) {
  886. musb_writew(regs, MUSB_CSR0, musb->ackpend);
  887. musb->ackpend = 0;
  888. }
  889. cleanup:
  890. spin_unlock_irqrestore(&musb->lock, lockflags);
  891. return status;
  892. }
  893. static int musb_g_ep0_dequeue(struct usb_ep *ep, struct usb_request *req)
  894. {
  895. /* we just won't support this */
  896. return -EINVAL;
  897. }
  898. static int musb_g_ep0_halt(struct usb_ep *e, int value)
  899. {
  900. struct musb_ep *ep;
  901. struct musb *musb;
  902. void __iomem *base, *regs;
  903. unsigned long flags;
  904. int status;
  905. u16 csr;
  906. if (!e || !value)
  907. return -EINVAL;
  908. ep = to_musb_ep(e);
  909. musb = ep->musb;
  910. base = musb->mregs;
  911. regs = musb->control_ep->regs;
  912. status = 0;
  913. spin_lock_irqsave(&musb->lock, flags);
  914. if (!list_empty(&ep->req_list)) {
  915. status = -EBUSY;
  916. goto cleanup;
  917. }
  918. musb_ep_select(base, 0);
  919. csr = musb->ackpend;
  920. switch (musb->ep0_state) {
  921. /* Stalls are usually issued after parsing SETUP packet, either
  922. * directly in irq context from setup() or else later.
  923. */
  924. case MUSB_EP0_STAGE_TX: /* control-IN data */
  925. case MUSB_EP0_STAGE_ACKWAIT: /* STALL for zero-length data */
  926. case MUSB_EP0_STAGE_RX: /* control-OUT data */
  927. csr = musb_readw(regs, MUSB_CSR0);
  928. /* FALLTHROUGH */
  929. /* It's also OK to issue stalls during callbacks when a non-empty
  930. * DATA stage buffer has been read (or even written).
  931. */
  932. case MUSB_EP0_STAGE_STATUSIN: /* control-OUT status */
  933. case MUSB_EP0_STAGE_STATUSOUT: /* control-IN status */
  934. csr |= MUSB_CSR0_P_SENDSTALL;
  935. musb_writew(regs, MUSB_CSR0, csr);
  936. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  937. musb->ackpend = 0;
  938. break;
  939. default:
  940. dev_dbg(musb->controller, "ep0 can't halt in state %d\n", musb->ep0_state);
  941. status = -EINVAL;
  942. }
  943. cleanup:
  944. spin_unlock_irqrestore(&musb->lock, flags);
  945. return status;
  946. }
  947. const struct usb_ep_ops musb_g_ep0_ops = {
  948. .enable = musb_g_ep0_enable,
  949. .disable = musb_g_ep0_disable,
  950. .alloc_request = musb_alloc_request,
  951. .free_request = musb_free_request,
  952. .queue = musb_g_ep0_queue,
  953. .dequeue = musb_g_ep0_dequeue,
  954. .set_halt = musb_g_ep0_halt,
  955. };