pxa2xx_udc.c 65 KB

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