2 * R8A66597 UDC (USB gadget)
4 * Copyright (C) 2006-2009 Renesas Solutions Corp.
6 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
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.
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.
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
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
28 #include <linux/platform_device.h>
29 #include <linux/clk.h>
30 #include <linux/err.h>
31 #include <linux/slab.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
36 #include "r8a66597-udc.h"
38 #define DRIVER_VERSION "2009-08-18"
40 static const char udc_name[] = "r8a66597_udc";
41 static const char *r8a66597_ep_name[] = {
42 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
46 static void disable_controller(struct r8a66597 *r8a66597);
47 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
48 static void irq_packet_write(struct r8a66597_ep *ep,
49 struct r8a66597_request *req);
50 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
53 static void transfer_complete(struct r8a66597_ep *ep,
54 struct r8a66597_request *req, int status);
56 /*-------------------------------------------------------------------------*/
57 static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
59 return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
62 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
67 tmp = r8a66597_read(r8a66597, INTENB0);
68 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
70 r8a66597_bset(r8a66597, (1 << pipenum), reg);
71 r8a66597_write(r8a66597, tmp, INTENB0);
74 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
79 tmp = r8a66597_read(r8a66597, INTENB0);
80 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
82 r8a66597_bclr(r8a66597, (1 << pipenum), reg);
83 r8a66597_write(r8a66597, tmp, INTENB0);
86 static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
88 r8a66597_bset(r8a66597, CTRE, INTENB0);
89 r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
91 r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
94 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
95 __releases(r8a66597->lock)
96 __acquires(r8a66597->lock)
98 r8a66597_bclr(r8a66597, CTRE, INTENB0);
99 r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
100 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
102 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
103 spin_unlock(&r8a66597->lock);
104 r8a66597->driver->disconnect(&r8a66597->gadget);
105 spin_lock(&r8a66597->lock);
107 disable_controller(r8a66597);
108 INIT_LIST_HEAD(&r8a66597->ep[0].queue);
111 static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
114 unsigned long offset;
117 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
118 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
119 offset = get_pipectr_addr(pipenum);
120 pid = r8a66597_read(r8a66597, offset) & PID;
122 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
127 static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
130 unsigned long offset;
133 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
134 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
135 offset = get_pipectr_addr(pipenum);
136 r8a66597_mdfy(r8a66597, pid, PID, offset);
138 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
141 static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
143 control_reg_set_pid(r8a66597, pipenum, PID_BUF);
146 static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
148 control_reg_set_pid(r8a66597, pipenum, PID_NAK);
151 static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
153 control_reg_set_pid(r8a66597, pipenum, PID_STALL);
156 static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
159 unsigned long offset;
162 ret = r8a66597_read(r8a66597, DCPCTR);
163 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
164 offset = get_pipectr_addr(pipenum);
165 ret = r8a66597_read(r8a66597, offset);
167 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
172 static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
174 unsigned long offset;
176 pipe_stop(r8a66597, pipenum);
179 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
180 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
181 offset = get_pipectr_addr(pipenum);
182 r8a66597_bset(r8a66597, SQCLR, offset);
184 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
187 static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
193 tmp = r8a66597_read(r8a66597, DCPCFG);
194 if ((tmp & R8A66597_CNTMD) != 0)
197 tmp = r8a66597_read(r8a66597, DCPMAXP);
201 r8a66597_write(r8a66597, pipenum, PIPESEL);
202 tmp = r8a66597_read(r8a66597, PIPECFG);
203 if ((tmp & R8A66597_CNTMD) != 0) {
204 tmp = r8a66597_read(r8a66597, PIPEBUF);
205 size = ((tmp >> 10) + 1) * 64;
207 tmp = r8a66597_read(r8a66597, PIPEMAXP);
215 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
217 if (r8a66597->pdata->on_chip)
223 static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
225 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
230 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
234 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
237 static int pipe_buffer_setting(struct r8a66597 *r8a66597,
238 struct r8a66597_pipe_info *info)
240 u16 bufnum = 0, buf_bsize = 0;
246 r8a66597_write(r8a66597, info->pipe, PIPESEL);
249 pipecfg |= R8A66597_DIR;
250 pipecfg |= info->type;
251 pipecfg |= info->epnum;
252 switch (info->type) {
254 bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
258 /* isochronous pipes may be used as bulk pipes */
259 if (info->pipe > R8A66597_BASE_PIPENUM_BULK)
260 bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
262 bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
264 bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
266 pipecfg |= R8A66597_DBLB;
268 pipecfg |= R8A66597_SHTNAK;
271 bufnum = R8A66597_BASE_BUFNUM +
272 (info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
277 if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
278 pr_err(KERN_ERR "r8a66597 pipe memory is insufficient\n");
282 r8a66597_write(r8a66597, pipecfg, PIPECFG);
283 r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
284 r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
287 r8a66597_write(r8a66597, info->interval, PIPEPERI);
292 static void pipe_buffer_release(struct r8a66597 *r8a66597,
293 struct r8a66597_pipe_info *info)
298 if (is_bulk_pipe(info->pipe))
300 else if (is_interrupt_pipe(info->pipe))
301 r8a66597->interrupt--;
302 else if (is_isoc_pipe(info->pipe)) {
303 r8a66597->isochronous--;
304 if (info->type == R8A66597_BULK)
307 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
311 static void pipe_initialize(struct r8a66597_ep *ep)
313 struct r8a66597 *r8a66597 = ep->r8a66597;
315 r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
317 r8a66597_write(r8a66597, ACLRM, ep->pipectr);
318 r8a66597_write(r8a66597, 0, ep->pipectr);
319 r8a66597_write(r8a66597, SQCLR, ep->pipectr);
321 r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
325 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
329 static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
330 struct r8a66597_ep *ep,
331 const struct usb_endpoint_descriptor *desc,
332 u16 pipenum, int dma)
335 ep->fifoaddr = CFIFO;
336 ep->fifosel = CFIFOSEL;
337 ep->fifoctr = CFIFOCTR;
340 ep->pipectr = get_pipectr_addr(pipenum);
341 ep->pipenum = pipenum;
342 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
343 r8a66597->pipenum2ep[pipenum] = ep;
344 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
346 INIT_LIST_HEAD(&ep->queue);
349 static void r8a66597_ep_release(struct r8a66597_ep *ep)
351 struct r8a66597 *r8a66597 = ep->r8a66597;
352 u16 pipenum = ep->pipenum;
364 static int alloc_pipe_config(struct r8a66597_ep *ep,
365 const struct usb_endpoint_descriptor *desc)
367 struct r8a66597 *r8a66597 = ep->r8a66597;
368 struct r8a66597_pipe_info info;
370 unsigned char *counter;
375 if (ep->pipenum) /* already allocated pipe */
378 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
379 case USB_ENDPOINT_XFER_BULK:
380 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
381 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
382 printk(KERN_ERR "bulk pipe is insufficient\n");
385 info.pipe = R8A66597_BASE_PIPENUM_ISOC
386 + r8a66597->isochronous;
387 counter = &r8a66597->isochronous;
390 info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
391 counter = &r8a66597->bulk;
393 info.type = R8A66597_BULK;
396 case USB_ENDPOINT_XFER_INT:
397 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
398 printk(KERN_ERR "interrupt pipe is insufficient\n");
401 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
402 info.type = R8A66597_INT;
403 counter = &r8a66597->interrupt;
405 case USB_ENDPOINT_XFER_ISOC:
406 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
407 printk(KERN_ERR "isochronous pipe is insufficient\n");
410 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
411 info.type = R8A66597_ISO;
412 counter = &r8a66597->isochronous;
415 printk(KERN_ERR "unexpect xfer type\n");
418 ep->type = info.type;
420 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
421 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
422 info.interval = desc->bInterval;
423 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
428 ret = pipe_buffer_setting(r8a66597, &info);
430 printk(KERN_ERR "pipe_buffer_setting fail\n");
435 if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
438 r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
444 static int free_pipe_config(struct r8a66597_ep *ep)
446 struct r8a66597 *r8a66597 = ep->r8a66597;
447 struct r8a66597_pipe_info info;
449 info.pipe = ep->pipenum;
450 info.type = ep->type;
451 pipe_buffer_release(r8a66597, &info);
452 r8a66597_ep_release(ep);
457 /*-------------------------------------------------------------------------*/
458 static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
460 enable_irq_ready(r8a66597, pipenum);
461 enable_irq_nrdy(r8a66597, pipenum);
464 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
466 disable_irq_ready(r8a66597, pipenum);
467 disable_irq_nrdy(r8a66597, pipenum);
470 /* if complete is true, gadget driver complete function is not call */
471 static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
473 r8a66597->ep[0].internal_ccpl = ccpl;
474 pipe_start(r8a66597, 0);
475 r8a66597_bset(r8a66597, CCPL, DCPCTR);
478 static void start_ep0_write(struct r8a66597_ep *ep,
479 struct r8a66597_request *req)
481 struct r8a66597 *r8a66597 = ep->r8a66597;
483 pipe_change(r8a66597, ep->pipenum);
484 r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
485 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
486 if (req->req.length == 0) {
487 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
488 pipe_start(r8a66597, 0);
489 transfer_complete(ep, req, 0);
491 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
492 irq_ep0_write(ep, req);
496 static void start_packet_write(struct r8a66597_ep *ep,
497 struct r8a66597_request *req)
499 struct r8a66597 *r8a66597 = ep->r8a66597;
502 pipe_change(r8a66597, ep->pipenum);
503 disable_irq_empty(r8a66597, ep->pipenum);
504 pipe_start(r8a66597, ep->pipenum);
506 tmp = r8a66597_read(r8a66597, ep->fifoctr);
507 if (unlikely((tmp & FRDY) == 0))
508 pipe_irq_enable(r8a66597, ep->pipenum);
510 irq_packet_write(ep, req);
513 static void start_packet_read(struct r8a66597_ep *ep,
514 struct r8a66597_request *req)
516 struct r8a66597 *r8a66597 = ep->r8a66597;
517 u16 pipenum = ep->pipenum;
519 if (ep->pipenum == 0) {
520 r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
521 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
522 pipe_start(r8a66597, pipenum);
523 pipe_irq_enable(r8a66597, pipenum);
526 r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
527 pipe_change(r8a66597, pipenum);
528 r8a66597_bset(r8a66597, TRENB, ep->fifosel);
529 r8a66597_write(r8a66597,
530 (req->req.length + ep->ep.maxpacket - 1)
534 pipe_start(r8a66597, pipenum); /* trigger once */
535 pipe_irq_enable(r8a66597, pipenum);
539 static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
541 if (ep->desc->bEndpointAddress & USB_DIR_IN)
542 start_packet_write(ep, req);
544 start_packet_read(ep, req);
547 static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
551 ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
555 start_ep0_write(ep, req);
558 start_packet_read(ep, req);
562 control_end(ep->r8a66597, 0);
565 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
570 static void init_controller(struct r8a66597 *r8a66597)
572 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
573 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
574 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
576 if (r8a66597->pdata->on_chip) {
577 r8a66597_bset(r8a66597, 0x04, SYSCFG1);
578 r8a66597_bset(r8a66597, HSE, SYSCFG0);
580 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
581 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
582 r8a66597_bset(r8a66597, USBE, SYSCFG0);
584 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
586 r8a66597_bset(r8a66597, irq_sense, INTENB1);
587 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
590 r8a66597_bset(r8a66597, vif | endian, PINCFG);
591 r8a66597_bset(r8a66597, HSE, SYSCFG0); /* High spd */
592 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
595 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
596 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
597 r8a66597_bset(r8a66597, USBE, SYSCFG0);
599 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
603 r8a66597_bset(r8a66597, PLLC, SYSCFG0);
607 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
609 r8a66597_bset(r8a66597, irq_sense, INTENB1);
610 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
615 static void disable_controller(struct r8a66597 *r8a66597)
617 if (r8a66597->pdata->on_chip) {
618 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
620 /* disable interrupts */
621 r8a66597_write(r8a66597, 0, INTENB0);
622 r8a66597_write(r8a66597, 0, INTENB1);
623 r8a66597_write(r8a66597, 0, BRDYENB);
624 r8a66597_write(r8a66597, 0, BEMPENB);
625 r8a66597_write(r8a66597, 0, NRDYENB);
628 r8a66597_write(r8a66597, 0, BRDYSTS);
629 r8a66597_write(r8a66597, 0, NRDYSTS);
630 r8a66597_write(r8a66597, 0, BEMPSTS);
632 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
633 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
636 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
638 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
641 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
645 static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
649 if (!r8a66597->pdata->on_chip) {
650 tmp = r8a66597_read(r8a66597, SYSCFG0);
652 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
656 static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
658 return list_entry(ep->queue.next, struct r8a66597_request, queue);
661 /*-------------------------------------------------------------------------*/
662 static void transfer_complete(struct r8a66597_ep *ep,
663 struct r8a66597_request *req, int status)
664 __releases(r8a66597->lock)
665 __acquires(r8a66597->lock)
669 if (unlikely(ep->pipenum == 0)) {
670 if (ep->internal_ccpl) {
671 ep->internal_ccpl = 0;
676 list_del_init(&req->queue);
677 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
678 req->req.status = -ESHUTDOWN;
680 req->req.status = status;
682 if (!list_empty(&ep->queue))
685 spin_unlock(&ep->r8a66597->lock);
686 req->req.complete(&ep->ep, &req->req);
687 spin_lock(&ep->r8a66597->lock);
690 req = get_request_from_ep(ep);
692 start_packet(ep, req);
696 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
703 u16 pipenum = ep->pipenum;
704 struct r8a66597 *r8a66597 = ep->r8a66597;
706 pipe_change(r8a66597, pipenum);
707 r8a66597_bset(r8a66597, ISEL, ep->fifosel);
711 tmp = r8a66597_read(r8a66597, ep->fifoctr);
713 printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
714 "conflict. please power off this controller.");
718 } while ((tmp & FRDY) == 0);
720 /* prepare parameters */
721 bufsize = get_buffer_size(r8a66597, pipenum);
722 buf = req->req.buf + req->req.actual;
723 size = min(bufsize, req->req.length - req->req.actual);
728 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
729 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
730 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
733 /* update parameters */
734 req->req.actual += size;
736 /* check transfer finish */
737 if ((!req->req.zero && (req->req.actual == req->req.length))
738 || (size % ep->ep.maxpacket)
740 disable_irq_ready(r8a66597, pipenum);
741 disable_irq_empty(r8a66597, pipenum);
743 disable_irq_ready(r8a66597, pipenum);
744 enable_irq_empty(r8a66597, pipenum);
746 pipe_start(r8a66597, pipenum);
749 static void irq_packet_write(struct r8a66597_ep *ep,
750 struct r8a66597_request *req)
756 u16 pipenum = ep->pipenum;
757 struct r8a66597 *r8a66597 = ep->r8a66597;
759 pipe_change(r8a66597, pipenum);
760 tmp = r8a66597_read(r8a66597, ep->fifoctr);
761 if (unlikely((tmp & FRDY) == 0)) {
762 pipe_stop(r8a66597, pipenum);
763 pipe_irq_disable(r8a66597, pipenum);
764 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
768 /* prepare parameters */
769 bufsize = get_buffer_size(r8a66597, pipenum);
770 buf = req->req.buf + req->req.actual;
771 size = min(bufsize, req->req.length - req->req.actual);
775 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
777 || ((size % ep->ep.maxpacket) != 0)
778 || ((bufsize != ep->ep.maxpacket)
779 && (bufsize > size)))
780 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
783 /* update parameters */
784 req->req.actual += size;
785 /* check transfer finish */
786 if ((!req->req.zero && (req->req.actual == req->req.length))
787 || (size % ep->ep.maxpacket)
789 disable_irq_ready(r8a66597, pipenum);
790 enable_irq_empty(r8a66597, pipenum);
792 disable_irq_empty(r8a66597, pipenum);
793 pipe_irq_enable(r8a66597, pipenum);
797 static void irq_packet_read(struct r8a66597_ep *ep,
798 struct r8a66597_request *req)
801 int rcv_len, bufsize, req_len;
804 u16 pipenum = ep->pipenum;
805 struct r8a66597 *r8a66597 = ep->r8a66597;
808 pipe_change(r8a66597, pipenum);
809 tmp = r8a66597_read(r8a66597, ep->fifoctr);
810 if (unlikely((tmp & FRDY) == 0)) {
811 req->req.status = -EPIPE;
812 pipe_stop(r8a66597, pipenum);
813 pipe_irq_disable(r8a66597, pipenum);
814 printk(KERN_ERR "read fifo not ready");
818 /* prepare parameters */
819 rcv_len = tmp & DTLN;
820 bufsize = get_buffer_size(r8a66597, pipenum);
822 buf = req->req.buf + req->req.actual;
823 req_len = req->req.length - req->req.actual;
824 if (rcv_len < bufsize)
825 size = min(rcv_len, req_len);
827 size = min(bufsize, req_len);
829 /* update parameters */
830 req->req.actual += size;
832 /* check transfer finish */
833 if ((!req->req.zero && (req->req.actual == req->req.length))
834 || (size % ep->ep.maxpacket)
836 pipe_stop(r8a66597, pipenum);
837 pipe_irq_disable(r8a66597, pipenum);
844 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
846 r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
850 if ((ep->pipenum != 0) && finish)
851 transfer_complete(ep, req, 0);
854 static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
858 struct r8a66597_ep *ep;
859 struct r8a66597_request *req;
861 if ((status & BRDY0) && (enb & BRDY0)) {
862 r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
863 r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
865 ep = &r8a66597->ep[0];
866 req = get_request_from_ep(ep);
867 irq_packet_read(ep, req);
869 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
870 check = 1 << pipenum;
871 if ((status & check) && (enb & check)) {
872 r8a66597_write(r8a66597, ~check, BRDYSTS);
873 ep = r8a66597->pipenum2ep[pipenum];
874 req = get_request_from_ep(ep);
875 if (ep->desc->bEndpointAddress & USB_DIR_IN)
876 irq_packet_write(ep, req);
878 irq_packet_read(ep, req);
884 static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
889 struct r8a66597_ep *ep;
890 struct r8a66597_request *req;
892 if ((status & BEMP0) && (enb & BEMP0)) {
893 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
895 ep = &r8a66597->ep[0];
896 req = get_request_from_ep(ep);
897 irq_ep0_write(ep, req);
899 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
900 check = 1 << pipenum;
901 if ((status & check) && (enb & check)) {
902 r8a66597_write(r8a66597, ~check, BEMPSTS);
903 tmp = control_reg_get(r8a66597, pipenum);
904 if ((tmp & INBUFM) == 0) {
905 disable_irq_empty(r8a66597, pipenum);
906 pipe_irq_disable(r8a66597, pipenum);
907 pipe_stop(r8a66597, pipenum);
908 ep = r8a66597->pipenum2ep[pipenum];
909 req = get_request_from_ep(ep);
910 if (!list_empty(&ep->queue))
911 transfer_complete(ep, req, 0);
918 static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
919 __releases(r8a66597->lock)
920 __acquires(r8a66597->lock)
922 struct r8a66597_ep *ep;
925 u16 w_index = le16_to_cpu(ctrl->wIndex);
927 switch (ctrl->bRequestType & USB_RECIP_MASK) {
928 case USB_RECIP_DEVICE:
929 status = 1 << USB_DEVICE_SELF_POWERED;
931 case USB_RECIP_INTERFACE:
934 case USB_RECIP_ENDPOINT:
935 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
936 pid = control_reg_get_pid(r8a66597, ep->pipenum);
937 if (pid == PID_STALL)
938 status = 1 << USB_ENDPOINT_HALT;
943 pipe_stall(r8a66597, 0);
947 r8a66597->ep0_data = cpu_to_le16(status);
948 r8a66597->ep0_req->buf = &r8a66597->ep0_data;
949 r8a66597->ep0_req->length = 2;
950 /* AV: what happens if we get called again before that gets through? */
951 spin_unlock(&r8a66597->lock);
952 r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
953 spin_lock(&r8a66597->lock);
956 static void clear_feature(struct r8a66597 *r8a66597,
957 struct usb_ctrlrequest *ctrl)
959 switch (ctrl->bRequestType & USB_RECIP_MASK) {
960 case USB_RECIP_DEVICE:
961 control_end(r8a66597, 1);
963 case USB_RECIP_INTERFACE:
964 control_end(r8a66597, 1);
966 case USB_RECIP_ENDPOINT: {
967 struct r8a66597_ep *ep;
968 struct r8a66597_request *req;
969 u16 w_index = le16_to_cpu(ctrl->wIndex);
971 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
973 pipe_stop(r8a66597, ep->pipenum);
974 control_reg_sqclr(r8a66597, ep->pipenum);
975 spin_unlock(&r8a66597->lock);
976 usb_ep_clear_halt(&ep->ep);
977 spin_lock(&r8a66597->lock);
980 control_end(r8a66597, 1);
982 req = get_request_from_ep(ep);
985 if (list_empty(&ep->queue))
987 start_packet(ep, req);
988 } else if (!list_empty(&ep->queue))
989 pipe_start(r8a66597, ep->pipenum);
993 pipe_stall(r8a66597, 0);
998 static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1001 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1002 case USB_RECIP_DEVICE:
1003 control_end(r8a66597, 1);
1005 case USB_RECIP_INTERFACE:
1006 control_end(r8a66597, 1);
1008 case USB_RECIP_ENDPOINT: {
1009 struct r8a66597_ep *ep;
1010 u16 w_index = le16_to_cpu(ctrl->wIndex);
1012 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1013 pipe_stall(r8a66597, ep->pipenum);
1015 control_end(r8a66597, 1);
1019 pipe_stall(r8a66597, 0);
1024 /* if return value is true, call class driver's setup() */
1025 static int setup_packet(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1027 u16 *p = (u16 *)ctrl;
1028 unsigned long offset = USBREQ;
1032 r8a66597_write(r8a66597, ~VALID, INTSTS0);
1034 for (i = 0; i < 4; i++)
1035 p[i] = r8a66597_read(r8a66597, offset + i*2);
1038 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1039 switch (ctrl->bRequest) {
1040 case USB_REQ_GET_STATUS:
1041 get_status(r8a66597, ctrl);
1043 case USB_REQ_CLEAR_FEATURE:
1044 clear_feature(r8a66597, ctrl);
1046 case USB_REQ_SET_FEATURE:
1047 set_feature(r8a66597, ctrl);
1058 static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1060 u16 speed = get_usb_speed(r8a66597);
1064 r8a66597->gadget.speed = USB_SPEED_HIGH;
1067 r8a66597->gadget.speed = USB_SPEED_FULL;
1070 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1071 printk(KERN_ERR "USB speed unknown\n");
1075 static void irq_device_state(struct r8a66597 *r8a66597)
1079 dvsq = r8a66597_read(r8a66597, INTSTS0) & DVSQ;
1080 r8a66597_write(r8a66597, ~DVST, INTSTS0);
1082 if (dvsq == DS_DFLT) {
1084 r8a66597->driver->disconnect(&r8a66597->gadget);
1085 r8a66597_update_usb_speed(r8a66597);
1087 if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
1088 r8a66597_update_usb_speed(r8a66597);
1089 if ((dvsq == DS_CNFG || dvsq == DS_ADDS)
1090 && r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1091 r8a66597_update_usb_speed(r8a66597);
1093 r8a66597->old_dvsq = dvsq;
1096 static void irq_control_stage(struct r8a66597 *r8a66597)
1097 __releases(r8a66597->lock)
1098 __acquires(r8a66597->lock)
1100 struct usb_ctrlrequest ctrl;
1103 ctsq = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1104 r8a66597_write(r8a66597, ~CTRT, INTSTS0);
1108 struct r8a66597_ep *ep;
1109 struct r8a66597_request *req;
1110 ep = &r8a66597->ep[0];
1111 req = get_request_from_ep(ep);
1112 transfer_complete(ep, req, 0);
1119 if (setup_packet(r8a66597, &ctrl)) {
1120 spin_unlock(&r8a66597->lock);
1121 if (r8a66597->driver->setup(&r8a66597->gadget, &ctrl)
1123 pipe_stall(r8a66597, 0);
1124 spin_lock(&r8a66597->lock);
1129 control_end(r8a66597, 0);
1132 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1137 static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1139 struct r8a66597 *r8a66597 = _r8a66597;
1142 u16 brdysts, nrdysts, bempsts;
1143 u16 brdyenb, nrdyenb, bempenb;
1147 spin_lock(&r8a66597->lock);
1149 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1150 intenb0 = r8a66597_read(r8a66597, INTENB0);
1152 savepipe = r8a66597_read(r8a66597, CFIFOSEL);
1154 mask0 = intsts0 & intenb0;
1156 brdysts = r8a66597_read(r8a66597, BRDYSTS);
1157 nrdysts = r8a66597_read(r8a66597, NRDYSTS);
1158 bempsts = r8a66597_read(r8a66597, BEMPSTS);
1159 brdyenb = r8a66597_read(r8a66597, BRDYENB);
1160 nrdyenb = r8a66597_read(r8a66597, NRDYENB);
1161 bempenb = r8a66597_read(r8a66597, BEMPENB);
1163 if (mask0 & VBINT) {
1164 r8a66597_write(r8a66597, 0xffff & ~VBINT,
1166 r8a66597_start_xclock(r8a66597);
1168 /* start vbus sampling */
1169 r8a66597->old_vbus = r8a66597_read(r8a66597, INTSTS0)
1171 r8a66597->scount = R8A66597_MAX_SAMPLING;
1173 mod_timer(&r8a66597->timer,
1174 jiffies + msecs_to_jiffies(50));
1177 irq_device_state(r8a66597);
1179 if ((intsts0 & BRDY) && (intenb0 & BRDYE)
1180 && (brdysts & brdyenb))
1181 irq_pipe_ready(r8a66597, brdysts, brdyenb);
1182 if ((intsts0 & BEMP) && (intenb0 & BEMPE)
1183 && (bempsts & bempenb))
1184 irq_pipe_empty(r8a66597, bempsts, bempenb);
1187 irq_control_stage(r8a66597);
1190 r8a66597_write(r8a66597, savepipe, CFIFOSEL);
1192 spin_unlock(&r8a66597->lock);
1196 static void r8a66597_timer(unsigned long _r8a66597)
1198 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1199 unsigned long flags;
1202 spin_lock_irqsave(&r8a66597->lock, flags);
1203 tmp = r8a66597_read(r8a66597, SYSCFG0);
1204 if (r8a66597->scount > 0) {
1205 tmp = r8a66597_read(r8a66597, INTSTS0) & VBSTS;
1206 if (tmp == r8a66597->old_vbus) {
1208 if (r8a66597->scount == 0) {
1210 r8a66597_usb_connect(r8a66597);
1212 r8a66597_usb_disconnect(r8a66597);
1214 mod_timer(&r8a66597->timer,
1215 jiffies + msecs_to_jiffies(50));
1218 r8a66597->scount = R8A66597_MAX_SAMPLING;
1219 r8a66597->old_vbus = tmp;
1220 mod_timer(&r8a66597->timer,
1221 jiffies + msecs_to_jiffies(50));
1224 spin_unlock_irqrestore(&r8a66597->lock, flags);
1227 /*-------------------------------------------------------------------------*/
1228 static int r8a66597_enable(struct usb_ep *_ep,
1229 const struct usb_endpoint_descriptor *desc)
1231 struct r8a66597_ep *ep;
1233 ep = container_of(_ep, struct r8a66597_ep, ep);
1234 return alloc_pipe_config(ep, desc);
1237 static int r8a66597_disable(struct usb_ep *_ep)
1239 struct r8a66597_ep *ep;
1240 struct r8a66597_request *req;
1241 unsigned long flags;
1243 ep = container_of(_ep, struct r8a66597_ep, ep);
1246 while (!list_empty(&ep->queue)) {
1247 req = get_request_from_ep(ep);
1248 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1249 transfer_complete(ep, req, -ECONNRESET);
1250 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1253 pipe_irq_disable(ep->r8a66597, ep->pipenum);
1254 return free_pipe_config(ep);
1257 static struct usb_request *r8a66597_alloc_request(struct usb_ep *_ep,
1260 struct r8a66597_request *req;
1262 req = kzalloc(sizeof(struct r8a66597_request), gfp_flags);
1266 INIT_LIST_HEAD(&req->queue);
1271 static void r8a66597_free_request(struct usb_ep *_ep, struct usb_request *_req)
1273 struct r8a66597_request *req;
1275 req = container_of(_req, struct r8a66597_request, req);
1279 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
1282 struct r8a66597_ep *ep;
1283 struct r8a66597_request *req;
1284 unsigned long flags;
1287 ep = container_of(_ep, struct r8a66597_ep, ep);
1288 req = container_of(_req, struct r8a66597_request, req);
1290 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1293 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1295 if (list_empty(&ep->queue))
1298 list_add_tail(&req->queue, &ep->queue);
1299 req->req.actual = 0;
1300 req->req.status = -EINPROGRESS;
1302 if (ep->desc == NULL) /* control */
1305 if (request && !ep->busy)
1306 start_packet(ep, req);
1309 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1314 static int r8a66597_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1316 struct r8a66597_ep *ep;
1317 struct r8a66597_request *req;
1318 unsigned long flags;
1320 ep = container_of(_ep, struct r8a66597_ep, ep);
1321 req = container_of(_req, struct r8a66597_request, req);
1323 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1324 if (!list_empty(&ep->queue))
1325 transfer_complete(ep, req, -ECONNRESET);
1326 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1331 static int r8a66597_set_halt(struct usb_ep *_ep, int value)
1333 struct r8a66597_ep *ep;
1334 struct r8a66597_request *req;
1335 unsigned long flags;
1338 ep = container_of(_ep, struct r8a66597_ep, ep);
1339 req = get_request_from_ep(ep);
1341 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1342 if (!list_empty(&ep->queue)) {
1348 pipe_stall(ep->r8a66597, ep->pipenum);
1352 pipe_stop(ep->r8a66597, ep->pipenum);
1356 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1360 static int r8a66597_set_wedge(struct usb_ep *_ep)
1362 struct r8a66597_ep *ep;
1363 unsigned long flags;
1365 ep = container_of(_ep, struct r8a66597_ep, ep);
1367 if (!ep || !ep->desc)
1370 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1372 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1374 return usb_ep_set_halt(_ep);
1377 static void r8a66597_fifo_flush(struct usb_ep *_ep)
1379 struct r8a66597_ep *ep;
1380 unsigned long flags;
1382 ep = container_of(_ep, struct r8a66597_ep, ep);
1383 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1384 if (list_empty(&ep->queue) && !ep->busy) {
1385 pipe_stop(ep->r8a66597, ep->pipenum);
1386 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1388 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1391 static struct usb_ep_ops r8a66597_ep_ops = {
1392 .enable = r8a66597_enable,
1393 .disable = r8a66597_disable,
1395 .alloc_request = r8a66597_alloc_request,
1396 .free_request = r8a66597_free_request,
1398 .queue = r8a66597_queue,
1399 .dequeue = r8a66597_dequeue,
1401 .set_halt = r8a66597_set_halt,
1402 .set_wedge = r8a66597_set_wedge,
1403 .fifo_flush = r8a66597_fifo_flush,
1406 /*-------------------------------------------------------------------------*/
1407 static struct r8a66597 *the_controller;
1409 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1411 struct r8a66597 *r8a66597 = the_controller;
1415 || driver->speed != USB_SPEED_HIGH
1421 if (r8a66597->driver)
1424 /* hook up the driver */
1425 driver->driver.bus = NULL;
1426 r8a66597->driver = driver;
1427 r8a66597->gadget.dev.driver = &driver->driver;
1429 retval = device_add(&r8a66597->gadget.dev);
1431 printk(KERN_ERR "device_add error (%d)\n", retval);
1435 retval = driver->bind(&r8a66597->gadget);
1437 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1438 device_del(&r8a66597->gadget.dev);
1442 r8a66597_bset(r8a66597, VBSE, INTENB0);
1443 if (r8a66597_read(r8a66597, INTSTS0) & VBSTS) {
1444 r8a66597_start_xclock(r8a66597);
1445 /* start vbus sampling */
1446 r8a66597->old_vbus = r8a66597_read(r8a66597,
1448 r8a66597->scount = R8A66597_MAX_SAMPLING;
1449 mod_timer(&r8a66597->timer, jiffies + msecs_to_jiffies(50));
1455 r8a66597->driver = NULL;
1456 r8a66597->gadget.dev.driver = NULL;
1460 EXPORT_SYMBOL(usb_gadget_register_driver);
1462 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1464 struct r8a66597 *r8a66597 = the_controller;
1465 unsigned long flags;
1467 if (driver != r8a66597->driver || !driver->unbind)
1470 spin_lock_irqsave(&r8a66597->lock, flags);
1471 if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1472 r8a66597_usb_disconnect(r8a66597);
1473 spin_unlock_irqrestore(&r8a66597->lock, flags);
1475 r8a66597_bclr(r8a66597, VBSE, INTENB0);
1477 driver->unbind(&r8a66597->gadget);
1479 init_controller(r8a66597);
1480 disable_controller(r8a66597);
1482 device_del(&r8a66597->gadget.dev);
1483 r8a66597->driver = NULL;
1486 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1488 /*-------------------------------------------------------------------------*/
1489 static int r8a66597_get_frame(struct usb_gadget *_gadget)
1491 struct r8a66597 *r8a66597 = gadget_to_r8a66597(_gadget);
1492 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1495 static struct usb_gadget_ops r8a66597_gadget_ops = {
1496 .get_frame = r8a66597_get_frame,
1499 static int __exit r8a66597_remove(struct platform_device *pdev)
1501 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
1503 del_timer_sync(&r8a66597->timer);
1504 iounmap((void *)r8a66597->reg);
1505 free_irq(platform_get_irq(pdev, 0), r8a66597);
1506 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1507 #ifdef CONFIG_HAVE_CLK
1508 if (r8a66597->pdata->on_chip) {
1509 clk_disable(r8a66597->clk);
1510 clk_put(r8a66597->clk);
1517 static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1521 static int __init r8a66597_probe(struct platform_device *pdev)
1523 #ifdef CONFIG_HAVE_CLK
1526 struct resource *res, *ires;
1528 void __iomem *reg = NULL;
1529 struct r8a66597 *r8a66597 = NULL;
1532 unsigned long irq_trigger;
1534 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1537 printk(KERN_ERR "platform_get_resource error.\n");
1541 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1543 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
1547 printk(KERN_ERR "platform_get_irq error.\n");
1551 reg = ioremap(res->start, resource_size(res));
1554 printk(KERN_ERR "ioremap error.\n");
1558 /* initialize ucd */
1559 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1560 if (r8a66597 == NULL) {
1561 printk(KERN_ERR "kzalloc error\n");
1565 spin_lock_init(&r8a66597->lock);
1566 dev_set_drvdata(&pdev->dev, r8a66597);
1567 r8a66597->pdata = pdev->dev.platform_data;
1568 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1570 r8a66597->gadget.ops = &r8a66597_gadget_ops;
1571 device_initialize(&r8a66597->gadget.dev);
1572 dev_set_name(&r8a66597->gadget.dev, "gadget");
1573 r8a66597->gadget.is_dualspeed = 1;
1574 r8a66597->gadget.dev.parent = &pdev->dev;
1575 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1576 r8a66597->gadget.dev.release = pdev->dev.release;
1577 r8a66597->gadget.name = udc_name;
1579 init_timer(&r8a66597->timer);
1580 r8a66597->timer.function = r8a66597_timer;
1581 r8a66597->timer.data = (unsigned long)r8a66597;
1582 r8a66597->reg = (unsigned long)reg;
1584 #ifdef CONFIG_HAVE_CLK
1585 if (r8a66597->pdata->on_chip) {
1586 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
1587 r8a66597->clk = clk_get(&pdev->dev, clk_name);
1588 if (IS_ERR(r8a66597->clk)) {
1589 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1591 ret = PTR_ERR(r8a66597->clk);
1594 clk_enable(r8a66597->clk);
1598 disable_controller(r8a66597); /* make sure controller is disabled */
1600 ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED,
1601 udc_name, r8a66597);
1603 printk(KERN_ERR "request_irq error (%d)\n", ret);
1607 INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
1608 r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
1609 INIT_LIST_HEAD(&r8a66597->gadget.ep0->ep_list);
1610 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
1611 struct r8a66597_ep *ep = &r8a66597->ep[i];
1614 INIT_LIST_HEAD(&r8a66597->ep[i].ep.ep_list);
1615 list_add_tail(&r8a66597->ep[i].ep.ep_list,
1616 &r8a66597->gadget.ep_list);
1618 ep->r8a66597 = r8a66597;
1619 INIT_LIST_HEAD(&ep->queue);
1620 ep->ep.name = r8a66597_ep_name[i];
1621 ep->ep.ops = &r8a66597_ep_ops;
1622 ep->ep.maxpacket = 512;
1624 r8a66597->ep[0].ep.maxpacket = 64;
1625 r8a66597->ep[0].pipenum = 0;
1626 r8a66597->ep[0].fifoaddr = CFIFO;
1627 r8a66597->ep[0].fifosel = CFIFOSEL;
1628 r8a66597->ep[0].fifoctr = CFIFOCTR;
1629 r8a66597->ep[0].fifotrn = 0;
1630 r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1631 r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1632 r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1634 the_controller = r8a66597;
1636 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1638 if (r8a66597->ep0_req == NULL)
1640 r8a66597->ep0_req->complete = nop_completion;
1642 init_controller(r8a66597);
1644 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1648 free_irq(irq, r8a66597);
1650 #ifdef CONFIG_HAVE_CLK
1651 if (r8a66597->pdata->on_chip) {
1652 clk_disable(r8a66597->clk);
1653 clk_put(r8a66597->clk);
1658 if (r8a66597->ep0_req)
1659 r8a66597_free_request(&r8a66597->ep[0].ep,
1669 /*-------------------------------------------------------------------------*/
1670 static struct platform_driver r8a66597_driver = {
1671 .remove = __exit_p(r8a66597_remove),
1673 .name = (char *) udc_name,
1677 static int __init r8a66597_udc_init(void)
1679 return platform_driver_probe(&r8a66597_driver, r8a66597_probe);
1681 module_init(r8a66597_udc_init);
1683 static void __exit r8a66597_udc_cleanup(void)
1685 platform_driver_unregister(&r8a66597_driver);
1687 module_exit(r8a66597_udc_cleanup);
1689 MODULE_DESCRIPTION("R8A66597 USB gadget driver");
1690 MODULE_LICENSE("GPL");
1691 MODULE_AUTHOR("Yoshihiro Shimoda");