pxa2xx_udc.c 58 KB

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