pxa2xx_udc.c 58 KB

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