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