omap_udc.c 72 KB

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