-
打開(kāi)
- int open(struct inode *inode,struct file *filp);
- 模塊使用計(jì)數(shù)加1
- 識(shí)別次設(shè)備號(hào)
- 硬件操作
- 檢查設(shè)備相關(guān)錯(cuò)誤(諸如設(shè)備未就緒或類似的硬件問(wèn)題)
- 如果設(shè)備是首次打開(kāi),則對(duì)其初始化
- 如果有中斷操作,申請(qǐng)中斷處理程序
-
關(guān)閉
- int release(struct inode *inode,struct file *filp);
- 模塊使用計(jì)數(shù)減1
- 釋放由open分配的,保存在filp>private_data里的所有內(nèi)容。
- 硬件操作:
- 如果申請(qǐng)了中斷,則釋放中斷中中斷處理程序。
- 在最后一次關(guān)閉操作時(shí)關(guān)閉設(shè)備。
-
read/write
- ssize_t read(struct file *filp,char __user *buff,sieze_t count,loff_t *offp);
- ssize_t write(struct file *filp,const char __user *buff,size_t count,loff_t *offp);
-
用戶空間和內(nèi)核空間之間的數(shù)據(jù)拷貝過(guò)程
- 不能簡(jiǎn)單的用指針操作或者memcpy來(lái)進(jìn)行數(shù)據(jù)的拷貝
- 用戶空間的數(shù)據(jù)是可以被換出的,會(huì)產(chǎn)生一個(gè)頁(yè)面失效的異常
- 用戶空間的地址無(wú)法在內(nèi)核空間中使用。
-
用戶空間和內(nèi)核空間之間進(jìn)行數(shù)據(jù)拷貝的函數(shù)
- unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);
- unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);
- 讀設(shè)備模板
ssize_t xxx_read(struct *filp,char __user *buf,size_t count,loff_t*f_fpos)
{
copy_to_user(buf,---)
}
-
- 寫(xiě)設(shè)備模板
ssize_t xxx_write(struct file *filp,const char __user *buf,size_t count,loff_t *f_ops)
{
?copy_from_user(--,buf,--);
}
-
ioctl函數(shù)
- 為設(shè)備驅(qū)動(dòng)程序執(zhí)行命令提供了一個(gè)特有的入口點(diǎn)
- 用來(lái)設(shè)置或者讀取設(shè)備的屬性信息
- int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
- cmd參數(shù)的定義
- 不推薦使用0x1,0x2之類的值
- linux對(duì)ioctl()的cmd參數(shù)有特殊的定義
- 設(shè)別號(hào)(type) ? 序列號(hào)(number) 方向(direction) 數(shù)據(jù)尺寸(size)
- 8bit ? ? ? ? ? ? ? ? 8bit ? ? ? ? ? ? ? ? ? ?2bit ? ? ? ? ? ? ? ? 13/14bit
-
構(gòu)造命令編號(hào)的宏:
- _IO(type,nr)用于構(gòu)造物參數(shù)的命令編號(hào);
- _IOR(type,nr,datatype)用于構(gòu)造從驅(qū)動(dòng)程序中讀取數(shù)據(jù)的命令編號(hào);
- _IOw(type,nr,datatype)用于寫(xiě)入數(shù)據(jù)的命令
-
_IOWR(type,nr,datatype)用于雙向傳輸。
- type,nr通過(guò)參數(shù)傳入,而size位字段通過(guò)對(duì)datatype參數(shù)的sizeof獲得。
- ioctl函數(shù)模板
int xxx_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
-----
switch(cmd)
{
case xxx_cmd1:
----
break;
case xxx_cmd2:
----
break;
default:
return -ENOTTY;
}
return 0;
}
- 字符設(shè)備驅(qū)動(dòng)結(jié)構(gòu):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

