musb_host.c 62 KB

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