原文转自(http://www.embedhq.org/html/jsbw/2011/0214/1066.html)
//=======================字符设备驱动模板开始 ===========================//
#define CHAR_DEV_DEVICE_NAME "char_dev" // 设备名
struct class *char_dev _class; // class结构用于自动创建设备结点
static int major = 0;/* 0表示动态分配主设备号,可以设置成未被系统分配的具体的数字。*/
static struct cdev char_dev_devs;// 定义一个cdev结构
// 设备建立子函数,被char_dev_init函数调用
static void char_dev_setup_cdev(struct cdev *dev, int minor, struct file_operations *fops)
{
int err, devno = MKDEV(major, minor);
cdev_init(dev, fops);
dev->owner = THIS_MODULE;
dev->ops = fops;
err = cdev_add(dev, devno, 1);
if( err )
{
printk(KERN_NOTICE "Error %d adding char_dev %d\n", err, minor);
}
}
// file_operations 结构体设置,该设备的所有对外接口在这里明确,此处只写出了几常用的
static struct file_operations char_dev_fops =
{
.owner = THIS_MODULE,
.open = char_dev_open, // 打开设备
.release = char_dev_release, // 关闭设备
.read = char_dev_read, // 实现设备读功能
.write = char_dev_write, // 实现设备写功能
.ioctl = char_dev_ioctl, //实现设备控制功能
};
// 进行初始化设置,打开设备,对应应用空间的open 系统调用
int char_dev_open (struct inode *inode, struct file *filp)
{
... // 这里可以进行一些初始化
return 0;
}
// 释放设备,关闭设备,对应应用空间的close 系统调用
static int char_dev_release (struct inode *node, struct file *file)
{
... // 这里可以进行一些资源的释放
return 0;
}
// 实现读功能,读设备,对应应用空间的read 系统调用
ssize_t char_dev_read (struct file *file,char __user *buff,size_t count,loff_t *offp)
{
...
return 0;
}
// 实现写功能,写设备,对应应用空间的write 系统调用
ssize_t char_dev_write(struct file *file,const char __user *buff,size_t count,loff_t *offp)
{
...
return 0;
}
// 实现主要控制功能,控制设备,对应应用空间的ioctl 系统调用
static int char_dev _ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
...
return 0;
}
// 设备初始化
static int char_dev_init(void)
{
int result;
dev_t dev = MKDEV(major, 0);
if( major )
{
// 给定设备号注册
result = register_chrdev_region(dev, 1, CHAR_DEV_DEVICE_NAME);
}
else
{
// 动态分配设备号
result = alloc_chrdev_region(&dev, 0, 1, CHAR_DEV_DEVICE_NAME);
major = MAJOR(dev);
}
char_dev_setup_cdev(&char_dev_devs, 0, &char_dev_fops);
printk("The major of the char_dev device is %d\n", major);
//==== 有中断的可以在此注册中断:request_irq,并要实现中断服务程序 ===//
// 创建设备结点
char_dev _class = class_create(THIS_MODULE,"ad_class");
if (IS_ERR(char_dev _class))
{
printk("Err: failed in creating class.\n");
return 0;
}
device_create(char_dev_class, NULL, dev, NULL, "char_dev");
return 0;
}
// 设备注销
static void char_dev_cleanup(void)
{
device_destroy(adc_class,dev);
class_destroy(adc_class);
cdev_del(&char_dev_devs);//字符设备的注销*/
unregister_chrdev_region(MKDEV(major, 0), 1);//设备号的注销
//======== 有中断的可以在此注销中断:free_irq ======//
printk("char_dev device uninstalled\n");
}
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:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
//========================= Makefile结束 =============================//
make编译后生成char_dev.ko,控制台输入加载和卸载命令,还可以使用lsmod查看已经加载的模块信息。
insmod char_dev.ko #加载驱动,会执行module_init中的语句
rmmod char_dev #卸载驱动,会执行module_exit中的语句
分享到:
相关推荐
这个是linux2.6.32内核源码,没有做任何修改。可以放心下载。
linux 2.6.32内核目录树及结构
Linux-2.6.32内核在mini2440上的移植 主要讲述yaffs2文件系统移植
主要涉及到mini2440中关于linux2.6.32的内核的移植,及USB加载,和网卡驱动
该程序是CY68013基于Linux 2.6.32内核的驱动源码,程序基于源码中usb-skeleton.c框架进行修改,能够实现33MB/S的传输速度
linux-2.6.32内核源代码之Part02
定制linux内核(linux2.6.32).doc
定制linux内核(linux2.6.32).pdf
定制linux内核(linux2.6.32)汇编.pdf
Mini2440嵌入式linux内核定制剖析(linux2.6.32),好资料,免费奉送!免费的才是真正的共享!
2.6内核在编译驱动程序之前一定要构建好内核树
all_root只能用于2.4版本。这里改到适用于2.6.32版本。 使用一个脚本获得sys_call_table地址,并传给可加载模块,可加载模块替换系统调用getuid,全给root权限。
Linux 2.6内核的嵌入式系统应用.pdf
linux-2.6.32内核源代码之Part04
linux-2.6.32内核源代码之Part05
kdb补丁适用于linux-2.6.32,官方原版的kdb补丁有编译错误,该补丁修复了编译错误,可以直接应用
linux exp aaa
linux2.6.32补丁
http://blog.csdn.net/lsp1100/article/details/13169055