pxa2xx_udc.c 66 KB

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