musb_host.c 62 KB

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