musb_gadget.c 56 KB

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