pxa2xx_udc.c 59 KB

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