at91_udc.c 44 KB

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