2 * Fusb300 UDC (USB gadget)
4 * Copyright (C) 2010 Faraday Technology Corp.
6 * Author : Yuan-hsin Chen <yhchen@faraday-tech.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 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
20 #include "fusb300_udc.h"
22 MODULE_DESCRIPTION("FUSB300 USB gadget driver");
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
25 MODULE_ALIAS("platform:fusb300_udc");
27 #define DRIVER_VERSION "20 October 2010"
29 static const char udc_name
[] = "fusb300_udc";
30 static const char * const fusb300_ep_name
[] = {
31 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
32 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
35 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
38 static void fusb300_enable_bit(struct fusb300
*fusb300
, u32 offset
,
41 u32 reg
= ioread32(fusb300
->reg
+ offset
);
44 iowrite32(reg
, fusb300
->reg
+ offset
);
47 static void fusb300_disable_bit(struct fusb300
*fusb300
, u32 offset
,
50 u32 reg
= ioread32(fusb300
->reg
+ offset
);
53 iowrite32(reg
, fusb300
->reg
+ offset
);
57 static void fusb300_ep_setting(struct fusb300_ep
*ep
,
58 struct fusb300_ep_info info
)
60 ep
->epnum
= info
.epnum
;
64 static int fusb300_ep_release(struct fusb300_ep
*ep
)
74 static void fusb300_set_fifo_entry(struct fusb300
*fusb300
,
77 u32 val
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
79 val
&= ~FUSB300_EPSET1_FIFOENTRY_MSK
;
80 val
|= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM
);
81 iowrite32(val
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
84 static void fusb300_set_start_entry(struct fusb300
*fusb300
,
87 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
88 u32 start_entry
= fusb300
->fifo_entry_num
* FUSB300_FIFO_ENTRY_NUM
;
90 reg
&= ~FUSB300_EPSET1_START_ENTRY_MSK
;
91 reg
|= FUSB300_EPSET1_START_ENTRY(start_entry
);
92 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
93 if (fusb300
->fifo_entry_num
== FUSB300_MAX_FIFO_ENTRY
) {
94 fusb300
->fifo_entry_num
= 0;
96 pr_err("fifo entry is over the maximum number!\n");
98 fusb300
->fifo_entry_num
++;
101 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
102 static void fusb300_set_epaddrofs(struct fusb300
*fusb300
,
103 struct fusb300_ep_info info
)
105 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
107 reg
&= ~FUSB300_EPSET2_ADDROFS_MSK
;
108 reg
|= FUSB300_EPSET2_ADDROFS(fusb300
->addrofs
);
109 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
110 fusb300
->addrofs
+= (info
.maxpacket
+ 7) / 8 * FUSB300_FIFO_ENTRY_NUM
;
113 static void ep_fifo_setting(struct fusb300
*fusb300
,
114 struct fusb300_ep_info info
)
116 fusb300_set_fifo_entry(fusb300
, info
.epnum
);
117 fusb300_set_start_entry(fusb300
, info
.epnum
);
118 fusb300_set_epaddrofs(fusb300
, info
);
121 static void fusb300_set_eptype(struct fusb300
*fusb300
,
122 struct fusb300_ep_info info
)
124 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
126 reg
&= ~FUSB300_EPSET1_TYPE_MSK
;
127 reg
|= FUSB300_EPSET1_TYPE(info
.type
);
128 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
131 static void fusb300_set_epdir(struct fusb300
*fusb300
,
132 struct fusb300_ep_info info
)
138 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
139 reg
&= ~FUSB300_EPSET1_DIR_MSK
;
140 reg
|= FUSB300_EPSET1_DIRIN
;
141 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
144 static void fusb300_set_ep_active(struct fusb300
*fusb300
,
147 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
149 reg
|= FUSB300_EPSET1_ACTEN
;
150 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
153 static void fusb300_set_epmps(struct fusb300
*fusb300
,
154 struct fusb300_ep_info info
)
156 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
158 reg
&= ~FUSB300_EPSET2_MPS_MSK
;
159 reg
|= FUSB300_EPSET2_MPS(info
.maxpacket
);
160 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
163 static void fusb300_set_interval(struct fusb300
*fusb300
,
164 struct fusb300_ep_info info
)
166 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
168 reg
&= ~FUSB300_EPSET1_INTERVAL(0x7);
169 reg
|= FUSB300_EPSET1_INTERVAL(info
.interval
);
170 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
173 static void fusb300_set_bwnum(struct fusb300
*fusb300
,
174 struct fusb300_ep_info info
)
176 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
178 reg
&= ~FUSB300_EPSET1_BWNUM(0x3);
179 reg
|= FUSB300_EPSET1_BWNUM(info
.bw_num
);
180 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
183 static void set_ep_reg(struct fusb300
*fusb300
,
184 struct fusb300_ep_info info
)
186 fusb300_set_eptype(fusb300
, info
);
187 fusb300_set_epdir(fusb300
, info
);
188 fusb300_set_epmps(fusb300
, info
);
191 fusb300_set_interval(fusb300
, info
);
194 fusb300_set_bwnum(fusb300
, info
);
196 fusb300_set_ep_active(fusb300
, info
.epnum
);
199 static int config_ep(struct fusb300_ep
*ep
,
200 const struct usb_endpoint_descriptor
*desc
)
202 struct fusb300
*fusb300
= ep
->fusb300
;
203 struct fusb300_ep_info info
;
211 info
.type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
212 info
.dir_in
= (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ? 1 : 0;
213 info
.maxpacket
= usb_endpoint_maxp(desc
);
214 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
216 if ((info
.type
== USB_ENDPOINT_XFER_INT
) ||
217 (info
.type
== USB_ENDPOINT_XFER_ISOC
)) {
218 info
.interval
= desc
->bInterval
;
219 if (info
.type
== USB_ENDPOINT_XFER_ISOC
)
220 info
.bw_num
= ((desc
->wMaxPacketSize
& 0x1800) >> 11);
223 ep_fifo_setting(fusb300
, info
);
225 set_ep_reg(fusb300
, info
);
227 fusb300_ep_setting(ep
, info
);
229 fusb300
->ep
[info
.epnum
] = ep
;
234 static int fusb300_enable(struct usb_ep
*_ep
,
235 const struct usb_endpoint_descriptor
*desc
)
237 struct fusb300_ep
*ep
;
239 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
241 if (ep
->fusb300
->reenum
) {
242 ep
->fusb300
->fifo_entry_num
= 0;
243 ep
->fusb300
->addrofs
= 0;
244 ep
->fusb300
->reenum
= 0;
247 return config_ep(ep
, desc
);
250 static int fusb300_disable(struct usb_ep
*_ep
)
252 struct fusb300_ep
*ep
;
253 struct fusb300_request
*req
;
256 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
260 while (!list_empty(&ep
->queue
)) {
261 req
= list_entry(ep
->queue
.next
, struct fusb300_request
, queue
);
262 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
263 done(ep
, req
, -ECONNRESET
);
264 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
267 return fusb300_ep_release(ep
);
270 static struct usb_request
*fusb300_alloc_request(struct usb_ep
*_ep
,
273 struct fusb300_request
*req
;
275 req
= kzalloc(sizeof(struct fusb300_request
), gfp_flags
);
278 INIT_LIST_HEAD(&req
->queue
);
283 static void fusb300_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
285 struct fusb300_request
*req
;
287 req
= container_of(_req
, struct fusb300_request
, req
);
291 static int enable_fifo_int(struct fusb300_ep
*ep
)
293 struct fusb300
*fusb300
= ep
->fusb300
;
296 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
297 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
299 pr_err("can't enable_fifo_int ep0\n");
306 static int disable_fifo_int(struct fusb300_ep
*ep
)
308 struct fusb300
*fusb300
= ep
->fusb300
;
311 fusb300_disable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
312 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
314 pr_err("can't disable_fifo_int ep0\n");
321 static void fusb300_set_cxlen(struct fusb300
*fusb300
, u32 length
)
325 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
326 reg
&= ~FUSB300_CSR_LEN_MSK
;
327 reg
|= FUSB300_CSR_LEN(length
);
328 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_CSR
);
331 /* write data to cx fifo */
332 static void fusb300_wrcxf(struct fusb300_ep
*ep
,
333 struct fusb300_request
*req
)
338 struct fusb300
*fusb300
= ep
->fusb300
;
339 u32 length
= req
->req
.length
- req
->req
.actual
;
341 tmp
= req
->req
.buf
+ req
->req
.actual
;
343 if (length
> SS_CTL_MAX_PACKET_SIZE
) {
344 fusb300_set_cxlen(fusb300
, SS_CTL_MAX_PACKET_SIZE
);
345 for (i
= (SS_CTL_MAX_PACKET_SIZE
>> 2); i
> 0; i
--) {
346 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
348 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
351 req
->req
.actual
+= SS_CTL_MAX_PACKET_SIZE
;
352 } else { /* length is less than max packet size */
353 fusb300_set_cxlen(fusb300
, length
);
354 for (i
= length
>> 2; i
> 0; i
--) {
355 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
357 printk(KERN_DEBUG
" 0x%x\n", data
);
358 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
361 switch (length
% 4) {
364 printk(KERN_DEBUG
" 0x%x\n", data
);
365 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
368 data
= *tmp
| *(tmp
+ 1) << 8;
369 printk(KERN_DEBUG
" 0x%x\n", data
);
370 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
373 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
374 printk(KERN_DEBUG
" 0x%x\n", data
);
375 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
380 req
->req
.actual
+= length
;
384 static void fusb300_set_epnstall(struct fusb300
*fusb300
, u8 ep
)
386 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
390 static void fusb300_clear_epnstall(struct fusb300
*fusb300
, u8 ep
)
392 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
394 if (reg
& FUSB300_EPSET0_STL
) {
395 printk(KERN_DEBUG
"EP%d stall... Clear!!\n", ep
);
396 reg
&= ~FUSB300_EPSET0_STL
;
397 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
401 static void ep0_queue(struct fusb300_ep
*ep
, struct fusb300_request
*req
)
403 if (ep
->fusb300
->ep0_dir
) { /* if IN */
404 if (req
->req
.length
) {
405 fusb300_wrcxf(ep
, req
);
407 printk(KERN_DEBUG
"%s : req->req.length = 0x%x\n",
408 __func__
, req
->req
.length
);
409 if ((req
->req
.length
== req
->req
.actual
) ||
410 (req
->req
.actual
< ep
->ep
.maxpacket
))
413 if (!req
->req
.length
)
416 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER1
,
417 FUSB300_IGER1_CX_OUT_INT
);
421 static int fusb300_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
424 struct fusb300_ep
*ep
;
425 struct fusb300_request
*req
;
429 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
430 req
= container_of(_req
, struct fusb300_request
, req
);
432 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
435 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
437 if (list_empty(&ep
->queue
))
440 list_add_tail(&req
->queue
, &ep
->queue
);
443 req
->req
.status
= -EINPROGRESS
;
445 if (ep
->desc
== NULL
) /* ep0 */
447 else if (request
&& !ep
->stall
)
450 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
455 static int fusb300_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
457 struct fusb300_ep
*ep
;
458 struct fusb300_request
*req
;
461 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
462 req
= container_of(_req
, struct fusb300_request
, req
);
464 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
465 if (!list_empty(&ep
->queue
))
466 done(ep
, req
, -ECONNRESET
);
467 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
472 static int fusb300_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
474 struct fusb300_ep
*ep
;
475 struct fusb300
*fusb300
;
479 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
481 fusb300
= ep
->fusb300
;
483 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
485 if (!list_empty(&ep
->queue
)) {
491 fusb300_set_epnstall(fusb300
, ep
->epnum
);
496 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
502 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
506 static int fusb300_set_halt(struct usb_ep
*_ep
, int value
)
508 return fusb300_set_halt_and_wedge(_ep
, value
, 0);
511 static int fusb300_set_wedge(struct usb_ep
*_ep
)
513 return fusb300_set_halt_and_wedge(_ep
, 1, 1);
516 static void fusb300_fifo_flush(struct usb_ep
*_ep
)
520 static struct usb_ep_ops fusb300_ep_ops
= {
521 .enable
= fusb300_enable
,
522 .disable
= fusb300_disable
,
524 .alloc_request
= fusb300_alloc_request
,
525 .free_request
= fusb300_free_request
,
527 .queue
= fusb300_queue
,
528 .dequeue
= fusb300_dequeue
,
530 .set_halt
= fusb300_set_halt
,
531 .fifo_flush
= fusb300_fifo_flush
,
532 .set_wedge
= fusb300_set_wedge
,
535 /*****************************************************************************/
536 static void fusb300_clear_int(struct fusb300
*fusb300
, u32 offset
,
539 iowrite32(value
, fusb300
->reg
+ offset
);
542 static void fusb300_reset(void)
546 static void fusb300_set_cxstall(struct fusb300
*fusb300
)
548 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
552 static void fusb300_set_cxdone(struct fusb300
*fusb300
)
554 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
558 /* read data from cx fifo */
559 void fusb300_rdcxf(struct fusb300
*fusb300
,
560 u8
*buffer
, u32 length
)
568 for (i
= (length
>> 2); i
> 0; i
--) {
569 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
570 printk(KERN_DEBUG
" 0x%x\n", data
);
572 *(tmp
+ 1) = (data
>> 8) & 0xFF;
573 *(tmp
+ 2) = (data
>> 16) & 0xFF;
574 *(tmp
+ 3) = (data
>> 24) & 0xFF;
578 switch (length
% 4) {
580 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
581 printk(KERN_DEBUG
" 0x%x\n", data
);
585 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
586 printk(KERN_DEBUG
" 0x%x\n", data
);
588 *(tmp
+ 1) = (data
>> 8) & 0xFF;
591 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
592 printk(KERN_DEBUG
" 0x%x\n", data
);
594 *(tmp
+ 1) = (data
>> 8) & 0xFF;
595 *(tmp
+ 2) = (data
>> 16) & 0xFF;
602 static void fusb300_rdfifo(struct fusb300_ep
*ep
,
603 struct fusb300_request
*req
,
609 struct fusb300
*fusb300
= ep
->fusb300
;
611 tmp
= req
->req
.buf
+ req
->req
.actual
;
612 req
->req
.actual
+= length
;
614 if (req
->req
.actual
> req
->req
.length
)
615 printk(KERN_DEBUG
"req->req.actual > req->req.length\n");
617 for (i
= (length
>> 2); i
> 0; i
--) {
618 data
= ioread32(fusb300
->reg
+
619 FUSB300_OFFSET_EPPORT(ep
->epnum
));
621 *(tmp
+ 1) = (data
>> 8) & 0xFF;
622 *(tmp
+ 2) = (data
>> 16) & 0xFF;
623 *(tmp
+ 3) = (data
>> 24) & 0xFF;
627 switch (length
% 4) {
629 data
= ioread32(fusb300
->reg
+
630 FUSB300_OFFSET_EPPORT(ep
->epnum
));
634 data
= ioread32(fusb300
->reg
+
635 FUSB300_OFFSET_EPPORT(ep
->epnum
));
637 *(tmp
+ 1) = (data
>> 8) & 0xFF;
640 data
= ioread32(fusb300
->reg
+
641 FUSB300_OFFSET_EPPORT(ep
->epnum
));
643 *(tmp
+ 1) = (data
>> 8) & 0xFF;
644 *(tmp
+ 2) = (data
>> 16) & 0xFF;
651 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
652 reg
&= FUSB300_IGR1_SYNF0_EMPTY_INT
;
654 printk(KERN_INFO
"sync fifo is not empty!\n");
659 static u8
fusb300_get_epnstall(struct fusb300
*fusb300
, u8 ep
)
662 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
664 value
= reg
& FUSB300_EPSET0_STL
;
669 static u8
fusb300_get_cxstall(struct fusb300
*fusb300
)
672 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
674 value
= (reg
& FUSB300_CSR_STL
) >> 1;
679 static void request_error(struct fusb300
*fusb300
)
681 fusb300_set_cxstall(fusb300
);
682 printk(KERN_DEBUG
"request error!!\n");
685 static void get_status(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
686 __releases(fusb300
->lock
)
687 __acquires(fusb300
->lock
)
691 u16 w_index
= ctrl
->wIndex
;
693 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
694 case USB_RECIP_DEVICE
:
695 status
= 1 << USB_DEVICE_SELF_POWERED
;
697 case USB_RECIP_INTERFACE
:
700 case USB_RECIP_ENDPOINT
:
701 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
703 if (fusb300_get_epnstall(fusb300
, ep
))
704 status
= 1 << USB_ENDPOINT_HALT
;
706 if (fusb300_get_cxstall(fusb300
))
712 request_error(fusb300
);
716 fusb300
->ep0_data
= cpu_to_le16(status
);
717 fusb300
->ep0_req
->buf
= &fusb300
->ep0_data
;
718 fusb300
->ep0_req
->length
= 2;
720 spin_unlock(&fusb300
->lock
);
721 fusb300_queue(fusb300
->gadget
.ep0
, fusb300
->ep0_req
, GFP_KERNEL
);
722 spin_lock(&fusb300
->lock
);
725 static void set_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
729 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
730 case USB_RECIP_DEVICE
:
731 fusb300_set_cxdone(fusb300
);
733 case USB_RECIP_INTERFACE
:
734 fusb300_set_cxdone(fusb300
);
736 case USB_RECIP_ENDPOINT
: {
737 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
739 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
741 fusb300_set_epnstall(fusb300
, ep
);
743 fusb300_set_cxstall(fusb300
);
744 fusb300_set_cxdone(fusb300
);
748 request_error(fusb300
);
753 static void fusb300_clear_seqnum(struct fusb300
*fusb300
, u8 ep
)
755 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
756 FUSB300_EPSET0_CLRSEQNUM
);
759 static void clear_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
761 struct fusb300_ep
*ep
=
762 fusb300
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
764 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
765 case USB_RECIP_DEVICE
:
766 fusb300_set_cxdone(fusb300
);
768 case USB_RECIP_INTERFACE
:
769 fusb300_set_cxdone(fusb300
);
771 case USB_RECIP_ENDPOINT
:
772 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
774 fusb300_set_cxdone(fusb300
);
779 fusb300_clear_seqnum(fusb300
, ep
->epnum
);
780 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
781 if (!list_empty(&ep
->queue
))
785 fusb300_set_cxdone(fusb300
);
788 request_error(fusb300
);
793 static void fusb300_set_dev_addr(struct fusb300
*fusb300
, u16 addr
)
795 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_DAR
);
797 reg
&= ~FUSB300_DAR_DRVADDR_MSK
;
798 reg
|= FUSB300_DAR_DRVADDR(addr
);
800 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_DAR
);
803 static void set_address(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
805 if (ctrl
->wValue
>= 0x0100)
806 request_error(fusb300
);
808 fusb300_set_dev_addr(fusb300
, ctrl
->wValue
);
809 fusb300_set_cxdone(fusb300
);
813 #define UVC_COPY_DESCRIPTORS(mem, src) \
815 const struct usb_descriptor_header * const *__src; \
816 for (__src = src; *__src; ++__src) { \
817 memcpy(mem, *__src, (*__src)->bLength); \
818 mem += (*__src)->bLength; \
822 static int setup_packet(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
828 fusb300_rdcxf(fusb300
, p
, 8);
829 fusb300
->ep0_dir
= ctrl
->bRequestType
& USB_DIR_IN
;
830 fusb300
->ep0_length
= ctrl
->wLength
;
833 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
834 switch (ctrl
->bRequest
) {
835 case USB_REQ_GET_STATUS
:
836 get_status(fusb300
, ctrl
);
838 case USB_REQ_CLEAR_FEATURE
:
839 clear_feature(fusb300
, ctrl
);
841 case USB_REQ_SET_FEATURE
:
842 set_feature(fusb300
, ctrl
);
844 case USB_REQ_SET_ADDRESS
:
845 set_address(fusb300
, ctrl
);
847 case USB_REQ_SET_CONFIGURATION
:
848 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_DAR
,
849 FUSB300_DAR_SETCONFG
);
850 /* clear sequence number */
851 for (i
= 1; i
<= FUSB300_MAX_NUM_EP
; i
++)
852 fusb300_clear_seqnum(fusb300
, i
);
866 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
869 list_del_init(&req
->queue
);
871 /* don't modify queue heads during completion callback */
872 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
873 req
->req
.status
= -ESHUTDOWN
;
875 req
->req
.status
= status
;
877 spin_unlock(&ep
->fusb300
->lock
);
878 req
->req
.complete(&ep
->ep
, &req
->req
);
879 spin_lock(&ep
->fusb300
->lock
);
882 disable_fifo_int(ep
);
883 if (!list_empty(&ep
->queue
))
886 fusb300_set_cxdone(ep
->fusb300
);
889 static void fusb300_fill_idma_prdtbl(struct fusb300_ep
*ep
, dma_addr_t d
,
897 reg
= ioread32(ep
->fusb300
->reg
+
898 FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
899 reg
&= FUSB300_EPPRD0_H
;
902 iowrite32(d
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W1(ep
->epnum
));
904 value
= FUSB300_EPPRD0_BTC(len
) | FUSB300_EPPRD0_H
|
905 FUSB300_EPPRD0_F
| FUSB300_EPPRD0_L
| FUSB300_EPPRD0_I
;
906 iowrite32(value
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
908 iowrite32(0x0, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W2(ep
->epnum
));
910 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_EPPRDRDY
,
911 FUSB300_EPPRDR_EP_PRD_RDY(ep
->epnum
));
914 static void fusb300_wait_idma_finished(struct fusb300_ep
*ep
)
919 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
920 if ((reg
& FUSB300_IGR1_VBUS_CHG_INT
) ||
921 (reg
& FUSB300_IGR1_WARM_RST_INT
) ||
922 (reg
& FUSB300_IGR1_HOT_RST_INT
) ||
923 (reg
& FUSB300_IGR1_USBRST_INT
)
926 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
927 reg
&= FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
);
930 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGR0
,
931 FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
));
933 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
934 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
937 static void fusb300_set_idma(struct fusb300_ep
*ep
,
938 struct fusb300_request
*req
)
942 d
= dma_map_single(NULL
, req
->req
.buf
, req
->req
.length
, DMA_TO_DEVICE
);
944 if (dma_mapping_error(NULL
, d
)) {
945 printk(KERN_DEBUG
"dma_mapping_error\n");
949 dma_sync_single_for_device(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
951 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
952 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
954 fusb300_fill_idma_prdtbl(ep
, d
, req
->req
.length
);
955 /* check idma is done */
956 fusb300_wait_idma_finished(ep
);
958 dma_unmap_single(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
961 static void in_ep_fifo_handler(struct fusb300_ep
*ep
)
963 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
964 struct fusb300_request
, queue
);
967 fusb300_set_idma(ep
, req
);
971 static void out_ep_fifo_handler(struct fusb300_ep
*ep
)
973 struct fusb300
*fusb300
= ep
->fusb300
;
974 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
975 struct fusb300_request
, queue
);
976 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
977 u32 length
= reg
& FUSB300_FFR_BYCNT
;
979 fusb300_rdfifo(ep
, req
, length
);
981 /* finish out transfer */
982 if ((req
->req
.length
== req
->req
.actual
) || (length
< ep
->ep
.maxpacket
))
986 static void check_device_mode(struct fusb300
*fusb300
)
988 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_GCR
);
990 switch (reg
& FUSB300_GCR_DEVEN_MSK
) {
991 case FUSB300_GCR_DEVEN_SS
:
992 fusb300
->gadget
.speed
= USB_SPEED_SUPER
;
994 case FUSB300_GCR_DEVEN_HS
:
995 fusb300
->gadget
.speed
= USB_SPEED_HIGH
;
997 case FUSB300_GCR_DEVEN_FS
:
998 fusb300
->gadget
.speed
= USB_SPEED_FULL
;
1001 fusb300
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1004 printk(KERN_INFO
"dev_mode = %d\n", (reg
& FUSB300_GCR_DEVEN_MSK
));
1008 static void fusb300_ep0out(struct fusb300
*fusb300
)
1010 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1013 if (!list_empty(&ep
->queue
)) {
1014 struct fusb300_request
*req
;
1016 req
= list_first_entry(&ep
->queue
,
1017 struct fusb300_request
, queue
);
1018 if (req
->req
.length
)
1019 fusb300_rdcxf(ep
->fusb300
, req
->req
.buf
,
1022 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1023 reg
&= ~FUSB300_IGER1_CX_OUT_INT
;
1024 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1026 pr_err("%s : empty queue\n", __func__
);
1029 static void fusb300_ep0in(struct fusb300
*fusb300
)
1031 struct fusb300_request
*req
;
1032 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1034 if ((!list_empty(&ep
->queue
)) && (fusb300
->ep0_dir
)) {
1035 req
= list_entry(ep
->queue
.next
,
1036 struct fusb300_request
, queue
);
1037 if (req
->req
.length
)
1038 fusb300_wrcxf(ep
, req
);
1039 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
1042 fusb300_set_cxdone(fusb300
);
1045 static void fusb300_grp2_handler(void)
1049 static void fusb300_grp3_handler(void)
1053 static void fusb300_grp4_handler(void)
1057 static void fusb300_grp5_handler(void)
1061 static irqreturn_t
fusb300_irq(int irq
, void *_fusb300
)
1063 struct fusb300
*fusb300
= _fusb300
;
1064 u32 int_grp1
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1065 u32 int_grp1_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1066 u32 int_grp0
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1067 u32 int_grp0_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER0
);
1068 struct usb_ctrlrequest ctrl
;
1073 spin_lock(&fusb300
->lock
);
1075 int_grp1
&= int_grp1_en
;
1076 int_grp0
&= int_grp0_en
;
1078 if (int_grp1
& FUSB300_IGR1_WARM_RST_INT
) {
1079 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1080 FUSB300_IGR1_WARM_RST_INT
);
1081 printk(KERN_INFO
"fusb300_warmreset\n");
1085 if (int_grp1
& FUSB300_IGR1_HOT_RST_INT
) {
1086 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1087 FUSB300_IGR1_HOT_RST_INT
);
1088 printk(KERN_INFO
"fusb300_hotreset\n");
1092 if (int_grp1
& FUSB300_IGR1_USBRST_INT
) {
1093 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1094 FUSB300_IGR1_USBRST_INT
);
1097 /* COMABT_INT has a highest priority */
1099 if (int_grp1
& FUSB300_IGR1_CX_COMABT_INT
) {
1100 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1101 FUSB300_IGR1_CX_COMABT_INT
);
1102 printk(KERN_INFO
"fusb300_ep0abt\n");
1105 if (int_grp1
& FUSB300_IGR1_VBUS_CHG_INT
) {
1106 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1107 FUSB300_IGR1_VBUS_CHG_INT
);
1108 printk(KERN_INFO
"fusb300_vbus_change\n");
1111 if (int_grp1
& FUSB300_IGR1_U3_EXIT_FAIL_INT
) {
1112 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1113 FUSB300_IGR1_U3_EXIT_FAIL_INT
);
1116 if (int_grp1
& FUSB300_IGR1_U2_EXIT_FAIL_INT
) {
1117 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1118 FUSB300_IGR1_U2_EXIT_FAIL_INT
);
1121 if (int_grp1
& FUSB300_IGR1_U1_EXIT_FAIL_INT
) {
1122 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1123 FUSB300_IGR1_U1_EXIT_FAIL_INT
);
1126 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_FAIL_INT
) {
1127 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1128 FUSB300_IGR1_U2_ENTRY_FAIL_INT
);
1131 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_FAIL_INT
) {
1132 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1133 FUSB300_IGR1_U1_ENTRY_FAIL_INT
);
1136 if (int_grp1
& FUSB300_IGR1_U3_EXIT_INT
) {
1137 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1138 FUSB300_IGR1_U3_EXIT_INT
);
1139 printk(KERN_INFO
"FUSB300_IGR1_U3_EXIT_INT\n");
1142 if (int_grp1
& FUSB300_IGR1_U2_EXIT_INT
) {
1143 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1144 FUSB300_IGR1_U2_EXIT_INT
);
1145 printk(KERN_INFO
"FUSB300_IGR1_U2_EXIT_INT\n");
1148 if (int_grp1
& FUSB300_IGR1_U1_EXIT_INT
) {
1149 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1150 FUSB300_IGR1_U1_EXIT_INT
);
1151 printk(KERN_INFO
"FUSB300_IGR1_U1_EXIT_INT\n");
1154 if (int_grp1
& FUSB300_IGR1_U3_ENTRY_INT
) {
1155 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1156 FUSB300_IGR1_U3_ENTRY_INT
);
1157 printk(KERN_INFO
"FUSB300_IGR1_U3_ENTRY_INT\n");
1158 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_SSCR1
,
1159 FUSB300_SSCR1_GO_U3_DONE
);
1162 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_INT
) {
1163 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1164 FUSB300_IGR1_U2_ENTRY_INT
);
1165 printk(KERN_INFO
"FUSB300_IGR1_U2_ENTRY_INT\n");
1168 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_INT
) {
1169 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1170 FUSB300_IGR1_U1_ENTRY_INT
);
1171 printk(KERN_INFO
"FUSB300_IGR1_U1_ENTRY_INT\n");
1174 if (int_grp1
& FUSB300_IGR1_RESM_INT
) {
1175 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1176 FUSB300_IGR1_RESM_INT
);
1177 printk(KERN_INFO
"fusb300_resume\n");
1180 if (int_grp1
& FUSB300_IGR1_SUSP_INT
) {
1181 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1182 FUSB300_IGR1_SUSP_INT
);
1183 printk(KERN_INFO
"fusb300_suspend\n");
1186 if (int_grp1
& FUSB300_IGR1_HS_LPM_INT
) {
1187 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1188 FUSB300_IGR1_HS_LPM_INT
);
1189 printk(KERN_INFO
"fusb300_HS_LPM_INT\n");
1192 if (int_grp1
& FUSB300_IGR1_DEV_MODE_CHG_INT
) {
1193 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1194 FUSB300_IGR1_DEV_MODE_CHG_INT
);
1195 check_device_mode(fusb300
);
1198 if (int_grp1
& FUSB300_IGR1_CX_COMFAIL_INT
) {
1199 fusb300_set_cxstall(fusb300
);
1200 printk(KERN_INFO
"fusb300_ep0fail\n");
1203 if (int_grp1
& FUSB300_IGR1_CX_SETUP_INT
) {
1204 printk(KERN_INFO
"fusb300_ep0setup\n");
1205 if (setup_packet(fusb300
, &ctrl
)) {
1206 spin_unlock(&fusb300
->lock
);
1207 if (fusb300
->driver
->setup(&fusb300
->gadget
, &ctrl
) < 0)
1208 fusb300_set_cxstall(fusb300
);
1209 spin_lock(&fusb300
->lock
);
1213 if (int_grp1
& FUSB300_IGR1_CX_CMDEND_INT
)
1214 printk(KERN_INFO
"fusb300_cmdend\n");
1217 if (int_grp1
& FUSB300_IGR1_CX_OUT_INT
) {
1218 printk(KERN_INFO
"fusb300_cxout\n");
1219 fusb300_ep0out(fusb300
);
1222 if (int_grp1
& FUSB300_IGR1_CX_IN_INT
) {
1223 printk(KERN_INFO
"fusb300_cxin\n");
1224 fusb300_ep0in(fusb300
);
1227 if (int_grp1
& FUSB300_IGR1_INTGRP5
)
1228 fusb300_grp5_handler();
1230 if (int_grp1
& FUSB300_IGR1_INTGRP4
)
1231 fusb300_grp4_handler();
1233 if (int_grp1
& FUSB300_IGR1_INTGRP3
)
1234 fusb300_grp3_handler();
1236 if (int_grp1
& FUSB300_IGR1_INTGRP2
)
1237 fusb300_grp2_handler();
1240 for (i
= 1; i
< FUSB300_MAX_NUM_EP
; i
++) {
1241 if (int_grp0
& FUSB300_IGR0_EPn_FIFO_INT(i
)) {
1242 reg
= ioread32(fusb300
->reg
+
1243 FUSB300_OFFSET_EPSET1(i
));
1244 in
= (reg
& FUSB300_EPSET1_DIRIN
) ? 1 : 0;
1246 in_ep_fifo_handler(fusb300
->ep
[i
]);
1248 out_ep_fifo_handler(fusb300
->ep
[i
]);
1253 spin_unlock(&fusb300
->lock
);
1258 static void fusb300_set_u2_timeout(struct fusb300
*fusb300
,
1263 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1265 reg
|= FUSB300_SSCR2_U2TIMEOUT(time
);
1267 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1270 static void fusb300_set_u1_timeout(struct fusb300
*fusb300
,
1275 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1276 reg
&= ~(0xff << 8);
1277 reg
|= FUSB300_SSCR2_U1TIMEOUT(time
);
1279 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1282 static void init_controller(struct fusb300
*fusb300
)
1289 mask
= val
= FUSB300_AHBBCR_S0_SPLIT_ON
| FUSB300_AHBBCR_S1_SPLIT_ON
;
1290 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1293 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1295 /* enable high-speed LPM */
1296 mask
= val
= FUSB300_HSCR_HS_LPM_PERMIT
;
1297 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1300 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1302 /*set u1 u2 timmer*/
1303 fusb300_set_u2_timeout(fusb300
, 0xff);
1304 fusb300_set_u1_timeout(fusb300
, 0xff);
1306 /* enable all grp1 interrupt */
1307 iowrite32(0xcfffff9f, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1309 /*------------------------------------------------------------------------*/
1310 static struct fusb300
*the_controller
;
1312 static int fusb300_udc_start(struct usb_gadget_driver
*driver
,
1313 int (*bind
)(struct usb_gadget
*))
1315 struct fusb300
*fusb300
= the_controller
;
1319 || driver
->speed
< USB_SPEED_FULL
1327 if (fusb300
->driver
)
1330 /* hook up the driver */
1331 driver
->driver
.bus
= NULL
;
1332 fusb300
->driver
= driver
;
1333 fusb300
->gadget
.dev
.driver
= &driver
->driver
;
1335 retval
= device_add(&fusb300
->gadget
.dev
);
1337 pr_err("device_add error (%d)\n", retval
);
1341 retval
= bind(&fusb300
->gadget
);
1343 pr_err("bind to driver error (%d)\n", retval
);
1344 device_del(&fusb300
->gadget
.dev
);
1351 fusb300
->driver
= NULL
;
1352 fusb300
->gadget
.dev
.driver
= NULL
;
1357 static int fusb300_udc_stop(struct usb_gadget_driver
*driver
)
1359 struct fusb300
*fusb300
= the_controller
;
1361 if (driver
!= fusb300
->driver
|| !driver
->unbind
)
1364 driver
->unbind(&fusb300
->gadget
);
1365 fusb300
->gadget
.dev
.driver
= NULL
;
1367 init_controller(fusb300
);
1368 device_del(&fusb300
->gadget
.dev
);
1369 fusb300
->driver
= NULL
;
1373 /*--------------------------------------------------------------------------*/
1375 static int fusb300_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
1380 static struct usb_gadget_ops fusb300_gadget_ops
= {
1381 .pullup
= fusb300_udc_pullup
,
1382 .start
= fusb300_udc_start
,
1383 .stop
= fusb300_udc_stop
,
1386 static int __exit
fusb300_remove(struct platform_device
*pdev
)
1388 struct fusb300
*fusb300
= dev_get_drvdata(&pdev
->dev
);
1390 usb_del_gadget_udc(&fusb300
->gadget
);
1391 iounmap(fusb300
->reg
);
1392 free_irq(platform_get_irq(pdev
, 0), fusb300
);
1394 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1400 static int __init
fusb300_probe(struct platform_device
*pdev
)
1402 struct resource
*res
, *ires
, *ires1
;
1403 void __iomem
*reg
= NULL
;
1404 struct fusb300
*fusb300
= NULL
;
1405 struct fusb300_ep
*_ep
[FUSB300_MAX_NUM_EP
];
1409 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1412 pr_err("platform_get_resource error.\n");
1416 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1420 "platform_get_resource IORESOURCE_IRQ error.\n");
1424 ires1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1428 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1432 reg
= ioremap(res
->start
, resource_size(res
));
1435 pr_err("ioremap error.\n");
1439 /* initialize udc */
1440 fusb300
= kzalloc(sizeof(struct fusb300
), GFP_KERNEL
);
1441 if (fusb300
== NULL
) {
1442 pr_err("kzalloc error\n");
1446 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1447 _ep
[i
] = kzalloc(sizeof(struct fusb300_ep
), GFP_KERNEL
);
1448 if (_ep
[i
] == NULL
) {
1449 pr_err("_ep kzalloc error\n");
1452 fusb300
->ep
[i
] = _ep
[i
];
1455 spin_lock_init(&fusb300
->lock
);
1457 dev_set_drvdata(&pdev
->dev
, fusb300
);
1459 fusb300
->gadget
.ops
= &fusb300_gadget_ops
;
1461 device_initialize(&fusb300
->gadget
.dev
);
1463 dev_set_name(&fusb300
->gadget
.dev
, "gadget");
1465 fusb300
->gadget
.is_dualspeed
= 1;
1466 fusb300
->gadget
.dev
.parent
= &pdev
->dev
;
1467 fusb300
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1468 fusb300
->gadget
.dev
.release
= pdev
->dev
.release
;
1469 fusb300
->gadget
.name
= udc_name
;
1472 ret
= request_irq(ires
->start
, fusb300_irq
, IRQF_SHARED
,
1475 pr_err("request_irq error (%d)\n", ret
);
1479 ret
= request_irq(ires1
->start
, fusb300_irq
,
1480 IRQF_SHARED
, udc_name
, fusb300
);
1482 pr_err("request_irq1 error (%d)\n", ret
);
1486 INIT_LIST_HEAD(&fusb300
->gadget
.ep_list
);
1488 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1489 struct fusb300_ep
*ep
= fusb300
->ep
[i
];
1492 INIT_LIST_HEAD(&fusb300
->ep
[i
]->ep
.ep_list
);
1493 list_add_tail(&fusb300
->ep
[i
]->ep
.ep_list
,
1494 &fusb300
->gadget
.ep_list
);
1496 ep
->fusb300
= fusb300
;
1497 INIT_LIST_HEAD(&ep
->queue
);
1498 ep
->ep
.name
= fusb300_ep_name
[i
];
1499 ep
->ep
.ops
= &fusb300_ep_ops
;
1500 ep
->ep
.maxpacket
= HS_BULK_MAX_PACKET_SIZE
;
1502 fusb300
->ep
[0]->ep
.maxpacket
= HS_CTL_MAX_PACKET_SIZE
;
1503 fusb300
->ep
[0]->epnum
= 0;
1504 fusb300
->gadget
.ep0
= &fusb300
->ep
[0]->ep
;
1505 INIT_LIST_HEAD(&fusb300
->gadget
.ep0
->ep_list
);
1507 the_controller
= fusb300
;
1509 fusb300
->ep0_req
= fusb300_alloc_request(&fusb300
->ep
[0]->ep
,
1511 if (fusb300
->ep0_req
== NULL
)
1514 init_controller(fusb300
);
1515 ret
= usb_add_gadget_udc(&pdev
->dev
, &fusb300
->gadget
);
1519 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1523 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1526 free_irq(ires
->start
, fusb300
);
1530 if (fusb300
->ep0_req
)
1531 fusb300_free_request(&fusb300
->ep
[0]->ep
,
1541 static struct platform_driver fusb300_driver
= {
1542 .remove
= __exit_p(fusb300_remove
),
1544 .name
= (char *) udc_name
,
1545 .owner
= THIS_MODULE
,
1549 static int __init
fusb300_udc_init(void)
1551 return platform_driver_probe(&fusb300_driver
, fusb300_probe
);
1554 module_init(fusb300_udc_init
);
1556 static void __exit
fusb300_udc_cleanup(void)
1558 platform_driver_unregister(&fusb300_driver
);
1560 module_exit(fusb300_udc_cleanup
);