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/errno.h>
32 #include <linux/init.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/platform_device.h>
37 #include <linux/clk.h>
38 #include <linux/gpio.h>
40 #include <linux/debugfs.h>
41 #include <linux/seq_file.h>
43 #include <linux/usb.h>
44 #include <linux/usb/gadget.h>
46 #include <asm/byteorder.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
51 #include <mach/irqs.h>
53 #include <mach/hardware.h>
55 #include <plat/regs-udc.h>
59 #include "s3c2410_udc.h"
61 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
62 #define DRIVER_VERSION "29 Apr 2007"
63 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
64 "Arnaud Patard <arnaud.patard@rtp-net.org>"
66 static const char gadget_name
[] = "s3c2410_udc";
67 static const char driver_desc
[] = DRIVER_DESC
;
69 static struct s3c2410_udc
*the_controller
;
70 static struct clk
*udc_clock
;
71 static struct clk
*usb_bus_clock
;
72 static void __iomem
*base_addr
;
73 static u64 rsrc_start
;
75 static struct dentry
*s3c2410_udc_debugfs_root
;
77 static inline u32
udc_read(u32 reg
)
79 return readb(base_addr
+ reg
);
82 static inline void udc_write(u32 value
, u32 reg
)
84 writeb(value
, base_addr
+ reg
);
87 static inline void udc_writeb(void __iomem
*base
, u32 value
, u32 reg
)
89 writeb(value
, base
+ reg
);
92 static struct s3c2410_udc_mach_info
*udc_info
;
94 /*************************** DEBUG FUNCTION ***************************/
95 #define DEBUG_NORMAL 1
96 #define DEBUG_VERBOSE 2
98 #ifdef CONFIG_USB_S3C2410_DEBUG
99 #define USB_S3C2410_DEBUG_LEVEL 0
101 static uint32_t s3c2410_ticks
= 0;
103 static int dprintk(int level
, const char *fmt
, ...)
105 static char printk_buf
[1024];
106 static long prevticks
;
107 static int invocation
;
111 if (level
> USB_S3C2410_DEBUG_LEVEL
)
114 if (s3c2410_ticks
!= prevticks
) {
115 prevticks
= s3c2410_ticks
;
119 len
= scnprintf(printk_buf
,
120 sizeof(printk_buf
), "%1lu.%02d USB: ",
121 prevticks
, invocation
++);
124 len
= vscnprintf(printk_buf
+len
,
125 sizeof(printk_buf
)-len
, fmt
, args
);
128 return printk(KERN_DEBUG
"%s", printk_buf
);
131 static int dprintk(int level
, const char *fmt
, ...)
136 static int s3c2410_udc_debugfs_seq_show(struct seq_file
*m
, void *p
)
138 u32 addr_reg
,pwr_reg
,ep_int_reg
,usb_int_reg
;
139 u32 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
;
140 u32 ep1_i_csr1
,ep1_i_csr2
,ep1_o_csr1
,ep1_o_csr2
;
141 u32 ep2_i_csr1
,ep2_i_csr2
,ep2_o_csr1
,ep2_o_csr2
;
143 addr_reg
= udc_read(S3C2410_UDC_FUNC_ADDR_REG
);
144 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
145 ep_int_reg
= udc_read(S3C2410_UDC_EP_INT_REG
);
146 usb_int_reg
= udc_read(S3C2410_UDC_USB_INT_REG
);
147 ep_int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
148 usb_int_en_reg
= udc_read(S3C2410_UDC_USB_INT_EN_REG
);
149 udc_write(0, S3C2410_UDC_INDEX_REG
);
150 ep0_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
151 udc_write(1, S3C2410_UDC_INDEX_REG
);
152 ep1_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
153 ep1_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
154 ep1_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
155 ep1_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
156 udc_write(2, S3C2410_UDC_INDEX_REG
);
157 ep2_i_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
158 ep2_i_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
159 ep2_o_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
160 ep2_o_csr2
= udc_read(S3C2410_UDC_IN_CSR2_REG
);
162 seq_printf(m
, "FUNC_ADDR_REG : 0x%04X\n"
164 "EP_INT_REG : 0x%04X\n"
165 "USB_INT_REG : 0x%04X\n"
166 "EP_INT_EN_REG : 0x%04X\n"
167 "USB_INT_EN_REG : 0x%04X\n"
169 "EP1_I_CSR1 : 0x%04X\n"
170 "EP1_I_CSR2 : 0x%04X\n"
171 "EP1_O_CSR1 : 0x%04X\n"
172 "EP1_O_CSR2 : 0x%04X\n"
173 "EP2_I_CSR1 : 0x%04X\n"
174 "EP2_I_CSR2 : 0x%04X\n"
175 "EP2_O_CSR1 : 0x%04X\n"
176 "EP2_O_CSR2 : 0x%04X\n",
177 addr_reg
,pwr_reg
,ep_int_reg
,usb_int_reg
,
178 ep_int_en_reg
, usb_int_en_reg
, ep0_csr
,
179 ep1_i_csr1
,ep1_i_csr2
,ep1_o_csr1
,ep1_o_csr2
,
180 ep2_i_csr1
,ep2_i_csr2
,ep2_o_csr1
,ep2_o_csr2
186 static int s3c2410_udc_debugfs_fops_open(struct inode
*inode
,
189 return single_open(file
, s3c2410_udc_debugfs_seq_show
, NULL
);
192 static const struct file_operations s3c2410_udc_debugfs_fops
= {
193 .open
= s3c2410_udc_debugfs_fops_open
,
196 .release
= single_release
,
197 .owner
= THIS_MODULE
,
202 static inline void s3c2410_udc_clear_ep0_opr(void __iomem
*base
)
204 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
205 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SOPKTRDY
,
206 S3C2410_UDC_EP0_CSR_REG
);
209 static inline void s3c2410_udc_clear_ep0_sst(void __iomem
*base
)
211 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
212 writeb(0x00, base
+ S3C2410_UDC_EP0_CSR_REG
);
215 static inline void s3c2410_udc_clear_ep0_se(void __iomem
*base
)
217 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
218 udc_writeb(base
, S3C2410_UDC_EP0_CSR_SSE
, S3C2410_UDC_EP0_CSR_REG
);
221 static inline void s3c2410_udc_set_ep0_ipr(void __iomem
*base
)
223 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
224 udc_writeb(base
, S3C2410_UDC_EP0_CSR_IPKRDY
, S3C2410_UDC_EP0_CSR_REG
);
227 static inline void s3c2410_udc_set_ep0_de(void __iomem
*base
)
229 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
230 udc_writeb(base
, S3C2410_UDC_EP0_CSR_DE
, S3C2410_UDC_EP0_CSR_REG
);
233 inline void s3c2410_udc_set_ep0_ss(void __iomem
*b
)
235 udc_writeb(b
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
236 udc_writeb(b
, S3C2410_UDC_EP0_CSR_SENDSTL
, S3C2410_UDC_EP0_CSR_REG
);
239 static inline void s3c2410_udc_set_ep0_de_out(void __iomem
*base
)
241 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
243 udc_writeb(base
,(S3C2410_UDC_EP0_CSR_SOPKTRDY
244 | S3C2410_UDC_EP0_CSR_DE
),
245 S3C2410_UDC_EP0_CSR_REG
);
248 static inline void s3c2410_udc_set_ep0_sse_out(void __iomem
*base
)
250 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
251 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_SOPKTRDY
252 | S3C2410_UDC_EP0_CSR_SSE
),
253 S3C2410_UDC_EP0_CSR_REG
);
256 static inline void s3c2410_udc_set_ep0_de_in(void __iomem
*base
)
258 udc_writeb(base
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
259 udc_writeb(base
, (S3C2410_UDC_EP0_CSR_IPKRDY
260 | S3C2410_UDC_EP0_CSR_DE
),
261 S3C2410_UDC_EP0_CSR_REG
);
264 /*------------------------- I/O ----------------------------------*/
269 static void s3c2410_udc_done(struct s3c2410_ep
*ep
,
270 struct s3c2410_request
*req
, int status
)
272 unsigned halted
= ep
->halted
;
274 list_del_init(&req
->queue
);
276 if (likely (req
->req
.status
== -EINPROGRESS
))
277 req
->req
.status
= status
;
279 status
= req
->req
.status
;
282 req
->req
.complete(&ep
->ep
, &req
->req
);
286 static void s3c2410_udc_nuke(struct s3c2410_udc
*udc
,
287 struct s3c2410_ep
*ep
, int status
)
290 if (&ep
->queue
== NULL
)
293 while (!list_empty (&ep
->queue
)) {
294 struct s3c2410_request
*req
;
295 req
= list_entry (ep
->queue
.next
, struct s3c2410_request
,
297 s3c2410_udc_done(ep
, req
, status
);
301 static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc
*dev
)
305 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
306 * fifos, and pending transactions mustn't be continued in any case.
309 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++)
310 s3c2410_udc_nuke(dev
, &dev
->ep
[i
], -ECONNABORTED
);
313 static inline int s3c2410_udc_fifo_count_out(void)
317 tmp
= udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG
) << 8;
318 tmp
|= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG
);
323 * s3c2410_udc_write_packet
325 static inline int s3c2410_udc_write_packet(int fifo
,
326 struct s3c2410_request
*req
,
329 unsigned len
= min(req
->req
.length
- req
->req
.actual
, max
);
330 u8
*buf
= req
->req
.buf
+ req
->req
.actual
;
334 dprintk(DEBUG_VERBOSE
, "%s %d %d %d %d\n", __func__
,
335 req
->req
.actual
, req
->req
.length
, len
, req
->req
.actual
+ len
);
337 req
->req
.actual
+= len
;
340 writesb(base_addr
+ fifo
, buf
, len
);
345 * s3c2410_udc_write_fifo
347 * return: 0 = still running, 1 = completed, negative = errno
349 static int s3c2410_udc_write_fifo(struct s3c2410_ep
*ep
,
350 struct s3c2410_request
*req
)
358 idx
= ep
->bEndpointAddress
& 0x7F;
363 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
366 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
369 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
372 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
375 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
379 count
= s3c2410_udc_write_packet(fifo_reg
, req
, ep
->ep
.maxpacket
);
381 /* last packet is often short (sometimes a zlp) */
382 if (count
!= ep
->ep
.maxpacket
)
384 else if (req
->req
.length
!= req
->req
.actual
|| req
->req
.zero
)
389 /* Only ep0 debug messages are interesting */
391 dprintk(DEBUG_NORMAL
,
392 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
393 idx
, count
, req
->req
.actual
, req
->req
.length
,
394 is_last
, req
->req
.zero
);
397 /* The order is important. It prevents sending 2 packets
398 * at the same time */
401 /* Reset signal => no need to say 'data sent' */
402 if (! (udc_read(S3C2410_UDC_USB_INT_REG
)
403 & S3C2410_UDC_USBINT_RESET
))
404 s3c2410_udc_set_ep0_de_in(base_addr
);
405 ep
->dev
->ep0state
=EP0_IDLE
;
407 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
408 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
409 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
410 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
411 S3C2410_UDC_IN_CSR1_REG
);
414 s3c2410_udc_done(ep
, req
, 0);
418 /* Reset signal => no need to say 'data sent' */
419 if (! (udc_read(S3C2410_UDC_USB_INT_REG
)
420 & S3C2410_UDC_USBINT_RESET
))
421 s3c2410_udc_set_ep0_ipr(base_addr
);
423 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
424 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
425 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
426 udc_write(ep_csr
| S3C2410_UDC_ICSR1_PKTRDY
,
427 S3C2410_UDC_IN_CSR1_REG
);
434 static inline int s3c2410_udc_read_packet(int fifo
, u8
*buf
,
435 struct s3c2410_request
*req
, unsigned avail
)
439 len
= min(req
->req
.length
- req
->req
.actual
, avail
);
440 req
->req
.actual
+= len
;
442 readsb(fifo
+ base_addr
, buf
, len
);
447 * return: 0 = still running, 1 = queue empty, negative = errno
449 static int s3c2410_udc_read_fifo(struct s3c2410_ep
*ep
,
450 struct s3c2410_request
*req
)
454 unsigned bufferspace
;
461 idx
= ep
->bEndpointAddress
& 0x7F;
467 fifo_reg
= S3C2410_UDC_EP0_FIFO_REG
;
470 fifo_reg
= S3C2410_UDC_EP1_FIFO_REG
;
473 fifo_reg
= S3C2410_UDC_EP2_FIFO_REG
;
476 fifo_reg
= S3C2410_UDC_EP3_FIFO_REG
;
479 fifo_reg
= S3C2410_UDC_EP4_FIFO_REG
;
483 if (!req
->req
.length
)
486 buf
= req
->req
.buf
+ req
->req
.actual
;
487 bufferspace
= req
->req
.length
- req
->req
.actual
;
489 dprintk(DEBUG_NORMAL
, "%s: buffer full!\n", __func__
);
493 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
495 fifo_count
= s3c2410_udc_fifo_count_out();
496 dprintk(DEBUG_NORMAL
, "%s fifo count : %d\n", __func__
, fifo_count
);
498 if (fifo_count
> ep
->ep
.maxpacket
)
499 avail
= ep
->ep
.maxpacket
;
503 fifo_count
= s3c2410_udc_read_packet(fifo_reg
, buf
, req
, avail
);
505 /* checking this with ep0 is not accurate as we already
506 * read a control request
508 if (idx
!= 0 && fifo_count
< ep
->ep
.maxpacket
) {
510 /* overflowed this request? flush extra data */
511 if (fifo_count
!= avail
)
512 req
->req
.status
= -EOVERFLOW
;
514 is_last
= (req
->req
.length
<= req
->req
.actual
) ? 1 : 0;
517 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
518 fifo_count
= s3c2410_udc_fifo_count_out();
520 /* Only ep0 debug messages are interesting */
522 dprintk(DEBUG_VERBOSE
, "%s fifo count : %d [last %d]\n",
523 __func__
, fifo_count
,is_last
);
527 s3c2410_udc_set_ep0_de_out(base_addr
);
528 ep
->dev
->ep0state
= EP0_IDLE
;
530 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
531 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
532 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
533 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
534 S3C2410_UDC_OUT_CSR1_REG
);
537 s3c2410_udc_done(ep
, req
, 0);
540 s3c2410_udc_clear_ep0_opr(base_addr
);
542 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
543 ep_csr
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
544 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
545 udc_write(ep_csr
& ~S3C2410_UDC_OCSR1_PKTRDY
,
546 S3C2410_UDC_OUT_CSR1_REG
);
553 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest
*crq
)
555 unsigned char *outbuf
= (unsigned char*)crq
;
558 udc_write(0, S3C2410_UDC_INDEX_REG
);
560 bytes_read
= s3c2410_udc_fifo_count_out();
562 dprintk(DEBUG_NORMAL
, "%s: fifo_count=%d\n", __func__
, bytes_read
);
564 if (bytes_read
> sizeof(struct usb_ctrlrequest
))
565 bytes_read
= sizeof(struct usb_ctrlrequest
);
567 readsb(S3C2410_UDC_EP0_FIFO_REG
+ base_addr
, outbuf
, bytes_read
);
569 dprintk(DEBUG_VERBOSE
, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__
,
570 bytes_read
, crq
->bRequest
, crq
->bRequestType
,
571 crq
->wValue
, crq
->wIndex
, crq
->wLength
);
576 static int s3c2410_udc_get_status(struct s3c2410_udc
*dev
,
577 struct usb_ctrlrequest
*crq
)
580 u8 ep_num
= crq
->wIndex
& 0x7F;
581 u8 is_in
= crq
->wIndex
& USB_DIR_IN
;
583 switch (crq
->bRequestType
& USB_RECIP_MASK
) {
584 case USB_RECIP_INTERFACE
:
587 case USB_RECIP_DEVICE
:
588 status
= dev
->devstatus
;
591 case USB_RECIP_ENDPOINT
:
592 if (ep_num
> 4 || crq
->wLength
> 2)
596 udc_write(0, S3C2410_UDC_INDEX_REG
);
597 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
598 status
= status
& S3C2410_UDC_EP0_CSR_SENDSTL
;
600 udc_write(ep_num
, S3C2410_UDC_INDEX_REG
);
602 status
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
603 status
= status
& S3C2410_UDC_ICSR1_SENDSTL
;
605 status
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
606 status
= status
& S3C2410_UDC_OCSR1_SENDSTL
;
610 status
= status
? 1 : 0;
617 /* Seems to be needed to get it working. ouch :( */
619 udc_write(status
& 0xFF, S3C2410_UDC_EP0_FIFO_REG
);
620 udc_write(status
>> 8, S3C2410_UDC_EP0_FIFO_REG
);
621 s3c2410_udc_set_ep0_de_in(base_addr
);
625 /*------------------------- usb state machine -------------------------------*/
626 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
);
628 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc
*dev
,
629 struct s3c2410_ep
*ep
,
630 struct usb_ctrlrequest
*crq
,
635 /* start control request? */
636 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
))
639 s3c2410_udc_nuke(dev
, ep
, -EPROTO
);
641 len
= s3c2410_udc_read_fifo_crq(crq
);
642 if (len
!= sizeof(*crq
)) {
643 dprintk(DEBUG_NORMAL
, "setup begin: fifo READ ERROR"
644 " wanted %d bytes got %d. Stalling out...\n",
646 s3c2410_udc_set_ep0_ss(base_addr
);
650 dprintk(DEBUG_NORMAL
, "bRequest = %d bRequestType %d wLength = %d\n",
651 crq
->bRequest
, crq
->bRequestType
, crq
->wLength
);
653 /* cope with automagic for some standard requests. */
654 dev
->req_std
= (crq
->bRequestType
& USB_TYPE_MASK
)
655 == USB_TYPE_STANDARD
;
657 dev
->req_pending
= 1;
659 switch (crq
->bRequest
) {
660 case USB_REQ_SET_CONFIGURATION
:
661 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_CONFIGURATION ... \n");
663 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
665 s3c2410_udc_set_ep0_de_out(base_addr
);
669 case USB_REQ_SET_INTERFACE
:
670 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_INTERFACE ... \n");
672 if (crq
->bRequestType
== USB_RECIP_INTERFACE
) {
674 s3c2410_udc_set_ep0_de_out(base_addr
);
678 case USB_REQ_SET_ADDRESS
:
679 dprintk(DEBUG_NORMAL
, "USB_REQ_SET_ADDRESS ... \n");
681 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
682 tmp
= crq
->wValue
& 0x7F;
684 udc_write((tmp
| S3C2410_UDC_FUNCADDR_UPDATE
),
685 S3C2410_UDC_FUNC_ADDR_REG
);
686 s3c2410_udc_set_ep0_de_out(base_addr
);
691 case USB_REQ_GET_STATUS
:
692 dprintk(DEBUG_NORMAL
, "USB_REQ_GET_STATUS ... \n");
693 s3c2410_udc_clear_ep0_opr(base_addr
);
696 if (!s3c2410_udc_get_status(dev
, crq
)) {
702 case USB_REQ_CLEAR_FEATURE
:
703 s3c2410_udc_clear_ep0_opr(base_addr
);
705 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
708 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
711 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 0);
712 s3c2410_udc_set_ep0_de_out(base_addr
);
715 case USB_REQ_SET_FEATURE
:
716 s3c2410_udc_clear_ep0_opr(base_addr
);
718 if (crq
->bRequestType
!= USB_RECIP_ENDPOINT
)
721 if (crq
->wValue
!= USB_ENDPOINT_HALT
|| crq
->wLength
!= 0)
724 s3c2410_udc_set_halt(&dev
->ep
[crq
->wIndex
& 0x7f].ep
, 1);
725 s3c2410_udc_set_ep0_de_out(base_addr
);
729 s3c2410_udc_clear_ep0_opr(base_addr
);
733 if (crq
->bRequestType
& USB_DIR_IN
)
734 dev
->ep0state
= EP0_IN_DATA_PHASE
;
736 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
741 /* deliver the request to the gadget driver */
742 ret
= dev
->driver
->setup(&dev
->gadget
, crq
);
744 if (dev
->req_config
) {
745 dprintk(DEBUG_NORMAL
, "config change %02x fail %d?\n",
750 if (ret
== -EOPNOTSUPP
)
751 dprintk(DEBUG_NORMAL
, "Operation not supported\n");
753 dprintk(DEBUG_NORMAL
,
754 "dev->driver->setup failed. (%d)\n", ret
);
757 s3c2410_udc_set_ep0_ss(base_addr
);
758 s3c2410_udc_set_ep0_de_out(base_addr
);
759 dev
->ep0state
= EP0_IDLE
;
760 /* deferred i/o == no response yet */
761 } else if (dev
->req_pending
) {
762 dprintk(DEBUG_VERBOSE
, "dev->req_pending... what now?\n");
766 dprintk(DEBUG_VERBOSE
, "ep0state %s\n", ep0states
[dev
->ep0state
]);
769 static void s3c2410_udc_handle_ep0(struct s3c2410_udc
*dev
)
772 struct s3c2410_ep
*ep
= &dev
->ep
[0];
773 struct s3c2410_request
*req
;
774 struct usb_ctrlrequest crq
;
776 if (list_empty(&ep
->queue
))
779 req
= list_entry(ep
->queue
.next
, struct s3c2410_request
, queue
);
781 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
782 * S3C2410_UDC_EP0_CSR_REG when index is zero */
784 udc_write(0, S3C2410_UDC_INDEX_REG
);
785 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
787 dprintk(DEBUG_NORMAL
, "ep0csr %x ep0state %s\n",
788 ep0csr
, ep0states
[dev
->ep0state
]);
790 /* clear stall status */
791 if (ep0csr
& S3C2410_UDC_EP0_CSR_SENTSTL
) {
792 s3c2410_udc_nuke(dev
, ep
, -EPIPE
);
793 dprintk(DEBUG_NORMAL
, "... clear SENT_STALL ...\n");
794 s3c2410_udc_clear_ep0_sst(base_addr
);
795 dev
->ep0state
= EP0_IDLE
;
799 /* clear setup end */
800 if (ep0csr
& S3C2410_UDC_EP0_CSR_SE
) {
801 dprintk(DEBUG_NORMAL
, "... serviced SETUP_END ...\n");
802 s3c2410_udc_nuke(dev
, ep
, 0);
803 s3c2410_udc_clear_ep0_se(base_addr
);
804 dev
->ep0state
= EP0_IDLE
;
807 switch (dev
->ep0state
) {
809 s3c2410_udc_handle_ep0_idle(dev
, ep
, &crq
, ep0csr
);
812 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
813 dprintk(DEBUG_NORMAL
, "EP0_IN_DATA_PHASE ... what now?\n");
814 if (!(ep0csr
& S3C2410_UDC_EP0_CSR_IPKRDY
) && req
) {
815 s3c2410_udc_write_fifo(ep
, req
);
819 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
820 dprintk(DEBUG_NORMAL
, "EP0_OUT_DATA_PHASE ... what now?\n");
821 if ((ep0csr
& S3C2410_UDC_EP0_CSR_OPKRDY
) && req
) {
822 s3c2410_udc_read_fifo(ep
,req
);
827 dprintk(DEBUG_NORMAL
, "EP0_END_XFER ... what now?\n");
828 dev
->ep0state
= EP0_IDLE
;
832 dprintk(DEBUG_NORMAL
, "EP0_STALL ... what now?\n");
833 dev
->ep0state
= EP0_IDLE
;
839 * handle_ep - Manage I/O endpoints
842 static void s3c2410_udc_handle_ep(struct s3c2410_ep
*ep
)
844 struct s3c2410_request
*req
;
845 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
849 if (likely (!list_empty(&ep
->queue
)))
850 req
= list_entry(ep
->queue
.next
,
851 struct s3c2410_request
, queue
);
855 idx
= ep
->bEndpointAddress
& 0x7F;
858 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
859 ep_csr1
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
860 dprintk(DEBUG_VERBOSE
, "ep%01d write csr:%02x %d\n",
861 idx
, ep_csr1
, req
? 1 : 0);
863 if (ep_csr1
& S3C2410_UDC_ICSR1_SENTSTL
) {
864 dprintk(DEBUG_VERBOSE
, "st\n");
865 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
866 udc_write(ep_csr1
& ~S3C2410_UDC_ICSR1_SENTSTL
,
867 S3C2410_UDC_IN_CSR1_REG
);
871 if (!(ep_csr1
& S3C2410_UDC_ICSR1_PKTRDY
) && req
) {
872 s3c2410_udc_write_fifo(ep
,req
);
875 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
876 ep_csr1
= udc_read(S3C2410_UDC_OUT_CSR1_REG
);
877 dprintk(DEBUG_VERBOSE
, "ep%01d rd csr:%02x\n", idx
, ep_csr1
);
879 if (ep_csr1
& S3C2410_UDC_OCSR1_SENTSTL
) {
880 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
881 udc_write(ep_csr1
& ~S3C2410_UDC_OCSR1_SENTSTL
,
882 S3C2410_UDC_OUT_CSR1_REG
);
886 if ((ep_csr1
& S3C2410_UDC_OCSR1_PKTRDY
) && req
) {
887 s3c2410_udc_read_fifo(ep
,req
);
892 #include <mach/regs-irq.h>
895 * s3c2410_udc_irq - interrupt handler
897 static irqreturn_t
s3c2410_udc_irq(int dummy
, void *_dev
)
899 struct s3c2410_udc
*dev
= _dev
;
908 spin_lock_irqsave(&dev
->lock
, flags
);
910 /* Driver connected ? */
912 /* Clear interrupts */
913 udc_write(udc_read(S3C2410_UDC_USB_INT_REG
),
914 S3C2410_UDC_USB_INT_REG
);
915 udc_write(udc_read(S3C2410_UDC_EP_INT_REG
),
916 S3C2410_UDC_EP_INT_REG
);
920 idx
= udc_read(S3C2410_UDC_INDEX_REG
);
922 /* Read status registers */
923 usb_status
= udc_read(S3C2410_UDC_USB_INT_REG
);
924 usbd_status
= udc_read(S3C2410_UDC_EP_INT_REG
);
925 pwr_reg
= udc_read(S3C2410_UDC_PWR_REG
);
927 udc_writeb(base_addr
, S3C2410_UDC_INDEX_EP0
, S3C2410_UDC_INDEX_REG
);
928 ep0csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
930 dprintk(DEBUG_NORMAL
, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
931 usb_status
, usbd_status
, pwr_reg
, ep0csr
);
934 * Now, handle interrupts. There's two types :
935 * - Reset, Resume, Suspend coming -> usb_int_reg
940 if (usb_status
& S3C2410_UDC_USBINT_RESET
) {
941 /* two kind of reset :
942 * - reset start -> pwr reg = 8
943 * - reset end -> pwr reg = 0
945 dprintk(DEBUG_NORMAL
, "USB reset csr %x pwr %x\n",
948 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
949 udc_write(0x00, S3C2410_UDC_INDEX_REG
);
950 udc_write((dev
->ep
[0].ep
.maxpacket
& 0x7ff) >> 3,
951 S3C2410_UDC_MAXP_REG
);
954 dev
->ep0state
= EP0_IDLE
;
955 dev
->gadget
.speed
= USB_SPEED_FULL
;
957 /* clear interrupt */
958 udc_write(S3C2410_UDC_USBINT_RESET
,
959 S3C2410_UDC_USB_INT_REG
);
961 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
962 spin_unlock_irqrestore(&dev
->lock
, flags
);
967 if (usb_status
& S3C2410_UDC_USBINT_RESUME
) {
968 dprintk(DEBUG_NORMAL
, "USB resume\n");
970 /* clear interrupt */
971 udc_write(S3C2410_UDC_USBINT_RESUME
,
972 S3C2410_UDC_USB_INT_REG
);
974 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
976 && dev
->driver
->resume
)
977 dev
->driver
->resume(&dev
->gadget
);
981 if (usb_status
& S3C2410_UDC_USBINT_SUSPEND
) {
982 dprintk(DEBUG_NORMAL
, "USB suspend\n");
984 /* clear interrupt */
985 udc_write(S3C2410_UDC_USBINT_SUSPEND
,
986 S3C2410_UDC_USB_INT_REG
);
988 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
990 && dev
->driver
->suspend
)
991 dev
->driver
->suspend(&dev
->gadget
);
993 dev
->ep0state
= EP0_IDLE
;
997 /* control traffic */
998 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
999 * generate an interrupt
1001 if (usbd_status
& S3C2410_UDC_INT_EP0
) {
1002 dprintk(DEBUG_VERBOSE
, "USB ep0 irq\n");
1003 /* Clear the interrupt bit by setting it to 1 */
1004 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_REG
);
1005 s3c2410_udc_handle_ep0(dev
);
1008 /* endpoint data transfers */
1009 for (i
= 1; i
< S3C2410_ENDPOINTS
; i
++) {
1011 if (usbd_status
& tmp
) {
1012 dprintk(DEBUG_VERBOSE
, "USB ep%d irq\n", i
);
1014 /* Clear the interrupt bit by setting it to 1 */
1015 udc_write(tmp
, S3C2410_UDC_EP_INT_REG
);
1016 s3c2410_udc_handle_ep(&dev
->ep
[i
]);
1020 dprintk(DEBUG_VERBOSE
, "irq: %d s3c2410_udc_done.\n", IRQ_USBD
);
1022 /* Restore old index */
1023 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1025 spin_unlock_irqrestore(&dev
->lock
, flags
);
1029 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1031 static inline struct s3c2410_ep
*to_s3c2410_ep(struct usb_ep
*ep
)
1033 return container_of(ep
, struct s3c2410_ep
, ep
);
1036 static inline struct s3c2410_udc
*to_s3c2410_udc(struct usb_gadget
*gadget
)
1038 return container_of(gadget
, struct s3c2410_udc
, gadget
);
1041 static inline struct s3c2410_request
*to_s3c2410_req(struct usb_request
*req
)
1043 return container_of(req
, struct s3c2410_request
, req
);
1047 * s3c2410_udc_ep_enable
1049 static int s3c2410_udc_ep_enable(struct usb_ep
*_ep
,
1050 const struct usb_endpoint_descriptor
*desc
)
1052 struct s3c2410_udc
*dev
;
1053 struct s3c2410_ep
*ep
;
1055 unsigned long flags
;
1059 ep
= to_s3c2410_ep(_ep
);
1061 if (!_ep
|| !desc
|| ep
->desc
1062 || _ep
->name
== ep0name
1063 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1067 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1070 max
= le16_to_cpu(desc
->wMaxPacketSize
) & 0x1fff;
1072 local_irq_save (flags
);
1073 _ep
->maxpacket
= max
& 0x7ff;
1076 ep
->bEndpointAddress
= desc
->bEndpointAddress
;
1078 /* set max packet */
1079 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1080 udc_write(max
>> 3, S3C2410_UDC_MAXP_REG
);
1082 /* set type, direction, address; reset fifo counters */
1083 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
1084 csr1
= S3C2410_UDC_ICSR1_FFLUSH
|S3C2410_UDC_ICSR1_CLRDT
;
1085 csr2
= S3C2410_UDC_ICSR2_MODEIN
|S3C2410_UDC_ICSR2_DMAIEN
;
1087 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1088 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1089 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1090 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1092 /* don't flush in fifo or it will cause endpoint interrupt */
1093 csr1
= S3C2410_UDC_ICSR1_CLRDT
;
1094 csr2
= S3C2410_UDC_ICSR2_DMAIEN
;
1096 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1097 udc_write(csr1
, S3C2410_UDC_IN_CSR1_REG
);
1098 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1099 udc_write(csr2
, S3C2410_UDC_IN_CSR2_REG
);
1101 csr1
= S3C2410_UDC_OCSR1_FFLUSH
| S3C2410_UDC_OCSR1_CLRDT
;
1102 csr2
= S3C2410_UDC_OCSR2_DMAIEN
;
1104 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1105 udc_write(csr1
, S3C2410_UDC_OUT_CSR1_REG
);
1106 udc_write(ep
->num
, S3C2410_UDC_INDEX_REG
);
1107 udc_write(csr2
, S3C2410_UDC_OUT_CSR2_REG
);
1111 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1112 udc_write(int_en_reg
| (1 << ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1114 /* print some debug message */
1115 tmp
= desc
->bEndpointAddress
;
1116 dprintk (DEBUG_NORMAL
, "enable %s(%d) ep%x%s-blk max %02x\n",
1117 _ep
->name
,ep
->num
, tmp
,
1118 desc
->bEndpointAddress
& USB_DIR_IN
? "in" : "out", max
);
1120 local_irq_restore (flags
);
1121 s3c2410_udc_set_halt(_ep
, 0);
1127 * s3c2410_udc_ep_disable
1129 static int s3c2410_udc_ep_disable(struct usb_ep
*_ep
)
1131 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1132 unsigned long flags
;
1135 if (!_ep
|| !ep
->desc
) {
1136 dprintk(DEBUG_NORMAL
, "%s not enabled\n",
1137 _ep
? ep
->ep
.name
: NULL
);
1141 local_irq_save(flags
);
1143 dprintk(DEBUG_NORMAL
, "ep_disable: %s\n", _ep
->name
);
1148 s3c2410_udc_nuke (ep
->dev
, ep
, -ESHUTDOWN
);
1151 int_en_reg
= udc_read(S3C2410_UDC_EP_INT_EN_REG
);
1152 udc_write(int_en_reg
& ~(1<<ep
->num
), S3C2410_UDC_EP_INT_EN_REG
);
1154 local_irq_restore(flags
);
1156 dprintk(DEBUG_NORMAL
, "%s disabled\n", _ep
->name
);
1162 * s3c2410_udc_alloc_request
1164 static struct usb_request
*
1165 s3c2410_udc_alloc_request(struct usb_ep
*_ep
, gfp_t mem_flags
)
1167 struct s3c2410_request
*req
;
1169 dprintk(DEBUG_VERBOSE
,"%s(%p,%d)\n", __func__
, _ep
, mem_flags
);
1174 req
= kzalloc (sizeof(struct s3c2410_request
), mem_flags
);
1178 INIT_LIST_HEAD (&req
->queue
);
1183 * s3c2410_udc_free_request
1186 s3c2410_udc_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1188 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1189 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1191 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1193 if (!ep
|| !_req
|| (!ep
->desc
&& _ep
->name
!= ep0name
))
1196 WARN_ON (!list_empty (&req
->queue
));
1203 static int s3c2410_udc_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1206 struct s3c2410_request
*req
= to_s3c2410_req(_req
);
1207 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1208 struct s3c2410_udc
*dev
;
1211 unsigned long flags
;
1213 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
1214 dprintk(DEBUG_NORMAL
, "%s: invalid args\n", __func__
);
1219 if (unlikely (!dev
->driver
1220 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
1224 local_irq_save (flags
);
1226 if (unlikely(!_req
|| !_req
->complete
1227 || !_req
->buf
|| !list_empty(&req
->queue
))) {
1229 dprintk(DEBUG_NORMAL
, "%s: 1 X X X\n", __func__
);
1231 dprintk(DEBUG_NORMAL
, "%s: 0 %01d %01d %01d\n",
1232 __func__
, !_req
->complete
,!_req
->buf
,
1233 !list_empty(&req
->queue
));
1236 local_irq_restore(flags
);
1240 _req
->status
= -EINPROGRESS
;
1243 dprintk(DEBUG_VERBOSE
, "%s: ep%x len %d\n",
1244 __func__
, ep
->bEndpointAddress
, _req
->length
);
1246 if (ep
->bEndpointAddress
) {
1247 udc_write(ep
->bEndpointAddress
& 0x7F, S3C2410_UDC_INDEX_REG
);
1249 ep_csr
= udc_read((ep
->bEndpointAddress
& USB_DIR_IN
)
1250 ? S3C2410_UDC_IN_CSR1_REG
1251 : S3C2410_UDC_OUT_CSR1_REG
);
1252 fifo_count
= s3c2410_udc_fifo_count_out();
1254 udc_write(0, S3C2410_UDC_INDEX_REG
);
1255 ep_csr
= udc_read(S3C2410_UDC_IN_CSR1_REG
);
1256 fifo_count
= s3c2410_udc_fifo_count_out();
1259 /* kickstart this i/o queue? */
1260 if (list_empty(&ep
->queue
) && !ep
->halted
) {
1261 if (ep
->bEndpointAddress
== 0 /* ep0 */) {
1262 switch (dev
->ep0state
) {
1263 case EP0_IN_DATA_PHASE
:
1264 if (!(ep_csr
&S3C2410_UDC_EP0_CSR_IPKRDY
)
1265 && s3c2410_udc_write_fifo(ep
,
1267 dev
->ep0state
= EP0_IDLE
;
1272 case EP0_OUT_DATA_PHASE
:
1274 || ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1275 && s3c2410_udc_read_fifo(ep
,
1277 dev
->ep0state
= EP0_IDLE
;
1283 local_irq_restore(flags
);
1286 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1287 && (!(ep_csr
&S3C2410_UDC_OCSR1_PKTRDY
))
1288 && s3c2410_udc_write_fifo(ep
, req
)) {
1290 } else if ((ep_csr
& S3C2410_UDC_OCSR1_PKTRDY
)
1292 && s3c2410_udc_read_fifo(ep
, req
)) {
1297 /* pio or dma irq handler advances the queue. */
1298 if (likely (req
!= 0))
1299 list_add_tail(&req
->queue
, &ep
->queue
);
1301 local_irq_restore(flags
);
1303 dprintk(DEBUG_VERBOSE
, "%s ok\n", __func__
);
1308 * s3c2410_udc_dequeue
1310 static int s3c2410_udc_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1312 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1313 struct s3c2410_udc
*udc
;
1314 int retval
= -EINVAL
;
1315 unsigned long flags
;
1316 struct s3c2410_request
*req
= NULL
;
1318 dprintk(DEBUG_VERBOSE
, "%s(%p,%p)\n", __func__
, _ep
, _req
);
1320 if (!the_controller
->driver
)
1326 udc
= to_s3c2410_udc(ep
->gadget
);
1328 local_irq_save (flags
);
1330 list_for_each_entry (req
, &ep
->queue
, queue
) {
1331 if (&req
->req
== _req
) {
1332 list_del_init (&req
->queue
);
1333 _req
->status
= -ECONNRESET
;
1340 dprintk(DEBUG_VERBOSE
,
1341 "dequeued req %p from %s, len %d buf %p\n",
1342 req
, _ep
->name
, _req
->length
, _req
->buf
);
1344 s3c2410_udc_done(ep
, req
, -ECONNRESET
);
1347 local_irq_restore (flags
);
1352 * s3c2410_udc_set_halt
1354 static int s3c2410_udc_set_halt(struct usb_ep
*_ep
, int value
)
1356 struct s3c2410_ep
*ep
= to_s3c2410_ep(_ep
);
1358 unsigned long flags
;
1361 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
1362 dprintk(DEBUG_NORMAL
, "%s: inval 2\n", __func__
);
1366 local_irq_save (flags
);
1368 idx
= ep
->bEndpointAddress
& 0x7F;
1371 s3c2410_udc_set_ep0_ss(base_addr
);
1372 s3c2410_udc_set_ep0_de_out(base_addr
);
1374 udc_write(idx
, S3C2410_UDC_INDEX_REG
);
1375 ep_csr
= udc_read((ep
->bEndpointAddress
&USB_DIR_IN
)
1376 ? S3C2410_UDC_IN_CSR1_REG
1377 : S3C2410_UDC_OUT_CSR1_REG
);
1379 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
1381 udc_write(ep_csr
| S3C2410_UDC_ICSR1_SENDSTL
,
1382 S3C2410_UDC_IN_CSR1_REG
);
1384 ep_csr
&= ~S3C2410_UDC_ICSR1_SENDSTL
;
1385 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1386 ep_csr
|= S3C2410_UDC_ICSR1_CLRDT
;
1387 udc_write(ep_csr
, S3C2410_UDC_IN_CSR1_REG
);
1391 udc_write(ep_csr
| S3C2410_UDC_OCSR1_SENDSTL
,
1392 S3C2410_UDC_OUT_CSR1_REG
);
1394 ep_csr
&= ~S3C2410_UDC_OCSR1_SENDSTL
;
1395 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1396 ep_csr
|= S3C2410_UDC_OCSR1_CLRDT
;
1397 udc_write(ep_csr
, S3C2410_UDC_OUT_CSR1_REG
);
1402 ep
->halted
= value
? 1 : 0;
1403 local_irq_restore (flags
);
1408 static const struct usb_ep_ops s3c2410_ep_ops
= {
1409 .enable
= s3c2410_udc_ep_enable
,
1410 .disable
= s3c2410_udc_ep_disable
,
1412 .alloc_request
= s3c2410_udc_alloc_request
,
1413 .free_request
= s3c2410_udc_free_request
,
1415 .queue
= s3c2410_udc_queue
,
1416 .dequeue
= s3c2410_udc_dequeue
,
1418 .set_halt
= s3c2410_udc_set_halt
,
1421 /*------------------------- usb_gadget_ops ----------------------------------*/
1424 * s3c2410_udc_get_frame
1426 static int s3c2410_udc_get_frame(struct usb_gadget
*_gadget
)
1430 dprintk(DEBUG_VERBOSE
, "%s()\n", __func__
);
1432 tmp
= udc_read(S3C2410_UDC_FRAME_NUM2_REG
) << 8;
1433 tmp
|= udc_read(S3C2410_UDC_FRAME_NUM1_REG
);
1438 * s3c2410_udc_wakeup
1440 static int s3c2410_udc_wakeup(struct usb_gadget
*_gadget
)
1442 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1447 * s3c2410_udc_set_selfpowered
1449 static int s3c2410_udc_set_selfpowered(struct usb_gadget
*gadget
, int value
)
1451 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1453 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1456 udc
->devstatus
|= (1 << USB_DEVICE_SELF_POWERED
);
1458 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1463 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
);
1464 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
);
1466 static int s3c2410_udc_set_pullup(struct s3c2410_udc
*udc
, int is_on
)
1468 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1470 if (udc_info
&& udc_info
->udc_command
) {
1472 s3c2410_udc_enable(udc
);
1474 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1475 if (udc
->driver
&& udc
->driver
->disconnect
)
1476 udc
->driver
->disconnect(&udc
->gadget
);
1479 s3c2410_udc_disable(udc
);
1488 static int s3c2410_udc_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1490 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1492 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1494 udc
->vbus
= (is_active
!= 0);
1495 s3c2410_udc_set_pullup(udc
, is_active
);
1499 static int s3c2410_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1501 struct s3c2410_udc
*udc
= to_s3c2410_udc(gadget
);
1503 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1505 s3c2410_udc_set_pullup(udc
, is_on
? 0 : 1);
1509 static irqreturn_t
s3c2410_udc_vbus_irq(int irq
, void *_dev
)
1511 struct s3c2410_udc
*dev
= _dev
;
1514 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1516 value
= gpio_get_value(udc_info
->vbus_pin
) ? 1 : 0;
1517 if (udc_info
->vbus_pin_inverted
)
1520 if (value
!= dev
->vbus
)
1521 s3c2410_udc_vbus_session(&dev
->gadget
, value
);
1526 static int s3c2410_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1528 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1530 if (udc_info
&& udc_info
->vbus_draw
) {
1531 udc_info
->vbus_draw(ma
);
1538 static const struct usb_gadget_ops s3c2410_ops
= {
1539 .get_frame
= s3c2410_udc_get_frame
,
1540 .wakeup
= s3c2410_udc_wakeup
,
1541 .set_selfpowered
= s3c2410_udc_set_selfpowered
,
1542 .pullup
= s3c2410_udc_pullup
,
1543 .vbus_session
= s3c2410_udc_vbus_session
,
1544 .vbus_draw
= s3c2410_vbus_draw
,
1547 /*------------------------- gadget driver handling---------------------------*/
1549 * s3c2410_udc_disable
1551 static void s3c2410_udc_disable(struct s3c2410_udc
*dev
)
1553 dprintk(DEBUG_NORMAL
, "%s()\n", __func__
);
1555 /* Disable all interrupts */
1556 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG
);
1557 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG
);
1559 /* Clear the interrupt registers */
1560 udc_write(S3C2410_UDC_USBINT_RESET
1561 | S3C2410_UDC_USBINT_RESUME
1562 | S3C2410_UDC_USBINT_SUSPEND
,
1563 S3C2410_UDC_USB_INT_REG
);
1565 udc_write(0x1F, S3C2410_UDC_EP_INT_REG
);
1567 /* Good bye, cruel world */
1568 if (udc_info
&& udc_info
->udc_command
)
1569 udc_info
->udc_command(S3C2410_UDC_P_DISABLE
);
1571 /* Set speed to unknown */
1572 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1576 * s3c2410_udc_reinit
1578 static void s3c2410_udc_reinit(struct s3c2410_udc
*dev
)
1582 /* device/ep0 records init */
1583 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1584 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1585 dev
->ep0state
= EP0_IDLE
;
1587 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1588 struct s3c2410_ep
*ep
= &dev
->ep
[i
];
1591 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1596 INIT_LIST_HEAD (&ep
->queue
);
1601 * s3c2410_udc_enable
1603 static void s3c2410_udc_enable(struct s3c2410_udc
*dev
)
1607 dprintk(DEBUG_NORMAL
, "s3c2410_udc_enable called\n");
1609 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1610 dev
->gadget
.speed
= USB_SPEED_FULL
;
1612 /* Set MAXP for all endpoints */
1613 for (i
= 0; i
< S3C2410_ENDPOINTS
; i
++) {
1614 udc_write(i
, S3C2410_UDC_INDEX_REG
);
1615 udc_write((dev
->ep
[i
].ep
.maxpacket
& 0x7ff) >> 3,
1616 S3C2410_UDC_MAXP_REG
);
1619 /* Set default power state */
1620 udc_write(DEFAULT_POWER_STATE
, S3C2410_UDC_PWR_REG
);
1622 /* Enable reset and suspend interrupt interrupts */
1623 udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_SUSPEND
,
1624 S3C2410_UDC_USB_INT_EN_REG
);
1626 /* Enable ep0 interrupt */
1627 udc_write(S3C2410_UDC_INT_EP0
, S3C2410_UDC_EP_INT_EN_REG
);
1629 /* time to say "hello, world" */
1630 if (udc_info
&& udc_info
->udc_command
)
1631 udc_info
->udc_command(S3C2410_UDC_P_ENABLE
);
1635 * usb_gadget_probe_driver
1637 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
1638 int (*bind
)(struct usb_gadget
*))
1640 struct s3c2410_udc
*udc
= the_controller
;
1643 dprintk(DEBUG_NORMAL
, "%s() '%s'\n", __func__
, driver
->driver
.name
);
1652 if (!bind
|| !driver
->setup
|| driver
->speed
< USB_SPEED_FULL
) {
1653 printk(KERN_ERR
"Invalid driver: bind %p setup %p speed %d\n",
1654 bind
, driver
->setup
, driver
->speed
);
1658 if (!driver
->unbind
) {
1659 printk(KERN_ERR
"Invalid driver: no unbind method\n");
1664 /* Hook the driver */
1665 udc
->driver
= driver
;
1666 udc
->gadget
.dev
.driver
= &driver
->driver
;
1668 /* Bind the driver */
1669 if ((retval
= device_add(&udc
->gadget
.dev
)) != 0) {
1670 printk(KERN_ERR
"Error in device_add() : %d\n",retval
);
1671 goto register_error
;
1674 dprintk(DEBUG_NORMAL
, "binding gadget driver '%s'\n",
1675 driver
->driver
.name
);
1677 if ((retval
= bind(&udc
->gadget
)) != 0) {
1678 device_del(&udc
->gadget
.dev
);
1679 goto register_error
;
1683 s3c2410_udc_enable(udc
);
1689 udc
->gadget
.dev
.driver
= NULL
;
1692 EXPORT_SYMBOL(usb_gadget_probe_driver
);
1695 * usb_gadget_unregister_driver
1697 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1699 struct s3c2410_udc
*udc
= the_controller
;
1704 if (!driver
|| driver
!= udc
->driver
|| !driver
->unbind
)
1707 dprintk(DEBUG_NORMAL
, "usb_gadget_unregister_driver() '%s'\n",
1708 driver
->driver
.name
);
1710 /* report disconnect */
1711 if (driver
->disconnect
)
1712 driver
->disconnect(&udc
->gadget
);
1714 driver
->unbind(&udc
->gadget
);
1716 device_del(&udc
->gadget
.dev
);
1720 s3c2410_udc_disable(udc
);
1725 /*---------------------------------------------------------------------------*/
1726 static struct s3c2410_udc memory
= {
1728 .ops
= &s3c2410_ops
,
1729 .ep0
= &memory
.ep
[0].ep
,
1730 .name
= gadget_name
,
1732 .init_name
= "gadget",
1736 /* control endpoint */
1741 .ops
= &s3c2410_ep_ops
,
1742 .maxpacket
= EP0_FIFO_SIZE
,
1747 /* first group of endpoints */
1752 .ops
= &s3c2410_ep_ops
,
1753 .maxpacket
= EP_FIFO_SIZE
,
1756 .fifo_size
= EP_FIFO_SIZE
,
1757 .bEndpointAddress
= 1,
1758 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1764 .ops
= &s3c2410_ep_ops
,
1765 .maxpacket
= EP_FIFO_SIZE
,
1768 .fifo_size
= EP_FIFO_SIZE
,
1769 .bEndpointAddress
= 2,
1770 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1776 .ops
= &s3c2410_ep_ops
,
1777 .maxpacket
= EP_FIFO_SIZE
,
1780 .fifo_size
= EP_FIFO_SIZE
,
1781 .bEndpointAddress
= 3,
1782 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1788 .ops
= &s3c2410_ep_ops
,
1789 .maxpacket
= EP_FIFO_SIZE
,
1792 .fifo_size
= EP_FIFO_SIZE
,
1793 .bEndpointAddress
= 4,
1794 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1800 * probe - binds to the platform device
1802 static int s3c2410_udc_probe(struct platform_device
*pdev
)
1804 struct s3c2410_udc
*udc
= &memory
;
1805 struct device
*dev
= &pdev
->dev
;
1809 dev_dbg(dev
, "%s()\n", __func__
);
1811 usb_bus_clock
= clk_get(NULL
, "usb-bus-gadget");
1812 if (IS_ERR(usb_bus_clock
)) {
1813 dev_err(dev
, "failed to get usb bus clock source\n");
1814 return PTR_ERR(usb_bus_clock
);
1817 clk_enable(usb_bus_clock
);
1819 udc_clock
= clk_get(NULL
, "usb-device");
1820 if (IS_ERR(udc_clock
)) {
1821 dev_err(dev
, "failed to get udc clock source\n");
1822 return PTR_ERR(udc_clock
);
1825 clk_enable(udc_clock
);
1829 dev_dbg(dev
, "got and enabled clocks\n");
1831 if (strncmp(pdev
->name
, "s3c2440", 7) == 0) {
1832 dev_info(dev
, "S3C2440: increasing FIFO to 128 bytes\n");
1833 memory
.ep
[1].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1834 memory
.ep
[2].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1835 memory
.ep
[3].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1836 memory
.ep
[4].fifo_size
= S3C2440_EP_FIFO_SIZE
;
1839 spin_lock_init (&udc
->lock
);
1840 udc_info
= pdev
->dev
.platform_data
;
1842 rsrc_start
= S3C2410_PA_USBDEV
;
1843 rsrc_len
= S3C24XX_SZ_USBDEV
;
1845 if (!request_mem_region(rsrc_start
, rsrc_len
, gadget_name
))
1848 base_addr
= ioremap(rsrc_start
, rsrc_len
);
1854 device_initialize(&udc
->gadget
.dev
);
1855 udc
->gadget
.dev
.parent
= &pdev
->dev
;
1856 udc
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1858 the_controller
= udc
;
1859 platform_set_drvdata(pdev
, udc
);
1861 s3c2410_udc_disable(udc
);
1862 s3c2410_udc_reinit(udc
);
1864 /* irq setup after old hardware state is cleaned up */
1865 retval
= request_irq(IRQ_USBD
, s3c2410_udc_irq
,
1866 IRQF_DISABLED
, gadget_name
, udc
);
1869 dev_err(dev
, "cannot get irq %i, err %d\n", IRQ_USBD
, retval
);
1874 dev_dbg(dev
, "got irq %i\n", IRQ_USBD
);
1876 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1877 retval
= gpio_request(udc_info
->vbus_pin
, "udc vbus");
1879 dev_err(dev
, "cannot claim vbus pin\n");
1883 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1885 dev_err(dev
, "no irq for gpio vbus pin\n");
1886 goto err_gpio_claim
;
1889 retval
= request_irq(irq
, s3c2410_udc_vbus_irq
,
1890 IRQF_DISABLED
| IRQF_TRIGGER_RISING
1891 | IRQF_TRIGGER_FALLING
| IRQF_SHARED
,
1895 dev_err(dev
, "can't get vbus irq %d, err %d\n",
1898 goto err_gpio_claim
;
1901 dev_dbg(dev
, "got irq %i\n", irq
);
1906 if (s3c2410_udc_debugfs_root
) {
1907 udc
->regs_info
= debugfs_create_file("registers", S_IRUGO
,
1908 s3c2410_udc_debugfs_root
,
1909 udc
, &s3c2410_udc_debugfs_fops
);
1910 if (!udc
->regs_info
)
1911 dev_warn(dev
, "debugfs file creation failed\n");
1914 dev_dbg(dev
, "probe ok\n");
1919 if (udc_info
&& udc_info
->vbus_pin
> 0)
1920 gpio_free(udc_info
->vbus_pin
);
1922 free_irq(IRQ_USBD
, udc
);
1926 release_mem_region(rsrc_start
, rsrc_len
);
1932 * s3c2410_udc_remove
1934 static int s3c2410_udc_remove(struct platform_device
*pdev
)
1936 struct s3c2410_udc
*udc
= platform_get_drvdata(pdev
);
1939 dev_dbg(&pdev
->dev
, "%s()\n", __func__
);
1943 debugfs_remove(udc
->regs_info
);
1945 if (udc_info
&& udc_info
->vbus_pin
> 0) {
1946 irq
= gpio_to_irq(udc_info
->vbus_pin
);
1950 free_irq(IRQ_USBD
, udc
);
1953 release_mem_region(rsrc_start
, rsrc_len
);
1955 platform_set_drvdata(pdev
, NULL
);
1957 if (!IS_ERR(udc_clock
) && udc_clock
!= NULL
) {
1958 clk_disable(udc_clock
);
1963 if (!IS_ERR(usb_bus_clock
) && usb_bus_clock
!= NULL
) {
1964 clk_disable(usb_bus_clock
);
1965 clk_put(usb_bus_clock
);
1966 usb_bus_clock
= NULL
;
1969 dev_dbg(&pdev
->dev
, "%s: remove ok\n", __func__
);
1974 static int s3c2410_udc_suspend(struct platform_device
*pdev
, pm_message_t message
)
1976 if (udc_info
&& udc_info
->udc_command
)
1977 udc_info
->udc_command(S3C2410_UDC_P_DISABLE
);
1982 static int s3c2410_udc_resume(struct platform_device
*pdev
)
1984 if (udc_info
&& udc_info
->udc_command
)
1985 udc_info
->udc_command(S3C2410_UDC_P_ENABLE
);
1990 #define s3c2410_udc_suspend NULL
1991 #define s3c2410_udc_resume NULL
1994 static struct platform_driver udc_driver_2410
= {
1996 .name
= "s3c2410-usbgadget",
1997 .owner
= THIS_MODULE
,
1999 .probe
= s3c2410_udc_probe
,
2000 .remove
= s3c2410_udc_remove
,
2001 .suspend
= s3c2410_udc_suspend
,
2002 .resume
= s3c2410_udc_resume
,
2005 static struct platform_driver udc_driver_2440
= {
2007 .name
= "s3c2440-usbgadget",
2008 .owner
= THIS_MODULE
,
2010 .probe
= s3c2410_udc_probe
,
2011 .remove
= s3c2410_udc_remove
,
2012 .suspend
= s3c2410_udc_suspend
,
2013 .resume
= s3c2410_udc_resume
,
2016 static int __init
udc_init(void)
2020 dprintk(DEBUG_NORMAL
, "%s: version %s\n", gadget_name
, DRIVER_VERSION
);
2022 s3c2410_udc_debugfs_root
= debugfs_create_dir(gadget_name
, NULL
);
2023 if (IS_ERR(s3c2410_udc_debugfs_root
)) {
2024 printk(KERN_ERR
"%s: debugfs dir creation failed %ld\n",
2025 gadget_name
, PTR_ERR(s3c2410_udc_debugfs_root
));
2026 s3c2410_udc_debugfs_root
= NULL
;
2029 retval
= platform_driver_register(&udc_driver_2410
);
2033 retval
= platform_driver_register(&udc_driver_2440
);
2040 debugfs_remove(s3c2410_udc_debugfs_root
);
2044 static void __exit
udc_exit(void)
2046 platform_driver_unregister(&udc_driver_2410
);
2047 platform_driver_unregister(&udc_driver_2440
);
2048 debugfs_remove(s3c2410_udc_debugfs_root
);
2051 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2053 module_init(udc_init
);
2054 module_exit(udc_exit
);
2056 MODULE_AUTHOR(DRIVER_AUTHOR
);
2057 MODULE_DESCRIPTION(DRIVER_DESC
);
2058 MODULE_VERSION(DRIVER_VERSION
);
2059 MODULE_LICENSE("GPL");
2060 MODULE_ALIAS("platform:s3c2410-usbgadget");
2061 MODULE_ALIAS("platform:s3c2440-usbgadget");