嵌入式Linux設備驅動開發之:字符設備驅動編程
11.2字符設備驅動編程
1.字符設備驅動編寫流程
設備驅動程序可以使用模塊的方式動態加載到內核中去。加載模塊的方式與以往的應用程序開發有很大的不同。以往在開發應用程序時都有一個main()函數作為程序的入口點,而在驅動開發時卻沒有main()函數,模塊在調用insmod命令時被加載,此時的入口點是init_module()函數,通常在該函數中完成設備的注冊。同樣,模塊在調用rmmod命令時被卸載,此時的入口點是cleanup_module()函數,在該函數中完成設備的卸載。在設備完成注冊加載之后,用戶的應用程序就可以對該設備進行一定的操作,如open()、read()、write()等,而驅動程序就是用于實現這些操作,在用戶應用程序調用相應入口函數時執行相關的操作,init_module()入口點函數則不需要完成其他如read()、write()之類功能。
上述函數之間的關系如圖11.3所示。
圖11.3設備驅動程序流程圖
2.重要數據結構
用戶應用程序調用設備的一些功能是在設備驅動程序中定義的,也就是設備驅動程序的入口點,它是一個在linux/fs.h>中定義的structfile_operations結構,這是一個內核結構,不會出現在用戶空間的程序中,它定義了常見文件I/O函數的入口,如下所示:
structfile_operations
{
loff_t(*llseek)(structfile*,loff_t,int);
ssize_t(*read)(structfile*filp,
char*buff,size_tcount,loff_t*offp);
ssize_t(*write)(structfile*filp,
constchar*buff,size_tcount,loff_t*offp);
int(*readdir)(structfile*,void*,filldir_t);
unsignedint(*poll)(structfile*,structpoll_table_struct*);
int(*ioctl)(structinode*,
structfile*,unsignedint,unsignedlong);
int(*mmap)(structfile*,structvm_area_struct*);
int(*open)(structinode*,structfile*);
int(*flush)(structfile*);
int(*release)(structinode*,structfile*);
int(*fsync)(structfile*,structdentry*);
int(*fasync)(int,structfile*,int);
int(*check_media_change)(kdev_tdev);
int(*revalidate)(kdev_tdev);
int(*lock)(structfile*,int,structfile_lock*);
};
這里定義的很多函數是否跟第6章中的文件I/O系統調用類似?其實當時的系統調用函數通過內核,最終調用對應的structfile_operations結構的接口函數(例如,open()文件操作是通過調用對應文件的file_operations結構的open函數接口而被實現)。當然,每個設備的驅動程序不一定要實現其中所有的函數操作,若不需要定義實現時,則只需將其設為NULL即可。
structinode結構提供了關于設備文件/dev/driver(假設此設備名為driver)的信息,structfile結構提供關于被打開的文件信息,主要用于與文件系統對應的設備驅動程序使用。structfile結構較為重要,這里列出了它的定義:
structfile
{
mode_tf_mode;/*標識文件是否可讀或可寫,FMODE_READ或FMODE_WRITE*/
dev_tf_rdev;/*用于/dev/tty*/
off_tf_pos;/*當前文件位移*/
unsignedshortf_flags;/*文件標志,如O_RDONLY、O_NONBLOCK和O_SYNC*/
unsignedshortf_count;/*打開的文件數目*/
unsignedshortf_reada;
structinode*f_inode;/*指向inode的結構指針*/
structfile_operations*f_op;/*文件索引指針*/
};
linux操作系統文章專題:linux操作系統詳解(linux不再難懂)linux相關文章:linux教程
評論