PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/usb/gadget/at91_udc.c

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