musb_gadget_ep0.c 26 KB

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