pxa2xx_udc.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658
  1. /*
  2. * linux/drivers/usb/gadget/pxa2xx_udc.c
  3. * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
  4. *
  5. * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
  6. * Copyright (C) 2003 Robert Schwebel, Pengutronix
  7. * Copyright (C) 2003 Benedikt Spranger, Pengutronix
  8. * Copyright (C) 2003 David Brownell
  9. * Copyright (C) 2003 Joshua Wise
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #undef DEBUG
  27. // #define VERBOSE DBG_VERBOSE
  28. #include <linux/config.h>
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/ioport.h>
  32. #include <linux/types.h>
  33. #include <linux/errno.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/slab.h>
  37. #include <linux/init.h>
  38. #include <linux/timer.h>
  39. #include <linux/list.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/mm.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/dma-mapping.h>
  45. #include <asm/byteorder.h>
  46. #include <asm/dma.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #include <asm/system.h>
  50. #include <asm/mach-types.h>
  51. #include <asm/unaligned.h>
  52. #include <asm/hardware.h>
  53. #include <asm/arch/pxa-regs.h>
  54. #include <linux/usb_ch9.h>
  55. #include <linux/usb_gadget.h>
  56. #include <asm/arch/udc.h>
  57. /*
  58. * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
  59. * series processors. The UDC for the IXP 4xx series is very similar.
  60. * There are fifteen endpoints, in addition to ep0.
  61. *
  62. * Such controller drivers work with a gadget driver. The gadget driver
  63. * returns descriptors, implements configuration and data protocols used
  64. * by the host to interact with this device, and allocates endpoints to
  65. * the different protocol interfaces. The controller driver virtualizes
  66. * usb hardware so that the gadget drivers will be more portable.
  67. *
  68. * This UDC hardware wants to implement a bit too much USB protocol, so
  69. * it constrains the sorts of USB configuration change events that work.
  70. * The errata for these chips are misleading; some "fixed" bugs from
  71. * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
  72. */
  73. #define DRIVER_VERSION "4-May-2005"
  74. #define DRIVER_DESC "PXA 25x USB Device Controller driver"
  75. static const char driver_name [] = "pxa2xx_udc";
  76. static const char ep0name [] = "ep0";
  77. // #define USE_DMA
  78. // #define USE_OUT_DMA
  79. // #define DISABLE_TEST_MODE
  80. #ifdef CONFIG_ARCH_IXP4XX
  81. #undef USE_DMA
  82. /* cpu-specific register addresses are compiled in to this code */
  83. #ifdef CONFIG_ARCH_PXA
  84. #error "Can't configure both IXP and PXA"
  85. #endif
  86. #endif
  87. #include "pxa2xx_udc.h"
  88. #ifdef USE_DMA
  89. static int use_dma = 1;
  90. module_param(use_dma, bool, 0);
  91. MODULE_PARM_DESC (use_dma, "true to use dma");
  92. static void dma_nodesc_handler (int dmach, void *_ep, struct pt_regs *r);
  93. static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req);
  94. #ifdef USE_OUT_DMA
  95. #define DMASTR " (dma support)"
  96. #else
  97. #define DMASTR " (dma in)"
  98. #endif
  99. #else /* !USE_DMA */
  100. #define DMASTR " (pio only)"
  101. #undef USE_OUT_DMA
  102. #endif
  103. #ifdef CONFIG_USB_PXA2XX_SMALL
  104. #define SIZE_STR " (small)"
  105. #else
  106. #define SIZE_STR ""
  107. #endif
  108. #ifdef DISABLE_TEST_MODE
  109. /* (mode == 0) == no undocumented chip tweaks
  110. * (mode & 1) == double buffer bulk IN
  111. * (mode & 2) == double buffer bulk OUT
  112. * ... so mode = 3 (or 7, 15, etc) does it for both
  113. */
  114. static ushort fifo_mode = 0;
  115. module_param(fifo_mode, ushort, 0);
  116. MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
  117. #endif
  118. /* ---------------------------------------------------------------------------
  119. * endpoint related parts of the api to the usb controller hardware,
  120. * used by gadget driver; and the inner talker-to-hardware core.
  121. * ---------------------------------------------------------------------------
  122. */
  123. static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
  124. static void nuke (struct pxa2xx_ep *, int status);
  125. static void pio_irq_enable(int bEndpointAddress)
  126. {
  127. bEndpointAddress &= 0xf;
  128. if (bEndpointAddress < 8)
  129. UICR0 &= ~(1 << bEndpointAddress);
  130. else {
  131. bEndpointAddress -= 8;
  132. UICR1 &= ~(1 << bEndpointAddress);
  133. }
  134. }
  135. static void pio_irq_disable(int bEndpointAddress)
  136. {
  137. bEndpointAddress &= 0xf;
  138. if (bEndpointAddress < 8)
  139. UICR0 |= 1 << bEndpointAddress;
  140. else {
  141. bEndpointAddress -= 8;
  142. UICR1 |= 1 << bEndpointAddress;
  143. }
  144. }
  145. /* The UDCCR reg contains mask and interrupt status bits,
  146. * so using '|=' isn't safe as it may ack an interrupt.
  147. */
  148. #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
  149. static inline void udc_set_mask_UDCCR(int mask)
  150. {
  151. UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
  152. }
  153. static inline void udc_clear_mask_UDCCR(int mask)
  154. {
  155. UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
  156. }
  157. static inline void udc_ack_int_UDCCR(int mask)
  158. {
  159. /* udccr contains the bits we dont want to change */
  160. __u32 udccr = UDCCR & UDCCR_MASK_BITS;
  161. UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
  162. }
  163. /*
  164. * endpoint enable/disable
  165. *
  166. * we need to verify the descriptors used to enable endpoints. since pxa2xx
  167. * endpoint configurations are fixed, and are pretty much always enabled,
  168. * there's not a lot to manage here.
  169. *
  170. * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
  171. * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
  172. * for a single interface (with only the default altsetting) and for gadget
  173. * drivers that don't halt endpoints (not reset by set_interface). that also
  174. * means that if you use ISO, you must violate the USB spec rule that all
  175. * iso endpoints must be in non-default altsettings.
  176. */
  177. static int pxa2xx_ep_enable (struct usb_ep *_ep,
  178. const struct usb_endpoint_descriptor *desc)
  179. {
  180. struct pxa2xx_ep *ep;
  181. struct pxa2xx_udc *dev;
  182. ep = container_of (_ep, struct pxa2xx_ep, ep);
  183. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  184. || desc->bDescriptorType != USB_DT_ENDPOINT
  185. || ep->bEndpointAddress != desc->bEndpointAddress
  186. || ep->fifo_size < le16_to_cpu
  187. (desc->wMaxPacketSize)) {
  188. DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
  189. return -EINVAL;
  190. }
  191. /* xfer types must match, except that interrupt ~= bulk */
  192. if (ep->bmAttributes != desc->bmAttributes
  193. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  194. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  195. DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
  196. return -EINVAL;
  197. }
  198. /* hardware _could_ do smaller, but driver doesn't */
  199. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  200. && le16_to_cpu (desc->wMaxPacketSize)
  201. != BULK_FIFO_SIZE)
  202. || !desc->wMaxPacketSize) {
  203. DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
  204. return -ERANGE;
  205. }
  206. dev = ep->dev;
  207. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  208. DMSG("%s, bogus device state\n", __FUNCTION__);
  209. return -ESHUTDOWN;
  210. }
  211. ep->desc = desc;
  212. ep->dma = -1;
  213. ep->stopped = 0;
  214. ep->pio_irqs = ep->dma_irqs = 0;
  215. ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
  216. /* flush fifo (mostly for OUT buffers) */
  217. pxa2xx_ep_fifo_flush (_ep);
  218. /* ... reset halt state too, if we could ... */
  219. #ifdef USE_DMA
  220. /* for (some) bulk and ISO endpoints, try to get a DMA channel and
  221. * bind it to the endpoint. otherwise use PIO.
  222. */
  223. switch (ep->bmAttributes) {
  224. case USB_ENDPOINT_XFER_ISOC:
  225. if (le16_to_cpu(desc->wMaxPacketSize) % 32)
  226. break;
  227. // fall through
  228. case USB_ENDPOINT_XFER_BULK:
  229. if (!use_dma || !ep->reg_drcmr)
  230. break;
  231. ep->dma = pxa_request_dma ((char *)_ep->name,
  232. (le16_to_cpu (desc->wMaxPacketSize) > 64)
  233. ? DMA_PRIO_MEDIUM /* some iso */
  234. : DMA_PRIO_LOW,
  235. dma_nodesc_handler, ep);
  236. if (ep->dma >= 0) {
  237. *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
  238. DMSG("%s using dma%d\n", _ep->name, ep->dma);
  239. }
  240. }
  241. #endif
  242. DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
  243. return 0;
  244. }
  245. static int pxa2xx_ep_disable (struct usb_ep *_ep)
  246. {
  247. struct pxa2xx_ep *ep;
  248. unsigned long flags;
  249. ep = container_of (_ep, struct pxa2xx_ep, ep);
  250. if (!_ep || !ep->desc) {
  251. DMSG("%s, %s not enabled\n", __FUNCTION__,
  252. _ep ? ep->ep.name : NULL);
  253. return -EINVAL;
  254. }
  255. local_irq_save(flags);
  256. nuke (ep, -ESHUTDOWN);
  257. #ifdef USE_DMA
  258. if (ep->dma >= 0) {
  259. *ep->reg_drcmr = 0;
  260. pxa_free_dma (ep->dma);
  261. ep->dma = -1;
  262. }
  263. #endif
  264. /* flush fifo (mostly for IN buffers) */
  265. pxa2xx_ep_fifo_flush (_ep);
  266. ep->desc = NULL;
  267. ep->stopped = 1;
  268. local_irq_restore(flags);
  269. DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
  270. return 0;
  271. }
  272. /*-------------------------------------------------------------------------*/
  273. /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
  274. * must still pass correctly initialized endpoints, since other controller
  275. * drivers may care about how it's currently set up (dma issues etc).
  276. */
  277. /*
  278. * pxa2xx_ep_alloc_request - allocate a request data structure
  279. */
  280. static struct usb_request *
  281. pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  282. {
  283. struct pxa2xx_request *req;
  284. req = kzalloc(sizeof(*req), gfp_flags);
  285. if (!req)
  286. return NULL;
  287. INIT_LIST_HEAD (&req->queue);
  288. return &req->req;
  289. }
  290. /*
  291. * pxa2xx_ep_free_request - deallocate a request data structure
  292. */
  293. static void
  294. pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
  295. {
  296. struct pxa2xx_request *req;
  297. req = container_of (_req, struct pxa2xx_request, req);
  298. WARN_ON (!list_empty (&req->queue));
  299. kfree(req);
  300. }
  301. /* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
  302. * no device-affinity and the heap works perfectly well for i/o buffers.
  303. * It wastes much less memory than dma_alloc_coherent() would, and even
  304. * prevents cacheline (32 bytes wide) sharing problems.
  305. */
  306. static void *
  307. pxa2xx_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
  308. dma_addr_t *dma, gfp_t gfp_flags)
  309. {
  310. char *retval;
  311. retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
  312. if (retval)
  313. #ifdef USE_DMA
  314. *dma = virt_to_bus (retval);
  315. #else
  316. *dma = (dma_addr_t)~0;
  317. #endif
  318. return retval;
  319. }
  320. static void
  321. pxa2xx_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
  322. unsigned bytes)
  323. {
  324. kfree (buf);
  325. }
  326. /*-------------------------------------------------------------------------*/
  327. /*
  328. * done - retire a request; caller blocked irqs
  329. */
  330. static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
  331. {
  332. unsigned stopped = ep->stopped;
  333. list_del_init(&req->queue);
  334. if (likely (req->req.status == -EINPROGRESS))
  335. req->req.status = status;
  336. else
  337. status = req->req.status;
  338. if (status && status != -ESHUTDOWN)
  339. DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
  340. ep->ep.name, &req->req, status,
  341. req->req.actual, req->req.length);
  342. /* don't modify queue heads during completion callback */
  343. ep->stopped = 1;
  344. req->req.complete(&ep->ep, &req->req);
  345. ep->stopped = stopped;
  346. }
  347. static inline void ep0_idle (struct pxa2xx_udc *dev)
  348. {
  349. dev->ep0state = EP0_IDLE;
  350. }
  351. static int
  352. write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
  353. {
  354. u8 *buf;
  355. unsigned length, count;
  356. buf = req->req.buf + req->req.actual;
  357. prefetch(buf);
  358. /* how big will this packet be? */
  359. length = min(req->req.length - req->req.actual, max);
  360. req->req.actual += length;
  361. count = length;
  362. while (likely(count--))
  363. *uddr = *buf++;
  364. return length;
  365. }
  366. /*
  367. * write to an IN endpoint fifo, as many packets as possible.
  368. * irqs will use this to write the rest later.
  369. * caller guarantees at least one packet buffer is ready (or a zlp).
  370. */
  371. static int
  372. write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
  373. {
  374. unsigned max;
  375. max = le16_to_cpu(ep->desc->wMaxPacketSize);
  376. do {
  377. unsigned count;
  378. int is_last, is_short;
  379. count = write_packet(ep->reg_uddr, req, max);
  380. /* last packet is usually short (or a zlp) */
  381. if (unlikely (count != max))
  382. is_last = is_short = 1;
  383. else {
  384. if (likely(req->req.length != req->req.actual)
  385. || req->req.zero)
  386. is_last = 0;
  387. else
  388. is_last = 1;
  389. /* interrupt/iso maxpacket may not fill the fifo */
  390. is_short = unlikely (max < ep->fifo_size);
  391. }
  392. DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
  393. ep->ep.name, count,
  394. is_last ? "/L" : "", is_short ? "/S" : "",
  395. req->req.length - req->req.actual, req);
  396. /* let loose that packet. maybe try writing another one,
  397. * double buffering might work. TSP, TPC, and TFS
  398. * bit values are the same for all normal IN endpoints.
  399. */
  400. *ep->reg_udccs = UDCCS_BI_TPC;
  401. if (is_short)
  402. *ep->reg_udccs = UDCCS_BI_TSP;
  403. /* requests complete when all IN data is in the FIFO */
  404. if (is_last) {
  405. done (ep, req, 0);
  406. if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
  407. pio_irq_disable (ep->bEndpointAddress);
  408. #ifdef USE_DMA
  409. /* unaligned data and zlps couldn't use dma */
  410. if (unlikely(!list_empty(&ep->queue))) {
  411. req = list_entry(ep->queue.next,
  412. struct pxa2xx_request, queue);
  413. kick_dma(ep,req);
  414. return 0;
  415. }
  416. #endif
  417. }
  418. return 1;
  419. }
  420. // TODO experiment: how robust can fifo mode tweaking be?
  421. // double buffering is off in the default fifo mode, which
  422. // prevents TFS from being set here.
  423. } while (*ep->reg_udccs & UDCCS_BI_TFS);
  424. return 0;
  425. }
  426. /* caller asserts req->pending (ep0 irq status nyet cleared); starts
  427. * ep0 data stage. these chips want very simple state transitions.
  428. */
  429. static inline
  430. void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
  431. {
  432. UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
  433. USIR0 = USIR0_IR0;
  434. dev->req_pending = 0;
  435. DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
  436. __FUNCTION__, tag, UDCCS0, flags);
  437. }
  438. static int
  439. write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
  440. {
  441. unsigned count;
  442. int is_short;
  443. count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
  444. ep->dev->stats.write.bytes += count;
  445. /* last packet "must be" short (or a zlp) */
  446. is_short = (count != EP0_FIFO_SIZE);
  447. DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
  448. req->req.length - req->req.actual, req);
  449. if (unlikely (is_short)) {
  450. if (ep->dev->req_pending)
  451. ep0start(ep->dev, UDCCS0_IPR, "short IN");
  452. else
  453. UDCCS0 = UDCCS0_IPR;
  454. count = req->req.length;
  455. done (ep, req, 0);
  456. ep0_idle(ep->dev);
  457. #if 1
  458. /* This seems to get rid of lost status irqs in some cases:
  459. * host responds quickly, or next request involves config
  460. * change automagic, or should have been hidden, or ...
  461. *
  462. * FIXME get rid of all udelays possible...
  463. */
  464. if (count >= EP0_FIFO_SIZE) {
  465. count = 100;
  466. do {
  467. if ((UDCCS0 & UDCCS0_OPR) != 0) {
  468. /* clear OPR, generate ack */
  469. UDCCS0 = UDCCS0_OPR;
  470. break;
  471. }
  472. count--;
  473. udelay(1);
  474. } while (count);
  475. }
  476. #endif
  477. } else if (ep->dev->req_pending)
  478. ep0start(ep->dev, 0, "IN");
  479. return is_short;
  480. }
  481. /*
  482. * read_fifo - unload packet(s) from the fifo we use for usb OUT
  483. * transfers and put them into the request. caller should have made
  484. * sure there's at least one packet ready.
  485. *
  486. * returns true if the request completed because of short packet or the
  487. * request buffer having filled (and maybe overran till end-of-packet).
  488. */
  489. static int
  490. read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
  491. {
  492. for (;;) {
  493. u32 udccs;
  494. u8 *buf;
  495. unsigned bufferspace, count, is_short;
  496. /* make sure there's a packet in the FIFO.
  497. * UDCCS_{BO,IO}_RPC are all the same bit value.
  498. * UDCCS_{BO,IO}_RNE are all the same bit value.
  499. */
  500. udccs = *ep->reg_udccs;
  501. if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
  502. break;
  503. buf = req->req.buf + req->req.actual;
  504. prefetchw(buf);
  505. bufferspace = req->req.length - req->req.actual;
  506. /* read all bytes from this packet */
  507. if (likely (udccs & UDCCS_BO_RNE)) {
  508. count = 1 + (0x0ff & *ep->reg_ubcr);
  509. req->req.actual += min (count, bufferspace);
  510. } else /* zlp */
  511. count = 0;
  512. is_short = (count < ep->ep.maxpacket);
  513. DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
  514. ep->ep.name, udccs, count,
  515. is_short ? "/S" : "",
  516. req, req->req.actual, req->req.length);
  517. while (likely (count-- != 0)) {
  518. u8 byte = (u8) *ep->reg_uddr;
  519. if (unlikely (bufferspace == 0)) {
  520. /* this happens when the driver's buffer
  521. * is smaller than what the host sent.
  522. * discard the extra data.
  523. */
  524. if (req->req.status != -EOVERFLOW)
  525. DMSG("%s overflow %d\n",
  526. ep->ep.name, count);
  527. req->req.status = -EOVERFLOW;
  528. } else {
  529. *buf++ = byte;
  530. bufferspace--;
  531. }
  532. }
  533. *ep->reg_udccs = UDCCS_BO_RPC;
  534. /* RPC/RSP/RNE could now reflect the other packet buffer */
  535. /* iso is one request per packet */
  536. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  537. if (udccs & UDCCS_IO_ROF)
  538. req->req.status = -EHOSTUNREACH;
  539. /* more like "is_done" */
  540. is_short = 1;
  541. }
  542. /* completion */
  543. if (is_short || req->req.actual == req->req.length) {
  544. done (ep, req, 0);
  545. if (list_empty(&ep->queue))
  546. pio_irq_disable (ep->bEndpointAddress);
  547. return 1;
  548. }
  549. /* finished that packet. the next one may be waiting... */
  550. }
  551. return 0;
  552. }
  553. /*
  554. * special ep0 version of the above. no UBCR0 or double buffering; status
  555. * handshaking is magic. most device protocols don't need control-OUT.
  556. * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
  557. * protocols do use them.
  558. */
  559. static int
  560. read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
  561. {
  562. u8 *buf, byte;
  563. unsigned bufferspace;
  564. buf = req->req.buf + req->req.actual;
  565. bufferspace = req->req.length - req->req.actual;
  566. while (UDCCS0 & UDCCS0_RNE) {
  567. byte = (u8) UDDR0;
  568. if (unlikely (bufferspace == 0)) {
  569. /* this happens when the driver's buffer
  570. * is smaller than what the host sent.
  571. * discard the extra data.
  572. */
  573. if (req->req.status != -EOVERFLOW)
  574. DMSG("%s overflow\n", ep->ep.name);
  575. req->req.status = -EOVERFLOW;
  576. } else {
  577. *buf++ = byte;
  578. req->req.actual++;
  579. bufferspace--;
  580. }
  581. }
  582. UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
  583. /* completion */
  584. if (req->req.actual >= req->req.length)
  585. return 1;
  586. /* finished that packet. the next one may be waiting... */
  587. return 0;
  588. }
  589. #ifdef USE_DMA
  590. #define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
  591. static void
  592. start_dma_nodesc(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int is_in)
  593. {
  594. u32 dcmd = req->req.length;
  595. u32 buf = req->req.dma;
  596. u32 fifo = io_v2p ((u32)ep->reg_uddr);
  597. /* caller guarantees there's a packet or more remaining
  598. * - IN may end with a short packet (TSP set separately),
  599. * - OUT is always full length
  600. */
  601. buf += req->req.actual;
  602. dcmd -= req->req.actual;
  603. ep->dma_fixup = 0;
  604. /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
  605. DCSR(ep->dma) = DCSR_NODESC;
  606. if (is_in) {
  607. DSADR(ep->dma) = buf;
  608. DTADR(ep->dma) = fifo;
  609. if (dcmd > MAX_IN_DMA)
  610. dcmd = MAX_IN_DMA;
  611. else
  612. ep->dma_fixup = (dcmd % ep->ep.maxpacket) != 0;
  613. dcmd |= DCMD_BURST32 | DCMD_WIDTH1
  614. | DCMD_FLOWTRG | DCMD_INCSRCADDR;
  615. } else {
  616. #ifdef USE_OUT_DMA
  617. DSADR(ep->dma) = fifo;
  618. DTADR(ep->dma) = buf;
  619. if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
  620. dcmd = ep->ep.maxpacket;
  621. dcmd |= DCMD_BURST32 | DCMD_WIDTH1
  622. | DCMD_FLOWSRC | DCMD_INCTRGADDR;
  623. #endif
  624. }
  625. DCMD(ep->dma) = dcmd;
  626. DCSR(ep->dma) = DCSR_RUN | DCSR_NODESC
  627. | (unlikely(is_in)
  628. ? DCSR_STOPIRQEN /* use dma_nodesc_handler() */
  629. : 0); /* use handle_ep() */
  630. }
  631. static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req)
  632. {
  633. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  634. if (is_in) {
  635. /* unaligned tx buffers and zlps only work with PIO */
  636. if ((req->req.dma & 0x0f) != 0
  637. || unlikely((req->req.length - req->req.actual)
  638. == 0)) {
  639. pio_irq_enable(ep->bEndpointAddress);
  640. if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0)
  641. (void) write_fifo(ep, req);
  642. } else {
  643. start_dma_nodesc(ep, req, USB_DIR_IN);
  644. }
  645. } else {
  646. if ((req->req.length - req->req.actual) < ep->ep.maxpacket) {
  647. DMSG("%s short dma read...\n", ep->ep.name);
  648. /* we're always set up for pio out */
  649. read_fifo (ep, req);
  650. } else {
  651. *ep->reg_udccs = UDCCS_BO_DME
  652. | (*ep->reg_udccs & UDCCS_BO_FST);
  653. start_dma_nodesc(ep, req, USB_DIR_OUT);
  654. }
  655. }
  656. }
  657. static void cancel_dma(struct pxa2xx_ep *ep)
  658. {
  659. struct pxa2xx_request *req;
  660. u32 tmp;
  661. if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
  662. return;
  663. DCSR(ep->dma) = 0;
  664. while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
  665. cpu_relax();
  666. req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
  667. tmp = DCMD(ep->dma) & DCMD_LENGTH;
  668. req->req.actual = req->req.length - (tmp & DCMD_LENGTH);
  669. /* the last tx packet may be incomplete, so flush the fifo.
  670. * FIXME correct req.actual if we can
  671. */
  672. if (ep->bEndpointAddress & USB_DIR_IN)
  673. *ep->reg_udccs = UDCCS_BI_FTF;
  674. }
  675. /* dma channel stopped ... normal tx end (IN), or on error (IN/OUT) */
  676. static void dma_nodesc_handler(int dmach, void *_ep, struct pt_regs *r)
  677. {
  678. struct pxa2xx_ep *ep = _ep;
  679. struct pxa2xx_request *req;
  680. u32 tmp, completed;
  681. local_irq_disable();
  682. req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
  683. ep->dma_irqs++;
  684. ep->dev->stats.irqs++;
  685. HEX_DISPLAY(ep->dev->stats.irqs);
  686. /* ack/clear */
  687. tmp = DCSR(ep->dma);
  688. DCSR(ep->dma) = tmp;
  689. if ((tmp & DCSR_STOPSTATE) == 0
  690. || (DDADR(ep->dma) & DDADR_STOP) != 0) {
  691. DBG(DBG_VERBOSE, "%s, dcsr %08x ddadr %08x\n",
  692. ep->ep.name, DCSR(ep->dma), DDADR(ep->dma));
  693. goto done;
  694. }
  695. DCSR(ep->dma) = 0; /* clear DCSR_STOPSTATE */
  696. /* update transfer status */
  697. completed = tmp & DCSR_BUSERR;
  698. if (ep->bEndpointAddress & USB_DIR_IN)
  699. tmp = DSADR(ep->dma);
  700. else
  701. tmp = DTADR(ep->dma);
  702. req->req.actual = tmp - req->req.dma;
  703. /* FIXME seems we sometimes see partial transfers... */
  704. if (unlikely(completed != 0))
  705. req->req.status = -EIO;
  706. else if (req->req.actual) {
  707. /* these registers have zeroes in low bits; they miscount
  708. * some (end-of-transfer) short packets: tx 14 as tx 12
  709. */
  710. if (ep->dma_fixup)
  711. req->req.actual = min(req->req.actual + 3,
  712. req->req.length);
  713. tmp = (req->req.length - req->req.actual);
  714. completed = (tmp == 0);
  715. if (completed && (ep->bEndpointAddress & USB_DIR_IN)) {
  716. /* maybe validate final short packet ... */
  717. if ((req->req.actual % ep->ep.maxpacket) != 0)
  718. *ep->reg_udccs = UDCCS_BI_TSP/*|UDCCS_BI_TPC*/;
  719. /* ... or zlp, using pio fallback */
  720. else if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK
  721. && req->req.zero) {
  722. DMSG("%s zlp terminate ...\n", ep->ep.name);
  723. completed = 0;
  724. }
  725. }
  726. }
  727. if (likely(completed)) {
  728. done(ep, req, 0);
  729. /* maybe re-activate after completion */
  730. if (ep->stopped || list_empty(&ep->queue))
  731. goto done;
  732. req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
  733. }
  734. kick_dma(ep, req);
  735. done:
  736. local_irq_enable();
  737. }
  738. #endif
  739. /*-------------------------------------------------------------------------*/
  740. static int
  741. pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  742. {
  743. struct pxa2xx_request *req;
  744. struct pxa2xx_ep *ep;
  745. struct pxa2xx_udc *dev;
  746. unsigned long flags;
  747. req = container_of(_req, struct pxa2xx_request, req);
  748. if (unlikely (!_req || !_req->complete || !_req->buf
  749. || !list_empty(&req->queue))) {
  750. DMSG("%s, bad params\n", __FUNCTION__);
  751. return -EINVAL;
  752. }
  753. ep = container_of(_ep, struct pxa2xx_ep, ep);
  754. if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  755. DMSG("%s, bad ep\n", __FUNCTION__);
  756. return -EINVAL;
  757. }
  758. dev = ep->dev;
  759. if (unlikely (!dev->driver
  760. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  761. DMSG("%s, bogus device state\n", __FUNCTION__);
  762. return -ESHUTDOWN;
  763. }
  764. /* iso is always one packet per request, that's the only way
  765. * we can report per-packet status. that also helps with dma.
  766. */
  767. if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  768. && req->req.length > le16_to_cpu
  769. (ep->desc->wMaxPacketSize)))
  770. return -EMSGSIZE;
  771. #ifdef USE_DMA
  772. // FIXME caller may already have done the dma mapping
  773. if (ep->dma >= 0) {
  774. _req->dma = dma_map_single(dev->dev,
  775. _req->buf, _req->length,
  776. ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  777. ? DMA_TO_DEVICE
  778. : DMA_FROM_DEVICE);
  779. }
  780. #endif
  781. DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
  782. _ep->name, _req, _req->length, _req->buf);
  783. local_irq_save(flags);
  784. _req->status = -EINPROGRESS;
  785. _req->actual = 0;
  786. /* kickstart this i/o queue? */
  787. if (list_empty(&ep->queue) && !ep->stopped) {
  788. if (ep->desc == 0 /* ep0 */) {
  789. unsigned length = _req->length;
  790. switch (dev->ep0state) {
  791. case EP0_IN_DATA_PHASE:
  792. dev->stats.write.ops++;
  793. if (write_ep0_fifo(ep, req))
  794. req = NULL;
  795. break;
  796. case EP0_OUT_DATA_PHASE:
  797. dev->stats.read.ops++;
  798. /* messy ... */
  799. if (dev->req_config) {
  800. DBG(DBG_VERBOSE, "ep0 config ack%s\n",
  801. dev->has_cfr ? "" : " raced");
  802. if (dev->has_cfr)
  803. UDCCFR = UDCCFR_AREN|UDCCFR_ACM
  804. |UDCCFR_MB1;
  805. done(ep, req, 0);
  806. dev->ep0state = EP0_END_XFER;
  807. local_irq_restore (flags);
  808. return 0;
  809. }
  810. if (dev->req_pending)
  811. ep0start(dev, UDCCS0_IPR, "OUT");
  812. if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
  813. && read_ep0_fifo(ep, req))) {
  814. ep0_idle(dev);
  815. done(ep, req, 0);
  816. req = NULL;
  817. }
  818. break;
  819. default:
  820. DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
  821. local_irq_restore (flags);
  822. return -EL2HLT;
  823. }
  824. #ifdef USE_DMA
  825. /* either start dma or prime pio pump */
  826. } else if (ep->dma >= 0) {
  827. kick_dma(ep, req);
  828. #endif
  829. /* can the FIFO can satisfy the request immediately? */
  830. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  831. if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
  832. && write_fifo(ep, req))
  833. req = NULL;
  834. } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
  835. && read_fifo(ep, req)) {
  836. req = NULL;
  837. }
  838. if (likely (req && ep->desc) && ep->dma < 0)
  839. pio_irq_enable(ep->bEndpointAddress);
  840. }
  841. /* pio or dma irq handler advances the queue. */
  842. if (likely (req != 0))
  843. list_add_tail(&req->queue, &ep->queue);
  844. local_irq_restore(flags);
  845. return 0;
  846. }
  847. /*
  848. * nuke - dequeue ALL requests
  849. */
  850. static void nuke(struct pxa2xx_ep *ep, int status)
  851. {
  852. struct pxa2xx_request *req;
  853. /* called with irqs blocked */
  854. #ifdef USE_DMA
  855. if (ep->dma >= 0 && !ep->stopped)
  856. cancel_dma(ep);
  857. #endif
  858. while (!list_empty(&ep->queue)) {
  859. req = list_entry(ep->queue.next,
  860. struct pxa2xx_request,
  861. queue);
  862. done(ep, req, status);
  863. }
  864. if (ep->desc)
  865. pio_irq_disable (ep->bEndpointAddress);
  866. }
  867. /* dequeue JUST ONE request */
  868. static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  869. {
  870. struct pxa2xx_ep *ep;
  871. struct pxa2xx_request *req;
  872. unsigned long flags;
  873. ep = container_of(_ep, struct pxa2xx_ep, ep);
  874. if (!_ep || ep->ep.name == ep0name)
  875. return -EINVAL;
  876. local_irq_save(flags);
  877. /* make sure it's actually queued on this endpoint */
  878. list_for_each_entry (req, &ep->queue, queue) {
  879. if (&req->req == _req)
  880. break;
  881. }
  882. if (&req->req != _req) {
  883. local_irq_restore(flags);
  884. return -EINVAL;
  885. }
  886. #ifdef USE_DMA
  887. if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
  888. cancel_dma(ep);
  889. done(ep, req, -ECONNRESET);
  890. /* restart i/o */
  891. if (!list_empty(&ep->queue)) {
  892. req = list_entry(ep->queue.next,
  893. struct pxa2xx_request, queue);
  894. kick_dma(ep, req);
  895. }
  896. } else
  897. #endif
  898. done(ep, req, -ECONNRESET);
  899. local_irq_restore(flags);
  900. return 0;
  901. }
  902. /*-------------------------------------------------------------------------*/
  903. static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
  904. {
  905. struct pxa2xx_ep *ep;
  906. unsigned long flags;
  907. ep = container_of(_ep, struct pxa2xx_ep, ep);
  908. if (unlikely (!_ep
  909. || (!ep->desc && ep->ep.name != ep0name))
  910. || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  911. DMSG("%s, bad ep\n", __FUNCTION__);
  912. return -EINVAL;
  913. }
  914. if (value == 0) {
  915. /* this path (reset toggle+halt) is needed to implement
  916. * SET_INTERFACE on normal hardware. but it can't be
  917. * done from software on the PXA UDC, and the hardware
  918. * forgets to do it as part of SET_INTERFACE automagic.
  919. */
  920. DMSG("only host can clear %s halt\n", _ep->name);
  921. return -EROFS;
  922. }
  923. local_irq_save(flags);
  924. if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  925. && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
  926. || !list_empty(&ep->queue))) {
  927. local_irq_restore(flags);
  928. return -EAGAIN;
  929. }
  930. /* FST bit is the same for control, bulk in, bulk out, interrupt in */
  931. *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
  932. /* ep0 needs special care */
  933. if (!ep->desc) {
  934. start_watchdog(ep->dev);
  935. ep->dev->req_pending = 0;
  936. ep->dev->ep0state = EP0_STALL;
  937. /* and bulk/intr endpoints like dropping stalls too */
  938. } else {
  939. unsigned i;
  940. for (i = 0; i < 1000; i += 20) {
  941. if (*ep->reg_udccs & UDCCS_BI_SST)
  942. break;
  943. udelay(20);
  944. }
  945. }
  946. local_irq_restore(flags);
  947. DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
  948. return 0;
  949. }
  950. static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
  951. {
  952. struct pxa2xx_ep *ep;
  953. ep = container_of(_ep, struct pxa2xx_ep, ep);
  954. if (!_ep) {
  955. DMSG("%s, bad ep\n", __FUNCTION__);
  956. return -ENODEV;
  957. }
  958. /* pxa can't report unclaimed bytes from IN fifos */
  959. if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  960. return -EOPNOTSUPP;
  961. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
  962. || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
  963. return 0;
  964. else
  965. return (*ep->reg_ubcr & 0xfff) + 1;
  966. }
  967. static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
  968. {
  969. struct pxa2xx_ep *ep;
  970. ep = container_of(_ep, struct pxa2xx_ep, ep);
  971. if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
  972. DMSG("%s, bad ep\n", __FUNCTION__);
  973. return;
  974. }
  975. /* toggle and halt bits stay unchanged */
  976. /* for OUT, just read and discard the FIFO contents. */
  977. if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
  978. while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
  979. (void) *ep->reg_uddr;
  980. return;
  981. }
  982. /* most IN status is the same, but ISO can't stall */
  983. *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
  984. | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  985. ? 0 : UDCCS_BI_SST;
  986. }
  987. static struct usb_ep_ops pxa2xx_ep_ops = {
  988. .enable = pxa2xx_ep_enable,
  989. .disable = pxa2xx_ep_disable,
  990. .alloc_request = pxa2xx_ep_alloc_request,
  991. .free_request = pxa2xx_ep_free_request,
  992. .alloc_buffer = pxa2xx_ep_alloc_buffer,
  993. .free_buffer = pxa2xx_ep_free_buffer,
  994. .queue = pxa2xx_ep_queue,
  995. .dequeue = pxa2xx_ep_dequeue,
  996. .set_halt = pxa2xx_ep_set_halt,
  997. .fifo_status = pxa2xx_ep_fifo_status,
  998. .fifo_flush = pxa2xx_ep_fifo_flush,
  999. };
  1000. /* ---------------------------------------------------------------------------
  1001. * device-scoped parts of the api to the usb controller hardware
  1002. * ---------------------------------------------------------------------------
  1003. */
  1004. static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
  1005. {
  1006. return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
  1007. }
  1008. static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
  1009. {
  1010. /* host may not have enabled remote wakeup */
  1011. if ((UDCCS0 & UDCCS0_DRWF) == 0)
  1012. return -EHOSTUNREACH;
  1013. udc_set_mask_UDCCR(UDCCR_RSM);
  1014. return 0;
  1015. }
  1016. static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
  1017. static void udc_enable (struct pxa2xx_udc *);
  1018. static void udc_disable(struct pxa2xx_udc *);
  1019. /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
  1020. * in active use.
  1021. */
  1022. static int pullup(struct pxa2xx_udc *udc, int is_active)
  1023. {
  1024. is_active = is_active && udc->vbus && udc->pullup;
  1025. DMSG("%s\n", is_active ? "active" : "inactive");
  1026. if (is_active)
  1027. udc_enable(udc);
  1028. else {
  1029. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  1030. DMSG("disconnect %s\n", udc->driver
  1031. ? udc->driver->driver.name
  1032. : "(no driver)");
  1033. stop_activity(udc, udc->driver);
  1034. }
  1035. udc_disable(udc);
  1036. }
  1037. return 0;
  1038. }
  1039. /* VBUS reporting logically comes from a transceiver */
  1040. static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  1041. {
  1042. struct pxa2xx_udc *udc;
  1043. udc = container_of(_gadget, struct pxa2xx_udc, gadget);
  1044. udc->vbus = is_active = (is_active != 0);
  1045. DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
  1046. pullup(udc, is_active);
  1047. return 0;
  1048. }
  1049. /* drivers may have software control over D+ pullup */
  1050. static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1051. {
  1052. struct pxa2xx_udc *udc;
  1053. udc = container_of(_gadget, struct pxa2xx_udc, gadget);
  1054. /* not all boards support pullup control */
  1055. if (!udc->mach->udc_command)
  1056. return -EOPNOTSUPP;
  1057. is_active = (is_active != 0);
  1058. udc->pullup = is_active;
  1059. pullup(udc, is_active);
  1060. return 0;
  1061. }
  1062. static const struct usb_gadget_ops pxa2xx_udc_ops = {
  1063. .get_frame = pxa2xx_udc_get_frame,
  1064. .wakeup = pxa2xx_udc_wakeup,
  1065. .vbus_session = pxa2xx_udc_vbus_session,
  1066. .pullup = pxa2xx_udc_pullup,
  1067. // .vbus_draw ... boards may consume current from VBUS, up to
  1068. // 100-500mA based on config. the 500uA suspend ceiling means
  1069. // that exclusively vbus-powered PXA designs violate USB specs.
  1070. };
  1071. /*-------------------------------------------------------------------------*/
  1072. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1073. static const char proc_node_name [] = "driver/udc";
  1074. static int
  1075. udc_proc_read(char *page, char **start, off_t off, int count,
  1076. int *eof, void *_dev)
  1077. {
  1078. char *buf = page;
  1079. struct pxa2xx_udc *dev = _dev;
  1080. char *next = buf;
  1081. unsigned size = count;
  1082. unsigned long flags;
  1083. int i, t;
  1084. u32 tmp;
  1085. if (off != 0)
  1086. return 0;
  1087. local_irq_save(flags);
  1088. /* basic device status */
  1089. t = scnprintf(next, size, DRIVER_DESC "\n"
  1090. "%s version: %s\nGadget driver: %s\nHost %s\n\n",
  1091. driver_name, DRIVER_VERSION SIZE_STR DMASTR,
  1092. dev->driver ? dev->driver->driver.name : "(none)",
  1093. is_vbus_present() ? "full speed" : "disconnected");
  1094. size -= t;
  1095. next += t;
  1096. /* registers for device and ep0 */
  1097. t = scnprintf(next, size,
  1098. "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  1099. UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
  1100. size -= t;
  1101. next += t;
  1102. tmp = UDCCR;
  1103. t = scnprintf(next, size,
  1104. "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1105. (tmp & UDCCR_REM) ? " rem" : "",
  1106. (tmp & UDCCR_RSTIR) ? " rstir" : "",
  1107. (tmp & UDCCR_SRM) ? " srm" : "",
  1108. (tmp & UDCCR_SUSIR) ? " susir" : "",
  1109. (tmp & UDCCR_RESIR) ? " resir" : "",
  1110. (tmp & UDCCR_RSM) ? " rsm" : "",
  1111. (tmp & UDCCR_UDA) ? " uda" : "",
  1112. (tmp & UDCCR_UDE) ? " ude" : "");
  1113. size -= t;
  1114. next += t;
  1115. tmp = UDCCS0;
  1116. t = scnprintf(next, size,
  1117. "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1118. (tmp & UDCCS0_SA) ? " sa" : "",
  1119. (tmp & UDCCS0_RNE) ? " rne" : "",
  1120. (tmp & UDCCS0_FST) ? " fst" : "",
  1121. (tmp & UDCCS0_SST) ? " sst" : "",
  1122. (tmp & UDCCS0_DRWF) ? " dwrf" : "",
  1123. (tmp & UDCCS0_FTF) ? " ftf" : "",
  1124. (tmp & UDCCS0_IPR) ? " ipr" : "",
  1125. (tmp & UDCCS0_OPR) ? " opr" : "");
  1126. size -= t;
  1127. next += t;
  1128. if (dev->has_cfr) {
  1129. tmp = UDCCFR;
  1130. t = scnprintf(next, size,
  1131. "udccfr %02X =%s%s\n", tmp,
  1132. (tmp & UDCCFR_AREN) ? " aren" : "",
  1133. (tmp & UDCCFR_ACM) ? " acm" : "");
  1134. size -= t;
  1135. next += t;
  1136. }
  1137. if (!is_vbus_present() || !dev->driver)
  1138. goto done;
  1139. t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
  1140. dev->stats.write.bytes, dev->stats.write.ops,
  1141. dev->stats.read.bytes, dev->stats.read.ops,
  1142. dev->stats.irqs);
  1143. size -= t;
  1144. next += t;
  1145. /* dump endpoint queues */
  1146. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1147. struct pxa2xx_ep *ep = &dev->ep [i];
  1148. struct pxa2xx_request *req;
  1149. int t;
  1150. if (i != 0) {
  1151. const struct usb_endpoint_descriptor *d;
  1152. d = ep->desc;
  1153. if (!d)
  1154. continue;
  1155. tmp = *dev->ep [i].reg_udccs;
  1156. t = scnprintf(next, size,
  1157. "%s max %d %s udccs %02x irqs %lu/%lu\n",
  1158. ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
  1159. (ep->dma >= 0) ? "dma" : "pio", tmp,
  1160. ep->pio_irqs, ep->dma_irqs);
  1161. /* TODO translate all five groups of udccs bits! */
  1162. } else /* ep0 should only have one transfer queued */
  1163. t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
  1164. ep->pio_irqs);
  1165. if (t <= 0 || t > size)
  1166. goto done;
  1167. size -= t;
  1168. next += t;
  1169. if (list_empty(&ep->queue)) {
  1170. t = scnprintf(next, size, "\t(nothing queued)\n");
  1171. if (t <= 0 || t > size)
  1172. goto done;
  1173. size -= t;
  1174. next += t;
  1175. continue;
  1176. }
  1177. list_for_each_entry(req, &ep->queue, queue) {
  1178. #ifdef USE_DMA
  1179. if (ep->dma >= 0 && req->queue.prev == &ep->queue)
  1180. t = scnprintf(next, size,
  1181. "\treq %p len %d/%d "
  1182. "buf %p (dma%d dcmd %08x)\n",
  1183. &req->req, req->req.actual,
  1184. req->req.length, req->req.buf,
  1185. ep->dma, DCMD(ep->dma)
  1186. // low 13 bits == bytes-to-go
  1187. );
  1188. else
  1189. #endif
  1190. t = scnprintf(next, size,
  1191. "\treq %p len %d/%d buf %p\n",
  1192. &req->req, req->req.actual,
  1193. req->req.length, req->req.buf);
  1194. if (t <= 0 || t > size)
  1195. goto done;
  1196. size -= t;
  1197. next += t;
  1198. }
  1199. }
  1200. done:
  1201. local_irq_restore(flags);
  1202. *eof = 1;
  1203. return count - size;
  1204. }
  1205. #define create_proc_files() \
  1206. create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
  1207. #define remove_proc_files() \
  1208. remove_proc_entry(proc_node_name, NULL)
  1209. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1210. #define create_proc_files() do {} while (0)
  1211. #define remove_proc_files() do {} while (0)
  1212. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1213. /* "function" sysfs attribute */
  1214. static ssize_t
  1215. show_function (struct device *_dev, struct device_attribute *attr, char *buf)
  1216. {
  1217. struct pxa2xx_udc *dev = dev_get_drvdata (_dev);
  1218. if (!dev->driver
  1219. || !dev->driver->function
  1220. || strlen (dev->driver->function) > PAGE_SIZE)
  1221. return 0;
  1222. return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
  1223. }
  1224. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  1225. /*-------------------------------------------------------------------------*/
  1226. /*
  1227. * udc_disable - disable USB device controller
  1228. */
  1229. static void udc_disable(struct pxa2xx_udc *dev)
  1230. {
  1231. /* block all irqs */
  1232. udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
  1233. UICR0 = UICR1 = 0xff;
  1234. UFNRH = UFNRH_SIM;
  1235. /* if hardware supports it, disconnect from usb */
  1236. pullup_off();
  1237. udc_clear_mask_UDCCR(UDCCR_UDE);
  1238. #ifdef CONFIG_ARCH_PXA
  1239. /* Disable clock for USB device */
  1240. pxa_set_cken(CKEN11_USB, 0);
  1241. #endif
  1242. ep0_idle (dev);
  1243. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1244. LED_CONNECTED_OFF;
  1245. }
  1246. /*
  1247. * udc_reinit - initialize software state
  1248. */
  1249. static void udc_reinit(struct pxa2xx_udc *dev)
  1250. {
  1251. u32 i;
  1252. /* device/ep0 records init */
  1253. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1254. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1255. dev->ep0state = EP0_IDLE;
  1256. /* basic endpoint records init */
  1257. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1258. struct pxa2xx_ep *ep = &dev->ep[i];
  1259. if (i != 0)
  1260. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  1261. ep->desc = NULL;
  1262. ep->stopped = 0;
  1263. INIT_LIST_HEAD (&ep->queue);
  1264. ep->pio_irqs = ep->dma_irqs = 0;
  1265. }
  1266. /* the rest was statically initialized, and is read-only */
  1267. }
  1268. /* until it's enabled, this UDC should be completely invisible
  1269. * to any USB host.
  1270. */
  1271. static void udc_enable (struct pxa2xx_udc *dev)
  1272. {
  1273. udc_clear_mask_UDCCR(UDCCR_UDE);
  1274. #ifdef CONFIG_ARCH_PXA
  1275. /* Enable clock for USB device */
  1276. pxa_set_cken(CKEN11_USB, 1);
  1277. udelay(5);
  1278. #endif
  1279. /* try to clear these bits before we enable the udc */
  1280. udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
  1281. ep0_idle(dev);
  1282. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1283. dev->stats.irqs = 0;
  1284. /*
  1285. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  1286. * - enable UDC
  1287. * - if RESET is already in progress, ack interrupt
  1288. * - unmask reset interrupt
  1289. */
  1290. udc_set_mask_UDCCR(UDCCR_UDE);
  1291. if (!(UDCCR & UDCCR_UDA))
  1292. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1293. if (dev->has_cfr /* UDC_RES2 is defined */) {
  1294. /* pxa255 (a0+) can avoid a set_config race that could
  1295. * prevent gadget drivers from configuring correctly
  1296. */
  1297. UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
  1298. } else {
  1299. /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
  1300. * which could result in missing packets and interrupts.
  1301. * supposedly one bit per endpoint, controlling whether it
  1302. * double buffers or not; ACM/AREN bits fit into the holes.
  1303. * zero bits (like USIR0_IRx) disable double buffering.
  1304. */
  1305. UDC_RES1 = 0x00;
  1306. UDC_RES2 = 0x00;
  1307. }
  1308. #ifdef DISABLE_TEST_MODE
  1309. /* "test mode" seems to have become the default in later chip
  1310. * revs, preventing double buffering (and invalidating docs).
  1311. * this EXPERIMENT enables it for bulk endpoints by tweaking
  1312. * undefined/reserved register bits (that other drivers clear).
  1313. * Belcarra code comments noted this usage.
  1314. */
  1315. if (fifo_mode & 1) { /* IN endpoints */
  1316. UDC_RES1 |= USIR0_IR1|USIR0_IR6;
  1317. UDC_RES2 |= USIR1_IR11;
  1318. }
  1319. if (fifo_mode & 2) { /* OUT endpoints */
  1320. UDC_RES1 |= USIR0_IR2|USIR0_IR7;
  1321. UDC_RES2 |= USIR1_IR12;
  1322. }
  1323. #endif
  1324. /* enable suspend/resume and reset irqs */
  1325. udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
  1326. /* enable ep0 irqs */
  1327. UICR0 &= ~UICR0_IM0;
  1328. /* if hardware supports it, pullup D+ and wait for reset */
  1329. pullup_on();
  1330. }
  1331. /* when a driver is successfully registered, it will receive
  1332. * control requests including set_configuration(), which enables
  1333. * non-control requests. then usb traffic follows until a
  1334. * disconnect is reported. then a host may connect again, or
  1335. * the driver might get unbound.
  1336. */
  1337. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1338. {
  1339. struct pxa2xx_udc *dev = the_controller;
  1340. int retval;
  1341. if (!driver
  1342. || driver->speed != USB_SPEED_FULL
  1343. || !driver->bind
  1344. || !driver->unbind
  1345. || !driver->disconnect
  1346. || !driver->setup)
  1347. return -EINVAL;
  1348. if (!dev)
  1349. return -ENODEV;
  1350. if (dev->driver)
  1351. return -EBUSY;
  1352. /* first hook up the driver ... */
  1353. dev->driver = driver;
  1354. dev->gadget.dev.driver = &driver->driver;
  1355. dev->pullup = 1;
  1356. device_add (&dev->gadget.dev);
  1357. retval = driver->bind(&dev->gadget);
  1358. if (retval) {
  1359. DMSG("bind to driver %s --> error %d\n",
  1360. driver->driver.name, retval);
  1361. device_del (&dev->gadget.dev);
  1362. dev->driver = NULL;
  1363. dev->gadget.dev.driver = NULL;
  1364. return retval;
  1365. }
  1366. device_create_file(dev->dev, &dev_attr_function);
  1367. /* ... then enable host detection and ep0; and we're ready
  1368. * for set_configuration as well as eventual disconnect.
  1369. */
  1370. DMSG("registered gadget driver '%s'\n", driver->driver.name);
  1371. pullup(dev, 1);
  1372. dump_state(dev);
  1373. return 0;
  1374. }
  1375. EXPORT_SYMBOL(usb_gadget_register_driver);
  1376. static void
  1377. stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
  1378. {
  1379. int i;
  1380. /* don't disconnect drivers more than once */
  1381. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1382. driver = NULL;
  1383. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1384. /* prevent new request submissions, kill any outstanding requests */
  1385. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1386. struct pxa2xx_ep *ep = &dev->ep[i];
  1387. ep->stopped = 1;
  1388. nuke(ep, -ESHUTDOWN);
  1389. }
  1390. del_timer_sync(&dev->timer);
  1391. /* report disconnect; the driver is already quiesced */
  1392. LED_CONNECTED_OFF;
  1393. if (driver)
  1394. driver->disconnect(&dev->gadget);
  1395. /* re-init driver-visible data structures */
  1396. udc_reinit(dev);
  1397. }
  1398. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1399. {
  1400. struct pxa2xx_udc *dev = the_controller;
  1401. if (!dev)
  1402. return -ENODEV;
  1403. if (!driver || driver != dev->driver)
  1404. return -EINVAL;
  1405. local_irq_disable();
  1406. pullup(dev, 0);
  1407. stop_activity(dev, driver);
  1408. local_irq_enable();
  1409. driver->unbind(&dev->gadget);
  1410. dev->driver = NULL;
  1411. device_del (&dev->gadget.dev);
  1412. device_remove_file(dev->dev, &dev_attr_function);
  1413. DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
  1414. dump_state(dev);
  1415. return 0;
  1416. }
  1417. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1418. /*-------------------------------------------------------------------------*/
  1419. #ifdef CONFIG_ARCH_LUBBOCK
  1420. /* Lubbock has separate connect and disconnect irqs. More typical designs
  1421. * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
  1422. */
  1423. static irqreturn_t
  1424. lubbock_vbus_irq(int irq, void *_dev, struct pt_regs *r)
  1425. {
  1426. struct pxa2xx_udc *dev = _dev;
  1427. int vbus;
  1428. dev->stats.irqs++;
  1429. HEX_DISPLAY(dev->stats.irqs);
  1430. switch (irq) {
  1431. case LUBBOCK_USB_IRQ:
  1432. LED_CONNECTED_ON;
  1433. vbus = 1;
  1434. disable_irq(LUBBOCK_USB_IRQ);
  1435. enable_irq(LUBBOCK_USB_DISC_IRQ);
  1436. break;
  1437. case LUBBOCK_USB_DISC_IRQ:
  1438. LED_CONNECTED_OFF;
  1439. vbus = 0;
  1440. disable_irq(LUBBOCK_USB_DISC_IRQ);
  1441. enable_irq(LUBBOCK_USB_IRQ);
  1442. break;
  1443. default:
  1444. return IRQ_NONE;
  1445. }
  1446. pxa2xx_udc_vbus_session(&dev->gadget, vbus);
  1447. return IRQ_HANDLED;
  1448. }
  1449. #endif
  1450. /*-------------------------------------------------------------------------*/
  1451. static inline void clear_ep_state (struct pxa2xx_udc *dev)
  1452. {
  1453. unsigned i;
  1454. /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  1455. * fifos, and pending transactions mustn't be continued in any case.
  1456. */
  1457. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
  1458. nuke(&dev->ep[i], -ECONNABORTED);
  1459. }
  1460. static void udc_watchdog(unsigned long _dev)
  1461. {
  1462. struct pxa2xx_udc *dev = (void *)_dev;
  1463. local_irq_disable();
  1464. if (dev->ep0state == EP0_STALL
  1465. && (UDCCS0 & UDCCS0_FST) == 0
  1466. && (UDCCS0 & UDCCS0_SST) == 0) {
  1467. UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
  1468. DBG(DBG_VERBOSE, "ep0 re-stall\n");
  1469. start_watchdog(dev);
  1470. }
  1471. local_irq_enable();
  1472. }
  1473. static void handle_ep0 (struct pxa2xx_udc *dev)
  1474. {
  1475. u32 udccs0 = UDCCS0;
  1476. struct pxa2xx_ep *ep = &dev->ep [0];
  1477. struct pxa2xx_request *req;
  1478. union {
  1479. struct usb_ctrlrequest r;
  1480. u8 raw [8];
  1481. u32 word [2];
  1482. } u;
  1483. if (list_empty(&ep->queue))
  1484. req = NULL;
  1485. else
  1486. req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
  1487. /* clear stall status */
  1488. if (udccs0 & UDCCS0_SST) {
  1489. nuke(ep, -EPIPE);
  1490. UDCCS0 = UDCCS0_SST;
  1491. del_timer(&dev->timer);
  1492. ep0_idle(dev);
  1493. }
  1494. /* previous request unfinished? non-error iff back-to-back ... */
  1495. if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
  1496. nuke(ep, 0);
  1497. del_timer(&dev->timer);
  1498. ep0_idle(dev);
  1499. }
  1500. switch (dev->ep0state) {
  1501. case EP0_IDLE:
  1502. /* late-breaking status? */
  1503. udccs0 = UDCCS0;
  1504. /* start control request? */
  1505. if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
  1506. == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
  1507. int i;
  1508. nuke (ep, -EPROTO);
  1509. /* read SETUP packet */
  1510. for (i = 0; i < 8; i++) {
  1511. if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
  1512. bad_setup:
  1513. DMSG("SETUP %d!\n", i);
  1514. goto stall;
  1515. }
  1516. u.raw [i] = (u8) UDDR0;
  1517. }
  1518. if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
  1519. goto bad_setup;
  1520. got_setup:
  1521. DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1522. u.r.bRequestType, u.r.bRequest,
  1523. le16_to_cpu(u.r.wValue),
  1524. le16_to_cpu(u.r.wIndex),
  1525. le16_to_cpu(u.r.wLength));
  1526. /* cope with automagic for some standard requests. */
  1527. dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
  1528. == USB_TYPE_STANDARD;
  1529. dev->req_config = 0;
  1530. dev->req_pending = 1;
  1531. switch (u.r.bRequest) {
  1532. /* hardware restricts gadget drivers here! */
  1533. case USB_REQ_SET_CONFIGURATION:
  1534. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1535. /* reflect hardware's automagic
  1536. * up to the gadget driver.
  1537. */
  1538. config_change:
  1539. dev->req_config = 1;
  1540. clear_ep_state(dev);
  1541. /* if !has_cfr, there's no synch
  1542. * else use AREN (later) not SA|OPR
  1543. * USIR0_IR0 acts edge sensitive
  1544. */
  1545. }
  1546. break;
  1547. /* ... and here, even more ... */
  1548. case USB_REQ_SET_INTERFACE:
  1549. if (u.r.bRequestType == USB_RECIP_INTERFACE) {
  1550. /* udc hardware is broken by design:
  1551. * - altsetting may only be zero;
  1552. * - hw resets all interfaces' eps;
  1553. * - ep reset doesn't include halt(?).
  1554. */
  1555. DMSG("broken set_interface (%d/%d)\n",
  1556. le16_to_cpu(u.r.wIndex),
  1557. le16_to_cpu(u.r.wValue));
  1558. goto config_change;
  1559. }
  1560. break;
  1561. /* hardware was supposed to hide this */
  1562. case USB_REQ_SET_ADDRESS:
  1563. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1564. ep0start(dev, 0, "address");
  1565. return;
  1566. }
  1567. break;
  1568. }
  1569. if (u.r.bRequestType & USB_DIR_IN)
  1570. dev->ep0state = EP0_IN_DATA_PHASE;
  1571. else
  1572. dev->ep0state = EP0_OUT_DATA_PHASE;
  1573. i = dev->driver->setup(&dev->gadget, &u.r);
  1574. if (i < 0) {
  1575. /* hardware automagic preventing STALL... */
  1576. if (dev->req_config) {
  1577. /* hardware sometimes neglects to tell
  1578. * tell us about config change events,
  1579. * so later ones may fail...
  1580. */
  1581. WARN("config change %02x fail %d?\n",
  1582. u.r.bRequest, i);
  1583. return;
  1584. /* TODO experiment: if has_cfr,
  1585. * hardware didn't ACK; maybe we
  1586. * could actually STALL!
  1587. */
  1588. }
  1589. DBG(DBG_VERBOSE, "protocol STALL, "
  1590. "%02x err %d\n", UDCCS0, i);
  1591. stall:
  1592. /* the watchdog timer helps deal with cases
  1593. * where udc seems to clear FST wrongly, and
  1594. * then NAKs instead of STALLing.
  1595. */
  1596. ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
  1597. start_watchdog(dev);
  1598. dev->ep0state = EP0_STALL;
  1599. /* deferred i/o == no response yet */
  1600. } else if (dev->req_pending) {
  1601. if (likely(dev->ep0state == EP0_IN_DATA_PHASE
  1602. || dev->req_std || u.r.wLength))
  1603. ep0start(dev, 0, "defer");
  1604. else
  1605. ep0start(dev, UDCCS0_IPR, "defer/IPR");
  1606. }
  1607. /* expect at least one data or status stage irq */
  1608. return;
  1609. } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
  1610. == (UDCCS0_OPR|UDCCS0_SA))) {
  1611. unsigned i;
  1612. /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
  1613. * still observed on a pxa255 a0.
  1614. */
  1615. DBG(DBG_VERBOSE, "e131\n");
  1616. nuke(ep, -EPROTO);
  1617. /* read SETUP data, but don't trust it too much */
  1618. for (i = 0; i < 8; i++)
  1619. u.raw [i] = (u8) UDDR0;
  1620. if ((u.r.bRequestType & USB_RECIP_MASK)
  1621. > USB_RECIP_OTHER)
  1622. goto stall;
  1623. if (u.word [0] == 0 && u.word [1] == 0)
  1624. goto stall;
  1625. goto got_setup;
  1626. } else {
  1627. /* some random early IRQ:
  1628. * - we acked FST
  1629. * - IPR cleared
  1630. * - OPR got set, without SA (likely status stage)
  1631. */
  1632. UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
  1633. }
  1634. break;
  1635. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  1636. if (udccs0 & UDCCS0_OPR) {
  1637. UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
  1638. DBG(DBG_VERBOSE, "ep0in premature status\n");
  1639. if (req)
  1640. done(ep, req, 0);
  1641. ep0_idle(dev);
  1642. } else /* irq was IPR clearing */ {
  1643. if (req) {
  1644. /* this IN packet might finish the request */
  1645. (void) write_ep0_fifo(ep, req);
  1646. } /* else IN token before response was written */
  1647. }
  1648. break;
  1649. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  1650. if (udccs0 & UDCCS0_OPR) {
  1651. if (req) {
  1652. /* this OUT packet might finish the request */
  1653. if (read_ep0_fifo(ep, req))
  1654. done(ep, req, 0);
  1655. /* else more OUT packets expected */
  1656. } /* else OUT token before read was issued */
  1657. } else /* irq was IPR clearing */ {
  1658. DBG(DBG_VERBOSE, "ep0out premature status\n");
  1659. if (req)
  1660. done(ep, req, 0);
  1661. ep0_idle(dev);
  1662. }
  1663. break;
  1664. case EP0_END_XFER:
  1665. if (req)
  1666. done(ep, req, 0);
  1667. /* ack control-IN status (maybe in-zlp was skipped)
  1668. * also appears after some config change events.
  1669. */
  1670. if (udccs0 & UDCCS0_OPR)
  1671. UDCCS0 = UDCCS0_OPR;
  1672. ep0_idle(dev);
  1673. break;
  1674. case EP0_STALL:
  1675. UDCCS0 = UDCCS0_FST;
  1676. break;
  1677. }
  1678. USIR0 = USIR0_IR0;
  1679. }
  1680. static void handle_ep(struct pxa2xx_ep *ep)
  1681. {
  1682. struct pxa2xx_request *req;
  1683. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  1684. int completed;
  1685. u32 udccs, tmp;
  1686. do {
  1687. completed = 0;
  1688. if (likely (!list_empty(&ep->queue)))
  1689. req = list_entry(ep->queue.next,
  1690. struct pxa2xx_request, queue);
  1691. else
  1692. req = NULL;
  1693. // TODO check FST handling
  1694. udccs = *ep->reg_udccs;
  1695. if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
  1696. tmp = UDCCS_BI_TUR;
  1697. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1698. tmp |= UDCCS_BI_SST;
  1699. tmp &= udccs;
  1700. if (likely (tmp))
  1701. *ep->reg_udccs = tmp;
  1702. if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
  1703. completed = write_fifo(ep, req);
  1704. } else { /* irq from RPC (or for ISO, ROF) */
  1705. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1706. tmp = UDCCS_BO_SST | UDCCS_BO_DME;
  1707. else
  1708. tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
  1709. tmp &= udccs;
  1710. if (likely(tmp))
  1711. *ep->reg_udccs = tmp;
  1712. /* fifos can hold packets, ready for reading... */
  1713. if (likely(req)) {
  1714. #ifdef USE_OUT_DMA
  1715. // TODO didn't yet debug out-dma. this approach assumes
  1716. // the worst about short packets and RPC; it might be better.
  1717. if (likely(ep->dma >= 0)) {
  1718. if (!(udccs & UDCCS_BO_RSP)) {
  1719. *ep->reg_udccs = UDCCS_BO_RPC;
  1720. ep->dma_irqs++;
  1721. return;
  1722. }
  1723. }
  1724. #endif
  1725. completed = read_fifo(ep, req);
  1726. } else
  1727. pio_irq_disable (ep->bEndpointAddress);
  1728. }
  1729. ep->pio_irqs++;
  1730. } while (completed);
  1731. }
  1732. /*
  1733. * pxa2xx_udc_irq - interrupt handler
  1734. *
  1735. * avoid delays in ep0 processing. the control handshaking isn't always
  1736. * under software control (pxa250c0 and the pxa255 are better), and delays
  1737. * could cause usb protocol errors.
  1738. */
  1739. static irqreturn_t
  1740. pxa2xx_udc_irq(int irq, void *_dev, struct pt_regs *r)
  1741. {
  1742. struct pxa2xx_udc *dev = _dev;
  1743. int handled;
  1744. dev->stats.irqs++;
  1745. HEX_DISPLAY(dev->stats.irqs);
  1746. do {
  1747. u32 udccr = UDCCR;
  1748. handled = 0;
  1749. /* SUSpend Interrupt Request */
  1750. if (unlikely(udccr & UDCCR_SUSIR)) {
  1751. udc_ack_int_UDCCR(UDCCR_SUSIR);
  1752. handled = 1;
  1753. DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
  1754. ? "" : "+disconnect");
  1755. if (!is_vbus_present())
  1756. stop_activity(dev, dev->driver);
  1757. else if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1758. && dev->driver
  1759. && dev->driver->suspend)
  1760. dev->driver->suspend(&dev->gadget);
  1761. ep0_idle (dev);
  1762. }
  1763. /* RESume Interrupt Request */
  1764. if (unlikely(udccr & UDCCR_RESIR)) {
  1765. udc_ack_int_UDCCR(UDCCR_RESIR);
  1766. handled = 1;
  1767. DBG(DBG_VERBOSE, "USB resume\n");
  1768. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1769. && dev->driver
  1770. && dev->driver->resume
  1771. && is_vbus_present())
  1772. dev->driver->resume(&dev->gadget);
  1773. }
  1774. /* ReSeT Interrupt Request - USB reset */
  1775. if (unlikely(udccr & UDCCR_RSTIR)) {
  1776. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1777. handled = 1;
  1778. if ((UDCCR & UDCCR_UDA) == 0) {
  1779. DBG(DBG_VERBOSE, "USB reset start\n");
  1780. /* reset driver and endpoints,
  1781. * in case that's not yet done
  1782. */
  1783. stop_activity (dev, dev->driver);
  1784. } else {
  1785. DBG(DBG_VERBOSE, "USB reset end\n");
  1786. dev->gadget.speed = USB_SPEED_FULL;
  1787. LED_CONNECTED_ON;
  1788. memset(&dev->stats, 0, sizeof dev->stats);
  1789. /* driver and endpoints are still reset */
  1790. }
  1791. } else {
  1792. u32 usir0 = USIR0 & ~UICR0;
  1793. u32 usir1 = USIR1 & ~UICR1;
  1794. int i;
  1795. if (unlikely (!usir0 && !usir1))
  1796. continue;
  1797. DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
  1798. /* control traffic */
  1799. if (usir0 & USIR0_IR0) {
  1800. dev->ep[0].pio_irqs++;
  1801. handle_ep0(dev);
  1802. handled = 1;
  1803. }
  1804. /* endpoint data transfers */
  1805. for (i = 0; i < 8; i++) {
  1806. u32 tmp = 1 << i;
  1807. if (i && (usir0 & tmp)) {
  1808. handle_ep(&dev->ep[i]);
  1809. USIR0 |= tmp;
  1810. handled = 1;
  1811. }
  1812. if (usir1 & tmp) {
  1813. handle_ep(&dev->ep[i+8]);
  1814. USIR1 |= tmp;
  1815. handled = 1;
  1816. }
  1817. }
  1818. }
  1819. /* we could also ask for 1 msec SOF (SIR) interrupts */
  1820. } while (handled);
  1821. return IRQ_HANDLED;
  1822. }
  1823. /*-------------------------------------------------------------------------*/
  1824. static void nop_release (struct device *dev)
  1825. {
  1826. DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
  1827. }
  1828. /* this uses load-time allocation and initialization (instead of
  1829. * doing it at run-time) to save code, eliminate fault paths, and
  1830. * be more obviously correct.
  1831. */
  1832. static struct pxa2xx_udc memory = {
  1833. .gadget = {
  1834. .ops = &pxa2xx_udc_ops,
  1835. .ep0 = &memory.ep[0].ep,
  1836. .name = driver_name,
  1837. .dev = {
  1838. .bus_id = "gadget",
  1839. .release = nop_release,
  1840. },
  1841. },
  1842. /* control endpoint */
  1843. .ep[0] = {
  1844. .ep = {
  1845. .name = ep0name,
  1846. .ops = &pxa2xx_ep_ops,
  1847. .maxpacket = EP0_FIFO_SIZE,
  1848. },
  1849. .dev = &memory,
  1850. .reg_udccs = &UDCCS0,
  1851. .reg_uddr = &UDDR0,
  1852. },
  1853. /* first group of endpoints */
  1854. .ep[1] = {
  1855. .ep = {
  1856. .name = "ep1in-bulk",
  1857. .ops = &pxa2xx_ep_ops,
  1858. .maxpacket = BULK_FIFO_SIZE,
  1859. },
  1860. .dev = &memory,
  1861. .fifo_size = BULK_FIFO_SIZE,
  1862. .bEndpointAddress = USB_DIR_IN | 1,
  1863. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1864. .reg_udccs = &UDCCS1,
  1865. .reg_uddr = &UDDR1,
  1866. drcmr (25)
  1867. },
  1868. .ep[2] = {
  1869. .ep = {
  1870. .name = "ep2out-bulk",
  1871. .ops = &pxa2xx_ep_ops,
  1872. .maxpacket = BULK_FIFO_SIZE,
  1873. },
  1874. .dev = &memory,
  1875. .fifo_size = BULK_FIFO_SIZE,
  1876. .bEndpointAddress = 2,
  1877. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1878. .reg_udccs = &UDCCS2,
  1879. .reg_ubcr = &UBCR2,
  1880. .reg_uddr = &UDDR2,
  1881. drcmr (26)
  1882. },
  1883. #ifndef CONFIG_USB_PXA2XX_SMALL
  1884. .ep[3] = {
  1885. .ep = {
  1886. .name = "ep3in-iso",
  1887. .ops = &pxa2xx_ep_ops,
  1888. .maxpacket = ISO_FIFO_SIZE,
  1889. },
  1890. .dev = &memory,
  1891. .fifo_size = ISO_FIFO_SIZE,
  1892. .bEndpointAddress = USB_DIR_IN | 3,
  1893. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1894. .reg_udccs = &UDCCS3,
  1895. .reg_uddr = &UDDR3,
  1896. drcmr (27)
  1897. },
  1898. .ep[4] = {
  1899. .ep = {
  1900. .name = "ep4out-iso",
  1901. .ops = &pxa2xx_ep_ops,
  1902. .maxpacket = ISO_FIFO_SIZE,
  1903. },
  1904. .dev = &memory,
  1905. .fifo_size = ISO_FIFO_SIZE,
  1906. .bEndpointAddress = 4,
  1907. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1908. .reg_udccs = &UDCCS4,
  1909. .reg_ubcr = &UBCR4,
  1910. .reg_uddr = &UDDR4,
  1911. drcmr (28)
  1912. },
  1913. .ep[5] = {
  1914. .ep = {
  1915. .name = "ep5in-int",
  1916. .ops = &pxa2xx_ep_ops,
  1917. .maxpacket = INT_FIFO_SIZE,
  1918. },
  1919. .dev = &memory,
  1920. .fifo_size = INT_FIFO_SIZE,
  1921. .bEndpointAddress = USB_DIR_IN | 5,
  1922. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1923. .reg_udccs = &UDCCS5,
  1924. .reg_uddr = &UDDR5,
  1925. },
  1926. /* second group of endpoints */
  1927. .ep[6] = {
  1928. .ep = {
  1929. .name = "ep6in-bulk",
  1930. .ops = &pxa2xx_ep_ops,
  1931. .maxpacket = BULK_FIFO_SIZE,
  1932. },
  1933. .dev = &memory,
  1934. .fifo_size = BULK_FIFO_SIZE,
  1935. .bEndpointAddress = USB_DIR_IN | 6,
  1936. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1937. .reg_udccs = &UDCCS6,
  1938. .reg_uddr = &UDDR6,
  1939. drcmr (30)
  1940. },
  1941. .ep[7] = {
  1942. .ep = {
  1943. .name = "ep7out-bulk",
  1944. .ops = &pxa2xx_ep_ops,
  1945. .maxpacket = BULK_FIFO_SIZE,
  1946. },
  1947. .dev = &memory,
  1948. .fifo_size = BULK_FIFO_SIZE,
  1949. .bEndpointAddress = 7,
  1950. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1951. .reg_udccs = &UDCCS7,
  1952. .reg_ubcr = &UBCR7,
  1953. .reg_uddr = &UDDR7,
  1954. drcmr (31)
  1955. },
  1956. .ep[8] = {
  1957. .ep = {
  1958. .name = "ep8in-iso",
  1959. .ops = &pxa2xx_ep_ops,
  1960. .maxpacket = ISO_FIFO_SIZE,
  1961. },
  1962. .dev = &memory,
  1963. .fifo_size = ISO_FIFO_SIZE,
  1964. .bEndpointAddress = USB_DIR_IN | 8,
  1965. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1966. .reg_udccs = &UDCCS8,
  1967. .reg_uddr = &UDDR8,
  1968. drcmr (32)
  1969. },
  1970. .ep[9] = {
  1971. .ep = {
  1972. .name = "ep9out-iso",
  1973. .ops = &pxa2xx_ep_ops,
  1974. .maxpacket = ISO_FIFO_SIZE,
  1975. },
  1976. .dev = &memory,
  1977. .fifo_size = ISO_FIFO_SIZE,
  1978. .bEndpointAddress = 9,
  1979. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1980. .reg_udccs = &UDCCS9,
  1981. .reg_ubcr = &UBCR9,
  1982. .reg_uddr = &UDDR9,
  1983. drcmr (33)
  1984. },
  1985. .ep[10] = {
  1986. .ep = {
  1987. .name = "ep10in-int",
  1988. .ops = &pxa2xx_ep_ops,
  1989. .maxpacket = INT_FIFO_SIZE,
  1990. },
  1991. .dev = &memory,
  1992. .fifo_size = INT_FIFO_SIZE,
  1993. .bEndpointAddress = USB_DIR_IN | 10,
  1994. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1995. .reg_udccs = &UDCCS10,
  1996. .reg_uddr = &UDDR10,
  1997. },
  1998. /* third group of endpoints */
  1999. .ep[11] = {
  2000. .ep = {
  2001. .name = "ep11in-bulk",
  2002. .ops = &pxa2xx_ep_ops,
  2003. .maxpacket = BULK_FIFO_SIZE,
  2004. },
  2005. .dev = &memory,
  2006. .fifo_size = BULK_FIFO_SIZE,
  2007. .bEndpointAddress = USB_DIR_IN | 11,
  2008. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  2009. .reg_udccs = &UDCCS11,
  2010. .reg_uddr = &UDDR11,
  2011. drcmr (35)
  2012. },
  2013. .ep[12] = {
  2014. .ep = {
  2015. .name = "ep12out-bulk",
  2016. .ops = &pxa2xx_ep_ops,
  2017. .maxpacket = BULK_FIFO_SIZE,
  2018. },
  2019. .dev = &memory,
  2020. .fifo_size = BULK_FIFO_SIZE,
  2021. .bEndpointAddress = 12,
  2022. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  2023. .reg_udccs = &UDCCS12,
  2024. .reg_ubcr = &UBCR12,
  2025. .reg_uddr = &UDDR12,
  2026. drcmr (36)
  2027. },
  2028. .ep[13] = {
  2029. .ep = {
  2030. .name = "ep13in-iso",
  2031. .ops = &pxa2xx_ep_ops,
  2032. .maxpacket = ISO_FIFO_SIZE,
  2033. },
  2034. .dev = &memory,
  2035. .fifo_size = ISO_FIFO_SIZE,
  2036. .bEndpointAddress = USB_DIR_IN | 13,
  2037. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  2038. .reg_udccs = &UDCCS13,
  2039. .reg_uddr = &UDDR13,
  2040. drcmr (37)
  2041. },
  2042. .ep[14] = {
  2043. .ep = {
  2044. .name = "ep14out-iso",
  2045. .ops = &pxa2xx_ep_ops,
  2046. .maxpacket = ISO_FIFO_SIZE,
  2047. },
  2048. .dev = &memory,
  2049. .fifo_size = ISO_FIFO_SIZE,
  2050. .bEndpointAddress = 14,
  2051. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  2052. .reg_udccs = &UDCCS14,
  2053. .reg_ubcr = &UBCR14,
  2054. .reg_uddr = &UDDR14,
  2055. drcmr (38)
  2056. },
  2057. .ep[15] = {
  2058. .ep = {
  2059. .name = "ep15in-int",
  2060. .ops = &pxa2xx_ep_ops,
  2061. .maxpacket = INT_FIFO_SIZE,
  2062. },
  2063. .dev = &memory,
  2064. .fifo_size = INT_FIFO_SIZE,
  2065. .bEndpointAddress = USB_DIR_IN | 15,
  2066. .bmAttributes = USB_ENDPOINT_XFER_INT,
  2067. .reg_udccs = &UDCCS15,
  2068. .reg_uddr = &UDDR15,
  2069. },
  2070. #endif /* !CONFIG_USB_PXA2XX_SMALL */
  2071. };
  2072. #define CP15R0_VENDOR_MASK 0xffffe000
  2073. #if defined(CONFIG_ARCH_PXA)
  2074. #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
  2075. #elif defined(CONFIG_ARCH_IXP4XX)
  2076. #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
  2077. #endif
  2078. #define CP15R0_PROD_MASK 0x000003f0
  2079. #define PXA25x 0x00000100 /* and PXA26x */
  2080. #define PXA210 0x00000120
  2081. #define CP15R0_REV_MASK 0x0000000f
  2082. #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
  2083. #define PXA255_A0 0x00000106 /* or PXA260_B1 */
  2084. #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
  2085. #define PXA250_B2 0x00000104
  2086. #define PXA250_B1 0x00000103 /* or PXA260_A0 */
  2087. #define PXA250_B0 0x00000102
  2088. #define PXA250_A1 0x00000101
  2089. #define PXA250_A0 0x00000100
  2090. #define PXA210_C0 0x00000125
  2091. #define PXA210_B2 0x00000124
  2092. #define PXA210_B1 0x00000123
  2093. #define PXA210_B0 0x00000122
  2094. #define IXP425_A0 0x000001c1
  2095. /*
  2096. * probe - binds to the platform device
  2097. */
  2098. static int __init pxa2xx_udc_probe(struct platform_device *pdev)
  2099. {
  2100. struct pxa2xx_udc *dev = &memory;
  2101. int retval, out_dma = 1;
  2102. u32 chiprev;
  2103. /* insist on Intel/ARM/XScale */
  2104. asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
  2105. if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
  2106. printk(KERN_ERR "%s: not XScale!\n", driver_name);
  2107. return -ENODEV;
  2108. }
  2109. /* trigger chiprev-specific logic */
  2110. switch (chiprev & CP15R0_PRODREV_MASK) {
  2111. #if defined(CONFIG_ARCH_PXA)
  2112. case PXA255_A0:
  2113. dev->has_cfr = 1;
  2114. break;
  2115. case PXA250_A0:
  2116. case PXA250_A1:
  2117. /* A0/A1 "not released"; ep 13, 15 unusable */
  2118. /* fall through */
  2119. case PXA250_B2: case PXA210_B2:
  2120. case PXA250_B1: case PXA210_B1:
  2121. case PXA250_B0: case PXA210_B0:
  2122. out_dma = 0;
  2123. /* fall through */
  2124. case PXA250_C0: case PXA210_C0:
  2125. break;
  2126. #elif defined(CONFIG_ARCH_IXP4XX)
  2127. case IXP425_A0:
  2128. out_dma = 0;
  2129. break;
  2130. #endif
  2131. default:
  2132. out_dma = 0;
  2133. printk(KERN_ERR "%s: unrecognized processor: %08x\n",
  2134. driver_name, chiprev);
  2135. /* iop3xx, ixp4xx, ... */
  2136. return -ENODEV;
  2137. }
  2138. pr_debug("%s: IRQ %d%s%s%s\n", driver_name, IRQ_USB,
  2139. dev->has_cfr ? "" : " (!cfr)",
  2140. out_dma ? "" : " (broken dma-out)",
  2141. SIZE_STR DMASTR
  2142. );
  2143. #ifdef USE_DMA
  2144. #ifndef USE_OUT_DMA
  2145. out_dma = 0;
  2146. #endif
  2147. /* pxa 250 erratum 130 prevents using OUT dma (fixed C0) */
  2148. if (!out_dma) {
  2149. DMSG("disabled OUT dma\n");
  2150. dev->ep[ 2].reg_drcmr = dev->ep[ 4].reg_drcmr = 0;
  2151. dev->ep[ 7].reg_drcmr = dev->ep[ 9].reg_drcmr = 0;
  2152. dev->ep[12].reg_drcmr = dev->ep[14].reg_drcmr = 0;
  2153. }
  2154. #endif
  2155. /* other non-static parts of init */
  2156. dev->dev = &pdev->dev;
  2157. dev->mach = pdev->dev.platform_data;
  2158. init_timer(&dev->timer);
  2159. dev->timer.function = udc_watchdog;
  2160. dev->timer.data = (unsigned long) dev;
  2161. device_initialize(&dev->gadget.dev);
  2162. dev->gadget.dev.parent = &pdev->dev;
  2163. dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
  2164. the_controller = dev;
  2165. platform_set_drvdata(pdev, dev);
  2166. udc_disable(dev);
  2167. udc_reinit(dev);
  2168. dev->vbus = is_vbus_present();
  2169. /* irq setup after old hardware state is cleaned up */
  2170. retval = request_irq(IRQ_USB, pxa2xx_udc_irq,
  2171. SA_INTERRUPT, driver_name, dev);
  2172. if (retval != 0) {
  2173. printk(KERN_ERR "%s: can't get irq %i, err %d\n",
  2174. driver_name, IRQ_USB, retval);
  2175. return -EBUSY;
  2176. }
  2177. dev->got_irq = 1;
  2178. #ifdef CONFIG_ARCH_LUBBOCK
  2179. if (machine_is_lubbock()) {
  2180. retval = request_irq(LUBBOCK_USB_DISC_IRQ,
  2181. lubbock_vbus_irq,
  2182. SA_INTERRUPT | SA_SAMPLE_RANDOM,
  2183. driver_name, dev);
  2184. if (retval != 0) {
  2185. printk(KERN_ERR "%s: can't get irq %i, err %d\n",
  2186. driver_name, LUBBOCK_USB_DISC_IRQ, retval);
  2187. lubbock_fail0:
  2188. free_irq(IRQ_USB, dev);
  2189. return -EBUSY;
  2190. }
  2191. retval = request_irq(LUBBOCK_USB_IRQ,
  2192. lubbock_vbus_irq,
  2193. SA_INTERRUPT | SA_SAMPLE_RANDOM,
  2194. driver_name, dev);
  2195. if (retval != 0) {
  2196. printk(KERN_ERR "%s: can't get irq %i, err %d\n",
  2197. driver_name, LUBBOCK_USB_IRQ, retval);
  2198. free_irq(LUBBOCK_USB_DISC_IRQ, dev);
  2199. goto lubbock_fail0;
  2200. }
  2201. #ifdef DEBUG
  2202. /* with U-Boot (but not BLOB), hex is off by default */
  2203. HEX_DISPLAY(dev->stats.irqs);
  2204. LUB_DISC_BLNK_LED &= 0xff;
  2205. #endif
  2206. }
  2207. #endif
  2208. create_proc_files();
  2209. return 0;
  2210. }
  2211. static void pxa2xx_udc_shutdown(struct platform_device *_dev)
  2212. {
  2213. pullup_off();
  2214. }
  2215. static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
  2216. {
  2217. struct pxa2xx_udc *dev = platform_get_drvdata(pdev);
  2218. udc_disable(dev);
  2219. remove_proc_files();
  2220. usb_gadget_unregister_driver(dev->driver);
  2221. if (dev->got_irq) {
  2222. free_irq(IRQ_USB, dev);
  2223. dev->got_irq = 0;
  2224. }
  2225. if (machine_is_lubbock()) {
  2226. free_irq(LUBBOCK_USB_DISC_IRQ, dev);
  2227. free_irq(LUBBOCK_USB_IRQ, dev);
  2228. }
  2229. platform_set_drvdata(pdev, NULL);
  2230. the_controller = NULL;
  2231. return 0;
  2232. }
  2233. /*-------------------------------------------------------------------------*/
  2234. #ifdef CONFIG_PM
  2235. /* USB suspend (controlled by the host) and system suspend (controlled
  2236. * by the PXA) don't necessarily work well together. If USB is active,
  2237. * the 48 MHz clock is required; so the system can't enter 33 MHz idle
  2238. * mode, or any deeper PM saving state.
  2239. *
  2240. * For now, we punt and forcibly disconnect from the USB host when PXA
  2241. * enters any suspend state. While we're disconnected, we always disable
  2242. * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
  2243. * Boards without software pullup control shouldn't use those states.
  2244. * VBUS IRQs should probably be ignored so that the PXA device just acts
  2245. * "dead" to USB hosts until system resume.
  2246. */
  2247. static int pxa2xx_udc_suspend(struct platform_device *dev, pm_message_t state)
  2248. {
  2249. struct pxa2xx_udc *udc = platform_get_drvdata(dev);
  2250. if (!udc->mach->udc_command)
  2251. WARN("USB host won't detect disconnect!\n");
  2252. pullup(udc, 0);
  2253. return 0;
  2254. }
  2255. static int pxa2xx_udc_resume(struct platform_device *dev)
  2256. {
  2257. struct pxa2xx_udc *udc = platform_get_drvdata(dev);
  2258. pullup(udc, 1);
  2259. return 0;
  2260. }
  2261. #else
  2262. #define pxa2xx_udc_suspend NULL
  2263. #define pxa2xx_udc_resume NULL
  2264. #endif
  2265. /*-------------------------------------------------------------------------*/
  2266. static struct platform_driver udc_driver = {
  2267. .probe = pxa2xx_udc_probe,
  2268. .shutdown = pxa2xx_udc_shutdown,
  2269. .remove = __exit_p(pxa2xx_udc_remove),
  2270. .suspend = pxa2xx_udc_suspend,
  2271. .resume = pxa2xx_udc_resume,
  2272. .driver = {
  2273. .owner = THIS_MODULE,
  2274. .name = "pxa2xx-udc",
  2275. },
  2276. };
  2277. static int __init udc_init(void)
  2278. {
  2279. printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
  2280. return platform_driver_register(&udc_driver);
  2281. }
  2282. module_init(udc_init);
  2283. static void __exit udc_exit(void)
  2284. {
  2285. platform_driver_unregister(&udc_driver);
  2286. }
  2287. module_exit(udc_exit);
  2288. MODULE_DESCRIPTION(DRIVER_DESC);
  2289. MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
  2290. MODULE_LICENSE("GPL");