musb_gadget.c 53 KB

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