1.驱动即是一个内核模块,需要模块初始化函数
module_init()
module_exit()
2.分配cdev
struct cdev dev;
3.初始化cdev并定义file_operation;
cdev_init(cdev *,const struct file_operation *);
4.获取设备号并注册cdev结构
alloc_chrdev_region( dev_t *, unsigned , unsigned , const char *);
cdev_add( struct cdev * ,dev_t , unsigned );
5.编写file_operation结构函数,实现设备操作;
open、release、write、read等
copy_to_user()把内核空间拷贝到用户空间
copy_from_user()把用户空间拷贝到内核空间
6.驱动程序的注销;
cdev_del(struct cdev *);
unregister_chrdev_region( dev_t, unsigned );
设备文件需要自己创建
在/proc/devices下看主设备号
Makefile
obj-m:=led.o
KERNELDIR:=/home/jz2440/linux-2.6.22.6 PWD:=$(shell pwd) default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean:
rm -rf *.o *.mod.c *.mod.o *.ko