at91_udc.c 47 KB

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