musb_gadget_ep0.c 26 KB

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