pxa25x_udc.c 58 KB

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