musb_gadget.c 54 KB

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