1、快速参考
本章介绍了下面符号和头文件.structfile_operations和structfile中的成员的列表这里不重复了.
#include<linux/types.h>
dev_t
dev_t是用来在内核里代表设备号的类型.
intMAJOR(dev_tdev);
intMINOR(dev_tdev);
从设备编号中抽取主次编号的宏.
dev_tMKDEV(unsignedintmajor,unsignedintminor);
从主次编号来建立dev_t数据项的宏定义.
#include<linux/fs.h>
"文件系统"头文件是编写设备驱动需要的头文件.许多重要的函数和数据结构在此定义.
intregister_chrdev_region(dev_tfirst,unsignedintcount,char*name)
intalloc_chrdev_region(dev_t*dev,unsignedintfirstminor,unsignedintcount,char*name)
voidunregister_chrdev_region(dev_tfirst,unsignedintcount);
允许驱动分配和释放设备编号的范围的函数.register_chrdev_region应当用在事先知道需要
的主编号时;对于动态分配,使用alloc_chrdev_region代替.
intregister_chrdev(unsignedintmajor,constchar*name,structfile_operations*fops);
老的(2.6之前)字符设备注册函数.它在2.6内核中被模拟,但是不应当给新代码使用.如果
主编号不是0,可以不变地用它;否则一个动态编号被分配给这个设备.
intunregister_chrdev(unsignedintmajor,constchar*name);
恢复一个由register_chrdev所作的注册的函数.major和name字符串必须包含之前用来注册
设备时同样的值.
structfile_operations;
structfile;
structinode;
大部分设备驱动使用的3个重要数据结构.file_operations结构持有一个字符驱动的方法;
structfile代表一个打开的文件,structinode代表磁盘上的一个文件.
#include<linux/cdev.h>
structcdev*cdev_alloc(void);
voidcdev_init(structcdev*dev,structfile_operations*fops);
intcdev_add(structcdev*dev,dev_tnum,unsignedintcount);
voidcdev_del(structcdev*dev);
cdev结构管理的函数,它代表内核中的字符设备.
#include<linux/kernel.h>
container_of(pointer,type,field);
一个传统宏定义,可用来获取一个结构指针,从它里面包含的某个其他结构的指针.
#include<asm/uaccess.h>
这个包含文件声明内核代码使用的函数来移动数据到和从用户空间.
unsignedlongcopy_from_user(void*to,constvoid*from,unsignedlongcount);
unsignedlongcopy_to_user(void*to,constvoid*from,unsignedlongcount);
在用户空间和内核空间拷贝数据.