- 论坛徽章:
- 0
|
linux2.6内核中字符设备驱动注册和之前版本的的确有些不一样。看到网上的scull代码,直接拿过来编译虽然通过,但怎么也无法达到想要的结构。
hello_read和hello_write事件无法触发。虽然用register_chardev()注册了。
在网上找到一篇相关文章,贴上他的代码如下:
/*chardev.c*/
#include linux/kernel.h>
#include linux/fs.h> /*for file-f_op*/
#include linux/module.h>
#include asm/uaccess.h> /*for copy_to_user()*/
#include linux/cdev.h> /*for cdev ,cdev_init,cdev_add....*/
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Helight");
#define DP_MAJOR 250 /*主设备号*/
#define DP_MINOR 0 /*次设备号*/
//
// 函数声明
//
static int char_open(struct inode *,struct file *); /* 打开字符设备 */
static int char_release(struct inode *,struct file *); /* 释放字符设备 */
static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *); /* 从kernel读数据 */
static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*); /* 写数据到内核 */
//
// 全局变量
//
static int chropen; /*字符设备是否打开*/
struct cdev *chardev; /*定义一个字符设备*/
static int len;
static const struct file_operations char_ops = {
.read = char_read,
.write = char_write,
.open = char_open,
.release = char_release,
};
static int __init char_init(void)
{
dev_t dev;
printk(KERN_ALERT"Initing......\n");
/* 自动分配一个设备号 */
dev=MKDEV(DP_MAJOR,DP_MINOR);
chardev = cdev_alloc();
if( NULL == chardev )
{
return -1;
}
/* 注册设备号 */
if( register_chrdev_region( dev, 10, "chardev" ) )
{
printk(KERN_ALERT"Register char dev error\n");
return -1;
}
chropen=0;
len=0;
/* 初始化设备结构 */
cdev_init( chardev, &char_ops );
/* 添加设备结构到字符设备 */
if(cdev_add(chardev,dev,1))
{
printk(KERN_ALERT"Add char dev error\n");
}
return 0;
}
static int char_open( struct inode *inode, struct file *file )
{
if(chropen==0)
{
chropen++;
}
else
{
printk(KERN_ALERT"Another process open the char device\n");
return -1;
}
try_module_get(THIS_MODULE);
return 0;
}
static int char_release(struct inode *inode,struct file *file)
{
chropen--;
module_put(THIS_MODULE);
return 0;
}
static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
if(length12)
{
if(!copy_to_user(buffer,"hello world!",length))
{
return 0;
}
}
else
{
if(!copy_to_user(buffer,"hello world!",strlen("hello world!")))
{
return 0;
}
}
return -1;
}
static int char_write(struct file *filp,const char __user *buffer,size_t length,loff_t *offset)
{
return 0;
}
static void __exit module_close(void)
{
len=0;
printk(KERN_ALERT"Unloading..........\n");
/* 卸载字符设备 */
unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
/* 释放字符设备结构体 */
cdev_del(chardev);
}
module_init(char_init);
module_exit(module_close);
test.c程序:
#include stdio.h>
#include stdlib.h>
#include sys/types.h>
#include sys/stat.h>
#include unistd.h>
#include fcntl.h>
#include string.h>
int main(void)
{
int testdev;
int i,rf=0;
char buf[15];
memset(buf, 0, sizeof(buf));
testdev = open("/dev/chardev0",O_RDWR);
if ( testdev == -1 )
{
perror("open\n");
exit(0);
}
rf=read(testdev,buf,12);
if(rf0)
{
perror("read error\n");
}
printf("R:%s\n",buf);
close(testdev);
return 0;
}
1.编译好,scull.ko.
2.先创建一个字符设备节点:
mknod /dev/chardev0 255 0
3.将scull.ko模块加载到内核中:insmod scull.ko
4.编译test.c文件:
gcc -o test test.c
5.运行./test。即便可看到结果。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93914/showart_1884604.html |
|