musb_gadget_ep0.c 27 KB

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