musb_gadget.c 54 KB

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