pxa2xx_udc.c 67 KB

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