55a571e15cd20365b0b872e3d16290fc4b7fb479
[firefly-linux-kernel-4.4.55.git] / drivers / usb / gadget / s3c2410_udc.c
1 /*
2  * linux/drivers/usb/gadget/s3c2410_udc.c
3  *
4  * Samsung S3C24xx series on-chip full speed USB device controllers
5  *
6  * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
7  *      Additional cleanups by Ben Dooks <ben-linux@fluff.org>
8  *
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.
13  */
14
15 #define pr_fmt(fmt) "s3c2410_udc: " fmt
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/delay.h>
20 #include <linux/ioport.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/timer.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/gpio.h>
31 #include <linux/prefetch.h>
32 #include <linux/io.h>
33
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
36
37 #include <linux/usb.h>
38 #include <linux/usb/gadget.h>
39
40 #include <asm/byteorder.h>
41 #include <asm/irq.h>
42 #include <asm/unaligned.h>
43 #include <mach/irqs.h>
44
45 #include <mach/hardware.h>
46
47 #include <plat/regs-udc.h>
48 #include <plat/udc.h>
49
50
51 #include "s3c2410_udc.h"
52
53 #define DRIVER_DESC     "S3C2410 USB Device Controller Gadget"
54 #define DRIVER_VERSION  "29 Apr 2007"
55 #define DRIVER_AUTHOR   "Herbert Pötzl <herbert@13thfloor.at>, " \
56                         "Arnaud Patard <arnaud.patard@rtp-net.org>"
57
58 static const char               gadget_name[] = "s3c2410_udc";
59 static const char               driver_desc[] = DRIVER_DESC;
60
61 static struct s3c2410_udc       *the_controller;
62 static struct clk               *udc_clock;
63 static struct clk               *usb_bus_clock;
64 static void __iomem             *base_addr;
65 static u64                      rsrc_start;
66 static u64                      rsrc_len;
67 static struct dentry            *s3c2410_udc_debugfs_root;
68
69 static inline u32 udc_read(u32 reg)
70 {
71         return readb(base_addr + reg);
72 }
73
74 static inline void udc_write(u32 value, u32 reg)
75 {
76         writeb(value, base_addr + reg);
77 }
78
79 static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
80 {
81         writeb(value, base + reg);
82 }
83
84 static struct s3c2410_udc_mach_info *udc_info;
85
86 /*************************** DEBUG FUNCTION ***************************/
87 #define DEBUG_NORMAL    1
88 #define DEBUG_VERBOSE   2
89
90 #ifdef CONFIG_USB_S3C2410_DEBUG
91 #define USB_S3C2410_DEBUG_LEVEL 0
92
93 static uint32_t s3c2410_ticks = 0;
94
95 static int dprintk(int level, const char *fmt, ...)
96 {
97         static char printk_buf[1024];
98         static long prevticks;
99         static int invocation;
100         va_list args;
101         int len;
102
103         if (level > USB_S3C2410_DEBUG_LEVEL)
104                 return 0;
105
106         if (s3c2410_ticks != prevticks) {
107                 prevticks = s3c2410_ticks;
108                 invocation = 0;
109         }
110
111         len = scnprintf(printk_buf,
112                         sizeof(printk_buf), "%1lu.%02d USB: ",
113                         prevticks, invocation++);
114
115         va_start(args, fmt);
116         len = vscnprintf(printk_buf+len,
117                         sizeof(printk_buf)-len, fmt, args);
118         va_end(args);
119
120         return pr_debug("%s", printk_buf);
121 }
122 #else
123 static int dprintk(int level, const char *fmt, ...)
124 {
125         return 0;
126 }
127 #endif
128 static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
129 {
130         u32 addr_reg,pwr_reg,ep_int_reg,usb_int_reg;
131         u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
132         u32 ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2;
133         u32 ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2;
134
135         addr_reg       = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
136         pwr_reg        = udc_read(S3C2410_UDC_PWR_REG);
137         ep_int_reg     = udc_read(S3C2410_UDC_EP_INT_REG);
138         usb_int_reg    = udc_read(S3C2410_UDC_USB_INT_REG);
139         ep_int_en_reg  = udc_read(S3C2410_UDC_EP_INT_EN_REG);
140         usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
141         udc_write(0, S3C2410_UDC_INDEX_REG);
142         ep0_csr        = udc_read(S3C2410_UDC_IN_CSR1_REG);
143         udc_write(1, S3C2410_UDC_INDEX_REG);
144         ep1_i_csr1     = udc_read(S3C2410_UDC_IN_CSR1_REG);
145         ep1_i_csr2     = udc_read(S3C2410_UDC_IN_CSR2_REG);
146         ep1_o_csr1     = udc_read(S3C2410_UDC_IN_CSR1_REG);
147         ep1_o_csr2     = udc_read(S3C2410_UDC_IN_CSR2_REG);
148         udc_write(2, S3C2410_UDC_INDEX_REG);
149         ep2_i_csr1     = udc_read(S3C2410_UDC_IN_CSR1_REG);
150         ep2_i_csr2     = udc_read(S3C2410_UDC_IN_CSR2_REG);
151         ep2_o_csr1     = udc_read(S3C2410_UDC_IN_CSR1_REG);
152         ep2_o_csr2     = udc_read(S3C2410_UDC_IN_CSR2_REG);
153
154         seq_printf(m, "FUNC_ADDR_REG  : 0x%04X\n"
155                  "PWR_REG        : 0x%04X\n"
156                  "EP_INT_REG     : 0x%04X\n"
157                  "USB_INT_REG    : 0x%04X\n"
158                  "EP_INT_EN_REG  : 0x%04X\n"
159                  "USB_INT_EN_REG : 0x%04X\n"
160                  "EP0_CSR        : 0x%04X\n"
161                  "EP1_I_CSR1     : 0x%04X\n"
162                  "EP1_I_CSR2     : 0x%04X\n"
163                  "EP1_O_CSR1     : 0x%04X\n"
164                  "EP1_O_CSR2     : 0x%04X\n"
165                  "EP2_I_CSR1     : 0x%04X\n"
166                  "EP2_I_CSR2     : 0x%04X\n"
167                  "EP2_O_CSR1     : 0x%04X\n"
168                  "EP2_O_CSR2     : 0x%04X\n",
169                         addr_reg,pwr_reg,ep_int_reg,usb_int_reg,
170                         ep_int_en_reg, usb_int_en_reg, ep0_csr,
171                         ep1_i_csr1,ep1_i_csr2,ep1_o_csr1,ep1_o_csr2,
172                         ep2_i_csr1,ep2_i_csr2,ep2_o_csr1,ep2_o_csr2
173                 );
174
175         return 0;
176 }
177
178 static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
179                                          struct file *file)
180 {
181         return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
182 }
183
184 static const struct file_operations s3c2410_udc_debugfs_fops = {
185         .open           = s3c2410_udc_debugfs_fops_open,
186         .read           = seq_read,
187         .llseek         = seq_lseek,
188         .release        = single_release,
189         .owner          = THIS_MODULE,
190 };
191
192 /* io macros */
193
194 static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
195 {
196         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
197         udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
198                         S3C2410_UDC_EP0_CSR_REG);
199 }
200
201 static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
202 {
203         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
204         writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
205 }
206
207 static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
208 {
209         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
210         udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
211 }
212
213 static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
214 {
215         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
216         udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
217 }
218
219 static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
220 {
221         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
222         udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
223 }
224
225 inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
226 {
227         udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
228         udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
229 }
230
231 static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
232 {
233         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
234
235         udc_writeb(base,(S3C2410_UDC_EP0_CSR_SOPKTRDY
236                                 | S3C2410_UDC_EP0_CSR_DE),
237                         S3C2410_UDC_EP0_CSR_REG);
238 }
239
240 static inline void s3c2410_udc_set_ep0_sse_out(void __iomem *base)
241 {
242         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_SSE),
245                         S3C2410_UDC_EP0_CSR_REG);
246 }
247
248 static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
249 {
250         udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
251         udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
252                         | S3C2410_UDC_EP0_CSR_DE),
253                 S3C2410_UDC_EP0_CSR_REG);
254 }
255
256 /*------------------------- I/O ----------------------------------*/
257
258 /*
259  *      s3c2410_udc_done
260  */
261 static void s3c2410_udc_done(struct s3c2410_ep *ep,
262                 struct s3c2410_request *req, int status)
263 {
264         unsigned halted = ep->halted;
265
266         list_del_init(&req->queue);
267
268         if (likely (req->req.status == -EINPROGRESS))
269                 req->req.status = status;
270         else
271                 status = req->req.status;
272
273         ep->halted = 1;
274         req->req.complete(&ep->ep, &req->req);
275         ep->halted = halted;
276 }
277
278 static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
279                 struct s3c2410_ep *ep, int status)
280 {
281         /* Sanity check */
282         if (&ep->queue == NULL)
283                 return;
284
285         while (!list_empty (&ep->queue)) {
286                 struct s3c2410_request *req;
287                 req = list_entry (ep->queue.next, struct s3c2410_request,
288                                 queue);
289                 s3c2410_udc_done(ep, req, status);
290         }
291 }
292
293 static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc *dev)
294 {
295         unsigned i;
296
297         /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
298          * fifos, and pending transactions mustn't be continued in any case.
299          */
300
301         for (i = 1; i < S3C2410_ENDPOINTS; i++)
302                 s3c2410_udc_nuke(dev, &dev->ep[i], -ECONNABORTED);
303 }
304
305 static inline int s3c2410_udc_fifo_count_out(void)
306 {
307         int tmp;
308
309         tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
310         tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
311         return tmp;
312 }
313
314 /*
315  *      s3c2410_udc_write_packet
316  */
317 static inline int s3c2410_udc_write_packet(int fifo,
318                 struct s3c2410_request *req,
319                 unsigned max)
320 {
321         unsigned len = min(req->req.length - req->req.actual, max);
322         u8 *buf = req->req.buf + req->req.actual;
323
324         prefetch(buf);
325
326         dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
327                 req->req.actual, req->req.length, len, req->req.actual + len);
328
329         req->req.actual += len;
330
331         udelay(5);
332         writesb(base_addr + fifo, buf, len);
333         return len;
334 }
335
336 /*
337  *      s3c2410_udc_write_fifo
338  *
339  * return:  0 = still running, 1 = completed, negative = errno
340  */
341 static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
342                 struct s3c2410_request *req)
343 {
344         unsigned        count;
345         int             is_last;
346         u32             idx;
347         int             fifo_reg;
348         u32             ep_csr;
349
350         idx = ep->bEndpointAddress & 0x7F;
351         switch (idx) {
352         default:
353                 idx = 0;
354         case 0:
355                 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
356                 break;
357         case 1:
358                 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
359                 break;
360         case 2:
361                 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
362                 break;
363         case 3:
364                 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
365                 break;
366         case 4:
367                 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
368                 break;
369         }
370
371         count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
372
373         /* last packet is often short (sometimes a zlp) */
374         if (count != ep->ep.maxpacket)
375                 is_last = 1;
376         else if (req->req.length != req->req.actual || req->req.zero)
377                 is_last = 0;
378         else
379                 is_last = 2;
380
381         /* Only ep0 debug messages are interesting */
382         if (idx == 0)
383                 dprintk(DEBUG_NORMAL,
384                         "Written ep%d %d.%d of %d b [last %d,z %d]\n",
385                         idx, count, req->req.actual, req->req.length,
386                         is_last, req->req.zero);
387
388         if (is_last) {
389                 /* The order is important. It prevents sending 2 packets
390                  * at the same time */
391
392                 if (idx == 0) {
393                         /* Reset signal => no need to say 'data sent' */
394                         if (! (udc_read(S3C2410_UDC_USB_INT_REG)
395                                         & S3C2410_UDC_USBINT_RESET))
396                                 s3c2410_udc_set_ep0_de_in(base_addr);
397                         ep->dev->ep0state=EP0_IDLE;
398                 } else {
399                         udc_write(idx, S3C2410_UDC_INDEX_REG);
400                         ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
401                         udc_write(idx, S3C2410_UDC_INDEX_REG);
402                         udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
403                                         S3C2410_UDC_IN_CSR1_REG);
404                 }
405
406                 s3c2410_udc_done(ep, req, 0);
407                 is_last = 1;
408         } else {
409                 if (idx == 0) {
410                         /* Reset signal => no need to say 'data sent' */
411                         if (! (udc_read(S3C2410_UDC_USB_INT_REG)
412                                         & S3C2410_UDC_USBINT_RESET))
413                                 s3c2410_udc_set_ep0_ipr(base_addr);
414                 } else {
415                         udc_write(idx, S3C2410_UDC_INDEX_REG);
416                         ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
417                         udc_write(idx, S3C2410_UDC_INDEX_REG);
418                         udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
419                                         S3C2410_UDC_IN_CSR1_REG);
420                 }
421         }
422
423         return is_last;
424 }
425
426 static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
427                 struct s3c2410_request *req, unsigned avail)
428 {
429         unsigned len;
430
431         len = min(req->req.length - req->req.actual, avail);
432         req->req.actual += len;
433
434         readsb(fifo + base_addr, buf, len);
435         return len;
436 }
437
438 /*
439  * return:  0 = still running, 1 = queue empty, negative = errno
440  */
441 static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
442                                  struct s3c2410_request *req)
443 {
444         u8              *buf;
445         u32             ep_csr;
446         unsigned        bufferspace;
447         int             is_last=1;
448         unsigned        avail;
449         int             fifo_count = 0;
450         u32             idx;
451         int             fifo_reg;
452
453         idx = ep->bEndpointAddress & 0x7F;
454
455         switch (idx) {
456         default:
457                 idx = 0;
458         case 0:
459                 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
460                 break;
461         case 1:
462                 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
463                 break;
464         case 2:
465                 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
466                 break;
467         case 3:
468                 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
469                 break;
470         case 4:
471                 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
472                 break;
473         }
474
475         if (!req->req.length)
476                 return 1;
477
478         buf = req->req.buf + req->req.actual;
479         bufferspace = req->req.length - req->req.actual;
480         if (!bufferspace) {
481                 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
482                 return -1;
483         }
484
485         udc_write(idx, S3C2410_UDC_INDEX_REG);
486
487         fifo_count = s3c2410_udc_fifo_count_out();
488         dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
489
490         if (fifo_count > ep->ep.maxpacket)
491                 avail = ep->ep.maxpacket;
492         else
493                 avail = fifo_count;
494
495         fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
496
497         /* checking this with ep0 is not accurate as we already
498          * read a control request
499          **/
500         if (idx != 0 && fifo_count < ep->ep.maxpacket) {
501                 is_last = 1;
502                 /* overflowed this request?  flush extra data */
503                 if (fifo_count != avail)
504                         req->req.status = -EOVERFLOW;
505         } else {
506                 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
507         }
508
509         udc_write(idx, S3C2410_UDC_INDEX_REG);
510         fifo_count = s3c2410_udc_fifo_count_out();
511
512         /* Only ep0 debug messages are interesting */
513         if (idx == 0)
514                 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
515                         __func__, fifo_count,is_last);
516
517         if (is_last) {
518                 if (idx == 0) {
519                         s3c2410_udc_set_ep0_de_out(base_addr);
520                         ep->dev->ep0state = EP0_IDLE;
521                 } else {
522                         udc_write(idx, S3C2410_UDC_INDEX_REG);
523                         ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
524                         udc_write(idx, S3C2410_UDC_INDEX_REG);
525                         udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
526                                         S3C2410_UDC_OUT_CSR1_REG);
527                 }
528
529                 s3c2410_udc_done(ep, req, 0);
530         } else {
531                 if (idx == 0) {
532                         s3c2410_udc_clear_ep0_opr(base_addr);
533                 } else {
534                         udc_write(idx, S3C2410_UDC_INDEX_REG);
535                         ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
536                         udc_write(idx, S3C2410_UDC_INDEX_REG);
537                         udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
538                                         S3C2410_UDC_OUT_CSR1_REG);
539                 }
540         }
541
542         return is_last;
543 }
544
545 static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
546 {
547         unsigned char *outbuf = (unsigned char*)crq;
548         int bytes_read = 0;
549
550         udc_write(0, S3C2410_UDC_INDEX_REG);
551
552         bytes_read = s3c2410_udc_fifo_count_out();
553
554         dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
555
556         if (bytes_read > sizeof(struct usb_ctrlrequest))
557                 bytes_read = sizeof(struct usb_ctrlrequest);
558
559         readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
560
561         dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
562                 bytes_read, crq->bRequest, crq->bRequestType,
563                 crq->wValue, crq->wIndex, crq->wLength);
564
565         return bytes_read;
566 }
567
568 static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
569                 struct usb_ctrlrequest *crq)
570 {
571         u16 status = 0;
572         u8 ep_num = crq->wIndex & 0x7F;
573         u8 is_in = crq->wIndex & USB_DIR_IN;
574
575         switch (crq->bRequestType & USB_RECIP_MASK) {
576         case USB_RECIP_INTERFACE:
577                 break;
578
579         case USB_RECIP_DEVICE:
580                 status = dev->devstatus;
581                 break;
582
583         case USB_RECIP_ENDPOINT:
584                 if (ep_num > 4 || crq->wLength > 2)
585                         return 1;
586
587                 if (ep_num == 0) {
588                         udc_write(0, S3C2410_UDC_INDEX_REG);
589                         status = udc_read(S3C2410_UDC_IN_CSR1_REG);
590                         status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
591                 } else {
592                         udc_write(ep_num, S3C2410_UDC_INDEX_REG);
593                         if (is_in) {
594                                 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
595                                 status = status & S3C2410_UDC_ICSR1_SENDSTL;
596                         } else {
597                                 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
598                                 status = status & S3C2410_UDC_OCSR1_SENDSTL;
599                         }
600                 }
601
602                 status = status ? 1 : 0;
603                 break;
604
605         default:
606                 return 1;
607         }
608
609         /* Seems to be needed to get it working. ouch :( */
610         udelay(5);
611         udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
612         udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
613         s3c2410_udc_set_ep0_de_in(base_addr);
614
615         return 0;
616 }
617 /*------------------------- usb state machine -------------------------------*/
618 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
619
620 static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
621                                         struct s3c2410_ep *ep,
622                                         struct usb_ctrlrequest *crq,
623                                         u32 ep0csr)
624 {
625         int len, ret, tmp;
626
627         /* start control request? */
628         if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
629                 return;
630
631         s3c2410_udc_nuke(dev, ep, -EPROTO);
632
633         len = s3c2410_udc_read_fifo_crq(crq);
634         if (len != sizeof(*crq)) {
635                 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
636                         " wanted %d bytes got %d. Stalling out...\n",
637                         sizeof(*crq), len);
638                 s3c2410_udc_set_ep0_ss(base_addr);
639                 return;
640         }
641
642         dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
643                 crq->bRequest, crq->bRequestType, crq->wLength);
644
645         /* cope with automagic for some standard requests. */
646         dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
647                 == USB_TYPE_STANDARD;
648         dev->req_config = 0;
649         dev->req_pending = 1;
650
651         switch (crq->bRequest) {
652         case USB_REQ_SET_CONFIGURATION:
653                 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ... \n");
654
655                 if (crq->bRequestType == USB_RECIP_DEVICE) {
656                         dev->req_config = 1;
657                         s3c2410_udc_set_ep0_de_out(base_addr);
658                 }
659                 break;
660
661         case USB_REQ_SET_INTERFACE:
662                 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ... \n");
663
664                 if (crq->bRequestType == USB_RECIP_INTERFACE) {
665                         dev->req_config = 1;
666                         s3c2410_udc_set_ep0_de_out(base_addr);
667                 }
668                 break;
669
670         case USB_REQ_SET_ADDRESS:
671                 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ... \n");
672
673                 if (crq->bRequestType == USB_RECIP_DEVICE) {
674                         tmp = crq->wValue & 0x7F;
675                         dev->address = tmp;
676                         udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
677                                         S3C2410_UDC_FUNC_ADDR_REG);
678                         s3c2410_udc_set_ep0_de_out(base_addr);
679                         return;
680                 }
681                 break;
682
683         case USB_REQ_GET_STATUS:
684                 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ... \n");
685                 s3c2410_udc_clear_ep0_opr(base_addr);
686
687                 if (dev->req_std) {
688                         if (!s3c2410_udc_get_status(dev, crq)) {
689                                 return;
690                         }
691                 }
692                 break;
693
694         case USB_REQ_CLEAR_FEATURE:
695                 s3c2410_udc_clear_ep0_opr(base_addr);
696
697                 if (crq->bRequestType != USB_RECIP_ENDPOINT)
698                         break;
699
700                 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
701                         break;
702
703                 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
704                 s3c2410_udc_set_ep0_de_out(base_addr);
705                 return;
706
707         case USB_REQ_SET_FEATURE:
708                 s3c2410_udc_clear_ep0_opr(base_addr);
709
710                 if (crq->bRequestType != USB_RECIP_ENDPOINT)
711                         break;
712
713                 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
714                         break;
715
716                 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
717                 s3c2410_udc_set_ep0_de_out(base_addr);
718                 return;
719
720         default:
721                 s3c2410_udc_clear_ep0_opr(base_addr);
722                 break;
723         }
724
725         if (crq->bRequestType & USB_DIR_IN)
726                 dev->ep0state = EP0_IN_DATA_PHASE;
727         else
728                 dev->ep0state = EP0_OUT_DATA_PHASE;
729
730         if (!dev->driver)
731                 return;
732
733         /* deliver the request to the gadget driver */
734         ret = dev->driver->setup(&dev->gadget, crq);
735         if (ret < 0) {
736                 if (dev->req_config) {
737                         dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
738                                 crq->bRequest, ret);
739                         return;
740                 }
741
742                 if (ret == -EOPNOTSUPP)
743                         dprintk(DEBUG_NORMAL, "Operation not supported\n");
744                 else
745                         dprintk(DEBUG_NORMAL,
746                                 "dev->driver->setup failed. (%d)\n", ret);
747
748                 udelay(5);
749                 s3c2410_udc_set_ep0_ss(base_addr);
750                 s3c2410_udc_set_ep0_de_out(base_addr);
751                 dev->ep0state = EP0_IDLE;
752                 /* deferred i/o == no response yet */
753         } else if (dev->req_pending) {
754                 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
755                 dev->req_pending=0;
756         }
757
758         dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
759 }
760
761 static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
762 {
763         u32                     ep0csr;
764         struct s3c2410_ep       *ep = &dev->ep[0];
765         struct s3c2410_request  *req;
766         struct usb_ctrlrequest  crq;
767
768         if (list_empty(&ep->queue))
769                 req = NULL;
770         else
771                 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
772
773         /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
774          * S3C2410_UDC_EP0_CSR_REG when index is zero */
775
776         udc_write(0, S3C2410_UDC_INDEX_REG);
777         ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
778
779         dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
780                 ep0csr, ep0states[dev->ep0state]);
781
782         /* clear stall status */
783         if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
784                 s3c2410_udc_nuke(dev, ep, -EPIPE);
785                 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
786                 s3c2410_udc_clear_ep0_sst(base_addr);
787                 dev->ep0state = EP0_IDLE;
788                 return;
789         }
790
791         /* clear setup end */
792         if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
793                 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
794                 s3c2410_udc_nuke(dev, ep, 0);
795                 s3c2410_udc_clear_ep0_se(base_addr);
796                 dev->ep0state = EP0_IDLE;
797         }
798
799         switch (dev->ep0state) {
800         case EP0_IDLE:
801                 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
802                 break;
803
804         case EP0_IN_DATA_PHASE:                 /* GET_DESCRIPTOR etc */
805                 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
806                 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req) {
807                         s3c2410_udc_write_fifo(ep, req);
808                 }
809                 break;
810
811         case EP0_OUT_DATA_PHASE:                /* SET_DESCRIPTOR etc */
812                 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
813                 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req ) {
814                         s3c2410_udc_read_fifo(ep,req);
815                 }
816                 break;
817
818         case EP0_END_XFER:
819                 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
820                 dev->ep0state = EP0_IDLE;
821                 break;
822
823         case EP0_STALL:
824                 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
825                 dev->ep0state = EP0_IDLE;
826                 break;
827         }
828 }
829
830 /*
831  *      handle_ep - Manage I/O endpoints
832  */
833
834 static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
835 {
836         struct s3c2410_request  *req;
837         int                     is_in = ep->bEndpointAddress & USB_DIR_IN;
838         u32                     ep_csr1;
839         u32                     idx;
840
841         if (likely (!list_empty(&ep->queue)))
842                 req = list_entry(ep->queue.next,
843                                 struct s3c2410_request, queue);
844         else
845                 req = NULL;
846
847         idx = ep->bEndpointAddress & 0x7F;
848
849         if (is_in) {
850                 udc_write(idx, S3C2410_UDC_INDEX_REG);
851                 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
852                 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
853                         idx, ep_csr1, req ? 1 : 0);
854
855                 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
856                         dprintk(DEBUG_VERBOSE, "st\n");
857                         udc_write(idx, S3C2410_UDC_INDEX_REG);
858                         udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
859                                         S3C2410_UDC_IN_CSR1_REG);
860                         return;
861                 }
862
863                 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req) {
864                         s3c2410_udc_write_fifo(ep,req);
865                 }
866         } else {
867                 udc_write(idx, S3C2410_UDC_INDEX_REG);
868                 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
869                 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
870
871                 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
872                         udc_write(idx, S3C2410_UDC_INDEX_REG);
873                         udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
874                                         S3C2410_UDC_OUT_CSR1_REG);
875                         return;
876                 }
877
878                 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req) {
879                         s3c2410_udc_read_fifo(ep,req);
880                 }
881         }
882 }
883
884 #include <mach/regs-irq.h>
885
886 /*
887  *      s3c2410_udc_irq - interrupt handler
888  */
889 static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
890 {
891         struct s3c2410_udc *dev = _dev;
892         int usb_status;
893         int usbd_status;
894         int pwr_reg;
895         int ep0csr;
896         int i;
897         u32 idx, idx2;
898         unsigned long flags;
899
900         spin_lock_irqsave(&dev->lock, flags);
901
902         /* Driver connected ? */
903         if (!dev->driver) {
904                 /* Clear interrupts */
905                 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
906                                 S3C2410_UDC_USB_INT_REG);
907                 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
908                                 S3C2410_UDC_EP_INT_REG);
909         }
910
911         /* Save index */
912         idx = udc_read(S3C2410_UDC_INDEX_REG);
913
914         /* Read status registers */
915         usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
916         usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
917         pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
918
919         udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
920         ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
921
922         dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
923                 usb_status, usbd_status, pwr_reg, ep0csr);
924
925         /*
926          * Now, handle interrupts. There's two types :
927          * - Reset, Resume, Suspend coming -> usb_int_reg
928          * - EP -> ep_int_reg
929          */
930
931         /* RESET */
932         if (usb_status & S3C2410_UDC_USBINT_RESET) {
933                 /* two kind of reset :
934                  * - reset start -> pwr reg = 8
935                  * - reset end   -> pwr reg = 0
936                  **/
937                 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
938                         ep0csr, pwr_reg);
939
940                 dev->gadget.speed = USB_SPEED_UNKNOWN;
941                 udc_write(0x00, S3C2410_UDC_INDEX_REG);
942                 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
943                                 S3C2410_UDC_MAXP_REG);
944                 dev->address = 0;
945
946                 dev->ep0state = EP0_IDLE;
947                 dev->gadget.speed = USB_SPEED_FULL;
948
949                 /* clear interrupt */
950                 udc_write(S3C2410_UDC_USBINT_RESET,
951                                 S3C2410_UDC_USB_INT_REG);
952
953                 udc_write(idx, S3C2410_UDC_INDEX_REG);
954                 spin_unlock_irqrestore(&dev->lock, flags);
955                 return IRQ_HANDLED;
956         }
957
958         /* RESUME */
959         if (usb_status & S3C2410_UDC_USBINT_RESUME) {
960                 dprintk(DEBUG_NORMAL, "USB resume\n");
961
962                 /* clear interrupt */
963                 udc_write(S3C2410_UDC_USBINT_RESUME,
964                                 S3C2410_UDC_USB_INT_REG);
965
966                 if (dev->gadget.speed != USB_SPEED_UNKNOWN
967                                 && dev->driver
968                                 && dev->driver->resume)
969                         dev->driver->resume(&dev->gadget);
970         }
971
972         /* SUSPEND */
973         if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
974                 dprintk(DEBUG_NORMAL, "USB suspend\n");
975
976                 /* clear interrupt */
977                 udc_write(S3C2410_UDC_USBINT_SUSPEND,
978                                 S3C2410_UDC_USB_INT_REG);
979
980                 if (dev->gadget.speed != USB_SPEED_UNKNOWN
981                                 && dev->driver
982                                 && dev->driver->suspend)
983                         dev->driver->suspend(&dev->gadget);
984
985                 dev->ep0state = EP0_IDLE;
986         }
987
988         /* EP */
989         /* control traffic */
990         /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
991          * generate an interrupt
992          */
993         if (usbd_status & S3C2410_UDC_INT_EP0) {
994                 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
995                 /* Clear the interrupt bit by setting it to 1 */
996                 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
997                 s3c2410_udc_handle_ep0(dev);
998         }
999
1000         /* endpoint data transfers */
1001         for (i = 1; i < S3C2410_ENDPOINTS; i++) {
1002                 u32 tmp = 1 << i;
1003                 if (usbd_status & tmp) {
1004                         dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
1005
1006                         /* Clear the interrupt bit by setting it to 1 */
1007                         udc_write(tmp, S3C2410_UDC_EP_INT_REG);
1008                         s3c2410_udc_handle_ep(&dev->ep[i]);
1009                 }
1010         }
1011
1012         /* what else causes this interrupt? a receive! who is it? */
1013         if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
1014                 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
1015                         idx2 = udc_read(S3C2410_UDC_INDEX_REG);
1016                         udc_write(i, S3C2410_UDC_INDEX_REG);
1017
1018                         if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
1019                                 s3c2410_udc_handle_ep(&dev->ep[i]);
1020
1021                         /* restore index */
1022                         udc_write(idx2, S3C2410_UDC_INDEX_REG);
1023                 }
1024         }
1025
1026         dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
1027
1028         /* Restore old index */
1029         udc_write(idx, S3C2410_UDC_INDEX_REG);
1030
1031         spin_unlock_irqrestore(&dev->lock, flags);
1032
1033         return IRQ_HANDLED;
1034 }
1035 /*------------------------- s3c2410_ep_ops ----------------------------------*/
1036
1037 static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1038 {
1039         return container_of(ep, struct s3c2410_ep, ep);
1040 }
1041
1042 static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1043 {
1044         return container_of(gadget, struct s3c2410_udc, gadget);
1045 }
1046
1047 static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1048 {
1049         return container_of(req, struct s3c2410_request, req);
1050 }
1051
1052 /*
1053  *      s3c2410_udc_ep_enable
1054  */
1055 static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1056                                  const struct usb_endpoint_descriptor *desc)
1057 {
1058         struct s3c2410_udc      *dev;
1059         struct s3c2410_ep       *ep;
1060         u32                     max, tmp;
1061         unsigned long           flags;
1062         u32                     csr1,csr2;
1063         u32                     int_en_reg;
1064
1065         ep = to_s3c2410_ep(_ep);
1066
1067         if (!_ep || !desc
1068                         || _ep->name == ep0name
1069                         || desc->bDescriptorType != USB_DT_ENDPOINT)
1070                 return -EINVAL;
1071
1072         dev = ep->dev;
1073         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1074                 return -ESHUTDOWN;
1075
1076         max = usb_endpoint_maxp(desc) & 0x1fff;
1077
1078         local_irq_save (flags);
1079         _ep->maxpacket = max & 0x7ff;
1080         ep->ep.desc = desc;
1081         ep->halted = 0;
1082         ep->bEndpointAddress = desc->bEndpointAddress;
1083
1084         /* set max packet */
1085         udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1086         udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1087
1088         /* set type, direction, address; reset fifo counters */
1089         if (desc->bEndpointAddress & USB_DIR_IN) {
1090                 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1091                 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1092
1093                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1094                 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1095                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1096                 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1097         } else {
1098                 /* don't flush in fifo or it will cause endpoint interrupt */
1099                 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1100                 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1101
1102                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1103                 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1104                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1105                 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1106
1107                 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1108                 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1109
1110                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1111                 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1112                 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1113                 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1114         }
1115
1116         /* enable irqs */
1117         int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1118         udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1119
1120         /* print some debug message */
1121         tmp = desc->bEndpointAddress;
1122         dprintk (DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1123                  _ep->name,ep->num, tmp,
1124                  desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1125
1126         local_irq_restore (flags);
1127         s3c2410_udc_set_halt(_ep, 0);
1128
1129         return 0;
1130 }
1131
1132 /*
1133  * s3c2410_udc_ep_disable
1134  */
1135 static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1136 {
1137         struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1138         unsigned long flags;
1139         u32 int_en_reg;
1140
1141         if (!_ep || !ep->ep.desc) {
1142                 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1143                         _ep ? ep->ep.name : NULL);
1144                 return -EINVAL;
1145         }
1146
1147         local_irq_save(flags);
1148
1149         dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1150
1151         ep->ep.desc = NULL;
1152         ep->halted = 1;
1153
1154         s3c2410_udc_nuke (ep->dev, ep, -ESHUTDOWN);
1155
1156         /* disable irqs */
1157         int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1158         udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1159
1160         local_irq_restore(flags);
1161
1162         dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1163
1164         return 0;
1165 }
1166
1167 /*
1168  * s3c2410_udc_alloc_request
1169  */
1170 static struct usb_request *
1171 s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1172 {
1173         struct s3c2410_request *req;
1174
1175         dprintk(DEBUG_VERBOSE,"%s(%p,%d)\n", __func__, _ep, mem_flags);
1176
1177         if (!_ep)
1178                 return NULL;
1179
1180         req = kzalloc (sizeof(struct s3c2410_request), mem_flags);
1181         if (!req)
1182                 return NULL;
1183
1184         INIT_LIST_HEAD (&req->queue);
1185         return &req->req;
1186 }
1187
1188 /*
1189  * s3c2410_udc_free_request
1190  */
1191 static void
1192 s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1193 {
1194         struct s3c2410_ep       *ep = to_s3c2410_ep(_ep);
1195         struct s3c2410_request  *req = to_s3c2410_req(_req);
1196
1197         dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1198
1199         if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
1200                 return;
1201
1202         WARN_ON (!list_empty (&req->queue));
1203         kfree(req);
1204 }
1205
1206 /*
1207  *      s3c2410_udc_queue
1208  */
1209 static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1210                 gfp_t gfp_flags)
1211 {
1212         struct s3c2410_request  *req = to_s3c2410_req(_req);
1213         struct s3c2410_ep       *ep = to_s3c2410_ep(_ep);
1214         struct s3c2410_udc      *dev;
1215         u32                     ep_csr = 0;
1216         int                     fifo_count = 0;
1217         unsigned long           flags;
1218
1219         if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1220                 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1221                 return -EINVAL;
1222         }
1223
1224         dev = ep->dev;
1225         if (unlikely (!dev->driver
1226                         || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1227                 return -ESHUTDOWN;
1228         }
1229
1230         local_irq_save (flags);
1231
1232         if (unlikely(!_req || !_req->complete
1233                         || !_req->buf || !list_empty(&req->queue))) {
1234                 if (!_req)
1235                         dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1236                 else {
1237                         dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
1238                                 __func__, !_req->complete,!_req->buf,
1239                                 !list_empty(&req->queue));
1240                 }
1241
1242                 local_irq_restore(flags);
1243                 return -EINVAL;
1244         }
1245
1246         _req->status = -EINPROGRESS;
1247         _req->actual = 0;
1248
1249         dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1250                  __func__, ep->bEndpointAddress, _req->length);
1251
1252         if (ep->bEndpointAddress) {
1253                 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1254
1255                 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1256                                 ? S3C2410_UDC_IN_CSR1_REG
1257                                 : S3C2410_UDC_OUT_CSR1_REG);
1258                 fifo_count = s3c2410_udc_fifo_count_out();
1259         } else {
1260                 udc_write(0, S3C2410_UDC_INDEX_REG);
1261                 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1262                 fifo_count = s3c2410_udc_fifo_count_out();
1263         }
1264
1265         /* kickstart this i/o queue? */
1266         if (list_empty(&ep->queue) && !ep->halted) {
1267                 if (ep->bEndpointAddress == 0 /* ep0 */) {
1268                         switch (dev->ep0state) {
1269                         case EP0_IN_DATA_PHASE:
1270                                 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1271                                                 && s3c2410_udc_write_fifo(ep,
1272                                                         req)) {
1273                                         dev->ep0state = EP0_IDLE;
1274                                         req = NULL;
1275                                 }
1276                                 break;
1277
1278                         case EP0_OUT_DATA_PHASE:
1279                                 if ((!_req->length)
1280                                         || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1281                                                 && s3c2410_udc_read_fifo(ep,
1282                                                         req))) {
1283                                         dev->ep0state = EP0_IDLE;
1284                                         req = NULL;
1285                                 }
1286                                 break;
1287
1288                         default:
1289                                 local_irq_restore(flags);
1290                                 return -EL2HLT;
1291                         }
1292                 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1293                                 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1294                                 && s3c2410_udc_write_fifo(ep, req)) {
1295                         req = NULL;
1296                 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1297                                 && fifo_count
1298                                 && s3c2410_udc_read_fifo(ep, req)) {
1299                         req = NULL;
1300                 }
1301         }
1302
1303         /* pio or dma irq handler advances the queue. */
1304         if (likely (req != 0))
1305                 list_add_tail(&req->queue, &ep->queue);
1306
1307         local_irq_restore(flags);
1308
1309         dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1310         return 0;
1311 }
1312
1313 /*
1314  *      s3c2410_udc_dequeue
1315  */
1316 static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1317 {
1318         struct s3c2410_ep       *ep = to_s3c2410_ep(_ep);
1319         struct s3c2410_udc      *udc;
1320         int                     retval = -EINVAL;
1321         unsigned long           flags;
1322         struct s3c2410_request  *req = NULL;
1323
1324         dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1325
1326         if (!the_controller->driver)
1327                 return -ESHUTDOWN;
1328
1329         if (!_ep || !_req)
1330                 return retval;
1331
1332         udc = to_s3c2410_udc(ep->gadget);
1333
1334         local_irq_save (flags);
1335
1336         list_for_each_entry (req, &ep->queue, queue) {
1337                 if (&req->req == _req) {
1338                         list_del_init (&req->queue);
1339                         _req->status = -ECONNRESET;
1340                         retval = 0;
1341                         break;
1342                 }
1343         }
1344
1345         if (retval == 0) {
1346                 dprintk(DEBUG_VERBOSE,
1347                         "dequeued req %p from %s, len %d buf %p\n",
1348                         req, _ep->name, _req->length, _req->buf);
1349
1350                 s3c2410_udc_done(ep, req, -ECONNRESET);
1351         }
1352
1353         local_irq_restore (flags);
1354         return retval;
1355 }
1356
1357 /*
1358  * s3c2410_udc_set_halt
1359  */
1360 static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1361 {
1362         struct s3c2410_ep       *ep = to_s3c2410_ep(_ep);
1363         u32                     ep_csr = 0;
1364         unsigned long           flags;
1365         u32                     idx;
1366
1367         if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
1368                 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1369                 return -EINVAL;
1370         }
1371
1372         local_irq_save (flags);
1373
1374         idx = ep->bEndpointAddress & 0x7F;
1375
1376         if (idx == 0) {
1377                 s3c2410_udc_set_ep0_ss(base_addr);
1378                 s3c2410_udc_set_ep0_de_out(base_addr);
1379         } else {
1380                 udc_write(idx, S3C2410_UDC_INDEX_REG);
1381                 ep_csr = udc_read((ep->bEndpointAddress &USB_DIR_IN)
1382                                 ? S3C2410_UDC_IN_CSR1_REG
1383                                 : S3C2410_UDC_OUT_CSR1_REG);
1384
1385                 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1386                         if (value)
1387                                 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1388                                         S3C2410_UDC_IN_CSR1_REG);
1389                         else {
1390                                 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1391                                 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1392                                 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1393                                 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1394                         }
1395                 } else {
1396                         if (value)
1397                                 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1398                                         S3C2410_UDC_OUT_CSR1_REG);
1399                         else {
1400                                 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1401                                 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1402                                 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1403                                 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1404                         }
1405                 }
1406         }
1407
1408         ep->halted = value ? 1 : 0;
1409         local_irq_restore (flags);
1410
1411         return 0;
1412 }
1413
1414 static const struct usb_ep_ops s3c2410_ep_ops = {
1415         .enable         = s3c2410_udc_ep_enable,
1416         .disable        = s3c2410_udc_ep_disable,
1417
1418         .alloc_request  = s3c2410_udc_alloc_request,
1419         .free_request   = s3c2410_udc_free_request,
1420
1421         .queue          = s3c2410_udc_queue,
1422         .dequeue        = s3c2410_udc_dequeue,
1423
1424         .set_halt       = s3c2410_udc_set_halt,
1425 };
1426
1427 /*------------------------- usb_gadget_ops ----------------------------------*/
1428
1429 /*
1430  *      s3c2410_udc_get_frame
1431  */
1432 static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1433 {
1434         int tmp;
1435
1436         dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1437
1438         tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1439         tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1440         return tmp;
1441 }
1442
1443 /*
1444  *      s3c2410_udc_wakeup
1445  */
1446 static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1447 {
1448         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1449         return 0;
1450 }
1451
1452 /*
1453  *      s3c2410_udc_set_selfpowered
1454  */
1455 static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1456 {
1457         struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1458
1459         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1460
1461         if (value)
1462                 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1463         else
1464                 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1465
1466         return 0;
1467 }
1468
1469 static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1470 static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1471
1472 static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1473 {
1474         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1475
1476         if (udc_info && (udc_info->udc_command ||
1477                 gpio_is_valid(udc_info->pullup_pin))) {
1478
1479                 if (is_on)
1480                         s3c2410_udc_enable(udc);
1481                 else {
1482                         if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1483                                 if (udc->driver && udc->driver->disconnect)
1484                                         udc->driver->disconnect(&udc->gadget);
1485
1486                         }
1487                         s3c2410_udc_disable(udc);
1488                 }
1489         }
1490         else
1491                 return -EOPNOTSUPP;
1492
1493         return 0;
1494 }
1495
1496 static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1497 {
1498         struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1499
1500         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1501
1502         udc->vbus = (is_active != 0);
1503         s3c2410_udc_set_pullup(udc, is_active);
1504         return 0;
1505 }
1506
1507 static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1508 {
1509         struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1510
1511         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1512
1513         s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
1514         return 0;
1515 }
1516
1517 static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1518 {
1519         struct s3c2410_udc      *dev = _dev;
1520         unsigned int            value;
1521
1522         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1523
1524         value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
1525         if (udc_info->vbus_pin_inverted)
1526                 value = !value;
1527
1528         if (value != dev->vbus)
1529                 s3c2410_udc_vbus_session(&dev->gadget, value);
1530
1531         return IRQ_HANDLED;
1532 }
1533
1534 static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1535 {
1536         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1537
1538         if (udc_info && udc_info->vbus_draw) {
1539                 udc_info->vbus_draw(ma);
1540                 return 0;
1541         }
1542
1543         return -ENOTSUPP;
1544 }
1545
1546 static int s3c2410_udc_start(struct usb_gadget_driver *driver,
1547                 int (*bind)(struct usb_gadget *));
1548 static int s3c2410_udc_stop(struct usb_gadget_driver *driver);
1549
1550 static const struct usb_gadget_ops s3c2410_ops = {
1551         .get_frame              = s3c2410_udc_get_frame,
1552         .wakeup                 = s3c2410_udc_wakeup,
1553         .set_selfpowered        = s3c2410_udc_set_selfpowered,
1554         .pullup                 = s3c2410_udc_pullup,
1555         .vbus_session           = s3c2410_udc_vbus_session,
1556         .vbus_draw              = s3c2410_vbus_draw,
1557         .start                  = s3c2410_udc_start,
1558         .stop                   = s3c2410_udc_stop,
1559 };
1560
1561 static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1562 {
1563         if (!udc_info)
1564                 return;
1565
1566         if (udc_info->udc_command) {
1567                 udc_info->udc_command(cmd);
1568         } else if (gpio_is_valid(udc_info->pullup_pin)) {
1569                 int value;
1570
1571                 switch (cmd) {
1572                 case S3C2410_UDC_P_ENABLE:
1573                         value = 1;
1574                         break;
1575                 case S3C2410_UDC_P_DISABLE:
1576                         value = 0;
1577                         break;
1578                 default:
1579                         return;
1580                 }
1581                 value ^= udc_info->pullup_pin_inverted;
1582
1583                 gpio_set_value(udc_info->pullup_pin, value);
1584         }
1585 }
1586
1587 /*------------------------- gadget driver handling---------------------------*/
1588 /*
1589  * s3c2410_udc_disable
1590  */
1591 static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1592 {
1593         dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1594
1595         /* Disable all interrupts */
1596         udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1597         udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1598
1599         /* Clear the interrupt registers */
1600         udc_write(S3C2410_UDC_USBINT_RESET
1601                                 | S3C2410_UDC_USBINT_RESUME
1602                                 | S3C2410_UDC_USBINT_SUSPEND,
1603                         S3C2410_UDC_USB_INT_REG);
1604
1605         udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1606
1607         /* Good bye, cruel world */
1608         s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
1609
1610         /* Set speed to unknown */
1611         dev->gadget.speed = USB_SPEED_UNKNOWN;
1612 }
1613
1614 /*
1615  * s3c2410_udc_reinit
1616  */
1617 static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1618 {
1619         u32 i;
1620
1621         /* device/ep0 records init */
1622         INIT_LIST_HEAD (&dev->gadget.ep_list);
1623         INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1624         dev->ep0state = EP0_IDLE;
1625
1626         for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1627                 struct s3c2410_ep *ep = &dev->ep[i];
1628
1629                 if (i != 0)
1630                         list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1631
1632                 ep->dev = dev;
1633                 ep->ep.desc = NULL;
1634                 ep->halted = 0;
1635                 INIT_LIST_HEAD (&ep->queue);
1636         }
1637 }
1638
1639 /*
1640  * s3c2410_udc_enable
1641  */
1642 static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1643 {
1644         int i;
1645
1646         dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1647
1648         /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1649         dev->gadget.speed = USB_SPEED_FULL;
1650
1651         /* Set MAXP for all endpoints */
1652         for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1653                 udc_write(i, S3C2410_UDC_INDEX_REG);
1654                 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1655                                 S3C2410_UDC_MAXP_REG);
1656         }
1657
1658         /* Set default power state */
1659         udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1660
1661         /* Enable reset and suspend interrupt interrupts */
1662         udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1663                         S3C2410_UDC_USB_INT_EN_REG);
1664
1665         /* Enable ep0 interrupt */
1666         udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1667
1668         /* time to say "hello, world" */
1669         s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
1670 }
1671
1672 static int s3c2410_udc_start(struct usb_gadget_driver *driver,
1673                 int (*bind)(struct usb_gadget *))
1674 {
1675         struct s3c2410_udc *udc = the_controller;
1676         int             retval;
1677
1678         dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
1679
1680         /* Sanity checks */
1681         if (!udc)
1682                 return -ENODEV;
1683
1684         if (udc->driver)
1685                 return -EBUSY;
1686
1687         if (!bind || !driver->setup || driver->max_speed < USB_SPEED_FULL) {
1688                 dev_err(&udc->gadget.dev, "Invalid driver: bind %p setup %p speed %d\n",
1689                         bind, driver->setup, driver->max_speed);
1690                 return -EINVAL;
1691         }
1692 #if defined(MODULE)
1693         if (!driver->unbind) {
1694                 dev_err(&udc->gadget.dev, "Invalid driver: no unbind method\n");
1695                 return -EINVAL;
1696         }
1697 #endif
1698
1699         /* Hook the driver */
1700         udc->driver = driver;
1701         udc->gadget.dev.driver = &driver->driver;
1702
1703         /* Bind the driver */
1704         if ((retval = device_add(&udc->gadget.dev)) != 0) {
1705                 dev_err(&udc->gadget.dev, "Error in device_add() : %d\n", retval);
1706                 goto register_error;
1707         }
1708
1709         dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n",
1710                 driver->driver.name);
1711
1712         if ((retval = bind(&udc->gadget)) != 0) {
1713                 device_del(&udc->gadget.dev);
1714                 goto register_error;
1715         }
1716
1717         /* Enable udc */
1718         s3c2410_udc_enable(udc);
1719
1720         return 0;
1721
1722 register_error:
1723         udc->driver = NULL;
1724         udc->gadget.dev.driver = NULL;
1725         return retval;
1726 }
1727
1728 static int s3c2410_udc_stop(struct usb_gadget_driver *driver)
1729 {
1730         struct s3c2410_udc *udc = the_controller;
1731
1732         if (!udc)
1733                 return -ENODEV;
1734
1735         if (!driver || driver != udc->driver || !driver->unbind)
1736                 return -EINVAL;
1737
1738         dprintk(DEBUG_NORMAL, "usb_gadget_unregister_driver() '%s'\n",
1739                 driver->driver.name);
1740
1741         /* report disconnect */
1742         if (driver->disconnect)
1743                 driver->disconnect(&udc->gadget);
1744
1745         driver->unbind(&udc->gadget);
1746
1747         device_del(&udc->gadget.dev);
1748         udc->driver = NULL;
1749
1750         /* Disable udc */
1751         s3c2410_udc_disable(udc);
1752
1753         return 0;
1754 }
1755
1756 /*---------------------------------------------------------------------------*/
1757 static struct s3c2410_udc memory = {
1758         .gadget = {
1759                 .ops            = &s3c2410_ops,
1760                 .ep0            = &memory.ep[0].ep,
1761                 .name           = gadget_name,
1762                 .dev = {
1763                         .init_name      = "gadget",
1764                 },
1765         },
1766
1767         /* control endpoint */
1768         .ep[0] = {
1769                 .num            = 0,
1770                 .ep = {
1771                         .name           = ep0name,
1772                         .ops            = &s3c2410_ep_ops,
1773                         .maxpacket      = EP0_FIFO_SIZE,
1774                 },
1775                 .dev            = &memory,
1776         },
1777
1778         /* first group of endpoints */
1779         .ep[1] = {
1780                 .num            = 1,
1781                 .ep = {
1782                         .name           = "ep1-bulk",
1783                         .ops            = &s3c2410_ep_ops,
1784                         .maxpacket      = EP_FIFO_SIZE,
1785                 },
1786                 .dev            = &memory,
1787                 .fifo_size      = EP_FIFO_SIZE,
1788                 .bEndpointAddress = 1,
1789                 .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1790         },
1791         .ep[2] = {
1792                 .num            = 2,
1793                 .ep = {
1794                         .name           = "ep2-bulk",
1795                         .ops            = &s3c2410_ep_ops,
1796                         .maxpacket      = EP_FIFO_SIZE,
1797                 },
1798                 .dev            = &memory,
1799                 .fifo_size      = EP_FIFO_SIZE,
1800                 .bEndpointAddress = 2,
1801                 .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1802         },
1803         .ep[3] = {
1804                 .num            = 3,
1805                 .ep = {
1806                         .name           = "ep3-bulk",
1807                         .ops            = &s3c2410_ep_ops,
1808                         .maxpacket      = EP_FIFO_SIZE,
1809                 },
1810                 .dev            = &memory,
1811                 .fifo_size      = EP_FIFO_SIZE,
1812                 .bEndpointAddress = 3,
1813                 .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1814         },
1815         .ep[4] = {
1816                 .num            = 4,
1817                 .ep = {
1818                         .name           = "ep4-bulk",
1819                         .ops            = &s3c2410_ep_ops,
1820                         .maxpacket      = EP_FIFO_SIZE,
1821                 },
1822                 .dev            = &memory,
1823                 .fifo_size      = EP_FIFO_SIZE,
1824                 .bEndpointAddress = 4,
1825                 .bmAttributes   = USB_ENDPOINT_XFER_BULK,
1826         }
1827
1828 };
1829
1830 /*
1831  *      probe - binds to the platform device
1832  */
1833 static int s3c2410_udc_probe(struct platform_device *pdev)
1834 {
1835         struct s3c2410_udc *udc = &memory;
1836         struct device *dev = &pdev->dev;
1837         int retval;
1838         int irq;
1839
1840         dev_dbg(dev, "%s()\n", __func__);
1841
1842         usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1843         if (IS_ERR(usb_bus_clock)) {
1844                 dev_err(dev, "failed to get usb bus clock source\n");
1845                 return PTR_ERR(usb_bus_clock);
1846         }
1847
1848         clk_enable(usb_bus_clock);
1849
1850         udc_clock = clk_get(NULL, "usb-device");
1851         if (IS_ERR(udc_clock)) {
1852                 dev_err(dev, "failed to get udc clock source\n");
1853                 return PTR_ERR(udc_clock);
1854         }
1855
1856         clk_enable(udc_clock);
1857
1858         mdelay(10);
1859
1860         dev_dbg(dev, "got and enabled clocks\n");
1861
1862         if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1863                 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1864                 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1865                 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1866                 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1867                 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1868         }
1869
1870         spin_lock_init (&udc->lock);
1871         udc_info = pdev->dev.platform_data;
1872
1873         rsrc_start = S3C2410_PA_USBDEV;
1874         rsrc_len   = S3C24XX_SZ_USBDEV;
1875
1876         if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1877                 return -EBUSY;
1878
1879         base_addr = ioremap(rsrc_start, rsrc_len);
1880         if (!base_addr) {
1881                 retval = -ENOMEM;
1882                 goto err_mem;
1883         }
1884
1885         device_initialize(&udc->gadget.dev);
1886         udc->gadget.dev.parent = &pdev->dev;
1887         udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
1888
1889         the_controller = udc;
1890         platform_set_drvdata(pdev, udc);
1891
1892         s3c2410_udc_disable(udc);
1893         s3c2410_udc_reinit(udc);
1894
1895         /* irq setup after old hardware state is cleaned up */
1896         retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
1897                              0, gadget_name, udc);
1898
1899         if (retval != 0) {
1900                 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1901                 retval = -EBUSY;
1902                 goto err_map;
1903         }
1904
1905         dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1906
1907         if (udc_info && udc_info->vbus_pin > 0) {
1908                 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1909                 if (retval < 0) {
1910                         dev_err(dev, "cannot claim vbus pin\n");
1911                         goto err_int;
1912                 }
1913
1914                 irq = gpio_to_irq(udc_info->vbus_pin);
1915                 if (irq < 0) {
1916                         dev_err(dev, "no irq for gpio vbus pin\n");
1917                         goto err_gpio_claim;
1918                 }
1919
1920                 retval = request_irq(irq, s3c2410_udc_vbus_irq,
1921                                      IRQF_TRIGGER_RISING
1922                                      | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1923                                      gadget_name, udc);
1924
1925                 if (retval != 0) {
1926                         dev_err(dev, "can't get vbus irq %d, err %d\n",
1927                                 irq, retval);
1928                         retval = -EBUSY;
1929                         goto err_gpio_claim;
1930                 }
1931
1932                 dev_dbg(dev, "got irq %i\n", irq);
1933         } else {
1934                 udc->vbus = 1;
1935         }
1936
1937         if (udc_info && !udc_info->udc_command &&
1938                 gpio_is_valid(udc_info->pullup_pin)) {
1939
1940                 retval = gpio_request_one(udc_info->pullup_pin,
1941                                 udc_info->vbus_pin_inverted ?
1942                                 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1943                                 "udc pullup");
1944                 if (retval)
1945                         goto err_vbus_irq;
1946         }
1947
1948         retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1949         if (retval)
1950                 goto err_add_udc;
1951
1952         if (s3c2410_udc_debugfs_root) {
1953                 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1954                                 s3c2410_udc_debugfs_root,
1955                                 udc, &s3c2410_udc_debugfs_fops);
1956                 if (!udc->regs_info)
1957                         dev_warn(dev, "debugfs file creation failed\n");
1958         }
1959
1960         dev_dbg(dev, "probe ok\n");
1961
1962         return 0;
1963
1964 err_add_udc:
1965         if (udc_info && !udc_info->udc_command &&
1966                         gpio_is_valid(udc_info->pullup_pin))
1967                 gpio_free(udc_info->pullup_pin);
1968 err_vbus_irq:
1969         if (udc_info && udc_info->vbus_pin > 0)
1970                 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
1971 err_gpio_claim:
1972         if (udc_info && udc_info->vbus_pin > 0)
1973                 gpio_free(udc_info->vbus_pin);
1974 err_int:
1975         free_irq(IRQ_USBD, udc);
1976 err_map:
1977         iounmap(base_addr);
1978 err_mem:
1979         release_mem_region(rsrc_start, rsrc_len);
1980
1981         return retval;
1982 }
1983
1984 /*
1985  *      s3c2410_udc_remove
1986  */
1987 static int s3c2410_udc_remove(struct platform_device *pdev)
1988 {
1989         struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1990         unsigned int irq;
1991
1992         dev_dbg(&pdev->dev, "%s()\n", __func__);
1993
1994         usb_del_gadget_udc(&udc->gadget);
1995         if (udc->driver)
1996                 return -EBUSY;
1997
1998         debugfs_remove(udc->regs_info);
1999
2000         if (udc_info && !udc_info->udc_command &&
2001                 gpio_is_valid(udc_info->pullup_pin))
2002                 gpio_free(udc_info->pullup_pin);
2003
2004         if (udc_info && udc_info->vbus_pin > 0) {
2005                 irq = gpio_to_irq(udc_info->vbus_pin);
2006                 free_irq(irq, udc);
2007         }
2008
2009         free_irq(IRQ_USBD, udc);
2010
2011         iounmap(base_addr);
2012         release_mem_region(rsrc_start, rsrc_len);
2013
2014         platform_set_drvdata(pdev, NULL);
2015
2016         if (!IS_ERR(udc_clock) && udc_clock != NULL) {
2017                 clk_disable(udc_clock);
2018                 clk_put(udc_clock);
2019                 udc_clock = NULL;
2020         }
2021
2022         if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
2023                 clk_disable(usb_bus_clock);
2024                 clk_put(usb_bus_clock);
2025                 usb_bus_clock = NULL;
2026         }
2027
2028         dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
2029         return 0;
2030 }
2031
2032 #ifdef CONFIG_PM
2033 static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
2034 {
2035         s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
2036
2037         return 0;
2038 }
2039
2040 static int s3c2410_udc_resume(struct platform_device *pdev)
2041 {
2042         s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
2043
2044         return 0;
2045 }
2046 #else
2047 #define s3c2410_udc_suspend     NULL
2048 #define s3c2410_udc_resume      NULL
2049 #endif
2050
2051 static const struct platform_device_id s3c_udc_ids[] = {
2052         { "s3c2410-usbgadget", },
2053         { "s3c2440-usbgadget", },
2054         { }
2055 };
2056 MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
2057
2058 static struct platform_driver udc_driver_24x0 = {
2059         .driver         = {
2060                 .name   = "s3c24x0-usbgadget",
2061                 .owner  = THIS_MODULE,
2062         },
2063         .probe          = s3c2410_udc_probe,
2064         .remove         = s3c2410_udc_remove,
2065         .suspend        = s3c2410_udc_suspend,
2066         .resume         = s3c2410_udc_resume,
2067         .id_table       = s3c_udc_ids,
2068 };
2069
2070 static int __init udc_init(void)
2071 {
2072         int retval;
2073
2074         dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
2075
2076         s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
2077         if (IS_ERR(s3c2410_udc_debugfs_root)) {
2078                 pr_err("%s: debugfs dir creation failed %ld\n",
2079                         gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2080                 s3c2410_udc_debugfs_root = NULL;
2081         }
2082
2083         retval = platform_driver_register(&udc_driver_24x0);
2084         if (retval)
2085                 goto err;
2086
2087         return 0;
2088
2089 err:
2090         debugfs_remove(s3c2410_udc_debugfs_root);
2091         return retval;
2092 }
2093
2094 static void __exit udc_exit(void)
2095 {
2096         platform_driver_unregister(&udc_driver_24x0);
2097         debugfs_remove(s3c2410_udc_debugfs_root);
2098 }
2099
2100 module_init(udc_init);
2101 module_exit(udc_exit);
2102
2103 MODULE_AUTHOR(DRIVER_AUTHOR);
2104 MODULE_DESCRIPTION(DRIVER_DESC);
2105 MODULE_VERSION(DRIVER_VERSION);
2106 MODULE_LICENSE("GPL");