musb_gadget.c 56 KB

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