musb_host.c 64 KB

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