pxa2xx_udc.c 59 KB

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