musb_gadget.c 59 KB

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