Linux 2.6.32内核字符设备驱…
引言:Linux驱动中,字符设备的设计一般会占产品驱动开发的90%以上,作者根据驱动开发的实际经验,总结了一个标准的字符设备驱动的模板,仅供参考。
//=======================字符设备驱动模板开始
#define
CHAR_DEV_DEVICE_NAME
struct
class *char_dev _class;
static int major = 0;
static
struct cdev char_dev_devs;//
//
static void char_dev_setup_cdev(struct cdev *dev, int minor, struct
file_operations *fops)
{
}
//
static struct file_operations char_dev_fops =
{
};
//
int char_dev_open (struct inode *inode, struct file
*filp)
{
...
return 0;
}
//
static int char_dev_release (struct inode *node, struct file *file)
{
...
return 0;
}
//
ssize_t char_dev_read (struct file *file,char __user *buff,size_t count,loff_t *offp)
{
return 0;
}
//
ssize_t char_dev_write(struct file *file,const char __user *buff,size_t count,loff_t *offp)
{
}
//
static int char_dev _ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
}
//
static int char_dev_init(void)
{
//
else
//
//====
//
char_dev _class = class_create(THIS_MODULE,"ad_class");
if (IS_ERR(char_dev _class))
{
}
device_create(char_dev_class, NULL, dev, NULL,
"char_dev");
}
//
static void char_dev_cleanup(void)
{
device_destroy(adc_class,dev);
class_destroy(adc_class);
//========
}
module_init(char_dev_init);//
module_exit(char_dev_cleanup);//模块注销接口
//
MODULE_AUTHOR("www.embedhq.org");
MODULE_LICENSE("GPL");
//====================
用Makefile模板编译,Makefile如下:
//======================= Makefile开始
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /your/target/source/directory/
KERNELDIR ?= /opt/kernal/linux-2.6.32.10/
PWD := $(shell pwd)
modules:
modules_install:
clean:
.PHONY: modules modules_install clean
//========================= Makefile结束
make编译后生成char_dev.ko,控制台输入加载和卸载命令,还可以使用lsmod查看已经加载的模块信息。
insmod char_dev.ko #加载驱动,会执行module_init中的语句
rmmod char_dev
(原文出处:http://www.embedhq.org/,