musb_host.c 65 KB

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