perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / usb / gadget / udc / s3c2410_udc.c
blob8bf5ad7a59add0ea1903b61bc33a8f2327b01f0e
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * linux/drivers/usb/gadget/s3c2410_udc.c
5 * Samsung S3C24xx series on-chip full speed USB device controllers
7 * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
8 * Additional cleanups by Ben Dooks <ben-linux@fluff.org>
9 */
11 #define pr_fmt(fmt) "s3c2410_udc: " fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/ioport.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/timer.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 #include <linux/gpio.h>
27 #include <linux/prefetch.h>
28 #include <linux/io.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
33 #include <linux/usb.h>
34 #include <linux/usb/gadget.h>
36 #include <asm/byteorder.h>
37 #include <asm/irq.h>
38 #include <asm/unaligned.h>
39 #include <mach/irqs.h>
41 #include <mach/hardware.h>
43 #include <plat/regs-udc.h>
44 #include <linux/platform_data/usb-s3c2410_udc.h>
47 #include "s3c2410_udc.h"
49 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
50 #define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
51 "Arnaud Patard <arnaud.patard@rtp-net.org>"
53 static const char gadget_name[] = "s3c2410_udc";
54 static const char driver_desc[] = DRIVER_DESC;
56 static struct s3c2410_udc *the_controller;
57 static struct clk *udc_clock;
58 static struct clk *usb_bus_clock;
59 static void __iomem *base_addr;
60 static u64 rsrc_start;
61 static u64 rsrc_len;
62 static struct dentry *s3c2410_udc_debugfs_root;
64 static inline u32 udc_read(u32 reg)
66 return readb(base_addr + reg);
69 static inline void udc_write(u32 value, u32 reg)
71 writeb(value, base_addr + reg);
74 static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
76 writeb(value, base + reg);
79 static struct s3c2410_udc_mach_info *udc_info;
81 /*************************** DEBUG FUNCTION ***************************/
82 #define DEBUG_NORMAL 1
83 #define DEBUG_VERBOSE 2
85 #ifdef CONFIG_USB_S3C2410_DEBUG
86 #define USB_S3C2410_DEBUG_LEVEL 0
88 static uint32_t s3c2410_ticks = 0;
90 __printf(2, 3)
91 static void dprintk(int level, const char *fmt, ...)
93 static long prevticks;
94 static int invocation;
95 struct va_format vaf;
96 va_list args;
98 if (level > USB_S3C2410_DEBUG_LEVEL)
99 return;
101 va_start(args, fmt);
103 vaf.fmt = fmt;
104 vaf.va = &args;
106 if (s3c2410_ticks != prevticks) {
107 prevticks = s3c2410_ticks;
108 invocation = 0;
111 pr_debug("%1lu.%02d USB: %pV", prevticks, invocation++, &vaf);
113 va_end(args);
115 #else
116 __printf(2, 3)
117 static void dprintk(int level, const char *fmt, ...)
120 #endif
122 static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
124 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
125 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
126 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
127 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
129 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
130 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
131 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
132 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
133 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
134 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
135 udc_write(0, S3C2410_UDC_INDEX_REG);
136 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
137 udc_write(1, S3C2410_UDC_INDEX_REG);
138 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
139 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
140 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
141 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
142 udc_write(2, S3C2410_UDC_INDEX_REG);
143 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
144 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
145 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
146 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
148 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
149 "PWR_REG : 0x%04X\n"
150 "EP_INT_REG : 0x%04X\n"
151 "USB_INT_REG : 0x%04X\n"
152 "EP_INT_EN_REG : 0x%04X\n"
153 "USB_INT_EN_REG : 0x%04X\n"
154 "EP0_CSR : 0x%04X\n"
155 "EP1_I_CSR1 : 0x%04X\n"
156 "EP1_I_CSR2 : 0x%04X\n"
157 "EP1_O_CSR1 : 0x%04X\n"
158 "EP1_O_CSR2 : 0x%04X\n"
159 "EP2_I_CSR1 : 0x%04X\n"
160 "EP2_I_CSR2 : 0x%04X\n"
161 "EP2_O_CSR1 : 0x%04X\n"
162 "EP2_O_CSR2 : 0x%04X\n",
163 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
164 ep_int_en_reg, usb_int_en_reg, ep0_csr,
165 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
166 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
169 return 0;
172 static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
173 struct file *file)
175 return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
178 static const struct file_operations s3c2410_udc_debugfs_fops = {
179 .open = s3c2410_udc_debugfs_fops_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183 .owner = THIS_MODULE,
186 /* io macros */
188 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
190 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
191 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
192 S3C2410_UDC_EP0_CSR_REG);
195 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
197 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
198 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
201 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
203 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
204 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
207 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
209 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
210 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
213 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
215 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
216 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
219 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
221 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
222 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
225 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
227 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
229 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
230 | S3C2410_UDC_EP0_CSR_DE),
231 S3C2410_UDC_EP0_CSR_REG);
234 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
236 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
237 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
238 | S3C2410_UDC_EP0_CSR_DE),
239 S3C2410_UDC_EP0_CSR_REG);
242 /*------------------------- I/O ----------------------------------*/
245 * s3c2410_udc_done
247 static void s3c2410_udc_done(struct s3c2410_ep *ep,
248 struct s3c2410_request *req, int status)
250 unsigned halted = ep->halted;
252 list_del_init(&req->queue);
254 if (likely(req->req.status == -EINPROGRESS))
255 req->req.status = status;
256 else
257 status = req->req.status;
259 ep->halted = 1;
260 usb_gadget_giveback_request(&ep->ep, &req->req);
261 ep->halted = halted;
264 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
265 struct s3c2410_ep *ep, int status)
267 /* Sanity check */
268 if (&ep->queue == NULL)
269 return;
271 while (!list_empty(&ep->queue)) {
272 struct s3c2410_request *req;
273 req = list_entry(ep->queue.next, struct s3c2410_request,
274 queue);
275 s3c2410_udc_done(ep, req, status);
279 static inline int s3c2410_udc_fifo_count_out(void)
281 int tmp;
283 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
284 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
285 return tmp;
289 * s3c2410_udc_write_packet
291 static inline int s3c2410_udc_write_packet(int fifo,
292 struct s3c2410_request *req,
293 unsigned max)
295 unsigned len = min(req->req.length - req->req.actual, max);
296 u8 *buf = req->req.buf + req->req.actual;
298 prefetch(buf);
300 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
301 req->req.actual, req->req.length, len, req->req.actual + len);
303 req->req.actual += len;
305 udelay(5);
306 writesb(base_addr + fifo, buf, len);
307 return len;
311 * s3c2410_udc_write_fifo
313 * return: 0 = still running, 1 = completed, negative = errno
315 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
316 struct s3c2410_request *req)
318 unsigned count;
319 int is_last;
320 u32 idx;
321 int fifo_reg;
322 u32 ep_csr;
324 idx = ep->bEndpointAddress & 0x7F;
325 switch (idx) {
326 default:
327 idx = 0;
328 case 0:
329 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
330 break;
331 case 1:
332 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
333 break;
334 case 2:
335 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
336 break;
337 case 3:
338 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
339 break;
340 case 4:
341 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
342 break;
345 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
347 /* last packet is often short (sometimes a zlp) */
348 if (count != ep->ep.maxpacket)
349 is_last = 1;
350 else if (req->req.length != req->req.actual || req->req.zero)
351 is_last = 0;
352 else
353 is_last = 2;
355 /* Only ep0 debug messages are interesting */
356 if (idx == 0)
357 dprintk(DEBUG_NORMAL,
358 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
359 idx, count, req->req.actual, req->req.length,
360 is_last, req->req.zero);
362 if (is_last) {
363 /* The order is important. It prevents sending 2 packets
364 * at the same time */
366 if (idx == 0) {
367 /* Reset signal => no need to say 'data sent' */
368 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
369 & S3C2410_UDC_USBINT_RESET))
370 s3c2410_udc_set_ep0_de_in(base_addr);
371 ep->dev->ep0state = EP0_IDLE;
372 } else {
373 udc_write(idx, S3C2410_UDC_INDEX_REG);
374 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
375 udc_write(idx, S3C2410_UDC_INDEX_REG);
376 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
377 S3C2410_UDC_IN_CSR1_REG);
380 s3c2410_udc_done(ep, req, 0);
381 is_last = 1;
382 } else {
383 if (idx == 0) {
384 /* Reset signal => no need to say 'data sent' */
385 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
386 & S3C2410_UDC_USBINT_RESET))
387 s3c2410_udc_set_ep0_ipr(base_addr);
388 } else {
389 udc_write(idx, S3C2410_UDC_INDEX_REG);
390 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
391 udc_write(idx, S3C2410_UDC_INDEX_REG);
392 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
393 S3C2410_UDC_IN_CSR1_REG);
397 return is_last;
400 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
401 struct s3c2410_request *req, unsigned avail)
403 unsigned len;
405 len = min(req->req.length - req->req.actual, avail);
406 req->req.actual += len;
408 readsb(fifo + base_addr, buf, len);
409 return len;
413 * return: 0 = still running, 1 = queue empty, negative = errno
415 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
416 struct s3c2410_request *req)
418 u8 *buf;
419 u32 ep_csr;
420 unsigned bufferspace;
421 int is_last = 1;
422 unsigned avail;
423 int fifo_count = 0;
424 u32 idx;
425 int fifo_reg;
427 idx = ep->bEndpointAddress & 0x7F;
429 switch (idx) {
430 default:
431 idx = 0;
432 case 0:
433 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
434 break;
435 case 1:
436 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
437 break;
438 case 2:
439 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
440 break;
441 case 3:
442 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
443 break;
444 case 4:
445 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
446 break;
449 if (!req->req.length)
450 return 1;
452 buf = req->req.buf + req->req.actual;
453 bufferspace = req->req.length - req->req.actual;
454 if (!bufferspace) {
455 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
456 return -1;
459 udc_write(idx, S3C2410_UDC_INDEX_REG);
461 fifo_count = s3c2410_udc_fifo_count_out();
462 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
464 if (fifo_count > ep->ep.maxpacket)
465 avail = ep->ep.maxpacket;
466 else
467 avail = fifo_count;
469 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
471 /* checking this with ep0 is not accurate as we already
472 * read a control request
474 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
475 is_last = 1;
476 /* overflowed this request? flush extra data */
477 if (fifo_count != avail)
478 req->req.status = -EOVERFLOW;
479 } else {
480 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
483 udc_write(idx, S3C2410_UDC_INDEX_REG);
484 fifo_count = s3c2410_udc_fifo_count_out();
486 /* Only ep0 debug messages are interesting */
487 if (idx == 0)
488 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
489 __func__, fifo_count, is_last);
491 if (is_last) {
492 if (idx == 0) {
493 s3c2410_udc_set_ep0_de_out(base_addr);
494 ep->dev->ep0state = EP0_IDLE;
495 } else {
496 udc_write(idx, S3C2410_UDC_INDEX_REG);
497 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
498 udc_write(idx, S3C2410_UDC_INDEX_REG);
499 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
500 S3C2410_UDC_OUT_CSR1_REG);
503 s3c2410_udc_done(ep, req, 0);
504 } else {
505 if (idx == 0) {
506 s3c2410_udc_clear_ep0_opr(base_addr);
507 } else {
508 udc_write(idx, S3C2410_UDC_INDEX_REG);
509 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
510 udc_write(idx, S3C2410_UDC_INDEX_REG);
511 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
512 S3C2410_UDC_OUT_CSR1_REG);
516 return is_last;
519 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
521 unsigned char *outbuf = (unsigned char *)crq;
522 int bytes_read = 0;
524 udc_write(0, S3C2410_UDC_INDEX_REG);
526 bytes_read = s3c2410_udc_fifo_count_out();
528 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
530 if (bytes_read > sizeof(struct usb_ctrlrequest))
531 bytes_read = sizeof(struct usb_ctrlrequest);
533 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
535 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
536 bytes_read, crq->bRequest, crq->bRequestType,
537 crq->wValue, crq->wIndex, crq->wLength);
539 return bytes_read;
542 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
543 struct usb_ctrlrequest *crq)
545 u16 status = 0;
546 u8 ep_num = crq->wIndex & 0x7F;
547 u8 is_in = crq->wIndex & USB_DIR_IN;
549 switch (crq->bRequestType & USB_RECIP_MASK) {
550 case USB_RECIP_INTERFACE:
551 break;
553 case USB_RECIP_DEVICE:
554 status = dev->devstatus;
555 break;
557 case USB_RECIP_ENDPOINT:
558 if (ep_num > 4 || crq->wLength > 2)
559 return 1;
561 if (ep_num == 0) {
562 udc_write(0, S3C2410_UDC_INDEX_REG);
563 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
564 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
565 } else {
566 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
567 if (is_in) {
568 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
569 status = status & S3C2410_UDC_ICSR1_SENDSTL;
570 } else {
571 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
572 status = status & S3C2410_UDC_OCSR1_SENDSTL;
576 status = status ? 1 : 0;
577 break;
579 default:
580 return 1;
583 /* Seems to be needed to get it working. ouch :( */
584 udelay(5);
585 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
586 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
587 s3c2410_udc_set_ep0_de_in(base_addr);
589 return 0;
591 /*------------------------- usb state machine -------------------------------*/
592 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
594 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
595 struct s3c2410_ep *ep,
596 struct usb_ctrlrequest *crq,
597 u32 ep0csr)
599 int len, ret, tmp;
601 /* start control request? */
602 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
603 return;
605 s3c2410_udc_nuke(dev, ep, -EPROTO);
607 len = s3c2410_udc_read_fifo_crq(crq);
608 if (len != sizeof(*crq)) {
609 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
610 " wanted %d bytes got %d. Stalling out...\n",
611 sizeof(*crq), len);
612 s3c2410_udc_set_ep0_ss(base_addr);
613 return;
616 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
617 crq->bRequest, crq->bRequestType, crq->wLength);
619 /* cope with automagic for some standard requests. */
620 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
621 == USB_TYPE_STANDARD;
622 dev->req_config = 0;
623 dev->req_pending = 1;
625 switch (crq->bRequest) {
626 case USB_REQ_SET_CONFIGURATION:
627 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
629 if (crq->bRequestType == USB_RECIP_DEVICE) {
630 dev->req_config = 1;
631 s3c2410_udc_set_ep0_de_out(base_addr);
633 break;
635 case USB_REQ_SET_INTERFACE:
636 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
638 if (crq->bRequestType == USB_RECIP_INTERFACE) {
639 dev->req_config = 1;
640 s3c2410_udc_set_ep0_de_out(base_addr);
642 break;
644 case USB_REQ_SET_ADDRESS:
645 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
647 if (crq->bRequestType == USB_RECIP_DEVICE) {
648 tmp = crq->wValue & 0x7F;
649 dev->address = tmp;
650 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
651 S3C2410_UDC_FUNC_ADDR_REG);
652 s3c2410_udc_set_ep0_de_out(base_addr);
653 return;
655 break;
657 case USB_REQ_GET_STATUS:
658 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
659 s3c2410_udc_clear_ep0_opr(base_addr);
661 if (dev->req_std) {
662 if (!s3c2410_udc_get_status(dev, crq))
663 return;
665 break;
667 case USB_REQ_CLEAR_FEATURE:
668 s3c2410_udc_clear_ep0_opr(base_addr);
670 if (crq->bRequestType != USB_RECIP_ENDPOINT)
671 break;
673 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
674 break;
676 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
677 s3c2410_udc_set_ep0_de_out(base_addr);
678 return;
680 case USB_REQ_SET_FEATURE:
681 s3c2410_udc_clear_ep0_opr(base_addr);
683 if (crq->bRequestType != USB_RECIP_ENDPOINT)
684 break;
686 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
687 break;
689 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
690 s3c2410_udc_set_ep0_de_out(base_addr);
691 return;
693 default:
694 s3c2410_udc_clear_ep0_opr(base_addr);
695 break;
698 if (crq->bRequestType & USB_DIR_IN)
699 dev->ep0state = EP0_IN_DATA_PHASE;
700 else
701 dev->ep0state = EP0_OUT_DATA_PHASE;
703 if (!dev->driver)
704 return;
706 /* deliver the request to the gadget driver */
707 ret = dev->driver->setup(&dev->gadget, crq);
708 if (ret < 0) {
709 if (dev->req_config) {
710 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
711 crq->bRequest, ret);
712 return;
715 if (ret == -EOPNOTSUPP)
716 dprintk(DEBUG_NORMAL, "Operation not supported\n");
717 else
718 dprintk(DEBUG_NORMAL,
719 "dev->driver->setup failed. (%d)\n", ret);
721 udelay(5);
722 s3c2410_udc_set_ep0_ss(base_addr);
723 s3c2410_udc_set_ep0_de_out(base_addr);
724 dev->ep0state = EP0_IDLE;
725 /* deferred i/o == no response yet */
726 } else if (dev->req_pending) {
727 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
728 dev->req_pending = 0;
731 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
734 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
736 u32 ep0csr;
737 struct s3c2410_ep *ep = &dev->ep[0];
738 struct s3c2410_request *req;
739 struct usb_ctrlrequest crq;
741 if (list_empty(&ep->queue))
742 req = NULL;
743 else
744 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
746 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
747 * S3C2410_UDC_EP0_CSR_REG when index is zero */
749 udc_write(0, S3C2410_UDC_INDEX_REG);
750 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
752 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
753 ep0csr, ep0states[dev->ep0state]);
755 /* clear stall status */
756 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
757 s3c2410_udc_nuke(dev, ep, -EPIPE);
758 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
759 s3c2410_udc_clear_ep0_sst(base_addr);
760 dev->ep0state = EP0_IDLE;
761 return;
764 /* clear setup end */
765 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
766 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
767 s3c2410_udc_nuke(dev, ep, 0);
768 s3c2410_udc_clear_ep0_se(base_addr);
769 dev->ep0state = EP0_IDLE;
772 switch (dev->ep0state) {
773 case EP0_IDLE:
774 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
775 break;
777 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
778 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
779 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
780 s3c2410_udc_write_fifo(ep, req);
781 break;
783 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
784 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
785 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
786 s3c2410_udc_read_fifo(ep, req);
787 break;
789 case EP0_END_XFER:
790 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
791 dev->ep0state = EP0_IDLE;
792 break;
794 case EP0_STALL:
795 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
796 dev->ep0state = EP0_IDLE;
797 break;
802 * handle_ep - Manage I/O endpoints
805 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
807 struct s3c2410_request *req;
808 int is_in = ep->bEndpointAddress & USB_DIR_IN;
809 u32 ep_csr1;
810 u32 idx;
812 if (likely(!list_empty(&ep->queue)))
813 req = list_entry(ep->queue.next,
814 struct s3c2410_request, queue);
815 else
816 req = NULL;
818 idx = ep->bEndpointAddress & 0x7F;
820 if (is_in) {
821 udc_write(idx, S3C2410_UDC_INDEX_REG);
822 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
823 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
824 idx, ep_csr1, req ? 1 : 0);
826 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
827 dprintk(DEBUG_VERBOSE, "st\n");
828 udc_write(idx, S3C2410_UDC_INDEX_REG);
829 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
830 S3C2410_UDC_IN_CSR1_REG);
831 return;
834 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
835 s3c2410_udc_write_fifo(ep, req);
836 } else {
837 udc_write(idx, S3C2410_UDC_INDEX_REG);
838 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
839 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
841 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
842 udc_write(idx, S3C2410_UDC_INDEX_REG);
843 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
844 S3C2410_UDC_OUT_CSR1_REG);
845 return;
848 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
849 s3c2410_udc_read_fifo(ep, req);
853 #include <mach/regs-irq.h>
856 * s3c2410_udc_irq - interrupt handler
858 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
860 struct s3c2410_udc *dev = _dev;
861 int usb_status;
862 int usbd_status;
863 int pwr_reg;
864 int ep0csr;
865 int i;
866 u32 idx, idx2;
867 unsigned long flags;
869 spin_lock_irqsave(&dev->lock, flags);
871 /* Driver connected ? */
872 if (!dev->driver) {
873 /* Clear interrupts */
874 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
875 S3C2410_UDC_USB_INT_REG);
876 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
877 S3C2410_UDC_EP_INT_REG);
880 /* Save index */
881 idx = udc_read(S3C2410_UDC_INDEX_REG);
883 /* Read status registers */
884 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
885 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
886 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
888 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
889 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
891 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
892 usb_status, usbd_status, pwr_reg, ep0csr);
895 * Now, handle interrupts. There's two types :
896 * - Reset, Resume, Suspend coming -> usb_int_reg
897 * - EP -> ep_int_reg
900 /* RESET */
901 if (usb_status & S3C2410_UDC_USBINT_RESET) {
902 /* two kind of reset :
903 * - reset start -> pwr reg = 8
904 * - reset end -> pwr reg = 0
906 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
907 ep0csr, pwr_reg);
909 dev->gadget.speed = USB_SPEED_UNKNOWN;
910 udc_write(0x00, S3C2410_UDC_INDEX_REG);
911 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
912 S3C2410_UDC_MAXP_REG);
913 dev->address = 0;
915 dev->ep0state = EP0_IDLE;
916 dev->gadget.speed = USB_SPEED_FULL;
918 /* clear interrupt */
919 udc_write(S3C2410_UDC_USBINT_RESET,
920 S3C2410_UDC_USB_INT_REG);
922 udc_write(idx, S3C2410_UDC_INDEX_REG);
923 spin_unlock_irqrestore(&dev->lock, flags);
924 return IRQ_HANDLED;
927 /* RESUME */
928 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
929 dprintk(DEBUG_NORMAL, "USB resume\n");
931 /* clear interrupt */
932 udc_write(S3C2410_UDC_USBINT_RESUME,
933 S3C2410_UDC_USB_INT_REG);
935 if (dev->gadget.speed != USB_SPEED_UNKNOWN
936 && dev->driver
937 && dev->driver->resume)
938 dev->driver->resume(&dev->gadget);
941 /* SUSPEND */
942 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
943 dprintk(DEBUG_NORMAL, "USB suspend\n");
945 /* clear interrupt */
946 udc_write(S3C2410_UDC_USBINT_SUSPEND,
947 S3C2410_UDC_USB_INT_REG);
949 if (dev->gadget.speed != USB_SPEED_UNKNOWN
950 && dev->driver
951 && dev->driver->suspend)
952 dev->driver->suspend(&dev->gadget);
954 dev->ep0state = EP0_IDLE;
957 /* EP */
958 /* control traffic */
959 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
960 * generate an interrupt
962 if (usbd_status & S3C2410_UDC_INT_EP0) {
963 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
964 /* Clear the interrupt bit by setting it to 1 */
965 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
966 s3c2410_udc_handle_ep0(dev);
969 /* endpoint data transfers */
970 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
971 u32 tmp = 1 << i;
972 if (usbd_status & tmp) {
973 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
975 /* Clear the interrupt bit by setting it to 1 */
976 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
977 s3c2410_udc_handle_ep(&dev->ep[i]);
981 /* what else causes this interrupt? a receive! who is it? */
982 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
983 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
984 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
985 udc_write(i, S3C2410_UDC_INDEX_REG);
987 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
988 s3c2410_udc_handle_ep(&dev->ep[i]);
990 /* restore index */
991 udc_write(idx2, S3C2410_UDC_INDEX_REG);
995 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
997 /* Restore old index */
998 udc_write(idx, S3C2410_UDC_INDEX_REG);
1000 spin_unlock_irqrestore(&dev->lock, flags);
1002 return IRQ_HANDLED;
1004 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1006 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1008 return container_of(ep, struct s3c2410_ep, ep);
1011 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1013 return container_of(gadget, struct s3c2410_udc, gadget);
1016 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1018 return container_of(req, struct s3c2410_request, req);
1022 * s3c2410_udc_ep_enable
1024 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1025 const struct usb_endpoint_descriptor *desc)
1027 struct s3c2410_udc *dev;
1028 struct s3c2410_ep *ep;
1029 u32 max, tmp;
1030 unsigned long flags;
1031 u32 csr1, csr2;
1032 u32 int_en_reg;
1034 ep = to_s3c2410_ep(_ep);
1036 if (!_ep || !desc
1037 || _ep->name == ep0name
1038 || desc->bDescriptorType != USB_DT_ENDPOINT)
1039 return -EINVAL;
1041 dev = ep->dev;
1042 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1043 return -ESHUTDOWN;
1045 max = usb_endpoint_maxp(desc);
1047 local_irq_save(flags);
1048 _ep->maxpacket = max;
1049 ep->ep.desc = desc;
1050 ep->halted = 0;
1051 ep->bEndpointAddress = desc->bEndpointAddress;
1053 /* set max packet */
1054 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1055 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1057 /* set type, direction, address; reset fifo counters */
1058 if (desc->bEndpointAddress & USB_DIR_IN) {
1059 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1060 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1062 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1063 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1064 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1065 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1066 } else {
1067 /* don't flush in fifo or it will cause endpoint interrupt */
1068 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1069 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1071 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1072 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1073 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1074 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1076 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1077 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1079 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1080 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1081 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1082 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1085 /* enable irqs */
1086 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1087 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1089 /* print some debug message */
1090 tmp = desc->bEndpointAddress;
1091 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1092 _ep->name, ep->num, tmp,
1093 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1095 local_irq_restore(flags);
1096 s3c2410_udc_set_halt(_ep, 0);
1098 return 0;
1102 * s3c2410_udc_ep_disable
1104 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1106 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1107 unsigned long flags;
1108 u32 int_en_reg;
1110 if (!_ep || !ep->ep.desc) {
1111 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1112 _ep ? ep->ep.name : NULL);
1113 return -EINVAL;
1116 local_irq_save(flags);
1118 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1120 ep->ep.desc = NULL;
1121 ep->halted = 1;
1123 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
1125 /* disable irqs */
1126 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1127 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1129 local_irq_restore(flags);
1131 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1133 return 0;
1137 * s3c2410_udc_alloc_request
1139 static struct usb_request *
1140 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1142 struct s3c2410_request *req;
1144 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
1146 if (!_ep)
1147 return NULL;
1149 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
1150 if (!req)
1151 return NULL;
1153 INIT_LIST_HEAD(&req->queue);
1154 return &req->req;
1158 * s3c2410_udc_free_request
1160 static void
1161 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1163 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1164 struct s3c2410_request *req = to_s3c2410_req(_req);
1166 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1168 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1169 return;
1171 WARN_ON(!list_empty(&req->queue));
1172 kfree(req);
1176 * s3c2410_udc_queue
1178 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1179 gfp_t gfp_flags)
1181 struct s3c2410_request *req = to_s3c2410_req(_req);
1182 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1183 struct s3c2410_udc *dev;
1184 u32 ep_csr = 0;
1185 int fifo_count = 0;
1186 unsigned long flags;
1188 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1189 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1190 return -EINVAL;
1193 dev = ep->dev;
1194 if (unlikely(!dev->driver
1195 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1196 return -ESHUTDOWN;
1199 local_irq_save(flags);
1201 if (unlikely(!_req || !_req->complete
1202 || !_req->buf || !list_empty(&req->queue))) {
1203 if (!_req)
1204 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1205 else {
1206 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1207 __func__, !_req->complete, !_req->buf,
1208 !list_empty(&req->queue));
1211 local_irq_restore(flags);
1212 return -EINVAL;
1215 _req->status = -EINPROGRESS;
1216 _req->actual = 0;
1218 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1219 __func__, ep->bEndpointAddress, _req->length);
1221 if (ep->bEndpointAddress) {
1222 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1224 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1225 ? S3C2410_UDC_IN_CSR1_REG
1226 : S3C2410_UDC_OUT_CSR1_REG);
1227 fifo_count = s3c2410_udc_fifo_count_out();
1228 } else {
1229 udc_write(0, S3C2410_UDC_INDEX_REG);
1230 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1231 fifo_count = s3c2410_udc_fifo_count_out();
1234 /* kickstart this i/o queue? */
1235 if (list_empty(&ep->queue) && !ep->halted) {
1236 if (ep->bEndpointAddress == 0 /* ep0 */) {
1237 switch (dev->ep0state) {
1238 case EP0_IN_DATA_PHASE:
1239 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1240 && s3c2410_udc_write_fifo(ep,
1241 req)) {
1242 dev->ep0state = EP0_IDLE;
1243 req = NULL;
1245 break;
1247 case EP0_OUT_DATA_PHASE:
1248 if ((!_req->length)
1249 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1250 && s3c2410_udc_read_fifo(ep,
1251 req))) {
1252 dev->ep0state = EP0_IDLE;
1253 req = NULL;
1255 break;
1257 default:
1258 local_irq_restore(flags);
1259 return -EL2HLT;
1261 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1262 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1263 && s3c2410_udc_write_fifo(ep, req)) {
1264 req = NULL;
1265 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1266 && fifo_count
1267 && s3c2410_udc_read_fifo(ep, req)) {
1268 req = NULL;
1272 /* pio or dma irq handler advances the queue. */
1273 if (likely(req))
1274 list_add_tail(&req->queue, &ep->queue);
1276 local_irq_restore(flags);
1278 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1279 return 0;
1283 * s3c2410_udc_dequeue
1285 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1287 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1288 struct s3c2410_udc *udc;
1289 int retval = -EINVAL;
1290 unsigned long flags;
1291 struct s3c2410_request *req = NULL;
1293 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1295 if (!the_controller->driver)
1296 return -ESHUTDOWN;
1298 if (!_ep || !_req)
1299 return retval;
1301 udc = to_s3c2410_udc(ep->gadget);
1303 local_irq_save(flags);
1305 list_for_each_entry(req, &ep->queue, queue) {
1306 if (&req->req == _req) {
1307 list_del_init(&req->queue);
1308 _req->status = -ECONNRESET;
1309 retval = 0;
1310 break;
1314 if (retval == 0) {
1315 dprintk(DEBUG_VERBOSE,
1316 "dequeued req %p from %s, len %d buf %p\n",
1317 req, _ep->name, _req->length, _req->buf);
1319 s3c2410_udc_done(ep, req, -ECONNRESET);
1322 local_irq_restore(flags);
1323 return retval;
1327 * s3c2410_udc_set_halt
1329 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1331 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1332 u32 ep_csr = 0;
1333 unsigned long flags;
1334 u32 idx;
1336 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1337 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1338 return -EINVAL;
1341 local_irq_save(flags);
1343 idx = ep->bEndpointAddress & 0x7F;
1345 if (idx == 0) {
1346 s3c2410_udc_set_ep0_ss(base_addr);
1347 s3c2410_udc_set_ep0_de_out(base_addr);
1348 } else {
1349 udc_write(idx, S3C2410_UDC_INDEX_REG);
1350 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1351 ? S3C2410_UDC_IN_CSR1_REG
1352 : S3C2410_UDC_OUT_CSR1_REG);
1354 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1355 if (value)
1356 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1357 S3C2410_UDC_IN_CSR1_REG);
1358 else {
1359 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1360 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1361 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1362 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1364 } else {
1365 if (value)
1366 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1367 S3C2410_UDC_OUT_CSR1_REG);
1368 else {
1369 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1370 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1371 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1372 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1377 ep->halted = value ? 1 : 0;
1378 local_irq_restore(flags);
1380 return 0;
1383 static const struct usb_ep_ops s3c2410_ep_ops = {
1384 .enable = s3c2410_udc_ep_enable,
1385 .disable = s3c2410_udc_ep_disable,
1387 .alloc_request = s3c2410_udc_alloc_request,
1388 .free_request = s3c2410_udc_free_request,
1390 .queue = s3c2410_udc_queue,
1391 .dequeue = s3c2410_udc_dequeue,
1393 .set_halt = s3c2410_udc_set_halt,
1396 /*------------------------- usb_gadget_ops ----------------------------------*/
1399 * s3c2410_udc_get_frame
1401 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1403 int tmp;
1405 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1407 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1408 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1409 return tmp;
1413 * s3c2410_udc_wakeup
1415 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1417 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1418 return 0;
1422 * s3c2410_udc_set_selfpowered
1424 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1426 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1428 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1430 gadget->is_selfpowered = (value != 0);
1431 if (value)
1432 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1433 else
1434 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1436 return 0;
1439 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1440 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1442 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1444 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1446 if (udc_info && (udc_info->udc_command ||
1447 gpio_is_valid(udc_info->pullup_pin))) {
1449 if (is_on)
1450 s3c2410_udc_enable(udc);
1451 else {
1452 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1453 if (udc->driver && udc->driver->disconnect)
1454 udc->driver->disconnect(&udc->gadget);
1457 s3c2410_udc_disable(udc);
1459 } else {
1460 return -EOPNOTSUPP;
1463 return 0;
1466 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1468 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1470 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1472 udc->vbus = (is_active != 0);
1473 s3c2410_udc_set_pullup(udc, is_active);
1474 return 0;
1477 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1479 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1481 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1483 s3c2410_udc_set_pullup(udc, is_on);
1484 return 0;
1487 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1489 struct s3c2410_udc *dev = _dev;
1490 unsigned int value;
1492 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1494 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1495 if (udc_info->vbus_pin_inverted)
1496 value = !value;
1498 if (value != dev->vbus)
1499 s3c2410_udc_vbus_session(&dev->gadget, value);
1501 return IRQ_HANDLED;
1504 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1506 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1508 if (udc_info && udc_info->vbus_draw) {
1509 udc_info->vbus_draw(ma);
1510 return 0;
1513 return -ENOTSUPP;
1516 static int s3c2410_udc_start(struct usb_gadget *g,
1517 struct usb_gadget_driver *driver);
1518 static int s3c2410_udc_stop(struct usb_gadget *g);
1520 static const struct usb_gadget_ops s3c2410_ops = {
1521 .get_frame = s3c2410_udc_get_frame,
1522 .wakeup = s3c2410_udc_wakeup,
1523 .set_selfpowered = s3c2410_udc_set_selfpowered,
1524 .pullup = s3c2410_udc_pullup,
1525 .vbus_session = s3c2410_udc_vbus_session,
1526 .vbus_draw = s3c2410_vbus_draw,
1527 .udc_start = s3c2410_udc_start,
1528 .udc_stop = s3c2410_udc_stop,
1531 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1533 if (!udc_info)
1534 return;
1536 if (udc_info->udc_command) {
1537 udc_info->udc_command(cmd);
1538 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1539 int value;
1541 switch (cmd) {
1542 case S3C2410_UDC_P_ENABLE:
1543 value = 1;
1544 break;
1545 case S3C2410_UDC_P_DISABLE:
1546 value = 0;
1547 break;
1548 default:
1549 return;
1551 value ^= udc_info->pullup_pin_inverted;
1553 gpio_set_value(udc_info->pullup_pin, value);
1557 /*------------------------- gadget driver handling---------------------------*/
1559 * s3c2410_udc_disable
1561 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1563 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1565 /* Disable all interrupts */
1566 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1567 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1569 /* Clear the interrupt registers */
1570 udc_write(S3C2410_UDC_USBINT_RESET
1571 | S3C2410_UDC_USBINT_RESUME
1572 | S3C2410_UDC_USBINT_SUSPEND,
1573 S3C2410_UDC_USB_INT_REG);
1575 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1577 /* Good bye, cruel world */
1578 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1580 /* Set speed to unknown */
1581 dev->gadget.speed = USB_SPEED_UNKNOWN;
1585 * s3c2410_udc_reinit
1587 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1589 u32 i;
1591 /* device/ep0 records init */
1592 INIT_LIST_HEAD(&dev->gadget.ep_list);
1593 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1594 dev->ep0state = EP0_IDLE;
1596 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1597 struct s3c2410_ep *ep = &dev->ep[i];
1599 if (i != 0)
1600 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1602 ep->dev = dev;
1603 ep->ep.desc = NULL;
1604 ep->halted = 0;
1605 INIT_LIST_HEAD(&ep->queue);
1606 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
1611 * s3c2410_udc_enable
1613 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1615 int i;
1617 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1619 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1620 dev->gadget.speed = USB_SPEED_FULL;
1622 /* Set MAXP for all endpoints */
1623 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1624 udc_write(i, S3C2410_UDC_INDEX_REG);
1625 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1626 S3C2410_UDC_MAXP_REG);
1629 /* Set default power state */
1630 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1632 /* Enable reset and suspend interrupt interrupts */
1633 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1634 S3C2410_UDC_USB_INT_EN_REG);
1636 /* Enable ep0 interrupt */
1637 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1639 /* time to say "hello, world" */
1640 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1643 static int s3c2410_udc_start(struct usb_gadget *g,
1644 struct usb_gadget_driver *driver)
1646 struct s3c2410_udc *udc = to_s3c2410(g);
1648 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1650 /* Hook the driver */
1651 udc->driver = driver;
1653 /* Enable udc */
1654 s3c2410_udc_enable(udc);
1656 return 0;
1659 static int s3c2410_udc_stop(struct usb_gadget *g)
1661 struct s3c2410_udc *udc = to_s3c2410(g);
1663 udc->driver = NULL;
1665 /* Disable udc */
1666 s3c2410_udc_disable(udc);
1668 return 0;
1671 /*---------------------------------------------------------------------------*/
1672 static struct s3c2410_udc memory = {
1673 .gadget = {
1674 .ops = &s3c2410_ops,
1675 .ep0 = &memory.ep[0].ep,
1676 .name = gadget_name,
1677 .dev = {
1678 .init_name = "gadget",
1682 /* control endpoint */
1683 .ep[0] = {
1684 .num = 0,
1685 .ep = {
1686 .name = ep0name,
1687 .ops = &s3c2410_ep_ops,
1688 .maxpacket = EP0_FIFO_SIZE,
1689 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
1690 USB_EP_CAPS_DIR_ALL),
1692 .dev = &memory,
1695 /* first group of endpoints */
1696 .ep[1] = {
1697 .num = 1,
1698 .ep = {
1699 .name = "ep1-bulk",
1700 .ops = &s3c2410_ep_ops,
1701 .maxpacket = EP_FIFO_SIZE,
1702 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1703 USB_EP_CAPS_DIR_ALL),
1705 .dev = &memory,
1706 .fifo_size = EP_FIFO_SIZE,
1707 .bEndpointAddress = 1,
1708 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1710 .ep[2] = {
1711 .num = 2,
1712 .ep = {
1713 .name = "ep2-bulk",
1714 .ops = &s3c2410_ep_ops,
1715 .maxpacket = EP_FIFO_SIZE,
1716 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1717 USB_EP_CAPS_DIR_ALL),
1719 .dev = &memory,
1720 .fifo_size = EP_FIFO_SIZE,
1721 .bEndpointAddress = 2,
1722 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1724 .ep[3] = {
1725 .num = 3,
1726 .ep = {
1727 .name = "ep3-bulk",
1728 .ops = &s3c2410_ep_ops,
1729 .maxpacket = EP_FIFO_SIZE,
1730 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1731 USB_EP_CAPS_DIR_ALL),
1733 .dev = &memory,
1734 .fifo_size = EP_FIFO_SIZE,
1735 .bEndpointAddress = 3,
1736 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1738 .ep[4] = {
1739 .num = 4,
1740 .ep = {
1741 .name = "ep4-bulk",
1742 .ops = &s3c2410_ep_ops,
1743 .maxpacket = EP_FIFO_SIZE,
1744 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1745 USB_EP_CAPS_DIR_ALL),
1747 .dev = &memory,
1748 .fifo_size = EP_FIFO_SIZE,
1749 .bEndpointAddress = 4,
1750 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1756 * probe - binds to the platform device
1758 static int s3c2410_udc_probe(struct platform_device *pdev)
1760 struct s3c2410_udc *udc = &memory;
1761 struct device *dev = &pdev->dev;
1762 int retval;
1763 int irq;
1765 dev_dbg(dev, "%s()\n", __func__);
1767 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1768 if (IS_ERR(usb_bus_clock)) {
1769 dev_err(dev, "failed to get usb bus clock source\n");
1770 return PTR_ERR(usb_bus_clock);
1773 clk_prepare_enable(usb_bus_clock);
1775 udc_clock = clk_get(NULL, "usb-device");
1776 if (IS_ERR(udc_clock)) {
1777 dev_err(dev, "failed to get udc clock source\n");
1778 return PTR_ERR(udc_clock);
1781 clk_prepare_enable(udc_clock);
1783 mdelay(10);
1785 dev_dbg(dev, "got and enabled clocks\n");
1787 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1788 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1789 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1790 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1791 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1792 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1795 spin_lock_init(&udc->lock);
1796 udc_info = dev_get_platdata(&pdev->dev);
1798 rsrc_start = S3C2410_PA_USBDEV;
1799 rsrc_len = S3C24XX_SZ_USBDEV;
1801 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1802 return -EBUSY;
1804 base_addr = ioremap(rsrc_start, rsrc_len);
1805 if (!base_addr) {
1806 retval = -ENOMEM;
1807 goto err_mem;
1810 the_controller = udc;
1811 platform_set_drvdata(pdev, udc);
1813 s3c2410_udc_disable(udc);
1814 s3c2410_udc_reinit(udc);
1816 /* irq setup after old hardware state is cleaned up */
1817 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1818 0, gadget_name, udc);
1820 if (retval != 0) {
1821 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1822 retval = -EBUSY;
1823 goto err_map;
1826 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1828 if (udc_info && udc_info->vbus_pin > 0) {
1829 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1830 if (retval < 0) {
1831 dev_err(dev, "cannot claim vbus pin\n");
1832 goto err_int;
1835 irq = gpio_to_irq(udc_info->vbus_pin);
1836 if (irq < 0) {
1837 dev_err(dev, "no irq for gpio vbus pin\n");
1838 retval = irq;
1839 goto err_gpio_claim;
1842 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1843 IRQF_TRIGGER_RISING
1844 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1845 gadget_name, udc);
1847 if (retval != 0) {
1848 dev_err(dev, "can't get vbus irq %d, err %d\n",
1849 irq, retval);
1850 retval = -EBUSY;
1851 goto err_gpio_claim;
1854 dev_dbg(dev, "got irq %i\n", irq);
1855 } else {
1856 udc->vbus = 1;
1859 if (udc_info && !udc_info->udc_command &&
1860 gpio_is_valid(udc_info->pullup_pin)) {
1862 retval = gpio_request_one(udc_info->pullup_pin,
1863 udc_info->vbus_pin_inverted ?
1864 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1865 "udc pullup");
1866 if (retval)
1867 goto err_vbus_irq;
1870 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1871 if (retval)
1872 goto err_add_udc;
1874 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1875 s3c2410_udc_debugfs_root, udc,
1876 &s3c2410_udc_debugfs_fops);
1878 dev_dbg(dev, "probe ok\n");
1880 return 0;
1882 err_add_udc:
1883 if (udc_info && !udc_info->udc_command &&
1884 gpio_is_valid(udc_info->pullup_pin))
1885 gpio_free(udc_info->pullup_pin);
1886 err_vbus_irq:
1887 if (udc_info && udc_info->vbus_pin > 0)
1888 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1889 err_gpio_claim:
1890 if (udc_info && udc_info->vbus_pin > 0)
1891 gpio_free(udc_info->vbus_pin);
1892 err_int:
1893 free_irq(IRQ_USBD, udc);
1894 err_map:
1895 iounmap(base_addr);
1896 err_mem:
1897 release_mem_region(rsrc_start, rsrc_len);
1899 return retval;
1903 * s3c2410_udc_remove
1905 static int s3c2410_udc_remove(struct platform_device *pdev)
1907 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1908 unsigned int irq;
1910 dev_dbg(&pdev->dev, "%s()\n", __func__);
1912 if (udc->driver)
1913 return -EBUSY;
1915 usb_del_gadget_udc(&udc->gadget);
1916 debugfs_remove(udc->regs_info);
1918 if (udc_info && !udc_info->udc_command &&
1919 gpio_is_valid(udc_info->pullup_pin))
1920 gpio_free(udc_info->pullup_pin);
1922 if (udc_info && udc_info->vbus_pin > 0) {
1923 irq = gpio_to_irq(udc_info->vbus_pin);
1924 free_irq(irq, udc);
1927 free_irq(IRQ_USBD, udc);
1929 iounmap(base_addr);
1930 release_mem_region(rsrc_start, rsrc_len);
1932 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
1933 clk_disable_unprepare(udc_clock);
1934 clk_put(udc_clock);
1935 udc_clock = NULL;
1938 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
1939 clk_disable_unprepare(usb_bus_clock);
1940 clk_put(usb_bus_clock);
1941 usb_bus_clock = NULL;
1944 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1945 return 0;
1948 #ifdef CONFIG_PM
1949 static int
1950 s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
1952 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1954 return 0;
1957 static int s3c2410_udc_resume(struct platform_device *pdev)
1959 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1961 return 0;
1963 #else
1964 #define s3c2410_udc_suspend NULL
1965 #define s3c2410_udc_resume NULL
1966 #endif
1968 static const struct platform_device_id s3c_udc_ids[] = {
1969 { "s3c2410-usbgadget", },
1970 { "s3c2440-usbgadget", },
1973 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
1975 static struct platform_driver udc_driver_24x0 = {
1976 .driver = {
1977 .name = "s3c24x0-usbgadget",
1979 .probe = s3c2410_udc_probe,
1980 .remove = s3c2410_udc_remove,
1981 .suspend = s3c2410_udc_suspend,
1982 .resume = s3c2410_udc_resume,
1983 .id_table = s3c_udc_ids,
1986 static int __init udc_init(void)
1988 int retval;
1990 dprintk(DEBUG_NORMAL, "%s\n", gadget_name);
1992 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
1994 retval = platform_driver_register(&udc_driver_24x0);
1995 if (retval)
1996 goto err;
1998 return 0;
2000 err:
2001 debugfs_remove(s3c2410_udc_debugfs_root);
2002 return retval;
2005 static void __exit udc_exit(void)
2007 platform_driver_unregister(&udc_driver_24x0);
2008 debugfs_remove_recursive(s3c2410_udc_debugfs_root);
2011 module_init(udc_init);
2012 module_exit(udc_exit);
2014 MODULE_AUTHOR(DRIVER_AUTHOR);
2015 MODULE_DESCRIPTION(DRIVER_DESC);
2016 MODULE_LICENSE("GPL");