摘要:1.驅(qū)動(dòng)即是一個(gè)內(nèi)核模塊,需要模塊初始化函數(shù)module_init()module_exit()2.分配cdevstruct cdev dev;3.初始化cdev并定義file_operation;cdev_init(cdev *,const struct file_operation *);4.獲取設(shè)備號(hào)并注冊(cè)cdev結(jié)構(gòu)alloc_chrdev_region( dev_t *, unsigne
1.驅(qū)動(dòng)即是一個(gè)內(nèi)核模塊,需要模塊初始化函數(shù)
module_init()
module_exit()
2.分配cdev
struct cdev dev;
3.初始化cdev并定義file_operation;
cdev_init(cdev *,const struct file_operation *);
4.獲取設(shè)備號(hào)并注冊(cè)cdev結(jié)構(gòu)
alloc_chrdev_region( dev_t *, unsigned , unsigned , const char *);
cdev_add( struct cdev * ,dev_t , unsigned );
5.編寫(xiě)file_operation結(jié)構(gòu)函數(shù),實(shí)現(xiàn)設(shè)備操作;
open、release、write、read等
copy_to_user()把內(nèi)核空間拷貝到用戶空間
copy_from_user()把用戶空間拷貝到內(nèi)核空間
6.驅(qū)動(dòng)程序的注銷(xiāo);
cdev_del(struct cdev *);
unregister_chrdev_region( dev_t, unsigned );
設(shè)備文件需要自己創(chuàng)建
在/proc/devices下看主設(shè)備號(hào)
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