musb_host.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  1. /*
  2. * MUSB OTG driver host 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) 2008-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/module.h>
  36. #include <linux/kernel.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/slab.h>
  40. #include <linux/errno.h>
  41. #include <linux/list.h>
  42. #include <linux/dma-mapping.h>
  43. #include "musb_core.h"
  44. #include "musb_host.h"
  45. /* MUSB HOST status 22-mar-2006
  46. *
  47. * - There's still lots of partial code duplication for fault paths, so
  48. * they aren't handled as consistently as they need to be.
  49. *
  50. * - PIO mostly behaved when last tested.
  51. * + including ep0, with all usbtest cases 9, 10
  52. * + usbtest 14 (ep0out) doesn't seem to run at all
  53. * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
  54. * configurations, but otherwise double buffering passes basic tests.
  55. * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
  56. *
  57. * - DMA (CPPI) ... partially behaves, not currently recommended
  58. * + about 1/15 the speed of typical EHCI implementations (PCI)
  59. * + RX, all too often reqpkt seems to misbehave after tx
  60. * + TX, no known issues (other than evident silicon issue)
  61. *
  62. * - DMA (Mentor/OMAP) ...has at least toggle update problems
  63. *
  64. * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
  65. * starvation ... nothing yet for TX, interrupt, or bulk.
  66. *
  67. * - Not tested with HNP, but some SRP paths seem to behave.
  68. *
  69. * NOTE 24-August-2006:
  70. *
  71. * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
  72. * extra endpoint for periodic use enabling hub + keybd + mouse. That
  73. * mostly works, except that with "usbnet" it's easy to trigger cases
  74. * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
  75. * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
  76. * although ARP RX wins. (That test was done with a full speed link.)
  77. */
  78. /*
  79. * NOTE on endpoint usage:
  80. *
  81. * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
  82. * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
  83. * (Yes, bulk _could_ use more of the endpoints than that, and would even
  84. * benefit from it.)
  85. *
  86. * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
  87. * So far that scheduling is both dumb and optimistic: the endpoint will be
  88. * "claimed" until its software queue is no longer refilled. No multiplexing
  89. * of transfers between endpoints, or anything clever.
  90. */
  91. struct musb *hcd_to_musb(struct usb_hcd *hcd)
  92. {
  93. return *(struct musb **) hcd->hcd_priv;
  94. }
  95. static void musb_ep_program(struct musb *musb, u8 epnum,
  96. struct urb *urb, int is_out,
  97. u8 *buf, u32 offset, u32 len);
  98. /*
  99. * Clear TX fifo. Needed to avoid BABBLE errors.
  100. */
  101. static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
  102. {
  103. struct musb *musb = ep->musb;
  104. void __iomem *epio = ep->regs;
  105. u16 csr;
  106. u16 lastcsr = 0;
  107. int retries = 1000;
  108. csr = musb_readw(epio, MUSB_TXCSR);
  109. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  110. if (csr != lastcsr)
  111. dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
  112. lastcsr = csr;
  113. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
  114. musb_writew(epio, MUSB_TXCSR, csr);
  115. csr = musb_readw(epio, MUSB_TXCSR);
  116. if (WARN(retries-- < 1,
  117. "Could not flush host TX%d fifo: csr: %04x\n",
  118. ep->epnum, csr))
  119. return;
  120. mdelay(1);
  121. }
  122. }
  123. static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
  124. {
  125. void __iomem *epio = ep->regs;
  126. u16 csr;
  127. int retries = 5;
  128. /* scrub any data left in the fifo */
  129. do {
  130. csr = musb_readw(epio, MUSB_TXCSR);
  131. if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
  132. break;
  133. musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
  134. csr = musb_readw(epio, MUSB_TXCSR);
  135. udelay(10);
  136. } while (--retries);
  137. WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
  138. ep->epnum, csr);
  139. /* and reset for the next transfer */
  140. musb_writew(epio, MUSB_TXCSR, 0);
  141. }
  142. /*
  143. * Start transmit. Caller is responsible for locking shared resources.
  144. * musb must be locked.
  145. */
  146. static inline void musb_h_tx_start(struct musb_hw_ep *ep)
  147. {
  148. u16 txcsr;
  149. /* NOTE: no locks here; caller should lock and select EP */
  150. if (ep->epnum) {
  151. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  152. txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
  153. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  154. } else {
  155. txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
  156. musb_writew(ep->regs, MUSB_CSR0, txcsr);
  157. }
  158. }
  159. static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
  160. {
  161. u16 txcsr;
  162. /* NOTE: no locks here; caller should lock and select EP */
  163. txcsr = musb_readw(ep->regs, MUSB_TXCSR);
  164. txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
  165. if (is_cppi_enabled(ep->musb))
  166. txcsr |= MUSB_TXCSR_DMAMODE;
  167. musb_writew(ep->regs, MUSB_TXCSR, txcsr);
  168. }
  169. static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
  170. {
  171. if (is_in != 0 || ep->is_shared_fifo)
  172. ep->in_qh = qh;
  173. if (is_in == 0 || ep->is_shared_fifo)
  174. ep->out_qh = qh;
  175. }
  176. static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
  177. {
  178. return is_in ? ep->in_qh : ep->out_qh;
  179. }
  180. /*
  181. * Start the URB at the front of an endpoint's queue
  182. * end must be claimed from the caller.
  183. *
  184. * Context: controller locked, irqs blocked
  185. */
  186. static void
  187. musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
  188. {
  189. u16 frame;
  190. u32 len;
  191. void __iomem *mbase = musb->mregs;
  192. struct urb *urb = next_urb(qh);
  193. void *buf = urb->transfer_buffer;
  194. u32 offset = 0;
  195. struct musb_hw_ep *hw_ep = qh->hw_ep;
  196. unsigned pipe = urb->pipe;
  197. u8 address = usb_pipedevice(pipe);
  198. int epnum = hw_ep->epnum;
  199. /* initialize software qh state */
  200. qh->offset = 0;
  201. qh->segsize = 0;
  202. /* gather right source of data */
  203. switch (qh->type) {
  204. case USB_ENDPOINT_XFER_CONTROL:
  205. /* control transfers always start with SETUP */
  206. is_in = 0;
  207. musb->ep0_stage = MUSB_EP0_START;
  208. buf = urb->setup_packet;
  209. len = 8;
  210. break;
  211. case USB_ENDPOINT_XFER_ISOC:
  212. qh->iso_idx = 0;
  213. qh->frame = 0;
  214. offset = urb->iso_frame_desc[0].offset;
  215. len = urb->iso_frame_desc[0].length;
  216. break;
  217. default: /* bulk, interrupt */
  218. /* actual_length may be nonzero on retry paths */
  219. buf = urb->transfer_buffer + urb->actual_length;
  220. len = urb->transfer_buffer_length - urb->actual_length;
  221. }
  222. dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
  223. qh, urb, address, qh->epnum,
  224. is_in ? "in" : "out",
  225. ({char *s; switch (qh->type) {
  226. case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
  227. case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
  228. case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
  229. default: s = "-intr"; break;
  230. } s; }),
  231. epnum, buf + offset, len);
  232. /* Configure endpoint */
  233. musb_ep_set_qh(hw_ep, is_in, qh);
  234. musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
  235. /* transmit may have more work: start it when it is time */
  236. if (is_in)
  237. return;
  238. /* determine if the time is right for a periodic transfer */
  239. switch (qh->type) {
  240. case USB_ENDPOINT_XFER_ISOC:
  241. case USB_ENDPOINT_XFER_INT:
  242. dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
  243. frame = musb_readw(mbase, MUSB_FRAME);
  244. /* FIXME this doesn't implement that scheduling policy ...
  245. * or handle framecounter wrapping
  246. */
  247. if (1) { /* Always assume URB_ISO_ASAP */
  248. /* REVISIT the SOF irq handler shouldn't duplicate
  249. * this code; and we don't init urb->start_frame...
  250. */
  251. qh->frame = 0;
  252. goto start;
  253. } else {
  254. qh->frame = urb->start_frame;
  255. /* enable SOF interrupt so we can count down */
  256. dev_dbg(musb->controller, "SOF for %d\n", epnum);
  257. #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
  258. musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
  259. #endif
  260. }
  261. break;
  262. default:
  263. start:
  264. dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
  265. hw_ep->tx_channel ? "dma" : "pio");
  266. if (!hw_ep->tx_channel)
  267. musb_h_tx_start(hw_ep);
  268. else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  269. musb_h_tx_dma_start(hw_ep);
  270. }
  271. }
  272. /* Context: caller owns controller lock, IRQs are blocked */
  273. static void musb_giveback(struct musb *musb, struct urb *urb, int status)
  274. __releases(musb->lock)
  275. __acquires(musb->lock)
  276. {
  277. dev_dbg(musb->controller,
  278. "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
  279. urb, urb->complete, status,
  280. usb_pipedevice(urb->pipe),
  281. usb_pipeendpoint(urb->pipe),
  282. usb_pipein(urb->pipe) ? "in" : "out",
  283. urb->actual_length, urb->transfer_buffer_length
  284. );
  285. usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
  286. spin_unlock(&musb->lock);
  287. usb_hcd_giveback_urb(musb->hcd, urb, status);
  288. spin_lock(&musb->lock);
  289. }
  290. /* For bulk/interrupt endpoints only */
  291. static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
  292. struct urb *urb)
  293. {
  294. void __iomem *epio = qh->hw_ep->regs;
  295. u16 csr;
  296. /*
  297. * FIXME: the current Mentor DMA code seems to have
  298. * problems getting toggle correct.
  299. */
  300. if (is_in)
  301. csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
  302. else
  303. csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
  304. usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
  305. }
  306. /*
  307. * Advance this hardware endpoint's queue, completing the specified URB and
  308. * advancing to either the next URB queued to that qh, or else invalidating
  309. * that qh and advancing to the next qh scheduled after the current one.
  310. *
  311. * Context: caller owns controller lock, IRQs are blocked
  312. */
  313. static void musb_advance_schedule(struct musb *musb, struct urb *urb,
  314. struct musb_hw_ep *hw_ep, int is_in)
  315. {
  316. struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
  317. struct musb_hw_ep *ep = qh->hw_ep;
  318. int ready = qh->is_ready;
  319. int status;
  320. status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
  321. /* save toggle eagerly, for paranoia */
  322. switch (qh->type) {
  323. case USB_ENDPOINT_XFER_BULK:
  324. case USB_ENDPOINT_XFER_INT:
  325. musb_save_toggle(qh, is_in, urb);
  326. break;
  327. case USB_ENDPOINT_XFER_ISOC:
  328. if (status == 0 && urb->error_count)
  329. status = -EXDEV;
  330. break;
  331. }
  332. qh->is_ready = 0;
  333. musb_giveback(musb, urb, status);
  334. qh->is_ready = ready;
  335. /* reclaim resources (and bandwidth) ASAP; deschedule it, and
  336. * invalidate qh as soon as list_empty(&hep->urb_list)
  337. */
  338. if (list_empty(&qh->hep->urb_list)) {
  339. struct list_head *head;
  340. struct dma_controller *dma = musb->dma_controller;
  341. if (is_in) {
  342. ep->rx_reinit = 1;
  343. if (ep->rx_channel) {
  344. dma->channel_release(ep->rx_channel);
  345. ep->rx_channel = NULL;
  346. }
  347. } else {
  348. ep->tx_reinit = 1;
  349. if (ep->tx_channel) {
  350. dma->channel_release(ep->tx_channel);
  351. ep->tx_channel = NULL;
  352. }
  353. }
  354. /* Clobber old pointers to this qh */
  355. musb_ep_set_qh(ep, is_in, NULL);
  356. qh->hep->hcpriv = NULL;
  357. switch (qh->type) {
  358. case USB_ENDPOINT_XFER_CONTROL:
  359. case USB_ENDPOINT_XFER_BULK:
  360. /* fifo policy for these lists, except that NAKing
  361. * should rotate a qh to the end (for fairness).
  362. */
  363. if (qh->mux == 1) {
  364. head = qh->ring.prev;
  365. list_del(&qh->ring);
  366. kfree(qh);
  367. qh = first_qh(head);
  368. break;
  369. }
  370. case USB_ENDPOINT_XFER_ISOC:
  371. case USB_ENDPOINT_XFER_INT:
  372. /* this is where periodic bandwidth should be
  373. * de-allocated if it's tracked and allocated;
  374. * and where we'd update the schedule tree...
  375. */
  376. kfree(qh);
  377. qh = NULL;
  378. break;
  379. }
  380. }
  381. if (qh != NULL && qh->is_ready) {
  382. dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
  383. hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
  384. musb_start_urb(musb, is_in, qh);
  385. }
  386. }
  387. static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
  388. {
  389. /* we don't want fifo to fill itself again;
  390. * ignore dma (various models),
  391. * leave toggle alone (may not have been saved yet)
  392. */
  393. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
  394. csr &= ~(MUSB_RXCSR_H_REQPKT
  395. | MUSB_RXCSR_H_AUTOREQ
  396. | MUSB_RXCSR_AUTOCLEAR);
  397. /* write 2x to allow double buffering */
  398. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  399. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  400. /* flush writebuffer */
  401. return musb_readw(hw_ep->regs, MUSB_RXCSR);
  402. }
  403. /*
  404. * PIO RX for a packet (or part of it).
  405. */
  406. static bool
  407. musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
  408. {
  409. u16 rx_count;
  410. u8 *buf;
  411. u16 csr;
  412. bool done = false;
  413. u32 length;
  414. int do_flush = 0;
  415. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  416. void __iomem *epio = hw_ep->regs;
  417. struct musb_qh *qh = hw_ep->in_qh;
  418. int pipe = urb->pipe;
  419. void *buffer = urb->transfer_buffer;
  420. /* musb_ep_select(mbase, epnum); */
  421. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  422. dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
  423. urb->transfer_buffer, qh->offset,
  424. urb->transfer_buffer_length);
  425. /* unload FIFO */
  426. if (usb_pipeisoc(pipe)) {
  427. int status = 0;
  428. struct usb_iso_packet_descriptor *d;
  429. if (iso_err) {
  430. status = -EILSEQ;
  431. urb->error_count++;
  432. }
  433. d = urb->iso_frame_desc + qh->iso_idx;
  434. buf = buffer + d->offset;
  435. length = d->length;
  436. if (rx_count > length) {
  437. if (status == 0) {
  438. status = -EOVERFLOW;
  439. urb->error_count++;
  440. }
  441. dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
  442. do_flush = 1;
  443. } else
  444. length = rx_count;
  445. urb->actual_length += length;
  446. d->actual_length = length;
  447. d->status = status;
  448. /* see if we are done */
  449. done = (++qh->iso_idx >= urb->number_of_packets);
  450. } else {
  451. /* non-isoch */
  452. buf = buffer + qh->offset;
  453. length = urb->transfer_buffer_length - qh->offset;
  454. if (rx_count > length) {
  455. if (urb->status == -EINPROGRESS)
  456. urb->status = -EOVERFLOW;
  457. dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
  458. do_flush = 1;
  459. } else
  460. length = rx_count;
  461. urb->actual_length += length;
  462. qh->offset += length;
  463. /* see if we are done */
  464. done = (urb->actual_length == urb->transfer_buffer_length)
  465. || (rx_count < qh->maxpacket)
  466. || (urb->status != -EINPROGRESS);
  467. if (done
  468. && (urb->status == -EINPROGRESS)
  469. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  470. && (urb->actual_length
  471. < urb->transfer_buffer_length))
  472. urb->status = -EREMOTEIO;
  473. }
  474. musb_read_fifo(hw_ep, length, buf);
  475. csr = musb_readw(epio, MUSB_RXCSR);
  476. csr |= MUSB_RXCSR_H_WZC_BITS;
  477. if (unlikely(do_flush))
  478. musb_h_flush_rxfifo(hw_ep, csr);
  479. else {
  480. /* REVISIT this assumes AUTOCLEAR is never set */
  481. csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
  482. if (!done)
  483. csr |= MUSB_RXCSR_H_REQPKT;
  484. musb_writew(epio, MUSB_RXCSR, csr);
  485. }
  486. return done;
  487. }
  488. /* we don't always need to reinit a given side of an endpoint...
  489. * when we do, use tx/rx reinit routine and then construct a new CSR
  490. * to address data toggle, NYET, and DMA or PIO.
  491. *
  492. * it's possible that driver bugs (especially for DMA) or aborting a
  493. * transfer might have left the endpoint busier than it should be.
  494. * the busy/not-empty tests are basically paranoia.
  495. */
  496. static void
  497. musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
  498. {
  499. struct musb_hw_ep *ep = musb->endpoints + epnum;
  500. u16 csr;
  501. /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
  502. * That always uses tx_reinit since ep0 repurposes TX register
  503. * offsets; the initial SETUP packet is also a kind of OUT.
  504. */
  505. /* if programmed for Tx, put it in RX mode */
  506. if (ep->is_shared_fifo) {
  507. csr = musb_readw(ep->regs, MUSB_TXCSR);
  508. if (csr & MUSB_TXCSR_MODE) {
  509. musb_h_tx_flush_fifo(ep);
  510. csr = musb_readw(ep->regs, MUSB_TXCSR);
  511. musb_writew(ep->regs, MUSB_TXCSR,
  512. csr | MUSB_TXCSR_FRCDATATOG);
  513. }
  514. /*
  515. * Clear the MODE bit (and everything else) to enable Rx.
  516. * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
  517. */
  518. if (csr & MUSB_TXCSR_DMAMODE)
  519. musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
  520. musb_writew(ep->regs, MUSB_TXCSR, 0);
  521. /* scrub all previous state, clearing toggle */
  522. } else {
  523. csr = musb_readw(ep->regs, MUSB_RXCSR);
  524. if (csr & MUSB_RXCSR_RXPKTRDY)
  525. WARNING("rx%d, packet/%d ready?\n", ep->epnum,
  526. musb_readw(ep->regs, MUSB_RXCOUNT));
  527. musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
  528. }
  529. /* target addr and (for multipoint) hub addr/port */
  530. if (musb->is_multipoint) {
  531. musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
  532. musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
  533. musb_write_rxhubport(musb, epnum, qh->h_port_reg);
  534. } else
  535. musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
  536. /* protocol/endpoint, interval/NAKlimit, i/o size */
  537. musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
  538. musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
  539. /* NOTE: bulk combining rewrites high bits of maxpacket */
  540. /* Set RXMAXP with the FIFO size of the endpoint
  541. * to disable double buffer mode.
  542. */
  543. if (musb->double_buffer_not_ok)
  544. musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
  545. else
  546. musb_writew(ep->regs, MUSB_RXMAXP,
  547. qh->maxpacket | ((qh->hb_mult - 1) << 11));
  548. ep->rx_reinit = 0;
  549. }
  550. static int musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
  551. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  552. struct urb *urb, u32 offset,
  553. u32 *length, u8 *mode)
  554. {
  555. struct dma_channel *channel = hw_ep->tx_channel;
  556. void __iomem *epio = hw_ep->regs;
  557. u16 pkt_size = qh->maxpacket;
  558. u16 csr;
  559. if (*length > channel->max_len)
  560. *length = channel->max_len;
  561. csr = musb_readw(epio, MUSB_TXCSR);
  562. if (*length > pkt_size) {
  563. *mode = 1;
  564. csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
  565. /* autoset shouldn't be set in high bandwidth */
  566. /*
  567. * Enable Autoset according to table
  568. * below
  569. * bulk_split hb_mult Autoset_Enable
  570. * 0 1 Yes(Normal)
  571. * 0 >1 No(High BW ISO)
  572. * 1 1 Yes(HS bulk)
  573. * 1 >1 Yes(FS bulk)
  574. */
  575. if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
  576. can_bulk_split(hw_ep->musb, qh->type)))
  577. csr |= MUSB_TXCSR_AUTOSET;
  578. } else {
  579. *mode = 0;
  580. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
  581. csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
  582. }
  583. channel->desired_mode = mode;
  584. musb_writew(epio, MUSB_TXCSR, csr);
  585. return 0;
  586. }
  587. static int musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
  588. struct musb_hw_ep *hw_ep,
  589. struct musb_qh *qh,
  590. struct urb *urb,
  591. u32 offset,
  592. u32 *length,
  593. u8 *mode)
  594. {
  595. struct dma_channel *channel = hw_ep->tx_channel;
  596. if (!is_cppi_enabled(hw_ep->musb) && !tusb_dma_omap(hw_ep->musb))
  597. return -ENODEV;
  598. channel->actual_len = 0;
  599. /*
  600. * TX uses "RNDIS" mode automatically but needs help
  601. * to identify the zero-length-final-packet case.
  602. */
  603. *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
  604. return 0;
  605. }
  606. static bool musb_tx_dma_program(struct dma_controller *dma,
  607. struct musb_hw_ep *hw_ep, struct musb_qh *qh,
  608. struct urb *urb, u32 offset, u32 length)
  609. {
  610. struct dma_channel *channel = hw_ep->tx_channel;
  611. u16 pkt_size = qh->maxpacket;
  612. u8 mode;
  613. int res;
  614. if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
  615. res = musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb,
  616. offset, &length, &mode);
  617. else
  618. res = musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb,
  619. offset, &length, &mode);
  620. if (res)
  621. return false;
  622. qh->segsize = length;
  623. /*
  624. * Ensure the data reaches to main memory before starting
  625. * DMA transfer
  626. */
  627. wmb();
  628. if (!dma->channel_program(channel, pkt_size, mode,
  629. urb->transfer_dma + offset, length)) {
  630. void __iomem *epio = hw_ep->regs;
  631. u16 csr;
  632. dma->channel_release(channel);
  633. hw_ep->tx_channel = NULL;
  634. csr = musb_readw(epio, MUSB_TXCSR);
  635. csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
  636. musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
  637. return false;
  638. }
  639. return true;
  640. }
  641. /*
  642. * Program an HDRC endpoint as per the given URB
  643. * Context: irqs blocked, controller lock held
  644. */
  645. static void musb_ep_program(struct musb *musb, u8 epnum,
  646. struct urb *urb, int is_out,
  647. u8 *buf, u32 offset, u32 len)
  648. {
  649. struct dma_controller *dma_controller;
  650. struct dma_channel *dma_channel;
  651. u8 dma_ok;
  652. void __iomem *mbase = musb->mregs;
  653. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  654. void __iomem *epio = hw_ep->regs;
  655. struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
  656. u16 packet_sz = qh->maxpacket;
  657. u8 use_dma = 1;
  658. u16 csr;
  659. dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
  660. "h_addr%02x h_port%02x bytes %d\n",
  661. is_out ? "-->" : "<--",
  662. epnum, urb, urb->dev->speed,
  663. qh->addr_reg, qh->epnum, is_out ? "out" : "in",
  664. qh->h_addr_reg, qh->h_port_reg,
  665. len);
  666. musb_ep_select(mbase, epnum);
  667. if (is_out && !len) {
  668. use_dma = 0;
  669. csr = musb_readw(epio, MUSB_TXCSR);
  670. csr &= ~MUSB_TXCSR_DMAENAB;
  671. musb_writew(epio, MUSB_TXCSR, csr);
  672. hw_ep->tx_channel = NULL;
  673. }
  674. /* candidate for DMA? */
  675. dma_controller = musb->dma_controller;
  676. if (use_dma && is_dma_capable() && epnum && dma_controller) {
  677. dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
  678. if (!dma_channel) {
  679. dma_channel = dma_controller->channel_alloc(
  680. dma_controller, hw_ep, is_out);
  681. if (is_out)
  682. hw_ep->tx_channel = dma_channel;
  683. else
  684. hw_ep->rx_channel = dma_channel;
  685. }
  686. } else
  687. dma_channel = NULL;
  688. /* make sure we clear DMAEnab, autoSet bits from previous run */
  689. /* OUT/transmit/EP0 or IN/receive? */
  690. if (is_out) {
  691. u16 csr;
  692. u16 int_txe;
  693. u16 load_count;
  694. csr = musb_readw(epio, MUSB_TXCSR);
  695. /* disable interrupt in case we flush */
  696. int_txe = musb->intrtxe;
  697. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  698. /* general endpoint setup */
  699. if (epnum) {
  700. /* flush all old state, set default */
  701. /*
  702. * We could be flushing valid
  703. * packets in double buffering
  704. * case
  705. */
  706. if (!hw_ep->tx_double_buffered)
  707. musb_h_tx_flush_fifo(hw_ep);
  708. /*
  709. * We must not clear the DMAMODE bit before or in
  710. * the same cycle with the DMAENAB bit, so we clear
  711. * the latter first...
  712. */
  713. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  714. | MUSB_TXCSR_AUTOSET
  715. | MUSB_TXCSR_DMAENAB
  716. | MUSB_TXCSR_FRCDATATOG
  717. | MUSB_TXCSR_H_RXSTALL
  718. | MUSB_TXCSR_H_ERROR
  719. | MUSB_TXCSR_TXPKTRDY
  720. );
  721. csr |= MUSB_TXCSR_MODE;
  722. if (!hw_ep->tx_double_buffered) {
  723. if (usb_gettoggle(urb->dev, qh->epnum, 1))
  724. csr |= MUSB_TXCSR_H_WR_DATATOGGLE
  725. | MUSB_TXCSR_H_DATATOGGLE;
  726. else
  727. csr |= MUSB_TXCSR_CLRDATATOG;
  728. }
  729. musb_writew(epio, MUSB_TXCSR, csr);
  730. /* REVISIT may need to clear FLUSHFIFO ... */
  731. csr &= ~MUSB_TXCSR_DMAMODE;
  732. musb_writew(epio, MUSB_TXCSR, csr);
  733. csr = musb_readw(epio, MUSB_TXCSR);
  734. } else {
  735. /* endpoint 0: just flush */
  736. musb_h_ep0_flush_fifo(hw_ep);
  737. }
  738. /* target addr and (for multipoint) hub addr/port */
  739. if (musb->is_multipoint) {
  740. musb_write_txfunaddr(musb, epnum, qh->addr_reg);
  741. musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
  742. musb_write_txhubport(musb, epnum, qh->h_port_reg);
  743. /* FIXME if !epnum, do the same for RX ... */
  744. } else
  745. musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
  746. /* protocol/endpoint/interval/NAKlimit */
  747. if (epnum) {
  748. musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
  749. if (musb->double_buffer_not_ok) {
  750. musb_writew(epio, MUSB_TXMAXP,
  751. hw_ep->max_packet_sz_tx);
  752. } else if (can_bulk_split(musb, qh->type)) {
  753. qh->hb_mult = hw_ep->max_packet_sz_tx
  754. / packet_sz;
  755. musb_writew(epio, MUSB_TXMAXP, packet_sz
  756. | ((qh->hb_mult) - 1) << 11);
  757. } else {
  758. musb_writew(epio, MUSB_TXMAXP,
  759. qh->maxpacket |
  760. ((qh->hb_mult - 1) << 11));
  761. }
  762. musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
  763. } else {
  764. musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
  765. if (musb->is_multipoint)
  766. musb_writeb(epio, MUSB_TYPE0,
  767. qh->type_reg);
  768. }
  769. if (can_bulk_split(musb, qh->type))
  770. load_count = min((u32) hw_ep->max_packet_sz_tx,
  771. len);
  772. else
  773. load_count = min((u32) packet_sz, len);
  774. if (dma_channel && musb_tx_dma_program(dma_controller,
  775. hw_ep, qh, urb, offset, len))
  776. load_count = 0;
  777. if (load_count) {
  778. /* PIO to load FIFO */
  779. qh->segsize = load_count;
  780. if (!buf) {
  781. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  782. SG_MITER_ATOMIC
  783. | SG_MITER_FROM_SG);
  784. if (!sg_miter_next(&qh->sg_miter)) {
  785. dev_err(musb->controller,
  786. "error: sg"
  787. "list empty\n");
  788. sg_miter_stop(&qh->sg_miter);
  789. goto finish;
  790. }
  791. buf = qh->sg_miter.addr + urb->sg->offset +
  792. urb->actual_length;
  793. load_count = min_t(u32, load_count,
  794. qh->sg_miter.length);
  795. musb_write_fifo(hw_ep, load_count, buf);
  796. qh->sg_miter.consumed = load_count;
  797. sg_miter_stop(&qh->sg_miter);
  798. } else
  799. musb_write_fifo(hw_ep, load_count, buf);
  800. }
  801. finish:
  802. /* re-enable interrupt */
  803. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  804. /* IN/receive */
  805. } else {
  806. u16 csr;
  807. if (hw_ep->rx_reinit) {
  808. musb_rx_reinit(musb, qh, epnum);
  809. /* init new state: toggle and NYET, maybe DMA later */
  810. if (usb_gettoggle(urb->dev, qh->epnum, 0))
  811. csr = MUSB_RXCSR_H_WR_DATATOGGLE
  812. | MUSB_RXCSR_H_DATATOGGLE;
  813. else
  814. csr = 0;
  815. if (qh->type == USB_ENDPOINT_XFER_INT)
  816. csr |= MUSB_RXCSR_DISNYET;
  817. } else {
  818. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  819. if (csr & (MUSB_RXCSR_RXPKTRDY
  820. | MUSB_RXCSR_DMAENAB
  821. | MUSB_RXCSR_H_REQPKT))
  822. ERR("broken !rx_reinit, ep%d csr %04x\n",
  823. hw_ep->epnum, csr);
  824. /* scrub any stale state, leaving toggle alone */
  825. csr &= MUSB_RXCSR_DISNYET;
  826. }
  827. /* kick things off */
  828. if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
  829. /* Candidate for DMA */
  830. dma_channel->actual_len = 0L;
  831. qh->segsize = len;
  832. /* AUTOREQ is in a DMA register */
  833. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  834. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  835. /*
  836. * Unless caller treats short RX transfers as
  837. * errors, we dare not queue multiple transfers.
  838. */
  839. dma_ok = dma_controller->channel_program(dma_channel,
  840. packet_sz, !(urb->transfer_flags &
  841. URB_SHORT_NOT_OK),
  842. urb->transfer_dma + offset,
  843. qh->segsize);
  844. if (!dma_ok) {
  845. dma_controller->channel_release(dma_channel);
  846. hw_ep->rx_channel = dma_channel = NULL;
  847. } else
  848. csr |= MUSB_RXCSR_DMAENAB;
  849. }
  850. csr |= MUSB_RXCSR_H_REQPKT;
  851. dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
  852. musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
  853. csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
  854. }
  855. }
  856. /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
  857. * the end; avoids starvation for other endpoints.
  858. */
  859. static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
  860. int is_in)
  861. {
  862. struct dma_channel *dma;
  863. struct urb *urb;
  864. void __iomem *mbase = musb->mregs;
  865. void __iomem *epio = ep->regs;
  866. struct musb_qh *cur_qh, *next_qh;
  867. u16 rx_csr, tx_csr;
  868. musb_ep_select(mbase, ep->epnum);
  869. if (is_in) {
  870. dma = is_dma_capable() ? ep->rx_channel : NULL;
  871. /* clear nak timeout bit */
  872. rx_csr = musb_readw(epio, MUSB_RXCSR);
  873. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  874. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  875. musb_writew(epio, MUSB_RXCSR, rx_csr);
  876. cur_qh = first_qh(&musb->in_bulk);
  877. } else {
  878. dma = is_dma_capable() ? ep->tx_channel : NULL;
  879. /* clear nak timeout bit */
  880. tx_csr = musb_readw(epio, MUSB_TXCSR);
  881. tx_csr |= MUSB_TXCSR_H_WZC_BITS;
  882. tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
  883. musb_writew(epio, MUSB_TXCSR, tx_csr);
  884. cur_qh = first_qh(&musb->out_bulk);
  885. }
  886. if (cur_qh) {
  887. urb = next_urb(cur_qh);
  888. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  889. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  890. musb->dma_controller->channel_abort(dma);
  891. urb->actual_length += dma->actual_len;
  892. dma->actual_len = 0L;
  893. }
  894. musb_save_toggle(cur_qh, is_in, urb);
  895. if (is_in) {
  896. /* move cur_qh to end of queue */
  897. list_move_tail(&cur_qh->ring, &musb->in_bulk);
  898. /* get the next qh from musb->in_bulk */
  899. next_qh = first_qh(&musb->in_bulk);
  900. /* set rx_reinit and schedule the next qh */
  901. ep->rx_reinit = 1;
  902. } else {
  903. /* move cur_qh to end of queue */
  904. list_move_tail(&cur_qh->ring, &musb->out_bulk);
  905. /* get the next qh from musb->out_bulk */
  906. next_qh = first_qh(&musb->out_bulk);
  907. /* set tx_reinit and schedule the next qh */
  908. ep->tx_reinit = 1;
  909. }
  910. musb_start_urb(musb, is_in, next_qh);
  911. }
  912. }
  913. /*
  914. * Service the default endpoint (ep0) as host.
  915. * Return true until it's time to start the status stage.
  916. */
  917. static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
  918. {
  919. bool more = false;
  920. u8 *fifo_dest = NULL;
  921. u16 fifo_count = 0;
  922. struct musb_hw_ep *hw_ep = musb->control_ep;
  923. struct musb_qh *qh = hw_ep->in_qh;
  924. struct usb_ctrlrequest *request;
  925. switch (musb->ep0_stage) {
  926. case MUSB_EP0_IN:
  927. fifo_dest = urb->transfer_buffer + urb->actual_length;
  928. fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
  929. urb->actual_length);
  930. if (fifo_count < len)
  931. urb->status = -EOVERFLOW;
  932. musb_read_fifo(hw_ep, fifo_count, fifo_dest);
  933. urb->actual_length += fifo_count;
  934. if (len < qh->maxpacket) {
  935. /* always terminate on short read; it's
  936. * rarely reported as an error.
  937. */
  938. } else if (urb->actual_length <
  939. urb->transfer_buffer_length)
  940. more = true;
  941. break;
  942. case MUSB_EP0_START:
  943. request = (struct usb_ctrlrequest *) urb->setup_packet;
  944. if (!request->wLength) {
  945. dev_dbg(musb->controller, "start no-DATA\n");
  946. break;
  947. } else if (request->bRequestType & USB_DIR_IN) {
  948. dev_dbg(musb->controller, "start IN-DATA\n");
  949. musb->ep0_stage = MUSB_EP0_IN;
  950. more = true;
  951. break;
  952. } else {
  953. dev_dbg(musb->controller, "start OUT-DATA\n");
  954. musb->ep0_stage = MUSB_EP0_OUT;
  955. more = true;
  956. }
  957. /* FALLTHROUGH */
  958. case MUSB_EP0_OUT:
  959. fifo_count = min_t(size_t, qh->maxpacket,
  960. urb->transfer_buffer_length -
  961. urb->actual_length);
  962. if (fifo_count) {
  963. fifo_dest = (u8 *) (urb->transfer_buffer
  964. + urb->actual_length);
  965. dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
  966. fifo_count,
  967. (fifo_count == 1) ? "" : "s",
  968. fifo_dest);
  969. musb_write_fifo(hw_ep, fifo_count, fifo_dest);
  970. urb->actual_length += fifo_count;
  971. more = true;
  972. }
  973. break;
  974. default:
  975. ERR("bogus ep0 stage %d\n", musb->ep0_stage);
  976. break;
  977. }
  978. return more;
  979. }
  980. /*
  981. * Handle default endpoint interrupt as host. Only called in IRQ time
  982. * from musb_interrupt().
  983. *
  984. * called with controller irqlocked
  985. */
  986. irqreturn_t musb_h_ep0_irq(struct musb *musb)
  987. {
  988. struct urb *urb;
  989. u16 csr, len;
  990. int status = 0;
  991. void __iomem *mbase = musb->mregs;
  992. struct musb_hw_ep *hw_ep = musb->control_ep;
  993. void __iomem *epio = hw_ep->regs;
  994. struct musb_qh *qh = hw_ep->in_qh;
  995. bool complete = false;
  996. irqreturn_t retval = IRQ_NONE;
  997. /* ep0 only has one queue, "in" */
  998. urb = next_urb(qh);
  999. musb_ep_select(mbase, 0);
  1000. csr = musb_readw(epio, MUSB_CSR0);
  1001. len = (csr & MUSB_CSR0_RXPKTRDY)
  1002. ? musb_readb(epio, MUSB_COUNT0)
  1003. : 0;
  1004. dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
  1005. csr, qh, len, urb, musb->ep0_stage);
  1006. /* if we just did status stage, we are done */
  1007. if (MUSB_EP0_STATUS == musb->ep0_stage) {
  1008. retval = IRQ_HANDLED;
  1009. complete = true;
  1010. }
  1011. /* prepare status */
  1012. if (csr & MUSB_CSR0_H_RXSTALL) {
  1013. dev_dbg(musb->controller, "STALLING ENDPOINT\n");
  1014. status = -EPIPE;
  1015. } else if (csr & MUSB_CSR0_H_ERROR) {
  1016. dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
  1017. status = -EPROTO;
  1018. } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
  1019. dev_dbg(musb->controller, "control NAK timeout\n");
  1020. /* NOTE: this code path would be a good place to PAUSE a
  1021. * control transfer, if another one is queued, so that
  1022. * ep0 is more likely to stay busy. That's already done
  1023. * for bulk RX transfers.
  1024. *
  1025. * if (qh->ring.next != &musb->control), then
  1026. * we have a candidate... NAKing is *NOT* an error
  1027. */
  1028. musb_writew(epio, MUSB_CSR0, 0);
  1029. retval = IRQ_HANDLED;
  1030. }
  1031. if (status) {
  1032. dev_dbg(musb->controller, "aborting\n");
  1033. retval = IRQ_HANDLED;
  1034. if (urb)
  1035. urb->status = status;
  1036. complete = true;
  1037. /* use the proper sequence to abort the transfer */
  1038. if (csr & MUSB_CSR0_H_REQPKT) {
  1039. csr &= ~MUSB_CSR0_H_REQPKT;
  1040. musb_writew(epio, MUSB_CSR0, csr);
  1041. csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
  1042. musb_writew(epio, MUSB_CSR0, csr);
  1043. } else {
  1044. musb_h_ep0_flush_fifo(hw_ep);
  1045. }
  1046. musb_writeb(epio, MUSB_NAKLIMIT0, 0);
  1047. /* clear it */
  1048. musb_writew(epio, MUSB_CSR0, 0);
  1049. }
  1050. if (unlikely(!urb)) {
  1051. /* stop endpoint since we have no place for its data, this
  1052. * SHOULD NEVER HAPPEN! */
  1053. ERR("no URB for end 0\n");
  1054. musb_h_ep0_flush_fifo(hw_ep);
  1055. goto done;
  1056. }
  1057. if (!complete) {
  1058. /* call common logic and prepare response */
  1059. if (musb_h_ep0_continue(musb, len, urb)) {
  1060. /* more packets required */
  1061. csr = (MUSB_EP0_IN == musb->ep0_stage)
  1062. ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
  1063. } else {
  1064. /* data transfer complete; perform status phase */
  1065. if (usb_pipeout(urb->pipe)
  1066. || !urb->transfer_buffer_length)
  1067. csr = MUSB_CSR0_H_STATUSPKT
  1068. | MUSB_CSR0_H_REQPKT;
  1069. else
  1070. csr = MUSB_CSR0_H_STATUSPKT
  1071. | MUSB_CSR0_TXPKTRDY;
  1072. /* disable ping token in status phase */
  1073. csr |= MUSB_CSR0_H_DIS_PING;
  1074. /* flag status stage */
  1075. musb->ep0_stage = MUSB_EP0_STATUS;
  1076. dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
  1077. }
  1078. musb_writew(epio, MUSB_CSR0, csr);
  1079. retval = IRQ_HANDLED;
  1080. } else
  1081. musb->ep0_stage = MUSB_EP0_IDLE;
  1082. /* call completion handler if done */
  1083. if (complete)
  1084. musb_advance_schedule(musb, urb, hw_ep, 1);
  1085. done:
  1086. return retval;
  1087. }
  1088. #ifdef CONFIG_USB_INVENTRA_DMA
  1089. /* Host side TX (OUT) using Mentor DMA works as follows:
  1090. submit_urb ->
  1091. - if queue was empty, Program Endpoint
  1092. - ... which starts DMA to fifo in mode 1 or 0
  1093. DMA Isr (transfer complete) -> TxAvail()
  1094. - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
  1095. only in musb_cleanup_urb)
  1096. - TxPktRdy has to be set in mode 0 or for
  1097. short packets in mode 1.
  1098. */
  1099. #endif
  1100. /* Service a Tx-Available or dma completion irq for the endpoint */
  1101. void musb_host_tx(struct musb *musb, u8 epnum)
  1102. {
  1103. int pipe;
  1104. bool done = false;
  1105. u16 tx_csr;
  1106. size_t length = 0;
  1107. size_t offset = 0;
  1108. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1109. void __iomem *epio = hw_ep->regs;
  1110. struct musb_qh *qh = hw_ep->out_qh;
  1111. struct urb *urb = next_urb(qh);
  1112. u32 status = 0;
  1113. void __iomem *mbase = musb->mregs;
  1114. struct dma_channel *dma;
  1115. bool transfer_pending = false;
  1116. musb_ep_select(mbase, epnum);
  1117. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1118. /* with CPPI, DMA sometimes triggers "extra" irqs */
  1119. if (!urb) {
  1120. dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  1121. return;
  1122. }
  1123. pipe = urb->pipe;
  1124. dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
  1125. dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
  1126. dma ? ", dma" : "");
  1127. /* check for errors */
  1128. if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
  1129. /* dma was disabled, fifo flushed */
  1130. dev_dbg(musb->controller, "TX end %d stall\n", epnum);
  1131. /* stall; record URB status */
  1132. status = -EPIPE;
  1133. } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
  1134. /* (NON-ISO) dma was disabled, fifo flushed */
  1135. dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
  1136. status = -ETIMEDOUT;
  1137. } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
  1138. if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
  1139. && !list_is_singular(&musb->out_bulk)) {
  1140. dev_dbg(musb->controller,
  1141. "NAK timeout on TX%d ep\n", epnum);
  1142. musb_bulk_nak_timeout(musb, hw_ep, 0);
  1143. } else {
  1144. dev_dbg(musb->controller,
  1145. "TX end=%d device not responding\n", epnum);
  1146. /* NOTE: this code path would be a good place to PAUSE a
  1147. * transfer, if there's some other (nonperiodic) tx urb
  1148. * that could use this fifo. (dma complicates it...)
  1149. * That's already done for bulk RX transfers.
  1150. *
  1151. * if (bulk && qh->ring.next != &musb->out_bulk), then
  1152. * we have a candidate... NAKing is *NOT* an error
  1153. */
  1154. musb_ep_select(mbase, epnum);
  1155. musb_writew(epio, MUSB_TXCSR,
  1156. MUSB_TXCSR_H_WZC_BITS
  1157. | MUSB_TXCSR_TXPKTRDY);
  1158. }
  1159. return;
  1160. }
  1161. done:
  1162. if (status) {
  1163. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1164. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1165. musb->dma_controller->channel_abort(dma);
  1166. }
  1167. /* do the proper sequence to abort the transfer in the
  1168. * usb core; the dma engine should already be stopped.
  1169. */
  1170. musb_h_tx_flush_fifo(hw_ep);
  1171. tx_csr &= ~(MUSB_TXCSR_AUTOSET
  1172. | MUSB_TXCSR_DMAENAB
  1173. | MUSB_TXCSR_H_ERROR
  1174. | MUSB_TXCSR_H_RXSTALL
  1175. | MUSB_TXCSR_H_NAKTIMEOUT
  1176. );
  1177. musb_ep_select(mbase, epnum);
  1178. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1179. /* REVISIT may need to clear FLUSHFIFO ... */
  1180. musb_writew(epio, MUSB_TXCSR, tx_csr);
  1181. musb_writeb(epio, MUSB_TXINTERVAL, 0);
  1182. done = true;
  1183. }
  1184. /* second cppi case */
  1185. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1186. dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
  1187. return;
  1188. }
  1189. if (is_dma_capable() && dma && !status) {
  1190. /*
  1191. * DMA has completed. But if we're using DMA mode 1 (multi
  1192. * packet DMA), we need a terminal TXPKTRDY interrupt before
  1193. * we can consider this transfer completed, lest we trash
  1194. * its last packet when writing the next URB's data. So we
  1195. * switch back to mode 0 to get that interrupt; we'll come
  1196. * back here once it happens.
  1197. */
  1198. if (tx_csr & MUSB_TXCSR_DMAMODE) {
  1199. /*
  1200. * We shouldn't clear DMAMODE with DMAENAB set; so
  1201. * clear them in a safe order. That should be OK
  1202. * once TXPKTRDY has been set (and I've never seen
  1203. * it being 0 at this moment -- DMA interrupt latency
  1204. * is significant) but if it hasn't been then we have
  1205. * no choice but to stop being polite and ignore the
  1206. * programmer's guide... :-)
  1207. *
  1208. * Note that we must write TXCSR with TXPKTRDY cleared
  1209. * in order not to re-trigger the packet send (this bit
  1210. * can't be cleared by CPU), and there's another caveat:
  1211. * TXPKTRDY may be set shortly and then cleared in the
  1212. * double-buffered FIFO mode, so we do an extra TXCSR
  1213. * read for debouncing...
  1214. */
  1215. tx_csr &= musb_readw(epio, MUSB_TXCSR);
  1216. if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
  1217. tx_csr &= ~(MUSB_TXCSR_DMAENAB |
  1218. MUSB_TXCSR_TXPKTRDY);
  1219. musb_writew(epio, MUSB_TXCSR,
  1220. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1221. }
  1222. tx_csr &= ~(MUSB_TXCSR_DMAMODE |
  1223. MUSB_TXCSR_TXPKTRDY);
  1224. musb_writew(epio, MUSB_TXCSR,
  1225. tx_csr | MUSB_TXCSR_H_WZC_BITS);
  1226. /*
  1227. * There is no guarantee that we'll get an interrupt
  1228. * after clearing DMAMODE as we might have done this
  1229. * too late (after TXPKTRDY was cleared by controller).
  1230. * Re-read TXCSR as we have spoiled its previous value.
  1231. */
  1232. tx_csr = musb_readw(epio, MUSB_TXCSR);
  1233. }
  1234. /*
  1235. * We may get here from a DMA completion or TXPKTRDY interrupt.
  1236. * In any case, we must check the FIFO status here and bail out
  1237. * only if the FIFO still has data -- that should prevent the
  1238. * "missed" TXPKTRDY interrupts and deal with double-buffered
  1239. * FIFO mode too...
  1240. */
  1241. if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
  1242. dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
  1243. "CSR %04x\n", tx_csr);
  1244. return;
  1245. }
  1246. }
  1247. if (!status || dma || usb_pipeisoc(pipe)) {
  1248. if (dma)
  1249. length = dma->actual_len;
  1250. else
  1251. length = qh->segsize;
  1252. qh->offset += length;
  1253. if (usb_pipeisoc(pipe)) {
  1254. struct usb_iso_packet_descriptor *d;
  1255. d = urb->iso_frame_desc + qh->iso_idx;
  1256. d->actual_length = length;
  1257. d->status = status;
  1258. if (++qh->iso_idx >= urb->number_of_packets) {
  1259. done = true;
  1260. } else {
  1261. d++;
  1262. offset = d->offset;
  1263. length = d->length;
  1264. }
  1265. } else if (dma && urb->transfer_buffer_length == qh->offset) {
  1266. done = true;
  1267. } else {
  1268. /* see if we need to send more data, or ZLP */
  1269. if (qh->segsize < qh->maxpacket)
  1270. done = true;
  1271. else if (qh->offset == urb->transfer_buffer_length
  1272. && !(urb->transfer_flags
  1273. & URB_ZERO_PACKET))
  1274. done = true;
  1275. if (!done) {
  1276. offset = qh->offset;
  1277. length = urb->transfer_buffer_length - offset;
  1278. transfer_pending = true;
  1279. }
  1280. }
  1281. }
  1282. /* urb->status != -EINPROGRESS means request has been faulted,
  1283. * so we must abort this transfer after cleanup
  1284. */
  1285. if (urb->status != -EINPROGRESS) {
  1286. done = true;
  1287. if (status == 0)
  1288. status = urb->status;
  1289. }
  1290. if (done) {
  1291. /* set status */
  1292. urb->status = status;
  1293. urb->actual_length = qh->offset;
  1294. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
  1295. return;
  1296. } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
  1297. if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
  1298. offset, length)) {
  1299. if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
  1300. musb_h_tx_dma_start(hw_ep);
  1301. return;
  1302. }
  1303. } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
  1304. dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
  1305. return;
  1306. }
  1307. /*
  1308. * PIO: start next packet in this URB.
  1309. *
  1310. * REVISIT: some docs say that when hw_ep->tx_double_buffered,
  1311. * (and presumably, FIFO is not half-full) we should write *two*
  1312. * packets before updating TXCSR; other docs disagree...
  1313. */
  1314. if (length > qh->maxpacket)
  1315. length = qh->maxpacket;
  1316. /* Unmap the buffer so that CPU can use it */
  1317. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1318. /*
  1319. * We need to map sg if the transfer_buffer is
  1320. * NULL.
  1321. */
  1322. if (!urb->transfer_buffer)
  1323. qh->use_sg = true;
  1324. if (qh->use_sg) {
  1325. /* sg_miter_start is already done in musb_ep_program */
  1326. if (!sg_miter_next(&qh->sg_miter)) {
  1327. dev_err(musb->controller, "error: sg list empty\n");
  1328. sg_miter_stop(&qh->sg_miter);
  1329. status = -EINVAL;
  1330. goto done;
  1331. }
  1332. urb->transfer_buffer = qh->sg_miter.addr;
  1333. length = min_t(u32, length, qh->sg_miter.length);
  1334. musb_write_fifo(hw_ep, length, urb->transfer_buffer);
  1335. qh->sg_miter.consumed = length;
  1336. sg_miter_stop(&qh->sg_miter);
  1337. } else {
  1338. musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
  1339. }
  1340. qh->segsize = length;
  1341. if (qh->use_sg) {
  1342. if (offset + length >= urb->transfer_buffer_length)
  1343. qh->use_sg = false;
  1344. }
  1345. musb_ep_select(mbase, epnum);
  1346. musb_writew(epio, MUSB_TXCSR,
  1347. MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
  1348. }
  1349. #ifdef CONFIG_USB_TI_CPPI41_DMA
  1350. /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
  1351. static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1352. struct musb_hw_ep *hw_ep,
  1353. struct musb_qh *qh,
  1354. struct urb *urb,
  1355. size_t len)
  1356. {
  1357. struct dma_channel *channel = hw_ep->tx_channel;
  1358. void __iomem *epio = hw_ep->regs;
  1359. dma_addr_t *buf;
  1360. u32 length, res;
  1361. u16 val;
  1362. buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
  1363. (u32)urb->transfer_dma;
  1364. length = urb->iso_frame_desc[qh->iso_idx].length;
  1365. val = musb_readw(epio, MUSB_RXCSR);
  1366. val |= MUSB_RXCSR_DMAENAB;
  1367. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1368. res = dma->channel_program(channel, qh->maxpacket, 0,
  1369. (u32)buf, length);
  1370. return res;
  1371. }
  1372. #else
  1373. static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
  1374. struct musb_hw_ep *hw_ep,
  1375. struct musb_qh *qh,
  1376. struct urb *urb,
  1377. size_t len)
  1378. {
  1379. return false;
  1380. }
  1381. #endif
  1382. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
  1383. defined(CONFIG_USB_TI_CPPI41_DMA)
  1384. /* Host side RX (IN) using Mentor DMA works as follows:
  1385. submit_urb ->
  1386. - if queue was empty, ProgramEndpoint
  1387. - first IN token is sent out (by setting ReqPkt)
  1388. LinuxIsr -> RxReady()
  1389. /\ => first packet is received
  1390. | - Set in mode 0 (DmaEnab, ~ReqPkt)
  1391. | -> DMA Isr (transfer complete) -> RxReady()
  1392. | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
  1393. | - if urb not complete, send next IN token (ReqPkt)
  1394. | | else complete urb.
  1395. | |
  1396. ---------------------------
  1397. *
  1398. * Nuances of mode 1:
  1399. * For short packets, no ack (+RxPktRdy) is sent automatically
  1400. * (even if AutoClear is ON)
  1401. * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
  1402. * automatically => major problem, as collecting the next packet becomes
  1403. * difficult. Hence mode 1 is not used.
  1404. *
  1405. * REVISIT
  1406. * All we care about at this driver level is that
  1407. * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
  1408. * (b) termination conditions are: short RX, or buffer full;
  1409. * (c) fault modes include
  1410. * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
  1411. * (and that endpoint's dma queue stops immediately)
  1412. * - overflow (full, PLUS more bytes in the terminal packet)
  1413. *
  1414. * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
  1415. * thus be a great candidate for using mode 1 ... for all but the
  1416. * last packet of one URB's transfer.
  1417. */
  1418. static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1419. struct musb_hw_ep *hw_ep,
  1420. struct musb_qh *qh,
  1421. struct urb *urb,
  1422. size_t len)
  1423. {
  1424. struct dma_channel *channel = hw_ep->rx_channel;
  1425. void __iomem *epio = hw_ep->regs;
  1426. u16 val;
  1427. int pipe;
  1428. bool done;
  1429. pipe = urb->pipe;
  1430. if (usb_pipeisoc(pipe)) {
  1431. struct usb_iso_packet_descriptor *d;
  1432. d = urb->iso_frame_desc + qh->iso_idx;
  1433. d->actual_length = len;
  1434. /* even if there was an error, we did the dma
  1435. * for iso_frame_desc->length
  1436. */
  1437. if (d->status != -EILSEQ && d->status != -EOVERFLOW)
  1438. d->status = 0;
  1439. if (++qh->iso_idx >= urb->number_of_packets) {
  1440. done = true;
  1441. } else {
  1442. /* REVISIT: Why ignore return value here? */
  1443. if (musb_dma_cppi41(hw_ep->musb))
  1444. done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
  1445. urb, len);
  1446. done = false;
  1447. }
  1448. } else {
  1449. /* done if urb buffer is full or short packet is recd */
  1450. done = (urb->actual_length + len >=
  1451. urb->transfer_buffer_length
  1452. || channel->actual_len < qh->maxpacket
  1453. || channel->rx_packet_done);
  1454. }
  1455. /* send IN token for next packet, without AUTOREQ */
  1456. if (!done) {
  1457. val = musb_readw(epio, MUSB_RXCSR);
  1458. val |= MUSB_RXCSR_H_REQPKT;
  1459. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1460. }
  1461. return done;
  1462. }
  1463. /* Disadvantage of using mode 1:
  1464. * It's basically usable only for mass storage class; essentially all
  1465. * other protocols also terminate transfers on short packets.
  1466. *
  1467. * Details:
  1468. * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
  1469. * If you try to use mode 1 for (transfer_buffer_length - 512), and try
  1470. * to use the extra IN token to grab the last packet using mode 0, then
  1471. * the problem is that you cannot be sure when the device will send the
  1472. * last packet and RxPktRdy set. Sometimes the packet is recd too soon
  1473. * such that it gets lost when RxCSR is re-set at the end of the mode 1
  1474. * transfer, while sometimes it is recd just a little late so that if you
  1475. * try to configure for mode 0 soon after the mode 1 transfer is
  1476. * completed, you will find rxcount 0. Okay, so you might think why not
  1477. * wait for an interrupt when the pkt is recd. Well, you won't get any!
  1478. */
  1479. static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1480. struct musb_hw_ep *hw_ep,
  1481. struct musb_qh *qh,
  1482. struct urb *urb,
  1483. size_t len,
  1484. u8 iso_err)
  1485. {
  1486. struct musb *musb = hw_ep->musb;
  1487. void __iomem *epio = hw_ep->regs;
  1488. struct dma_channel *channel = hw_ep->rx_channel;
  1489. u16 rx_count, val;
  1490. int length, pipe, done;
  1491. dma_addr_t buf;
  1492. rx_count = musb_readw(epio, MUSB_RXCOUNT);
  1493. pipe = urb->pipe;
  1494. if (usb_pipeisoc(pipe)) {
  1495. int d_status = 0;
  1496. struct usb_iso_packet_descriptor *d;
  1497. d = urb->iso_frame_desc + qh->iso_idx;
  1498. if (iso_err) {
  1499. d_status = -EILSEQ;
  1500. urb->error_count++;
  1501. }
  1502. if (rx_count > d->length) {
  1503. if (d_status == 0) {
  1504. d_status = -EOVERFLOW;
  1505. urb->error_count++;
  1506. }
  1507. dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
  1508. rx_count, d->length);
  1509. length = d->length;
  1510. } else
  1511. length = rx_count;
  1512. d->status = d_status;
  1513. buf = urb->transfer_dma + d->offset;
  1514. } else {
  1515. length = rx_count;
  1516. buf = urb->transfer_dma + urb->actual_length;
  1517. }
  1518. channel->desired_mode = 0;
  1519. #ifdef USE_MODE1
  1520. /* because of the issue below, mode 1 will
  1521. * only rarely behave with correct semantics.
  1522. */
  1523. if ((urb->transfer_flags & URB_SHORT_NOT_OK)
  1524. && (urb->transfer_buffer_length - urb->actual_length)
  1525. > qh->maxpacket)
  1526. channel->desired_mode = 1;
  1527. if (rx_count < hw_ep->max_packet_sz_rx) {
  1528. length = rx_count;
  1529. channel->desired_mode = 0;
  1530. } else {
  1531. length = urb->transfer_buffer_length;
  1532. }
  1533. #endif
  1534. /* See comments above on disadvantages of using mode 1 */
  1535. val = musb_readw(epio, MUSB_RXCSR);
  1536. val &= ~MUSB_RXCSR_H_REQPKT;
  1537. if (channel->desired_mode == 0)
  1538. val &= ~MUSB_RXCSR_H_AUTOREQ;
  1539. else
  1540. val |= MUSB_RXCSR_H_AUTOREQ;
  1541. val |= MUSB_RXCSR_DMAENAB;
  1542. /* autoclear shouldn't be set in high bandwidth */
  1543. if (qh->hb_mult == 1)
  1544. val |= MUSB_RXCSR_AUTOCLEAR;
  1545. musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
  1546. /* REVISIT if when actual_length != 0,
  1547. * transfer_buffer_length needs to be
  1548. * adjusted first...
  1549. */
  1550. done = dma->channel_program(channel, qh->maxpacket,
  1551. channel->desired_mode,
  1552. buf, length);
  1553. if (!done) {
  1554. dma->channel_release(channel);
  1555. hw_ep->rx_channel = NULL;
  1556. channel = NULL;
  1557. val = musb_readw(epio, MUSB_RXCSR);
  1558. val &= ~(MUSB_RXCSR_DMAENAB
  1559. | MUSB_RXCSR_H_AUTOREQ
  1560. | MUSB_RXCSR_AUTOCLEAR);
  1561. musb_writew(epio, MUSB_RXCSR, val);
  1562. }
  1563. return done;
  1564. }
  1565. #else
  1566. static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
  1567. struct musb_hw_ep *hw_ep,
  1568. struct musb_qh *qh,
  1569. struct urb *urb,
  1570. size_t len)
  1571. {
  1572. return false;
  1573. }
  1574. static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
  1575. struct musb_hw_ep *hw_ep,
  1576. struct musb_qh *qh,
  1577. struct urb *urb,
  1578. size_t len,
  1579. u8 iso_err)
  1580. {
  1581. return false;
  1582. }
  1583. #endif
  1584. /*
  1585. * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
  1586. * and high-bandwidth IN transfer cases.
  1587. */
  1588. void musb_host_rx(struct musb *musb, u8 epnum)
  1589. {
  1590. struct urb *urb;
  1591. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1592. struct dma_controller *c = musb->dma_controller;
  1593. void __iomem *epio = hw_ep->regs;
  1594. struct musb_qh *qh = hw_ep->in_qh;
  1595. size_t xfer_len;
  1596. void __iomem *mbase = musb->mregs;
  1597. int pipe;
  1598. u16 rx_csr, val;
  1599. bool iso_err = false;
  1600. bool done = false;
  1601. u32 status;
  1602. struct dma_channel *dma;
  1603. unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
  1604. musb_ep_select(mbase, epnum);
  1605. urb = next_urb(qh);
  1606. dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
  1607. status = 0;
  1608. xfer_len = 0;
  1609. rx_csr = musb_readw(epio, MUSB_RXCSR);
  1610. val = rx_csr;
  1611. if (unlikely(!urb)) {
  1612. /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
  1613. * usbtest #11 (unlinks) triggers it regularly, sometimes
  1614. * with fifo full. (Only with DMA??)
  1615. */
  1616. dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
  1617. musb_readw(epio, MUSB_RXCOUNT));
  1618. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1619. return;
  1620. }
  1621. pipe = urb->pipe;
  1622. dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
  1623. epnum, rx_csr, urb->actual_length,
  1624. dma ? dma->actual_len : 0);
  1625. /* check for errors, concurrent stall & unlink is not really
  1626. * handled yet! */
  1627. if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
  1628. dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
  1629. /* stall; record URB status */
  1630. status = -EPIPE;
  1631. } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
  1632. dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
  1633. status = -EPROTO;
  1634. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1635. } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
  1636. if (USB_ENDPOINT_XFER_ISOC != qh->type) {
  1637. dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
  1638. /* NOTE: NAKing is *NOT* an error, so we want to
  1639. * continue. Except ... if there's a request for
  1640. * another QH, use that instead of starving it.
  1641. *
  1642. * Devices like Ethernet and serial adapters keep
  1643. * reads posted at all times, which will starve
  1644. * other devices without this logic.
  1645. */
  1646. if (usb_pipebulk(urb->pipe)
  1647. && qh->mux == 1
  1648. && !list_is_singular(&musb->in_bulk)) {
  1649. musb_bulk_nak_timeout(musb, hw_ep, 1);
  1650. return;
  1651. }
  1652. musb_ep_select(mbase, epnum);
  1653. rx_csr |= MUSB_RXCSR_H_WZC_BITS;
  1654. rx_csr &= ~MUSB_RXCSR_DATAERROR;
  1655. musb_writew(epio, MUSB_RXCSR, rx_csr);
  1656. goto finish;
  1657. } else {
  1658. dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
  1659. /* packet error reported later */
  1660. iso_err = true;
  1661. }
  1662. } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
  1663. dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
  1664. epnum);
  1665. status = -EPROTO;
  1666. }
  1667. /* faults abort the transfer */
  1668. if (status) {
  1669. /* clean up dma and collect transfer count */
  1670. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1671. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1672. musb->dma_controller->channel_abort(dma);
  1673. xfer_len = dma->actual_len;
  1674. }
  1675. musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
  1676. musb_writeb(epio, MUSB_RXINTERVAL, 0);
  1677. done = true;
  1678. goto finish;
  1679. }
  1680. if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
  1681. /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
  1682. ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
  1683. goto finish;
  1684. }
  1685. /* thorough shutdown for now ... given more precise fault handling
  1686. * and better queueing support, we might keep a DMA pipeline going
  1687. * while processing this irq for earlier completions.
  1688. */
  1689. /* FIXME this is _way_ too much in-line logic for Mentor DMA */
  1690. if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
  1691. (rx_csr & MUSB_RXCSR_H_REQPKT)) {
  1692. /* REVISIT this happened for a while on some short reads...
  1693. * the cleanup still needs investigation... looks bad...
  1694. * and also duplicates dma cleanup code above ... plus,
  1695. * shouldn't this be the "half full" double buffer case?
  1696. */
  1697. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  1698. dma->status = MUSB_DMA_STATUS_CORE_ABORT;
  1699. musb->dma_controller->channel_abort(dma);
  1700. xfer_len = dma->actual_len;
  1701. done = true;
  1702. }
  1703. dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
  1704. xfer_len, dma ? ", dma" : "");
  1705. rx_csr &= ~MUSB_RXCSR_H_REQPKT;
  1706. musb_ep_select(mbase, epnum);
  1707. musb_writew(epio, MUSB_RXCSR,
  1708. MUSB_RXCSR_H_WZC_BITS | rx_csr);
  1709. }
  1710. if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
  1711. xfer_len = dma->actual_len;
  1712. val &= ~(MUSB_RXCSR_DMAENAB
  1713. | MUSB_RXCSR_H_AUTOREQ
  1714. | MUSB_RXCSR_AUTOCLEAR
  1715. | MUSB_RXCSR_RXPKTRDY);
  1716. musb_writew(hw_ep->regs, MUSB_RXCSR, val);
  1717. if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1718. musb_dma_cppi41(musb)) {
  1719. done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
  1720. dev_dbg(hw_ep->musb->controller,
  1721. "ep %d dma %s, rxcsr %04x, rxcount %d\n",
  1722. epnum, done ? "off" : "reset",
  1723. musb_readw(epio, MUSB_RXCSR),
  1724. musb_readw(epio, MUSB_RXCOUNT));
  1725. } else {
  1726. done = true;
  1727. }
  1728. } else if (urb->status == -EINPROGRESS) {
  1729. /* if no errors, be sure a packet is ready for unloading */
  1730. if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
  1731. status = -EPROTO;
  1732. ERR("Rx interrupt with no errors or packet!\n");
  1733. /* FIXME this is another "SHOULD NEVER HAPPEN" */
  1734. /* SCRUB (RX) */
  1735. /* do the proper sequence to abort the transfer */
  1736. musb_ep_select(mbase, epnum);
  1737. val &= ~MUSB_RXCSR_H_REQPKT;
  1738. musb_writew(epio, MUSB_RXCSR, val);
  1739. goto finish;
  1740. }
  1741. /* we are expecting IN packets */
  1742. if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
  1743. musb_dma_cppi41(musb)) && dma) {
  1744. dev_dbg(hw_ep->musb->controller,
  1745. "RX%d count %d, buffer 0x%llx len %d/%d\n",
  1746. epnum, musb_readw(epio, MUSB_RXCOUNT),
  1747. (unsigned long long) urb->transfer_dma
  1748. + urb->actual_length,
  1749. qh->offset,
  1750. urb->transfer_buffer_length);
  1751. done = musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh,
  1752. urb, xfer_len,
  1753. iso_err);
  1754. if (done)
  1755. goto finish;
  1756. else
  1757. dev_err(musb->controller, "error: rx_dma failed\n");
  1758. }
  1759. if (!dma) {
  1760. unsigned int received_len;
  1761. /* Unmap the buffer so that CPU can use it */
  1762. usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
  1763. /*
  1764. * We need to map sg if the transfer_buffer is
  1765. * NULL.
  1766. */
  1767. if (!urb->transfer_buffer) {
  1768. qh->use_sg = true;
  1769. sg_miter_start(&qh->sg_miter, urb->sg, 1,
  1770. sg_flags);
  1771. }
  1772. if (qh->use_sg) {
  1773. if (!sg_miter_next(&qh->sg_miter)) {
  1774. dev_err(musb->controller, "error: sg list empty\n");
  1775. sg_miter_stop(&qh->sg_miter);
  1776. status = -EINVAL;
  1777. done = true;
  1778. goto finish;
  1779. }
  1780. urb->transfer_buffer = qh->sg_miter.addr;
  1781. received_len = urb->actual_length;
  1782. qh->offset = 0x0;
  1783. done = musb_host_packet_rx(musb, urb, epnum,
  1784. iso_err);
  1785. /* Calculate the number of bytes received */
  1786. received_len = urb->actual_length -
  1787. received_len;
  1788. qh->sg_miter.consumed = received_len;
  1789. sg_miter_stop(&qh->sg_miter);
  1790. } else {
  1791. done = musb_host_packet_rx(musb, urb,
  1792. epnum, iso_err);
  1793. }
  1794. dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
  1795. }
  1796. }
  1797. finish:
  1798. urb->actual_length += xfer_len;
  1799. qh->offset += xfer_len;
  1800. if (done) {
  1801. if (qh->use_sg)
  1802. qh->use_sg = false;
  1803. if (urb->status == -EINPROGRESS)
  1804. urb->status = status;
  1805. musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
  1806. }
  1807. }
  1808. /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
  1809. * the software schedule associates multiple such nodes with a given
  1810. * host side hardware endpoint + direction; scheduling may activate
  1811. * that hardware endpoint.
  1812. */
  1813. static int musb_schedule(
  1814. struct musb *musb,
  1815. struct musb_qh *qh,
  1816. int is_in)
  1817. {
  1818. int idle = 0;
  1819. int best_diff;
  1820. int best_end, epnum;
  1821. struct musb_hw_ep *hw_ep = NULL;
  1822. struct list_head *head = NULL;
  1823. u8 toggle;
  1824. u8 txtype;
  1825. struct urb *urb = next_urb(qh);
  1826. /* use fixed hardware for control and bulk */
  1827. if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
  1828. head = &musb->control;
  1829. hw_ep = musb->control_ep;
  1830. goto success;
  1831. }
  1832. /* else, periodic transfers get muxed to other endpoints */
  1833. /*
  1834. * We know this qh hasn't been scheduled, so all we need to do
  1835. * is choose which hardware endpoint to put it on ...
  1836. *
  1837. * REVISIT what we really want here is a regular schedule tree
  1838. * like e.g. OHCI uses.
  1839. */
  1840. best_diff = 4096;
  1841. best_end = -1;
  1842. for (epnum = 1, hw_ep = musb->endpoints + 1;
  1843. epnum < musb->nr_endpoints;
  1844. epnum++, hw_ep++) {
  1845. int diff;
  1846. if (musb_ep_get_qh(hw_ep, is_in) != NULL)
  1847. continue;
  1848. if (hw_ep == musb->bulk_ep)
  1849. continue;
  1850. if (is_in)
  1851. diff = hw_ep->max_packet_sz_rx;
  1852. else
  1853. diff = hw_ep->max_packet_sz_tx;
  1854. diff -= (qh->maxpacket * qh->hb_mult);
  1855. if (diff >= 0 && best_diff > diff) {
  1856. /*
  1857. * Mentor controller has a bug in that if we schedule
  1858. * a BULK Tx transfer on an endpoint that had earlier
  1859. * handled ISOC then the BULK transfer has to start on
  1860. * a zero toggle. If the BULK transfer starts on a 1
  1861. * toggle then this transfer will fail as the mentor
  1862. * controller starts the Bulk transfer on a 0 toggle
  1863. * irrespective of the programming of the toggle bits
  1864. * in the TXCSR register. Check for this condition
  1865. * while allocating the EP for a Tx Bulk transfer. If
  1866. * so skip this EP.
  1867. */
  1868. hw_ep = musb->endpoints + epnum;
  1869. toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
  1870. txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
  1871. >> 4) & 0x3;
  1872. if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
  1873. toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
  1874. continue;
  1875. best_diff = diff;
  1876. best_end = epnum;
  1877. }
  1878. }
  1879. /* use bulk reserved ep1 if no other ep is free */
  1880. if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
  1881. hw_ep = musb->bulk_ep;
  1882. if (is_in)
  1883. head = &musb->in_bulk;
  1884. else
  1885. head = &musb->out_bulk;
  1886. /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
  1887. * multiplexed. This scheme does not work in high speed to full
  1888. * speed scenario as NAK interrupts are not coming from a
  1889. * full speed device connected to a high speed device.
  1890. * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
  1891. * 4 (8 frame or 8ms) for FS device.
  1892. */
  1893. if (qh->dev)
  1894. qh->intv_reg =
  1895. (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
  1896. goto success;
  1897. } else if (best_end < 0) {
  1898. return -ENOSPC;
  1899. }
  1900. idle = 1;
  1901. qh->mux = 0;
  1902. hw_ep = musb->endpoints + best_end;
  1903. dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
  1904. success:
  1905. if (head) {
  1906. idle = list_empty(head);
  1907. list_add_tail(&qh->ring, head);
  1908. qh->mux = 1;
  1909. }
  1910. qh->hw_ep = hw_ep;
  1911. qh->hep->hcpriv = qh;
  1912. if (idle)
  1913. musb_start_urb(musb, is_in, qh);
  1914. return 0;
  1915. }
  1916. static int musb_urb_enqueue(
  1917. struct usb_hcd *hcd,
  1918. struct urb *urb,
  1919. gfp_t mem_flags)
  1920. {
  1921. unsigned long flags;
  1922. struct musb *musb = hcd_to_musb(hcd);
  1923. struct usb_host_endpoint *hep = urb->ep;
  1924. struct musb_qh *qh;
  1925. struct usb_endpoint_descriptor *epd = &hep->desc;
  1926. int ret;
  1927. unsigned type_reg;
  1928. unsigned interval;
  1929. /* host role must be active */
  1930. if (!is_host_active(musb) || !musb->is_active)
  1931. return -ENODEV;
  1932. spin_lock_irqsave(&musb->lock, flags);
  1933. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1934. qh = ret ? NULL : hep->hcpriv;
  1935. if (qh)
  1936. urb->hcpriv = qh;
  1937. spin_unlock_irqrestore(&musb->lock, flags);
  1938. /* DMA mapping was already done, if needed, and this urb is on
  1939. * hep->urb_list now ... so we're done, unless hep wasn't yet
  1940. * scheduled onto a live qh.
  1941. *
  1942. * REVISIT best to keep hep->hcpriv valid until the endpoint gets
  1943. * disabled, testing for empty qh->ring and avoiding qh setup costs
  1944. * except for the first urb queued after a config change.
  1945. */
  1946. if (qh || ret)
  1947. return ret;
  1948. /* Allocate and initialize qh, minimizing the work done each time
  1949. * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
  1950. *
  1951. * REVISIT consider a dedicated qh kmem_cache, so it's harder
  1952. * for bugs in other kernel code to break this driver...
  1953. */
  1954. qh = kzalloc(sizeof *qh, mem_flags);
  1955. if (!qh) {
  1956. spin_lock_irqsave(&musb->lock, flags);
  1957. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1958. spin_unlock_irqrestore(&musb->lock, flags);
  1959. return -ENOMEM;
  1960. }
  1961. qh->hep = hep;
  1962. qh->dev = urb->dev;
  1963. INIT_LIST_HEAD(&qh->ring);
  1964. qh->is_ready = 1;
  1965. qh->maxpacket = usb_endpoint_maxp(epd);
  1966. qh->type = usb_endpoint_type(epd);
  1967. /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
  1968. * Some musb cores don't support high bandwidth ISO transfers; and
  1969. * we don't (yet!) support high bandwidth interrupt transfers.
  1970. */
  1971. qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
  1972. if (qh->hb_mult > 1) {
  1973. int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
  1974. if (ok)
  1975. ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
  1976. || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
  1977. if (!ok) {
  1978. ret = -EMSGSIZE;
  1979. goto done;
  1980. }
  1981. qh->maxpacket &= 0x7ff;
  1982. }
  1983. qh->epnum = usb_endpoint_num(epd);
  1984. /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
  1985. qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
  1986. /* precompute rxtype/txtype/type0 register */
  1987. type_reg = (qh->type << 4) | qh->epnum;
  1988. switch (urb->dev->speed) {
  1989. case USB_SPEED_LOW:
  1990. type_reg |= 0xc0;
  1991. break;
  1992. case USB_SPEED_FULL:
  1993. type_reg |= 0x80;
  1994. break;
  1995. default:
  1996. type_reg |= 0x40;
  1997. }
  1998. qh->type_reg = type_reg;
  1999. /* Precompute RXINTERVAL/TXINTERVAL register */
  2000. switch (qh->type) {
  2001. case USB_ENDPOINT_XFER_INT:
  2002. /*
  2003. * Full/low speeds use the linear encoding,
  2004. * high speed uses the logarithmic encoding.
  2005. */
  2006. if (urb->dev->speed <= USB_SPEED_FULL) {
  2007. interval = max_t(u8, epd->bInterval, 1);
  2008. break;
  2009. }
  2010. /* FALLTHROUGH */
  2011. case USB_ENDPOINT_XFER_ISOC:
  2012. /* ISO always uses logarithmic encoding */
  2013. interval = min_t(u8, epd->bInterval, 16);
  2014. break;
  2015. default:
  2016. /* REVISIT we actually want to use NAK limits, hinting to the
  2017. * transfer scheduling logic to try some other qh, e.g. try
  2018. * for 2 msec first:
  2019. *
  2020. * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
  2021. *
  2022. * The downside of disabling this is that transfer scheduling
  2023. * gets VERY unfair for nonperiodic transfers; a misbehaving
  2024. * peripheral could make that hurt. That's perfectly normal
  2025. * for reads from network or serial adapters ... so we have
  2026. * partial NAKlimit support for bulk RX.
  2027. *
  2028. * The upside of disabling it is simpler transfer scheduling.
  2029. */
  2030. interval = 0;
  2031. }
  2032. qh->intv_reg = interval;
  2033. /* precompute addressing for external hub/tt ports */
  2034. if (musb->is_multipoint) {
  2035. struct usb_device *parent = urb->dev->parent;
  2036. if (parent != hcd->self.root_hub) {
  2037. qh->h_addr_reg = (u8) parent->devnum;
  2038. /* set up tt info if needed */
  2039. if (urb->dev->tt) {
  2040. qh->h_port_reg = (u8) urb->dev->ttport;
  2041. if (urb->dev->tt->hub)
  2042. qh->h_addr_reg =
  2043. (u8) urb->dev->tt->hub->devnum;
  2044. if (urb->dev->tt->multi)
  2045. qh->h_addr_reg |= 0x80;
  2046. }
  2047. }
  2048. }
  2049. /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
  2050. * until we get real dma queues (with an entry for each urb/buffer),
  2051. * we only have work to do in the former case.
  2052. */
  2053. spin_lock_irqsave(&musb->lock, flags);
  2054. if (hep->hcpriv || !next_urb(qh)) {
  2055. /* some concurrent activity submitted another urb to hep...
  2056. * odd, rare, error prone, but legal.
  2057. */
  2058. kfree(qh);
  2059. qh = NULL;
  2060. ret = 0;
  2061. } else
  2062. ret = musb_schedule(musb, qh,
  2063. epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
  2064. if (ret == 0) {
  2065. urb->hcpriv = qh;
  2066. /* FIXME set urb->start_frame for iso/intr, it's tested in
  2067. * musb_start_urb(), but otherwise only konicawc cares ...
  2068. */
  2069. }
  2070. spin_unlock_irqrestore(&musb->lock, flags);
  2071. done:
  2072. if (ret != 0) {
  2073. spin_lock_irqsave(&musb->lock, flags);
  2074. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2075. spin_unlock_irqrestore(&musb->lock, flags);
  2076. kfree(qh);
  2077. }
  2078. return ret;
  2079. }
  2080. /*
  2081. * abort a transfer that's at the head of a hardware queue.
  2082. * called with controller locked, irqs blocked
  2083. * that hardware queue advances to the next transfer, unless prevented
  2084. */
  2085. static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
  2086. {
  2087. struct musb_hw_ep *ep = qh->hw_ep;
  2088. struct musb *musb = ep->musb;
  2089. void __iomem *epio = ep->regs;
  2090. unsigned hw_end = ep->epnum;
  2091. void __iomem *regs = ep->musb->mregs;
  2092. int is_in = usb_pipein(urb->pipe);
  2093. int status = 0;
  2094. u16 csr;
  2095. musb_ep_select(regs, hw_end);
  2096. if (is_dma_capable()) {
  2097. struct dma_channel *dma;
  2098. dma = is_in ? ep->rx_channel : ep->tx_channel;
  2099. if (dma) {
  2100. status = ep->musb->dma_controller->channel_abort(dma);
  2101. dev_dbg(musb->controller,
  2102. "abort %cX%d DMA for urb %p --> %d\n",
  2103. is_in ? 'R' : 'T', ep->epnum,
  2104. urb, status);
  2105. urb->actual_length += dma->actual_len;
  2106. }
  2107. }
  2108. /* turn off DMA requests, discard state, stop polling ... */
  2109. if (ep->epnum && is_in) {
  2110. /* giveback saves bulk toggle */
  2111. csr = musb_h_flush_rxfifo(ep, 0);
  2112. /* REVISIT we still get an irq; should likely clear the
  2113. * endpoint's irq status here to avoid bogus irqs.
  2114. * clearing that status is platform-specific...
  2115. */
  2116. } else if (ep->epnum) {
  2117. musb_h_tx_flush_fifo(ep);
  2118. csr = musb_readw(epio, MUSB_TXCSR);
  2119. csr &= ~(MUSB_TXCSR_AUTOSET
  2120. | MUSB_TXCSR_DMAENAB
  2121. | MUSB_TXCSR_H_RXSTALL
  2122. | MUSB_TXCSR_H_NAKTIMEOUT
  2123. | MUSB_TXCSR_H_ERROR
  2124. | MUSB_TXCSR_TXPKTRDY);
  2125. musb_writew(epio, MUSB_TXCSR, csr);
  2126. /* REVISIT may need to clear FLUSHFIFO ... */
  2127. musb_writew(epio, MUSB_TXCSR, csr);
  2128. /* flush cpu writebuffer */
  2129. csr = musb_readw(epio, MUSB_TXCSR);
  2130. } else {
  2131. musb_h_ep0_flush_fifo(ep);
  2132. }
  2133. if (status == 0)
  2134. musb_advance_schedule(ep->musb, urb, ep, is_in);
  2135. return status;
  2136. }
  2137. static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2138. {
  2139. struct musb *musb = hcd_to_musb(hcd);
  2140. struct musb_qh *qh;
  2141. unsigned long flags;
  2142. int is_in = usb_pipein(urb->pipe);
  2143. int ret;
  2144. dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
  2145. usb_pipedevice(urb->pipe),
  2146. usb_pipeendpoint(urb->pipe),
  2147. is_in ? "in" : "out");
  2148. spin_lock_irqsave(&musb->lock, flags);
  2149. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  2150. if (ret)
  2151. goto done;
  2152. qh = urb->hcpriv;
  2153. if (!qh)
  2154. goto done;
  2155. /*
  2156. * Any URB not actively programmed into endpoint hardware can be
  2157. * immediately given back; that's any URB not at the head of an
  2158. * endpoint queue, unless someday we get real DMA queues. And even
  2159. * if it's at the head, it might not be known to the hardware...
  2160. *
  2161. * Otherwise abort current transfer, pending DMA, etc.; urb->status
  2162. * has already been updated. This is a synchronous abort; it'd be
  2163. * OK to hold off until after some IRQ, though.
  2164. *
  2165. * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
  2166. */
  2167. if (!qh->is_ready
  2168. || urb->urb_list.prev != &qh->hep->urb_list
  2169. || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
  2170. int ready = qh->is_ready;
  2171. qh->is_ready = 0;
  2172. musb_giveback(musb, urb, 0);
  2173. qh->is_ready = ready;
  2174. /* If nothing else (usually musb_giveback) is using it
  2175. * and its URB list has emptied, recycle this qh.
  2176. */
  2177. if (ready && list_empty(&qh->hep->urb_list)) {
  2178. qh->hep->hcpriv = NULL;
  2179. list_del(&qh->ring);
  2180. kfree(qh);
  2181. }
  2182. } else
  2183. ret = musb_cleanup_urb(urb, qh);
  2184. done:
  2185. spin_unlock_irqrestore(&musb->lock, flags);
  2186. return ret;
  2187. }
  2188. /* disable an endpoint */
  2189. static void
  2190. musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
  2191. {
  2192. u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
  2193. unsigned long flags;
  2194. struct musb *musb = hcd_to_musb(hcd);
  2195. struct musb_qh *qh;
  2196. struct urb *urb;
  2197. spin_lock_irqsave(&musb->lock, flags);
  2198. qh = hep->hcpriv;
  2199. if (qh == NULL)
  2200. goto exit;
  2201. /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
  2202. /* Kick the first URB off the hardware, if needed */
  2203. qh->is_ready = 0;
  2204. if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
  2205. urb = next_urb(qh);
  2206. /* make software (then hardware) stop ASAP */
  2207. if (!urb->unlinked)
  2208. urb->status = -ESHUTDOWN;
  2209. /* cleanup */
  2210. musb_cleanup_urb(urb, qh);
  2211. /* Then nuke all the others ... and advance the
  2212. * queue on hw_ep (e.g. bulk ring) when we're done.
  2213. */
  2214. while (!list_empty(&hep->urb_list)) {
  2215. urb = next_urb(qh);
  2216. urb->status = -ESHUTDOWN;
  2217. musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
  2218. }
  2219. } else {
  2220. /* Just empty the queue; the hardware is busy with
  2221. * other transfers, and since !qh->is_ready nothing
  2222. * will activate any of these as it advances.
  2223. */
  2224. while (!list_empty(&hep->urb_list))
  2225. musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
  2226. hep->hcpriv = NULL;
  2227. list_del(&qh->ring);
  2228. kfree(qh);
  2229. }
  2230. exit:
  2231. spin_unlock_irqrestore(&musb->lock, flags);
  2232. }
  2233. static int musb_h_get_frame_number(struct usb_hcd *hcd)
  2234. {
  2235. struct musb *musb = hcd_to_musb(hcd);
  2236. return musb_readw(musb->mregs, MUSB_FRAME);
  2237. }
  2238. static int musb_h_start(struct usb_hcd *hcd)
  2239. {
  2240. struct musb *musb = hcd_to_musb(hcd);
  2241. /* NOTE: musb_start() is called when the hub driver turns
  2242. * on port power, or when (OTG) peripheral starts.
  2243. */
  2244. hcd->state = HC_STATE_RUNNING;
  2245. musb->port1_status = 0;
  2246. return 0;
  2247. }
  2248. static void musb_h_stop(struct usb_hcd *hcd)
  2249. {
  2250. musb_stop(hcd_to_musb(hcd));
  2251. hcd->state = HC_STATE_HALT;
  2252. }
  2253. static int musb_bus_suspend(struct usb_hcd *hcd)
  2254. {
  2255. struct musb *musb = hcd_to_musb(hcd);
  2256. u8 devctl;
  2257. musb_port_suspend(musb, true);
  2258. if (!is_host_active(musb))
  2259. return 0;
  2260. switch (musb->xceiv->otg->state) {
  2261. case OTG_STATE_A_SUSPEND:
  2262. return 0;
  2263. case OTG_STATE_A_WAIT_VRISE:
  2264. /* ID could be grounded even if there's no device
  2265. * on the other end of the cable. NOTE that the
  2266. * A_WAIT_VRISE timers are messy with MUSB...
  2267. */
  2268. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2269. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  2270. musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
  2271. break;
  2272. default:
  2273. break;
  2274. }
  2275. if (musb->is_active) {
  2276. WARNING("trying to suspend as %s while active\n",
  2277. usb_otg_state_string(musb->xceiv->otg->state));
  2278. return -EBUSY;
  2279. } else
  2280. return 0;
  2281. }
  2282. static int musb_bus_resume(struct usb_hcd *hcd)
  2283. {
  2284. struct musb *musb = hcd_to_musb(hcd);
  2285. if (musb->config &&
  2286. musb->config->host_port_deassert_reset_at_resume)
  2287. musb_port_reset(musb, false);
  2288. return 0;
  2289. }
  2290. #ifndef CONFIG_MUSB_PIO_ONLY
  2291. #define MUSB_USB_DMA_ALIGN 4
  2292. struct musb_temp_buffer {
  2293. void *kmalloc_ptr;
  2294. void *old_xfer_buffer;
  2295. u8 data[0];
  2296. };
  2297. static void musb_free_temp_buffer(struct urb *urb)
  2298. {
  2299. enum dma_data_direction dir;
  2300. struct musb_temp_buffer *temp;
  2301. size_t length;
  2302. if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
  2303. return;
  2304. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2305. temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
  2306. data);
  2307. if (dir == DMA_FROM_DEVICE) {
  2308. if (usb_pipeisoc(urb->pipe))
  2309. length = urb->transfer_buffer_length;
  2310. else
  2311. length = urb->actual_length;
  2312. memcpy(temp->old_xfer_buffer, temp->data, length);
  2313. }
  2314. urb->transfer_buffer = temp->old_xfer_buffer;
  2315. kfree(temp->kmalloc_ptr);
  2316. urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
  2317. }
  2318. static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
  2319. {
  2320. enum dma_data_direction dir;
  2321. struct musb_temp_buffer *temp;
  2322. void *kmalloc_ptr;
  2323. size_t kmalloc_size;
  2324. if (urb->num_sgs || urb->sg ||
  2325. urb->transfer_buffer_length == 0 ||
  2326. !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
  2327. return 0;
  2328. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  2329. /* Allocate a buffer with enough padding for alignment */
  2330. kmalloc_size = urb->transfer_buffer_length +
  2331. sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
  2332. kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
  2333. if (!kmalloc_ptr)
  2334. return -ENOMEM;
  2335. /* Position our struct temp_buffer such that data is aligned */
  2336. temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
  2337. temp->kmalloc_ptr = kmalloc_ptr;
  2338. temp->old_xfer_buffer = urb->transfer_buffer;
  2339. if (dir == DMA_TO_DEVICE)
  2340. memcpy(temp->data, urb->transfer_buffer,
  2341. urb->transfer_buffer_length);
  2342. urb->transfer_buffer = temp->data;
  2343. urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
  2344. return 0;
  2345. }
  2346. static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  2347. gfp_t mem_flags)
  2348. {
  2349. struct musb *musb = hcd_to_musb(hcd);
  2350. int ret;
  2351. /*
  2352. * The DMA engine in RTL1.8 and above cannot handle
  2353. * DMA addresses that are not aligned to a 4 byte boundary.
  2354. * For such engine implemented (un)map_urb_for_dma hooks.
  2355. * Do not use these hooks for RTL<1.8
  2356. */
  2357. if (musb->hwvers < MUSB_HWVERS_1800)
  2358. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2359. ret = musb_alloc_temp_buffer(urb, mem_flags);
  2360. if (ret)
  2361. return ret;
  2362. ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  2363. if (ret)
  2364. musb_free_temp_buffer(urb);
  2365. return ret;
  2366. }
  2367. static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  2368. {
  2369. struct musb *musb = hcd_to_musb(hcd);
  2370. usb_hcd_unmap_urb_for_dma(hcd, urb);
  2371. /* Do not use this hook for RTL<1.8 (see description above) */
  2372. if (musb->hwvers < MUSB_HWVERS_1800)
  2373. return;
  2374. musb_free_temp_buffer(urb);
  2375. }
  2376. #endif /* !CONFIG_MUSB_PIO_ONLY */
  2377. static const struct hc_driver musb_hc_driver = {
  2378. .description = "musb-hcd",
  2379. .product_desc = "MUSB HDRC host driver",
  2380. .hcd_priv_size = sizeof(struct musb *),
  2381. .flags = HCD_USB2 | HCD_MEMORY | HCD_BH,
  2382. /* not using irq handler or reset hooks from usbcore, since
  2383. * those must be shared with peripheral code for OTG configs
  2384. */
  2385. .start = musb_h_start,
  2386. .stop = musb_h_stop,
  2387. .get_frame_number = musb_h_get_frame_number,
  2388. .urb_enqueue = musb_urb_enqueue,
  2389. .urb_dequeue = musb_urb_dequeue,
  2390. .endpoint_disable = musb_h_disable,
  2391. #ifndef CONFIG_MUSB_PIO_ONLY
  2392. .map_urb_for_dma = musb_map_urb_for_dma,
  2393. .unmap_urb_for_dma = musb_unmap_urb_for_dma,
  2394. #endif
  2395. .hub_status_data = musb_hub_status_data,
  2396. .hub_control = musb_hub_control,
  2397. .bus_suspend = musb_bus_suspend,
  2398. .bus_resume = musb_bus_resume,
  2399. /* .start_port_reset = NULL, */
  2400. /* .hub_irq_enable = NULL, */
  2401. };
  2402. int musb_host_alloc(struct musb *musb)
  2403. {
  2404. struct device *dev = musb->controller;
  2405. /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
  2406. musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
  2407. if (!musb->hcd)
  2408. return -EINVAL;
  2409. *musb->hcd->hcd_priv = (unsigned long) musb;
  2410. musb->hcd->self.uses_pio_for_control = 1;
  2411. musb->hcd->uses_new_polling = 1;
  2412. musb->hcd->has_tt = 1;
  2413. return 0;
  2414. }
  2415. void musb_host_cleanup(struct musb *musb)
  2416. {
  2417. if (musb->port_mode == MUSB_PORT_MODE_GADGET)
  2418. return;
  2419. usb_remove_hcd(musb->hcd);
  2420. }
  2421. void musb_host_free(struct musb *musb)
  2422. {
  2423. usb_put_hcd(musb->hcd);
  2424. }
  2425. int musb_host_setup(struct musb *musb, int power_budget)
  2426. {
  2427. int ret;
  2428. struct usb_hcd *hcd = musb->hcd;
  2429. MUSB_HST_MODE(musb);
  2430. musb->xceiv->otg->default_a = 1;
  2431. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  2432. otg_set_host(musb->xceiv->otg, &hcd->self);
  2433. hcd->self.otg_port = 1;
  2434. musb->xceiv->otg->host = &hcd->self;
  2435. hcd->power_budget = 2 * (power_budget ? : 250);
  2436. ret = usb_add_hcd(hcd, 0, 0);
  2437. if (ret < 0)
  2438. return ret;
  2439. device_wakeup_enable(hcd->self.controller);
  2440. return 0;
  2441. }
  2442. void musb_host_resume_root_hub(struct musb *musb)
  2443. {
  2444. usb_hcd_resume_root_hub(musb->hcd);
  2445. }
  2446. void musb_host_poke_root_hub(struct musb *musb)
  2447. {
  2448. MUSB_HST_MODE(musb);
  2449. if (musb->hcd->status_urb)
  2450. usb_hcd_poll_rh_status(musb->hcd);
  2451. else
  2452. usb_hcd_resume_root_hub(musb->hcd);
  2453. }