omap_udc.c 75 KB

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