USB驅(qū)動(dòng)程序源代碼_第1頁
USB驅(qū)動(dòng)程序源代碼_第2頁
USB驅(qū)動(dòng)程序源代碼_第3頁
USB驅(qū)動(dòng)程序源代碼_第4頁
USB驅(qū)動(dòng)程序源代碼_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、項(xiàng)目報(bào)告7USB驅(qū)動(dòng)程序源代碼 作者:羅仕波一 頭文件go7007sb.h/* *go7007sb.h - this file includes all relative header files that *will be used in go7007sb vedio usb interface driver, and it *also defines all relative driver private data structures and *its io control commands. */#ifndef _GO7007SB_H#define _GO7007SB_H#includ

2、e #include #include #include #include #include #include #include #include #include #include #include /#define DEBUG#define DRIVER_VERSION 1.0.0#define DRIVER_DESC USB GO7007SB Driver#include MODULE_AUTHOR(Luo Shibo);MODULE_DESCRIPTION(DRIVER_DESC DRIVER_VERSION);MODULE_LICENSE(GPL);/* *io control co

3、mmands definition,these commands will be *used to control the device in function iocntl_go7007sb */#defineGO7007SB_IOC_MAGICU/command magic number#define GO7007SB_IOC_RESET_IO(GO7007SB_IOC_MAGIC,0)/software reset the device#defineGO7007SB_IOC_SHUTDOWN_IO(GO7007SB_IOC_MAGIC,1)/shutdown the device#def

4、ineGO7007SB_IOC_PAUSE_IO(GO7007SB_IOC_MAGIC,2)/pause compression#defineGO7007SB_IOC_RESUME_IO(GO7007SB_IOC_MAGIC,3)/resume from pause status#define GO7007SB_IOC_GETSTATE_IOR(GO7007SB_IOC_MAGIC,4,unsigned int)/get state of dev/* *go7007sb device file number,the major of all USB devices have been defi

5、ned as 180, *we just need to define minors here,currently,we accept only 4 devices *their minors start from 64,so minors:64,65,66,67 should be ok. */#ifdef _KERNEL_/*_KERNEL_*/#define GO7007SB_MAX_MNR 4#define GO7007SB_BASE_MNR 64#define USB_GO7007SB_MINOR(inode)MINOR(inode)-i_rdev)-GO7007SB_BASE_MN

6、R/* *go7007sb video interface number */#defineGO7007SB_VIDEO_IFNUM0/* *endpoints definition */#defineGO7007SB_MAX_EP_NUM4/maximum endpoint number#defineGO7007SB_EP_BULK_IN1/in endpoint for stream#define GO7007SB_EP_CTRL2/out endpoint for control#defineGO7007SB_EP_BULK_OUT3/out endpoint for initializ

7、ation#define GO7007SB_EP_INTR4/in endpoint for interrupt#defineIS_EP_BULK(ep)(ep).bmAttributes=USB_ENDPOINT_XFER_BULK ? 1 : 0)#defineIS_EP_BULK_IN(ep)(IS_EP_BULK(ep)& (ep).bEndpointAddress & USB_ENDPOINT_DIR_MASK)=USB_DIR_IN)#defineIS_EP_BULK_OUT(ep) (IS_EP_BULK(ep)& (ep).bEndpointAddress & USB_ENDP

8、OINT_DIR_MASK)=USB_DIR_OUT)#define IS_EP_INTR(ep) (ep).bmAttributes=USB_ENDPOINT_XFER_INT ? 1 : 0)#define IS_EP_CTRL(ep) (ep).bmAttributes=USB_ENDPOINT_XFER_CONTROL ? 1 : 0)#define USB_ENDPOINT_NUM(ep)(ep).bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)#define USB_EP_DIR_IN0#defineUSB_EP_DIR_OUT1/* *bu

9、ffer sizes */#define GO7007SB_STRM_BUF_SZ128/streaming buffer#define GO7007SB_INIT_BUF_SZ64/initial buffer#define IN_URB_BUF_SZ1024/buffer for in urb#defineOUT_URB_BUF_SZ4*GO7007SB_INIT_BUF_SZ/buffer for out urb/* *times */#define GO7007SB_INT_INTERVAL5/interrupt interval:5ms#define GO7007SB_RD_TIME

10、OUT3*HZ/timeout per read:3s#defineGO7007SB_RD_EXPIRE3/expires after 3 read timeouts#define GO7007SB_WR_TIMEOUT3*HZ#define GO7007SB_WR_EXPIRE10/* *interrupts and command values:values with REQ are requests sent from go7007sb *values with CMD are commands sent from host to control go7007sb */#defineGO

11、7007SB_IRQ_VALUE_MASK0xFFFF0000#define go7007sb_irq_value(u32_data)(u32_data) & GO7007SB_IRQ_VALUE_MASK) 16)#defineGO7007SB_IRQ_BOOT0x55AA/boota : go7007sb request to boot#define GO7007SB_IRQ_INIT0x5A5A/bootb2: boot done, request to init#define GO7007SB_IRQ_RUN0x0000/initz : init done, request to ru

12、n#define GO7007SB_IRQ_PAUSE0x0020/the ack of PAUSE command#define GO7007SB_IRQ_SHUTDOWN0xA55A/fatal error,request to shutdown#define GO7007SB_CMD_BOOT0x55AA/boot go7007sb,write it into init buffer#define GO7007SB_CMD_INIT_BICP0x1000/use Block-Init-Config-Package to init#define GO7007SB_CMD_INIT_RICP

13、0x2000/use Register-Init_Config-Package to init#define GO7007SB_CMD_INIT_DICP0x4000/use DRAM-Init-Config-Packet to init#define GO7007SB_CMD_START0x8000/start encoding#define GO7007SB_CMD_PAUSE0x0020/pause encoding#define GO7007SB_CMD_SHUTDOWN0x0081/shutdown the device#define GO7007SB_CMD_SOFTRESET0x

14、0082/software reset the device#define XRISC_EXCODE_START_ADDR0x0200/xrisc executable code starting address#define NUM_OF_FIRMWARE_PKT1/number of firmware packet downloaded/* *file paths */#define PKT_TYPE_FIRMWARE0#define PKT_TYPE_INITPKT1#define DEFAULT_FIRMWARE_PATH/etc/go7007sb/go7007fw.bin#defin

15、e DEFAULT_INITPKT_PATH/etc/go7007sb/go7007in.bin#define DEFAULT_CONF_FILE_PATH/etc/go7007sb.conf/* *go7007sb device id:this will be used for system to *find the driver for a hotpluged go7007sb vedio device *here we only need to match vendor and productor id. */static struct usb_device_id go7007sb_de

16、vice_id=match_flags:USB_DEVICE_ID_MATCH_DEVICE |USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS,idVendor:0x0EB1,idProduct:0x7007,bInterfaceClass:0xFF,bInterfaceSubClass:0x00,;#endif/*_KERNEL_*/* *go7007sb device private data:this struct descripts the *relative info about this device

17、. */#define GO7007SB_STATE_NOTBOOTED0 /before bootA state#define GO7007SB_STATE_BOOTFAILED1 /failed to bootup the device#define GO7007SB_STATE_BOOTDONE2 /succeeded to boot the device#define GO7007SB_STATE_INITFAILED3 /failed to initialize the device#define GO7007SB_STATE_PAUSED4 /paused#define GO700

18、7SB_STATE_HALTED5 /device halted#define GO7007SB_STATE_RUNNING6 /device is running#define GO7007SB_STATE_READY7 /start-coding command sent#ifdef_KERNEL_/*_KERNEL_*/struct go7007sb_usb_datastruct usb_device*go7007sb_dev;/the usb device structstruct urbgo7007sb_irq;/the interrupt urb of this deviceuns

19、igned intifnum;/interface number of the vedio interfacekdev_tgo7007sb_minor;/the device minor number of go7007sbcharisopen;/not zero if the device is opencharpresent;/not zero if the device is presentunsigned intstate;/device state_u32irq;/buffer for interrupt endpoint,4 bytes_u64ctrl;/buffer for co

20、ntrol endpoint,8 byteschar*out_buf,*in_buf;/the io buffers used to transfercharbulk_in_ep;/data in endpoint,should be 1charctrl_ep;/control out endpoint num,should be 2charbulk_out_ep;/init out endpoint,should be 3charintr_ep;/interrupt listening endpoint,should be 4wait_queue_head_trd_wait_q;/wait

21、queue on read requestwait_queue_head_twr_wait_q;/wait queue on write requeststruct semaphoresem;/semphore to prevent concurrent operationunsigned intrd_nak_timeout;/seconds to wait before timeoutunsigned intwr_nak_timeout;/seconds to wait before timeout;#endif /*_KERNEL_*/#endif二源代碼go7007sb.c/* *go7

22、007sb.c - defines all the global varibales and functions of the driver */#ifndef MODULE#define MODULE#endif#ifndef _KERNEL_#define _KERNEL_#endif#include go7007sb.h/* *global variables */struct go7007sb_usb_data *p_go7007sb_data_tableGO7007SB_MAX_MNR=NULL,/*.*/;static DECLARE_MUTEX(go7007sb_mutex);/

23、* *macroes to get/set the device state */#define set_state(go_data,s)do(go_data)-state=(s);while(0)#define get_state(go_data)(go_data)-state)/* *function to build the initial buffer to send command to the device */static inline int build_cmd_buffer(char *buf,_u16 cmd)_u16*u16_buf=(_u16 *)buf;if(!u16

24、_buf)return -ENOMEM;switch(cmd)case GO7007SB_CMD_BOOT:u16_buf0=cpu_to_le16(GO7007SB_CMD_BOOT);u16_buf1=cpu_to_le16(XRISC_EXCODE_START_ADDR);u16_buf3=cpu_to_le16(NUM_OF_FIRMWARE_PKT);break;case GO7007SB_CMD_START:u16_buf0=cpu_to_le16(GO7007SB_CMD_START);/*note:buffer1-buffer31should reffer to*/*the v

25、endor sdk and to be handled later */break;case GO7007SB_CMD_PAUSE:u16_buf0=cpu_to_le16(GO7007SB_CMD_PAUSE);break;case GO7007SB_CMD_SHUTDOWN:u16_buf0=cpu_to_le16(GO7007SB_CMD_SHUTDOWN);break;case GO7007SB_CMD_SOFTRESET:u16_buf0=cpu_to_le16(GO7007SB_CMD_SOFTRESET);break;default:return -EINVAL;return 0

26、;/* *function to locate the paths of firmware and config packet */static struct file *locate_vendor_image(int type)struct file *filp;switch(type)case PKT_TYPE_FIRMWARE:filp=filp_open(DEFAULT_FIRMWARE_PATH,O_RDONLY,0644);if(IS_ERR(filp)goto error;break;case PKT_TYPE_INITPKT:filp=filp_open(DEFAULT_INI

27、TPKT_PATH,O_RDONLY,0644);if(IS_ERR(filp)goto error;break;default:goto error;return filp;error:return NULL;/* *function to send command to device through bulk out endpoint */static int send_command(struct go7007sb_usb_data *go_data,_u16 cmd)intretval;intbytes;intwr_expires=GO7007SB_WR_EXPIRE;struct u

28、sb_device *dev;char*cmd_buf=go_data-out_buf;kdev_tgo_minor=go_data-go7007sb_minor;if(!cmd_buf)return -ENOMEM;/* *build the initial buffer */if(retval=build_cmd_buffer(cmd_buf,cmd)!=0)return retval;/* *fill the urb and then submit it */dev=go_data-go7007sb_dev;try_again:retval=usb_bulk_msg(dev,usb_sn

29、dbulkpipe(dev,go_data-bulk_out_ep),cmd_buf,GO7007SB_INIT_BUF_SZ,&bytes,GO7007SB_WR_TIMEOUT);/* *check the result */if(retval=USB_ST_TIMEOUT & !bytes)/timeout and no data sentif(-wr_expiresbulk_out_ep)err(send_command(%d):endpoint halted!,go_minor);goto try_again;else if(retvalgo7007sb_minor;buf=go_d

30、ata-out_buf;if(!buf)return -ENOMEM;dev=go_data-go7007sb_dev;result=0;wr_expires=GO7007SB_WR_EXPIRE;while(count=kernel_read(fp,fp-f_pos,buf,OUT_URB_BUF_SZ)0)this_write=(count bulk_out_ep),buf,this_write,&partial,GO7007SB_WR_TIMEOUT);if(result=USB_ST_TIMEOUT & !partial) /timed out and no data writenif

31、(-wr_expiresbulk_out_ep)err(download_file(%d):bulk out endpoint halted!,go_minor);break;continue;/if halt condition clearedelse if(resultf_pos+=partial;return result;/ go7007sb device file operations definition block/* *function:open_go7007sb - to open the device */static int open_go7007sb(struct in

32、ode *inode,struct file *file)struct go7007sb_usb_data*go_data;struct usb_device*dev;kdev_tgo_minor;interr=0;down(&go7007sb_mutex);go_minor=USB_GO7007SB_MINOR(inode);dbg(open_go7007sb(%d): try to open device,go_minor);go_data=p_go7007sb_data_tablego_minor;if(!go_data)printk(open_go7007sb(%d):unable t

33、o get private data of deviden,go_minor);err=-ENODEV;goto out_error;dev=go_data-go7007sb_dev;if(!dev)printk(open_go7007sb(%d):device is not present!n,go_minor);err=-ENODEV;goto out_error;if(!go_data-present)printk(open_go7007sb(%d):device is not present!n,go_minor);err=-ENODEV;goto out_error;if(go_da

34、ta-isopen)printk(open_go7007sb(%d):device is already opened!n,go_minor);err=-EBUSY;goto out_error;if(get_state(go_data)!=GO7007SB_STATE_READY)printk(open_go7007sb(%d):device is not ready!n,go_minor);err=-ENODEV;goto out_error;down(&go_data-sem);/* *then send the start coding command */if(err=send_co

35、mmand(go_data,GO7007SB_CMD_START)!=0)printk(open_go7007sb(%d):send start command failed!n,go_minor);up(&go_data-sem);goto out_error;up(&go_data-sem);/release the mutex before sleeping/* *wait for the processing of command */interruptible_sleep_on_timeout(&go_data-wr_wait_q,GO7007SB_WR_TIMEOUT);down(

36、&go_data-sem); /regain the mutexif(signal_pending(current)up(&go_data-sem);err=-EINTR;goto out_error;if(get_state(go_data)!=GO7007SB_STATE_RUNNING)printk(open_go7007sb(%d):process start command failed!n,go_minor);up(&go_data-sem);err=-EAGAIN;goto out_error;dbg(open_go7007sb(%d):device opened!,go_min

37、or);go_data-isopen=1;up(&go_data-sem);file-private_data=go_data;MOD_INC_USE_COUNT;out_error:up(&go7007sb_mutex );return err;/* *function:close_go7007sb - to close the device */static int close_go7007sb(struct inode *inode,struct file *file)struct go7007sb_usb_data *go_data;kdev_t go_minor;int err=0;

38、down(&go7007sb_mutex);go_minor=USB_GO7007SB_MINOR(inode);dbg(close_go7007sb(%d):try to close device,go_minor);go_data=p_go7007sb_data_tablego_minor;if(!go_data)printk(close_go7007sb(%d):invalid device minorn,go_minor);err=-ENODEV;goto out_error;down(&go_data-sem);if(err=send_command(go_data,GO7007SB

39、_CMD_PAUSE)!=0)printk(close_go7007sb(%d):send pause command failed!n,go_minor);up(&go_data-sem);goto out_error;up(&go_data-sem);interruptible_sleep_on_timeout(&go_data-wr_wait_q,GO7007SB_WR_TIMEOUT);down(&go_data-sem);if(signal_pending(current)up(&go_data-sem);err=-EINTR;goto out_error;if(get_state(

40、go_data)!=GO7007SB_STATE_READY)printk(close_go7007sb(%d):send pause command failed!n,go_minor);err=-EAGAIN;up(&go_data-sem);goto out_error;dbg(close_go7007sb:device(%d):device closed!n,go_minor);go_data-isopen=0;up(&go_data-sem);file-private_data=NULL;MOD_DEC_USE_COUNT;out_error:up(&go7007sb_mutex);

41、return err;/* *function:read_go7007sb - to read data from go7007sb device */static ssize_t read_go7007sb(struct file *file,char *buffer, size_t count,loff_t *ppos)struct go7007sb_usb_data*go_data;struct usb_device*dev;ssize_tret;/return valuessize_tbytes_read;/totally bytes readkdev_tgo_minor;intres

42、ult;/function statusintpartial;/actually bytes per readintthis_read;/bytes wanted per readintrd_expire;/how many timeouts will drive us madchar*urb_buf;go_data=file-private_data;down(&go_data-sem);/enter the critical sectiondev=go_data-go7007sb_dev;go_minor=go_data-go7007sb_minor;urb_buf=go_data-in_

43、buf;bytes_read=0;ret=0;rd_expire=GO7007SB_RD_EXPIRE;/* *update the access time */file-f_dentry-d_inode-i_atime=CURRENT_TIME;while(count0)this_read=(count IN_URB_BUF_SZ) ? IN_URB_BUF_SZ : count;result=usb_bulk_msg(dev,usb_rcvbulkpipe(dev,go_data-bulk_in_ep),urb_buf,this_read,&partial,GO7007SB_RD_TIME

44、OUT);/* *now check the return status of submited urb */if(result=-ETIMEDOUT)/timed outif(!partial)/no data readif(-rd_expirebulk_in_ep)err(read_go7007sb(%d):failed to clear endpoint halt condition!,go_minor);ret=result;break;else if(resultsem);return ret ? ret : bytes_read;/* *function:write_go7007sb - to write data to go7007sb device, *commonly,this happens when trying to initialize the device */static ssize_t write_go7007sb(struct file *file,const char *buffer, size_t count,loff_t *ppos)/* *write operation is not

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論