musb_gadget_ep0.c 37 KB

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