musb_gadget.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  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. size_t request_size;
  267. /* setup DMA, then program endpoint CSR */
  268. request_size = min_t(size_t, request->length - request->actual,
  269. musb_ep->dma->max_len);
  270. use_dma = (request->dma != DMA_ADDR_INVALID);
  271. /* MUSB_TXCSR_P_ISO is still set correctly */
  272. #ifdef CONFIG_USB_INVENTRA_DMA
  273. {
  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 + request->actual,
  330. request_size);
  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 + request->actual,
  343. request_size);
  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. if (request->actual == request->length) {
  444. musb_g_giveback(musb_ep, request, 0);
  445. request = musb_ep->desc ? next_request(musb_ep) : NULL;
  446. if (!request) {
  447. DBG(4, "%s idle now\n",
  448. musb_ep->end_point.name);
  449. return;
  450. }
  451. }
  452. }
  453. txstate(musb, to_musb_request(request));
  454. }
  455. }
  456. /* ------------------------------------------------------------ */
  457. #ifdef CONFIG_USB_INVENTRA_DMA
  458. /* Peripheral rx (OUT) using Mentor DMA works as follows:
  459. - Only mode 0 is used.
  460. - Request is queued by the gadget class driver.
  461. -> if queue was previously empty, rxstate()
  462. - Host sends OUT token which causes an endpoint interrupt
  463. /\ -> RxReady
  464. | -> if request queued, call rxstate
  465. | /\ -> setup DMA
  466. | | -> DMA interrupt on completion
  467. | | -> RxReady
  468. | | -> stop DMA
  469. | | -> ack the read
  470. | | -> if data recd = max expected
  471. | | by the request, or host
  472. | | sent a short packet,
  473. | | complete the request,
  474. | | and start the next one.
  475. | |_____________________________________|
  476. | else just wait for the host
  477. | to send the next OUT token.
  478. |__________________________________________________|
  479. * Non-Mentor DMA engines can of course work differently.
  480. */
  481. #endif
  482. /*
  483. * Context: controller locked, IRQs blocked, endpoint selected
  484. */
  485. static void rxstate(struct musb *musb, struct musb_request *req)
  486. {
  487. const u8 epnum = req->epnum;
  488. struct usb_request *request = &req->request;
  489. struct musb_ep *musb_ep;
  490. void __iomem *epio = musb->endpoints[epnum].regs;
  491. unsigned fifo_count = 0;
  492. u16 len;
  493. u16 csr = musb_readw(epio, MUSB_RXCSR);
  494. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  495. if (hw_ep->is_shared_fifo)
  496. musb_ep = &hw_ep->ep_in;
  497. else
  498. musb_ep = &hw_ep->ep_out;
  499. len = musb_ep->packet_sz;
  500. /* We shouldn't get here while DMA is active, but we do... */
  501. if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
  502. DBG(4, "DMA pending...\n");
  503. return;
  504. }
  505. if (csr & MUSB_RXCSR_P_SENDSTALL) {
  506. DBG(5, "%s stalling, RXCSR %04x\n",
  507. musb_ep->end_point.name, csr);
  508. return;
  509. }
  510. if (is_cppi_enabled() && musb_ep->dma) {
  511. struct dma_controller *c = musb->dma_controller;
  512. struct dma_channel *channel = musb_ep->dma;
  513. /* NOTE: CPPI won't actually stop advancing the DMA
  514. * queue after short packet transfers, so this is almost
  515. * always going to run as IRQ-per-packet DMA so that
  516. * faults will be handled correctly.
  517. */
  518. if (c->channel_program(channel,
  519. musb_ep->packet_sz,
  520. !request->short_not_ok,
  521. request->dma + request->actual,
  522. request->length - request->actual)) {
  523. /* make sure that if an rxpkt arrived after the irq,
  524. * the cppi engine will be ready to take it as soon
  525. * as DMA is enabled
  526. */
  527. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  528. | MUSB_RXCSR_DMAMODE);
  529. csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
  530. musb_writew(epio, MUSB_RXCSR, csr);
  531. return;
  532. }
  533. }
  534. if (csr & MUSB_RXCSR_RXPKTRDY) {
  535. len = musb_readw(epio, MUSB_RXCOUNT);
  536. if (request->actual < request->length) {
  537. #ifdef CONFIG_USB_INVENTRA_DMA
  538. if (is_dma_capable() && musb_ep->dma) {
  539. struct dma_controller *c;
  540. struct dma_channel *channel;
  541. int use_dma = 0;
  542. c = musb->dma_controller;
  543. channel = musb_ep->dma;
  544. /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
  545. * mode 0 only. So we do not get endpoint interrupts due to DMA
  546. * completion. We only get interrupts from DMA controller.
  547. *
  548. * We could operate in DMA mode 1 if we knew the size of the tranfer
  549. * in advance. For mass storage class, request->length = what the host
  550. * sends, so that'd work. But for pretty much everything else,
  551. * request->length is routinely more than what the host sends. For
  552. * most these gadgets, end of is signified either by a short packet,
  553. * or filling the last byte of the buffer. (Sending extra data in
  554. * that last pckate should trigger an overflow fault.) But in mode 1,
  555. * we don't get DMA completion interrrupt for short packets.
  556. *
  557. * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
  558. * to get endpoint interrupt on every DMA req, but that didn't seem
  559. * to work reliably.
  560. *
  561. * REVISIT an updated g_file_storage can set req->short_not_ok, which
  562. * then becomes usable as a runtime "use mode 1" hint...
  563. */
  564. csr |= MUSB_RXCSR_DMAENAB;
  565. csr |= MUSB_RXCSR_AUTOCLEAR;
  566. #ifdef USE_MODE1
  567. /* csr |= MUSB_RXCSR_DMAMODE; */
  568. /* this special sequence (enabling and then
  569. * disabling MUSB_RXCSR_DMAMODE) is required
  570. * to get DMAReq to activate
  571. */
  572. musb_writew(epio, MUSB_RXCSR,
  573. csr | MUSB_RXCSR_DMAMODE);
  574. #endif
  575. musb_writew(epio, MUSB_RXCSR, csr);
  576. if (request->actual < request->length) {
  577. int transfer_size = 0;
  578. #ifdef USE_MODE1
  579. transfer_size = min(request->length - request->actual,
  580. channel->max_len);
  581. #else
  582. transfer_size = min(request->length - request->actual,
  583. (unsigned)len);
  584. #endif
  585. if (transfer_size <= musb_ep->packet_sz)
  586. musb_ep->dma->desired_mode = 0;
  587. else
  588. musb_ep->dma->desired_mode = 1;
  589. use_dma = c->channel_program(
  590. channel,
  591. musb_ep->packet_sz,
  592. channel->desired_mode,
  593. request->dma
  594. + request->actual,
  595. transfer_size);
  596. }
  597. if (use_dma)
  598. return;
  599. }
  600. #endif /* Mentor's DMA */
  601. fifo_count = request->length - request->actual;
  602. DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
  603. musb_ep->end_point.name,
  604. len, fifo_count,
  605. musb_ep->packet_sz);
  606. fifo_count = min_t(unsigned, len, fifo_count);
  607. #ifdef CONFIG_USB_TUSB_OMAP_DMA
  608. if (tusb_dma_omap() && musb_ep->dma) {
  609. struct dma_controller *c = musb->dma_controller;
  610. struct dma_channel *channel = musb_ep->dma;
  611. u32 dma_addr = request->dma + request->actual;
  612. int ret;
  613. ret = c->channel_program(channel,
  614. musb_ep->packet_sz,
  615. channel->desired_mode,
  616. dma_addr,
  617. fifo_count);
  618. if (ret)
  619. return;
  620. }
  621. #endif
  622. musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
  623. (request->buf + request->actual));
  624. request->actual += fifo_count;
  625. /* REVISIT if we left anything in the fifo, flush
  626. * it and report -EOVERFLOW
  627. */
  628. /* ack the read! */
  629. csr |= MUSB_RXCSR_P_WZC_BITS;
  630. csr &= ~MUSB_RXCSR_RXPKTRDY;
  631. musb_writew(epio, MUSB_RXCSR, csr);
  632. }
  633. }
  634. /* reach the end or short packet detected */
  635. if (request->actual == request->length || len < musb_ep->packet_sz)
  636. musb_g_giveback(musb_ep, request, 0);
  637. }
  638. /*
  639. * Data ready for a request; called from IRQ
  640. */
  641. void musb_g_rx(struct musb *musb, u8 epnum)
  642. {
  643. u16 csr;
  644. struct usb_request *request;
  645. void __iomem *mbase = musb->mregs;
  646. struct musb_ep *musb_ep;
  647. void __iomem *epio = musb->endpoints[epnum].regs;
  648. struct dma_channel *dma;
  649. struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
  650. if (hw_ep->is_shared_fifo)
  651. musb_ep = &hw_ep->ep_in;
  652. else
  653. musb_ep = &hw_ep->ep_out;
  654. musb_ep_select(mbase, epnum);
  655. request = next_request(musb_ep);
  656. if (!request)
  657. return;
  658. csr = musb_readw(epio, MUSB_RXCSR);
  659. dma = is_dma_capable() ? musb_ep->dma : NULL;
  660. DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
  661. csr, dma ? " (dma)" : "", request);
  662. if (csr & MUSB_RXCSR_P_SENTSTALL) {
  663. csr |= MUSB_RXCSR_P_WZC_BITS;
  664. csr &= ~MUSB_RXCSR_P_SENTSTALL;
  665. musb_writew(epio, MUSB_RXCSR, csr);
  666. return;
  667. }
  668. if (csr & MUSB_RXCSR_P_OVERRUN) {
  669. /* csr |= MUSB_RXCSR_P_WZC_BITS; */
  670. csr &= ~MUSB_RXCSR_P_OVERRUN;
  671. musb_writew(epio, MUSB_RXCSR, csr);
  672. DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
  673. if (request && request->status == -EINPROGRESS)
  674. request->status = -EOVERFLOW;
  675. }
  676. if (csr & MUSB_RXCSR_INCOMPRX) {
  677. /* REVISIT not necessarily an error */
  678. DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
  679. }
  680. if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
  681. /* "should not happen"; likely RXPKTRDY pending for DMA */
  682. DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
  683. "%s busy, csr %04x\n",
  684. musb_ep->end_point.name, csr);
  685. return;
  686. }
  687. if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
  688. csr &= ~(MUSB_RXCSR_AUTOCLEAR
  689. | MUSB_RXCSR_DMAENAB
  690. | MUSB_RXCSR_DMAMODE);
  691. musb_writew(epio, MUSB_RXCSR,
  692. MUSB_RXCSR_P_WZC_BITS | csr);
  693. request->actual += musb_ep->dma->actual_len;
  694. DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
  695. epnum, csr,
  696. musb_readw(epio, MUSB_RXCSR),
  697. musb_ep->dma->actual_len, request);
  698. #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
  699. /* Autoclear doesn't clear RxPktRdy for short packets */
  700. if ((dma->desired_mode == 0)
  701. || (dma->actual_len
  702. & (musb_ep->packet_sz - 1))) {
  703. /* ack the read! */
  704. csr &= ~MUSB_RXCSR_RXPKTRDY;
  705. musb_writew(epio, MUSB_RXCSR, csr);
  706. }
  707. /* incomplete, and not short? wait for next IN packet */
  708. if ((request->actual < request->length)
  709. && (musb_ep->dma->actual_len
  710. == musb_ep->packet_sz))
  711. return;
  712. #endif
  713. musb_g_giveback(musb_ep, request, 0);
  714. request = next_request(musb_ep);
  715. if (!request)
  716. return;
  717. }
  718. /* analyze request if the ep is hot */
  719. if (request)
  720. rxstate(musb, to_musb_request(request));
  721. else
  722. DBG(3, "packet waiting for %s%s request\n",
  723. musb_ep->desc ? "" : "inactive ",
  724. musb_ep->end_point.name);
  725. return;
  726. }
  727. /* ------------------------------------------------------------ */
  728. static int musb_gadget_enable(struct usb_ep *ep,
  729. const struct usb_endpoint_descriptor *desc)
  730. {
  731. unsigned long flags;
  732. struct musb_ep *musb_ep;
  733. struct musb_hw_ep *hw_ep;
  734. void __iomem *regs;
  735. struct musb *musb;
  736. void __iomem *mbase;
  737. u8 epnum;
  738. u16 csr;
  739. unsigned tmp;
  740. int status = -EINVAL;
  741. if (!ep || !desc)
  742. return -EINVAL;
  743. musb_ep = to_musb_ep(ep);
  744. hw_ep = musb_ep->hw_ep;
  745. regs = hw_ep->regs;
  746. musb = musb_ep->musb;
  747. mbase = musb->mregs;
  748. epnum = musb_ep->current_epnum;
  749. spin_lock_irqsave(&musb->lock, flags);
  750. if (musb_ep->desc) {
  751. status = -EBUSY;
  752. goto fail;
  753. }
  754. musb_ep->type = usb_endpoint_type(desc);
  755. /* check direction and (later) maxpacket size against endpoint */
  756. if (usb_endpoint_num(desc) != epnum)
  757. goto fail;
  758. /* REVISIT this rules out high bandwidth periodic transfers */
  759. tmp = le16_to_cpu(desc->wMaxPacketSize);
  760. if (tmp & ~0x07ff)
  761. goto fail;
  762. musb_ep->packet_sz = tmp;
  763. /* enable the interrupts for the endpoint, set the endpoint
  764. * packet size (or fail), set the mode, clear the fifo
  765. */
  766. musb_ep_select(mbase, epnum);
  767. if (usb_endpoint_dir_in(desc)) {
  768. u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
  769. if (hw_ep->is_shared_fifo)
  770. musb_ep->is_in = 1;
  771. if (!musb_ep->is_in)
  772. goto fail;
  773. if (tmp > hw_ep->max_packet_sz_tx)
  774. goto fail;
  775. int_txe |= (1 << epnum);
  776. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  777. /* REVISIT if can_bulk_split(), use by updating "tmp";
  778. * likewise high bandwidth periodic tx
  779. */
  780. /* Set TXMAXP with the FIFO size of the endpoint
  781. * to disable double buffering mode. Currently, It seems that double
  782. * buffering has problem if musb RTL revision number < 2.0.
  783. */
  784. if (musb->hwvers < MUSB_HWVERS_2000)
  785. musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
  786. else
  787. musb_writew(regs, MUSB_TXMAXP, tmp);
  788. csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
  789. if (musb_readw(regs, MUSB_TXCSR)
  790. & MUSB_TXCSR_FIFONOTEMPTY)
  791. csr |= MUSB_TXCSR_FLUSHFIFO;
  792. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  793. csr |= MUSB_TXCSR_P_ISO;
  794. /* set twice in case of double buffering */
  795. musb_writew(regs, MUSB_TXCSR, csr);
  796. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  797. musb_writew(regs, MUSB_TXCSR, csr);
  798. } else {
  799. u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
  800. if (hw_ep->is_shared_fifo)
  801. musb_ep->is_in = 0;
  802. if (musb_ep->is_in)
  803. goto fail;
  804. if (tmp > hw_ep->max_packet_sz_rx)
  805. goto fail;
  806. int_rxe |= (1 << epnum);
  807. musb_writew(mbase, MUSB_INTRRXE, int_rxe);
  808. /* REVISIT if can_bulk_combine() use by updating "tmp"
  809. * likewise high bandwidth periodic rx
  810. */
  811. /* Set RXMAXP with the FIFO size of the endpoint
  812. * to disable double buffering mode.
  813. */
  814. if (musb->hwvers < MUSB_HWVERS_2000)
  815. musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_rx);
  816. else
  817. musb_writew(regs, MUSB_RXMAXP, tmp);
  818. /* force shared fifo to OUT-only mode */
  819. if (hw_ep->is_shared_fifo) {
  820. csr = musb_readw(regs, MUSB_TXCSR);
  821. csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
  822. musb_writew(regs, MUSB_TXCSR, csr);
  823. }
  824. csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
  825. if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
  826. csr |= MUSB_RXCSR_P_ISO;
  827. else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
  828. csr |= MUSB_RXCSR_DISNYET;
  829. /* set twice in case of double buffering */
  830. musb_writew(regs, MUSB_RXCSR, csr);
  831. musb_writew(regs, MUSB_RXCSR, csr);
  832. }
  833. /* NOTE: all the I/O code _should_ work fine without DMA, in case
  834. * for some reason you run out of channels here.
  835. */
  836. if (is_dma_capable() && musb->dma_controller) {
  837. struct dma_controller *c = musb->dma_controller;
  838. musb_ep->dma = c->channel_alloc(c, hw_ep,
  839. (desc->bEndpointAddress & USB_DIR_IN));
  840. } else
  841. musb_ep->dma = NULL;
  842. musb_ep->desc = desc;
  843. musb_ep->busy = 0;
  844. musb_ep->wedged = 0;
  845. status = 0;
  846. pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
  847. musb_driver_name, musb_ep->end_point.name,
  848. ({ char *s; switch (musb_ep->type) {
  849. case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
  850. case USB_ENDPOINT_XFER_INT: s = "int"; break;
  851. default: s = "iso"; break;
  852. }; s; }),
  853. musb_ep->is_in ? "IN" : "OUT",
  854. musb_ep->dma ? "dma, " : "",
  855. musb_ep->packet_sz);
  856. schedule_work(&musb->irq_work);
  857. fail:
  858. spin_unlock_irqrestore(&musb->lock, flags);
  859. return status;
  860. }
  861. /*
  862. * Disable an endpoint flushing all requests queued.
  863. */
  864. static int musb_gadget_disable(struct usb_ep *ep)
  865. {
  866. unsigned long flags;
  867. struct musb *musb;
  868. u8 epnum;
  869. struct musb_ep *musb_ep;
  870. void __iomem *epio;
  871. int status = 0;
  872. musb_ep = to_musb_ep(ep);
  873. musb = musb_ep->musb;
  874. epnum = musb_ep->current_epnum;
  875. epio = musb->endpoints[epnum].regs;
  876. spin_lock_irqsave(&musb->lock, flags);
  877. musb_ep_select(musb->mregs, epnum);
  878. /* zero the endpoint sizes */
  879. if (musb_ep->is_in) {
  880. u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
  881. int_txe &= ~(1 << epnum);
  882. musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
  883. musb_writew(epio, MUSB_TXMAXP, 0);
  884. } else {
  885. u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
  886. int_rxe &= ~(1 << epnum);
  887. musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
  888. musb_writew(epio, MUSB_RXMAXP, 0);
  889. }
  890. musb_ep->desc = NULL;
  891. /* abort all pending DMA and requests */
  892. nuke(musb_ep, -ESHUTDOWN);
  893. schedule_work(&musb->irq_work);
  894. spin_unlock_irqrestore(&(musb->lock), flags);
  895. DBG(2, "%s\n", musb_ep->end_point.name);
  896. return status;
  897. }
  898. /*
  899. * Allocate a request for an endpoint.
  900. * Reused by ep0 code.
  901. */
  902. struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  903. {
  904. struct musb_ep *musb_ep = to_musb_ep(ep);
  905. struct musb_request *request = NULL;
  906. request = kzalloc(sizeof *request, gfp_flags);
  907. if (request) {
  908. INIT_LIST_HEAD(&request->request.list);
  909. request->request.dma = DMA_ADDR_INVALID;
  910. request->epnum = musb_ep->current_epnum;
  911. request->ep = musb_ep;
  912. }
  913. return &request->request;
  914. }
  915. /*
  916. * Free a request
  917. * Reused by ep0 code.
  918. */
  919. void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  920. {
  921. kfree(to_musb_request(req));
  922. }
  923. static LIST_HEAD(buffers);
  924. struct free_record {
  925. struct list_head list;
  926. struct device *dev;
  927. unsigned bytes;
  928. dma_addr_t dma;
  929. };
  930. /*
  931. * Context: controller locked, IRQs blocked.
  932. */
  933. void musb_ep_restart(struct musb *musb, struct musb_request *req)
  934. {
  935. DBG(3, "<== %s request %p len %u on hw_ep%d\n",
  936. req->tx ? "TX/IN" : "RX/OUT",
  937. &req->request, req->request.length, req->epnum);
  938. musb_ep_select(musb->mregs, req->epnum);
  939. if (req->tx)
  940. txstate(musb, req);
  941. else
  942. rxstate(musb, req);
  943. }
  944. static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
  945. gfp_t gfp_flags)
  946. {
  947. struct musb_ep *musb_ep;
  948. struct musb_request *request;
  949. struct musb *musb;
  950. int status = 0;
  951. unsigned long lockflags;
  952. if (!ep || !req)
  953. return -EINVAL;
  954. if (!req->buf)
  955. return -ENODATA;
  956. musb_ep = to_musb_ep(ep);
  957. musb = musb_ep->musb;
  958. request = to_musb_request(req);
  959. request->musb = musb;
  960. if (request->ep != musb_ep)
  961. return -EINVAL;
  962. DBG(4, "<== to %s request=%p\n", ep->name, req);
  963. /* request is mine now... */
  964. request->request.actual = 0;
  965. request->request.status = -EINPROGRESS;
  966. request->epnum = musb_ep->current_epnum;
  967. request->tx = musb_ep->is_in;
  968. if (is_dma_capable() && musb_ep->dma) {
  969. if (request->request.dma == DMA_ADDR_INVALID) {
  970. request->request.dma = dma_map_single(
  971. musb->controller,
  972. request->request.buf,
  973. request->request.length,
  974. request->tx
  975. ? DMA_TO_DEVICE
  976. : DMA_FROM_DEVICE);
  977. request->mapped = 1;
  978. } else {
  979. dma_sync_single_for_device(musb->controller,
  980. request->request.dma,
  981. request->request.length,
  982. request->tx
  983. ? DMA_TO_DEVICE
  984. : DMA_FROM_DEVICE);
  985. request->mapped = 0;
  986. }
  987. } else if (!req->buf) {
  988. return -ENODATA;
  989. } else
  990. request->mapped = 0;
  991. spin_lock_irqsave(&musb->lock, lockflags);
  992. /* don't queue if the ep is down */
  993. if (!musb_ep->desc) {
  994. DBG(4, "req %p queued to %s while ep %s\n",
  995. req, ep->name, "disabled");
  996. status = -ESHUTDOWN;
  997. goto cleanup;
  998. }
  999. /* add request to the list */
  1000. list_add_tail(&(request->request.list), &(musb_ep->req_list));
  1001. /* it this is the head of the queue, start i/o ... */
  1002. if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
  1003. musb_ep_restart(musb, request);
  1004. cleanup:
  1005. spin_unlock_irqrestore(&musb->lock, lockflags);
  1006. return status;
  1007. }
  1008. static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
  1009. {
  1010. struct musb_ep *musb_ep = to_musb_ep(ep);
  1011. struct usb_request *r;
  1012. unsigned long flags;
  1013. int status = 0;
  1014. struct musb *musb = musb_ep->musb;
  1015. if (!ep || !request || to_musb_request(request)->ep != musb_ep)
  1016. return -EINVAL;
  1017. spin_lock_irqsave(&musb->lock, flags);
  1018. list_for_each_entry(r, &musb_ep->req_list, list) {
  1019. if (r == request)
  1020. break;
  1021. }
  1022. if (r != request) {
  1023. DBG(3, "request %p not queued to %s\n", request, ep->name);
  1024. status = -EINVAL;
  1025. goto done;
  1026. }
  1027. /* if the hardware doesn't have the request, easy ... */
  1028. if (musb_ep->req_list.next != &request->list || musb_ep->busy)
  1029. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1030. /* ... else abort the dma transfer ... */
  1031. else if (is_dma_capable() && musb_ep->dma) {
  1032. struct dma_controller *c = musb->dma_controller;
  1033. musb_ep_select(musb->mregs, musb_ep->current_epnum);
  1034. if (c->channel_abort)
  1035. status = c->channel_abort(musb_ep->dma);
  1036. else
  1037. status = -EBUSY;
  1038. if (status == 0)
  1039. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1040. } else {
  1041. /* NOTE: by sticking to easily tested hardware/driver states,
  1042. * we leave counting of in-flight packets imprecise.
  1043. */
  1044. musb_g_giveback(musb_ep, request, -ECONNRESET);
  1045. }
  1046. done:
  1047. spin_unlock_irqrestore(&musb->lock, flags);
  1048. return status;
  1049. }
  1050. /*
  1051. * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
  1052. * data but will queue requests.
  1053. *
  1054. * exported to ep0 code
  1055. */
  1056. static int musb_gadget_set_halt(struct usb_ep *ep, int value)
  1057. {
  1058. struct musb_ep *musb_ep = to_musb_ep(ep);
  1059. u8 epnum = musb_ep->current_epnum;
  1060. struct musb *musb = musb_ep->musb;
  1061. void __iomem *epio = musb->endpoints[epnum].regs;
  1062. void __iomem *mbase;
  1063. unsigned long flags;
  1064. u16 csr;
  1065. struct musb_request *request;
  1066. int status = 0;
  1067. if (!ep)
  1068. return -EINVAL;
  1069. mbase = musb->mregs;
  1070. spin_lock_irqsave(&musb->lock, flags);
  1071. if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
  1072. status = -EINVAL;
  1073. goto done;
  1074. }
  1075. musb_ep_select(mbase, epnum);
  1076. request = to_musb_request(next_request(musb_ep));
  1077. if (value) {
  1078. if (request) {
  1079. DBG(3, "request in progress, cannot halt %s\n",
  1080. ep->name);
  1081. status = -EAGAIN;
  1082. goto done;
  1083. }
  1084. /* Cannot portably stall with non-empty FIFO */
  1085. if (musb_ep->is_in) {
  1086. csr = musb_readw(epio, MUSB_TXCSR);
  1087. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1088. DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
  1089. status = -EAGAIN;
  1090. goto done;
  1091. }
  1092. }
  1093. } else
  1094. musb_ep->wedged = 0;
  1095. /* set/clear the stall and toggle bits */
  1096. DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
  1097. if (musb_ep->is_in) {
  1098. csr = musb_readw(epio, MUSB_TXCSR);
  1099. csr |= MUSB_TXCSR_P_WZC_BITS
  1100. | MUSB_TXCSR_CLRDATATOG;
  1101. if (value)
  1102. csr |= MUSB_TXCSR_P_SENDSTALL;
  1103. else
  1104. csr &= ~(MUSB_TXCSR_P_SENDSTALL
  1105. | MUSB_TXCSR_P_SENTSTALL);
  1106. csr &= ~MUSB_TXCSR_TXPKTRDY;
  1107. musb_writew(epio, MUSB_TXCSR, csr);
  1108. } else {
  1109. csr = musb_readw(epio, MUSB_RXCSR);
  1110. csr |= MUSB_RXCSR_P_WZC_BITS
  1111. | MUSB_RXCSR_FLUSHFIFO
  1112. | MUSB_RXCSR_CLRDATATOG;
  1113. if (value)
  1114. csr |= MUSB_RXCSR_P_SENDSTALL;
  1115. else
  1116. csr &= ~(MUSB_RXCSR_P_SENDSTALL
  1117. | MUSB_RXCSR_P_SENTSTALL);
  1118. musb_writew(epio, MUSB_RXCSR, csr);
  1119. }
  1120. /* maybe start the first request in the queue */
  1121. if (!musb_ep->busy && !value && request) {
  1122. DBG(3, "restarting the request\n");
  1123. musb_ep_restart(musb, request);
  1124. }
  1125. done:
  1126. spin_unlock_irqrestore(&musb->lock, flags);
  1127. return status;
  1128. }
  1129. /*
  1130. * Sets the halt feature with the clear requests ignored
  1131. */
  1132. static int musb_gadget_set_wedge(struct usb_ep *ep)
  1133. {
  1134. struct musb_ep *musb_ep = to_musb_ep(ep);
  1135. if (!ep)
  1136. return -EINVAL;
  1137. musb_ep->wedged = 1;
  1138. return usb_ep_set_halt(ep);
  1139. }
  1140. static int musb_gadget_fifo_status(struct usb_ep *ep)
  1141. {
  1142. struct musb_ep *musb_ep = to_musb_ep(ep);
  1143. void __iomem *epio = musb_ep->hw_ep->regs;
  1144. int retval = -EINVAL;
  1145. if (musb_ep->desc && !musb_ep->is_in) {
  1146. struct musb *musb = musb_ep->musb;
  1147. int epnum = musb_ep->current_epnum;
  1148. void __iomem *mbase = musb->mregs;
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&musb->lock, flags);
  1151. musb_ep_select(mbase, epnum);
  1152. /* FIXME return zero unless RXPKTRDY is set */
  1153. retval = musb_readw(epio, MUSB_RXCOUNT);
  1154. spin_unlock_irqrestore(&musb->lock, flags);
  1155. }
  1156. return retval;
  1157. }
  1158. static void musb_gadget_fifo_flush(struct usb_ep *ep)
  1159. {
  1160. struct musb_ep *musb_ep = to_musb_ep(ep);
  1161. struct musb *musb = musb_ep->musb;
  1162. u8 epnum = musb_ep->current_epnum;
  1163. void __iomem *epio = musb->endpoints[epnum].regs;
  1164. void __iomem *mbase;
  1165. unsigned long flags;
  1166. u16 csr, int_txe;
  1167. mbase = musb->mregs;
  1168. spin_lock_irqsave(&musb->lock, flags);
  1169. musb_ep_select(mbase, (u8) epnum);
  1170. /* disable interrupts */
  1171. int_txe = musb_readw(mbase, MUSB_INTRTXE);
  1172. musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
  1173. if (musb_ep->is_in) {
  1174. csr = musb_readw(epio, MUSB_TXCSR);
  1175. if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1176. csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
  1177. musb_writew(epio, MUSB_TXCSR, csr);
  1178. /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
  1179. musb_writew(epio, MUSB_TXCSR, csr);
  1180. }
  1181. } else {
  1182. csr = musb_readw(epio, MUSB_RXCSR);
  1183. csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
  1184. musb_writew(epio, MUSB_RXCSR, csr);
  1185. musb_writew(epio, MUSB_RXCSR, csr);
  1186. }
  1187. /* re-enable interrupt */
  1188. musb_writew(mbase, MUSB_INTRTXE, int_txe);
  1189. spin_unlock_irqrestore(&musb->lock, flags);
  1190. }
  1191. static const struct usb_ep_ops musb_ep_ops = {
  1192. .enable = musb_gadget_enable,
  1193. .disable = musb_gadget_disable,
  1194. .alloc_request = musb_alloc_request,
  1195. .free_request = musb_free_request,
  1196. .queue = musb_gadget_queue,
  1197. .dequeue = musb_gadget_dequeue,
  1198. .set_halt = musb_gadget_set_halt,
  1199. .set_wedge = musb_gadget_set_wedge,
  1200. .fifo_status = musb_gadget_fifo_status,
  1201. .fifo_flush = musb_gadget_fifo_flush
  1202. };
  1203. /* ----------------------------------------------------------------------- */
  1204. static int musb_gadget_get_frame(struct usb_gadget *gadget)
  1205. {
  1206. struct musb *musb = gadget_to_musb(gadget);
  1207. return (int)musb_readw(musb->mregs, MUSB_FRAME);
  1208. }
  1209. static int musb_gadget_wakeup(struct usb_gadget *gadget)
  1210. {
  1211. struct musb *musb = gadget_to_musb(gadget);
  1212. void __iomem *mregs = musb->mregs;
  1213. unsigned long flags;
  1214. int status = -EINVAL;
  1215. u8 power, devctl;
  1216. int retries;
  1217. spin_lock_irqsave(&musb->lock, flags);
  1218. switch (musb->xceiv->state) {
  1219. case OTG_STATE_B_PERIPHERAL:
  1220. /* NOTE: OTG state machine doesn't include B_SUSPENDED;
  1221. * that's part of the standard usb 1.1 state machine, and
  1222. * doesn't affect OTG transitions.
  1223. */
  1224. if (musb->may_wakeup && musb->is_suspended)
  1225. break;
  1226. goto done;
  1227. case OTG_STATE_B_IDLE:
  1228. /* Start SRP ... OTG not required. */
  1229. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1230. DBG(2, "Sending SRP: devctl: %02x\n", devctl);
  1231. devctl |= MUSB_DEVCTL_SESSION;
  1232. musb_writeb(mregs, MUSB_DEVCTL, devctl);
  1233. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1234. retries = 100;
  1235. while (!(devctl & MUSB_DEVCTL_SESSION)) {
  1236. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1237. if (retries-- < 1)
  1238. break;
  1239. }
  1240. retries = 10000;
  1241. while (devctl & MUSB_DEVCTL_SESSION) {
  1242. devctl = musb_readb(mregs, MUSB_DEVCTL);
  1243. if (retries-- < 1)
  1244. break;
  1245. }
  1246. /* Block idling for at least 1s */
  1247. musb_platform_try_idle(musb,
  1248. jiffies + msecs_to_jiffies(1 * HZ));
  1249. status = 0;
  1250. goto done;
  1251. default:
  1252. DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
  1253. goto done;
  1254. }
  1255. status = 0;
  1256. power = musb_readb(mregs, MUSB_POWER);
  1257. power |= MUSB_POWER_RESUME;
  1258. musb_writeb(mregs, MUSB_POWER, power);
  1259. DBG(2, "issue wakeup\n");
  1260. /* FIXME do this next chunk in a timer callback, no udelay */
  1261. mdelay(2);
  1262. power = musb_readb(mregs, MUSB_POWER);
  1263. power &= ~MUSB_POWER_RESUME;
  1264. musb_writeb(mregs, MUSB_POWER, power);
  1265. done:
  1266. spin_unlock_irqrestore(&musb->lock, flags);
  1267. return status;
  1268. }
  1269. static int
  1270. musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
  1271. {
  1272. struct musb *musb = gadget_to_musb(gadget);
  1273. musb->is_self_powered = !!is_selfpowered;
  1274. return 0;
  1275. }
  1276. static void musb_pullup(struct musb *musb, int is_on)
  1277. {
  1278. u8 power;
  1279. power = musb_readb(musb->mregs, MUSB_POWER);
  1280. if (is_on)
  1281. power |= MUSB_POWER_SOFTCONN;
  1282. else
  1283. power &= ~MUSB_POWER_SOFTCONN;
  1284. /* FIXME if on, HdrcStart; if off, HdrcStop */
  1285. DBG(3, "gadget %s D+ pullup %s\n",
  1286. musb->gadget_driver->function, is_on ? "on" : "off");
  1287. musb_writeb(musb->mregs, MUSB_POWER, power);
  1288. }
  1289. #if 0
  1290. static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
  1291. {
  1292. DBG(2, "<= %s =>\n", __func__);
  1293. /*
  1294. * FIXME iff driver's softconnect flag is set (as it is during probe,
  1295. * though that can clear it), just musb_pullup().
  1296. */
  1297. return -EINVAL;
  1298. }
  1299. #endif
  1300. static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1301. {
  1302. struct musb *musb = gadget_to_musb(gadget);
  1303. if (!musb->xceiv->set_power)
  1304. return -EOPNOTSUPP;
  1305. return otg_set_power(musb->xceiv, mA);
  1306. }
  1307. static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
  1308. {
  1309. struct musb *musb = gadget_to_musb(gadget);
  1310. unsigned long flags;
  1311. is_on = !!is_on;
  1312. /* NOTE: this assumes we are sensing vbus; we'd rather
  1313. * not pullup unless the B-session is active.
  1314. */
  1315. spin_lock_irqsave(&musb->lock, flags);
  1316. if (is_on != musb->softconnect) {
  1317. musb->softconnect = is_on;
  1318. musb_pullup(musb, is_on);
  1319. }
  1320. spin_unlock_irqrestore(&musb->lock, flags);
  1321. return 0;
  1322. }
  1323. static const struct usb_gadget_ops musb_gadget_operations = {
  1324. .get_frame = musb_gadget_get_frame,
  1325. .wakeup = musb_gadget_wakeup,
  1326. .set_selfpowered = musb_gadget_set_self_powered,
  1327. /* .vbus_session = musb_gadget_vbus_session, */
  1328. .vbus_draw = musb_gadget_vbus_draw,
  1329. .pullup = musb_gadget_pullup,
  1330. };
  1331. /* ----------------------------------------------------------------------- */
  1332. /* Registration */
  1333. /* Only this registration code "knows" the rule (from USB standards)
  1334. * about there being only one external upstream port. It assumes
  1335. * all peripheral ports are external...
  1336. */
  1337. static struct musb *the_gadget;
  1338. static void musb_gadget_release(struct device *dev)
  1339. {
  1340. /* kref_put(WHAT) */
  1341. dev_dbg(dev, "%s\n", __func__);
  1342. }
  1343. static void __init
  1344. init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
  1345. {
  1346. struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
  1347. memset(ep, 0, sizeof *ep);
  1348. ep->current_epnum = epnum;
  1349. ep->musb = musb;
  1350. ep->hw_ep = hw_ep;
  1351. ep->is_in = is_in;
  1352. INIT_LIST_HEAD(&ep->req_list);
  1353. sprintf(ep->name, "ep%d%s", epnum,
  1354. (!epnum || hw_ep->is_shared_fifo) ? "" : (
  1355. is_in ? "in" : "out"));
  1356. ep->end_point.name = ep->name;
  1357. INIT_LIST_HEAD(&ep->end_point.ep_list);
  1358. if (!epnum) {
  1359. ep->end_point.maxpacket = 64;
  1360. ep->end_point.ops = &musb_g_ep0_ops;
  1361. musb->g.ep0 = &ep->end_point;
  1362. } else {
  1363. if (is_in)
  1364. ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
  1365. else
  1366. ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
  1367. ep->end_point.ops = &musb_ep_ops;
  1368. list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
  1369. }
  1370. }
  1371. /*
  1372. * Initialize the endpoints exposed to peripheral drivers, with backlinks
  1373. * to the rest of the driver state.
  1374. */
  1375. static inline void __init musb_g_init_endpoints(struct musb *musb)
  1376. {
  1377. u8 epnum;
  1378. struct musb_hw_ep *hw_ep;
  1379. unsigned count = 0;
  1380. /* intialize endpoint list just once */
  1381. INIT_LIST_HEAD(&(musb->g.ep_list));
  1382. for (epnum = 0, hw_ep = musb->endpoints;
  1383. epnum < musb->nr_endpoints;
  1384. epnum++, hw_ep++) {
  1385. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1386. init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
  1387. count++;
  1388. } else {
  1389. if (hw_ep->max_packet_sz_tx) {
  1390. init_peripheral_ep(musb, &hw_ep->ep_in,
  1391. epnum, 1);
  1392. count++;
  1393. }
  1394. if (hw_ep->max_packet_sz_rx) {
  1395. init_peripheral_ep(musb, &hw_ep->ep_out,
  1396. epnum, 0);
  1397. count++;
  1398. }
  1399. }
  1400. }
  1401. }
  1402. /* called once during driver setup to initialize and link into
  1403. * the driver model; memory is zeroed.
  1404. */
  1405. int __init musb_gadget_setup(struct musb *musb)
  1406. {
  1407. int status;
  1408. /* REVISIT minor race: if (erroneously) setting up two
  1409. * musb peripherals at the same time, only the bus lock
  1410. * is probably held.
  1411. */
  1412. if (the_gadget)
  1413. return -EBUSY;
  1414. the_gadget = musb;
  1415. musb->g.ops = &musb_gadget_operations;
  1416. musb->g.is_dualspeed = 1;
  1417. musb->g.speed = USB_SPEED_UNKNOWN;
  1418. /* this "gadget" abstracts/virtualizes the controller */
  1419. dev_set_name(&musb->g.dev, "gadget");
  1420. musb->g.dev.parent = musb->controller;
  1421. musb->g.dev.dma_mask = musb->controller->dma_mask;
  1422. musb->g.dev.release = musb_gadget_release;
  1423. musb->g.name = musb_driver_name;
  1424. if (is_otg_enabled(musb))
  1425. musb->g.is_otg = 1;
  1426. musb_g_init_endpoints(musb);
  1427. musb->is_active = 0;
  1428. musb_platform_try_idle(musb, 0);
  1429. status = device_register(&musb->g.dev);
  1430. if (status != 0)
  1431. the_gadget = NULL;
  1432. return status;
  1433. }
  1434. void musb_gadget_cleanup(struct musb *musb)
  1435. {
  1436. if (musb != the_gadget)
  1437. return;
  1438. device_unregister(&musb->g.dev);
  1439. the_gadget = NULL;
  1440. }
  1441. /*
  1442. * Register the gadget driver. Used by gadget drivers when
  1443. * registering themselves with the controller.
  1444. *
  1445. * -EINVAL something went wrong (not driver)
  1446. * -EBUSY another gadget is already using the controller
  1447. * -ENOMEM no memeory to perform the operation
  1448. *
  1449. * @param driver the gadget driver
  1450. * @return <0 if error, 0 if everything is fine
  1451. */
  1452. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1453. {
  1454. int retval;
  1455. unsigned long flags;
  1456. struct musb *musb = the_gadget;
  1457. if (!driver
  1458. || driver->speed != USB_SPEED_HIGH
  1459. || !driver->bind
  1460. || !driver->setup)
  1461. return -EINVAL;
  1462. /* driver must be initialized to support peripheral mode */
  1463. if (!musb) {
  1464. DBG(1, "%s, no dev??\n", __func__);
  1465. return -ENODEV;
  1466. }
  1467. DBG(3, "registering driver %s\n", driver->function);
  1468. spin_lock_irqsave(&musb->lock, flags);
  1469. if (musb->gadget_driver) {
  1470. DBG(1, "%s is already bound to %s\n",
  1471. musb_driver_name,
  1472. musb->gadget_driver->driver.name);
  1473. retval = -EBUSY;
  1474. } else {
  1475. musb->gadget_driver = driver;
  1476. musb->g.dev.driver = &driver->driver;
  1477. driver->driver.bus = NULL;
  1478. musb->softconnect = 1;
  1479. retval = 0;
  1480. }
  1481. spin_unlock_irqrestore(&musb->lock, flags);
  1482. if (retval == 0) {
  1483. retval = driver->bind(&musb->g);
  1484. if (retval != 0) {
  1485. DBG(3, "bind to driver %s failed --> %d\n",
  1486. driver->driver.name, retval);
  1487. musb->gadget_driver = NULL;
  1488. musb->g.dev.driver = NULL;
  1489. }
  1490. spin_lock_irqsave(&musb->lock, flags);
  1491. otg_set_peripheral(musb->xceiv, &musb->g);
  1492. musb->xceiv->state = OTG_STATE_B_IDLE;
  1493. musb->is_active = 1;
  1494. /* FIXME this ignores the softconnect flag. Drivers are
  1495. * allowed hold the peripheral inactive until for example
  1496. * userspace hooks up printer hardware or DSP codecs, so
  1497. * hosts only see fully functional devices.
  1498. */
  1499. if (!is_otg_enabled(musb))
  1500. musb_start(musb);
  1501. otg_set_peripheral(musb->xceiv, &musb->g);
  1502. spin_unlock_irqrestore(&musb->lock, flags);
  1503. if (is_otg_enabled(musb)) {
  1504. DBG(3, "OTG startup...\n");
  1505. /* REVISIT: funcall to other code, which also
  1506. * handles power budgeting ... this way also
  1507. * ensures HdrcStart is indirectly called.
  1508. */
  1509. retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
  1510. if (retval < 0) {
  1511. DBG(1, "add_hcd failed, %d\n", retval);
  1512. spin_lock_irqsave(&musb->lock, flags);
  1513. otg_set_peripheral(musb->xceiv, NULL);
  1514. musb->gadget_driver = NULL;
  1515. musb->g.dev.driver = NULL;
  1516. spin_unlock_irqrestore(&musb->lock, flags);
  1517. }
  1518. }
  1519. }
  1520. return retval;
  1521. }
  1522. EXPORT_SYMBOL(usb_gadget_register_driver);
  1523. static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
  1524. {
  1525. int i;
  1526. struct musb_hw_ep *hw_ep;
  1527. /* don't disconnect if it's not connected */
  1528. if (musb->g.speed == USB_SPEED_UNKNOWN)
  1529. driver = NULL;
  1530. else
  1531. musb->g.speed = USB_SPEED_UNKNOWN;
  1532. /* deactivate the hardware */
  1533. if (musb->softconnect) {
  1534. musb->softconnect = 0;
  1535. musb_pullup(musb, 0);
  1536. }
  1537. musb_stop(musb);
  1538. /* killing any outstanding requests will quiesce the driver;
  1539. * then report disconnect
  1540. */
  1541. if (driver) {
  1542. for (i = 0, hw_ep = musb->endpoints;
  1543. i < musb->nr_endpoints;
  1544. i++, hw_ep++) {
  1545. musb_ep_select(musb->mregs, i);
  1546. if (hw_ep->is_shared_fifo /* || !epnum */) {
  1547. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1548. } else {
  1549. if (hw_ep->max_packet_sz_tx)
  1550. nuke(&hw_ep->ep_in, -ESHUTDOWN);
  1551. if (hw_ep->max_packet_sz_rx)
  1552. nuke(&hw_ep->ep_out, -ESHUTDOWN);
  1553. }
  1554. }
  1555. spin_unlock(&musb->lock);
  1556. driver->disconnect(&musb->g);
  1557. spin_lock(&musb->lock);
  1558. }
  1559. }
  1560. /*
  1561. * Unregister the gadget driver. Used by gadget drivers when
  1562. * unregistering themselves from the controller.
  1563. *
  1564. * @param driver the gadget driver to unregister
  1565. */
  1566. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1567. {
  1568. unsigned long flags;
  1569. int retval = 0;
  1570. struct musb *musb = the_gadget;
  1571. if (!driver || !driver->unbind || !musb)
  1572. return -EINVAL;
  1573. /* REVISIT always use otg_set_peripheral() here too;
  1574. * this needs to shut down the OTG engine.
  1575. */
  1576. spin_lock_irqsave(&musb->lock, flags);
  1577. #ifdef CONFIG_USB_MUSB_OTG
  1578. musb_hnp_stop(musb);
  1579. #endif
  1580. if (musb->gadget_driver == driver) {
  1581. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1582. musb->xceiv->state = OTG_STATE_UNDEFINED;
  1583. stop_activity(musb, driver);
  1584. otg_set_peripheral(musb->xceiv, NULL);
  1585. DBG(3, "unregistering driver %s\n", driver->function);
  1586. spin_unlock_irqrestore(&musb->lock, flags);
  1587. driver->unbind(&musb->g);
  1588. spin_lock_irqsave(&musb->lock, flags);
  1589. musb->gadget_driver = NULL;
  1590. musb->g.dev.driver = NULL;
  1591. musb->is_active = 0;
  1592. musb_platform_try_idle(musb, 0);
  1593. } else
  1594. retval = -EINVAL;
  1595. spin_unlock_irqrestore(&musb->lock, flags);
  1596. if (is_otg_enabled(musb) && retval == 0) {
  1597. usb_remove_hcd(musb_to_hcd(musb));
  1598. /* FIXME we need to be able to register another
  1599. * gadget driver here and have everything work;
  1600. * that currently misbehaves.
  1601. */
  1602. }
  1603. return retval;
  1604. }
  1605. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1606. /* ----------------------------------------------------------------------- */
  1607. /* lifecycle operations called through plat_uds.c */
  1608. void musb_g_resume(struct musb *musb)
  1609. {
  1610. musb->is_suspended = 0;
  1611. switch (musb->xceiv->state) {
  1612. case OTG_STATE_B_IDLE:
  1613. break;
  1614. case OTG_STATE_B_WAIT_ACON:
  1615. case OTG_STATE_B_PERIPHERAL:
  1616. musb->is_active = 1;
  1617. if (musb->gadget_driver && musb->gadget_driver->resume) {
  1618. spin_unlock(&musb->lock);
  1619. musb->gadget_driver->resume(&musb->g);
  1620. spin_lock(&musb->lock);
  1621. }
  1622. break;
  1623. default:
  1624. WARNING("unhandled RESUME transition (%s)\n",
  1625. otg_state_string(musb));
  1626. }
  1627. }
  1628. /* called when SOF packets stop for 3+ msec */
  1629. void musb_g_suspend(struct musb *musb)
  1630. {
  1631. u8 devctl;
  1632. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1633. DBG(3, "devctl %02x\n", devctl);
  1634. switch (musb->xceiv->state) {
  1635. case OTG_STATE_B_IDLE:
  1636. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
  1637. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1638. break;
  1639. case OTG_STATE_B_PERIPHERAL:
  1640. musb->is_suspended = 1;
  1641. if (musb->gadget_driver && musb->gadget_driver->suspend) {
  1642. spin_unlock(&musb->lock);
  1643. musb->gadget_driver->suspend(&musb->g);
  1644. spin_lock(&musb->lock);
  1645. }
  1646. break;
  1647. default:
  1648. /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
  1649. * A_PERIPHERAL may need care too
  1650. */
  1651. WARNING("unhandled SUSPEND transition (%s)\n",
  1652. otg_state_string(musb));
  1653. }
  1654. }
  1655. /* Called during SRP */
  1656. void musb_g_wakeup(struct musb *musb)
  1657. {
  1658. musb_gadget_wakeup(&musb->g);
  1659. }
  1660. /* called when VBUS drops below session threshold, and in other cases */
  1661. void musb_g_disconnect(struct musb *musb)
  1662. {
  1663. void __iomem *mregs = musb->mregs;
  1664. u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
  1665. DBG(3, "devctl %02x\n", devctl);
  1666. /* clear HR */
  1667. musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
  1668. /* don't draw vbus until new b-default session */
  1669. (void) musb_gadget_vbus_draw(&musb->g, 0);
  1670. musb->g.speed = USB_SPEED_UNKNOWN;
  1671. if (musb->gadget_driver && musb->gadget_driver->disconnect) {
  1672. spin_unlock(&musb->lock);
  1673. musb->gadget_driver->disconnect(&musb->g);
  1674. spin_lock(&musb->lock);
  1675. }
  1676. switch (musb->xceiv->state) {
  1677. default:
  1678. #ifdef CONFIG_USB_MUSB_OTG
  1679. DBG(2, "Unhandled disconnect %s, setting a_idle\n",
  1680. otg_state_string(musb));
  1681. musb->xceiv->state = OTG_STATE_A_IDLE;
  1682. MUSB_HST_MODE(musb);
  1683. break;
  1684. case OTG_STATE_A_PERIPHERAL:
  1685. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  1686. MUSB_HST_MODE(musb);
  1687. break;
  1688. case OTG_STATE_B_WAIT_ACON:
  1689. case OTG_STATE_B_HOST:
  1690. #endif
  1691. case OTG_STATE_B_PERIPHERAL:
  1692. case OTG_STATE_B_IDLE:
  1693. musb->xceiv->state = OTG_STATE_B_IDLE;
  1694. break;
  1695. case OTG_STATE_B_SRP_INIT:
  1696. break;
  1697. }
  1698. musb->is_active = 0;
  1699. }
  1700. void musb_g_reset(struct musb *musb)
  1701. __releases(musb->lock)
  1702. __acquires(musb->lock)
  1703. {
  1704. void __iomem *mbase = musb->mregs;
  1705. u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
  1706. u8 power;
  1707. DBG(3, "<== %s addr=%x driver '%s'\n",
  1708. (devctl & MUSB_DEVCTL_BDEVICE)
  1709. ? "B-Device" : "A-Device",
  1710. musb_readb(mbase, MUSB_FADDR),
  1711. musb->gadget_driver
  1712. ? musb->gadget_driver->driver.name
  1713. : NULL
  1714. );
  1715. /* report disconnect, if we didn't already (flushing EP state) */
  1716. if (musb->g.speed != USB_SPEED_UNKNOWN)
  1717. musb_g_disconnect(musb);
  1718. /* clear HR */
  1719. else if (devctl & MUSB_DEVCTL_HR)
  1720. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  1721. /* what speed did we negotiate? */
  1722. power = musb_readb(mbase, MUSB_POWER);
  1723. musb->g.speed = (power & MUSB_POWER_HSMODE)
  1724. ? USB_SPEED_HIGH : USB_SPEED_FULL;
  1725. /* start in USB_STATE_DEFAULT */
  1726. musb->is_active = 1;
  1727. musb->is_suspended = 0;
  1728. MUSB_DEV_MODE(musb);
  1729. musb->address = 0;
  1730. musb->ep0_state = MUSB_EP0_STAGE_SETUP;
  1731. musb->may_wakeup = 0;
  1732. musb->g.b_hnp_enable = 0;
  1733. musb->g.a_alt_hnp_support = 0;
  1734. musb->g.a_hnp_support = 0;
  1735. /* Normal reset, as B-Device;
  1736. * or else after HNP, as A-Device
  1737. */
  1738. if (devctl & MUSB_DEVCTL_BDEVICE) {
  1739. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  1740. musb->g.is_a_peripheral = 0;
  1741. } else if (is_otg_enabled(musb)) {
  1742. musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
  1743. musb->g.is_a_peripheral = 1;
  1744. } else
  1745. WARN_ON(1);
  1746. /* start with default limits on VBUS power draw */
  1747. (void) musb_gadget_vbus_draw(&musb->g,
  1748. is_otg_enabled(musb) ? 8 : 100);
  1749. }