omap_udc.c 80 KB

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