at91_udc.c 47 KB

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