基本的函数与结构
函数
//驱动注册,注销函数
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等函数都会如此的调用。