11 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
18 #include <linux/list.h>
20 #include <linux/usb/ch9.h>
26 #include <mach/board.h>
34 #ifdef CONFIG_USB_GADGET_DEBUG_FS
47 INIT_LIST_HEAD(queue_data);
49 spin_lock_irq(&ep->
udc->lock);
56 spin_unlock_irq(&ep->
udc->lock);
62 spin_unlock_irq(&ep->
udc->lock);
85 static ssize_t queue_dbg_read(
struct file *file,
char __user *
buf,
86 size_t nbytes, loff_t *ppos)
90 size_t len, remaining, actual = 0;
98 len =
snprintf(tmpbuf,
sizeof(tmpbuf),
99 "%8p %08x %c%c%c %5d %c%c%c\n",
100 req->
req.buf, req->
req.length,
101 req->
req.no_interrupt ?
'i' :
'I',
102 req->
req.zero ?
'Z' :
'z',
103 req->
req.short_not_ok ?
's' :
'S',
108 len =
min(len,
sizeof(tmpbuf));
116 actual += len - remaining;
128 static int queue_dbg_release(
struct inode *inode,
struct file *file)
141 static int regs_dbg_open(
struct inode *inode,
struct file *file)
154 spin_lock_irq(&udc->
lock);
155 for (i = 0; i < inode->
i_size / 4; i++)
157 spin_unlock_irq(&udc->
lock);
168 static ssize_t regs_dbg_read(
struct file *file,
char __user *buf,
169 size_t nbytes, loff_t *ppos)
171 struct inode *inode = file->f_dentry->d_inode;
177 file->f_dentry->d_inode->i_size);
183 static int regs_dbg_release(
struct inode *inode,
struct file *file)
191 .open = queue_dbg_open,
193 .read = queue_dbg_read,
194 .release = queue_dbg_release,
199 .open = regs_dbg_open,
201 .read = regs_dbg_read,
202 .release = regs_dbg_release,
205 static void usba_ep_init_debugfs(
struct usba_udc *udc,
213 ep->debugfs_dir = ep_root;
216 ep, &queue_dbg_fops);
217 if (!ep->debugfs_queue)
221 ep->debugfs_dma_status
223 &ep->last_dma_status);
224 if (!ep->debugfs_dma_status)
231 if (!ep->debugfs_state)
246 "failed to create debugfs directory for %s\n", ep->
ep.name);
249 static void usba_ep_cleanup_debugfs(
struct usba_ep *ep)
255 ep->debugfs_dma_status =
NULL;
256 ep->debugfs_dir =
NULL;
259 static void usba_init_debugfs(
struct usba_udc *udc)
265 if (IS_ERR(root) || !root)
267 udc->debugfs_root = root;
275 regs->
d_inode->i_size = resource_size(regs_resource);
276 udc->debugfs_regs =
regs;
278 usba_ep_init_debugfs(udc, to_usba_ep(udc->
gadget.ep0));
285 udc->debugfs_root =
NULL;
286 dev_err(&udc->
pdev->dev,
"debugfs is not available\n");
289 static void usba_cleanup_debugfs(
struct usba_udc *udc)
291 usba_ep_cleanup_debugfs(to_usba_ep(udc->
gadget.ep0));
294 udc->debugfs_regs =
NULL;
295 udc->debugfs_root =
NULL;
298 static inline void usba_ep_init_debugfs(
struct usba_udc *udc,
304 static inline void usba_ep_cleanup_debugfs(
struct usba_ep *ep)
309 static inline void usba_init_debugfs(
struct usba_udc *udc)
314 static inline void usba_cleanup_debugfs(
struct usba_udc *udc)
320 static int vbus_is_present(
struct usba_udc *udc)
329 #if defined(CONFIG_ARCH_AT91SAM9RL)
333 static void toggle_bias(
int is_on)
345 static void toggle_bias(
int is_on)
351 static void next_fifo_transaction(
struct usba_ep *ep,
struct usba_request *req)
353 unsigned int transaction_len;
355 transaction_len = req->
req.length - req->
req.actual;
357 if (transaction_len > ep->
ep.maxpacket) {
358 transaction_len = ep->
ep.maxpacket;
360 }
else if (transaction_len == ep->
ep.maxpacket && req->
req.zero)
363 DBG(
DBG_QUEUE,
"%s: submit_transaction, req %p (length %d)%s\n",
364 ep->
ep.name, req, transaction_len,
369 req->
req.actual += transaction_len;
372 static void submit_request(
struct usba_ep *ep,
struct usba_request *req)
374 DBG(
DBG_QUEUE,
"%s: submit_request: req %p (length %d)\n",
375 ep->
ep.name, req, req->
req.length);
381 if (req->
req.length == 0) {
394 next_fifo_transaction(ep, req);
405 static void submit_next_request(
struct usba_ep *ep)
409 if (list_empty(&ep->
queue)) {
416 submit_request(ep, req);
419 static void send_status(
struct usba_udc *udc,
struct usba_ep *ep)
426 static void receive_data(
struct usba_ep *ep)
431 unsigned int bytecount, nr_busy;
439 while (nr_busy > 0) {
440 if (list_empty(&ep->
queue)) {
449 if (status & (1 << 31))
451 if (req->
req.actual + bytecount >= req->
req.length) {
453 bytecount = req->
req.length - req->
req.actual;
457 ep->
fifo, bytecount);
458 req->
req.actual += bytecount;
465 list_del_init(&req->
queue);
467 spin_unlock(&udc->
lock);
468 req->
req.complete(&ep->
ep, &req->
req);
469 spin_lock(&udc->
lock);
476 send_status(udc, ep);
483 request_complete(
struct usba_ep *ep,
struct usba_request *req,
int status)
494 &udc->
pdev->dev, req->
req.dma, req->
req.length,
501 "%s: req %p complete: status %d, actual %u\n",
502 ep->
ep.name, req, req->
req.status, req->
req.actual);
504 spin_unlock(&udc->
lock);
505 req->
req.complete(&ep->
ep, &req->
req);
506 spin_lock(&udc->
lock);
510 request_complete_list(
struct usba_ep *ep,
struct list_head *
list,
int status)
515 list_del_init(&req->
queue);
516 request_complete(ep, req, status);
523 struct usba_ep *ep = to_usba_ep(_ep);
525 unsigned long flags, ept_cfg, maxpacket;
526 unsigned int nr_trans;
530 maxpacket = usb_endpoint_maxp(desc) & 0x7ff;
548 ept_cfg =
USBA_BF(EPT_SIZE, fls(maxpacket - 1) - 3);
550 DBG(
DBG_HW,
"%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
551 ep->
ep.name, ept_cfg, maxpacket);
553 if (usb_endpoint_dir_in(desc)) {
558 switch (usb_endpoint_type(desc)) {
565 DBG(
DBG_ERR,
"ep_enable: %s is not isoc capable\n",
574 nr_trans = ((usb_endpoint_maxp(desc) >> 11) & 3) + 1;
584 if (nr_trans > 1 && ep->
nr_banks == 3)
588 ept_cfg |=
USBA_BF(NB_TRANS, nr_trans);
603 ep->
ep.maxpacket = maxpacket;
623 spin_unlock_irqrestore(&udc->
lock, flags);
627 DBG(
DBG_HW,
"INT_ENB after init: %#08lx\n",
633 static int usba_ep_disable(
struct usb_ep *_ep)
635 struct usba_ep *ep = to_usba_ep(_ep);
645 spin_unlock_irqrestore(&udc->
lock, flags);
657 list_splice_init(&ep->
queue, &req_list);
668 request_complete_list(ep, &req_list, -
ESHUTDOWN);
670 spin_unlock_irqrestore(&udc->
lock, flags);
676 usba_ep_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
680 DBG(
DBG_GADGET,
"ep_alloc_request: %p, 0x%x\n", _ep, gfp_flags);
682 req = kzalloc(
sizeof(*req), gfp_flags);
686 INIT_LIST_HEAD(&req->
queue);
702 static int queue_dma(
struct usba_udc *udc,
struct usba_ep *ep,
709 ep->
ep.name, req->
req.length, req->
req.dma,
710 req->
req.zero ?
'Z' :
'z',
711 req->
req.short_not_ok ?
'S' :
's',
712 req->
req.no_interrupt ?
'I' :
'i');
714 if (req->
req.length > 0x10000) {
716 DBG(
DBG_ERR,
"invalid request length %u\n", req->
req.length);
724 &udc->
pdev->dev, req->
req.buf, req->
req.length,
729 &udc->
pdev->dev, req->
req.dma, req->
req.length,
749 if (list_empty(&ep->
queue))
750 submit_request(ep, req);
755 spin_unlock_irqrestore(&udc->
lock, flags);
764 struct usba_ep *ep = to_usba_ep(_ep);
784 return queue_dma(udc, ep, req, gfp_flags);
801 spin_unlock_irqrestore(&udc->
lock, flags);
807 usba_update_req(
struct usba_ep *ep,
struct usba_request *req,
u32 status)
812 static int stop_dma(
struct usba_ep *ep,
u32 *pstatus)
824 for (timeout = 40; timeout; --timeout) {
836 "%s: timed out waiting for DMA FIFO to empty\n",
846 struct usba_ep *ep = to_usba_ep(_ep);
865 stop_dma(ep, &status);
867 #ifdef CONFIG_USB_GADGET_DEBUG_FS
868 ep->last_dma_status =
status;
873 usba_update_req(ep, req, status);
881 list_del_init(&req->
queue);
886 submit_next_request(ep);
887 spin_unlock_irqrestore(&udc->
lock, flags);
892 static int usba_ep_set_halt(
struct usb_ep *_ep,
int value)
894 struct usba_ep *ep = to_usba_ep(_ep);
900 value ?
"set" :
"clear");
903 DBG(
DBG_ERR,
"Attempted to halt uninitialized ep %s\n",
908 DBG(
DBG_ERR,
"Attempted to halt isochronous ep %s\n",
919 if (!list_empty(&ep->
queue)
921 &
USBA_BF(BUSY_BANKS, -1L))))) {
932 spin_unlock_irqrestore(&udc->
lock, flags);
937 static int usba_ep_fifo_status(
struct usb_ep *_ep)
939 struct usba_ep *ep = to_usba_ep(_ep);
944 static void usba_ep_fifo_flush(
struct usb_ep *_ep)
946 struct usba_ep *ep = to_usba_ep(_ep);
952 static const struct usb_ep_ops usba_ep_ops = {
953 .enable = usba_ep_enable,
954 .disable = usba_ep_disable,
955 .alloc_request = usba_ep_alloc_request,
956 .free_request = usba_ep_free_request,
957 .queue = usba_ep_queue,
958 .dequeue = usba_ep_dequeue,
959 .set_halt = usba_ep_set_halt,
960 .fifo_status = usba_ep_fifo_status,
961 .fifo_flush = usba_ep_fifo_flush,
964 static int usba_udc_get_frame(
struct usb_gadget *gadget)
966 struct usba_udc *udc = to_usba_udc(gadget);
971 static int usba_udc_wakeup(
struct usb_gadget *gadget)
973 struct usba_udc *udc = to_usba_udc(gadget);
984 spin_unlock_irqrestore(&udc->
lock, flags);
990 usba_udc_set_selfpowered(
struct usb_gadget *gadget,
int is_selfpowered)
992 struct usba_udc *udc = to_usba_udc(gadget);
1000 spin_unlock_irqrestore(&udc->
lock, flags);
1005 static int atmel_usba_start(
struct usb_gadget *gadget,
1007 static int atmel_usba_stop(
struct usb_gadget *gadget,
1010 .get_frame = usba_udc_get_frame,
1011 .wakeup = usba_udc_wakeup,
1012 .set_selfpowered = usba_udc_set_selfpowered,
1013 .udc_start = atmel_usba_start,
1014 .udc_stop = atmel_usba_stop,
1020 .bEndpointAddress = 0,
1027 static void nop_release(
struct device *
dev)
1034 .ops = &usba_udc_ops,
1037 .name =
"atmel_usba_udc",
1039 .init_name =
"gadget",
1040 .release = nop_release,
1048 static void reset_all_endpoints(
struct usba_udc *udc)
1055 ep = to_usba_ep(udc->
gadget.ep0);
1057 list_del_init(&req->
queue);
1069 spin_unlock(&udc->
lock);
1070 usba_ep_disable(&ep->
ep);
1071 spin_lock(&udc->
lock);
1081 return to_usba_ep(udc->
gadget.ep0);
1088 bEndpointAddress = ep->
ep.desc->bEndpointAddress;
1089 if ((wIndex ^ bEndpointAddress) &
USB_DIR_IN)
1091 if ((bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)
1092 == (wIndex & USB_ENDPOINT_NUMBER_MASK))
1100 static inline void set_protocol_stall(
struct usba_udc *udc,
struct usba_ep *ep)
1106 static inline int is_stalled(
struct usba_udc *udc,
struct usba_ep *ep)
1113 static inline void set_address(
struct usba_udc *udc,
unsigned int addr)
1117 DBG(
DBG_BUS,
"setting address %u...\n", addr);
1123 static int do_test_mode(
struct usba_udc *udc)
1125 static const char test_packet_buffer[] = {
1127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1129 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1131 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1133 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1134 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1136 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1138 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1147 reset_all_endpoints(udc);
1149 switch (test_mode) {
1153 dev_info(dev,
"Entering Test_J mode...\n");
1158 dev_info(dev,
"Entering Test_K mode...\n");
1174 set_protocol_stall(udc, ep);
1175 dev_err(dev,
"Test_SE0_NAK: ep0 not mapped\n");
1178 dev_info(dev,
"Entering Test_SE0_NAK mode...\n");
1190 set_protocol_stall(udc, ep);
1191 dev_err(dev,
"Test_Packet: ep0 not mapped\n");
1196 sizeof(test_packet_buffer));
1198 dev_info(dev,
"Entering Test_Packet mode...\n");
1202 dev_err(dev,
"Invalid test mode: 0x%04x\n", test_mode);
1210 static inline bool feature_is_dev_remote_wakeup(
struct usb_ctrlrequest *crq)
1217 static inline bool feature_is_dev_test_mode(
struct usb_ctrlrequest *crq)
1231 static int handle_ep0_setup(
struct usba_udc *udc,
struct usba_ep *ep,
1254 if (is_stalled(udc, target))
1270 if (feature_is_dev_remote_wakeup(crq))
1280 || !feature_is_ep_halt(crq))
1287 if (target->
index != 0)
1294 send_status(udc, ep);
1300 if (feature_is_dev_test_mode(crq)) {
1301 send_status(udc, ep);
1305 }
else if (feature_is_dev_remote_wakeup(crq)) {
1314 || !feature_is_ep_halt(crq))
1325 send_status(udc, ep);
1334 send_status(udc, ep);
1340 spin_unlock(&udc->
lock);
1342 spin_lock(&udc->
lock);
1348 pr_err(
"udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1349 "halting endpoint...\n",
1353 set_protocol_stall(udc, ep);
1357 static void usba_control_irq(
struct usba_udc *udc,
struct usba_ep *ep)
1368 ep->
ep.name, ep->
state, epstatus, epctrl);
1371 if (!list_empty(&ep->
queue))
1377 next_fifo_transaction(ep, req);
1379 submit_request(ep, req);
1390 switch (ep->
state) {
1405 list_del_init(&req->
queue);
1406 request_complete(ep, req, 0);
1407 submit_next_request(ep);
1415 if (do_test_mode(udc))
1416 set_protocol_stall(udc, ep);
1419 pr_err(
"udc: %s: TXCOMP: Invalid endpoint state %d, "
1420 "halting endpoint...\n",
1422 set_protocol_stall(udc, ep);
1429 switch (ep->
state) {
1435 list_del_init(&req->
queue);
1436 request_complete(ep, req, 0);
1448 pr_err(
"udc: %s: RXRDY: Invalid endpoint state %d, "
1449 "halting endpoint...\n",
1451 set_protocol_stall(udc, ep);
1460 unsigned long data[2];
1485 list_del_init(&req->
queue);
1486 request_complete(ep, req, status);
1491 DBG(
DBG_HW,
"Packet length: %u\n", pkt_len);
1492 if (pkt_len !=
sizeof(crq)) {
1494 "(expected %zu)\n", pkt_len,
sizeof(crq));
1495 set_protocol_stall(udc, ep);
1527 ret = handle_ep0_setup(udc, ep, &crq.crq);
1529 spin_unlock(&udc->
lock);
1531 spin_lock(&udc->
lock);
1534 DBG(
DBG_BUS,
"req %02x.%02x, length %d, state %d, ret %d\n",
1540 set_protocol_stall(udc, ep);
1545 static void usba_ep_irq(
struct usba_udc *udc,
struct usba_ep *ep)
1554 DBG(
DBG_INT,
"%s: interrupt, status: 0x%08x\n", ep->
ep.name, epstatus);
1556 while ((epctrl & USBA_TX_PK_RDY) && !(epstatus & USBA_TX_PK_RDY)) {
1559 if (list_empty(&ep->
queue)) {
1573 list_del_init(&req->
queue);
1574 submit_next_request(ep);
1575 request_complete(ep, req, 0);
1578 next_fifo_transaction(ep, req);
1580 submit_request(ep, req);
1583 list_del_init(&req->
queue);
1584 submit_next_request(ep);
1585 request_complete(ep, req, 0);
1592 if ((epstatus & epctrl) & USBA_RX_BK_RDY) {
1599 static void usba_dma_irq(
struct usba_udc *udc,
struct usba_ep *ep)
1606 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1607 ep->last_dma_status =
status;
1614 "DMA_CH_EN is set after transfer is finished!\n");
1616 "status=%#08x, pending=%#08x, control=%#08x\n",
1617 status, pending, control);
1625 if (list_empty(&ep->
queue))
1631 usba_update_req(ep, req, status);
1633 list_del_init(&req->
queue);
1634 submit_next_request(ep);
1635 request_complete(ep, req, 0);
1646 spin_lock(&udc->
lock);
1657 spin_unlock(&udc->
lock);
1659 spin_lock(&udc->
lock);
1674 spin_unlock(&udc->
lock);
1676 spin_lock(&udc->
lock);
1685 if (dma_status & (1 << i))
1686 usba_dma_irq(udc, &usba_ep[i]);
1694 if (ep_status & (1 << i)) {
1696 usba_control_irq(udc, &usba_ep[i]);
1698 usba_ep_irq(udc, &usba_ep[i]);
1703 struct usba_ep *ep0;
1706 reset_all_endpoints(udc);
1709 && udc->
driver->disconnect) {
1711 spin_unlock(&udc->
lock);
1713 spin_lock(&udc->
lock);
1724 ep0->
ep.desc = &usba_ep0_desc;
1736 | USBA_END_OF_RESUME));
1744 "ODD: EP0 configuration is invalid!\n");
1747 spin_unlock(&udc->
lock);
1752 static irqreturn_t usba_vbus_irq(
int irq,
void *devid)
1760 spin_lock(&udc->
lock);
1766 vbus = vbus_is_present(udc);
1774 reset_all_endpoints(udc);
1777 if (udc->
driver->disconnect) {
1778 spin_unlock(&udc->
lock);
1780 spin_lock(&udc->
lock);
1787 spin_unlock(&udc->
lock);
1792 static int atmel_usba_start(
struct usb_gadget *gadget,
1796 unsigned long flags;
1803 spin_unlock_irqrestore(&udc->
lock, flags);
1816 if (vbus_is_present(udc) && udc->
vbus_prev == 0) {
1821 spin_unlock_irqrestore(&udc->
lock, flags);
1826 static int atmel_usba_stop(
struct usb_gadget *gadget,
1830 unsigned long flags;
1837 reset_all_endpoints(udc);
1838 spin_unlock_irqrestore(&udc->
lock, flags);
1865 if (!regs || !fifo || !pdata)
1874 return PTR_ERR(pclk);
1877 ret = PTR_ERR(hclk);
1890 dev_err(&pdev->
dev,
"Unable to map I/O memory, aborting.\n");
1893 dev_info(&pdev->
dev,
"MMIO registers at 0x%08lx mapped at %p\n",
1897 dev_err(&pdev->
dev,
"Unable to map FIFO, aborting.\n");
1900 dev_info(&pdev->
dev,
"FIFO at 0x%08lx mapped at %p\n",
1905 udc->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1907 platform_set_drvdata(pdev, udc);
1915 usba_ep = kzalloc(
sizeof(
struct usba_ep) * pdata->
num_ep,
1920 the_udc.
gadget.ep0 = &usba_ep[0].
ep;
1922 INIT_LIST_HEAD(&usba_ep[0].ep.ep_list);
1926 usba_ep[0].
ep.ops = &usba_ep_ops;
1927 usba_ep[0].
ep.name = pdata->
ep[0].name;
1928 usba_ep[0].
ep.maxpacket = pdata->
ep[0].fifo_size;
1929 usba_ep[0].
udc = &the_udc;
1930 INIT_LIST_HEAD(&usba_ep[0].queue);
1933 usba_ep[0].
index = pdata->
ep[0].index;
1934 usba_ep[0].
can_dma = pdata->
ep[0].can_dma;
1937 for (i = 1; i < pdata->
num_ep; i++) {
1938 struct usba_ep *ep = &usba_ep[
i];
1943 ep->
ep.ops = &usba_ep_ops;
1944 ep->
ep.name = pdata->
ep[
i].name;
1945 ep->
ep.maxpacket = pdata->
ep[
i].fifo_size;
1947 INIT_LIST_HEAD(&ep->
queue);
1957 ret =
request_irq(irq, usba_udc_irq, 0,
"atmel_usba_udc", udc);
1959 dev_err(&pdev->
dev,
"Cannot request irq %d (error %d)\n",
1961 goto err_request_irq;
1967 dev_dbg(&pdev->
dev,
"Could not add gadget: %d\n", ret);
1968 goto err_device_add;
1971 if (gpio_is_valid(pdata->
vbus_pin)) {
1978 "atmel_usba_udc", udc);
1983 "failed to request vbus irq; "
1984 "assuming always on\n");
1998 usba_init_debugfs(udc);
1999 for (i = 1; i < pdata->
num_ep; i++)
2000 usba_ep_init_debugfs(udc, &usba_ep[i]);
2005 if (gpio_is_valid(pdata->
vbus_pin)) {
2025 platform_set_drvdata(pdev,
NULL);
2036 udc = platform_get_drvdata(pdev);
2040 for (i = 1; i < pdata->
num_ep; i++)
2041 usba_ep_cleanup_debugfs(&usba_ep[i]);
2042 usba_cleanup_debugfs(udc);
2044 if (gpio_is_valid(udc->
vbus_pin)) {
2062 .remove =
__exit_p(usba_udc_remove),
2064 .name =
"atmel_usba_udc",
2069 static int __init udc_init(
void)
2075 static void __exit udc_exit(
void)