musb_host.c 64 KB

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