at91_udc.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. /*
  2. * at91_udc -- driver for at91-series USB peripheral controller
  3. *
  4. * Copyright (C) 2004 by Thomas Rathbone
  5. * Copyright (C) 2005 by HP Labs
  6. * Copyright (C) 2005 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. #undef VERBOSE_DEBUG
  24. #undef PACKET_TRACE
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/init.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/clk.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <asm/byteorder.h>
  40. #include <mach/hardware.h>
  41. #include <asm/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/system.h>
  44. #include <asm/gpio.h>
  45. #include <mach/board.h>
  46. #include <mach/cpu.h>
  47. #include <mach/at91sam9261_matrix.h>
  48. #include "at91_udc.h"
  49. /*
  50. * This controller is simple and PIO-only. It's used in many AT91-series
  51. * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
  52. * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
  53. *
  54. * This driver expects the board has been wired with two GPIOs suppporting
  55. * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
  56. * testing hasn't covered such cases.)
  57. *
  58. * The pullup is most important (so it's integrated on sam926x parts). It
  59. * provides software control over whether the host enumerates the device.
  60. *
  61. * The VBUS sensing helps during enumeration, and allows both USB clocks
  62. * (and the transceiver) to stay gated off until they're necessary, saving
  63. * power. During USB suspend, the 48 MHz clock is gated off in hardware;
  64. * it may also be gated off by software during some Linux sleep states.
  65. */
  66. #define DRIVER_VERSION "3 May 2006"
  67. static const char driver_name [] = "at91_udc";
  68. static const char ep0name[] = "ep0";
  69. #define at91_udp_read(dev, reg) \
  70. __raw_readl((dev)->udp_baseaddr + (reg))
  71. #define at91_udp_write(dev, reg, val) \
  72. __raw_writel((val), (dev)->udp_baseaddr + (reg))
  73. /*-------------------------------------------------------------------------*/
  74. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  75. #include <linux/seq_file.h>
  76. static const char debug_filename[] = "driver/udc";
  77. #define FOURBITS "%s%s%s%s"
  78. #define EIGHTBITS FOURBITS FOURBITS
  79. static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
  80. {
  81. static char *types[] = {
  82. "control", "out-iso", "out-bulk", "out-int",
  83. "BOGUS", "in-iso", "in-bulk", "in-int"};
  84. u32 csr;
  85. struct at91_request *req;
  86. unsigned long flags;
  87. local_irq_save(flags);
  88. csr = __raw_readl(ep->creg);
  89. /* NOTE: not collecting per-endpoint irq statistics... */
  90. seq_printf(s, "\n");
  91. seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
  92. ep->ep.name, ep->ep.maxpacket,
  93. ep->is_in ? "in" : "out",
  94. ep->is_iso ? " iso" : "",
  95. ep->is_pingpong
  96. ? (ep->fifo_bank ? "pong" : "ping")
  97. : "",
  98. ep->stopped ? " stopped" : "");
  99. seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
  100. csr,
  101. (csr & 0x07ff0000) >> 16,
  102. (csr & (1 << 15)) ? "enabled" : "disabled",
  103. (csr & (1 << 11)) ? "DATA1" : "DATA0",
  104. types[(csr & 0x700) >> 8],
  105. /* iff type is control then print current direction */
  106. (!(csr & 0x700))
  107. ? ((csr & (1 << 7)) ? " IN" : " OUT")
  108. : "",
  109. (csr & (1 << 6)) ? " rxdatabk1" : "",
  110. (csr & (1 << 5)) ? " forcestall" : "",
  111. (csr & (1 << 4)) ? " txpktrdy" : "",
  112. (csr & (1 << 3)) ? " stallsent" : "",
  113. (csr & (1 << 2)) ? " rxsetup" : "",
  114. (csr & (1 << 1)) ? " rxdatabk0" : "",
  115. (csr & (1 << 0)) ? " txcomp" : "");
  116. if (list_empty (&ep->queue))
  117. seq_printf(s, "\t(queue empty)\n");
  118. else list_for_each_entry (req, &ep->queue, queue) {
  119. unsigned length = req->req.actual;
  120. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  121. &req->req, length,
  122. req->req.length, req->req.buf);
  123. }
  124. local_irq_restore(flags);
  125. }
  126. static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
  127. {
  128. int i;
  129. seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
  130. (mask & (1 << 13)) ? " wakeup" : "",
  131. (mask & (1 << 12)) ? " endbusres" : "",
  132. (mask & (1 << 11)) ? " sofint" : "",
  133. (mask & (1 << 10)) ? " extrsm" : "",
  134. (mask & (1 << 9)) ? " rxrsm" : "",
  135. (mask & (1 << 8)) ? " rxsusp" : "");
  136. for (i = 0; i < 8; i++) {
  137. if (mask & (1 << i))
  138. seq_printf(s, " ep%d", i);
  139. }
  140. seq_printf(s, "\n");
  141. }
  142. static int proc_udc_show(struct seq_file *s, void *unused)
  143. {
  144. struct at91_udc *udc = s->private;
  145. struct at91_ep *ep;
  146. u32 tmp;
  147. seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
  148. seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
  149. udc->vbus ? "present" : "off",
  150. udc->enabled
  151. ? (udc->vbus ? "active" : "enabled")
  152. : "disabled",
  153. udc->selfpowered ? "self" : "VBUS",
  154. udc->suspended ? ", suspended" : "",
  155. udc->driver ? udc->driver->driver.name : "(none)");
  156. /* don't access registers when interface isn't clocked */
  157. if (!udc->clocked) {
  158. seq_printf(s, "(not clocked)\n");
  159. return 0;
  160. }
  161. tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
  162. seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
  163. (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
  164. (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
  165. (tmp & AT91_UDP_NUM));
  166. tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
  167. seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
  168. (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
  169. (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
  170. (tmp & AT91_UDP_ESR) ? " esr" : "",
  171. (tmp & AT91_UDP_CONFG) ? " confg" : "",
  172. (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
  173. tmp = at91_udp_read(udc, AT91_UDP_FADDR);
  174. seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
  175. (tmp & AT91_UDP_FEN) ? " fen" : "",
  176. (tmp & AT91_UDP_FADD));
  177. proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
  178. proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
  179. if (udc->enabled && udc->vbus) {
  180. proc_ep_show(s, &udc->ep[0]);
  181. list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
  182. if (ep->desc)
  183. proc_ep_show(s, ep);
  184. }
  185. }
  186. return 0;
  187. }
  188. static int proc_udc_open(struct inode *inode, struct file *file)
  189. {
  190. return single_open(file, proc_udc_show, PDE(inode)->data);
  191. }
  192. static const struct file_operations proc_ops = {
  193. .owner = THIS_MODULE,
  194. .open = proc_udc_open,
  195. .read = seq_read,
  196. .llseek = seq_lseek,
  197. .release = single_release,
  198. };
  199. static void create_debug_file(struct at91_udc *udc)
  200. {
  201. udc->pde = proc_create_data(debug_filename, 0, NULL, &proc_ops, udc);
  202. }
  203. static void remove_debug_file(struct at91_udc *udc)
  204. {
  205. if (udc->pde)
  206. remove_proc_entry(debug_filename, NULL);
  207. }
  208. #else
  209. static inline void create_debug_file(struct at91_udc *udc) {}
  210. static inline void remove_debug_file(struct at91_udc *udc) {}
  211. #endif
  212. /*-------------------------------------------------------------------------*/
  213. static void done(struct at91_ep *ep, struct at91_request *req, int status)
  214. {
  215. unsigned stopped = ep->stopped;
  216. struct at91_udc *udc = ep->udc;
  217. list_del_init(&req->queue);
  218. if (req->req.status == -EINPROGRESS)
  219. req->req.status = status;
  220. else
  221. status = req->req.status;
  222. if (status && status != -ESHUTDOWN)
  223. VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
  224. ep->stopped = 1;
  225. req->req.complete(&ep->ep, &req->req);
  226. ep->stopped = stopped;
  227. /* ep0 is always ready; other endpoints need a non-empty queue */
  228. if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
  229. at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
  230. }
  231. /*-------------------------------------------------------------------------*/
  232. /* bits indicating OUT fifo has data ready */
  233. #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
  234. /*
  235. * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
  236. * back most of the value you just read (because of side effects, including
  237. * bits that may change after reading and before writing).
  238. *
  239. * Except when changing a specific bit, always write values which:
  240. * - clear SET_FX bits (setting them could change something)
  241. * - set CLR_FX bits (clearing them could change something)
  242. *
  243. * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
  244. * that shouldn't normally be changed.
  245. *
  246. * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
  247. * implying a need to wait for one write to complete (test relevant bits)
  248. * before starting the next write. This shouldn't be an issue given how
  249. * infrequently we write, except maybe for write-then-read idioms.
  250. */
  251. #define SET_FX (AT91_UDP_TXPKTRDY)
  252. #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
  253. | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
  254. /* pull OUT packet data from the endpoint's fifo */
  255. static int read_fifo (struct at91_ep *ep, struct at91_request *req)
  256. {
  257. u32 __iomem *creg = ep->creg;
  258. u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
  259. u32 csr;
  260. u8 *buf;
  261. unsigned int count, bufferspace, is_done;
  262. buf = req->req.buf + req->req.actual;
  263. bufferspace = req->req.length - req->req.actual;
  264. /*
  265. * there might be nothing to read if ep_queue() calls us,
  266. * or if we already emptied both pingpong buffers
  267. */
  268. rescan:
  269. csr = __raw_readl(creg);
  270. if ((csr & RX_DATA_READY) == 0)
  271. return 0;
  272. count = (csr & AT91_UDP_RXBYTECNT) >> 16;
  273. if (count > ep->ep.maxpacket)
  274. count = ep->ep.maxpacket;
  275. if (count > bufferspace) {
  276. DBG("%s buffer overflow\n", ep->ep.name);
  277. req->req.status = -EOVERFLOW;
  278. count = bufferspace;
  279. }
  280. __raw_readsb(dreg, buf, count);
  281. /* release and swap pingpong mem bank */
  282. csr |= CLR_FX;
  283. if (ep->is_pingpong) {
  284. if (ep->fifo_bank == 0) {
  285. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  286. ep->fifo_bank = 1;
  287. } else {
  288. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
  289. ep->fifo_bank = 0;
  290. }
  291. } else
  292. csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
  293. __raw_writel(csr, creg);
  294. req->req.actual += count;
  295. is_done = (count < ep->ep.maxpacket);
  296. if (count == bufferspace)
  297. is_done = 1;
  298. PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
  299. is_done ? " (done)" : "");
  300. /*
  301. * avoid extra trips through IRQ logic for packets already in
  302. * the fifo ... maybe preventing an extra (expensive) OUT-NAK
  303. */
  304. if (is_done)
  305. done(ep, req, 0);
  306. else if (ep->is_pingpong) {
  307. /*
  308. * One dummy read to delay the code because of a HW glitch:
  309. * CSR returns bad RXCOUNT when read too soon after updating
  310. * RX_DATA_BK flags.
  311. */
  312. csr = __raw_readl(creg);
  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 = usb_endpoint_type(desc);
  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 = usb_endpoint_dir_in(desc);
  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() || cpu_is_at91sam9g20()) {
  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() || cpu_is_at91sam9g10()) {
  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() || cpu_is_at91sam9g20()) {
  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() || cpu_is_at91sam9g10()) {
  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. int disable_clock = 0;
  1189. if (!udc->clocked) {
  1190. clk_on(udc);
  1191. disable_clock = 1;
  1192. }
  1193. while (rescans--) {
  1194. u32 status;
  1195. status = at91_udp_read(udc, AT91_UDP_ISR)
  1196. & at91_udp_read(udc, AT91_UDP_IMR);
  1197. if (!status)
  1198. break;
  1199. /* USB reset irq: not maskable */
  1200. if (status & AT91_UDP_ENDBUSRES) {
  1201. at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
  1202. at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
  1203. /* Atmel code clears this irq twice */
  1204. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1205. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
  1206. VDBG("end bus reset\n");
  1207. udc->addr = 0;
  1208. stop_activity(udc);
  1209. /* enable ep0 */
  1210. at91_udp_write(udc, AT91_UDP_CSR(0),
  1211. AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
  1212. udc->gadget.speed = USB_SPEED_FULL;
  1213. udc->suspended = 0;
  1214. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
  1215. /*
  1216. * NOTE: this driver keeps clocks off unless the
  1217. * USB host is present. That saves power, but for
  1218. * boards that don't support VBUS detection, both
  1219. * clocks need to be active most of the time.
  1220. */
  1221. /* host initiated suspend (3+ms bus idle) */
  1222. } else if (status & AT91_UDP_RXSUSP) {
  1223. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
  1224. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
  1225. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
  1226. // VDBG("bus suspend\n");
  1227. if (udc->suspended)
  1228. continue;
  1229. udc->suspended = 1;
  1230. /*
  1231. * NOTE: when suspending a VBUS-powered device, the
  1232. * gadget driver should switch into slow clock mode
  1233. * and then into standby to avoid drawing more than
  1234. * 500uA power (2500uA for some high-power configs).
  1235. */
  1236. if (udc->driver && udc->driver->suspend)
  1237. udc->driver->suspend(&udc->gadget);
  1238. /* host initiated resume */
  1239. } else if (status & AT91_UDP_RXRSM) {
  1240. at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
  1241. at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
  1242. at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
  1243. // VDBG("bus resume\n");
  1244. if (!udc->suspended)
  1245. continue;
  1246. udc->suspended = 0;
  1247. /*
  1248. * NOTE: for a VBUS-powered device, the gadget driver
  1249. * would normally want to switch out of slow clock
  1250. * mode into normal mode.
  1251. */
  1252. if (udc->driver && udc->driver->resume)
  1253. udc->driver->resume(&udc->gadget);
  1254. /* endpoint IRQs are cleared by handling them */
  1255. } else {
  1256. int i;
  1257. unsigned mask = 1;
  1258. struct at91_ep *ep = &udc->ep[1];
  1259. if (status & mask)
  1260. handle_ep0(udc);
  1261. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1262. mask <<= 1;
  1263. if (status & mask)
  1264. handle_ep(ep);
  1265. ep++;
  1266. }
  1267. }
  1268. }
  1269. if (disable_clock)
  1270. clk_off(udc);
  1271. return IRQ_HANDLED;
  1272. }
  1273. /*-------------------------------------------------------------------------*/
  1274. static void nop_release(struct device *dev)
  1275. {
  1276. /* nothing to free */
  1277. }
  1278. static struct at91_udc controller = {
  1279. .gadget = {
  1280. .ops = &at91_udc_ops,
  1281. .ep0 = &controller.ep[0].ep,
  1282. .name = driver_name,
  1283. .dev = {
  1284. .init_name = "gadget",
  1285. .release = nop_release,
  1286. }
  1287. },
  1288. .ep[0] = {
  1289. .ep = {
  1290. .name = ep0name,
  1291. .ops = &at91_ep_ops,
  1292. },
  1293. .udc = &controller,
  1294. .maxpacket = 8,
  1295. .int_mask = 1 << 0,
  1296. },
  1297. .ep[1] = {
  1298. .ep = {
  1299. .name = "ep1",
  1300. .ops = &at91_ep_ops,
  1301. },
  1302. .udc = &controller,
  1303. .is_pingpong = 1,
  1304. .maxpacket = 64,
  1305. .int_mask = 1 << 1,
  1306. },
  1307. .ep[2] = {
  1308. .ep = {
  1309. .name = "ep2",
  1310. .ops = &at91_ep_ops,
  1311. },
  1312. .udc = &controller,
  1313. .is_pingpong = 1,
  1314. .maxpacket = 64,
  1315. .int_mask = 1 << 2,
  1316. },
  1317. .ep[3] = {
  1318. .ep = {
  1319. /* could actually do bulk too */
  1320. .name = "ep3-int",
  1321. .ops = &at91_ep_ops,
  1322. },
  1323. .udc = &controller,
  1324. .maxpacket = 8,
  1325. .int_mask = 1 << 3,
  1326. },
  1327. .ep[4] = {
  1328. .ep = {
  1329. .name = "ep4",
  1330. .ops = &at91_ep_ops,
  1331. },
  1332. .udc = &controller,
  1333. .is_pingpong = 1,
  1334. .maxpacket = 256,
  1335. .int_mask = 1 << 4,
  1336. },
  1337. .ep[5] = {
  1338. .ep = {
  1339. .name = "ep5",
  1340. .ops = &at91_ep_ops,
  1341. },
  1342. .udc = &controller,
  1343. .is_pingpong = 1,
  1344. .maxpacket = 256,
  1345. .int_mask = 1 << 5,
  1346. },
  1347. /* ep6 and ep7 are also reserved (custom silicon might use them) */
  1348. };
  1349. static irqreturn_t at91_vbus_irq(int irq, void *_udc)
  1350. {
  1351. struct at91_udc *udc = _udc;
  1352. unsigned value;
  1353. /* vbus needs at least brief debouncing */
  1354. udelay(10);
  1355. value = gpio_get_value(udc->board.vbus_pin);
  1356. if (value != udc->vbus)
  1357. at91_vbus_session(&udc->gadget, value);
  1358. return IRQ_HANDLED;
  1359. }
  1360. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1361. {
  1362. struct at91_udc *udc = &controller;
  1363. int retval;
  1364. if (!driver
  1365. || driver->speed < USB_SPEED_FULL
  1366. || !driver->bind
  1367. || !driver->setup) {
  1368. DBG("bad parameter.\n");
  1369. return -EINVAL;
  1370. }
  1371. if (udc->driver) {
  1372. DBG("UDC already has a gadget driver\n");
  1373. return -EBUSY;
  1374. }
  1375. udc->driver = driver;
  1376. udc->gadget.dev.driver = &driver->driver;
  1377. dev_set_drvdata(&udc->gadget.dev, &driver->driver);
  1378. udc->enabled = 1;
  1379. udc->selfpowered = 1;
  1380. retval = driver->bind(&udc->gadget);
  1381. if (retval) {
  1382. DBG("driver->bind() returned %d\n", retval);
  1383. udc->driver = NULL;
  1384. udc->gadget.dev.driver = NULL;
  1385. dev_set_drvdata(&udc->gadget.dev, NULL);
  1386. udc->enabled = 0;
  1387. udc->selfpowered = 0;
  1388. return retval;
  1389. }
  1390. local_irq_disable();
  1391. pullup(udc, 1);
  1392. local_irq_enable();
  1393. DBG("bound to %s\n", driver->driver.name);
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL (usb_gadget_register_driver);
  1397. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1398. {
  1399. struct at91_udc *udc = &controller;
  1400. if (!driver || driver != udc->driver || !driver->unbind)
  1401. return -EINVAL;
  1402. local_irq_disable();
  1403. udc->enabled = 0;
  1404. at91_udp_write(udc, AT91_UDP_IDR, ~0);
  1405. pullup(udc, 0);
  1406. local_irq_enable();
  1407. driver->unbind(&udc->gadget);
  1408. udc->gadget.dev.driver = NULL;
  1409. dev_set_drvdata(&udc->gadget.dev, NULL);
  1410. udc->driver = NULL;
  1411. DBG("unbound from %s\n", driver->driver.name);
  1412. return 0;
  1413. }
  1414. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1415. /*-------------------------------------------------------------------------*/
  1416. static void at91udc_shutdown(struct platform_device *dev)
  1417. {
  1418. /* force disconnect on reboot */
  1419. pullup(platform_get_drvdata(dev), 0);
  1420. }
  1421. static int __init at91udc_probe(struct platform_device *pdev)
  1422. {
  1423. struct device *dev = &pdev->dev;
  1424. struct at91_udc *udc;
  1425. int retval;
  1426. struct resource *res;
  1427. if (!dev->platform_data) {
  1428. /* small (so we copy it) but critical! */
  1429. DBG("missing platform_data\n");
  1430. return -ENODEV;
  1431. }
  1432. if (pdev->num_resources != 2) {
  1433. DBG("invalid num_resources\n");
  1434. return -ENODEV;
  1435. }
  1436. if ((pdev->resource[0].flags != IORESOURCE_MEM)
  1437. || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
  1438. DBG("invalid resource type\n");
  1439. return -ENODEV;
  1440. }
  1441. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1442. if (!res)
  1443. return -ENXIO;
  1444. if (!request_mem_region(res->start, resource_size(res), driver_name)) {
  1445. DBG("someone's using UDC memory\n");
  1446. return -EBUSY;
  1447. }
  1448. /* init software state */
  1449. udc = &controller;
  1450. udc->gadget.dev.parent = dev;
  1451. udc->board = *(struct at91_udc_data *) dev->platform_data;
  1452. udc->pdev = pdev;
  1453. udc->enabled = 0;
  1454. /* rm9200 needs manual D+ pullup; off by default */
  1455. if (cpu_is_at91rm9200()) {
  1456. if (udc->board.pullup_pin <= 0) {
  1457. DBG("no D+ pullup?\n");
  1458. retval = -ENODEV;
  1459. goto fail0;
  1460. }
  1461. retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
  1462. if (retval) {
  1463. DBG("D+ pullup is busy\n");
  1464. goto fail0;
  1465. }
  1466. gpio_direction_output(udc->board.pullup_pin,
  1467. udc->board.pullup_active_low);
  1468. }
  1469. /* newer chips have more FIFO memory than rm9200 */
  1470. if (cpu_is_at91sam9260()) {
  1471. udc->ep[0].maxpacket = 64;
  1472. udc->ep[3].maxpacket = 64;
  1473. udc->ep[4].maxpacket = 512;
  1474. udc->ep[5].maxpacket = 512;
  1475. } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
  1476. udc->ep[3].maxpacket = 64;
  1477. } else if (cpu_is_at91sam9263()) {
  1478. udc->ep[0].maxpacket = 64;
  1479. udc->ep[3].maxpacket = 64;
  1480. }
  1481. udc->udp_baseaddr = ioremap(res->start, resource_size(res));
  1482. if (!udc->udp_baseaddr) {
  1483. retval = -ENOMEM;
  1484. goto fail0a;
  1485. }
  1486. udc_reinit(udc);
  1487. /* get interface and function clocks */
  1488. udc->iclk = clk_get(dev, "udc_clk");
  1489. udc->fclk = clk_get(dev, "udpck");
  1490. if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
  1491. DBG("clocks missing\n");
  1492. retval = -ENODEV;
  1493. /* NOTE: we "know" here that refcounts on these are NOPs */
  1494. goto fail0b;
  1495. }
  1496. retval = device_register(&udc->gadget.dev);
  1497. if (retval < 0)
  1498. goto fail0b;
  1499. /* don't do anything until we have both gadget driver and VBUS */
  1500. clk_enable(udc->iclk);
  1501. at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
  1502. at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
  1503. /* Clear all pending interrupts - UDP may be used by bootloader. */
  1504. at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
  1505. clk_disable(udc->iclk);
  1506. /* request UDC and maybe VBUS irqs */
  1507. udc->udp_irq = platform_get_irq(pdev, 0);
  1508. retval = request_irq(udc->udp_irq, at91_udc_irq,
  1509. IRQF_DISABLED, driver_name, udc);
  1510. if (retval < 0) {
  1511. DBG("request irq %d failed\n", udc->udp_irq);
  1512. goto fail1;
  1513. }
  1514. if (udc->board.vbus_pin > 0) {
  1515. retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
  1516. if (retval < 0) {
  1517. DBG("request vbus pin failed\n");
  1518. goto fail2;
  1519. }
  1520. gpio_direction_input(udc->board.vbus_pin);
  1521. /*
  1522. * Get the initial state of VBUS - we cannot expect
  1523. * a pending interrupt.
  1524. */
  1525. udc->vbus = gpio_get_value(udc->board.vbus_pin);
  1526. if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
  1527. IRQF_DISABLED, driver_name, udc)) {
  1528. DBG("request vbus irq %d failed\n",
  1529. udc->board.vbus_pin);
  1530. retval = -EBUSY;
  1531. goto fail3;
  1532. }
  1533. } else {
  1534. DBG("no VBUS detection, assuming always-on\n");
  1535. udc->vbus = 1;
  1536. }
  1537. dev_set_drvdata(dev, udc);
  1538. device_init_wakeup(dev, 1);
  1539. create_debug_file(udc);
  1540. INFO("%s version %s\n", driver_name, DRIVER_VERSION);
  1541. return 0;
  1542. fail3:
  1543. if (udc->board.vbus_pin > 0)
  1544. gpio_free(udc->board.vbus_pin);
  1545. fail2:
  1546. free_irq(udc->udp_irq, udc);
  1547. fail1:
  1548. device_unregister(&udc->gadget.dev);
  1549. fail0b:
  1550. iounmap(udc->udp_baseaddr);
  1551. fail0a:
  1552. if (cpu_is_at91rm9200())
  1553. gpio_free(udc->board.pullup_pin);
  1554. fail0:
  1555. release_mem_region(res->start, resource_size(res));
  1556. DBG("%s probe failed, %d\n", driver_name, retval);
  1557. return retval;
  1558. }
  1559. static int __exit at91udc_remove(struct platform_device *pdev)
  1560. {
  1561. struct at91_udc *udc = platform_get_drvdata(pdev);
  1562. struct resource *res;
  1563. DBG("remove\n");
  1564. if (udc->driver)
  1565. return -EBUSY;
  1566. pullup(udc, 0);
  1567. device_init_wakeup(&pdev->dev, 0);
  1568. remove_debug_file(udc);
  1569. if (udc->board.vbus_pin > 0) {
  1570. free_irq(udc->board.vbus_pin, udc);
  1571. gpio_free(udc->board.vbus_pin);
  1572. }
  1573. free_irq(udc->udp_irq, udc);
  1574. device_unregister(&udc->gadget.dev);
  1575. iounmap(udc->udp_baseaddr);
  1576. if (cpu_is_at91rm9200())
  1577. gpio_free(udc->board.pullup_pin);
  1578. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1579. release_mem_region(res->start, resource_size(res));
  1580. clk_put(udc->iclk);
  1581. clk_put(udc->fclk);
  1582. return 0;
  1583. }
  1584. #ifdef CONFIG_PM
  1585. static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  1586. {
  1587. struct at91_udc *udc = platform_get_drvdata(pdev);
  1588. int wake = udc->driver && device_may_wakeup(&pdev->dev);
  1589. /* Unless we can act normally to the host (letting it wake us up
  1590. * whenever it has work for us) force disconnect. Wakeup requires
  1591. * PLLB for USB events (signaling for reset, wakeup, or incoming
  1592. * tokens) and VBUS irqs (on systems which support them).
  1593. */
  1594. if ((!udc->suspended && udc->addr)
  1595. || !wake
  1596. || at91_suspend_entering_slow_clock()) {
  1597. pullup(udc, 0);
  1598. wake = 0;
  1599. } else
  1600. enable_irq_wake(udc->udp_irq);
  1601. udc->active_suspend = wake;
  1602. if (udc->board.vbus_pin > 0 && wake)
  1603. enable_irq_wake(udc->board.vbus_pin);
  1604. return 0;
  1605. }
  1606. static int at91udc_resume(struct platform_device *pdev)
  1607. {
  1608. struct at91_udc *udc = platform_get_drvdata(pdev);
  1609. if (udc->board.vbus_pin > 0 && udc->active_suspend)
  1610. disable_irq_wake(udc->board.vbus_pin);
  1611. /* maybe reconnect to host; if so, clocks on */
  1612. if (udc->active_suspend)
  1613. disable_irq_wake(udc->udp_irq);
  1614. else
  1615. pullup(udc, 1);
  1616. return 0;
  1617. }
  1618. #else
  1619. #define at91udc_suspend NULL
  1620. #define at91udc_resume NULL
  1621. #endif
  1622. static struct platform_driver at91_udc_driver = {
  1623. .remove = __exit_p(at91udc_remove),
  1624. .shutdown = at91udc_shutdown,
  1625. .suspend = at91udc_suspend,
  1626. .resume = at91udc_resume,
  1627. .driver = {
  1628. .name = (char *) driver_name,
  1629. .owner = THIS_MODULE,
  1630. },
  1631. };
  1632. static int __init udc_init_module(void)
  1633. {
  1634. return platform_driver_probe(&at91_udc_driver, at91udc_probe);
  1635. }
  1636. module_init(udc_init_module);
  1637. static void __exit udc_exit_module(void)
  1638. {
  1639. platform_driver_unregister(&at91_udc_driver);
  1640. }
  1641. module_exit(udc_exit_module);
  1642. MODULE_DESCRIPTION("AT91 udc driver");
  1643. MODULE_AUTHOR("Thomas Rathbone, David Brownell");
  1644. MODULE_LICENSE("GPL");
  1645. MODULE_ALIAS("platform:at91_udc");