musb_gadget.c 56 KB

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