blob: eb3571ee59e3c4a454b87e2103a921ed88eea2f7 [file] [log] [blame]
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001/*
2 * linux/drivers/usb/gadget/s3c2410_udc.c
3 *
4 * Samsung S3C24xx series on-chip full speed USB device controllers
5 *
Al Virod36b6912011-12-29 17:09:01 -05006 * Copyright (C) 2004-2007 Herbert Pötzl - Arnaud Patard
Arnaud Patard3fc154b2007-06-06 21:05:49 -07007 * 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.
Arnaud Patard3fc154b2007-06-06 21:05:49 -070013 */
14
Sachin Kamatc2892cd2012-08-22 15:48:59 +053015#define pr_fmt(fmt) "s3c2410_udc: " fmt
16
Arnaud Patard3fc154b2007-06-06 21:05:49 -070017#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>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070023#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>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070029#include <linux/clk.h>
Ben Dooksea99ecf2008-11-24 11:45:03 -080030#include <linux/gpio.h>
Bryan Wub38b03b2011-06-02 12:51:29 +080031#include <linux/prefetch.h>
Sachin Kamat2e8e25b2012-08-22 15:48:58 +053032#include <linux/io.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070033
34#include <linux/debugfs.h>
35#include <linux/seq_file.h>
36
37#include <linux/usb.h>
David Brownell9454a572007-10-04 18:05:17 -070038#include <linux/usb/gadget.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070039
40#include <asm/byteorder.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070041#include <asm/irq.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070042#include <asm/unaligned.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/irqs.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070044
Russell Kinga09e64f2008-08-05 16:14:15 +010045#include <mach/hardware.h>
Ben Dooks899d5662007-11-19 22:28:13 +000046
Ben Dooks57bd4b92008-10-30 10:14:37 +000047#include <plat/regs-udc.h>
Arnd Bergmann436d42c2012-08-24 15:22:12 +020048#include <linux/platform_data/usb-s3c2410_udc.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -070049
Arnaud Patard3fc154b2007-06-06 21:05:49 -070050
51#include "s3c2410_udc.h"
52
53#define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
54#define DRIVER_VERSION "29 Apr 2007"
Al Virod36b6912011-12-29 17:09:01 -050055#define DRIVER_AUTHOR "Herbert Pötzl <herbert@13thfloor.at>, " \
Arnaud Patard3fc154b2007-06-06 21:05:49 -070056 "Arnaud Patard <arnaud.patard@rtp-net.org>"
57
58static const char gadget_name[] = "s3c2410_udc";
59static const char driver_desc[] = DRIVER_DESC;
60
61static struct s3c2410_udc *the_controller;
62static struct clk *udc_clock;
63static struct clk *usb_bus_clock;
64static void __iomem *base_addr;
65static u64 rsrc_start;
66static u64 rsrc_len;
67static struct dentry *s3c2410_udc_debugfs_root;
68
69static inline u32 udc_read(u32 reg)
70{
71 return readb(base_addr + reg);
72}
73
74static inline void udc_write(u32 value, u32 reg)
75{
76 writeb(value, base_addr + reg);
77}
78
79static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
80{
81 writeb(value, base + reg);
82}
83
84static 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
93static uint32_t s3c2410_ticks = 0;
94
Joe Perchesd74c23d2015-03-26 22:43:39 -070095__printf(2, 3)
96static void dprintk(int level, const char *fmt, ...)
Arnaud Patard3fc154b2007-06-06 21:05:49 -070097{
Arnaud Patard3fc154b2007-06-06 21:05:49 -070098 static long prevticks;
99 static int invocation;
Joe Perchesd74c23d2015-03-26 22:43:39 -0700100 struct va_format vaf;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700101 va_list args;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700102
103 if (level > USB_S3C2410_DEBUG_LEVEL)
Joe Perchesd74c23d2015-03-26 22:43:39 -0700104 return;
105
106 va_start(args, fmt);
107
108 vaf.fmt = fmt;
109 vaf.va = &args;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700110
111 if (s3c2410_ticks != prevticks) {
112 prevticks = s3c2410_ticks;
113 invocation = 0;
114 }
115
Joe Perchesd74c23d2015-03-26 22:43:39 -0700116 pr_debug("%1lu.%02d USB: %pV", prevticks, invocation++, &vaf);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700117
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700118 va_end(args);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700119}
120#else
Joe Perchesd74c23d2015-03-26 22:43:39 -0700121__printf(2, 3)
122static void dprintk(int level, const char *fmt, ...)
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700123{
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700124}
125#endif
Joe Perchesd74c23d2015-03-26 22:43:39 -0700126
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700127static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)
128{
Sachin Kamatff241662012-08-22 15:49:00 +0530129 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700130 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
Sachin Kamatff241662012-08-22 15:49:00 +0530131 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
132 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700133
134 addr_reg = udc_read(S3C2410_UDC_FUNC_ADDR_REG);
135 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
136 ep_int_reg = udc_read(S3C2410_UDC_EP_INT_REG);
137 usb_int_reg = udc_read(S3C2410_UDC_USB_INT_REG);
138 ep_int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
139 usb_int_en_reg = udc_read(S3C2410_UDC_USB_INT_EN_REG);
140 udc_write(0, S3C2410_UDC_INDEX_REG);
141 ep0_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
142 udc_write(1, S3C2410_UDC_INDEX_REG);
143 ep1_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
144 ep1_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
145 ep1_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
146 ep1_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
147 udc_write(2, S3C2410_UDC_INDEX_REG);
148 ep2_i_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
149 ep2_i_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
150 ep2_o_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
151 ep2_o_csr2 = udc_read(S3C2410_UDC_IN_CSR2_REG);
152
153 seq_printf(m, "FUNC_ADDR_REG : 0x%04X\n"
154 "PWR_REG : 0x%04X\n"
155 "EP_INT_REG : 0x%04X\n"
156 "USB_INT_REG : 0x%04X\n"
157 "EP_INT_EN_REG : 0x%04X\n"
158 "USB_INT_EN_REG : 0x%04X\n"
159 "EP0_CSR : 0x%04X\n"
160 "EP1_I_CSR1 : 0x%04X\n"
161 "EP1_I_CSR2 : 0x%04X\n"
162 "EP1_O_CSR1 : 0x%04X\n"
163 "EP1_O_CSR2 : 0x%04X\n"
164 "EP2_I_CSR1 : 0x%04X\n"
165 "EP2_I_CSR2 : 0x%04X\n"
166 "EP2_O_CSR1 : 0x%04X\n"
167 "EP2_O_CSR2 : 0x%04X\n",
Sachin Kamatff241662012-08-22 15:49:00 +0530168 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700169 ep_int_en_reg, usb_int_en_reg, ep0_csr,
Sachin Kamatff241662012-08-22 15:49:00 +0530170 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
171 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700172 );
173
174 return 0;
175}
176
177static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
178 struct file *file)
179{
180 return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);
181}
182
183static const struct file_operations s3c2410_udc_debugfs_fops = {
184 .open = s3c2410_udc_debugfs_fops_open,
185 .read = seq_read,
186 .llseek = seq_lseek,
187 .release = single_release,
188 .owner = THIS_MODULE,
189};
190
191/* io macros */
192
193static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
194{
195 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
196 udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
197 S3C2410_UDC_EP0_CSR_REG);
198}
199
200static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
201{
202 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
203 writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);
204}
205
206static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
207{
208 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
209 udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);
210}
211
212static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
213{
214 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
215 udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);
216}
217
218static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
219{
220 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
221 udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);
222}
223
224inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
225{
226 udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
227 udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);
228}
229
230static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
231{
232 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
233
Sachin Kamatff241662012-08-22 15:49:00 +0530234 udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700235 | S3C2410_UDC_EP0_CSR_DE),
236 S3C2410_UDC_EP0_CSR_REG);
237}
238
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700239static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
240{
241 udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
242 udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
243 | S3C2410_UDC_EP0_CSR_DE),
244 S3C2410_UDC_EP0_CSR_REG);
245}
246
247/*------------------------- I/O ----------------------------------*/
248
249/*
250 * s3c2410_udc_done
251 */
252static void s3c2410_udc_done(struct s3c2410_ep *ep,
253 struct s3c2410_request *req, int status)
254{
255 unsigned halted = ep->halted;
256
257 list_del_init(&req->queue);
258
Sachin Kamatff241662012-08-22 15:49:00 +0530259 if (likely(req->req.status == -EINPROGRESS))
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700260 req->req.status = status;
261 else
262 status = req->req.status;
263
264 ep->halted = 1;
Michal Sojka304f7e52014-09-24 22:43:19 +0200265 usb_gadget_giveback_request(&ep->ep, &req->req);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700266 ep->halted = halted;
267}
268
269static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
270 struct s3c2410_ep *ep, int status)
271{
272 /* Sanity check */
273 if (&ep->queue == NULL)
274 return;
275
Sachin Kamatff241662012-08-22 15:49:00 +0530276 while (!list_empty(&ep->queue)) {
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700277 struct s3c2410_request *req;
Sachin Kamatff241662012-08-22 15:49:00 +0530278 req = list_entry(ep->queue.next, struct s3c2410_request,
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700279 queue);
280 s3c2410_udc_done(ep, req, status);
281 }
282}
283
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700284static inline int s3c2410_udc_fifo_count_out(void)
285{
286 int tmp;
287
288 tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;
289 tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);
290 return tmp;
291}
292
293/*
294 * s3c2410_udc_write_packet
295 */
296static inline int s3c2410_udc_write_packet(int fifo,
297 struct s3c2410_request *req,
298 unsigned max)
299{
300 unsigned len = min(req->req.length - req->req.actual, max);
301 u8 *buf = req->req.buf + req->req.actual;
302
303 prefetch(buf);
304
305 dprintk(DEBUG_VERBOSE, "%s %d %d %d %d\n", __func__,
306 req->req.actual, req->req.length, len, req->req.actual + len);
307
308 req->req.actual += len;
309
310 udelay(5);
311 writesb(base_addr + fifo, buf, len);
312 return len;
313}
314
315/*
316 * s3c2410_udc_write_fifo
317 *
318 * return: 0 = still running, 1 = completed, negative = errno
319 */
320static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
321 struct s3c2410_request *req)
322{
323 unsigned count;
324 int is_last;
325 u32 idx;
326 int fifo_reg;
327 u32 ep_csr;
328
329 idx = ep->bEndpointAddress & 0x7F;
330 switch (idx) {
331 default:
332 idx = 0;
333 case 0:
334 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
335 break;
336 case 1:
337 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
338 break;
339 case 2:
340 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
341 break;
342 case 3:
343 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
344 break;
345 case 4:
346 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
347 break;
348 }
349
350 count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);
351
352 /* last packet is often short (sometimes a zlp) */
353 if (count != ep->ep.maxpacket)
354 is_last = 1;
355 else if (req->req.length != req->req.actual || req->req.zero)
356 is_last = 0;
357 else
358 is_last = 2;
359
360 /* Only ep0 debug messages are interesting */
361 if (idx == 0)
362 dprintk(DEBUG_NORMAL,
363 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
364 idx, count, req->req.actual, req->req.length,
365 is_last, req->req.zero);
366
367 if (is_last) {
368 /* The order is important. It prevents sending 2 packets
369 * at the same time */
370
371 if (idx == 0) {
372 /* Reset signal => no need to say 'data sent' */
Sachin Kamatff241662012-08-22 15:49:00 +0530373 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700374 & S3C2410_UDC_USBINT_RESET))
375 s3c2410_udc_set_ep0_de_in(base_addr);
Sachin Kamatff241662012-08-22 15:49:00 +0530376 ep->dev->ep0state = EP0_IDLE;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700377 } else {
378 udc_write(idx, S3C2410_UDC_INDEX_REG);
379 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
380 udc_write(idx, S3C2410_UDC_INDEX_REG);
381 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
382 S3C2410_UDC_IN_CSR1_REG);
383 }
384
385 s3c2410_udc_done(ep, req, 0);
386 is_last = 1;
387 } else {
388 if (idx == 0) {
389 /* Reset signal => no need to say 'data sent' */
Sachin Kamatff241662012-08-22 15:49:00 +0530390 if (!(udc_read(S3C2410_UDC_USB_INT_REG)
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700391 & S3C2410_UDC_USBINT_RESET))
392 s3c2410_udc_set_ep0_ipr(base_addr);
393 } else {
394 udc_write(idx, S3C2410_UDC_INDEX_REG);
395 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
396 udc_write(idx, S3C2410_UDC_INDEX_REG);
397 udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
398 S3C2410_UDC_IN_CSR1_REG);
399 }
400 }
401
402 return is_last;
403}
404
405static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
406 struct s3c2410_request *req, unsigned avail)
407{
408 unsigned len;
409
410 len = min(req->req.length - req->req.actual, avail);
411 req->req.actual += len;
412
413 readsb(fifo + base_addr, buf, len);
414 return len;
415}
416
417/*
418 * return: 0 = still running, 1 = queue empty, negative = errno
419 */
420static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
421 struct s3c2410_request *req)
422{
423 u8 *buf;
424 u32 ep_csr;
425 unsigned bufferspace;
Sachin Kamatff241662012-08-22 15:49:00 +0530426 int is_last = 1;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700427 unsigned avail;
428 int fifo_count = 0;
429 u32 idx;
430 int fifo_reg;
431
432 idx = ep->bEndpointAddress & 0x7F;
433
434 switch (idx) {
435 default:
436 idx = 0;
437 case 0:
438 fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
439 break;
440 case 1:
441 fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
442 break;
443 case 2:
444 fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
445 break;
446 case 3:
447 fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
448 break;
449 case 4:
450 fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
451 break;
452 }
453
454 if (!req->req.length)
455 return 1;
456
457 buf = req->req.buf + req->req.actual;
458 bufferspace = req->req.length - req->req.actual;
459 if (!bufferspace) {
460 dprintk(DEBUG_NORMAL, "%s: buffer full!\n", __func__);
461 return -1;
462 }
463
464 udc_write(idx, S3C2410_UDC_INDEX_REG);
465
466 fifo_count = s3c2410_udc_fifo_count_out();
467 dprintk(DEBUG_NORMAL, "%s fifo count : %d\n", __func__, fifo_count);
468
469 if (fifo_count > ep->ep.maxpacket)
470 avail = ep->ep.maxpacket;
471 else
472 avail = fifo_count;
473
474 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
475
476 /* checking this with ep0 is not accurate as we already
477 * read a control request
478 **/
479 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
480 is_last = 1;
481 /* overflowed this request? flush extra data */
482 if (fifo_count != avail)
483 req->req.status = -EOVERFLOW;
484 } else {
485 is_last = (req->req.length <= req->req.actual) ? 1 : 0;
486 }
487
488 udc_write(idx, S3C2410_UDC_INDEX_REG);
489 fifo_count = s3c2410_udc_fifo_count_out();
490
491 /* Only ep0 debug messages are interesting */
492 if (idx == 0)
493 dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
Sachin Kamatff241662012-08-22 15:49:00 +0530494 __func__, fifo_count, is_last);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700495
496 if (is_last) {
497 if (idx == 0) {
498 s3c2410_udc_set_ep0_de_out(base_addr);
499 ep->dev->ep0state = EP0_IDLE;
500 } else {
501 udc_write(idx, S3C2410_UDC_INDEX_REG);
502 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
503 udc_write(idx, S3C2410_UDC_INDEX_REG);
504 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
505 S3C2410_UDC_OUT_CSR1_REG);
506 }
507
508 s3c2410_udc_done(ep, req, 0);
509 } else {
510 if (idx == 0) {
511 s3c2410_udc_clear_ep0_opr(base_addr);
512 } else {
513 udc_write(idx, S3C2410_UDC_INDEX_REG);
514 ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
515 udc_write(idx, S3C2410_UDC_INDEX_REG);
516 udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
517 S3C2410_UDC_OUT_CSR1_REG);
518 }
519 }
520
521 return is_last;
522}
523
524static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
525{
Sachin Kamatff241662012-08-22 15:49:00 +0530526 unsigned char *outbuf = (unsigned char *)crq;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700527 int bytes_read = 0;
528
529 udc_write(0, S3C2410_UDC_INDEX_REG);
530
531 bytes_read = s3c2410_udc_fifo_count_out();
532
533 dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
534
535 if (bytes_read > sizeof(struct usb_ctrlrequest))
536 bytes_read = sizeof(struct usb_ctrlrequest);
537
538 readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);
539
540 dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
541 bytes_read, crq->bRequest, crq->bRequestType,
542 crq->wValue, crq->wIndex, crq->wLength);
543
544 return bytes_read;
545}
546
547static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
548 struct usb_ctrlrequest *crq)
549{
550 u16 status = 0;
551 u8 ep_num = crq->wIndex & 0x7F;
552 u8 is_in = crq->wIndex & USB_DIR_IN;
553
554 switch (crq->bRequestType & USB_RECIP_MASK) {
555 case USB_RECIP_INTERFACE:
556 break;
557
558 case USB_RECIP_DEVICE:
559 status = dev->devstatus;
560 break;
561
562 case USB_RECIP_ENDPOINT:
563 if (ep_num > 4 || crq->wLength > 2)
564 return 1;
565
566 if (ep_num == 0) {
567 udc_write(0, S3C2410_UDC_INDEX_REG);
568 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
569 status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
570 } else {
571 udc_write(ep_num, S3C2410_UDC_INDEX_REG);
572 if (is_in) {
573 status = udc_read(S3C2410_UDC_IN_CSR1_REG);
574 status = status & S3C2410_UDC_ICSR1_SENDSTL;
575 } else {
576 status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
577 status = status & S3C2410_UDC_OCSR1_SENDSTL;
578 }
579 }
580
581 status = status ? 1 : 0;
582 break;
583
584 default:
585 return 1;
586 }
587
588 /* Seems to be needed to get it working. ouch :( */
589 udelay(5);
590 udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
591 udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
592 s3c2410_udc_set_ep0_de_in(base_addr);
593
594 return 0;
595}
596/*------------------------- usb state machine -------------------------------*/
597static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
598
599static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
600 struct s3c2410_ep *ep,
601 struct usb_ctrlrequest *crq,
602 u32 ep0csr)
603{
604 int len, ret, tmp;
605
606 /* start control request? */
607 if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))
608 return;
609
610 s3c2410_udc_nuke(dev, ep, -EPROTO);
611
612 len = s3c2410_udc_read_fifo_crq(crq);
613 if (len != sizeof(*crq)) {
614 dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
615 " wanted %d bytes got %d. Stalling out...\n",
616 sizeof(*crq), len);
617 s3c2410_udc_set_ep0_ss(base_addr);
618 return;
619 }
620
621 dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
622 crq->bRequest, crq->bRequestType, crq->wLength);
623
624 /* cope with automagic for some standard requests. */
625 dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
626 == USB_TYPE_STANDARD;
627 dev->req_config = 0;
628 dev->req_pending = 1;
629
630 switch (crq->bRequest) {
631 case USB_REQ_SET_CONFIGURATION:
Sachin Kamatff241662012-08-22 15:49:00 +0530632 dprintk(DEBUG_NORMAL, "USB_REQ_SET_CONFIGURATION ...\n");
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700633
634 if (crq->bRequestType == USB_RECIP_DEVICE) {
635 dev->req_config = 1;
636 s3c2410_udc_set_ep0_de_out(base_addr);
637 }
638 break;
639
640 case USB_REQ_SET_INTERFACE:
Sachin Kamatff241662012-08-22 15:49:00 +0530641 dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ...\n");
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700642
643 if (crq->bRequestType == USB_RECIP_INTERFACE) {
644 dev->req_config = 1;
645 s3c2410_udc_set_ep0_de_out(base_addr);
646 }
647 break;
648
649 case USB_REQ_SET_ADDRESS:
Sachin Kamatff241662012-08-22 15:49:00 +0530650 dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ...\n");
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700651
652 if (crq->bRequestType == USB_RECIP_DEVICE) {
653 tmp = crq->wValue & 0x7F;
654 dev->address = tmp;
655 udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
656 S3C2410_UDC_FUNC_ADDR_REG);
657 s3c2410_udc_set_ep0_de_out(base_addr);
658 return;
659 }
660 break;
661
662 case USB_REQ_GET_STATUS:
Sachin Kamatff241662012-08-22 15:49:00 +0530663 dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ...\n");
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700664 s3c2410_udc_clear_ep0_opr(base_addr);
665
666 if (dev->req_std) {
Sachin Kamatff241662012-08-22 15:49:00 +0530667 if (!s3c2410_udc_get_status(dev, crq))
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700668 return;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700669 }
670 break;
671
672 case USB_REQ_CLEAR_FEATURE:
673 s3c2410_udc_clear_ep0_opr(base_addr);
674
675 if (crq->bRequestType != USB_RECIP_ENDPOINT)
676 break;
677
678 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
679 break;
680
681 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
682 s3c2410_udc_set_ep0_de_out(base_addr);
683 return;
684
685 case USB_REQ_SET_FEATURE:
686 s3c2410_udc_clear_ep0_opr(base_addr);
687
688 if (crq->bRequestType != USB_RECIP_ENDPOINT)
689 break;
690
691 if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
692 break;
693
694 s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
695 s3c2410_udc_set_ep0_de_out(base_addr);
696 return;
697
698 default:
699 s3c2410_udc_clear_ep0_opr(base_addr);
700 break;
701 }
702
703 if (crq->bRequestType & USB_DIR_IN)
704 dev->ep0state = EP0_IN_DATA_PHASE;
705 else
706 dev->ep0state = EP0_OUT_DATA_PHASE;
707
Vladimir Zapolskiy00c05aa2010-06-29 23:36:26 +0400708 if (!dev->driver)
709 return;
710
711 /* deliver the request to the gadget driver */
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700712 ret = dev->driver->setup(&dev->gadget, crq);
713 if (ret < 0) {
714 if (dev->req_config) {
715 dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
716 crq->bRequest, ret);
717 return;
718 }
719
720 if (ret == -EOPNOTSUPP)
721 dprintk(DEBUG_NORMAL, "Operation not supported\n");
722 else
723 dprintk(DEBUG_NORMAL,
724 "dev->driver->setup failed. (%d)\n", ret);
725
726 udelay(5);
727 s3c2410_udc_set_ep0_ss(base_addr);
728 s3c2410_udc_set_ep0_de_out(base_addr);
729 dev->ep0state = EP0_IDLE;
730 /* deferred i/o == no response yet */
731 } else if (dev->req_pending) {
732 dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
Sachin Kamatff241662012-08-22 15:49:00 +0530733 dev->req_pending = 0;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700734 }
735
736 dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
737}
738
739static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
740{
741 u32 ep0csr;
742 struct s3c2410_ep *ep = &dev->ep[0];
743 struct s3c2410_request *req;
744 struct usb_ctrlrequest crq;
745
746 if (list_empty(&ep->queue))
747 req = NULL;
748 else
749 req = list_entry(ep->queue.next, struct s3c2410_request, queue);
750
751 /* We make the assumption that S3C2410_UDC_IN_CSR1_REG equal to
752 * S3C2410_UDC_EP0_CSR_REG when index is zero */
753
754 udc_write(0, S3C2410_UDC_INDEX_REG);
755 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
756
757 dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
758 ep0csr, ep0states[dev->ep0state]);
759
760 /* clear stall status */
761 if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {
762 s3c2410_udc_nuke(dev, ep, -EPIPE);
763 dprintk(DEBUG_NORMAL, "... clear SENT_STALL ...\n");
764 s3c2410_udc_clear_ep0_sst(base_addr);
765 dev->ep0state = EP0_IDLE;
766 return;
767 }
768
769 /* clear setup end */
770 if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {
771 dprintk(DEBUG_NORMAL, "... serviced SETUP_END ...\n");
772 s3c2410_udc_nuke(dev, ep, 0);
773 s3c2410_udc_clear_ep0_se(base_addr);
774 dev->ep0state = EP0_IDLE;
775 }
776
777 switch (dev->ep0state) {
778 case EP0_IDLE:
779 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
780 break;
781
782 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
783 dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
Sachin Kamatff241662012-08-22 15:49:00 +0530784 if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700785 s3c2410_udc_write_fifo(ep, req);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700786 break;
787
788 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
789 dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
Sachin Kamatff241662012-08-22 15:49:00 +0530790 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
791 s3c2410_udc_read_fifo(ep, req);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700792 break;
793
794 case EP0_END_XFER:
795 dprintk(DEBUG_NORMAL, "EP0_END_XFER ... what now?\n");
796 dev->ep0state = EP0_IDLE;
797 break;
798
799 case EP0_STALL:
800 dprintk(DEBUG_NORMAL, "EP0_STALL ... what now?\n");
801 dev->ep0state = EP0_IDLE;
802 break;
803 }
804}
805
806/*
807 * handle_ep - Manage I/O endpoints
808 */
809
810static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
811{
812 struct s3c2410_request *req;
813 int is_in = ep->bEndpointAddress & USB_DIR_IN;
814 u32 ep_csr1;
815 u32 idx;
816
Sachin Kamatff241662012-08-22 15:49:00 +0530817 if (likely(!list_empty(&ep->queue)))
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700818 req = list_entry(ep->queue.next,
819 struct s3c2410_request, queue);
820 else
821 req = NULL;
822
823 idx = ep->bEndpointAddress & 0x7F;
824
825 if (is_in) {
826 udc_write(idx, S3C2410_UDC_INDEX_REG);
827 ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);
828 dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
829 idx, ep_csr1, req ? 1 : 0);
830
831 if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {
832 dprintk(DEBUG_VERBOSE, "st\n");
833 udc_write(idx, S3C2410_UDC_INDEX_REG);
834 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
835 S3C2410_UDC_IN_CSR1_REG);
836 return;
837 }
838
Sachin Kamatff241662012-08-22 15:49:00 +0530839 if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req)
840 s3c2410_udc_write_fifo(ep, req);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700841 } else {
842 udc_write(idx, S3C2410_UDC_INDEX_REG);
843 ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);
844 dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
845
846 if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
847 udc_write(idx, S3C2410_UDC_INDEX_REG);
848 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
849 S3C2410_UDC_OUT_CSR1_REG);
850 return;
851 }
852
Sachin Kamatff241662012-08-22 15:49:00 +0530853 if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req)
854 s3c2410_udc_read_fifo(ep, req);
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700855 }
856}
857
Russell Kinga09e64f2008-08-05 16:14:15 +0100858#include <mach/regs-irq.h>
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700859
860/*
861 * s3c2410_udc_irq - interrupt handler
862 */
Jeff Garzikd8d42232007-11-21 15:13:10 -0800863static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700864{
865 struct s3c2410_udc *dev = _dev;
866 int usb_status;
867 int usbd_status;
868 int pwr_reg;
869 int ep0csr;
870 int i;
Fabian Godehardt16f08a02011-02-07 12:53:05 +0200871 u32 idx, idx2;
Arnaud Patard3fc154b2007-06-06 21:05:49 -0700872 unsigned long flags;
873
874 spin_lock_irqsave(&dev->lock, flags);
875
876 /* Driver connected ? */
877 if (!dev->driver) {
878 /* Clear interrupts */
879 udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
880 S3C2410_UDC_USB_INT_REG);
881 udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
882 S3C2410_UDC_EP_INT_REG);
883 }
884
885 /* Save index */
886 idx = udc_read(S3C2410_UDC_INDEX_REG);
887
888 /* Read status registers */
889 usb_status = udc_read(S3C2410_UDC_USB_INT_REG);
890 usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);
891 pwr_reg = udc_read(S3C2410_UDC_PWR_REG);
892
893 udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);
894 ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
895
896 dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
897 usb_status, usbd_status, pwr_reg, ep0csr);
898
899 /*
900 * Now, handle interrupts. There's two types :
901 * - Reset, Resume, Suspend coming -> usb_int_reg
902 * - EP -> ep_int_reg
903 */
904
905 /* RESET */
906 if (usb_status & S3C2410_UDC_USBINT_RESET) {
907 /* two kind of reset :
908 * - reset start -> pwr reg = 8
909 * - reset end -> pwr reg = 0
910 **/
911 dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
912 ep0csr, pwr_reg);
913
914 dev->gadget.speed = USB_SPEED_UNKNOWN;
915 udc_write(0x00, S3C2410_UDC_INDEX_REG);
916 udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
917 S3C2410_UDC_MAXP_REG);
918 dev->address = 0;
919
920 dev->ep0state = EP0_IDLE;
921 dev->gadget.speed = USB_SPEED_FULL;
922
923 /* clear interrupt */
924 udc_write(S3C2410_UDC_USBINT_RESET,
925 S3C2410_UDC_USB_INT_REG);
926
927 udc_write(idx, S3C2410_UDC_INDEX_REG);
928 spin_unlock_irqrestore(&dev->lock, flags);
929 return IRQ_HANDLED;
930 }
931
932 /* RESUME */
933 if (usb_status & S3C2410_UDC_USBINT_RESUME) {
934 dprintk(DEBUG_NORMAL, "USB resume\n");
935
936 /* clear interrupt */
937 udc_write(S3C2410_UDC_USBINT_RESUME,
938 S3C2410_UDC_USB_INT_REG);
939
940 if (dev->gadget.speed != USB_SPEED_UNKNOWN
941 && dev->driver
942 && dev->driver->resume)
943 dev->driver->resume(&dev->gadget);
944 }
945
946 /* SUSPEND */
947 if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
948 dprintk(DEBUG_NORMAL, "USB suspend\n");
949
950 /* clear interrupt */
951 udc_write(S3C2410_UDC_USBINT_SUSPEND,
952 S3C2410_UDC_USB_INT_REG);
953
954 if (dev->gadget.speed != USB_SPEED_UNKNOWN
955 && dev->driver
956 && dev->driver->suspend)
957 dev->driver->suspend(&dev->gadget);
958
959 dev->ep0state = EP0_IDLE;
960 }
961
962 /* EP */
963 /* control traffic */
964 /* check on ep0csr != 0 is not a good idea as clearing in_pkt_ready
965 * generate an interrupt
966 */
967 if (usbd_status & S3C2410_UDC_INT_EP0) {
968 dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
969 /* Clear the interrupt bit by setting it to 1 */
970 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
971 s3c2410_udc_handle_ep0(dev);
972 }
973
974 /* endpoint data transfers */
975 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
976 u32 tmp = 1 << i;
977 if (usbd_status & tmp) {
978 dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
979
980 /* Clear the interrupt bit by setting it to 1 */
981 udc_write(tmp, S3C2410_UDC_EP_INT_REG);
982 s3c2410_udc_handle_ep(&dev->ep[i]);
983 }
984 }
985
Fabian Godehardt16f08a02011-02-07 12:53:05 +0200986 /* what else causes this interrupt? a receive! who is it? */
987 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
988 for (i = 1; i < S3C2410_ENDPOINTS; i++) {
989 idx2 = udc_read(S3C2410_UDC_INDEX_REG);
990 udc_write(i, S3C2410_UDC_INDEX_REG);
991
992 if (udc_read(S3C2410_UDC_OUT_CSR1_REG) & 0x1)
993 s3c2410_udc_handle_ep(&dev->ep[i]);
994
995 /* restore index */
996 udc_write(idx2, S3C2410_UDC_INDEX_REG);
997 }
998 }
999
Jeff Garzikd8d42232007-11-21 15:13:10 -08001000 dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001001
1002 /* Restore old index */
1003 udc_write(idx, S3C2410_UDC_INDEX_REG);
1004
1005 spin_unlock_irqrestore(&dev->lock, flags);
1006
1007 return IRQ_HANDLED;
1008}
1009/*------------------------- s3c2410_ep_ops ----------------------------------*/
1010
1011static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
1012{
1013 return container_of(ep, struct s3c2410_ep, ep);
1014}
1015
1016static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
1017{
1018 return container_of(gadget, struct s3c2410_udc, gadget);
1019}
1020
1021static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
1022{
1023 return container_of(req, struct s3c2410_request, req);
1024}
1025
1026/*
1027 * s3c2410_udc_ep_enable
1028 */
1029static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
1030 const struct usb_endpoint_descriptor *desc)
1031{
1032 struct s3c2410_udc *dev;
1033 struct s3c2410_ep *ep;
1034 u32 max, tmp;
1035 unsigned long flags;
Sachin Kamatff241662012-08-22 15:49:00 +05301036 u32 csr1, csr2;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001037 u32 int_en_reg;
1038
1039 ep = to_s3c2410_ep(_ep);
1040
Ido Shayevitz0561ed42012-06-04 13:35:27 +03001041 if (!_ep || !desc
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001042 || _ep->name == ep0name
1043 || desc->bDescriptorType != USB_DT_ENDPOINT)
1044 return -EINVAL;
1045
1046 dev = ep->dev;
1047 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1048 return -ESHUTDOWN;
1049
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001050 max = usb_endpoint_maxp(desc) & 0x1fff;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001051
Sachin Kamatff241662012-08-22 15:49:00 +05301052 local_irq_save(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001053 _ep->maxpacket = max & 0x7ff;
Ido Shayevitzfa42e522012-03-12 20:25:38 +02001054 ep->ep.desc = desc;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001055 ep->halted = 0;
1056 ep->bEndpointAddress = desc->bEndpointAddress;
1057
1058 /* set max packet */
1059 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1060 udc_write(max >> 3, S3C2410_UDC_MAXP_REG);
1061
1062 /* set type, direction, address; reset fifo counters */
1063 if (desc->bEndpointAddress & USB_DIR_IN) {
1064 csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
1065 csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
1066
1067 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1068 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1069 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1070 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1071 } else {
1072 /* don't flush in fifo or it will cause endpoint interrupt */
1073 csr1 = S3C2410_UDC_ICSR1_CLRDT;
1074 csr2 = S3C2410_UDC_ICSR2_DMAIEN;
1075
1076 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1077 udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);
1078 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1079 udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);
1080
1081 csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
1082 csr2 = S3C2410_UDC_OCSR2_DMAIEN;
1083
1084 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1085 udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);
1086 udc_write(ep->num, S3C2410_UDC_INDEX_REG);
1087 udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);
1088 }
1089
1090 /* enable irqs */
1091 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1092 udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);
1093
1094 /* print some debug message */
1095 tmp = desc->bEndpointAddress;
Sachin Kamatff241662012-08-22 15:49:00 +05301096 dprintk(DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
1097 _ep->name, ep->num, tmp,
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001098 desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
1099
Sachin Kamatff241662012-08-22 15:49:00 +05301100 local_irq_restore(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001101 s3c2410_udc_set_halt(_ep, 0);
1102
1103 return 0;
1104}
1105
1106/*
1107 * s3c2410_udc_ep_disable
1108 */
1109static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
1110{
1111 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1112 unsigned long flags;
1113 u32 int_en_reg;
1114
Ido Shayevitzfa42e522012-03-12 20:25:38 +02001115 if (!_ep || !ep->ep.desc) {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001116 dprintk(DEBUG_NORMAL, "%s not enabled\n",
1117 _ep ? ep->ep.name : NULL);
1118 return -EINVAL;
1119 }
1120
1121 local_irq_save(flags);
1122
1123 dprintk(DEBUG_NORMAL, "ep_disable: %s\n", _ep->name);
1124
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001125 ep->ep.desc = NULL;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001126 ep->halted = 1;
1127
Sachin Kamatff241662012-08-22 15:49:00 +05301128 s3c2410_udc_nuke(ep->dev, ep, -ESHUTDOWN);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001129
1130 /* disable irqs */
1131 int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
1132 udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);
1133
1134 local_irq_restore(flags);
1135
1136 dprintk(DEBUG_NORMAL, "%s disabled\n", _ep->name);
1137
1138 return 0;
1139}
1140
1141/*
1142 * s3c2410_udc_alloc_request
1143 */
1144static struct usb_request *
1145s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
1146{
1147 struct s3c2410_request *req;
1148
Sachin Kamatff241662012-08-22 15:49:00 +05301149 dprintk(DEBUG_VERBOSE, "%s(%p,%d)\n", __func__, _ep, mem_flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001150
1151 if (!_ep)
1152 return NULL;
1153
Sachin Kamatff241662012-08-22 15:49:00 +05301154 req = kzalloc(sizeof(struct s3c2410_request), mem_flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001155 if (!req)
1156 return NULL;
1157
Sachin Kamatff241662012-08-22 15:49:00 +05301158 INIT_LIST_HEAD(&req->queue);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001159 return &req->req;
1160}
1161
1162/*
1163 * s3c2410_udc_free_request
1164 */
1165static void
1166s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
1167{
1168 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1169 struct s3c2410_request *req = to_s3c2410_req(_req);
1170
1171 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1172
Ido Shayevitzfa42e522012-03-12 20:25:38 +02001173 if (!ep || !_req || (!ep->ep.desc && _ep->name != ep0name))
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001174 return;
1175
Sachin Kamatff241662012-08-22 15:49:00 +05301176 WARN_ON(!list_empty(&req->queue));
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001177 kfree(req);
1178}
1179
1180/*
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001181 * s3c2410_udc_queue
1182 */
1183static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
1184 gfp_t gfp_flags)
1185{
1186 struct s3c2410_request *req = to_s3c2410_req(_req);
1187 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1188 struct s3c2410_udc *dev;
1189 u32 ep_csr = 0;
1190 int fifo_count = 0;
1191 unsigned long flags;
1192
Ido Shayevitzfa42e522012-03-12 20:25:38 +02001193 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001194 dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
1195 return -EINVAL;
1196 }
1197
1198 dev = ep->dev;
Sachin Kamatff241662012-08-22 15:49:00 +05301199 if (unlikely(!dev->driver
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001200 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1201 return -ESHUTDOWN;
1202 }
1203
Sachin Kamatff241662012-08-22 15:49:00 +05301204 local_irq_save(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001205
1206 if (unlikely(!_req || !_req->complete
1207 || !_req->buf || !list_empty(&req->queue))) {
1208 if (!_req)
1209 dprintk(DEBUG_NORMAL, "%s: 1 X X X\n", __func__);
1210 else {
1211 dprintk(DEBUG_NORMAL, "%s: 0 %01d %01d %01d\n",
Sachin Kamatff241662012-08-22 15:49:00 +05301212 __func__, !_req->complete, !_req->buf,
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001213 !list_empty(&req->queue));
1214 }
1215
1216 local_irq_restore(flags);
1217 return -EINVAL;
1218 }
1219
1220 _req->status = -EINPROGRESS;
1221 _req->actual = 0;
1222
1223 dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
1224 __func__, ep->bEndpointAddress, _req->length);
1225
1226 if (ep->bEndpointAddress) {
1227 udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);
1228
1229 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
1230 ? S3C2410_UDC_IN_CSR1_REG
1231 : S3C2410_UDC_OUT_CSR1_REG);
1232 fifo_count = s3c2410_udc_fifo_count_out();
1233 } else {
1234 udc_write(0, S3C2410_UDC_INDEX_REG);
1235 ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);
1236 fifo_count = s3c2410_udc_fifo_count_out();
1237 }
1238
1239 /* kickstart this i/o queue? */
1240 if (list_empty(&ep->queue) && !ep->halted) {
1241 if (ep->bEndpointAddress == 0 /* ep0 */) {
1242 switch (dev->ep0state) {
1243 case EP0_IN_DATA_PHASE:
1244 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1245 && s3c2410_udc_write_fifo(ep,
1246 req)) {
1247 dev->ep0state = EP0_IDLE;
1248 req = NULL;
1249 }
1250 break;
1251
1252 case EP0_OUT_DATA_PHASE:
1253 if ((!_req->length)
1254 || ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1255 && s3c2410_udc_read_fifo(ep,
1256 req))) {
1257 dev->ep0state = EP0_IDLE;
1258 req = NULL;
1259 }
1260 break;
1261
1262 default:
1263 local_irq_restore(flags);
1264 return -EL2HLT;
1265 }
1266 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1267 && (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
1268 && s3c2410_udc_write_fifo(ep, req)) {
1269 req = NULL;
1270 } else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1271 && fifo_count
1272 && s3c2410_udc_read_fifo(ep, req)) {
1273 req = NULL;
1274 }
1275 }
1276
1277 /* pio or dma irq handler advances the queue. */
Sachin Kamat86bab362012-08-22 15:49:02 +05301278 if (likely(req))
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001279 list_add_tail(&req->queue, &ep->queue);
1280
1281 local_irq_restore(flags);
1282
1283 dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
1284 return 0;
1285}
1286
1287/*
1288 * s3c2410_udc_dequeue
1289 */
1290static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1291{
1292 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1293 struct s3c2410_udc *udc;
1294 int retval = -EINVAL;
1295 unsigned long flags;
1296 struct s3c2410_request *req = NULL;
1297
1298 dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
1299
1300 if (!the_controller->driver)
1301 return -ESHUTDOWN;
1302
1303 if (!_ep || !_req)
1304 return retval;
1305
1306 udc = to_s3c2410_udc(ep->gadget);
1307
Sachin Kamatff241662012-08-22 15:49:00 +05301308 local_irq_save(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001309
Sachin Kamatff241662012-08-22 15:49:00 +05301310 list_for_each_entry(req, &ep->queue, queue) {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001311 if (&req->req == _req) {
Sachin Kamatff241662012-08-22 15:49:00 +05301312 list_del_init(&req->queue);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001313 _req->status = -ECONNRESET;
1314 retval = 0;
1315 break;
1316 }
1317 }
1318
1319 if (retval == 0) {
1320 dprintk(DEBUG_VERBOSE,
1321 "dequeued req %p from %s, len %d buf %p\n",
1322 req, _ep->name, _req->length, _req->buf);
1323
1324 s3c2410_udc_done(ep, req, -ECONNRESET);
1325 }
1326
Sachin Kamatff241662012-08-22 15:49:00 +05301327 local_irq_restore(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001328 return retval;
1329}
1330
1331/*
1332 * s3c2410_udc_set_halt
1333 */
1334static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
1335{
1336 struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
1337 u32 ep_csr = 0;
1338 unsigned long flags;
1339 u32 idx;
1340
Ido Shayevitzfa42e522012-03-12 20:25:38 +02001341 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001342 dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
1343 return -EINVAL;
1344 }
1345
Sachin Kamatff241662012-08-22 15:49:00 +05301346 local_irq_save(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001347
1348 idx = ep->bEndpointAddress & 0x7F;
1349
1350 if (idx == 0) {
1351 s3c2410_udc_set_ep0_ss(base_addr);
1352 s3c2410_udc_set_ep0_de_out(base_addr);
1353 } else {
1354 udc_write(idx, S3C2410_UDC_INDEX_REG);
Sachin Kamatff241662012-08-22 15:49:00 +05301355 ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001356 ? S3C2410_UDC_IN_CSR1_REG
1357 : S3C2410_UDC_OUT_CSR1_REG);
1358
1359 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
1360 if (value)
1361 udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
1362 S3C2410_UDC_IN_CSR1_REG);
1363 else {
1364 ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
1365 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1366 ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
1367 udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);
1368 }
1369 } else {
1370 if (value)
1371 udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
1372 S3C2410_UDC_OUT_CSR1_REG);
1373 else {
1374 ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
1375 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1376 ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
1377 udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);
1378 }
1379 }
1380 }
1381
1382 ep->halted = value ? 1 : 0;
Sachin Kamatff241662012-08-22 15:49:00 +05301383 local_irq_restore(flags);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001384
1385 return 0;
1386}
1387
1388static const struct usb_ep_ops s3c2410_ep_ops = {
1389 .enable = s3c2410_udc_ep_enable,
1390 .disable = s3c2410_udc_ep_disable,
1391
1392 .alloc_request = s3c2410_udc_alloc_request,
1393 .free_request = s3c2410_udc_free_request,
1394
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001395 .queue = s3c2410_udc_queue,
1396 .dequeue = s3c2410_udc_dequeue,
1397
1398 .set_halt = s3c2410_udc_set_halt,
1399};
1400
1401/*------------------------- usb_gadget_ops ----------------------------------*/
1402
1403/*
1404 * s3c2410_udc_get_frame
1405 */
1406static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
1407{
1408 int tmp;
1409
1410 dprintk(DEBUG_VERBOSE, "%s()\n", __func__);
1411
1412 tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
1413 tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
1414 return tmp;
1415}
1416
1417/*
1418 * s3c2410_udc_wakeup
1419 */
1420static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
1421{
1422 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1423 return 0;
1424}
1425
1426/*
1427 * s3c2410_udc_set_selfpowered
1428 */
1429static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
1430{
1431 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1432
1433 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1434
Peter Chenb3764dd2015-01-28 16:32:36 +08001435 gadget->is_selfpowered = (value != 0);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001436 if (value)
1437 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
1438 else
1439 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1440
1441 return 0;
1442}
1443
1444static void s3c2410_udc_disable(struct s3c2410_udc *dev);
1445static void s3c2410_udc_enable(struct s3c2410_udc *dev);
1446
1447static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
1448{
1449 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1450
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001451 if (udc_info && (udc_info->udc_command ||
1452 gpio_is_valid(udc_info->pullup_pin))) {
1453
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001454 if (is_on)
1455 s3c2410_udc_enable(udc);
1456 else {
1457 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1458 if (udc->driver && udc->driver->disconnect)
1459 udc->driver->disconnect(&udc->gadget);
1460
1461 }
1462 s3c2410_udc_disable(udc);
1463 }
Sachin Kamatff241662012-08-22 15:49:00 +05301464 } else {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001465 return -EOPNOTSUPP;
Sachin Kamatff241662012-08-22 15:49:00 +05301466 }
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001467
1468 return 0;
1469}
1470
1471static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
1472{
1473 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1474
1475 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1476
1477 udc->vbus = (is_active != 0);
1478 s3c2410_udc_set_pullup(udc, is_active);
1479 return 0;
1480}
1481
1482static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
1483{
1484 struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
1485
1486 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1487
Sergiy Kibrik10f09582015-05-16 16:55:03 +03001488 s3c2410_udc_set_pullup(udc, is_on);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001489 return 0;
1490}
1491
1492static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
1493{
1494 struct s3c2410_udc *dev = _dev;
1495 unsigned int value;
1496
1497 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
Ben Dooks5f629ad2007-11-19 22:28:15 +00001498
Ben Dooksea99ecf2008-11-24 11:45:03 -08001499 value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001500 if (udc_info->vbus_pin_inverted)
1501 value = !value;
1502
1503 if (value != dev->vbus)
1504 s3c2410_udc_vbus_session(&dev->gadget, value);
1505
1506 return IRQ_HANDLED;
1507}
1508
1509static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1510{
1511 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1512
1513 if (udc_info && udc_info->vbus_draw) {
1514 udc_info->vbus_draw(ma);
1515 return 0;
1516 }
1517
1518 return -ENOTSUPP;
1519}
1520
Felipe Balbi4991e102013-01-24 17:20:46 +02001521static int s3c2410_udc_start(struct usb_gadget *g,
1522 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -05001523static int s3c2410_udc_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001524
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001525static const struct usb_gadget_ops s3c2410_ops = {
1526 .get_frame = s3c2410_udc_get_frame,
1527 .wakeup = s3c2410_udc_wakeup,
1528 .set_selfpowered = s3c2410_udc_set_selfpowered,
1529 .pullup = s3c2410_udc_pullup,
1530 .vbus_session = s3c2410_udc_vbus_session,
1531 .vbus_draw = s3c2410_vbus_draw,
Felipe Balbi4991e102013-01-24 17:20:46 +02001532 .udc_start = s3c2410_udc_start,
1533 .udc_stop = s3c2410_udc_stop,
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001534};
1535
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001536static void s3c2410_udc_command(enum s3c2410_udc_cmd_e cmd)
1537{
1538 if (!udc_info)
1539 return;
1540
1541 if (udc_info->udc_command) {
Viliam Mateicka5b826132011-06-16 10:04:36 +02001542 udc_info->udc_command(cmd);
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001543 } else if (gpio_is_valid(udc_info->pullup_pin)) {
1544 int value;
1545
1546 switch (cmd) {
1547 case S3C2410_UDC_P_ENABLE:
1548 value = 1;
1549 break;
1550 case S3C2410_UDC_P_DISABLE:
1551 value = 0;
1552 break;
1553 default:
1554 return;
1555 }
1556 value ^= udc_info->pullup_pin_inverted;
1557
1558 gpio_set_value(udc_info->pullup_pin, value);
1559 }
1560}
1561
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001562/*------------------------- gadget driver handling---------------------------*/
1563/*
1564 * s3c2410_udc_disable
1565 */
1566static void s3c2410_udc_disable(struct s3c2410_udc *dev)
1567{
1568 dprintk(DEBUG_NORMAL, "%s()\n", __func__);
1569
1570 /* Disable all interrupts */
1571 udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
1572 udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
1573
1574 /* Clear the interrupt registers */
1575 udc_write(S3C2410_UDC_USBINT_RESET
1576 | S3C2410_UDC_USBINT_RESUME
1577 | S3C2410_UDC_USBINT_SUSPEND,
1578 S3C2410_UDC_USB_INT_REG);
1579
1580 udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
1581
1582 /* Good bye, cruel world */
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001583 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001584
1585 /* Set speed to unknown */
1586 dev->gadget.speed = USB_SPEED_UNKNOWN;
1587}
1588
1589/*
1590 * s3c2410_udc_reinit
1591 */
1592static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
1593{
1594 u32 i;
1595
1596 /* device/ep0 records init */
Sachin Kamatff241662012-08-22 15:49:00 +05301597 INIT_LIST_HEAD(&dev->gadget.ep_list);
1598 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001599 dev->ep0state = EP0_IDLE;
1600
1601 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1602 struct s3c2410_ep *ep = &dev->ep[i];
1603
1604 if (i != 0)
Sachin Kamatff241662012-08-22 15:49:00 +05301605 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001606
1607 ep->dev = dev;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +02001608 ep->ep.desc = NULL;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001609 ep->halted = 0;
Sachin Kamatff241662012-08-22 15:49:00 +05301610 INIT_LIST_HEAD(&ep->queue);
Sachin Kamatd246c9d2014-02-03 13:59:38 +05301611 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001612 }
1613}
1614
1615/*
1616 * s3c2410_udc_enable
1617 */
1618static void s3c2410_udc_enable(struct s3c2410_udc *dev)
1619{
1620 int i;
1621
1622 dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
1623
1624 /* dev->gadget.speed = USB_SPEED_UNKNOWN; */
1625 dev->gadget.speed = USB_SPEED_FULL;
1626
1627 /* Set MAXP for all endpoints */
1628 for (i = 0; i < S3C2410_ENDPOINTS; i++) {
1629 udc_write(i, S3C2410_UDC_INDEX_REG);
1630 udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
1631 S3C2410_UDC_MAXP_REG);
1632 }
1633
1634 /* Set default power state */
1635 udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
1636
1637 /* Enable reset and suspend interrupt interrupts */
1638 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1639 S3C2410_UDC_USB_INT_EN_REG);
1640
1641 /* Enable ep0 interrupt */
1642 udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
1643
1644 /* time to say "hello, world" */
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001645 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001646}
1647
Felipe Balbi4991e102013-01-24 17:20:46 +02001648static int s3c2410_udc_start(struct usb_gadget *g,
1649 struct usb_gadget_driver *driver)
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001650{
Felipe Balbi4ea34de2013-02-25 21:25:04 +02001651 struct s3c2410_udc *udc = to_s3c2410(g);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001652
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001653 dprintk(DEBUG_NORMAL, "%s() '%s'\n", __func__, driver->driver.name);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001654
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001655 /* Hook the driver */
1656 udc->driver = driver;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001657
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001658 /* Enable udc */
1659 s3c2410_udc_enable(udc);
1660
1661 return 0;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001662}
1663
Felipe Balbi22835b82014-10-17 12:05:12 -05001664static int s3c2410_udc_stop(struct usb_gadget *g)
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001665{
Felipe Balbi4991e102013-01-24 17:20:46 +02001666 struct s3c2410_udc *udc = to_s3c2410(g);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001667
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001668 udc->driver = NULL;
1669
1670 /* Disable udc */
1671 s3c2410_udc_disable(udc);
1672
1673 return 0;
1674}
1675
1676/*---------------------------------------------------------------------------*/
1677static struct s3c2410_udc memory = {
1678 .gadget = {
1679 .ops = &s3c2410_ops,
1680 .ep0 = &memory.ep[0].ep,
1681 .name = gadget_name,
1682 .dev = {
Kay Sieversc682b172009-01-06 10:44:42 -08001683 .init_name = "gadget",
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001684 },
1685 },
1686
1687 /* control endpoint */
1688 .ep[0] = {
1689 .num = 0,
1690 .ep = {
1691 .name = ep0name,
1692 .ops = &s3c2410_ep_ops,
1693 .maxpacket = EP0_FIFO_SIZE,
Robert Baldyga06487722015-07-31 16:00:43 +02001694 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
1695 USB_EP_CAPS_DIR_ALL),
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001696 },
1697 .dev = &memory,
1698 },
1699
1700 /* first group of endpoints */
1701 .ep[1] = {
1702 .num = 1,
1703 .ep = {
1704 .name = "ep1-bulk",
1705 .ops = &s3c2410_ep_ops,
1706 .maxpacket = EP_FIFO_SIZE,
Robert Baldyga06487722015-07-31 16:00:43 +02001707 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1708 USB_EP_CAPS_DIR_ALL),
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001709 },
1710 .dev = &memory,
1711 .fifo_size = EP_FIFO_SIZE,
1712 .bEndpointAddress = 1,
1713 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1714 },
1715 .ep[2] = {
1716 .num = 2,
1717 .ep = {
1718 .name = "ep2-bulk",
1719 .ops = &s3c2410_ep_ops,
1720 .maxpacket = EP_FIFO_SIZE,
Robert Baldyga06487722015-07-31 16:00:43 +02001721 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1722 USB_EP_CAPS_DIR_ALL),
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001723 },
1724 .dev = &memory,
1725 .fifo_size = EP_FIFO_SIZE,
1726 .bEndpointAddress = 2,
1727 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1728 },
1729 .ep[3] = {
1730 .num = 3,
1731 .ep = {
1732 .name = "ep3-bulk",
1733 .ops = &s3c2410_ep_ops,
1734 .maxpacket = EP_FIFO_SIZE,
Robert Baldyga06487722015-07-31 16:00:43 +02001735 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1736 USB_EP_CAPS_DIR_ALL),
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001737 },
1738 .dev = &memory,
1739 .fifo_size = EP_FIFO_SIZE,
1740 .bEndpointAddress = 3,
1741 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1742 },
1743 .ep[4] = {
1744 .num = 4,
1745 .ep = {
1746 .name = "ep4-bulk",
1747 .ops = &s3c2410_ep_ops,
1748 .maxpacket = EP_FIFO_SIZE,
Robert Baldyga06487722015-07-31 16:00:43 +02001749 .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
1750 USB_EP_CAPS_DIR_ALL),
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001751 },
1752 .dev = &memory,
1753 .fifo_size = EP_FIFO_SIZE,
1754 .bEndpointAddress = 4,
1755 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1756 }
1757
1758};
1759
1760/*
1761 * probe - binds to the platform device
1762 */
1763static int s3c2410_udc_probe(struct platform_device *pdev)
1764{
1765 struct s3c2410_udc *udc = &memory;
1766 struct device *dev = &pdev->dev;
1767 int retval;
Ben Dooksea99ecf2008-11-24 11:45:03 -08001768 int irq;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001769
1770 dev_dbg(dev, "%s()\n", __func__);
1771
1772 usb_bus_clock = clk_get(NULL, "usb-bus-gadget");
1773 if (IS_ERR(usb_bus_clock)) {
1774 dev_err(dev, "failed to get usb bus clock source\n");
1775 return PTR_ERR(usb_bus_clock);
1776 }
1777
Vasily Khoruzhick527b5702014-06-30 22:13:29 +03001778 clk_prepare_enable(usb_bus_clock);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001779
1780 udc_clock = clk_get(NULL, "usb-device");
1781 if (IS_ERR(udc_clock)) {
1782 dev_err(dev, "failed to get udc clock source\n");
1783 return PTR_ERR(udc_clock);
1784 }
1785
Vasily Khoruzhick527b5702014-06-30 22:13:29 +03001786 clk_prepare_enable(udc_clock);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001787
1788 mdelay(10);
1789
1790 dev_dbg(dev, "got and enabled clocks\n");
1791
1792 if (strncmp(pdev->name, "s3c2440", 7) == 0) {
1793 dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
1794 memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;
1795 memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
1796 memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
1797 memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
1798 }
1799
Sachin Kamatff241662012-08-22 15:49:00 +05301800 spin_lock_init(&udc->lock);
Jingoo Hane01ee9f2013-07-30 17:00:51 +09001801 udc_info = dev_get_platdata(&pdev->dev);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001802
1803 rsrc_start = S3C2410_PA_USBDEV;
1804 rsrc_len = S3C24XX_SZ_USBDEV;
1805
1806 if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))
1807 return -EBUSY;
1808
1809 base_addr = ioremap(rsrc_start, rsrc_len);
1810 if (!base_addr) {
1811 retval = -ENOMEM;
1812 goto err_mem;
1813 }
1814
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001815 the_controller = udc;
1816 platform_set_drvdata(pdev, udc);
1817
1818 s3c2410_udc_disable(udc);
1819 s3c2410_udc_reinit(udc);
1820
1821 /* irq setup after old hardware state is cleaned up */
1822 retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08001823 0, gadget_name, udc);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001824
1825 if (retval != 0) {
1826 dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
1827 retval = -EBUSY;
1828 goto err_map;
1829 }
1830
1831 dev_dbg(dev, "got irq %i\n", IRQ_USBD);
1832
1833 if (udc_info && udc_info->vbus_pin > 0) {
Ben Dooksea99ecf2008-11-24 11:45:03 -08001834 retval = gpio_request(udc_info->vbus_pin, "udc vbus");
1835 if (retval < 0) {
1836 dev_err(dev, "cannot claim vbus pin\n");
1837 goto err_int;
1838 }
1839
1840 irq = gpio_to_irq(udc_info->vbus_pin);
1841 if (irq < 0) {
1842 dev_err(dev, "no irq for gpio vbus pin\n");
Wei Yongjun48ad20f2013-05-07 19:48:22 +08001843 retval = irq;
Ben Dooksea99ecf2008-11-24 11:45:03 -08001844 goto err_gpio_claim;
1845 }
1846
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001847 retval = request_irq(irq, s3c2410_udc_vbus_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08001848 IRQF_TRIGGER_RISING
Ben Dooks8802bca2007-11-19 22:28:14 +00001849 | IRQF_TRIGGER_FALLING | IRQF_SHARED,
1850 gadget_name, udc);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001851
1852 if (retval != 0) {
Ben Dooksea99ecf2008-11-24 11:45:03 -08001853 dev_err(dev, "can't get vbus irq %d, err %d\n",
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001854 irq, retval);
1855 retval = -EBUSY;
Ben Dooksea99ecf2008-11-24 11:45:03 -08001856 goto err_gpio_claim;
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001857 }
1858
1859 dev_dbg(dev, "got irq %i\n", irq);
1860 } else {
1861 udc->vbus = 1;
1862 }
1863
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001864 if (udc_info && !udc_info->udc_command &&
1865 gpio_is_valid(udc_info->pullup_pin)) {
1866
1867 retval = gpio_request_one(udc_info->pullup_pin,
1868 udc_info->vbus_pin_inverted ?
1869 GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
1870 "udc pullup");
1871 if (retval)
1872 goto err_vbus_irq;
1873 }
1874
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001875 retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
1876 if (retval)
1877 goto err_add_udc;
1878
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001879 if (s3c2410_udc_debugfs_root) {
1880 udc->regs_info = debugfs_create_file("registers", S_IRUGO,
1881 s3c2410_udc_debugfs_root,
1882 udc, &s3c2410_udc_debugfs_fops);
Zhaoleid66937d2008-10-20 18:53:04 +08001883 if (!udc->regs_info)
1884 dev_warn(dev, "debugfs file creation failed\n");
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001885 }
1886
1887 dev_dbg(dev, "probe ok\n");
1888
1889 return 0;
1890
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001891err_add_udc:
1892 if (udc_info && !udc_info->udc_command &&
1893 gpio_is_valid(udc_info->pullup_pin))
1894 gpio_free(udc_info->pullup_pin);
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001895err_vbus_irq:
1896 if (udc_info && udc_info->vbus_pin > 0)
1897 free_irq(gpio_to_irq(udc_info->vbus_pin), udc);
Ben Dooksea99ecf2008-11-24 11:45:03 -08001898err_gpio_claim:
1899 if (udc_info && udc_info->vbus_pin > 0)
1900 gpio_free(udc_info->vbus_pin);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001901err_int:
1902 free_irq(IRQ_USBD, udc);
1903err_map:
1904 iounmap(base_addr);
1905err_mem:
1906 release_mem_region(rsrc_start, rsrc_len);
1907
1908 return retval;
1909}
1910
1911/*
1912 * s3c2410_udc_remove
1913 */
1914static int s3c2410_udc_remove(struct platform_device *pdev)
1915{
1916 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
1917 unsigned int irq;
1918
1919 dev_dbg(&pdev->dev, "%s()\n", __func__);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001920
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001921 if (udc->driver)
1922 return -EBUSY;
1923
Felipe Balbi7597a492013-02-25 21:11:50 +02001924 usb_del_gadget_udc(&udc->gadget);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001925 debugfs_remove(udc->regs_info);
1926
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001927 if (udc_info && !udc_info->udc_command &&
1928 gpio_is_valid(udc_info->pullup_pin))
1929 gpio_free(udc_info->pullup_pin);
1930
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001931 if (udc_info && udc_info->vbus_pin > 0) {
Ben Dooksea99ecf2008-11-24 11:45:03 -08001932 irq = gpio_to_irq(udc_info->vbus_pin);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001933 free_irq(irq, udc);
1934 }
1935
1936 free_irq(IRQ_USBD, udc);
1937
1938 iounmap(base_addr);
1939 release_mem_region(rsrc_start, rsrc_len);
1940
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001941 if (!IS_ERR(udc_clock) && udc_clock != NULL) {
Vasily Khoruzhick527b5702014-06-30 22:13:29 +03001942 clk_disable_unprepare(udc_clock);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001943 clk_put(udc_clock);
1944 udc_clock = NULL;
1945 }
1946
1947 if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
Vasily Khoruzhick527b5702014-06-30 22:13:29 +03001948 clk_disable_unprepare(usb_bus_clock);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001949 clk_put(usb_bus_clock);
1950 usb_bus_clock = NULL;
1951 }
1952
1953 dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
1954 return 0;
1955}
1956
1957#ifdef CONFIG_PM
Sachin Kamatff241662012-08-22 15:49:00 +05301958static int
1959s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001960{
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001961 s3c2410_udc_command(S3C2410_UDC_P_DISABLE);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001962
1963 return 0;
1964}
1965
1966static int s3c2410_udc_resume(struct platform_device *pdev)
1967{
Lars-Peter Clausena74022a2011-03-07 08:41:59 +01001968 s3c2410_udc_command(S3C2410_UDC_P_ENABLE);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001969
1970 return 0;
1971}
1972#else
1973#define s3c2410_udc_suspend NULL
1974#define s3c2410_udc_resume NULL
1975#endif
1976
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03001977static const struct platform_device_id s3c_udc_ids[] = {
1978 { "s3c2410-usbgadget", },
1979 { "s3c2440-usbgadget", },
Axel Lin7e9d40f2011-07-12 19:05:29 +08001980 { }
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001981};
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03001982MODULE_DEVICE_TABLE(platform, s3c_udc_ids);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001983
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03001984static struct platform_driver udc_driver_24x0 = {
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001985 .driver = {
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03001986 .name = "s3c24x0-usbgadget",
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001987 },
1988 .probe = s3c2410_udc_probe,
1989 .remove = s3c2410_udc_remove,
1990 .suspend = s3c2410_udc_suspend,
1991 .resume = s3c2410_udc_resume,
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03001992 .id_table = s3c_udc_ids,
Arnaud Patard3fc154b2007-06-06 21:05:49 -07001993};
1994
1995static int __init udc_init(void)
1996{
1997 int retval;
1998
1999 dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
2000
2001 s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
2002 if (IS_ERR(s3c2410_udc_debugfs_root)) {
Sachin Kamatc2892cd2012-08-22 15:48:59 +05302003 pr_err("%s: debugfs dir creation failed %ld\n",
Arnaud Patard3fc154b2007-06-06 21:05:49 -07002004 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2005 s3c2410_udc_debugfs_root = NULL;
2006 }
2007
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03002008 retval = platform_driver_register(&udc_driver_24x0);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07002009 if (retval)
2010 goto err;
2011
2012 return 0;
2013
2014err:
2015 debugfs_remove(s3c2410_udc_debugfs_root);
2016 return retval;
2017}
2018
2019static void __exit udc_exit(void)
2020{
Sebastian Andrzej Siewior513385a2011-06-29 16:41:56 +03002021 platform_driver_unregister(&udc_driver_24x0);
Arnaud Patard3fc154b2007-06-06 21:05:49 -07002022 debugfs_remove(s3c2410_udc_debugfs_root);
2023}
2024
Arnaud Patard3fc154b2007-06-06 21:05:49 -07002025module_init(udc_init);
2026module_exit(udc_exit);
2027
2028MODULE_AUTHOR(DRIVER_AUTHOR);
2029MODULE_DESCRIPTION(DRIVER_DESC);
2030MODULE_VERSION(DRIVER_VERSION);
2031MODULE_LICENSE("GPL");