musb_gadget_ep0.c 27 KB

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