at91_udc.c 46 KB

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