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