📄 usbdev.c
字号:
/* * BRIEF MODULE DESCRIPTION * Au1000 USB Device-Side (device layer) * * Copyright 2001-2002 MontaVista Software Inc. * Author: MontaVista Software, Inc. * stevel@mvista.com or source@mvista.com * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/kernel.h>#include <linux/ioport.h>#include <linux/sched.h>#include <linux/signal.h>#include <linux/errno.h>#include <linux/poll.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/fcntl.h>#include <linux/module.h>#include <linux/spinlock.h>#include <linux/list.h>#include <linux/smp_lock.h>#define DEBUG#include <linux/usb.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/mipsregs.h>#include <asm/au1000.h>#include <asm/au1000_dma.h>#include <asm/au1000_usbdev.h>#ifdef DEBUG#undef VDEBUG#ifdef VDEBUG#define vdbg(fmt, arg...) printk(KERN_DEBUG __FILE__ ": " fmt "\n" , ## arg)#else#define vdbg(fmt, arg...) do {} while (0)#endif#else#define vdbg(fmt, arg...) do {} while (0)#endif#define ALLOC_FLAGS (in_interrupt () ? GFP_ATOMIC : GFP_KERNEL)#define EP_FIFO_DEPTH 8typedef enum { SETUP_STAGE = 0, DATA_STAGE, STATUS_STAGE} ep0_stage_t;typedef struct { int read_fifo; int write_fifo; int ctrl_stat; int read_fifo_status; int write_fifo_status;} endpoint_reg_t;typedef struct { usbdev_pkt_t *head; usbdev_pkt_t *tail; int count;} pkt_list_t;typedef struct { int active; struct usb_endpoint_descriptor *desc; endpoint_reg_t *reg; /* Only one of these are used, unless this is the control ep */ pkt_list_t inlist; pkt_list_t outlist; unsigned int indma, outdma; /* DMA channel numbers for IN, OUT */ /* following are extracted from endpoint descriptor for easy access */ int max_pkt_size; int type; int direction; /* WE assign endpoint addresses! */ int address; spinlock_t lock;} endpoint_t;static struct usb_dev { endpoint_t ep[6]; ep0_stage_t ep0_stage; struct usb_device_descriptor * dev_desc; struct usb_interface_descriptor* if_desc; struct usb_config_descriptor * conf_desc; u8 * full_conf_desc; struct usb_string_descriptor * str_desc[6]; /* callback to function layer */ void (*func_cb)(usbdev_cb_type_t type, unsigned long arg, void *cb_data); void* cb_data; usbdev_state_t state; // device state int suspended; // suspended flag int address; // device address int interface; int num_ep; u8 alternate_setting; u8 configuration; // configuration value int remote_wakeup_en;} usbdev;static endpoint_reg_t ep_reg[] = { // FIFO's 0 and 1 are EP0 default control {USBD_EP0RD, USBD_EP0WR, USBD_EP0CS, USBD_EP0RDSTAT, USBD_EP0WRSTAT }, {0}, // FIFO 2 is EP2, IN { -1, USBD_EP2WR, USBD_EP2CS, -1, USBD_EP2WRSTAT }, // FIFO 3 is EP3, IN { -1, USBD_EP3WR, USBD_EP3CS, -1, USBD_EP3WRSTAT }, // FIFO 4 is EP4, OUT {USBD_EP4RD, -1, USBD_EP4CS, USBD_EP4RDSTAT, -1 }, // FIFO 5 is EP5, OUT {USBD_EP5RD, -1, USBD_EP5CS, USBD_EP5RDSTAT, -1 }};static struct { unsigned int id; const char *str;} ep_dma_id[] = { { DMA_ID_USBDEV_EP0_TX, "USBDev EP0 IN" }, { DMA_ID_USBDEV_EP0_RX, "USBDev EP0 OUT" }, { DMA_ID_USBDEV_EP2_TX, "USBDev EP2 IN" }, { DMA_ID_USBDEV_EP3_TX, "USBDev EP3 IN" }, { DMA_ID_USBDEV_EP4_RX, "USBDev EP4 OUT" }, { DMA_ID_USBDEV_EP5_RX, "USBDev EP5 OUT" }};#define DIR_OUT 0#define DIR_IN (1<<3)#define CONTROL_EP USB_ENDPOINT_XFER_CONTROL#define BULK_EP USB_ENDPOINT_XFER_BULKstatic inline endpoint_t *epaddr_to_ep(struct usb_dev* dev, int ep_addr){ if (ep_addr >= 0 && ep_addr < 2) return &dev->ep[0]; if (ep_addr < 6) return &dev->ep[ep_addr]; return NULL;}static const char* std_req_name[] = { "GET_STATUS", "CLEAR_FEATURE", "RESERVED", "SET_FEATURE", "RESERVED", "SET_ADDRESS", "GET_DESCRIPTOR", "SET_DESCRIPTOR", "GET_CONFIGURATION", "SET_CONFIGURATION", "GET_INTERFACE", "SET_INTERFACE", "SYNCH_FRAME"};static inline const char*get_std_req_name(int req){ return (req >= 0 && req <= 12) ? std_req_name[req] : "UNKNOWN";}#if 0static voiddump_setup(struct usb_ctrlrequest* s){ dbg("%s: requesttype=%d", __FUNCTION__, s->requesttype); dbg("%s: request=%d %s", __FUNCTION__, s->request, get_std_req_name(s->request)); dbg("%s: value=0x%04x", __FUNCTION__, s->wValue); dbg("%s: index=%d", __FUNCTION__, s->index); dbg("%s: length=%d", __FUNCTION__, s->length);}#endifstatic inline usbdev_pkt_t *alloc_packet(endpoint_t * ep, int data_size, void* data){ usbdev_pkt_t* pkt = kmalloc(sizeof(usbdev_pkt_t) + data_size, ALLOC_FLAGS); if (!pkt) return NULL; pkt->ep_addr = ep->address; pkt->size = data_size; pkt->status = 0; pkt->next = NULL; if (data) memcpy(pkt->payload, data, data_size); return pkt;}/* * Link a packet to the tail of the enpoint's packet list. * EP spinlock must be held when calling. */static voidlink_tail(endpoint_t * ep, pkt_list_t * list, usbdev_pkt_t * pkt){ if (!list->tail) { list->head = list->tail = pkt; list->count = 1; } else { list->tail->next = pkt; list->tail = pkt; list->count++; }}/* * Unlink and return a packet from the head of the given packet * list. It is the responsibility of the caller to free the packet. * EP spinlock must be held when calling. */static usbdev_pkt_t *unlink_head(pkt_list_t * list){ usbdev_pkt_t *pkt; pkt = list->head; if (!pkt || !list->count) { return NULL; } list->head = pkt->next; if (!list->head) { list->head = list->tail = NULL; list->count = 0; } else list->count--; return pkt;}/* * Create and attach a new packet to the tail of the enpoint's * packet list. EP spinlock must be held when calling. */static usbdev_pkt_t *add_packet(endpoint_t * ep, pkt_list_t * list, int size){ usbdev_pkt_t *pkt = alloc_packet(ep, size, NULL); if (!pkt) return NULL; link_tail(ep, list, pkt); return pkt;}/* * Unlink and free a packet from the head of the enpoint's * packet list. EP spinlock must be held when calling. */static inline voidfree_packet(pkt_list_t * list){ kfree(unlink_head(list));}/* EP spinlock must be held when calling. */static inline voidflush_pkt_list(pkt_list_t * list){ while (list->count) free_packet(list);}/* EP spinlock must be held when calling */static inline voidflush_write_fifo(endpoint_t * ep){ if (ep->reg->write_fifo_status >= 0) { au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->reg->write_fifo_status); //udelay(100); //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, // ep->reg->write_fifo_status); }}/* EP spinlock must be held when calling */static inline voidflush_read_fifo(endpoint_t * ep){ if (ep->reg->read_fifo_status >= 0) { au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, ep->reg->read_fifo_status); //udelay(100); //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF, // ep->reg->read_fifo_status); }}/* EP spinlock must be held when calling. */static voidendpoint_flush(endpoint_t * ep){ // First, flush all packets flush_pkt_list(&ep->inlist); flush_pkt_list(&ep->outlist); // Now flush the endpoint's h/w FIFO(s) flush_write_fifo(ep); flush_read_fifo(ep);}/* EP spinlock must be held when calling. */static voidendpoint_stall(endpoint_t * ep){ u32 cs; warn("%s", __FUNCTION__); cs = au_readl(ep->reg->ctrl_stat) | USBDEV_CS_STALL; au_writel(cs, ep->reg->ctrl_stat);}/* EP spinlock must be held when calling. */static voidendpoint_unstall(endpoint_t * ep){ u32 cs; warn("%s", __FUNCTION__); cs = au_readl(ep->reg->ctrl_stat) & ~USBDEV_CS_STALL; au_writel(cs, ep->reg->ctrl_stat);}static voidendpoint_reset_datatoggle(endpoint_t * ep){ // FIXME: is this possible?}/* EP spinlock must be held when calling. */static intendpoint_fifo_read(endpoint_t * ep){ int read_count = 0; u8 *bufptr; usbdev_pkt_t *pkt = ep->outlist.tail; if (!pkt) return -EINVAL; bufptr = &pkt->payload[pkt->size]; while (au_readl(ep->reg->read_fifo_status) & USBDEV_FSTAT_FCNT_MASK) { *bufptr++ = au_readl(ep->reg->read_fifo) & 0xff; read_count++; pkt->size++; } return read_count;}#if 0/* EP spinlock must be held when calling. */static intendpoint_fifo_write(endpoint_t * ep, int index){ int write_count = 0; u8 *bufptr; usbdev_pkt_t *pkt = ep->inlist.head; if (!pkt) return -EINVAL; bufptr = &pkt->payload[index]; while ((au_readl(ep->reg->write_fifo_status) & USBDEV_FSTAT_FCNT_MASK) < EP_FIFO_DEPTH) { if (bufptr < pkt->payload + pkt->size) { au_writel(*bufptr++, ep->reg->write_fifo); write_count++; } else { break; } } return write_count;}#endif/* * This routine is called to restart transmission of a packet. * The endpoint's TSIZE must be set to the new packet's size, * and DMA to the write FIFO needs to be restarted. * EP spinlock must be held when calling. */static voidkickstart_send_packet(endpoint_t * ep){ u32 cs; usbdev_pkt_t *pkt = ep->inlist.head; vdbg("%s: ep%d, pkt=%p", __FUNCTION__, ep->address, pkt); if (!pkt) { err("%s: head=NULL! list->count=%d", __FUNCTION__, ep->inlist.count); return; } dma_cache_wback_inv((unsigned long)pkt->payload, pkt->size); /* * make sure FIFO is empty */ flush_write_fifo(ep); cs = au_readl(ep->reg->ctrl_stat) & USBDEV_CS_STALL; cs |= (pkt->size << USBDEV_CS_TSIZE_BIT); au_writel(cs, ep->reg->ctrl_stat); if (get_dma_active_buffer(ep->indma) == 1) { set_dma_count1(ep->indma, pkt->size); set_dma_addr1(ep->indma, virt_to_phys(pkt->payload)); enable_dma_buffer1(ep->indma); // reenable } else { set_dma_count0(ep->indma, pkt->size); set_dma_addr0(ep->indma, virt_to_phys(pkt->payload)); enable_dma_buffer0(ep->indma); // reenable } if (dma_halted(ep->indma)) start_dma(ep->indma);}/* * This routine is called when a packet in the inlist has been * completed. Frees the completed packet and starts sending the * next. EP spinlock must be held when calling. */static usbdev_pkt_t *send_packet_complete(endpoint_t * ep){ usbdev_pkt_t *pkt = unlink_head(&ep->inlist); if (pkt) { pkt->status = (au_readl(ep->reg->ctrl_stat) & USBDEV_CS_NAK) ? PKT_STATUS_NAK : PKT_STATUS_ACK; vdbg("%s: ep%d, %s pkt=%p, list count=%d", __FUNCTION__, ep->address, (pkt->status & PKT_STATUS_NAK) ? "NAK" : "ACK", pkt, ep->inlist.count); } /* * The write fifo should already be drained if things are * working right, but flush it anyway just in case. */ flush_write_fifo(ep); // begin transmitting next packet in the inlist if (ep->inlist.count) { kickstart_send_packet(ep); } return pkt;}/* * Add a new packet to the tail of the given ep's packet * inlist. The transmit complete interrupt frees packets from * the head of this list. EP spinlock must be held when calling. */static intsend_packet(struct usb_dev* dev, usbdev_pkt_t *pkt, int async){ pkt_list_t *list; endpoint_t* ep; if (!pkt || !(ep = epaddr_to_ep(dev, pkt->ep_addr))) return -EINVAL; if (!pkt->size) return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -