musb_host.c 63 KB

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