2 * Driver for the Atmel USBA high speed USB device controller
4 * Copyright (C) 2005-2007 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/clk/at91_pmc.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/list.h>
20 #include <linux/platform_device.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/atmel_usba_udc.h>
24 #include <linux/delay.h>
26 #include <linux/of_gpio.h>
30 #include "atmel_usba_udc.h"
32 #ifdef CONFIG_USB_GADGET_DEBUG_FS
33 #include <linux/debugfs.h>
34 #include <linux/uaccess.h>
36 static int queue_dbg_open(struct inode
*inode
, struct file
*file
)
38 struct usba_ep
*ep
= inode
->i_private
;
39 struct usba_request
*req
, *req_copy
;
40 struct list_head
*queue_data
;
42 queue_data
= kmalloc(sizeof(*queue_data
), GFP_KERNEL
);
45 INIT_LIST_HEAD(queue_data
);
47 spin_lock_irq(&ep
->udc
->lock
);
48 list_for_each_entry(req
, &ep
->queue
, queue
) {
49 req_copy
= kmemdup(req
, sizeof(*req_copy
), GFP_ATOMIC
);
52 list_add_tail(&req_copy
->queue
, queue_data
);
54 spin_unlock_irq(&ep
->udc
->lock
);
56 file
->private_data
= queue_data
;
60 spin_unlock_irq(&ep
->udc
->lock
);
61 list_for_each_entry_safe(req
, req_copy
, queue_data
, queue
) {
62 list_del(&req
->queue
);
70 * bbbbbbbb llllllll IZS sssss nnnn FDL\n\0
74 * I/i: interrupt/no interrupt
76 * S/s: short ok/short not ok
79 * F/f: submitted/not submitted to FIFO
80 * D/d: using/not using DMA
81 * L/l: last transaction/not last transaction
83 static ssize_t
queue_dbg_read(struct file
*file
, char __user
*buf
,
84 size_t nbytes
, loff_t
*ppos
)
86 struct list_head
*queue
= file
->private_data
;
87 struct usba_request
*req
, *tmp_req
;
88 size_t len
, remaining
, actual
= 0;
91 if (!access_ok(VERIFY_WRITE
, buf
, nbytes
))
94 mutex_lock(&file_inode(file
)->i_mutex
);
95 list_for_each_entry_safe(req
, tmp_req
, queue
, queue
) {
96 len
= snprintf(tmpbuf
, sizeof(tmpbuf
),
97 "%8p %08x %c%c%c %5d %c%c%c\n",
98 req
->req
.buf
, req
->req
.length
,
99 req
->req
.no_interrupt
? 'i' : 'I',
100 req
->req
.zero
? 'Z' : 'z',
101 req
->req
.short_not_ok
? 's' : 'S',
103 req
->submitted
? 'F' : 'f',
104 req
->using_dma
? 'D' : 'd',
105 req
->last_transaction
? 'L' : 'l');
106 len
= min(len
, sizeof(tmpbuf
));
110 list_del(&req
->queue
);
113 remaining
= __copy_to_user(buf
, tmpbuf
, len
);
114 actual
+= len
- remaining
;
121 mutex_unlock(&file_inode(file
)->i_mutex
);
126 static int queue_dbg_release(struct inode
*inode
, struct file
*file
)
128 struct list_head
*queue_data
= file
->private_data
;
129 struct usba_request
*req
, *tmp_req
;
131 list_for_each_entry_safe(req
, tmp_req
, queue_data
, queue
) {
132 list_del(&req
->queue
);
139 static int regs_dbg_open(struct inode
*inode
, struct file
*file
)
141 struct usba_udc
*udc
;
146 mutex_lock(&inode
->i_mutex
);
147 udc
= inode
->i_private
;
148 data
= kmalloc(inode
->i_size
, GFP_KERNEL
);
152 spin_lock_irq(&udc
->lock
);
153 for (i
= 0; i
< inode
->i_size
/ 4; i
++)
154 data
[i
] = usba_io_readl(udc
->regs
+ i
* 4);
155 spin_unlock_irq(&udc
->lock
);
157 file
->private_data
= data
;
161 mutex_unlock(&inode
->i_mutex
);
166 static ssize_t
regs_dbg_read(struct file
*file
, char __user
*buf
,
167 size_t nbytes
, loff_t
*ppos
)
169 struct inode
*inode
= file_inode(file
);
172 mutex_lock(&inode
->i_mutex
);
173 ret
= simple_read_from_buffer(buf
, nbytes
, ppos
,
175 file_inode(file
)->i_size
);
176 mutex_unlock(&inode
->i_mutex
);
181 static int regs_dbg_release(struct inode
*inode
, struct file
*file
)
183 kfree(file
->private_data
);
187 const struct file_operations queue_dbg_fops
= {
188 .owner
= THIS_MODULE
,
189 .open
= queue_dbg_open
,
191 .read
= queue_dbg_read
,
192 .release
= queue_dbg_release
,
195 const struct file_operations regs_dbg_fops
= {
196 .owner
= THIS_MODULE
,
197 .open
= regs_dbg_open
,
198 .llseek
= generic_file_llseek
,
199 .read
= regs_dbg_read
,
200 .release
= regs_dbg_release
,
203 static void usba_ep_init_debugfs(struct usba_udc
*udc
,
206 struct dentry
*ep_root
;
208 ep_root
= debugfs_create_dir(ep
->ep
.name
, udc
->debugfs_root
);
211 ep
->debugfs_dir
= ep_root
;
213 ep
->debugfs_queue
= debugfs_create_file("queue", 0400, ep_root
,
214 ep
, &queue_dbg_fops
);
215 if (!ep
->debugfs_queue
)
219 ep
->debugfs_dma_status
220 = debugfs_create_u32("dma_status", 0400, ep_root
,
221 &ep
->last_dma_status
);
222 if (!ep
->debugfs_dma_status
)
225 if (ep_is_control(ep
)) {
227 = debugfs_create_u32("state", 0400, ep_root
,
229 if (!ep
->debugfs_state
)
237 debugfs_remove(ep
->debugfs_dma_status
);
239 debugfs_remove(ep
->debugfs_queue
);
241 debugfs_remove(ep_root
);
243 dev_err(&ep
->udc
->pdev
->dev
,
244 "failed to create debugfs directory for %s\n", ep
->ep
.name
);
247 static void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
249 debugfs_remove(ep
->debugfs_queue
);
250 debugfs_remove(ep
->debugfs_dma_status
);
251 debugfs_remove(ep
->debugfs_state
);
252 debugfs_remove(ep
->debugfs_dir
);
253 ep
->debugfs_dma_status
= NULL
;
254 ep
->debugfs_dir
= NULL
;
257 static void usba_init_debugfs(struct usba_udc
*udc
)
259 struct dentry
*root
, *regs
;
260 struct resource
*regs_resource
;
262 root
= debugfs_create_dir(udc
->gadget
.name
, NULL
);
263 if (IS_ERR(root
) || !root
)
265 udc
->debugfs_root
= root
;
267 regs_resource
= platform_get_resource(udc
->pdev
, IORESOURCE_MEM
,
271 regs
= debugfs_create_file_size("regs", 0400, root
, udc
,
273 resource_size(regs_resource
));
276 udc
->debugfs_regs
= regs
;
279 usba_ep_init_debugfs(udc
, to_usba_ep(udc
->gadget
.ep0
));
284 debugfs_remove(root
);
286 udc
->debugfs_root
= NULL
;
287 dev_err(&udc
->pdev
->dev
, "debugfs is not available\n");
290 static void usba_cleanup_debugfs(struct usba_udc
*udc
)
292 usba_ep_cleanup_debugfs(to_usba_ep(udc
->gadget
.ep0
));
293 debugfs_remove(udc
->debugfs_regs
);
294 debugfs_remove(udc
->debugfs_root
);
295 udc
->debugfs_regs
= NULL
;
296 udc
->debugfs_root
= NULL
;
299 static inline void usba_ep_init_debugfs(struct usba_udc
*udc
,
305 static inline void usba_ep_cleanup_debugfs(struct usba_ep
*ep
)
310 static inline void usba_init_debugfs(struct usba_udc
*udc
)
315 static inline void usba_cleanup_debugfs(struct usba_udc
*udc
)
321 static inline u32
usba_int_enb_get(struct usba_udc
*udc
)
323 return udc
->int_enb_cache
;
326 static inline void usba_int_enb_set(struct usba_udc
*udc
, u32 val
)
328 usba_writel(udc
, INT_ENB
, val
);
329 udc
->int_enb_cache
= val
;
332 static int vbus_is_present(struct usba_udc
*udc
)
334 if (gpio_is_valid(udc
->vbus_pin
))
335 return gpio_get_value(udc
->vbus_pin
) ^ udc
->vbus_pin_inverted
;
337 /* No Vbus detection: Assume always present */
341 static void toggle_bias(struct usba_udc
*udc
, int is_on
)
343 if (udc
->errata
&& udc
->errata
->toggle_bias
)
344 udc
->errata
->toggle_bias(udc
, is_on
);
347 static void generate_bias_pulse(struct usba_udc
*udc
)
349 if (!udc
->bias_pulse_needed
)
352 if (udc
->errata
&& udc
->errata
->pulse_bias
)
353 udc
->errata
->pulse_bias(udc
);
355 udc
->bias_pulse_needed
= false;
358 static void next_fifo_transaction(struct usba_ep
*ep
, struct usba_request
*req
)
360 unsigned int transaction_len
;
362 transaction_len
= req
->req
.length
- req
->req
.actual
;
363 req
->last_transaction
= 1;
364 if (transaction_len
> ep
->ep
.maxpacket
) {
365 transaction_len
= ep
->ep
.maxpacket
;
366 req
->last_transaction
= 0;
367 } else if (transaction_len
== ep
->ep
.maxpacket
&& req
->req
.zero
)
368 req
->last_transaction
= 0;
370 DBG(DBG_QUEUE
, "%s: submit_transaction, req %p (length %d)%s\n",
371 ep
->ep
.name
, req
, transaction_len
,
372 req
->last_transaction
? ", done" : "");
374 memcpy_toio(ep
->fifo
, req
->req
.buf
+ req
->req
.actual
, transaction_len
);
375 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
376 req
->req
.actual
+= transaction_len
;
379 static void submit_request(struct usba_ep
*ep
, struct usba_request
*req
)
381 DBG(DBG_QUEUE
, "%s: submit_request: req %p (length %d)\n",
382 ep
->ep
.name
, req
, req
->req
.length
);
387 if (req
->using_dma
) {
388 if (req
->req
.length
== 0) {
389 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
394 usba_ep_writel(ep
, CTL_ENB
, USBA_SHORT_PACKET
);
396 usba_ep_writel(ep
, CTL_DIS
, USBA_SHORT_PACKET
);
398 usba_dma_writel(ep
, ADDRESS
, req
->req
.dma
);
399 usba_dma_writel(ep
, CONTROL
, req
->ctrl
);
401 next_fifo_transaction(ep
, req
);
402 if (req
->last_transaction
) {
403 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
404 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
406 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
407 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
412 static void submit_next_request(struct usba_ep
*ep
)
414 struct usba_request
*req
;
416 if (list_empty(&ep
->queue
)) {
417 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
| USBA_RX_BK_RDY
);
421 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
423 submit_request(ep
, req
);
426 static void send_status(struct usba_udc
*udc
, struct usba_ep
*ep
)
428 ep
->state
= STATUS_STAGE_IN
;
429 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
430 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
433 static void receive_data(struct usba_ep
*ep
)
435 struct usba_udc
*udc
= ep
->udc
;
436 struct usba_request
*req
;
437 unsigned long status
;
438 unsigned int bytecount
, nr_busy
;
441 status
= usba_ep_readl(ep
, STA
);
442 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
444 DBG(DBG_QUEUE
, "receive data: nr_busy=%u\n", nr_busy
);
446 while (nr_busy
> 0) {
447 if (list_empty(&ep
->queue
)) {
448 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
451 req
= list_entry(ep
->queue
.next
,
452 struct usba_request
, queue
);
454 bytecount
= USBA_BFEXT(BYTE_COUNT
, status
);
456 if (status
& (1 << 31))
458 if (req
->req
.actual
+ bytecount
>= req
->req
.length
) {
460 bytecount
= req
->req
.length
- req
->req
.actual
;
463 memcpy_fromio(req
->req
.buf
+ req
->req
.actual
,
464 ep
->fifo
, bytecount
);
465 req
->req
.actual
+= bytecount
;
467 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
470 DBG(DBG_QUEUE
, "%s: request done\n", ep
->ep
.name
);
472 list_del_init(&req
->queue
);
473 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
474 spin_unlock(&udc
->lock
);
475 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
476 spin_lock(&udc
->lock
);
479 status
= usba_ep_readl(ep
, STA
);
480 nr_busy
= USBA_BFEXT(BUSY_BANKS
, status
);
482 if (is_complete
&& ep_is_control(ep
)) {
483 send_status(udc
, ep
);
490 request_complete(struct usba_ep
*ep
, struct usba_request
*req
, int status
)
492 struct usba_udc
*udc
= ep
->udc
;
494 WARN_ON(!list_empty(&req
->queue
));
496 if (req
->req
.status
== -EINPROGRESS
)
497 req
->req
.status
= status
;
500 usb_gadget_unmap_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
502 DBG(DBG_GADGET
| DBG_REQ
,
503 "%s: req %p complete: status %d, actual %u\n",
504 ep
->ep
.name
, req
, req
->req
.status
, req
->req
.actual
);
506 spin_unlock(&udc
->lock
);
507 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
508 spin_lock(&udc
->lock
);
512 request_complete_list(struct usba_ep
*ep
, struct list_head
*list
, int status
)
514 struct usba_request
*req
, *tmp_req
;
516 list_for_each_entry_safe(req
, tmp_req
, list
, queue
) {
517 list_del_init(&req
->queue
);
518 request_complete(ep
, req
, status
);
523 usba_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
525 struct usba_ep
*ep
= to_usba_ep(_ep
);
526 struct usba_udc
*udc
= ep
->udc
;
527 unsigned long flags
, ept_cfg
, maxpacket
;
528 unsigned int nr_trans
;
530 DBG(DBG_GADGET
, "%s: ep_enable: desc=%p\n", ep
->ep
.name
, desc
);
532 maxpacket
= usb_endpoint_maxp(desc
) & 0x7ff;
534 if (((desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
) != ep
->index
)
536 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
538 || maxpacket
> ep
->fifo_size
) {
539 DBG(DBG_ERR
, "ep_enable: Invalid argument");
547 ept_cfg
= USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_8
);
549 /* LSB is bit 1, not 0 */
550 ept_cfg
= USBA_BF(EPT_SIZE
, fls(maxpacket
- 1) - 3);
552 DBG(DBG_HW
, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
553 ep
->ep
.name
, ept_cfg
, maxpacket
);
555 if (usb_endpoint_dir_in(desc
)) {
557 ept_cfg
|= USBA_EPT_DIR_IN
;
560 switch (usb_endpoint_type(desc
)) {
561 case USB_ENDPOINT_XFER_CONTROL
:
562 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
);
563 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
);
565 case USB_ENDPOINT_XFER_ISOC
:
567 DBG(DBG_ERR
, "ep_enable: %s is not isoc capable\n",
573 * Bits 11:12 specify number of _additional_
574 * transactions per microframe.
576 nr_trans
= ((usb_endpoint_maxp(desc
) >> 11) & 3) + 1;
581 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_ISO
);
584 * Do triple-buffering on high-bandwidth iso endpoints.
586 if (nr_trans
> 1 && ep
->nr_banks
== 3)
587 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_TRIPLE
);
589 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
590 ept_cfg
|= USBA_BF(NB_TRANS
, nr_trans
);
592 case USB_ENDPOINT_XFER_BULK
:
593 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
);
594 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
596 case USB_ENDPOINT_XFER_INT
:
597 ept_cfg
|= USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_INT
);
598 ept_cfg
|= USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_DOUBLE
);
602 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
605 ep
->ep
.maxpacket
= maxpacket
;
607 usba_ep_writel(ep
, CFG
, ept_cfg
);
608 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
613 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
614 USBA_BF(EPT_INT
, 1 << ep
->index
) |
615 USBA_BF(DMA_INT
, 1 << ep
->index
));
616 ctrl
= USBA_AUTO_VALID
| USBA_INTDIS_DMA
;
617 usba_ep_writel(ep
, CTL_ENB
, ctrl
);
619 usba_int_enb_set(udc
, usba_int_enb_get(udc
) |
620 USBA_BF(EPT_INT
, 1 << ep
->index
));
623 spin_unlock_irqrestore(&udc
->lock
, flags
);
625 DBG(DBG_HW
, "EPT_CFG%d after init: %#08lx\n", ep
->index
,
626 (unsigned long)usba_ep_readl(ep
, CFG
));
627 DBG(DBG_HW
, "INT_ENB after init: %#08lx\n",
628 (unsigned long)usba_int_enb_get(udc
));
633 static int usba_ep_disable(struct usb_ep
*_ep
)
635 struct usba_ep
*ep
= to_usba_ep(_ep
);
636 struct usba_udc
*udc
= ep
->udc
;
640 DBG(DBG_GADGET
, "ep_disable: %s\n", ep
->ep
.name
);
642 spin_lock_irqsave(&udc
->lock
, flags
);
645 spin_unlock_irqrestore(&udc
->lock
, flags
);
646 /* REVISIT because this driver disables endpoints in
647 * reset_all_endpoints() before calling disconnect(),
648 * most gadget drivers would trigger this non-error ...
650 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
651 DBG(DBG_ERR
, "ep_disable: %s not enabled\n",
657 list_splice_init(&ep
->queue
, &req_list
);
659 usba_dma_writel(ep
, CONTROL
, 0);
660 usba_dma_writel(ep
, ADDRESS
, 0);
661 usba_dma_readl(ep
, STATUS
);
663 usba_ep_writel(ep
, CTL_DIS
, USBA_EPT_ENABLE
);
664 usba_int_enb_set(udc
, usba_int_enb_get(udc
) &
665 ~USBA_BF(EPT_INT
, 1 << ep
->index
));
667 request_complete_list(ep
, &req_list
, -ESHUTDOWN
);
669 spin_unlock_irqrestore(&udc
->lock
, flags
);
674 static struct usb_request
*
675 usba_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
677 struct usba_request
*req
;
679 DBG(DBG_GADGET
, "ep_alloc_request: %p, 0x%x\n", _ep
, gfp_flags
);
681 req
= kzalloc(sizeof(*req
), gfp_flags
);
685 INIT_LIST_HEAD(&req
->queue
);
691 usba_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
693 struct usba_request
*req
= to_usba_req(_req
);
695 DBG(DBG_GADGET
, "ep_free_request: %p, %p\n", _ep
, _req
);
700 static int queue_dma(struct usba_udc
*udc
, struct usba_ep
*ep
,
701 struct usba_request
*req
, gfp_t gfp_flags
)
706 DBG(DBG_DMA
, "%s: req l/%u d/%pad %c%c%c\n",
707 ep
->ep
.name
, req
->req
.length
, &req
->req
.dma
,
708 req
->req
.zero
? 'Z' : 'z',
709 req
->req
.short_not_ok
? 'S' : 's',
710 req
->req
.no_interrupt
? 'I' : 'i');
712 if (req
->req
.length
> 0x10000) {
713 /* Lengths from 0 to 65536 (inclusive) are supported */
714 DBG(DBG_ERR
, "invalid request length %u\n", req
->req
.length
);
718 ret
= usb_gadget_map_request(&udc
->gadget
, &req
->req
, ep
->is_in
);
723 req
->ctrl
= USBA_BF(DMA_BUF_LEN
, req
->req
.length
)
724 | USBA_DMA_CH_EN
| USBA_DMA_END_BUF_IE
725 | USBA_DMA_END_BUF_EN
;
728 req
->ctrl
|= USBA_DMA_END_TR_EN
| USBA_DMA_END_TR_IE
;
731 * Add this request to the queue and submit for DMA if
732 * possible. Check if we're still alive first -- we may have
733 * received a reset since last time we checked.
736 spin_lock_irqsave(&udc
->lock
, flags
);
738 if (list_empty(&ep
->queue
))
739 submit_request(ep
, req
);
741 list_add_tail(&req
->queue
, &ep
->queue
);
744 spin_unlock_irqrestore(&udc
->lock
, flags
);
750 usba_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
752 struct usba_request
*req
= to_usba_req(_req
);
753 struct usba_ep
*ep
= to_usba_ep(_ep
);
754 struct usba_udc
*udc
= ep
->udc
;
758 DBG(DBG_GADGET
| DBG_QUEUE
| DBG_REQ
, "%s: queue req %p, len %u\n",
759 ep
->ep
.name
, req
, _req
->length
);
761 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
||
767 req
->last_transaction
= 0;
769 _req
->status
= -EINPROGRESS
;
773 return queue_dma(udc
, ep
, req
, gfp_flags
);
775 /* May have received a reset since last time we checked */
777 spin_lock_irqsave(&udc
->lock
, flags
);
779 list_add_tail(&req
->queue
, &ep
->queue
);
781 if ((!ep_is_control(ep
) && ep
->is_in
) ||
783 && (ep
->state
== DATA_STAGE_IN
784 || ep
->state
== STATUS_STAGE_IN
)))
785 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_PK_RDY
);
787 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
790 spin_unlock_irqrestore(&udc
->lock
, flags
);
796 usba_update_req(struct usba_ep
*ep
, struct usba_request
*req
, u32 status
)
798 req
->req
.actual
= req
->req
.length
- USBA_BFEXT(DMA_BUF_LEN
, status
);
801 static int stop_dma(struct usba_ep
*ep
, u32
*pstatus
)
803 unsigned int timeout
;
807 * Stop the DMA controller. When writing both CH_EN
808 * and LINK to 0, the other bits are not affected.
810 usba_dma_writel(ep
, CONTROL
, 0);
812 /* Wait for the FIFO to empty */
813 for (timeout
= 40; timeout
; --timeout
) {
814 status
= usba_dma_readl(ep
, STATUS
);
815 if (!(status
& USBA_DMA_CH_EN
))
824 dev_err(&ep
->udc
->pdev
->dev
,
825 "%s: timed out waiting for DMA FIFO to empty\n",
833 static int usba_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
835 struct usba_ep
*ep
= to_usba_ep(_ep
);
836 struct usba_udc
*udc
= ep
->udc
;
837 struct usba_request
*req
;
841 DBG(DBG_GADGET
| DBG_QUEUE
, "ep_dequeue: %s, req %p\n",
844 spin_lock_irqsave(&udc
->lock
, flags
);
846 list_for_each_entry(req
, &ep
->queue
, queue
) {
847 if (&req
->req
== _req
)
851 if (&req
->req
!= _req
) {
852 spin_unlock_irqrestore(&udc
->lock
, flags
);
856 if (req
->using_dma
) {
858 * If this request is currently being transferred,
859 * stop the DMA controller and reset the FIFO.
861 if (ep
->queue
.next
== &req
->queue
) {
862 status
= usba_dma_readl(ep
, STATUS
);
863 if (status
& USBA_DMA_CH_EN
)
864 stop_dma(ep
, &status
);
866 #ifdef CONFIG_USB_GADGET_DEBUG_FS
867 ep
->last_dma_status
= status
;
870 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
872 usba_update_req(ep
, req
, status
);
877 * Errors should stop the queue from advancing until the
878 * completion function returns.
880 list_del_init(&req
->queue
);
882 request_complete(ep
, req
, -ECONNRESET
);
884 /* Process the next request if any */
885 submit_next_request(ep
);
886 spin_unlock_irqrestore(&udc
->lock
, flags
);
891 static int usba_ep_set_halt(struct usb_ep
*_ep
, int value
)
893 struct usba_ep
*ep
= to_usba_ep(_ep
);
894 struct usba_udc
*udc
= ep
->udc
;
898 DBG(DBG_GADGET
, "endpoint %s: %s HALT\n", ep
->ep
.name
,
899 value
? "set" : "clear");
902 DBG(DBG_ERR
, "Attempted to halt uninitialized ep %s\n",
907 DBG(DBG_ERR
, "Attempted to halt isochronous ep %s\n",
912 spin_lock_irqsave(&udc
->lock
, flags
);
915 * We can't halt IN endpoints while there are still data to be
918 if (!list_empty(&ep
->queue
)
919 || ((value
&& ep
->is_in
&& (usba_ep_readl(ep
, STA
)
920 & USBA_BF(BUSY_BANKS
, -1L))))) {
924 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
926 usba_ep_writel(ep
, CLR_STA
,
927 USBA_FORCE_STALL
| USBA_TOGGLE_CLR
);
928 usba_ep_readl(ep
, STA
);
931 spin_unlock_irqrestore(&udc
->lock
, flags
);
936 static int usba_ep_fifo_status(struct usb_ep
*_ep
)
938 struct usba_ep
*ep
= to_usba_ep(_ep
);
940 return USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
943 static void usba_ep_fifo_flush(struct usb_ep
*_ep
)
945 struct usba_ep
*ep
= to_usba_ep(_ep
);
946 struct usba_udc
*udc
= ep
->udc
;
948 usba_writel(udc
, EPT_RST
, 1 << ep
->index
);
951 static const struct usb_ep_ops usba_ep_ops
= {
952 .enable
= usba_ep_enable
,
953 .disable
= usba_ep_disable
,
954 .alloc_request
= usba_ep_alloc_request
,
955 .free_request
= usba_ep_free_request
,
956 .queue
= usba_ep_queue
,
957 .dequeue
= usba_ep_dequeue
,
958 .set_halt
= usba_ep_set_halt
,
959 .fifo_status
= usba_ep_fifo_status
,
960 .fifo_flush
= usba_ep_fifo_flush
,
963 static int usba_udc_get_frame(struct usb_gadget
*gadget
)
965 struct usba_udc
*udc
= to_usba_udc(gadget
);
967 return USBA_BFEXT(FRAME_NUMBER
, usba_readl(udc
, FNUM
));
970 static int usba_udc_wakeup(struct usb_gadget
*gadget
)
972 struct usba_udc
*udc
= to_usba_udc(gadget
);
977 spin_lock_irqsave(&udc
->lock
, flags
);
978 if (udc
->devstatus
& (1 << USB_DEVICE_REMOTE_WAKEUP
)) {
979 ctrl
= usba_readl(udc
, CTRL
);
980 usba_writel(udc
, CTRL
, ctrl
| USBA_REMOTE_WAKE_UP
);
983 spin_unlock_irqrestore(&udc
->lock
, flags
);
989 usba_udc_set_selfpowered(struct usb_gadget
*gadget
, int is_selfpowered
)
991 struct usba_udc
*udc
= to_usba_udc(gadget
);
994 gadget
->is_selfpowered
= (is_selfpowered
!= 0);
995 spin_lock_irqsave(&udc
->lock
, flags
);
997 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
999 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1000 spin_unlock_irqrestore(&udc
->lock
, flags
);
1005 static int atmel_usba_start(struct usb_gadget
*gadget
,
1006 struct usb_gadget_driver
*driver
);
1007 static int atmel_usba_stop(struct usb_gadget
*gadget
);
1009 static const struct usb_gadget_ops usba_udc_ops
= {
1010 .get_frame
= usba_udc_get_frame
,
1011 .wakeup
= usba_udc_wakeup
,
1012 .set_selfpowered
= usba_udc_set_selfpowered
,
1013 .udc_start
= atmel_usba_start
,
1014 .udc_stop
= atmel_usba_stop
,
1017 static struct usb_endpoint_descriptor usba_ep0_desc
= {
1018 .bLength
= USB_DT_ENDPOINT_SIZE
,
1019 .bDescriptorType
= USB_DT_ENDPOINT
,
1020 .bEndpointAddress
= 0,
1021 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1022 .wMaxPacketSize
= cpu_to_le16(64),
1023 /* FIXME: I have no idea what to put here */
1027 static struct usb_gadget usba_gadget_template
= {
1028 .ops
= &usba_udc_ops
,
1029 .max_speed
= USB_SPEED_HIGH
,
1030 .name
= "atmel_usba_udc",
1034 * Called with interrupts disabled and udc->lock held.
1036 static void reset_all_endpoints(struct usba_udc
*udc
)
1039 struct usba_request
*req
, *tmp_req
;
1041 usba_writel(udc
, EPT_RST
, ~0UL);
1043 ep
= to_usba_ep(udc
->gadget
.ep0
);
1044 list_for_each_entry_safe(req
, tmp_req
, &ep
->queue
, queue
) {
1045 list_del_init(&req
->queue
);
1046 request_complete(ep
, req
, -ECONNRESET
);
1049 /* NOTE: normally, the next call to the gadget driver is in
1050 * charge of disabling endpoints... usually disconnect().
1051 * The exception would be entering a high speed test mode.
1053 * FIXME remove this code ... and retest thoroughly.
1055 list_for_each_entry(ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1057 spin_unlock(&udc
->lock
);
1058 usba_ep_disable(&ep
->ep
);
1059 spin_lock(&udc
->lock
);
1064 static struct usba_ep
*get_ep_by_addr(struct usba_udc
*udc
, u16 wIndex
)
1068 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
1069 return to_usba_ep(udc
->gadget
.ep0
);
1071 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
1072 u8 bEndpointAddress
;
1076 bEndpointAddress
= ep
->ep
.desc
->bEndpointAddress
;
1077 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
1079 if ((bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
)
1080 == (wIndex
& USB_ENDPOINT_NUMBER_MASK
))
1087 /* Called with interrupts disabled and udc->lock held */
1088 static inline void set_protocol_stall(struct usba_udc
*udc
, struct usba_ep
*ep
)
1090 usba_ep_writel(ep
, SET_STA
, USBA_FORCE_STALL
);
1091 ep
->state
= WAIT_FOR_SETUP
;
1094 static inline int is_stalled(struct usba_udc
*udc
, struct usba_ep
*ep
)
1096 if (usba_ep_readl(ep
, STA
) & USBA_FORCE_STALL
)
1101 static inline void set_address(struct usba_udc
*udc
, unsigned int addr
)
1105 DBG(DBG_BUS
, "setting address %u...\n", addr
);
1106 regval
= usba_readl(udc
, CTRL
);
1107 regval
= USBA_BFINS(DEV_ADDR
, addr
, regval
);
1108 usba_writel(udc
, CTRL
, regval
);
1111 static int do_test_mode(struct usba_udc
*udc
)
1113 static const char test_packet_buffer
[] = {
1115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1117 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1119 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
1120 /* JJJJJJJKKKKKKK * 8 */
1121 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1122 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1124 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD,
1125 /* {JKKKKKKK * 10}, JK */
1126 0xFC, 0x7E, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0x7E
1129 struct device
*dev
= &udc
->pdev
->dev
;
1132 test_mode
= udc
->test_mode
;
1134 /* Start from a clean slate */
1135 reset_all_endpoints(udc
);
1137 switch (test_mode
) {
1140 usba_writel(udc
, TST
, USBA_TST_J_MODE
);
1141 dev_info(dev
, "Entering Test_J mode...\n");
1145 usba_writel(udc
, TST
, USBA_TST_K_MODE
);
1146 dev_info(dev
, "Entering Test_K mode...\n");
1150 * Test_SE0_NAK: Force high-speed mode and set up ep0
1151 * for Bulk IN transfers
1153 ep
= &udc
->usba_ep
[0];
1154 usba_writel(udc
, TST
,
1155 USBA_BF(SPEED_CFG
, USBA_SPEED_CFG_FORCE_HIGH
));
1156 usba_ep_writel(ep
, CFG
,
1157 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1159 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1160 | USBA_BF(BK_NUMBER
, 1));
1161 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1162 set_protocol_stall(udc
, ep
);
1163 dev_err(dev
, "Test_SE0_NAK: ep0 not mapped\n");
1165 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1166 dev_info(dev
, "Entering Test_SE0_NAK mode...\n");
1171 ep
= &udc
->usba_ep
[0];
1172 usba_ep_writel(ep
, CFG
,
1173 USBA_BF(EPT_SIZE
, USBA_EPT_SIZE_64
)
1175 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_BULK
)
1176 | USBA_BF(BK_NUMBER
, 1));
1177 if (!(usba_ep_readl(ep
, CFG
) & USBA_EPT_MAPPED
)) {
1178 set_protocol_stall(udc
, ep
);
1179 dev_err(dev
, "Test_Packet: ep0 not mapped\n");
1181 usba_ep_writel(ep
, CTL_ENB
, USBA_EPT_ENABLE
);
1182 usba_writel(udc
, TST
, USBA_TST_PKT_MODE
);
1183 memcpy_toio(ep
->fifo
, test_packet_buffer
,
1184 sizeof(test_packet_buffer
));
1185 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1186 dev_info(dev
, "Entering Test_Packet mode...\n");
1190 dev_err(dev
, "Invalid test mode: 0x%04x\n", test_mode
);
1197 /* Avoid overly long expressions */
1198 static inline bool feature_is_dev_remote_wakeup(struct usb_ctrlrequest
*crq
)
1200 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP
))
1205 static inline bool feature_is_dev_test_mode(struct usb_ctrlrequest
*crq
)
1207 if (crq
->wValue
== cpu_to_le16(USB_DEVICE_TEST_MODE
))
1212 static inline bool feature_is_ep_halt(struct usb_ctrlrequest
*crq
)
1214 if (crq
->wValue
== cpu_to_le16(USB_ENDPOINT_HALT
))
1219 static int handle_ep0_setup(struct usba_udc
*udc
, struct usba_ep
*ep
,
1220 struct usb_ctrlrequest
*crq
)
1224 switch (crq
->bRequest
) {
1225 case USB_REQ_GET_STATUS
: {
1228 if (crq
->bRequestType
== (USB_DIR_IN
| USB_RECIP_DEVICE
)) {
1229 status
= cpu_to_le16(udc
->devstatus
);
1230 } else if (crq
->bRequestType
1231 == (USB_DIR_IN
| USB_RECIP_INTERFACE
)) {
1232 status
= cpu_to_le16(0);
1233 } else if (crq
->bRequestType
1234 == (USB_DIR_IN
| USB_RECIP_ENDPOINT
)) {
1235 struct usba_ep
*target
;
1237 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1242 if (is_stalled(udc
, target
))
1243 status
|= cpu_to_le16(1);
1247 /* Write directly to the FIFO. No queueing is done. */
1248 if (crq
->wLength
!= cpu_to_le16(sizeof(status
)))
1250 ep
->state
= DATA_STAGE_IN
;
1251 usba_io_writew(status
, ep
->fifo
);
1252 usba_ep_writel(ep
, SET_STA
, USBA_TX_PK_RDY
);
1256 case USB_REQ_CLEAR_FEATURE
: {
1257 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1258 if (feature_is_dev_remote_wakeup(crq
))
1260 &= ~(1 << USB_DEVICE_REMOTE_WAKEUP
);
1262 /* Can't CLEAR_FEATURE TEST_MODE */
1264 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1265 struct usba_ep
*target
;
1267 if (crq
->wLength
!= cpu_to_le16(0)
1268 || !feature_is_ep_halt(crq
))
1270 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1274 usba_ep_writel(target
, CLR_STA
, USBA_FORCE_STALL
);
1275 if (target
->index
!= 0)
1276 usba_ep_writel(target
, CLR_STA
,
1282 send_status(udc
, ep
);
1286 case USB_REQ_SET_FEATURE
: {
1287 if (crq
->bRequestType
== USB_RECIP_DEVICE
) {
1288 if (feature_is_dev_test_mode(crq
)) {
1289 send_status(udc
, ep
);
1290 ep
->state
= STATUS_STAGE_TEST
;
1291 udc
->test_mode
= le16_to_cpu(crq
->wIndex
);
1293 } else if (feature_is_dev_remote_wakeup(crq
)) {
1294 udc
->devstatus
|= 1 << USB_DEVICE_REMOTE_WAKEUP
;
1298 } else if (crq
->bRequestType
== USB_RECIP_ENDPOINT
) {
1299 struct usba_ep
*target
;
1301 if (crq
->wLength
!= cpu_to_le16(0)
1302 || !feature_is_ep_halt(crq
))
1305 target
= get_ep_by_addr(udc
, le16_to_cpu(crq
->wIndex
));
1309 usba_ep_writel(target
, SET_STA
, USBA_FORCE_STALL
);
1313 send_status(udc
, ep
);
1317 case USB_REQ_SET_ADDRESS
:
1318 if (crq
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
1321 set_address(udc
, le16_to_cpu(crq
->wValue
));
1322 send_status(udc
, ep
);
1323 ep
->state
= STATUS_STAGE_ADDR
;
1328 spin_unlock(&udc
->lock
);
1329 retval
= udc
->driver
->setup(&udc
->gadget
, crq
);
1330 spin_lock(&udc
->lock
);
1336 pr_err("udc: %s: Invalid setup request: %02x.%02x v%04x i%04x l%d, "
1337 "halting endpoint...\n",
1338 ep
->ep
.name
, crq
->bRequestType
, crq
->bRequest
,
1339 le16_to_cpu(crq
->wValue
), le16_to_cpu(crq
->wIndex
),
1340 le16_to_cpu(crq
->wLength
));
1341 set_protocol_stall(udc
, ep
);
1345 static void usba_control_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1347 struct usba_request
*req
;
1352 epstatus
= usba_ep_readl(ep
, STA
);
1353 epctrl
= usba_ep_readl(ep
, CTL
);
1355 DBG(DBG_INT
, "%s [%d]: s/%08x c/%08x\n",
1356 ep
->ep
.name
, ep
->state
, epstatus
, epctrl
);
1359 if (!list_empty(&ep
->queue
))
1360 req
= list_entry(ep
->queue
.next
,
1361 struct usba_request
, queue
);
1363 if ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1365 next_fifo_transaction(ep
, req
);
1367 submit_request(ep
, req
);
1369 if (req
->last_transaction
) {
1370 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1371 usba_ep_writel(ep
, CTL_ENB
, USBA_TX_COMPLETE
);
1375 if ((epstatus
& epctrl
) & USBA_TX_COMPLETE
) {
1376 usba_ep_writel(ep
, CLR_STA
, USBA_TX_COMPLETE
);
1378 switch (ep
->state
) {
1380 usba_ep_writel(ep
, CTL_ENB
, USBA_RX_BK_RDY
);
1381 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1382 ep
->state
= STATUS_STAGE_OUT
;
1384 case STATUS_STAGE_ADDR
:
1385 /* Activate our new address */
1386 usba_writel(udc
, CTRL
, (usba_readl(udc
, CTRL
)
1388 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1389 ep
->state
= WAIT_FOR_SETUP
;
1391 case STATUS_STAGE_IN
:
1393 list_del_init(&req
->queue
);
1394 request_complete(ep
, req
, 0);
1395 submit_next_request(ep
);
1397 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1398 ep
->state
= WAIT_FOR_SETUP
;
1400 case STATUS_STAGE_TEST
:
1401 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_COMPLETE
);
1402 ep
->state
= WAIT_FOR_SETUP
;
1403 if (do_test_mode(udc
))
1404 set_protocol_stall(udc
, ep
);
1407 pr_err("udc: %s: TXCOMP: Invalid endpoint state %d, "
1408 "halting endpoint...\n",
1409 ep
->ep
.name
, ep
->state
);
1410 set_protocol_stall(udc
, ep
);
1416 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1417 switch (ep
->state
) {
1418 case STATUS_STAGE_OUT
:
1419 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1420 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1423 list_del_init(&req
->queue
);
1424 request_complete(ep
, req
, 0);
1426 ep
->state
= WAIT_FOR_SETUP
;
1429 case DATA_STAGE_OUT
:
1434 usba_ep_writel(ep
, CLR_STA
, USBA_RX_BK_RDY
);
1435 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1436 pr_err("udc: %s: RXRDY: Invalid endpoint state %d, "
1437 "halting endpoint...\n",
1438 ep
->ep
.name
, ep
->state
);
1439 set_protocol_stall(udc
, ep
);
1445 if (epstatus
& USBA_RX_SETUP
) {
1447 struct usb_ctrlrequest crq
;
1448 unsigned long data
[2];
1450 unsigned int pkt_len
;
1453 if (ep
->state
!= WAIT_FOR_SETUP
) {
1455 * Didn't expect a SETUP packet at this
1456 * point. Clean up any pending requests (which
1457 * may be successful).
1459 int status
= -EPROTO
;
1462 * RXRDY and TXCOMP are dropped when SETUP
1463 * packets arrive. Just pretend we received
1464 * the status packet.
1466 if (ep
->state
== STATUS_STAGE_OUT
1467 || ep
->state
== STATUS_STAGE_IN
) {
1468 usba_ep_writel(ep
, CTL_DIS
, USBA_RX_BK_RDY
);
1473 list_del_init(&req
->queue
);
1474 request_complete(ep
, req
, status
);
1478 pkt_len
= USBA_BFEXT(BYTE_COUNT
, usba_ep_readl(ep
, STA
));
1479 DBG(DBG_HW
, "Packet length: %u\n", pkt_len
);
1480 if (pkt_len
!= sizeof(crq
)) {
1481 pr_warning("udc: Invalid packet length %u "
1482 "(expected %zu)\n", pkt_len
, sizeof(crq
));
1483 set_protocol_stall(udc
, ep
);
1487 DBG(DBG_FIFO
, "Copying ctrl request from 0x%p:\n", ep
->fifo
);
1488 memcpy_fromio(crq
.data
, ep
->fifo
, sizeof(crq
));
1490 /* Free up one bank in the FIFO so that we can
1491 * generate or receive a reply right away. */
1492 usba_ep_writel(ep
, CLR_STA
, USBA_RX_SETUP
);
1494 /* printk(KERN_DEBUG "setup: %d: %02x.%02x\n",
1495 ep->state, crq.crq.bRequestType,
1496 crq.crq.bRequest); */
1498 if (crq
.crq
.bRequestType
& USB_DIR_IN
) {
1500 * The USB 2.0 spec states that "if wLength is
1501 * zero, there is no data transfer phase."
1502 * However, testusb #14 seems to actually
1503 * expect a data phase even if wLength = 0...
1505 ep
->state
= DATA_STAGE_IN
;
1507 if (crq
.crq
.wLength
!= cpu_to_le16(0))
1508 ep
->state
= DATA_STAGE_OUT
;
1510 ep
->state
= STATUS_STAGE_IN
;
1515 ret
= handle_ep0_setup(udc
, ep
, &crq
.crq
);
1517 spin_unlock(&udc
->lock
);
1518 ret
= udc
->driver
->setup(&udc
->gadget
, &crq
.crq
);
1519 spin_lock(&udc
->lock
);
1522 DBG(DBG_BUS
, "req %02x.%02x, length %d, state %d, ret %d\n",
1523 crq
.crq
.bRequestType
, crq
.crq
.bRequest
,
1524 le16_to_cpu(crq
.crq
.wLength
), ep
->state
, ret
);
1527 /* Let the host know that we failed */
1528 set_protocol_stall(udc
, ep
);
1533 static void usba_ep_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1535 struct usba_request
*req
;
1539 epstatus
= usba_ep_readl(ep
, STA
);
1540 epctrl
= usba_ep_readl(ep
, CTL
);
1542 DBG(DBG_INT
, "%s: interrupt, status: 0x%08x\n", ep
->ep
.name
, epstatus
);
1544 while ((epctrl
& USBA_TX_PK_RDY
) && !(epstatus
& USBA_TX_PK_RDY
)) {
1545 DBG(DBG_BUS
, "%s: TX PK ready\n", ep
->ep
.name
);
1547 if (list_empty(&ep
->queue
)) {
1548 dev_warn(&udc
->pdev
->dev
, "ep_irq: queue empty\n");
1549 usba_ep_writel(ep
, CTL_DIS
, USBA_TX_PK_RDY
);
1553 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1555 if (req
->using_dma
) {
1556 /* Send a zero-length packet */
1557 usba_ep_writel(ep
, SET_STA
,
1559 usba_ep_writel(ep
, CTL_DIS
,
1561 list_del_init(&req
->queue
);
1562 submit_next_request(ep
);
1563 request_complete(ep
, req
, 0);
1566 next_fifo_transaction(ep
, req
);
1568 submit_request(ep
, req
);
1570 if (req
->last_transaction
) {
1571 list_del_init(&req
->queue
);
1572 submit_next_request(ep
);
1573 request_complete(ep
, req
, 0);
1577 epstatus
= usba_ep_readl(ep
, STA
);
1578 epctrl
= usba_ep_readl(ep
, CTL
);
1580 if ((epstatus
& epctrl
) & USBA_RX_BK_RDY
) {
1581 DBG(DBG_BUS
, "%s: RX data ready\n", ep
->ep
.name
);
1586 static void usba_dma_irq(struct usba_udc
*udc
, struct usba_ep
*ep
)
1588 struct usba_request
*req
;
1589 u32 status
, control
, pending
;
1591 status
= usba_dma_readl(ep
, STATUS
);
1592 control
= usba_dma_readl(ep
, CONTROL
);
1593 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1594 ep
->last_dma_status
= status
;
1596 pending
= status
& control
;
1597 DBG(DBG_INT
| DBG_DMA
, "dma irq, s/%#08x, c/%#08x\n", status
, control
);
1599 if (status
& USBA_DMA_CH_EN
) {
1600 dev_err(&udc
->pdev
->dev
,
1601 "DMA_CH_EN is set after transfer is finished!\n");
1602 dev_err(&udc
->pdev
->dev
,
1603 "status=%#08x, pending=%#08x, control=%#08x\n",
1604 status
, pending
, control
);
1607 * try to pretend nothing happened. We might have to
1608 * do something here...
1612 if (list_empty(&ep
->queue
))
1613 /* Might happen if a reset comes along at the right moment */
1616 if (pending
& (USBA_DMA_END_TR_ST
| USBA_DMA_END_BUF_ST
)) {
1617 req
= list_entry(ep
->queue
.next
, struct usba_request
, queue
);
1618 usba_update_req(ep
, req
, status
);
1620 list_del_init(&req
->queue
);
1621 submit_next_request(ep
);
1622 request_complete(ep
, req
, 0);
1626 static irqreturn_t
usba_udc_irq(int irq
, void *devid
)
1628 struct usba_udc
*udc
= devid
;
1629 u32 status
, int_enb
;
1633 spin_lock(&udc
->lock
);
1635 int_enb
= usba_int_enb_get(udc
);
1636 status
= usba_readl(udc
, INT_STA
) & (int_enb
| USBA_HIGH_SPEED
);
1637 DBG(DBG_INT
, "irq, status=%#08x\n", status
);
1639 if (status
& USBA_DET_SUSPEND
) {
1640 toggle_bias(udc
, 0);
1641 usba_writel(udc
, INT_CLR
, USBA_DET_SUSPEND
);
1642 usba_int_enb_set(udc
, int_enb
| USBA_WAKE_UP
);
1643 udc
->bias_pulse_needed
= true;
1644 DBG(DBG_BUS
, "Suspend detected\n");
1645 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1646 && udc
->driver
&& udc
->driver
->suspend
) {
1647 spin_unlock(&udc
->lock
);
1648 udc
->driver
->suspend(&udc
->gadget
);
1649 spin_lock(&udc
->lock
);
1653 if (status
& USBA_WAKE_UP
) {
1654 toggle_bias(udc
, 1);
1655 usba_writel(udc
, INT_CLR
, USBA_WAKE_UP
);
1656 usba_int_enb_set(udc
, int_enb
& ~USBA_WAKE_UP
);
1657 DBG(DBG_BUS
, "Wake Up CPU detected\n");
1660 if (status
& USBA_END_OF_RESUME
) {
1661 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESUME
);
1662 generate_bias_pulse(udc
);
1663 DBG(DBG_BUS
, "Resume detected\n");
1664 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
1665 && udc
->driver
&& udc
->driver
->resume
) {
1666 spin_unlock(&udc
->lock
);
1667 udc
->driver
->resume(&udc
->gadget
);
1668 spin_lock(&udc
->lock
);
1672 dma_status
= USBA_BFEXT(DMA_INT
, status
);
1676 for (i
= 1; i
<= USBA_NR_DMAS
; i
++)
1677 if (dma_status
& (1 << i
))
1678 usba_dma_irq(udc
, &udc
->usba_ep
[i
]);
1681 ep_status
= USBA_BFEXT(EPT_INT
, status
);
1685 for (i
= 0; i
< udc
->num_ep
; i
++)
1686 if (ep_status
& (1 << i
)) {
1687 if (ep_is_control(&udc
->usba_ep
[i
]))
1688 usba_control_irq(udc
, &udc
->usba_ep
[i
]);
1690 usba_ep_irq(udc
, &udc
->usba_ep
[i
]);
1694 if (status
& USBA_END_OF_RESET
) {
1695 struct usba_ep
*ep0
;
1697 usba_writel(udc
, INT_CLR
, USBA_END_OF_RESET
);
1698 generate_bias_pulse(udc
);
1699 reset_all_endpoints(udc
);
1701 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
&& udc
->driver
) {
1702 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1703 spin_unlock(&udc
->lock
);
1704 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1705 spin_lock(&udc
->lock
);
1708 if (status
& USBA_HIGH_SPEED
)
1709 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1711 udc
->gadget
.speed
= USB_SPEED_FULL
;
1712 DBG(DBG_BUS
, "%s bus reset detected\n",
1713 usb_speed_string(udc
->gadget
.speed
));
1715 ep0
= &udc
->usba_ep
[0];
1716 ep0
->ep
.desc
= &usba_ep0_desc
;
1717 ep0
->state
= WAIT_FOR_SETUP
;
1718 usba_ep_writel(ep0
, CFG
,
1719 (USBA_BF(EPT_SIZE
, EP0_EPT_SIZE
)
1720 | USBA_BF(EPT_TYPE
, USBA_EPT_TYPE_CONTROL
)
1721 | USBA_BF(BK_NUMBER
, USBA_BK_NUMBER_ONE
)));
1722 usba_ep_writel(ep0
, CTL_ENB
,
1723 USBA_EPT_ENABLE
| USBA_RX_SETUP
);
1724 usba_int_enb_set(udc
, int_enb
| USBA_BF(EPT_INT
, 1) |
1725 USBA_DET_SUSPEND
| USBA_END_OF_RESUME
);
1728 * Unclear why we hit this irregularly, e.g. in usbtest,
1729 * but it's clearly harmless...
1731 if (!(usba_ep_readl(ep0
, CFG
) & USBA_EPT_MAPPED
))
1732 dev_dbg(&udc
->pdev
->dev
,
1733 "ODD: EP0 configuration is invalid!\n");
1736 spin_unlock(&udc
->lock
);
1741 static int start_clock(struct usba_udc
*udc
)
1748 ret
= clk_prepare_enable(udc
->pclk
);
1751 ret
= clk_prepare_enable(udc
->hclk
);
1753 clk_disable_unprepare(udc
->pclk
);
1757 udc
->clocked
= true;
1761 static void stop_clock(struct usba_udc
*udc
)
1766 clk_disable_unprepare(udc
->hclk
);
1767 clk_disable_unprepare(udc
->pclk
);
1769 udc
->clocked
= false;
1772 static int usba_start(struct usba_udc
*udc
)
1774 unsigned long flags
;
1777 ret
= start_clock(udc
);
1781 spin_lock_irqsave(&udc
->lock
, flags
);
1782 toggle_bias(udc
, 1);
1783 usba_writel(udc
, CTRL
, USBA_ENABLE_MASK
);
1784 usba_int_enb_set(udc
, USBA_END_OF_RESET
);
1785 spin_unlock_irqrestore(&udc
->lock
, flags
);
1790 static void usba_stop(struct usba_udc
*udc
)
1792 unsigned long flags
;
1794 spin_lock_irqsave(&udc
->lock
, flags
);
1795 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1796 reset_all_endpoints(udc
);
1798 /* This will also disable the DP pullup */
1799 toggle_bias(udc
, 0);
1800 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
1801 spin_unlock_irqrestore(&udc
->lock
, flags
);
1806 static irqreturn_t
usba_vbus_irq_thread(int irq
, void *devid
)
1808 struct usba_udc
*udc
= devid
;
1814 mutex_lock(&udc
->vbus_mutex
);
1816 vbus
= vbus_is_present(udc
);
1817 if (vbus
!= udc
->vbus_prev
) {
1823 if (udc
->driver
->disconnect
)
1824 udc
->driver
->disconnect(&udc
->gadget
);
1826 udc
->vbus_prev
= vbus
;
1829 mutex_unlock(&udc
->vbus_mutex
);
1833 static int atmel_usba_start(struct usb_gadget
*gadget
,
1834 struct usb_gadget_driver
*driver
)
1837 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1838 unsigned long flags
;
1840 spin_lock_irqsave(&udc
->lock
, flags
);
1841 udc
->devstatus
= 1 << USB_DEVICE_SELF_POWERED
;
1842 udc
->driver
= driver
;
1843 spin_unlock_irqrestore(&udc
->lock
, flags
);
1845 mutex_lock(&udc
->vbus_mutex
);
1847 if (gpio_is_valid(udc
->vbus_pin
))
1848 enable_irq(gpio_to_irq(udc
->vbus_pin
));
1850 /* If Vbus is present, enable the controller and wait for reset */
1851 udc
->vbus_prev
= vbus_is_present(udc
);
1852 if (udc
->vbus_prev
) {
1853 ret
= usba_start(udc
);
1858 mutex_unlock(&udc
->vbus_mutex
);
1862 if (gpio_is_valid(udc
->vbus_pin
))
1863 disable_irq(gpio_to_irq(udc
->vbus_pin
));
1865 mutex_unlock(&udc
->vbus_mutex
);
1867 spin_lock_irqsave(&udc
->lock
, flags
);
1868 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1870 spin_unlock_irqrestore(&udc
->lock
, flags
);
1874 static int atmel_usba_stop(struct usb_gadget
*gadget
)
1876 struct usba_udc
*udc
= container_of(gadget
, struct usba_udc
, gadget
);
1878 if (gpio_is_valid(udc
->vbus_pin
))
1879 disable_irq(gpio_to_irq(udc
->vbus_pin
));
1889 static void at91sam9rl_toggle_bias(struct usba_udc
*udc
, int is_on
)
1891 unsigned int uckr
= at91_pmc_read(AT91_CKGR_UCKR
);
1894 at91_pmc_write(AT91_CKGR_UCKR
, uckr
| AT91_PMC_BIASEN
);
1896 at91_pmc_write(AT91_CKGR_UCKR
, uckr
& ~(AT91_PMC_BIASEN
));
1899 static void at91sam9g45_pulse_bias(struct usba_udc
*udc
)
1901 unsigned int uckr
= at91_pmc_read(AT91_CKGR_UCKR
);
1903 at91_pmc_write(AT91_CKGR_UCKR
, uckr
& ~(AT91_PMC_BIASEN
));
1904 at91_pmc_write(AT91_CKGR_UCKR
, uckr
| AT91_PMC_BIASEN
);
1907 static const struct usba_udc_errata at91sam9rl_errata
= {
1908 .toggle_bias
= at91sam9rl_toggle_bias
,
1911 static const struct usba_udc_errata at91sam9g45_errata
= {
1912 .pulse_bias
= at91sam9g45_pulse_bias
,
1915 static const struct of_device_id atmel_udc_dt_ids
[] = {
1916 { .compatible
= "atmel,at91sam9rl-udc", .data
= &at91sam9rl_errata
},
1917 { .compatible
= "atmel,at91sam9g45-udc", .data
= &at91sam9g45_errata
},
1918 { .compatible
= "atmel,sama5d3-udc" },
1922 MODULE_DEVICE_TABLE(of
, atmel_udc_dt_ids
);
1924 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
1925 struct usba_udc
*udc
)
1929 enum of_gpio_flags flags
;
1930 struct device_node
*np
= pdev
->dev
.of_node
;
1931 const struct of_device_id
*match
;
1932 struct device_node
*pp
;
1934 struct usba_ep
*eps
, *ep
;
1936 match
= of_match_node(atmel_udc_dt_ids
, np
);
1938 return ERR_PTR(-EINVAL
);
1940 udc
->errata
= match
->data
;
1944 udc
->vbus_pin
= of_get_named_gpio_flags(np
, "atmel,vbus-gpio", 0,
1946 udc
->vbus_pin_inverted
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
1949 while ((pp
= of_get_next_child(np
, pp
)))
1952 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * udc
->num_ep
,
1955 return ERR_PTR(-ENOMEM
);
1957 udc
->gadget
.ep0
= &eps
[0].ep
;
1959 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
1963 while ((pp
= of_get_next_child(np
, pp
))) {
1966 ret
= of_property_read_u32(pp
, "reg", &val
);
1968 dev_err(&pdev
->dev
, "of_probe: reg error(%d)\n", ret
);
1973 ret
= of_property_read_u32(pp
, "atmel,fifo-size", &val
);
1975 dev_err(&pdev
->dev
, "of_probe: fifo-size error(%d)\n", ret
);
1978 ep
->fifo_size
= val
;
1980 ret
= of_property_read_u32(pp
, "atmel,nb-banks", &val
);
1982 dev_err(&pdev
->dev
, "of_probe: nb-banks error(%d)\n", ret
);
1987 ep
->can_dma
= of_property_read_bool(pp
, "atmel,can-dma");
1988 ep
->can_isoc
= of_property_read_bool(pp
, "atmel,can-isoc");
1990 ret
= of_property_read_string(pp
, "name", &name
);
1992 dev_err(&pdev
->dev
, "of_probe: name error(%d)\n", ret
);
1997 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
1998 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
1999 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2000 ep
->ep
.ops
= &usba_ep_ops
;
2001 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2003 INIT_LIST_HEAD(&ep
->queue
);
2005 if (ep
->index
== 0) {
2006 ep
->ep
.caps
.type_control
= true;
2008 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2009 ep
->ep
.caps
.type_bulk
= true;
2010 ep
->ep
.caps
.type_int
= true;
2013 ep
->ep
.caps
.dir_in
= true;
2014 ep
->ep
.caps
.dir_out
= true;
2017 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2023 dev_err(&pdev
->dev
, "of_probe: no endpoint specified\n");
2030 return ERR_PTR(ret
);
2033 static struct usba_ep
* atmel_udc_of_init(struct platform_device
*pdev
,
2034 struct usba_udc
*udc
)
2036 return ERR_PTR(-ENOSYS
);
2040 static struct usba_ep
* usba_udc_pdata(struct platform_device
*pdev
,
2041 struct usba_udc
*udc
)
2043 struct usba_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2044 struct usba_ep
*eps
;
2048 return ERR_PTR(-ENXIO
);
2050 eps
= devm_kzalloc(&pdev
->dev
, sizeof(struct usba_ep
) * pdata
->num_ep
,
2053 return ERR_PTR(-ENOMEM
);
2055 udc
->gadget
.ep0
= &eps
[0].ep
;
2057 udc
->vbus_pin
= pdata
->vbus_pin
;
2058 udc
->vbus_pin_inverted
= pdata
->vbus_pin_inverted
;
2059 udc
->num_ep
= pdata
->num_ep
;
2061 INIT_LIST_HEAD(&eps
[0].ep
.ep_list
);
2063 for (i
= 0; i
< pdata
->num_ep
; i
++) {
2064 struct usba_ep
*ep
= &eps
[i
];
2066 ep
->ep_regs
= udc
->regs
+ USBA_EPT_BASE(i
);
2067 ep
->dma_regs
= udc
->regs
+ USBA_DMA_BASE(i
);
2068 ep
->fifo
= udc
->fifo
+ USBA_FIFO_BASE(i
);
2069 ep
->ep
.ops
= &usba_ep_ops
;
2070 ep
->ep
.name
= pdata
->ep
[i
].name
;
2071 ep
->fifo_size
= pdata
->ep
[i
].fifo_size
;
2072 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->fifo_size
);
2074 INIT_LIST_HEAD(&ep
->queue
);
2075 ep
->nr_banks
= pdata
->ep
[i
].nr_banks
;
2076 ep
->index
= pdata
->ep
[i
].index
;
2077 ep
->can_dma
= pdata
->ep
[i
].can_dma
;
2078 ep
->can_isoc
= pdata
->ep
[i
].can_isoc
;
2081 ep
->ep
.caps
.type_control
= true;
2083 ep
->ep
.caps
.type_iso
= ep
->can_isoc
;
2084 ep
->ep
.caps
.type_bulk
= true;
2085 ep
->ep
.caps
.type_int
= true;
2088 ep
->ep
.caps
.dir_in
= true;
2089 ep
->ep
.caps
.dir_out
= true;
2092 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2098 static int usba_udc_probe(struct platform_device
*pdev
)
2100 struct resource
*regs
, *fifo
;
2101 struct clk
*pclk
, *hclk
;
2102 struct usba_udc
*udc
;
2105 udc
= devm_kzalloc(&pdev
->dev
, sizeof(*udc
), GFP_KERNEL
);
2109 udc
->gadget
= usba_gadget_template
;
2110 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2112 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, CTRL_IOMEM_ID
);
2113 fifo
= platform_get_resource(pdev
, IORESOURCE_MEM
, FIFO_IOMEM_ID
);
2117 irq
= platform_get_irq(pdev
, 0);
2121 pclk
= devm_clk_get(&pdev
->dev
, "pclk");
2123 return PTR_ERR(pclk
);
2124 hclk
= devm_clk_get(&pdev
->dev
, "hclk");
2126 return PTR_ERR(hclk
);
2128 spin_lock_init(&udc
->lock
);
2129 mutex_init(&udc
->vbus_mutex
);
2133 udc
->vbus_pin
= -ENODEV
;
2136 udc
->regs
= devm_ioremap(&pdev
->dev
, regs
->start
, resource_size(regs
));
2138 dev_err(&pdev
->dev
, "Unable to map I/O memory, aborting.\n");
2141 dev_info(&pdev
->dev
, "MMIO registers at 0x%08lx mapped at %p\n",
2142 (unsigned long)regs
->start
, udc
->regs
);
2143 udc
->fifo
= devm_ioremap(&pdev
->dev
, fifo
->start
, resource_size(fifo
));
2145 dev_err(&pdev
->dev
, "Unable to map FIFO, aborting.\n");
2148 dev_info(&pdev
->dev
, "FIFO at 0x%08lx mapped at %p\n",
2149 (unsigned long)fifo
->start
, udc
->fifo
);
2151 platform_set_drvdata(pdev
, udc
);
2153 /* Make sure we start from a clean slate */
2154 ret
= clk_prepare_enable(pclk
);
2156 dev_err(&pdev
->dev
, "Unable to enable pclk, aborting.\n");
2160 usba_writel(udc
, CTRL
, USBA_DISABLE_MASK
);
2161 clk_disable_unprepare(pclk
);
2163 if (pdev
->dev
.of_node
)
2164 udc
->usba_ep
= atmel_udc_of_init(pdev
, udc
);
2166 udc
->usba_ep
= usba_udc_pdata(pdev
, udc
);
2168 toggle_bias(udc
, 0);
2170 if (IS_ERR(udc
->usba_ep
))
2171 return PTR_ERR(udc
->usba_ep
);
2173 ret
= devm_request_irq(&pdev
->dev
, irq
, usba_udc_irq
, 0,
2174 "atmel_usba_udc", udc
);
2176 dev_err(&pdev
->dev
, "Cannot request irq %d (error %d)\n",
2182 if (gpio_is_valid(udc
->vbus_pin
)) {
2183 if (!devm_gpio_request(&pdev
->dev
, udc
->vbus_pin
, "atmel_usba_udc")) {
2184 irq_set_status_flags(gpio_to_irq(udc
->vbus_pin
),
2186 ret
= devm_request_threaded_irq(&pdev
->dev
,
2187 gpio_to_irq(udc
->vbus_pin
), NULL
,
2188 usba_vbus_irq_thread
, IRQF_ONESHOT
,
2189 "atmel_usba_udc", udc
);
2191 udc
->vbus_pin
= -ENODEV
;
2192 dev_warn(&udc
->pdev
->dev
,
2193 "failed to request vbus irq; "
2194 "assuming always on\n");
2197 /* gpio_request fail so use -EINVAL for gpio_is_valid */
2198 udc
->vbus_pin
= -EINVAL
;
2202 ret
= usb_add_gadget_udc(&pdev
->dev
, &udc
->gadget
);
2205 device_init_wakeup(&pdev
->dev
, 1);
2207 usba_init_debugfs(udc
);
2208 for (i
= 1; i
< udc
->num_ep
; i
++)
2209 usba_ep_init_debugfs(udc
, &udc
->usba_ep
[i
]);
2214 static int usba_udc_remove(struct platform_device
*pdev
)
2216 struct usba_udc
*udc
;
2219 udc
= platform_get_drvdata(pdev
);
2221 device_init_wakeup(&pdev
->dev
, 0);
2222 usb_del_gadget_udc(&udc
->gadget
);
2224 for (i
= 1; i
< udc
->num_ep
; i
++)
2225 usba_ep_cleanup_debugfs(&udc
->usba_ep
[i
]);
2226 usba_cleanup_debugfs(udc
);
2231 #ifdef CONFIG_PM_SLEEP
2232 static int usba_udc_suspend(struct device
*dev
)
2234 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2240 mutex_lock(&udc
->vbus_mutex
);
2242 if (!device_may_wakeup(dev
)) {
2248 * Device may wake up. We stay clocked if we failed
2249 * to request vbus irq, assuming always on.
2251 if (gpio_is_valid(udc
->vbus_pin
)) {
2253 enable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2257 mutex_unlock(&udc
->vbus_mutex
);
2261 static int usba_udc_resume(struct device
*dev
)
2263 struct usba_udc
*udc
= dev_get_drvdata(dev
);
2269 if (device_may_wakeup(dev
) && gpio_is_valid(udc
->vbus_pin
))
2270 disable_irq_wake(gpio_to_irq(udc
->vbus_pin
));
2272 /* If Vbus is present, enable the controller and wait for reset */
2273 mutex_lock(&udc
->vbus_mutex
);
2274 udc
->vbus_prev
= vbus_is_present(udc
);
2277 mutex_unlock(&udc
->vbus_mutex
);
2283 static SIMPLE_DEV_PM_OPS(usba_udc_pm_ops
, usba_udc_suspend
, usba_udc_resume
);
2285 static struct platform_driver udc_driver
= {
2286 .remove
= usba_udc_remove
,
2288 .name
= "atmel_usba_udc",
2289 .pm
= &usba_udc_pm_ops
,
2290 .of_match_table
= of_match_ptr(atmel_udc_dt_ids
),
2294 module_platform_driver_probe(udc_driver
, usba_udc_probe
);
2296 MODULE_DESCRIPTION("Atmel USBA UDC driver");
2297 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2298 MODULE_LICENSE("GPL");
2299 MODULE_ALIAS("platform:atmel_usba_udc");