musb_gadget.c 53 KB

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