at91_udc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /*
  2. * at91_udc -- driver for at91-series USB peripheral controller
  3. *
  4. * Copyright (C) 2004 by Thomas Rathbone
  5. * Copyright (C) 2005 by HP Labs
  6. * Copyright (C) 2005 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #undef DEBUG
  24. #undef VERBOSE
  25. #undef PACKET_TRACE
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/delay.h>
  30. #include <linux/ioport.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/errno.h>
  35. #include <linux/init.h>
  36. #include <linux/list.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/clk.h>
  40. #include <linux/usb/ch9.h>
  41. #include <linux/usb_gadget.h>
  42. #include <asm/byteorder.h>
  43. #include <asm/hardware.h>
  44. #include <asm/io.h>
  45. #include <asm/irq.h>
  46. #include <asm/system.h>
  47. #include <asm/mach-types.h>
  48. #include <asm/arch/gpio.h>
  49. #include <asm/arch/board.h>
  50. #include <asm/arch/cpu.h>
  51. #include <asm/arch/at91sam9261_matrix.h>
  52. #include "at91_udc.h"
  53. /*
  54. * This controller is simple and PIO-only. It's used in many AT91-series
  55. * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
  56. * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
  57. *
  58. * This driver expects the board has been wired with two GPIOs suppporting
  59. * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
  60. * testing hasn't covered such cases.)
  61. *
  62. * The pullup is most important (so it's integrated on sam926x parts). It
  63. * provides software control over whether the host enumerates the device.
  64. *
  65. * The VBUS sensing helps during enumeration, and allows both USB clocks
  66. * (and the transceiver) to stay gated off until they're necessary, saving
  67. * power. During USB suspend, the 48 MHz clock is gated off in hardware;
  68. * it may also be gated off by software during some Linux sleep states.
  69. */
  70. #define DRIVER_VERSION "3 May 2006"
  71. static const char driver_name [] = "at91_udc";
  72. static const char ep0name[] = "ep0";
  73. #define at91_udp_read(dev, reg) \
  74. __raw_readl((dev)->udp_baseaddr + (reg))
  75. #define at91_udp_write(dev, reg, val) \
  76. __raw_writel((val), (dev)->udp_baseaddr + (reg))
  77. /*-------------------------------------------------------------------------*/
  78. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  79. #include <linux/seq_file.h>
  80. static const char debug_filename[] = "driver/udc";
  81. #define FOURBITS "%s%s%s%s"
  82. #define EIGHTBITS FOURBITS FOURBITS
  83. static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
  84. {
  85. static char *types[] = {
  86. "control", "out-iso", "out-bulk", "out-int",
  87. "BOGUS", "in-iso", "in-bulk", "in-int"};
  88. u32 csr;
  89. struct at91_request *req;
  90. unsigned long flags;
  91. local_irq_save(flags);
  92. csr = __raw_readl(ep->creg);
  93. /* NOTE: not collecting per-endpoint irq statistics... */
  94. seq_printf(s, "\n");
  95. seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
  96. ep->ep.name, ep->ep.maxpacket,
  97. ep->is_in ? "in" : "out",
  98. ep->is_iso ? " iso" : "",
  99. ep->is_pingpong
  100. ? (ep->fifo_bank ? "pong" : "ping")
  101. : "",
  102. ep->stopped ? " stopped" : "");
  103. seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
  104. csr,
  105. (csr & 0x07ff0000) >> 16,
  106. (csr & (1 << 15)) ? "enabled" : "disabled",
  107. (csr & (1 << 11)) ? "DATA1" : "DATA0",
  108. types[(csr & 0x700) >> 8],
  109. /* iff type is control then print current direction */
  110. (!(csr & 0x700))
  111. ? ((csr & (1 << 7)) ? " IN" : " OUT")
  112. : "",
  113. (csr & (1 << 6)) ? " rxdatabk1" : "",
  114. (csr & (1 << 5)) ? " forcestall" : "",
  115. (csr & (1 << 4)) ? " txpktrdy" : "",
  116. (csr & (1 << 3)) ? " stallsent" : "",
  117. (csr & (1 << 2)) ? " rxsetup" : "",
  118. (csr & (1 << 1)) ? " rxdatabk0" : "",
  119. (csr & (1 << 0)) ? " txcomp" : "");
  120. if (list_empty (&ep->queue))
  121. seq_printf(s, "\t(queue empty)\n");
  122. else list_for_each_entry (req, &ep->queue, queue) {
  123. unsigned length = req->req.actual;
  124. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  125. &req->req, length,
  126. req->req.length, req->req.buf);
  127. }
  128. local_irq_restore(flags);
  129. }
  130. static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
  131. {
  132. int i;
  133. seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
  134. (mask & (1 << 13)) ? " wakeup" : "",
  135. (mask & (1 << 12)) ? " endbusres" : "",
  136. (mask & (1 << 11)) ? " sofint" : "",
  137. (mask & (1 << 10)) ? " extrsm" : "",
  138. (mask & (1 << 9)) ? " rxrsm" : "",
  139. (mask & (1 << 8)) ? " rxsusp" : "");
  140. for (i = 0; i < 8; i++) {
  141. if (mask & (1 << i))
  142. seq_printf(s, " ep%d", i);
  143. }
  144. seq_printf(s, "\n");
  145. }
  146. static int proc_udc_show(struct seq_file *s, void *unused)
  147. {
  148. struct at91_udc *udc = s->private;
  149. struct at91_ep *ep;
  150. u32 tmp;
  151. seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
  152. seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
  153. udc->vbus ? "present" : "off",
  154. udc->enabled
  155. ? (udc->vbus ? "active" : "enabled")
  156. : "disabled",
  157. udc->selfpowered ? "self" : "VBUS",
  158. udc->suspended ? ", suspended" : "",
  159. udc->driver ? udc->driver->driver.name : "(none)");
  160. /* don't access registers when interface isn't clocked */
  161. if (!udc->clocked) {
  162. seq_printf(s, "(not clocked)\n");
  163. return 0;
  164. }
  165. tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
  166. seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
  167. (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
  168. (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
  169. (tmp & AT91_UDP_NUM));
  170. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  171. seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
  172. (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
  173. (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
  174. (tmp & AT91_UDP_ESR) ? " esr" : "",
  175. (tmp & AT91_UDP_CONFG) ? " confg" : "",
  176. (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
  177. tmp = at91_udp_read(udc, AT91_UDP_FADDR);
  178. seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
  179. (tmp & AT91_UDP_FEN) ? " fen" : "",
  180. (tmp & AT91_UDP_FADD));
  181. proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
  182. proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
  183. if (udc->enabled && udc->vbus) {
  184. proc_ep_show(s, &udc->ep[0]);
  185. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  186. if (ep->desc)
  187. proc_ep_show(s, ep);
  188. }
  189. }
  190. return 0;
  191. }
  192. static int proc_udc_open(struct inode *inode, struct file *file)
  193. {
  194. return single_open(file, proc_udc_show, PDE(inode)->data);
  195. }
  196. static const struct file_operations proc_ops = {
  197. .open = proc_udc_open,
  198. .read = seq_read,
  199. .llseek = seq_lseek,
  200. .release = single_release,
  201. };
  202. static void create_debug_file(struct at91_udc *udc)
  203. {
  204. struct proc_dir_entry *pde;
  205. pde = create_proc_entry (debug_filename, 0, NULL);
  206. udc->pde = pde;
  207. if (pde == NULL)
  208. return;
  209. pde->proc_fops = &proc_ops;
  210. pde->data = udc;
  211. }
  212. static void remove_debug_file(struct at91_udc *udc)
  213. {
  214. if (udc->pde)
  215. remove_proc_entry(debug_filename, NULL);
  216. }
  217. #else
  218. static inline void create_debug_file(struct at91_udc *udc) {}
  219. static inline void remove_debug_file(struct at91_udc *udc) {}
  220. #endif
  221. /*-------------------------------------------------------------------------*/
  222. static void done(struct at91_ep *ep, struct at91_request *req, int status)
  223. {
  224. unsigned stopped = ep->stopped;
  225. struct at91_udc *udc = ep->udc;
  226. list_del_init(&req->queue);
  227. if (req->req.status == -EINPROGRESS)
  228. req->req.status = status;
  229. else
  230. status = req->req.status;
  231. if (status && status != -ESHUTDOWN)
  232. VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
  233. ep->stopped = 1;
  234. req->req.complete(&ep->ep, &req->req);
  235. ep->stopped = stopped;
  236. /* ep0 is always ready; other endpoints need a non-empty queue */
  237. if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
  238. at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
  239. }
  240. /*-------------------------------------------------------------------------*/
  241. /* bits indicating OUT fifo has data ready */
  242. #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
  243. /*
  244. * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
  245. * back most of the value you just read (because of side effects, including
  246. * bits that may change after reading and before writing).
  247. *
  248. * Except when changing a specific bit, always write values which:
  249. * - clear SET_FX bits (setting them could change something)
  250. * - set CLR_FX bits (clearing them could change something)
  251. *
  252. * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
  253. * that shouldn't normally be changed.
  254. *
  255. * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
  256. * implying a need to wait for one write to complete (test relevant bits)
  257. * before starting the next write. This shouldn't be an issue given how
  258. * infrequently we write, except maybe for write-then-read idioms.
  259. */
  260. #define SET_FX (AT91_UDP_TXPKTRDY)
  261. #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
  262. | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
  263. /* pull OUT packet data from the endpoint's fifo */
  264. static int read_fifo (struct at91_ep *ep, struct at91_request *req)
  265. {
  266. u32 __iomem *creg = ep->creg;
  267. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  268. u32 csr;
  269. u8 *buf;
  270. unsigned int count, bufferspace, is_done;
  271. buf = req->req.buf + req->req.actual;
  272. bufferspace = req->req.length - req->req.actual;
  273. /*
  274. * there might be nothing to read if ep_queue() calls us,
  275. * or if we already emptied both pingpong buffers
  276. */
  277. rescan:
  278. csr = __raw_readl(creg);
  279. if ((csr & RX_DATA_READY) == 0)
  280. return 0;
  281. count = (csr & AT91_UDP_RXBYTECNT) >> 16;
  282. if (count > ep->ep.maxpacket)
  283. count = ep->ep.maxpacket;
  284. if (count > bufferspace) {
  285. DBG("%s buffer overflow\n", ep->ep.name);
  286. req->req.status = -EOVERFLOW;
  287. count = bufferspace;
  288. }
  289. __raw_readsb(dreg, buf, count);
  290. /* release and swap pingpong mem bank */
  291. csr |= CLR_FX;
  292. if (ep->is_pingpong) {
  293. if (ep->fifo_bank == 0) {
  294. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  295. ep->fifo_bank = 1;
  296. } else {
  297. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
  298. ep->fifo_bank = 0;
  299. }
  300. } else
  301. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  302. __raw_writel(csr, creg);
  303. req->req.actual += count;
  304. is_done = (count < ep->ep.maxpacket);
  305. if (count == bufferspace)
  306. is_done = 1;
  307. PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
  308. is_done ? " (done)" : "");
  309. /*
  310. * avoid extra trips through IRQ logic for packets already in
  311. * the fifo ... maybe preventing an extra (expensive) OUT-NAK
  312. */
  313. if (is_done)
  314. done(ep, req, 0);
  315. else if (ep->is_pingpong) {
  316. bufferspace -= count;
  317. buf += count;
  318. goto rescan;
  319. }
  320. return is_done;
  321. }
  322. /* load fifo for an IN packet */
  323. static int write_fifo(struct at91_ep *ep, struct at91_request *req)
  324. {
  325. u32 __iomem *creg = ep->creg;
  326. u32 csr = __raw_readl(creg);
  327. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  328. unsigned total, count, is_last;
  329. /*
  330. * TODO: allow for writing two packets to the fifo ... that'll
  331. * reduce the amount of IN-NAKing, but probably won't affect
  332. * throughput much. (Unlike preventing OUT-NAKing!)
  333. */
  334. /*
  335. * If ep_queue() calls us, the queue is empty and possibly in
  336. * odd states like TXCOMP not yet cleared (we do it, saving at
  337. * least one IRQ) or the fifo not yet being free. Those aren't
  338. * issues normally (IRQ handler fast path).
  339. */
  340. if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
  341. if (csr & AT91_UDP_TXCOMP) {
  342. csr |= CLR_FX;
  343. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  344. __raw_writel(csr, creg);
  345. csr = __raw_readl(creg);
  346. }
  347. if (csr & AT91_UDP_TXPKTRDY)
  348. return 0;
  349. }
  350. total = req->req.length - req->req.actual;
  351. if (ep->ep.maxpacket < total) {
  352. count = ep->ep.maxpacket;
  353. is_last = 0;
  354. } else {
  355. count = total;
  356. is_last = (count < ep->ep.maxpacket) || !req->req.zero;
  357. }
  358. /*
  359. * Write the packet, maybe it's a ZLP.
  360. *
  361. * NOTE: incrementing req->actual before we receive the ACK means
  362. * gadget driver IN bytecounts can be wrong in fault cases. That's
  363. * fixable with PIO drivers like this one (save "count" here, and
  364. * do the increment later on TX irq), but not for most DMA hardware.
  365. *
  366. * So all gadget drivers must accept that potential error. Some
  367. * hardware supports precise fifo status reporting, letting them
  368. * recover when the actual bytecount matters (e.g. for USB Test
  369. * and Measurement Class devices).
  370. */
  371. __raw_writesb(dreg, req->req.buf + req->req.actual, count);
  372. csr &= ~SET_FX;
  373. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  374. __raw_writel(csr, creg);
  375. req->req.actual += count;
  376. PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
  377. is_last ? " (done)" : "");
  378. if (is_last)
  379. done(ep, req, 0);
  380. return is_last;
  381. }
  382. static void nuke(struct at91_ep *ep, int status)
  383. {
  384. struct at91_request *req;
  385. // terminer chaque requete dans la queue
  386. ep->stopped = 1;
  387. if (list_empty(&ep->queue))
  388. return;
  389. VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
  390. while (!list_empty(&ep->queue)) {
  391. req = list_entry(ep->queue.next, struct at91_request, queue);
  392. done(ep, req, status);
  393. }
  394. }
  395. /*-------------------------------------------------------------------------*/
  396. static int at91_ep_enable(struct usb_ep *_ep,
  397. const struct usb_endpoint_descriptor *desc)
  398. {
  399. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  400. struct at91_udc *dev = ep->udc;
  401. u16 maxpacket;
  402. u32 tmp;
  403. unsigned long flags;
  404. if (!_ep || !ep
  405. || !desc || ep->desc
  406. || _ep->name == ep0name
  407. || desc->bDescriptorType != USB_DT_ENDPOINT
  408. || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
  409. || maxpacket > ep->maxpacket) {
  410. DBG("bad ep or descriptor\n");
  411. return -EINVAL;
  412. }
  413. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  414. DBG("bogus device state\n");
  415. return -ESHUTDOWN;
  416. }
  417. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  418. switch (tmp) {
  419. case USB_ENDPOINT_XFER_CONTROL:
  420. DBG("only one control endpoint\n");
  421. return -EINVAL;
  422. case USB_ENDPOINT_XFER_INT:
  423. if (maxpacket > 64)
  424. goto bogus_max;
  425. break;
  426. case USB_ENDPOINT_XFER_BULK:
  427. switch (maxpacket) {
  428. case 8:
  429. case 16:
  430. case 32:
  431. case 64:
  432. goto ok;
  433. }
  434. bogus_max:
  435. DBG("bogus maxpacket %d\n", maxpacket);
  436. return -EINVAL;
  437. case USB_ENDPOINT_XFER_ISOC:
  438. if (!ep->is_pingpong) {
  439. DBG("iso requires double buffering\n");
  440. return -EINVAL;
  441. }
  442. break;
  443. }
  444. ok:
  445. local_irq_save(flags);
  446. /* initialize endpoint to match this descriptor */
  447. ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  448. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
  449. ep->stopped = 0;
  450. if (ep->is_in)
  451. tmp |= 0x04;
  452. tmp <<= 8;
  453. tmp |= AT91_UDP_EPEDS;
  454. __raw_writel(tmp, ep->creg);
  455. ep->desc = desc;
  456. ep->ep.maxpacket = maxpacket;
  457. /*
  458. * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
  459. * since endpoint resets don't reset hw pingpong state.
  460. */
  461. at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
  462. at91_udp_write(dev, AT91_UDP_RST_EP, 0);
  463. local_irq_restore(flags);
  464. return 0;
  465. }
  466. static int at91_ep_disable (struct usb_ep * _ep)
  467. {
  468. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  469. struct at91_udc *udc = ep->udc;
  470. unsigned long flags;
  471. if (ep == &ep->udc->ep[0])
  472. return -EINVAL;
  473. local_irq_save(flags);
  474. nuke(ep, -ESHUTDOWN);
  475. /* restore the endpoint's pristine config */
  476. ep->desc = NULL;
  477. ep->ep.maxpacket = ep->maxpacket;
  478. /* reset fifos and endpoint */
  479. if (ep->udc->clocked) {
  480. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  481. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  482. __raw_writel(0, ep->creg);
  483. }
  484. local_irq_restore(flags);
  485. return 0;
  486. }
  487. /*
  488. * this is a PIO-only driver, so there's nothing
  489. * interesting for request or buffer allocation.
  490. */
  491. static struct usb_request *
  492. at91_ep_alloc_request(struct usb_ep *_ep, unsigned int gfp_flags)
  493. {
  494. struct at91_request *req;
  495. req = kzalloc(sizeof (struct at91_request), gfp_flags);
  496. if (!req)
  497. return NULL;
  498. INIT_LIST_HEAD(&req->queue);
  499. return &req->req;
  500. }
  501. static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  502. {
  503. struct at91_request *req;
  504. req = container_of(_req, struct at91_request, req);
  505. BUG_ON(!list_empty(&req->queue));
  506. kfree(req);
  507. }
  508. static void *at91_ep_alloc_buffer(
  509. struct usb_ep *_ep,
  510. unsigned bytes,
  511. dma_addr_t *dma,
  512. gfp_t gfp_flags)
  513. {
  514. *dma = ~0;
  515. return kmalloc(bytes, gfp_flags);
  516. }
  517. static void at91_ep_free_buffer(
  518. struct usb_ep *ep,
  519. void *buf,
  520. dma_addr_t dma,
  521. unsigned bytes)
  522. {
  523. kfree(buf);
  524. }
  525. static int at91_ep_queue(struct usb_ep *_ep,
  526. struct usb_request *_req, gfp_t gfp_flags)
  527. {
  528. struct at91_request *req;
  529. struct at91_ep *ep;
  530. struct at91_udc *dev;
  531. int status;
  532. unsigned long flags;
  533. req = container_of(_req, struct at91_request, req);
  534. ep = container_of(_ep, struct at91_ep, ep);
  535. if (!_req || !_req->complete
  536. || !_req->buf || !list_empty(&req->queue)) {
  537. DBG("invalid request\n");
  538. return -EINVAL;
  539. }
  540. if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
  541. DBG("invalid ep\n");
  542. return -EINVAL;
  543. }
  544. dev = ep->udc;
  545. if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  546. DBG("invalid device\n");
  547. return -EINVAL;
  548. }
  549. _req->status = -EINPROGRESS;
  550. _req->actual = 0;
  551. local_irq_save(flags);
  552. /* try to kickstart any empty and idle queue */
  553. if (list_empty(&ep->queue) && !ep->stopped) {
  554. int is_ep0;
  555. /*
  556. * If this control request has a non-empty DATA stage, this
  557. * will start that stage. It works just like a non-control
  558. * request (until the status stage starts, maybe early).
  559. *
  560. * If the data stage is empty, then this starts a successful
  561. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  562. */
  563. is_ep0 = (ep->ep.name == ep0name);
  564. if (is_ep0) {
  565. u32 tmp;
  566. if (!dev->req_pending) {
  567. status = -EINVAL;
  568. goto done;
  569. }
  570. /*
  571. * defer changing CONFG until after the gadget driver
  572. * reconfigures the endpoints.
  573. */
  574. if (dev->wait_for_config_ack) {
  575. tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
  576. tmp ^= AT91_UDP_CONFG;
  577. VDBG("toggle config\n");
  578. at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
  579. }
  580. if (req->req.length == 0) {
  581. ep0_in_status:
  582. PACKET("ep0 in/status\n");
  583. status = 0;
  584. tmp = __raw_readl(ep->creg);
  585. tmp &= ~SET_FX;
  586. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  587. __raw_writel(tmp, ep->creg);
  588. dev->req_pending = 0;
  589. goto done;
  590. }
  591. }
  592. if (ep->is_in)
  593. status = write_fifo(ep, req);
  594. else {
  595. status = read_fifo(ep, req);
  596. /* IN/STATUS stage is otherwise triggered by irq */
  597. if (status && is_ep0)
  598. goto ep0_in_status;
  599. }
  600. } else
  601. status = 0;
  602. if (req && !status) {
  603. list_add_tail (&req->queue, &ep->queue);
  604. at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
  605. }
  606. done:
  607. local_irq_restore(flags);
  608. return (status < 0) ? status : 0;
  609. }
  610. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  611. {
  612. struct at91_ep *ep;
  613. struct at91_request *req;
  614. ep = container_of(_ep, struct at91_ep, ep);
  615. if (!_ep || ep->ep.name == ep0name)
  616. return -EINVAL;
  617. /* make sure it's actually queued on this endpoint */
  618. list_for_each_entry (req, &ep->queue, queue) {
  619. if (&req->req == _req)
  620. break;
  621. }
  622. if (&req->req != _req)
  623. return -EINVAL;
  624. done(ep, req, -ECONNRESET);
  625. return 0;
  626. }
  627. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  628. {
  629. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  630. struct at91_udc *udc = ep->udc;
  631. u32 __iomem *creg;
  632. u32 csr;
  633. unsigned long flags;
  634. int status = 0;
  635. if (!_ep || ep->is_iso || !ep->udc->clocked)
  636. return -EINVAL;
  637. creg = ep->creg;
  638. local_irq_save(flags);
  639. csr = __raw_readl(creg);
  640. /*
  641. * fail with still-busy IN endpoints, ensuring correct sequencing
  642. * of data tx then stall. note that the fifo rx bytecount isn't
  643. * completely accurate as a tx bytecount.
  644. */
  645. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  646. status = -EAGAIN;
  647. else {
  648. csr |= CLR_FX;
  649. csr &= ~SET_FX;
  650. if (value) {
  651. csr |= AT91_UDP_FORCESTALL;
  652. VDBG("halt %s\n", ep->ep.name);
  653. } else {
  654. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  655. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  656. csr &= ~AT91_UDP_FORCESTALL;
  657. }
  658. __raw_writel(csr, creg);
  659. }
  660. local_irq_restore(flags);
  661. return status;
  662. }
  663. static struct usb_ep_ops at91_ep_ops = {
  664. .enable = at91_ep_enable,
  665. .disable = at91_ep_disable,
  666. .alloc_request = at91_ep_alloc_request,
  667. .free_request = at91_ep_free_request,
  668. .alloc_buffer = at91_ep_alloc_buffer,
  669. .free_buffer = at91_ep_free_buffer,
  670. .queue = at91_ep_queue,
  671. .dequeue = at91_ep_dequeue,
  672. .set_halt = at91_ep_set_halt,
  673. // there's only imprecise fifo status reporting
  674. };
  675. /*-------------------------------------------------------------------------*/
  676. static int at91_get_frame(struct usb_gadget *gadget)
  677. {
  678. struct at91_udc *udc = to_udc(gadget);
  679. if (!to_udc(gadget)->clocked)
  680. return -EINVAL;
  681. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  682. }
  683. static int at91_wakeup(struct usb_gadget *gadget)
  684. {
  685. struct at91_udc *udc = to_udc(gadget);
  686. u32 glbstate;
  687. int status = -EINVAL;
  688. unsigned long flags;
  689. DBG("%s\n", __FUNCTION__ );
  690. local_irq_save(flags);
  691. if (!udc->clocked || !udc->suspended)
  692. goto done;
  693. /* NOTE: some "early versions" handle ESR differently ... */
  694. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  695. if (!(glbstate & AT91_UDP_ESR))
  696. goto done;
  697. glbstate |= AT91_UDP_ESR;
  698. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  699. done:
  700. local_irq_restore(flags);
  701. return status;
  702. }
  703. /* reinit == restore inital software state */
  704. static void udc_reinit(struct at91_udc *udc)
  705. {
  706. u32 i;
  707. INIT_LIST_HEAD(&udc->gadget.ep_list);
  708. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  709. for (i = 0; i < NUM_ENDPOINTS; i++) {
  710. struct at91_ep *ep = &udc->ep[i];
  711. if (i != 0)
  712. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  713. ep->desc = NULL;
  714. ep->stopped = 0;
  715. ep->fifo_bank = 0;
  716. ep->ep.maxpacket = ep->maxpacket;
  717. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  718. // initialiser une queue par endpoint
  719. INIT_LIST_HEAD(&ep->queue);
  720. }
  721. }
  722. static void stop_activity(struct at91_udc *udc)
  723. {
  724. struct usb_gadget_driver *driver = udc->driver;
  725. int i;
  726. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  727. driver = NULL;
  728. udc->gadget.speed = USB_SPEED_UNKNOWN;
  729. udc->suspended = 0;
  730. for (i = 0; i < NUM_ENDPOINTS; i++) {
  731. struct at91_ep *ep = &udc->ep[i];
  732. ep->stopped = 1;
  733. nuke(ep, -ESHUTDOWN);
  734. }
  735. if (driver)
  736. driver->disconnect(&udc->gadget);
  737. udc_reinit(udc);
  738. }
  739. static void clk_on(struct at91_udc *udc)
  740. {
  741. if (udc->clocked)
  742. return;
  743. udc->clocked = 1;
  744. clk_enable(udc->iclk);
  745. clk_enable(udc->fclk);
  746. }
  747. static void clk_off(struct at91_udc *udc)
  748. {
  749. if (!udc->clocked)
  750. return;
  751. udc->clocked = 0;
  752. udc->gadget.speed = USB_SPEED_UNKNOWN;
  753. clk_disable(udc->fclk);
  754. clk_disable(udc->iclk);
  755. }
  756. /*
  757. * activate/deactivate link with host; minimize power usage for
  758. * inactive links by cutting clocks and transceiver power.
  759. */
  760. static void pullup(struct at91_udc *udc, int is_on)
  761. {
  762. if (!udc->enabled || !udc->vbus)
  763. is_on = 0;
  764. DBG("%sactive\n", is_on ? "" : "in");
  765. if (is_on) {
  766. clk_on(udc);
  767. at91_udp_write(udc, AT91_UDP_TXVC, 0);
  768. if (cpu_is_at91rm9200())
  769. at91_set_gpio_value(udc->board.pullup_pin, 1);
  770. else if (cpu_is_at91sam9260()) {
  771. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  772. txvc |= AT91_UDP_TXVC_PUON;
  773. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  774. } else if (cpu_is_at91sam9261()) {
  775. u32 usbpucr;
  776. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  777. usbpucr |= AT91_MATRIX_USBPUCR_PUON;
  778. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  779. }
  780. } else {
  781. stop_activity(udc);
  782. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  783. if (cpu_is_at91rm9200())
  784. at91_set_gpio_value(udc->board.pullup_pin, 0);
  785. else if (cpu_is_at91sam9260()) {
  786. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  787. txvc &= ~AT91_UDP_TXVC_PUON;
  788. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  789. } else if (cpu_is_at91sam9261()) {
  790. u32 usbpucr;
  791. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  792. usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
  793. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  794. }
  795. clk_off(udc);
  796. }
  797. }
  798. /* vbus is here! turn everything on that's ready */
  799. static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
  800. {
  801. struct at91_udc *udc = to_udc(gadget);
  802. unsigned long flags;
  803. // VDBG("vbus %s\n", is_active ? "on" : "off");
  804. local_irq_save(flags);
  805. udc->vbus = (is_active != 0);
  806. if (udc->driver)
  807. pullup(udc, is_active);
  808. else
  809. pullup(udc, 0);
  810. local_irq_restore(flags);
  811. return 0;
  812. }
  813. static int at91_pullup(struct usb_gadget *gadget, int is_on)
  814. {
  815. struct at91_udc *udc = to_udc(gadget);
  816. unsigned long flags;
  817. local_irq_save(flags);
  818. udc->enabled = is_on = !!is_on;
  819. pullup(udc, is_on);
  820. local_irq_restore(flags);
  821. return 0;
  822. }
  823. static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
  824. {
  825. struct at91_udc *udc = to_udc(gadget);
  826. unsigned long flags;
  827. local_irq_save(flags);
  828. udc->selfpowered = (is_on != 0);
  829. local_irq_restore(flags);
  830. return 0;
  831. }
  832. static const struct usb_gadget_ops at91_udc_ops = {
  833. .get_frame = at91_get_frame,
  834. .wakeup = at91_wakeup,
  835. .set_selfpowered = at91_set_selfpowered,
  836. .vbus_session = at91_vbus_session,
  837. .pullup = at91_pullup,
  838. /*
  839. * VBUS-powered devices may also also want to support bigger
  840. * power budgets after an appropriate SET_CONFIGURATION.
  841. */
  842. // .vbus_power = at91_vbus_power,
  843. };
  844. /*-------------------------------------------------------------------------*/
  845. static int handle_ep(struct at91_ep *ep)
  846. {
  847. struct at91_request *req;
  848. u32 __iomem *creg = ep->creg;
  849. u32 csr = __raw_readl(creg);
  850. if (!list_empty(&ep->queue))
  851. req = list_entry(ep->queue.next,
  852. struct at91_request, queue);
  853. else
  854. req = NULL;
  855. if (ep->is_in) {
  856. if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
  857. csr |= CLR_FX;
  858. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
  859. __raw_writel(csr, creg);
  860. }
  861. if (req)
  862. return write_fifo(ep, req);
  863. } else {
  864. if (csr & AT91_UDP_STALLSENT) {
  865. /* STALLSENT bit == ISOERR */
  866. if (ep->is_iso && req)
  867. req->req.status = -EILSEQ;
  868. csr |= CLR_FX;
  869. csr &= ~(SET_FX | AT91_UDP_STALLSENT);
  870. __raw_writel(csr, creg);
  871. csr = __raw_readl(creg);
  872. }
  873. if (req && (csr & RX_DATA_READY))
  874. return read_fifo(ep, req);
  875. }
  876. return 0;
  877. }
  878. union setup {
  879. u8 raw[8];
  880. struct usb_ctrlrequest r;
  881. };
  882. static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
  883. {
  884. u32 __iomem *creg = ep->creg;
  885. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  886. unsigned rxcount, i = 0;
  887. u32 tmp;
  888. union setup pkt;
  889. int status = 0;
  890. /* read and ack SETUP; hard-fail for bogus packets */
  891. rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
  892. if (likely(rxcount == 8)) {
  893. while (rxcount--)
  894. pkt.raw[i++] = __raw_readb(dreg);
  895. if (pkt.r.bRequestType & USB_DIR_IN) {
  896. csr |= AT91_UDP_DIR;
  897. ep->is_in = 1;
  898. } else {
  899. csr &= ~AT91_UDP_DIR;
  900. ep->is_in = 0;
  901. }
  902. } else {
  903. // REVISIT this happens sometimes under load; why??
  904. ERR("SETUP len %d, csr %08x\n", rxcount, csr);
  905. status = -EINVAL;
  906. }
  907. csr |= CLR_FX;
  908. csr &= ~(SET_FX | AT91_UDP_RXSETUP);
  909. __raw_writel(csr, creg);
  910. udc->wait_for_addr_ack = 0;
  911. udc->wait_for_config_ack = 0;
  912. ep->stopped = 0;
  913. if (unlikely(status != 0))
  914. goto stall;
  915. #define w_index le16_to_cpu(pkt.r.wIndex)
  916. #define w_value le16_to_cpu(pkt.r.wValue)
  917. #define w_length le16_to_cpu(pkt.r.wLength)
  918. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  919. pkt.r.bRequestType, pkt.r.bRequest,
  920. w_value, w_index, w_length);
  921. /*
  922. * A few standard requests get handled here, ones that touch
  923. * hardware ... notably for device and endpoint features.
  924. */
  925. udc->req_pending = 1;
  926. csr = __raw_readl(creg);
  927. csr |= CLR_FX;
  928. csr &= ~SET_FX;
  929. switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
  930. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  931. | USB_REQ_SET_ADDRESS:
  932. __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
  933. udc->addr = w_value;
  934. udc->wait_for_addr_ack = 1;
  935. udc->req_pending = 0;
  936. /* FADDR is set later, when we ack host STATUS */
  937. return;
  938. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  939. | USB_REQ_SET_CONFIGURATION:
  940. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
  941. if (pkt.r.wValue)
  942. udc->wait_for_config_ack = (tmp == 0);
  943. else
  944. udc->wait_for_config_ack = (tmp != 0);
  945. if (udc->wait_for_config_ack)
  946. VDBG("wait for config\n");
  947. /* CONFG is toggled later, if gadget driver succeeds */
  948. break;
  949. /*
  950. * Hosts may set or clear remote wakeup status, and
  951. * devices may report they're VBUS powered.
  952. */
  953. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  954. | USB_REQ_GET_STATUS:
  955. tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  956. if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
  957. tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  958. PACKET("get device status\n");
  959. __raw_writeb(tmp, dreg);
  960. __raw_writeb(0, dreg);
  961. goto write_in;
  962. /* then STATUS starts later, automatically */
  963. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  964. | USB_REQ_SET_FEATURE:
  965. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  966. goto stall;
  967. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  968. tmp |= AT91_UDP_ESR;
  969. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  970. goto succeed;
  971. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  972. | USB_REQ_CLEAR_FEATURE:
  973. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  974. goto stall;
  975. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  976. tmp &= ~AT91_UDP_ESR;
  977. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  978. goto succeed;
  979. /*
  980. * Interfaces have no feature settings; this is pretty useless.
  981. * we won't even insist the interface exists...
  982. */
  983. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  984. | USB_REQ_GET_STATUS:
  985. PACKET("get interface status\n");
  986. __raw_writeb(0, dreg);
  987. __raw_writeb(0, dreg);
  988. goto write_in;
  989. /* then STATUS starts later, automatically */
  990. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  991. | USB_REQ_SET_FEATURE:
  992. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  993. | USB_REQ_CLEAR_FEATURE:
  994. goto stall;
  995. /*
  996. * Hosts may clear bulk/intr endpoint halt after the gadget
  997. * driver sets it (not widely used); or set it (for testing)
  998. */
  999. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1000. | USB_REQ_GET_STATUS:
  1001. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1002. ep = &udc->ep[tmp];
  1003. if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc))
  1004. goto stall;
  1005. if (tmp) {
  1006. if ((w_index & USB_DIR_IN)) {
  1007. if (!ep->is_in)
  1008. goto stall;
  1009. } else if (ep->is_in)
  1010. goto stall;
  1011. }
  1012. PACKET("get %s status\n", ep->ep.name);
  1013. if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
  1014. tmp = (1 << USB_ENDPOINT_HALT);
  1015. else
  1016. tmp = 0;
  1017. __raw_writeb(tmp, dreg);
  1018. __raw_writeb(0, dreg);
  1019. goto write_in;
  1020. /* then STATUS starts later, automatically */
  1021. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1022. | USB_REQ_SET_FEATURE:
  1023. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1024. ep = &udc->ep[tmp];
  1025. if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
  1026. goto stall;
  1027. if (!ep->desc || ep->is_iso)
  1028. goto stall;
  1029. if ((w_index & USB_DIR_IN)) {
  1030. if (!ep->is_in)
  1031. goto stall;
  1032. } else if (ep->is_in)
  1033. goto stall;
  1034. tmp = __raw_readl(ep->creg);
  1035. tmp &= ~SET_FX;
  1036. tmp |= CLR_FX | AT91_UDP_FORCESTALL;
  1037. __raw_writel(tmp, ep->creg);
  1038. goto succeed;
  1039. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1040. | USB_REQ_CLEAR_FEATURE:
  1041. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1042. ep = &udc->ep[tmp];
  1043. if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
  1044. goto stall;
  1045. if (tmp == 0)
  1046. goto succeed;
  1047. if (!ep->desc || ep->is_iso)
  1048. goto stall;
  1049. if ((w_index & USB_DIR_IN)) {
  1050. if (!ep->is_in)
  1051. goto stall;
  1052. } else if (ep->is_in)
  1053. goto stall;
  1054. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  1055. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  1056. tmp = __raw_readl(ep->creg);
  1057. tmp |= CLR_FX;
  1058. tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
  1059. __raw_writel(tmp, ep->creg);
  1060. if (!list_empty(&ep->queue))
  1061. handle_ep(ep);
  1062. goto succeed;
  1063. }
  1064. #undef w_value
  1065. #undef w_index
  1066. #undef w_length
  1067. /* pass request up to the gadget driver */
  1068. if (udc->driver)
  1069. status = udc->driver->setup(&udc->gadget, &pkt.r);
  1070. else
  1071. status = -ENODEV;
  1072. if (status < 0) {
  1073. stall:
  1074. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1075. pkt.r.bRequestType, pkt.r.bRequest, status);
  1076. csr |= AT91_UDP_FORCESTALL;
  1077. __raw_writel(csr, creg);
  1078. udc->req_pending = 0;
  1079. }
  1080. return;
  1081. succeed:
  1082. /* immediate successful (IN) STATUS after zero length DATA */
  1083. PACKET("ep0 in/status\n");
  1084. write_in:
  1085. csr |= AT91_UDP_TXPKTRDY;
  1086. __raw_writel(csr, creg);
  1087. udc->req_pending = 0;
  1088. return;
  1089. }
  1090. static void handle_ep0(struct at91_udc *udc)
  1091. {
  1092. struct at91_ep *ep0 = &udc->ep[0];
  1093. u32 __iomem *creg = ep0->creg;
  1094. u32 csr = __raw_readl(creg);
  1095. struct at91_request *req;
  1096. if (unlikely(csr & AT91_UDP_STALLSENT)) {
  1097. nuke(ep0, -EPROTO);
  1098. udc->req_pending = 0;
  1099. csr |= CLR_FX;
  1100. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
  1101. __raw_writel(csr, creg);
  1102. VDBG("ep0 stalled\n");
  1103. csr = __raw_readl(creg);
  1104. }
  1105. if (csr & AT91_UDP_RXSETUP) {
  1106. nuke(ep0, 0);
  1107. udc->req_pending = 0;
  1108. handle_setup(udc, ep0, csr);
  1109. return;
  1110. }
  1111. if (list_empty(&ep0->queue))
  1112. req = NULL;
  1113. else
  1114. req = list_entry(ep0->queue.next, struct at91_request, queue);
  1115. /* host ACKed an IN packet that we sent */
  1116. if (csr & AT91_UDP_TXCOMP) {
  1117. csr |= CLR_FX;
  1118. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  1119. /* write more IN DATA? */
  1120. if (req && ep0->is_in) {
  1121. if (handle_ep(ep0))
  1122. udc->req_pending = 0;
  1123. /*
  1124. * Ack after:
  1125. * - last IN DATA packet (including GET_STATUS)
  1126. * - IN/STATUS for OUT DATA
  1127. * - IN/STATUS for any zero-length DATA stage
  1128. * except for the IN DATA case, the host should send
  1129. * an OUT status later, which we'll ack.
  1130. */
  1131. } else {
  1132. udc->req_pending = 0;
  1133. __raw_writel(csr, creg);
  1134. /*
  1135. * SET_ADDRESS takes effect only after the STATUS
  1136. * (to the original address) gets acked.
  1137. */
  1138. if (udc->wait_for_addr_ack) {
  1139. u32 tmp;
  1140. at91_udp_write(udc, AT91_UDP_FADDR,
  1141. AT91_UDP_FEN | udc->addr);
  1142. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  1143. tmp &= ~AT91_UDP_FADDEN;
  1144. if (udc->addr)
  1145. tmp |= AT91_UDP_FADDEN;
  1146. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  1147. udc->wait_for_addr_ack = 0;
  1148. VDBG("address %d\n", udc->addr);
  1149. }
  1150. }
  1151. }
  1152. /* OUT packet arrived ... */
  1153. else if (csr & AT91_UDP_RX_DATA_BK0) {
  1154. csr |= CLR_FX;
  1155. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  1156. /* OUT DATA stage */
  1157. if (!ep0->is_in) {
  1158. if (req) {
  1159. if (handle_ep(ep0)) {
  1160. /* send IN/STATUS */
  1161. PACKET("ep0 in/status\n");
  1162. csr = __raw_readl(creg);
  1163. csr &= ~SET_FX;
  1164. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  1165. __raw_writel(csr, creg);
  1166. udc->req_pending = 0;
  1167. }
  1168. } else if (udc->req_pending) {
  1169. /*
  1170. * AT91 hardware has a hard time with this
  1171. * "deferred response" mode for control-OUT
  1172. * transfers. (For control-IN it's fine.)
  1173. *
  1174. * The normal solution leaves OUT data in the
  1175. * fifo until the gadget driver is ready.
  1176. * We couldn't do that here without disabling
  1177. * the IRQ that tells about SETUP packets,
  1178. * e.g. when the host gets impatient...
  1179. *
  1180. * Working around it by copying into a buffer
  1181. * would almost be a non-deferred response,
  1182. * except that it wouldn't permit reliable
  1183. * stalling of the request. Instead, demand
  1184. * that gadget drivers not use this mode.
  1185. */
  1186. DBG("no control-OUT deferred responses!\n");
  1187. __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
  1188. udc->req_pending = 0;
  1189. }
  1190. /* STATUS stage for control-IN; ack. */
  1191. } else {
  1192. PACKET("ep0 out/status ACK\n");
  1193. __raw_writel(csr, creg);
  1194. /* "early" status stage */
  1195. if (req)
  1196. done(ep0, req, 0);
  1197. }
  1198. }
  1199. }
  1200. static irqreturn_t at91_udc_irq (int irq, void *_udc)
  1201. {
  1202. struct at91_udc *udc = _udc;
  1203. u32 rescans = 5;
  1204. while (rescans--) {
  1205. u32 status;
  1206. status = at91_udp_read(udc, AT91_UDP_ISR)
  1207. & at91_udp_read(udc, AT91_UDP_IMR);
  1208. if (!status)
  1209. break;
  1210. /* USB reset irq: not maskable */
  1211. if (status & AT91_UDP_ENDBUSRES) {
  1212. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1213. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1214. /* Atmel code clears this irq twice */
  1215. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1216. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1217. VDBG("end bus reset\n");
  1218. udc->addr = 0;
  1219. stop_activity(udc);
  1220. /* enable ep0 */
  1221. at91_udp_write(udc, AT91_UDP_CSR(0),
  1222. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1223. udc->gadget.speed = USB_SPEED_FULL;
  1224. udc->suspended = 0;
  1225. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1226. /*
  1227. * NOTE: this driver keeps clocks off unless the
  1228. * USB host is present. That saves power, but for
  1229. * boards that don't support VBUS detection, both
  1230. * clocks need to be active most of the time.
  1231. */
  1232. /* host initiated suspend (3+ms bus idle) */
  1233. } else if (status & AT91_UDP_RXSUSP) {
  1234. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1235. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1236. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1237. // VDBG("bus suspend\n");
  1238. if (udc->suspended)
  1239. continue;
  1240. udc->suspended = 1;
  1241. /*
  1242. * NOTE: when suspending a VBUS-powered device, the
  1243. * gadget driver should switch into slow clock mode
  1244. * and then into standby to avoid drawing more than
  1245. * 500uA power (2500uA for some high-power configs).
  1246. */
  1247. if (udc->driver && udc->driver->suspend)
  1248. udc->driver->suspend(&udc->gadget);
  1249. /* host initiated resume */
  1250. } else if (status & AT91_UDP_RXRSM) {
  1251. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1252. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1253. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1254. // VDBG("bus resume\n");
  1255. if (!udc->suspended)
  1256. continue;
  1257. udc->suspended = 0;
  1258. /*
  1259. * NOTE: for a VBUS-powered device, the gadget driver
  1260. * would normally want to switch out of slow clock
  1261. * mode into normal mode.
  1262. */
  1263. if (udc->driver && udc->driver->resume)
  1264. udc->driver->resume(&udc->gadget);
  1265. /* endpoint IRQs are cleared by handling them */
  1266. } else {
  1267. int i;
  1268. unsigned mask = 1;
  1269. struct at91_ep *ep = &udc->ep[1];
  1270. if (status & mask)
  1271. handle_ep0(udc);
  1272. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1273. mask <<= 1;
  1274. if (status & mask)
  1275. handle_ep(ep);
  1276. ep++;
  1277. }
  1278. }
  1279. }
  1280. return IRQ_HANDLED;
  1281. }
  1282. /*-------------------------------------------------------------------------*/
  1283. static void nop_release(struct device *dev)
  1284. {
  1285. /* nothing to free */
  1286. }
  1287. static struct at91_udc controller = {
  1288. .gadget = {
  1289. .ops = &at91_udc_ops,
  1290. .ep0 = &controller.ep[0].ep,
  1291. .name = driver_name,
  1292. .dev = {
  1293. .bus_id = "gadget",
  1294. .release = nop_release,
  1295. }
  1296. },
  1297. .ep[0] = {
  1298. .ep = {
  1299. .name = ep0name,
  1300. .ops = &at91_ep_ops,
  1301. },
  1302. .udc = &controller,
  1303. .maxpacket = 8,
  1304. .int_mask = 1 << 0,
  1305. },
  1306. .ep[1] = {
  1307. .ep = {
  1308. .name = "ep1",
  1309. .ops = &at91_ep_ops,
  1310. },
  1311. .udc = &controller,
  1312. .is_pingpong = 1,
  1313. .maxpacket = 64,
  1314. .int_mask = 1 << 1,
  1315. },
  1316. .ep[2] = {
  1317. .ep = {
  1318. .name = "ep2",
  1319. .ops = &at91_ep_ops,
  1320. },
  1321. .udc = &controller,
  1322. .is_pingpong = 1,
  1323. .maxpacket = 64,
  1324. .int_mask = 1 << 2,
  1325. },
  1326. .ep[3] = {
  1327. .ep = {
  1328. /* could actually do bulk too */
  1329. .name = "ep3-int",
  1330. .ops = &at91_ep_ops,
  1331. },
  1332. .udc = &controller,
  1333. .maxpacket = 8,
  1334. .int_mask = 1 << 3,
  1335. },
  1336. .ep[4] = {
  1337. .ep = {
  1338. .name = "ep4",
  1339. .ops = &at91_ep_ops,
  1340. },
  1341. .udc = &controller,
  1342. .is_pingpong = 1,
  1343. .maxpacket = 256,
  1344. .int_mask = 1 << 4,
  1345. },
  1346. .ep[5] = {
  1347. .ep = {
  1348. .name = "ep5",
  1349. .ops = &at91_ep_ops,
  1350. },
  1351. .udc = &controller,
  1352. .is_pingpong = 1,
  1353. .maxpacket = 256,
  1354. .int_mask = 1 << 5,
  1355. },
  1356. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1357. };
  1358. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1359. {
  1360. struct at91_udc *udc = _udc;
  1361. unsigned value;
  1362. /* vbus needs at least brief debouncing */
  1363. udelay(10);
  1364. value = at91_get_gpio_value(udc->board.vbus_pin);
  1365. if (value != udc->vbus)
  1366. at91_vbus_session(&udc->gadget, value);
  1367. return IRQ_HANDLED;
  1368. }
  1369. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1370. {
  1371. struct at91_udc *udc = &controller;
  1372. int retval;
  1373. if (!driver
  1374. || driver->speed < USB_SPEED_FULL
  1375. || !driver->bind
  1376. || !driver->setup) {
  1377. DBG("bad parameter.\n");
  1378. return -EINVAL;
  1379. }
  1380. if (udc->driver) {
  1381. DBG("UDC already has a gadget driver\n");
  1382. return -EBUSY;
  1383. }
  1384. udc->driver = driver;
  1385. udc->gadget.dev.driver = &driver->driver;
  1386. udc->gadget.dev.driver_data = &driver->driver;
  1387. udc->enabled = 1;
  1388. udc->selfpowered = 1;
  1389. retval = driver->bind(&udc->gadget);
  1390. if (retval) {
  1391. DBG("driver->bind() returned %d\n", retval);
  1392. udc->driver = NULL;
  1393. udc->gadget.dev.driver = NULL;
  1394. udc->gadget.dev.driver_data = NULL;
  1395. udc->enabled = 0;
  1396. udc->selfpowered = 0;
  1397. return retval;
  1398. }
  1399. local_irq_disable();
  1400. pullup(udc, 1);
  1401. local_irq_enable();
  1402. DBG("bound to %s\n", driver->driver.name);
  1403. return 0;
  1404. }
  1405. EXPORT_SYMBOL (usb_gadget_register_driver);
  1406. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1407. {
  1408. struct at91_udc *udc = &controller;
  1409. if (!driver || driver != udc->driver || !driver->unbind)
  1410. return -EINVAL;
  1411. local_irq_disable();
  1412. udc->enabled = 0;
  1413. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1414. pullup(udc, 0);
  1415. local_irq_enable();
  1416. driver->unbind(&udc->gadget);
  1417. udc->driver = NULL;
  1418. DBG("unbound from %s\n", driver->driver.name);
  1419. return 0;
  1420. }
  1421. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1422. /*-------------------------------------------------------------------------*/
  1423. static void at91udc_shutdown(struct platform_device *dev)
  1424. {
  1425. /* force disconnect on reboot */
  1426. pullup(platform_get_drvdata(dev), 0);
  1427. }
  1428. static int __devinit at91udc_probe(struct platform_device *pdev)
  1429. {
  1430. struct device *dev = &pdev->dev;
  1431. struct at91_udc *udc;
  1432. int retval;
  1433. struct resource *res;
  1434. if (!dev->platform_data) {
  1435. /* small (so we copy it) but critical! */
  1436. DBG("missing platform_data\n");
  1437. return -ENODEV;
  1438. }
  1439. if (pdev->num_resources != 2) {
  1440. DBG("invalid num_resources");
  1441. return -ENODEV;
  1442. }
  1443. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1444. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1445. DBG("invalid resource type");
  1446. return -ENODEV;
  1447. }
  1448. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1449. if (!res)
  1450. return -ENXIO;
  1451. if (!request_mem_region(res->start,
  1452. res->end - res->start + 1,
  1453. driver_name)) {
  1454. DBG("someone's using UDC memory\n");
  1455. return -EBUSY;
  1456. }
  1457. /* init software state */
  1458. udc = &controller;
  1459. udc->gadget.dev.parent = dev;
  1460. udc->board = *(struct at91_udc_data *) dev->platform_data;
  1461. udc->pdev = pdev;
  1462. udc->enabled = 0;
  1463. udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
  1464. if (!udc->udp_baseaddr) {
  1465. release_mem_region(res->start, res->end - res->start + 1);
  1466. return -ENOMEM;
  1467. }
  1468. udc_reinit(udc);
  1469. /* get interface and function clocks */
  1470. udc->iclk = clk_get(dev, "udc_clk");
  1471. udc->fclk = clk_get(dev, "udpck");
  1472. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1473. DBG("clocks missing\n");
  1474. retval = -ENODEV;
  1475. goto fail0;
  1476. }
  1477. retval = device_register(&udc->gadget.dev);
  1478. if (retval < 0)
  1479. goto fail0;
  1480. /* don't do anything until we have both gadget driver and VBUS */
  1481. clk_enable(udc->iclk);
  1482. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1483. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1484. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1485. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1486. clk_disable(udc->iclk);
  1487. /* request UDC and maybe VBUS irqs */
  1488. udc->udp_irq = platform_get_irq(pdev, 0);
  1489. if (request_irq(udc->udp_irq, at91_udc_irq,
  1490. IRQF_DISABLED, driver_name, udc)) {
  1491. DBG("request irq %d failed\n", udc->udp_irq);
  1492. retval = -EBUSY;
  1493. goto fail1;
  1494. }
  1495. if (udc->board.vbus_pin > 0) {
  1496. /*
  1497. * Get the initial state of VBUS - we cannot expect
  1498. * a pending interrupt.
  1499. */
  1500. udc->vbus = at91_get_gpio_value(udc->board.vbus_pin);
  1501. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1502. IRQF_DISABLED, driver_name, udc)) {
  1503. DBG("request vbus irq %d failed\n",
  1504. udc->board.vbus_pin);
  1505. free_irq(udc->udp_irq, udc);
  1506. retval = -EBUSY;
  1507. goto fail1;
  1508. }
  1509. } else {
  1510. DBG("no VBUS detection, assuming always-on\n");
  1511. udc->vbus = 1;
  1512. }
  1513. dev_set_drvdata(dev, udc);
  1514. device_init_wakeup(dev, 1);
  1515. create_debug_file(udc);
  1516. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1517. return 0;
  1518. fail1:
  1519. device_unregister(&udc->gadget.dev);
  1520. fail0:
  1521. release_mem_region(res->start, res->end - res->start + 1);
  1522. DBG("%s probe failed, %d\n", driver_name, retval);
  1523. return retval;
  1524. }
  1525. static int __devexit at91udc_remove(struct platform_device *pdev)
  1526. {
  1527. struct at91_udc *udc = platform_get_drvdata(pdev);
  1528. struct resource *res;
  1529. DBG("remove\n");
  1530. if (udc->driver)
  1531. return -EBUSY;
  1532. pullup(udc, 0);
  1533. device_init_wakeup(&pdev->dev, 0);
  1534. remove_debug_file(udc);
  1535. if (udc->board.vbus_pin > 0)
  1536. free_irq(udc->board.vbus_pin, udc);
  1537. free_irq(udc->udp_irq, udc);
  1538. device_unregister(&udc->gadget.dev);
  1539. iounmap(udc->udp_baseaddr);
  1540. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1541. release_mem_region(res->start, res->end - res->start + 1);
  1542. clk_put(udc->iclk);
  1543. clk_put(udc->fclk);
  1544. return 0;
  1545. }
  1546. #ifdef CONFIG_PM
  1547. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1548. {
  1549. struct at91_udc *udc = platform_get_drvdata(pdev);
  1550. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1551. /* Unless we can act normally to the host (letting it wake us up
  1552. * whenever it has work for us) force disconnect. Wakeup requires
  1553. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1554. * tokens) and VBUS irqs (on systems which support them).
  1555. */
  1556. if ((!udc->suspended && udc->addr)
  1557. || !wake
  1558. || at91_suspend_entering_slow_clock()) {
  1559. pullup(udc, 0);
  1560. wake = 0;
  1561. } else
  1562. enable_irq_wake(udc->udp_irq);
  1563. udc->active_suspend = wake;
  1564. if (udc->board.vbus_pin > 0 && wake)
  1565. enable_irq_wake(udc->board.vbus_pin);
  1566. return 0;
  1567. }
  1568. static int at91udc_resume(struct platform_device *pdev)
  1569. {
  1570. struct at91_udc *udc = platform_get_drvdata(pdev);
  1571. if (udc->board.vbus_pin > 0 && udc->active_suspend)
  1572. disable_irq_wake(udc->board.vbus_pin);
  1573. /* maybe reconnect to host; if so, clocks on */
  1574. if (udc->active_suspend)
  1575. disable_irq_wake(udc->udp_irq);
  1576. else
  1577. pullup(udc, 1);
  1578. return 0;
  1579. }
  1580. #else
  1581. #define at91udc_suspend NULL
  1582. #define at91udc_resume NULL
  1583. #endif
  1584. static struct platform_driver at91_udc = {
  1585. .probe = at91udc_probe,
  1586. .remove = __devexit_p(at91udc_remove),
  1587. .shutdown = at91udc_shutdown,
  1588. .suspend = at91udc_suspend,
  1589. .resume = at91udc_resume,
  1590. .driver = {
  1591. .name = (char *) driver_name,
  1592. .owner = THIS_MODULE,
  1593. },
  1594. };
  1595. static int __devinit udc_init_module(void)
  1596. {
  1597. return platform_driver_register(&at91_udc);
  1598. }
  1599. module_init(udc_init_module);
  1600. static void __devexit udc_exit_module(void)
  1601. {
  1602. platform_driver_unregister(&at91_udc);
  1603. }
  1604. module_exit(udc_exit_module);
  1605. MODULE_DESCRIPTION("AT91 udc driver");
  1606. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1607. MODULE_LICENSE("GPL");