pxa2xx_udc.c 67 KB

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