目录
申请设备号函数
返回值 int
register_chrdev(unsigned int major,
const char * name,
const struct file_operations * fops)
参数1:设备号
参数2:设备描述字符
参数3:文件操作结构体
创建文件信息结构体
返回值 struct class * cls
struct class * class_create(owner,name);
参数1:拥有者,一般为(THIS_MODULE)
参数2:描述信息
创建设备节点函数
返回值 struct class * cls
device_create( struct class * class,
struct device * parent,
dev_t devt,
void * drvdata,
const char * fmt, ...)
参数1:设备文件信息结构体
参数2:继承的设备文件结构体,一般为NULL
参数3:设备号
参数4:私有数据
参数5:设备文件名名
地址映射
返回值 一般为相应的PIN对应的物理地址
void * ioremap(cookie,size);
参数1:物理地址
参数2:映射内容的大小
获取设备节点
返回值 设备节点:struct device_node * node
of_find_node_by_path(const char *path);
参数:在内核中的设备节点
从节点中获取到中断号
返回值 中断号:unsigned int irqno
irq_of_parse_and_map(struct device_node *dev,int index);
参数1:设备节点
参数2:索引号
申请中断操作
int request_irq(irqno,key_interrupt,IRQF_TRIGGER_FALLING,"key_interrupt",NULL);
参数1:中断号
参数2:函数指针,也就是处理中断的函数
参数3:处理方式标志
参数4:中断的描述信息
参数5:参数2的函数参数
创建等待队列
创建等待队列头:wait_queue_head_t head;
init_waitqueue_head(&head);
参数:等待队列头
阻塞等待
wait_event_interruptible(wq,condition);
参数1:等待队列头
参数2:是否等待的条件
阻塞唤醒
wake_up_interruptible(&head);
参数:等待队列头
tasklet任务队列
void tasklet_init( struct tasklet_struct * t,
void(* func)(unsigned long),
unsigned long data )
参数1:
任务队列头节点
参数2:下半部分的实现逻辑函数
参数3:参数2函数的参数
启动任务队列
tasklet_schedule(struct tasklet_struct * t);
参数:任务队列头
workqueue工作队列
INIT_WORK(struct work_struct *work,void (*work_func_t)(struct work_struct *work));
参数1:初始化的工作队列对象(头节点)
参数2:工作队列下半部分的实现逻辑函数
启动工作队列
schedule_work(struct work_struct *work);
参数:工作队列指针结构体
__exit 函数内容函数
//映射释放
iounmap(映射的虚拟内存地址);
//释放设备文件
void device_destroy(struct class * class,dev_t devt);
//释放文件信息结构体
void class_destroy(struct class * cls);
//释放设备号
void unregister_chrdev(unsigned int major,const char * name);
//释放中断号
void free_irq(unsigned int irq,void * dev_id)
参数1:中断号
参数2:与申请中断第五个参数保持一致
总线bus:
struct bus_type:总线对象,描述一条总线,管理device、driver,进行匹配
struct bus_type
{
const char*name;//总线名字
int (*match)(struct device *dev, struct device_driver *drv);//总线调用匹配设备和驱动,返回值就表示匹配成功与否
};
注册总线:
int bus_register(struct bus_type * bus);
参数:struct bus_type * bus:总线对象
注销总线:
void bus_unregister(struct bus_type * bus);
驱动driver:
struct device_driver :驱动对象,描述一个驱动,对驱动进行说明
struct device_driver {
const char*name; //驱动的名字
struct bus_type*bus; //总线对象,表示要把驱动注册到哪条总线
int (*probe) (struct device *dev); //如果匹配成功,则调用该驱动的probe函数,创建驱动(申请设备号...)
int (*remove) (struct device *dev); //当设备对象和驱动对象移除总线时会调用
};
注册驱动到总线:
int driver_register(struct device_driver * drv);
从总线上注销:
void driver_unregister(struct device_driver * drv);
设备device:
device {
struct kobject kobj;//所有对象的父类
const char*init_name;//设备名
struct bus_type*bus;//总线对象,表示要把设备注册到哪条总线
void*platform_data;//自定义数据,指向任意类型,可以存储设备信息
void(*release)(struct device *dev);//设备对象从总线移除时会调用
};
注册设备到总线:
int device_register(struct device * dev);
从总线上注销:
void device_unregister(struct device * dev);
struct