musb_gadget.c 53 KB

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