Linux驱动编程 step-by-step (三) 字符设备中 重要的数据结构
生活随笔
收集整理的這篇文章主要介紹了
Linux驱动编程 step-by-step (三) 字符设备中 重要的数据结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符設備中 重要的數據結構
大部分字符驅動設計三個重要的數據結構
<linux/fs.h>
struct?file_operations
struct?file
struct?inode
?
一、文件操作
?? ? ? ? ? 在之前的一篇文章中已經有介紹了如何去生情字符設備設備號,但是沒有做任何的工作,也就只能寫一個不能工作的字符設備;
struct file_operations 結構域用來連接設備與操作,實現系統調用。
重要字段介紹:
struct?file_operations?{
????struct?module?*owner;//表示擁有這個結構模塊的指針,幾乎所有的驅動都會設置為THIS_MODULE<linux/module.h>
????loff_t?(*llseek)?(struct?file?*,?loff_t,?int);//文件讀寫位置調整函數
????ssize_t?(*read)?(struct?file?*,?char?__user?*,?size_t,?loff_t?*);//從設備讀取數據
????ssize_t?(*write)?(struct?file?*,?const?char?__user?*,?size_t,?loff_t?*);//項設備寫入數據
????unsigned?int?(*poll)?(struct?file?*,?struct?poll_table_struct?*);//查詢文件描述符上的讀取寫入是否被阻塞
????int?(*mmap)?(struct?file?*,?struct?vm_area_struct?*);//將設備內存映射到進程空間
????int?(*open)?(struct?inode?*,?struct?file?*);//對應于打開設備
????int?(*release)?(struct?inode?*,?struct?file?*);//對應于關閉一個設備
????.
????.
????.
};
結構還有很多的操作因為還沒有學會,就沒有多做介紹,(ioctl函數操作在2.6.35之后就變成了unlocked_ioctl、compat_ioctl其實作用也不是很大 也沒有做介紹,以后有機會再做介紹)
當open 與release置為NULL時 就以內核默認的方式進行打開或者關閉,并且不會出錯,
當其他函數沒有被定義時候,應用程序調用會出錯。
下邊是一個最重要的幾個設備操作方法的定義。
struct?file_operations?simple_fops={
????.owner???=?THIS_MODULE,
????.open????=?simple_open,
????.release?=?simple_close,
????.read????=?simple_read,
????.write???=?simple_write,
????.llseek??=?simple_llseek,
????.poll????=?simple_poll,
????.mmap????=?simple_mmap,
};
二、file結構
這里介紹的file結構并不是C語言中的FILE結構,兩者沒有任何關聯,struct file只是一個內核的結構,每一個打開的文件,都會對應一個struct file結構,一個文件可以對應于不同的struct file結構
struct?file?{
????struct?path????f_path;?//文件位置
????const?struct?file_operations????*f_op;.//文件操作符
????spinlock_t????f_lock;?
????atomic_long_t????f_count;
????unsigned?int?????f_flags;//文件標識(O_NONBLOCK,?O_RDONLY等由應用程序傳入)
????fmode_t????f_mode;//文件模式可讀可寫
????loff_t??????f_pos;//文件讀寫位置
????struct?fown_struct????f_owner;
????const?struct?cred???*f_cred;
????struct?file_ra_state??f_ra;
????.
????.
????.
????void???????*private_data;?//most?important!!?私有數據,驅動可以使用它指向任何的數據結構
}; 三、inode結構
linux內核使用inode結構表示一個文件,與file不同,file可以理解為用來表示文件描述符的結構,一個文件可以對應很多的文件描述符,而最后只會指向同一個inode結構
struct?inode?{
????...
????dev_t???????i_rdev;?//保存設備號
????????union?{
????????struct?pipe_inode_info????*i_pipe;
????????struct?block_device???*i_bdev;
????????struct?cdev????*i_cdev;?//指向了struct?cdev結構
????};
????...
};
四、file 結構 與inode結構圖解
<linux/fs.h>
struct?file_operations
struct?file
struct?inode
?
一、文件操作
?? ? ? ? ? 在之前的一篇文章中已經有介紹了如何去生情字符設備設備號,但是沒有做任何的工作,也就只能寫一個不能工作的字符設備;
struct file_operations 結構域用來連接設備與操作,實現系統調用。
重要字段介紹:
struct?file_operations?{
????struct?module?*owner;//表示擁有這個結構模塊的指針,幾乎所有的驅動都會設置為THIS_MODULE<linux/module.h>
????loff_t?(*llseek)?(struct?file?*,?loff_t,?int);//文件讀寫位置調整函數
????ssize_t?(*read)?(struct?file?*,?char?__user?*,?size_t,?loff_t?*);//從設備讀取數據
????ssize_t?(*write)?(struct?file?*,?const?char?__user?*,?size_t,?loff_t?*);//項設備寫入數據
????unsigned?int?(*poll)?(struct?file?*,?struct?poll_table_struct?*);//查詢文件描述符上的讀取寫入是否被阻塞
????int?(*mmap)?(struct?file?*,?struct?vm_area_struct?*);//將設備內存映射到進程空間
????int?(*open)?(struct?inode?*,?struct?file?*);//對應于打開設備
????int?(*release)?(struct?inode?*,?struct?file?*);//對應于關閉一個設備
????.
????.
????.
};
結構還有很多的操作因為還沒有學會,就沒有多做介紹,(ioctl函數操作在2.6.35之后就變成了unlocked_ioctl、compat_ioctl其實作用也不是很大 也沒有做介紹,以后有機會再做介紹)
當open 與release置為NULL時 就以內核默認的方式進行打開或者關閉,并且不會出錯,
當其他函數沒有被定義時候,應用程序調用會出錯。
下邊是一個最重要的幾個設備操作方法的定義。
struct?file_operations?simple_fops={
????.owner???=?THIS_MODULE,
????.open????=?simple_open,
????.release?=?simple_close,
????.read????=?simple_read,
????.write???=?simple_write,
????.llseek??=?simple_llseek,
????.poll????=?simple_poll,
????.mmap????=?simple_mmap,
};
二、file結構
這里介紹的file結構并不是C語言中的FILE結構,兩者沒有任何關聯,struct file只是一個內核的結構,每一個打開的文件,都會對應一個struct file結構,一個文件可以對應于不同的struct file結構
struct?file?{
????struct?path????f_path;?//文件位置
????const?struct?file_operations????*f_op;.//文件操作符
????spinlock_t????f_lock;?
????atomic_long_t????f_count;
????unsigned?int?????f_flags;//文件標識(O_NONBLOCK,?O_RDONLY等由應用程序傳入)
????fmode_t????f_mode;//文件模式可讀可寫
????loff_t??????f_pos;//文件讀寫位置
????struct?fown_struct????f_owner;
????const?struct?cred???*f_cred;
????struct?file_ra_state??f_ra;
????.
????.
????.
????void???????*private_data;?//most?important!!?私有數據,驅動可以使用它指向任何的數據結構
}; 三、inode結構
linux內核使用inode結構表示一個文件,與file不同,file可以理解為用來表示文件描述符的結構,一個文件可以對應很多的文件描述符,而最后只會指向同一個inode結構
struct?inode?{
????...
????dev_t???????i_rdev;?//保存設備號
????????union?{
????????struct?pipe_inode_info????*i_pipe;
????????struct?block_device???*i_bdev;
????????struct?cdev????*i_cdev;?//指向了struct?cdev結構
????};
????...
};
四、file 結構 與inode結構圖解
總結
以上是生活随笔為你收集整理的Linux驱动编程 step-by-step (三) 字符设备中 重要的数据结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux驱动编程 step-by-st
- 下一篇: Linux驱动编程 step-by-st