omap_udc.c 75 KB

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