Linux内核提供了一组接口用于操作机器上的中断状态,提供了申请与释放系统中观、使能和评比中断的功能,都是与体系结构相关,可以在<asm/system.h>和《asm/irq.h》
二、字符设备驱动结构
编写一个使用的字符设备驱动之前,需要了解字符设备设计的一些模式以及相应的数据结构。例如,主、次设备号。
1、设备号;主设备号表示设备的烈性,与一个确定的驱动对应,次设备号由内核使用。内核用dev_t 《linux/types.h》来保存设备编号;我们可以使用两个宏来获得设备的主、次设别编号:MAJOR(dev_t dev_id) MINOR(dev_t dev_id); 将主次设备号转换为dev_t类型可以使用下面的宏: MKDEV(int major, int minor);
2、分配设备号; 建立字符设备前,首先需要申请设备号,分别为静态申请和动态申请两种方法, 都包含在头文件中,
int register_chrdev_region(dev_t first, unsigned int count, char *name); firsr 为设备编号起始范围,count是所请求的连续设备编号的个数,
int alloc_chrdev_region(dev_t *dev, unsigned firstminor, int count, char *name); dev_t 用于保存申请成功后第一个动态分配的设备号,fristminor是请求使用的第一个次设备号,其余参数一样;
释放:void unregister_chrdev_region(dev_t from, unsigned count);
3、设备号分配模型
static int mem_major =251; //一般定义为全局变量
int result;
dev_t devno=MKDEV(men_major, 0);
if(mem_major)
result= register_chrdev_region(devno,1, “mymem”);
else{ result = alloc_chrdev_region(&devno, 0, 1, "mymem"); mem_major=MAJOR(devno); }
if(result<0) return result;
字符设备驱动中涉及file_operation数据结构,file结构, inode结构着3个重要的数据结构;
字符设备注册的详细步骤; 设备号, 分配设备号, 定义并初始化file_operation结构体;
file_operations 结构体用于连接设备号和驱动程序的操作。该结构体内部包含一组函数指针,这些函数用来实现系统调用;通常需要注册如下函数
struct module *owner :用来指向拥有该结构体的模块
ssize_t read(struct file *filp, char __user *buf, size_t mount, loff_t *f_ops);用来读取设备数据,filp为文件属性结构体指针,buf为用户态函数使用的字符内存缓冲,count为要读取的数据数;f_ops为文件指针的偏移量;ssize_t write(struct file *filp. const char __user *buf,size _t count, loff_t *f_ops)用来向设备输入数据;
int open(struct inode *inode, struct file *):用来打开一个设备;
int release(struct inode *inode , struct file *) 关闭设备;
内核内部使用stuct cdev结构来表示字符设备,