musb_host.c 68 KB

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