at91_udc.c 46 KB

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