at91_udc.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
  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. u8 *buf;
  327. /*
  328. * TODO: allow for writing two packets to the fifo ... that'll
  329. * reduce the amount of IN-NAKing, but probably won't affect
  330. * throughput much. (Unlike preventing OUT-NAKing!)
  331. */
  332. /*
  333. * If ep_queue() calls us, the queue is empty and possibly in
  334. * odd states like TXCOMP not yet cleared (we do it, saving at
  335. * least one IRQ) or the fifo not yet being free. Those aren't
  336. * issues normally (IRQ handler fast path).
  337. */
  338. if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
  339. if (csr & AT91_UDP_TXCOMP) {
  340. csr |= CLR_FX;
  341. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  342. __raw_writel(csr, creg);
  343. csr = __raw_readl(creg);
  344. }
  345. if (csr & AT91_UDP_TXPKTRDY)
  346. return 0;
  347. }
  348. buf = req->req.buf + req->req.actual;
  349. prefetch(buf);
  350. total = req->req.length - req->req.actual;
  351. if (ep->ep.maxpacket < total) {
  352. count = ep->ep.maxpacket;
  353. is_last = 0;
  354. } else {
  355. count = total;
  356. is_last = (count < ep->ep.maxpacket) || !req->req.zero;
  357. }
  358. /*
  359. * Write the packet, maybe it's a ZLP.
  360. *
  361. * NOTE: incrementing req->actual before we receive the ACK means
  362. * gadget driver IN bytecounts can be wrong in fault cases. That's
  363. * fixable with PIO drivers like this one (save "count" here, and
  364. * do the increment later on TX irq), but not for most DMA hardware.
  365. *
  366. * So all gadget drivers must accept that potential error. Some
  367. * hardware supports precise fifo status reporting, letting them
  368. * recover when the actual bytecount matters (e.g. for USB Test
  369. * and Measurement Class devices).
  370. */
  371. __raw_writesb(dreg, buf, count);
  372. csr &= ~SET_FX;
  373. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  374. __raw_writel(csr, creg);
  375. req->req.actual += count;
  376. PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
  377. is_last ? " (done)" : "");
  378. if (is_last)
  379. done(ep, req, 0);
  380. return is_last;
  381. }
  382. static void nuke(struct at91_ep *ep, int status)
  383. {
  384. struct at91_request *req;
  385. // terminer chaque requete dans la queue
  386. ep->stopped = 1;
  387. if (list_empty(&ep->queue))
  388. return;
  389. VDBG("%s %s\n", __func__, ep->ep.name);
  390. while (!list_empty(&ep->queue)) {
  391. req = list_entry(ep->queue.next, struct at91_request, queue);
  392. done(ep, req, status);
  393. }
  394. }
  395. /*-------------------------------------------------------------------------*/
  396. static int at91_ep_enable(struct usb_ep *_ep,
  397. const struct usb_endpoint_descriptor *desc)
  398. {
  399. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  400. struct at91_udc *dev = ep->udc;
  401. u16 maxpacket;
  402. u32 tmp;
  403. unsigned long flags;
  404. if (!_ep || !ep
  405. || !desc || ep->desc
  406. || _ep->name == ep0name
  407. || desc->bDescriptorType != USB_DT_ENDPOINT
  408. || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
  409. || maxpacket > ep->maxpacket) {
  410. DBG("bad ep or descriptor\n");
  411. return -EINVAL;
  412. }
  413. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  414. DBG("bogus device state\n");
  415. return -ESHUTDOWN;
  416. }
  417. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  418. switch (tmp) {
  419. case USB_ENDPOINT_XFER_CONTROL:
  420. DBG("only one control endpoint\n");
  421. return -EINVAL;
  422. case USB_ENDPOINT_XFER_INT:
  423. if (maxpacket > 64)
  424. goto bogus_max;
  425. break;
  426. case USB_ENDPOINT_XFER_BULK:
  427. switch (maxpacket) {
  428. case 8:
  429. case 16:
  430. case 32:
  431. case 64:
  432. goto ok;
  433. }
  434. bogus_max:
  435. DBG("bogus maxpacket %d\n", maxpacket);
  436. return -EINVAL;
  437. case USB_ENDPOINT_XFER_ISOC:
  438. if (!ep->is_pingpong) {
  439. DBG("iso requires double buffering\n");
  440. return -EINVAL;
  441. }
  442. break;
  443. }
  444. ok:
  445. local_irq_save(flags);
  446. /* initialize endpoint to match this descriptor */
  447. ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  448. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
  449. ep->stopped = 0;
  450. if (ep->is_in)
  451. tmp |= 0x04;
  452. tmp <<= 8;
  453. tmp |= AT91_UDP_EPEDS;
  454. __raw_writel(tmp, ep->creg);
  455. ep->desc = desc;
  456. ep->ep.maxpacket = maxpacket;
  457. /*
  458. * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
  459. * since endpoint resets don't reset hw pingpong state.
  460. */
  461. at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
  462. at91_udp_write(dev, AT91_UDP_RST_EP, 0);
  463. local_irq_restore(flags);
  464. return 0;
  465. }
  466. static int at91_ep_disable (struct usb_ep * _ep)
  467. {
  468. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  469. struct at91_udc *udc = ep->udc;
  470. unsigned long flags;
  471. if (ep == &ep->udc->ep[0])
  472. return -EINVAL;
  473. local_irq_save(flags);
  474. nuke(ep, -ESHUTDOWN);
  475. /* restore the endpoint's pristine config */
  476. ep->desc = NULL;
  477. ep->ep.maxpacket = ep->maxpacket;
  478. /* reset fifos and endpoint */
  479. if (ep->udc->clocked) {
  480. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  481. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  482. __raw_writel(0, ep->creg);
  483. }
  484. local_irq_restore(flags);
  485. return 0;
  486. }
  487. /*
  488. * this is a PIO-only driver, so there's nothing
  489. * interesting for request or buffer allocation.
  490. */
  491. static struct usb_request *
  492. at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  493. {
  494. struct at91_request *req;
  495. req = kzalloc(sizeof (struct at91_request), gfp_flags);
  496. if (!req)
  497. return NULL;
  498. INIT_LIST_HEAD(&req->queue);
  499. return &req->req;
  500. }
  501. static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  502. {
  503. struct at91_request *req;
  504. req = container_of(_req, struct at91_request, req);
  505. BUG_ON(!list_empty(&req->queue));
  506. kfree(req);
  507. }
  508. static int at91_ep_queue(struct usb_ep *_ep,
  509. struct usb_request *_req, gfp_t gfp_flags)
  510. {
  511. struct at91_request *req;
  512. struct at91_ep *ep;
  513. struct at91_udc *dev;
  514. int status;
  515. unsigned long flags;
  516. req = container_of(_req, struct at91_request, req);
  517. ep = container_of(_ep, struct at91_ep, ep);
  518. if (!_req || !_req->complete
  519. || !_req->buf || !list_empty(&req->queue)) {
  520. DBG("invalid request\n");
  521. return -EINVAL;
  522. }
  523. if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
  524. DBG("invalid ep\n");
  525. return -EINVAL;
  526. }
  527. dev = ep->udc;
  528. if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  529. DBG("invalid device\n");
  530. return -EINVAL;
  531. }
  532. _req->status = -EINPROGRESS;
  533. _req->actual = 0;
  534. local_irq_save(flags);
  535. /* try to kickstart any empty and idle queue */
  536. if (list_empty(&ep->queue) && !ep->stopped) {
  537. int is_ep0;
  538. /*
  539. * If this control request has a non-empty DATA stage, this
  540. * will start that stage. It works just like a non-control
  541. * request (until the status stage starts, maybe early).
  542. *
  543. * If the data stage is empty, then this starts a successful
  544. * IN/STATUS stage. (Unsuccessful ones use set_halt.)
  545. */
  546. is_ep0 = (ep->ep.name == ep0name);
  547. if (is_ep0) {
  548. u32 tmp;
  549. if (!dev->req_pending) {
  550. status = -EINVAL;
  551. goto done;
  552. }
  553. /*
  554. * defer changing CONFG until after the gadget driver
  555. * reconfigures the endpoints.
  556. */
  557. if (dev->wait_for_config_ack) {
  558. tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
  559. tmp ^= AT91_UDP_CONFG;
  560. VDBG("toggle config\n");
  561. at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
  562. }
  563. if (req->req.length == 0) {
  564. ep0_in_status:
  565. PACKET("ep0 in/status\n");
  566. status = 0;
  567. tmp = __raw_readl(ep->creg);
  568. tmp &= ~SET_FX;
  569. tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
  570. __raw_writel(tmp, ep->creg);
  571. dev->req_pending = 0;
  572. goto done;
  573. }
  574. }
  575. if (ep->is_in)
  576. status = write_fifo(ep, req);
  577. else {
  578. status = read_fifo(ep, req);
  579. /* IN/STATUS stage is otherwise triggered by irq */
  580. if (status && is_ep0)
  581. goto ep0_in_status;
  582. }
  583. } else
  584. status = 0;
  585. if (req && !status) {
  586. list_add_tail (&req->queue, &ep->queue);
  587. at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
  588. }
  589. done:
  590. local_irq_restore(flags);
  591. return (status < 0) ? status : 0;
  592. }
  593. static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  594. {
  595. struct at91_ep *ep;
  596. struct at91_request *req;
  597. ep = container_of(_ep, struct at91_ep, ep);
  598. if (!_ep || ep->ep.name == ep0name)
  599. return -EINVAL;
  600. /* make sure it's actually queued on this endpoint */
  601. list_for_each_entry (req, &ep->queue, queue) {
  602. if (&req->req == _req)
  603. break;
  604. }
  605. if (&req->req != _req)
  606. return -EINVAL;
  607. done(ep, req, -ECONNRESET);
  608. return 0;
  609. }
  610. static int at91_ep_set_halt(struct usb_ep *_ep, int value)
  611. {
  612. struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
  613. struct at91_udc *udc = ep->udc;
  614. u32 __iomem *creg;
  615. u32 csr;
  616. unsigned long flags;
  617. int status = 0;
  618. if (!_ep || ep->is_iso || !ep->udc->clocked)
  619. return -EINVAL;
  620. creg = ep->creg;
  621. local_irq_save(flags);
  622. csr = __raw_readl(creg);
  623. /*
  624. * fail with still-busy IN endpoints, ensuring correct sequencing
  625. * of data tx then stall. note that the fifo rx bytecount isn't
  626. * completely accurate as a tx bytecount.
  627. */
  628. if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
  629. status = -EAGAIN;
  630. else {
  631. csr |= CLR_FX;
  632. csr &= ~SET_FX;
  633. if (value) {
  634. csr |= AT91_UDP_FORCESTALL;
  635. VDBG("halt %s\n", ep->ep.name);
  636. } else {
  637. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  638. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  639. csr &= ~AT91_UDP_FORCESTALL;
  640. }
  641. __raw_writel(csr, creg);
  642. }
  643. local_irq_restore(flags);
  644. return status;
  645. }
  646. static const struct usb_ep_ops at91_ep_ops = {
  647. .enable = at91_ep_enable,
  648. .disable = at91_ep_disable,
  649. .alloc_request = at91_ep_alloc_request,
  650. .free_request = at91_ep_free_request,
  651. .queue = at91_ep_queue,
  652. .dequeue = at91_ep_dequeue,
  653. .set_halt = at91_ep_set_halt,
  654. // there's only imprecise fifo status reporting
  655. };
  656. /*-------------------------------------------------------------------------*/
  657. static int at91_get_frame(struct usb_gadget *gadget)
  658. {
  659. struct at91_udc *udc = to_udc(gadget);
  660. if (!to_udc(gadget)->clocked)
  661. return -EINVAL;
  662. return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
  663. }
  664. static int at91_wakeup(struct usb_gadget *gadget)
  665. {
  666. struct at91_udc *udc = to_udc(gadget);
  667. u32 glbstate;
  668. int status = -EINVAL;
  669. unsigned long flags;
  670. DBG("%s\n", __func__ );
  671. local_irq_save(flags);
  672. if (!udc->clocked || !udc->suspended)
  673. goto done;
  674. /* NOTE: some "early versions" handle ESR differently ... */
  675. glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  676. if (!(glbstate & AT91_UDP_ESR))
  677. goto done;
  678. glbstate |= AT91_UDP_ESR;
  679. at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
  680. done:
  681. local_irq_restore(flags);
  682. return status;
  683. }
  684. /* reinit == restore inital software state */
  685. static void udc_reinit(struct at91_udc *udc)
  686. {
  687. u32 i;
  688. INIT_LIST_HEAD(&udc->gadget.ep_list);
  689. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  690. for (i = 0; i < NUM_ENDPOINTS; i++) {
  691. struct at91_ep *ep = &udc->ep[i];
  692. if (i != 0)
  693. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  694. ep->desc = NULL;
  695. ep->stopped = 0;
  696. ep->fifo_bank = 0;
  697. ep->ep.maxpacket = ep->maxpacket;
  698. ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
  699. // initialiser une queue par endpoint
  700. INIT_LIST_HEAD(&ep->queue);
  701. }
  702. }
  703. static void stop_activity(struct at91_udc *udc)
  704. {
  705. struct usb_gadget_driver *driver = udc->driver;
  706. int i;
  707. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  708. driver = NULL;
  709. udc->gadget.speed = USB_SPEED_UNKNOWN;
  710. udc->suspended = 0;
  711. for (i = 0; i < NUM_ENDPOINTS; i++) {
  712. struct at91_ep *ep = &udc->ep[i];
  713. ep->stopped = 1;
  714. nuke(ep, -ESHUTDOWN);
  715. }
  716. if (driver)
  717. driver->disconnect(&udc->gadget);
  718. udc_reinit(udc);
  719. }
  720. static void clk_on(struct at91_udc *udc)
  721. {
  722. if (udc->clocked)
  723. return;
  724. udc->clocked = 1;
  725. clk_enable(udc->iclk);
  726. clk_enable(udc->fclk);
  727. }
  728. static void clk_off(struct at91_udc *udc)
  729. {
  730. if (!udc->clocked)
  731. return;
  732. udc->clocked = 0;
  733. udc->gadget.speed = USB_SPEED_UNKNOWN;
  734. clk_disable(udc->fclk);
  735. clk_disable(udc->iclk);
  736. }
  737. /*
  738. * activate/deactivate link with host; minimize power usage for
  739. * inactive links by cutting clocks and transceiver power.
  740. */
  741. static void pullup(struct at91_udc *udc, int is_on)
  742. {
  743. int active = !udc->board.pullup_active_low;
  744. if (!udc->enabled || !udc->vbus)
  745. is_on = 0;
  746. DBG("%sactive\n", is_on ? "" : "in");
  747. if (is_on) {
  748. clk_on(udc);
  749. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  750. at91_udp_write(udc, AT91_UDP_TXVC, 0);
  751. if (cpu_is_at91rm9200())
  752. gpio_set_value(udc->board.pullup_pin, active);
  753. else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
  754. u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
  755. txvc |= AT91_UDP_TXVC_PUON;
  756. at91_udp_write(udc, AT91_UDP_TXVC, txvc);
  757. } else if (cpu_is_at91sam9261()) {
  758. u32 usbpucr;
  759. usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
  760. usbpucr |= AT91_MATRIX_USBPUCR_PUON;
  761. at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
  762. }
  763. } else {
  764. stop_activity(udc);
  765. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  766. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  767. if (cpu_is_at91rm9200())
  768. gpio_set_value(udc->board.pullup_pin, !active);
  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. clk_off(udc);
  780. }
  781. }
  782. /* vbus is here! turn everything on that's ready */
  783. static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
  784. {
  785. struct at91_udc *udc = to_udc(gadget);
  786. unsigned long flags;
  787. // VDBG("vbus %s\n", is_active ? "on" : "off");
  788. local_irq_save(flags);
  789. udc->vbus = (is_active != 0);
  790. if (udc->driver)
  791. pullup(udc, is_active);
  792. else
  793. pullup(udc, 0);
  794. local_irq_restore(flags);
  795. return 0;
  796. }
  797. static int at91_pullup(struct usb_gadget *gadget, int is_on)
  798. {
  799. struct at91_udc *udc = to_udc(gadget);
  800. unsigned long flags;
  801. local_irq_save(flags);
  802. udc->enabled = is_on = !!is_on;
  803. pullup(udc, is_on);
  804. local_irq_restore(flags);
  805. return 0;
  806. }
  807. static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
  808. {
  809. struct at91_udc *udc = to_udc(gadget);
  810. unsigned long flags;
  811. local_irq_save(flags);
  812. udc->selfpowered = (is_on != 0);
  813. local_irq_restore(flags);
  814. return 0;
  815. }
  816. static const struct usb_gadget_ops at91_udc_ops = {
  817. .get_frame = at91_get_frame,
  818. .wakeup = at91_wakeup,
  819. .set_selfpowered = at91_set_selfpowered,
  820. .vbus_session = at91_vbus_session,
  821. .pullup = at91_pullup,
  822. /*
  823. * VBUS-powered devices may also also want to support bigger
  824. * power budgets after an appropriate SET_CONFIGURATION.
  825. */
  826. // .vbus_power = at91_vbus_power,
  827. };
  828. /*-------------------------------------------------------------------------*/
  829. static int handle_ep(struct at91_ep *ep)
  830. {
  831. struct at91_request *req;
  832. u32 __iomem *creg = ep->creg;
  833. u32 csr = __raw_readl(creg);
  834. if (!list_empty(&ep->queue))
  835. req = list_entry(ep->queue.next,
  836. struct at91_request, queue);
  837. else
  838. req = NULL;
  839. if (ep->is_in) {
  840. if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
  841. csr |= CLR_FX;
  842. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
  843. __raw_writel(csr, creg);
  844. }
  845. if (req)
  846. return write_fifo(ep, req);
  847. } else {
  848. if (csr & AT91_UDP_STALLSENT) {
  849. /* STALLSENT bit == ISOERR */
  850. if (ep->is_iso && req)
  851. req->req.status = -EILSEQ;
  852. csr |= CLR_FX;
  853. csr &= ~(SET_FX | AT91_UDP_STALLSENT);
  854. __raw_writel(csr, creg);
  855. csr = __raw_readl(creg);
  856. }
  857. if (req && (csr & RX_DATA_READY))
  858. return read_fifo(ep, req);
  859. }
  860. return 0;
  861. }
  862. union setup {
  863. u8 raw[8];
  864. struct usb_ctrlrequest r;
  865. };
  866. static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
  867. {
  868. u32 __iomem *creg = ep->creg;
  869. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  870. unsigned rxcount, i = 0;
  871. u32 tmp;
  872. union setup pkt;
  873. int status = 0;
  874. /* read and ack SETUP; hard-fail for bogus packets */
  875. rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
  876. if (likely(rxcount == 8)) {
  877. while (rxcount--)
  878. pkt.raw[i++] = __raw_readb(dreg);
  879. if (pkt.r.bRequestType & USB_DIR_IN) {
  880. csr |= AT91_UDP_DIR;
  881. ep->is_in = 1;
  882. } else {
  883. csr &= ~AT91_UDP_DIR;
  884. ep->is_in = 0;
  885. }
  886. } else {
  887. // REVISIT this happens sometimes under load; why??
  888. ERR("SETUP len %d, csr %08x\n", rxcount, csr);
  889. status = -EINVAL;
  890. }
  891. csr |= CLR_FX;
  892. csr &= ~(SET_FX | AT91_UDP_RXSETUP);
  893. __raw_writel(csr, creg);
  894. udc->wait_for_addr_ack = 0;
  895. udc->wait_for_config_ack = 0;
  896. ep->stopped = 0;
  897. if (unlikely(status != 0))
  898. goto stall;
  899. #define w_index le16_to_cpu(pkt.r.wIndex)
  900. #define w_value le16_to_cpu(pkt.r.wValue)
  901. #define w_length le16_to_cpu(pkt.r.wLength)
  902. VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
  903. pkt.r.bRequestType, pkt.r.bRequest,
  904. w_value, w_index, w_length);
  905. /*
  906. * A few standard requests get handled here, ones that touch
  907. * hardware ... notably for device and endpoint features.
  908. */
  909. udc->req_pending = 1;
  910. csr = __raw_readl(creg);
  911. csr |= CLR_FX;
  912. csr &= ~SET_FX;
  913. switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
  914. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  915. | USB_REQ_SET_ADDRESS:
  916. __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
  917. udc->addr = w_value;
  918. udc->wait_for_addr_ack = 1;
  919. udc->req_pending = 0;
  920. /* FADDR is set later, when we ack host STATUS */
  921. return;
  922. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  923. | USB_REQ_SET_CONFIGURATION:
  924. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
  925. if (pkt.r.wValue)
  926. udc->wait_for_config_ack = (tmp == 0);
  927. else
  928. udc->wait_for_config_ack = (tmp != 0);
  929. if (udc->wait_for_config_ack)
  930. VDBG("wait for config\n");
  931. /* CONFG is toggled later, if gadget driver succeeds */
  932. break;
  933. /*
  934. * Hosts may set or clear remote wakeup status, and
  935. * devices may report they're VBUS powered.
  936. */
  937. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  938. | USB_REQ_GET_STATUS:
  939. tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  940. if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
  941. tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  942. PACKET("get device status\n");
  943. __raw_writeb(tmp, dreg);
  944. __raw_writeb(0, dreg);
  945. goto write_in;
  946. /* then STATUS starts later, automatically */
  947. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  948. | USB_REQ_SET_FEATURE:
  949. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  950. goto stall;
  951. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  952. tmp |= AT91_UDP_ESR;
  953. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  954. goto succeed;
  955. case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
  956. | USB_REQ_CLEAR_FEATURE:
  957. if (w_value != USB_DEVICE_REMOTE_WAKEUP)
  958. goto stall;
  959. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  960. tmp &= ~AT91_UDP_ESR;
  961. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  962. goto succeed;
  963. /*
  964. * Interfaces have no feature settings; this is pretty useless.
  965. * we won't even insist the interface exists...
  966. */
  967. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  968. | USB_REQ_GET_STATUS:
  969. PACKET("get interface status\n");
  970. __raw_writeb(0, dreg);
  971. __raw_writeb(0, dreg);
  972. goto write_in;
  973. /* then STATUS starts later, automatically */
  974. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  975. | USB_REQ_SET_FEATURE:
  976. case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
  977. | USB_REQ_CLEAR_FEATURE:
  978. goto stall;
  979. /*
  980. * Hosts may clear bulk/intr endpoint halt after the gadget
  981. * driver sets it (not widely used); or set it (for testing)
  982. */
  983. case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  984. | USB_REQ_GET_STATUS:
  985. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  986. ep = &udc->ep[tmp];
  987. if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
  988. goto stall;
  989. if (tmp) {
  990. if ((w_index & USB_DIR_IN)) {
  991. if (!ep->is_in)
  992. goto stall;
  993. } else if (ep->is_in)
  994. goto stall;
  995. }
  996. PACKET("get %s status\n", ep->ep.name);
  997. if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
  998. tmp = (1 << USB_ENDPOINT_HALT);
  999. else
  1000. tmp = 0;
  1001. __raw_writeb(tmp, dreg);
  1002. __raw_writeb(0, dreg);
  1003. goto write_in;
  1004. /* then STATUS starts later, automatically */
  1005. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1006. | USB_REQ_SET_FEATURE:
  1007. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1008. ep = &udc->ep[tmp];
  1009. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1010. goto stall;
  1011. if (!ep->desc || ep->is_iso)
  1012. goto stall;
  1013. if ((w_index & USB_DIR_IN)) {
  1014. if (!ep->is_in)
  1015. goto stall;
  1016. } else if (ep->is_in)
  1017. goto stall;
  1018. tmp = __raw_readl(ep->creg);
  1019. tmp &= ~SET_FX;
  1020. tmp |= CLR_FX | AT91_UDP_FORCESTALL;
  1021. __raw_writel(tmp, ep->creg);
  1022. goto succeed;
  1023. case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
  1024. | USB_REQ_CLEAR_FEATURE:
  1025. tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
  1026. ep = &udc->ep[tmp];
  1027. if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
  1028. goto stall;
  1029. if (tmp == 0)
  1030. goto succeed;
  1031. if (!ep->desc || ep->is_iso)
  1032. goto stall;
  1033. if ((w_index & USB_DIR_IN)) {
  1034. if (!ep->is_in)
  1035. goto stall;
  1036. } else if (ep->is_in)
  1037. goto stall;
  1038. at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
  1039. at91_udp_write(udc, AT91_UDP_RST_EP, 0);
  1040. tmp = __raw_readl(ep->creg);
  1041. tmp |= CLR_FX;
  1042. tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
  1043. __raw_writel(tmp, ep->creg);
  1044. if (!list_empty(&ep->queue))
  1045. handle_ep(ep);
  1046. goto succeed;
  1047. }
  1048. #undef w_value
  1049. #undef w_index
  1050. #undef w_length
  1051. /* pass request up to the gadget driver */
  1052. if (udc->driver)
  1053. status = udc->driver->setup(&udc->gadget, &pkt.r);
  1054. else
  1055. status = -ENODEV;
  1056. if (status < 0) {
  1057. stall:
  1058. VDBG("req %02x.%02x protocol STALL; stat %d\n",
  1059. pkt.r.bRequestType, pkt.r.bRequest, status);
  1060. csr |= AT91_UDP_FORCESTALL;
  1061. __raw_writel(csr, creg);
  1062. udc->req_pending = 0;
  1063. }
  1064. return;
  1065. succeed:
  1066. /* immediate successful (IN) STATUS after zero length DATA */
  1067. PACKET("ep0 in/status\n");
  1068. write_in:
  1069. csr |= AT91_UDP_TXPKTRDY;
  1070. __raw_writel(csr, creg);
  1071. udc->req_pending = 0;
  1072. return;
  1073. }
  1074. static void handle_ep0(struct at91_udc *udc)
  1075. {
  1076. struct at91_ep *ep0 = &udc->ep[0];
  1077. u32 __iomem *creg = ep0->creg;
  1078. u32 csr = __raw_readl(creg);
  1079. struct at91_request *req;
  1080. if (unlikely(csr & AT91_UDP_STALLSENT)) {
  1081. nuke(ep0, -EPROTO);
  1082. udc->req_pending = 0;
  1083. csr |= CLR_FX;
  1084. csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
  1085. __raw_writel(csr, creg);
  1086. VDBG("ep0 stalled\n");
  1087. csr = __raw_readl(creg);
  1088. }
  1089. if (csr & AT91_UDP_RXSETUP) {
  1090. nuke(ep0, 0);
  1091. udc->req_pending = 0;
  1092. handle_setup(udc, ep0, csr);
  1093. return;
  1094. }
  1095. if (list_empty(&ep0->queue))
  1096. req = NULL;
  1097. else
  1098. req = list_entry(ep0->queue.next, struct at91_request, queue);
  1099. /* host ACKed an IN packet that we sent */
  1100. if (csr & AT91_UDP_TXCOMP) {
  1101. csr |= CLR_FX;
  1102. csr &= ~(SET_FX | AT91_UDP_TXCOMP);
  1103. /* write more IN DATA? */
  1104. if (req && ep0->is_in) {
  1105. if (handle_ep(ep0))
  1106. udc->req_pending = 0;
  1107. /*
  1108. * Ack after:
  1109. * - last IN DATA packet (including GET_STATUS)
  1110. * - IN/STATUS for OUT DATA
  1111. * - IN/STATUS for any zero-length DATA stage
  1112. * except for the IN DATA case, the host should send
  1113. * an OUT status later, which we'll ack.
  1114. */
  1115. } else {
  1116. udc->req_pending = 0;
  1117. __raw_writel(csr, creg);
  1118. /*
  1119. * SET_ADDRESS takes effect only after the STATUS
  1120. * (to the original address) gets acked.
  1121. */
  1122. if (udc->wait_for_addr_ack) {
  1123. u32 tmp;
  1124. at91_udp_write(udc, AT91_UDP_FADDR,
  1125. AT91_UDP_FEN | udc->addr);
  1126. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  1127. tmp &= ~AT91_UDP_FADDEN;
  1128. if (udc->addr)
  1129. tmp |= AT91_UDP_FADDEN;
  1130. at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
  1131. udc->wait_for_addr_ack = 0;
  1132. VDBG("address %d\n", udc->addr);
  1133. }
  1134. }
  1135. }
  1136. /* OUT packet arrived ... */
  1137. else if (csr & AT91_UDP_RX_DATA_BK0) {
  1138. csr |= CLR_FX;
  1139. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  1140. /* OUT DATA stage */
  1141. if (!ep0->is_in) {
  1142. if (req) {
  1143. if (handle_ep(ep0)) {
  1144. /* send IN/STATUS */
  1145. PACKET("ep0 in/status\n");
  1146. csr = __raw_readl(creg);
  1147. csr &= ~SET_FX;
  1148. csr |= CLR_FX | AT91_UDP_TXPKTRDY;
  1149. __raw_writel(csr, creg);
  1150. udc->req_pending = 0;
  1151. }
  1152. } else if (udc->req_pending) {
  1153. /*
  1154. * AT91 hardware has a hard time with this
  1155. * "deferred response" mode for control-OUT
  1156. * transfers. (For control-IN it's fine.)
  1157. *
  1158. * The normal solution leaves OUT data in the
  1159. * fifo until the gadget driver is ready.
  1160. * We couldn't do that here without disabling
  1161. * the IRQ that tells about SETUP packets,
  1162. * e.g. when the host gets impatient...
  1163. *
  1164. * Working around it by copying into a buffer
  1165. * would almost be a non-deferred response,
  1166. * except that it wouldn't permit reliable
  1167. * stalling of the request. Instead, demand
  1168. * that gadget drivers not use this mode.
  1169. */
  1170. DBG("no control-OUT deferred responses!\n");
  1171. __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
  1172. udc->req_pending = 0;
  1173. }
  1174. /* STATUS stage for control-IN; ack. */
  1175. } else {
  1176. PACKET("ep0 out/status ACK\n");
  1177. __raw_writel(csr, creg);
  1178. /* "early" status stage */
  1179. if (req)
  1180. done(ep0, req, 0);
  1181. }
  1182. }
  1183. }
  1184. static irqreturn_t at91_udc_irq (int irq, void *_udc)
  1185. {
  1186. struct at91_udc *udc = _udc;
  1187. u32 rescans = 5;
  1188. while (rescans--) {
  1189. u32 status;
  1190. status = at91_udp_read(udc, AT91_UDP_ISR)
  1191. & at91_udp_read(udc, AT91_UDP_IMR);
  1192. if (!status)
  1193. break;
  1194. /* USB reset irq: not maskable */
  1195. if (status & AT91_UDP_ENDBUSRES) {
  1196. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1197. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1198. /* Atmel code clears this irq twice */
  1199. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1200. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1201. VDBG("end bus reset\n");
  1202. udc->addr = 0;
  1203. stop_activity(udc);
  1204. /* enable ep0 */
  1205. at91_udp_write(udc, AT91_UDP_CSR(0),
  1206. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1207. udc->gadget.speed = USB_SPEED_FULL;
  1208. udc->suspended = 0;
  1209. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1210. /*
  1211. * NOTE: this driver keeps clocks off unless the
  1212. * USB host is present. That saves power, but for
  1213. * boards that don't support VBUS detection, both
  1214. * clocks need to be active most of the time.
  1215. */
  1216. /* host initiated suspend (3+ms bus idle) */
  1217. } else if (status & AT91_UDP_RXSUSP) {
  1218. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1219. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1220. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1221. // VDBG("bus suspend\n");
  1222. if (udc->suspended)
  1223. continue;
  1224. udc->suspended = 1;
  1225. /*
  1226. * NOTE: when suspending a VBUS-powered device, the
  1227. * gadget driver should switch into slow clock mode
  1228. * and then into standby to avoid drawing more than
  1229. * 500uA power (2500uA for some high-power configs).
  1230. */
  1231. if (udc->driver && udc->driver->suspend)
  1232. udc->driver->suspend(&udc->gadget);
  1233. /* host initiated resume */
  1234. } else if (status & AT91_UDP_RXRSM) {
  1235. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1236. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1237. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1238. // VDBG("bus resume\n");
  1239. if (!udc->suspended)
  1240. continue;
  1241. udc->suspended = 0;
  1242. /*
  1243. * NOTE: for a VBUS-powered device, the gadget driver
  1244. * would normally want to switch out of slow clock
  1245. * mode into normal mode.
  1246. */
  1247. if (udc->driver && udc->driver->resume)
  1248. udc->driver->resume(&udc->gadget);
  1249. /* endpoint IRQs are cleared by handling them */
  1250. } else {
  1251. int i;
  1252. unsigned mask = 1;
  1253. struct at91_ep *ep = &udc->ep[1];
  1254. if (status & mask)
  1255. handle_ep0(udc);
  1256. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1257. mask <<= 1;
  1258. if (status & mask)
  1259. handle_ep(ep);
  1260. ep++;
  1261. }
  1262. }
  1263. }
  1264. return IRQ_HANDLED;
  1265. }
  1266. /*-------------------------------------------------------------------------*/
  1267. static void nop_release(struct device *dev)
  1268. {
  1269. /* nothing to free */
  1270. }
  1271. static struct at91_udc controller = {
  1272. .gadget = {
  1273. .ops = &at91_udc_ops,
  1274. .ep0 = &controller.ep[0].ep,
  1275. .name = driver_name,
  1276. .dev = {
  1277. .bus_id = "gadget",
  1278. .release = nop_release,
  1279. }
  1280. },
  1281. .ep[0] = {
  1282. .ep = {
  1283. .name = ep0name,
  1284. .ops = &at91_ep_ops,
  1285. },
  1286. .udc = &controller,
  1287. .maxpacket = 8,
  1288. .int_mask = 1 << 0,
  1289. },
  1290. .ep[1] = {
  1291. .ep = {
  1292. .name = "ep1",
  1293. .ops = &at91_ep_ops,
  1294. },
  1295. .udc = &controller,
  1296. .is_pingpong = 1,
  1297. .maxpacket = 64,
  1298. .int_mask = 1 << 1,
  1299. },
  1300. .ep[2] = {
  1301. .ep = {
  1302. .name = "ep2",
  1303. .ops = &at91_ep_ops,
  1304. },
  1305. .udc = &controller,
  1306. .is_pingpong = 1,
  1307. .maxpacket = 64,
  1308. .int_mask = 1 << 2,
  1309. },
  1310. .ep[3] = {
  1311. .ep = {
  1312. /* could actually do bulk too */
  1313. .name = "ep3-int",
  1314. .ops = &at91_ep_ops,
  1315. },
  1316. .udc = &controller,
  1317. .maxpacket = 8,
  1318. .int_mask = 1 << 3,
  1319. },
  1320. .ep[4] = {
  1321. .ep = {
  1322. .name = "ep4",
  1323. .ops = &at91_ep_ops,
  1324. },
  1325. .udc = &controller,
  1326. .is_pingpong = 1,
  1327. .maxpacket = 256,
  1328. .int_mask = 1 << 4,
  1329. },
  1330. .ep[5] = {
  1331. .ep = {
  1332. .name = "ep5",
  1333. .ops = &at91_ep_ops,
  1334. },
  1335. .udc = &controller,
  1336. .is_pingpong = 1,
  1337. .maxpacket = 256,
  1338. .int_mask = 1 << 5,
  1339. },
  1340. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1341. };
  1342. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1343. {
  1344. struct at91_udc *udc = _udc;
  1345. unsigned value;
  1346. /* vbus needs at least brief debouncing */
  1347. udelay(10);
  1348. value = gpio_get_value(udc->board.vbus_pin);
  1349. if (value != udc->vbus)
  1350. at91_vbus_session(&udc->gadget, value);
  1351. return IRQ_HANDLED;
  1352. }
  1353. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1354. {
  1355. struct at91_udc *udc = &controller;
  1356. int retval;
  1357. if (!driver
  1358. || driver->speed < USB_SPEED_FULL
  1359. || !driver->bind
  1360. || !driver->setup) {
  1361. DBG("bad parameter.\n");
  1362. return -EINVAL;
  1363. }
  1364. if (udc->driver) {
  1365. DBG("UDC already has a gadget driver\n");
  1366. return -EBUSY;
  1367. }
  1368. udc->driver = driver;
  1369. udc->gadget.dev.driver = &driver->driver;
  1370. udc->gadget.dev.driver_data = &driver->driver;
  1371. udc->enabled = 1;
  1372. udc->selfpowered = 1;
  1373. retval = driver->bind(&udc->gadget);
  1374. if (retval) {
  1375. DBG("driver->bind() returned %d\n", retval);
  1376. udc->driver = NULL;
  1377. udc->gadget.dev.driver = NULL;
  1378. udc->gadget.dev.driver_data = NULL;
  1379. udc->enabled = 0;
  1380. udc->selfpowered = 0;
  1381. return retval;
  1382. }
  1383. local_irq_disable();
  1384. pullup(udc, 1);
  1385. local_irq_enable();
  1386. DBG("bound to %s\n", driver->driver.name);
  1387. return 0;
  1388. }
  1389. EXPORT_SYMBOL (usb_gadget_register_driver);
  1390. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1391. {
  1392. struct at91_udc *udc = &controller;
  1393. if (!driver || driver != udc->driver || !driver->unbind)
  1394. return -EINVAL;
  1395. local_irq_disable();
  1396. udc->enabled = 0;
  1397. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1398. pullup(udc, 0);
  1399. local_irq_enable();
  1400. driver->unbind(&udc->gadget);
  1401. udc->gadget.dev.driver = NULL;
  1402. udc->gadget.dev.driver_data = NULL;
  1403. udc->driver = NULL;
  1404. DBG("unbound from %s\n", driver->driver.name);
  1405. return 0;
  1406. }
  1407. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1408. /*-------------------------------------------------------------------------*/
  1409. static void at91udc_shutdown(struct platform_device *dev)
  1410. {
  1411. /* force disconnect on reboot */
  1412. pullup(platform_get_drvdata(dev), 0);
  1413. }
  1414. static int __init at91udc_probe(struct platform_device *pdev)
  1415. {
  1416. struct device *dev = &pdev->dev;
  1417. struct at91_udc *udc;
  1418. int retval;
  1419. struct resource *res;
  1420. if (!dev->platform_data) {
  1421. /* small (so we copy it) but critical! */
  1422. DBG("missing platform_data\n");
  1423. return -ENODEV;
  1424. }
  1425. if (pdev->num_resources != 2) {
  1426. DBG("invalid num_resources\n");
  1427. return -ENODEV;
  1428. }
  1429. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1430. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1431. DBG("invalid resource type\n");
  1432. return -ENODEV;
  1433. }
  1434. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1435. if (!res)
  1436. return -ENXIO;
  1437. if (!request_mem_region(res->start,
  1438. res->end - res->start + 1,
  1439. driver_name)) {
  1440. DBG("someone's using UDC memory\n");
  1441. return -EBUSY;
  1442. }
  1443. /* init software state */
  1444. udc = &controller;
  1445. udc->gadget.dev.parent = dev;
  1446. udc->board = *(struct at91_udc_data *) dev->platform_data;
  1447. udc->pdev = pdev;
  1448. udc->enabled = 0;
  1449. /* rm9200 needs manual D+ pullup; off by default */
  1450. if (cpu_is_at91rm9200()) {
  1451. if (udc->board.pullup_pin <= 0) {
  1452. DBG("no D+ pullup?\n");
  1453. retval = -ENODEV;
  1454. goto fail0;
  1455. }
  1456. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1457. if (retval) {
  1458. DBG("D+ pullup is busy\n");
  1459. goto fail0;
  1460. }
  1461. gpio_direction_output(udc->board.pullup_pin,
  1462. udc->board.pullup_active_low);
  1463. }
  1464. udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
  1465. if (!udc->udp_baseaddr) {
  1466. retval = -ENOMEM;
  1467. goto fail0a;
  1468. }
  1469. udc_reinit(udc);
  1470. /* get interface and function clocks */
  1471. udc->iclk = clk_get(dev, "udc_clk");
  1472. udc->fclk = clk_get(dev, "udpck");
  1473. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1474. DBG("clocks missing\n");
  1475. retval = -ENODEV;
  1476. /* NOTE: we "know" here that refcounts on these are NOPs */
  1477. goto fail0b;
  1478. }
  1479. retval = device_register(&udc->gadget.dev);
  1480. if (retval < 0)
  1481. goto fail0b;
  1482. /* don't do anything until we have both gadget driver and VBUS */
  1483. clk_enable(udc->iclk);
  1484. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1485. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1486. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1487. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1488. clk_disable(udc->iclk);
  1489. /* request UDC and maybe VBUS irqs */
  1490. udc->udp_irq = platform_get_irq(pdev, 0);
  1491. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1492. IRQF_DISABLED, driver_name, udc);
  1493. if (retval < 0) {
  1494. DBG("request irq %d failed\n", udc->udp_irq);
  1495. goto fail1;
  1496. }
  1497. if (udc->board.vbus_pin > 0) {
  1498. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1499. if (retval < 0) {
  1500. DBG("request vbus pin failed\n");
  1501. goto fail2;
  1502. }
  1503. gpio_direction_input(udc->board.vbus_pin);
  1504. /*
  1505. * Get the initial state of VBUS - we cannot expect
  1506. * a pending interrupt.
  1507. */
  1508. udc->vbus = gpio_get_value(udc->board.vbus_pin);
  1509. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1510. IRQF_DISABLED, driver_name, udc)) {
  1511. DBG("request vbus irq %d failed\n",
  1512. udc->board.vbus_pin);
  1513. free_irq(udc->udp_irq, udc);
  1514. retval = -EBUSY;
  1515. goto fail3;
  1516. }
  1517. } else {
  1518. DBG("no VBUS detection, assuming always-on\n");
  1519. udc->vbus = 1;
  1520. }
  1521. dev_set_drvdata(dev, udc);
  1522. device_init_wakeup(dev, 1);
  1523. create_debug_file(udc);
  1524. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1525. return 0;
  1526. fail3:
  1527. if (udc->board.vbus_pin > 0)
  1528. gpio_free(udc->board.vbus_pin);
  1529. fail2:
  1530. free_irq(udc->udp_irq, udc);
  1531. fail1:
  1532. device_unregister(&udc->gadget.dev);
  1533. fail0b:
  1534. iounmap(udc->udp_baseaddr);
  1535. fail0a:
  1536. if (cpu_is_at91rm9200())
  1537. gpio_free(udc->board.pullup_pin);
  1538. fail0:
  1539. release_mem_region(res->start, res->end - res->start + 1);
  1540. DBG("%s probe failed, %d\n", driver_name, retval);
  1541. return retval;
  1542. }
  1543. static int __exit at91udc_remove(struct platform_device *pdev)
  1544. {
  1545. struct at91_udc *udc = platform_get_drvdata(pdev);
  1546. struct resource *res;
  1547. DBG("remove\n");
  1548. if (udc->driver)
  1549. return -EBUSY;
  1550. pullup(udc, 0);
  1551. device_init_wakeup(&pdev->dev, 0);
  1552. remove_debug_file(udc);
  1553. if (udc->board.vbus_pin > 0) {
  1554. free_irq(udc->board.vbus_pin, udc);
  1555. gpio_free(udc->board.vbus_pin);
  1556. }
  1557. free_irq(udc->udp_irq, udc);
  1558. device_unregister(&udc->gadget.dev);
  1559. iounmap(udc->udp_baseaddr);
  1560. if (cpu_is_at91rm9200())
  1561. gpio_free(udc->board.pullup_pin);
  1562. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1563. release_mem_region(res->start, res->end - res->start + 1);
  1564. clk_put(udc->iclk);
  1565. clk_put(udc->fclk);
  1566. return 0;
  1567. }
  1568. #ifdef CONFIG_PM
  1569. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1570. {
  1571. struct at91_udc *udc = platform_get_drvdata(pdev);
  1572. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1573. /* Unless we can act normally to the host (letting it wake us up
  1574. * whenever it has work for us) force disconnect. Wakeup requires
  1575. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1576. * tokens) and VBUS irqs (on systems which support them).
  1577. */
  1578. if ((!udc->suspended && udc->addr)
  1579. || !wake
  1580. || at91_suspend_entering_slow_clock()) {
  1581. pullup(udc, 0);
  1582. wake = 0;
  1583. } else
  1584. enable_irq_wake(udc->udp_irq);
  1585. udc->active_suspend = wake;
  1586. if (udc->board.vbus_pin > 0 && wake)
  1587. enable_irq_wake(udc->board.vbus_pin);
  1588. return 0;
  1589. }
  1590. static int at91udc_resume(struct platform_device *pdev)
  1591. {
  1592. struct at91_udc *udc = platform_get_drvdata(pdev);
  1593. if (udc->board.vbus_pin > 0 && udc->active_suspend)
  1594. disable_irq_wake(udc->board.vbus_pin);
  1595. /* maybe reconnect to host; if so, clocks on */
  1596. if (udc->active_suspend)
  1597. disable_irq_wake(udc->udp_irq);
  1598. else
  1599. pullup(udc, 1);
  1600. return 0;
  1601. }
  1602. #else
  1603. #define at91udc_suspend NULL
  1604. #define at91udc_resume NULL
  1605. #endif
  1606. static struct platform_driver at91_udc_driver = {
  1607. .remove = __exit_p(at91udc_remove),
  1608. .shutdown = at91udc_shutdown,
  1609. .suspend = at91udc_suspend,
  1610. .resume = at91udc_resume,
  1611. .driver = {
  1612. .name = (char *) driver_name,
  1613. .owner = THIS_MODULE,
  1614. },
  1615. };
  1616. static int __init udc_init_module(void)
  1617. {
  1618. return platform_driver_probe(&at91_udc_driver, at91udc_probe);
  1619. }
  1620. module_init(udc_init_module);
  1621. static void __exit udc_exit_module(void)
  1622. {
  1623. platform_driver_unregister(&at91_udc_driver);
  1624. }
  1625. module_exit(udc_exit_module);
  1626. MODULE_DESCRIPTION("AT91 udc driver");
  1627. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1628. MODULE_LICENSE("GPL");
  1629. MODULE_ALIAS("platform:at91_udc");