omap_udc.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  1. /*
  2. * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
  3. *
  4. * Copyright (C) 2004 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2005 David Brownell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #undef DEBUG
  22. #undef VERBOSE
  23. #include <linux/config.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/ioport.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/delay.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/timer.h>
  34. #include <linux/list.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/mm.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/usb_ch9.h>
  41. #include <linux/usb_gadget.h>
  42. #include <linux/usb_otg.h>
  43. #include <linux/dma-mapping.h>
  44. #include <asm/byteorder.h>
  45. #include <asm/io.h>
  46. #include <asm/irq.h>
  47. #include <asm/system.h>
  48. #include <asm/unaligned.h>
  49. #include <asm/mach-types.h>
  50. #include <asm/arch/dma.h>
  51. #include <asm/arch/usb.h>
  52. #include "omap_udc.h"
  53. #undef USB_TRACE
  54. /* bulk DMA seems to be behaving for both IN and OUT */
  55. #define USE_DMA
  56. /* ISO too */
  57. #define USE_ISO
  58. #define DRIVER_DESC "OMAP UDC driver"
  59. #define DRIVER_VERSION "4 October 2004"
  60. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  61. /*
  62. * The OMAP UDC needs _very_ early endpoint setup: before enabling the
  63. * D+ pullup to allow enumeration. That's too early for the gadget
  64. * framework to use from usb_endpoint_enable(), which happens after
  65. * enumeration as part of activating an interface. (But if we add an
  66. * optional new "UDC not yet running" state to the gadget driver model,
  67. * even just during driver binding, the endpoint autoconfig logic is the
  68. * natural spot to manufacture new endpoints.)
  69. *
  70. * So instead of using endpoint enable calls to control the hardware setup,
  71. * this driver defines a "fifo mode" parameter. It's used during driver
  72. * initialization to choose among a set of pre-defined endpoint configs.
  73. * See omap_udc_setup() for available modes, or to add others. That code
  74. * lives in an init section, so use this driver as a module if you need
  75. * to change the fifo mode after the kernel boots.
  76. *
  77. * Gadget drivers normally ignore endpoints they don't care about, and
  78. * won't include them in configuration descriptors. That means only
  79. * misbehaving hosts would even notice they exist.
  80. */
  81. #ifdef USE_ISO
  82. static unsigned fifo_mode = 3;
  83. #else
  84. static unsigned fifo_mode = 0;
  85. #endif
  86. /* "modprobe omap_udc fifo_mode=42", or else as a kernel
  87. * boot parameter "omap_udc:fifo_mode=42"
  88. */
  89. module_param (fifo_mode, uint, 0);
  90. MODULE_PARM_DESC (fifo_mode, "endpoint setup (0 == default)");
  91. #ifdef USE_DMA
  92. static unsigned use_dma = 1;
  93. /* "modprobe omap_udc use_dma=y", or else as a kernel
  94. * boot parameter "omap_udc:use_dma=y"
  95. */
  96. module_param (use_dma, bool, 0);
  97. MODULE_PARM_DESC (use_dma, "enable/disable DMA");
  98. #else /* !USE_DMA */
  99. /* save a bit of code */
  100. #define use_dma 0
  101. #endif /* !USE_DMA */
  102. static const char driver_name [] = "omap_udc";
  103. static const char driver_desc [] = DRIVER_DESC;
  104. /*-------------------------------------------------------------------------*/
  105. /* there's a notion of "current endpoint" for modifying endpoint
  106. * state, and PIO access to its FIFO.
  107. */
  108. static void use_ep(struct omap_ep *ep, u16 select)
  109. {
  110. u16 num = ep->bEndpointAddress & 0x0f;
  111. if (ep->bEndpointAddress & USB_DIR_IN)
  112. num |= UDC_EP_DIR;
  113. UDC_EP_NUM_REG = num | select;
  114. /* when select, MUST deselect later !! */
  115. }
  116. static inline void deselect_ep(void)
  117. {
  118. UDC_EP_NUM_REG &= ~UDC_EP_SEL;
  119. /* 6 wait states before TX will happen */
  120. }
  121. static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
  122. /*-------------------------------------------------------------------------*/
  123. static int omap_ep_enable(struct usb_ep *_ep,
  124. const struct usb_endpoint_descriptor *desc)
  125. {
  126. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  127. struct omap_udc *udc;
  128. unsigned long flags;
  129. u16 maxp;
  130. /* catch various bogus parameters */
  131. if (!_ep || !desc || ep->desc
  132. || desc->bDescriptorType != USB_DT_ENDPOINT
  133. || ep->bEndpointAddress != desc->bEndpointAddress
  134. || ep->maxpacket < le16_to_cpu
  135. (desc->wMaxPacketSize)) {
  136. DBG("%s, bad ep or descriptor\n", __FUNCTION__);
  137. return -EINVAL;
  138. }
  139. maxp = le16_to_cpu (desc->wMaxPacketSize);
  140. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  141. && maxp != ep->maxpacket)
  142. || le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket
  143. || !desc->wMaxPacketSize) {
  144. DBG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
  145. return -ERANGE;
  146. }
  147. #ifdef USE_ISO
  148. if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
  149. && desc->bInterval != 1)) {
  150. /* hardware wants period = 1; USB allows 2^(Interval-1) */
  151. DBG("%s, unsupported ISO period %dms\n", _ep->name,
  152. 1 << (desc->bInterval - 1));
  153. return -EDOM;
  154. }
  155. #else
  156. if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  157. DBG("%s, ISO nyet\n", _ep->name);
  158. return -EDOM;
  159. }
  160. #endif
  161. /* xfer types must match, except that interrupt ~= bulk */
  162. if (ep->bmAttributes != desc->bmAttributes
  163. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  164. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  165. DBG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
  166. return -EINVAL;
  167. }
  168. udc = ep->udc;
  169. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  170. DBG("%s, bogus device state\n", __FUNCTION__);
  171. return -ESHUTDOWN;
  172. }
  173. spin_lock_irqsave(&udc->lock, flags);
  174. ep->desc = desc;
  175. ep->irqs = 0;
  176. ep->stopped = 0;
  177. ep->ep.maxpacket = maxp;
  178. /* set endpoint to initial state */
  179. ep->dma_channel = 0;
  180. ep->has_dma = 0;
  181. ep->lch = -1;
  182. use_ep(ep, UDC_EP_SEL);
  183. UDC_CTRL_REG = udc->clr_halt;
  184. ep->ackwait = 0;
  185. deselect_ep();
  186. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  187. list_add(&ep->iso, &udc->iso);
  188. /* maybe assign a DMA channel to this endpoint */
  189. if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
  190. /* FIXME ISO can dma, but prefers first channel */
  191. dma_channel_claim(ep, 0);
  192. /* PIO OUT may RX packets */
  193. if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
  194. && !ep->has_dma
  195. && !(ep->bEndpointAddress & USB_DIR_IN)) {
  196. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  197. ep->ackwait = 1 + ep->double_buf;
  198. }
  199. spin_unlock_irqrestore(&udc->lock, flags);
  200. VDBG("%s enabled\n", _ep->name);
  201. return 0;
  202. }
  203. static void nuke(struct omap_ep *, int status);
  204. static int omap_ep_disable(struct usb_ep *_ep)
  205. {
  206. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  207. unsigned long flags;
  208. if (!_ep || !ep->desc) {
  209. DBG("%s, %s not enabled\n", __FUNCTION__,
  210. _ep ? ep->ep.name : NULL);
  211. return -EINVAL;
  212. }
  213. spin_lock_irqsave(&ep->udc->lock, flags);
  214. ep->desc = NULL;
  215. nuke (ep, -ESHUTDOWN);
  216. ep->ep.maxpacket = ep->maxpacket;
  217. ep->has_dma = 0;
  218. UDC_CTRL_REG = UDC_SET_HALT;
  219. list_del_init(&ep->iso);
  220. del_timer(&ep->timer);
  221. spin_unlock_irqrestore(&ep->udc->lock, flags);
  222. VDBG("%s disabled\n", _ep->name);
  223. return 0;
  224. }
  225. /*-------------------------------------------------------------------------*/
  226. static struct usb_request *
  227. omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  228. {
  229. struct omap_req *req;
  230. req = kzalloc(sizeof(*req), gfp_flags);
  231. if (req) {
  232. req->req.dma = DMA_ADDR_INVALID;
  233. INIT_LIST_HEAD (&req->queue);
  234. }
  235. return &req->req;
  236. }
  237. static void
  238. omap_free_request(struct usb_ep *ep, struct usb_request *_req)
  239. {
  240. struct omap_req *req = container_of(_req, struct omap_req, req);
  241. if (_req)
  242. kfree (req);
  243. }
  244. /*-------------------------------------------------------------------------*/
  245. static void *
  246. omap_alloc_buffer(
  247. struct usb_ep *_ep,
  248. unsigned bytes,
  249. dma_addr_t *dma,
  250. gfp_t gfp_flags
  251. )
  252. {
  253. void *retval;
  254. struct omap_ep *ep;
  255. ep = container_of(_ep, struct omap_ep, ep);
  256. if (use_dma && ep->has_dma) {
  257. static int warned;
  258. if (!warned && bytes < PAGE_SIZE) {
  259. dev_warn(ep->udc->gadget.dev.parent,
  260. "using dma_alloc_coherent for "
  261. "small allocations wastes memory\n");
  262. warned++;
  263. }
  264. return dma_alloc_coherent(ep->udc->gadget.dev.parent,
  265. bytes, dma, gfp_flags);
  266. }
  267. retval = kmalloc(bytes, gfp_flags);
  268. if (retval)
  269. *dma = virt_to_phys(retval);
  270. return retval;
  271. }
  272. static void omap_free_buffer(
  273. struct usb_ep *_ep,
  274. void *buf,
  275. dma_addr_t dma,
  276. unsigned bytes
  277. )
  278. {
  279. struct omap_ep *ep;
  280. ep = container_of(_ep, struct omap_ep, ep);
  281. if (use_dma && _ep && ep->has_dma)
  282. dma_free_coherent(ep->udc->gadget.dev.parent, bytes, buf, dma);
  283. else
  284. kfree (buf);
  285. }
  286. /*-------------------------------------------------------------------------*/
  287. static void
  288. done(struct omap_ep *ep, struct omap_req *req, int status)
  289. {
  290. unsigned stopped = ep->stopped;
  291. list_del_init(&req->queue);
  292. if (req->req.status == -EINPROGRESS)
  293. req->req.status = status;
  294. else
  295. status = req->req.status;
  296. if (use_dma && ep->has_dma) {
  297. if (req->mapped) {
  298. dma_unmap_single(ep->udc->gadget.dev.parent,
  299. req->req.dma, req->req.length,
  300. (ep->bEndpointAddress & USB_DIR_IN)
  301. ? DMA_TO_DEVICE
  302. : DMA_FROM_DEVICE);
  303. req->req.dma = DMA_ADDR_INVALID;
  304. req->mapped = 0;
  305. } else
  306. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  307. req->req.dma, req->req.length,
  308. (ep->bEndpointAddress & USB_DIR_IN)
  309. ? DMA_TO_DEVICE
  310. : DMA_FROM_DEVICE);
  311. }
  312. #ifndef USB_TRACE
  313. if (status && status != -ESHUTDOWN)
  314. #endif
  315. VDBG("complete %s req %p stat %d len %u/%u\n",
  316. ep->ep.name, &req->req, status,
  317. req->req.actual, req->req.length);
  318. /* don't modify queue heads during completion callback */
  319. ep->stopped = 1;
  320. spin_unlock(&ep->udc->lock);
  321. req->req.complete(&ep->ep, &req->req);
  322. spin_lock(&ep->udc->lock);
  323. ep->stopped = stopped;
  324. }
  325. /*-------------------------------------------------------------------------*/
  326. #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
  327. #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
  328. #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
  329. #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
  330. static inline int
  331. write_packet(u8 *buf, struct omap_req *req, unsigned max)
  332. {
  333. unsigned len;
  334. u16 *wp;
  335. len = min(req->req.length - req->req.actual, max);
  336. req->req.actual += len;
  337. max = len;
  338. if (likely((((int)buf) & 1) == 0)) {
  339. wp = (u16 *)buf;
  340. while (max >= 2) {
  341. UDC_DATA_REG = *wp++;
  342. max -= 2;
  343. }
  344. buf = (u8 *)wp;
  345. }
  346. while (max--)
  347. *(volatile u8 *)&UDC_DATA_REG = *buf++;
  348. return len;
  349. }
  350. // FIXME change r/w fifo calling convention
  351. // return: 0 = still running, 1 = completed, negative = errno
  352. static int write_fifo(struct omap_ep *ep, struct omap_req *req)
  353. {
  354. u8 *buf;
  355. unsigned count;
  356. int is_last;
  357. u16 ep_stat;
  358. buf = req->req.buf + req->req.actual;
  359. prefetch(buf);
  360. /* PIO-IN isn't double buffered except for iso */
  361. ep_stat = UDC_STAT_FLG_REG;
  362. if (ep_stat & UDC_FIFO_UNWRITABLE)
  363. return 0;
  364. count = ep->ep.maxpacket;
  365. count = write_packet(buf, req, count);
  366. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  367. ep->ackwait = 1;
  368. /* last packet is often short (sometimes a zlp) */
  369. if (count != ep->ep.maxpacket)
  370. is_last = 1;
  371. else if (req->req.length == req->req.actual
  372. && !req->req.zero)
  373. is_last = 1;
  374. else
  375. is_last = 0;
  376. /* NOTE: requests complete when all IN data is in a
  377. * FIFO (or sometimes later, if a zlp was needed).
  378. * Use usb_ep_fifo_status() where needed.
  379. */
  380. if (is_last)
  381. done(ep, req, 0);
  382. return is_last;
  383. }
  384. static inline int
  385. read_packet(u8 *buf, struct omap_req *req, unsigned avail)
  386. {
  387. unsigned len;
  388. u16 *wp;
  389. len = min(req->req.length - req->req.actual, avail);
  390. req->req.actual += len;
  391. avail = len;
  392. if (likely((((int)buf) & 1) == 0)) {
  393. wp = (u16 *)buf;
  394. while (avail >= 2) {
  395. *wp++ = UDC_DATA_REG;
  396. avail -= 2;
  397. }
  398. buf = (u8 *)wp;
  399. }
  400. while (avail--)
  401. *buf++ = *(volatile u8 *)&UDC_DATA_REG;
  402. return len;
  403. }
  404. // return: 0 = still running, 1 = queue empty, negative = errno
  405. static int read_fifo(struct omap_ep *ep, struct omap_req *req)
  406. {
  407. u8 *buf;
  408. unsigned count, avail;
  409. int is_last;
  410. buf = req->req.buf + req->req.actual;
  411. prefetchw(buf);
  412. for (;;) {
  413. u16 ep_stat = UDC_STAT_FLG_REG;
  414. is_last = 0;
  415. if (ep_stat & FIFO_EMPTY) {
  416. if (!ep->double_buf)
  417. break;
  418. ep->fnf = 1;
  419. }
  420. if (ep_stat & UDC_EP_HALTED)
  421. break;
  422. if (ep_stat & UDC_FIFO_FULL)
  423. avail = ep->ep.maxpacket;
  424. else {
  425. avail = UDC_RXFSTAT_REG;
  426. ep->fnf = ep->double_buf;
  427. }
  428. count = read_packet(buf, req, avail);
  429. /* partial packet reads may not be errors */
  430. if (count < ep->ep.maxpacket) {
  431. is_last = 1;
  432. /* overflowed this request? flush extra data */
  433. if (count != avail) {
  434. req->req.status = -EOVERFLOW;
  435. avail -= count;
  436. while (avail--)
  437. (void) *(volatile u8 *)&UDC_DATA_REG;
  438. }
  439. } else if (req->req.length == req->req.actual)
  440. is_last = 1;
  441. else
  442. is_last = 0;
  443. if (!ep->bEndpointAddress)
  444. break;
  445. if (is_last)
  446. done(ep, req, 0);
  447. break;
  448. }
  449. return is_last;
  450. }
  451. /*-------------------------------------------------------------------------*/
  452. static inline dma_addr_t dma_csac(unsigned lch)
  453. {
  454. dma_addr_t csac;
  455. /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  456. * read before the DMA controller finished disabling the channel.
  457. */
  458. csac = omap_readw(OMAP_DMA_CSAC(lch));
  459. if (csac == 0)
  460. csac = omap_readw(OMAP_DMA_CSAC(lch));
  461. return csac;
  462. }
  463. static inline dma_addr_t dma_cdac(unsigned lch)
  464. {
  465. dma_addr_t cdac;
  466. /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  467. * read before the DMA controller finished disabling the channel.
  468. */
  469. cdac = omap_readw(OMAP_DMA_CDAC(lch));
  470. if (cdac == 0)
  471. cdac = omap_readw(OMAP_DMA_CDAC(lch));
  472. return cdac;
  473. }
  474. static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
  475. {
  476. dma_addr_t end;
  477. /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
  478. * the last transfer's bytecount by more than a FIFO's worth.
  479. */
  480. if (cpu_is_omap15xx())
  481. return 0;
  482. end = dma_csac(ep->lch);
  483. if (end == ep->dma_counter)
  484. return 0;
  485. end |= start & (0xffff << 16);
  486. if (end < start)
  487. end += 0x10000;
  488. return end - start;
  489. }
  490. #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
  491. ? omap_readw(OMAP_DMA_CSAC(x)) /* really: CPC */ \
  492. : dma_cdac(x))
  493. static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
  494. {
  495. dma_addr_t end;
  496. end = DMA_DEST_LAST(ep->lch);
  497. if (end == ep->dma_counter)
  498. return 0;
  499. end |= start & (0xffff << 16);
  500. if (cpu_is_omap15xx())
  501. end++;
  502. if (end < start)
  503. end += 0x10000;
  504. return end - start;
  505. }
  506. /* Each USB transfer request using DMA maps to one or more DMA transfers.
  507. * When DMA completion isn't request completion, the UDC continues with
  508. * the next DMA transfer for that USB transfer.
  509. */
  510. static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
  511. {
  512. u16 txdma_ctrl;
  513. unsigned length = req->req.length - req->req.actual;
  514. const int sync_mode = cpu_is_omap15xx()
  515. ? OMAP_DMA_SYNC_FRAME
  516. : OMAP_DMA_SYNC_ELEMENT;
  517. /* measure length in either bytes or packets */
  518. if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
  519. || (cpu_is_omap15xx() && length < ep->maxpacket)) {
  520. txdma_ctrl = UDC_TXN_EOT | length;
  521. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
  522. length, 1, sync_mode);
  523. } else {
  524. length = min(length / ep->maxpacket,
  525. (unsigned) UDC_TXN_TSC + 1);
  526. txdma_ctrl = length;
  527. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
  528. ep->ep.maxpacket >> 1, length, sync_mode);
  529. length *= ep->maxpacket;
  530. }
  531. omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
  532. OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
  533. omap_start_dma(ep->lch);
  534. ep->dma_counter = dma_csac(ep->lch);
  535. UDC_DMA_IRQ_EN_REG |= UDC_TX_DONE_IE(ep->dma_channel);
  536. UDC_TXDMA_REG(ep->dma_channel) = UDC_TXN_START | txdma_ctrl;
  537. req->dma_bytes = length;
  538. }
  539. static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
  540. {
  541. if (status == 0) {
  542. req->req.actual += req->dma_bytes;
  543. /* return if this request needs to send data or zlp */
  544. if (req->req.actual < req->req.length)
  545. return;
  546. if (req->req.zero
  547. && req->dma_bytes != 0
  548. && (req->req.actual % ep->maxpacket) == 0)
  549. return;
  550. } else
  551. req->req.actual += dma_src_len(ep, req->req.dma
  552. + req->req.actual);
  553. /* tx completion */
  554. omap_stop_dma(ep->lch);
  555. UDC_DMA_IRQ_EN_REG &= ~UDC_TX_DONE_IE(ep->dma_channel);
  556. done(ep, req, status);
  557. }
  558. static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
  559. {
  560. unsigned packets;
  561. /* NOTE: we filtered out "short reads" before, so we know
  562. * the buffer has only whole numbers of packets.
  563. */
  564. /* set up this DMA transfer, enable the fifo, start */
  565. packets = (req->req.length - req->req.actual) / ep->ep.maxpacket;
  566. packets = min(packets, (unsigned)UDC_RXN_TC + 1);
  567. req->dma_bytes = packets * ep->ep.maxpacket;
  568. omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
  569. ep->ep.maxpacket >> 1, packets,
  570. OMAP_DMA_SYNC_ELEMENT);
  571. omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
  572. OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
  573. ep->dma_counter = DMA_DEST_LAST(ep->lch);
  574. UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
  575. UDC_DMA_IRQ_EN_REG |= UDC_RX_EOT_IE(ep->dma_channel);
  576. UDC_EP_NUM_REG = (ep->bEndpointAddress & 0xf);
  577. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  578. omap_start_dma(ep->lch);
  579. }
  580. static void
  581. finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
  582. {
  583. u16 count;
  584. if (status == 0)
  585. ep->dma_counter = (u16) (req->req.dma + req->req.actual);
  586. count = dma_dest_len(ep, req->req.dma + req->req.actual);
  587. count += req->req.actual;
  588. if (one)
  589. count--;
  590. if (count <= req->req.length)
  591. req->req.actual = count;
  592. if (count != req->dma_bytes || status)
  593. omap_stop_dma(ep->lch);
  594. /* if this wasn't short, request may need another transfer */
  595. else if (req->req.actual < req->req.length)
  596. return;
  597. /* rx completion */
  598. UDC_DMA_IRQ_EN_REG &= ~UDC_RX_EOT_IE(ep->dma_channel);
  599. done(ep, req, status);
  600. }
  601. static void dma_irq(struct omap_udc *udc, u16 irq_src)
  602. {
  603. u16 dman_stat = UDC_DMAN_STAT_REG;
  604. struct omap_ep *ep;
  605. struct omap_req *req;
  606. /* IN dma: tx to host */
  607. if (irq_src & UDC_TXN_DONE) {
  608. ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
  609. ep->irqs++;
  610. /* can see TXN_DONE after dma abort */
  611. if (!list_empty(&ep->queue)) {
  612. req = container_of(ep->queue.next,
  613. struct omap_req, queue);
  614. finish_in_dma(ep, req, 0);
  615. }
  616. UDC_IRQ_SRC_REG = UDC_TXN_DONE;
  617. if (!list_empty (&ep->queue)) {
  618. req = container_of(ep->queue.next,
  619. struct omap_req, queue);
  620. next_in_dma(ep, req);
  621. }
  622. }
  623. /* OUT dma: rx from host */
  624. if (irq_src & UDC_RXN_EOT) {
  625. ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
  626. ep->irqs++;
  627. /* can see RXN_EOT after dma abort */
  628. if (!list_empty(&ep->queue)) {
  629. req = container_of(ep->queue.next,
  630. struct omap_req, queue);
  631. finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
  632. }
  633. UDC_IRQ_SRC_REG = UDC_RXN_EOT;
  634. if (!list_empty (&ep->queue)) {
  635. req = container_of(ep->queue.next,
  636. struct omap_req, queue);
  637. next_out_dma(ep, req);
  638. }
  639. }
  640. if (irq_src & UDC_RXN_CNT) {
  641. ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
  642. ep->irqs++;
  643. /* omap15xx does this unasked... */
  644. VDBG("%s, RX_CNT irq?\n", ep->ep.name);
  645. UDC_IRQ_SRC_REG = UDC_RXN_CNT;
  646. }
  647. }
  648. static void dma_error(int lch, u16 ch_status, void *data)
  649. {
  650. struct omap_ep *ep = data;
  651. /* if ch_status & OMAP_DMA_DROP_IRQ ... */
  652. /* if ch_status & OMAP_DMA_TOUT_IRQ ... */
  653. ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
  654. /* complete current transfer ... */
  655. }
  656. static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
  657. {
  658. u16 reg;
  659. int status, restart, is_in;
  660. is_in = ep->bEndpointAddress & USB_DIR_IN;
  661. if (is_in)
  662. reg = UDC_TXDMA_CFG_REG;
  663. else
  664. reg = UDC_RXDMA_CFG_REG;
  665. reg |= UDC_DMA_REQ; /* "pulse" activated */
  666. ep->dma_channel = 0;
  667. ep->lch = -1;
  668. if (channel == 0 || channel > 3) {
  669. if ((reg & 0x0f00) == 0)
  670. channel = 3;
  671. else if ((reg & 0x00f0) == 0)
  672. channel = 2;
  673. else if ((reg & 0x000f) == 0) /* preferred for ISO */
  674. channel = 1;
  675. else {
  676. status = -EMLINK;
  677. goto just_restart;
  678. }
  679. }
  680. reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
  681. ep->dma_channel = channel;
  682. if (is_in) {
  683. status = omap_request_dma(OMAP_DMA_USB_W2FC_TX0 - 1 + channel,
  684. ep->ep.name, dma_error, ep, &ep->lch);
  685. if (status == 0) {
  686. UDC_TXDMA_CFG_REG = reg;
  687. /* EMIFF */
  688. omap_set_dma_src_burst_mode(ep->lch,
  689. OMAP_DMA_DATA_BURST_4);
  690. omap_set_dma_src_data_pack(ep->lch, 1);
  691. /* TIPB */
  692. omap_set_dma_dest_params(ep->lch,
  693. OMAP_DMA_PORT_TIPB,
  694. OMAP_DMA_AMODE_CONSTANT,
  695. (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
  696. }
  697. } else {
  698. status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
  699. ep->ep.name, dma_error, ep, &ep->lch);
  700. if (status == 0) {
  701. UDC_RXDMA_CFG_REG = reg;
  702. /* TIPB */
  703. omap_set_dma_src_params(ep->lch,
  704. OMAP_DMA_PORT_TIPB,
  705. OMAP_DMA_AMODE_CONSTANT,
  706. (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
  707. /* EMIFF */
  708. omap_set_dma_dest_burst_mode(ep->lch,
  709. OMAP_DMA_DATA_BURST_4);
  710. omap_set_dma_dest_data_pack(ep->lch, 1);
  711. }
  712. }
  713. if (status)
  714. ep->dma_channel = 0;
  715. else {
  716. ep->has_dma = 1;
  717. omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
  718. /* channel type P: hw synch (fifo) */
  719. if (!cpu_is_omap15xx())
  720. omap_writew(2, OMAP_DMA_LCH_CTRL(ep->lch));
  721. }
  722. just_restart:
  723. /* restart any queue, even if the claim failed */
  724. restart = !ep->stopped && !list_empty(&ep->queue);
  725. if (status)
  726. DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
  727. restart ? " (restart)" : "");
  728. else
  729. DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
  730. is_in ? 't' : 'r',
  731. ep->dma_channel - 1, ep->lch,
  732. restart ? " (restart)" : "");
  733. if (restart) {
  734. struct omap_req *req;
  735. req = container_of(ep->queue.next, struct omap_req, queue);
  736. if (ep->has_dma)
  737. (is_in ? next_in_dma : next_out_dma)(ep, req);
  738. else {
  739. use_ep(ep, UDC_EP_SEL);
  740. (is_in ? write_fifo : read_fifo)(ep, req);
  741. deselect_ep();
  742. if (!is_in) {
  743. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  744. ep->ackwait = 1 + ep->double_buf;
  745. }
  746. /* IN: 6 wait states before it'll tx */
  747. }
  748. }
  749. }
  750. static void dma_channel_release(struct omap_ep *ep)
  751. {
  752. int shift = 4 * (ep->dma_channel - 1);
  753. u16 mask = 0x0f << shift;
  754. struct omap_req *req;
  755. int active;
  756. /* abort any active usb transfer request */
  757. if (!list_empty(&ep->queue))
  758. req = container_of(ep->queue.next, struct omap_req, queue);
  759. else
  760. req = NULL;
  761. active = ((1 << 7) & omap_readl(OMAP_DMA_CCR(ep->lch))) != 0;
  762. DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
  763. active ? "active" : "idle",
  764. (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
  765. ep->dma_channel - 1, req);
  766. /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
  767. * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
  768. */
  769. /* wait till current packet DMA finishes, and fifo empties */
  770. if (ep->bEndpointAddress & USB_DIR_IN) {
  771. UDC_TXDMA_CFG_REG = (UDC_TXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
  772. if (req) {
  773. finish_in_dma(ep, req, -ECONNRESET);
  774. /* clear FIFO; hosts probably won't empty it */
  775. use_ep(ep, UDC_EP_SEL);
  776. UDC_CTRL_REG = UDC_CLR_EP;
  777. deselect_ep();
  778. }
  779. while (UDC_TXDMA_CFG_REG & mask)
  780. udelay(10);
  781. } else {
  782. UDC_RXDMA_CFG_REG = (UDC_RXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
  783. /* dma empties the fifo */
  784. while (UDC_RXDMA_CFG_REG & mask)
  785. udelay(10);
  786. if (req)
  787. finish_out_dma(ep, req, -ECONNRESET, 0);
  788. }
  789. omap_free_dma(ep->lch);
  790. ep->dma_channel = 0;
  791. ep->lch = -1;
  792. /* has_dma still set, till endpoint is fully quiesced */
  793. }
  794. /*-------------------------------------------------------------------------*/
  795. static int
  796. omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  797. {
  798. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  799. struct omap_req *req = container_of(_req, struct omap_req, req);
  800. struct omap_udc *udc;
  801. unsigned long flags;
  802. int is_iso = 0;
  803. /* catch various bogus parameters */
  804. if (!_req || !req->req.complete || !req->req.buf
  805. || !list_empty(&req->queue)) {
  806. DBG("%s, bad params\n", __FUNCTION__);
  807. return -EINVAL;
  808. }
  809. if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
  810. DBG("%s, bad ep\n", __FUNCTION__);
  811. return -EINVAL;
  812. }
  813. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  814. if (req->req.length > ep->ep.maxpacket)
  815. return -EMSGSIZE;
  816. is_iso = 1;
  817. }
  818. /* this isn't bogus, but OMAP DMA isn't the only hardware to
  819. * have a hard time with partial packet reads... reject it.
  820. */
  821. if (use_dma
  822. && ep->has_dma
  823. && ep->bEndpointAddress != 0
  824. && (ep->bEndpointAddress & USB_DIR_IN) == 0
  825. && (req->req.length % ep->ep.maxpacket) != 0) {
  826. DBG("%s, no partial packet OUT reads\n", __FUNCTION__);
  827. return -EMSGSIZE;
  828. }
  829. udc = ep->udc;
  830. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
  831. return -ESHUTDOWN;
  832. if (use_dma && ep->has_dma) {
  833. if (req->req.dma == DMA_ADDR_INVALID) {
  834. req->req.dma = dma_map_single(
  835. ep->udc->gadget.dev.parent,
  836. req->req.buf,
  837. req->req.length,
  838. (ep->bEndpointAddress & USB_DIR_IN)
  839. ? DMA_TO_DEVICE
  840. : DMA_FROM_DEVICE);
  841. req->mapped = 1;
  842. } else {
  843. dma_sync_single_for_device(
  844. ep->udc->gadget.dev.parent,
  845. req->req.dma, req->req.length,
  846. (ep->bEndpointAddress & USB_DIR_IN)
  847. ? DMA_TO_DEVICE
  848. : DMA_FROM_DEVICE);
  849. req->mapped = 0;
  850. }
  851. }
  852. VDBG("%s queue req %p, len %d buf %p\n",
  853. ep->ep.name, _req, _req->length, _req->buf);
  854. spin_lock_irqsave(&udc->lock, flags);
  855. req->req.status = -EINPROGRESS;
  856. req->req.actual = 0;
  857. /* maybe kickstart non-iso i/o queues */
  858. if (is_iso)
  859. UDC_IRQ_EN_REG |= UDC_SOF_IE;
  860. else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
  861. int is_in;
  862. if (ep->bEndpointAddress == 0) {
  863. if (!udc->ep0_pending || !list_empty (&ep->queue)) {
  864. spin_unlock_irqrestore(&udc->lock, flags);
  865. return -EL2HLT;
  866. }
  867. /* empty DATA stage? */
  868. is_in = udc->ep0_in;
  869. if (!req->req.length) {
  870. /* chip became CONFIGURED or ADDRESSED
  871. * earlier; drivers may already have queued
  872. * requests to non-control endpoints
  873. */
  874. if (udc->ep0_set_config) {
  875. u16 irq_en = UDC_IRQ_EN_REG;
  876. irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
  877. if (!udc->ep0_reset_config)
  878. irq_en |= UDC_EPN_RX_IE
  879. | UDC_EPN_TX_IE;
  880. UDC_IRQ_EN_REG = irq_en;
  881. }
  882. /* STATUS for zero length DATA stages is
  883. * always an IN ... even for IN transfers,
  884. * a wierd case which seem to stall OMAP.
  885. */
  886. UDC_EP_NUM_REG = (UDC_EP_SEL|UDC_EP_DIR);
  887. UDC_CTRL_REG = UDC_CLR_EP;
  888. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  889. UDC_EP_NUM_REG = UDC_EP_DIR;
  890. /* cleanup */
  891. udc->ep0_pending = 0;
  892. done(ep, req, 0);
  893. req = NULL;
  894. /* non-empty DATA stage */
  895. } else if (is_in) {
  896. UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
  897. } else {
  898. if (udc->ep0_setup)
  899. goto irq_wait;
  900. UDC_EP_NUM_REG = UDC_EP_SEL;
  901. }
  902. } else {
  903. is_in = ep->bEndpointAddress & USB_DIR_IN;
  904. if (!ep->has_dma)
  905. use_ep(ep, UDC_EP_SEL);
  906. /* if ISO: SOF IRQs must be enabled/disabled! */
  907. }
  908. if (ep->has_dma)
  909. (is_in ? next_in_dma : next_out_dma)(ep, req);
  910. else if (req) {
  911. if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
  912. req = NULL;
  913. deselect_ep();
  914. if (!is_in) {
  915. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  916. ep->ackwait = 1 + ep->double_buf;
  917. }
  918. /* IN: 6 wait states before it'll tx */
  919. }
  920. }
  921. irq_wait:
  922. /* irq handler advances the queue */
  923. if (req != NULL)
  924. list_add_tail(&req->queue, &ep->queue);
  925. spin_unlock_irqrestore(&udc->lock, flags);
  926. return 0;
  927. }
  928. static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  929. {
  930. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  931. struct omap_req *req;
  932. unsigned long flags;
  933. if (!_ep || !_req)
  934. return -EINVAL;
  935. spin_lock_irqsave(&ep->udc->lock, flags);
  936. /* make sure it's actually queued on this endpoint */
  937. list_for_each_entry (req, &ep->queue, queue) {
  938. if (&req->req == _req)
  939. break;
  940. }
  941. if (&req->req != _req) {
  942. spin_unlock_irqrestore(&ep->udc->lock, flags);
  943. return -EINVAL;
  944. }
  945. if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
  946. int channel = ep->dma_channel;
  947. /* releasing the channel cancels the request,
  948. * reclaiming the channel restarts the queue
  949. */
  950. dma_channel_release(ep);
  951. dma_channel_claim(ep, channel);
  952. } else
  953. done(ep, req, -ECONNRESET);
  954. spin_unlock_irqrestore(&ep->udc->lock, flags);
  955. return 0;
  956. }
  957. /*-------------------------------------------------------------------------*/
  958. static int omap_ep_set_halt(struct usb_ep *_ep, int value)
  959. {
  960. struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
  961. unsigned long flags;
  962. int status = -EOPNOTSUPP;
  963. spin_lock_irqsave(&ep->udc->lock, flags);
  964. /* just use protocol stalls for ep0; real halts are annoying */
  965. if (ep->bEndpointAddress == 0) {
  966. if (!ep->udc->ep0_pending)
  967. status = -EINVAL;
  968. else if (value) {
  969. if (ep->udc->ep0_set_config) {
  970. WARN("error changing config?\n");
  971. UDC_SYSCON2_REG = UDC_CLR_CFG;
  972. }
  973. UDC_SYSCON2_REG = UDC_STALL_CMD;
  974. ep->udc->ep0_pending = 0;
  975. status = 0;
  976. } else /* NOP */
  977. status = 0;
  978. /* otherwise, all active non-ISO endpoints can halt */
  979. } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
  980. /* IN endpoints must already be idle */
  981. if ((ep->bEndpointAddress & USB_DIR_IN)
  982. && !list_empty(&ep->queue)) {
  983. status = -EAGAIN;
  984. goto done;
  985. }
  986. if (value) {
  987. int channel;
  988. if (use_dma && ep->dma_channel
  989. && !list_empty(&ep->queue)) {
  990. channel = ep->dma_channel;
  991. dma_channel_release(ep);
  992. } else
  993. channel = 0;
  994. use_ep(ep, UDC_EP_SEL);
  995. if (UDC_STAT_FLG_REG & UDC_NON_ISO_FIFO_EMPTY) {
  996. UDC_CTRL_REG = UDC_SET_HALT;
  997. status = 0;
  998. } else
  999. status = -EAGAIN;
  1000. deselect_ep();
  1001. if (channel)
  1002. dma_channel_claim(ep, channel);
  1003. } else {
  1004. use_ep(ep, 0);
  1005. UDC_CTRL_REG = ep->udc->clr_halt;
  1006. ep->ackwait = 0;
  1007. if (!(ep->bEndpointAddress & USB_DIR_IN)) {
  1008. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1009. ep->ackwait = 1 + ep->double_buf;
  1010. }
  1011. }
  1012. }
  1013. done:
  1014. VDBG("%s %s halt stat %d\n", ep->ep.name,
  1015. value ? "set" : "clear", status);
  1016. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1017. return status;
  1018. }
  1019. static struct usb_ep_ops omap_ep_ops = {
  1020. .enable = omap_ep_enable,
  1021. .disable = omap_ep_disable,
  1022. .alloc_request = omap_alloc_request,
  1023. .free_request = omap_free_request,
  1024. .alloc_buffer = omap_alloc_buffer,
  1025. .free_buffer = omap_free_buffer,
  1026. .queue = omap_ep_queue,
  1027. .dequeue = omap_ep_dequeue,
  1028. .set_halt = omap_ep_set_halt,
  1029. // fifo_status ... report bytes in fifo
  1030. // fifo_flush ... flush fifo
  1031. };
  1032. /*-------------------------------------------------------------------------*/
  1033. static int omap_get_frame(struct usb_gadget *gadget)
  1034. {
  1035. u16 sof = UDC_SOF_REG;
  1036. return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
  1037. }
  1038. static int omap_wakeup(struct usb_gadget *gadget)
  1039. {
  1040. struct omap_udc *udc;
  1041. unsigned long flags;
  1042. int retval = -EHOSTUNREACH;
  1043. udc = container_of(gadget, struct omap_udc, gadget);
  1044. spin_lock_irqsave(&udc->lock, flags);
  1045. if (udc->devstat & UDC_SUS) {
  1046. /* NOTE: OTG spec erratum says that OTG devices may
  1047. * issue wakeups without host enable.
  1048. */
  1049. if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
  1050. DBG("remote wakeup...\n");
  1051. UDC_SYSCON2_REG = UDC_RMT_WKP;
  1052. retval = 0;
  1053. }
  1054. /* NOTE: non-OTG systems may use SRP TOO... */
  1055. } else if (!(udc->devstat & UDC_ATT)) {
  1056. if (udc->transceiver)
  1057. retval = otg_start_srp(udc->transceiver);
  1058. }
  1059. spin_unlock_irqrestore(&udc->lock, flags);
  1060. return retval;
  1061. }
  1062. static int
  1063. omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
  1064. {
  1065. struct omap_udc *udc;
  1066. unsigned long flags;
  1067. u16 syscon1;
  1068. udc = container_of(gadget, struct omap_udc, gadget);
  1069. spin_lock_irqsave(&udc->lock, flags);
  1070. syscon1 = UDC_SYSCON1_REG;
  1071. if (is_selfpowered)
  1072. syscon1 |= UDC_SELF_PWR;
  1073. else
  1074. syscon1 &= ~UDC_SELF_PWR;
  1075. UDC_SYSCON1_REG = syscon1;
  1076. spin_unlock_irqrestore(&udc->lock, flags);
  1077. return 0;
  1078. }
  1079. static int can_pullup(struct omap_udc *udc)
  1080. {
  1081. return udc->driver && udc->softconnect && udc->vbus_active;
  1082. }
  1083. static void pullup_enable(struct omap_udc *udc)
  1084. {
  1085. udc->gadget.dev.parent->power.power_state = PMSG_ON;
  1086. udc->gadget.dev.power.power_state = PMSG_ON;
  1087. UDC_SYSCON1_REG |= UDC_PULLUP_EN;
  1088. #ifndef CONFIG_USB_OTG
  1089. if (!cpu_is_omap15xx())
  1090. OTG_CTRL_REG |= OTG_BSESSVLD;
  1091. #endif
  1092. UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
  1093. }
  1094. static void pullup_disable(struct omap_udc *udc)
  1095. {
  1096. #ifndef CONFIG_USB_OTG
  1097. if (!cpu_is_omap15xx())
  1098. OTG_CTRL_REG &= ~OTG_BSESSVLD;
  1099. #endif
  1100. UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
  1101. UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
  1102. }
  1103. /*
  1104. * Called by whatever detects VBUS sessions: external transceiver
  1105. * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
  1106. */
  1107. static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
  1108. {
  1109. struct omap_udc *udc;
  1110. unsigned long flags;
  1111. udc = container_of(gadget, struct omap_udc, gadget);
  1112. spin_lock_irqsave(&udc->lock, flags);
  1113. VDBG("VBUS %s\n", is_active ? "on" : "off");
  1114. udc->vbus_active = (is_active != 0);
  1115. if (cpu_is_omap15xx()) {
  1116. /* "software" detect, ignored if !VBUS_MODE_1510 */
  1117. if (is_active)
  1118. FUNC_MUX_CTRL_0_REG |= VBUS_CTRL_1510;
  1119. else
  1120. FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
  1121. }
  1122. if (can_pullup(udc))
  1123. pullup_enable(udc);
  1124. else
  1125. pullup_disable(udc);
  1126. spin_unlock_irqrestore(&udc->lock, flags);
  1127. return 0;
  1128. }
  1129. static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  1130. {
  1131. struct omap_udc *udc;
  1132. udc = container_of(gadget, struct omap_udc, gadget);
  1133. if (udc->transceiver)
  1134. return otg_set_power(udc->transceiver, mA);
  1135. return -EOPNOTSUPP;
  1136. }
  1137. static int omap_pullup(struct usb_gadget *gadget, int is_on)
  1138. {
  1139. struct omap_udc *udc;
  1140. unsigned long flags;
  1141. udc = container_of(gadget, struct omap_udc, gadget);
  1142. spin_lock_irqsave(&udc->lock, flags);
  1143. udc->softconnect = (is_on != 0);
  1144. if (can_pullup(udc))
  1145. pullup_enable(udc);
  1146. else
  1147. pullup_disable(udc);
  1148. spin_unlock_irqrestore(&udc->lock, flags);
  1149. return 0;
  1150. }
  1151. static struct usb_gadget_ops omap_gadget_ops = {
  1152. .get_frame = omap_get_frame,
  1153. .wakeup = omap_wakeup,
  1154. .set_selfpowered = omap_set_selfpowered,
  1155. .vbus_session = omap_vbus_session,
  1156. .vbus_draw = omap_vbus_draw,
  1157. .pullup = omap_pullup,
  1158. };
  1159. /*-------------------------------------------------------------------------*/
  1160. /* dequeue ALL requests; caller holds udc->lock */
  1161. static void nuke(struct omap_ep *ep, int status)
  1162. {
  1163. struct omap_req *req;
  1164. ep->stopped = 1;
  1165. if (use_dma && ep->dma_channel)
  1166. dma_channel_release(ep);
  1167. use_ep(ep, 0);
  1168. UDC_CTRL_REG = UDC_CLR_EP;
  1169. if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
  1170. UDC_CTRL_REG = UDC_SET_HALT;
  1171. while (!list_empty(&ep->queue)) {
  1172. req = list_entry(ep->queue.next, struct omap_req, queue);
  1173. done(ep, req, status);
  1174. }
  1175. }
  1176. /* caller holds udc->lock */
  1177. static void udc_quiesce(struct omap_udc *udc)
  1178. {
  1179. struct omap_ep *ep;
  1180. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1181. nuke(&udc->ep[0], -ESHUTDOWN);
  1182. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
  1183. nuke(ep, -ESHUTDOWN);
  1184. }
  1185. /*-------------------------------------------------------------------------*/
  1186. static void update_otg(struct omap_udc *udc)
  1187. {
  1188. u16 devstat;
  1189. if (!udc->gadget.is_otg)
  1190. return;
  1191. if (OTG_CTRL_REG & OTG_ID)
  1192. devstat = UDC_DEVSTAT_REG;
  1193. else
  1194. devstat = 0;
  1195. udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
  1196. udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
  1197. udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
  1198. /* Enable HNP early, avoiding races on suspend irq path.
  1199. * ASSUMES OTG state machine B_BUS_REQ input is true.
  1200. */
  1201. if (udc->gadget.b_hnp_enable)
  1202. OTG_CTRL_REG = (OTG_CTRL_REG | OTG_B_HNPEN | OTG_B_BUSREQ)
  1203. & ~OTG_PULLUP;
  1204. }
  1205. static void ep0_irq(struct omap_udc *udc, u16 irq_src)
  1206. {
  1207. struct omap_ep *ep0 = &udc->ep[0];
  1208. struct omap_req *req = NULL;
  1209. ep0->irqs++;
  1210. /* Clear any pending requests and then scrub any rx/tx state
  1211. * before starting to handle the SETUP request.
  1212. */
  1213. if (irq_src & UDC_SETUP) {
  1214. u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
  1215. nuke(ep0, 0);
  1216. if (ack) {
  1217. UDC_IRQ_SRC_REG = ack;
  1218. irq_src = UDC_SETUP;
  1219. }
  1220. }
  1221. /* IN/OUT packets mean we're in the DATA or STATUS stage.
  1222. * This driver uses only uses protocol stalls (ep0 never halts),
  1223. * and if we got this far the gadget driver already had a
  1224. * chance to stall. Tries to be forgiving of host oddities.
  1225. *
  1226. * NOTE: the last chance gadget drivers have to stall control
  1227. * requests is during their request completion callback.
  1228. */
  1229. if (!list_empty(&ep0->queue))
  1230. req = container_of(ep0->queue.next, struct omap_req, queue);
  1231. /* IN == TX to host */
  1232. if (irq_src & UDC_EP0_TX) {
  1233. int stat;
  1234. UDC_IRQ_SRC_REG = UDC_EP0_TX;
  1235. UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
  1236. stat = UDC_STAT_FLG_REG;
  1237. if (stat & UDC_ACK) {
  1238. if (udc->ep0_in) {
  1239. /* write next IN packet from response,
  1240. * or set up the status stage.
  1241. */
  1242. if (req)
  1243. stat = write_fifo(ep0, req);
  1244. UDC_EP_NUM_REG = UDC_EP_DIR;
  1245. if (!req && udc->ep0_pending) {
  1246. UDC_EP_NUM_REG = UDC_EP_SEL;
  1247. UDC_CTRL_REG = UDC_CLR_EP;
  1248. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1249. UDC_EP_NUM_REG = 0;
  1250. udc->ep0_pending = 0;
  1251. } /* else: 6 wait states before it'll tx */
  1252. } else {
  1253. /* ack status stage of OUT transfer */
  1254. UDC_EP_NUM_REG = UDC_EP_DIR;
  1255. if (req)
  1256. done(ep0, req, 0);
  1257. }
  1258. req = NULL;
  1259. } else if (stat & UDC_STALL) {
  1260. UDC_CTRL_REG = UDC_CLR_HALT;
  1261. UDC_EP_NUM_REG = UDC_EP_DIR;
  1262. } else {
  1263. UDC_EP_NUM_REG = UDC_EP_DIR;
  1264. }
  1265. }
  1266. /* OUT == RX from host */
  1267. if (irq_src & UDC_EP0_RX) {
  1268. int stat;
  1269. UDC_IRQ_SRC_REG = UDC_EP0_RX;
  1270. UDC_EP_NUM_REG = UDC_EP_SEL;
  1271. stat = UDC_STAT_FLG_REG;
  1272. if (stat & UDC_ACK) {
  1273. if (!udc->ep0_in) {
  1274. stat = 0;
  1275. /* read next OUT packet of request, maybe
  1276. * reactiviting the fifo; stall on errors.
  1277. */
  1278. if (!req || (stat = read_fifo(ep0, req)) < 0) {
  1279. UDC_SYSCON2_REG = UDC_STALL_CMD;
  1280. udc->ep0_pending = 0;
  1281. stat = 0;
  1282. } else if (stat == 0)
  1283. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1284. UDC_EP_NUM_REG = 0;
  1285. /* activate status stage */
  1286. if (stat == 1) {
  1287. done(ep0, req, 0);
  1288. /* that may have STALLed ep0... */
  1289. UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
  1290. UDC_CTRL_REG = UDC_CLR_EP;
  1291. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1292. UDC_EP_NUM_REG = UDC_EP_DIR;
  1293. udc->ep0_pending = 0;
  1294. }
  1295. } else {
  1296. /* ack status stage of IN transfer */
  1297. UDC_EP_NUM_REG = 0;
  1298. if (req)
  1299. done(ep0, req, 0);
  1300. }
  1301. } else if (stat & UDC_STALL) {
  1302. UDC_CTRL_REG = UDC_CLR_HALT;
  1303. UDC_EP_NUM_REG = 0;
  1304. } else {
  1305. UDC_EP_NUM_REG = 0;
  1306. }
  1307. }
  1308. /* SETUP starts all control transfers */
  1309. if (irq_src & UDC_SETUP) {
  1310. union u {
  1311. u16 word[4];
  1312. struct usb_ctrlrequest r;
  1313. } u;
  1314. int status = -EINVAL;
  1315. struct omap_ep *ep;
  1316. /* read the (latest) SETUP message */
  1317. do {
  1318. UDC_EP_NUM_REG = UDC_SETUP_SEL;
  1319. /* two bytes at a time */
  1320. u.word[0] = UDC_DATA_REG;
  1321. u.word[1] = UDC_DATA_REG;
  1322. u.word[2] = UDC_DATA_REG;
  1323. u.word[3] = UDC_DATA_REG;
  1324. UDC_EP_NUM_REG = 0;
  1325. } while (UDC_IRQ_SRC_REG & UDC_SETUP);
  1326. #define w_value le16_to_cpup (&u.r.wValue)
  1327. #define w_index le16_to_cpup (&u.r.wIndex)
  1328. #define w_length le16_to_cpup (&u.r.wLength)
  1329. /* Delegate almost all control requests to the gadget driver,
  1330. * except for a handful of ch9 status/feature requests that
  1331. * hardware doesn't autodecode _and_ the gadget API hides.
  1332. */
  1333. udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  1334. udc->ep0_set_config = 0;
  1335. udc->ep0_pending = 1;
  1336. ep0->stopped = 0;
  1337. ep0->ackwait = 0;
  1338. switch (u.r.bRequest) {
  1339. case USB_REQ_SET_CONFIGURATION:
  1340. /* udc needs to know when ep != 0 is valid */
  1341. if (u.r.bRequestType != USB_RECIP_DEVICE)
  1342. goto delegate;
  1343. if (w_length != 0)
  1344. goto do_stall;
  1345. udc->ep0_set_config = 1;
  1346. udc->ep0_reset_config = (w_value == 0);
  1347. VDBG("set config %d\n", w_value);
  1348. /* update udc NOW since gadget driver may start
  1349. * queueing requests immediately; clear config
  1350. * later if it fails the request.
  1351. */
  1352. if (udc->ep0_reset_config)
  1353. UDC_SYSCON2_REG = UDC_CLR_CFG;
  1354. else
  1355. UDC_SYSCON2_REG = UDC_DEV_CFG;
  1356. update_otg(udc);
  1357. goto delegate;
  1358. case USB_REQ_CLEAR_FEATURE:
  1359. /* clear endpoint halt */
  1360. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1361. goto delegate;
  1362. if (w_value != USB_ENDPOINT_HALT
  1363. || w_length != 0)
  1364. goto do_stall;
  1365. ep = &udc->ep[w_index & 0xf];
  1366. if (ep != ep0) {
  1367. if (w_index & USB_DIR_IN)
  1368. ep += 16;
  1369. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1370. || !ep->desc)
  1371. goto do_stall;
  1372. use_ep(ep, 0);
  1373. UDC_CTRL_REG = udc->clr_halt;
  1374. ep->ackwait = 0;
  1375. if (!(ep->bEndpointAddress & USB_DIR_IN)) {
  1376. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1377. ep->ackwait = 1 + ep->double_buf;
  1378. }
  1379. /* NOTE: assumes the host behaves sanely,
  1380. * only clearing real halts. Else we may
  1381. * need to kill pending transfers and then
  1382. * restart the queue... very messy for DMA!
  1383. */
  1384. }
  1385. VDBG("%s halt cleared by host\n", ep->name);
  1386. goto ep0out_status_stage;
  1387. case USB_REQ_SET_FEATURE:
  1388. /* set endpoint halt */
  1389. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  1390. goto delegate;
  1391. if (w_value != USB_ENDPOINT_HALT
  1392. || w_length != 0)
  1393. goto do_stall;
  1394. ep = &udc->ep[w_index & 0xf];
  1395. if (w_index & USB_DIR_IN)
  1396. ep += 16;
  1397. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  1398. || ep == ep0 || !ep->desc)
  1399. goto do_stall;
  1400. if (use_dma && ep->has_dma) {
  1401. /* this has rude side-effects (aborts) and
  1402. * can't really work if DMA-IN is active
  1403. */
  1404. DBG("%s host set_halt, NYET \n", ep->name);
  1405. goto do_stall;
  1406. }
  1407. use_ep(ep, 0);
  1408. /* can't halt if fifo isn't empty... */
  1409. UDC_CTRL_REG = UDC_CLR_EP;
  1410. UDC_CTRL_REG = UDC_SET_HALT;
  1411. VDBG("%s halted by host\n", ep->name);
  1412. ep0out_status_stage:
  1413. status = 0;
  1414. UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
  1415. UDC_CTRL_REG = UDC_CLR_EP;
  1416. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1417. UDC_EP_NUM_REG = UDC_EP_DIR;
  1418. udc->ep0_pending = 0;
  1419. break;
  1420. case USB_REQ_GET_STATUS:
  1421. /* return interface status. if we were pedantic,
  1422. * we'd detect non-existent interfaces, and stall.
  1423. */
  1424. if (u.r.bRequestType
  1425. != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1426. goto delegate;
  1427. /* return two zero bytes */
  1428. UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
  1429. UDC_DATA_REG = 0;
  1430. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1431. UDC_EP_NUM_REG = UDC_EP_DIR;
  1432. status = 0;
  1433. VDBG("GET_STATUS, interface %d\n", w_index);
  1434. /* next, status stage */
  1435. break;
  1436. default:
  1437. delegate:
  1438. /* activate the ep0out fifo right away */
  1439. if (!udc->ep0_in && w_length) {
  1440. UDC_EP_NUM_REG = 0;
  1441. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1442. }
  1443. /* gadget drivers see class/vendor specific requests,
  1444. * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
  1445. * and more
  1446. */
  1447. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  1448. u.r.bRequestType, u.r.bRequest,
  1449. w_value, w_index, w_length);
  1450. #undef w_value
  1451. #undef w_index
  1452. #undef w_length
  1453. /* The gadget driver may return an error here,
  1454. * causing an immediate protocol stall.
  1455. *
  1456. * Else it must issue a response, either queueing a
  1457. * response buffer for the DATA stage, or halting ep0
  1458. * (causing a protocol stall, not a real halt). A
  1459. * zero length buffer means no DATA stage.
  1460. *
  1461. * It's fine to issue that response after the setup()
  1462. * call returns, and this IRQ was handled.
  1463. */
  1464. udc->ep0_setup = 1;
  1465. spin_unlock(&udc->lock);
  1466. status = udc->driver->setup (&udc->gadget, &u.r);
  1467. spin_lock(&udc->lock);
  1468. udc->ep0_setup = 0;
  1469. }
  1470. if (status < 0) {
  1471. do_stall:
  1472. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1473. u.r.bRequestType, u.r.bRequest, status);
  1474. if (udc->ep0_set_config) {
  1475. if (udc->ep0_reset_config)
  1476. WARN("error resetting config?\n");
  1477. else
  1478. UDC_SYSCON2_REG = UDC_CLR_CFG;
  1479. }
  1480. UDC_SYSCON2_REG = UDC_STALL_CMD;
  1481. udc->ep0_pending = 0;
  1482. }
  1483. }
  1484. }
  1485. /*-------------------------------------------------------------------------*/
  1486. #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
  1487. static void devstate_irq(struct omap_udc *udc, u16 irq_src)
  1488. {
  1489. u16 devstat, change;
  1490. devstat = UDC_DEVSTAT_REG;
  1491. change = devstat ^ udc->devstat;
  1492. udc->devstat = devstat;
  1493. if (change & (UDC_USB_RESET|UDC_ATT)) {
  1494. udc_quiesce(udc);
  1495. if (change & UDC_ATT) {
  1496. /* driver for any external transceiver will
  1497. * have called omap_vbus_session() already
  1498. */
  1499. if (devstat & UDC_ATT) {
  1500. udc->gadget.speed = USB_SPEED_FULL;
  1501. VDBG("connect\n");
  1502. if (!udc->transceiver)
  1503. pullup_enable(udc);
  1504. // if (driver->connect) call it
  1505. } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1506. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1507. if (!udc->transceiver)
  1508. pullup_disable(udc);
  1509. DBG("disconnect, gadget %s\n",
  1510. udc->driver->driver.name);
  1511. if (udc->driver->disconnect) {
  1512. spin_unlock(&udc->lock);
  1513. udc->driver->disconnect(&udc->gadget);
  1514. spin_lock(&udc->lock);
  1515. }
  1516. }
  1517. change &= ~UDC_ATT;
  1518. }
  1519. if (change & UDC_USB_RESET) {
  1520. if (devstat & UDC_USB_RESET) {
  1521. VDBG("RESET=1\n");
  1522. } else {
  1523. udc->gadget.speed = USB_SPEED_FULL;
  1524. INFO("USB reset done, gadget %s\n",
  1525. udc->driver->driver.name);
  1526. /* ep0 traffic is legal from now on */
  1527. UDC_IRQ_EN_REG = UDC_DS_CHG_IE | UDC_EP0_IE;
  1528. }
  1529. change &= ~UDC_USB_RESET;
  1530. }
  1531. }
  1532. if (change & UDC_SUS) {
  1533. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1534. // FIXME tell isp1301 to suspend/resume (?)
  1535. if (devstat & UDC_SUS) {
  1536. VDBG("suspend\n");
  1537. update_otg(udc);
  1538. /* HNP could be under way already */
  1539. if (udc->gadget.speed == USB_SPEED_FULL
  1540. && udc->driver->suspend) {
  1541. spin_unlock(&udc->lock);
  1542. udc->driver->suspend(&udc->gadget);
  1543. spin_lock(&udc->lock);
  1544. }
  1545. if (udc->transceiver)
  1546. otg_set_suspend(udc->transceiver, 1);
  1547. } else {
  1548. VDBG("resume\n");
  1549. if (udc->transceiver)
  1550. otg_set_suspend(udc->transceiver, 0);
  1551. if (udc->gadget.speed == USB_SPEED_FULL
  1552. && udc->driver->resume) {
  1553. spin_unlock(&udc->lock);
  1554. udc->driver->resume(&udc->gadget);
  1555. spin_lock(&udc->lock);
  1556. }
  1557. }
  1558. }
  1559. change &= ~UDC_SUS;
  1560. }
  1561. if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
  1562. update_otg(udc);
  1563. change &= ~OTG_FLAGS;
  1564. }
  1565. change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
  1566. if (change)
  1567. VDBG("devstat %03x, ignore change %03x\n",
  1568. devstat, change);
  1569. UDC_IRQ_SRC_REG = UDC_DS_CHG;
  1570. }
  1571. static irqreturn_t
  1572. omap_udc_irq(int irq, void *_udc, struct pt_regs *r)
  1573. {
  1574. struct omap_udc *udc = _udc;
  1575. u16 irq_src;
  1576. irqreturn_t status = IRQ_NONE;
  1577. unsigned long flags;
  1578. spin_lock_irqsave(&udc->lock, flags);
  1579. irq_src = UDC_IRQ_SRC_REG;
  1580. /* Device state change (usb ch9 stuff) */
  1581. if (irq_src & UDC_DS_CHG) {
  1582. devstate_irq(_udc, irq_src);
  1583. status = IRQ_HANDLED;
  1584. irq_src &= ~UDC_DS_CHG;
  1585. }
  1586. /* EP0 control transfers */
  1587. if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
  1588. ep0_irq(_udc, irq_src);
  1589. status = IRQ_HANDLED;
  1590. irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
  1591. }
  1592. /* DMA transfer completion */
  1593. if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
  1594. dma_irq(_udc, irq_src);
  1595. status = IRQ_HANDLED;
  1596. irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
  1597. }
  1598. irq_src &= ~(UDC_SOF|UDC_EPN_TX|UDC_EPN_RX);
  1599. if (irq_src)
  1600. DBG("udc_irq, unhandled %03x\n", irq_src);
  1601. spin_unlock_irqrestore(&udc->lock, flags);
  1602. return status;
  1603. }
  1604. /* workaround for seemingly-lost IRQs for RX ACKs... */
  1605. #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
  1606. #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
  1607. static void pio_out_timer(unsigned long _ep)
  1608. {
  1609. struct omap_ep *ep = (void *) _ep;
  1610. unsigned long flags;
  1611. u16 stat_flg;
  1612. spin_lock_irqsave(&ep->udc->lock, flags);
  1613. if (!list_empty(&ep->queue) && ep->ackwait) {
  1614. use_ep(ep, 0);
  1615. stat_flg = UDC_STAT_FLG_REG;
  1616. if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
  1617. || (ep->double_buf && HALF_FULL(stat_flg)))) {
  1618. struct omap_req *req;
  1619. VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
  1620. req = container_of(ep->queue.next,
  1621. struct omap_req, queue);
  1622. UDC_EP_NUM_REG = ep->bEndpointAddress | UDC_EP_SEL;
  1623. (void) read_fifo(ep, req);
  1624. UDC_EP_NUM_REG = ep->bEndpointAddress;
  1625. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1626. ep->ackwait = 1 + ep->double_buf;
  1627. }
  1628. }
  1629. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1630. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1631. }
  1632. static irqreturn_t
  1633. omap_udc_pio_irq(int irq, void *_dev, struct pt_regs *r)
  1634. {
  1635. u16 epn_stat, irq_src;
  1636. irqreturn_t status = IRQ_NONE;
  1637. struct omap_ep *ep;
  1638. int epnum;
  1639. struct omap_udc *udc = _dev;
  1640. struct omap_req *req;
  1641. unsigned long flags;
  1642. spin_lock_irqsave(&udc->lock, flags);
  1643. epn_stat = UDC_EPN_STAT_REG;
  1644. irq_src = UDC_IRQ_SRC_REG;
  1645. /* handle OUT first, to avoid some wasteful NAKs */
  1646. if (irq_src & UDC_EPN_RX) {
  1647. epnum = (epn_stat >> 8) & 0x0f;
  1648. UDC_IRQ_SRC_REG = UDC_EPN_RX;
  1649. status = IRQ_HANDLED;
  1650. ep = &udc->ep[epnum];
  1651. ep->irqs++;
  1652. UDC_EP_NUM_REG = epnum | UDC_EP_SEL;
  1653. ep->fnf = 0;
  1654. if ((UDC_STAT_FLG_REG & UDC_ACK)) {
  1655. ep->ackwait--;
  1656. if (!list_empty(&ep->queue)) {
  1657. int stat;
  1658. req = container_of(ep->queue.next,
  1659. struct omap_req, queue);
  1660. stat = read_fifo(ep, req);
  1661. if (!ep->double_buf)
  1662. ep->fnf = 1;
  1663. }
  1664. }
  1665. /* min 6 clock delay before clearing EP_SEL ... */
  1666. epn_stat = UDC_EPN_STAT_REG;
  1667. epn_stat = UDC_EPN_STAT_REG;
  1668. UDC_EP_NUM_REG = epnum;
  1669. /* enabling fifo _after_ clearing ACK, contrary to docs,
  1670. * reduces lossage; timer still needed though (sigh).
  1671. */
  1672. if (ep->fnf) {
  1673. UDC_CTRL_REG = UDC_SET_FIFO_EN;
  1674. ep->ackwait = 1 + ep->double_buf;
  1675. }
  1676. mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
  1677. }
  1678. /* then IN transfers */
  1679. else if (irq_src & UDC_EPN_TX) {
  1680. epnum = epn_stat & 0x0f;
  1681. UDC_IRQ_SRC_REG = UDC_EPN_TX;
  1682. status = IRQ_HANDLED;
  1683. ep = &udc->ep[16 + epnum];
  1684. ep->irqs++;
  1685. UDC_EP_NUM_REG = epnum | UDC_EP_DIR | UDC_EP_SEL;
  1686. if ((UDC_STAT_FLG_REG & UDC_ACK)) {
  1687. ep->ackwait = 0;
  1688. if (!list_empty(&ep->queue)) {
  1689. req = container_of(ep->queue.next,
  1690. struct omap_req, queue);
  1691. (void) write_fifo(ep, req);
  1692. }
  1693. }
  1694. /* min 6 clock delay before clearing EP_SEL ... */
  1695. epn_stat = UDC_EPN_STAT_REG;
  1696. epn_stat = UDC_EPN_STAT_REG;
  1697. UDC_EP_NUM_REG = epnum | UDC_EP_DIR;
  1698. /* then 6 clocks before it'd tx */
  1699. }
  1700. spin_unlock_irqrestore(&udc->lock, flags);
  1701. return status;
  1702. }
  1703. #ifdef USE_ISO
  1704. static irqreturn_t
  1705. omap_udc_iso_irq(int irq, void *_dev, struct pt_regs *r)
  1706. {
  1707. struct omap_udc *udc = _dev;
  1708. struct omap_ep *ep;
  1709. int pending = 0;
  1710. unsigned long flags;
  1711. spin_lock_irqsave(&udc->lock, flags);
  1712. /* handle all non-DMA ISO transfers */
  1713. list_for_each_entry (ep, &udc->iso, iso) {
  1714. u16 stat;
  1715. struct omap_req *req;
  1716. if (ep->has_dma || list_empty(&ep->queue))
  1717. continue;
  1718. req = list_entry(ep->queue.next, struct omap_req, queue);
  1719. use_ep(ep, UDC_EP_SEL);
  1720. stat = UDC_STAT_FLG_REG;
  1721. /* NOTE: like the other controller drivers, this isn't
  1722. * currently reporting lost or damaged frames.
  1723. */
  1724. if (ep->bEndpointAddress & USB_DIR_IN) {
  1725. if (stat & UDC_MISS_IN)
  1726. /* done(ep, req, -EPROTO) */;
  1727. else
  1728. write_fifo(ep, req);
  1729. } else {
  1730. int status = 0;
  1731. if (stat & UDC_NO_RXPACKET)
  1732. status = -EREMOTEIO;
  1733. else if (stat & UDC_ISO_ERR)
  1734. status = -EILSEQ;
  1735. else if (stat & UDC_DATA_FLUSH)
  1736. status = -ENOSR;
  1737. if (status)
  1738. /* done(ep, req, status) */;
  1739. else
  1740. read_fifo(ep, req);
  1741. }
  1742. deselect_ep();
  1743. /* 6 wait states before next EP */
  1744. ep->irqs++;
  1745. if (!list_empty(&ep->queue))
  1746. pending = 1;
  1747. }
  1748. if (!pending)
  1749. UDC_IRQ_EN_REG &= ~UDC_SOF_IE;
  1750. UDC_IRQ_SRC_REG = UDC_SOF;
  1751. spin_unlock_irqrestore(&udc->lock, flags);
  1752. return IRQ_HANDLED;
  1753. }
  1754. #endif
  1755. /*-------------------------------------------------------------------------*/
  1756. static struct omap_udc *udc;
  1757. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1758. {
  1759. int status = -ENODEV;
  1760. struct omap_ep *ep;
  1761. unsigned long flags;
  1762. /* basic sanity tests */
  1763. if (!udc)
  1764. return -ENODEV;
  1765. if (!driver
  1766. // FIXME if otg, check: driver->is_otg
  1767. || driver->speed < USB_SPEED_FULL
  1768. || !driver->bind
  1769. || !driver->unbind
  1770. || !driver->setup)
  1771. return -EINVAL;
  1772. spin_lock_irqsave(&udc->lock, flags);
  1773. if (udc->driver) {
  1774. spin_unlock_irqrestore(&udc->lock, flags);
  1775. return -EBUSY;
  1776. }
  1777. /* reset state */
  1778. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  1779. ep->irqs = 0;
  1780. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  1781. continue;
  1782. use_ep(ep, 0);
  1783. UDC_CTRL_REG = UDC_SET_HALT;
  1784. }
  1785. udc->ep0_pending = 0;
  1786. udc->ep[0].irqs = 0;
  1787. udc->softconnect = 1;
  1788. /* hook up the driver */
  1789. driver->driver.bus = NULL;
  1790. udc->driver = driver;
  1791. udc->gadget.dev.driver = &driver->driver;
  1792. spin_unlock_irqrestore(&udc->lock, flags);
  1793. status = driver->bind (&udc->gadget);
  1794. if (status) {
  1795. DBG("bind to %s --> %d\n", driver->driver.name, status);
  1796. udc->gadget.dev.driver = NULL;
  1797. udc->driver = NULL;
  1798. goto done;
  1799. }
  1800. DBG("bound to driver %s\n", driver->driver.name);
  1801. UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
  1802. /* connect to bus through transceiver */
  1803. if (udc->transceiver) {
  1804. status = otg_set_peripheral(udc->transceiver, &udc->gadget);
  1805. if (status < 0) {
  1806. ERR("can't bind to transceiver\n");
  1807. driver->unbind (&udc->gadget);
  1808. udc->gadget.dev.driver = NULL;
  1809. udc->driver = NULL;
  1810. goto done;
  1811. }
  1812. } else {
  1813. if (can_pullup(udc))
  1814. pullup_enable (udc);
  1815. else
  1816. pullup_disable (udc);
  1817. }
  1818. /* boards that don't have VBUS sensing can't autogate 48MHz;
  1819. * can't enter deep sleep while a gadget driver is active.
  1820. */
  1821. if (machine_is_omap_innovator() || machine_is_omap_osk())
  1822. omap_vbus_session(&udc->gadget, 1);
  1823. done:
  1824. return status;
  1825. }
  1826. EXPORT_SYMBOL(usb_gadget_register_driver);
  1827. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1828. {
  1829. unsigned long flags;
  1830. int status = -ENODEV;
  1831. if (!udc)
  1832. return -ENODEV;
  1833. if (!driver || driver != udc->driver)
  1834. return -EINVAL;
  1835. if (machine_is_omap_innovator() || machine_is_omap_osk())
  1836. omap_vbus_session(&udc->gadget, 0);
  1837. if (udc->transceiver)
  1838. (void) otg_set_peripheral(udc->transceiver, NULL);
  1839. else
  1840. pullup_disable(udc);
  1841. spin_lock_irqsave(&udc->lock, flags);
  1842. udc_quiesce(udc);
  1843. spin_unlock_irqrestore(&udc->lock, flags);
  1844. driver->unbind(&udc->gadget);
  1845. udc->gadget.dev.driver = NULL;
  1846. udc->driver = NULL;
  1847. DBG("unregistered driver '%s'\n", driver->driver.name);
  1848. return status;
  1849. }
  1850. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1851. /*-------------------------------------------------------------------------*/
  1852. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1853. #include <linux/seq_file.h>
  1854. static const char proc_filename[] = "driver/udc";
  1855. #define FOURBITS "%s%s%s%s"
  1856. #define EIGHTBITS FOURBITS FOURBITS
  1857. static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
  1858. {
  1859. u16 stat_flg;
  1860. struct omap_req *req;
  1861. char buf[20];
  1862. use_ep(ep, 0);
  1863. if (use_dma && ep->has_dma)
  1864. snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
  1865. (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
  1866. ep->dma_channel - 1, ep->lch);
  1867. else
  1868. buf[0] = 0;
  1869. stat_flg = UDC_STAT_FLG_REG;
  1870. seq_printf(s,
  1871. "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
  1872. ep->name, buf,
  1873. ep->double_buf ? "dbuf " : "",
  1874. ({char *s; switch(ep->ackwait){
  1875. case 0: s = ""; break;
  1876. case 1: s = "(ackw) "; break;
  1877. case 2: s = "(ackw2) "; break;
  1878. default: s = "(?) "; break;
  1879. } s;}),
  1880. ep->irqs, stat_flg,
  1881. (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
  1882. (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
  1883. (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
  1884. (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
  1885. (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
  1886. (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
  1887. (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
  1888. (stat_flg & UDC_STALL) ? "STALL " : "",
  1889. (stat_flg & UDC_NAK) ? "NAK " : "",
  1890. (stat_flg & UDC_ACK) ? "ACK " : "",
  1891. (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
  1892. (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
  1893. (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
  1894. if (list_empty (&ep->queue))
  1895. seq_printf(s, "\t(queue empty)\n");
  1896. else
  1897. list_for_each_entry (req, &ep->queue, queue) {
  1898. unsigned length = req->req.actual;
  1899. if (use_dma && buf[0]) {
  1900. length += ((ep->bEndpointAddress & USB_DIR_IN)
  1901. ? dma_src_len : dma_dest_len)
  1902. (ep, req->req.dma + length);
  1903. buf[0] = 0;
  1904. }
  1905. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  1906. &req->req, length,
  1907. req->req.length, req->req.buf);
  1908. }
  1909. }
  1910. static char *trx_mode(unsigned m, int enabled)
  1911. {
  1912. switch (m) {
  1913. case 0: return enabled ? "*6wire" : "unused";
  1914. case 1: return "4wire";
  1915. case 2: return "3wire";
  1916. case 3: return "6wire";
  1917. default: return "unknown";
  1918. }
  1919. }
  1920. static int proc_otg_show(struct seq_file *s)
  1921. {
  1922. u32 tmp;
  1923. u32 trans;
  1924. tmp = OTG_REV_REG;
  1925. trans = USB_TRANSCEIVER_CTRL_REG;
  1926. seq_printf(s, "\nOTG rev %d.%d, transceiver_ctrl %05x\n",
  1927. tmp >> 4, tmp & 0xf, trans);
  1928. tmp = OTG_SYSCON_1_REG;
  1929. seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
  1930. FOURBITS "\n", tmp,
  1931. trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
  1932. trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
  1933. (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
  1934. ? "internal"
  1935. : trx_mode(USB0_TRX_MODE(tmp), 1),
  1936. (tmp & OTG_IDLE_EN) ? " !otg" : "",
  1937. (tmp & HST_IDLE_EN) ? " !host" : "",
  1938. (tmp & DEV_IDLE_EN) ? " !dev" : "",
  1939. (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
  1940. tmp = OTG_SYSCON_2_REG;
  1941. seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
  1942. " b_ase_brst=%d hmc=%d\n", tmp,
  1943. (tmp & OTG_EN) ? " otg_en" : "",
  1944. (tmp & USBX_SYNCHRO) ? " synchro" : "",
  1945. // much more SRP stuff
  1946. (tmp & SRP_DATA) ? " srp_data" : "",
  1947. (tmp & SRP_VBUS) ? " srp_vbus" : "",
  1948. (tmp & OTG_PADEN) ? " otg_paden" : "",
  1949. (tmp & HMC_PADEN) ? " hmc_paden" : "",
  1950. (tmp & UHOST_EN) ? " uhost_en" : "",
  1951. (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
  1952. (tmp & HMC_TLLATTACH) ? " tllattach" : "",
  1953. B_ASE_BRST(tmp),
  1954. OTG_HMC(tmp));
  1955. tmp = OTG_CTRL_REG;
  1956. seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
  1957. (tmp & OTG_ASESSVLD) ? " asess" : "",
  1958. (tmp & OTG_BSESSEND) ? " bsess_end" : "",
  1959. (tmp & OTG_BSESSVLD) ? " bsess" : "",
  1960. (tmp & OTG_VBUSVLD) ? " vbus" : "",
  1961. (tmp & OTG_ID) ? " id" : "",
  1962. (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
  1963. (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
  1964. (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
  1965. (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
  1966. (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
  1967. (tmp & OTG_BUSDROP) ? " busdrop" : "",
  1968. (tmp & OTG_PULLDOWN) ? " down" : "",
  1969. (tmp & OTG_PULLUP) ? " up" : "",
  1970. (tmp & OTG_DRV_VBUS) ? " drv" : "",
  1971. (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
  1972. (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
  1973. (tmp & OTG_PU_ID) ? " pu_id" : ""
  1974. );
  1975. tmp = OTG_IRQ_EN_REG;
  1976. seq_printf(s, "otg_irq_en %04x" "\n", tmp);
  1977. tmp = OTG_IRQ_SRC_REG;
  1978. seq_printf(s, "otg_irq_src %04x" "\n", tmp);
  1979. tmp = OTG_OUTCTRL_REG;
  1980. seq_printf(s, "otg_outctrl %04x" "\n", tmp);
  1981. tmp = OTG_TEST_REG;
  1982. seq_printf(s, "otg_test %04x" "\n", tmp);
  1983. return 0;
  1984. }
  1985. static int proc_udc_show(struct seq_file *s, void *_)
  1986. {
  1987. u32 tmp;
  1988. struct omap_ep *ep;
  1989. unsigned long flags;
  1990. spin_lock_irqsave(&udc->lock, flags);
  1991. seq_printf(s, "%s, version: " DRIVER_VERSION
  1992. #ifdef USE_ISO
  1993. " (iso)"
  1994. #endif
  1995. "%s\n",
  1996. driver_desc,
  1997. use_dma ? " (dma)" : "");
  1998. tmp = UDC_REV_REG & 0xff;
  1999. seq_printf(s,
  2000. "UDC rev %d.%d, fifo mode %d, gadget %s\n"
  2001. "hmc %d, transceiver %s\n",
  2002. tmp >> 4, tmp & 0xf,
  2003. fifo_mode,
  2004. udc->driver ? udc->driver->driver.name : "(none)",
  2005. HMC,
  2006. udc->transceiver ? udc->transceiver->label : "(none)");
  2007. seq_printf(s, "ULPD control %04x req %04x status %04x\n",
  2008. __REG16(ULPD_CLOCK_CTRL),
  2009. __REG16(ULPD_SOFT_REQ),
  2010. __REG16(ULPD_STATUS_REQ));
  2011. /* OTG controller registers */
  2012. if (!cpu_is_omap15xx())
  2013. proc_otg_show(s);
  2014. tmp = UDC_SYSCON1_REG;
  2015. seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
  2016. (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
  2017. (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
  2018. (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
  2019. (tmp & UDC_NAK_EN) ? " nak" : "",
  2020. (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
  2021. (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
  2022. (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
  2023. (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
  2024. // syscon2 is write-only
  2025. /* UDC controller registers */
  2026. if (!(tmp & UDC_PULLUP_EN)) {
  2027. seq_printf(s, "(suspended)\n");
  2028. spin_unlock_irqrestore(&udc->lock, flags);
  2029. return 0;
  2030. }
  2031. tmp = UDC_DEVSTAT_REG;
  2032. seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
  2033. (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
  2034. (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
  2035. (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
  2036. (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
  2037. (tmp & UDC_USB_RESET) ? " usb_reset" : "",
  2038. (tmp & UDC_SUS) ? " SUS" : "",
  2039. (tmp & UDC_CFG) ? " CFG" : "",
  2040. (tmp & UDC_ADD) ? " ADD" : "",
  2041. (tmp & UDC_DEF) ? " DEF" : "",
  2042. (tmp & UDC_ATT) ? " ATT" : "");
  2043. seq_printf(s, "sof %04x\n", UDC_SOF_REG);
  2044. tmp = UDC_IRQ_EN_REG;
  2045. seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
  2046. (tmp & UDC_SOF_IE) ? " sof" : "",
  2047. (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
  2048. (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
  2049. (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
  2050. (tmp & UDC_EP0_IE) ? " ep0" : "");
  2051. tmp = UDC_IRQ_SRC_REG;
  2052. seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
  2053. (tmp & UDC_TXN_DONE) ? " txn_done" : "",
  2054. (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
  2055. (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
  2056. (tmp & UDC_SOF) ? " sof" : "",
  2057. (tmp & UDC_EPN_RX) ? " epn_rx" : "",
  2058. (tmp & UDC_EPN_TX) ? " epn_tx" : "",
  2059. (tmp & UDC_DS_CHG) ? " ds_chg" : "",
  2060. (tmp & UDC_SETUP) ? " setup" : "",
  2061. (tmp & UDC_EP0_RX) ? " ep0out" : "",
  2062. (tmp & UDC_EP0_TX) ? " ep0in" : "");
  2063. if (use_dma) {
  2064. unsigned i;
  2065. tmp = UDC_DMA_IRQ_EN_REG;
  2066. seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
  2067. (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
  2068. (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
  2069. (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
  2070. (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
  2071. (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
  2072. (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
  2073. (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
  2074. (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
  2075. (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
  2076. tmp = UDC_RXDMA_CFG_REG;
  2077. seq_printf(s, "rxdma_cfg %04x\n", tmp);
  2078. if (tmp) {
  2079. for (i = 0; i < 3; i++) {
  2080. if ((tmp & (0x0f << (i * 4))) == 0)
  2081. continue;
  2082. seq_printf(s, "rxdma[%d] %04x\n", i,
  2083. UDC_RXDMA_REG(i + 1));
  2084. }
  2085. }
  2086. tmp = UDC_TXDMA_CFG_REG;
  2087. seq_printf(s, "txdma_cfg %04x\n", tmp);
  2088. if (tmp) {
  2089. for (i = 0; i < 3; i++) {
  2090. if (!(tmp & (0x0f << (i * 4))))
  2091. continue;
  2092. seq_printf(s, "txdma[%d] %04x\n", i,
  2093. UDC_TXDMA_REG(i + 1));
  2094. }
  2095. }
  2096. }
  2097. tmp = UDC_DEVSTAT_REG;
  2098. if (tmp & UDC_ATT) {
  2099. proc_ep_show(s, &udc->ep[0]);
  2100. if (tmp & UDC_ADD) {
  2101. list_for_each_entry (ep, &udc->gadget.ep_list,
  2102. ep.ep_list) {
  2103. if (ep->desc)
  2104. proc_ep_show(s, ep);
  2105. }
  2106. }
  2107. }
  2108. spin_unlock_irqrestore(&udc->lock, flags);
  2109. return 0;
  2110. }
  2111. static int proc_udc_open(struct inode *inode, struct file *file)
  2112. {
  2113. return single_open(file, proc_udc_show, NULL);
  2114. }
  2115. static struct file_operations proc_ops = {
  2116. .open = proc_udc_open,
  2117. .read = seq_read,
  2118. .llseek = seq_lseek,
  2119. .release = single_release,
  2120. };
  2121. static void create_proc_file(void)
  2122. {
  2123. struct proc_dir_entry *pde;
  2124. pde = create_proc_entry (proc_filename, 0, NULL);
  2125. if (pde)
  2126. pde->proc_fops = &proc_ops;
  2127. }
  2128. static void remove_proc_file(void)
  2129. {
  2130. remove_proc_entry(proc_filename, NULL);
  2131. }
  2132. #else
  2133. static inline void create_proc_file(void) {}
  2134. static inline void remove_proc_file(void) {}
  2135. #endif
  2136. /*-------------------------------------------------------------------------*/
  2137. /* Before this controller can enumerate, we need to pick an endpoint
  2138. * configuration, or "fifo_mode" That involves allocating 2KB of packet
  2139. * buffer space among the endpoints we'll be operating.
  2140. *
  2141. * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
  2142. * UDC_SYSCON_1_REG.CFG_LOCK is set can now work. We won't use that
  2143. * capability yet though.
  2144. */
  2145. static unsigned __init
  2146. omap_ep_setup(char *name, u8 addr, u8 type,
  2147. unsigned buf, unsigned maxp, int dbuf)
  2148. {
  2149. struct omap_ep *ep;
  2150. u16 epn_rxtx = 0;
  2151. /* OUT endpoints first, then IN */
  2152. ep = &udc->ep[addr & 0xf];
  2153. if (addr & USB_DIR_IN)
  2154. ep += 16;
  2155. /* in case of ep init table bugs */
  2156. BUG_ON(ep->name[0]);
  2157. /* chip setup ... bit values are same for IN, OUT */
  2158. if (type == USB_ENDPOINT_XFER_ISOC) {
  2159. switch (maxp) {
  2160. case 8: epn_rxtx = 0 << 12; break;
  2161. case 16: epn_rxtx = 1 << 12; break;
  2162. case 32: epn_rxtx = 2 << 12; break;
  2163. case 64: epn_rxtx = 3 << 12; break;
  2164. case 128: epn_rxtx = 4 << 12; break;
  2165. case 256: epn_rxtx = 5 << 12; break;
  2166. case 512: epn_rxtx = 6 << 12; break;
  2167. default: BUG();
  2168. }
  2169. epn_rxtx |= UDC_EPN_RX_ISO;
  2170. dbuf = 1;
  2171. } else {
  2172. /* double-buffering "not supported" on 15xx,
  2173. * and ignored for PIO-IN on 16xx
  2174. */
  2175. if (!use_dma || cpu_is_omap15xx())
  2176. dbuf = 0;
  2177. switch (maxp) {
  2178. case 8: epn_rxtx = 0 << 12; break;
  2179. case 16: epn_rxtx = 1 << 12; break;
  2180. case 32: epn_rxtx = 2 << 12; break;
  2181. case 64: epn_rxtx = 3 << 12; break;
  2182. default: BUG();
  2183. }
  2184. if (dbuf && addr)
  2185. epn_rxtx |= UDC_EPN_RX_DB;
  2186. init_timer(&ep->timer);
  2187. ep->timer.function = pio_out_timer;
  2188. ep->timer.data = (unsigned long) ep;
  2189. }
  2190. if (addr)
  2191. epn_rxtx |= UDC_EPN_RX_VALID;
  2192. BUG_ON(buf & 0x07);
  2193. epn_rxtx |= buf >> 3;
  2194. DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
  2195. name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
  2196. if (addr & USB_DIR_IN)
  2197. UDC_EP_TX_REG(addr & 0xf) = epn_rxtx;
  2198. else
  2199. UDC_EP_RX_REG(addr) = epn_rxtx;
  2200. /* next endpoint's buffer starts after this one's */
  2201. buf += maxp;
  2202. if (dbuf)
  2203. buf += maxp;
  2204. BUG_ON(buf > 2048);
  2205. /* set up driver data structures */
  2206. BUG_ON(strlen(name) >= sizeof ep->name);
  2207. strlcpy(ep->name, name, sizeof ep->name);
  2208. INIT_LIST_HEAD(&ep->queue);
  2209. INIT_LIST_HEAD(&ep->iso);
  2210. ep->bEndpointAddress = addr;
  2211. ep->bmAttributes = type;
  2212. ep->double_buf = dbuf;
  2213. ep->udc = udc;
  2214. ep->ep.name = ep->name;
  2215. ep->ep.ops = &omap_ep_ops;
  2216. ep->ep.maxpacket = ep->maxpacket = maxp;
  2217. list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
  2218. return buf;
  2219. }
  2220. static void omap_udc_release(struct device *dev)
  2221. {
  2222. complete(udc->done);
  2223. kfree (udc);
  2224. udc = NULL;
  2225. }
  2226. static int __init
  2227. omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
  2228. {
  2229. unsigned tmp, buf;
  2230. /* abolish any previous hardware state */
  2231. UDC_SYSCON1_REG = 0;
  2232. UDC_IRQ_EN_REG = 0;
  2233. UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
  2234. UDC_DMA_IRQ_EN_REG = 0;
  2235. UDC_RXDMA_CFG_REG = 0;
  2236. UDC_TXDMA_CFG_REG = 0;
  2237. /* UDC_PULLUP_EN gates the chip clock */
  2238. // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
  2239. udc = kzalloc(sizeof(*udc), SLAB_KERNEL);
  2240. if (!udc)
  2241. return -ENOMEM;
  2242. spin_lock_init (&udc->lock);
  2243. udc->gadget.ops = &omap_gadget_ops;
  2244. udc->gadget.ep0 = &udc->ep[0].ep;
  2245. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2246. INIT_LIST_HEAD(&udc->iso);
  2247. udc->gadget.speed = USB_SPEED_UNKNOWN;
  2248. udc->gadget.name = driver_name;
  2249. device_initialize(&udc->gadget.dev);
  2250. strcpy (udc->gadget.dev.bus_id, "gadget");
  2251. udc->gadget.dev.release = omap_udc_release;
  2252. udc->gadget.dev.parent = &odev->dev;
  2253. if (use_dma)
  2254. udc->gadget.dev.dma_mask = odev->dev.dma_mask;
  2255. udc->transceiver = xceiv;
  2256. /* ep0 is special; put it right after the SETUP buffer */
  2257. buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
  2258. 8 /* after SETUP */, 64 /* maxpacket */, 0);
  2259. list_del_init(&udc->ep[0].ep.ep_list);
  2260. /* initially disable all non-ep0 endpoints */
  2261. for (tmp = 1; tmp < 15; tmp++) {
  2262. UDC_EP_RX_REG(tmp) = 0;
  2263. UDC_EP_TX_REG(tmp) = 0;
  2264. }
  2265. #define OMAP_BULK_EP(name,addr) \
  2266. buf = omap_ep_setup(name "-bulk", addr, \
  2267. USB_ENDPOINT_XFER_BULK, buf, 64, 1);
  2268. #define OMAP_INT_EP(name,addr, maxp) \
  2269. buf = omap_ep_setup(name "-int", addr, \
  2270. USB_ENDPOINT_XFER_INT, buf, maxp, 0);
  2271. #define OMAP_ISO_EP(name,addr, maxp) \
  2272. buf = omap_ep_setup(name "-iso", addr, \
  2273. USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
  2274. switch (fifo_mode) {
  2275. case 0:
  2276. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2277. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2278. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2279. break;
  2280. case 1:
  2281. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2282. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2283. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2284. OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
  2285. OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
  2286. OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
  2287. OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
  2288. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2289. OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
  2290. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2291. OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
  2292. OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
  2293. OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
  2294. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2295. OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
  2296. OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
  2297. OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
  2298. OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
  2299. OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
  2300. OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
  2301. OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
  2302. OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
  2303. break;
  2304. #ifdef USE_ISO
  2305. case 2: /* mixed iso/bulk */
  2306. OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
  2307. OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
  2308. OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
  2309. OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
  2310. OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
  2311. OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
  2312. OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
  2313. OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
  2314. break;
  2315. case 3: /* mixed bulk/iso */
  2316. OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
  2317. OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
  2318. OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
  2319. OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
  2320. OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
  2321. OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
  2322. OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
  2323. OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
  2324. OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
  2325. break;
  2326. #endif
  2327. /* add more modes as needed */
  2328. default:
  2329. ERR("unsupported fifo_mode #%d\n", fifo_mode);
  2330. return -ENODEV;
  2331. }
  2332. UDC_SYSCON1_REG = UDC_CFG_LOCK|UDC_SELF_PWR;
  2333. INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
  2334. return 0;
  2335. }
  2336. static int __init omap_udc_probe(struct platform_device *pdev)
  2337. {
  2338. int status = -ENODEV;
  2339. int hmc;
  2340. struct otg_transceiver *xceiv = NULL;
  2341. const char *type = NULL;
  2342. struct omap_usb_config *config = pdev->dev.platform_data;
  2343. /* NOTE: "knows" the order of the resources! */
  2344. if (!request_mem_region(pdev->resource[0].start,
  2345. pdev->resource[0].end - pdev->resource[0].start + 1,
  2346. driver_name)) {
  2347. DBG("request_mem_region failed\n");
  2348. return -EBUSY;
  2349. }
  2350. INFO("OMAP UDC rev %d.%d%s\n",
  2351. UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
  2352. config->otg ? ", Mini-AB" : "");
  2353. /* use the mode given to us by board init code */
  2354. if (cpu_is_omap15xx()) {
  2355. hmc = HMC_1510;
  2356. type = "(unknown)";
  2357. if (machine_is_omap_innovator()) {
  2358. /* just set up software VBUS detect, and then
  2359. * later rig it so we always report VBUS.
  2360. * FIXME without really sensing VBUS, we can't
  2361. * know when to turn PULLUP_EN on/off; and that
  2362. * means we always "need" the 48MHz clock.
  2363. */
  2364. u32 tmp = FUNC_MUX_CTRL_0_REG;
  2365. FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
  2366. tmp |= VBUS_MODE_1510;
  2367. tmp &= ~VBUS_CTRL_1510;
  2368. FUNC_MUX_CTRL_0_REG = tmp;
  2369. }
  2370. } else {
  2371. /* The transceiver may package some GPIO logic or handle
  2372. * loopback and/or transceiverless setup; if we find one,
  2373. * use it. Except for OTG, we don't _need_ to talk to one;
  2374. * but not having one probably means no VBUS detection.
  2375. */
  2376. xceiv = otg_get_transceiver();
  2377. if (xceiv)
  2378. type = xceiv->label;
  2379. else if (config->otg) {
  2380. DBG("OTG requires external transceiver!\n");
  2381. goto cleanup0;
  2382. }
  2383. hmc = HMC_1610;
  2384. switch (hmc) {
  2385. case 0: /* POWERUP DEFAULT == 0 */
  2386. case 4:
  2387. case 12:
  2388. case 20:
  2389. if (!cpu_is_omap1710()) {
  2390. type = "integrated";
  2391. break;
  2392. }
  2393. /* FALL THROUGH */
  2394. case 3:
  2395. case 11:
  2396. case 16:
  2397. case 19:
  2398. case 25:
  2399. if (!xceiv) {
  2400. DBG("external transceiver not registered!\n");
  2401. type = "unknown";
  2402. }
  2403. break;
  2404. case 21: /* internal loopback */
  2405. type = "loopback";
  2406. break;
  2407. case 14: /* transceiverless */
  2408. if (cpu_is_omap1710())
  2409. goto bad_on_1710;
  2410. /* FALL THROUGH */
  2411. case 13:
  2412. case 15:
  2413. type = "no";
  2414. break;
  2415. default:
  2416. bad_on_1710:
  2417. ERR("unrecognized UDC HMC mode %d\n", hmc);
  2418. goto cleanup0;
  2419. }
  2420. }
  2421. INFO("hmc mode %d, %s transceiver\n", hmc, type);
  2422. /* a "gadget" abstracts/virtualizes the controller */
  2423. status = omap_udc_setup(pdev, xceiv);
  2424. if (status) {
  2425. goto cleanup0;
  2426. }
  2427. xceiv = NULL;
  2428. // "udc" is now valid
  2429. pullup_disable(udc);
  2430. #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
  2431. udc->gadget.is_otg = (config->otg != 0);
  2432. #endif
  2433. /* starting with omap1710 es2.0, clear toggle is a separate bit */
  2434. if (UDC_REV_REG >= 0x61)
  2435. udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
  2436. else
  2437. udc->clr_halt = UDC_RESET_EP;
  2438. /* USB general purpose IRQ: ep0, state changes, dma, etc */
  2439. status = request_irq(pdev->resource[1].start, omap_udc_irq,
  2440. SA_SAMPLE_RANDOM, driver_name, udc);
  2441. if (status != 0) {
  2442. ERR( "can't get irq %ld, err %d\n",
  2443. pdev->resource[1].start, status);
  2444. goto cleanup1;
  2445. }
  2446. /* USB "non-iso" IRQ (PIO for all but ep0) */
  2447. status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
  2448. SA_SAMPLE_RANDOM, "omap_udc pio", udc);
  2449. if (status != 0) {
  2450. ERR( "can't get irq %ld, err %d\n",
  2451. pdev->resource[2].start, status);
  2452. goto cleanup2;
  2453. }
  2454. #ifdef USE_ISO
  2455. status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
  2456. SA_INTERRUPT, "omap_udc iso", udc);
  2457. if (status != 0) {
  2458. ERR("can't get irq %ld, err %d\n",
  2459. pdev->resource[3].start, status);
  2460. goto cleanup3;
  2461. }
  2462. #endif
  2463. create_proc_file();
  2464. device_add(&udc->gadget.dev);
  2465. return 0;
  2466. #ifdef USE_ISO
  2467. cleanup3:
  2468. free_irq(pdev->resource[2].start, udc);
  2469. #endif
  2470. cleanup2:
  2471. free_irq(pdev->resource[1].start, udc);
  2472. cleanup1:
  2473. kfree (udc);
  2474. udc = NULL;
  2475. cleanup0:
  2476. if (xceiv)
  2477. put_device(xceiv->dev);
  2478. release_mem_region(pdev->resource[0].start,
  2479. pdev->resource[0].end - pdev->resource[0].start + 1);
  2480. return status;
  2481. }
  2482. static int __exit omap_udc_remove(struct platform_device *pdev)
  2483. {
  2484. DECLARE_COMPLETION(done);
  2485. if (!udc)
  2486. return -ENODEV;
  2487. udc->done = &done;
  2488. pullup_disable(udc);
  2489. if (udc->transceiver) {
  2490. put_device(udc->transceiver->dev);
  2491. udc->transceiver = NULL;
  2492. }
  2493. UDC_SYSCON1_REG = 0;
  2494. remove_proc_file();
  2495. #ifdef USE_ISO
  2496. free_irq(pdev->resource[3].start, udc);
  2497. #endif
  2498. free_irq(pdev->resource[2].start, udc);
  2499. free_irq(pdev->resource[1].start, udc);
  2500. release_mem_region(pdev->resource[0].start,
  2501. pdev->resource[0].end - pdev->resource[0].start + 1);
  2502. device_unregister(&udc->gadget.dev);
  2503. wait_for_completion(&done);
  2504. return 0;
  2505. }
  2506. /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
  2507. * system is forced into deep sleep
  2508. *
  2509. * REVISIT we should probably reject suspend requests when there's a host
  2510. * session active, rather than disconnecting, at least on boards that can
  2511. * report VBUS irqs (UDC_DEVSTAT_REG.UDC_ATT). And in any case, we need to
  2512. * make host resumes and VBUS detection trigger OMAP wakeup events; that
  2513. * may involve talking to an external transceiver (e.g. isp1301).
  2514. */
  2515. static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
  2516. {
  2517. u32 devstat;
  2518. devstat = UDC_DEVSTAT_REG;
  2519. /* we're requesting 48 MHz clock if the pullup is enabled
  2520. * (== we're attached to the host) and we're not suspended,
  2521. * which would prevent entry to deep sleep...
  2522. */
  2523. if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
  2524. WARN("session active; suspend requires disconnect\n");
  2525. omap_pullup(&udc->gadget, 0);
  2526. }
  2527. udc->gadget.dev.power.power_state = PMSG_SUSPEND;
  2528. udc->gadget.dev.parent->power.power_state = PMSG_SUSPEND;
  2529. return 0;
  2530. }
  2531. static int omap_udc_resume(struct platform_device *dev)
  2532. {
  2533. DBG("resume + wakeup/SRP\n");
  2534. omap_pullup(&udc->gadget, 1);
  2535. /* maybe the host would enumerate us if we nudged it */
  2536. msleep(100);
  2537. return omap_wakeup(&udc->gadget);
  2538. }
  2539. /*-------------------------------------------------------------------------*/
  2540. static struct platform_driver udc_driver = {
  2541. .probe = omap_udc_probe,
  2542. .remove = __exit_p(omap_udc_remove),
  2543. .suspend = omap_udc_suspend,
  2544. .resume = omap_udc_resume,
  2545. .driver = {
  2546. .owner = THIS_MODULE,
  2547. .name = (char *) driver_name,
  2548. },
  2549. };
  2550. static int __init udc_init(void)
  2551. {
  2552. INFO("%s, version: " DRIVER_VERSION
  2553. #ifdef USE_ISO
  2554. " (iso)"
  2555. #endif
  2556. "%s\n", driver_desc,
  2557. use_dma ? " (dma)" : "");
  2558. return platform_driver_register(&udc_driver);
  2559. }
  2560. module_init(udc_init);
  2561. static void __exit udc_exit(void)
  2562. {
  2563. platform_driver_unregister(&udc_driver);
  2564. }
  2565. module_exit(udc_exit);
  2566. MODULE_DESCRIPTION(DRIVER_DESC);
  2567. MODULE_LICENSE("GPL");