pxa2xx_udc.c 58 KB

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