musb_host.c 71 KB

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