]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/usb/gadget/r8a66597-udc.c
usb: r8a66597-udc buffer management update
[mv-sheeva.git] / drivers / usb / gadget / r8a66597-udc.c
1 /*
2  * R8A66597 UDC (USB gadget)
3  *
4  * Copyright (C) 2006-2009 Renesas Solutions Corp.
5  *
6  * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/io.h>
27 #include <linux/platform_device.h>
28 #include <linux/clk.h>
29
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32
33 #include "r8a66597-udc.h"
34
35 #define DRIVER_VERSION  "2009-08-18"
36
37 static const char udc_name[] = "r8a66597_udc";
38 static const char *r8a66597_ep_name[] = {
39         "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
40         "ep8", "ep9",
41 };
42
43 static void disable_controller(struct r8a66597 *r8a66597);
44 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
45 static void irq_packet_write(struct r8a66597_ep *ep,
46                                 struct r8a66597_request *req);
47 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
48                         gfp_t gfp_flags);
49
50 static void transfer_complete(struct r8a66597_ep *ep,
51                 struct r8a66597_request *req, int status);
52
53 /*-------------------------------------------------------------------------*/
54 static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
55 {
56         return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
57 }
58
59 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
60                 unsigned long reg)
61 {
62         u16 tmp;
63
64         tmp = r8a66597_read(r8a66597, INTENB0);
65         r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
66                         INTENB0);
67         r8a66597_bset(r8a66597, (1 << pipenum), reg);
68         r8a66597_write(r8a66597, tmp, INTENB0);
69 }
70
71 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
72                 unsigned long reg)
73 {
74         u16 tmp;
75
76         tmp = r8a66597_read(r8a66597, INTENB0);
77         r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
78                         INTENB0);
79         r8a66597_bclr(r8a66597, (1 << pipenum), reg);
80         r8a66597_write(r8a66597, tmp, INTENB0);
81 }
82
83 static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
84 {
85         r8a66597_bset(r8a66597, CTRE, INTENB0);
86         r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
87
88         r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
89 }
90
91 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
92 __releases(r8a66597->lock)
93 __acquires(r8a66597->lock)
94 {
95         r8a66597_bclr(r8a66597, CTRE, INTENB0);
96         r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
97         r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
98
99         r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
100         spin_unlock(&r8a66597->lock);
101         r8a66597->driver->disconnect(&r8a66597->gadget);
102         spin_lock(&r8a66597->lock);
103
104         disable_controller(r8a66597);
105         INIT_LIST_HEAD(&r8a66597->ep[0].queue);
106 }
107
108 static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
109 {
110         u16 pid = 0;
111         unsigned long offset;
112
113         if (pipenum == 0)
114                 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
115         else if (pipenum < R8A66597_MAX_NUM_PIPE) {
116                 offset = get_pipectr_addr(pipenum);
117                 pid = r8a66597_read(r8a66597, offset) & PID;
118         } else
119                 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
120
121         return pid;
122 }
123
124 static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
125                 u16 pid)
126 {
127         unsigned long offset;
128
129         if (pipenum == 0)
130                 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
131         else if (pipenum < R8A66597_MAX_NUM_PIPE) {
132                 offset = get_pipectr_addr(pipenum);
133                 r8a66597_mdfy(r8a66597, pid, PID, offset);
134         } else
135                 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
136 }
137
138 static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
139 {
140         control_reg_set_pid(r8a66597, pipenum, PID_BUF);
141 }
142
143 static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
144 {
145         control_reg_set_pid(r8a66597, pipenum, PID_NAK);
146 }
147
148 static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
149 {
150         control_reg_set_pid(r8a66597, pipenum, PID_STALL);
151 }
152
153 static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
154 {
155         u16 ret = 0;
156         unsigned long offset;
157
158         if (pipenum == 0)
159                 ret = r8a66597_read(r8a66597, DCPCTR);
160         else if (pipenum < R8A66597_MAX_NUM_PIPE) {
161                 offset = get_pipectr_addr(pipenum);
162                 ret = r8a66597_read(r8a66597, offset);
163         } else
164                 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
165
166         return ret;
167 }
168
169 static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
170 {
171         unsigned long offset;
172
173         pipe_stop(r8a66597, pipenum);
174
175         if (pipenum == 0)
176                 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
177         else if (pipenum < R8A66597_MAX_NUM_PIPE) {
178                 offset = get_pipectr_addr(pipenum);
179                 r8a66597_bset(r8a66597, SQCLR, offset);
180         } else
181                 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
182 }
183
184 static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
185 {
186         u16 tmp;
187         int size;
188
189         if (pipenum == 0) {
190                 tmp = r8a66597_read(r8a66597, DCPCFG);
191                 if ((tmp & R8A66597_CNTMD) != 0)
192                         size = 256;
193                 else {
194                         tmp = r8a66597_read(r8a66597, DCPMAXP);
195                         size = tmp & MAXP;
196                 }
197         } else {
198                 r8a66597_write(r8a66597, pipenum, PIPESEL);
199                 tmp = r8a66597_read(r8a66597, PIPECFG);
200                 if ((tmp & R8A66597_CNTMD) != 0) {
201                         tmp = r8a66597_read(r8a66597, PIPEBUF);
202                         size = ((tmp >> 10) + 1) * 64;
203                 } else {
204                         tmp = r8a66597_read(r8a66597, PIPEMAXP);
205                         size = tmp & MXPS;
206                 }
207         }
208
209         return size;
210 }
211
212 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
213 {
214         if (r8a66597->pdata->on_chip)
215                 return MBW_32;
216         else
217                 return MBW_16;
218 }
219
220 static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
221 {
222         struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
223
224         if (ep->use_dma)
225                 return;
226
227         r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
228
229         ndelay(450);
230
231         r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
232 }
233
234 static int pipe_buffer_setting(struct r8a66597 *r8a66597,
235                 struct r8a66597_pipe_info *info)
236 {
237         u16 bufnum = 0, buf_bsize = 0;
238         u16 pipecfg = 0;
239
240         if (info->pipe == 0)
241                 return -EINVAL;
242
243         r8a66597_write(r8a66597, info->pipe, PIPESEL);
244
245         if (info->dir_in)
246                 pipecfg |= R8A66597_DIR;
247         pipecfg |= info->type;
248         pipecfg |= info->epnum;
249         switch (info->type) {
250         case R8A66597_INT:
251                 bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
252                 buf_bsize = 0;
253                 break;
254         case R8A66597_BULK:
255                 /* isochronous pipes may be used as bulk pipes */
256                 if (info->pipe > R8A66597_BASE_PIPENUM_BULK)
257                         bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
258                 else
259                         bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
260
261                 bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
262                 buf_bsize = 7;
263                 pipecfg |= R8A66597_DBLB;
264                 if (!info->dir_in)
265                         pipecfg |= R8A66597_SHTNAK;
266                 break;
267         case R8A66597_ISO:
268                 bufnum = R8A66597_BASE_BUFNUM +
269                          (info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
270                 buf_bsize = 7;
271                 break;
272         }
273
274         if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
275                 pr_err(KERN_ERR "r8a66597 pipe memory is insufficient\n");
276                 return -ENOMEM;
277         }
278
279         r8a66597_write(r8a66597, pipecfg, PIPECFG);
280         r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
281         r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
282         if (info->interval)
283                 info->interval--;
284         r8a66597_write(r8a66597, info->interval, PIPEPERI);
285
286         return 0;
287 }
288
289 static void pipe_buffer_release(struct r8a66597 *r8a66597,
290                                 struct r8a66597_pipe_info *info)
291 {
292         if (info->pipe == 0)
293                 return;
294
295         if (is_bulk_pipe(info->pipe))
296                 r8a66597->bulk--;
297         else if (is_interrupt_pipe(info->pipe))
298                 r8a66597->interrupt--;
299         else if (is_isoc_pipe(info->pipe)) {
300                 r8a66597->isochronous--;
301                 if (info->type == R8A66597_BULK)
302                         r8a66597->bulk--;
303         } else
304                 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
305                                 info->pipe);
306 }
307
308 static void pipe_initialize(struct r8a66597_ep *ep)
309 {
310         struct r8a66597 *r8a66597 = ep->r8a66597;
311
312         r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
313
314         r8a66597_write(r8a66597, ACLRM, ep->pipectr);
315         r8a66597_write(r8a66597, 0, ep->pipectr);
316         r8a66597_write(r8a66597, SQCLR, ep->pipectr);
317         if (ep->use_dma) {
318                 r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
319
320                 ndelay(450);
321
322                 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
323         }
324 }
325
326 static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
327                                 struct r8a66597_ep *ep,
328                                 const struct usb_endpoint_descriptor *desc,
329                                 u16 pipenum, int dma)
330 {
331         ep->use_dma = 0;
332         ep->fifoaddr = CFIFO;
333         ep->fifosel = CFIFOSEL;
334         ep->fifoctr = CFIFOCTR;
335         ep->fifotrn = 0;
336
337         ep->pipectr = get_pipectr_addr(pipenum);
338         ep->pipenum = pipenum;
339         ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
340         r8a66597->pipenum2ep[pipenum] = ep;
341         r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
342                 = ep;
343         INIT_LIST_HEAD(&ep->queue);
344 }
345
346 static void r8a66597_ep_release(struct r8a66597_ep *ep)
347 {
348         struct r8a66597 *r8a66597 = ep->r8a66597;
349         u16 pipenum = ep->pipenum;
350
351         if (pipenum == 0)
352                 return;
353
354         if (ep->use_dma)
355                 r8a66597->num_dma--;
356         ep->pipenum = 0;
357         ep->busy = 0;
358         ep->use_dma = 0;
359 }
360
361 static int alloc_pipe_config(struct r8a66597_ep *ep,
362                 const struct usb_endpoint_descriptor *desc)
363 {
364         struct r8a66597 *r8a66597 = ep->r8a66597;
365         struct r8a66597_pipe_info info;
366         int dma = 0;
367         unsigned char *counter;
368         int ret;
369
370         ep->desc = desc;
371
372         if (ep->pipenum)        /* already allocated pipe  */
373                 return 0;
374
375         switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
376         case USB_ENDPOINT_XFER_BULK:
377                 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
378                         if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
379                                 printk(KERN_ERR "bulk pipe is insufficient\n");
380                                 return -ENODEV;
381                         } else {
382                                 info.pipe = R8A66597_BASE_PIPENUM_ISOC
383                                                 + r8a66597->isochronous;
384                                 counter = &r8a66597->isochronous;
385                         }
386                 } else {
387                         info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
388                         counter = &r8a66597->bulk;
389                 }
390                 info.type = R8A66597_BULK;
391                 dma = 1;
392                 break;
393         case USB_ENDPOINT_XFER_INT:
394                 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
395                         printk(KERN_ERR "interrupt pipe is insufficient\n");
396                         return -ENODEV;
397                 }
398                 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
399                 info.type = R8A66597_INT;
400                 counter = &r8a66597->interrupt;
401                 break;
402         case USB_ENDPOINT_XFER_ISOC:
403                 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
404                         printk(KERN_ERR "isochronous pipe is insufficient\n");
405                         return -ENODEV;
406                 }
407                 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
408                 info.type = R8A66597_ISO;
409                 counter = &r8a66597->isochronous;
410                 break;
411         default:
412                 printk(KERN_ERR "unexpect xfer type\n");
413                 return -EINVAL;
414         }
415         ep->type = info.type;
416
417         info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
418         info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
419         info.interval = desc->bInterval;
420         if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
421                 info.dir_in = 1;
422         else
423                 info.dir_in = 0;
424
425         ret = pipe_buffer_setting(r8a66597, &info);
426         if (ret < 0) {
427                 printk(KERN_ERR "pipe_buffer_setting fail\n");
428                 return ret;
429         }
430
431         (*counter)++;
432         if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
433                 r8a66597->bulk++;
434
435         r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
436         pipe_initialize(ep);
437
438         return 0;
439 }
440
441 static int free_pipe_config(struct r8a66597_ep *ep)
442 {
443         struct r8a66597 *r8a66597 = ep->r8a66597;
444         struct r8a66597_pipe_info info;
445
446         info.pipe = ep->pipenum;
447         info.type = ep->type;
448         pipe_buffer_release(r8a66597, &info);
449         r8a66597_ep_release(ep);
450
451         return 0;
452 }
453
454 /*-------------------------------------------------------------------------*/
455 static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
456 {
457         enable_irq_ready(r8a66597, pipenum);
458         enable_irq_nrdy(r8a66597, pipenum);
459 }
460
461 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
462 {
463         disable_irq_ready(r8a66597, pipenum);
464         disable_irq_nrdy(r8a66597, pipenum);
465 }
466
467 /* if complete is true, gadget driver complete function is not call */
468 static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
469 {
470         r8a66597->ep[0].internal_ccpl = ccpl;
471         pipe_start(r8a66597, 0);
472         r8a66597_bset(r8a66597, CCPL, DCPCTR);
473 }
474
475 static void start_ep0_write(struct r8a66597_ep *ep,
476                                 struct r8a66597_request *req)
477 {
478         struct r8a66597 *r8a66597 = ep->r8a66597;
479
480         pipe_change(r8a66597, ep->pipenum);
481         r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
482         r8a66597_write(r8a66597, BCLR, ep->fifoctr);
483         if (req->req.length == 0) {
484                 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
485                 pipe_start(r8a66597, 0);
486                 transfer_complete(ep, req, 0);
487         } else {
488                 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
489                 irq_ep0_write(ep, req);
490         }
491 }
492
493 static void start_packet_write(struct r8a66597_ep *ep,
494                                 struct r8a66597_request *req)
495 {
496         struct r8a66597 *r8a66597 = ep->r8a66597;
497         u16 tmp;
498
499         pipe_change(r8a66597, ep->pipenum);
500         disable_irq_empty(r8a66597, ep->pipenum);
501         pipe_start(r8a66597, ep->pipenum);
502
503         tmp = r8a66597_read(r8a66597, ep->fifoctr);
504         if (unlikely((tmp & FRDY) == 0))
505                 pipe_irq_enable(r8a66597, ep->pipenum);
506         else
507                 irq_packet_write(ep, req);
508 }
509
510 static void start_packet_read(struct r8a66597_ep *ep,
511                                 struct r8a66597_request *req)
512 {
513         struct r8a66597 *r8a66597 = ep->r8a66597;
514         u16 pipenum = ep->pipenum;
515
516         if (ep->pipenum == 0) {
517                 r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
518                 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
519                 pipe_start(r8a66597, pipenum);
520                 pipe_irq_enable(r8a66597, pipenum);
521         } else {
522                 if (ep->use_dma) {
523                         r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
524                         pipe_change(r8a66597, pipenum);
525                         r8a66597_bset(r8a66597, TRENB, ep->fifosel);
526                         r8a66597_write(r8a66597,
527                                 (req->req.length + ep->ep.maxpacket - 1)
528                                         / ep->ep.maxpacket,
529                                 ep->fifotrn);
530                 }
531                 pipe_start(r8a66597, pipenum);  /* trigger once */
532                 pipe_irq_enable(r8a66597, pipenum);
533         }
534 }
535
536 static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
537 {
538         if (ep->desc->bEndpointAddress & USB_DIR_IN)
539                 start_packet_write(ep, req);
540         else
541                 start_packet_read(ep, req);
542 }
543
544 static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
545 {
546         u16 ctsq;
547
548         ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
549
550         switch (ctsq) {
551         case CS_RDDS:
552                 start_ep0_write(ep, req);
553                 break;
554         case CS_WRDS:
555                 start_packet_read(ep, req);
556                 break;
557
558         case CS_WRND:
559                 control_end(ep->r8a66597, 0);
560                 break;
561         default:
562                 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
563                 break;
564         }
565 }
566
567 static void init_controller(struct r8a66597 *r8a66597)
568 {
569         u16 vif = r8a66597->pdata->vif ? LDRV : 0;
570         u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
571         u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
572
573         if (r8a66597->pdata->on_chip) {
574                 r8a66597_bset(r8a66597, 0x04, SYSCFG1);
575                 r8a66597_bset(r8a66597, HSE, SYSCFG0);
576
577                 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
578                 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
579                 r8a66597_bset(r8a66597, USBE, SYSCFG0);
580
581                 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
582
583                 r8a66597_bset(r8a66597, irq_sense, INTENB1);
584                 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
585                                 DMA0CFG);
586         } else {
587                 r8a66597_bset(r8a66597, vif | endian, PINCFG);
588                 r8a66597_bset(r8a66597, HSE, SYSCFG0);          /* High spd */
589                 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
590                                 XTAL, SYSCFG0);
591
592                 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
593                 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
594                 r8a66597_bset(r8a66597, USBE, SYSCFG0);
595
596                 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
597
598                 msleep(3);
599
600                 r8a66597_bset(r8a66597, PLLC, SYSCFG0);
601
602                 msleep(1);
603
604                 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
605
606                 r8a66597_bset(r8a66597, irq_sense, INTENB1);
607                 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
608                                DMA0CFG);
609         }
610 }
611
612 static void disable_controller(struct r8a66597 *r8a66597)
613 {
614         if (r8a66597->pdata->on_chip) {
615                 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
616
617                 r8a66597_write(r8a66597, 0, INTENB0);
618                 r8a66597_write(r8a66597, 0, INTENB1);
619
620                 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
621                 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
622
623         } else {
624                 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
625                 udelay(1);
626                 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
627                 udelay(1);
628                 udelay(1);
629                 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
630         }
631 }
632
633 static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
634 {
635         u16 tmp;
636
637         if (!r8a66597->pdata->on_chip) {
638                 tmp = r8a66597_read(r8a66597, SYSCFG0);
639                 if (!(tmp & XCKE))
640                         r8a66597_bset(r8a66597, XCKE, SYSCFG0);
641         }
642 }
643
644 static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
645 {
646         return list_entry(ep->queue.next, struct r8a66597_request, queue);
647 }
648
649 /*-------------------------------------------------------------------------*/
650 static void transfer_complete(struct r8a66597_ep *ep,
651                 struct r8a66597_request *req, int status)
652 __releases(r8a66597->lock)
653 __acquires(r8a66597->lock)
654 {
655         int restart = 0;
656
657         if (unlikely(ep->pipenum == 0)) {
658                 if (ep->internal_ccpl) {
659                         ep->internal_ccpl = 0;
660                         return;
661                 }
662         }
663
664         list_del_init(&req->queue);
665         if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
666                 req->req.status = -ESHUTDOWN;
667         else
668                 req->req.status = status;
669
670         if (!list_empty(&ep->queue))
671                 restart = 1;
672
673         spin_unlock(&ep->r8a66597->lock);
674         req->req.complete(&ep->ep, &req->req);
675         spin_lock(&ep->r8a66597->lock);
676
677         if (restart) {
678                 req = get_request_from_ep(ep);
679                 if (ep->desc)
680                         start_packet(ep, req);
681         }
682 }
683
684 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
685 {
686         int i;
687         u16 tmp;
688         unsigned bufsize;
689         size_t size;
690         void *buf;
691         u16 pipenum = ep->pipenum;
692         struct r8a66597 *r8a66597 = ep->r8a66597;
693
694         pipe_change(r8a66597, pipenum);
695         r8a66597_bset(r8a66597, ISEL, ep->fifosel);
696
697         i = 0;
698         do {
699                 tmp = r8a66597_read(r8a66597, ep->fifoctr);
700                 if (i++ > 100000) {
701                         printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
702                                 "conflict. please power off this controller.");
703                         return;
704                 }
705                 ndelay(1);
706         } while ((tmp & FRDY) == 0);
707
708         /* prepare parameters */
709         bufsize = get_buffer_size(r8a66597, pipenum);
710         buf = req->req.buf + req->req.actual;
711         size = min(bufsize, req->req.length - req->req.actual);
712
713         /* write fifo */
714         if (req->req.buf) {
715                 if (size > 0)
716                         r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
717                 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
718                         r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
719         }
720
721         /* update parameters */
722         req->req.actual += size;
723
724         /* check transfer finish */
725         if ((!req->req.zero && (req->req.actual == req->req.length))
726                         || (size % ep->ep.maxpacket)
727                         || (size == 0)) {
728                 disable_irq_ready(r8a66597, pipenum);
729                 disable_irq_empty(r8a66597, pipenum);
730         } else {
731                 disable_irq_ready(r8a66597, pipenum);
732                 enable_irq_empty(r8a66597, pipenum);
733         }
734         pipe_start(r8a66597, pipenum);
735 }
736
737 static void irq_packet_write(struct r8a66597_ep *ep,
738                                 struct r8a66597_request *req)
739 {
740         u16 tmp;
741         unsigned bufsize;
742         size_t size;
743         void *buf;
744         u16 pipenum = ep->pipenum;
745         struct r8a66597 *r8a66597 = ep->r8a66597;
746
747         pipe_change(r8a66597, pipenum);
748         tmp = r8a66597_read(r8a66597, ep->fifoctr);
749         if (unlikely((tmp & FRDY) == 0)) {
750                 pipe_stop(r8a66597, pipenum);
751                 pipe_irq_disable(r8a66597, pipenum);
752                 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
753                 return;
754         }
755
756         /* prepare parameters */
757         bufsize = get_buffer_size(r8a66597, pipenum);
758         buf = req->req.buf + req->req.actual;
759         size = min(bufsize, req->req.length - req->req.actual);
760
761         /* write fifo */
762         if (req->req.buf) {
763                 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
764                 if ((size == 0)
765                                 || ((size % ep->ep.maxpacket) != 0)
766                                 || ((bufsize != ep->ep.maxpacket)
767                                         && (bufsize > size)))
768                         r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
769         }
770
771         /* update parameters */
772         req->req.actual += size;
773         /* check transfer finish */
774         if ((!req->req.zero && (req->req.actual == req->req.length))
775                         || (size % ep->ep.maxpacket)
776                         || (size == 0)) {
777                 disable_irq_ready(r8a66597, pipenum);
778                 enable_irq_empty(r8a66597, pipenum);
779         } else {
780                 disable_irq_empty(r8a66597, pipenum);
781                 pipe_irq_enable(r8a66597, pipenum);
782         }
783 }
784
785 static void irq_packet_read(struct r8a66597_ep *ep,
786                                 struct r8a66597_request *req)
787 {
788         u16 tmp;
789         int rcv_len, bufsize, req_len;
790         int size;
791         void *buf;
792         u16 pipenum = ep->pipenum;
793         struct r8a66597 *r8a66597 = ep->r8a66597;
794         int finish = 0;
795
796         pipe_change(r8a66597, pipenum);
797         tmp = r8a66597_read(r8a66597, ep->fifoctr);
798         if (unlikely((tmp & FRDY) == 0)) {
799                 req->req.status = -EPIPE;
800                 pipe_stop(r8a66597, pipenum);
801                 pipe_irq_disable(r8a66597, pipenum);
802                 printk(KERN_ERR "read fifo not ready");
803                 return;
804         }
805
806         /* prepare parameters */
807         rcv_len = tmp & DTLN;
808         bufsize = get_buffer_size(r8a66597, pipenum);
809
810         buf = req->req.buf + req->req.actual;
811         req_len = req->req.length - req->req.actual;
812         if (rcv_len < bufsize)
813                 size = min(rcv_len, req_len);
814         else
815                 size = min(bufsize, req_len);
816
817         /* update parameters */
818         req->req.actual += size;
819
820         /* check transfer finish */
821         if ((!req->req.zero && (req->req.actual == req->req.length))
822                         || (size % ep->ep.maxpacket)
823                         || (size == 0)) {
824                 pipe_stop(r8a66597, pipenum);
825                 pipe_irq_disable(r8a66597, pipenum);
826                 finish = 1;
827         }
828
829         /* read fifo */
830         if (req->req.buf) {
831                 if (size == 0)
832                         r8a66597_write(r8a66597, BCLR, ep->fifoctr);
833                 else
834                         r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
835
836         }
837
838         if ((ep->pipenum != 0) && finish)
839                 transfer_complete(ep, req, 0);
840 }
841
842 static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
843 {
844         u16 check;
845         u16 pipenum;
846         struct r8a66597_ep *ep;
847         struct r8a66597_request *req;
848
849         if ((status & BRDY0) && (enb & BRDY0)) {
850                 r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
851                 r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
852
853                 ep = &r8a66597->ep[0];
854                 req = get_request_from_ep(ep);
855                 irq_packet_read(ep, req);
856         } else {
857                 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
858                         check = 1 << pipenum;
859                         if ((status & check) && (enb & check)) {
860                                 r8a66597_write(r8a66597, ~check, BRDYSTS);
861                                 ep = r8a66597->pipenum2ep[pipenum];
862                                 req = get_request_from_ep(ep);
863                                 if (ep->desc->bEndpointAddress & USB_DIR_IN)
864                                         irq_packet_write(ep, req);
865                                 else
866                                         irq_packet_read(ep, req);
867                         }
868                 }
869         }
870 }
871
872 static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
873 {
874         u16 tmp;
875         u16 check;
876         u16 pipenum;
877         struct r8a66597_ep *ep;
878         struct r8a66597_request *req;
879
880         if ((status & BEMP0) && (enb & BEMP0)) {
881                 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
882
883                 ep = &r8a66597->ep[0];
884                 req = get_request_from_ep(ep);
885                 irq_ep0_write(ep, req);
886         } else {
887                 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
888                         check = 1 << pipenum;
889                         if ((status & check) && (enb & check)) {
890                                 r8a66597_write(r8a66597, ~check, BEMPSTS);
891                                 tmp = control_reg_get(r8a66597, pipenum);
892                                 if ((tmp & INBUFM) == 0) {
893                                         disable_irq_empty(r8a66597, pipenum);
894                                         pipe_irq_disable(r8a66597, pipenum);
895                                         pipe_stop(r8a66597, pipenum);
896                                         ep = r8a66597->pipenum2ep[pipenum];
897                                         req = get_request_from_ep(ep);
898                                         if (!list_empty(&ep->queue))
899                                                 transfer_complete(ep, req, 0);
900                                 }
901                         }
902                 }
903         }
904 }
905
906 static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
907 __releases(r8a66597->lock)
908 __acquires(r8a66597->lock)
909 {
910         struct r8a66597_ep *ep;
911         u16 pid;
912         u16 status = 0;
913         u16 w_index = le16_to_cpu(ctrl->wIndex);
914
915         switch (ctrl->bRequestType & USB_RECIP_MASK) {
916         case USB_RECIP_DEVICE:
917                 status = 1 << USB_DEVICE_SELF_POWERED;
918                 break;
919         case USB_RECIP_INTERFACE:
920                 status = 0;
921                 break;
922         case USB_RECIP_ENDPOINT:
923                 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
924                 pid = control_reg_get_pid(r8a66597, ep->pipenum);
925                 if (pid == PID_STALL)
926                         status = 1 << USB_ENDPOINT_HALT;
927                 else
928                         status = 0;
929                 break;
930         default:
931                 pipe_stall(r8a66597, 0);
932                 return;         /* exit */
933         }
934
935         r8a66597->ep0_data = cpu_to_le16(status);
936         r8a66597->ep0_req->buf = &r8a66597->ep0_data;
937         r8a66597->ep0_req->length = 2;
938         /* AV: what happens if we get called again before that gets through? */
939         spin_unlock(&r8a66597->lock);
940         r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
941         spin_lock(&r8a66597->lock);
942 }
943
944 static void clear_feature(struct r8a66597 *r8a66597,
945                                 struct usb_ctrlrequest *ctrl)
946 {
947         switch (ctrl->bRequestType & USB_RECIP_MASK) {
948         case USB_RECIP_DEVICE:
949                 control_end(r8a66597, 1);
950                 break;
951         case USB_RECIP_INTERFACE:
952                 control_end(r8a66597, 1);
953                 break;
954         case USB_RECIP_ENDPOINT: {
955                 struct r8a66597_ep *ep;
956                 struct r8a66597_request *req;
957                 u16 w_index = le16_to_cpu(ctrl->wIndex);
958
959                 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
960                 pipe_stop(r8a66597, ep->pipenum);
961                 control_reg_sqclr(r8a66597, ep->pipenum);
962
963                 control_end(r8a66597, 1);
964
965                 req = get_request_from_ep(ep);
966                 if (ep->busy) {
967                         ep->busy = 0;
968                         if (list_empty(&ep->queue))
969                                 break;
970                         start_packet(ep, req);
971                 } else if (!list_empty(&ep->queue))
972                         pipe_start(r8a66597, ep->pipenum);
973                 }
974                 break;
975         default:
976                 pipe_stall(r8a66597, 0);
977                 break;
978         }
979 }
980
981 static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
982 {
983
984         switch (ctrl->bRequestType & USB_RECIP_MASK) {
985         case USB_RECIP_DEVICE:
986                 control_end(r8a66597, 1);
987                 break;
988         case USB_RECIP_INTERFACE:
989                 control_end(r8a66597, 1);
990                 break;
991         case USB_RECIP_ENDPOINT: {
992                 struct r8a66597_ep *ep;
993                 u16 w_index = le16_to_cpu(ctrl->wIndex);
994
995                 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
996                 pipe_stall(r8a66597, ep->pipenum);
997
998                 control_end(r8a66597, 1);
999                 }
1000                 break;
1001         default:
1002                 pipe_stall(r8a66597, 0);
1003                 break;
1004         }
1005 }
1006
1007 /* if return value is true, call class driver's setup() */
1008 static int setup_packet(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1009 {
1010         u16 *p = (u16 *)ctrl;
1011         unsigned long offset = USBREQ;
1012         int i, ret = 0;
1013
1014         /* read fifo */
1015         r8a66597_write(r8a66597, ~VALID, INTSTS0);
1016
1017         for (i = 0; i < 4; i++)
1018                 p[i] = r8a66597_read(r8a66597, offset + i*2);
1019
1020         /* check request */
1021         if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1022                 switch (ctrl->bRequest) {
1023                 case USB_REQ_GET_STATUS:
1024                         get_status(r8a66597, ctrl);
1025                         break;
1026                 case USB_REQ_CLEAR_FEATURE:
1027                         clear_feature(r8a66597, ctrl);
1028                         break;
1029                 case USB_REQ_SET_FEATURE:
1030                         set_feature(r8a66597, ctrl);
1031                         break;
1032                 default:
1033                         ret = 1;
1034                         break;
1035                 }
1036         } else
1037                 ret = 1;
1038         return ret;
1039 }
1040
1041 static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1042 {
1043         u16 speed = get_usb_speed(r8a66597);
1044
1045         switch (speed) {
1046         case HSMODE:
1047                 r8a66597->gadget.speed = USB_SPEED_HIGH;
1048                 break;
1049         case FSMODE:
1050                 r8a66597->gadget.speed = USB_SPEED_FULL;
1051                 break;
1052         default:
1053                 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1054                 printk(KERN_ERR "USB speed unknown\n");
1055         }
1056 }
1057
1058 static void irq_device_state(struct r8a66597 *r8a66597)
1059 {
1060         u16 dvsq;
1061
1062         dvsq = r8a66597_read(r8a66597, INTSTS0) & DVSQ;
1063         r8a66597_write(r8a66597, ~DVST, INTSTS0);
1064
1065         if (dvsq == DS_DFLT) {
1066                 /* bus reset */
1067                 r8a66597->driver->disconnect(&r8a66597->gadget);
1068                 r8a66597_update_usb_speed(r8a66597);
1069         }
1070         if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
1071                 r8a66597_update_usb_speed(r8a66597);
1072         if ((dvsq == DS_CNFG || dvsq == DS_ADDS)
1073                         && r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1074                 r8a66597_update_usb_speed(r8a66597);
1075
1076         r8a66597->old_dvsq = dvsq;
1077 }
1078
1079 static void irq_control_stage(struct r8a66597 *r8a66597)
1080 __releases(r8a66597->lock)
1081 __acquires(r8a66597->lock)
1082 {
1083         struct usb_ctrlrequest ctrl;
1084         u16 ctsq;
1085
1086         ctsq = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1087         r8a66597_write(r8a66597, ~CTRT, INTSTS0);
1088
1089         switch (ctsq) {
1090         case CS_IDST: {
1091                 struct r8a66597_ep *ep;
1092                 struct r8a66597_request *req;
1093                 ep = &r8a66597->ep[0];
1094                 req = get_request_from_ep(ep);
1095                 transfer_complete(ep, req, 0);
1096                 }
1097                 break;
1098
1099         case CS_RDDS:
1100         case CS_WRDS:
1101         case CS_WRND:
1102                 if (setup_packet(r8a66597, &ctrl)) {
1103                         spin_unlock(&r8a66597->lock);
1104                         if (r8a66597->driver->setup(&r8a66597->gadget, &ctrl)
1105                                 < 0)
1106                                 pipe_stall(r8a66597, 0);
1107                         spin_lock(&r8a66597->lock);
1108                 }
1109                 break;
1110         case CS_RDSS:
1111         case CS_WRSS:
1112                 control_end(r8a66597, 0);
1113                 break;
1114         default:
1115                 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1116                 break;
1117         }
1118 }
1119
1120 static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1121 {
1122         struct r8a66597 *r8a66597 = _r8a66597;
1123         u16 intsts0;
1124         u16 intenb0;
1125         u16 brdysts, nrdysts, bempsts;
1126         u16 brdyenb, nrdyenb, bempenb;
1127         u16 savepipe;
1128         u16 mask0;
1129
1130         spin_lock(&r8a66597->lock);
1131
1132         intsts0 = r8a66597_read(r8a66597, INTSTS0);
1133         intenb0 = r8a66597_read(r8a66597, INTENB0);
1134
1135         savepipe = r8a66597_read(r8a66597, CFIFOSEL);
1136
1137         mask0 = intsts0 & intenb0;
1138         if (mask0) {
1139                 brdysts = r8a66597_read(r8a66597, BRDYSTS);
1140                 nrdysts = r8a66597_read(r8a66597, NRDYSTS);
1141                 bempsts = r8a66597_read(r8a66597, BEMPSTS);
1142                 brdyenb = r8a66597_read(r8a66597, BRDYENB);
1143                 nrdyenb = r8a66597_read(r8a66597, NRDYENB);
1144                 bempenb = r8a66597_read(r8a66597, BEMPENB);
1145
1146                 if (mask0 & VBINT) {
1147                         r8a66597_write(r8a66597,  0xffff & ~VBINT,
1148                                         INTSTS0);
1149                         r8a66597_start_xclock(r8a66597);
1150
1151                         /* start vbus sampling */
1152                         r8a66597->old_vbus = r8a66597_read(r8a66597, INTSTS0)
1153                                         & VBSTS;
1154                         r8a66597->scount = R8A66597_MAX_SAMPLING;
1155
1156                         mod_timer(&r8a66597->timer,
1157                                         jiffies + msecs_to_jiffies(50));
1158                 }
1159                 if (intsts0 & DVSQ)
1160                         irq_device_state(r8a66597);
1161
1162                 if ((intsts0 & BRDY) && (intenb0 & BRDYE)
1163                                 && (brdysts & brdyenb))
1164                         irq_pipe_ready(r8a66597, brdysts, brdyenb);
1165                 if ((intsts0 & BEMP) && (intenb0 & BEMPE)
1166                                 && (bempsts & bempenb))
1167                         irq_pipe_empty(r8a66597, bempsts, bempenb);
1168
1169                 if (intsts0 & CTRT)
1170                         irq_control_stage(r8a66597);
1171         }
1172
1173         r8a66597_write(r8a66597, savepipe, CFIFOSEL);
1174
1175         spin_unlock(&r8a66597->lock);
1176         return IRQ_HANDLED;
1177 }
1178
1179 static void r8a66597_timer(unsigned long _r8a66597)
1180 {
1181         struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1182         unsigned long flags;
1183         u16 tmp;
1184
1185         spin_lock_irqsave(&r8a66597->lock, flags);
1186         tmp = r8a66597_read(r8a66597, SYSCFG0);
1187         if (r8a66597->scount > 0) {
1188                 tmp = r8a66597_read(r8a66597, INTSTS0) & VBSTS;
1189                 if (tmp == r8a66597->old_vbus) {
1190                         r8a66597->scount--;
1191                         if (r8a66597->scount == 0) {
1192                                 if (tmp == VBSTS)
1193                                         r8a66597_usb_connect(r8a66597);
1194                                 else
1195                                         r8a66597_usb_disconnect(r8a66597);
1196                         } else {
1197                                 mod_timer(&r8a66597->timer,
1198                                         jiffies + msecs_to_jiffies(50));
1199                         }
1200                 } else {
1201                         r8a66597->scount = R8A66597_MAX_SAMPLING;
1202                         r8a66597->old_vbus = tmp;
1203                         mod_timer(&r8a66597->timer,
1204                                         jiffies + msecs_to_jiffies(50));
1205                 }
1206         }
1207         spin_unlock_irqrestore(&r8a66597->lock, flags);
1208 }
1209
1210 /*-------------------------------------------------------------------------*/
1211 static int r8a66597_enable(struct usb_ep *_ep,
1212                          const struct usb_endpoint_descriptor *desc)
1213 {
1214         struct r8a66597_ep *ep;
1215
1216         ep = container_of(_ep, struct r8a66597_ep, ep);
1217         return alloc_pipe_config(ep, desc);
1218 }
1219
1220 static int r8a66597_disable(struct usb_ep *_ep)
1221 {
1222         struct r8a66597_ep *ep;
1223         struct r8a66597_request *req;
1224         unsigned long flags;
1225
1226         ep = container_of(_ep, struct r8a66597_ep, ep);
1227         BUG_ON(!ep);
1228
1229         while (!list_empty(&ep->queue)) {
1230                 req = get_request_from_ep(ep);
1231                 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1232                 transfer_complete(ep, req, -ECONNRESET);
1233                 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1234         }
1235
1236         pipe_irq_disable(ep->r8a66597, ep->pipenum);
1237         return free_pipe_config(ep);
1238 }
1239
1240 static struct usb_request *r8a66597_alloc_request(struct usb_ep *_ep,
1241                                                 gfp_t gfp_flags)
1242 {
1243         struct r8a66597_request *req;
1244
1245         req = kzalloc(sizeof(struct r8a66597_request), gfp_flags);
1246         if (!req)
1247                 return NULL;
1248
1249         INIT_LIST_HEAD(&req->queue);
1250
1251         return &req->req;
1252 }
1253
1254 static void r8a66597_free_request(struct usb_ep *_ep, struct usb_request *_req)
1255 {
1256         struct r8a66597_request *req;
1257
1258         req = container_of(_req, struct r8a66597_request, req);
1259         kfree(req);
1260 }
1261
1262 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
1263                         gfp_t gfp_flags)
1264 {
1265         struct r8a66597_ep *ep;
1266         struct r8a66597_request *req;
1267         unsigned long flags;
1268         int request = 0;
1269
1270         ep = container_of(_ep, struct r8a66597_ep, ep);
1271         req = container_of(_req, struct r8a66597_request, req);
1272
1273         if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1274                 return -ESHUTDOWN;
1275
1276         spin_lock_irqsave(&ep->r8a66597->lock, flags);
1277
1278         if (list_empty(&ep->queue))
1279                 request = 1;
1280
1281         list_add_tail(&req->queue, &ep->queue);
1282         req->req.actual = 0;
1283         req->req.status = -EINPROGRESS;
1284
1285         if (ep->desc == NULL)   /* control */
1286                 start_ep0(ep, req);
1287         else {
1288                 if (request && !ep->busy)
1289                         start_packet(ep, req);
1290         }
1291
1292         spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1293
1294         return 0;
1295 }
1296
1297 static int r8a66597_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1298 {
1299         struct r8a66597_ep *ep;
1300         struct r8a66597_request *req;
1301         unsigned long flags;
1302
1303         ep = container_of(_ep, struct r8a66597_ep, ep);
1304         req = container_of(_req, struct r8a66597_request, req);
1305
1306         spin_lock_irqsave(&ep->r8a66597->lock, flags);
1307         if (!list_empty(&ep->queue))
1308                 transfer_complete(ep, req, -ECONNRESET);
1309         spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1310
1311         return 0;
1312 }
1313
1314 static int r8a66597_set_halt(struct usb_ep *_ep, int value)
1315 {
1316         struct r8a66597_ep *ep;
1317         struct r8a66597_request *req;
1318         unsigned long flags;
1319         int ret = 0;
1320
1321         ep = container_of(_ep, struct r8a66597_ep, ep);
1322         req = get_request_from_ep(ep);
1323
1324         spin_lock_irqsave(&ep->r8a66597->lock, flags);
1325         if (!list_empty(&ep->queue)) {
1326                 ret = -EAGAIN;
1327                 goto out;
1328         }
1329         if (value) {
1330                 ep->busy = 1;
1331                 pipe_stall(ep->r8a66597, ep->pipenum);
1332         } else {
1333                 ep->busy = 0;
1334                 pipe_stop(ep->r8a66597, ep->pipenum);
1335         }
1336
1337 out:
1338         spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1339         return ret;
1340 }
1341
1342 static void r8a66597_fifo_flush(struct usb_ep *_ep)
1343 {
1344         struct r8a66597_ep *ep;
1345         unsigned long flags;
1346
1347         ep = container_of(_ep, struct r8a66597_ep, ep);
1348         spin_lock_irqsave(&ep->r8a66597->lock, flags);
1349         if (list_empty(&ep->queue) && !ep->busy) {
1350                 pipe_stop(ep->r8a66597, ep->pipenum);
1351                 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1352         }
1353         spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1354 }
1355
1356 static struct usb_ep_ops r8a66597_ep_ops = {
1357         .enable         = r8a66597_enable,
1358         .disable        = r8a66597_disable,
1359
1360         .alloc_request  = r8a66597_alloc_request,
1361         .free_request   = r8a66597_free_request,
1362
1363         .queue          = r8a66597_queue,
1364         .dequeue        = r8a66597_dequeue,
1365
1366         .set_halt       = r8a66597_set_halt,
1367         .fifo_flush     = r8a66597_fifo_flush,
1368 };
1369
1370 /*-------------------------------------------------------------------------*/
1371 static struct r8a66597 *the_controller;
1372
1373 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1374 {
1375         struct r8a66597 *r8a66597 = the_controller;
1376         int retval;
1377
1378         if (!driver
1379                         || driver->speed != USB_SPEED_HIGH
1380                         || !driver->bind
1381                         || !driver->setup)
1382                 return -EINVAL;
1383         if (!r8a66597)
1384                 return -ENODEV;
1385         if (r8a66597->driver)
1386                 return -EBUSY;
1387
1388         /* hook up the driver */
1389         driver->driver.bus = NULL;
1390         r8a66597->driver = driver;
1391         r8a66597->gadget.dev.driver = &driver->driver;
1392
1393         retval = device_add(&r8a66597->gadget.dev);
1394         if (retval) {
1395                 printk(KERN_ERR "device_add error (%d)\n", retval);
1396                 goto error;
1397         }
1398
1399         retval = driver->bind(&r8a66597->gadget);
1400         if (retval) {
1401                 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1402                 device_del(&r8a66597->gadget.dev);
1403                 goto error;
1404         }
1405
1406         r8a66597_bset(r8a66597, VBSE, INTENB0);
1407         if (r8a66597_read(r8a66597, INTSTS0) & VBSTS) {
1408                 r8a66597_start_xclock(r8a66597);
1409                 /* start vbus sampling */
1410                 r8a66597->old_vbus = r8a66597_read(r8a66597,
1411                                          INTSTS0) & VBSTS;
1412                 r8a66597->scount = R8A66597_MAX_SAMPLING;
1413                 mod_timer(&r8a66597->timer, jiffies + msecs_to_jiffies(50));
1414         }
1415
1416         return 0;
1417
1418 error:
1419         r8a66597->driver = NULL;
1420         r8a66597->gadget.dev.driver = NULL;
1421
1422         return retval;
1423 }
1424 EXPORT_SYMBOL(usb_gadget_register_driver);
1425
1426 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1427 {
1428         struct r8a66597 *r8a66597 = the_controller;
1429         unsigned long flags;
1430
1431         if (driver != r8a66597->driver || !driver->unbind)
1432                 return -EINVAL;
1433
1434         spin_lock_irqsave(&r8a66597->lock, flags);
1435         if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1436                 r8a66597_usb_disconnect(r8a66597);
1437         spin_unlock_irqrestore(&r8a66597->lock, flags);
1438
1439         r8a66597_bclr(r8a66597, VBSE, INTENB0);
1440
1441         driver->unbind(&r8a66597->gadget);
1442
1443         init_controller(r8a66597);
1444         disable_controller(r8a66597);
1445
1446         device_del(&r8a66597->gadget.dev);
1447         r8a66597->driver = NULL;
1448         return 0;
1449 }
1450 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1451
1452 /*-------------------------------------------------------------------------*/
1453 static int r8a66597_get_frame(struct usb_gadget *_gadget)
1454 {
1455         struct r8a66597 *r8a66597 = gadget_to_r8a66597(_gadget);
1456         return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1457 }
1458
1459 static struct usb_gadget_ops r8a66597_gadget_ops = {
1460         .get_frame              = r8a66597_get_frame,
1461 };
1462
1463 static int __exit r8a66597_remove(struct platform_device *pdev)
1464 {
1465         struct r8a66597         *r8a66597 = dev_get_drvdata(&pdev->dev);
1466
1467         del_timer_sync(&r8a66597->timer);
1468         iounmap((void *)r8a66597->reg);
1469         free_irq(platform_get_irq(pdev, 0), r8a66597);
1470         r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1471 #ifdef CONFIG_HAVE_CLK
1472         if (r8a66597->pdata->on_chip) {
1473                 clk_disable(r8a66597->clk);
1474                 clk_put(r8a66597->clk);
1475         }
1476 #endif
1477         kfree(r8a66597);
1478         return 0;
1479 }
1480
1481 static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1482 {
1483 }
1484
1485 static int __init r8a66597_probe(struct platform_device *pdev)
1486 {
1487 #ifdef CONFIG_HAVE_CLK
1488         char clk_name[8];
1489 #endif
1490         struct resource *res, *ires;
1491         int irq;
1492         void __iomem *reg = NULL;
1493         struct r8a66597 *r8a66597 = NULL;
1494         int ret = 0;
1495         int i;
1496         unsigned long irq_trigger;
1497
1498         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1499         if (!res) {
1500                 ret = -ENODEV;
1501                 printk(KERN_ERR "platform_get_resource error.\n");
1502                 goto clean_up;
1503         }
1504
1505         ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1506         irq = ires->start;
1507         irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
1508
1509         if (irq < 0) {
1510                 ret = -ENODEV;
1511                 printk(KERN_ERR "platform_get_irq error.\n");
1512                 goto clean_up;
1513         }
1514
1515         reg = ioremap(res->start, resource_size(res));
1516         if (reg == NULL) {
1517                 ret = -ENOMEM;
1518                 printk(KERN_ERR "ioremap error.\n");
1519                 goto clean_up;
1520         }
1521
1522         /* initialize ucd */
1523         r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1524         if (r8a66597 == NULL) {
1525                 printk(KERN_ERR "kzalloc error\n");
1526                 goto clean_up;
1527         }
1528
1529         spin_lock_init(&r8a66597->lock);
1530         dev_set_drvdata(&pdev->dev, r8a66597);
1531         r8a66597->pdata = pdev->dev.platform_data;
1532         r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1533
1534         r8a66597->gadget.ops = &r8a66597_gadget_ops;
1535         device_initialize(&r8a66597->gadget.dev);
1536         dev_set_name(&r8a66597->gadget.dev, "gadget");
1537         r8a66597->gadget.is_dualspeed = 1;
1538         r8a66597->gadget.dev.parent = &pdev->dev;
1539         r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1540         r8a66597->gadget.dev.release = pdev->dev.release;
1541         r8a66597->gadget.name = udc_name;
1542
1543         init_timer(&r8a66597->timer);
1544         r8a66597->timer.function = r8a66597_timer;
1545         r8a66597->timer.data = (unsigned long)r8a66597;
1546         r8a66597->reg = (unsigned long)reg;
1547
1548 #ifdef CONFIG_HAVE_CLK
1549         if (r8a66597->pdata->on_chip) {
1550                 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
1551                 r8a66597->clk = clk_get(&pdev->dev, clk_name);
1552                 if (IS_ERR(r8a66597->clk)) {
1553                         dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1554                                 clk_name);
1555                         ret = PTR_ERR(r8a66597->clk);
1556                         goto clean_up;
1557                 }
1558                 clk_enable(r8a66597->clk);
1559         }
1560 #endif
1561
1562         disable_controller(r8a66597); /* make sure controller is disabled */
1563
1564         ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED,
1565                         udc_name, r8a66597);
1566         if (ret < 0) {
1567                 printk(KERN_ERR "request_irq error (%d)\n", ret);
1568                 goto clean_up2;
1569         }
1570
1571         INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
1572         r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
1573         INIT_LIST_HEAD(&r8a66597->gadget.ep0->ep_list);
1574         for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
1575                 struct r8a66597_ep *ep = &r8a66597->ep[i];
1576
1577                 if (i != 0) {
1578                         INIT_LIST_HEAD(&r8a66597->ep[i].ep.ep_list);
1579                         list_add_tail(&r8a66597->ep[i].ep.ep_list,
1580                                         &r8a66597->gadget.ep_list);
1581                 }
1582                 ep->r8a66597 = r8a66597;
1583                 INIT_LIST_HEAD(&ep->queue);
1584                 ep->ep.name = r8a66597_ep_name[i];
1585                 ep->ep.ops = &r8a66597_ep_ops;
1586                 ep->ep.maxpacket = 512;
1587         }
1588         r8a66597->ep[0].ep.maxpacket = 64;
1589         r8a66597->ep[0].pipenum = 0;
1590         r8a66597->ep[0].fifoaddr = CFIFO;
1591         r8a66597->ep[0].fifosel = CFIFOSEL;
1592         r8a66597->ep[0].fifoctr = CFIFOCTR;
1593         r8a66597->ep[0].fifotrn = 0;
1594         r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1595         r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1596         r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1597
1598         the_controller = r8a66597;
1599
1600         r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1601                                                         GFP_KERNEL);
1602         if (r8a66597->ep0_req == NULL)
1603                 goto clean_up3;
1604         r8a66597->ep0_req->complete = nop_completion;
1605
1606         init_controller(r8a66597);
1607
1608         dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1609         return 0;
1610
1611 clean_up3:
1612         free_irq(irq, r8a66597);
1613 clean_up2:
1614 #ifdef CONFIG_HAVE_CLK
1615         if (r8a66597->pdata->on_chip) {
1616                 clk_disable(r8a66597->clk);
1617                 clk_put(r8a66597->clk);
1618         }
1619 #endif
1620 clean_up:
1621         if (r8a66597) {
1622                 if (r8a66597->ep0_req)
1623                         r8a66597_free_request(&r8a66597->ep[0].ep,
1624                                                 r8a66597->ep0_req);
1625                 kfree(r8a66597);
1626         }
1627         if (reg)
1628                 iounmap(reg);
1629
1630         return ret;
1631 }
1632
1633 /*-------------------------------------------------------------------------*/
1634 static struct platform_driver r8a66597_driver = {
1635         .remove =       __exit_p(r8a66597_remove),
1636         .driver         = {
1637                 .name = (char *) udc_name,
1638         },
1639 };
1640
1641 static int __init r8a66597_udc_init(void)
1642 {
1643         return platform_driver_probe(&r8a66597_driver, r8a66597_probe);
1644 }
1645 module_init(r8a66597_udc_init);
1646
1647 static void __exit r8a66597_udc_cleanup(void)
1648 {
1649         platform_driver_unregister(&r8a66597_driver);
1650 }
1651 module_exit(r8a66597_udc_cleanup);
1652
1653 MODULE_DESCRIPTION("R8A66597 USB gadget driver");
1654 MODULE_LICENSE("GPL");
1655 MODULE_AUTHOR("Yoshihiro Shimoda");
1656