at91_udc.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * at91_udc -- driver for at91-series USB peripheral controller
  3. *
  4. * Copyright (C) 2004 by Thomas Rathbone
  5. * Copyright (C) 2005 by HP Labs
  6. * Copyright (C) 2005 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #undef VERBOSE_DEBUG
  24. #undef PACKET_TRACE
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/init.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/clk.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <asm/byteorder.h>
  40. #include <asm/hardware.h>
  41. #include <asm/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/system.h>
  44. #include <asm/mach-types.h>
  45. #include <asm/gpio.h>
  46. #include <asm/arch/board.h>
  47. #include <asm/arch/cpu.h>
  48. #include <asm/arch/at91sam9261_matrix.h>
  49. #include "at91_udc.h"
  50. /*
  51. * This controller is simple and PIO-only. It's used in many AT91-series
  52. * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
  53. * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
  54. *
  55. * This driver expects the board has been wired with two GPIOs suppporting
  56. * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
  57. * testing hasn't covered such cases.)
  58. *
  59. * The pullup is most important (so it's integrated on sam926x parts). It
  60. * provides software control over whether the host enumerates the device.
  61. *
  62. * The VBUS sensing helps during enumeration, and allows both USB clocks
  63. * (and the transceiver) to stay gated off until they're necessary, saving
  64. * power. During USB suspend, the 48 MHz clock is gated off in hardware;
  65. * it may also be gated off by software during some Linux sleep states.
  66. */
  67. #define DRIVER_VERSION "3 May 2006"
  68. static const char driver_name [] = "at91_udc";
  69. static const char ep0name[] = "ep0";
  70. #define at91_udp_read(dev, reg) \
  71. __raw_readl((dev)->udp_baseaddr + (reg))
  72. #define at91_udp_write(dev, reg, val) \
  73. __raw_writel((val), (dev)->udp_baseaddr + (reg))
  74. /*-------------------------------------------------------------------------*/
  75. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  76. #include <linux/seq_file.h>
  77. static const char debug_filename[] = "driver/udc";
  78. #define FOURBITS "%s%s%s%s"
  79. #define EIGHTBITS FOURBITS FOURBITS
  80. static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
  81. {
  82. static char *types[] = {
  83. "control", "out-iso", "out-bulk", "out-int",
  84. "BOGUS", "in-iso", "in-bulk", "in-int"};
  85. u32 csr;
  86. struct at91_request *req;
  87. unsigned long flags;
  88. local_irq_save(flags);
  89. csr = __raw_readl(ep->creg);
  90. /* NOTE: not collecting per-endpoint irq statistics... */
  91. seq_printf(s, "\n");
  92. seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
  93. ep->ep.name, ep->ep.maxpacket,
  94. ep->is_in ? "in" : "out",
  95. ep->is_iso ? " iso" : "",
  96. ep->is_pingpong
  97. ? (ep->fifo_bank ? "pong" : "ping")
  98. : "",
  99. ep->stopped ? " stopped" : "");
  100. seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
  101. csr,
  102. (csr & 0x07ff0000) >> 16,
  103. (csr & (1 << 15)) ? "enabled" : "disabled",
  104. (csr & (1 << 11)) ? "DATA1" : "DATA0",
  105. types[(csr & 0x700) >> 8],
  106. /* iff type is control then print current direction */
  107. (!(csr & 0x700))
  108. ? ((csr & (1 << 7)) ? " IN" : " OUT")
  109. : "",
  110. (csr & (1 << 6)) ? " rxdatabk1" : "",
  111. (csr & (1 << 5)) ? " forcestall" : "",
  112. (csr & (1 << 4)) ? " txpktrdy" : "",
  113. (csr & (1 << 3)) ? " stallsent" : "",
  114. (csr & (1 << 2)) ? " rxsetup" : "",
  115. (csr & (1 << 1)) ? " rxdatabk0" : "",
  116. (csr & (1 << 0)) ? " txcomp" : "");
  117. if (list_empty (&ep->queue))
  118. seq_printf(s, "\t(queue empty)\n");
  119. else list_for_each_entry (req, &ep->queue, queue) {
  120. unsigned length = req->req.actual;
  121. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  122. &req->req, length,
  123. req->req.length, req->req.buf);
  124. }
  125. local_irq_restore(flags);
  126. }
  127. static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
  128. {
  129. int i;
  130. seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
  131. (mask & (1 << 13)) ? " wakeup" : "",
  132. (mask & (1 << 12)) ? " endbusres" : "",
  133. (mask & (1 << 11)) ? " sofint" : "",
  134. (mask & (1 << 10)) ? " extrsm" : "",
  135. (mask & (1 << 9)) ? " rxrsm" : "",
  136. (mask & (1 << 8)) ? " rxsusp" : "");
  137. for (i = 0; i < 8; i++) {
  138. if (mask & (1 << i))
  139. seq_printf(s, " ep%d", i);
  140. }
  141. seq_printf(s, "\n");
  142. }
  143. static int proc_udc_show(struct seq_file *s, void *unused)
  144. {
  145. struct at91_udc *udc = s->private;
  146. struct at91_ep *ep;
  147. u32 tmp;
  148. seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
  149. seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
  150. udc->vbus ? "present" : "off",
  151. udc->enabled
  152. ? (udc->vbus ? "active" : "enabled")
  153. : "disabled",
  154. udc->selfpowered ? "self" : "VBUS",
  155. udc->suspended ? ", suspended" : "",
  156. udc->driver ? udc->driver->driver.name : "(none)");
  157. /* don't access registers when interface isn't clocked */
  158. if (!udc->clocked) {
  159. seq_printf(s, "(not clocked)\n");
  160. return 0;
  161. }
  162. tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
  163. seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
  164. (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
  165. (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
  166. (tmp & AT91_UDP_NUM));
  167. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  168. seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
  169. (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
  170. (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
  171. (tmp & AT91_UDP_ESR) ? " esr" : "",
  172. (tmp & AT91_UDP_CONFG) ? " confg" : "",
  173. (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
  174. tmp = at91_udp_read(udc, AT91_UDP_FADDR);
  175. seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
  176. (tmp & AT91_UDP_FEN) ? " fen" : "",
  177. (tmp & AT91_UDP_FADD));
  178. proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
  179. proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
  180. if (udc->enabled && udc->vbus) {
  181. proc_ep_show(s, &udc->ep[0]);
  182. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  183. if (ep->desc)
  184. proc_ep_show(s, ep);
  185. }
  186. }
  187. return 0;
  188. }
  189. static int proc_udc_open(struct inode *inode, struct file *file)
  190. {
  191. return single_open(file, proc_udc_show, PDE(inode)->data);
  192. }
  193. static const struct file_operations proc_ops = {
  194. .open = proc_udc_open,
  195. .read = seq_read,
  196. .llseek = seq_lseek,
  197. .release = single_release,
  198. };
  199. static void create_debug_file(struct at91_udc *udc)
  200. {
  201. struct proc_dir_entry *pde;
  202. pde = create_proc_entry (debug_filename, 0, NULL);
  203. udc->pde = pde;
  204. if (pde == NULL)
  205. return;
  206. pde->proc_fops = &proc_ops;
  207. pde->data = udc;
  208. }
  209. static void remove_debug_file(struct at91_udc *udc)
  210. {
  211. if (udc->pde)
  212. remove_proc_entry(debug_filename, NULL);
  213. }
  214. #else
  215. static inline void create_debug_file(struct at91_udc *udc) {}
  216. static inline void remove_debug_file(struct at91_udc *udc) {}
  217. #endif
  218. /*-------------------------------------------------------------------------*/
  219. static void done(struct at91_ep *ep, struct at91_request *req, int status)
  220. {
  221. unsigned stopped = ep->stopped;
  222. struct at91_udc *udc = ep->udc;
  223. list_del_init(&req->queue);
  224. if (req->req.status == -EINPROGRESS)
  225. req->req.status = status;
  226. else
  227. status = req->req.status;
  228. if (status && status != -ESHUTDOWN)
  229. VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
  230. ep->stopped = 1;
  231. req->req.complete(&ep->ep, &req->req);
  232. ep->stopped = stopped;
  233. /* ep0 is always ready; other endpoints need a non-empty queue */
  234. if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
  235. at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
  236. }
  237. /*-------------------------------------------------------------------------*/
  238. /* bits indicating OUT fifo has data ready */
  239. #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
  240. /*
  241. * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
  242. * back most of the value you just read (because of side effects, including
  243. * bits that may change after reading and before writing).
  244. *
  245. * Except when changing a specific bit, always write values which:
  246. * - clear SET_FX bits (setting them could change something)
  247. * - set CLR_FX bits (clearing them could change something)
  248. *
  249. * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
  250. * that shouldn't normally be changed.
  251. *
  252. * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
  253. * implying a need to wait for one write to complete (test relevant bits)
  254. * before starting the next write. This shouldn't be an issue given how
  255. * infrequently we write, except maybe for write-then-read idioms.
  256. */
  257. #define SET_FX (AT91_UDP_TXPKTRDY)
  258. #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
  259. | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
  260. /* pull OUT packet data from the endpoint's fifo */
  261. static int read_fifo (struct at91_ep *ep, struct at91_request *req)
  262. {
  263. u32 __iomem *creg = ep->creg;
  264. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  265. u32 csr;
  266. u8 *buf;
  267. unsigned int count, bufferspace, is_done;
  268. buf = req->req.buf + req->req.actual;
  269. bufferspace = req->req.length - req->req.actual;
  270. /*
  271. * there might be nothing to read if ep_queue() calls us,
  272. * or if we already emptied both pingpong buffers
  273. */
  274. rescan:
  275. csr = __raw_readl(creg);
  276. if ((csr & RX_DATA_READY) == 0)
  277. return 0;
  278. count = (csr & AT91_UDP_RXBYTECNT) >> 16;
  279. if (count > ep->ep.maxpacket)
  280. count = ep->ep.maxpacket;
  281. if (count > bufferspace) {
  282. DBG("%s buffer overflow\n", ep->ep.name);
  283. req->req.status = -EOVERFLOW;
  284. count = bufferspace;
  285. }
  286. __raw_readsb(dreg, buf, count);
  287. /* release and swap pingpong mem bank */
  288. csr |= CLR_FX;
  289. if (ep->is_pingpong) {
  290. if (ep->fifo_bank == 0) {
  291. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  292. ep->fifo_bank = 1;
  293. } else {
  294. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
  295. ep->fifo_bank = 0;
  296. }
  297. } else
  298. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  299. __raw_writel(csr, creg);
  300. req->req.actual += count;
  301. is_done = (count < ep->ep.maxpacket);
  302. if (count == bufferspace)
  303. is_done = 1;
  304. PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
  305. is_done ? " (done)" : "");
  306. /*
  307. * avoid extra trips through IRQ logic for packets already in
  308. * the fifo ... maybe preventing an extra (expensive) OUT-NAK
  309. */
  310. if (is_done)
  311. done(ep, req, 0);
  312. else if (ep->is_pingpong) {
  313. bufferspace -= count;
  314. buf += count;
  315. goto rescan;
  316. }
  317. return is_done;
  318. }
  319. /* load fifo for an IN packet */
  320. static int write_fifo(struct at91_ep *ep, struct at91_request *req)
  321. {
  322. u32 __iomem *creg = ep->creg;
  323. u32 csr = __raw_readl(creg);
  324. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  325. unsigned total, count, is_last;
  326. /*
  327. * TODO: allow for writing two packets to the fifo ... that'll
  328. * reduce the amount of IN-NAKing, but probably won't affect
  329. * throughput much. (Unlike preventing OUT-NAKing!)
  330. */
  331. /*
  332. * If ep_queue() calls us, the queue is empty and possibly in
  333. * odd states like TXCOMP not yet cleared (we do it, saving at
  334. * least one IRQ) or the fifo not yet being free. Those aren't
  335. * issues normally (IRQ handler fast path).
  336. */
  337. if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
  338. if (csr & AT91_UDP_TXCOMP) {
  339. csr |= CLR_FX;
  340. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  341. __raw_writel(csr, creg);
  342. csr = __raw_readl(creg);
  343. }
  344. if (csr & AT91_UDP_TXPKTRDY)
  345. return 0;
  346. }
  347. total = req->req.length - req->req.actual;
  348. if (ep->ep.maxpacket < total) {
  349. count = ep->ep.maxpacket;
  350. is_last = 0;
  351. } else {
  352. count = total;
  353. is_last = (count < ep->ep.maxpacket) || !req->req.zero;
  354. }
  355. /*
  356. * Write the packet, maybe it's a ZLP.
  357. *
  358. * NOTE: incrementing req->actual before we receive the ACK means
  359. * gadget driver IN bytecounts can be wrong in fault cases. That's
  360. * fixable with PIO drivers like this one (save "count" here, and
  361. * do the increment later on TX irq), but not for most DMA hardware.
  362. *
  363. * So all gadget drivers must accept that potential error. Some
  364. * hardware supports precise fifo status reporting, letting them
  365. * recover when the actual bytecount matters (e.g. for USB Test
  366. * and Measurement Class devices).
  367. */
  368. __raw_writesb(dreg, req->req.buf + req->req.actual, count);
  369. csr &= ~SET_FX;
  370. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  371. __raw_writel(csr, creg);
  372. req->req.actual += count;
  373. PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
  374. is_last ? " (done)" : "");
  375. if (is_last)
  376. done(ep, req, 0);
  377. return is_last;
  378. }
  379. static void nuke(struct at91_ep *ep, int status)
  380. {
  381. struct at91_request *req;
  382. // terminer chaque requete dans la queue
  383. ep->stopped = 1;
  384. if (list_empty(&ep->queue))
  385. return;
  386. VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
  387. while (!list_empty(&ep->queue)) {
  388. req = list_entry(ep->queue.next, struct at91_request, queue);
  389. done(ep, req, status);
  390. }
  391. }
  392. /*-------------------------------------------------------------------------*/
  393. static int at91_ep_enable(struct usb_ep *_ep,
  394. const struct usb_endpoint_descriptor *desc)
  395. {
  396. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  397. struct at91_udc *dev = ep->udc;
  398. u16 maxpacket;
  399. u32 tmp;
  400. unsigned long flags;
  401. if (!_ep || !ep
  402. || !desc || ep->desc
  403. || _ep->name == ep0name
  404. || desc->bDescriptorType != USB_DT_ENDPOINT
  405. || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
  406. || maxpacket > ep->maxpacket) {
  407. DBG("bad ep or descriptor\n");
  408. return -EINVAL;
  409. }
  410. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  411. DBG("bogus device state\n");
  412. return -ESHUTDOWN;
  413. }
  414. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  415. switch (tmp) {
  416. case USB_ENDPOINT_XFER_CONTROL:
  417. DBG("only one control endpoint\n");
  418. return -EINVAL;
  419. case USB_ENDPOINT_XFER_INT:
  420. if (maxpacket > 64)
  421. goto bogus_max;
  422. break;
  423. case USB_ENDPOINT_XFER_BULK:
  424. switch (maxpacket) {
  425. case 8:
  426. case 16:
  427. case 32:
  428. case 64:
  429. goto ok;
  430. }
  431. bogus_max:
  432. DBG("bogus maxpacket %d\n", maxpacket);
  433. return -EINVAL;
  434. case USB_ENDPOINT_XFER_ISOC:
  435. if (!ep->is_pingpong) {
  436. DBG("iso requires double buffering\n");
  437. return -EINVAL;
  438. }
  439. break;
  440. }
  441. ok:
  442. local_irq_save(flags);
  443. /* initialize endpoint to match this descriptor */
  444. ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  445. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
  446. ep->stopped = 0;
  447. if (ep->is_in)
  448. tmp |= 0x04;
  449. tmp <<= 8;
  450. tmp |= AT91_UDP_EPEDS;
  451. __raw_writel(tmp, ep->creg);
  452. ep->desc = desc;
  453. ep->ep.maxpacket = maxpacket;
  454. /*
  455. * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
  456. * since endpoint resets don't reset hw pingpong state.
  457. */
  458. at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
  459. at91_udp_write(dev, AT91_UDP_RST_EP, 0);
  460. local_irq_restore(flags);
  461. return 0;
  462. }
  463. static int at91_ep_disable (struct usb_ep * _ep)
  464. {
  465. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  466. struct at91_udc *udc = ep->udc;
  467. unsigned long flags;
  468. if (ep == &ep->udc->ep[0])
  469. return -EINVAL;
  470. local_irq_save(flags);
  471. nuke(ep, -ESHUTDOWN);
  472. /* restore the endpoint's pristine config */
  473. ep->desc = NULL;
  474. ep->ep.maxpacket = ep->maxpacket;
  475. /* reset fifos and endpoint */
  476. if (ep->udc->clocked) {
  477. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  478. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  479. __raw_writel(0, ep->creg);
  480. }
  481. local_irq_restore(flags);
  482. return 0;
  483. }
  484. /*
  485. * this is a PIO-only driver, so there's nothing
  486. * interesting for request or buffer allocation.
  487. */
  488. static struct usb_request *
  489. at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  490. {
  491. struct at91_request *req;
  492. req = kzalloc(sizeof (struct at91_request), gfp_flags);
  493. if (!req)
  494. return NULL;
  495. INIT_LIST_HEAD(&req->queue);
  496. return &req->req;
  497. }
  498. static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  499. {
  500. struct at91_request *req;
  501. req = container_of(_req, struct at91_request, req);
  502. BUG_ON(!list_empty(&req->queue));
  503. kfree(req);
  504. }
  505. static int at91_ep_queue(struct usb_ep *_ep,
  506. struct usb_request *_req, gfp_t gfp_flags)
  507. {
  508. struct at91_request *req;
  509. struct at91_ep *ep;
  510. struct at91_udc *dev;
  511. int status;
  512. unsigned long flags;
  513. req = container_of(_req, struct at91_request, req);
  514. ep = container_of(_ep, struct at91_ep, ep);
  515. if (!_req || !_req->complete
  516. || !_req->buf || !list_empty(&req->queue)) {
  517. DBG("invalid request\n");
  518. return -EINVAL;
  519. }
  520. if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
  521. DBG("invalid ep\n");
  522. return -EINVAL;
  523. }
  524. dev = ep->udc;
  525. if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  526. DBG("invalid device\n");
  527. return -EINVAL;
  528. }
  529. _req->status = -EINPROGRESS;
  530. _req->actual = 0;
  531. local_irq_save(flags);
  532. /* try to kickstart any empty and idle queue */
  533. if (list_empty(&ep->queue) && !ep->stopped) {
  534. int is_ep0;
  535. /*
  536. * If this control request has a non-empty DATA stage, this
  537. * will start that stage. It works just like a non-control
  538. * request (until the status stage starts, maybe early).
  539. *
  540. * If the data stage is empty, then this starts a successful
  541. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  542. */
  543. is_ep0 = (ep->ep.name == ep0name);
  544. if (is_ep0) {
  545. u32 tmp;
  546. if (!dev->req_pending) {
  547. status = -EINVAL;
  548. goto done;
  549. }
  550. /*
  551. * defer changing CONFG until after the gadget driver
  552. * reconfigures the endpoints.
  553. */
  554. if (dev->wait_for_config_ack) {
  555. tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
  556. tmp ^= AT91_UDP_CONFG;
  557. VDBG("toggle config\n");
  558. at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
  559. }
  560. if (req->req.length == 0) {
  561. ep0_in_status:
  562. PACKET("ep0 in/status\n");
  563. status = 0;
  564. tmp = __raw_readl(ep->creg);
  565. tmp &= ~SET_FX;
  566. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  567. __raw_writel(tmp, ep->creg);
  568. dev->req_pending = 0;
  569. goto done;
  570. }
  571. }
  572. if (ep->is_in)
  573. status = write_fifo(ep, req);
  574. else {
  575. status = read_fifo(ep, req);
  576. /* IN/STATUS stage is otherwise triggered by irq */
  577. if (status && is_ep0)
  578. goto ep0_in_status;
  579. }
  580. } else
  581. status = 0;
  582. if (req && !status) {
  583. list_add_tail (&req->queue, &ep->queue);
  584. at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
  585. }
  586. done:
  587. local_irq_restore(flags);
  588. return (status < 0) ? status : 0;
  589. }
  590. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  591. {
  592. struct at91_ep *ep;
  593. struct at91_request *req;
  594. ep = container_of(_ep, struct at91_ep, ep);
  595. if (!_ep || ep->ep.name == ep0name)
  596. return -EINVAL;
  597. /* make sure it's actually queued on this endpoint */
  598. list_for_each_entry (req, &ep->queue, queue) {
  599. if (&req->req == _req)
  600. break;
  601. }
  602. if (&req->req != _req)
  603. return -EINVAL;
  604. done(ep, req, -ECONNRESET);
  605. return 0;
  606. }
  607. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  608. {
  609. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  610. struct at91_udc *udc = ep->udc;
  611. u32 __iomem *creg;
  612. u32 csr;
  613. unsigned long flags;
  614. int status = 0;
  615. if (!_ep || ep->is_iso || !ep->udc->clocked)
  616. return -EINVAL;
  617. creg = ep->creg;
  618. local_irq_save(flags);
  619. csr = __raw_readl(creg);
  620. /*
  621. * fail with still-busy IN endpoints, ensuring correct sequencing
  622. * of data tx then stall. note that the fifo rx bytecount isn't
  623. * completely accurate as a tx bytecount.
  624. */
  625. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  626. status = -EAGAIN;
  627. else {
  628. csr |= CLR_FX;
  629. csr &= ~SET_FX;
  630. if (value) {
  631. csr |= AT91_UDP_FORCESTALL;
  632. VDBG("halt %s\n", ep->ep.name);
  633. } else {
  634. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  635. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  636. csr &= ~AT91_UDP_FORCESTALL;
  637. }
  638. __raw_writel(csr, creg);
  639. }
  640. local_irq_restore(flags);
  641. return status;
  642. }
  643. static const struct usb_ep_ops at91_ep_ops = {
  644. .enable = at91_ep_enable,
  645. .disable = at91_ep_disable,
  646. .alloc_request = at91_ep_alloc_request,
  647. .free_request = at91_ep_free_request,
  648. .queue = at91_ep_queue,
  649. .dequeue = at91_ep_dequeue,
  650. .set_halt = at91_ep_set_halt,
  651. // there's only imprecise fifo status reporting
  652. };
  653. /*-------------------------------------------------------------------------*/
  654. static int at91_get_frame(struct usb_gadget *gadget)
  655. {
  656. struct at91_udc *udc = to_udc(gadget);
  657. if (!to_udc(gadget)->clocked)
  658. return -EINVAL;
  659. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  660. }
  661. static int at91_wakeup(struct usb_gadget *gadget)
  662. {
  663. struct at91_udc *udc = to_udc(gadget);
  664. u32 glbstate;
  665. int status = -EINVAL;
  666. unsigned long flags;
  667. DBG("%s\n", __FUNCTION__ );
  668. local_irq_save(flags);
  669. if (!udc->clocked || !udc->suspended)
  670. goto done;
  671. /* NOTE: some "early versions" handle ESR differently ... */
  672. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  673. if (!(glbstate & AT91_UDP_ESR))
  674. goto done;
  675. glbstate |= AT91_UDP_ESR;
  676. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  677. done:
  678. local_irq_restore(flags);
  679. return status;
  680. }
  681. /* reinit == restore inital software state */
  682. static void udc_reinit(struct at91_udc *udc)
  683. {
  684. u32 i;
  685. INIT_LIST_HEAD(&udc->gadget.ep_list);
  686. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  687. for (i = 0; i < NUM_ENDPOINTS; i++) {
  688. struct at91_ep *ep = &udc->ep[i];
  689. if (i != 0)
  690. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  691. ep->desc = NULL;
  692. ep->stopped = 0;
  693. ep->fifo_bank = 0;
  694. ep->ep.maxpacket = ep->maxpacket;
  695. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  696. // initialiser une queue par endpoint
  697. INIT_LIST_HEAD(&ep->queue);
  698. }
  699. }
  700. static void stop_activity(struct at91_udc *udc)
  701. {
  702. struct usb_gadget_driver *driver = udc->driver;
  703. int i;
  704. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  705. driver = NULL;
  706. udc->gadget.speed = USB_SPEED_UNKNOWN;
  707. udc->suspended = 0;
  708. for (i = 0; i < NUM_ENDPOINTS; i++) {
  709. struct at91_ep *ep = &udc->ep[i];
  710. ep->stopped = 1;
  711. nuke(ep, -ESHUTDOWN);
  712. }
  713. if (driver)
  714. driver->disconnect(&udc->gadget);
  715. udc_reinit(udc);
  716. }
  717. static void clk_on(struct at91_udc *udc)
  718. {
  719. if (udc->clocked)
  720. return;
  721. udc->clocked = 1;
  722. clk_enable(udc->iclk);
  723. clk_enable(udc->fclk);
  724. }
  725. static void clk_off(struct at91_udc *udc)
  726. {
  727. if (!udc->clocked)
  728. return;
  729. udc->clocked = 0;
  730. udc->gadget.speed = USB_SPEED_UNKNOWN;
  731. clk_disable(udc->fclk);
  732. clk_disable(udc->iclk);
  733. }
  734. /*
  735. * activate/deactivate link with host; minimize power usage for
  736. * inactive links by cutting clocks and transceiver power.
  737. */
  738. static void pullup(struct at91_udc *udc, int is_on)
  739. {
  740. int active = !udc->board.pullup_active_low;
  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. gpio_set_value(udc->board.pullup_pin, active);
  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. gpio_set_value(udc->board.pullup_pin, !active);
  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 = gpio_get_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\n");
  1424. return -ENODEV;
  1425. }
  1426. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1427. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1428. DBG("invalid resource type\n");
  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. /* rm9200 needs manual D+ pullup; off by default */
  1447. if (cpu_is_at91rm9200()) {
  1448. if (udc->board.pullup_pin <= 0) {
  1449. DBG("no D+ pullup?\n");
  1450. retval = -ENODEV;
  1451. goto fail0;
  1452. }
  1453. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1454. if (retval) {
  1455. DBG("D+ pullup is busy\n");
  1456. goto fail0;
  1457. }
  1458. gpio_direction_output(udc->board.pullup_pin,
  1459. udc->board.pullup_active_low);
  1460. }
  1461. udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
  1462. if (!udc->udp_baseaddr) {
  1463. retval = -ENOMEM;
  1464. goto fail0a;
  1465. }
  1466. udc_reinit(udc);
  1467. /* get interface and function clocks */
  1468. udc->iclk = clk_get(dev, "udc_clk");
  1469. udc->fclk = clk_get(dev, "udpck");
  1470. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1471. DBG("clocks missing\n");
  1472. retval = -ENODEV;
  1473. /* NOTE: we "know" here that refcounts on these are NOPs */
  1474. goto fail0b;
  1475. }
  1476. retval = device_register(&udc->gadget.dev);
  1477. if (retval < 0)
  1478. goto fail0b;
  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. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1489. IRQF_DISABLED, driver_name, udc);
  1490. if (retval < 0) {
  1491. DBG("request irq %d failed\n", udc->udp_irq);
  1492. goto fail1;
  1493. }
  1494. if (udc->board.vbus_pin > 0) {
  1495. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1496. if (retval < 0) {
  1497. DBG("request vbus pin failed\n");
  1498. goto fail2;
  1499. }
  1500. gpio_direction_input(udc->board.vbus_pin);
  1501. /*
  1502. * Get the initial state of VBUS - we cannot expect
  1503. * a pending interrupt.
  1504. */
  1505. udc->vbus = gpio_get_value(udc->board.vbus_pin);
  1506. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1507. IRQF_DISABLED, driver_name, udc)) {
  1508. DBG("request vbus irq %d failed\n",
  1509. udc->board.vbus_pin);
  1510. free_irq(udc->udp_irq, udc);
  1511. retval = -EBUSY;
  1512. goto fail3;
  1513. }
  1514. } else {
  1515. DBG("no VBUS detection, assuming always-on\n");
  1516. udc->vbus = 1;
  1517. }
  1518. dev_set_drvdata(dev, udc);
  1519. device_init_wakeup(dev, 1);
  1520. create_debug_file(udc);
  1521. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1522. return 0;
  1523. fail3:
  1524. if (udc->board.vbus_pin > 0)
  1525. gpio_free(udc->board.vbus_pin);
  1526. fail2:
  1527. free_irq(udc->udp_irq, udc);
  1528. fail1:
  1529. device_unregister(&udc->gadget.dev);
  1530. fail0b:
  1531. iounmap(udc->udp_baseaddr);
  1532. fail0a:
  1533. if (cpu_is_at91rm9200())
  1534. gpio_free(udc->board.pullup_pin);
  1535. fail0:
  1536. release_mem_region(res->start, res->end - res->start + 1);
  1537. DBG("%s probe failed, %d\n", driver_name, retval);
  1538. return retval;
  1539. }
  1540. static int __exit at91udc_remove(struct platform_device *pdev)
  1541. {
  1542. struct at91_udc *udc = platform_get_drvdata(pdev);
  1543. struct resource *res;
  1544. DBG("remove\n");
  1545. if (udc->driver)
  1546. return -EBUSY;
  1547. pullup(udc, 0);
  1548. device_init_wakeup(&pdev->dev, 0);
  1549. remove_debug_file(udc);
  1550. if (udc->board.vbus_pin > 0) {
  1551. free_irq(udc->board.vbus_pin, udc);
  1552. gpio_free(udc->board.vbus_pin);
  1553. }
  1554. free_irq(udc->udp_irq, udc);
  1555. device_unregister(&udc->gadget.dev);
  1556. iounmap(udc->udp_baseaddr);
  1557. if (cpu_is_at91rm9200())
  1558. gpio_free(udc->board.pullup_pin);
  1559. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1560. release_mem_region(res->start, res->end - res->start + 1);
  1561. clk_put(udc->iclk);
  1562. clk_put(udc->fclk);
  1563. return 0;
  1564. }
  1565. #ifdef CONFIG_PM
  1566. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1567. {
  1568. struct at91_udc *udc = platform_get_drvdata(pdev);
  1569. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1570. /* Unless we can act normally to the host (letting it wake us up
  1571. * whenever it has work for us) force disconnect. Wakeup requires
  1572. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1573. * tokens) and VBUS irqs (on systems which support them).
  1574. */
  1575. if ((!udc->suspended && udc->addr)
  1576. || !wake
  1577. || at91_suspend_entering_slow_clock()) {
  1578. pullup(udc, 0);
  1579. wake = 0;
  1580. } else
  1581. enable_irq_wake(udc->udp_irq);
  1582. udc->active_suspend = wake;
  1583. if (udc->board.vbus_pin > 0 && wake)
  1584. enable_irq_wake(udc->board.vbus_pin);
  1585. return 0;
  1586. }
  1587. static int at91udc_resume(struct platform_device *pdev)
  1588. {
  1589. struct at91_udc *udc = platform_get_drvdata(pdev);
  1590. if (udc->board.vbus_pin > 0 && udc->active_suspend)
  1591. disable_irq_wake(udc->board.vbus_pin);
  1592. /* maybe reconnect to host; if so, clocks on */
  1593. if (udc->active_suspend)
  1594. disable_irq_wake(udc->udp_irq);
  1595. else
  1596. pullup(udc, 1);
  1597. return 0;
  1598. }
  1599. #else
  1600. #define at91udc_suspend NULL
  1601. #define at91udc_resume NULL
  1602. #endif
  1603. static struct platform_driver at91_udc_driver = {
  1604. .remove = __exit_p(at91udc_remove),
  1605. .shutdown = at91udc_shutdown,
  1606. .suspend = at91udc_suspend,
  1607. .resume = at91udc_resume,
  1608. .driver = {
  1609. .name = (char *) driver_name,
  1610. .owner = THIS_MODULE,
  1611. },
  1612. };
  1613. static int __init udc_init_module(void)
  1614. {
  1615. return platform_driver_probe(&at91_udc_driver, at91udc_probe);
  1616. }
  1617. module_init(udc_init_module);
  1618. static void __exit udc_exit_module(void)
  1619. {
  1620. platform_driver_unregister(&at91_udc_driver);
  1621. }
  1622. module_exit(udc_exit_module);
  1623. MODULE_DESCRIPTION("AT91 udc driver");
  1624. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1625. MODULE_LICENSE("GPL");
  1626. MODULE_ALIAS("platform:at91_udc");