2 * M66592 UDC (USB gadget)
4 * Copyright (C) 2006-2007 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/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/timer.h>
30 #include <linux/delay.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/platform_device.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb_gadget.h>
39 #include <asm/system.h>
41 #include "m66592-udc.h"
43 MODULE_DESCRIPTION("M66592 USB gadget driiver");
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Yoshihiro Shimoda");
47 #define DRIVER_VERSION "29 May 2007"
49 /* module parameters */
50 static unsigned short clock
= M66592_XTAL24
;
51 module_param(clock
, ushort
, 0644);
52 MODULE_PARM_DESC(clock
, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0(default=16384)");
53 static unsigned short vif
= M66592_LDRV
;
54 module_param(vif
, ushort
, 0644);
55 MODULE_PARM_DESC(vif
, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
56 static unsigned short endian
= 0;
57 module_param(endian
, ushort
, 0644);
58 MODULE_PARM_DESC(endian
, "data endian: big=256, little=0(default=0)");
59 static unsigned short irq_sense
= M66592_INTL
;
60 module_param(irq_sense
, ushort
, 0644);
61 MODULE_PARM_DESC(irq_sense
, "IRQ sense: low level=2, falling edge=0(default=2)");
63 static const char udc_name
[] = "m66592_udc";
64 static const char *m66592_ep_name
[] = {
65 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7"
68 static void disable_controller(struct m66592
*m66592
);
69 static void irq_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
);
70 static void irq_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
);
71 static int m66592_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
74 static void transfer_complete(struct m66592_ep
*ep
,
75 struct m66592_request
*req
,
77 /*-------------------------------------------------------------------------*/
78 static inline u16
get_usb_speed(struct m66592
*m66592
)
80 return (m66592_read(m66592
, M66592_DVSTCTR
) & M66592_RHST
);
83 static void enable_pipe_irq(struct m66592
*m66592
, u16 pipenum
,
88 tmp
= m66592_read(m66592
, M66592_INTENB0
);
89 m66592_bclr(m66592
, M66592_BEMPE
| M66592_NRDYE
| M66592_BRDYE
,
91 m66592_bset(m66592
, (1 << pipenum
), reg
);
92 m66592_write(m66592
, tmp
, M66592_INTENB0
);
95 static void disable_pipe_irq(struct m66592
*m66592
, u16 pipenum
,
100 tmp
= m66592_read(m66592
, M66592_INTENB0
);
101 m66592_bclr(m66592
, M66592_BEMPE
| M66592_NRDYE
| M66592_BRDYE
,
103 m66592_bclr(m66592
, (1 << pipenum
), reg
);
104 m66592_write(m66592
, tmp
, M66592_INTENB0
);
107 static void m66592_usb_connect(struct m66592
*m66592
)
109 m66592_bset(m66592
, M66592_CTRE
, M66592_INTENB0
);
110 m66592_bset(m66592
, M66592_WDST
| M66592_RDST
| M66592_CMPL
,
112 m66592_bset(m66592
, M66592_BEMPE
| M66592_BRDYE
, M66592_INTENB0
);
114 m66592_bset(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
117 static void m66592_usb_disconnect(struct m66592
*m66592
)
119 m66592_bclr(m66592
, M66592_CTRE
, M66592_INTENB0
);
120 m66592_bclr(m66592
, M66592_WDST
| M66592_RDST
| M66592_CMPL
,
122 m66592_bclr(m66592
, M66592_BEMPE
| M66592_BRDYE
, M66592_INTENB0
);
123 m66592_bclr(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
125 m66592
->gadget
.speed
= USB_SPEED_UNKNOWN
;
126 spin_unlock(&m66592
->lock
);
127 m66592
->driver
->disconnect(&m66592
->gadget
);
128 spin_lock(&m66592
->lock
);
130 disable_controller(m66592
);
131 INIT_LIST_HEAD(&m66592
->ep
[0].queue
);
134 static inline u16
control_reg_get_pid(struct m66592
*m66592
, u16 pipenum
)
137 unsigned long offset
;
140 pid
= m66592_read(m66592
, M66592_DCPCTR
) & M66592_PID
;
141 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
142 offset
= get_pipectr_addr(pipenum
);
143 pid
= m66592_read(m66592
, offset
) & M66592_PID
;
145 printk(KERN_ERR
"unexpect pipe num (%d)\n", pipenum
);
150 static inline void control_reg_set_pid(struct m66592
*m66592
, u16 pipenum
,
153 unsigned long offset
;
156 m66592_mdfy(m66592
, pid
, M66592_PID
, M66592_DCPCTR
);
157 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
158 offset
= get_pipectr_addr(pipenum
);
159 m66592_mdfy(m66592
, pid
, M66592_PID
, offset
);
161 printk(KERN_ERR
"unexpect pipe num (%d)\n", pipenum
);
164 static inline void pipe_start(struct m66592
*m66592
, u16 pipenum
)
166 control_reg_set_pid(m66592
, pipenum
, M66592_PID_BUF
);
169 static inline void pipe_stop(struct m66592
*m66592
, u16 pipenum
)
171 control_reg_set_pid(m66592
, pipenum
, M66592_PID_NAK
);
174 static inline void pipe_stall(struct m66592
*m66592
, u16 pipenum
)
176 control_reg_set_pid(m66592
, pipenum
, M66592_PID_STALL
);
179 static inline u16
control_reg_get(struct m66592
*m66592
, u16 pipenum
)
182 unsigned long offset
;
185 ret
= m66592_read(m66592
, M66592_DCPCTR
);
186 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
187 offset
= get_pipectr_addr(pipenum
);
188 ret
= m66592_read(m66592
, offset
);
190 printk(KERN_ERR
"unexpect pipe num (%d)\n", pipenum
);
195 static inline void control_reg_sqclr(struct m66592
*m66592
, u16 pipenum
)
197 unsigned long offset
;
199 pipe_stop(m66592
, pipenum
);
202 m66592_bset(m66592
, M66592_SQCLR
, M66592_DCPCTR
);
203 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
204 offset
= get_pipectr_addr(pipenum
);
205 m66592_bset(m66592
, M66592_SQCLR
, offset
);
207 printk(KERN_ERR
"unexpect pipe num(%d)\n", pipenum
);
210 static inline int get_buffer_size(struct m66592
*m66592
, u16 pipenum
)
216 tmp
= m66592_read(m66592
, M66592_DCPCFG
);
217 if ((tmp
& M66592_CNTMD
) != 0)
220 tmp
= m66592_read(m66592
, M66592_DCPMAXP
);
221 size
= tmp
& M66592_MAXP
;
224 m66592_write(m66592
, pipenum
, M66592_PIPESEL
);
225 tmp
= m66592_read(m66592
, M66592_PIPECFG
);
226 if ((tmp
& M66592_CNTMD
) != 0) {
227 tmp
= m66592_read(m66592
, M66592_PIPEBUF
);
228 size
= ((tmp
>> 10) + 1) * 64;
230 tmp
= m66592_read(m66592
, M66592_PIPEMAXP
);
231 size
= tmp
& M66592_MXPS
;
238 static inline void pipe_change(struct m66592
*m66592
, u16 pipenum
)
240 struct m66592_ep
*ep
= m66592
->pipenum2ep
[pipenum
];
245 m66592_mdfy(m66592
, pipenum
, M66592_CURPIPE
, ep
->fifosel
);
249 m66592_bset(m66592
, M66592_MBW
, ep
->fifosel
);
252 static int pipe_buffer_setting(struct m66592
*m66592
,
253 struct m66592_pipe_info
*info
)
255 u16 bufnum
= 0, buf_bsize
= 0;
261 m66592_write(m66592
, info
->pipe
, M66592_PIPESEL
);
264 pipecfg
|= M66592_DIR
;
265 pipecfg
|= info
->type
;
266 pipecfg
|= info
->epnum
;
267 switch (info
->type
) {
269 bufnum
= 4 + (info
->pipe
- M66592_BASE_PIPENUM_INT
);
273 bufnum
= m66592
->bi_bufnum
+
274 (info
->pipe
- M66592_BASE_PIPENUM_BULK
) * 16;
275 m66592
->bi_bufnum
+= 16;
277 pipecfg
|= M66592_DBLB
;
279 pipecfg
|= M66592_SHTNAK
;
282 bufnum
= m66592
->bi_bufnum
+
283 (info
->pipe
- M66592_BASE_PIPENUM_ISOC
) * 16;
284 m66592
->bi_bufnum
+= 16;
288 if (m66592
->bi_bufnum
> M66592_MAX_BUFNUM
) {
289 printk(KERN_ERR
"m66592 pipe memory is insufficient(%d)\n",
294 m66592_write(m66592
, pipecfg
, M66592_PIPECFG
);
295 m66592_write(m66592
, (buf_bsize
<< 10) | (bufnum
), M66592_PIPEBUF
);
296 m66592_write(m66592
, info
->maxpacket
, M66592_PIPEMAXP
);
299 m66592_write(m66592
, info
->interval
, M66592_PIPEPERI
);
304 static void pipe_buffer_release(struct m66592
*m66592
,
305 struct m66592_pipe_info
*info
)
310 switch (info
->type
) {
312 if (is_bulk_pipe(info
->pipe
))
313 m66592
->bi_bufnum
-= 16;
316 if (is_isoc_pipe(info
->pipe
))
317 m66592
->bi_bufnum
-= 16;
321 if (is_bulk_pipe(info
->pipe
)) {
323 } else if (is_interrupt_pipe(info
->pipe
))
325 else if (is_isoc_pipe(info
->pipe
)) {
326 m66592
->isochronous
--;
327 if (info
->type
== M66592_BULK
)
330 printk(KERN_ERR
"ep_release: unexpect pipenum (%d)\n",
334 static void pipe_initialize(struct m66592_ep
*ep
)
336 struct m66592
*m66592
= ep
->m66592
;
338 m66592_mdfy(m66592
, 0, M66592_CURPIPE
, ep
->fifosel
);
340 m66592_write(m66592
, M66592_ACLRM
, ep
->pipectr
);
341 m66592_write(m66592
, 0, ep
->pipectr
);
342 m66592_write(m66592
, M66592_SQCLR
, ep
->pipectr
);
344 m66592_mdfy(m66592
, ep
->pipenum
, M66592_CURPIPE
, ep
->fifosel
);
348 m66592_bset(m66592
, M66592_MBW
, ep
->fifosel
);
352 static void m66592_ep_setting(struct m66592
*m66592
, struct m66592_ep
*ep
,
353 const struct usb_endpoint_descriptor
*desc
,
354 u16 pipenum
, int dma
)
356 if ((pipenum
!= 0) && dma
) {
357 if (m66592
->num_dma
== 0) {
360 ep
->fifoaddr
= M66592_D0FIFO
;
361 ep
->fifosel
= M66592_D0FIFOSEL
;
362 ep
->fifoctr
= M66592_D0FIFOCTR
;
363 ep
->fifotrn
= M66592_D0FIFOTRN
;
364 } else if (m66592
->num_dma
== 1) {
367 ep
->fifoaddr
= M66592_D1FIFO
;
368 ep
->fifosel
= M66592_D1FIFOSEL
;
369 ep
->fifoctr
= M66592_D1FIFOCTR
;
370 ep
->fifotrn
= M66592_D1FIFOTRN
;
373 ep
->fifoaddr
= M66592_CFIFO
;
374 ep
->fifosel
= M66592_CFIFOSEL
;
375 ep
->fifoctr
= M66592_CFIFOCTR
;
380 ep
->fifoaddr
= M66592_CFIFO
;
381 ep
->fifosel
= M66592_CFIFOSEL
;
382 ep
->fifoctr
= M66592_CFIFOCTR
;
386 ep
->pipectr
= get_pipectr_addr(pipenum
);
387 ep
->pipenum
= pipenum
;
388 ep
->ep
.maxpacket
= desc
->wMaxPacketSize
;
389 m66592
->pipenum2ep
[pipenum
] = ep
;
390 m66592
->epaddr2ep
[desc
->bEndpointAddress
&USB_ENDPOINT_NUMBER_MASK
] = ep
;
391 INIT_LIST_HEAD(&ep
->queue
);
394 static void m66592_ep_release(struct m66592_ep
*ep
)
396 struct m66592
*m66592
= ep
->m66592
;
397 u16 pipenum
= ep
->pipenum
;
409 static int alloc_pipe_config(struct m66592_ep
*ep
,
410 const struct usb_endpoint_descriptor
*desc
)
412 struct m66592
*m66592
= ep
->m66592
;
413 struct m66592_pipe_info info
;
422 switch(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
423 case USB_ENDPOINT_XFER_BULK
:
424 if (m66592
->bulk
>= M66592_MAX_NUM_BULK
) {
425 if (m66592
->isochronous
>= M66592_MAX_NUM_ISOC
) {
426 printk(KERN_ERR
"bulk pipe is insufficient\n");
429 info
.pipe
= M66592_BASE_PIPENUM_ISOC
+
431 counter
= &m66592
->isochronous
;
434 info
.pipe
= M66592_BASE_PIPENUM_BULK
+ m66592
->bulk
;
435 counter
= &m66592
->bulk
;
437 info
.type
= M66592_BULK
;
440 case USB_ENDPOINT_XFER_INT
:
441 if (m66592
->interrupt
>= M66592_MAX_NUM_INT
) {
442 printk(KERN_ERR
"interrupt pipe is insufficient\n");
445 info
.pipe
= M66592_BASE_PIPENUM_INT
+ m66592
->interrupt
;
446 info
.type
= M66592_INT
;
447 counter
= &m66592
->interrupt
;
449 case USB_ENDPOINT_XFER_ISOC
:
450 if (m66592
->isochronous
>= M66592_MAX_NUM_ISOC
) {
451 printk(KERN_ERR
"isochronous pipe is insufficient\n");
454 info
.pipe
= M66592_BASE_PIPENUM_ISOC
+ m66592
->isochronous
;
455 info
.type
= M66592_ISO
;
456 counter
= &m66592
->isochronous
;
459 printk(KERN_ERR
"unexpect xfer type\n");
462 ep
->type
= info
.type
;
464 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
465 info
.maxpacket
= desc
->wMaxPacketSize
;
466 info
.interval
= desc
->bInterval
;
467 if (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
472 ret
= pipe_buffer_setting(m66592
, &info
);
474 printk(KERN_ERR
"pipe_buffer_setting fail\n");
479 if ((counter
== &m66592
->isochronous
) && info
.type
== M66592_BULK
)
482 m66592_ep_setting(m66592
, ep
, desc
, info
.pipe
, dma
);
488 static int free_pipe_config(struct m66592_ep
*ep
)
490 struct m66592
*m66592
= ep
->m66592
;
491 struct m66592_pipe_info info
;
493 info
.pipe
= ep
->pipenum
;
494 info
.type
= ep
->type
;
495 pipe_buffer_release(m66592
, &info
);
496 m66592_ep_release(ep
);
501 /*-------------------------------------------------------------------------*/
502 static void pipe_irq_enable(struct m66592
*m66592
, u16 pipenum
)
504 enable_irq_ready(m66592
, pipenum
);
505 enable_irq_nrdy(m66592
, pipenum
);
508 static void pipe_irq_disable(struct m66592
*m66592
, u16 pipenum
)
510 disable_irq_ready(m66592
, pipenum
);
511 disable_irq_nrdy(m66592
, pipenum
);
514 /* if complete is true, gadget driver complete function is not call */
515 static void control_end(struct m66592
*m66592
, unsigned ccpl
)
517 m66592
->ep
[0].internal_ccpl
= ccpl
;
518 pipe_start(m66592
, 0);
519 m66592_bset(m66592
, M66592_CCPL
, M66592_DCPCTR
);
522 static void start_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
524 struct m66592
*m66592
= ep
->m66592
;
526 pipe_change(m66592
, ep
->pipenum
);
527 m66592_mdfy(m66592
, M66592_ISEL
| M66592_PIPE0
,
528 (M66592_ISEL
| M66592_CURPIPE
),
530 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
531 if (req
->req
.length
== 0) {
532 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
533 pipe_start(m66592
, 0);
534 transfer_complete(ep
, req
, 0);
536 m66592_write(m66592
, ~M66592_BEMP0
, M66592_BEMPSTS
);
537 irq_ep0_write(ep
, req
);
541 static void start_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
543 struct m66592
*m66592
= ep
->m66592
;
546 pipe_change(m66592
, ep
->pipenum
);
547 disable_irq_empty(m66592
, ep
->pipenum
);
548 pipe_start(m66592
, ep
->pipenum
);
550 tmp
= m66592_read(m66592
, ep
->fifoctr
);
551 if (unlikely((tmp
& M66592_FRDY
) == 0))
552 pipe_irq_enable(m66592
, ep
->pipenum
);
554 irq_packet_write(ep
, req
);
557 static void start_packet_read(struct m66592_ep
*ep
, struct m66592_request
*req
)
559 struct m66592
*m66592
= ep
->m66592
;
560 u16 pipenum
= ep
->pipenum
;
562 if (ep
->pipenum
== 0) {
563 m66592_mdfy(m66592
, M66592_PIPE0
,
564 (M66592_ISEL
| M66592_CURPIPE
),
566 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
567 pipe_start(m66592
, pipenum
);
568 pipe_irq_enable(m66592
, pipenum
);
571 m66592_bset(m66592
, M66592_TRCLR
, ep
->fifosel
);
572 pipe_change(m66592
, pipenum
);
573 m66592_bset(m66592
, M66592_TRENB
, ep
->fifosel
);
575 (req
->req
.length
+ ep
->ep
.maxpacket
- 1) /
576 ep
->ep
.maxpacket
, ep
->fifotrn
);
578 pipe_start(m66592
, pipenum
); /* trigger once */
579 pipe_irq_enable(m66592
, pipenum
);
583 static void start_packet(struct m66592_ep
*ep
, struct m66592_request
*req
)
585 if (ep
->desc
->bEndpointAddress
& USB_DIR_IN
)
586 start_packet_write(ep
, req
);
588 start_packet_read(ep
, req
);
591 static void start_ep0(struct m66592_ep
*ep
, struct m66592_request
*req
)
595 ctsq
= m66592_read(ep
->m66592
, M66592_INTSTS0
) & M66592_CTSQ
;
599 start_ep0_write(ep
, req
);
602 start_packet_read(ep
, req
);
606 control_end(ep
->m66592
, 0);
609 printk(KERN_ERR
"start_ep0: unexpect ctsq(%x)\n", ctsq
);
614 static void init_controller(struct m66592
*m66592
)
616 m66592_bset(m66592
, (vif
& M66592_LDRV
) | (endian
& M66592_BIGEND
),
618 m66592_bset(m66592
, M66592_HSE
, M66592_SYSCFG
); /* High spd */
619 m66592_mdfy(m66592
, clock
& M66592_XTAL
, M66592_XTAL
, M66592_SYSCFG
);
621 m66592_bclr(m66592
, M66592_USBE
, M66592_SYSCFG
);
622 m66592_bclr(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
623 m66592_bset(m66592
, M66592_USBE
, M66592_SYSCFG
);
625 m66592_bset(m66592
, M66592_XCKE
, M66592_SYSCFG
);
629 m66592_bset(m66592
, M66592_RCKE
| M66592_PLLC
, M66592_SYSCFG
);
633 m66592_bset(m66592
, M66592_SCKE
, M66592_SYSCFG
);
635 m66592_bset(m66592
, irq_sense
& M66592_INTL
, M66592_INTENB1
);
636 m66592_write(m66592
, M66592_BURST
| M66592_CPU_ADR_RD_WR
,
640 static void disable_controller(struct m66592
*m66592
)
642 m66592_bclr(m66592
, M66592_SCKE
, M66592_SYSCFG
);
644 m66592_bclr(m66592
, M66592_PLLC
, M66592_SYSCFG
);
646 m66592_bclr(m66592
, M66592_RCKE
, M66592_SYSCFG
);
648 m66592_bclr(m66592
, M66592_XCKE
, M66592_SYSCFG
);
651 static void m66592_start_xclock(struct m66592
*m66592
)
655 tmp
= m66592_read(m66592
, M66592_SYSCFG
);
656 if (!(tmp
& M66592_XCKE
))
657 m66592_bset(m66592
, M66592_XCKE
, M66592_SYSCFG
);
660 /*-------------------------------------------------------------------------*/
661 static void transfer_complete(struct m66592_ep
*ep
,
662 struct m66592_request
*req
,
667 if (unlikely(ep
->pipenum
== 0)) {
668 if (ep
->internal_ccpl
) {
669 ep
->internal_ccpl
= 0;
674 list_del_init(&req
->queue
);
675 if (ep
->m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
676 req
->req
.status
= -ESHUTDOWN
;
678 req
->req
.status
= status
;
680 if (!list_empty(&ep
->queue
))
683 if (likely(req
->req
.complete
))
684 req
->req
.complete(&ep
->ep
, &req
->req
);
687 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
689 start_packet(ep
, req
);
693 static void irq_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
700 u16 pipenum
= ep
->pipenum
;
701 struct m66592
*m66592
= ep
->m66592
;
703 pipe_change(m66592
, pipenum
);
704 m66592_bset(m66592
, M66592_ISEL
, ep
->fifosel
);
708 tmp
= m66592_read(m66592
, ep
->fifoctr
);
710 printk(KERN_ERR
"pipe0 is busy. maybe cpu i/o bus"
711 "conflict. please power off this controller.");
715 } while ((tmp
& M66592_FRDY
) == 0);
717 /* prepare parameters */
718 bufsize
= get_buffer_size(m66592
, pipenum
);
719 buf
= req
->req
.buf
+ req
->req
.actual
;
720 size
= min(bufsize
, req
->req
.length
- req
->req
.actual
);
725 m66592_write_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
726 if ((size
== 0) || ((size
% ep
->ep
.maxpacket
) != 0))
727 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
730 /* update parameters */
731 req
->req
.actual
+= size
;
733 /* check transfer finish */
734 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
)) ||
735 (size
% ep
->ep
.maxpacket
) || (size
== 0)) {
736 disable_irq_ready(m66592
, pipenum
);
737 disable_irq_empty(m66592
, pipenum
);
739 disable_irq_ready(m66592
, pipenum
);
740 enable_irq_empty(m66592
, pipenum
);
742 pipe_start(m66592
, pipenum
);
745 static void irq_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
751 u16 pipenum
= ep
->pipenum
;
752 struct m66592
*m66592
= ep
->m66592
;
754 pipe_change(m66592
, pipenum
);
755 tmp
= m66592_read(m66592
, ep
->fifoctr
);
756 if (unlikely((tmp
& M66592_FRDY
) == 0)) {
757 pipe_stop(m66592
, pipenum
);
758 pipe_irq_disable(m66592
, pipenum
);
759 printk(KERN_ERR
"write fifo not ready. pipnum=%d\n", pipenum
);
763 /* prepare parameters */
764 bufsize
= get_buffer_size(m66592
, pipenum
);
765 buf
= req
->req
.buf
+ req
->req
.actual
;
766 size
= min(bufsize
, req
->req
.length
- req
->req
.actual
);
770 m66592_write_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
771 if ((size
== 0) || ((size
% ep
->ep
.maxpacket
) != 0) ||
772 ((bufsize
!= ep
->ep
.maxpacket
) && (bufsize
> size
)))
773 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
776 /* update parameters */
777 req
->req
.actual
+= size
;
778 /* check transfer finish */
779 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
)) ||
780 (size
% ep
->ep
.maxpacket
) || (size
== 0)) {
781 disable_irq_ready(m66592
, pipenum
);
782 enable_irq_empty(m66592
, pipenum
);
784 disable_irq_empty(m66592
, pipenum
);
785 pipe_irq_enable(m66592
, pipenum
);
789 static void irq_packet_read(struct m66592_ep
*ep
, struct m66592_request
*req
)
792 int rcv_len
, bufsize
, req_len
;
795 u16 pipenum
= ep
->pipenum
;
796 struct m66592
*m66592
= ep
->m66592
;
799 pipe_change(m66592
, pipenum
);
800 tmp
= m66592_read(m66592
, ep
->fifoctr
);
801 if (unlikely((tmp
& M66592_FRDY
) == 0)) {
802 req
->req
.status
= -EPIPE
;
803 pipe_stop(m66592
, pipenum
);
804 pipe_irq_disable(m66592
, pipenum
);
805 printk(KERN_ERR
"read fifo not ready");
809 /* prepare parameters */
810 rcv_len
= tmp
& M66592_DTLN
;
811 bufsize
= get_buffer_size(m66592
, pipenum
);
813 buf
= req
->req
.buf
+ req
->req
.actual
;
814 req_len
= req
->req
.length
- req
->req
.actual
;
815 if (rcv_len
< bufsize
)
816 size
= min(rcv_len
, req_len
);
818 size
= min(bufsize
, req_len
);
820 /* update parameters */
821 req
->req
.actual
+= size
;
823 /* check transfer finish */
824 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
)) ||
825 (size
% ep
->ep
.maxpacket
) || (size
== 0)) {
826 pipe_stop(m66592
, pipenum
);
827 pipe_irq_disable(m66592
, pipenum
);
834 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
836 m66592_read_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
839 if ((ep
->pipenum
!= 0) && finish
)
840 transfer_complete(ep
, req
, 0);
843 static void irq_pipe_ready(struct m66592
*m66592
, u16 status
, u16 enb
)
847 struct m66592_ep
*ep
;
848 struct m66592_request
*req
;
850 if ((status
& M66592_BRDY0
) && (enb
& M66592_BRDY0
)) {
851 m66592_write(m66592
, ~M66592_BRDY0
, M66592_BRDYSTS
);
852 m66592_mdfy(m66592
, M66592_PIPE0
, M66592_CURPIPE
,
856 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
857 irq_packet_read(ep
, req
);
859 for (pipenum
= 1; pipenum
< M66592_MAX_NUM_PIPE
; pipenum
++) {
860 check
= 1 << pipenum
;
861 if ((status
& check
) && (enb
& check
)) {
862 m66592_write(m66592
, ~check
, M66592_BRDYSTS
);
863 ep
= m66592
->pipenum2ep
[pipenum
];
864 req
= list_entry(ep
->queue
.next
,
865 struct m66592_request
, queue
);
866 if (ep
->desc
->bEndpointAddress
& USB_DIR_IN
)
867 irq_packet_write(ep
, req
);
869 irq_packet_read(ep
, req
);
875 static void irq_pipe_empty(struct m66592
*m66592
, u16 status
, u16 enb
)
880 struct m66592_ep
*ep
;
881 struct m66592_request
*req
;
883 if ((status
& M66592_BEMP0
) && (enb
& M66592_BEMP0
)) {
884 m66592_write(m66592
, ~M66592_BEMP0
, M66592_BEMPSTS
);
887 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
888 irq_ep0_write(ep
, req
);
890 for (pipenum
= 1; pipenum
< M66592_MAX_NUM_PIPE
; pipenum
++) {
891 check
= 1 << pipenum
;
892 if ((status
& check
) && (enb
& check
)) {
893 m66592_write(m66592
, ~check
, M66592_BEMPSTS
);
894 tmp
= control_reg_get(m66592
, pipenum
);
895 if ((tmp
& M66592_INBUFM
) == 0) {
896 disable_irq_empty(m66592
, pipenum
);
897 pipe_irq_disable(m66592
, pipenum
);
898 pipe_stop(m66592
, pipenum
);
899 ep
= m66592
->pipenum2ep
[pipenum
];
900 req
= list_entry(ep
->queue
.next
,
901 struct m66592_request
,
903 if (!list_empty(&ep
->queue
))
904 transfer_complete(ep
, req
, 0);
911 static void get_status(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
913 struct m66592_ep
*ep
;
917 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
918 case USB_RECIP_DEVICE
:
919 status
= 1; /* selfpower */
921 case USB_RECIP_INTERFACE
:
924 case USB_RECIP_ENDPOINT
:
925 ep
= m66592
->epaddr2ep
[ctrl
->wIndex
&USB_ENDPOINT_NUMBER_MASK
];
926 pid
= control_reg_get_pid(m66592
, ep
->pipenum
);
927 if (pid
== M66592_PID_STALL
)
933 pipe_stall(m66592
, 0);
937 *m66592
->ep0_buf
= status
;
938 m66592
->ep0_req
->buf
= m66592
->ep0_buf
;
939 m66592
->ep0_req
->length
= 2;
940 m66592_queue(m66592
->gadget
.ep0
, m66592
->ep0_req
, GFP_KERNEL
);
943 static void clear_feature(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
945 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
946 case USB_RECIP_DEVICE
:
947 control_end(m66592
, 1);
949 case USB_RECIP_INTERFACE
:
950 control_end(m66592
, 1);
952 case USB_RECIP_ENDPOINT
: {
953 struct m66592_ep
*ep
;
954 struct m66592_request
*req
;
956 ep
= m66592
->epaddr2ep
[ctrl
->wIndex
&USB_ENDPOINT_NUMBER_MASK
];
957 pipe_stop(m66592
, ep
->pipenum
);
958 control_reg_sqclr(m66592
, ep
->pipenum
);
960 control_end(m66592
, 1);
962 req
= list_entry(ep
->queue
.next
,
963 struct m66592_request
, queue
);
966 if (list_empty(&ep
->queue
))
968 start_packet(ep
, req
);
969 } else if (!list_empty(&ep
->queue
))
970 pipe_start(m66592
, ep
->pipenum
);
974 pipe_stall(m66592
, 0);
979 static void set_feature(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
982 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
983 case USB_RECIP_DEVICE
:
984 control_end(m66592
, 1);
986 case USB_RECIP_INTERFACE
:
987 control_end(m66592
, 1);
989 case USB_RECIP_ENDPOINT
: {
990 struct m66592_ep
*ep
;
992 ep
= m66592
->epaddr2ep
[ctrl
->wIndex
&USB_ENDPOINT_NUMBER_MASK
];
993 pipe_stall(m66592
, ep
->pipenum
);
995 control_end(m66592
, 1);
999 pipe_stall(m66592
, 0);
1004 /* if return value is true, call class driver's setup() */
1005 static int setup_packet(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
1007 u16
*p
= (u16
*)ctrl
;
1008 unsigned long offset
= M66592_USBREQ
;
1012 m66592_write(m66592
, ~M66592_VALID
, M66592_INTSTS0
);
1014 for (i
= 0; i
< 4; i
++)
1015 p
[i
] = m66592_read(m66592
, offset
+ i
*2);
1018 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1019 switch (ctrl
->bRequest
) {
1020 case USB_REQ_GET_STATUS
:
1021 get_status(m66592
, ctrl
);
1023 case USB_REQ_CLEAR_FEATURE
:
1024 clear_feature(m66592
, ctrl
);
1026 case USB_REQ_SET_FEATURE
:
1027 set_feature(m66592
, ctrl
);
1038 static void m66592_update_usb_speed(struct m66592
*m66592
)
1040 u16 speed
= get_usb_speed(m66592
);
1044 m66592
->gadget
.speed
= USB_SPEED_HIGH
;
1047 m66592
->gadget
.speed
= USB_SPEED_FULL
;
1050 m66592
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1051 printk(KERN_ERR
"USB speed unknown\n");
1055 static void irq_device_state(struct m66592
*m66592
)
1059 dvsq
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_DVSQ
;
1060 m66592_write(m66592
, ~M66592_DVST
, M66592_INTSTS0
);
1062 if (dvsq
== M66592_DS_DFLT
) { /* bus reset */
1063 m66592
->driver
->disconnect(&m66592
->gadget
);
1064 m66592_update_usb_speed(m66592
);
1066 if (m66592
->old_dvsq
== M66592_DS_CNFG
&& dvsq
!= M66592_DS_CNFG
)
1067 m66592_update_usb_speed(m66592
);
1068 if ((dvsq
== M66592_DS_CNFG
|| dvsq
== M66592_DS_ADDS
) &&
1069 m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1070 m66592_update_usb_speed(m66592
);
1072 m66592
->old_dvsq
= dvsq
;
1075 static void irq_control_stage(struct m66592
*m66592
)
1077 struct usb_ctrlrequest ctrl
;
1080 ctsq
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_CTSQ
;
1081 m66592_write(m66592
, ~M66592_CTRT
, M66592_INTSTS0
);
1084 case M66592_CS_IDST
: {
1085 struct m66592_ep
*ep
;
1086 struct m66592_request
*req
;
1087 ep
= &m66592
->ep
[0];
1088 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1089 transfer_complete(ep
, req
, 0);
1093 case M66592_CS_RDDS
:
1094 case M66592_CS_WRDS
:
1095 case M66592_CS_WRND
:
1096 if (setup_packet(m66592
, &ctrl
)) {
1097 if (m66592
->driver
->setup(&m66592
->gadget
, &ctrl
) < 0)
1098 pipe_stall(m66592
, 0);
1101 case M66592_CS_RDSS
:
1102 case M66592_CS_WRSS
:
1103 control_end(m66592
, 0);
1106 printk(KERN_ERR
"ctrl_stage: unexpect ctsq(%x)\n", ctsq
);
1111 static irqreturn_t
m66592_irq(int irq
, void *_m66592
)
1113 struct m66592
*m66592
= _m66592
;
1116 u16 brdysts
, nrdysts
, bempsts
;
1117 u16 brdyenb
, nrdyenb
, bempenb
;
1121 intsts0
= m66592_read(m66592
, M66592_INTSTS0
);
1122 intenb0
= m66592_read(m66592
, M66592_INTENB0
);
1124 savepipe
= m66592_read(m66592
, M66592_CFIFOSEL
);
1126 mask0
= intsts0
& intenb0
;
1128 brdysts
= m66592_read(m66592
, M66592_BRDYSTS
);
1129 nrdysts
= m66592_read(m66592
, M66592_NRDYSTS
);
1130 bempsts
= m66592_read(m66592
, M66592_BEMPSTS
);
1131 brdyenb
= m66592_read(m66592
, M66592_BRDYENB
);
1132 nrdyenb
= m66592_read(m66592
, M66592_NRDYENB
);
1133 bempenb
= m66592_read(m66592
, M66592_BEMPENB
);
1135 if (mask0
& M66592_VBINT
) {
1136 m66592_write(m66592
, (u16
)~M66592_VBINT
,
1138 m66592_start_xclock(m66592
);
1140 /* start vbus sampling */
1141 m66592
->old_vbus
= m66592_read(m66592
, M66592_INTSTS0
)
1143 m66592
->scount
= M66592_MAX_SAMPLING
;
1145 mod_timer(&m66592
->timer
,
1146 jiffies
+ msecs_to_jiffies(50));
1148 if (intsts0
& M66592_DVSQ
)
1149 irq_device_state(m66592
);
1151 if ((intsts0
& M66592_BRDY
) && (intenb0
& M66592_BRDYE
) &&
1152 (brdysts
& brdyenb
)) {
1153 irq_pipe_ready(m66592
, brdysts
, brdyenb
);
1155 if ((intsts0
& M66592_BEMP
) && (intenb0
& M66592_BEMPE
) &&
1156 (bempsts
& bempenb
)) {
1157 irq_pipe_empty(m66592
, bempsts
, bempenb
);
1160 if (intsts0
& M66592_CTRT
)
1161 irq_control_stage(m66592
);
1164 m66592_write(m66592
, savepipe
, M66592_CFIFOSEL
);
1169 static void m66592_timer(unsigned long _m66592
)
1171 struct m66592
*m66592
= (struct m66592
*)_m66592
;
1172 unsigned long flags
;
1175 spin_lock_irqsave(&m66592
->lock
, flags
);
1176 tmp
= m66592_read(m66592
, M66592_SYSCFG
);
1177 if (!(tmp
& M66592_RCKE
)) {
1178 m66592_bset(m66592
, M66592_RCKE
| M66592_PLLC
, M66592_SYSCFG
);
1180 m66592_bset(m66592
, M66592_SCKE
, M66592_SYSCFG
);
1182 if (m66592
->scount
> 0) {
1183 tmp
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_VBSTS
;
1184 if (tmp
== m66592
->old_vbus
) {
1186 if (m66592
->scount
== 0) {
1187 if (tmp
== M66592_VBSTS
)
1188 m66592_usb_connect(m66592
);
1190 m66592_usb_disconnect(m66592
);
1192 mod_timer(&m66592
->timer
,
1193 jiffies
+ msecs_to_jiffies(50));
1196 m66592
->scount
= M66592_MAX_SAMPLING
;
1197 m66592
->old_vbus
= tmp
;
1198 mod_timer(&m66592
->timer
,
1199 jiffies
+ msecs_to_jiffies(50));
1202 spin_unlock_irqrestore(&m66592
->lock
, flags
);
1205 /*-------------------------------------------------------------------------*/
1206 static int m66592_enable(struct usb_ep
*_ep
,
1207 const struct usb_endpoint_descriptor
*desc
)
1209 struct m66592_ep
*ep
;
1211 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1212 return alloc_pipe_config(ep
, desc
);
1215 static int m66592_disable(struct usb_ep
*_ep
)
1217 struct m66592_ep
*ep
;
1218 struct m66592_request
*req
;
1219 unsigned long flags
;
1221 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1224 while (!list_empty(&ep
->queue
)) {
1225 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1226 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1227 transfer_complete(ep
, req
, -ECONNRESET
);
1228 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1231 pipe_irq_disable(ep
->m66592
, ep
->pipenum
);
1232 return free_pipe_config(ep
);
1235 static struct usb_request
*m66592_alloc_request(struct usb_ep
*_ep
,
1238 struct m66592_request
*req
;
1240 req
= kzalloc(sizeof(struct m66592_request
), gfp_flags
);
1244 INIT_LIST_HEAD(&req
->queue
);
1249 static void m66592_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1251 struct m66592_request
*req
;
1253 req
= container_of(_req
, struct m66592_request
, req
);
1257 static void *m66592_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
1258 dma_addr_t
*dma
, gfp_t gfp_flags
)
1262 buf
= kzalloc(bytes
, gfp_flags
);
1264 *dma
= virt_to_bus(buf
);
1269 static void m66592_free_buffer(struct usb_ep
*_ep
, void *buf
,
1270 dma_addr_t dma
, unsigned bytes
)
1275 static int m66592_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1278 struct m66592_ep
*ep
;
1279 struct m66592_request
*req
;
1280 unsigned long flags
;
1283 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1284 req
= container_of(_req
, struct m66592_request
, req
);
1286 if (ep
->m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1289 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1291 if (list_empty(&ep
->queue
))
1294 list_add_tail(&req
->queue
, &ep
->queue
);
1295 req
->req
.actual
= 0;
1296 req
->req
.status
= -EINPROGRESS
;
1298 if (ep
->desc
== 0) /* control */
1301 if (request
&& !ep
->busy
)
1302 start_packet(ep
, req
);
1305 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1310 static int m66592_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1312 struct m66592_ep
*ep
;
1313 struct m66592_request
*req
;
1314 unsigned long flags
;
1316 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1317 req
= container_of(_req
, struct m66592_request
, req
);
1319 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1320 if (!list_empty(&ep
->queue
))
1321 transfer_complete(ep
, req
, -ECONNRESET
);
1322 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1327 static int m66592_set_halt(struct usb_ep
*_ep
, int value
)
1329 struct m66592_ep
*ep
;
1330 struct m66592_request
*req
;
1331 unsigned long flags
;
1334 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1335 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1337 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1338 if (!list_empty(&ep
->queue
)) {
1344 pipe_stall(ep
->m66592
, ep
->pipenum
);
1347 pipe_stop(ep
->m66592
, ep
->pipenum
);
1351 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1355 static int m66592_fifo_status(struct usb_ep
*_ep
)
1360 static void m66592_fifo_flush(struct usb_ep
*_ep
)
1362 struct m66592_ep
*ep
;
1363 unsigned long flags
;
1365 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1366 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1367 if (list_empty(&ep
->queue
) && !ep
->busy
) {
1368 pipe_stop(ep
->m66592
, ep
->pipenum
);
1369 m66592_bclr(ep
->m66592
, M66592_BCLR
, ep
->fifoctr
);
1371 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1374 static struct usb_ep_ops m66592_ep_ops
= {
1375 .enable
= m66592_enable
,
1376 .disable
= m66592_disable
,
1378 .alloc_request
= m66592_alloc_request
,
1379 .free_request
= m66592_free_request
,
1381 .alloc_buffer
= m66592_alloc_buffer
,
1382 .free_buffer
= m66592_free_buffer
,
1384 .queue
= m66592_queue
,
1385 .dequeue
= m66592_dequeue
,
1387 .set_halt
= m66592_set_halt
,
1388 .fifo_status
= m66592_fifo_status
,
1389 .fifo_flush
= m66592_fifo_flush
,
1392 /*-------------------------------------------------------------------------*/
1393 static struct m66592
*the_controller
;
1395 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1397 struct m66592
*m66592
= the_controller
;
1401 driver
->speed
!= USB_SPEED_HIGH
||
1411 /* hook up the driver */
1412 driver
->driver
.bus
= NULL
;
1413 m66592
->driver
= driver
;
1414 m66592
->gadget
.dev
.driver
= &driver
->driver
;
1416 retval
= device_add(&m66592
->gadget
.dev
);
1418 printk(KERN_ERR
"device_add error (%d)\n", retval
);
1422 retval
= driver
->bind (&m66592
->gadget
);
1424 printk(KERN_ERR
"bind to driver error (%d)\n", retval
);
1425 device_del(&m66592
->gadget
.dev
);
1429 m66592_bset(m66592
, M66592_VBSE
| M66592_URST
, M66592_INTENB0
);
1430 if (m66592_read(m66592
, M66592_INTSTS0
) & M66592_VBSTS
) {
1431 m66592_start_xclock(m66592
);
1432 /* start vbus sampling */
1433 m66592
->old_vbus
= m66592_read(m66592
,
1434 M66592_INTSTS0
) & M66592_VBSTS
;
1435 m66592
->scount
= M66592_MAX_SAMPLING
;
1436 mod_timer(&m66592
->timer
,
1437 jiffies
+ msecs_to_jiffies(50));
1443 m66592
->driver
= NULL
;
1444 m66592
->gadget
.dev
.driver
= NULL
;
1448 EXPORT_SYMBOL(usb_gadget_register_driver
);
1450 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1452 struct m66592
*m66592
= the_controller
;
1453 unsigned long flags
;
1455 spin_lock_irqsave(&m66592
->lock
, flags
);
1456 if (m66592
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
1457 m66592_usb_disconnect(m66592
);
1458 spin_unlock_irqrestore(&m66592
->lock
, flags
);
1460 m66592_bclr(m66592
, M66592_VBSE
| M66592_URST
, M66592_INTENB0
);
1462 driver
->unbind(&m66592
->gadget
);
1464 init_controller(m66592
);
1465 disable_controller(m66592
);
1467 device_del(&m66592
->gadget
.dev
);
1468 m66592
->driver
= NULL
;
1471 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1473 /*-------------------------------------------------------------------------*/
1474 static int m66592_get_frame(struct usb_gadget
*_gadget
)
1476 struct m66592
*m66592
= gadget_to_m66592(_gadget
);
1477 return m66592_read(m66592
, M66592_FRMNUM
) & 0x03FF;
1480 static struct usb_gadget_ops m66592_gadget_ops
= {
1481 .get_frame
= m66592_get_frame
,
1484 #if defined(CONFIG_PM)
1485 static int m66592_suspend(struct platform_device
*pdev
, pm_message_t state
)
1487 pdev
->dev
.power
.power_state
= state
;
1491 static int m66592_resume(struct platform_device
*pdev
)
1493 pdev
->dev
.power
.power_state
= PMSG_ON
;
1496 #else /* if defined(CONFIG_PM) */
1497 #define m66592_suspend NULL
1498 #define m66592_resume NULL
1501 static int __init_or_module
m66592_remove(struct platform_device
*pdev
)
1503 struct m66592
*m66592
= dev_get_drvdata(&pdev
->dev
);
1505 del_timer_sync(&m66592
->timer
);
1506 iounmap(m66592
->reg
);
1507 free_irq(platform_get_irq(pdev
, 0), m66592
);
1512 #define resource_len(r) (((r)->end - (r)->start) + 1)
1513 static int __init
m66592_probe(struct platform_device
*pdev
)
1515 struct resource
*res
= NULL
;
1517 void __iomem
*reg
= NULL
;
1518 struct m66592
*m66592
= NULL
;
1522 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
1526 printk(KERN_ERR
"platform_get_resource_byname error.\n");
1530 irq
= platform_get_irq(pdev
, 0);
1533 printk(KERN_ERR
"platform_get_irq error.\n");
1537 reg
= ioremap(res
->start
, resource_len(res
));
1540 printk(KERN_ERR
"ioremap error.\n");
1544 /* initialize ucd */
1545 m66592
= kzalloc(sizeof(struct m66592
), GFP_KERNEL
);
1546 if (m66592
== NULL
) {
1547 printk(KERN_ERR
"kzalloc error\n");
1551 spin_lock_init(&m66592
->lock
);
1552 dev_set_drvdata(&pdev
->dev
, m66592
);
1554 m66592
->gadget
.ops
= &m66592_gadget_ops
;
1555 device_initialize(&m66592
->gadget
.dev
);
1556 strcpy(m66592
->gadget
.dev
.bus_id
, "gadget");
1557 m66592
->gadget
.is_dualspeed
= 1;
1558 m66592
->gadget
.dev
.parent
= &pdev
->dev
;
1559 m66592
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1560 m66592
->gadget
.dev
.release
= pdev
->dev
.release
;
1561 m66592
->gadget
.name
= udc_name
;
1563 init_timer(&m66592
->timer
);
1564 m66592
->timer
.function
= m66592_timer
;
1565 m66592
->timer
.data
= (unsigned long)m66592
;
1568 m66592
->bi_bufnum
= M66592_BASE_BUFNUM
;
1570 ret
= request_irq(irq
, m66592_irq
, IRQF_DISABLED
| IRQF_SHARED
,
1573 printk(KERN_ERR
"request_irq error (%d)\n", ret
);
1577 INIT_LIST_HEAD(&m66592
->gadget
.ep_list
);
1578 m66592
->gadget
.ep0
= &m66592
->ep
[0].ep
;
1579 INIT_LIST_HEAD(&m66592
->gadget
.ep0
->ep_list
);
1580 for (i
= 0; i
< M66592_MAX_NUM_PIPE
; i
++) {
1581 struct m66592_ep
*ep
= &m66592
->ep
[i
];
1584 INIT_LIST_HEAD(&m66592
->ep
[i
].ep
.ep_list
);
1585 list_add_tail(&m66592
->ep
[i
].ep
.ep_list
,
1586 &m66592
->gadget
.ep_list
);
1588 ep
->m66592
= m66592
;
1589 INIT_LIST_HEAD(&ep
->queue
);
1590 ep
->ep
.name
= m66592_ep_name
[i
];
1591 ep
->ep
.ops
= &m66592_ep_ops
;
1592 ep
->ep
.maxpacket
= 512;
1594 m66592
->ep
[0].ep
.maxpacket
= 64;
1595 m66592
->ep
[0].pipenum
= 0;
1596 m66592
->ep
[0].fifoaddr
= M66592_CFIFO
;
1597 m66592
->ep
[0].fifosel
= M66592_CFIFOSEL
;
1598 m66592
->ep
[0].fifoctr
= M66592_CFIFOCTR
;
1599 m66592
->ep
[0].fifotrn
= 0;
1600 m66592
->ep
[0].pipectr
= get_pipectr_addr(0);
1601 m66592
->pipenum2ep
[0] = &m66592
->ep
[0];
1602 m66592
->epaddr2ep
[0] = &m66592
->ep
[0];
1604 the_controller
= m66592
;
1606 m66592
->ep0_req
= m66592_alloc_request(&m66592
->ep
[0].ep
, GFP_KERNEL
);
1607 if (m66592
->ep0_req
== NULL
)
1609 m66592
->ep0_buf
= m66592_alloc_buffer(&m66592
->ep
[0].ep
, 2, NULL
,
1611 if (m66592
->ep0_buf
== NULL
)
1614 init_controller(m66592
);
1616 printk("driver %s, %s\n", udc_name
, DRIVER_VERSION
);
1621 if (m66592
->ep0_req
)
1622 m66592_free_request(&m66592
->ep
[0].ep
, m66592
->ep0_req
);
1631 /*-------------------------------------------------------------------------*/
1632 static struct platform_driver m66592_driver
= {
1633 .probe
= m66592_probe
,
1634 .remove
= m66592_remove
,
1635 .suspend
= m66592_suspend
,
1636 .resume
= m66592_resume
,
1638 .name
= (char *) udc_name
,
1642 static int __init
m66592_udc_init(void)
1644 return platform_driver_register(&m66592_driver
);
1646 module_init(m66592_udc_init
);
1648 static void __exit
m66592_udc_cleanup(void)
1650 platform_driver_unregister(&m66592_driver
);
1652 module_exit(m66592_udc_cleanup
);