26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
35 #include <linux/list.h>
38 #include <linux/device.h>
39 #include <linux/usb/ch9.h>
41 #include <linux/prefetch.h>
43 #include <asm/byteorder.h>
46 #include <asm/unaligned.h>
51 #define DRIVER_DESC "TC86C001 USB Device Controller"
52 #define DRIVER_VERSION "30-Oct 2003"
54 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
109 if (ep == &dev->
ep[0])
113 if (ep->
num != usb_endpoint_num(desc))
116 switch (usb_endpoint_type(desc)) {
145 ep->
is_in = usb_endpoint_dir_in(desc);
152 DBG(dev,
"%s out-dma hides short packets\n",
171 tmp = (ep->
dma ? 0x10 : 0x11) << ep->
num;
177 ep->
ep.maxpacket =
max;
180 spin_unlock_irqrestore(&ep->
dev->lock, flags);
182 DBG(dev,
"enable %s %s %s maxpacket %u\n", ep->
ep.name,
183 ep->
is_in ?
"IN" :
"OUT",
184 ep->
dma ?
"dma" :
"pio",
206 readl(®s->int_enable);
212 tmp &= ~(0x11 << ep->
num);
216 tmp &= ~(0x11 << ep->
num);
231 writel(master, ®s->dma_master);
242 static int goku_ep_disable(
struct usb_ep *_ep)
249 if (!_ep || !ep->
ep.desc)
255 VDBG(dev,
"disable %s\n", _ep->
name);
259 ep_reset(dev->
regs, ep);
260 spin_unlock_irqrestore(&dev->
lock, flags);
268 goku_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
274 req = kzalloc(
sizeof *req, gfp_flags);
279 INIT_LIST_HEAD(&req->
queue);
304 list_del_init(&req->
queue);
309 status = req->
req.status;
319 VDBG(dev,
"complete %s req %p stat %d len %u/%u\n",
320 ep->
ep.name, &req->
req, status,
321 req->
req.actual, req->
req.length);
325 spin_unlock(&dev->
lock);
326 req->
req.complete(&ep->
ep, &req->
req);
327 spin_lock(&dev->
lock);
338 length =
min(req->
req.length - req->
req.actual, max);
357 buf = req->
req.buf + req->
req.actual;
372 count = write_packet(ep->
reg_fifo, buf, req, ep->
ep.maxpacket);
378 dev->
ep[0].stopped = 1;
391 VDBG(dev,
"wrote %s %u bytes%s IN %u left %p\n",
392 ep->
ep.name, count, is_last ?
"/last" :
"",
393 req->
req.length - req->
req.actual, req);
412 unsigned bufferspace, is_short, dbuff;
414 regs = ep->
dev->regs;
416 buf = req->
req.buf + req->
req.actual;
422 dbuff = (ep->
num == 1 || ep->
num == 2);
430 bufferspace = req->
req.length - req->
req.actual;
433 if (
likely(ep->
num != 0 || bufferspace != 0)) {
439 if (!(size & PACKET_ACTIVE))
449 is_short = (size < ep->
ep.maxpacket);
451 VDBG(ep->
dev,
"read %s %u bytes%s OUT req %p %u/%u\n",
452 ep->
ep.name, size, is_short ?
"/S" :
"",
453 req, req->
req.actual, req->
req.length);
455 while (
likely(size-- != 0)) {
464 DBG(ep->
dev,
"%s overflow %u\n",
477 if (ep->
dev->req_config)
490 if (dbuff && !list_empty(&ep->
queue)) {
502 pio_irq_enable(
struct goku_udc *dev,
511 pio_irq_disable(
struct goku_udc *dev,
520 pio_advance(
struct goku_ep *ep)
527 (ep->
is_in ? write_fifo : read_fifo)(ep, req);
546 DBG (ep->
dev,
"start, IN active dma %03x!!\n",
556 else if ((req->
req.length % ep->
ep.maxpacket) != 0
571 DBG (ep->
dev,
"start, OUT active dma %03x!!\n",
622 req->
req.actual -= req->
req.dma;
626 VDBG(dev,
"done %s %s dma, %u/%u bytes, req %p\n",
627 ep->
ep.name, ep->
is_in ?
"IN" :
"OUT",
628 req->
req.actual, req->
req.length, req);
631 if (list_empty(&ep->
queue))
634 (
void) start_dma(ep, req);
637 static void abort_dma(
struct goku_ep *ep,
int status)
672 DBG(ep->
dev,
"IN dma active after reset!\n");
687 DBG(ep->
dev,
"OUT dma active after reset!\n");
689 req->
req.actual = (curr - req->
req.dma) + 1;
692 VDBG(ep->
dev,
"%s %s %s %d/%d\n", __func__, ep->
ep.name,
693 ep->
is_in ?
"IN" :
"OUT",
694 req->
req.actual, req->
req.length);
703 req->
req.actual = req->
req.length;
721 || !_req->
buf || !list_empty(&req->
queue)))
743 VDBG(dev,
"%s queue req %p, len %u buf %p\n",
765 status = start_dma(ep, req);
767 status = (ep->
is_in ? write_fifo : read_fifo)(ep, req);
784 pio_irq_enable(dev, dev->
regs, ep->
num);
786 spin_unlock_irqrestore(&dev->
lock, flags);
793 static void nuke(
struct goku_ep *ep,
int status)
798 if (list_empty(&ep->
queue))
801 abort_dma(ep, status);
802 while (!list_empty(&ep->
queue)) {
804 done(ep, req, status);
817 if (!_ep || !_req || (!ep->
ep.desc && ep->
num != 0))
827 VDBG(dev,
"%s %s %s %s %p\n", __func__, _ep->
name,
828 ep->
is_in ?
"IN" :
"OUT",
829 ep->
dma ?
"dma" :
"pio",
836 if (&req->
req == _req)
839 if (&req->
req != _req) {
840 spin_unlock_irqrestore (&dev->
lock, flags);
847 dma_advance(dev, ep);
848 }
else if (!list_empty(&req->
queue))
852 spin_unlock_irqrestore(&dev->
lock, flags);
859 static void goku_clear_halt(
struct goku_ep *ep)
862 VDBG(ep->
dev,
"%s clear halt\n", ep->
ep.name);
870 if (list_empty(&ep->
queue))
874 (
void) start_dma(ep, req);
880 static int goku_set_halt(
struct usb_ep *_ep,
int value)
893 ep->
dev->ep[0].stopped = 1;
898 }
else if (!ep->
ep.desc) {
899 DBG(ep->
dev,
"%s %s inactive?\n", __func__, ep->
ep.name);
904 if (!list_empty(&ep->
queue))
906 else if (ep->
is_in && value
915 VDBG(ep->
dev,
"%s set halt\n", ep->
ep.name);
919 spin_unlock_irqrestore(&ep->
dev->lock, flags);
923 static int goku_fifo_status(
struct usb_ep *_ep)
938 regs = ep->
dev->regs;
941 VDBG(ep->
dev,
"%s %s %u\n", __func__, ep->
ep.name, size);
945 static void goku_fifo_flush(
struct usb_ep *_ep)
954 VDBG(ep->
dev,
"%s %s\n", __func__, ep->
ep.name);
957 if (!ep->
ep.desc && ep->
num != 0) {
958 DBG(ep->
dev,
"%s %s inactive?\n", __func__, ep->
ep.name);
962 regs = ep->
dev->regs;
975 .enable = goku_ep_enable,
976 .disable = goku_ep_disable,
978 .alloc_request = goku_alloc_request,
979 .free_request = goku_free_request,
982 .dequeue = goku_dequeue,
984 .set_halt = goku_set_halt,
985 .fifo_status = goku_fifo_status,
986 .fifo_flush = goku_fifo_flush,
991 static int goku_get_frame(
struct usb_gadget *_gadget)
1001 .get_frame = goku_get_frame,
1002 .start = goku_start,
1010 static inline char *dmastr(
void)
1013 return "(dma disabled)";
1015 return "(dma IN and OUT)";
1020 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1022 static const char proc_node_name [] =
"driver/udc";
1024 #define FOURBITS "%s%s%s%s"
1025 #define EIGHTBITS FOURBITS FOURBITS
1034 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS
"\n",
1043 (mask &
INT_ERR) ?
" err" :
"",
1044 (mask &
INT_SOF) ?
" sof" :
"",
1066 udc_proc_read(
char *
buffer,
char **start,
off_t off,
int count,
1067 int *eof,
void *_dev)
1073 unsigned size =
count;
1074 unsigned long flags;
1075 int i,
t, is_usb_connected;
1088 "%s version: %s %s\n"
1089 "Gadget driver: %s\n"
1092 pci_name(dev->
pdev), driver_desc,
1096 ? ((tmp &
PW_PULLUP) ?
"full speed" :
"powered")
1107 default:
state =
"ep0_?";
break;
1120 t =
scnprintf(next, size,
"\nirqs %lu\ndataset %02x "
1121 "single.bcs %02x.%02x state %x addr %u\n",
1131 "dma %03X =" EIGHTBITS
"%s %s\n", tmp,
1140 (tmp & MST_RD_ENA) ?
" IN" :
"",
1150 for (i = 0; i < 4; i++) {
1154 if (i && !ep->
ep.desc)
1159 "%s %s max %u %s, irqs %lu, "
1160 "status %02x (%s) " FOURBITS
"\n",
1162 ep->
is_in ?
"in" :
"out",
1164 ep->
dma ?
"dma" :
"pio",
1171 s =
"packet";
break;
1175 s =
"tx_err";
break;
1177 s =
"rx_err";
break;
1183 s =
"invalid";
break;
1192 if (t <= 0 || t > size)
1197 if (list_empty(&ep->
queue)) {
1198 t =
scnprintf(next, size,
"\t(nothing queued)\n");
1199 if (t <= 0 || t > size)
1211 tmp -= req->
req.dma;
1214 tmp = req->
req.actual;
1217 "\treq %p len %u/%u buf %p\n",
1218 &req->
req, tmp, req->
req.length,
1220 if (t <= 0 || t > size)
1230 return count -
size;
1237 static void udc_reinit (
struct goku_udc *dev)
1239 static char *names [] = {
"ep0",
"ep1-bulk",
"ep2-bulk",
"ep3-bulk" };
1243 INIT_LIST_HEAD (&dev->
gadget.ep_list);
1249 for (i = 0; i < 4; i++) {
1253 ep->
ep.name = names[
i];
1258 ep->
ep.ops = &goku_ep_ops;
1261 INIT_LIST_HEAD (&ep->
queue);
1266 dev->
ep[0].reg_mode =
NULL;
1268 list_del_init (&dev->
ep[0].ep.ep_list);
1271 static void udc_reset(
struct goku_udc *dev)
1288 static void ep0_start(
struct goku_udc *dev)
1293 VDBG(dev,
"%s\n", __func__);
1306 for (i = 0; i < 4; i++)
1307 dev->
ep[i].irqs = 0;
1323 static void udc_enable(
struct goku_udc *dev)
1326 if (
readl(&dev->
regs->power_detect) & PW_DETECT)
1329 DBG(dev,
"%s\n", __func__);
1371 retval = bind(&dev->
gadget, driver);
1373 DBG(dev,
"bind to driver %s --> error %d\n",
1374 driver->
driver.name, retval);
1385 DBG(dev,
"registered gadget driver '%s'\n", driver->
driver.name);
1394 DBG (dev,
"%s\n", __func__);
1401 for (i = 0; i < 4; i++)
1404 spin_unlock(&dev->
lock);
1406 spin_lock(&dev->
lock);
1416 unsigned long flags;
1420 if (!driver || driver != dev->
driver || !driver->
unbind)
1425 stop_activity(dev, driver);
1426 spin_unlock_irqrestore(&dev->
lock, flags);
1431 DBG(dev,
"unregistered driver '%s'\n", driver->
driver.name);
1437 static void ep0_setup(
struct goku_udc *dev)
1454 nuke(&dev->
ep[0], 0);
1455 dev->
ep[0].stopped = 0;
1457 dev->
ep[0].is_in = 1;
1462 dev->
ep[0].is_in = 0;
1469 switch (
ctrl.bRequest) {
1471 switch (
ctrl.bRequestType) {
1476 (!dev->
ep[tmp].ep.desc && tmp != 0))
1480 if (!dev->
ep[tmp].is_in)
1483 if (dev->
ep[tmp].is_in)
1490 goku_clear_halt(&dev->
ep[tmp]);
1494 dev->
ep[0].stopped = 1;
1501 VDBG(dev,
"clear dev remote wakeup\n");
1515 VDBG(dev,
"SETUP %02x.%02x v%04x i%04x l%04x\n",
1530 spin_unlock (&dev->
lock);
1532 spin_lock (&dev->
lock);
1536 VDBG(dev,
"req %02x.%02x protocol STALL; err %d\n",
1537 ctrl.bRequestType,
ctrl.bRequest, tmp);
1540 dev->
ep[0].stopped = 1;
1547 #define ACK(irqbit) { \
1549 writel(~irqbit, ®s->int_status); \
1559 unsigned i, rescans = 5;
1561 spin_lock(&dev->
lock);
1571 if (stat & INT_SYSERROR) {
1572 ERROR(dev,
"system error\n");
1573 stop_activity(dev, dev->
driver);
1580 if (stat & INT_PWRDETECT) {
1582 if (
readl(&dev->
regs->power_detect) & PW_DETECT) {
1583 VDBG(dev,
"connect\n");
1586 DBG(dev,
"disconnect\n");
1588 stop_activity(dev, dev->
driver);
1607 DBG(dev,
"USB suspend\n");
1611 && dev->
driver->suspend) {
1612 spin_unlock(&dev->
lock);
1614 spin_lock(&dev->
lock);
1618 DBG(dev,
"bogus USB resume %d\n",
1622 DBG(dev,
"USB resume\n");
1626 && dev->
driver->resume) {
1627 spin_unlock(&dev->
lock);
1629 spin_lock(&dev->
lock);
1636 INFO(dev,
"USB reset done, gadget %s\n",
1637 dev->
driver->driver.name);
1645 if (stat & INT_SETUP) {
1650 if (stat & INT_STATUSNAK) {
1651 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1660 if (stat & INT_ENDPOINT0) {
1668 if (stat & INT_MSTRDEND) {
1672 dma_advance(dev, ep);
1674 if (stat & INT_MSTWREND) {
1678 dma_advance(dev, ep);
1680 if (stat & INT_MSTWRTMOUT) {
1681 ACK(INT_MSTWRTMOUT);
1684 ERROR(dev,
"%s write timeout ?\n", ep->
ep.name);
1689 for (i = 1; i < 4; i++) {
1696 if (list_empty (&ep->
queue))
1697 pio_irq_disable(dev, regs, i);
1708 spin_unlock(&dev->
lock);
1710 DBG(dev,
"unhandled irq status: %05x (%05x, %05x)\n", stat,
1719 static void gadget_release(
struct device *_dev)
1730 struct goku_udc *dev = pci_get_drvdata(pdev);
1732 DBG(dev,
"%s\n", __func__);
1738 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1755 pci_set_drvdata(pdev,
NULL);
1757 the_controller =
NULL;
1759 INFO(dev,
"unbind\n");
1776 if (the_controller) {
1789 pr_debug(
"enomem %s\n", pci_name(pdev));
1796 dev->
gadget.ops = &goku_ops;
1802 dev->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1803 dev->
gadget.dev.release = gadget_release;
1809 DBG(dev,
"can't enable, %d\n", retval);
1817 DBG(dev,
"controller already in use\n");
1825 DBG(dev,
"can't map memory\n");
1831 pci_set_drvdata(pdev, dev);
1832 INFO(dev,
"%s\n", driver_desc);
1834 INFO(dev,
"irq %d, pci mem %p\n", pdev->
irq, base);
1841 DBG(dev,
"request interrupt %d failed\n", pdev->
irq);
1850 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1851 create_proc_read_entry(proc_node_name, 0,
NULL, udc_proc_read, dev);
1854 the_controller =
dev;
1892 .
probe = goku_probe,
1893 .
remove = goku_remove,