musb_gadget_ep0.c 27 KB

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