musb_gadget_ep0.c 24 KB

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