musb_gadget.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * MUSB OTG driver peripheral 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) 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/kernel.h>
  36. #include <linux/list.h>
  37. #include <linux/timer.h>
  38. #include <linux/module.h>
  39. #include <linux/smp.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/delay.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/stat.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/slab.h>
  46. #include "musb_core.h"
  47. /* MUSB PERIPHERAL status 3-mar-2006:
  48. *
  49. * - EP0 seems solid. It passes both USBCV and usbtest control cases.
  50. * Minor glitches:
  51. *
  52. * + remote wakeup to Linux hosts work, but saw USBCV failures;
  53. * in one test run (operator error?)
  54. * + endpoint halt tests -- in both usbtest and usbcv -- seem
  55. * to break when dma is enabled ... is something wrongly
  56. * clearing SENDSTALL?
  57. *
  58. * - Mass storage behaved ok when last tested. Network traffic patterns
  59. * (with lots of short transfers etc) need retesting; they turn up the
  60. * worst cases of the DMA, since short packets are typical but are not
  61. * required.
  62. *
  63. * - TX/IN
  64. * + both pio and dma behave in with network and g_zero tests
  65. * + no cppi throughput issues other than no-hw-queueing
  66. * + failed with FLAT_REG (DaVinci)
  67. * + seems to behave with double buffering, PIO -and- CPPI
  68. * + with gadgetfs + AIO, requests got lost?
  69. *
  70. * - RX/OUT
  71. * + both pio and dma behave in with network and g_zero tests
  72. * + dma is slow in typical case (short_not_ok is clear)
  73. * + double buffering ok with PIO
  74. * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
  75. * + request lossage observed with gadgetfs
  76. *
  77. * - ISO not tested ... might work, but only weakly isochronous
  78. *
  79. * - Gadget driver disabling of softconnect during bind() is ignored; so
  80. * drivers can't hold off host requests until userspace is ready.
  81. * (Workaround: they can turn it off later.)
  82. *
  83. * - PORTABILITY (assumes PIO works):
  84. * + DaVinci, basically works with cppi dma
  85. * + OMAP 2430, ditto with mentor dma
  86. * + TUSB 6010, platform-specific dma in the works
  87. */
  88. /* ----------------------------------------------------------------------- */
  89. /*
  90. * Immediately complete a request.
  91. *
  92. * @param request the request to complete
  93. * @param status the status to complete the request with
  94. * Context: controller locked, IRQs blocked.
  95. */
  96. void musb_g_giveback(
  97. struct musb_ep *ep,
  98. struct usb_request *request,
  99. int status)
  100. __releases(ep->musb->lock)
  101. __acquires(ep->musb->lock)
  102. {
  103. struct musb_request *req;
  104. struct musb *musb;
  105. int busy = ep->busy;
  106. req = to_musb_request(request);
  107. list_del(&request->list);
  108. if (req->request.status == -EINPROGRESS)
  109. req->request.status = status;
  110. musb = req->musb;
  111. ep->busy = 1;
  112. spin_unlock(&musb->lock);
  113. if (is_dma_capable()) {
  114. if (req->mapped) {
  115. dma_unmap_single(musb->controller,
  116. req->request.dma,
  117. req->request.length,
  118. req->tx
  119. ? DMA_TO_DEVICE
  120. : DMA_FROM_DEVICE);
  121. req->request.dma = DMA_ADDR_INVALID;
  122. req->mapped = 0;
  123. } else if (req->request.dma != DMA_ADDR_INVALID)
  124. dma_sync_single_for_cpu(musb->controller,
  125. req->request.dma,
  126. req->request.length,
  127. req->tx
  128. ? DMA_TO_DEVICE
  129. : DMA_FROM_DEVICE);
  130. }
  131. if (request->status == 0)
  132. DBG(5, "%s done request %p, %d/%d\n",
  133. ep->end_point.name, request,
  134. req->request.actual, req->request.length);
  135. else
  136. DBG(2, "%s request %p, %d/%d fault %d\n",
  137. ep->end_point.name, request,
  138. req->request.actual, req->request.length,
  139. request->status);
  140. req->request.complete(&req->ep->end_point, &req->request);
  141. spin_lock(&musb->lock);
  142. ep->busy = busy;
  143. }
  144. /* ----------------------------------------------------------------------- */
  145. /*
  146. * Abort requests queued to an endpoint using the status. Synchronous.
  147. * caller locked controller and blocked irqs, and selected this ep.
  148. */
  149. static void nuke(struct musb_ep *ep, const int status)
  150. {
  151. struct musb_request *req = NULL;
  152. void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
  153. ep->busy = 1;
  154. if (is_dma_capable() && ep->dma) {
  155. struct dma_controller *c = ep->musb->dma_controller;
  156. int value;
  157. if (ep->is_in) {
  158. /*
  159. * The programming guide says that we must not clear
  160. * the DMAMODE bit before DMAENAB, so we only
  161. * clear it in the second write...
  162. */
  163. musb_writew(epio, MUSB_TXCSR,
  164. MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
  165. musb_writew(epio, MUSB_TXCSR,
  166. 0 | MUSB_TXCSR_FLUSHFIFO);
  167. } else {
  168. musb_writew(epio, MUSB_RXCSR,
  169. 0 | MUSB_RXCSR_FLUSHFIFO);
  170. musb_writew(epio, MUSB_RXCSR,
  171. 0 | MUSB_RXCSR_FLUSHFIFO);
  172. }
  173. value = c->channel_abort(ep->dma);
  174. DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value);
  175. c->channel_release(ep->dma);
  176. ep->dma = NULL;
  177. }
  178. while (!list_empty(&(ep->req_list))) {
  179. req = container_of(ep->req_list.next, struct musb_request,
  180. request.list);
  181. musb_g_giveback(ep, &req->request, status);
  182. }
  183. }
  184. /* ----------------------------------------------------------------------- */
  185. /* Data transfers - pure PIO, pure DMA, or mixed mode */
  186. /*
  187. * This assumes the separate CPPI engine is responding to DMA requests
  188. * from the usb core ... sequenced a bit differently from mentor dma.
  189. */
  190. static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
  191. {
  192. if (can_bulk_split(musb, ep->type))
  193. return ep->hw_ep->max_packet_sz_tx;
  194. else
  195. return ep->packet_sz;
  196. }
  197. #ifdef CONFIG_USB_INVENTRA_DMA
  198. /* Peripheral tx (IN) using Mentor DMA works as follows:
  199. Only mode 0 is used for transfers <= wPktSize,
  200. mode 1 is used for larger transfers,
  201. One of the following happens:
  202. - Host sends IN token which causes an endpoint interrupt
  203. -> TxAvail
  204. -> if DMA is currently busy, exit.
  205. -> if queue is non-empty, txstate().
  206. - Request is queued by the gadget driver.
  207. -> if queue was previously empty, txstate()
  208. txstate()
  209. -> start
  210. /\ -> setup DMA
  211. | (data is transferred to the FIFO, then sent out when
  212. | IN token(s) are recd from Host.
  213. | -> DMA interrupt on completion
  214. | calls TxAvail.
  215. | -> stop DMA, ~DMAENAB,
  216. | -> set TxPktRdy for last short pkt or zlp
  217. | -> Complete Request
  218. | -> Continue next request (call txstate)
  219. |___________________________________|
  220. * Non-Mentor DMA engines can of course work differently, such as by
  221. * upleveling from irq-per-packet to irq-per-buffer.
  222. */
  223. #endif
  224. /*
  225. * An endpoint is transmitting data. This can be called either from
  226. * the IRQ routine or from ep.queue() to kickstart a request on an
  227. * endpoint.
  228. *
  229. * Context: controller locked, IRQs blocked, endpoint selected
  230. */
  231. static void txstate(struct musb *musb, struct musb_request *req)
  232. {
  233. u8 epnum = req->epnum;
  234. struct musb_ep *musb_ep;
  235. void __iomem *epio = musb->endpoints[epnum].regs;
  236. struct usb_request *request;
  237. u16 fifo_count = 0, csr;
  238. int use_dma = 0;
  239. musb_ep = req->ep;
  240. /* we shouldn't get here while DMA is active ... but we do ... */
  241. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  242. DBG(4, "dma pending...\n");
  243. return;
  244. }
  245. /* read TXCSR before */
  246. csr = musb_readw(epio, MUSB_TXCSR);
  247. request = &req->request;
  248. fifo_count = min(max_ep_writesize(musb, musb_ep),
  249. (int)(request->length - request->actual));
  250. if (csr & MUSB_TXCSR_TXPKTRDY) {
  251. DBG(5, "%s old packet still ready , txcsr %03x\n",
  252. musb_ep->end_point.name, csr);
  253. return;
  254. }
  255. if (csr & MUSB_TXCSR_P_SENDSTALL) {
  256. DBG(5, "%s stalling, txcsr %03x\n",
  257. musb_ep->end_point.name, csr);
  258. return;
  259. }
  260. DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
  261. epnum, musb_ep->packet_sz, fifo_count,
  262. csr);
  263. #ifndef CONFIG_MUSB_PIO_ONLY
  264. if (is_dma_capable() && musb_ep->dma) {
  265. struct dma_controller *c = musb->dma_controller;
  266. use_dma = (request->dma != DMA_ADDR_INVALID);
  267. /* MUSB_TXCSR_P_ISO is still set correctly */
  268. #ifdef CONFIG_USB_INVENTRA_DMA
  269. {
  270. size_t request_size;
  271. /* setup DMA, then program endpoint CSR */
  272. request_size = min_t(size_t, request->length,
  273. musb_ep->dma->max_len);
  274. if (request_size < musb_ep->packet_sz)
  275. musb_ep->dma->desired_mode = 0;
  276. else
  277. musb_ep->dma->desired_mode = 1;
  278. use_dma = use_dma && c->channel_program(
  279. musb_ep->dma, musb_ep->packet_sz,
  280. musb_ep->dma->desired_mode,
  281. request->dma + request->actual, request_size);
  282. if (use_dma) {
  283. if (musb_ep->dma->desired_mode == 0) {
  284. /*
  285. * We must not clear the DMAMODE bit
  286. * before the DMAENAB bit -- and the
  287. * latter doesn't always get cleared
  288. * before we get here...
  289. */
  290. csr &= ~(MUSB_TXCSR_AUTOSET
  291. | MUSB_TXCSR_DMAENAB);
  292. musb_writew(epio, MUSB_TXCSR, csr
  293. | MUSB_TXCSR_P_WZC_BITS);
  294. csr &= ~MUSB_TXCSR_DMAMODE;
  295. csr |= (MUSB_TXCSR_DMAENAB |
  296. MUSB_TXCSR_MODE);
  297. /* against programming guide */
  298. } else
  299. csr |= (MUSB_TXCSR_AUTOSET
  300. | MUSB_TXCSR_DMAENAB
  301. | MUSB_TXCSR_DMAMODE
  302. | MUSB_TXCSR_MODE);
  303. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  304. musb_writew(epio, MUSB_TXCSR, csr);
  305. }
  306. }
  307. #elif defined(CONFIG_USB_TI_CPPI_DMA)
  308. /* program endpoint CSR first, then setup DMA */
  309. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  310. csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
  311. MUSB_TXCSR_MODE;
  312. musb_writew(epio, MUSB_TXCSR,
  313. (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
  314. | csr);
  315. /* ensure writebuffer is empty */
  316. csr = musb_readw(epio, MUSB_TXCSR);
  317. /* NOTE host side sets DMAENAB later than this; both are
  318. * OK since the transfer dma glue (between CPPI and Mentor
  319. * fifos) just tells CPPI it could start. Data only moves
  320. * to the USB TX fifo when both fifos are ready.
  321. */
  322. /* "mode" is irrelevant here; handle terminating ZLPs like
  323. * PIO does, since the hardware RNDIS mode seems unreliable
  324. * except for the last-packet-is-already-short case.
  325. */
  326. use_dma = use_dma && c->channel_program(
  327. musb_ep->dma, musb_ep->packet_sz,
  328. 0,
  329. request->dma,
  330. request->length);
  331. if (!use_dma) {
  332. c->channel_release(musb_ep->dma);
  333. musb_ep->dma = NULL;
  334. csr &= ~MUSB_TXCSR_DMAENAB;
  335. musb_writew(epio, MUSB_TXCSR, csr);
  336. /* invariant: prequest->buf is non-null */
  337. }
  338. #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
  339. use_dma = use_dma && c->channel_program(
  340. musb_ep->dma, musb_ep->packet_sz,
  341. request->zero,
  342. request->dma,
  343. request->length);
  344. #endif
  345. }
  346. #endif
  347. if (!use_dma) {
  348. musb_write_fifo(musb_ep->hw_ep, fifo_count,
  349. (u8 *) (request->buf + request->actual));
  350. request->actual += fifo_count;
  351. csr |= MUSB_TXCSR_TXPKTRDY;
  352. csr &= ~MUSB_TXCSR_P_UNDERRUN;
  353. musb_writew(epio, MUSB_TXCSR, csr);
  354. }
  355. /* host may already have the data when this message shows... */
  356. DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
  357. musb_ep->end_point.name, use_dma ? "dma" : "pio",
  358. request->actual, request->length,
  359. musb_readw(epio, MUSB_TXCSR),
  360. fifo_count,
  361. musb_readw(epio, MUSB_TXMAXP));
  362. }
  363. /*
  364. * FIFO state update (e.g. data ready).
  365. * Called from IRQ, with controller locked.
  366. */
  367. void musb_g_tx(struct musb *musb, u8 epnum)
  368. {
  369. u16 csr;
  370. struct usb_request *request;
  371. u8 __iomem *mbase = musb->mregs;
  372. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
  373. void __iomem *epio = musb->endpoints[epnum].regs;
  374. struct dma_channel *dma;
  375. musb_ep_select(mbase, epnum);
  376. request = next_request(musb_ep);
  377. csr = musb_readw(epio, MUSB_TXCSR);
  378. DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
  379. dma = is_dma_capable() ? musb_ep->dma : NULL;
  380. /*
  381. * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
  382. * probably rates reporting as a host error.
  383. */
  384. if (csr & MUSB_TXCSR_P_SENTSTALL) {
  385. csr |= MUSB_TXCSR_P_WZC_BITS;
  386. csr &= ~MUSB_TXCSR_P_SENTSTALL;
  387. musb_writew(epio, MUSB_TXCSR, csr);
  388. return;
  389. }
  390. if (csr & MUSB_TXCSR_P_UNDERRUN) {
  391. /* We NAKed, no big deal... little reason to care. */
  392. csr |= MUSB_TXCSR_P_WZC_BITS;
  393. csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
  394. musb_writew(epio, MUSB_TXCSR, csr);
  395. DBG(20, "underrun on ep%d, req %p\n", epnum, request);
  396. }
  397. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  398. /*
  399. * SHOULD NOT HAPPEN... has with CPPI though, after
  400. * changing SENDSTALL (and other cases); harmless?
  401. */
  402. DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
  403. return;
  404. }
  405. if (request) {
  406. u8 is_dma = 0;
  407. if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
  408. is_dma = 1;
  409. csr |= MUSB_TXCSR_P_WZC_BITS;
  410. csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
  411. MUSB_TXCSR_TXPKTRDY);
  412. musb_writew(epio, MUSB_TXCSR, csr);
  413. /* Ensure writebuffer is empty. */
  414. csr = musb_readw(epio, MUSB_TXCSR);
  415. request->actual += musb_ep->dma->actual_len;
  416. DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
  417. epnum, csr, musb_ep->dma->actual_len, request);
  418. }
  419. if (is_dma || request->actual == request->length) {
  420. /*
  421. * First, maybe a terminating short packet. Some DMA
  422. * engines might handle this by themselves.
  423. */
  424. if ((request->zero && request->length
  425. && request->length % musb_ep->packet_sz == 0)
  426. #ifdef CONFIG_USB_INVENTRA_DMA
  427. || (is_dma && (!dma->desired_mode ||
  428. (request->actual &
  429. (musb_ep->packet_sz - 1))))
  430. #endif
  431. ) {
  432. /*
  433. * On DMA completion, FIFO may not be
  434. * available yet...
  435. */
  436. if (csr & MUSB_TXCSR_TXPKTRDY)
  437. return;
  438. DBG(4, "sending zero pkt\n");
  439. musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
  440. | MUSB_TXCSR_TXPKTRDY);
  441. request->zero = 0;
  442. }
  443. /* ... or if not, then complete it. */
  444. musb_g_giveback(musb_ep, request, 0);
  445. /*
  446. * Kickstart next transfer if appropriate;
  447. * the packet that just completed might not
  448. * be transmitted for hours or days.
  449. * REVISIT for double buffering...
  450. * FIXME revisit for stalls too...
  451. */
  452. musb_ep_select(mbase, epnum);
  453. csr = musb_readw(epio, MUSB_TXCSR);
  454. if (csr & MUSB_TXCSR_FIFONOTEMPTY)
  455. return;
  456. request = musb_ep->desc ? next_request(musb_ep) : NULL;
  457. if (!request) {
  458. DBG(4, "%s idle now\n",
  459. musb_ep->end_point.name);
  460. return;
  461. }
  462. }
  463. txstate(musb, to_musb_request(request));
  464. }
  465. }
  466. /* ------------------------------------------------------------ */
  467. #ifdef CONFIG_USB_INVENTRA_DMA
  468. /* Peripheral rx (OUT) using Mentor DMA works as follows:
  469. - Only mode 0 is used.
  470. - Request is queued by the gadget class driver.
  471. -> if queue was previously empty, rxstate()
  472. - Host sends OUT token which causes an endpoint interrupt
  473. /\ -> RxReady
  474. | -> if request queued, call rxstate
  475. | /\ -> setup DMA
  476. | | -> DMA interrupt on completion
  477. | | -> RxReady
  478. | | -> stop DMA
  479. | | -> ack the read
  480. | | -> if data recd = max expected
  481. | | by the request, or host
  482. | | sent a short packet,
  483. | | complete the request,
  484. | | and start the next one.
  485. | |_____________________________________|
  486. | else just wait for the host
  487. | to send the next OUT token.
  488. |__________________________________________________|
  489. * Non-Mentor DMA engines can of course work differently.
  490. */
  491. #endif
  492. /*
  493. * Context: controller locked, IRQs blocked, endpoint selected
  494. */
  495. static void rxstate(struct musb *musb, struct musb_request *req)
  496. {
  497. const u8 epnum = req->epnum;
  498. struct usb_request *request = &req->request;
  499. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_out;
  500. void __iomem *epio = musb->endpoints[epnum].regs;
  501. unsigned fifo_count = 0;
  502. u16 len = musb_ep->packet_sz;
  503. u16 csr = musb_readw(epio, MUSB_RXCSR);
  504. /* We shouldn't get here while DMA is active, but we do... */
  505. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  506. DBG(4, "DMA pending...\n");
  507. return;
  508. }
  509. if (csr & MUSB_RXCSR_P_SENDSTALL) {
  510. DBG(5, "%s stalling, RXCSR %04x\n",
  511. musb_ep->end_point.name, csr);
  512. return;
  513. }
  514. if (is_cppi_enabled() && musb_ep->dma) {
  515. struct dma_controller *c = musb->dma_controller;
  516. struct dma_channel *channel = musb_ep->dma;
  517. /* NOTE: CPPI won't actually stop advancing the DMA
  518. * queue after short packet transfers, so this is almost
  519. * always going to run as IRQ-per-packet DMA so that
  520. * faults will be handled correctly.
  521. */
  522. if (c->channel_program(channel,
  523. musb_ep->packet_sz,
  524. !request->short_not_ok,
  525. request->dma + request->actual,
  526. request->length - request->actual)) {
  527. /* make sure that if an rxpkt arrived after the irq,
  528. * the cppi engine will be ready to take it as soon
  529. * as DMA is enabled
  530. */
  531. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  532. | MUSB_RXCSR_DMAMODE);
  533. csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
  534. musb_writew(epio, MUSB_RXCSR, csr);
  535. return;
  536. }
  537. }
  538. if (csr & MUSB_RXCSR_RXPKTRDY) {
  539. len = musb_readw(epio, MUSB_RXCOUNT);
  540. if (request->actual < request->length) {
  541. #ifdef CONFIG_USB_INVENTRA_DMA
  542. if (is_dma_capable() && musb_ep->dma) {
  543. struct dma_controller *c;
  544. struct dma_channel *channel;
  545. int use_dma = 0;
  546. c = musb->dma_controller;
  547. channel = musb_ep->dma;
  548. /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
  549. * mode 0 only. So we do not get endpoint interrupts due to DMA
  550. * completion. We only get interrupts from DMA controller.
  551. *
  552. * We could operate in DMA mode 1 if we knew the size of the tranfer
  553. * in advance. For mass storage class, request->length = what the host
  554. * sends, so that'd work. But for pretty much everything else,
  555. * request->length is routinely more than what the host sends. For
  556. * most these gadgets, end of is signified either by a short packet,
  557. * or filling the last byte of the buffer. (Sending extra data in
  558. * that last pckate should trigger an overflow fault.) But in mode 1,
  559. * we don't get DMA completion interrrupt for short packets.
  560. *
  561. * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
  562. * to get endpoint interrupt on every DMA req, but that didn't seem
  563. * to work reliably.
  564. *
  565. * REVISIT an updated g_file_storage can set req->short_not_ok, which
  566. * then becomes usable as a runtime "use mode 1" hint...
  567. */
  568. csr |= MUSB_RXCSR_DMAENAB;
  569. #ifdef USE_MODE1
  570. csr |= MUSB_RXCSR_AUTOCLEAR;
  571. /* csr |= MUSB_RXCSR_DMAMODE; */
  572. /* this special sequence (enabling and then
  573. * disabling MUSB_RXCSR_DMAMODE) is required
  574. * to get DMAReq to activate
  575. */
  576. musb_writew(epio, MUSB_RXCSR,
  577. csr | MUSB_RXCSR_DMAMODE);
  578. #endif
  579. musb_writew(epio, MUSB_RXCSR, csr);
  580. if (request->actual < request->length) {
  581. int transfer_size = 0;
  582. #ifdef USE_MODE1
  583. transfer_size = min(request->length,
  584. channel->max_len);
  585. #else
  586. transfer_size = len;
  587. #endif
  588. if (transfer_size <= musb_ep->packet_sz)
  589. musb_ep->dma->desired_mode = 0;
  590. else
  591. musb_ep->dma->desired_mode = 1;
  592. use_dma = c->channel_program(
  593. channel,
  594. musb_ep->packet_sz,
  595. channel->desired_mode,
  596. request->dma
  597. + request->actual,
  598. transfer_size);
  599. }
  600. if (use_dma)
  601. return;
  602. }
  603. #endif /* Mentor's DMA */
  604. fifo_count = request->length - request->actual;
  605. DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
  606. musb_ep->end_point.name,
  607. len, fifo_count,
  608. musb_ep->packet_sz);
  609. fifo_count = min_t(unsigned, len, fifo_count);
  610. #ifdef CONFIG_USB_TUSB_OMAP_DMA
  611. if (tusb_dma_omap() && musb_ep->dma) {
  612. struct dma_controller *c = musb->dma_controller;
  613. struct dma_channel *channel = musb_ep->dma;
  614. u32 dma_addr = request->dma + request->actual;
  615. int ret;
  616. ret = c->channel_program(channel,
  617. musb_ep->packet_sz,
  618. channel->desired_mode,
  619. dma_addr,
  620. fifo_count);
  621. if (ret)
  622. return;
  623. }
  624. #endif
  625. musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
  626. (request->buf + request->actual));
  627. request->actual += fifo_count;
  628. /* REVISIT if we left anything in the fifo, flush
  629. * it and report -EOVERFLOW
  630. */
  631. /* ack the read! */
  632. csr |= MUSB_RXCSR_P_WZC_BITS;
  633. csr &= ~MUSB_RXCSR_RXPKTRDY;
  634. musb_writew(epio, MUSB_RXCSR, csr);
  635. }
  636. }
  637. /* reach the end or short packet detected */
  638. if (request->actual == request->length || len < musb_ep->packet_sz)
  639. musb_g_giveback(musb_ep, request, 0);
  640. }
  641. /*
  642. * Data ready for a request; called from IRQ
  643. */
  644. void musb_g_rx(struct musb *musb, u8 epnum)
  645. {
  646. u16 csr;
  647. struct usb_request *request;
  648. void __iomem *mbase = musb->mregs;
  649. struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_out;
  650. void __iomem *epio = musb->endpoints[epnum].regs;
  651. struct dma_channel *dma;
  652. musb_ep_select(mbase, epnum);
  653. request = next_request(musb_ep);
  654. if (!request)
  655. return;
  656. csr = musb_readw(epio, MUSB_RXCSR);
  657. dma = is_dma_capable() ? musb_ep->dma : NULL;
  658. DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
  659. csr, dma ? " (dma)" : "", request);
  660. if (csr & MUSB_RXCSR_P_SENTSTALL) {
  661. csr |= MUSB_RXCSR_P_WZC_BITS;
  662. csr &= ~MUSB_RXCSR_P_SENTSTALL;
  663. musb_writew(epio, MUSB_RXCSR, csr);
  664. return;
  665. }
  666. if (csr & MUSB_RXCSR_P_OVERRUN) {
  667. /* csr |= MUSB_RXCSR_P_WZC_BITS; */
  668. csr &= ~MUSB_RXCSR_P_OVERRUN;
  669. musb_writew(epio, MUSB_RXCSR, csr);
  670. DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
  671. if (request && request->status == -EINPROGRESS)
  672. request->status = -EOVERFLOW;
  673. }
  674. if (csr & MUSB_RXCSR_INCOMPRX) {
  675. /* REVISIT not necessarily an error */
  676. DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
  677. }
  678. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  679. /* "should not happen"; likely RXPKTRDY pending for DMA */
  680. DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
  681. "%s busy, csr %04x\n",
  682. musb_ep->end_point.name, csr);
  683. return;
  684. }
  685. if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
  686. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  687. | MUSB_RXCSR_DMAENAB
  688. | MUSB_RXCSR_DMAMODE);
  689. musb_writew(epio, MUSB_RXCSR,
  690. MUSB_RXCSR_P_WZC_BITS | csr);
  691. request->actual += musb_ep->dma->actual_len;
  692. DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
  693. epnum, csr,
  694. musb_readw(epio, MUSB_RXCSR),
  695. musb_ep->dma->actual_len, request);
  696. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
  697. /* Autoclear doesn't clear RxPktRdy for short packets */
  698. if ((dma->desired_mode == 0)
  699. || (dma->actual_len
  700. & (musb_ep->packet_sz - 1))) {
  701. /* ack the read! */
  702. csr &= ~MUSB_RXCSR_RXPKTRDY;
  703. musb_writew(epio, MUSB_RXCSR, csr);
  704. }
  705. /* incomplete, and not short? wait for next IN packet */
  706. if ((request->actual < request->length)
  707. && (musb_ep->dma->actual_len
  708. == musb_ep->packet_sz))
  709. return;
  710. #endif
  711. musb_g_giveback(musb_ep, request, 0);
  712. request = next_request(musb_ep);
  713. if (!request)
  714. return;
  715. }
  716. /* analyze request if the ep is hot */
  717. if (request)
  718. rxstate(musb, to_musb_request(request));
  719. else
  720. DBG(3, "packet waiting for %s%s request\n",
  721. musb_ep->desc ? "" : "inactive ",
  722. musb_ep->end_point.name);
  723. return;
  724. }
  725. /* ------------------------------------------------------------ */
  726. static int musb_gadget_enable(struct usb_ep *ep,
  727. const struct usb_endpoint_descriptor *desc)
  728. {
  729. unsigned long flags;
  730. struct musb_ep *musb_ep;
  731. struct musb_hw_ep *hw_ep;
  732. void __iomem *regs;
  733. struct musb *musb;
  734. void __iomem *mbase;
  735. u8 epnum;
  736. u16 csr;
  737. unsigned tmp;
  738. int status = -EINVAL;
  739. if (!ep || !desc)
  740. return -EINVAL;
  741. musb_ep = to_musb_ep(ep);
  742. hw_ep = musb_ep->hw_ep;
  743. regs = hw_ep->regs;
  744. musb = musb_ep->musb;
  745. mbase = musb->mregs;
  746. epnum = musb_ep->current_epnum;
  747. spin_lock_irqsave(&musb->lock, flags);
  748. if (musb_ep->desc) {
  749. status = -EBUSY;
  750. goto fail;
  751. }
  752. musb_ep->type = usb_endpoint_type(desc);
  753. /* check direction and (later) maxpacket size against endpoint */
  754. if (usb_endpoint_num(desc) != epnum)
  755. goto fail;
  756. /* REVISIT this rules out high bandwidth periodic transfers */
  757. tmp = le16_to_cpu(desc->wMaxPacketSize);
  758. if (tmp & ~0x07ff)
  759. goto fail;
  760. musb_ep->packet_sz = tmp;
  761. /* enable the interrupts for the endpoint, set the endpoint
  762. * packet size (or fail), set the mode, clear the fifo
  763. */
  764. musb_ep_select(mbase, epnum);
  765. if (usb_endpoint_dir_in(desc)) {
  766. u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
  767. if (hw_ep->is_shared_fifo)
  768. musb_ep->is_in = 1;
  769. if (!musb_ep->is_in)
  770. goto fail;
  771. if (tmp > hw_ep->max_packet_sz_tx)
  772. goto fail;
  773. int_txe |= (1 << epnum);
  774. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  775. /* REVISIT if can_bulk_split(), use by updating "tmp";
  776. * likewise high bandwidth periodic tx
  777. */
  778. /* Set TXMAXP with the FIFO size of the endpoint
  779. * to disable double buffering mode. Currently, It seems that double
  780. * buffering has problem if musb RTL revision number < 2.0.
  781. */
  782. if (musb->hwvers < MUSB_HWVERS_2000)
  783. musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
  784. else
  785. musb_writew(regs, MUSB_TXMAXP, tmp);
  786. csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
  787. if (musb_readw(regs, MUSB_TXCSR)
  788. & MUSB_TXCSR_FIFONOTEMPTY)
  789. csr |= MUSB_TXCSR_FLUSHFIFO;
  790. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  791. csr |= MUSB_TXCSR_P_ISO;
  792. /* set twice in case of double buffering */
  793. musb_writew(regs, MUSB_TXCSR, csr);
  794. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  795. musb_writew(regs, MUSB_TXCSR, csr);
  796. } else {
  797. u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
  798. if (hw_ep->is_shared_fifo)
  799. musb_ep->is_in = 0;
  800. if (musb_ep->is_in)
  801. goto fail;
  802. if (tmp > hw_ep->max_packet_sz_rx)
  803. goto fail;
  804. int_rxe |= (1 << epnum);
  805. musb_writew(mbase, MUSB_INTRRXE, int_rxe);
  806. /* REVISIT if can_bulk_combine() use by updating "tmp"
  807. * likewise high bandwidth periodic rx
  808. */
  809. /* Set RXMAXP with the FIFO size of the endpoint
  810. * to disable double buffering mode.
  811. */
  812. if (musb->hwvers < MUSB_HWVERS_2000)
  813. musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_rx);
  814. else
  815. musb_writew(regs, MUSB_RXMAXP, tmp);
  816. /* force shared fifo to OUT-only mode */
  817. if (hw_ep->is_shared_fifo) {
  818. csr = musb_readw(regs, MUSB_TXCSR);
  819. csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
  820. musb_writew(regs, MUSB_TXCSR, csr);
  821. }
  822. csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
  823. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  824. csr |= MUSB_RXCSR_P_ISO;
  825. else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
  826. csr |= MUSB_RXCSR_DISNYET;
  827. /* set twice in case of double buffering */
  828. musb_writew(regs, MUSB_RXCSR, csr);
  829. musb_writew(regs, MUSB_RXCSR, csr);
  830. }
  831. /* NOTE: all the I/O code _should_ work fine without DMA, in case
  832. * for some reason you run out of channels here.
  833. */
  834. if (is_dma_capable() && musb->dma_controller) {
  835. struct dma_controller *c = musb->dma_controller;
  836. musb_ep->dma = c->channel_alloc(c, hw_ep,
  837. (desc->bEndpointAddress & USB_DIR_IN));
  838. } else
  839. musb_ep->dma = NULL;
  840. musb_ep->desc = desc;
  841. musb_ep->busy = 0;
  842. musb_ep->wedged = 0;
  843. status = 0;
  844. pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
  845. musb_driver_name, musb_ep->end_point.name,
  846. ({ char *s; switch (musb_ep->type) {
  847. case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
  848. case USB_ENDPOINT_XFER_INT: s = "int"; break;
  849. default: s = "iso"; break;
  850. }; s; }),
  851. musb_ep->is_in ? "IN" : "OUT",
  852. musb_ep->dma ? "dma, " : "",
  853. musb_ep->packet_sz);
  854. schedule_work(&musb->irq_work);
  855. fail:
  856. spin_unlock_irqrestore(&musb->lock, flags);
  857. return status;
  858. }
  859. /*
  860. * Disable an endpoint flushing all requests queued.
  861. */
  862. static int musb_gadget_disable(struct usb_ep *ep)
  863. {
  864. unsigned long flags;
  865. struct musb *musb;
  866. u8 epnum;
  867. struct musb_ep *musb_ep;
  868. void __iomem *epio;
  869. int status = 0;
  870. musb_ep = to_musb_ep(ep);
  871. musb = musb_ep->musb;
  872. epnum = musb_ep->current_epnum;
  873. epio = musb->endpoints[epnum].regs;
  874. spin_lock_irqsave(&musb->lock, flags);
  875. musb_ep_select(musb->mregs, epnum);
  876. /* zero the endpoint sizes */
  877. if (musb_ep->is_in) {
  878. u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
  879. int_txe &= ~(1 << epnum);
  880. musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
  881. musb_writew(epio, MUSB_TXMAXP, 0);
  882. } else {
  883. u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
  884. int_rxe &= ~(1 << epnum);
  885. musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
  886. musb_writew(epio, MUSB_RXMAXP, 0);
  887. }
  888. musb_ep->desc = NULL;
  889. /* abort all pending DMA and requests */
  890. nuke(musb_ep, -ESHUTDOWN);
  891. schedule_work(&musb->irq_work);
  892. spin_unlock_irqrestore(&(musb->lock), flags);
  893. DBG(2, "%s\n", musb_ep->end_point.name);
  894. return status;
  895. }
  896. /*
  897. * Allocate a request for an endpoint.
  898. * Reused by ep0 code.
  899. */
  900. struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  901. {
  902. struct musb_ep *musb_ep = to_musb_ep(ep);
  903. struct musb_request *request = NULL;
  904. request = kzalloc(sizeof *request, gfp_flags);
  905. if (request) {
  906. INIT_LIST_HEAD(&request->request.list);
  907. request->request.dma = DMA_ADDR_INVALID;
  908. request->epnum = musb_ep->current_epnum;
  909. request->ep = musb_ep;
  910. }
  911. return &request->request;
  912. }
  913. /*
  914. * Free a request
  915. * Reused by ep0 code.
  916. */
  917. void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  918. {
  919. kfree(to_musb_request(req));
  920. }
  921. static LIST_HEAD(buffers);
  922. struct free_record {
  923. struct list_head list;
  924. struct device *dev;
  925. unsigned bytes;
  926. dma_addr_t dma;
  927. };
  928. /*
  929. * Context: controller locked, IRQs blocked.
  930. */
  931. static void musb_ep_restart(struct musb *musb, struct musb_request *req)
  932. {
  933. DBG(3, "<== %s request %p len %u on hw_ep%d\n",
  934. req->tx ? "TX/IN" : "RX/OUT",
  935. &req->request, req->request.length, req->epnum);
  936. musb_ep_select(musb->mregs, req->epnum);
  937. if (req->tx)
  938. txstate(musb, req);
  939. else
  940. rxstate(musb, req);
  941. }
  942. static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
  943. gfp_t gfp_flags)
  944. {
  945. struct musb_ep *musb_ep;
  946. struct musb_request *request;
  947. struct musb *musb;
  948. int status = 0;
  949. unsigned long lockflags;
  950. if (!ep || !req)
  951. return -EINVAL;
  952. if (!req->buf)
  953. return -ENODATA;
  954. musb_ep = to_musb_ep(ep);
  955. musb = musb_ep->musb;
  956. request = to_musb_request(req);
  957. request->musb = musb;
  958. if (request->ep != musb_ep)
  959. return -EINVAL;
  960. DBG(4, "<== to %s request=%p\n", ep->name, req);
  961. /* request is mine now... */
  962. request->request.actual = 0;
  963. request->request.status = -EINPROGRESS;
  964. request->epnum = musb_ep->current_epnum;
  965. request->tx = musb_ep->is_in;
  966. if (is_dma_capable() && musb_ep->dma) {
  967. if (request->request.dma == DMA_ADDR_INVALID) {
  968. request->request.dma = dma_map_single(
  969. musb->controller,
  970. request->request.buf,
  971. request->request.length,
  972. request->tx
  973. ? DMA_TO_DEVICE
  974. : DMA_FROM_DEVICE);
  975. request->mapped = 1;
  976. } else {
  977. dma_sync_single_for_device(musb->controller,
  978. request->request.dma,
  979. request->request.length,
  980. request->tx
  981. ? DMA_TO_DEVICE
  982. : DMA_FROM_DEVICE);
  983. request->mapped = 0;
  984. }
  985. } else if (!req->buf) {
  986. return -ENODATA;
  987. } else
  988. request->mapped = 0;
  989. spin_lock_irqsave(&musb->lock, lockflags);
  990. /* don't queue if the ep is down */
  991. if (!musb_ep->desc) {
  992. DBG(4, "req %p queued to %s while ep %s\n",
  993. req, ep->name, "disabled");
  994. status = -ESHUTDOWN;
  995. goto cleanup;
  996. }
  997. /* add request to the list */
  998. list_add_tail(&(request->request.list), &(musb_ep->req_list));
  999. /* it this is the head of the queue, start i/o ... */
  1000. if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
  1001. musb_ep_restart(musb, request);
  1002. cleanup:
  1003. spin_unlock_irqrestore(&musb->lock, lockflags);
  1004. return status;
  1005. }
  1006. static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
  1007. {
  1008. struct musb_ep *musb_ep = to_musb_ep(ep);
  1009. struct usb_request *r;
  1010. unsigned long flags;
  1011. int status = 0;
  1012. struct musb *musb = musb_ep->musb;
  1013. if (!ep || !request || to_musb_request(request)->ep != musb_ep)
  1014. return -EINVAL;
  1015. spin_lock_irqsave(&musb->lock, flags);
  1016. list_for_each_entry(r, &musb_ep->req_list, list) {
  1017. if (r == request)
  1018. break;
  1019. }
  1020. if (r != request) {
  1021. DBG(3, "request %p not queued to %s\n", request, ep->name);
  1022. status = -EINVAL;
  1023. goto done;
  1024. }
  1025. /* if the hardware doesn't have the request, easy ... */
  1026. if (musb_ep->req_list.next != &request->list || musb_ep->busy)
  1027. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1028. /* ... else abort the dma transfer ... */
  1029. else if (is_dma_capable() && musb_ep->dma) {
  1030. struct dma_controller *c = musb->dma_controller;
  1031. musb_ep_select(musb->mregs, musb_ep->current_epnum);
  1032. if (c->channel_abort)
  1033. status = c->channel_abort(musb_ep->dma);
  1034. else
  1035. status = -EBUSY;
  1036. if (status == 0)
  1037. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1038. } else {
  1039. /* NOTE: by sticking to easily tested hardware/driver states,
  1040. * we leave counting of in-flight packets imprecise.
  1041. */
  1042. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1043. }
  1044. done:
  1045. spin_unlock_irqrestore(&musb->lock, flags);
  1046. return status;
  1047. }
  1048. /*
  1049. * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
  1050. * data but will queue requests.
  1051. *
  1052. * exported to ep0 code
  1053. */
  1054. static int musb_gadget_set_halt(struct usb_ep *ep, int value)
  1055. {
  1056. struct musb_ep *musb_ep = to_musb_ep(ep);
  1057. u8 epnum = musb_ep->current_epnum;
  1058. struct musb *musb = musb_ep->musb;
  1059. void __iomem *epio = musb->endpoints[epnum].regs;
  1060. void __iomem *mbase;
  1061. unsigned long flags;
  1062. u16 csr;
  1063. struct musb_request *request;
  1064. int status = 0;
  1065. if (!ep)
  1066. return -EINVAL;
  1067. mbase = musb->mregs;
  1068. spin_lock_irqsave(&musb->lock, flags);
  1069. if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
  1070. status = -EINVAL;
  1071. goto done;
  1072. }
  1073. musb_ep_select(mbase, epnum);
  1074. request = to_musb_request(next_request(musb_ep));
  1075. if (value) {
  1076. if (request) {
  1077. DBG(3, "request in progress, cannot halt %s\n",
  1078. ep->name);
  1079. status = -EAGAIN;
  1080. goto done;
  1081. }
  1082. /* Cannot portably stall with non-empty FIFO */
  1083. if (musb_ep->is_in) {
  1084. csr = musb_readw(epio, MUSB_TXCSR);
  1085. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1086. DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
  1087. status = -EAGAIN;
  1088. goto done;
  1089. }
  1090. }
  1091. } else
  1092. musb_ep->wedged = 0;
  1093. /* set/clear the stall and toggle bits */
  1094. DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
  1095. if (musb_ep->is_in) {
  1096. csr = musb_readw(epio, MUSB_TXCSR);
  1097. csr |= MUSB_TXCSR_P_WZC_BITS
  1098. | MUSB_TXCSR_CLRDATATOG;
  1099. if (value)
  1100. csr |= MUSB_TXCSR_P_SENDSTALL;
  1101. else
  1102. csr &= ~(MUSB_TXCSR_P_SENDSTALL
  1103. | MUSB_TXCSR_P_SENTSTALL);
  1104. csr &= ~MUSB_TXCSR_TXPKTRDY;
  1105. musb_writew(epio, MUSB_TXCSR, csr);
  1106. } else {
  1107. csr = musb_readw(epio, MUSB_RXCSR);
  1108. csr |= MUSB_RXCSR_P_WZC_BITS
  1109. | MUSB_RXCSR_FLUSHFIFO
  1110. | MUSB_RXCSR_CLRDATATOG;
  1111. if (value)
  1112. csr |= MUSB_RXCSR_P_SENDSTALL;
  1113. else
  1114. csr &= ~(MUSB_RXCSR_P_SENDSTALL
  1115. | MUSB_RXCSR_P_SENTSTALL);
  1116. musb_writew(epio, MUSB_RXCSR, csr);
  1117. }
  1118. /* maybe start the first request in the queue */
  1119. if (!musb_ep->busy && !value && request) {
  1120. DBG(3, "restarting the request\n");
  1121. musb_ep_restart(musb, request);
  1122. }
  1123. done:
  1124. spin_unlock_irqrestore(&musb->lock, flags);
  1125. return status;
  1126. }
  1127. /*
  1128. * Sets the halt feature with the clear requests ignored
  1129. */
  1130. static int musb_gadget_set_wedge(struct usb_ep *ep)
  1131. {
  1132. struct musb_ep *musb_ep = to_musb_ep(ep);
  1133. if (!ep)
  1134. return -EINVAL;
  1135. musb_ep->wedged = 1;
  1136. return usb_ep_set_halt(ep);
  1137. }
  1138. static int musb_gadget_fifo_status(struct usb_ep *ep)
  1139. {
  1140. struct musb_ep *musb_ep = to_musb_ep(ep);
  1141. void __iomem *epio = musb_ep->hw_ep->regs;
  1142. int retval = -EINVAL;
  1143. if (musb_ep->desc && !musb_ep->is_in) {
  1144. struct musb *musb = musb_ep->musb;
  1145. int epnum = musb_ep->current_epnum;
  1146. void __iomem *mbase = musb->mregs;
  1147. unsigned long flags;
  1148. spin_lock_irqsave(&musb->lock, flags);
  1149. musb_ep_select(mbase, epnum);
  1150. /* FIXME return zero unless RXPKTRDY is set */
  1151. retval = musb_readw(epio, MUSB_RXCOUNT);
  1152. spin_unlock_irqrestore(&musb->lock, flags);
  1153. }
  1154. return retval;
  1155. }
  1156. static void musb_gadget_fifo_flush(struct usb_ep *ep)
  1157. {
  1158. struct musb_ep *musb_ep = to_musb_ep(ep);
  1159. struct musb *musb = musb_ep->musb;
  1160. u8 epnum = musb_ep->current_epnum;
  1161. void __iomem *epio = musb->endpoints[epnum].regs;
  1162. void __iomem *mbase;
  1163. unsigned long flags;
  1164. u16 csr, int_txe;
  1165. mbase = musb->mregs;
  1166. spin_lock_irqsave(&musb->lock, flags);
  1167. musb_ep_select(mbase, (u8) epnum);
  1168. /* disable interrupts */
  1169. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  1170. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  1171. if (musb_ep->is_in) {
  1172. csr = musb_readw(epio, MUSB_TXCSR);
  1173. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1174. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
  1175. musb_writew(epio, MUSB_TXCSR, csr);
  1176. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  1177. musb_writew(epio, MUSB_TXCSR, csr);
  1178. }
  1179. } else {
  1180. csr = musb_readw(epio, MUSB_RXCSR);
  1181. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
  1182. musb_writew(epio, MUSB_RXCSR, csr);
  1183. musb_writew(epio, MUSB_RXCSR, csr);
  1184. }
  1185. /* re-enable interrupt */
  1186. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  1187. spin_unlock_irqrestore(&musb->lock, flags);
  1188. }
  1189. static const struct usb_ep_ops musb_ep_ops = {
  1190. .enable = musb_gadget_enable,
  1191. .disable = musb_gadget_disable,
  1192. .alloc_request = musb_alloc_request,
  1193. .free_request = musb_free_request,
  1194. .queue = musb_gadget_queue,
  1195. .dequeue = musb_gadget_dequeue,
  1196. .set_halt = musb_gadget_set_halt,
  1197. .set_wedge = musb_gadget_set_wedge,
  1198. .fifo_status = musb_gadget_fifo_status,
  1199. .fifo_flush = musb_gadget_fifo_flush
  1200. };
  1201. /* ----------------------------------------------------------------------- */
  1202. static int musb_gadget_get_frame(struct usb_gadget *gadget)
  1203. {
  1204. struct musb *musb = gadget_to_musb(gadget);
  1205. return (int)musb_readw(musb->mregs, MUSB_FRAME);
  1206. }
  1207. static int musb_gadget_wakeup(struct usb_gadget *gadget)
  1208. {
  1209. struct musb *musb = gadget_to_musb(gadget);
  1210. void __iomem *mregs = musb->mregs;
  1211. unsigned long flags;
  1212. int status = -EINVAL;
  1213. u8 power, devctl;
  1214. int retries;
  1215. spin_lock_irqsave(&musb->lock, flags);
  1216. switch (musb->xceiv->state) {
  1217. case OTG_STATE_B_PERIPHERAL:
  1218. /* NOTE: OTG state machine doesn't include B_SUSPENDED;
  1219. * that's part of the standard usb 1.1 state machine, and
  1220. * doesn't affect OTG transitions.
  1221. */
  1222. if (musb->may_wakeup && musb->is_suspended)
  1223. break;
  1224. goto done;
  1225. case OTG_STATE_B_IDLE:
  1226. /* Start SRP ... OTG not required. */
  1227. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1228. DBG(2, "Sending SRP: devctl: %02x\n", devctl);
  1229. devctl |= MUSB_DEVCTL_SESSION;
  1230. musb_writeb(mregs, MUSB_DEVCTL, devctl);
  1231. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1232. retries = 100;
  1233. while (!(devctl & MUSB_DEVCTL_SESSION)) {
  1234. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1235. if (retries-- < 1)
  1236. break;
  1237. }
  1238. retries = 10000;
  1239. while (devctl & MUSB_DEVCTL_SESSION) {
  1240. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1241. if (retries-- < 1)
  1242. break;
  1243. }
  1244. /* Block idling for at least 1s */
  1245. musb_platform_try_idle(musb,
  1246. jiffies + msecs_to_jiffies(1 * HZ));
  1247. status = 0;
  1248. goto done;
  1249. default:
  1250. DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
  1251. goto done;
  1252. }
  1253. status = 0;
  1254. power = musb_readb(mregs, MUSB_POWER);
  1255. power |= MUSB_POWER_RESUME;
  1256. musb_writeb(mregs, MUSB_POWER, power);
  1257. DBG(2, "issue wakeup\n");
  1258. /* FIXME do this next chunk in a timer callback, no udelay */
  1259. mdelay(2);
  1260. power = musb_readb(mregs, MUSB_POWER);
  1261. power &= ~MUSB_POWER_RESUME;
  1262. musb_writeb(mregs, MUSB_POWER, power);
  1263. done:
  1264. spin_unlock_irqrestore(&musb->lock, flags);
  1265. return status;
  1266. }
  1267. static int
  1268. musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
  1269. {
  1270. struct musb *musb = gadget_to_musb(gadget);
  1271. musb->is_self_powered = !!is_selfpowered;
  1272. return 0;
  1273. }
  1274. static void musb_pullup(struct musb *musb, int is_on)
  1275. {
  1276. u8 power;
  1277. power = musb_readb(musb->mregs, MUSB_POWER);
  1278. if (is_on)
  1279. power |= MUSB_POWER_SOFTCONN;
  1280. else
  1281. power &= ~MUSB_POWER_SOFTCONN;
  1282. /* FIXME if on, HdrcStart; if off, HdrcStop */
  1283. DBG(3, "gadget %s D+ pullup %s\n",
  1284. musb->gadget_driver->function, is_on ? "on" : "off");
  1285. musb_writeb(musb->mregs, MUSB_POWER, power);
  1286. }
  1287. #if 0
  1288. static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
  1289. {
  1290. DBG(2, "<= %s =>\n", __func__);
  1291. /*
  1292. * FIXME iff driver's softconnect flag is set (as it is during probe,
  1293. * though that can clear it), just musb_pullup().
  1294. */
  1295. return -EINVAL;
  1296. }
  1297. #endif
  1298. static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1299. {
  1300. struct musb *musb = gadget_to_musb(gadget);
  1301. if (!musb->xceiv->set_power)
  1302. return -EOPNOTSUPP;
  1303. return otg_set_power(musb->xceiv, mA);
  1304. }
  1305. static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1306. {
  1307. struct musb *musb = gadget_to_musb(gadget);
  1308. unsigned long flags;
  1309. is_on = !!is_on;
  1310. /* NOTE: this assumes we are sensing vbus; we'd rather
  1311. * not pullup unless the B-session is active.
  1312. */
  1313. spin_lock_irqsave(&musb->lock, flags);
  1314. if (is_on != musb->softconnect) {
  1315. musb->softconnect = is_on;
  1316. musb_pullup(musb, is_on);
  1317. }
  1318. spin_unlock_irqrestore(&musb->lock, flags);
  1319. return 0;
  1320. }
  1321. static const struct usb_gadget_ops musb_gadget_operations = {
  1322. .get_frame = musb_gadget_get_frame,
  1323. .wakeup = musb_gadget_wakeup,
  1324. .set_selfpowered = musb_gadget_set_self_powered,
  1325. /* .vbus_session = musb_gadget_vbus_session, */
  1326. .vbus_draw = musb_gadget_vbus_draw,
  1327. .pullup = musb_gadget_pullup,
  1328. };
  1329. /* ----------------------------------------------------------------------- */
  1330. /* Registration */
  1331. /* Only this registration code "knows" the rule (from USB standards)
  1332. * about there being only one external upstream port. It assumes
  1333. * all peripheral ports are external...
  1334. */
  1335. static struct musb *the_gadget;
  1336. static void musb_gadget_release(struct device *dev)
  1337. {
  1338. /* kref_put(WHAT) */
  1339. dev_dbg(dev, "%s\n", __func__);
  1340. }
  1341. static void __init
  1342. init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
  1343. {
  1344. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1345. memset(ep, 0, sizeof *ep);
  1346. ep->current_epnum = epnum;
  1347. ep->musb = musb;
  1348. ep->hw_ep = hw_ep;
  1349. ep->is_in = is_in;
  1350. INIT_LIST_HEAD(&ep->req_list);
  1351. sprintf(ep->name, "ep%d%s", epnum,
  1352. (!epnum || hw_ep->is_shared_fifo) ? "" : (
  1353. is_in ? "in" : "out"));
  1354. ep->end_point.name = ep->name;
  1355. INIT_LIST_HEAD(&ep->end_point.ep_list);
  1356. if (!epnum) {
  1357. ep->end_point.maxpacket = 64;
  1358. ep->end_point.ops = &musb_g_ep0_ops;
  1359. musb->g.ep0 = &ep->end_point;
  1360. } else {
  1361. if (is_in)
  1362. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1363. else
  1364. ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
  1365. ep->end_point.ops = &musb_ep_ops;
  1366. list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
  1367. }
  1368. }
  1369. /*
  1370. * Initialize the endpoints exposed to peripheral drivers, with backlinks
  1371. * to the rest of the driver state.
  1372. */
  1373. static inline void __init musb_g_init_endpoints(struct musb *musb)
  1374. {
  1375. u8 epnum;
  1376. struct musb_hw_ep *hw_ep;
  1377. unsigned count = 0;
  1378. /* intialize endpoint list just once */
  1379. INIT_LIST_HEAD(&(musb->g.ep_list));
  1380. for (epnum = 0, hw_ep = musb->endpoints;
  1381. epnum < musb->nr_endpoints;
  1382. epnum++, hw_ep++) {
  1383. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1384. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
  1385. count++;
  1386. } else {
  1387. if (hw_ep->max_packet_sz_tx) {
  1388. init_peripheral_ep(musb, &hw_ep->ep_in,
  1389. epnum, 1);
  1390. count++;
  1391. }
  1392. if (hw_ep->max_packet_sz_rx) {
  1393. init_peripheral_ep(musb, &hw_ep->ep_out,
  1394. epnum, 0);
  1395. count++;
  1396. }
  1397. }
  1398. }
  1399. }
  1400. /* called once during driver setup to initialize and link into
  1401. * the driver model; memory is zeroed.
  1402. */
  1403. int __init musb_gadget_setup(struct musb *musb)
  1404. {
  1405. int status;
  1406. /* REVISIT minor race: if (erroneously) setting up two
  1407. * musb peripherals at the same time, only the bus lock
  1408. * is probably held.
  1409. */
  1410. if (the_gadget)
  1411. return -EBUSY;
  1412. the_gadget = musb;
  1413. musb->g.ops = &musb_gadget_operations;
  1414. musb->g.is_dualspeed = 1;
  1415. musb->g.speed = USB_SPEED_UNKNOWN;
  1416. /* this "gadget" abstracts/virtualizes the controller */
  1417. dev_set_name(&musb->g.dev, "gadget");
  1418. musb->g.dev.parent = musb->controller;
  1419. musb->g.dev.dma_mask = musb->controller->dma_mask;
  1420. musb->g.dev.release = musb_gadget_release;
  1421. musb->g.name = musb_driver_name;
  1422. if (is_otg_enabled(musb))
  1423. musb->g.is_otg = 1;
  1424. musb_g_init_endpoints(musb);
  1425. musb->is_active = 0;
  1426. musb_platform_try_idle(musb, 0);
  1427. status = device_register(&musb->g.dev);
  1428. if (status != 0)
  1429. the_gadget = NULL;
  1430. return status;
  1431. }
  1432. void musb_gadget_cleanup(struct musb *musb)
  1433. {
  1434. if (musb != the_gadget)
  1435. return;
  1436. device_unregister(&musb->g.dev);
  1437. the_gadget = NULL;
  1438. }
  1439. /*
  1440. * Register the gadget driver. Used by gadget drivers when
  1441. * registering themselves with the controller.
  1442. *
  1443. * -EINVAL something went wrong (not driver)
  1444. * -EBUSY another gadget is already using the controller
  1445. * -ENOMEM no memeory to perform the operation
  1446. *
  1447. * @param driver the gadget driver
  1448. * @return <0 if error, 0 if everything is fine
  1449. */
  1450. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1451. {
  1452. int retval;
  1453. unsigned long flags;
  1454. struct musb *musb = the_gadget;
  1455. if (!driver
  1456. || driver->speed != USB_SPEED_HIGH
  1457. || !driver->bind
  1458. || !driver->setup)
  1459. return -EINVAL;
  1460. /* driver must be initialized to support peripheral mode */
  1461. if (!musb) {
  1462. DBG(1, "%s, no dev??\n", __func__);
  1463. return -ENODEV;
  1464. }
  1465. DBG(3, "registering driver %s\n", driver->function);
  1466. spin_lock_irqsave(&musb->lock, flags);
  1467. if (musb->gadget_driver) {
  1468. DBG(1, "%s is already bound to %s\n",
  1469. musb_driver_name,
  1470. musb->gadget_driver->driver.name);
  1471. retval = -EBUSY;
  1472. } else {
  1473. musb->gadget_driver = driver;
  1474. musb->g.dev.driver = &driver->driver;
  1475. driver->driver.bus = NULL;
  1476. musb->softconnect = 1;
  1477. retval = 0;
  1478. }
  1479. spin_unlock_irqrestore(&musb->lock, flags);
  1480. if (retval == 0) {
  1481. retval = driver->bind(&musb->g);
  1482. if (retval != 0) {
  1483. DBG(3, "bind to driver %s failed --> %d\n",
  1484. driver->driver.name, retval);
  1485. musb->gadget_driver = NULL;
  1486. musb->g.dev.driver = NULL;
  1487. }
  1488. spin_lock_irqsave(&musb->lock, flags);
  1489. otg_set_peripheral(musb->xceiv, &musb->g);
  1490. musb->xceiv->state = OTG_STATE_B_IDLE;
  1491. musb->is_active = 1;
  1492. /* FIXME this ignores the softconnect flag. Drivers are
  1493. * allowed hold the peripheral inactive until for example
  1494. * userspace hooks up printer hardware or DSP codecs, so
  1495. * hosts only see fully functional devices.
  1496. */
  1497. if (!is_otg_enabled(musb))
  1498. musb_start(musb);
  1499. otg_set_peripheral(musb->xceiv, &musb->g);
  1500. spin_unlock_irqrestore(&musb->lock, flags);
  1501. if (is_otg_enabled(musb)) {
  1502. DBG(3, "OTG startup...\n");
  1503. /* REVISIT: funcall to other code, which also
  1504. * handles power budgeting ... this way also
  1505. * ensures HdrcStart is indirectly called.
  1506. */
  1507. retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
  1508. if (retval < 0) {
  1509. DBG(1, "add_hcd failed, %d\n", retval);
  1510. spin_lock_irqsave(&musb->lock, flags);
  1511. otg_set_peripheral(musb->xceiv, NULL);
  1512. musb->gadget_driver = NULL;
  1513. musb->g.dev.driver = NULL;
  1514. spin_unlock_irqrestore(&musb->lock, flags);
  1515. }
  1516. }
  1517. }
  1518. return retval;
  1519. }
  1520. EXPORT_SYMBOL(usb_gadget_register_driver);
  1521. static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
  1522. {
  1523. int i;
  1524. struct musb_hw_ep *hw_ep;
  1525. /* don't disconnect if it's not connected */
  1526. if (musb->g.speed == USB_SPEED_UNKNOWN)
  1527. driver = NULL;
  1528. else
  1529. musb->g.speed = USB_SPEED_UNKNOWN;
  1530. /* deactivate the hardware */
  1531. if (musb->softconnect) {
  1532. musb->softconnect = 0;
  1533. musb_pullup(musb, 0);
  1534. }
  1535. musb_stop(musb);
  1536. /* killing any outstanding requests will quiesce the driver;
  1537. * then report disconnect
  1538. */
  1539. if (driver) {
  1540. for (i = 0, hw_ep = musb->endpoints;
  1541. i < musb->nr_endpoints;
  1542. i++, hw_ep++) {
  1543. musb_ep_select(musb->mregs, i);
  1544. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1545. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1546. } else {
  1547. if (hw_ep->max_packet_sz_tx)
  1548. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1549. if (hw_ep->max_packet_sz_rx)
  1550. nuke(&hw_ep->ep_out, -ESHUTDOWN);
  1551. }
  1552. }
  1553. spin_unlock(&musb->lock);
  1554. driver->disconnect(&musb->g);
  1555. spin_lock(&musb->lock);
  1556. }
  1557. }
  1558. /*
  1559. * Unregister the gadget driver. Used by gadget drivers when
  1560. * unregistering themselves from the controller.
  1561. *
  1562. * @param driver the gadget driver to unregister
  1563. */
  1564. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1565. {
  1566. unsigned long flags;
  1567. int retval = 0;
  1568. struct musb *musb = the_gadget;
  1569. if (!driver || !driver->unbind || !musb)
  1570. return -EINVAL;
  1571. /* REVISIT always use otg_set_peripheral() here too;
  1572. * this needs to shut down the OTG engine.
  1573. */
  1574. spin_lock_irqsave(&musb->lock, flags);
  1575. #ifdef CONFIG_USB_MUSB_OTG
  1576. musb_hnp_stop(musb);
  1577. #endif
  1578. if (musb->gadget_driver == driver) {
  1579. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1580. musb->xceiv->state = OTG_STATE_UNDEFINED;
  1581. stop_activity(musb, driver);
  1582. otg_set_peripheral(musb->xceiv, NULL);
  1583. DBG(3, "unregistering driver %s\n", driver->function);
  1584. spin_unlock_irqrestore(&musb->lock, flags);
  1585. driver->unbind(&musb->g);
  1586. spin_lock_irqsave(&musb->lock, flags);
  1587. musb->gadget_driver = NULL;
  1588. musb->g.dev.driver = NULL;
  1589. musb->is_active = 0;
  1590. musb_platform_try_idle(musb, 0);
  1591. } else
  1592. retval = -EINVAL;
  1593. spin_unlock_irqrestore(&musb->lock, flags);
  1594. if (is_otg_enabled(musb) && retval == 0) {
  1595. usb_remove_hcd(musb_to_hcd(musb));
  1596. /* FIXME we need to be able to register another
  1597. * gadget driver here and have everything work;
  1598. * that currently misbehaves.
  1599. */
  1600. }
  1601. return retval;
  1602. }
  1603. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1604. /* ----------------------------------------------------------------------- */
  1605. /* lifecycle operations called through plat_uds.c */
  1606. void musb_g_resume(struct musb *musb)
  1607. {
  1608. musb->is_suspended = 0;
  1609. switch (musb->xceiv->state) {
  1610. case OTG_STATE_B_IDLE:
  1611. break;
  1612. case OTG_STATE_B_WAIT_ACON:
  1613. case OTG_STATE_B_PERIPHERAL:
  1614. musb->is_active = 1;
  1615. if (musb->gadget_driver && musb->gadget_driver->resume) {
  1616. spin_unlock(&musb->lock);
  1617. musb->gadget_driver->resume(&musb->g);
  1618. spin_lock(&musb->lock);
  1619. }
  1620. break;
  1621. default:
  1622. WARNING("unhandled RESUME transition (%s)\n",
  1623. otg_state_string(musb));
  1624. }
  1625. }
  1626. /* called when SOF packets stop for 3+ msec */
  1627. void musb_g_suspend(struct musb *musb)
  1628. {
  1629. u8 devctl;
  1630. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1631. DBG(3, "devctl %02x\n", devctl);
  1632. switch (musb->xceiv->state) {
  1633. case OTG_STATE_B_IDLE:
  1634. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  1635. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1636. break;
  1637. case OTG_STATE_B_PERIPHERAL:
  1638. musb->is_suspended = 1;
  1639. if (musb->gadget_driver && musb->gadget_driver->suspend) {
  1640. spin_unlock(&musb->lock);
  1641. musb->gadget_driver->suspend(&musb->g);
  1642. spin_lock(&musb->lock);
  1643. }
  1644. break;
  1645. default:
  1646. /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
  1647. * A_PERIPHERAL may need care too
  1648. */
  1649. WARNING("unhandled SUSPEND transition (%s)\n",
  1650. otg_state_string(musb));
  1651. }
  1652. }
  1653. /* Called during SRP */
  1654. void musb_g_wakeup(struct musb *musb)
  1655. {
  1656. musb_gadget_wakeup(&musb->g);
  1657. }
  1658. /* called when VBUS drops below session threshold, and in other cases */
  1659. void musb_g_disconnect(struct musb *musb)
  1660. {
  1661. void __iomem *mregs = musb->mregs;
  1662. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  1663. DBG(3, "devctl %02x\n", devctl);
  1664. /* clear HR */
  1665. musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
  1666. /* don't draw vbus until new b-default session */
  1667. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1668. musb->g.speed = USB_SPEED_UNKNOWN;
  1669. if (musb->gadget_driver && musb->gadget_driver->disconnect) {
  1670. spin_unlock(&musb->lock);
  1671. musb->gadget_driver->disconnect(&musb->g);
  1672. spin_lock(&musb->lock);
  1673. }
  1674. switch (musb->xceiv->state) {
  1675. default:
  1676. #ifdef CONFIG_USB_MUSB_OTG
  1677. DBG(2, "Unhandled disconnect %s, setting a_idle\n",
  1678. otg_state_string(musb));
  1679. musb->xceiv->state = OTG_STATE_A_IDLE;
  1680. MUSB_HST_MODE(musb);
  1681. break;
  1682. case OTG_STATE_A_PERIPHERAL:
  1683. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  1684. MUSB_HST_MODE(musb);
  1685. break;
  1686. case OTG_STATE_B_WAIT_ACON:
  1687. case OTG_STATE_B_HOST:
  1688. #endif
  1689. case OTG_STATE_B_PERIPHERAL:
  1690. case OTG_STATE_B_IDLE:
  1691. musb->xceiv->state = OTG_STATE_B_IDLE;
  1692. break;
  1693. case OTG_STATE_B_SRP_INIT:
  1694. break;
  1695. }
  1696. musb->is_active = 0;
  1697. }
  1698. void musb_g_reset(struct musb *musb)
  1699. __releases(musb->lock)
  1700. __acquires(musb->lock)
  1701. {
  1702. void __iomem *mbase = musb->mregs;
  1703. u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
  1704. u8 power;
  1705. DBG(3, "<== %s addr=%x driver '%s'\n",
  1706. (devctl & MUSB_DEVCTL_BDEVICE)
  1707. ? "B-Device" : "A-Device",
  1708. musb_readb(mbase, MUSB_FADDR),
  1709. musb->gadget_driver
  1710. ? musb->gadget_driver->driver.name
  1711. : NULL
  1712. );
  1713. /* report disconnect, if we didn't already (flushing EP state) */
  1714. if (musb->g.speed != USB_SPEED_UNKNOWN)
  1715. musb_g_disconnect(musb);
  1716. /* clear HR */
  1717. else if (devctl & MUSB_DEVCTL_HR)
  1718. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  1719. /* what speed did we negotiate? */
  1720. power = musb_readb(mbase, MUSB_POWER);
  1721. musb->g.speed = (power & MUSB_POWER_HSMODE)
  1722. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  1723. /* start in USB_STATE_DEFAULT */
  1724. musb->is_active = 1;
  1725. musb->is_suspended = 0;
  1726. MUSB_DEV_MODE(musb);
  1727. musb->address = 0;
  1728. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  1729. musb->may_wakeup = 0;
  1730. musb->g.b_hnp_enable = 0;
  1731. musb->g.a_alt_hnp_support = 0;
  1732. musb->g.a_hnp_support = 0;
  1733. /* Normal reset, as B-Device;
  1734. * or else after HNP, as A-Device
  1735. */
  1736. if (devctl & MUSB_DEVCTL_BDEVICE) {
  1737. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1738. musb->g.is_a_peripheral = 0;
  1739. } else if (is_otg_enabled(musb)) {
  1740. musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
  1741. musb->g.is_a_peripheral = 1;
  1742. } else
  1743. WARN_ON(1);
  1744. /* start with default limits on VBUS power draw */
  1745. (void) musb_gadget_vbus_draw(&musb->g,
  1746. is_otg_enabled(musb) ? 8 : 100);
  1747. }