pxa2xx_udc.c 58 KB

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