musb_gadget.c 53 KB

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