musb_gadget.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. /*
  2. * MUSB OTG driver peripheral support
  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) 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/module.h>
  39. #include <linux/smp.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/delay.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/stat.h>
  44. #include <linux/dma-mapping.h>
  45. #include "musb_core.h"
  46. /* MUSB PERIPHERAL status 3-mar-2006:
  47. *
  48. * - EP0 seems solid. It passes both USBCV and usbtest control cases.
  49. * Minor glitches:
  50. *
  51. * + remote wakeup to Linux hosts work, but saw USBCV failures;
  52. * in one test run (operator error?)
  53. * + endpoint halt tests -- in both usbtest and usbcv -- seem
  54. * to break when dma is enabled ... is something wrongly
  55. * clearing SENDSTALL?
  56. *
  57. * - Mass storage behaved ok when last tested. Network traffic patterns
  58. * (with lots of short transfers etc) need retesting; they turn up the
  59. * worst cases of the DMA, since short packets are typical but are not
  60. * required.
  61. *
  62. * - TX/IN
  63. * + both pio and dma behave in with network and g_zero tests
  64. * + no cppi throughput issues other than no-hw-queueing
  65. * + failed with FLAT_REG (DaVinci)
  66. * + seems to behave with double buffering, PIO -and- CPPI
  67. * + with gadgetfs + AIO, requests got lost?
  68. *
  69. * - RX/OUT
  70. * + both pio and dma behave in with network and g_zero tests
  71. * + dma is slow in typical case (short_not_ok is clear)
  72. * + double buffering ok with PIO
  73. * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
  74. * + request lossage observed with gadgetfs
  75. *
  76. * - ISO not tested ... might work, but only weakly isochronous
  77. *
  78. * - Gadget driver disabling of softconnect during bind() is ignored; so
  79. * drivers can't hold off host requests until userspace is ready.
  80. * (Workaround: they can turn it off later.)
  81. *
  82. * - PORTABILITY (assumes PIO works):
  83. * + DaVinci, basically works with cppi dma
  84. * + OMAP 2430, ditto with mentor dma
  85. * + TUSB 6010, platform-specific dma in the works
  86. */
  87. /* ----------------------------------------------------------------------- */
  88. /*
  89. * Immediately complete a request.
  90. *
  91. * @param request the request to complete
  92. * @param status the status to complete the request with
  93. * Context: controller locked, IRQs blocked.
  94. */
  95. void musb_g_giveback(
  96. struct musb_ep *ep,
  97. struct usb_request *request,
  98. int status)
  99. __releases(ep->musb->lock)
  100. __acquires(ep->musb->lock)
  101. {
  102. struct musb_request *req;
  103. struct musb *musb;
  104. int busy = ep->busy;
  105. req = to_musb_request(request);
  106. list_del(&request->list);
  107. if (req->request.status == -EINPROGRESS)
  108. req->request.status = status;
  109. musb = req->musb;
  110. ep->busy = 1;
  111. spin_unlock(&musb->lock);
  112. if (is_dma_capable()) {
  113. if (req->mapped) {
  114. dma_unmap_single(musb->controller,
  115. req->request.dma,
  116. req->request.length,
  117. req->tx
  118. ? DMA_TO_DEVICE
  119. : DMA_FROM_DEVICE);
  120. req->request.dma = DMA_ADDR_INVALID;
  121. req->mapped = 0;
  122. } else if (req->request.dma != DMA_ADDR_INVALID)
  123. dma_sync_single_for_cpu(musb->controller,
  124. req->request.dma,
  125. req->request.length,
  126. req->tx
  127. ? DMA_TO_DEVICE
  128. : DMA_FROM_DEVICE);
  129. }
  130. if (request->status == 0)
  131. DBG(5, "%s done request %p, %d/%d\n",
  132. ep->end_point.name, request,
  133. req->request.actual, req->request.length);
  134. else
  135. DBG(2, "%s request %p, %d/%d fault %d\n",
  136. ep->end_point.name, request,
  137. req->request.actual, req->request.length,
  138. request->status);
  139. req->request.complete(&req->ep->end_point, &req->request);
  140. spin_lock(&musb->lock);
  141. ep->busy = busy;
  142. }
  143. /* ----------------------------------------------------------------------- */
  144. /*
  145. * Abort requests queued to an endpoint using the status. Synchronous.
  146. * caller locked controller and blocked irqs, and selected this ep.
  147. */
  148. static void nuke(struct musb_ep *ep, const int status)
  149. {
  150. struct musb_request *req = NULL;
  151. void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
  152. ep->busy = 1;
  153. if (is_dma_capable() && ep->dma) {
  154. struct dma_controller *c = ep->musb->dma_controller;
  155. int value;
  156. if (ep->is_in) {
  157. /*
  158. * The programming guide says that we must not clear
  159. * the DMAMODE bit before DMAENAB, so we only
  160. * clear it in the second write...
  161. */
  162. musb_writew(epio, MUSB_TXCSR,
  163. MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
  164. musb_writew(epio, MUSB_TXCSR,
  165. 0 | MUSB_TXCSR_FLUSHFIFO);
  166. } else {
  167. musb_writew(epio, MUSB_RXCSR,
  168. 0 | MUSB_RXCSR_FLUSHFIFO);
  169. musb_writew(epio, MUSB_RXCSR,
  170. 0 | MUSB_RXCSR_FLUSHFIFO);
  171. }
  172. value = c->channel_abort(ep->dma);
  173. DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value);
  174. c->channel_release(ep->dma);
  175. ep->dma = NULL;
  176. }
  177. while (!list_empty(&(ep->req_list))) {
  178. req = container_of(ep->req_list.next, struct musb_request,
  179. request.list);
  180. musb_g_giveback(ep, &req->request, status);
  181. }
  182. }
  183. /* ----------------------------------------------------------------------- */
  184. /* Data transfers - pure PIO, pure DMA, or mixed mode */
  185. /*
  186. * This assumes the separate CPPI engine is responding to DMA requests
  187. * from the usb core ... sequenced a bit differently from mentor dma.
  188. */
  189. static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
  190. {
  191. if (can_bulk_split(musb, ep->type))
  192. return ep->hw_ep->max_packet_sz_tx;
  193. else
  194. return ep->packet_sz;
  195. }
  196. #ifdef CONFIG_USB_INVENTRA_DMA
  197. /* Peripheral tx (IN) using Mentor DMA works as follows:
  198. Only mode 0 is used for transfers <= wPktSize,
  199. mode 1 is used for larger transfers,
  200. One of the following happens:
  201. - Host sends IN token which causes an endpoint interrupt
  202. -> TxAvail
  203. -> if DMA is currently busy, exit.
  204. -> if queue is non-empty, txstate().
  205. - Request is queued by the gadget driver.
  206. -> if queue was previously empty, txstate()
  207. txstate()
  208. -> start
  209. /\ -> setup DMA
  210. | (data is transferred to the FIFO, then sent out when
  211. | IN token(s) are recd from Host.
  212. | -> DMA interrupt on completion
  213. | calls TxAvail.
  214. | -> stop DMA, ~DMAENAB,
  215. | -> set TxPktRdy for last short pkt or zlp
  216. | -> Complete Request
  217. | -> Continue next request (call txstate)
  218. |___________________________________|
  219. * Non-Mentor DMA engines can of course work differently, such as by
  220. * upleveling from irq-per-packet to irq-per-buffer.
  221. */
  222. #endif
  223. /*
  224. * An endpoint is transmitting data. This can be called either from
  225. * the IRQ routine or from ep.queue() to kickstart a request on an
  226. * endpoint.
  227. *
  228. * Context: controller locked, IRQs blocked, endpoint selected
  229. */
  230. static void txstate(struct musb *musb, struct musb_request *req)
  231. {
  232. u8 epnum = req->epnum;
  233. struct musb_ep *musb_ep;
  234. void __iomem *epio = musb->endpoints[epnum].regs;
  235. struct usb_request *request;
  236. u16 fifo_count = 0, csr;
  237. int use_dma = 0;
  238. musb_ep = req->ep;
  239. /* we shouldn't get here while DMA is active ... but we do ... */
  240. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  241. DBG(4, "dma pending...\n");
  242. return;
  243. }
  244. /* read TXCSR before */
  245. csr = musb_readw(epio, MUSB_TXCSR);
  246. request = &req->request;
  247. fifo_count = min(max_ep_writesize(musb, musb_ep),
  248. (int)(request->length - request->actual));
  249. if (csr & MUSB_TXCSR_TXPKTRDY) {
  250. DBG(5, "%s old packet still ready , txcsr %03x\n",
  251. musb_ep->end_point.name, csr);
  252. return;
  253. }
  254. if (csr & MUSB_TXCSR_P_SENDSTALL) {
  255. DBG(5, "%s stalling, txcsr %03x\n",
  256. musb_ep->end_point.name, csr);
  257. return;
  258. }
  259. DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
  260. epnum, musb_ep->packet_sz, fifo_count,
  261. csr);
  262. #ifndef CONFIG_MUSB_PIO_ONLY
  263. if (is_dma_capable() && musb_ep->dma) {
  264. struct dma_controller *c = musb->dma_controller;
  265. use_dma = (request->dma != DMA_ADDR_INVALID);
  266. /* MUSB_TXCSR_P_ISO is still set correctly */
  267. #ifdef CONFIG_USB_INVENTRA_DMA
  268. {
  269. size_t request_size;
  270. /* setup DMA, then program endpoint CSR */
  271. request_size = min_t(size_t, request->length,
  272. musb_ep->dma->max_len);
  273. if (request_size < musb_ep->packet_sz)
  274. musb_ep->dma->desired_mode = 0;
  275. else
  276. musb_ep->dma->desired_mode = 1;
  277. use_dma = use_dma && c->channel_program(
  278. musb_ep->dma, musb_ep->packet_sz,
  279. musb_ep->dma->desired_mode,
  280. request->dma + request->actual, request_size);
  281. if (use_dma) {
  282. if (musb_ep->dma->desired_mode == 0) {
  283. /*
  284. * We must not clear the DMAMODE bit
  285. * before the DMAENAB bit -- and the
  286. * latter doesn't always get cleared
  287. * before we get here...
  288. */
  289. csr &= ~(MUSB_TXCSR_AUTOSET
  290. | MUSB_TXCSR_DMAENAB);
  291. musb_writew(epio, MUSB_TXCSR, csr
  292. | MUSB_TXCSR_P_WZC_BITS);
  293. csr &= ~MUSB_TXCSR_DMAMODE;
  294. csr |= (MUSB_TXCSR_DMAENAB |
  295. MUSB_TXCSR_MODE);
  296. /* against programming guide */
  297. } else
  298. csr |= (MUSB_TXCSR_AUTOSET
  299. | MUSB_TXCSR_DMAENAB
  300. | MUSB_TXCSR_DMAMODE
  301. | MUSB_TXCSR_MODE);
  302. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  303. musb_writew(epio, MUSB_TXCSR, csr);
  304. }
  305. }
  306. #elif defined(CONFIG_USB_TI_CPPI_DMA)
  307. /* program endpoint CSR first, then setup DMA */
  308. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  309. csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
  310. MUSB_TXCSR_MODE;
  311. musb_writew(epio, MUSB_TXCSR,
  312. (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
  313. | csr);
  314. /* ensure writebuffer is empty */
  315. csr = musb_readw(epio, MUSB_TXCSR);
  316. /* NOTE host side sets DMAENAB later than this; both are
  317. * OK since the transfer dma glue (between CPPI and Mentor
  318. * fifos) just tells CPPI it could start. Data only moves
  319. * to the USB TX fifo when both fifos are ready.
  320. */
  321. /* "mode" is irrelevant here; handle terminating ZLPs like
  322. * PIO does, since the hardware RNDIS mode seems unreliable
  323. * except for the last-packet-is-already-short case.
  324. */
  325. use_dma = use_dma && c->channel_program(
  326. musb_ep->dma, musb_ep->packet_sz,
  327. 0,
  328. request->dma,
  329. request->length);
  330. if (!use_dma) {
  331. c->channel_release(musb_ep->dma);
  332. musb_ep->dma = NULL;
  333. csr &= ~MUSB_TXCSR_DMAENAB;
  334. musb_writew(epio, MUSB_TXCSR, csr);
  335. /* invariant: prequest->buf is non-null */
  336. }
  337. #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
  338. use_dma = use_dma && c->channel_program(
  339. musb_ep->dma, musb_ep->packet_sz,
  340. request->zero,
  341. request->dma,
  342. request->length);
  343. #endif
  344. }
  345. #endif
  346. if (!use_dma) {
  347. musb_write_fifo(musb_ep->hw_ep, fifo_count,
  348. (u8 *) (request->buf + request->actual));
  349. request->actual += fifo_count;
  350. csr |= MUSB_TXCSR_TXPKTRDY;
  351. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  352. musb_writew(epio, MUSB_TXCSR, csr);
  353. }
  354. /* host may already have the data when this message shows... */
  355. DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
  356. musb_ep->end_point.name, use_dma ? "dma" : "pio",
  357. request->actual, request->length,
  358. musb_readw(epio, MUSB_TXCSR),
  359. fifo_count,
  360. musb_readw(epio, MUSB_TXMAXP));
  361. }
  362. /*
  363. * FIFO state update (e.g. data ready).
  364. * Called from IRQ, with controller locked.
  365. */
  366. void musb_g_tx(struct musb *musb, u8 epnum)
  367. {
  368. u16 csr;
  369. struct usb_request *request;
  370. u8 __iomem *mbase = musb->mregs;
  371. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
  372. void __iomem *epio = musb->endpoints[epnum].regs;
  373. struct dma_channel *dma;
  374. musb_ep_select(mbase, epnum);
  375. request = next_request(musb_ep);
  376. csr = musb_readw(epio, MUSB_TXCSR);
  377. DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
  378. dma = is_dma_capable() ? musb_ep->dma : NULL;
  379. /*
  380. * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
  381. * probably rates reporting as a host error.
  382. */
  383. if (csr & MUSB_TXCSR_P_SENTSTALL) {
  384. csr |= MUSB_TXCSR_P_WZC_BITS;
  385. csr &= ~MUSB_TXCSR_P_SENTSTALL;
  386. musb_writew(epio, MUSB_TXCSR, csr);
  387. return;
  388. }
  389. if (csr & MUSB_TXCSR_P_UNDERRUN) {
  390. /* We NAKed, no big deal... little reason to care. */
  391. csr |= MUSB_TXCSR_P_WZC_BITS;
  392. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  393. musb_writew(epio, MUSB_TXCSR, csr);
  394. DBG(20, "underrun on ep%d, req %p\n", epnum, request);
  395. }
  396. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  397. /*
  398. * SHOULD NOT HAPPEN... has with CPPI though, after
  399. * changing SENDSTALL (and other cases); harmless?
  400. */
  401. DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
  402. return;
  403. }
  404. if (request) {
  405. u8 is_dma = 0;
  406. if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
  407. is_dma = 1;
  408. csr |= MUSB_TXCSR_P_WZC_BITS;
  409. csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
  410. MUSB_TXCSR_TXPKTRDY);
  411. musb_writew(epio, MUSB_TXCSR, csr);
  412. /* Ensure writebuffer is empty. */
  413. csr = musb_readw(epio, MUSB_TXCSR);
  414. request->actual += musb_ep->dma->actual_len;
  415. DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
  416. epnum, csr, musb_ep->dma->actual_len, request);
  417. }
  418. if (is_dma || request->actual == request->length) {
  419. /*
  420. * First, maybe a terminating short packet. Some DMA
  421. * engines might handle this by themselves.
  422. */
  423. if ((request->zero && request->length
  424. && request->length % musb_ep->packet_sz == 0)
  425. #ifdef CONFIG_USB_INVENTRA_DMA
  426. || (is_dma && (!dma->desired_mode ||
  427. (request->actual &
  428. (musb_ep->packet_sz - 1))))
  429. #endif
  430. ) {
  431. /*
  432. * On DMA completion, FIFO may not be
  433. * available yet...
  434. */
  435. if (csr & MUSB_TXCSR_TXPKTRDY)
  436. return;
  437. DBG(4, "sending zero pkt\n");
  438. musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
  439. | MUSB_TXCSR_TXPKTRDY);
  440. request->zero = 0;
  441. }
  442. /* ... or if not, then complete it. */
  443. musb_g_giveback(musb_ep, request, 0);
  444. /*
  445. * Kickstart next transfer if appropriate;
  446. * the packet that just completed might not
  447. * be transmitted for hours or days.
  448. * REVISIT for double buffering...
  449. * FIXME revisit for stalls too...
  450. */
  451. musb_ep_select(mbase, epnum);
  452. csr = musb_readw(epio, MUSB_TXCSR);
  453. if (csr & MUSB_TXCSR_FIFONOTEMPTY)
  454. return;
  455. request = musb_ep->desc ? next_request(musb_ep) : NULL;
  456. if (!request) {
  457. DBG(4, "%s idle now\n",
  458. musb_ep->end_point.name);
  459. return;
  460. }
  461. }
  462. txstate(musb, to_musb_request(request));
  463. }
  464. }
  465. /* ------------------------------------------------------------ */
  466. #ifdef CONFIG_USB_INVENTRA_DMA
  467. /* Peripheral rx (OUT) using Mentor DMA works as follows:
  468. - Only mode 0 is used.
  469. - Request is queued by the gadget class driver.
  470. -> if queue was previously empty, rxstate()
  471. - Host sends OUT token which causes an endpoint interrupt
  472. /\ -> RxReady
  473. | -> if request queued, call rxstate
  474. | /\ -> setup DMA
  475. | | -> DMA interrupt on completion
  476. | | -> RxReady
  477. | | -> stop DMA
  478. | | -> ack the read
  479. | | -> if data recd = max expected
  480. | | by the request, or host
  481. | | sent a short packet,
  482. | | complete the request,
  483. | | and start the next one.
  484. | |_____________________________________|
  485. | else just wait for the host
  486. | to send the next OUT token.
  487. |__________________________________________________|
  488. * Non-Mentor DMA engines can of course work differently.
  489. */
  490. #endif
  491. /*
  492. * Context: controller locked, IRQs blocked, endpoint selected
  493. */
  494. static void rxstate(struct musb *musb, struct musb_request *req)
  495. {
  496. const u8 epnum = req->epnum;
  497. struct usb_request *request = &req->request;
  498. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_out;
  499. void __iomem *epio = musb->endpoints[epnum].regs;
  500. unsigned fifo_count = 0;
  501. u16 len = musb_ep->packet_sz;
  502. u16 csr = musb_readw(epio, MUSB_RXCSR);
  503. /* We shouldn't get here while DMA is active, but we do... */
  504. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  505. DBG(4, "DMA pending...\n");
  506. return;
  507. }
  508. if (csr & MUSB_RXCSR_P_SENDSTALL) {
  509. DBG(5, "%s stalling, RXCSR %04x\n",
  510. musb_ep->end_point.name, csr);
  511. return;
  512. }
  513. if (is_cppi_enabled() && musb_ep->dma) {
  514. struct dma_controller *c = musb->dma_controller;
  515. struct dma_channel *channel = musb_ep->dma;
  516. /* NOTE: CPPI won't actually stop advancing the DMA
  517. * queue after short packet transfers, so this is almost
  518. * always going to run as IRQ-per-packet DMA so that
  519. * faults will be handled correctly.
  520. */
  521. if (c->channel_program(channel,
  522. musb_ep->packet_sz,
  523. !request->short_not_ok,
  524. request->dma + request->actual,
  525. request->length - request->actual)) {
  526. /* make sure that if an rxpkt arrived after the irq,
  527. * the cppi engine will be ready to take it as soon
  528. * as DMA is enabled
  529. */
  530. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  531. | MUSB_RXCSR_DMAMODE);
  532. csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
  533. musb_writew(epio, MUSB_RXCSR, csr);
  534. return;
  535. }
  536. }
  537. if (csr & MUSB_RXCSR_RXPKTRDY) {
  538. len = musb_readw(epio, MUSB_RXCOUNT);
  539. if (request->actual < request->length) {
  540. #ifdef CONFIG_USB_INVENTRA_DMA
  541. if (is_dma_capable() && musb_ep->dma) {
  542. struct dma_controller *c;
  543. struct dma_channel *channel;
  544. int use_dma = 0;
  545. c = musb->dma_controller;
  546. channel = musb_ep->dma;
  547. /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
  548. * mode 0 only. So we do not get endpoint interrupts due to DMA
  549. * completion. We only get interrupts from DMA controller.
  550. *
  551. * We could operate in DMA mode 1 if we knew the size of the tranfer
  552. * in advance. For mass storage class, request->length = what the host
  553. * sends, so that'd work. But for pretty much everything else,
  554. * request->length is routinely more than what the host sends. For
  555. * most these gadgets, end of is signified either by a short packet,
  556. * or filling the last byte of the buffer. (Sending extra data in
  557. * that last pckate should trigger an overflow fault.) But in mode 1,
  558. * we don't get DMA completion interrrupt for short packets.
  559. *
  560. * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
  561. * to get endpoint interrupt on every DMA req, but that didn't seem
  562. * to work reliably.
  563. *
  564. * REVISIT an updated g_file_storage can set req->short_not_ok, which
  565. * then becomes usable as a runtime "use mode 1" hint...
  566. */
  567. csr |= MUSB_RXCSR_DMAENAB;
  568. #ifdef USE_MODE1
  569. csr |= MUSB_RXCSR_AUTOCLEAR;
  570. /* csr |= MUSB_RXCSR_DMAMODE; */
  571. /* this special sequence (enabling and then
  572. * disabling MUSB_RXCSR_DMAMODE) is required
  573. * to get DMAReq to activate
  574. */
  575. musb_writew(epio, MUSB_RXCSR,
  576. csr | MUSB_RXCSR_DMAMODE);
  577. #endif
  578. musb_writew(epio, MUSB_RXCSR, csr);
  579. if (request->actual < request->length) {
  580. int transfer_size = 0;
  581. #ifdef USE_MODE1
  582. transfer_size = min(request->length,
  583. channel->max_len);
  584. #else
  585. transfer_size = len;
  586. #endif
  587. if (transfer_size <= musb_ep->packet_sz)
  588. musb_ep->dma->desired_mode = 0;
  589. else
  590. musb_ep->dma->desired_mode = 1;
  591. use_dma = c->channel_program(
  592. channel,
  593. musb_ep->packet_sz,
  594. channel->desired_mode,
  595. request->dma
  596. + request->actual,
  597. transfer_size);
  598. }
  599. if (use_dma)
  600. return;
  601. }
  602. #endif /* Mentor's DMA */
  603. fifo_count = request->length - request->actual;
  604. DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
  605. musb_ep->end_point.name,
  606. len, fifo_count,
  607. musb_ep->packet_sz);
  608. fifo_count = min_t(unsigned, len, fifo_count);
  609. #ifdef CONFIG_USB_TUSB_OMAP_DMA
  610. if (tusb_dma_omap() && musb_ep->dma) {
  611. struct dma_controller *c = musb->dma_controller;
  612. struct dma_channel *channel = musb_ep->dma;
  613. u32 dma_addr = request->dma + request->actual;
  614. int ret;
  615. ret = c->channel_program(channel,
  616. musb_ep->packet_sz,
  617. channel->desired_mode,
  618. dma_addr,
  619. fifo_count);
  620. if (ret)
  621. return;
  622. }
  623. #endif
  624. musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
  625. (request->buf + request->actual));
  626. request->actual += fifo_count;
  627. /* REVISIT if we left anything in the fifo, flush
  628. * it and report -EOVERFLOW
  629. */
  630. /* ack the read! */
  631. csr |= MUSB_RXCSR_P_WZC_BITS;
  632. csr &= ~MUSB_RXCSR_RXPKTRDY;
  633. musb_writew(epio, MUSB_RXCSR, csr);
  634. }
  635. }
  636. /* reach the end or short packet detected */
  637. if (request->actual == request->length || len < musb_ep->packet_sz)
  638. musb_g_giveback(musb_ep, request, 0);
  639. }
  640. /*
  641. * Data ready for a request; called from IRQ
  642. */
  643. void musb_g_rx(struct musb *musb, u8 epnum)
  644. {
  645. u16 csr;
  646. struct usb_request *request;
  647. void __iomem *mbase = musb->mregs;
  648. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_out;
  649. void __iomem *epio = musb->endpoints[epnum].regs;
  650. struct dma_channel *dma;
  651. musb_ep_select(mbase, epnum);
  652. request = next_request(musb_ep);
  653. if (!request)
  654. return;
  655. csr = musb_readw(epio, MUSB_RXCSR);
  656. dma = is_dma_capable() ? musb_ep->dma : NULL;
  657. DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
  658. csr, dma ? " (dma)" : "", request);
  659. if (csr & MUSB_RXCSR_P_SENTSTALL) {
  660. csr |= MUSB_RXCSR_P_WZC_BITS;
  661. csr &= ~MUSB_RXCSR_P_SENTSTALL;
  662. musb_writew(epio, MUSB_RXCSR, csr);
  663. return;
  664. }
  665. if (csr & MUSB_RXCSR_P_OVERRUN) {
  666. /* csr |= MUSB_RXCSR_P_WZC_BITS; */
  667. csr &= ~MUSB_RXCSR_P_OVERRUN;
  668. musb_writew(epio, MUSB_RXCSR, csr);
  669. DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
  670. if (request && request->status == -EINPROGRESS)
  671. request->status = -EOVERFLOW;
  672. }
  673. if (csr & MUSB_RXCSR_INCOMPRX) {
  674. /* REVISIT not necessarily an error */
  675. DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
  676. }
  677. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  678. /* "should not happen"; likely RXPKTRDY pending for DMA */
  679. DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
  680. "%s busy, csr %04x\n",
  681. musb_ep->end_point.name, csr);
  682. return;
  683. }
  684. if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
  685. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  686. | MUSB_RXCSR_DMAENAB
  687. | MUSB_RXCSR_DMAMODE);
  688. musb_writew(epio, MUSB_RXCSR,
  689. MUSB_RXCSR_P_WZC_BITS | csr);
  690. request->actual += musb_ep->dma->actual_len;
  691. DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
  692. epnum, csr,
  693. musb_readw(epio, MUSB_RXCSR),
  694. musb_ep->dma->actual_len, request);
  695. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
  696. /* Autoclear doesn't clear RxPktRdy for short packets */
  697. if ((dma->desired_mode == 0)
  698. || (dma->actual_len
  699. & (musb_ep->packet_sz - 1))) {
  700. /* ack the read! */
  701. csr &= ~MUSB_RXCSR_RXPKTRDY;
  702. musb_writew(epio, MUSB_RXCSR, csr);
  703. }
  704. /* incomplete, and not short? wait for next IN packet */
  705. if ((request->actual < request->length)
  706. && (musb_ep->dma->actual_len
  707. == musb_ep->packet_sz))
  708. return;
  709. #endif
  710. musb_g_giveback(musb_ep, request, 0);
  711. request = next_request(musb_ep);
  712. if (!request)
  713. return;
  714. }
  715. /* analyze request if the ep is hot */
  716. if (request)
  717. rxstate(musb, to_musb_request(request));
  718. else
  719. DBG(3, "packet waiting for %s%s request\n",
  720. musb_ep->desc ? "" : "inactive ",
  721. musb_ep->end_point.name);
  722. return;
  723. }
  724. /* ------------------------------------------------------------ */
  725. static int musb_gadget_enable(struct usb_ep *ep,
  726. const struct usb_endpoint_descriptor *desc)
  727. {
  728. unsigned long flags;
  729. struct musb_ep *musb_ep;
  730. struct musb_hw_ep *hw_ep;
  731. void __iomem *regs;
  732. struct musb *musb;
  733. void __iomem *mbase;
  734. u8 epnum;
  735. u16 csr;
  736. unsigned tmp;
  737. int status = -EINVAL;
  738. if (!ep || !desc)
  739. return -EINVAL;
  740. musb_ep = to_musb_ep(ep);
  741. hw_ep = musb_ep->hw_ep;
  742. regs = hw_ep->regs;
  743. musb = musb_ep->musb;
  744. mbase = musb->mregs;
  745. epnum = musb_ep->current_epnum;
  746. spin_lock_irqsave(&musb->lock, flags);
  747. if (musb_ep->desc) {
  748. status = -EBUSY;
  749. goto fail;
  750. }
  751. musb_ep->type = usb_endpoint_type(desc);
  752. /* check direction and (later) maxpacket size against endpoint */
  753. if (usb_endpoint_num(desc) != epnum)
  754. goto fail;
  755. /* REVISIT this rules out high bandwidth periodic transfers */
  756. tmp = le16_to_cpu(desc->wMaxPacketSize);
  757. if (tmp & ~0x07ff)
  758. goto fail;
  759. musb_ep->packet_sz = tmp;
  760. /* enable the interrupts for the endpoint, set the endpoint
  761. * packet size (or fail), set the mode, clear the fifo
  762. */
  763. musb_ep_select(mbase, epnum);
  764. if (usb_endpoint_dir_in(desc)) {
  765. u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
  766. if (hw_ep->is_shared_fifo)
  767. musb_ep->is_in = 1;
  768. if (!musb_ep->is_in)
  769. goto fail;
  770. if (tmp > hw_ep->max_packet_sz_tx)
  771. goto fail;
  772. int_txe |= (1 << epnum);
  773. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  774. /* REVISIT if can_bulk_split(), use by updating "tmp";
  775. * likewise high bandwidth periodic tx
  776. */
  777. musb_writew(regs, MUSB_TXMAXP, tmp);
  778. csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
  779. if (musb_readw(regs, MUSB_TXCSR)
  780. & MUSB_TXCSR_FIFONOTEMPTY)
  781. csr |= MUSB_TXCSR_FLUSHFIFO;
  782. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  783. csr |= MUSB_TXCSR_P_ISO;
  784. /* set twice in case of double buffering */
  785. musb_writew(regs, MUSB_TXCSR, csr);
  786. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  787. musb_writew(regs, MUSB_TXCSR, csr);
  788. } else {
  789. u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
  790. if (hw_ep->is_shared_fifo)
  791. musb_ep->is_in = 0;
  792. if (musb_ep->is_in)
  793. goto fail;
  794. if (tmp > hw_ep->max_packet_sz_rx)
  795. goto fail;
  796. int_rxe |= (1 << epnum);
  797. musb_writew(mbase, MUSB_INTRRXE, int_rxe);
  798. /* REVISIT if can_bulk_combine() use by updating "tmp"
  799. * likewise high bandwidth periodic rx
  800. */
  801. musb_writew(regs, MUSB_RXMAXP, tmp);
  802. /* force shared fifo to OUT-only mode */
  803. if (hw_ep->is_shared_fifo) {
  804. csr = musb_readw(regs, MUSB_TXCSR);
  805. csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
  806. musb_writew(regs, MUSB_TXCSR, csr);
  807. }
  808. csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
  809. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  810. csr |= MUSB_RXCSR_P_ISO;
  811. else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
  812. csr |= MUSB_RXCSR_DISNYET;
  813. /* set twice in case of double buffering */
  814. musb_writew(regs, MUSB_RXCSR, csr);
  815. musb_writew(regs, MUSB_RXCSR, csr);
  816. }
  817. /* NOTE: all the I/O code _should_ work fine without DMA, in case
  818. * for some reason you run out of channels here.
  819. */
  820. if (is_dma_capable() && musb->dma_controller) {
  821. struct dma_controller *c = musb->dma_controller;
  822. musb_ep->dma = c->channel_alloc(c, hw_ep,
  823. (desc->bEndpointAddress & USB_DIR_IN));
  824. } else
  825. musb_ep->dma = NULL;
  826. musb_ep->desc = desc;
  827. musb_ep->busy = 0;
  828. musb_ep->wedged = 0;
  829. status = 0;
  830. pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
  831. musb_driver_name, musb_ep->end_point.name,
  832. ({ char *s; switch (musb_ep->type) {
  833. case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
  834. case USB_ENDPOINT_XFER_INT: s = "int"; break;
  835. default: s = "iso"; break;
  836. }; s; }),
  837. musb_ep->is_in ? "IN" : "OUT",
  838. musb_ep->dma ? "dma, " : "",
  839. musb_ep->packet_sz);
  840. schedule_work(&musb->irq_work);
  841. fail:
  842. spin_unlock_irqrestore(&musb->lock, flags);
  843. return status;
  844. }
  845. /*
  846. * Disable an endpoint flushing all requests queued.
  847. */
  848. static int musb_gadget_disable(struct usb_ep *ep)
  849. {
  850. unsigned long flags;
  851. struct musb *musb;
  852. u8 epnum;
  853. struct musb_ep *musb_ep;
  854. void __iomem *epio;
  855. int status = 0;
  856. musb_ep = to_musb_ep(ep);
  857. musb = musb_ep->musb;
  858. epnum = musb_ep->current_epnum;
  859. epio = musb->endpoints[epnum].regs;
  860. spin_lock_irqsave(&musb->lock, flags);
  861. musb_ep_select(musb->mregs, epnum);
  862. /* zero the endpoint sizes */
  863. if (musb_ep->is_in) {
  864. u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
  865. int_txe &= ~(1 << epnum);
  866. musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
  867. musb_writew(epio, MUSB_TXMAXP, 0);
  868. } else {
  869. u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
  870. int_rxe &= ~(1 << epnum);
  871. musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
  872. musb_writew(epio, MUSB_RXMAXP, 0);
  873. }
  874. musb_ep->desc = NULL;
  875. /* abort all pending DMA and requests */
  876. nuke(musb_ep, -ESHUTDOWN);
  877. schedule_work(&musb->irq_work);
  878. spin_unlock_irqrestore(&(musb->lock), flags);
  879. DBG(2, "%s\n", musb_ep->end_point.name);
  880. return status;
  881. }
  882. /*
  883. * Allocate a request for an endpoint.
  884. * Reused by ep0 code.
  885. */
  886. struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  887. {
  888. struct musb_ep *musb_ep = to_musb_ep(ep);
  889. struct musb_request *request = NULL;
  890. request = kzalloc(sizeof *request, gfp_flags);
  891. if (request) {
  892. INIT_LIST_HEAD(&request->request.list);
  893. request->request.dma = DMA_ADDR_INVALID;
  894. request->epnum = musb_ep->current_epnum;
  895. request->ep = musb_ep;
  896. }
  897. return &request->request;
  898. }
  899. /*
  900. * Free a request
  901. * Reused by ep0 code.
  902. */
  903. void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  904. {
  905. kfree(to_musb_request(req));
  906. }
  907. static LIST_HEAD(buffers);
  908. struct free_record {
  909. struct list_head list;
  910. struct device *dev;
  911. unsigned bytes;
  912. dma_addr_t dma;
  913. };
  914. /*
  915. * Context: controller locked, IRQs blocked.
  916. */
  917. static void musb_ep_restart(struct musb *musb, struct musb_request *req)
  918. {
  919. DBG(3, "<== %s request %p len %u on hw_ep%d\n",
  920. req->tx ? "TX/IN" : "RX/OUT",
  921. &req->request, req->request.length, req->epnum);
  922. musb_ep_select(musb->mregs, req->epnum);
  923. if (req->tx)
  924. txstate(musb, req);
  925. else
  926. rxstate(musb, req);
  927. }
  928. static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
  929. gfp_t gfp_flags)
  930. {
  931. struct musb_ep *musb_ep;
  932. struct musb_request *request;
  933. struct musb *musb;
  934. int status = 0;
  935. unsigned long lockflags;
  936. if (!ep || !req)
  937. return -EINVAL;
  938. if (!req->buf)
  939. return -ENODATA;
  940. musb_ep = to_musb_ep(ep);
  941. musb = musb_ep->musb;
  942. request = to_musb_request(req);
  943. request->musb = musb;
  944. if (request->ep != musb_ep)
  945. return -EINVAL;
  946. DBG(4, "<== to %s request=%p\n", ep->name, req);
  947. /* request is mine now... */
  948. request->request.actual = 0;
  949. request->request.status = -EINPROGRESS;
  950. request->epnum = musb_ep->current_epnum;
  951. request->tx = musb_ep->is_in;
  952. if (is_dma_capable() && musb_ep->dma) {
  953. if (request->request.dma == DMA_ADDR_INVALID) {
  954. request->request.dma = dma_map_single(
  955. musb->controller,
  956. request->request.buf,
  957. request->request.length,
  958. request->tx
  959. ? DMA_TO_DEVICE
  960. : DMA_FROM_DEVICE);
  961. request->mapped = 1;
  962. } else {
  963. dma_sync_single_for_device(musb->controller,
  964. request->request.dma,
  965. request->request.length,
  966. request->tx
  967. ? DMA_TO_DEVICE
  968. : DMA_FROM_DEVICE);
  969. request->mapped = 0;
  970. }
  971. } else if (!req->buf) {
  972. return -ENODATA;
  973. } else
  974. request->mapped = 0;
  975. spin_lock_irqsave(&musb->lock, lockflags);
  976. /* don't queue if the ep is down */
  977. if (!musb_ep->desc) {
  978. DBG(4, "req %p queued to %s while ep %s\n",
  979. req, ep->name, "disabled");
  980. status = -ESHUTDOWN;
  981. goto cleanup;
  982. }
  983. /* add request to the list */
  984. list_add_tail(&(request->request.list), &(musb_ep->req_list));
  985. /* it this is the head of the queue, start i/o ... */
  986. if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
  987. musb_ep_restart(musb, request);
  988. cleanup:
  989. spin_unlock_irqrestore(&musb->lock, lockflags);
  990. return status;
  991. }
  992. static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
  993. {
  994. struct musb_ep *musb_ep = to_musb_ep(ep);
  995. struct usb_request *r;
  996. unsigned long flags;
  997. int status = 0;
  998. struct musb *musb = musb_ep->musb;
  999. if (!ep || !request || to_musb_request(request)->ep != musb_ep)
  1000. return -EINVAL;
  1001. spin_lock_irqsave(&musb->lock, flags);
  1002. list_for_each_entry(r, &musb_ep->req_list, list) {
  1003. if (r == request)
  1004. break;
  1005. }
  1006. if (r != request) {
  1007. DBG(3, "request %p not queued to %s\n", request, ep->name);
  1008. status = -EINVAL;
  1009. goto done;
  1010. }
  1011. /* if the hardware doesn't have the request, easy ... */
  1012. if (musb_ep->req_list.next != &request->list || musb_ep->busy)
  1013. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1014. /* ... else abort the dma transfer ... */
  1015. else if (is_dma_capable() && musb_ep->dma) {
  1016. struct dma_controller *c = musb->dma_controller;
  1017. musb_ep_select(musb->mregs, musb_ep->current_epnum);
  1018. if (c->channel_abort)
  1019. status = c->channel_abort(musb_ep->dma);
  1020. else
  1021. status = -EBUSY;
  1022. if (status == 0)
  1023. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1024. } else {
  1025. /* NOTE: by sticking to easily tested hardware/driver states,
  1026. * we leave counting of in-flight packets imprecise.
  1027. */
  1028. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1029. }
  1030. done:
  1031. spin_unlock_irqrestore(&musb->lock, flags);
  1032. return status;
  1033. }
  1034. /*
  1035. * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
  1036. * data but will queue requests.
  1037. *
  1038. * exported to ep0 code
  1039. */
  1040. static int musb_gadget_set_halt(struct usb_ep *ep, int value)
  1041. {
  1042. struct musb_ep *musb_ep = to_musb_ep(ep);
  1043. u8 epnum = musb_ep->current_epnum;
  1044. struct musb *musb = musb_ep->musb;
  1045. void __iomem *epio = musb->endpoints[epnum].regs;
  1046. void __iomem *mbase;
  1047. unsigned long flags;
  1048. u16 csr;
  1049. struct musb_request *request;
  1050. int status = 0;
  1051. if (!ep)
  1052. return -EINVAL;
  1053. mbase = musb->mregs;
  1054. spin_lock_irqsave(&musb->lock, flags);
  1055. if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
  1056. status = -EINVAL;
  1057. goto done;
  1058. }
  1059. musb_ep_select(mbase, epnum);
  1060. request = to_musb_request(next_request(musb_ep));
  1061. if (value) {
  1062. if (request) {
  1063. DBG(3, "request in progress, cannot halt %s\n",
  1064. ep->name);
  1065. status = -EAGAIN;
  1066. goto done;
  1067. }
  1068. /* Cannot portably stall with non-empty FIFO */
  1069. if (musb_ep->is_in) {
  1070. csr = musb_readw(epio, MUSB_TXCSR);
  1071. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1072. DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
  1073. status = -EAGAIN;
  1074. goto done;
  1075. }
  1076. }
  1077. } else
  1078. musb_ep->wedged = 0;
  1079. /* set/clear the stall and toggle bits */
  1080. DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
  1081. if (musb_ep->is_in) {
  1082. csr = musb_readw(epio, MUSB_TXCSR);
  1083. csr |= MUSB_TXCSR_P_WZC_BITS
  1084. | MUSB_TXCSR_CLRDATATOG;
  1085. if (value)
  1086. csr |= MUSB_TXCSR_P_SENDSTALL;
  1087. else
  1088. csr &= ~(MUSB_TXCSR_P_SENDSTALL
  1089. | MUSB_TXCSR_P_SENTSTALL);
  1090. csr &= ~MUSB_TXCSR_TXPKTRDY;
  1091. musb_writew(epio, MUSB_TXCSR, csr);
  1092. } else {
  1093. csr = musb_readw(epio, MUSB_RXCSR);
  1094. csr |= MUSB_RXCSR_P_WZC_BITS
  1095. | MUSB_RXCSR_FLUSHFIFO
  1096. | MUSB_RXCSR_CLRDATATOG;
  1097. if (value)
  1098. csr |= MUSB_RXCSR_P_SENDSTALL;
  1099. else
  1100. csr &= ~(MUSB_RXCSR_P_SENDSTALL
  1101. | MUSB_RXCSR_P_SENTSTALL);
  1102. musb_writew(epio, MUSB_RXCSR, csr);
  1103. }
  1104. /* maybe start the first request in the queue */
  1105. if (!musb_ep->busy && !value && request) {
  1106. DBG(3, "restarting the request\n");
  1107. musb_ep_restart(musb, request);
  1108. }
  1109. done:
  1110. spin_unlock_irqrestore(&musb->lock, flags);
  1111. return status;
  1112. }
  1113. /*
  1114. * Sets the halt feature with the clear requests ignored
  1115. */
  1116. static int musb_gadget_set_wedge(struct usb_ep *ep)
  1117. {
  1118. struct musb_ep *musb_ep = to_musb_ep(ep);
  1119. if (!ep)
  1120. return -EINVAL;
  1121. musb_ep->wedged = 1;
  1122. return usb_ep_set_halt(ep);
  1123. }
  1124. static int musb_gadget_fifo_status(struct usb_ep *ep)
  1125. {
  1126. struct musb_ep *musb_ep = to_musb_ep(ep);
  1127. void __iomem *epio = musb_ep->hw_ep->regs;
  1128. int retval = -EINVAL;
  1129. if (musb_ep->desc && !musb_ep->is_in) {
  1130. struct musb *musb = musb_ep->musb;
  1131. int epnum = musb_ep->current_epnum;
  1132. void __iomem *mbase = musb->mregs;
  1133. unsigned long flags;
  1134. spin_lock_irqsave(&musb->lock, flags);
  1135. musb_ep_select(mbase, epnum);
  1136. /* FIXME return zero unless RXPKTRDY is set */
  1137. retval = musb_readw(epio, MUSB_RXCOUNT);
  1138. spin_unlock_irqrestore(&musb->lock, flags);
  1139. }
  1140. return retval;
  1141. }
  1142. static void musb_gadget_fifo_flush(struct usb_ep *ep)
  1143. {
  1144. struct musb_ep *musb_ep = to_musb_ep(ep);
  1145. struct musb *musb = musb_ep->musb;
  1146. u8 epnum = musb_ep->current_epnum;
  1147. void __iomem *epio = musb->endpoints[epnum].regs;
  1148. void __iomem *mbase;
  1149. unsigned long flags;
  1150. u16 csr, int_txe;
  1151. mbase = musb->mregs;
  1152. spin_lock_irqsave(&musb->lock, flags);
  1153. musb_ep_select(mbase, (u8) epnum);
  1154. /* disable interrupts */
  1155. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  1156. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  1157. if (musb_ep->is_in) {
  1158. csr = musb_readw(epio, MUSB_TXCSR);
  1159. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1160. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
  1161. musb_writew(epio, MUSB_TXCSR, csr);
  1162. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  1163. musb_writew(epio, MUSB_TXCSR, csr);
  1164. }
  1165. } else {
  1166. csr = musb_readw(epio, MUSB_RXCSR);
  1167. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
  1168. musb_writew(epio, MUSB_RXCSR, csr);
  1169. musb_writew(epio, MUSB_RXCSR, csr);
  1170. }
  1171. /* re-enable interrupt */
  1172. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  1173. spin_unlock_irqrestore(&musb->lock, flags);
  1174. }
  1175. static const struct usb_ep_ops musb_ep_ops = {
  1176. .enable = musb_gadget_enable,
  1177. .disable = musb_gadget_disable,
  1178. .alloc_request = musb_alloc_request,
  1179. .free_request = musb_free_request,
  1180. .queue = musb_gadget_queue,
  1181. .dequeue = musb_gadget_dequeue,
  1182. .set_halt = musb_gadget_set_halt,
  1183. .set_wedge = musb_gadget_set_wedge,
  1184. .fifo_status = musb_gadget_fifo_status,
  1185. .fifo_flush = musb_gadget_fifo_flush
  1186. };
  1187. /* ----------------------------------------------------------------------- */
  1188. static int musb_gadget_get_frame(struct usb_gadget *gadget)
  1189. {
  1190. struct musb *musb = gadget_to_musb(gadget);
  1191. return (int)musb_readw(musb->mregs, MUSB_FRAME);
  1192. }
  1193. static int musb_gadget_wakeup(struct usb_gadget *gadget)
  1194. {
  1195. struct musb *musb = gadget_to_musb(gadget);
  1196. void __iomem *mregs = musb->mregs;
  1197. unsigned long flags;
  1198. int status = -EINVAL;
  1199. u8 power, devctl;
  1200. int retries;
  1201. spin_lock_irqsave(&musb->lock, flags);
  1202. switch (musb->xceiv->state) {
  1203. case OTG_STATE_B_PERIPHERAL:
  1204. /* NOTE: OTG state machine doesn't include B_SUSPENDED;
  1205. * that's part of the standard usb 1.1 state machine, and
  1206. * doesn't affect OTG transitions.
  1207. */
  1208. if (musb->may_wakeup && musb->is_suspended)
  1209. break;
  1210. goto done;
  1211. case OTG_STATE_B_IDLE:
  1212. /* Start SRP ... OTG not required. */
  1213. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1214. DBG(2, "Sending SRP: devctl: %02x\n", devctl);
  1215. devctl |= MUSB_DEVCTL_SESSION;
  1216. musb_writeb(mregs, MUSB_DEVCTL, devctl);
  1217. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1218. retries = 100;
  1219. while (!(devctl & MUSB_DEVCTL_SESSION)) {
  1220. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1221. if (retries-- < 1)
  1222. break;
  1223. }
  1224. retries = 10000;
  1225. while (devctl & MUSB_DEVCTL_SESSION) {
  1226. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1227. if (retries-- < 1)
  1228. break;
  1229. }
  1230. /* Block idling for at least 1s */
  1231. musb_platform_try_idle(musb,
  1232. jiffies + msecs_to_jiffies(1 * HZ));
  1233. status = 0;
  1234. goto done;
  1235. default:
  1236. DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
  1237. goto done;
  1238. }
  1239. status = 0;
  1240. power = musb_readb(mregs, MUSB_POWER);
  1241. power |= MUSB_POWER_RESUME;
  1242. musb_writeb(mregs, MUSB_POWER, power);
  1243. DBG(2, "issue wakeup\n");
  1244. /* FIXME do this next chunk in a timer callback, no udelay */
  1245. mdelay(2);
  1246. power = musb_readb(mregs, MUSB_POWER);
  1247. power &= ~MUSB_POWER_RESUME;
  1248. musb_writeb(mregs, MUSB_POWER, power);
  1249. done:
  1250. spin_unlock_irqrestore(&musb->lock, flags);
  1251. return status;
  1252. }
  1253. static int
  1254. musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
  1255. {
  1256. struct musb *musb = gadget_to_musb(gadget);
  1257. musb->is_self_powered = !!is_selfpowered;
  1258. return 0;
  1259. }
  1260. static void musb_pullup(struct musb *musb, int is_on)
  1261. {
  1262. u8 power;
  1263. power = musb_readb(musb->mregs, MUSB_POWER);
  1264. if (is_on)
  1265. power |= MUSB_POWER_SOFTCONN;
  1266. else
  1267. power &= ~MUSB_POWER_SOFTCONN;
  1268. /* FIXME if on, HdrcStart; if off, HdrcStop */
  1269. DBG(3, "gadget %s D+ pullup %s\n",
  1270. musb->gadget_driver->function, is_on ? "on" : "off");
  1271. musb_writeb(musb->mregs, MUSB_POWER, power);
  1272. }
  1273. #if 0
  1274. static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
  1275. {
  1276. DBG(2, "<= %s =>\n", __func__);
  1277. /*
  1278. * FIXME iff driver's softconnect flag is set (as it is during probe,
  1279. * though that can clear it), just musb_pullup().
  1280. */
  1281. return -EINVAL;
  1282. }
  1283. #endif
  1284. static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1285. {
  1286. struct musb *musb = gadget_to_musb(gadget);
  1287. if (!musb->xceiv->set_power)
  1288. return -EOPNOTSUPP;
  1289. return otg_set_power(musb->xceiv, mA);
  1290. }
  1291. static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1292. {
  1293. struct musb *musb = gadget_to_musb(gadget);
  1294. unsigned long flags;
  1295. is_on = !!is_on;
  1296. /* NOTE: this assumes we are sensing vbus; we'd rather
  1297. * not pullup unless the B-session is active.
  1298. */
  1299. spin_lock_irqsave(&musb->lock, flags);
  1300. if (is_on != musb->softconnect) {
  1301. musb->softconnect = is_on;
  1302. musb_pullup(musb, is_on);
  1303. }
  1304. spin_unlock_irqrestore(&musb->lock, flags);
  1305. return 0;
  1306. }
  1307. static const struct usb_gadget_ops musb_gadget_operations = {
  1308. .get_frame = musb_gadget_get_frame,
  1309. .wakeup = musb_gadget_wakeup,
  1310. .set_selfpowered = musb_gadget_set_self_powered,
  1311. /* .vbus_session = musb_gadget_vbus_session, */
  1312. .vbus_draw = musb_gadget_vbus_draw,
  1313. .pullup = musb_gadget_pullup,
  1314. };
  1315. /* ----------------------------------------------------------------------- */
  1316. /* Registration */
  1317. /* Only this registration code "knows" the rule (from USB standards)
  1318. * about there being only one external upstream port. It assumes
  1319. * all peripheral ports are external...
  1320. */
  1321. static struct musb *the_gadget;
  1322. static void musb_gadget_release(struct device *dev)
  1323. {
  1324. /* kref_put(WHAT) */
  1325. dev_dbg(dev, "%s\n", __func__);
  1326. }
  1327. static void __init
  1328. init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
  1329. {
  1330. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1331. memset(ep, 0, sizeof *ep);
  1332. ep->current_epnum = epnum;
  1333. ep->musb = musb;
  1334. ep->hw_ep = hw_ep;
  1335. ep->is_in = is_in;
  1336. INIT_LIST_HEAD(&ep->req_list);
  1337. sprintf(ep->name, "ep%d%s", epnum,
  1338. (!epnum || hw_ep->is_shared_fifo) ? "" : (
  1339. is_in ? "in" : "out"));
  1340. ep->end_point.name = ep->name;
  1341. INIT_LIST_HEAD(&ep->end_point.ep_list);
  1342. if (!epnum) {
  1343. ep->end_point.maxpacket = 64;
  1344. ep->end_point.ops = &musb_g_ep0_ops;
  1345. musb->g.ep0 = &ep->end_point;
  1346. } else {
  1347. if (is_in)
  1348. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1349. else
  1350. ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
  1351. ep->end_point.ops = &musb_ep_ops;
  1352. list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
  1353. }
  1354. }
  1355. /*
  1356. * Initialize the endpoints exposed to peripheral drivers, with backlinks
  1357. * to the rest of the driver state.
  1358. */
  1359. static inline void __init musb_g_init_endpoints(struct musb *musb)
  1360. {
  1361. u8 epnum;
  1362. struct musb_hw_ep *hw_ep;
  1363. unsigned count = 0;
  1364. /* intialize endpoint list just once */
  1365. INIT_LIST_HEAD(&(musb->g.ep_list));
  1366. for (epnum = 0, hw_ep = musb->endpoints;
  1367. epnum < musb->nr_endpoints;
  1368. epnum++, hw_ep++) {
  1369. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1370. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
  1371. count++;
  1372. } else {
  1373. if (hw_ep->max_packet_sz_tx) {
  1374. init_peripheral_ep(musb, &hw_ep->ep_in,
  1375. epnum, 1);
  1376. count++;
  1377. }
  1378. if (hw_ep->max_packet_sz_rx) {
  1379. init_peripheral_ep(musb, &hw_ep->ep_out,
  1380. epnum, 0);
  1381. count++;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. /* called once during driver setup to initialize and link into
  1387. * the driver model; memory is zeroed.
  1388. */
  1389. int __init musb_gadget_setup(struct musb *musb)
  1390. {
  1391. int status;
  1392. /* REVISIT minor race: if (erroneously) setting up two
  1393. * musb peripherals at the same time, only the bus lock
  1394. * is probably held.
  1395. */
  1396. if (the_gadget)
  1397. return -EBUSY;
  1398. the_gadget = musb;
  1399. musb->g.ops = &musb_gadget_operations;
  1400. musb->g.is_dualspeed = 1;
  1401. musb->g.speed = USB_SPEED_UNKNOWN;
  1402. /* this "gadget" abstracts/virtualizes the controller */
  1403. dev_set_name(&musb->g.dev, "gadget");
  1404. musb->g.dev.parent = musb->controller;
  1405. musb->g.dev.dma_mask = musb->controller->dma_mask;
  1406. musb->g.dev.release = musb_gadget_release;
  1407. musb->g.name = musb_driver_name;
  1408. if (is_otg_enabled(musb))
  1409. musb->g.is_otg = 1;
  1410. musb_g_init_endpoints(musb);
  1411. musb->is_active = 0;
  1412. musb_platform_try_idle(musb, 0);
  1413. status = device_register(&musb->g.dev);
  1414. if (status != 0)
  1415. the_gadget = NULL;
  1416. return status;
  1417. }
  1418. void musb_gadget_cleanup(struct musb *musb)
  1419. {
  1420. if (musb != the_gadget)
  1421. return;
  1422. device_unregister(&musb->g.dev);
  1423. the_gadget = NULL;
  1424. }
  1425. /*
  1426. * Register the gadget driver. Used by gadget drivers when
  1427. * registering themselves with the controller.
  1428. *
  1429. * -EINVAL something went wrong (not driver)
  1430. * -EBUSY another gadget is already using the controller
  1431. * -ENOMEM no memeory to perform the operation
  1432. *
  1433. * @param driver the gadget driver
  1434. * @return <0 if error, 0 if everything is fine
  1435. */
  1436. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1437. {
  1438. int retval;
  1439. unsigned long flags;
  1440. struct musb *musb = the_gadget;
  1441. if (!driver
  1442. || driver->speed != USB_SPEED_HIGH
  1443. || !driver->bind
  1444. || !driver->setup)
  1445. return -EINVAL;
  1446. /* driver must be initialized to support peripheral mode */
  1447. if (!musb || !(musb->board_mode == MUSB_OTG
  1448. || musb->board_mode != MUSB_OTG)) {
  1449. DBG(1, "%s, no dev??\n", __func__);
  1450. return -ENODEV;
  1451. }
  1452. DBG(3, "registering driver %s\n", driver->function);
  1453. spin_lock_irqsave(&musb->lock, flags);
  1454. if (musb->gadget_driver) {
  1455. DBG(1, "%s is already bound to %s\n",
  1456. musb_driver_name,
  1457. musb->gadget_driver->driver.name);
  1458. retval = -EBUSY;
  1459. } else {
  1460. musb->gadget_driver = driver;
  1461. musb->g.dev.driver = &driver->driver;
  1462. driver->driver.bus = NULL;
  1463. musb->softconnect = 1;
  1464. retval = 0;
  1465. }
  1466. spin_unlock_irqrestore(&musb->lock, flags);
  1467. if (retval == 0) {
  1468. retval = driver->bind(&musb->g);
  1469. if (retval != 0) {
  1470. DBG(3, "bind to driver %s failed --> %d\n",
  1471. driver->driver.name, retval);
  1472. musb->gadget_driver = NULL;
  1473. musb->g.dev.driver = NULL;
  1474. }
  1475. spin_lock_irqsave(&musb->lock, flags);
  1476. otg_set_peripheral(musb->xceiv, &musb->g);
  1477. musb->xceiv->state = OTG_STATE_B_IDLE;
  1478. musb->is_active = 1;
  1479. /* FIXME this ignores the softconnect flag. Drivers are
  1480. * allowed hold the peripheral inactive until for example
  1481. * userspace hooks up printer hardware or DSP codecs, so
  1482. * hosts only see fully functional devices.
  1483. */
  1484. if (!is_otg_enabled(musb))
  1485. musb_start(musb);
  1486. otg_set_peripheral(musb->xceiv, &musb->g);
  1487. spin_unlock_irqrestore(&musb->lock, flags);
  1488. if (is_otg_enabled(musb)) {
  1489. DBG(3, "OTG startup...\n");
  1490. /* REVISIT: funcall to other code, which also
  1491. * handles power budgeting ... this way also
  1492. * ensures HdrcStart is indirectly called.
  1493. */
  1494. retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
  1495. if (retval < 0) {
  1496. DBG(1, "add_hcd failed, %d\n", retval);
  1497. spin_lock_irqsave(&musb->lock, flags);
  1498. otg_set_peripheral(musb->xceiv, NULL);
  1499. musb->gadget_driver = NULL;
  1500. musb->g.dev.driver = NULL;
  1501. spin_unlock_irqrestore(&musb->lock, flags);
  1502. }
  1503. }
  1504. }
  1505. return retval;
  1506. }
  1507. EXPORT_SYMBOL(usb_gadget_register_driver);
  1508. static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
  1509. {
  1510. int i;
  1511. struct musb_hw_ep *hw_ep;
  1512. /* don't disconnect if it's not connected */
  1513. if (musb->g.speed == USB_SPEED_UNKNOWN)
  1514. driver = NULL;
  1515. else
  1516. musb->g.speed = USB_SPEED_UNKNOWN;
  1517. /* deactivate the hardware */
  1518. if (musb->softconnect) {
  1519. musb->softconnect = 0;
  1520. musb_pullup(musb, 0);
  1521. }
  1522. musb_stop(musb);
  1523. /* killing any outstanding requests will quiesce the driver;
  1524. * then report disconnect
  1525. */
  1526. if (driver) {
  1527. for (i = 0, hw_ep = musb->endpoints;
  1528. i < musb->nr_endpoints;
  1529. i++, hw_ep++) {
  1530. musb_ep_select(musb->mregs, i);
  1531. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1532. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1533. } else {
  1534. if (hw_ep->max_packet_sz_tx)
  1535. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1536. if (hw_ep->max_packet_sz_rx)
  1537. nuke(&hw_ep->ep_out, -ESHUTDOWN);
  1538. }
  1539. }
  1540. spin_unlock(&musb->lock);
  1541. driver->disconnect(&musb->g);
  1542. spin_lock(&musb->lock);
  1543. }
  1544. }
  1545. /*
  1546. * Unregister the gadget driver. Used by gadget drivers when
  1547. * unregistering themselves from the controller.
  1548. *
  1549. * @param driver the gadget driver to unregister
  1550. */
  1551. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1552. {
  1553. unsigned long flags;
  1554. int retval = 0;
  1555. struct musb *musb = the_gadget;
  1556. if (!driver || !driver->unbind || !musb)
  1557. return -EINVAL;
  1558. /* REVISIT always use otg_set_peripheral() here too;
  1559. * this needs to shut down the OTG engine.
  1560. */
  1561. spin_lock_irqsave(&musb->lock, flags);
  1562. #ifdef CONFIG_USB_MUSB_OTG
  1563. musb_hnp_stop(musb);
  1564. #endif
  1565. if (musb->gadget_driver == driver) {
  1566. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1567. musb->xceiv->state = OTG_STATE_UNDEFINED;
  1568. stop_activity(musb, driver);
  1569. otg_set_peripheral(musb->xceiv, NULL);
  1570. DBG(3, "unregistering driver %s\n", driver->function);
  1571. spin_unlock_irqrestore(&musb->lock, flags);
  1572. driver->unbind(&musb->g);
  1573. spin_lock_irqsave(&musb->lock, flags);
  1574. musb->gadget_driver = NULL;
  1575. musb->g.dev.driver = NULL;
  1576. musb->is_active = 0;
  1577. musb_platform_try_idle(musb, 0);
  1578. } else
  1579. retval = -EINVAL;
  1580. spin_unlock_irqrestore(&musb->lock, flags);
  1581. if (is_otg_enabled(musb) && retval == 0) {
  1582. usb_remove_hcd(musb_to_hcd(musb));
  1583. /* FIXME we need to be able to register another
  1584. * gadget driver here and have everything work;
  1585. * that currently misbehaves.
  1586. */
  1587. }
  1588. return retval;
  1589. }
  1590. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1591. /* ----------------------------------------------------------------------- */
  1592. /* lifecycle operations called through plat_uds.c */
  1593. void musb_g_resume(struct musb *musb)
  1594. {
  1595. musb->is_suspended = 0;
  1596. switch (musb->xceiv->state) {
  1597. case OTG_STATE_B_IDLE:
  1598. break;
  1599. case OTG_STATE_B_WAIT_ACON:
  1600. case OTG_STATE_B_PERIPHERAL:
  1601. musb->is_active = 1;
  1602. if (musb->gadget_driver && musb->gadget_driver->resume) {
  1603. spin_unlock(&musb->lock);
  1604. musb->gadget_driver->resume(&musb->g);
  1605. spin_lock(&musb->lock);
  1606. }
  1607. break;
  1608. default:
  1609. WARNING("unhandled RESUME transition (%s)\n",
  1610. otg_state_string(musb));
  1611. }
  1612. }
  1613. /* called when SOF packets stop for 3+ msec */
  1614. void musb_g_suspend(struct musb *musb)
  1615. {
  1616. u8 devctl;
  1617. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1618. DBG(3, "devctl %02x\n", devctl);
  1619. switch (musb->xceiv->state) {
  1620. case OTG_STATE_B_IDLE:
  1621. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  1622. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1623. break;
  1624. case OTG_STATE_B_PERIPHERAL:
  1625. musb->is_suspended = 1;
  1626. if (musb->gadget_driver && musb->gadget_driver->suspend) {
  1627. spin_unlock(&musb->lock);
  1628. musb->gadget_driver->suspend(&musb->g);
  1629. spin_lock(&musb->lock);
  1630. }
  1631. break;
  1632. default:
  1633. /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
  1634. * A_PERIPHERAL may need care too
  1635. */
  1636. WARNING("unhandled SUSPEND transition (%s)\n",
  1637. otg_state_string(musb));
  1638. }
  1639. }
  1640. /* Called during SRP */
  1641. void musb_g_wakeup(struct musb *musb)
  1642. {
  1643. musb_gadget_wakeup(&musb->g);
  1644. }
  1645. /* called when VBUS drops below session threshold, and in other cases */
  1646. void musb_g_disconnect(struct musb *musb)
  1647. {
  1648. void __iomem *mregs = musb->mregs;
  1649. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  1650. DBG(3, "devctl %02x\n", devctl);
  1651. /* clear HR */
  1652. musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
  1653. /* don't draw vbus until new b-default session */
  1654. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1655. musb->g.speed = USB_SPEED_UNKNOWN;
  1656. if (musb->gadget_driver && musb->gadget_driver->disconnect) {
  1657. spin_unlock(&musb->lock);
  1658. musb->gadget_driver->disconnect(&musb->g);
  1659. spin_lock(&musb->lock);
  1660. }
  1661. switch (musb->xceiv->state) {
  1662. default:
  1663. #ifdef CONFIG_USB_MUSB_OTG
  1664. DBG(2, "Unhandled disconnect %s, setting a_idle\n",
  1665. otg_state_string(musb));
  1666. musb->xceiv->state = OTG_STATE_A_IDLE;
  1667. MUSB_HST_MODE(musb);
  1668. break;
  1669. case OTG_STATE_A_PERIPHERAL:
  1670. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  1671. MUSB_HST_MODE(musb);
  1672. break;
  1673. case OTG_STATE_B_WAIT_ACON:
  1674. case OTG_STATE_B_HOST:
  1675. #endif
  1676. case OTG_STATE_B_PERIPHERAL:
  1677. case OTG_STATE_B_IDLE:
  1678. musb->xceiv->state = OTG_STATE_B_IDLE;
  1679. break;
  1680. case OTG_STATE_B_SRP_INIT:
  1681. break;
  1682. }
  1683. musb->is_active = 0;
  1684. }
  1685. void musb_g_reset(struct musb *musb)
  1686. __releases(musb->lock)
  1687. __acquires(musb->lock)
  1688. {
  1689. void __iomem *mbase = musb->mregs;
  1690. u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
  1691. u8 power;
  1692. DBG(3, "<== %s addr=%x driver '%s'\n",
  1693. (devctl & MUSB_DEVCTL_BDEVICE)
  1694. ? "B-Device" : "A-Device",
  1695. musb_readb(mbase, MUSB_FADDR),
  1696. musb->gadget_driver
  1697. ? musb->gadget_driver->driver.name
  1698. : NULL
  1699. );
  1700. /* report disconnect, if we didn't already (flushing EP state) */
  1701. if (musb->g.speed != USB_SPEED_UNKNOWN)
  1702. musb_g_disconnect(musb);
  1703. /* clear HR */
  1704. else if (devctl & MUSB_DEVCTL_HR)
  1705. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  1706. /* what speed did we negotiate? */
  1707. power = musb_readb(mbase, MUSB_POWER);
  1708. musb->g.speed = (power & MUSB_POWER_HSMODE)
  1709. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  1710. /* start in USB_STATE_DEFAULT */
  1711. musb->is_active = 1;
  1712. musb->is_suspended = 0;
  1713. MUSB_DEV_MODE(musb);
  1714. musb->address = 0;
  1715. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  1716. musb->may_wakeup = 0;
  1717. musb->g.b_hnp_enable = 0;
  1718. musb->g.a_alt_hnp_support = 0;
  1719. musb->g.a_hnp_support = 0;
  1720. /* Normal reset, as B-Device;
  1721. * or else after HNP, as A-Device
  1722. */
  1723. if (devctl & MUSB_DEVCTL_BDEVICE) {
  1724. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1725. musb->g.is_a_peripheral = 0;
  1726. } else if (is_otg_enabled(musb)) {
  1727. musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
  1728. musb->g.is_a_peripheral = 1;
  1729. } else
  1730. WARN_ON(1);
  1731. /* start with default limits on VBUS power draw */
  1732. (void) musb_gadget_vbus_draw(&musb->g,
  1733. is_otg_enabled(musb) ? 8 : 100);
  1734. }