omap_udc.c 74 KB

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