musb_host.c 64 KB

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