omap_udc.c 75 KB

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