17 #include <linux/kernel.h>
18 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
25 #include <linux/list.h>
28 #include <linux/prefetch.h>
30 #include <linux/usb/ch9.h>
35 #include <asm/byteorder.h>
36 #include <mach/hardware.h>
41 #include <mach/board.h>
67 #define DRIVER_VERSION "3 May 2006"
70 static const char ep0name[] =
"ep0";
72 #define VBUS_POLL_TIMEOUT msecs_to_jiffies(1000)
74 #define at91_udp_read(udc, reg) \
75 __raw_readl((udc)->udp_baseaddr + (reg))
76 #define at91_udp_write(udc, reg, val) \
77 __raw_writel((val), (udc)->udp_baseaddr + (reg))
81 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
85 static const char debug_filename[] =
"driver/udc";
87 #define FOURBITS "%s%s%s%s"
88 #define EIGHTBITS FOURBITS FOURBITS
92 static char *types[] = {
93 "control",
"out-iso",
"out-bulk",
"out-int",
94 "BOGUS",
"in-iso",
"in-bulk",
"in-int"};
109 ep->
ep.name, ep->
ep.maxpacket,
110 ep->
is_in ?
"in" :
"out",
115 ep->
stopped ?
" stopped" :
"");
116 seq_printf(s,
"csr %08x rxbytes=%d %s %s %s" EIGHTBITS
"\n",
118 (csr & 0x07ff0000) >> 16,
119 (csr & (1 << 15)) ?
"enabled" :
"disabled",
120 (csr & (1 << 11)) ?
"DATA1" :
"DATA0",
121 types[(csr & 0x700) >> 8],
125 ? ((csr & (1 << 7)) ?
" IN" :
" OUT")
127 (csr & (1 << 6)) ?
" rxdatabk1" :
"",
128 (csr & (1 << 5)) ?
" forcestall" :
"",
129 (csr & (1 << 4)) ?
" txpktrdy" :
"",
131 (csr & (1 << 3)) ?
" stallsent" :
"",
132 (csr & (1 << 2)) ?
" rxsetup" :
"",
133 (csr & (1 << 1)) ?
" rxdatabk0" :
"",
134 (csr & (1 << 0)) ?
" txcomp" :
"");
135 if (list_empty (&ep->
queue))
143 req->
req.length, req->
req.buf);
145 spin_unlock_irqrestore(&udc->
lock, flags);
152 seq_printf(s,
"%s %04x:%s%s" FOURBITS, label, mask,
153 (mask & (1 << 13)) ?
" wakeup" :
"",
154 (mask & (1 << 12)) ?
" endbusres" :
"",
156 (mask & (1 << 11)) ?
" sofint" :
"",
157 (mask & (1 << 10)) ?
" extrsm" :
"",
158 (mask & (1 << 9)) ?
" rxrsm" :
"",
159 (mask & (1 << 8)) ?
" rxsusp" :
"");
160 for (i = 0; i < 8; i++) {
175 seq_printf(s,
"vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
176 udc->
vbus ?
"present" :
"off",
178 ? (udc->
vbus ?
"active" :
"enabled")
191 seq_printf(s,
"frame %05x:%s%s frame=%d\n", tmp,
197 seq_printf(s,
"glbstate %02x:%s" FOURBITS
"\n", tmp,
213 proc_ep_show(s, &udc->
ep[0]);
229 .open = proc_udc_open,
261 list_del_init(&req->
queue);
265 status = req->
req.status;
267 VDBG(
"%s done %p, status %d\n", ep->
ep.name, req, status);
270 spin_unlock(&udc->
lock);
271 req->
req.complete(&ep->
ep, &req->
req);
272 spin_lock(&udc->
lock);
283 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
302 #define SET_FX (AT91_UDP_TXPKTRDY)
303 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
304 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
313 unsigned int count, bufferspace, is_done;
315 buf = req->
req.buf + req->
req.actual;
316 bufferspace = req->
req.length - req->
req.actual;
328 if (count > ep->
ep.maxpacket)
329 count = ep->
ep.maxpacket;
330 if (count > bufferspace) {
331 DBG(
"%s buffer overflow\n", ep->
ep.name);
352 is_done = (count < ep->
ep.maxpacket);
353 if (count == bufferspace)
356 PACKET(
"%s %p out/%d%s\n", ep->
ep.name, &req->
req, count,
357 is_done ?
" (done)" :
"");
373 bufferspace -=
count;
387 unsigned total,
count, is_last;
413 buf = req->
req.buf + req->
req.actual;
415 total = req->
req.length - req->
req.actual;
416 if (ep->
ep.maxpacket < total) {
417 count = ep->
ep.maxpacket;
421 is_last = (count < ep->
ep.maxpacket) || !req->
req.zero;
443 PACKET(
"%s %p in/%d%s\n", ep->
ep.name, &req->
req, count,
444 is_last ?
" (done)" :
"");
450 static void nuke(
struct at91_ep *ep,
int status)
456 if (list_empty(&ep->
queue))
459 VDBG(
"%s %s\n", __func__, ep->
ep.name);
460 while (!list_empty(&ep->
queue)) {
462 done(ep, req, status);
468 static int at91_ep_enable(
struct usb_ep *_ep,
478 || !desc || _ep->
name == ep0name
480 || (maxpacket = usb_endpoint_maxp(desc)) == 0
482 DBG(
"bad ep or descriptor\n");
488 DBG(
"bogus device state\n");
492 tmp = usb_endpoint_type(desc);
495 DBG(
"only one control endpoint\n");
510 DBG(
"bogus maxpacket %d\n", maxpacket);
514 DBG(
"iso requires double buffering\n");
524 ep->
is_in = usb_endpoint_dir_in(desc);
533 ep->
ep.maxpacket = maxpacket;
542 spin_unlock_irqrestore(&udc->
lock, flags);
546 static int at91_ep_disable (
struct usb_ep * _ep)
552 if (ep == &ep->
udc->ep[0])
564 if (ep->
udc->clocked) {
570 spin_unlock_irqrestore(&udc->
lock, flags);
580 at91_ep_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
588 INIT_LIST_HEAD(&req->
queue);
601 static int at91_ep_queue(
struct usb_ep *_ep,
614 || !_req->
buf || !list_empty(&req->
queue)) {
615 DBG(
"invalid request\n");
619 if (!_ep || (!ep->
ep.desc && ep->
ep.name != ep0name)) {
627 DBG(
"invalid device\n");
648 is_ep0 = (ep->
ep.name == ep0name);
664 VDBG(
"toggle config\n");
667 if (req->
req.length == 0) {
669 PACKET(
"ep0 in/status\n");
681 status = write_fifo(ep, req);
683 status = read_fifo(ep, req);
686 if (status && is_ep0)
692 if (req && !status) {
697 spin_unlock_irqrestore(&udc->
lock, flags);
698 return (status < 0) ? status : 0;
709 if (!_ep || ep->
ep.name == ep0name)
718 if (&req->
req == _req)
721 if (&req->
req != _req) {
722 spin_unlock_irqrestore(&udc->
lock, flags);
727 spin_unlock_irqrestore(&udc->
lock, flags);
731 static int at91_ep_set_halt(
struct usb_ep *_ep,
int value)
740 if (!_ep || ep->
is_iso || !ep->
udc->clocked)
753 if (ep->
is_in && (!list_empty(&ep->
queue) || (csr >> 16) != 0))
760 VDBG(
"halt %s\n", ep->
ep.name);
769 spin_unlock_irqrestore(&udc->
lock, flags);
773 static const struct usb_ep_ops at91_ep_ops = {
774 .enable = at91_ep_enable,
775 .disable = at91_ep_disable,
776 .alloc_request = at91_ep_alloc_request,
777 .free_request = at91_ep_free_request,
778 .queue = at91_ep_queue,
779 .dequeue = at91_ep_dequeue,
780 .set_halt = at91_ep_set_halt,
786 static int at91_get_frame(
struct usb_gadget *gadget)
788 struct at91_udc *udc = to_udc(gadget);
795 static int at91_wakeup(
struct usb_gadget *gadget)
797 struct at91_udc *udc = to_udc(gadget);
802 DBG(
"%s\n", __func__ );
811 if (!(glbstate & AT91_UDP_ESR))
817 spin_unlock_irqrestore(&udc->
lock, flags);
822 static void udc_reinit(
struct at91_udc *udc)
826 INIT_LIST_HEAD(&udc->
gadget.ep_list);
827 INIT_LIST_HEAD(&udc->
gadget.ep0->ep_list);
840 INIT_LIST_HEAD(&ep->
queue);
844 static void stop_activity(
struct at91_udc *udc)
860 spin_unlock(&udc->
lock);
862 spin_lock(&udc->
lock);
868 static void clk_on(
struct at91_udc *udc)
877 static void clk_off(
struct at91_udc *udc)
891 static void pullup(
struct at91_udc *udc,
int is_on)
897 DBG(
"%sactive\n", is_on ?
"" :
"in");
942 struct at91_udc *udc = to_udc(gadget);
947 udc->
vbus = (is_active != 0);
949 pullup(udc, is_active);
952 spin_unlock_irqrestore(&udc->
lock, flags);
956 static int at91_pullup(
struct usb_gadget *gadget,
int is_on)
958 struct at91_udc *udc = to_udc(gadget);
962 udc->
enabled = is_on = !!is_on;
964 spin_unlock_irqrestore(&udc->
lock, flags);
968 static int at91_set_selfpowered(
struct usb_gadget *gadget,
int is_on)
970 struct at91_udc *udc = to_udc(gadget);
975 spin_unlock_irqrestore(&udc->
lock, flags);
979 static int at91_start(
struct usb_gadget *gadget,
981 static int at91_stop(
struct usb_gadget *gadget,
984 .get_frame = at91_get_frame,
985 .wakeup = at91_wakeup,
986 .set_selfpowered = at91_set_selfpowered,
987 .vbus_session = at91_vbus_session,
988 .pullup = at91_pullup,
989 .udc_start = at91_start,
990 .udc_stop = at91_stop,
1001 static int handle_ep(
struct at91_ep *ep)
1007 if (!list_empty(&ep->
queue))
1020 return write_fifo(ep, req);
1033 return read_fifo(ep, req);
1047 unsigned rxcount, i = 0;
1054 if (
likely(rxcount == 8)) {
1066 ERR(
"SETUP len %d, csr %08x\n", rxcount, csr);
1078 #define w_index le16_to_cpu(pkt.r.wIndex)
1079 #define w_value le16_to_cpu(pkt.r.wValue)
1080 #define w_length le16_to_cpu(pkt.r.wLength)
1082 VDBG(
"SETUP %02x.%02x v%04x i%04x l%04x\n",
1083 pkt.r.bRequestType, pkt.r.bRequest,
1084 w_value, w_index, w_length);
1094 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1113 VDBG(
"wait for config\n");
1126 PACKET(
"get device status\n");
1144 tmp &= ~AT91_UDP_ESR;
1154 PACKET(
"get interface status\n");
1173 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->
ep.desc))
1180 }
else if (ep->
is_in)
1183 PACKET(
"get %s status\n", ep->
ep.name);
1200 if ((w_index & USB_DIR_IN)) {
1203 }
else if (ep->
is_in)
1221 if ((w_index & USB_DIR_IN)) {
1224 }
else if (ep->
is_in)
1233 if (!list_empty(&ep->
queue))
1244 spin_unlock(&udc->
lock);
1246 spin_lock(&udc->
lock);
1252 VDBG(
"req %02x.%02x protocol STALL; stat %d\n",
1253 pkt.r.bRequestType, pkt.r.bRequest, status);
1262 PACKET(
"ep0 in/status\n");
1269 static void handle_ep0(
struct at91_udc *udc)
1276 if (
unlikely(csr & AT91_UDP_STALLSENT)) {
1282 VDBG(
"ep0 stalled\n");
1288 handle_setup(udc, ep0, csr);
1292 if (list_empty(&ep0->
queue))
1298 if (csr & AT91_UDP_TXCOMP) {
1303 if (req && ep0->
is_in) {
1327 AT91_UDP_FEN | udc->
addr);
1348 if (handle_ep(ep0)) {
1350 PACKET(
"ep0 in/status\n");
1375 DBG(
"no control-OUT deferred responses!\n");
1382 PACKET(
"ep0 out/status ACK\n");
1392 static irqreturn_t at91_udc_irq (
int irq,
void *_udc)
1396 int disable_clock = 0;
1397 unsigned long flags;
1421 VDBG(
"end bus reset\n");
1456 spin_unlock(&udc->
lock);
1458 spin_lock(&udc->
lock);
1477 spin_unlock(&udc->
lock);
1479 spin_lock(&udc->
lock);
1502 spin_unlock_irqrestore(&udc->
lock, flags);
1509 static void nop_release(
struct device *
dev)
1516 .ops = &at91_udc_ops,
1517 .ep0 = &controller.
ep[0].ep,
1520 .init_name =
"gadget",
1521 .release = nop_release,
1527 .ops = &at91_ep_ops,
1536 .ops = &at91_ep_ops,
1546 .ops = &at91_ep_ops,
1557 .ops = &at91_ep_ops,
1566 .ops = &at91_ep_ops,
1576 .ops = &at91_ep_ops,
1586 static void at91_vbus_update(
struct at91_udc *udc,
unsigned value)
1588 value ^= udc->
board.vbus_active_low;
1589 if (value != udc->
vbus)
1590 at91_vbus_session(&udc->
gadget, value);
1593 static irqreturn_t at91_vbus_irq(
int irq,
void *_udc)
1615 static void at91_vbus_timer(
unsigned long data)
1628 static int at91_start(
struct usb_gadget *gadget,
1636 udc->
gadget.dev.of_node = udc->
pdev->dev.of_node;
1640 DBG(
"bound to %s\n", driver->
driver.name);
1644 static int at91_stop(
struct usb_gadget *gadget,
1648 unsigned long flags;
1654 spin_unlock_irqrestore(&udc->
lock, flags);
1659 DBG(
"unbound from %s\n", driver->
driver.name);
1667 struct at91_udc *udc = platform_get_drvdata(dev);
1668 unsigned long flags;
1672 pullup(platform_get_drvdata(dev), 0);
1673 spin_unlock_irqrestore(&udc->
lock, flags);
1683 if (of_property_read_u32(np,
"atmel,vbus-polled", &val) == 0)
1705 DBG(
"missing platform_data\n");
1710 DBG(
"invalid num_resources\n");
1715 DBG(
"invalid resource type\n");
1724 DBG(
"someone's using UDC memory\n");
1731 if (pdev->
dev.of_node)
1732 at91udc_of_init(udc, pdev->
dev.of_node);
1742 if (gpio_is_valid(udc->
board.pullup_pin)) {
1743 DBG(
"no D+ pullup?\n");
1749 DBG(
"D+ pullup is busy\n");
1753 udc->
board.pullup_active_low);
1758 udc->
ep[0].maxpacket = 64;
1759 udc->
ep[3].maxpacket = 64;
1760 udc->
ep[4].maxpacket = 512;
1761 udc->
ep[5].maxpacket = 512;
1763 udc->
ep[3].maxpacket = 64;
1765 udc->
ep[0].maxpacket = 64;
1766 udc->
ep[3].maxpacket = 64;
1780 if (IS_ERR(udc->
iclk) || IS_ERR(udc->
fclk)) {
1781 DBG(
"clocks missing\n");
1806 DBG(
"request irq %d failed\n", udc->
udp_irq);
1809 if (gpio_is_valid(udc->
board.vbus_pin)) {
1812 DBG(
"request vbus pin failed\n");
1822 udc->
board.vbus_active_low;
1824 if (udc->
board.vbus_polled) {
1827 (
unsigned long)udc);
1833 DBG(
"request vbus irq %d failed\n",
1834 udc->
board.vbus_pin);
1840 DBG(
"no VBUS detection, assuming always-on\n");
1853 if (gpio_is_valid(udc->
board.vbus_pin) && !udc->
board.vbus_polled)
1856 if (gpio_is_valid(udc->
board.vbus_pin))
1875 struct at91_udc *udc = platform_get_drvdata(pdev);
1877 unsigned long flags;
1887 spin_unlock_irqrestore(&udc->
lock, flags);
1891 if (gpio_is_valid(udc->
board.vbus_pin)) {
1915 struct at91_udc *udc = platform_get_drvdata(pdev);
1916 int wake = udc->
driver && device_may_wakeup(&pdev->
dev);
1917 unsigned long flags;
1930 spin_unlock_irqrestore(&udc->
lock, flags);
1932 enable_irq_wake(udc->
udp_irq);
1935 if (gpio_is_valid(udc->
board.vbus_pin) && !udc->
board.vbus_polled && wake)
1936 enable_irq_wake(udc->
board.vbus_pin);
1942 struct at91_udc *udc = platform_get_drvdata(pdev);
1943 unsigned long flags;
1945 if (gpio_is_valid(udc->
board.vbus_pin) && !udc->
board.vbus_polled &&
1947 disable_irq_wake(udc->
board.vbus_pin);
1951 disable_irq_wake(udc->
udp_irq);
1955 spin_unlock_irqrestore(&udc->
lock, flags);
1960 #define at91udc_suspend NULL
1961 #define at91udc_resume NULL
1964 #if defined(CONFIG_OF)
1974 .remove =
__exit_p(at91udc_remove),
1975 .shutdown = at91udc_shutdown,
1985 static int __init udc_init_module(
void)
1991 static void __exit udc_exit_module(
void)