
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、linux字符設備驅動/*linux字符設備驅動源代碼sd.c*/ ilude linux/init.h /*模塊頭文件*/ include linux/mole.h include linux/types.h /*dev_t頭文件*/ include linux/kdev_t.h /*major和minor宏頭文件*/ include linux/fs.h /*register_ch_region等函數 _operations結構體*/ include linux/cdev.h /*suct cdev結構體*/ include asm/uaccess.h /*copy_to_user函數*/
2、 define device_name sc /*定義設備名*/ define device_major 250 struct cdev my_cdev; int scdd_open(struct inode *inode,structfile *filp) return0; int scdd_close(struct inode *inode,structfile *filp) return0; ssize_t scdd_read(struct file *filp,char_user *buff,size_t size,loff_t *offp) intleft; chardata=1;
3、for(left=size;left left-) /*拷貝數據到用戶空間*/ copy_to_user(buff, data,1); buff returnsize; ssize_t scdd_(struct file *filp,char_user *buff,size_t size,loff_t *offp) return0; /*file_operations結構體*/ struct file_operations scdd_fops= .owner=this_module, .read=scdd_read, .write=scdd_write, .open=scdd_open, .r
4、elease=scdd_close, ; ic int _init scdd_init(vo) /*模塊初始化函數*/ intsmajor; smajor=device_major; dev_tdev_n=mkdev(smajor,0); /*申請設備號*/ if(!register_chrdev_region(dev_n,1,device_name) /*靜態(tài)申請*/ printk( registerccessn ee gotoregister_error; /*else /*動態(tài)申請*/ /*alloc_chrdev_region( dev_n,0,1,device_name); smaj
5、or=major(dev_n); */ /*初始化cdev結構體*/ cdev_init( my_cdev, scdd_fops); my_cdev.owner=this_module; my_cdev.ops= scdd_fops; /*注冊字符設備*/ cdev_add( my_cdev,dev_n,1); return0; register_error: unregister_chrdev_region(mkdev(device_major,0),1); return0; static void _it scdd_exit(void) /*模塊卸載函數*/ cdev_del( my_cd
6、ev); unregister_chrdev_region(mkdev(device_major,0),1); module_init(scdd_init); module_exit(scdd_exit); module_license( dual bsd/gpl 這個程序只是容易演示字符注冊的一個完整過程,并不帶有復雜的操作,調用read時向用戶空間寫全1 要點: 1.設備號,主設備號用來標識設備所對應的驅動程序,同一個驅動程序可以對應多個設備,次設備號就是用來區(qū)別采納同一個驅動程序的不同設備文件。 內核中采納dev_t來描述設備號,其實dev_t實質為unsign int類型,其中高12位
7、為主設備號,低20位為次設備號,其定義在linux/types.h中 從dev_t中分解出主設備號和次設備號 major(dev_t dev) minor(dev_t dev) 將主設備號和次設備號轉換成dev_t類型 mkdev(intmajor,int minor) 2.申請設備號 靜態(tài)申請,函數定義在linux/fs.h中 int register_chrdev_region(dev_t from,unsigned count,const char *name) from是申請設備號起始 ,from次設備號常常被置成0,count是所哀求延續(xù)設備號個數,name是該設備號范圍關聯的名稱,
8、它將浮現在/proc/device和sysfs中。 分配勝利返回0,錯誤狀況下,將返回一個負的錯誤碼,并且不能用法所哀求的設備號。 動態(tài)申請 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name) 申請的設備號將保存在dev中,baseminor為所申請的第一個次設備號 無論哪種方式申請設備號,不用法時要釋放設備號 void unregister_chrdev_region(dev_t first,unsigned int count) 3.三個重要的結構 struct
9、file_operations struct file struct inode struct file_operations一個函數指針集合,定義能在設備上舉行的操作,常用的有: int (*open)(structinode *inode,stuuct file *filep) void (*release)(structinode *inode,struct file *filep) ssize_t(*read)(struct file *filep,char _user *buff,size_t size,loff_t *offp) ssize_t(*write)(struct fil
10、e *filep,char _user *buff,size_t seze,loff_t * offp) int(*ioctl)(struct inode *inode,struct file *filep,unsigned int cmd,unsigned longarg) off_t(*llseek)(struct file *filp,loff_t,int) struct file表示一個打開的文件,一個文件被打開十次,則有10個structfile結構 struct file結構成員有: mode_t f_mode 文件模式,例如:可讀可寫 lofft_t f_pos 當前讀/寫位置
11、struct file_operations *f_op 與文件相關操作,執(zhí)行open操作時對這個指針賦 void *private_data在open時置為null,用于跨系統(tǒng)調用時保存十分實用的資源 struct inode用來記錄文件的物理上的信息,一個文件被打開10次,但惟獨一個structinode結構,struct inode結構成員如下: dev_t i_rdev 用來保存設備號 struct cdev*i_cdev 指向字符設備結構struct cdev的指針 4.字符設備的注冊 內核用法struct cdev結構表示字符設備,定義在linux/cdev.h中。 初始化cdev
12、結構體有兩種方式 靜態(tài): struct cdev my_cdev; cdev_init( my_cdev, fops); my_cdev.owner = this_module; 動態(tài)(就是通過kmalloc去申請cdev結構): struct cdev *my_cdev = cdev_alloc(); my_cdev- ops= my_fops; my_cdev.owner= this_module; 注冊 int cdev_add(struct cdev *dev,dev_t num,unsigned int count) num是設備號,count常常取1 注銷 void cdev_del(struct cd
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論