at91_udc.c 46 KB

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