Linux字符驱动程序的基本结构与函数
生活随笔
收集整理的這篇文章主要介紹了
Linux字符驱动程序的基本结构与函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本的函數與結構
函數
//驅動注冊,注銷函數 module_init(void (*func)); module_exit(void (*func));這兩個函數,將會在執行insmod和rmmod時對傳入的函數進行回調。
intregister_chrdev(unsignedintmajor,constchar*name, struct file_operations*fops);其中參數major如果等于0,則表示采用系統動態分配的主設備號;不為0,則表示靜態注冊。
int unregister_chrdev(unsignedintmajor,constchar*name);注銷字符設備可以使用unregister_chrdev函數。
//打開文件 int open(const char *pathname, int flags, mode_t mode);//讀取文件到buf int read(int fd, const void *buf, size_t length);//寫buf到文件 int write(int fd, const void *buf, size_t length);//ioctl是設備驅動程序中對設備的I/O通道進行管理的函數 //用于應用函數到驅動程序的傳參 int ioctl(int fd, ind cmd, …);結構
struct file_operations {struct module *owner;ssize_t (*read)(struct file *,charchar *, size_t, loff_t *);//從設備同步讀取數據 ssize_t (*write)(struct file *,const charchar *, size_t, loff_t *); //向設備同步寫入數據int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);//執行設備IO控制命令 int (*open) (struct inode *, struct file *);//打開 ... }應用程序函數與驅動程序函數的關系
當在應用程序中用open打開某個設備時,在file_operations 結構體的open指向的成員函數,將會被回調;相應的,read,write,ioctl等函數都會如此的調用。
總結
以上是生活随笔為你收集整理的Linux字符驱动程序的基本结构与函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux驱动入门基础基础知识
- 下一篇: Linux LED驱动源码简析