at91_udc.c 45 KB

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