musb_gadget_ep0.c 30 KB

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