musb_gadget_ep0.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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/init.h>
  40. #include <linux/device.h>
  41. #include <linux/interrupt.h>
  42. #include "musb_core.h"
  43. #include "mu3d_hal_osal.h"
  44. #include "mu3d_hal_usb_drv.h"
  45. #include "mu3d_hal_hw.h"
  46. /* ep0 is always musb->endpoints[0].ep_in */
  47. #define next_ep0_request(musb) next_in_request(&(musb)->endpoints[0])
  48. struct usb_dcd_config_params_ext pwr_params;
  49. /*
  50. * locking note: we use only the controller lock, for simpler correctness.
  51. * It's always held with IRQs blocked.
  52. *
  53. * It protects the ep0 request queue as well as ep0_state, not just the
  54. * controller and indexed registers. And that lock stays held unless it
  55. * needs to be dropped to allow reentering this driver ... like upcalls to
  56. * the gadget driver, or adjusting endpoint halt status.
  57. */
  58. static char *decode_ep0stage(u8 stage)
  59. {
  60. switch (stage) {
  61. case MUSB_EP0_STAGE_IDLE:
  62. return "idle";
  63. case MUSB_EP0_STAGE_SETUP:
  64. return "setup";
  65. case MUSB_EP0_STAGE_TX:
  66. return "in";
  67. case MUSB_EP0_STAGE_RX:
  68. return "out";
  69. case MUSB_EP0_STAGE_ACKWAIT:
  70. return "wait";
  71. case MUSB_EP0_STAGE_STATUSIN:
  72. return "in/status";
  73. case MUSB_EP0_STAGE_STATUSOUT:
  74. return "out/status";
  75. default:
  76. return "?";
  77. }
  78. }
  79. static int
  80. forward_to_driver(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
  81. __releases(musb->lock) __acquires(musb->lock)
  82. {
  83. int usb_state = 0;
  84. int retval;
  85. os_printk(K_DEBUG, "%s\n", __func__);
  86. if (!musb->gadget_driver)
  87. return -EOPNOTSUPP;
  88. spin_unlock(&musb->lock);
  89. retval = musb->gadget_driver->setup(&musb->g, ctrlrequest);
  90. os_printk(K_DEBUG, "%s retval=%d\n", __func__, retval);
  91. if (ctrlrequest->bRequest == USB_REQ_SET_CONFIGURATION) {
  92. if (ctrlrequest->wValue & 0xff)
  93. usb_state = USB_CONFIGURED;
  94. else
  95. usb_state = USB_UNCONFIGURED;
  96. musb_sync_with_bat(musb, usb_state); /* annonce to the battery */
  97. }
  98. spin_lock(&musb->lock);
  99. return retval;
  100. }
  101. /* handle a standard GET_STATUS request
  102. * Context: caller holds controller lock
  103. */
  104. static int service_tx_status_request(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
  105. {
  106. int handled = 1, maxp;
  107. u8 result[2], epnum = 0;
  108. const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
  109. os_printk(K_DEBUG, "%s\n", __func__);
  110. result[1] = 0;
  111. switch (recip) {
  112. case USB_RECIP_DEVICE:
  113. result[0] = musb->is_self_powered << USB_DEVICE_SELF_POWERED;
  114. result[0] |= musb->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
  115. /* superspeed only */
  116. if (musb->g.speed == USB_SPEED_SUPER) {
  117. result[0] |= pwr_params.bU1Enabled << USB_DEV_STAT_U1_ENABLED;
  118. result[0] |= pwr_params.bU2Enabled << USB_DEV_STAT_U2_ENABLED;
  119. }
  120. if (musb->g.is_otg) {
  121. result[0] |= musb->g.b_hnp_enable << USB_DEVICE_B_HNP_ENABLE;
  122. result[0] |= musb->g.a_alt_hnp_support << USB_DEVICE_A_ALT_HNP_SUPPORT;
  123. result[0] |= musb->g.a_hnp_support << USB_DEVICE_A_HNP_SUPPORT;
  124. }
  125. os_printk(K_DEBUG, "%s result=%x, U1=%x, U2=%x\n", __func__, result[0],
  126. pwr_params.bU1Enabled, pwr_params.bU2Enabled);
  127. break;
  128. case USB_RECIP_INTERFACE:
  129. result[0] = 0;
  130. break;
  131. case USB_RECIP_ENDPOINT:{
  132. int is_in;
  133. struct musb_ep *ep;
  134. u32 tmp;
  135. epnum = (u8) ctrlrequest->wIndex;
  136. if (!epnum) {
  137. result[0] = 0;
  138. break;
  139. }
  140. is_in = epnum & USB_DIR_IN;
  141. if (is_in) {
  142. epnum &= 0x0f;
  143. ep = &musb->endpoints[epnum].ep_in;
  144. } else {
  145. ep = &musb->endpoints[epnum].ep_out;
  146. }
  147. if (epnum >= MUSB_C_NUM_EPS || !ep->desc) {
  148. handled = -EINVAL;
  149. break;
  150. }
  151. if (is_in) {
  152. tmp = (USB_ReadCsr32(U3D_TX1CSR0, epnum) & TX_SENDSTALL)
  153. || (USB_ReadCsr32(U3D_TX1CSR0, epnum) & TX_SENTSTALL);
  154. /* tmp = USB_ReadCsr16(U3D_TX1CSR0, epnum) & TX_SENDSTALL; */
  155. } else {
  156. tmp = (USB_ReadCsr32(U3D_RX1CSR0, epnum) & RX_SENDSTALL)
  157. || (USB_ReadCsr32(U3D_RX1CSR0, epnum) & RX_SENTSTALL);
  158. }
  159. result[0] = tmp ? 1 : 0;
  160. }
  161. break;
  162. default:
  163. /* class, vendor, etc ... delegate */
  164. handled = 0;
  165. break;
  166. }
  167. /* fill up the fifo; caller updates csr0 */
  168. if (handled > 0) {
  169. u16 len = le16_to_cpu(ctrlrequest->wLength);
  170. if (len > 2)
  171. len = 2;
  172. maxp = musb->endpoints->max_packet_sz_tx;
  173. mu3d_hal_write_fifo(0, len, result, maxp);
  174. }
  175. return handled;
  176. }
  177. /*
  178. * handle a control-IN request, the end0 buffer contains the current request
  179. * that is supposed to be a standard control request. Assumes the fifo to
  180. * be at least 2 bytes long.
  181. *
  182. * @return 0 if the request was NOT HANDLED,
  183. * < 0 when error
  184. * > 0 when the request is processed
  185. *
  186. * Context: caller holds controller lock
  187. */
  188. static int service_in_request(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
  189. {
  190. int handled = 0; /* not handled */
  191. os_printk(K_DEBUG, "%s\n", __func__);
  192. if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
  193. == USB_TYPE_STANDARD) {
  194. switch (ctrlrequest->bRequest) {
  195. case USB_REQ_GET_STATUS:
  196. os_printk(K_DEBUG, "USB_REQ_GET_STATUS\n");
  197. handled = service_tx_status_request(musb, ctrlrequest);
  198. break;
  199. /* case USB_REQ_SYNC_FRAME: */
  200. default:
  201. break;
  202. }
  203. }
  204. return handled;
  205. }
  206. /*
  207. * Context: caller holds controller lock
  208. */
  209. static void musb_g_ep0_giveback(struct musb *musb, struct usb_request *req)
  210. {
  211. os_printk(K_DEBUG, "%s\n", __func__);
  212. musb_g_giveback(&musb->endpoints[0].ep_in, req, 0);
  213. }
  214. /*
  215. * Tries to start B-device HNP negotiation if enabled via sysfs
  216. */
  217. static inline void musb_try_b_hnp_enable(struct musb *musb)
  218. {
  219. u32 devctl;
  220. dev_dbg(musb->controller, "HNP: Setting HR\n");
  221. devctl = os_readl(U3D_DEVICE_CONTROL);
  222. /* We temporarily disable this because the bitfile doesn't support yet.*/
  223. /* os_writeb(mbase + MAC_DEVICE_CONTROL, devctl | USB_DEVCTL_HOSTREQUEST); */
  224. }
  225. #ifdef CONFIG_USBIF_COMPLIANCE
  226. static void update_U1_U2_pwr_params(__u8 bRequest, __le16 wValue)
  227. {
  228. unsigned int tmp;
  229. tmp = (wValue == USB_DEVICE_U1_ENABLE) ?
  230. SW_U1_ACCEPT_ENABLE :
  231. SW_U2_ACCEPT_ENABLE;
  232. if (bRequest == USB_REQ_CLEAR_FEATURE) {
  233. os_printk(K_INFO, "%s CLEAR_FEATURE reg=%x, tmp=%x\n", __func__,
  234. os_readl(U3D_LINK_POWER_CONTROL), tmp);
  235. os_clrmsk(U3D_LINK_POWER_CONTROL, tmp);
  236. os_printk(K_INFO, "%s CLEAR_FEATURE reg=%x\n", __func__,
  237. os_readl(U3D_LINK_POWER_CONTROL));
  238. if (wValue == USB_DEVICE_U1_ENABLE)
  239. pwr_params.bU1Enabled = 0;
  240. else
  241. pwr_params.bU2Enabled = 0;
  242. } else if (bRequest == USB_REQ_SET_FEATURE) {
  243. os_printk(K_INFO, "%s SET_FEATURE %s handled=%d val=%x\n", __func__,
  244. (wValue == USB_DEVICE_U1_ENABLE) ? "U1" :
  245. "U2", handled, tmp);
  246. os_setmsk(U3D_LINK_POWER_CONTROL, tmp);
  247. os_printk(K_INFO, "%s SET_FEATURE reg=%x\n", __func__,
  248. os_readl(U3D_LINK_POWER_CONTROL));
  249. if (wValue == USB_DEVICE_U1_ENABLE)
  250. pwr_params.bU1Enabled = 1;
  251. else
  252. pwr_params.bU2Enabled = 1;
  253. } else
  254. pr_err("update_U1_U2_pwr_params error\n");
  255. }
  256. #endif
  257. /*
  258. * Handle all control requests with no DATA stage, including standard
  259. * requests such as:
  260. * USB_REQ_SET_CONFIGURATION, USB_REQ_SET_INTERFACE, unrecognized
  261. * always delegated to the gadget driver
  262. * USB_REQ_SET_ADDRESS, USB_REQ_CLEAR_FEATURE, USB_REQ_SET_FEATURE
  263. * always handled here, except for class/vendor/... features
  264. *
  265. * Context: caller holds controller lock
  266. */
  267. static int
  268. service_zero_data_request(struct musb *musb,
  269. struct usb_ctrlrequest *ctrlrequest)
  270. __releases(musb->lock) __acquires(musb->lock)
  271. {
  272. int handled = -EINVAL;
  273. const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
  274. #ifdef CONFIG_USBIF_COMPLIANCE
  275. unsigned int tmp;
  276. #endif
  277. os_printk(K_DEBUG, "%s\n", __func__);
  278. /* the gadget driver handles everything except what we MUST handle */
  279. if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
  280. == USB_TYPE_STANDARD) {
  281. switch (ctrlrequest->bRequest) {
  282. case USB_REQ_SET_ISOCH_DELAY:
  283. handled = 1;
  284. break;
  285. case USB_REQ_SET_ADDRESS:
  286. /* change it after the status stage */
  287. musb->set_address = true;
  288. musb->address = (u8) (ctrlrequest->wValue & 0x7f);
  289. handled = 1;
  290. break;
  291. case USB_REQ_CLEAR_FEATURE:
  292. switch (recip) {
  293. case USB_RECIP_DEVICE:
  294. switch (ctrlrequest->wValue) {
  295. case USB_DEVICE_REMOTE_WAKEUP:
  296. musb->may_wakeup = 0;
  297. handled = 1;
  298. break;
  299. case USB_DEVICE_U1_ENABLE:
  300. case USB_DEVICE_U2_ENABLE:
  301. /* superspeed only */
  302. if (musb->g.speed == USB_SPEED_SUPER) {
  303. /* forward the request because of device state check */
  304. handled = forward_to_driver(musb, ctrlrequest);
  305. #ifdef CONFIG_USBIF_COMPLIANCE
  306. if (handled >= 0)
  307. update_U1_U2_pwr_params(ctrlrequest->bRequest, ctrlrequest->wValue);
  308. else
  309. os_printk(K_ERR,
  310. "[COM] composite driver can not handle this control request!\n");
  311. #else
  312. /*
  313. * DO NOT SUPPORT U1/U2 for IOT. Return STALL.
  314. * Under TUSB7340 + Lenovo MT-M5852-B88, IOT test failed.
  315. */
  316. handled = -EINVAL;
  317. #endif /* NEVER */
  318. }
  319. break;
  320. default:
  321. handled = -EINVAL;
  322. break;
  323. }
  324. break;
  325. case USB_RECIP_INTERFACE:
  326. /* superspeed only */
  327. if ((ctrlrequest->wValue == USB_INTRF_FUNC_SUSPEND)
  328. && (musb->g.speed == USB_SPEED_SUPER))
  329. /* forward the request because of device state check */
  330. handled = forward_to_driver(musb, ctrlrequest);
  331. break;
  332. case USB_RECIP_ENDPOINT:{
  333. const u8 epnum = ctrlrequest->wIndex & 0x0f;
  334. struct musb_ep *musb_ep;
  335. struct musb_hw_ep *ep;
  336. struct musb_request *request;
  337. int is_in;
  338. u32 csr;
  339. if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
  340. ctrlrequest->wValue != USB_ENDPOINT_HALT)
  341. break;
  342. ep = musb->endpoints + epnum;
  343. is_in = ctrlrequest->wIndex & USB_DIR_IN;
  344. if (is_in)
  345. musb_ep = &ep->ep_in;
  346. else
  347. musb_ep = &ep->ep_out;
  348. if (!musb_ep->desc)
  349. break;
  350. handled = 1;
  351. /* Ignore request if endpoint is wedged */
  352. if (musb_ep->wedged)
  353. break;
  354. if (is_in) { /* TX */
  355. csr =
  356. USB_ReadCsr32(U3D_TX1CSR0, epnum) & TX_W1C_BITS;
  357. csr = (csr & (~TX_SENDSTALL)) | TX_SENTSTALL;
  358. /* csr = csr & (~TX_SENDSTALL); */
  359. USB_WriteCsr32(U3D_TX1CSR0, epnum, csr);
  360. os_printk(K_DEBUG,
  361. " clear tx stall --> write csr[%d] 0x%04x. new CSR is: 0x%04x\n",
  362. epnum, csr, USB_ReadCsr32(U3D_TX1CSR0,
  363. epnum));
  364. /* reset TX EP */
  365. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) | (BIT16 << epnum));
  366. /* reset reset TX EP */
  367. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) & ~(BIT16 << epnum));
  368. os_printk(K_DEBUG, "RST TX%d\n", epnum);
  369. /* We cannot flush QMU now, because the MSC gadget
  370. * will not re-submit the CBW request after clear halt. */
  371. /* _ex_mu3d_hal_flush_qmu(epnum, USB_TX); */
  372. /* mu3d_hal_restart_qmu(epnum, USB_TX); */
  373. } else {
  374. csr =
  375. USB_ReadCsr32(U3D_RX1CSR0, epnum) & RX_W1C_BITS;
  376. csr = (csr & (~RX_SENDSTALL)) | RX_SENTSTALL;
  377. USB_WriteCsr32(U3D_RX1CSR0, epnum, csr);
  378. os_printk(K_DEBUG,
  379. " clear rx stall --> write csr[%d] 0x%04x. new CSR is: 0x%04x\n",
  380. epnum, csr, USB_ReadCsr32(U3D_RX1CSR0, epnum));
  381. /* reset RX EP */
  382. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) | (1 << epnum));
  383. /* reset reset RX EP */
  384. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) & (~(1 << epnum)));
  385. os_printk(K_DEBUG, "RST RX%d\n", epnum);
  386. /* We cannot flush QMU now, because the MSC gadget will not
  387. * re-submit the CBW request after clear halt. */
  388. /* _ex_mu3d_hal_flush_qmu(epnum, USB_RX); */
  389. /* mu3d_hal_restart_qmu(epnum, USB_RX); */
  390. }
  391. /* Maybe start the first request in the queue */
  392. request = next_request(musb_ep);
  393. if (!musb_ep->busy && request) {
  394. dev_dbg(musb->controller,
  395. "restarting the request\n");
  396. musb_ep_restart(musb, request);
  397. }
  398. /* select ep0 again */
  399. }
  400. break;
  401. default:
  402. /* class, vendor, etc ... delegate */
  403. handled = 0;
  404. break;
  405. }
  406. break;
  407. case USB_REQ_SET_FEATURE:
  408. switch (recip) {
  409. case USB_RECIP_DEVICE:
  410. handled = 1;
  411. switch (ctrlrequest->wValue) {
  412. case USB_DEVICE_REMOTE_WAKEUP:
  413. musb->may_wakeup = 1;
  414. break;
  415. case USB_DEVICE_TEST_MODE:
  416. if (musb->g.speed != USB_SPEED_HIGH)
  417. goto stall;
  418. if (ctrlrequest->wIndex & 0xff)
  419. goto stall;
  420. switch (ctrlrequest->wIndex >> 8) {
  421. case 1:
  422. pr_debug("TEST_J\n");
  423. /* TEST_J */
  424. musb->test_mode_nr = TEST_J_MODE;
  425. break;
  426. case 2:
  427. /* TEST_K */
  428. pr_debug("TEST_K\n");
  429. musb->test_mode_nr = TEST_K_MODE;
  430. break;
  431. case 3:
  432. /* TEST_SE0_NAK */
  433. pr_debug("TEST_SE0_NAK\n");
  434. musb->test_mode_nr = TEST_SE0_NAK_MODE;
  435. break;
  436. case 4:
  437. /* TEST_PACKET */
  438. pr_debug("TEST_PACKET\n");
  439. musb->test_mode_nr = TEST_PACKET_MODE;
  440. break;
  441. case 0xc0:
  442. /* TEST_FORCE_HS */
  443. pr_debug("TEST_FORCE_HS\n");
  444. musb->test_mode_nr = FORCE_HS;
  445. break;
  446. case 0xc1:
  447. /* TEST_FORCE_FS */
  448. pr_debug("TEST_FORCE_FS\n");
  449. musb->test_mode_nr = FORCE_FS;
  450. break;
  451. case 0xc2:
  452. /* TEST_FIFO_ACCESS */
  453. pr_debug("TEST_FIFO_ACCESS\n");
  454. musb->test_mode_nr = FIFO_ACCESS;
  455. break;
  456. case 0xc3:
  457. /* TEST_FORCE_HOST */
  458. pr_debug("TEST_FORCE_HOST\n");
  459. musb->test_mode_nr = FORCE_HOST;
  460. break;
  461. default:
  462. goto stall;
  463. }
  464. /* enter test mode after irq */
  465. if (handled > 0)
  466. musb->test_mode = true;
  467. break;
  468. case USB_DEVICE_B_HNP_ENABLE:
  469. if (!musb->g.is_otg)
  470. goto stall;
  471. musb->g.b_hnp_enable = 1;
  472. musb_try_b_hnp_enable(musb);
  473. break;
  474. case USB_DEVICE_A_HNP_SUPPORT:
  475. if (!musb->g.is_otg)
  476. goto stall;
  477. musb->g.a_hnp_support = 1;
  478. break;
  479. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  480. if (!musb->g.is_otg)
  481. goto stall;
  482. musb->g.a_alt_hnp_support = 1;
  483. break;
  484. case USB_DEVICE_U1_ENABLE:
  485. case USB_DEVICE_U2_ENABLE:
  486. /* superspeed only */
  487. if (musb->g.speed != USB_SPEED_SUPER)
  488. break;
  489. /* forward the request because of device state check */
  490. handled = forward_to_driver(musb, ctrlrequest);
  491. #ifdef CONFIG_USBIF_COMPLIANCE
  492. if (handled >= 0)
  493. update_U1_U2_pwr_params(ctrlrequest->bRequest, ctrlrequest->wValue);
  494. else
  495. os_printk(K_ERR,
  496. "[COM] composite driver can not handle this control request!\n");
  497. #else
  498. /*
  499. * DO NOT SUPPORT U1/U2 for IOT. Return STALL.
  500. * Under TUSB7340 + Lenovo MT-M5852-B88, IOT test failed.
  501. */
  502. handled = -EINVAL;
  503. #endif /* NEVER */
  504. break;
  505. case USB_DEVICE_DEBUG_MODE:
  506. handled = 0;
  507. break;
  508. stall:
  509. default:
  510. handled = -EINVAL;
  511. break;
  512. }
  513. break;
  514. case USB_RECIP_INTERFACE:
  515. /* superspeed only */
  516. if ((ctrlrequest->wValue == USB_INTRF_FUNC_SUSPEND)
  517. && (musb->g.speed == USB_SPEED_SUPER)) {
  518. /* forward the request because of device state check */
  519. /* handled = forward_to_driver(musb, ctrlrequest); */
  520. os_printk(K_DEBUG, "wIndex=%x\n", ctrlrequest->wIndex);
  521. if (ctrlrequest->wIndex & USB_INTRF_FUNC_SUSPEND_LP)
  522. os_printk(K_DEBUG, "USB_INTRF_FUNC_SUSPEND_LP\n");
  523. else if (ctrlrequest->wIndex & USB_INTRF_FUNC_SUSPEND_RW)
  524. os_printk(K_DEBUG, "USB_INTRF_FUNC_SUSPEND_RW\n");
  525. }
  526. os_printk(K_DEBUG, "USB_RECIP_INTERFACE handled=%d\n", handled);
  527. /* Just pretend that the gadget driver can fully handle this control request */
  528. handled = 1;
  529. break;
  530. case USB_RECIP_ENDPOINT:{
  531. const u8 epnum = ctrlrequest->wIndex & 0x0f;
  532. struct musb_ep *musb_ep;
  533. struct musb_hw_ep *ep;
  534. int is_in;
  535. u32 csr;
  536. if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
  537. ctrlrequest->wValue != USB_ENDPOINT_HALT)
  538. break;
  539. ep = musb->endpoints + epnum;
  540. is_in = ctrlrequest->wIndex & USB_DIR_IN;
  541. if (is_in)
  542. musb_ep = &ep->ep_in;
  543. else
  544. musb_ep = &ep->ep_out;
  545. if (!musb_ep->desc)
  546. break;
  547. if (is_in) { /* tx */
  548. csr = USB_ReadCsr32(U3D_TX1CSR0, epnum);
  549. if (!(csr & TX_FIFOEMPTY)) {
  550. /*
  551. csr &= TX_W1C_BITS; //don't clear W1C bits
  552. csr |= USB_TXCSR_FLUSHFIFO;
  553. //os_printk(K_DEBUG, "EP%d USB_TXCSR_FLUSHFIFO\n", epnum);
  554. //flush fifo before sendstall.
  555. SSUSB_WriteCsr16(U3D_TX1CSR0, epnum, csr );
  556. Follow ssusb programming guide.
  557. while(USB_ReadCsr16(U3D_TX1CSR0, epnum) & USB_TXCSR_FLUSHFIFO)
  558. {
  559. cpu_relax();
  560. }
  561. */
  562. /* reset TX EP */
  563. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) | (BIT16 << epnum));
  564. /* reset reset TX EP */
  565. os_writel(U3D_EP_RST, os_readl(U3D_EP_RST) & ~(BIT16 << epnum));
  566. }
  567. csr &= TX_W1C_BITS;
  568. csr |= TX_SENDSTALL;
  569. os_printk(K_DEBUG,
  570. "@@@@@@@@@@@@@@@@@ EP%d(IN/TX) SEND_STALL\n",
  571. epnum);
  572. /* ssusb: need further check. is WZC_BITS needed? */
  573. USB_WriteCsr32(U3D_TX1CSR0, epnum, csr);
  574. } else {
  575. csr = USB_ReadCsr32(U3D_RX1CSR0, epnum);
  576. csr &= RX_W1C_BITS;
  577. csr |= RX_SENDSTALL;
  578. os_printk(K_DEBUG,
  579. "@@@@@@@@@@@@@@@@@ EP%d(OUT/RX) SEND_STALL\n",
  580. epnum);
  581. /* musb_writew(regs, MUSB_RXCSR, csr); */
  582. USB_WriteCsr32(U3D_RX1CSR0, epnum, csr);
  583. }
  584. /* select ep0 again */
  585. handled = 1;
  586. }
  587. break;
  588. default:
  589. /* class, vendor, etc ... delegate */
  590. handled = 0;
  591. break;
  592. }
  593. break;
  594. default:
  595. /* delegate SET_CONFIGURATION, etc */
  596. handled = 0;
  597. }
  598. } else
  599. handled = 0;
  600. return handled;
  601. }
  602. /* we have an ep0out data packet
  603. * Context: caller holds controller lock
  604. */
  605. static void ep0_rxstate(struct musb *musb)
  606. {
  607. struct musb_request *request;
  608. struct usb_request *req;
  609. u32 csr;
  610. u16 count = 0;
  611. os_printk(K_DEBUG, "%s\n", __func__);
  612. /* Set the register which is W1C as 0. */
  613. csr = os_readl(U3D_EP0CSR) & EP0_W1C_BITS;
  614. request = next_ep0_request(musb);
  615. req = &request->request;
  616. /* read packet and ack; or stall because of gadget driver bug:
  617. * should have provided the rx buffer before setup() returned.
  618. */
  619. if (req) {
  620. void *buf = req->buf + req->actual;
  621. unsigned len = req->length - req->actual;
  622. #ifdef AUTOCLEAR
  623. if (!(os_readl(U3D_EP0CSR) & EP0_AUTOCLEAR))
  624. os_writel(U3D_EP0CSR, os_readl(U3D_EP0CSR) | EP0_AUTOCLEAR);
  625. #endif
  626. /* read the buffer */
  627. count = os_readl(U3D_RXCOUNT0);
  628. if (count > len) {
  629. req->status = -EOVERFLOW;
  630. count = len;
  631. }
  632. musb_read_fifo(&musb->endpoints[0], count, buf);
  633. req->actual += count;
  634. csr |= EP0_RXPKTRDY;
  635. /*REVISIT-J: 64 is usb20 ep0 maxp, but usb30 ep0 maxp is 512.
  636. *Do we need the modification? */
  637. if (count < 64 || req->actual == req->length) {
  638. /* musb->ep0_state = MUSB_EP0_STAGE_STATUSIN; */
  639. /* in ssusb, there is no interrupt to transit to idle phase. */
  640. musb->ep0_state = MUSB_EP0_IDLE;
  641. os_printk(K_DEBUG,
  642. "----- ep0 state: MUSB_EP0_STAGE_STATUSIN then MUSB_EP0_IDLE\n");
  643. csr |= EP0_DATAEND;
  644. } else
  645. req = NULL;
  646. } else {
  647. csr |= EP0_RXPKTRDY | EP0_SENDSTALL;
  648. os_printk(K_DEBUG, "@@@@@@@@@@@@@@@ SENDSTALL\n");
  649. }
  650. /* Completion handler may choose to stall, e.g. because the
  651. * message just received holds invalid data.
  652. */
  653. if (req) {
  654. musb->ackpend = csr;
  655. musb_g_ep0_giveback(musb, req);
  656. if (!musb->ackpend)
  657. return;
  658. musb->ackpend = 0;
  659. }
  660. os_writel(U3D_EP0CSR, csr);
  661. }
  662. /*
  663. * transmitting to the host (IN), this code might be called from IRQ
  664. * and from kernel thread.
  665. *
  666. * Context: caller holds controller lock
  667. */
  668. static void ep0_txstate(struct musb *musb)
  669. {
  670. struct musb_request *req = next_ep0_request(musb);
  671. struct usb_request *request;
  672. u32 csr = EP0_TXPKTRDY;
  673. u8 *fifo_src;
  674. u16 fifo_count;
  675. u32 maxp;
  676. os_printk(K_DEBUG, "%s\n", __func__);
  677. if (!req) {
  678. /* WARN_ON(1); */
  679. /* dev_dbg(musb->controller, "odd; csr0 %04x\n", musb_readw(regs, MUSB_CSR0)); */
  680. return;
  681. }
  682. maxp = musb->endpoints->max_packet_sz_tx;
  683. request = &req->request;
  684. /* load the data */
  685. fifo_src = (u8 *) request->buf + request->actual;
  686. fifo_count = min((unsigned)maxp, request->length - request->actual);
  687. musb_write_fifo(&musb->endpoints[0], fifo_count, fifo_src);
  688. os_printk(K_DEBUG, "%s act=%d, len=%d, cnt=%d, maxp=%d zero=%d\n",
  689. __func__, request->actual, request->length, fifo_count, maxp, request->zero);
  690. /*
  691. * The flow is difference between MTU3D and original musb.
  692. * For example:
  693. * Host <-- 12byte -- Device
  694. * MUSB:
  695. * Interrupt #1 : Write 12bytes in FIFO and set TXPKTRDY + DATAEND
  696. * Interrupt #2 : Do nothing.
  697. * MTU3D:
  698. * Interrupt #1 : Write 12bytes in FIFO and set TXPKTRDY
  699. * Interrupt #2 : set DATAEND
  700. */
  701. /* update the flags */
  702. if (request->actual == request->length) {
  703. if (request->zero && (request->length % maxp == 0) && (request->length / maxp > 0)) {
  704. /*
  705. * Send a ZLP without DATAEND. Because the length host requires
  706. * is longer than the actual data device sends. So pad a ZLP to
  707. * tell HOST the end of the data. If the transferred data length
  708. * is exactly what host wants, just set DATAEND without ZLP.
  709. */
  710. request->zero = 0;
  711. os_printk(K_DEBUG, "%s Send a padding ZLP!!!!\n", __func__);
  712. request = NULL;
  713. } else {
  714. musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
  715. csr |= EP0_DATAEND;
  716. }
  717. } else {
  718. /* Add the amount of the data written into fifo to "actual" first. */
  719. request->actual += fifo_count;
  720. request = NULL;
  721. }
  722. /* report completions as soon as the fifo's loaded; there's no
  723. * win in waiting till this last packet gets acked. (other than
  724. * very precise fault reporting, needed by USB TMC; possible with
  725. * this hardware, but not usable from portable gadget drivers.)
  726. */
  727. if (request) {
  728. musb->ackpend = csr;
  729. musb_g_ep0_giveback(musb, request);
  730. if (!musb->ackpend)
  731. return;
  732. musb->ackpend = 0;
  733. }
  734. os_printk(K_DEBUG, "%s csr=%x\n", __func__, csr);
  735. /* send it out, triggering a "txpktrdy cleared" irq */
  736. os_writel(U3D_EP0CSR, os_readl(U3D_EP0CSR) | csr);
  737. }
  738. /*
  739. * Read a SETUP packet (struct usb_ctrlrequest) from the hardware.
  740. * Fields are left in USB byte-order.
  741. *
  742. * Context: caller holds controller lock.
  743. */
  744. static void musb_read_setup(struct musb *musb, struct usb_ctrlrequest *req)
  745. {
  746. struct musb_request *r;
  747. u32 csr = 0;
  748. os_printk(K_DEBUG, "%s\n", __func__);
  749. csr = os_readl(U3D_EP0CSR) & EP0_W1C_BITS; /* Don't W1C */
  750. if (!(os_readl(U3D_EP0CSR) & EP0_AUTOCLEAR))
  751. os_writel(U3D_EP0CSR, os_readl(U3D_EP0CSR) | EP0_AUTOCLEAR);
  752. mu3d_hal_read_fifo(0, (u8 *) req);
  753. /* NOTE: earlier 2.6 versions changed setup packets to host
  754. * order, but now USB packets always stay in USB byte order.
  755. */
  756. /* dev_dbg(musb->controller, "SETUP req%02x.%02x v%04x i%04x l%d\n", */
  757. os_printk(K_DEBUG, "SETUP req%02x.%02x v%04x i%04x l%d\n",
  758. req->bRequestType,
  759. req->bRequest,
  760. le16_to_cpu(req->wValue), le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
  761. /* clean up any leftover transfers */
  762. r = next_ep0_request(musb);
  763. if (r)
  764. musb_g_ep0_giveback(musb, &r->request);
  765. /* For zero-data requests we want to delay the STATUS stage to
  766. * avoid SETUPEND errors. If we read data (OUT), delay accepting
  767. * packets until there's a buffer to store them in.
  768. *
  769. * If we write data, the controller acts happier if we enable
  770. * the TX FIFO right away, and give the controller a moment
  771. * to switch modes...
  772. */
  773. musb->set_address = false;
  774. if (req->wLength == 0) {
  775. musb->ep0_state = MUSB_EP0_STAGE_ACKWAIT;
  776. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_ACKWAIT\n");
  777. } else if (req->bRequestType & USB_DIR_IN) {
  778. musb->ep0_state = MUSB_EP0_STAGE_TX;
  779. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_TX\n");
  780. os_writel(U3D_EP0CSR, csr | EP0_SETUPPKTRDY | EP0_DPHTX);
  781. musb->ackpend = 0;
  782. } else {
  783. /* Set CSR0.SetupPktRdy(W1C) & CSR0.DPHTX=0 */
  784. os_writel(U3D_EP0CSR, (csr | EP0_SETUPPKTRDY) & (~EP0_DPHTX));
  785. musb->ackpend = 0;
  786. musb->ep0_state = MUSB_EP0_STAGE_RX;
  787. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_RX\n");
  788. }
  789. }
  790. /*
  791. * Handle peripheral ep0 interrupt
  792. *
  793. * Context: irq handler; we won't re-enter the driver that way.
  794. */
  795. irqreturn_t musb_g_ep0_irq(struct musb *musb)
  796. {
  797. u32 csr;
  798. u16 len;
  799. irqreturn_t retval = IRQ_NONE;
  800. int i = 0;
  801. csr = os_readl(U3D_EP0CSR);
  802. len = (u16) os_readl(U3D_RXCOUNT0);
  803. os_printk(K_DEBUG, "%s csr=0x%X\n", __func__, csr);
  804. /* dev_dbg(musb->controller, "csr %04x, count %d, myaddr %d, ep0stage %s\n",
  805. csr, len,
  806. musb_readb(mbase, MUSB_FADDR),
  807. decode_ep0stage(musb->ep0_state));
  808. */
  809. /* if (csr & MUSB_CSR0_P_DATAEND) { */
  810. /*
  811. * If DATAEND is set we should not call the callback,
  812. * hence the status stage is not complete.
  813. */
  814. /* return IRQ_HANDLED;
  815. } */
  816. /* I sent a stall.. need to acknowledge it now.. */
  817. if (csr & EP0_SENTSTALL) {
  818. os_writel(U3D_EP0CSR, (csr & EP0_W1C_BITS) | EP0_SENTSTALL); /* EP0_SENTSTALL is W1C */
  819. if (os_readl(U3D_EP0CSR) & EP0_TXPKTRDY) { /* try to flushfifo after clear sentstall */
  820. #if 0
  821. u32 csr0 = 0;
  822. csr0 = os_readl(U3D_EP0CSR);
  823. csr0 &= EP0_W1C_BITS; /* don't clear W1C bits */
  824. csr0 |= CSR0_FLUSHFIFO;
  825. os_writel(U3D_EP0CSR, csr0);
  826. os_printk(K_DEBUG, "waiting for flushfifo.....");
  827. while (os_readl(U3D_EP0CSR) & CSR0_FLUSHFIFO) { /* proceed before it clears */
  828. cpu_relax();
  829. }
  830. os_printk(K_DEBUG, "done.\n");
  831. #else
  832. /* toggle EP0_RST */
  833. os_setmsk(U3D_EP_RST, EP0_RST);
  834. os_clrmsk(U3D_EP_RST, EP0_RST);
  835. #endif
  836. }
  837. retval = IRQ_HANDLED;
  838. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  839. csr = os_readl(U3D_EP0CSR);
  840. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_IDLE. now csr is 0x%04x\n",
  841. csr);
  842. }
  843. /* SSUSB does not support this bit. So comment it.*/
  844. #ifdef NEVER
  845. /* request ended "early" */
  846. if (csr & CSR0_SETUPEND) {
  847. os_writel(U3D_EP0CSR, (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) | CSR0_SERVICESETUPEND);
  848. retval = IRQ_HANDLED;
  849. /* Transition into the early status phase */
  850. switch (musb->ep0_state) {
  851. case MUSB_EP0_STAGE_TX:
  852. musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
  853. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_STATUSOUT\n");
  854. break;
  855. case MUSB_EP0_STAGE_RX:
  856. musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
  857. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_STATUSIN\n");
  858. break;
  859. default:
  860. ERR("SetupEnd came in a wrong ep0stage %s\n",
  861. decode_ep0stage(musb->ep0_state));
  862. }
  863. csr = os_readl(U3D_EP0CSR);
  864. /* NOTE: request may need completion */
  865. }
  866. #endif /* NEVER */
  867. os_printk(K_DEBUG, "ep0_state=%d\n", musb->ep0_state);
  868. /* docs from Mentor only describe tx, rx, and idle/setup states.
  869. * we need to handle nuances around status stages, and also the
  870. * case where status and setup stages come back-to-back ...
  871. */
  872. switch (musb->ep0_state) {
  873. case MUSB_EP0_STAGE_TX:
  874. /* irq on clearing txpktrdy */
  875. if ((csr & EP0_FIFOFULL) == 0) {
  876. os_printk(K_DEBUG, "csr & EP0_FIFOFULL\n");
  877. ep0_txstate(musb);
  878. retval = IRQ_HANDLED;
  879. }
  880. break;
  881. case MUSB_EP0_STAGE_RX:
  882. /* irq on set rxpktrdy */
  883. if (csr & EP0_RXPKTRDY) {
  884. ep0_rxstate(musb);
  885. retval = IRQ_HANDLED;
  886. }
  887. break;
  888. case MUSB_EP0_STAGE_STATUSIN:
  889. /* Because ssusb doesn't have interrupt after In status, we actually don't have STATUSIN stage.
  890. * It has been moved to MUSB_EP0_STAGE_SETUP. */
  891. #if 0
  892. /* end of sequence #2 (OUT/RX state) or #3 (no data) */
  893. /* update address (if needed) only @ the end of the
  894. * status phase per usb spec, which also guarantees
  895. * we get 10 msec to receive this irq... until this
  896. * is done we won't see the next packet.
  897. */
  898. if (musb->set_address) {
  899. musb->set_address = false;
  900. os_writel(U3D_DEVICE_CONF,
  901. os_readl(U3D_DEVICE_CONF) | (musb->address << DEV_ADDR_OFST));
  902. }
  903. /* enter test mode if needed (exit by reset) */
  904. else if (musb->test_mode) {
  905. dev_dbg(musb->controller, "entering TESTMODE\n");
  906. os_printk(K_DEBUG, "entering TESTMODE 1\n");
  907. if (TEST_PACKET_MODE == musb->test_mode_nr)
  908. musb_load_testpacket(musb);
  909. os_writel(U3D_USB2_TEST_MODE, musb->test_mode_nr);
  910. }
  911. #endif
  912. /* FALLTHROUGH */
  913. case MUSB_EP0_STAGE_STATUSOUT:
  914. /* end of sequence #1: write to host (TX state) */
  915. {
  916. struct musb_request *req;
  917. req = next_ep0_request(musb);
  918. if (req)
  919. musb_g_ep0_giveback(musb, &req->request);
  920. }
  921. /*
  922. * In case when several interrupts can get coalesced,
  923. * check to see if we've already received a SETUP packet...
  924. */
  925. if (csr & EP0_SETUPPKTRDY) /* in ssusb, we check SETUPPKTRDY for setup packet. */
  926. goto setup;
  927. retval = IRQ_HANDLED;
  928. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  929. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_IDLE\n");
  930. break;
  931. case MUSB_EP0_STAGE_IDLE:
  932. /*
  933. * This state is typically (but not always) indiscernible
  934. * from the status states since the corresponding interrupts
  935. * tend to happen within too little period of time (with only
  936. * a zero-length packet in between) and so get coalesced...
  937. */
  938. retval = IRQ_HANDLED;
  939. /* REVISIT-J: No need, Cuz the following sequence does not effect. */
  940. /* added for ssusb: */
  941. if (!(csr & EP0_SETUPPKTRDY)) {
  942. os_printk(K_DEBUG, "break from MUSB_EP0_STAGE_IDLE\n");
  943. break; /* Don't process, keep idle. a. */
  944. }
  945. /* added for ssusb. */
  946. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  947. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_SETUP\n");
  948. /* FALLTHROUGH */
  949. case MUSB_EP0_STAGE_SETUP:
  950. setup:
  951. if (csr & EP0_SETUPPKTRDY) {
  952. struct usb_ctrlrequest setup;
  953. int handled = 0;
  954. if (len != 8) {
  955. ERR("SETUP packet len %d != 8 ?\n", len);
  956. break;
  957. }
  958. musb_read_setup(musb, &setup);
  959. retval = IRQ_HANDLED;
  960. /* sometimes the RESET won't be reported */
  961. if (unlikely(musb->g.speed == USB_SPEED_UNKNOWN)) {
  962. /*REVISIT-J: Shall we implement it? */
  963. /* mark temporarily for ssusb because PMU is not ready.
  964. u8 power;
  965. printk(KERN_NOTICE "%s: peripheral reset "
  966. "irq lost!\n",
  967. musb_driver_name);
  968. power = musb_readb(mbase, MUSB_POWER);
  969. musb->g.speed = (power & HS_MODE)
  970. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  971. */
  972. }
  973. switch (musb->ep0_state) {
  974. /* sequence #3 (no data stage), includes requests
  975. * we can't forward (notably SET_ADDRESS and the
  976. * device/endpoint feature set/clear operations)
  977. * plus SET_CONFIGURATION and others we must
  978. */
  979. case MUSB_EP0_STAGE_ACKWAIT:
  980. os_printk(K_DEBUG,
  981. "&&&&&&&&&&&&&&&&& process MUSB_EP0_STAGE_ACKWAIT\n");
  982. handled = service_zero_data_request(musb, &setup);
  983. /*
  984. * We're expecting no data in any case, so
  985. * always set the DATAEND bit -- doing this
  986. * here helps avoid SetupEnd interrupt coming
  987. * in the idle stage when we're stalling...
  988. */
  989. /*if (1)*/ /*marked for DEEP_INDENTATION*/
  990. {
  991. /* Because status phase currently doesn't
  992. * have interrupt, we process here. */
  993. /* Process here according to ssusb programming guide */
  994. if (musb->set_address) {
  995. musb->set_address = false;
  996. /* musb_writeb(mbase, MUSB_FADDR, musb->address); */
  997. os_printk(K_DEBUG, "Set address to 0x%08x...\n",
  998. musb->address);
  999. os_writel(U3D_DEVICE_CONF,
  1000. os_readl(U3D_DEVICE_CONF) | (musb->address
  1001. <<
  1002. DEV_ADDR_OFST));
  1003. } else if (musb->test_mode) {
  1004. os_printk(K_DEBUG, "entering TESTMODE 2\n");
  1005. if (TEST_PACKET_MODE == musb->test_mode_nr)
  1006. musb_load_testpacket(musb);
  1007. /* musb_writeb(mbase, MUSB_TESTMODE, */
  1008. /* musb->test_mode_nr); */
  1009. /* Need to send status before really entering test mode. */
  1010. os_writel(U3D_EP0CSR,
  1011. (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) |
  1012. musb->ackpend | EP0_DATAEND |
  1013. EP0_SETUPPKTRDY);
  1014. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1015. while ((os_readl(U3D_EP0CSR) & EP0_DATAEND) != 0) {
  1016. /* Need to wait for status really loaded by host */
  1017. mdelay(1);/* Without this delay, it will fail. */
  1018. }
  1019. os_writel(U3D_USB2_TEST_MODE, musb->test_mode_nr);
  1020. return retval;
  1021. }
  1022. /* end of sequence #1: write to host (TX state) */
  1023. {
  1024. struct usb_request *request;
  1025. struct musb_request *req;
  1026. req = next_ep0_request(musb);
  1027. if (req) {
  1028. request = &(req->request);
  1029. os_printk(K_DEBUG,
  1030. "&&&&&&&&&&&&&&&&& next_ep0_request\n");
  1031. musb_g_ep0_giveback(musb, request);
  1032. } else {
  1033. /* os_printk(K_DEBUG,
  1034. "&&&&&&&&&&&&&&&&& next_ep0_request returns null\n"); */
  1035. }
  1036. }
  1037. }
  1038. /* Set CSR0.SetupPktRdy(W1C) & CSR0.DataEnd */
  1039. musb->ackpend |= EP0_DATAEND | EP0_SETUPPKTRDY;
  1040. /* status stage might be immediate */
  1041. if (handled > 0) {
  1042. /* Change to idle because status in will be completed
  1043. * immediately after dataend is set */
  1044. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1045. os_printk(K_DEBUG,
  1046. "----- ep0 state: MUSB_EP0_STAGE_IDLE\n");
  1047. }
  1048. break;
  1049. /* sequence #1 (IN to host), includes GET_STATUS
  1050. * requests that we can't forward, GET_DESCRIPTOR
  1051. * and others that we must
  1052. */
  1053. case MUSB_EP0_STAGE_TX:
  1054. handled = service_in_request(musb, &setup);
  1055. if (handled <= 0) {
  1056. /* os_printk(K_DEBUG, "Cannot service_in_request.\n"); */
  1057. break;
  1058. }
  1059. os_printk(K_DEBUG, "handled MUSB_EP0_STAGE_TX.\n");
  1060. /* Wait until FIFOFULL cleared by hrdc */
  1061. while ((os_readl(U3D_EP0CSR) & EP0_FIFOFULL)) {
  1062. mdelay(5);
  1063. i++;
  1064. if (i > 5) {
  1065. os_printk(K_INFO,
  1066. "ep0 still full, something wrong!!!\n");
  1067. break;
  1068. }
  1069. }
  1070. musb->ackpend |= EP0_DATAEND;
  1071. musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
  1072. os_printk(K_DEBUG,
  1073. "----- ep0 state: MUSB_EP0_STAGE_STATUSOUT (%s:%d)\n",
  1074. __func__, __LINE__);
  1075. /*if (1) */ /*DEEP_INDENTATION*/
  1076. {
  1077. /* process MUSB_EP0_STAGE_STATUSOUT because currently
  1078. * we don't have interrupt after status out phase. */
  1079. /* end of sequence #1: write to host (TX state) */
  1080. {
  1081. struct usb_request *request;
  1082. struct musb_request *req;
  1083. req = next_ep0_request(musb);
  1084. if (req) {
  1085. request = &(req->request);
  1086. musb_g_ep0_giveback(musb, request);
  1087. }
  1088. }
  1089. /*
  1090. * In case when several interrupts can get coalesced,
  1091. * check to see if we've already received a SETUP packet...
  1092. */
  1093. retval = IRQ_HANDLED;
  1094. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1095. os_printk(K_DEBUG,
  1096. "----- ep0 state: MUSB_EP0_STAGE_IDLE (%s:%d)\n",
  1097. __func__, __LINE__);
  1098. }
  1099. /* process MUSB_EP0_STAGE_STATUSOUT because currently
  1100. * we don't have interrupt after status out phase. */
  1101. break;
  1102. /* sequence #2 (OUT from host), always forward */
  1103. default: /* MUSB_EP0_STAGE_RX */
  1104. break;
  1105. }
  1106. /* dev_dbg(musb->controller, "handled %d, csr %04x, ep0stage %s\n", */
  1107. os_printk(K_DEBUG, "handled %d, csr %04x, ep0stage %s\n",
  1108. handled, csr, decode_ep0stage(musb->ep0_state));
  1109. /* unless we need to delegate this to the gadget
  1110. * driver, we know how to wrap this up: csr0 has
  1111. * not yet been written.
  1112. */
  1113. if (handled < 0)
  1114. goto stall;
  1115. else if (handled > 0)
  1116. goto finish;
  1117. handled = forward_to_driver(musb, &setup);
  1118. if (handled < 0) {
  1119. stall:
  1120. os_printk(K_INFO, "stall (%d)\n", handled);
  1121. /* flushfifo before send SENDSTALL */
  1122. if (os_readl(U3D_EP0CSR) & EP0_TXPKTRDY) {
  1123. /* try to flushfifo after clear sentstall */
  1124. #if 0
  1125. u32 csr0 = 0;
  1126. csr0 = os_readl(U3D_EP0CSR);
  1127. csr0 &= EP0_W1C_BITS; /* don't clear W1C bits */
  1128. csr0 |= CSR0_FLUSHFIFO;
  1129. os_writel(U3D_EP0CSR, csr0);
  1130. os_printk(K_DEBUG, "waiting for flushfifo.....");
  1131. while (os_readl(U3D_EP0CSR) & CSR0_FLUSHFIFO) {
  1132. /* proceed before it clears */
  1133. cpu_relax();
  1134. }
  1135. os_printk(K_DEBUG, "done.\n");
  1136. #else
  1137. /* toggle EP0_RST */
  1138. os_setmsk(U3D_EP_RST, EP0_RST);
  1139. os_clrmsk(U3D_EP_RST, EP0_RST);
  1140. #endif
  1141. }
  1142. if (musb->ackpend & EP0_DATAEND) {
  1143. os_printk(K_DEBUG, "Do not send dataend due to stall.\n");
  1144. musb->ackpend &= ~EP0_DATAEND;
  1145. }
  1146. musb->ackpend |= EP0_SENDSTALL;
  1147. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1148. os_printk(K_INFO, "@@@@@@@@@@@@@@@ SENDSTALL\n");
  1149. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_IDLE\n");
  1150. finish:
  1151. os_writel(U3D_EP0CSR,
  1152. (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) | musb->ackpend);
  1153. musb->ackpend = 0;
  1154. }
  1155. }
  1156. break;
  1157. case MUSB_EP0_STAGE_ACKWAIT:
  1158. /* This should not happen. But happens with tusb6010 with
  1159. * g_file_storage and high speed. Do nothing.
  1160. */
  1161. retval = IRQ_HANDLED;
  1162. break;
  1163. default:
  1164. /* "can't happen" */
  1165. WARN_ON(1);
  1166. os_printk(K_INFO, "@@@@@@@@@@@@@@@ SENDSTALL\n");
  1167. /* flushfifo before send SENDSTALL */
  1168. if (os_readl(U3D_EP0CSR) & EP0_TXPKTRDY) { /* try to flushfifo after clear sentstall */
  1169. #if 0
  1170. u32 csr0 = 0;
  1171. csr0 = os_readl(U3D_EP0CSR);
  1172. csr0 &= EP0_W1C_BITS; /* don't clear W1C bits */
  1173. csr0 |= CSR0_FLUSHFIFO;
  1174. os_writel(U3D_EP0CSR, csr0);
  1175. os_printk(K_DEBUG, "waiting for flushfifo.....");
  1176. while (os_readl(U3D_EP0CSR) & CSR0_FLUSHFIFO) { /* proceed before it clears */
  1177. cpu_relax();
  1178. }
  1179. os_printk(K_DEBUG, "done.\n");
  1180. #else
  1181. /* toggle EP0_RST */
  1182. os_setmsk(U3D_EP_RST, EP0_RST);
  1183. os_clrmsk(U3D_EP_RST, EP0_RST);
  1184. #endif
  1185. }
  1186. os_writel(U3D_EP0CSR, (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) | EP0_SENDSTALL);
  1187. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1188. break;
  1189. }
  1190. return retval;
  1191. }
  1192. static int musb_g_ep0_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *desc)
  1193. {
  1194. /* always enabled */
  1195. return -EINVAL;
  1196. }
  1197. static int musb_g_ep0_disable(struct usb_ep *e)
  1198. {
  1199. /* always enabled */
  1200. return -EINVAL;
  1201. }
  1202. static int musb_g_ep0_queue(struct usb_ep *e, struct usb_request *r, gfp_t gfp_flags)
  1203. {
  1204. struct musb_ep *ep;
  1205. struct musb_request *req;
  1206. struct musb *musb;
  1207. int status;
  1208. unsigned long lockflags;
  1209. if (!e || !r)
  1210. return -EINVAL;
  1211. os_printk(K_DEBUG, "%s\n", __func__);
  1212. ep = to_musb_ep(e);
  1213. musb = ep->musb;
  1214. req = to_musb_request(r);
  1215. req->musb = musb;
  1216. req->request.actual = 0;
  1217. req->request.status = -EINPROGRESS;
  1218. req->tx = ep->is_in;
  1219. spin_lock_irqsave(&musb->lock, lockflags);
  1220. if (!list_empty(&ep->req_list)) {
  1221. status = -EBUSY;
  1222. goto cleanup;
  1223. }
  1224. switch (musb->ep0_state) {
  1225. case MUSB_EP0_STAGE_RX: /* control-OUT data */
  1226. case MUSB_EP0_STAGE_TX: /* control-IN data */
  1227. case MUSB_EP0_STAGE_ACKWAIT: /* zero-length data */
  1228. status = 0;
  1229. break;
  1230. default:
  1231. dev_dbg(musb->controller, "ep0 request queued in state %d\n", musb->ep0_state);
  1232. status = -EINVAL;
  1233. goto cleanup;
  1234. }
  1235. /* add request to the list */
  1236. list_add_tail(&req->list, &ep->req_list);
  1237. /* dev_dbg(musb->controller, "queue to %s (%s), length=%d\n", */
  1238. os_printk(K_DEBUG, "queue to %s (%s), length=%d\n",
  1239. ep->name, ep->is_in ? "IN/TX" : "OUT/RX", req->request.length);
  1240. /* sequence #1, IN ... start writing the data */
  1241. if (musb->ep0_state == MUSB_EP0_STAGE_TX)
  1242. ep0_txstate(musb);
  1243. /* sequence #3, no-data ... issue IN status */
  1244. else if (musb->ep0_state == MUSB_EP0_STAGE_ACKWAIT) {
  1245. if (req->request.length)
  1246. status = -EINVAL;
  1247. else {
  1248. musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
  1249. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_STATUSIN\n");
  1250. os_writel(U3D_EP0CSR,
  1251. (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) | musb->ackpend |
  1252. EP0_DATAEND);
  1253. musb->ackpend = 0;
  1254. musb_g_ep0_giveback(ep->musb, r);
  1255. }
  1256. /* else for sequence #2 (OUT), caller provides a buffer
  1257. * before the next packet arrives. deferred responses
  1258. * (after SETUP is acked) are racey.
  1259. */
  1260. } else if (musb->ackpend) {
  1261. os_writel(U3D_EP0CSR, (os_readl(U3D_EP0CSR) & EP0_W1C_BITS) | musb->ackpend);
  1262. musb->ackpend = 0;
  1263. }
  1264. cleanup:
  1265. spin_unlock_irqrestore(&musb->lock, lockflags);
  1266. return status;
  1267. }
  1268. static int musb_g_ep0_dequeue(struct usb_ep *ep, struct usb_request *req)
  1269. {
  1270. /* we just won't support this */
  1271. return -EINVAL;
  1272. }
  1273. static int musb_g_ep0_halt(struct usb_ep *e, int value)
  1274. {
  1275. struct musb_ep *ep;
  1276. struct musb *musb;
  1277. void __iomem *base;
  1278. unsigned long flags;
  1279. int status;
  1280. u32 csr;
  1281. os_printk(K_INFO, "%s\n", __func__);
  1282. if (!e || !value)
  1283. return -EINVAL;
  1284. ep = to_musb_ep(e);
  1285. musb = ep->musb;
  1286. base = musb->mregs;
  1287. status = 0;
  1288. spin_lock_irqsave(&musb->lock, flags);
  1289. if (!list_empty(&ep->req_list)) {
  1290. status = -EBUSY;
  1291. goto cleanup;
  1292. }
  1293. csr = musb->ackpend;
  1294. switch (musb->ep0_state) {
  1295. /* Stalls are usually issued after parsing SETUP packet, either
  1296. * directly in irq context from setup() or else later.
  1297. */
  1298. case MUSB_EP0_STAGE_TX: /* control-IN data */
  1299. case MUSB_EP0_STAGE_ACKWAIT: /* STALL for zero-length data */
  1300. case MUSB_EP0_STAGE_RX: /* control-OUT data */
  1301. csr = os_readl(U3D_EP0CSR);
  1302. /* FALLTHROUGH */
  1303. /* It's also OK to issue stalls during callbacks when a non-empty
  1304. * DATA stage buffer has been read (or even written).
  1305. */
  1306. case MUSB_EP0_STAGE_STATUSIN: /* control-OUT status */
  1307. case MUSB_EP0_STAGE_STATUSOUT: /* control-IN status */
  1308. if (os_readl(U3D_EP0CSR) & EP0_TXPKTRDY) { /* try to flushfifo after clear sentstall */
  1309. #if 0
  1310. u32 csr0 = 0;
  1311. csr0 = os_readl(U3D_EP0CSR);
  1312. csr0 &= EP0_W1C_BITS; /* don't clear W1C bits */
  1313. csr0 |= CSR0_FLUSHFIFO;
  1314. os_writel(U3D_EP0CSR, csr0);
  1315. os_printk(K_DEBUG, "waiting for flushfifo.....");
  1316. while (os_readl(U3D_EP0CSR) & CSR0_FLUSHFIFO) { /* proceed before it clears */
  1317. cpu_relax();
  1318. }
  1319. os_printk(K_DEBUG, "done.\n");
  1320. #else
  1321. /* toggle EP0_RST */
  1322. os_setmsk(U3D_EP_RST, EP0_RST);
  1323. os_clrmsk(U3D_EP_RST, EP0_RST);
  1324. #endif
  1325. }
  1326. csr = (csr & EP0_W1C_BITS) | EP0_SENDSTALL;
  1327. os_printk(K_INFO, "@@@@@@@@@@@@@@@ SENDSTALL\n");
  1328. os_writel(U3D_EP0CSR, csr);
  1329. musb->ep0_state = MUSB_EP0_STAGE_IDLE;
  1330. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_STAGE_IDLE\n");
  1331. musb->ackpend = 0;
  1332. break;
  1333. default:
  1334. dev_dbg(musb->controller, "ep0 can't halt in state %d\n", musb->ep0_state);
  1335. status = -EINVAL;
  1336. }
  1337. cleanup:
  1338. spin_unlock_irqrestore(&musb->lock, flags);
  1339. return status;
  1340. }
  1341. const struct usb_ep_ops musb_g_ep0_ops = {
  1342. .enable = musb_g_ep0_enable,
  1343. .disable = musb_g_ep0_disable,
  1344. .alloc_request = musb_alloc_request,
  1345. .free_request = musb_free_request,
  1346. .queue = musb_g_ep0_queue,
  1347. .dequeue = musb_g_ep0_dequeue,
  1348. .set_halt = musb_g_ep0_halt,
  1349. };