at91_udc.c 47 KB

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