musb_host.c 63 KB

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