at91_udc.c 46 KB

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