musb_gadget.c 55 KB

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