at91_udc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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 int at91_ep_queue(struct usb_ep *_ep,
  507. struct usb_request *_req, gfp_t gfp_flags)
  508. {
  509. struct at91_request *req;
  510. struct at91_ep *ep;
  511. struct at91_udc *dev;
  512. int status;
  513. unsigned long flags;
  514. req = container_of(_req, struct at91_request, req);
  515. ep = container_of(_ep, struct at91_ep, ep);
  516. if (!_req || !_req->complete
  517. || !_req->buf || !list_empty(&req->queue)) {
  518. DBG("invalid request\n");
  519. return -EINVAL;
  520. }
  521. if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
  522. DBG("invalid ep\n");
  523. return -EINVAL;
  524. }
  525. dev = ep->udc;
  526. if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  527. DBG("invalid device\n");
  528. return -EINVAL;
  529. }
  530. _req->status = -EINPROGRESS;
  531. _req->actual = 0;
  532. local_irq_save(flags);
  533. /* try to kickstart any empty and idle queue */
  534. if (list_empty(&ep->queue) && !ep->stopped) {
  535. int is_ep0;
  536. /*
  537. * If this control request has a non-empty DATA stage, this
  538. * will start that stage. It works just like a non-control
  539. * request (until the status stage starts, maybe early).
  540. *
  541. * If the data stage is empty, then this starts a successful
  542. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  543. */
  544. is_ep0 = (ep->ep.name == ep0name);
  545. if (is_ep0) {
  546. u32 tmp;
  547. if (!dev->req_pending) {
  548. status = -EINVAL;
  549. goto done;
  550. }
  551. /*
  552. * defer changing CONFG until after the gadget driver
  553. * reconfigures the endpoints.
  554. */
  555. if (dev->wait_for_config_ack) {
  556. tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
  557. tmp ^= AT91_UDP_CONFG;
  558. VDBG("toggle config\n");
  559. at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
  560. }
  561. if (req->req.length == 0) {
  562. ep0_in_status:
  563. PACKET("ep0 in/status\n");
  564. status = 0;
  565. tmp = __raw_readl(ep->creg);
  566. tmp &= ~SET_FX;
  567. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  568. __raw_writel(tmp, ep->creg);
  569. dev->req_pending = 0;
  570. goto done;
  571. }
  572. }
  573. if (ep->is_in)
  574. status = write_fifo(ep, req);
  575. else {
  576. status = read_fifo(ep, req);
  577. /* IN/STATUS stage is otherwise triggered by irq */
  578. if (status && is_ep0)
  579. goto ep0_in_status;
  580. }
  581. } else
  582. status = 0;
  583. if (req && !status) {
  584. list_add_tail (&req->queue, &ep->queue);
  585. at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
  586. }
  587. done:
  588. local_irq_restore(flags);
  589. return (status < 0) ? status : 0;
  590. }
  591. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  592. {
  593. struct at91_ep *ep;
  594. struct at91_request *req;
  595. ep = container_of(_ep, struct at91_ep, ep);
  596. if (!_ep || ep->ep.name == ep0name)
  597. return -EINVAL;
  598. /* make sure it's actually queued on this endpoint */
  599. list_for_each_entry (req, &ep->queue, queue) {
  600. if (&req->req == _req)
  601. break;
  602. }
  603. if (&req->req != _req)
  604. return -EINVAL;
  605. done(ep, req, -ECONNRESET);
  606. return 0;
  607. }
  608. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  609. {
  610. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  611. struct at91_udc *udc = ep->udc;
  612. u32 __iomem *creg;
  613. u32 csr;
  614. unsigned long flags;
  615. int status = 0;
  616. if (!_ep || ep->is_iso || !ep->udc->clocked)
  617. return -EINVAL;
  618. creg = ep->creg;
  619. local_irq_save(flags);
  620. csr = __raw_readl(creg);
  621. /*
  622. * fail with still-busy IN endpoints, ensuring correct sequencing
  623. * of data tx then stall. note that the fifo rx bytecount isn't
  624. * completely accurate as a tx bytecount.
  625. */
  626. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  627. status = -EAGAIN;
  628. else {
  629. csr |= CLR_FX;
  630. csr &= ~SET_FX;
  631. if (value) {
  632. csr |= AT91_UDP_FORCESTALL;
  633. VDBG("halt %s\n", ep->ep.name);
  634. } else {
  635. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  636. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  637. csr &= ~AT91_UDP_FORCESTALL;
  638. }
  639. __raw_writel(csr, creg);
  640. }
  641. local_irq_restore(flags);
  642. return status;
  643. }
  644. static const struct usb_ep_ops at91_ep_ops = {
  645. .enable = at91_ep_enable,
  646. .disable = at91_ep_disable,
  647. .alloc_request = at91_ep_alloc_request,
  648. .free_request = at91_ep_free_request,
  649. .queue = at91_ep_queue,
  650. .dequeue = at91_ep_dequeue,
  651. .set_halt = at91_ep_set_halt,
  652. // there's only imprecise fifo status reporting
  653. };
  654. /*-------------------------------------------------------------------------*/
  655. static int at91_get_frame(struct usb_gadget *gadget)
  656. {
  657. struct at91_udc *udc = to_udc(gadget);
  658. if (!to_udc(gadget)->clocked)
  659. return -EINVAL;
  660. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  661. }
  662. static int at91_wakeup(struct usb_gadget *gadget)
  663. {
  664. struct at91_udc *udc = to_udc(gadget);
  665. u32 glbstate;
  666. int status = -EINVAL;
  667. unsigned long flags;
  668. DBG("%s\n", __FUNCTION__ );
  669. local_irq_save(flags);
  670. if (!udc->clocked || !udc->suspended)
  671. goto done;
  672. /* NOTE: some "early versions" handle ESR differently ... */
  673. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  674. if (!(glbstate & AT91_UDP_ESR))
  675. goto done;
  676. glbstate |= AT91_UDP_ESR;
  677. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  678. done:
  679. local_irq_restore(flags);
  680. return status;
  681. }
  682. /* reinit == restore inital software state */
  683. static void udc_reinit(struct at91_udc *udc)
  684. {
  685. u32 i;
  686. INIT_LIST_HEAD(&udc->gadget.ep_list);
  687. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  688. for (i = 0; i < NUM_ENDPOINTS; i++) {
  689. struct at91_ep *ep = &udc->ep[i];
  690. if (i != 0)
  691. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  692. ep->desc = NULL;
  693. ep->stopped = 0;
  694. ep->fifo_bank = 0;
  695. ep->ep.maxpacket = ep->maxpacket;
  696. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  697. // initialiser une queue par endpoint
  698. INIT_LIST_HEAD(&ep->queue);
  699. }
  700. }
  701. static void stop_activity(struct at91_udc *udc)
  702. {
  703. struct usb_gadget_driver *driver = udc->driver;
  704. int i;
  705. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  706. driver = NULL;
  707. udc->gadget.speed = USB_SPEED_UNKNOWN;
  708. udc->suspended = 0;
  709. for (i = 0; i < NUM_ENDPOINTS; i++) {
  710. struct at91_ep *ep = &udc->ep[i];
  711. ep->stopped = 1;
  712. nuke(ep, -ESHUTDOWN);
  713. }
  714. if (driver)
  715. driver->disconnect(&udc->gadget);
  716. udc_reinit(udc);
  717. }
  718. static void clk_on(struct at91_udc *udc)
  719. {
  720. if (udc->clocked)
  721. return;
  722. udc->clocked = 1;
  723. clk_enable(udc->iclk);
  724. clk_enable(udc->fclk);
  725. }
  726. static void clk_off(struct at91_udc *udc)
  727. {
  728. if (!udc->clocked)
  729. return;
  730. udc->clocked = 0;
  731. udc->gadget.speed = USB_SPEED_UNKNOWN;
  732. clk_disable(udc->fclk);
  733. clk_disable(udc->iclk);
  734. }
  735. /*
  736. * activate/deactivate link with host; minimize power usage for
  737. * inactive links by cutting clocks and transceiver power.
  738. */
  739. static void pullup(struct at91_udc *udc, int is_on)
  740. {
  741. if (!udc->enabled || !udc->vbus)
  742. is_on = 0;
  743. DBG("%sactive\n", is_on ? "" : "in");
  744. if (is_on) {
  745. clk_on(udc);
  746. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  747. at91_udp_write(udc, AT91_UDP_TXVC, 0);
  748. if (cpu_is_at91rm9200())
  749. at91_set_gpio_value(udc->board.pullup_pin, 1);
  750. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
  751. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  752. txvc |= AT91_UDP_TXVC_PUON;
  753. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  754. } else if (cpu_is_at91sam9261()) {
  755. u32 usbpucr;
  756. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  757. usbpucr |= AT91_MATRIX_USBPUCR_PUON;
  758. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  759. }
  760. } else {
  761. stop_activity(udc);
  762. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  763. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  764. if (cpu_is_at91rm9200())
  765. at91_set_gpio_value(udc->board.pullup_pin, 0);
  766. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
  767. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  768. txvc &= ~AT91_UDP_TXVC_PUON;
  769. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  770. } else if (cpu_is_at91sam9261()) {
  771. u32 usbpucr;
  772. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  773. usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
  774. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  775. }
  776. clk_off(udc);
  777. }
  778. }
  779. /* vbus is here! turn everything on that's ready */
  780. static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
  781. {
  782. struct at91_udc *udc = to_udc(gadget);
  783. unsigned long flags;
  784. // VDBG("vbus %s\n", is_active ? "on" : "off");
  785. local_irq_save(flags);
  786. udc->vbus = (is_active != 0);
  787. if (udc->driver)
  788. pullup(udc, is_active);
  789. else
  790. pullup(udc, 0);
  791. local_irq_restore(flags);
  792. return 0;
  793. }
  794. static int at91_pullup(struct usb_gadget *gadget, int is_on)
  795. {
  796. struct at91_udc *udc = to_udc(gadget);
  797. unsigned long flags;
  798. local_irq_save(flags);
  799. udc->enabled = is_on = !!is_on;
  800. pullup(udc, is_on);
  801. local_irq_restore(flags);
  802. return 0;
  803. }
  804. static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
  805. {
  806. struct at91_udc *udc = to_udc(gadget);
  807. unsigned long flags;
  808. local_irq_save(flags);
  809. udc->selfpowered = (is_on != 0);
  810. local_irq_restore(flags);
  811. return 0;
  812. }
  813. static const struct usb_gadget_ops at91_udc_ops = {
  814. .get_frame = at91_get_frame,
  815. .wakeup = at91_wakeup,
  816. .set_selfpowered = at91_set_selfpowered,
  817. .vbus_session = at91_vbus_session,
  818. .pullup = at91_pullup,
  819. /*
  820. * VBUS-powered devices may also also want to support bigger
  821. * power budgets after an appropriate SET_CONFIGURATION.
  822. */
  823. // .vbus_power = at91_vbus_power,
  824. };
  825. /*-------------------------------------------------------------------------*/
  826. static int handle_ep(struct at91_ep *ep)
  827. {
  828. struct at91_request *req;
  829. u32 __iomem *creg = ep->creg;
  830. u32 csr = __raw_readl(creg);
  831. if (!list_empty(&ep->queue))
  832. req = list_entry(ep->queue.next,
  833. struct at91_request, queue);
  834. else
  835. req = NULL;
  836. if (ep->is_in) {
  837. if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
  838. csr |= CLR_FX;
  839. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
  840. __raw_writel(csr, creg);
  841. }
  842. if (req)
  843. return write_fifo(ep, req);
  844. } else {
  845. if (csr & AT91_UDP_STALLSENT) {
  846. /* STALLSENT bit == ISOERR */
  847. if (ep->is_iso && req)
  848. req->req.status = -EILSEQ;
  849. csr |= CLR_FX;
  850. csr &= ~(SET_FX | AT91_UDP_STALLSENT);
  851. __raw_writel(csr, creg);
  852. csr = __raw_readl(creg);
  853. }
  854. if (req && (csr & RX_DATA_READY))
  855. return read_fifo(ep, req);
  856. }
  857. return 0;
  858. }
  859. union setup {
  860. u8 raw[8];
  861. struct usb_ctrlrequest r;
  862. };
  863. static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
  864. {
  865. u32 __iomem *creg = ep->creg;
  866. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  867. unsigned rxcount, i = 0;
  868. u32 tmp;
  869. union setup pkt;
  870. int status = 0;
  871. /* read and ack SETUP; hard-fail for bogus packets */
  872. rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
  873. if (likely(rxcount == 8)) {
  874. while (rxcount--)
  875. pkt.raw[i++] = __raw_readb(dreg);
  876. if (pkt.r.bRequestType & USB_DIR_IN) {
  877. csr |= AT91_UDP_DIR;
  878. ep->is_in = 1;
  879. } else {
  880. csr &= ~AT91_UDP_DIR;
  881. ep->is_in = 0;
  882. }
  883. } else {
  884. // REVISIT this happens sometimes under load; why??
  885. ERR("SETUP len %d, csr %08x\n", rxcount, csr);
  886. status = -EINVAL;
  887. }
  888. csr |= CLR_FX;
  889. csr &= ~(SET_FX | AT91_UDP_RXSETUP);
  890. __raw_writel(csr, creg);
  891. udc->wait_for_addr_ack = 0;
  892. udc->wait_for_config_ack = 0;
  893. ep->stopped = 0;
  894. if (unlikely(status != 0))
  895. goto stall;
  896. #define w_index le16_to_cpu(pkt.r.wIndex)
  897. #define w_value le16_to_cpu(pkt.r.wValue)
  898. #define w_length le16_to_cpu(pkt.r.wLength)
  899. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  900. pkt.r.bRequestType, pkt.r.bRequest,
  901. w_value, w_index, w_length);
  902. /*
  903. * A few standard requests get handled here, ones that touch
  904. * hardware ... notably for device and endpoint features.
  905. */
  906. udc->req_pending = 1;
  907. csr = __raw_readl(creg);
  908. csr |= CLR_FX;
  909. csr &= ~SET_FX;
  910. switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
  911. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  912. | USB_REQ_SET_ADDRESS:
  913. __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
  914. udc->addr = w_value;
  915. udc->wait_for_addr_ack = 1;
  916. udc->req_pending = 0;
  917. /* FADDR is set later, when we ack host STATUS */
  918. return;
  919. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  920. | USB_REQ_SET_CONFIGURATION:
  921. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
  922. if (pkt.r.wValue)
  923. udc->wait_for_config_ack = (tmp == 0);
  924. else
  925. udc->wait_for_config_ack = (tmp != 0);
  926. if (udc->wait_for_config_ack)
  927. VDBG("wait for config\n");
  928. /* CONFG is toggled later, if gadget driver succeeds */
  929. break;
  930. /*
  931. * Hosts may set or clear remote wakeup status, and
  932. * devices may report they're VBUS powered.
  933. */
  934. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  935. | USB_REQ_GET_STATUS:
  936. tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  937. if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
  938. tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  939. PACKET("get device status\n");
  940. __raw_writeb(tmp, dreg);
  941. __raw_writeb(0, dreg);
  942. goto write_in;
  943. /* then STATUS starts later, automatically */
  944. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  945. | USB_REQ_SET_FEATURE:
  946. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  947. goto stall;
  948. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  949. tmp |= AT91_UDP_ESR;
  950. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  951. goto succeed;
  952. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  953. | USB_REQ_CLEAR_FEATURE:
  954. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  955. goto stall;
  956. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  957. tmp &= ~AT91_UDP_ESR;
  958. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  959. goto succeed;
  960. /*
  961. * Interfaces have no feature settings; this is pretty useless.
  962. * we won't even insist the interface exists...
  963. */
  964. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  965. | USB_REQ_GET_STATUS:
  966. PACKET("get interface status\n");
  967. __raw_writeb(0, dreg);
  968. __raw_writeb(0, dreg);
  969. goto write_in;
  970. /* then STATUS starts later, automatically */
  971. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  972. | USB_REQ_SET_FEATURE:
  973. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  974. | USB_REQ_CLEAR_FEATURE:
  975. goto stall;
  976. /*
  977. * Hosts may clear bulk/intr endpoint halt after the gadget
  978. * driver sets it (not widely used); or set it (for testing)
  979. */
  980. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  981. | USB_REQ_GET_STATUS:
  982. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  983. ep = &udc->ep[tmp];
  984. if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc))
  985. goto stall;
  986. if (tmp) {
  987. if ((w_index & USB_DIR_IN)) {
  988. if (!ep->is_in)
  989. goto stall;
  990. } else if (ep->is_in)
  991. goto stall;
  992. }
  993. PACKET("get %s status\n", ep->ep.name);
  994. if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
  995. tmp = (1 << USB_ENDPOINT_HALT);
  996. else
  997. tmp = 0;
  998. __raw_writeb(tmp, dreg);
  999. __raw_writeb(0, dreg);
  1000. goto write_in;
  1001. /* then STATUS starts later, automatically */
  1002. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1003. | USB_REQ_SET_FEATURE:
  1004. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1005. ep = &udc->ep[tmp];
  1006. if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
  1007. goto stall;
  1008. if (!ep->desc || ep->is_iso)
  1009. goto stall;
  1010. if ((w_index & USB_DIR_IN)) {
  1011. if (!ep->is_in)
  1012. goto stall;
  1013. } else if (ep->is_in)
  1014. goto stall;
  1015. tmp = __raw_readl(ep->creg);
  1016. tmp &= ~SET_FX;
  1017. tmp |= CLR_FX | AT91_UDP_FORCESTALL;
  1018. __raw_writel(tmp, ep->creg);
  1019. goto succeed;
  1020. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1021. | USB_REQ_CLEAR_FEATURE:
  1022. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1023. ep = &udc->ep[tmp];
  1024. if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
  1025. goto stall;
  1026. if (tmp == 0)
  1027. goto succeed;
  1028. if (!ep->desc || ep->is_iso)
  1029. goto stall;
  1030. if ((w_index & USB_DIR_IN)) {
  1031. if (!ep->is_in)
  1032. goto stall;
  1033. } else if (ep->is_in)
  1034. goto stall;
  1035. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  1036. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  1037. tmp = __raw_readl(ep->creg);
  1038. tmp |= CLR_FX;
  1039. tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
  1040. __raw_writel(tmp, ep->creg);
  1041. if (!list_empty(&ep->queue))
  1042. handle_ep(ep);
  1043. goto succeed;
  1044. }
  1045. #undef w_value
  1046. #undef w_index
  1047. #undef w_length
  1048. /* pass request up to the gadget driver */
  1049. if (udc->driver)
  1050. status = udc->driver->setup(&udc->gadget, &pkt.r);
  1051. else
  1052. status = -ENODEV;
  1053. if (status < 0) {
  1054. stall:
  1055. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1056. pkt.r.bRequestType, pkt.r.bRequest, status);
  1057. csr |= AT91_UDP_FORCESTALL;
  1058. __raw_writel(csr, creg);
  1059. udc->req_pending = 0;
  1060. }
  1061. return;
  1062. succeed:
  1063. /* immediate successful (IN) STATUS after zero length DATA */
  1064. PACKET("ep0 in/status\n");
  1065. write_in:
  1066. csr |= AT91_UDP_TXPKTRDY;
  1067. __raw_writel(csr, creg);
  1068. udc->req_pending = 0;
  1069. return;
  1070. }
  1071. static void handle_ep0(struct at91_udc *udc)
  1072. {
  1073. struct at91_ep *ep0 = &udc->ep[0];
  1074. u32 __iomem *creg = ep0->creg;
  1075. u32 csr = __raw_readl(creg);
  1076. struct at91_request *req;
  1077. if (unlikely(csr & AT91_UDP_STALLSENT)) {
  1078. nuke(ep0, -EPROTO);
  1079. udc->req_pending = 0;
  1080. csr |= CLR_FX;
  1081. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
  1082. __raw_writel(csr, creg);
  1083. VDBG("ep0 stalled\n");
  1084. csr = __raw_readl(creg);
  1085. }
  1086. if (csr & AT91_UDP_RXSETUP) {
  1087. nuke(ep0, 0);
  1088. udc->req_pending = 0;
  1089. handle_setup(udc, ep0, csr);
  1090. return;
  1091. }
  1092. if (list_empty(&ep0->queue))
  1093. req = NULL;
  1094. else
  1095. req = list_entry(ep0->queue.next, struct at91_request, queue);
  1096. /* host ACKed an IN packet that we sent */
  1097. if (csr & AT91_UDP_TXCOMP) {
  1098. csr |= CLR_FX;
  1099. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  1100. /* write more IN DATA? */
  1101. if (req && ep0->is_in) {
  1102. if (handle_ep(ep0))
  1103. udc->req_pending = 0;
  1104. /*
  1105. * Ack after:
  1106. * - last IN DATA packet (including GET_STATUS)
  1107. * - IN/STATUS for OUT DATA
  1108. * - IN/STATUS for any zero-length DATA stage
  1109. * except for the IN DATA case, the host should send
  1110. * an OUT status later, which we'll ack.
  1111. */
  1112. } else {
  1113. udc->req_pending = 0;
  1114. __raw_writel(csr, creg);
  1115. /*
  1116. * SET_ADDRESS takes effect only after the STATUS
  1117. * (to the original address) gets acked.
  1118. */
  1119. if (udc->wait_for_addr_ack) {
  1120. u32 tmp;
  1121. at91_udp_write(udc, AT91_UDP_FADDR,
  1122. AT91_UDP_FEN | udc->addr);
  1123. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  1124. tmp &= ~AT91_UDP_FADDEN;
  1125. if (udc->addr)
  1126. tmp |= AT91_UDP_FADDEN;
  1127. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  1128. udc->wait_for_addr_ack = 0;
  1129. VDBG("address %d\n", udc->addr);
  1130. }
  1131. }
  1132. }
  1133. /* OUT packet arrived ... */
  1134. else if (csr & AT91_UDP_RX_DATA_BK0) {
  1135. csr |= CLR_FX;
  1136. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  1137. /* OUT DATA stage */
  1138. if (!ep0->is_in) {
  1139. if (req) {
  1140. if (handle_ep(ep0)) {
  1141. /* send IN/STATUS */
  1142. PACKET("ep0 in/status\n");
  1143. csr = __raw_readl(creg);
  1144. csr &= ~SET_FX;
  1145. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  1146. __raw_writel(csr, creg);
  1147. udc->req_pending = 0;
  1148. }
  1149. } else if (udc->req_pending) {
  1150. /*
  1151. * AT91 hardware has a hard time with this
  1152. * "deferred response" mode for control-OUT
  1153. * transfers. (For control-IN it's fine.)
  1154. *
  1155. * The normal solution leaves OUT data in the
  1156. * fifo until the gadget driver is ready.
  1157. * We couldn't do that here without disabling
  1158. * the IRQ that tells about SETUP packets,
  1159. * e.g. when the host gets impatient...
  1160. *
  1161. * Working around it by copying into a buffer
  1162. * would almost be a non-deferred response,
  1163. * except that it wouldn't permit reliable
  1164. * stalling of the request. Instead, demand
  1165. * that gadget drivers not use this mode.
  1166. */
  1167. DBG("no control-OUT deferred responses!\n");
  1168. __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
  1169. udc->req_pending = 0;
  1170. }
  1171. /* STATUS stage for control-IN; ack. */
  1172. } else {
  1173. PACKET("ep0 out/status ACK\n");
  1174. __raw_writel(csr, creg);
  1175. /* "early" status stage */
  1176. if (req)
  1177. done(ep0, req, 0);
  1178. }
  1179. }
  1180. }
  1181. static irqreturn_t at91_udc_irq (int irq, void *_udc)
  1182. {
  1183. struct at91_udc *udc = _udc;
  1184. u32 rescans = 5;
  1185. while (rescans--) {
  1186. u32 status;
  1187. status = at91_udp_read(udc, AT91_UDP_ISR)
  1188. & at91_udp_read(udc, AT91_UDP_IMR);
  1189. if (!status)
  1190. break;
  1191. /* USB reset irq: not maskable */
  1192. if (status & AT91_UDP_ENDBUSRES) {
  1193. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1194. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1195. /* Atmel code clears this irq twice */
  1196. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1197. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1198. VDBG("end bus reset\n");
  1199. udc->addr = 0;
  1200. stop_activity(udc);
  1201. /* enable ep0 */
  1202. at91_udp_write(udc, AT91_UDP_CSR(0),
  1203. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1204. udc->gadget.speed = USB_SPEED_FULL;
  1205. udc->suspended = 0;
  1206. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1207. /*
  1208. * NOTE: this driver keeps clocks off unless the
  1209. * USB host is present. That saves power, but for
  1210. * boards that don't support VBUS detection, both
  1211. * clocks need to be active most of the time.
  1212. */
  1213. /* host initiated suspend (3+ms bus idle) */
  1214. } else if (status & AT91_UDP_RXSUSP) {
  1215. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1216. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1217. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1218. // VDBG("bus suspend\n");
  1219. if (udc->suspended)
  1220. continue;
  1221. udc->suspended = 1;
  1222. /*
  1223. * NOTE: when suspending a VBUS-powered device, the
  1224. * gadget driver should switch into slow clock mode
  1225. * and then into standby to avoid drawing more than
  1226. * 500uA power (2500uA for some high-power configs).
  1227. */
  1228. if (udc->driver && udc->driver->suspend)
  1229. udc->driver->suspend(&udc->gadget);
  1230. /* host initiated resume */
  1231. } else if (status & AT91_UDP_RXRSM) {
  1232. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1233. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1234. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1235. // VDBG("bus resume\n");
  1236. if (!udc->suspended)
  1237. continue;
  1238. udc->suspended = 0;
  1239. /*
  1240. * NOTE: for a VBUS-powered device, the gadget driver
  1241. * would normally want to switch out of slow clock
  1242. * mode into normal mode.
  1243. */
  1244. if (udc->driver && udc->driver->resume)
  1245. udc->driver->resume(&udc->gadget);
  1246. /* endpoint IRQs are cleared by handling them */
  1247. } else {
  1248. int i;
  1249. unsigned mask = 1;
  1250. struct at91_ep *ep = &udc->ep[1];
  1251. if (status & mask)
  1252. handle_ep0(udc);
  1253. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1254. mask <<= 1;
  1255. if (status & mask)
  1256. handle_ep(ep);
  1257. ep++;
  1258. }
  1259. }
  1260. }
  1261. return IRQ_HANDLED;
  1262. }
  1263. /*-------------------------------------------------------------------------*/
  1264. static void nop_release(struct device *dev)
  1265. {
  1266. /* nothing to free */
  1267. }
  1268. static struct at91_udc controller = {
  1269. .gadget = {
  1270. .ops = &at91_udc_ops,
  1271. .ep0 = &controller.ep[0].ep,
  1272. .name = driver_name,
  1273. .dev = {
  1274. .bus_id = "gadget",
  1275. .release = nop_release,
  1276. }
  1277. },
  1278. .ep[0] = {
  1279. .ep = {
  1280. .name = ep0name,
  1281. .ops = &at91_ep_ops,
  1282. },
  1283. .udc = &controller,
  1284. .maxpacket = 8,
  1285. .int_mask = 1 << 0,
  1286. },
  1287. .ep[1] = {
  1288. .ep = {
  1289. .name = "ep1",
  1290. .ops = &at91_ep_ops,
  1291. },
  1292. .udc = &controller,
  1293. .is_pingpong = 1,
  1294. .maxpacket = 64,
  1295. .int_mask = 1 << 1,
  1296. },
  1297. .ep[2] = {
  1298. .ep = {
  1299. .name = "ep2",
  1300. .ops = &at91_ep_ops,
  1301. },
  1302. .udc = &controller,
  1303. .is_pingpong = 1,
  1304. .maxpacket = 64,
  1305. .int_mask = 1 << 2,
  1306. },
  1307. .ep[3] = {
  1308. .ep = {
  1309. /* could actually do bulk too */
  1310. .name = "ep3-int",
  1311. .ops = &at91_ep_ops,
  1312. },
  1313. .udc = &controller,
  1314. .maxpacket = 8,
  1315. .int_mask = 1 << 3,
  1316. },
  1317. .ep[4] = {
  1318. .ep = {
  1319. .name = "ep4",
  1320. .ops = &at91_ep_ops,
  1321. },
  1322. .udc = &controller,
  1323. .is_pingpong = 1,
  1324. .maxpacket = 256,
  1325. .int_mask = 1 << 4,
  1326. },
  1327. .ep[5] = {
  1328. .ep = {
  1329. .name = "ep5",
  1330. .ops = &at91_ep_ops,
  1331. },
  1332. .udc = &controller,
  1333. .is_pingpong = 1,
  1334. .maxpacket = 256,
  1335. .int_mask = 1 << 5,
  1336. },
  1337. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1338. };
  1339. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1340. {
  1341. struct at91_udc *udc = _udc;
  1342. unsigned value;
  1343. /* vbus needs at least brief debouncing */
  1344. udelay(10);
  1345. value = at91_get_gpio_value(udc->board.vbus_pin);
  1346. if (value != udc->vbus)
  1347. at91_vbus_session(&udc->gadget, value);
  1348. return IRQ_HANDLED;
  1349. }
  1350. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1351. {
  1352. struct at91_udc *udc = &controller;
  1353. int retval;
  1354. if (!driver
  1355. || driver->speed < USB_SPEED_FULL
  1356. || !driver->bind
  1357. || !driver->setup) {
  1358. DBG("bad parameter.\n");
  1359. return -EINVAL;
  1360. }
  1361. if (udc->driver) {
  1362. DBG("UDC already has a gadget driver\n");
  1363. return -EBUSY;
  1364. }
  1365. udc->driver = driver;
  1366. udc->gadget.dev.driver = &driver->driver;
  1367. udc->gadget.dev.driver_data = &driver->driver;
  1368. udc->enabled = 1;
  1369. udc->selfpowered = 1;
  1370. retval = driver->bind(&udc->gadget);
  1371. if (retval) {
  1372. DBG("driver->bind() returned %d\n", retval);
  1373. udc->driver = NULL;
  1374. udc->gadget.dev.driver = NULL;
  1375. udc->gadget.dev.driver_data = NULL;
  1376. udc->enabled = 0;
  1377. udc->selfpowered = 0;
  1378. return retval;
  1379. }
  1380. local_irq_disable();
  1381. pullup(udc, 1);
  1382. local_irq_enable();
  1383. DBG("bound to %s\n", driver->driver.name);
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL (usb_gadget_register_driver);
  1387. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1388. {
  1389. struct at91_udc *udc = &controller;
  1390. if (!driver || driver != udc->driver || !driver->unbind)
  1391. return -EINVAL;
  1392. local_irq_disable();
  1393. udc->enabled = 0;
  1394. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1395. pullup(udc, 0);
  1396. local_irq_enable();
  1397. driver->unbind(&udc->gadget);
  1398. udc->driver = NULL;
  1399. DBG("unbound from %s\n", driver->driver.name);
  1400. return 0;
  1401. }
  1402. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1403. /*-------------------------------------------------------------------------*/
  1404. static void at91udc_shutdown(struct platform_device *dev)
  1405. {
  1406. /* force disconnect on reboot */
  1407. pullup(platform_get_drvdata(dev), 0);
  1408. }
  1409. static int __init at91udc_probe(struct platform_device *pdev)
  1410. {
  1411. struct device *dev = &pdev->dev;
  1412. struct at91_udc *udc;
  1413. int retval;
  1414. struct resource *res;
  1415. if (!dev->platform_data) {
  1416. /* small (so we copy it) but critical! */
  1417. DBG("missing platform_data\n");
  1418. return -ENODEV;
  1419. }
  1420. if (pdev->num_resources != 2) {
  1421. DBG("invalid num_resources");
  1422. return -ENODEV;
  1423. }
  1424. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1425. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1426. DBG("invalid resource type");
  1427. return -ENODEV;
  1428. }
  1429. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1430. if (!res)
  1431. return -ENXIO;
  1432. if (!request_mem_region(res->start,
  1433. res->end - res->start + 1,
  1434. driver_name)) {
  1435. DBG("someone's using UDC memory\n");
  1436. return -EBUSY;
  1437. }
  1438. /* init software state */
  1439. udc = &controller;
  1440. udc->gadget.dev.parent = dev;
  1441. udc->board = *(struct at91_udc_data *) dev->platform_data;
  1442. udc->pdev = pdev;
  1443. udc->enabled = 0;
  1444. udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
  1445. if (!udc->udp_baseaddr) {
  1446. release_mem_region(res->start, res->end - res->start + 1);
  1447. return -ENOMEM;
  1448. }
  1449. udc_reinit(udc);
  1450. /* get interface and function clocks */
  1451. udc->iclk = clk_get(dev, "udc_clk");
  1452. udc->fclk = clk_get(dev, "udpck");
  1453. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1454. DBG("clocks missing\n");
  1455. retval = -ENODEV;
  1456. goto fail0;
  1457. }
  1458. retval = device_register(&udc->gadget.dev);
  1459. if (retval < 0)
  1460. goto fail0;
  1461. /* don't do anything until we have both gadget driver and VBUS */
  1462. clk_enable(udc->iclk);
  1463. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1464. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1465. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1466. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1467. clk_disable(udc->iclk);
  1468. /* request UDC and maybe VBUS irqs */
  1469. udc->udp_irq = platform_get_irq(pdev, 0);
  1470. if (request_irq(udc->udp_irq, at91_udc_irq,
  1471. IRQF_DISABLED, driver_name, udc)) {
  1472. DBG("request irq %d failed\n", udc->udp_irq);
  1473. retval = -EBUSY;
  1474. goto fail1;
  1475. }
  1476. if (udc->board.vbus_pin > 0) {
  1477. /*
  1478. * Get the initial state of VBUS - we cannot expect
  1479. * a pending interrupt.
  1480. */
  1481. udc->vbus = at91_get_gpio_value(udc->board.vbus_pin);
  1482. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1483. IRQF_DISABLED, driver_name, udc)) {
  1484. DBG("request vbus irq %d failed\n",
  1485. udc->board.vbus_pin);
  1486. free_irq(udc->udp_irq, udc);
  1487. retval = -EBUSY;
  1488. goto fail1;
  1489. }
  1490. } else {
  1491. DBG("no VBUS detection, assuming always-on\n");
  1492. udc->vbus = 1;
  1493. }
  1494. dev_set_drvdata(dev, udc);
  1495. device_init_wakeup(dev, 1);
  1496. create_debug_file(udc);
  1497. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1498. return 0;
  1499. fail1:
  1500. device_unregister(&udc->gadget.dev);
  1501. fail0:
  1502. release_mem_region(res->start, res->end - res->start + 1);
  1503. DBG("%s probe failed, %d\n", driver_name, retval);
  1504. return retval;
  1505. }
  1506. static int __exit at91udc_remove(struct platform_device *pdev)
  1507. {
  1508. struct at91_udc *udc = platform_get_drvdata(pdev);
  1509. struct resource *res;
  1510. DBG("remove\n");
  1511. if (udc->driver)
  1512. return -EBUSY;
  1513. pullup(udc, 0);
  1514. device_init_wakeup(&pdev->dev, 0);
  1515. remove_debug_file(udc);
  1516. if (udc->board.vbus_pin > 0)
  1517. free_irq(udc->board.vbus_pin, udc);
  1518. free_irq(udc->udp_irq, udc);
  1519. device_unregister(&udc->gadget.dev);
  1520. iounmap(udc->udp_baseaddr);
  1521. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1522. release_mem_region(res->start, res->end - res->start + 1);
  1523. clk_put(udc->iclk);
  1524. clk_put(udc->fclk);
  1525. return 0;
  1526. }
  1527. #ifdef CONFIG_PM
  1528. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1529. {
  1530. struct at91_udc *udc = platform_get_drvdata(pdev);
  1531. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1532. /* Unless we can act normally to the host (letting it wake us up
  1533. * whenever it has work for us) force disconnect. Wakeup requires
  1534. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1535. * tokens) and VBUS irqs (on systems which support them).
  1536. */
  1537. if ((!udc->suspended && udc->addr)
  1538. || !wake
  1539. || at91_suspend_entering_slow_clock()) {
  1540. pullup(udc, 0);
  1541. wake = 0;
  1542. } else
  1543. enable_irq_wake(udc->udp_irq);
  1544. udc->active_suspend = wake;
  1545. if (udc->board.vbus_pin > 0 && wake)
  1546. enable_irq_wake(udc->board.vbus_pin);
  1547. return 0;
  1548. }
  1549. static int at91udc_resume(struct platform_device *pdev)
  1550. {
  1551. struct at91_udc *udc = platform_get_drvdata(pdev);
  1552. if (udc->board.vbus_pin > 0 && udc->active_suspend)
  1553. disable_irq_wake(udc->board.vbus_pin);
  1554. /* maybe reconnect to host; if so, clocks on */
  1555. if (udc->active_suspend)
  1556. disable_irq_wake(udc->udp_irq);
  1557. else
  1558. pullup(udc, 1);
  1559. return 0;
  1560. }
  1561. #else
  1562. #define at91udc_suspend NULL
  1563. #define at91udc_resume NULL
  1564. #endif
  1565. static struct platform_driver at91_udc_driver = {
  1566. .remove = __exit_p(at91udc_remove),
  1567. .shutdown = at91udc_shutdown,
  1568. .suspend = at91udc_suspend,
  1569. .resume = at91udc_resume,
  1570. .driver = {
  1571. .name = (char *) driver_name,
  1572. .owner = THIS_MODULE,
  1573. },
  1574. };
  1575. static int __init udc_init_module(void)
  1576. {
  1577. return platform_driver_probe(&at91_udc_driver, at91udc_probe);
  1578. }
  1579. module_init(udc_init_module);
  1580. static void __exit udc_exit_module(void)
  1581. {
  1582. platform_driver_unregister(&at91_udc_driver);
  1583. }
  1584. module_exit(udc_exit_module);
  1585. MODULE_DESCRIPTION("AT91 udc driver");
  1586. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1587. MODULE_LICENSE("GPL");