2 * linux/drivers/usb/gadget/s3c2410_udc.c
4 * Samsung S3C24xx series on-chip full speed USB device controllers
6 * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
7 * Additional cleanups by Ben Dooks <ben-linux@fluff.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/smp_lock.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/platform_device.h>
38 #include <linux/version.h>
39 #include <linux/clk.h>
41 #include <linux/debugfs.h>
42 #include <linux/seq_file.h>
44 #include <linux/usb.h>
45 #include <linux/usb/gadget.h>
47 #include <asm/byteorder.h>
50 #include <asm/system.h>
51 #include <asm/unaligned.h>
52 #include <mach/irqs.h>
54 #include <mach/hardware.h>
55 #include <mach/regs-gpio.h>
57 #include <asm/plat-s3c24xx/regs-udc.h>
58 #include <asm/plat-s3c24xx/udc.h>
61 #include "s3c2410_udc.h"
63 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
64 #define DRIVER_VERSION "29 Apr 2007"
65 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
66 "Arnaud Patard <arnaud.patard@rtp-net.org>"
68 static const char gadget_name[] = "s3c2410_udc";
69 static const char driver_desc[] = DRIVER_DESC;
71 static struct s3c2410_udc *the_controller;
72 static struct clk *udc_clock;
73 static struct clk *usb_bus_clock;
74 static void __iomem *base_addr;
75 static u64 rsrc_start;
77 static struct dentry *s3c2410_udc_debugfs_root;
79 static inline u32 udc_read(u32 reg)
81 return readb(base_addr + reg);
84 static inline void udc_write(u32 value, u32 reg)
86 writeb(value, base_addr + reg);
89 static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
91 writeb(value, base + reg);
94 static struct s3c2410_udc_mach_info *udc_info;
96 /*************************** DEBUG FUNCTION ***************************/
97 #define DEBUG_NORMAL 1
98 #define DEBUG_VERBOSE 2
100 #ifdef CONFIG_USB_S3C2410_DEBUG
101 #define USB_S3C2410_DEBUG_LEVEL 0
103 static uint32_t s3c2410_ticks = 0;
105 static int dprintk(int level, const char *fmt, ...)
107 static char printk_buf[1024];
108 static long prevticks;
109 static int invocation;
113 if (level > USB_S3C2410_DEBUG_LEVEL)
116 if (s3c2410_ticks != prevticks) {
117 prevticks = s3c2410_ticks;
121 len = scnprintf(printk_buf,
122 sizeof(printk_buf), "%1lu.%02d USB: ",
123 prevticks, invocation++);
126 len = vscnprintf(printk_buf+len,
127 sizeof(printk_buf)-len, fmt, args);
130 return printk(KERN_DEBUG "%s", printk_buf);
133 static int dprintk(int level, const char *fmt, ...)
138 static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
140 u32 addr_reg,pwr_reg,ep_int_reg,usb_int_reg;
141 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
142 u32 ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2;
143 u32 ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2;
145 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
146 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
147 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
148 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
149 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
150 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
151 udc_write(0, S3C2410_UDC_INDEX_REG);
152 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
153 udc_write(1, S3C2410_UDC_INDEX_REG);
154 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
155 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
156 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
157 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
158 udc_write(2, S3C2410_UDC_INDEX_REG);
159 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
160 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
161 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
162 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
164 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
166 "EP_INT_REG : 0x%04X\n"
167 "USB_INT_REG : 0x%04X\n"
168 "EP_INT_EN_REG : 0x%04X\n"
169 "USB_INT_EN_REG : 0x%04X\n"
171 "EP1_I_CSR1 : 0x%04X\n"
172 "EP1_I_CSR2 : 0x%04X\n"
173 "EP1_O_CSR1 : 0x%04X\n"
174 "EP1_O_CSR2 : 0x%04X\n"
175 "EP2_I_CSR1 : 0x%04X\n"
176 "EP2_I_CSR2 : 0x%04X\n"
177 "EP2_O_CSR1 : 0x%04X\n"
178 "EP2_O_CSR2 : 0x%04X\n",
179 addr_reg,pwr_reg,ep_int_reg,usb_int_reg,
180 ep_int_en_reg, usb_int_en_reg, ep0_csr,
181 ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2,
182 ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2
188 static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
191 return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
194 static const struct file_operations s3c2410_udc_debugfs_fops = {
195 .open = s3c2410_udc_debugfs_fops_open,
198 .release = single_release,
199 .owner = THIS_MODULE,
204 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
206 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
207 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
208 S3C2410_UDC_EP0_CSR_REG);
211 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
213 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
214 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
217 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
219 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
220 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
223 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
225 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
226 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
229 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
231 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
232 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
235 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
237 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
238 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
241 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
243 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
245 udc_writeb(base,(S3C2410_UDC_EP0_CSR_SOPKTRDY
246 | S3C2410_UDC_EP0_CSR_DE),
247 S3C2410_UDC_EP0_CSR_REG);
250 static inline void s3c2410_udc_set_ep0_sse_out(void __iomem *base)
252 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
253 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
254 | S3C2410_UDC_EP0_CSR_SSE),
255 S3C2410_UDC_EP0_CSR_REG);
258 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
260 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
261 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
262 | S3C2410_UDC_EP0_CSR_DE),
263 S3C2410_UDC_EP0_CSR_REG);
266 /*------------------------- I/O ----------------------------------*/
271 static void s3c2410_udc_done(struct s3c2410_ep *ep,
272 struct s3c2410_request *req, int status)
274 unsigned halted = ep->halted;
276 list_del_init(&req->queue);
278 if (likely (req->req.status == -EINPROGRESS))
279 req->req.status = status;
281 status = req->req.status;
284 req->req.complete(&ep->ep, &req->req);
288 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
289 struct s3c2410_ep *ep, int status)
292 if (&ep->queue == NULL)
295 while (!list_empty (&ep->queue)) {
296 struct s3c2410_request *req;
297 req = list_entry (ep->queue.next, struct s3c2410_request,
299 s3c2410_udc_done(ep, req, status);
303 static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc *dev)
307 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
308 * fifos, and pending transactions mustn't be continued in any case.
311 for (i = 1; i < S3C2410_ENDPOINTS; i++)
312 s3c2410_udc_nuke(dev, &dev->ep[i], -ECONNABORTED);
315 static inline int s3c2410_udc_fifo_count_out(void)
319 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
320 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
325 * s3c2410_udc_write_packet
327 static inline int s3c2410_udc_write_packet(int fifo,
328 struct s3c2410_request *req,
331 unsigned len = min(req->req.length - req->req.actual, max);
332 u8 *buf = req->req.buf + req->req.actual;
336 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
337 req->req.actual, req->req.length, len, req->req.actual + len);
339 req->req.actual += len;
342 writesb(base_addr + fifo, buf, len);
347 * s3c2410_udc_write_fifo
349 * return: 0 = still running, 1 = completed, negative = errno
351 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
352 struct s3c2410_request *req)
360 idx = ep->bEndpointAddress & 0x7F;
365 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
368 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
371 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
374 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
377 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
381 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
383 /* last packet is often short (sometimes a zlp) */
384 if (count != ep->ep.maxpacket)
386 else if (req->req.length != req->req.actual || req->req.zero)
391 /* Only ep0 debug messages are interesting */
393 dprintk(DEBUG_NORMAL,
394 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
395 idx, count, req->req.actual, req->req.length,
396 is_last, req->req.zero);
399 /* The order is important. It prevents sending 2 packets
400 * at the same time */
403 /* Reset signal => no need to say 'data sent' */
404 if (! (udc_read(S3C2410_UDC_USB_INT_REG)
405 & S3C2410_UDC_USBINT_RESET))
406 s3c2410_udc_set_ep0_de_in(base_addr);
407 ep->dev->ep0state=EP0_IDLE;
409 udc_write(idx, S3C2410_UDC_INDEX_REG);
410 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
411 udc_write(idx, S3C2410_UDC_INDEX_REG);
412 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
413 S3C2410_UDC_IN_CSR1_REG);
416 s3c2410_udc_done(ep, req, 0);
420 /* Reset signal => no need to say 'data sent' */
421 if (! (udc_read(S3C2410_UDC_USB_INT_REG)
422 & S3C2410_UDC_USBINT_RESET))
423 s3c2410_udc_set_ep0_ipr(base_addr);
425 udc_write(idx, S3C2410_UDC_INDEX_REG);
426 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
427 udc_write(idx, S3C2410_UDC_INDEX_REG);
428 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
429 S3C2410_UDC_IN_CSR1_REG);
436 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
437 struct s3c2410_request *req, unsigned avail)
441 len = min(req->req.length - req->req.actual, avail);
442 req->req.actual += len;
444 readsb(fifo + base_addr, buf, len);
449 * return: 0 = still running, 1 = queue empty, negative = errno
451 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
452 struct s3c2410_request *req)
456 unsigned bufferspace;
463 idx = ep->bEndpointAddress & 0x7F;
469 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
472 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
475 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
478 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
481 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
485 if (!req->req.length)
488 buf = req->req.buf + req->req.actual;
489 bufferspace = req->req.length - req->req.actual;
491 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
495 udc_write(idx, S3C2410_UDC_INDEX_REG);
497 fifo_count = s3c2410_udc_fifo_count_out();
498 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
500 if (fifo_count > ep->ep.maxpacket)
501 avail = ep->ep.maxpacket;
505 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
507 /* checking this with ep0 is not accurate as we already
508 * read a control request
510 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
512 /* overflowed this request? flush extra data */
513 if (fifo_count != avail)
514 req->req.status = -EOVERFLOW;
516 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
519 udc_write(idx, S3C2410_UDC_INDEX_REG);
520 fifo_count = s3c2410_udc_fifo_count_out();
522 /* Only ep0 debug messages are interesting */
524 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
525 __func__, fifo_count,is_last);
529 s3c2410_udc_set_ep0_de_out(base_addr);
530 ep->dev->ep0state = EP0_IDLE;
532 udc_write(idx, S3C2410_UDC_INDEX_REG);
533 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
534 udc_write(idx, S3C2410_UDC_INDEX_REG);
535 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
536 S3C2410_UDC_OUT_CSR1_REG);
539 s3c2410_udc_done(ep, req, 0);
542 s3c2410_udc_clear_ep0_opr(base_addr);
544 udc_write(idx, S3C2410_UDC_INDEX_REG);
545 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
546 udc_write(idx, S3C2410_UDC_INDEX_REG);
547 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
548 S3C2410_UDC_OUT_CSR1_REG);
555 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
557 unsigned char *outbuf = (unsigned char*)crq;
560 udc_write(0, S3C2410_UDC_INDEX_REG);
562 bytes_read = s3c2410_udc_fifo_count_out();
564 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
566 if (bytes_read > sizeof(struct usb_ctrlrequest))
567 bytes_read = sizeof(struct usb_ctrlrequest);
569 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
571 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
572 bytes_read, crq->bRequest, crq->bRequestType,
573 crq->wValue, crq->wIndex, crq->wLength);
578 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
579 struct usb_ctrlrequest *crq)
582 u8 ep_num = crq->wIndex & 0x7F;
583 u8 is_in = crq->wIndex & USB_DIR_IN;
585 switch (crq->bRequestType & USB_RECIP_MASK) {
586 case USB_RECIP_INTERFACE:
589 case USB_RECIP_DEVICE:
590 status = dev->devstatus;
593 case USB_RECIP_ENDPOINT:
594 if (ep_num > 4 || crq->wLength > 2)
598 udc_write(0, S3C2410_UDC_INDEX_REG);
599 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
600 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
602 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
604 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
605 status = status & S3C2410_UDC_ICSR1_SENDSTL;
607 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
608 status = status & S3C2410_UDC_OCSR1_SENDSTL;
612 status = status ? 1 : 0;
619 /* Seems to be needed to get it working. ouch :( */
621 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
622 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
623 s3c2410_udc_set_ep0_de_in(base_addr);
627 /*------------------------- usb state machine -------------------------------*/
628 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
630 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
631 struct s3c2410_ep *ep,
632 struct usb_ctrlrequest *crq,
637 /* start control request? */
638 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
641 s3c2410_udc_nuke(dev, ep, -EPROTO);
643 len = s3c2410_udc_read_fifo_crq(crq);
644 if (len != sizeof(*crq)) {
645 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
646 " wanted %d bytes got %d. Stalling out...\n",
648 s3c2410_udc_set_ep0_ss(base_addr);
652 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
653 crq->bRequest, crq->bRequestType, crq->wLength);
655 /* cope with automagic for some standard requests. */
656 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
657 == USB_TYPE_STANDARD;
659 dev->req_pending = 1;
661 switch (crq->bRequest) {
662 case USB_REQ_SET_CONFIGURATION:
663 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ... \n");
665 if (crq->bRequestType == USB_RECIP_DEVICE) {
667 s3c2410_udc_set_ep0_de_out(base_addr);
671 case USB_REQ_SET_INTERFACE:
672 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ... \n");
674 if (crq->bRequestType == USB_RECIP_INTERFACE) {
676 s3c2410_udc_set_ep0_de_out(base_addr);
680 case USB_REQ_SET_ADDRESS:
681 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ... \n");
683 if (crq->bRequestType == USB_RECIP_DEVICE) {
684 tmp = crq->wValue & 0x7F;
686 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
687 S3C2410_UDC_FUNC_ADDR_REG);
688 s3c2410_udc_set_ep0_de_out(base_addr);
693 case USB_REQ_GET_STATUS:
694 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ... \n");
695 s3c2410_udc_clear_ep0_opr(base_addr);
698 if (!s3c2410_udc_get_status(dev, crq)) {
704 case USB_REQ_CLEAR_FEATURE:
705 s3c2410_udc_clear_ep0_opr(base_addr);
707 if (crq->bRequestType != USB_RECIP_ENDPOINT)
710 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
713 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
714 s3c2410_udc_set_ep0_de_out(base_addr);
717 case USB_REQ_SET_FEATURE:
718 s3c2410_udc_clear_ep0_opr(base_addr);
720 if (crq->bRequestType != USB_RECIP_ENDPOINT)
723 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
726 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
727 s3c2410_udc_set_ep0_de_out(base_addr);
731 s3c2410_udc_clear_ep0_opr(base_addr);
735 if (crq->bRequestType & USB_DIR_IN)
736 dev->ep0state = EP0_IN_DATA_PHASE;
738 dev->ep0state = EP0_OUT_DATA_PHASE;
740 ret = dev->driver->setup(&dev->gadget, crq);
742 if (dev->req_config) {
743 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
748 if (ret == -EOPNOTSUPP)
749 dprintk(DEBUG_NORMAL, "Operation not supported\n");
751 dprintk(DEBUG_NORMAL,
752 "dev->driver->setup failed. (%d)\n", ret);
755 s3c2410_udc_set_ep0_ss(base_addr);
756 s3c2410_udc_set_ep0_de_out(base_addr);
757 dev->ep0state = EP0_IDLE;
758 /* deferred i/o == no response yet */
759 } else if (dev->req_pending) {
760 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
764 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
767 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
770 struct s3c2410_ep *ep = &dev->ep[0];
771 struct s3c2410_request *req;
772 struct usb_ctrlrequest crq;
774 if (list_empty(&ep->queue))
777 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
779 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
780 * S3C2410_UDC_EP0_CSR_REG when index is zero */
782 udc_write(0, S3C2410_UDC_INDEX_REG);
783 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
785 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
786 ep0csr, ep0states[dev->ep0state]);
788 /* clear stall status */
789 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
790 s3c2410_udc_nuke(dev, ep, -EPIPE);
791 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
792 s3c2410_udc_clear_ep0_sst(base_addr);
793 dev->ep0state = EP0_IDLE;
797 /* clear setup end */
798 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
799 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
800 s3c2410_udc_nuke(dev, ep, 0);
801 s3c2410_udc_clear_ep0_se(base_addr);
802 dev->ep0state = EP0_IDLE;
805 switch (dev->ep0state) {
807 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
810 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
811 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
812 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req) {
813 s3c2410_udc_write_fifo(ep, req);
817 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
818 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
819 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req ) {
820 s3c2410_udc_read_fifo(ep,req);
825 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
826 dev->ep0state = EP0_IDLE;
830 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
831 dev->ep0state = EP0_IDLE;
837 * handle_ep - Manage I/O endpoints
840 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
842 struct s3c2410_request *req;
843 int is_in = ep->bEndpointAddress & USB_DIR_IN;
847 if (likely (!list_empty(&ep->queue)))
848 req = list_entry(ep->queue.next,
849 struct s3c2410_request, queue);
853 idx = ep->bEndpointAddress & 0x7F;
856 udc_write(idx, S3C2410_UDC_INDEX_REG);
857 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
858 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
859 idx, ep_csr1, req ? 1 : 0);
861 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
862 dprintk(DEBUG_VERBOSE, "st\n");
863 udc_write(idx, S3C2410_UDC_INDEX_REG);
864 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
865 S3C2410_UDC_IN_CSR1_REG);
869 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req) {
870 s3c2410_udc_write_fifo(ep,req);
873 udc_write(idx, S3C2410_UDC_INDEX_REG);
874 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
875 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
877 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
878 udc_write(idx, S3C2410_UDC_INDEX_REG);
879 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
880 S3C2410_UDC_OUT_CSR1_REG);
884 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req) {
885 s3c2410_udc_read_fifo(ep,req);
890 #include <mach/regs-irq.h>
893 * s3c2410_udc_irq - interrupt handler
895 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
897 struct s3c2410_udc *dev = _dev;
906 spin_lock_irqsave(&dev->lock, flags);
908 /* Driver connected ? */
910 /* Clear interrupts */
911 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
912 S3C2410_UDC_USB_INT_REG);
913 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
914 S3C2410_UDC_EP_INT_REG);
918 idx = udc_read(S3C2410_UDC_INDEX_REG);
920 /* Read status registers */
921 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
922 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
923 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
925 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
926 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
928 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
929 usb_status, usbd_status, pwr_reg, ep0csr);
932 * Now, handle interrupts. There's two types :
933 * - Reset, Resume, Suspend coming -> usb_int_reg
938 if (usb_status & S3C2410_UDC_USBINT_RESET) {
939 /* two kind of reset :
940 * - reset start -> pwr reg = 8
941 * - reset end -> pwr reg = 0
943 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
946 dev->gadget.speed = USB_SPEED_UNKNOWN;
947 udc_write(0x00, S3C2410_UDC_INDEX_REG);
948 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
949 S3C2410_UDC_MAXP_REG);
952 dev->ep0state = EP0_IDLE;
953 dev->gadget.speed = USB_SPEED_FULL;
955 /* clear interrupt */
956 udc_write(S3C2410_UDC_USBINT_RESET,
957 S3C2410_UDC_USB_INT_REG);
959 udc_write(idx, S3C2410_UDC_INDEX_REG);
960 spin_unlock_irqrestore(&dev->lock, flags);
965 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
966 dprintk(DEBUG_NORMAL, "USB resume\n");
968 /* clear interrupt */
969 udc_write(S3C2410_UDC_USBINT_RESUME,
970 S3C2410_UDC_USB_INT_REG);
972 if (dev->gadget.speed != USB_SPEED_UNKNOWN
974 && dev->driver->resume)
975 dev->driver->resume(&dev->gadget);
979 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
980 dprintk(DEBUG_NORMAL, "USB suspend\n");
982 /* clear interrupt */
983 udc_write(S3C2410_UDC_USBINT_SUSPEND,
984 S3C2410_UDC_USB_INT_REG);
986 if (dev->gadget.speed != USB_SPEED_UNKNOWN
988 && dev->driver->suspend)
989 dev->driver->suspend(&dev->gadget);
991 dev->ep0state = EP0_IDLE;
995 /* control traffic */
996 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
997 * generate an interrupt
999 if (usbd_status & S3C2410_UDC_INT_EP0) {
1000 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
1001 /* Clear the interrupt bit by setting it to 1 */
1002 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
1003 s3c2410_udc_handle_ep0(dev);
1006 /* endpoint data transfers */
1007 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
1009 if (usbd_status & tmp) {
1010 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
1012 /* Clear the interrupt bit by setting it to 1 */
1013 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
1014 s3c2410_udc_handle_ep(&dev->ep[i]);
1018 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
1020 /* Restore old index */
1021 udc_write(idx, S3C2410_UDC_INDEX_REG);
1023 spin_unlock_irqrestore(&dev->lock, flags);
1027 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1029 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1031 return container_of(ep, struct s3c2410_ep, ep);
1034 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1036 return container_of(gadget, struct s3c2410_udc, gadget);
1039 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1041 return container_of(req, struct s3c2410_request, req);
1045 * s3c2410_udc_ep_enable
1047 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1048 const struct usb_endpoint_descriptor *desc)
1050 struct s3c2410_udc *dev;
1051 struct s3c2410_ep *ep;
1053 unsigned long flags;
1057 ep = to_s3c2410_ep(_ep);
1059 if (!_ep || !desc || ep->desc
1060 || _ep->name == ep0name
1061 || desc->bDescriptorType != USB_DT_ENDPOINT)
1065 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1068 max = le16_to_cpu(desc->wMaxPacketSize) & 0x1fff;
1070 local_irq_save (flags);
1071 _ep->maxpacket = max & 0x7ff;
1074 ep->bEndpointAddress = desc->bEndpointAddress;
1076 /* set max packet */
1077 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1078 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1080 /* set type, direction, address; reset fifo counters */
1081 if (desc->bEndpointAddress & USB_DIR_IN) {
1082 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1083 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1085 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1086 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1087 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1088 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1090 /* don't flush in fifo or it will cause endpoint interrupt */
1091 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1092 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1094 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1095 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1096 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1097 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1099 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1100 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1102 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1103 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1104 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1105 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1109 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1110 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1112 /* print some debug message */
1113 tmp = desc->bEndpointAddress;
1114 dprintk (DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1115 _ep->name,ep->num, tmp,
1116 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1118 local_irq_restore (flags);
1119 s3c2410_udc_set_halt(_ep, 0);
1125 * s3c2410_udc_ep_disable
1127 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1129 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1130 unsigned long flags;
1133 if (!_ep || !ep->desc) {
1134 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1135 _ep ? ep->ep.name : NULL);
1139 local_irq_save(flags);
1141 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1146 s3c2410_udc_nuke (ep->dev, ep, -ESHUTDOWN);
1149 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1150 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1152 local_irq_restore(flags);
1154 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1160 * s3c2410_udc_alloc_request
1162 static struct usb_request *
1163 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1165 struct s3c2410_request *req;
1167 dprintk(DEBUG_VERBOSE,"%s(%p,%d)\n", __func__, _ep, mem_flags);
1172 req = kzalloc (sizeof(struct s3c2410_request), mem_flags);
1176 INIT_LIST_HEAD (&req->queue);
1181 * s3c2410_udc_free_request
1184 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1186 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1187 struct s3c2410_request *req = to_s3c2410_req(_req);
1189 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1191 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
1194 WARN_ON (!list_empty (&req->queue));
1201 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1204 struct s3c2410_request *req = to_s3c2410_req(_req);
1205 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1206 struct s3c2410_udc *dev;
1209 unsigned long flags;
1211 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1212 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1217 if (unlikely (!dev->driver
1218 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1222 local_irq_save (flags);
1224 if (unlikely(!_req || !_req->complete
1225 || !_req->buf || !list_empty(&req->queue))) {
1227 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1229 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1230 __func__, !_req->complete,!_req->buf,
1231 !list_empty(&req->queue));
1234 local_irq_restore(flags);
1238 _req->status = -EINPROGRESS;
1241 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1242 __func__, ep->bEndpointAddress, _req->length);
1244 if (ep->bEndpointAddress) {
1245 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1247 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1248 ? S3C2410_UDC_IN_CSR1_REG
1249 : S3C2410_UDC_OUT_CSR1_REG);
1250 fifo_count = s3c2410_udc_fifo_count_out();
1252 udc_write(0, S3C2410_UDC_INDEX_REG);
1253 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1254 fifo_count = s3c2410_udc_fifo_count_out();
1257 /* kickstart this i/o queue? */
1258 if (list_empty(&ep->queue) && !ep->halted) {
1259 if (ep->bEndpointAddress == 0 /* ep0 */) {
1260 switch (dev->ep0state) {
1261 case EP0_IN_DATA_PHASE:
1262 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1263 && s3c2410_udc_write_fifo(ep,
1265 dev->ep0state = EP0_IDLE;
1270 case EP0_OUT_DATA_PHASE:
1272 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1273 && s3c2410_udc_read_fifo(ep,
1275 dev->ep0state = EP0_IDLE;
1281 local_irq_restore(flags);
1284 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1285 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1286 && s3c2410_udc_write_fifo(ep, req)) {
1288 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1290 && s3c2410_udc_read_fifo(ep, req)) {
1295 /* pio or dma irq handler advances the queue. */
1296 if (likely (req != 0))
1297 list_add_tail(&req->queue, &ep->queue);
1299 local_irq_restore(flags);
1301 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1306 * s3c2410_udc_dequeue
1308 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1310 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1311 struct s3c2410_udc *udc;
1312 int retval = -EINVAL;
1313 unsigned long flags;
1314 struct s3c2410_request *req = NULL;
1316 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1318 if (!the_controller->driver)
1324 udc = to_s3c2410_udc(ep->gadget);
1326 local_irq_save (flags);
1328 list_for_each_entry (req, &ep->queue, queue) {
1329 if (&req->req == _req) {
1330 list_del_init (&req->queue);
1331 _req->status = -ECONNRESET;
1338 dprintk(DEBUG_VERBOSE,
1339 "dequeued req %p from %s, len %d buf %p\n",
1340 req, _ep->name, _req->length, _req->buf);
1342 s3c2410_udc_done(ep, req, -ECONNRESET);
1345 local_irq_restore (flags);
1350 * s3c2410_udc_set_halt
1352 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1354 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1356 unsigned long flags;
1359 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1360 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1364 local_irq_save (flags);
1366 idx = ep->bEndpointAddress & 0x7F;
1369 s3c2410_udc_set_ep0_ss(base_addr);
1370 s3c2410_udc_set_ep0_de_out(base_addr);
1372 udc_write(idx, S3C2410_UDC_INDEX_REG);
1373 ep_csr = udc_read((ep->bEndpointAddress &USB_DIR_IN)
1374 ? S3C2410_UDC_IN_CSR1_REG
1375 : S3C2410_UDC_OUT_CSR1_REG);
1377 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1379 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1380 S3C2410_UDC_IN_CSR1_REG);
1382 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1383 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1384 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1385 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1389 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1390 S3C2410_UDC_OUT_CSR1_REG);
1392 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1393 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1394 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1395 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1400 ep->halted = value ? 1 : 0;
1401 local_irq_restore (flags);
1406 static const struct usb_ep_ops s3c2410_ep_ops = {
1407 .enable = s3c2410_udc_ep_enable,
1408 .disable = s3c2410_udc_ep_disable,
1410 .alloc_request = s3c2410_udc_alloc_request,
1411 .free_request = s3c2410_udc_free_request,
1413 .queue = s3c2410_udc_queue,
1414 .dequeue = s3c2410_udc_dequeue,
1416 .set_halt = s3c2410_udc_set_halt,
1419 /*------------------------- usb_gadget_ops ----------------------------------*/
1422 * s3c2410_udc_get_frame
1424 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1428 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1430 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1431 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1436 * s3c2410_udc_wakeup
1438 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1440 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1445 * s3c2410_udc_set_selfpowered
1447 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1449 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1451 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1454 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1456 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1461 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1462 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1464 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1466 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1468 if (udc_info && udc_info->udc_command) {
1470 s3c2410_udc_enable(udc);
1472 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1473 if (udc->driver && udc->driver->disconnect)
1474 udc->driver->disconnect(&udc->gadget);
1477 s3c2410_udc_disable(udc);
1486 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1488 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1490 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1492 udc->vbus = (is_active != 0);
1493 s3c2410_udc_set_pullup(udc, is_active);
1497 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1499 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1501 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1503 s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
1507 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1509 struct s3c2410_udc *dev = _dev;
1512 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1514 /* some cpus cannot read from an line configured to IRQ! */
1515 s3c2410_gpio_cfgpin(udc_info->vbus_pin, S3C2410_GPIO_INPUT);
1516 value = s3c2410_gpio_getpin(udc_info->vbus_pin);
1517 s3c2410_gpio_cfgpin(udc_info->vbus_pin, S3C2410_GPIO_SFN2);
1519 if (udc_info->vbus_pin_inverted)
1522 if (value != dev->vbus)
1523 s3c2410_udc_vbus_session(&dev->gadget, value);
1528 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1530 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1532 if (udc_info && udc_info->vbus_draw) {
1533 udc_info->vbus_draw(ma);
1540 static const struct usb_gadget_ops s3c2410_ops = {
1541 .get_frame = s3c2410_udc_get_frame,
1542 .wakeup = s3c2410_udc_wakeup,
1543 .set_selfpowered = s3c2410_udc_set_selfpowered,
1544 .pullup = s3c2410_udc_pullup,
1545 .vbus_session = s3c2410_udc_vbus_session,
1546 .vbus_draw = s3c2410_vbus_draw,
1549 /*------------------------- gadget driver handling---------------------------*/
1551 * s3c2410_udc_disable
1553 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1555 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1557 /* Disable all interrupts */
1558 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1559 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1561 /* Clear the interrupt registers */
1562 udc_write(S3C2410_UDC_USBINT_RESET
1563 | S3C2410_UDC_USBINT_RESUME
1564 | S3C2410_UDC_USBINT_SUSPEND,
1565 S3C2410_UDC_USB_INT_REG);
1567 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1569 /* Good bye, cruel world */
1570 if (udc_info && udc_info->udc_command)
1571 udc_info->udc_command(S3C2410_UDC_P_DISABLE);
1573 /* Set speed to unknown */
1574 dev->gadget.speed = USB_SPEED_UNKNOWN;
1578 * s3c2410_udc_reinit
1580 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1584 /* device/ep0 records init */
1585 INIT_LIST_HEAD (&dev->gadget.ep_list);
1586 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1587 dev->ep0state = EP0_IDLE;
1589 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1590 struct s3c2410_ep *ep = &dev->ep[i];
1593 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1598 INIT_LIST_HEAD (&ep->queue);
1603 * s3c2410_udc_enable
1605 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1609 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1611 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1612 dev->gadget.speed = USB_SPEED_FULL;
1614 /* Set MAXP for all endpoints */
1615 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1616 udc_write(i, S3C2410_UDC_INDEX_REG);
1617 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1618 S3C2410_UDC_MAXP_REG);
1621 /* Set default power state */
1622 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1624 /* Enable reset and suspend interrupt interrupts */
1625 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1626 S3C2410_UDC_USB_INT_EN_REG);
1628 /* Enable ep0 interrupt */
1629 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1631 /* time to say "hello, world" */
1632 if (udc_info && udc_info->udc_command)
1633 udc_info->udc_command(S3C2410_UDC_P_ENABLE);
1637 * usb_gadget_register_driver
1639 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1641 struct s3c2410_udc *udc = the_controller;
1644 dprintk(DEBUG_NORMAL, "usb_gadget_register_driver() '%s'\n",
1645 driver->driver.name);
1654 if (!driver->bind || !driver->setup
1655 || driver->speed != USB_SPEED_FULL) {
1656 printk(KERN_ERR "Invalid driver: bind %p setup %p speed %d\n",
1657 driver->bind, driver->setup, driver->speed);
1661 if (!driver->unbind) {
1662 printk(KERN_ERR "Invalid driver: no unbind method\n");
1667 /* Hook the driver */
1668 udc->driver = driver;
1669 udc->gadget.dev.driver = &driver->driver;
1671 /* Bind the driver */
1672 if ((retval = device_add(&udc->gadget.dev)) != 0) {
1673 printk(KERN_ERR "Error in device_add() : %d\n",retval);
1674 goto register_error;
1677 dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n",
1678 driver->driver.name);
1680 if ((retval = driver->bind (&udc->gadget)) != 0) {
1681 device_del(&udc->gadget.dev);
1682 goto register_error;
1686 s3c2410_udc_enable(udc);
1692 udc->gadget.dev.driver = NULL;
1697 * usb_gadget_unregister_driver
1699 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1701 struct s3c2410_udc *udc = the_controller;
1706 if (!driver || driver != udc->driver || !driver->unbind)
1709 dprintk(DEBUG_NORMAL,"usb_gadget_register_driver() '%s'\n",
1710 driver->driver.name);
1712 if (driver->disconnect)
1713 driver->disconnect(&udc->gadget);
1715 device_del(&udc->gadget.dev);
1719 s3c2410_udc_disable(udc);
1724 /*---------------------------------------------------------------------------*/
1725 static struct s3c2410_udc memory = {
1727 .ops = &s3c2410_ops,
1728 .ep0 = &memory.ep[0].ep,
1729 .name = gadget_name,
1735 /* control endpoint */
1740 .ops = &s3c2410_ep_ops,
1741 .maxpacket = EP0_FIFO_SIZE,
1746 /* first group of endpoints */
1751 .ops = &s3c2410_ep_ops,
1752 .maxpacket = EP_FIFO_SIZE,
1755 .fifo_size = EP_FIFO_SIZE,
1756 .bEndpointAddress = 1,
1757 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1763 .ops = &s3c2410_ep_ops,
1764 .maxpacket = EP_FIFO_SIZE,
1767 .fifo_size = EP_FIFO_SIZE,
1768 .bEndpointAddress = 2,
1769 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1775 .ops = &s3c2410_ep_ops,
1776 .maxpacket = EP_FIFO_SIZE,
1779 .fifo_size = EP_FIFO_SIZE,
1780 .bEndpointAddress = 3,
1781 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1787 .ops = &s3c2410_ep_ops,
1788 .maxpacket = EP_FIFO_SIZE,
1791 .fifo_size = EP_FIFO_SIZE,
1792 .bEndpointAddress = 4,
1793 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1799 * probe - binds to the platform device
1801 static int s3c2410_udc_probe(struct platform_device *pdev)
1803 struct s3c2410_udc *udc = &memory;
1804 struct device *dev = &pdev->dev;
1808 dev_dbg(dev, "%s()\n", __func__);
1810 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1811 if (IS_ERR(usb_bus_clock)) {
1812 dev_err(dev, "failed to get usb bus clock source\n");
1813 return PTR_ERR(usb_bus_clock);
1816 clk_enable(usb_bus_clock);
1818 udc_clock = clk_get(NULL, "usb-device");
1819 if (IS_ERR(udc_clock)) {
1820 dev_err(dev, "failed to get udc clock source\n");
1821 return PTR_ERR(udc_clock);
1824 clk_enable(udc_clock);
1828 dev_dbg(dev, "got and enabled clocks\n");
1830 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1831 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1832 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1833 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1834 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1835 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1838 spin_lock_init (&udc->lock);
1839 udc_info = pdev->dev.platform_data;
1841 rsrc_start = S3C2410_PA_USBDEV;
1842 rsrc_len = S3C24XX_SZ_USBDEV;
1844 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1847 base_addr = ioremap(rsrc_start, rsrc_len);
1853 device_initialize(&udc->gadget.dev);
1854 udc->gadget.dev.parent = &pdev->dev;
1855 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
1857 the_controller = udc;
1858 platform_set_drvdata(pdev, udc);
1860 s3c2410_udc_disable(udc);
1861 s3c2410_udc_reinit(udc);
1863 /* irq setup after old hardware state is cleaned up */
1864 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1865 IRQF_DISABLED, gadget_name, udc);
1868 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1873 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1875 if (udc_info && udc_info->vbus_pin > 0) {
1876 irq = s3c2410_gpio_getirq(udc_info->vbus_pin);
1877 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1878 IRQF_DISABLED | IRQF_TRIGGER_RISING
1879 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1883 dev_err(dev, "can't get vbus irq %i, err %d\n",
1889 dev_dbg(dev, "got irq %i\n", irq);
1894 if (s3c2410_udc_debugfs_root) {
1895 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1896 s3c2410_udc_debugfs_root,
1897 udc, &s3c2410_udc_debugfs_fops);
1898 if (IS_ERR(udc->regs_info)) {
1899 dev_warn(dev, "debugfs file creation failed %ld\n",
1900 PTR_ERR(udc->regs_info));
1901 udc->regs_info = NULL;
1905 dev_dbg(dev, "probe ok\n");
1910 free_irq(IRQ_USBD, udc);
1914 release_mem_region(rsrc_start, rsrc_len);
1920 * s3c2410_udc_remove
1922 static int s3c2410_udc_remove(struct platform_device *pdev)
1924 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1927 dev_dbg(&pdev->dev, "%s()\n", __func__);
1931 debugfs_remove(udc->regs_info);
1933 if (udc_info && udc_info->vbus_pin > 0) {
1934 irq = s3c2410_gpio_getirq(udc_info->vbus_pin);
1938 free_irq(IRQ_USBD, udc);
1941 release_mem_region(rsrc_start, rsrc_len);
1943 platform_set_drvdata(pdev, NULL);
1945 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1946 clk_disable(udc_clock);
1951 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1952 clk_disable(usb_bus_clock);
1953 clk_put(usb_bus_clock);
1954 usb_bus_clock = NULL;
1957 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1962 static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1964 if (udc_info && udc_info->udc_command)
1965 udc_info->udc_command(S3C2410_UDC_P_DISABLE);
1970 static int s3c2410_udc_resume(struct platform_device *pdev)
1972 if (udc_info && udc_info->udc_command)
1973 udc_info->udc_command(S3C2410_UDC_P_ENABLE);
1978 #define s3c2410_udc_suspend NULL
1979 #define s3c2410_udc_resume NULL
1982 static struct platform_driver udc_driver_2410 = {
1984 .name = "s3c2410-usbgadget",
1985 .owner = THIS_MODULE,
1987 .probe = s3c2410_udc_probe,
1988 .remove = s3c2410_udc_remove,
1989 .suspend = s3c2410_udc_suspend,
1990 .resume = s3c2410_udc_resume,
1993 static struct platform_driver udc_driver_2440 = {
1995 .name = "s3c2440-usbgadget",
1996 .owner = THIS_MODULE,
1998 .probe = s3c2410_udc_probe,
1999 .remove = s3c2410_udc_remove,
2000 .suspend = s3c2410_udc_suspend,
2001 .resume = s3c2410_udc_resume,
2004 static int __init udc_init(void)
2008 dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
2010 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
2011 if (IS_ERR(s3c2410_udc_debugfs_root)) {
2012 printk(KERN_ERR "%s: debugfs dir creation failed %ld\n",
2013 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2014 s3c2410_udc_debugfs_root = NULL;
2017 retval = platform_driver_register(&udc_driver_2410);
2021 retval = platform_driver_register(&udc_driver_2440);
2028 debugfs_remove(s3c2410_udc_debugfs_root);
2032 static void __exit udc_exit(void)
2034 platform_driver_unregister(&udc_driver_2410);
2035 platform_driver_unregister(&udc_driver_2440);
2036 debugfs_remove(s3c2410_udc_debugfs_root);
2039 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2040 EXPORT_SYMBOL(usb_gadget_register_driver);
2042 module_init(udc_init);
2043 module_exit(udc_exit);
2045 MODULE_AUTHOR(DRIVER_AUTHOR);
2046 MODULE_DESCRIPTION(DRIVER_DESC);
2047 MODULE_VERSION(DRIVER_VERSION);
2048 MODULE_LICENSE("GPL");
2049 MODULE_ALIAS("platform:s3c2410-usbgadget");
2050 MODULE_ALIAS("platform:s3c2440-usbgadget");