at91_udc.c 44 KB

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