pxa2xx_udc.c 66 KB

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