版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sophisticated_/article/details/83008376
管理设备号
#include<linux/types.h>
dev_t
内核中用来表示设备编号的数据类型,32位,12位用来表示主设备号,其余20位表示次设备号
int MAJOR(dev_t dev);
int MINOR(dev_t dev);
这两个宏从设备编号中抽取出主、次设备号
dev_t MKDEV(unsigned int major, unsigned int minor);
这个宏由主、次设备号构造一个dev_t数据项
#include<linux/fs.h>
文件系统头文件,它是编写设备驱动程序必须的头文件,声明了很多重要的函数和结构
int register_chdev_region(dev_t first, unsigned int count, char *name);
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);
void unregister_chrdev_region(dev_t first, unsigned int count);
提供给驱动程序分配和释放设备编号的函数。在期望主设备号预先知道的情况下,应调用register_chdev_region;而对于动态分配,使用 alloc_chrdev_region
struct file_operations;
struct file;
struct inode;
file_operations结构保存字符设备驱动程序的方法;struct file表示一个打开的文件;struct inode表示磁盘上的文件
管理字符设备
#include<linux/cdev.h>
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *dev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);
用来管理cdev数据结构的函数,内核中用该结构表示字符设备
#include<kernel.h>
container_of(pointer, type, field);
一个方便的宏,它可用于从包含在某个结构中的指针获得结构本身的指针
#include<asm/uaccess.h>
该头文件声明了在内核代码和用户空间之间移动数据的函数
unsigned long copy_from_user(void *to, const void *from, unsigned long count);
unsigned long copy_to_user(void *to, const void *from, unsigned long count);
在用户空间和内核空间之间拷贝数据