本文由4M小水管编写,由于水平有限难免有错误,欢迎纠正
static inline u32 udc_read(u32 reg)
{
return readb(base_addr + reg);//从寄存器读出值
}
static inline void udc_write(u32 value, u32 reg)
{
writeb(value, base_addr + reg);//把数据写入寄存器
static inline void udc_writeb(void __iomem *base, u32 value, u32 reg)
{
writeb(value, base + reg);
}
static struct s3c2410_udc_mach_info *udc_info;
/*************************** DEBUG FUNCTION ***************************/
#define DEBUG_NORMAL 1
#define DEBUG_VERBOSE 2
#define USB_S3C2410_DEBUG_LEVEL 0
static uint32_t s3c2410_ticks = 0;
static int dprintk(int level, const char *fmt, ...)
{
return 0;
}
static int s3c2410_udc_debugfs_seq_show(struct seq_file *m, void *p)//读出各端点的状态然后打印出来
{
。。。。
return 0;
}
static int s3c2410_udc_debugfs_fops_open(struct inode *inode,
struct file *file)
{
return single_open(file, s3c2410_udc_debugfs_seq_show, NULL);//打开属性文件
static const struct file_operations s3c2410_udc_debugfs_fops = {
.open = s3c2410_udc_debugfs_fops_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
.owner = THIS_MODULE,
};
/* io macros */
static inline void s3c2410_udc_clear_ep0_opr(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, S3C2410_UDC_EP0_CSR_SOPKTRDY,
S3C2410_UDC_EP0_CSR_REG);//清除OUT_PKT_RDY
}
static inline void s3c2410_udc_clear_ep0_sst(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
writeb(0x00, base + S3C2410_UDC_EP0_CSR_REG);//把端点0的状态寄存器设为初始状态
}
static inline void s3c2410_udc_clear_ep0_se(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, S3C2410_UDC_EP0_CSR_SSE, S3C2410_UDC_EP0_CSR_REG);//清除SET_UP结束
}
static inline void s3c2410_udc_set_ep0_ipr(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, S3C2410_UDC_EP0_CSR_IPKRDY, S3C2410_UDC_EP0_CSR_REG);//清除IN_PKT_RDY,这个是数据写到FIFO而置位
}
static inline void s3c2410_udc_set_ep0_de(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, S3C2410_UDC_EP0_CSR_DE, S3C2410_UDC_EP0_CSR_REG);//加载最后的数据到FIFO而置位
}
inline void s3c2410_udc_set_ep0_ss(void __iomem *b)
{
udc_writeb(b, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(b, S3C2410_UDC_EP0_CSR_SENDSTL, S3C2410_UDC_EP0_CSR_REG);//清除SEND_STALL
}
static inline void s3c2410_udc_set_ep0_de_out(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base,(S3C2410_UDC_EP0_CSR_SOPKTRDY
| S3C2410_UDC_EP0_CSR_DE),
S3C2410_UDC_EP0_CSR_REG);//清除IN_PKT_RDY和DATA_END
}
static inline void s3c2410_udc_set_ep0_sse_out(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, (S3C2410_UDC_EP0_CSR_SOPKTRDY
| S3C2410_UDC_EP0_CSR_SSE),
S3C2410_UDC_EP0_CSR_REG);//清除SERVICED_OUT_PKT_RDY清除SET_UP结束
}
static inline void s3c2410_udc_set_ep0_de_in(void __iomem *base)
{
udc_writeb(base, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//端点0有效
udc_writeb(base, (S3C2410_UDC_EP0_CSR_IPKRDY
| S3C2410_UDC_EP0_CSR_DE),
S3C2410_UDC_EP0_CSR_REG);//清除IN_PKT_RDY清除SET_UP结束
}
static void s3c2410_udc_done(struct s3c2410_ep *ep,
struct s3c2410_request *req, int status)
{
unsigned halted = ep->halted;
list_del_init(&req->queue);//删除队列
if (likely (req->req.status == -EINPROGRESS))//EINPROGRESS表示操作就进行中
req->req.status = status;
else
status = req->req.status;//-EPIPE毁掉管道
ep->halted = 1;
req->req.complete(&ep->ep, &req->req);//请求完成
ep->halted = halted;
}
static void s3c2410_udc_nuke(struct s3c2410_udc *udc,
struct s3c2410_ep *ep, int status)
{
/* Sanity check */
if (&ep->queue == NULL)//如果队列为空返回
return;
while (!list_empty (&ep->queue)) {
struct s3c2410_request *req;
req = list_entry (ep->queue.next, struct s3c2410_request,
queue);//遍历队列链表的请求
s3c2410_udc_done(ep, req, status);//根据status执行请求
}
}
static inline void s3c2410_udc_clear_ep_state(struct s3c2410_udc *dev)
{
unsigned i;
for (i = 1; i < S3C2410_ENDPOINTS; i++)
s3c2410_udc_nuke(dev, &dev->ep[i], -ECONNABORTED);//根据status执行请求
}
}
static inline int s3c2410_udc_fifo_count_out(void)
{
int tmp;
tmp = udc_read(S3C2410_UDC_OUT_FIFO_CNT2_REG) << 8;//读出包的字节数
tmp |= udc_read(S3C2410_UDC_OUT_FIFO_CNT1_REG);//读出包的字节数
return tmp;
}
/*
* s3c2410_udc_write_packet
*/
static inline int s3c2410_udc_write_packet(int fifo,
struct s3c2410_request *req,
unsigned max)
{
unsigned len = min(req->req.length - req->req.actual, max);
u8 *buf = req->req.buf + req->req.actual;
prefetch(buf);
req->req.actual += len;
udelay(5);
writesb(base_addr + fifo, buf, len);//把数据写到FIFO
return len;
}
static int s3c2410_udc_write_fifo(struct s3c2410_ep *ep,
struct s3c2410_request *req)
{
unsigned count;
int is_last;
u32 idx;
int fifo_reg;
u32 ep_csr;
idx = ep->bEndpointAddress & 0x7F;
switch (idx) {
default:
idx = 0;
case 0:
fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
break;
case 1:
fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
break;
case 2:
fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
break;
case 3:
fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
break;
case 4:
fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
break;
}//判断是哪端点的FIFO
count = s3c2410_udc_write_packet(fifo_reg, req, ep->ep.maxpacket);//把数据写到FIFO
return len;
/* last packet is often short (sometimes a zlp) */
if (count != ep->ep.maxpacket)
is_last = 1;
else if (req->req.length != req->req.actual || req->req.zero)
is_last = 0;
else
is_last = 2;
if (is_last) {
/* The order is important. It prevents sending 2 packets
* at the same time */
if (idx == 0) {
/* Reset signal => no need to say 'data sent' */
if (! (udc_read(S3C2410_UDC_USB_INT_REG)
& S3C2410_UDC_USBINT_RESET))
s3c2410_udc_set_ep0_de_in(base_addr);//清除IN_PKT_RDY清除SET_UP结束
ep->dev->ep0state=EP0_IDLE;//端点0空闲
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);//读出状态
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
S3C2410_UDC_IN_CSR1_REG);//清除OUT_PKT_RDY
}
s3c2410_udc_done(ep, req, 0);//根据status执行请求
is_last = 1;
} else {
if (idx == 0) {
/* Reset signal => no need to say 'data sent' */
if (! (udc_read(S3C2410_UDC_USB_INT_REG)
& S3C2410_UDC_USBINT_RESET))
s3c2410_udc_set_ep0_ipr(base_addr);//清除IN_PKT_RDY,这个是数据写到FIFO而置位
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);//读出状态
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(ep_csr | S3C2410_UDC_ICSR1_PKTRDY,
S3C2410_UDC_IN_CSR1_REG);//清除OUT_PKT_RDY
}
}
return is_last;
}
static inline int s3c2410_udc_read_packet(int fifo, u8 *buf,
struct s3c2410_request *req, unsigned avail)
{
unsigned len;
len = min(req->req.length - req->req.actual, avail);
req->req.actual += len;
readsb(fifo + base_addr, buf, len);//读出FIFIO数据
return len;
}
static int s3c2410_udc_read_fifo(struct s3c2410_ep *ep,
struct s3c2410_request *req)
{
u8 *buf;
u32 ep_csr;
unsigned bufferspace;
int is_last=1;
unsigned avail;
int fifo_count = 0;
u32 idx;
int fifo_reg;
idx = ep->bEndpointAddress & 0x7F;
switch (idx) {
default:
idx = 0;
case 0:
fifo_reg = S3C2410_UDC_EP0_FIFO_REG;
break;
case 1:
fifo_reg = S3C2410_UDC_EP1_FIFO_REG;
break;
case 2:
fifo_reg = S3C2410_UDC_EP2_FIFO_REG;
break;
case 3:
fifo_reg = S3C2410_UDC_EP3_FIFO_REG;
break;
case 4:
fifo_reg = S3C2410_UDC_EP4_FIFO_REG;
break;
}//判断是哪端点的FIFO
if (!req->req.length)
return 1;
buf = req->req.buf + req->req.actual;
bufferspace = req->req.length - req->req.actual;
if (!bufferspace) {
return -1;
}
udc_write(idx, S3C2410_UDC_INDEX_REG);//某个端点有效
fifo_count = s3c2410_udc_fifo_count_out();//读出FIFIO数据的字节数
if (fifo_count > ep->ep.maxpacket)
avail = ep->ep.maxpacket;
else
avail = fifo_count;
fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);//读出FIFIO数据
if (idx != 0 && fifo_count < ep->ep.maxpacket) {
is_last = 1;
if (fifo_count != avail)
req->req.status = -EOVERFLOW;
} else {
is_last = (req->req.length <= req->req.actual) ? 1 : 0;
}
udc_write(idx, S3C2410_UDC_INDEX_REG);//某个端点有效
fifo_count = s3c2410_udc_fifo_count_out();//读出包的字节数
if (idx == 0)
dprintk(DEBUG_VERBOSE, "%s fifo count : %d [last %d]\n",
__func__, fifo_count,is_last);
if (is_last) {
if (idx == 0) {
s3c2410_udc_set_ep0_de_out(base_addr);
ep->dev->ep0state = EP0_IDLE;
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);
ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
udc_write(idx, S3C2410_UDC_INDEX_REG);
udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
S3C2410_UDC_OUT_CSR1_REG);
}
s3c2410_udc_done(ep, req, 0);
} else {
if (idx == 0) {
s3c2410_udc_clear_ep0_opr(base_addr);
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);
ep_csr = udc_read(S3C2410_UDC_OUT_CSR1_REG);
udc_write(idx, S3C2410_UDC_INDEX_REG);
udc_write(ep_csr & ~S3C2410_UDC_OCSR1_PKTRDY,
S3C2410_UDC_OUT_CSR1_REG);
}
}
return is_last;
}
static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq)
{
unsigned char *outbuf = (unsigned char*)crq;
int bytes_read = 0;
udc_write(0, S3C2410_UDC_INDEX_REG);//端点0有效
bytes_read = s3c2410_udc_fifo_count_out();//读出包的字节数
dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read);
if (bytes_read > sizeof(struct usb_ctrlrequest))
bytes_read = sizeof(struct usb_ctrlrequest);
readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read);//从FIFO读出值
dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__,
bytes_read, crq->bRequest, crq->bRequestType,
crq->wValue, crq->wIndex, crq->wLength);
return bytes_read;
}
static int s3c2410_udc_get_status(struct s3c2410_udc *dev,
struct usb_ctrlrequest *crq)
{
u16 status = 0;
u8 ep_num = crq->wIndex & 0x7F;
u8 is_in = crq->wIndex & USB_DIR_IN;
switch (crq->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_INTERFACE:
break;
case USB_RECIP_DEVICE:
status = dev->devstatus;
break;
case USB_RECIP_ENDPOINT:
if (ep_num > 4 || crq->wLength > 2)
return 1;
if (ep_num == 0) {
udc_write(0, S3C2410_UDC_INDEX_REG);
status = udc_read(S3C2410_UDC_IN_CSR1_REG);
status = status & S3C2410_UDC_EP0_CSR_SENDSTL;
} else {
udc_write(ep_num, S3C2410_UDC_INDEX_REG);
if (is_in) {
status = udc_read(S3C2410_UDC_IN_CSR1_REG);
status = status & S3C2410_UDC_ICSR1_SENDSTL;
} else {
status = udc_read(S3C2410_UDC_OUT_CSR1_REG);
status = status & S3C2410_UDC_OCSR1_SENDSTL;
}
}
status = status ? 1 : 0;
break;
default:
return 1;
}
udelay(5);
udc_write(status & 0xFF, S3C2410_UDC_EP0_FIFO_REG);
udc_write(status >> 8, S3C2410_UDC_EP0_FIFO_REG);
s3c2410_udc_set_ep0_de_in(base_addr);
return 0;
}
static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value);
static void s3c2410_udc_handle_ep0_idle(struct s3c2410_udc *dev,
struct s3c2410_ep *ep,
struct usb_ctrlrequest *crq,
u32 ep0csr)
{
int len, ret, tmp;
/* start control request? */
if (!(ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY))//如果没有效的令牌包写到FIFO中
return;
s3c2410_udc_nuke(dev, ep, -EPROTO);//协议错误
len = s3c2410_udc_read_fifo_crq(crq);//从FIFO读出数据
if (len != sizeof(*crq)) {
dprintk(DEBUG_NORMAL, "setup begin: fifo READ ERROR"
" wanted %d bytes got %d. Stalling out...\n",
sizeof(*crq), len);
s3c2410_udc_set_ep0_ss(base_addr);//清除SEND_STALL
return;
}
dprintk(DEBUG_NORMAL, "bRequest = %d bRequestType %d wLength = %d\n",
crq->bRequest, crq->bRequestType, crq->wLength);
/* cope with automagic for some standard requests. */
dev->req_std = (crq->bRequestType & USB_TYPE_MASK)
== USB_TYPE_STANDARD;
dev->req_config = 0;
dev->req_pending = 1;
switch (crq->bRequest) {
case USB_REQ_SET_CONFIGURATION:
if (crq->bRequestType == USB_RECIP_DEVICE) {
dev->req_config = 1;
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
}
break;
case USB_REQ_SET_INTERFACE:
dprintk(DEBUG_NORMAL, "USB_REQ_SET_INTERFACE ... \n");
if (crq->bRequestType == USB_RECIP_INTERFACE) {
dev->req_config = 1;
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
}
break;
case USB_REQ_SET_ADDRESS:
dprintk(DEBUG_NORMAL, "USB_REQ_SET_ADDRESS ... \n");
if (crq->bRequestType == USB_RECIP_DEVICE) {
tmp = crq->wValue & 0x7F;
dev->address = tmp;
udc_write((tmp | S3C2410_UDC_FUNCADDR_UPDATE),
S3C2410_UDC_FUNC_ADDR_REG);
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
}
return;
}
break;
case USB_REQ_GET_STATUS:
dprintk(DEBUG_NORMAL, "USB_REQ_GET_STATUS ... \n");
s3c2410_udc_clear_ep0_opr(base_addr);//清除OUT_PKT_RDY
if (dev->req_std) {
if (!s3c2410_udc_get_status(dev, crq)) {
return;
}
}
break;
case USB_REQ_CLEAR_FEATURE:
s3c2410_udc_clear_ep0_opr(base_addr);//清除OUT_PKT_RDY
if (crq->bRequestType != USB_RECIP_ENDPOINT)
break;
if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
break;
s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 0);
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
return;
case USB_REQ_SET_FEATURE:
s3c2410_udc_clear_ep0_opr(base_addr);
if (crq->bRequestType != USB_RECIP_ENDPOINT)
break;
if (crq->wValue != USB_ENDPOINT_HALT || crq->wLength != 0)
break;
s3c2410_udc_set_halt(&dev->ep[crq->wIndex & 0x7f].ep, 1);
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
}
return;
default:
s3c2410_udc_clear_ep0_opr(base_addr);//清除OUT_PKT_RDY
break;
}
if (crq->bRequestType & USB_DIR_IN)
dev->ep0state = EP0_IN_DATA_PHASE;
else
dev->ep0state = EP0_OUT_DATA_PHASE;
ret = dev->driver->setup(&dev->gadget, crq);
if (ret < 0) {
if (dev->req_config) {
dprintk(DEBUG_NORMAL, "config change %02x fail %d?\n",
crq->bRequest, ret);
return;
}
if (ret == -EOPNOTSUPP)
dprintk(DEBUG_NORMAL, "Operation not supported\n");
else
dprintk(DEBUG_NORMAL,
"dev->driver->setup failed. (%d)\n", ret);
udelay(5);
s3c2410_udc_set_ep0_ss(base_addr);//清除SEND_STALL
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
dev->ep0state = EP0_IDLE;
/* deferred i/o == no response yet */
} else if (dev->req_pending) {
dprintk(DEBUG_VERBOSE, "dev->req_pending... what now?\n");
dev->req_pending=0;
}
dprintk(DEBUG_VERBOSE, "ep0state %s\n", ep0states[dev->ep0state]);
}
static void s3c2410_udc_handle_ep0(struct s3c2410_udc *dev)
{
u32 ep0csr;
struct s3c2410_ep *ep = &dev->ep[0];//从设备取得端点0
struct s3c2410_request *req;
struct usb_ctrlrequest crq;
if (list_empty(&ep->queue))
req = NULL;
else
req = list_entry(ep->queue.next, struct s3c2410_request, queue);
udc_write(0, S3C2410_UDC_INDEX_REG);//开始用端点0
ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);//把状态读出来
dprintk(DEBUG_NORMAL, "ep0csr %x ep0state %s\n",
ep0csr, ep0states[dev->ep0state]);
/* clear stall status */
if (ep0csr & S3C2410_UDC_EP0_CSR_SENTSTL) {//控制事件由于违反协议而停止usb
s3c2410_udc_nuke(dev, ep, -EPIPE);//标志-EPIPE毁掉管道
s3c2410_udc_clear_ep0_sst(base_addr);//把端点0状态设为初始状态,都为0.
dev->ep0state = EP0_IDLE;//端点0空闲
return;
}
/* clear setup end */
if (ep0csr & S3C2410_UDC_EP0_CSR_SE) {//数据传输结束
s3c2410_udc_nuke(dev, ep, 0);//调用3c2410_udc_done(ep, req, status);根据status执行请求
}
s3c2410_udc_clear_ep0_se(base_addr);//清除SET_UP结束
dev->ep0state = EP0_IDLE;//端点0空闲
}
switch (dev->ep0state) {
case EP0_IDLE://端点0空闲
s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
break;
case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
dprintk(DEBUG_NORMAL, "EP0_IN_DATA_PHASE ... what now?\n");
if (!(ep0csr & S3C2410_UDC_EP0_CSR_IPKRDY) && req)//判断FIFO 是否有数据和是否有请求{
s3c2410_udc_write_fifo(ep, req);//把数据写到FIFO
}
break;
case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
dprintk(DEBUG_NORMAL, "EP0_OUT_DATA_PHASE ... what now?\n");
if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req ) {
s3c2410_udc_read_fifo(ep,req);
}
break;
case EP0_END_XFER:
dev->ep0state = EP0_IDLE;
break;
case EP0_STALL:
dev->ep0state = EP0_IDLE;
break;
}
}
static void s3c2410_udc_handle_ep(struct s3c2410_ep *ep)
{
struct s3c2410_request *req;
int is_in = ep->bEndpointAddress & USB_DIR_IN;
u32 ep_csr1;
u32 idx;
if (likely (!list_empty(&ep->queue)))
req = list_entry(ep->queue.next,
struct s3c2410_request, queue);/* 遍历端点的队列 */
else
req = NULL;
idx = ep->bEndpointAddress & 0x7F;//端点索引值
if (is_in) {//是否输入端点
udc_write(idx, S3C2410_UDC_INDEX_REG);//把端点索引值写到寄存器
ep_csr1 = udc_read(S3C2410_UDC_IN_CSR1_REG);//读出该端点状态
dprintk(DEBUG_VERBOSE, "ep%01d write csr:%02x %d\n",
idx, ep_csr1, req ? 1 : 0);
if (ep_csr1 & S3C2410_UDC_ICSR1_SENTSTL) {//判断是否SEND_STALL
dprintk(DEBUG_VERBOSE, "st\n");
udc_write(idx, S3C2410_UDC_INDEX_REG);
udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
S3C2410_UDC_IN_CSR1_REG);//设置完成SEND_STALL
return;
}
if (!(ep_csr1 & S3C2410_UDC_ICSR1_PKTRDY) && req) {
s3c2410_udc_write_fifo(ep,req);//写数据到FIFO
}
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
ep_csr1 = udc_read(S3C2410_UDC_OUT_CSR1_REG);//读出状态
dprintk(DEBUG_VERBOSE, "ep%01d rd csr:%02x\n", idx, ep_csr1);
if (ep_csr1 & S3C2410_UDC_OCSR1_SENTSTL) {
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
S3C2410_UDC_OUT_CSR1_REG);//清除SENT_STALL
return;
}
if ((ep_csr1 & S3C2410_UDC_OCSR1_PKTRDY) && req) {
s3c2410_udc_read_fifo(ep,req);//读处数据
}
}
}
#include <mach/regs-irq.h>
static irqreturn_t s3c2410_udc_irq(int dummy, void *_dev)
{
struct s3c2410_udc *dev = _dev;
int usb_status;
int usbd_status;
int pwr_reg;
int ep0csr;
int i;
u32 idx;
unsigned long flags;
spin_lock_irqsave(&dev->lock, flags);
/* Driver connected ? */
if (!dev->driver) {
/* 清除中断 */
udc_write(udc_read(S3C2410_UDC_USB_INT_REG),
S3C2410_UDC_USB_INT_REG);
udc_write(udc_read(S3C2410_UDC_EP_INT_REG),
S3C2410_UDC_EP_INT_REG);
}
/* 保存索引值,代表哪个端点*/
idx = udc_read(S3C2410_UDC_INDEX_REG);
/* 从状态寄存器读出各状态*/
usb_status = udc_read(S3C2410_UDC_USB_INT_REG);//比如RESET,RESUME,SUSPEND
usbd_status = udc_read(S3C2410_UDC_EP_INT_REG);//0到4哪个端点的中断状态
pwr_reg = udc_read(S3C2410_UDC_PWR_REG);//电源控制
udc_writeb(base_addr, S3C2410_UDC_INDEX_EP0, S3C2410_UDC_INDEX_REG);//意思端点0有效
ep0csr = udc_read(S3C2410_UDC_IN_CSR1_REG);//读出端点0的状态
dprintk(DEBUG_NORMAL, "usbs=%02x, usbds=%02x, pwr=%02x ep0csr=%02x\n",
usb_status, usbd_status, pwr_reg, ep0csr);
if (usb_status & S3C2410_UDC_USBINT_RESET) {//是否收到复位
dprintk(DEBUG_NORMAL, "USB reset csr %x pwr %x\n",
ep0csr, pwr_reg);
dev->gadget.speed = USB_SPEED_UNKNOWN;//速度不知道
udc_write(0x00, S3C2410_UDC_INDEX_REG);//意思端点0有效
udc_write((dev->ep[0].ep.maxpacket & 0x7ff) >> 3,
S3C2410_UDC_MAXP_REG);//把0端点最大包大小写到寄存器
dev->address = 0;//地址为0
dev->ep0state = EP0_IDLE;
dev->gadget.speed = USB_SPEED_FULL;//全速
/* 清中断 */
udc_write(S3C2410_UDC_USBINT_RESET,
S3C2410_UDC_USB_INT_REG);
udc_write(idx, S3C2410_UDC_INDEX_REG);/* 恢复进中断时那个端点*/
spin_unlock_irqrestore(&dev->lock, flags);
return IRQ_HANDLED;
}
/*恢复*/
if (usb_status & S3C2410_UDC_USBINT_RESUME) {
dprintk(DEBUG_NORMAL, "USB resume\n");
/* 清中断 */
udc_write(S3C2410_UDC_USBINT_RESUME,
S3C2410_UDC_USB_INT_REG);
if (dev->gadget.speed != USB_SPEED_UNKNOWN
&& dev->driver
&& dev->driver->resume)//如果速度不是不知道的且驱动存在并且驱动启动,下面就恢复设备
dev->driver->resume(&dev->gadget);/*恢复启动设备*/
}
/* 设备挂起 */
if (usb_status & S3C2410_UDC_USBINT_SUSPEND) {
dprintk(DEBUG_NORMAL, "USB suspend\n");
/* 清中断 */
udc_write(S3C2410_UDC_USBINT_SUSPEND,
S3C2410_UDC_USB_INT_REG);
if (dev->gadget.speed != USB_SPEED_UNKNOWN
&& dev->driver
&& dev->driver->suspend)//如果速度不是不知道且驱动存在并且驱动挂起
dev->driver->suspend(&dev->gadget);/* 设备挂起 */
dev->ep0state = EP0_IDLE;//端点0空闲
}
if (usbd_status & S3C2410_UDC_INT_EP0) {//如果端点0是能了
dprintk(DEBUG_VERBOSE, "USB ep0 irq\n");
/* 通过设置 1来清除端点0的中断 */
udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_REG);
s3c2410_udc_handle_ep0(dev);
}
for (i = 1; i < S3C2410_ENDPOINTS; i++) {
u32 tmp = 1 << i;
if (usbd_status & tmp) /* 哪个端点数据传输产生中断 */{
dprintk(DEBUG_VERBOSE, "USB ep%d irq\n", i);
/* 清除该中断 1 */
udc_write(tmp, S3C2410_UDC_EP_INT_REG);
s3c2410_udc_handle_ep(&dev->ep[i]);
}
}
dprintk(DEBUG_VERBOSE, "irq: %d s3c2410_udc_done.\n", IRQ_USBD);
/* Restore old index */
udc_write(idx, S3C2410_UDC_INDEX_REG);
spin_unlock_irqrestore(&dev->lock, flags);
return IRQ_HANDLED;
}
static inline struct s3c2410_ep *to_s3c2410_ep(struct usb_ep *ep)
{
return container_of(ep, struct s3c2410_ep, ep);
}
static inline struct s3c2410_udc *to_s3c2410_udc(struct usb_gadget *gadget)
{
return container_of(gadget, struct s3c2410_udc, gadget);
}
static inline struct s3c2410_request *to_s3c2410_req(struct usb_request *req)
{
return container_of(req, struct s3c2410_request, req);
}
static int s3c2410_udc_ep_enable(struct usb_ep *_ep,
const struct usb_endpoint_descriptor *desc)
{
struct s3c2410_udc *dev;
struct s3c2410_ep *ep;
u32 max, tmp;
unsigned long flags;
u32 csr1,csr2;
u32 int_en_reg;
ep = to_s3c2410_ep(_ep);//得到端点
if (!_ep || !desc || ep->desc
|| _ep->name == ep0name
|| desc->bDescriptorType != USB_DT_ENDPOINT)
return -EINVAL;
dev = ep->dev;//得到端设备
if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
return -ESHUTDOWN;
max = le16_to_cpu(desc->wMaxPacketSize) & 0x1fff;
local_irq_save (flags);
_ep->maxpacket = max & 0x7ff;
ep->desc = desc;
ep->halted = 0;
ep->bEndpointAddress = desc->bEndpointAddress;
/* set max packet */
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(max >> 3, S3C2410_UDC_MAXP_REG);//设置最大数据包
/* set type, direction, address; reset fifo counters */
if (desc->bEndpointAddress & USB_DIR_IN) {//是否是输入端点
csr1 = S3C2410_UDC_ICSR1_FFLUSH|S3C2410_UDC_ICSR1_CLRDT;
csr2 = S3C2410_UDC_ICSR2_MODEIN|S3C2410_UDC_ICSR2_DMAIEN;
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);//清除SERVICED_OUT_PKT_RDY,DATA_END
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);//禁止中断,配置方向为输入
} else {
/* don't flush in fifo or it will cause endpoint interrupt */
csr1 = S3C2410_UDC_ICSR1_CLRDT;
csr2 = S3C2410_UDC_ICSR2_DMAIEN;
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr1, S3C2410_UDC_IN_CSR1_REG);//清除SERVICED_OUT_PKT_RDY
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr2, S3C2410_UDC_IN_CSR2_REG);//禁止中断
csr1 = S3C2410_UDC_OCSR1_FFLUSH | S3C2410_UDC_OCSR1_CLRDT;
csr2 = S3C2410_UDC_OCSR2_DMAIEN;
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr1, S3C2410_UDC_OUT_CSR1_REG);//复位数据切换到DATA0,刷新FIFO
udc_write(ep->num, S3C2410_UDC_INDEX_REG);//某端点有效
udc_write(csr2, S3C2410_UDC_OUT_CSR2_REG);//中断禁止
}
int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);
udc_write(int_en_reg | (1 << ep->num), S3C2410_UDC_EP_INT_EN_REG);//使能某个中断
/* print some debug message */
tmp = desc->bEndpointAddress;//获得端点地址
dprintk (DEBUG_NORMAL, "enable %s(%d) ep%x%s-blk max %02x\n",
_ep->name,ep->num, tmp,
desc->bEndpointAddress & USB_DIR_IN ? "in" : "out", max);
local_irq_restore (flags);
s3c2410_udc_set_halt(_ep, 0);
return 0;
}
static int s3c2410_udc_ep_disable(struct usb_ep *_ep)
{
struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
unsigned long flags;
u32 int_en_reg;
if (!_ep || !ep->desc) {
return -EINVAL;
}
local_irq_save(flags);
ep->desc = NULL;
ep->halted = 1;
s3c2410_udc_nuke (ep->dev, ep, -ESHUTDOWN);//根据status执行请求
/* disable irqs */
int_en_reg = udc_read(S3C2410_UDC_EP_INT_EN_REG);//读出中断是能寄存器的值
udc_write(int_en_reg & ~(1<<ep->num), S3C2410_UDC_EP_INT_EN_REG);//禁止中断
local_irq_restore(flags);
return 0;
}
static struct usb_request *
s3c2410_udc_alloc_request(struct usb_ep *_ep, gfp_t mem_flags)
{
struct s3c2410_request *req;
if (!_ep)
return NULL;
req = kzalloc (sizeof(struct s3c2410_request), mem_flags);//分配一个请求
if (!req)
return NULL;
INIT_LIST_HEAD (&req->queue);//初始化请求队列
return &req->req;
}
static void
s3c2410_udc_free_request(struct usb_ep *_ep, struct usb_request *_req)
{
struct s3c2410_ep *ep = to_s3c2410_ep(_ep);//获得端点
struct s3c2410_request *req = to_s3c2410_req(_req);//获得请求
if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
return;
WARN_ON (!list_empty (&req->queue));
kfree(req);//释放请求
}
static int s3c2410_udc_queue(struct usb_ep *_ep, struct usb_request *_req,
gfp_t gfp_flags)
{
struct s3c2410_request *req = to_s3c2410_req(_req);//获得请求
struct s3c2410_ep *ep = to_s3c2410_ep(_ep);//获得端点
struct s3c2410_udc *dev;
u32 ep_csr = 0;
int fifo_count = 0;
unsigned long flags;
if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
dprintk(DEBUG_NORMAL, "%s: invalid args\n", __func__);
return -EINVAL;
}
dev = ep->dev;
if (unlikely (!dev->driver
|| dev->gadget.speed == USB_SPEED_UNKNOWN)) {
return -ESHUTDOWN;
}
local_irq_save (flags);
if (unlikely(!_req || !_req->complete
|| !_req->buf || !list_empty(&req->queue))) {
local_irq_restore(flags);
return -EINVAL;
}
_req->status = -EINPROGRESS;
_req->actual = 0;
dprintk(DEBUG_VERBOSE, "%s: ep%x len %d\n",
__func__, ep->bEndpointAddress, _req->length);
if (ep->bEndpointAddress) {
udc_write(ep->bEndpointAddress & 0x7F, S3C2410_UDC_INDEX_REG);//把端点地址写到索引寄存器,表示某端点有效
ep_csr = udc_read((ep->bEndpointAddress & USB_DIR_IN)
? S3C2410_UDC_IN_CSR1_REG
: S3C2410_UDC_OUT_CSR1_REG);//读出端点方向,比如输入或输出
fifo_count = s3c2410_udc_fifo_count_out();//读出包的字节数
} else {
udc_write(0, S3C2410_UDC_INDEX_REG);//端点0有效
ep_csr = udc_read(S3C2410_UDC_IN_CSR1_REG);//读出状态
fifo_count = s3c2410_udc_fifo_count_out();//读出包的字节数
}
if (list_empty(&ep->queue) && !ep->halted) {
if (ep->bEndpointAddress == 0 /* ep0 */) {
switch (dev->ep0state) {
case EP0_IN_DATA_PHASE://输入端点
if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
&& s3c2410_udc_write_fifo(ep,
req)) {//把数据写到FIFO
dev->ep0state = EP0_IDLE;//端点0空闲
req = NULL;
}
break;
case EP0_OUT_DATA_PHASE://输出端点
if ((!_req->length)
|| ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
&& s3c2410_udc_read_fifo(ep,
req))) {//从FIFO读出数据
dev->ep0state = EP0_IDLE;//端点0空闲
req = NULL;
}
break;
default:
local_irq_restore(flags);
return -EL2HLT;
}
} else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
&& (!(ep_csr&S3C2410_UDC_OCSR1_PKTRDY))
&& s3c2410_udc_write_fifo(ep, req)) {//把数据写到FIFO
req = NULL;
} else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
&& fifo_count
&& s3c2410_udc_read_fifo(ep, req)) {//从FIFO读出数据
req = NULL;
}
}
if (likely (req != 0))
list_add_tail(&req->queue, &ep->queue);//把队列加到链表
local_irq_restore(flags);
dprintk(DEBUG_VERBOSE, "%s ok\n", __func__);
return 0;
}
static int s3c2410_udc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
struct s3c2410_udc *udc;
int retval = -EINVAL;
unsigned long flags;
struct s3c2410_request *req = NULL;
dprintk(DEBUG_VERBOSE, "%s(%p,%p)\n", __func__, _ep, _req);
if (!the_controller->driver)
return -ESHUTDOWN;
if (!_ep || !_req)
return retval;
udc = to_s3c2410_udc(ep->gadget);//获得设备
local_irq_save (flags);
list_for_each_entry (req, &ep->queue, queue) {//遍历队列链表
if (&req->req == _req) {
list_del_init (&req->queue);//删除队列
_req->status = -ECONNRESET;//连接复位
retval = 0;
break;
}
}
if (retval == 0) {
dprintk(DEBUG_VERBOSE,
"dequeued req %p from %s, len %d buf %p\n",
req, _ep->name, _req->length, _req->buf);
s3c2410_udc_done(ep, req, -ECONNRESET);//根据status执行请求,连接复位?
}
local_irq_restore (flags);
return retval;
}
static int s3c2410_udc_set_halt(struct usb_ep *_ep, int value)
{
struct s3c2410_ep *ep = to_s3c2410_ep(_ep);
u32 ep_csr = 0;
unsigned long flags;
u32 idx;
if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
dprintk(DEBUG_NORMAL, "%s: inval 2\n", __func__);
return -EINVAL;
}
local_irq_save (flags);
idx = ep->bEndpointAddress & 0x7F;//得到端点索引
if (idx == 0) {
s3c2410_udc_set_ep0_ss(base_addr);//清除SEND_STAL
s3c2410_udc_set_ep0_de_out(base_addr);//清除IN_PKT_RDY和DATA_END
} else {
udc_write(idx, S3C2410_UDC_INDEX_REG);//某端点有效
ep_csr = udc_read((ep->bEndpointAddress &USB_DIR_IN)
? S3C2410_UDC_IN_CSR1_REG
: S3C2410_UDC_OUT_CSR1_REG);
if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {//是否为输入端点
if (value)
udc_write(ep_csr | S3C2410_UDC_ICSR1_SENDSTL,
S3C2410_UDC_IN_CSR1_REG);//SETUP_END传输完成
else {
ep_csr &= ~S3C2410_UDC_ICSR1_SENDSTL;
udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);//SETUP_END没传输完
ep_csr |= S3C2410_UDC_ICSR1_CLRDT;
udc_write(ep_csr, S3C2410_UDC_IN_CSR1_REG);//清除SERVICED_OUT_PKT_RDY
}
} else {
if (value)
udc_write(ep_csr | S3C2410_UDC_OCSR1_SENDSTL,
S3C2410_UDC_OUT_CSR1_REG);//发送SEBD_STALL握手
else {
ep_csr &= ~S3C2410_UDC_OCSR1_SENDSTL;
udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);//结束状态握手
ep_csr |= S3C2410_UDC_OCSR1_CLRDT;
udc_write(ep_csr, S3C2410_UDC_OUT_CSR1_REG);//复位切换到DATA0
}
}
}
ep->halted = value ? 1 : 0;
local_irq_restore (flags);
return 0;
}
static const struct usb_ep_ops s3c2410_ep_ops = {
.enable = s3c2410_udc_ep_enable,//禁止端点中断
.disable = s3c2410_udc_ep_disable,//禁侄说愎中断
.alloc_request = s3c2410_udc_alloc_request,//分配一个请求
.free_request = s3c2410_udc_free_request,//释放请求
.queue = s3c2410_udc_queue,//处理队列
.dequeue = s3c2410_udc_dequeue,//删除队列请求
.set_halt = s3c2410_udc_set_halt,
};
static int s3c2410_udc_get_frame(struct usb_gadget *_gadget)
{
int tmp;
tmp = udc_read(S3C2410_UDC_FRAME_NUM2_REG) << 8;
tmp |= udc_read(S3C2410_UDC_FRAME_NUM1_REG);
return tmp;
}
static int s3c2410_udc_wakeup(struct usb_gadget *_gadget)
{
dprintk(DEBUG_NORMAL, "%s()\n", __func__);
return 0;
}
static int s3c2410_udc_set_selfpowered(struct usb_gadget *gadget, int value)
{
struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
if (value)
udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
else
udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
return 0;
}
static void s3c2410_udc_disable(struct s3c2410_udc *dev);
static void s3c2410_udc_enable(struct s3c2410_udc *dev);
static int s3c2410_udc_set_pullup(struct s3c2410_udc *udc, int is_on)
{
if (udc_info && udc_info->udc_command) {
if (is_on)
s3c2410_udc_enable(udc);
else {
if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
if (udc->driver && udc->driver->disconnect)
udc->driver->disconnect(&udc->gadget);
}
s3c2410_udc_disable(udc);
}
}
else
return -EOPNOTSUPP;
return 0;
}
static int s3c2410_udc_vbus_session(struct usb_gadget *gadget, int is_active)
{
struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
udc->vbus = (is_active != 0);
s3c2410_udc_set_pullup(udc, is_active);
return 0;
}
static int s3c2410_udc_pullup(struct usb_gadget *gadget, int is_on)
{
struct s3c2410_udc *udc = to_s3c2410_udc(gadget);
s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
return 0;
}
static irqreturn_t s3c2410_udc_vbus_irq(int irq, void *_dev)
{
struct s3c2410_udc *dev = _dev;
unsigned int value;
value = gpio_get_value(udc_info->vbus_pin) ? 1 : 0;
if (udc_info->vbus_pin_inverted)
value = !value;
if (value != dev->vbus)
s3c2410_udc_vbus_session(&dev->gadget, value);
return IRQ_HANDLED;
}
static int s3c2410_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
{
dprintk(DEBUG_NORMAL, "%s()\n", __func__);
if (udc_info && udc_info->vbus_draw) {
udc_info->vbus_draw(ma);
return 0;
}
return -ENOTSUPP;
}
static const struct usb_gadget_ops s3c2410_ops = {
.get_frame = s3c2410_udc_get_frame,
.wakeup = s3c2410_udc_wakeup,
.set_selfpowered = s3c2410_udc_set_selfpowered,
.pullup = s3c2410_udc_pullup,
.vbus_session = s3c2410_udc_vbus_session,
.vbus_draw = s3c2410_vbus_draw,
};
static void s3c2410_udc_disable(struct s3c2410_udc *dev)
{
dprintk(DEBUG_NORMAL, "%s()\n", __func__);
udc_write(0x00, S3C2410_UDC_USB_INT_EN_REG);
udc_write(0x00, S3C2410_UDC_EP_INT_EN_REG);
udc_write(S3C2410_UDC_USBINT_RESET
| S3C2410_UDC_USBINT_RESUME
| S3C2410_UDC_USBINT_SUSPEND,
S3C2410_UDC_USB_INT_REG);
udc_write(0x1F, S3C2410_UDC_EP_INT_REG);
if (udc_info && udc_info->udc_command)
udc_info->udc_command(S3C2410_UDC_P_DISABLE);
dev->gadget.speed = USB_SPEED_UNKNOWN;//-EPIPE毁掉管道
}
static void s3c2410_udc_reinit(struct s3c2410_udc *dev)
{
u32 i;
/* device/ep0 records init */
INIT_LIST_HEAD (&dev->gadget.ep_list);//初始化端点链表
INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);//初始化端点0链表
dev->ep0state = EP0_IDLE;//端点0空闲
//把设备的端点记录下来然后加到链表
for (i = 0; i < S3C2410_ENDPOINTS; i++) {
struct s3c2410_ep *ep = &dev->ep[i];
if (i != 0)
list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
ep->dev = dev;
ep->desc = NULL;
ep->halted = 0;
INIT_LIST_HEAD (&ep->queue);//初始化端点的队列
}
}
static void s3c2410_udc_enable(struct s3c2410_udc *dev)
{
int i;
dprintk(DEBUG_NORMAL, "s3c2410_udc_enable called\n");
/* dev->gadget.speed = USB_SPEED_UNKNOWN; */
dev->gadget.speed = USB_SPEED_FULL;
/* Set MAXP for all endpoints */
for (i = 0; i < S3C2410_ENDPOINTS; i++) {
udc_write(i, S3C2410_UDC_INDEX_REG);
udc_write((dev->ep[i].ep.maxpacket & 0x7ff) >> 3,
S3C2410_UDC_MAXP_REG);
}
udc_write(DEFAULT_POWER_STATE, S3C2410_UDC_PWR_REG);
/* Enable reset and suspend interrupt interrupts */
udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
S3C2410_UDC_USB_INT_EN_REG);
/* Enable ep0 interrupt */
udc_write(S3C2410_UDC_INT_EP0, S3C2410_UDC_EP_INT_EN_REG);
if (udc_info && udc_info->udc_command)
udc_info->udc_command(S3C2410_UDC_P_ENABLE);
}
/* 设备注册 */
int usb_gadget_register_driver(struct usb_gadget_driver *driver)
{
struct s3c2410_udc *udc = the_controller;
int retval;
dprintk(DEBUG_NORMAL, "usb_gadget_register_driver() '%s'\n",
driver->driver.name);
/* Sanity checks */
if (!udc)
return -ENODEV;
if (udc->driver)
return -EBUSY;
if (!driver->bind || !driver->setup
|| driver->speed < USB_SPEED_FULL) {
printk(KERN_ERR "Invalid driver: bind %p setup %p speed %d\n",
driver->bind, driver->setup, driver->speed);
return -EINVAL;
}
#if defined(MODULE)
if (!driver->unbind) {
printk(KERN_ERR "Invalid driver: no unbind method\n");
return -EINVAL;
}
#endif
/* Hook the driver */
udc->driver = driver;
udc->gadget.dev.driver = &driver->driver;
/* Bind the driver */
if ((retval = device_add(&udc->gadget.dev)) != 0) {
printk(KERN_ERR "Error in device_add() : %d\n",retval);
goto register_error;
}
dprintk(DEBUG_NORMAL, "binding gadget driver '%s'\n",
driver->driver.name);
if ((retval = driver->bind (&udc->gadget)) != 0)/* 把驱动和设备绑定起来*/ {
device_del(&udc->gadget.dev);
goto register_error;
}
/* 使能控制器 */
s3c2410_udc_enable(udc);
return 0;
register_error:
udc->driver = NULL;
udc->gadget.dev.driver = NULL;
return retval;
}
int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
{
struct s3c2410_udc *udc = the_controller;
if (!udc)
return -ENODEV;
if (!driver || driver != udc->driver || !driver->unbind)
return -EINVAL;
dprintk(DEBUG_NORMAL,"usb_gadget_register_driver() '%s'\n",
driver->driver.name);
driver->unbind(&udc->gadget);/* 取消绑定 */
device_del(&udc->gadget.dev);/* 删除设备 */
udc->driver = NULL;
/* Disable udc */
s3c2410_udc_disable(udc);
return 0;
}
/*---------------------------------------------------------------------------*/
static struct s3c2410_udc memory = {
.gadget = {
.ops = &s3c2410_ops,
.ep0 = &memory.ep[0].ep,
.name = gadget_name,
.dev = {
.init_name = "gadget",
},
},
/* control endpoint */
.ep[0] = {
.num = 0,
.ep = {
.name = ep0name,
.ops = &s3c2410_ep_ops,
.maxpacket = EP0_FIFO_SIZE,
},
.dev = &memory,
},
/* first group of endpoints */
.ep[1] = {
.num = 1,
.ep = {
.name = "ep1-bulk",
.ops = &s3c2410_ep_ops,
.maxpacket = EP_FIFO_SIZE,
},
.dev = &memory,
.fifo_size = EP_FIFO_SIZE,
.bEndpointAddress = 1,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
},
.ep[2] = {
.num = 2,
.ep = {
.name = "ep2-bulk",
.ops = &s3c2410_ep_ops,
.maxpacket = EP_FIFO_SIZE,
},
.dev = &memory,
.fifo_size = EP_FIFO_SIZE,
.bEndpointAddress = 2,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
},
.ep[3] = {
.num = 3,
.ep = {
.name = "ep3-bulk",
.ops = &s3c2410_ep_ops,
.maxpacket = EP_FIFO_SIZE,
},
.dev = &memory,
.fifo_size = EP_FIFO_SIZE,
.bEndpointAddress = 3,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
},
.ep[4] = {
.num = 4,
.ep = {
.name = "ep4-bulk",
.ops = &s3c2410_ep_ops,
.maxpacket = EP_FIFO_SIZE,
},
.dev = &memory,
.fifo_size = EP_FIFO_SIZE,
.bEndpointAddress = 4,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
}
};
static int s3c2410_udc_probe(struct platform_device *pdev)
{
struct s3c2410_udc *udc = &memory;
struct device *dev = &pdev->dev;
int retval;
int irq;
dev_dbg(dev, "%s()\n", __func__);
usb_bus_clock = clk_get(NULL, "usb-bus-gadget");//获得gadget时钟
if (IS_ERR(usb_bus_clock)) {
dev_err(dev, "failed to get usb bus clock source\n");
return PTR_ERR(usb_bus_clock);
}
clk_enable(usb_bus_clock);//使能时钟
udc_clock = clk_get(NULL, "usb-device");//获得设备主控制器时钟
if (IS_ERR(udc_clock)) {
dev_err(dev, "failed to get udc clock source\n");
return PTR_ERR(udc_clock);
}
clk_enable(udc_clock);//使能时钟
mdelay(10);
dev_dbg(dev, "got and enabled clocks\n");
if (strncmp(pdev->name, "s3c2440", 7) == 0) {
dev_info(dev, "S3C2440: increasing FIFO to 128 bytes\n");
memory.ep[1].fifo_size = S3C2440_EP_FIFO_SIZE;//设置fifo大小
memory.ep[2].fifo_size = S3C2440_EP_FIFO_SIZE;
memory.ep[3].fifo_size = S3C2440_EP_FIFO_SIZE;
memory.ep[4].fifo_size = S3C2440_EP_FIFO_SIZE;
}
spin_lock_init (&udc->lock);//初始化锁
udc_info = pdev->dev.platform_data;
rsrc_start = S3C2410_PA_USBDEV;//设备起始地址
rsrc_len = S3C24XX_SZ_USBDEV;//长度
if (!request_mem_region(rsrc_start, rsrc_len, gadget_name))//内存使用请求
return -EBUSY;
base_addr = ioremap(rsrc_start, rsrc_len);//映射内存
if (!base_addr) {
retval = -ENOMEM;
goto err_mem;
}
//初始化内部成员 初始化dma链表头 初始化信号量 初始化锁
//设置设备不能唤醒
//初始化内部成员设置电源状态
//设置NUMA节点
device_initialize(&udc->gadget.dev);
//初始化gadget.dev.
udc->gadget.dev.parent = &pdev->dev;
udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
the_controller = udc;
//把udc赋给pdev里的data
platform_set_drvdata(pdev, udc);
//禁止USB主控制器
s3c2410_udc_disable(udc);
//把设备的端点记录下来然后加到链表
s3c2410_udc_reinit(udc);
/* 注册中断 */
retval = request_irq(IRQ_USBD, s3c2410_udc_irq,
IRQF_DISABLED, gadget_name, udc);
if (retval != 0) {
dev_err(dev, "cannot get irq %i, err %d\n", IRQ_USBD, retval);
retval = -EBUSY;
goto err_map;
}
dev_dbg(dev, "got irq %i\n", IRQ_USBD);
if (udc_info && udc_info->vbus_pin > 0) {
retval = gpio_request(udc_info->vbus_pin, "udc vbus");
if (retval < 0) {
dev_err(dev, "cannot claim vbus pin\n");
goto err_int;
}
irq = gpio_to_irq(udc_info->vbus_pin);
if (irq < 0) {
dev_err(dev, "no irq for gpio vbus pin\n");
goto err_gpio_claim;
}
retval = request_irq(irq, s3c2410_udc_vbus_irq,
IRQF_DISABLED | IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING | IRQF_SHARED,
gadget_name, udc);
if (retval != 0) {
dev_err(dev, "can't get vbus irq %d, err %d\n",
irq, retval);
retval = -EBUSY;
goto err_gpio_claim;
}
dev_dbg(dev, "got irq %i\n", irq);
} else {
udc->vbus = 1;
}
//创建设备属性文件
if (s3c2410_udc_debugfs_root) {
udc->regs_info = debugfs_create_file("registers", S_IRUGO,
s3c2410_udc_debugfs_root,
udc, &s3c2410_udc_debugfs_fops);
if (!udc->regs_info)
dev_warn(dev, "debugfs file creation failed\n");
}
dev_dbg(dev, "probe ok\n");
return 0;
err_gpio_claim:
if (udc_info && udc_info->vbus_pin > 0)
gpio_free(udc_info->vbus_pin);
err_int:
free_irq(IRQ_USBD, udc);
err_map:
iounmap(base_addr);
err_mem:
release_mem_region(rsrc_start, rsrc_len);
return retval;
}
static int s3c2410_udc_remove(struct platform_device *pdev)
{
struct s3c2410_udc *udc = platform_get_drvdata(pdev);
unsigned int irq;
dev_dbg(&pdev->dev, "%s()\n", __func__);
if (udc->driver)
return -EBUSY;
debugfs_remove(udc->regs_info);//删除设备属性文件
if (udc_info && udc_info->vbus_pin > 0) {
irq = gpio_to_irq(udc_info->vbus_pin);
free_irq(irq, udc);
}
free_irq(IRQ_USBD, udc);//释放中断
iounmap(base_addr);//解除地址映射
release_mem_region(rsrc_start, rsrc_len);//释放内存请求
platform_set_drvdata(pdev, NULL);//把私有数据置空
if (!IS_ERR(udc_clock) && udc_clock != NULL) {
clk_disable(udc_clock);
clk_put(udc_clock);//释放时钟
udc_clock = NULL;
}
if (!IS_ERR(usb_bus_clock) && usb_bus_clock != NULL) {
clk_disable(usb_bus_clock);
clk_put(usb_bus_clock);
usb_bus_clock = NULL;
}
dev_dbg(&pdev->dev, "%s: remove ok\n", __func__);
return 0;
}
static int s3c2410_udc_suspend(struct platform_device *pdev, pm_message_t message)
{
if (udc_info && udc_info->udc_command)
udc_info->udc_command(S3C2410_UDC_P_DISABLE);//挂起
return 0;
}
static int s3c2410_udc_resume(struct platform_device *pdev)
{
if (udc_info && udc_info->udc_command)
udc_info->udc_command(S3C2410_UDC_P_ENABLE);
return 0;
}
static struct platform_driver udc_driver_2440 = {
.driver = {
.name = "s3c2440-usbgadget",
.owner = THIS_MODULE,
},
.probe = s3c2410_udc_probe,
.remove = s3c2410_udc_remove,
.suspend = s3c2410_udc_suspend,
.resume = s3c2410_udc_resume,
};
static int __init udc_init(void)
{
int retval;
dprintk(DEBUG_NORMAL, "%s: version %s\n", gadget_name, DRIVER_VERSION);
s3c2410_udc_debugfs_root = debugfs_create_dir(gadget_name, NULL);
if (IS_ERR(s3c2410_udc_debugfs_root)) {
printk(KERN_ERR "%s: debugfs dir creation failed %ld\n",
gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
s3c2410_udc_debugfs_root = NULL;
}
retval = platform_driver_register(&udc_driver_2440);
if (retval)
goto err;
return 0;
err:
debugfs_remove(s3c2410_udc_debugfs_root);
return retval;
}
static void __exit udc_exit(void)
{
platform_driver_unregister(&udc_driver_2440);
debugfs_remove(s3c2410_udc_debugfs_root);
}
module_init(udc_init);
module_exit(udc_exit);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL");