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 * 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
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/interrupt.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
31 #include "fusb300_udc.h"
33 MODULE_DESCRIPTION("FUSB300 USB gadget driver");
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
36 MODULE_ALIAS("platform:fusb300_udc");
38 #define DRIVER_VERSION "20 October 2010"
40 static const char udc_name
[] = "fusb300_udc";
41 static const char * const fusb300_ep_name
[] = {
42 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
43 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
46 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
49 static void fusb300_enable_bit(struct fusb300
*fusb300
, u32 offset
,
52 u32 reg
= ioread32(fusb300
->reg
+ offset
);
55 iowrite32(reg
, fusb300
->reg
+ offset
);
58 static void fusb300_disable_bit(struct fusb300
*fusb300
, u32 offset
,
61 u32 reg
= ioread32(fusb300
->reg
+ offset
);
64 iowrite32(reg
, fusb300
->reg
+ offset
);
68 static void fusb300_ep_setting(struct fusb300_ep
*ep
,
69 struct fusb300_ep_info info
)
71 ep
->epnum
= info
.epnum
;
75 static int fusb300_ep_release(struct fusb300_ep
*ep
)
85 static void fusb300_set_fifo_entry(struct fusb300
*fusb300
,
88 u32 val
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
90 val
&= ~FUSB300_EPSET1_FIFOENTRY_MSK
;
91 val
|= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM
);
92 iowrite32(val
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
95 static void fusb300_set_start_entry(struct fusb300
*fusb300
,
98 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
99 u32 start_entry
= fusb300
->fifo_entry_num
* FUSB300_FIFO_ENTRY_NUM
;
101 reg
&= ~FUSB300_EPSET1_START_ENTRY_MSK
;
102 reg
|= FUSB300_EPSET1_START_ENTRY(start_entry
);
103 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
104 if (fusb300
->fifo_entry_num
== FUSB300_MAX_FIFO_ENTRY
) {
105 fusb300
->fifo_entry_num
= 0;
106 fusb300
->addrofs
= 0;
107 pr_err("fifo entry is over the maximum number!\n");
109 fusb300
->fifo_entry_num
++;
112 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
113 static void fusb300_set_epaddrofs(struct fusb300
*fusb300
,
114 struct fusb300_ep_info info
)
116 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
118 reg
&= ~FUSB300_EPSET2_ADDROFS_MSK
;
119 reg
|= FUSB300_EPSET2_ADDROFS(fusb300
->addrofs
);
120 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
121 fusb300
->addrofs
+= (info
.maxpacket
+ 7) / 8 * FUSB300_FIFO_ENTRY_NUM
;
124 static void ep_fifo_setting(struct fusb300
*fusb300
,
125 struct fusb300_ep_info info
)
127 fusb300_set_fifo_entry(fusb300
, info
.epnum
);
128 fusb300_set_start_entry(fusb300
, info
.epnum
);
129 fusb300_set_epaddrofs(fusb300
, info
);
132 static void fusb300_set_eptype(struct fusb300
*fusb300
,
133 struct fusb300_ep_info info
)
135 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
137 reg
&= ~FUSB300_EPSET1_TYPE_MSK
;
138 reg
|= FUSB300_EPSET1_TYPE(info
.type
);
139 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
142 static void fusb300_set_epdir(struct fusb300
*fusb300
,
143 struct fusb300_ep_info info
)
149 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
150 reg
&= ~FUSB300_EPSET1_DIR_MSK
;
151 reg
|= FUSB300_EPSET1_DIRIN
;
152 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
155 static void fusb300_set_ep_active(struct fusb300
*fusb300
,
158 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
160 reg
|= FUSB300_EPSET1_ACTEN
;
161 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
164 static void fusb300_set_epmps(struct fusb300
*fusb300
,
165 struct fusb300_ep_info info
)
167 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
169 reg
&= ~FUSB300_EPSET2_MPS_MSK
;
170 reg
|= FUSB300_EPSET2_MPS(info
.maxpacket
);
171 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
174 static void fusb300_set_interval(struct fusb300
*fusb300
,
175 struct fusb300_ep_info info
)
177 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
179 reg
&= ~FUSB300_EPSET1_INTERVAL(0x7);
180 reg
|= FUSB300_EPSET1_INTERVAL(info
.interval
);
181 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
184 static void fusb300_set_bwnum(struct fusb300
*fusb300
,
185 struct fusb300_ep_info info
)
187 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
189 reg
&= ~FUSB300_EPSET1_BWNUM(0x3);
190 reg
|= FUSB300_EPSET1_BWNUM(info
.bw_num
);
191 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
194 static void set_ep_reg(struct fusb300
*fusb300
,
195 struct fusb300_ep_info info
)
197 fusb300_set_eptype(fusb300
, info
);
198 fusb300_set_epdir(fusb300
, info
);
199 fusb300_set_epmps(fusb300
, info
);
202 fusb300_set_interval(fusb300
, info
);
205 fusb300_set_bwnum(fusb300
, info
);
207 fusb300_set_ep_active(fusb300
, info
.epnum
);
210 static int config_ep(struct fusb300_ep
*ep
,
211 const struct usb_endpoint_descriptor
*desc
)
213 struct fusb300
*fusb300
= ep
->fusb300
;
214 struct fusb300_ep_info info
;
222 info
.type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
223 info
.dir_in
= (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ? 1 : 0;
224 info
.maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
225 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
227 if ((info
.type
== USB_ENDPOINT_XFER_INT
) ||
228 (info
.type
== USB_ENDPOINT_XFER_ISOC
)) {
229 info
.interval
= desc
->bInterval
;
230 if (info
.type
== USB_ENDPOINT_XFER_ISOC
)
231 info
.bw_num
= ((desc
->wMaxPacketSize
& 0x1800) >> 11);
234 ep_fifo_setting(fusb300
, info
);
236 set_ep_reg(fusb300
, info
);
238 fusb300_ep_setting(ep
, info
);
240 fusb300
->ep
[info
.epnum
] = ep
;
245 static int fusb300_enable(struct usb_ep
*_ep
,
246 const struct usb_endpoint_descriptor
*desc
)
248 struct fusb300_ep
*ep
;
250 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
252 if (ep
->fusb300
->reenum
) {
253 ep
->fusb300
->fifo_entry_num
= 0;
254 ep
->fusb300
->addrofs
= 0;
255 ep
->fusb300
->reenum
= 0;
258 return config_ep(ep
, desc
);
261 static int fusb300_disable(struct usb_ep
*_ep
)
263 struct fusb300_ep
*ep
;
264 struct fusb300_request
*req
;
267 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
271 while (!list_empty(&ep
->queue
)) {
272 req
= list_entry(ep
->queue
.next
, struct fusb300_request
, queue
);
273 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
274 done(ep
, req
, -ECONNRESET
);
275 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
278 return fusb300_ep_release(ep
);
281 static struct usb_request
*fusb300_alloc_request(struct usb_ep
*_ep
,
284 struct fusb300_request
*req
;
286 req
= kzalloc(sizeof(struct fusb300_request
), gfp_flags
);
289 INIT_LIST_HEAD(&req
->queue
);
294 static void fusb300_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
296 struct fusb300_request
*req
;
298 req
= container_of(_req
, struct fusb300_request
, req
);
302 static int enable_fifo_int(struct fusb300_ep
*ep
)
304 struct fusb300
*fusb300
= ep
->fusb300
;
307 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
308 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
310 pr_err("can't enable_fifo_int ep0\n");
317 static int disable_fifo_int(struct fusb300_ep
*ep
)
319 struct fusb300
*fusb300
= ep
->fusb300
;
322 fusb300_disable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
323 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
325 pr_err("can't disable_fifo_int ep0\n");
332 static void fusb300_set_cxlen(struct fusb300
*fusb300
, u32 length
)
336 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
337 reg
&= ~FUSB300_CSR_LEN_MSK
;
338 reg
|= FUSB300_CSR_LEN(length
);
339 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_CSR
);
342 /* write data to cx fifo */
343 static void fusb300_wrcxf(struct fusb300_ep
*ep
,
344 struct fusb300_request
*req
)
349 struct fusb300
*fusb300
= ep
->fusb300
;
350 u32 length
= req
->req
.length
- req
->req
.actual
;
352 tmp
= req
->req
.buf
+ req
->req
.actual
;
354 if (length
> SS_CTL_MAX_PACKET_SIZE
) {
355 fusb300_set_cxlen(fusb300
, SS_CTL_MAX_PACKET_SIZE
);
356 for (i
= (SS_CTL_MAX_PACKET_SIZE
>> 2); i
> 0; i
--) {
357 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
359 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
362 req
->req
.actual
+= SS_CTL_MAX_PACKET_SIZE
;
363 } else { /* length is less than max packet size */
364 fusb300_set_cxlen(fusb300
, length
);
365 for (i
= length
>> 2; i
> 0; i
--) {
366 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
368 printk(KERN_DEBUG
" 0x%x\n", data
);
369 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
372 switch (length
% 4) {
375 printk(KERN_DEBUG
" 0x%x\n", data
);
376 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
379 data
= *tmp
| *(tmp
+ 1) << 8;
380 printk(KERN_DEBUG
" 0x%x\n", data
);
381 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
384 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
385 printk(KERN_DEBUG
" 0x%x\n", data
);
386 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
391 req
->req
.actual
+= length
;
395 static void fusb300_set_epnstall(struct fusb300
*fusb300
, u8 ep
)
397 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
401 static void fusb300_clear_epnstall(struct fusb300
*fusb300
, u8 ep
)
403 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
405 if (reg
& FUSB300_EPSET0_STL
) {
406 printk(KERN_DEBUG
"EP%d stall... Clear!!\n", ep
);
407 reg
&= ~FUSB300_EPSET0_STL
;
408 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
412 static void ep0_queue(struct fusb300_ep
*ep
, struct fusb300_request
*req
)
414 if (ep
->fusb300
->ep0_dir
) { /* if IN */
415 if (req
->req
.length
) {
416 fusb300_wrcxf(ep
, req
);
418 printk(KERN_DEBUG
"%s : req->req.length = 0x%x\n",
419 __func__
, req
->req
.length
);
420 if ((req
->req
.length
== req
->req
.actual
) ||
421 (req
->req
.actual
< ep
->ep
.maxpacket
))
424 if (!req
->req
.length
)
427 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER1
,
428 FUSB300_IGER1_CX_OUT_INT
);
432 static int fusb300_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
435 struct fusb300_ep
*ep
;
436 struct fusb300_request
*req
;
440 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
441 req
= container_of(_req
, struct fusb300_request
, req
);
443 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
446 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
448 if (list_empty(&ep
->queue
))
451 list_add_tail(&req
->queue
, &ep
->queue
);
454 req
->req
.status
= -EINPROGRESS
;
456 if (ep
->desc
== NULL
) /* ep0 */
458 else if (request
&& !ep
->stall
)
461 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
466 static int fusb300_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
468 struct fusb300_ep
*ep
;
469 struct fusb300_request
*req
;
472 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
473 req
= container_of(_req
, struct fusb300_request
, req
);
475 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
476 if (!list_empty(&ep
->queue
))
477 done(ep
, req
, -ECONNRESET
);
478 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
483 static int fusb300_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
485 struct fusb300_ep
*ep
;
486 struct fusb300
*fusb300
;
490 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
492 fusb300
= ep
->fusb300
;
494 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
496 if (!list_empty(&ep
->queue
)) {
502 fusb300_set_epnstall(fusb300
, ep
->epnum
);
507 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
513 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
517 static int fusb300_set_halt(struct usb_ep
*_ep
, int value
)
519 return fusb300_set_halt_and_wedge(_ep
, value
, 0);
522 static int fusb300_set_wedge(struct usb_ep
*_ep
)
524 return fusb300_set_halt_and_wedge(_ep
, 1, 1);
527 static void fusb300_fifo_flush(struct usb_ep
*_ep
)
531 static struct usb_ep_ops fusb300_ep_ops
= {
532 .enable
= fusb300_enable
,
533 .disable
= fusb300_disable
,
535 .alloc_request
= fusb300_alloc_request
,
536 .free_request
= fusb300_free_request
,
538 .queue
= fusb300_queue
,
539 .dequeue
= fusb300_dequeue
,
541 .set_halt
= fusb300_set_halt
,
542 .fifo_flush
= fusb300_fifo_flush
,
543 .set_wedge
= fusb300_set_wedge
,
546 /*****************************************************************************/
547 static void fusb300_clear_int(struct fusb300
*fusb300
, u32 offset
,
550 iowrite32(value
, fusb300
->reg
+ offset
);
553 static void fusb300_reset(void)
557 static void fusb300_set_cxstall(struct fusb300
*fusb300
)
559 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
563 static void fusb300_set_cxdone(struct fusb300
*fusb300
)
565 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
569 /* read data from cx fifo */
570 void fusb300_rdcxf(struct fusb300
*fusb300
,
571 u8
*buffer
, u32 length
)
579 for (i
= (length
>> 2); i
> 0; i
--) {
580 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
581 printk(KERN_DEBUG
" 0x%x\n", data
);
583 *(tmp
+ 1) = (data
>> 8) & 0xFF;
584 *(tmp
+ 2) = (data
>> 16) & 0xFF;
585 *(tmp
+ 3) = (data
>> 24) & 0xFF;
589 switch (length
% 4) {
591 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
592 printk(KERN_DEBUG
" 0x%x\n", data
);
596 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
597 printk(KERN_DEBUG
" 0x%x\n", data
);
599 *(tmp
+ 1) = (data
>> 8) & 0xFF;
602 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
603 printk(KERN_DEBUG
" 0x%x\n", data
);
605 *(tmp
+ 1) = (data
>> 8) & 0xFF;
606 *(tmp
+ 2) = (data
>> 16) & 0xFF;
613 static void fusb300_rdfifo(struct fusb300_ep
*ep
,
614 struct fusb300_request
*req
,
620 struct fusb300
*fusb300
= ep
->fusb300
;
622 tmp
= req
->req
.buf
+ req
->req
.actual
;
623 req
->req
.actual
+= length
;
625 if (req
->req
.actual
> req
->req
.length
)
626 printk(KERN_DEBUG
"req->req.actual > req->req.length\n");
628 for (i
= (length
>> 2); i
> 0; i
--) {
629 data
= ioread32(fusb300
->reg
+
630 FUSB300_OFFSET_EPPORT(ep
->epnum
));
632 *(tmp
+ 1) = (data
>> 8) & 0xFF;
633 *(tmp
+ 2) = (data
>> 16) & 0xFF;
634 *(tmp
+ 3) = (data
>> 24) & 0xFF;
638 switch (length
% 4) {
640 data
= ioread32(fusb300
->reg
+
641 FUSB300_OFFSET_EPPORT(ep
->epnum
));
645 data
= ioread32(fusb300
->reg
+
646 FUSB300_OFFSET_EPPORT(ep
->epnum
));
648 *(tmp
+ 1) = (data
>> 8) & 0xFF;
651 data
= ioread32(fusb300
->reg
+
652 FUSB300_OFFSET_EPPORT(ep
->epnum
));
654 *(tmp
+ 1) = (data
>> 8) & 0xFF;
655 *(tmp
+ 2) = (data
>> 16) & 0xFF;
662 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
663 reg
&= FUSB300_IGR1_SYNF0_EMPTY_INT
;
665 printk(KERN_INFO
"sync fifo is not empty!\n");
670 static u8
fusb300_get_epnstall(struct fusb300
*fusb300
, u8 ep
)
673 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
675 value
= reg
& FUSB300_EPSET0_STL
;
680 static u8
fusb300_get_cxstall(struct fusb300
*fusb300
)
683 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
685 value
= (reg
& FUSB300_CSR_STL
) >> 1;
690 static void request_error(struct fusb300
*fusb300
)
692 fusb300_set_cxstall(fusb300
);
693 printk(KERN_DEBUG
"request error!!\n");
696 static void get_status(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
697 __releases(fusb300
->lock
)
698 __acquires(fusb300
->lock
)
702 u16 w_index
= ctrl
->wIndex
;
704 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
705 case USB_RECIP_DEVICE
:
706 status
= 1 << USB_DEVICE_SELF_POWERED
;
708 case USB_RECIP_INTERFACE
:
711 case USB_RECIP_ENDPOINT
:
712 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
714 if (fusb300_get_epnstall(fusb300
, ep
))
715 status
= 1 << USB_ENDPOINT_HALT
;
717 if (fusb300_get_cxstall(fusb300
))
723 request_error(fusb300
);
727 fusb300
->ep0_data
= cpu_to_le16(status
);
728 fusb300
->ep0_req
->buf
= &fusb300
->ep0_data
;
729 fusb300
->ep0_req
->length
= 2;
731 spin_unlock(&fusb300
->lock
);
732 fusb300_queue(fusb300
->gadget
.ep0
, fusb300
->ep0_req
, GFP_KERNEL
);
733 spin_lock(&fusb300
->lock
);
736 static void set_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
740 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
741 case USB_RECIP_DEVICE
:
742 fusb300_set_cxdone(fusb300
);
744 case USB_RECIP_INTERFACE
:
745 fusb300_set_cxdone(fusb300
);
747 case USB_RECIP_ENDPOINT
: {
748 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
750 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
752 fusb300_set_epnstall(fusb300
, ep
);
754 fusb300_set_cxstall(fusb300
);
755 fusb300_set_cxdone(fusb300
);
759 request_error(fusb300
);
764 static void fusb300_clear_seqnum(struct fusb300
*fusb300
, u8 ep
)
766 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
767 FUSB300_EPSET0_CLRSEQNUM
);
770 static void clear_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
772 struct fusb300_ep
*ep
=
773 fusb300
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
775 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
776 case USB_RECIP_DEVICE
:
777 fusb300_set_cxdone(fusb300
);
779 case USB_RECIP_INTERFACE
:
780 fusb300_set_cxdone(fusb300
);
782 case USB_RECIP_ENDPOINT
:
783 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
785 fusb300_set_cxdone(fusb300
);
790 fusb300_clear_seqnum(fusb300
, ep
->epnum
);
791 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
792 if (!list_empty(&ep
->queue
))
796 fusb300_set_cxdone(fusb300
);
799 request_error(fusb300
);
804 static void fusb300_set_dev_addr(struct fusb300
*fusb300
, u16 addr
)
806 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_DAR
);
808 reg
&= ~FUSB300_DAR_DRVADDR_MSK
;
809 reg
|= FUSB300_DAR_DRVADDR(addr
);
811 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_DAR
);
814 static void set_address(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
816 if (ctrl
->wValue
>= 0x0100)
817 request_error(fusb300
);
819 fusb300_set_dev_addr(fusb300
, ctrl
->wValue
);
820 fusb300_set_cxdone(fusb300
);
824 #define UVC_COPY_DESCRIPTORS(mem, src) \
826 const struct usb_descriptor_header * const *__src; \
827 for (__src = src; *__src; ++__src) { \
828 memcpy(mem, *__src, (*__src)->bLength); \
829 mem += (*__src)->bLength; \
833 static int setup_packet(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
839 fusb300_rdcxf(fusb300
, p
, 8);
840 fusb300
->ep0_dir
= ctrl
->bRequestType
& USB_DIR_IN
;
841 fusb300
->ep0_length
= ctrl
->wLength
;
844 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
845 switch (ctrl
->bRequest
) {
846 case USB_REQ_GET_STATUS
:
847 get_status(fusb300
, ctrl
);
849 case USB_REQ_CLEAR_FEATURE
:
850 clear_feature(fusb300
, ctrl
);
852 case USB_REQ_SET_FEATURE
:
853 set_feature(fusb300
, ctrl
);
855 case USB_REQ_SET_ADDRESS
:
856 set_address(fusb300
, ctrl
);
858 case USB_REQ_SET_CONFIGURATION
:
859 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_DAR
,
860 FUSB300_DAR_SETCONFG
);
861 /* clear sequence number */
862 for (i
= 1; i
<= FUSB300_MAX_NUM_EP
; i
++)
863 fusb300_clear_seqnum(fusb300
, i
);
877 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
880 list_del_init(&req
->queue
);
882 /* don't modify queue heads during completion callback */
883 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
884 req
->req
.status
= -ESHUTDOWN
;
886 req
->req
.status
= status
;
888 spin_unlock(&ep
->fusb300
->lock
);
889 req
->req
.complete(&ep
->ep
, &req
->req
);
890 spin_lock(&ep
->fusb300
->lock
);
893 disable_fifo_int(ep
);
894 if (!list_empty(&ep
->queue
))
897 fusb300_set_cxdone(ep
->fusb300
);
900 static void fusb300_fill_idma_prdtbl(struct fusb300_ep
*ep
, dma_addr_t d
,
908 reg
= ioread32(ep
->fusb300
->reg
+
909 FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
910 reg
&= FUSB300_EPPRD0_H
;
913 iowrite32(d
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W1(ep
->epnum
));
915 value
= FUSB300_EPPRD0_BTC(len
) | FUSB300_EPPRD0_H
|
916 FUSB300_EPPRD0_F
| FUSB300_EPPRD0_L
| FUSB300_EPPRD0_I
;
917 iowrite32(value
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
919 iowrite32(0x0, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W2(ep
->epnum
));
921 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_EPPRDRDY
,
922 FUSB300_EPPRDR_EP_PRD_RDY(ep
->epnum
));
925 static void fusb300_wait_idma_finished(struct fusb300_ep
*ep
)
930 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
931 if ((reg
& FUSB300_IGR1_VBUS_CHG_INT
) ||
932 (reg
& FUSB300_IGR1_WARM_RST_INT
) ||
933 (reg
& FUSB300_IGR1_HOT_RST_INT
) ||
934 (reg
& FUSB300_IGR1_USBRST_INT
)
937 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
938 reg
&= FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
);
941 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGR0
,
942 FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
));
944 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
945 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
948 static void fusb300_set_idma(struct fusb300_ep
*ep
,
949 struct fusb300_request
*req
)
953 d
= dma_map_single(NULL
, req
->req
.buf
, req
->req
.length
, DMA_TO_DEVICE
);
955 if (dma_mapping_error(NULL
, d
)) {
956 printk(KERN_DEBUG
"dma_mapping_error\n");
960 dma_sync_single_for_device(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
962 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
963 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
965 fusb300_fill_idma_prdtbl(ep
, d
, req
->req
.length
);
966 /* check idma is done */
967 fusb300_wait_idma_finished(ep
);
969 dma_unmap_single(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
972 static void in_ep_fifo_handler(struct fusb300_ep
*ep
)
974 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
975 struct fusb300_request
, queue
);
978 fusb300_set_idma(ep
, req
);
982 static void out_ep_fifo_handler(struct fusb300_ep
*ep
)
984 struct fusb300
*fusb300
= ep
->fusb300
;
985 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
986 struct fusb300_request
, queue
);
987 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
988 u32 length
= reg
& FUSB300_FFR_BYCNT
;
990 fusb300_rdfifo(ep
, req
, length
);
992 /* finish out transfer */
993 if ((req
->req
.length
== req
->req
.actual
) || (length
< ep
->ep
.maxpacket
))
997 static void check_device_mode(struct fusb300
*fusb300
)
999 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_GCR
);
1001 switch (reg
& FUSB300_GCR_DEVEN_MSK
) {
1002 case FUSB300_GCR_DEVEN_SS
:
1003 fusb300
->gadget
.speed
= USB_SPEED_SUPER
;
1005 case FUSB300_GCR_DEVEN_HS
:
1006 fusb300
->gadget
.speed
= USB_SPEED_HIGH
;
1008 case FUSB300_GCR_DEVEN_FS
:
1009 fusb300
->gadget
.speed
= USB_SPEED_FULL
;
1012 fusb300
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1015 printk(KERN_INFO
"dev_mode = %d\n", (reg
& FUSB300_GCR_DEVEN_MSK
));
1019 static void fusb300_ep0out(struct fusb300
*fusb300
)
1021 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1024 if (!list_empty(&ep
->queue
)) {
1025 struct fusb300_request
*req
;
1027 req
= list_first_entry(&ep
->queue
,
1028 struct fusb300_request
, queue
);
1029 if (req
->req
.length
)
1030 fusb300_rdcxf(ep
->fusb300
, req
->req
.buf
,
1033 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1034 reg
&= ~FUSB300_IGER1_CX_OUT_INT
;
1035 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1037 pr_err("%s : empty queue\n", __func__
);
1040 static void fusb300_ep0in(struct fusb300
*fusb300
)
1042 struct fusb300_request
*req
;
1043 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1045 if ((!list_empty(&ep
->queue
)) && (fusb300
->ep0_dir
)) {
1046 req
= list_entry(ep
->queue
.next
,
1047 struct fusb300_request
, queue
);
1048 if (req
->req
.length
)
1049 fusb300_wrcxf(ep
, req
);
1050 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
1053 fusb300_set_cxdone(fusb300
);
1056 static void fusb300_grp2_handler(void)
1060 static void fusb300_grp3_handler(void)
1064 static void fusb300_grp4_handler(void)
1068 static void fusb300_grp5_handler(void)
1072 static irqreturn_t
fusb300_irq(int irq
, void *_fusb300
)
1074 struct fusb300
*fusb300
= _fusb300
;
1075 u32 int_grp1
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1076 u32 int_grp1_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1077 u32 int_grp0
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1078 u32 int_grp0_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER0
);
1079 struct usb_ctrlrequest ctrl
;
1084 spin_lock(&fusb300
->lock
);
1086 int_grp1
&= int_grp1_en
;
1087 int_grp0
&= int_grp0_en
;
1089 if (int_grp1
& FUSB300_IGR1_WARM_RST_INT
) {
1090 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1091 FUSB300_IGR1_WARM_RST_INT
);
1092 printk(KERN_INFO
"fusb300_warmreset\n");
1096 if (int_grp1
& FUSB300_IGR1_HOT_RST_INT
) {
1097 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1098 FUSB300_IGR1_HOT_RST_INT
);
1099 printk(KERN_INFO
"fusb300_hotreset\n");
1103 if (int_grp1
& FUSB300_IGR1_USBRST_INT
) {
1104 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1105 FUSB300_IGR1_USBRST_INT
);
1108 /* COMABT_INT has a highest priority */
1110 if (int_grp1
& FUSB300_IGR1_CX_COMABT_INT
) {
1111 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1112 FUSB300_IGR1_CX_COMABT_INT
);
1113 printk(KERN_INFO
"fusb300_ep0abt\n");
1116 if (int_grp1
& FUSB300_IGR1_VBUS_CHG_INT
) {
1117 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1118 FUSB300_IGR1_VBUS_CHG_INT
);
1119 printk(KERN_INFO
"fusb300_vbus_change\n");
1122 if (int_grp1
& FUSB300_IGR1_U3_EXIT_FAIL_INT
) {
1123 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1124 FUSB300_IGR1_U3_EXIT_FAIL_INT
);
1127 if (int_grp1
& FUSB300_IGR1_U2_EXIT_FAIL_INT
) {
1128 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1129 FUSB300_IGR1_U2_EXIT_FAIL_INT
);
1132 if (int_grp1
& FUSB300_IGR1_U1_EXIT_FAIL_INT
) {
1133 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1134 FUSB300_IGR1_U1_EXIT_FAIL_INT
);
1137 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_FAIL_INT
) {
1138 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1139 FUSB300_IGR1_U2_ENTRY_FAIL_INT
);
1142 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_FAIL_INT
) {
1143 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1144 FUSB300_IGR1_U1_ENTRY_FAIL_INT
);
1147 if (int_grp1
& FUSB300_IGR1_U3_EXIT_INT
) {
1148 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1149 FUSB300_IGR1_U3_EXIT_INT
);
1150 printk(KERN_INFO
"FUSB300_IGR1_U3_EXIT_INT\n");
1153 if (int_grp1
& FUSB300_IGR1_U2_EXIT_INT
) {
1154 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1155 FUSB300_IGR1_U2_EXIT_INT
);
1156 printk(KERN_INFO
"FUSB300_IGR1_U2_EXIT_INT\n");
1159 if (int_grp1
& FUSB300_IGR1_U1_EXIT_INT
) {
1160 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1161 FUSB300_IGR1_U1_EXIT_INT
);
1162 printk(KERN_INFO
"FUSB300_IGR1_U1_EXIT_INT\n");
1165 if (int_grp1
& FUSB300_IGR1_U3_ENTRY_INT
) {
1166 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1167 FUSB300_IGR1_U3_ENTRY_INT
);
1168 printk(KERN_INFO
"FUSB300_IGR1_U3_ENTRY_INT\n");
1169 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_SSCR1
,
1170 FUSB300_SSCR1_GO_U3_DONE
);
1173 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_INT
) {
1174 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1175 FUSB300_IGR1_U2_ENTRY_INT
);
1176 printk(KERN_INFO
"FUSB300_IGR1_U2_ENTRY_INT\n");
1179 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_INT
) {
1180 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1181 FUSB300_IGR1_U1_ENTRY_INT
);
1182 printk(KERN_INFO
"FUSB300_IGR1_U1_ENTRY_INT\n");
1185 if (int_grp1
& FUSB300_IGR1_RESM_INT
) {
1186 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1187 FUSB300_IGR1_RESM_INT
);
1188 printk(KERN_INFO
"fusb300_resume\n");
1191 if (int_grp1
& FUSB300_IGR1_SUSP_INT
) {
1192 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1193 FUSB300_IGR1_SUSP_INT
);
1194 printk(KERN_INFO
"fusb300_suspend\n");
1197 if (int_grp1
& FUSB300_IGR1_HS_LPM_INT
) {
1198 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1199 FUSB300_IGR1_HS_LPM_INT
);
1200 printk(KERN_INFO
"fusb300_HS_LPM_INT\n");
1203 if (int_grp1
& FUSB300_IGR1_DEV_MODE_CHG_INT
) {
1204 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1205 FUSB300_IGR1_DEV_MODE_CHG_INT
);
1206 check_device_mode(fusb300
);
1209 if (int_grp1
& FUSB300_IGR1_CX_COMFAIL_INT
) {
1210 fusb300_set_cxstall(fusb300
);
1211 printk(KERN_INFO
"fusb300_ep0fail\n");
1214 if (int_grp1
& FUSB300_IGR1_CX_SETUP_INT
) {
1215 printk(KERN_INFO
"fusb300_ep0setup\n");
1216 if (setup_packet(fusb300
, &ctrl
)) {
1217 spin_unlock(&fusb300
->lock
);
1218 if (fusb300
->driver
->setup(&fusb300
->gadget
, &ctrl
) < 0)
1219 fusb300_set_cxstall(fusb300
);
1220 spin_lock(&fusb300
->lock
);
1224 if (int_grp1
& FUSB300_IGR1_CX_CMDEND_INT
)
1225 printk(KERN_INFO
"fusb300_cmdend\n");
1228 if (int_grp1
& FUSB300_IGR1_CX_OUT_INT
) {
1229 printk(KERN_INFO
"fusb300_cxout\n");
1230 fusb300_ep0out(fusb300
);
1233 if (int_grp1
& FUSB300_IGR1_CX_IN_INT
) {
1234 printk(KERN_INFO
"fusb300_cxin\n");
1235 fusb300_ep0in(fusb300
);
1238 if (int_grp1
& FUSB300_IGR1_INTGRP5
)
1239 fusb300_grp5_handler();
1241 if (int_grp1
& FUSB300_IGR1_INTGRP4
)
1242 fusb300_grp4_handler();
1244 if (int_grp1
& FUSB300_IGR1_INTGRP3
)
1245 fusb300_grp3_handler();
1247 if (int_grp1
& FUSB300_IGR1_INTGRP2
)
1248 fusb300_grp2_handler();
1251 for (i
= 1; i
< FUSB300_MAX_NUM_EP
; i
++) {
1252 if (int_grp0
& FUSB300_IGR0_EPn_FIFO_INT(i
)) {
1253 reg
= ioread32(fusb300
->reg
+
1254 FUSB300_OFFSET_EPSET1(i
));
1255 in
= (reg
& FUSB300_EPSET1_DIRIN
) ? 1 : 0;
1257 in_ep_fifo_handler(fusb300
->ep
[i
]);
1259 out_ep_fifo_handler(fusb300
->ep
[i
]);
1264 spin_unlock(&fusb300
->lock
);
1269 static void fusb300_set_u2_timeout(struct fusb300
*fusb300
,
1274 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1276 reg
|= FUSB300_SSCR2_U2TIMEOUT(time
);
1278 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1281 static void fusb300_set_u1_timeout(struct fusb300
*fusb300
,
1286 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1287 reg
&= ~(0xff << 8);
1288 reg
|= FUSB300_SSCR2_U1TIMEOUT(time
);
1290 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1293 static void init_controller(struct fusb300
*fusb300
)
1300 mask
= val
= FUSB300_AHBBCR_S0_SPLIT_ON
| FUSB300_AHBBCR_S1_SPLIT_ON
;
1301 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1304 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1306 /* enable high-speed LPM */
1307 mask
= val
= FUSB300_HSCR_HS_LPM_PERMIT
;
1308 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1311 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1313 /*set u1 u2 timmer*/
1314 fusb300_set_u2_timeout(fusb300
, 0xff);
1315 fusb300_set_u1_timeout(fusb300
, 0xff);
1317 /* enable all grp1 interrupt */
1318 iowrite32(0xcfffff9f, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1320 /*------------------------------------------------------------------------*/
1321 static struct fusb300
*the_controller
;
1323 static int fusb300_udc_start(struct usb_gadget_driver
*driver
,
1324 int (*bind
)(struct usb_gadget
*))
1326 struct fusb300
*fusb300
= the_controller
;
1330 || driver
->speed
< USB_SPEED_FULL
1338 if (fusb300
->driver
)
1341 /* hook up the driver */
1342 driver
->driver
.bus
= NULL
;
1343 fusb300
->driver
= driver
;
1344 fusb300
->gadget
.dev
.driver
= &driver
->driver
;
1346 retval
= device_add(&fusb300
->gadget
.dev
);
1348 pr_err("device_add error (%d)\n", retval
);
1352 retval
= bind(&fusb300
->gadget
);
1354 pr_err("bind to driver error (%d)\n", retval
);
1355 device_del(&fusb300
->gadget
.dev
);
1362 fusb300
->driver
= NULL
;
1363 fusb300
->gadget
.dev
.driver
= NULL
;
1368 static int fusb300_udc_stop(struct usb_gadget_driver
*driver
)
1370 struct fusb300
*fusb300
= the_controller
;
1372 if (driver
!= fusb300
->driver
|| !driver
->unbind
)
1375 driver
->unbind(&fusb300
->gadget
);
1376 fusb300
->gadget
.dev
.driver
= NULL
;
1378 init_controller(fusb300
);
1379 device_del(&fusb300
->gadget
.dev
);
1380 fusb300
->driver
= NULL
;
1384 /*--------------------------------------------------------------------------*/
1386 static int fusb300_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
1391 static struct usb_gadget_ops fusb300_gadget_ops
= {
1392 .pullup
= fusb300_udc_pullup
,
1393 .start
= fusb300_udc_start
,
1394 .stop
= fusb300_udc_stop
,
1397 static int __exit
fusb300_remove(struct platform_device
*pdev
)
1399 struct fusb300
*fusb300
= dev_get_drvdata(&pdev
->dev
);
1401 usb_del_gadget_udc(&fusb300
->gadget
);
1402 iounmap(fusb300
->reg
);
1403 free_irq(platform_get_irq(pdev
, 0), fusb300
);
1405 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1411 static int __init
fusb300_probe(struct platform_device
*pdev
)
1413 struct resource
*res
, *ires
, *ires1
;
1414 void __iomem
*reg
= NULL
;
1415 struct fusb300
*fusb300
= NULL
;
1416 struct fusb300_ep
*_ep
[FUSB300_MAX_NUM_EP
];
1420 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1423 pr_err("platform_get_resource error.\n");
1427 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1431 "platform_get_resource IORESOURCE_IRQ error.\n");
1435 ires1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1439 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1443 reg
= ioremap(res
->start
, resource_size(res
));
1446 pr_err("ioremap error.\n");
1450 /* initialize udc */
1451 fusb300
= kzalloc(sizeof(struct fusb300
), GFP_KERNEL
);
1452 if (fusb300
== NULL
) {
1453 pr_err("kzalloc error\n");
1457 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1458 _ep
[i
] = kzalloc(sizeof(struct fusb300_ep
), GFP_KERNEL
);
1459 if (_ep
[i
] == NULL
) {
1460 pr_err("_ep kzalloc error\n");
1463 fusb300
->ep
[i
] = _ep
[i
];
1466 spin_lock_init(&fusb300
->lock
);
1468 dev_set_drvdata(&pdev
->dev
, fusb300
);
1470 fusb300
->gadget
.ops
= &fusb300_gadget_ops
;
1472 device_initialize(&fusb300
->gadget
.dev
);
1474 dev_set_name(&fusb300
->gadget
.dev
, "gadget");
1476 fusb300
->gadget
.is_dualspeed
= 1;
1477 fusb300
->gadget
.dev
.parent
= &pdev
->dev
;
1478 fusb300
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1479 fusb300
->gadget
.dev
.release
= pdev
->dev
.release
;
1480 fusb300
->gadget
.name
= udc_name
;
1483 ret
= request_irq(ires
->start
, fusb300_irq
, IRQF_DISABLED
| IRQF_SHARED
,
1486 pr_err("request_irq error (%d)\n", ret
);
1490 ret
= request_irq(ires1
->start
, fusb300_irq
,
1491 IRQF_DISABLED
| IRQF_SHARED
, udc_name
, fusb300
);
1493 pr_err("request_irq1 error (%d)\n", ret
);
1497 INIT_LIST_HEAD(&fusb300
->gadget
.ep_list
);
1499 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1500 struct fusb300_ep
*ep
= fusb300
->ep
[i
];
1503 INIT_LIST_HEAD(&fusb300
->ep
[i
]->ep
.ep_list
);
1504 list_add_tail(&fusb300
->ep
[i
]->ep
.ep_list
,
1505 &fusb300
->gadget
.ep_list
);
1507 ep
->fusb300
= fusb300
;
1508 INIT_LIST_HEAD(&ep
->queue
);
1509 ep
->ep
.name
= fusb300_ep_name
[i
];
1510 ep
->ep
.ops
= &fusb300_ep_ops
;
1511 ep
->ep
.maxpacket
= HS_BULK_MAX_PACKET_SIZE
;
1513 fusb300
->ep
[0]->ep
.maxpacket
= HS_CTL_MAX_PACKET_SIZE
;
1514 fusb300
->ep
[0]->epnum
= 0;
1515 fusb300
->gadget
.ep0
= &fusb300
->ep
[0]->ep
;
1516 INIT_LIST_HEAD(&fusb300
->gadget
.ep0
->ep_list
);
1518 the_controller
= fusb300
;
1520 fusb300
->ep0_req
= fusb300_alloc_request(&fusb300
->ep
[0]->ep
,
1522 if (fusb300
->ep0_req
== NULL
)
1525 init_controller(fusb300
);
1526 ret
= usb_add_gadget_udc(&pdev
->dev
, &fusb300
->gadget
);
1530 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1534 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1537 free_irq(ires
->start
, fusb300
);
1541 if (fusb300
->ep0_req
)
1542 fusb300_free_request(&fusb300
->ep
[0]->ep
,
1552 static struct platform_driver fusb300_driver
= {
1553 .remove
= __exit_p(fusb300_remove
),
1555 .name
= (char *) udc_name
,
1556 .owner
= THIS_MODULE
,
1560 static int __init
fusb300_udc_init(void)
1562 return platform_driver_probe(&fusb300_driver
, fusb300_probe
);
1565 module_init(fusb300_udc_init
);
1567 static void __exit
fusb300_udc_cleanup(void)
1569 platform_driver_unregister(&fusb300_driver
);
1571 module_exit(fusb300_udc_cleanup
);