当前位置: 技术问答>linux和unix
新手问题请教
来源: 互联网 发布时间:2016-08-23
本文导语: 在linux内核加栽下面的模块: #include #include #include #include #include #include #include #include #define PINFO(fmt,args...) printk(KERN_INFO "[%s]" fmt,__FUNCTION__ ##args) #define SIZE 100 int major=0; int minor=0; unsigned int devno; struct...
在linux内核加栽下面的模块:
#include
#include
#include
#include
#include
#include
#include
#include
#define PINFO(fmt,args...) printk(KERN_INFO "[%s]" fmt,__FUNCTION__ ##args)
#define SIZE 100
int major=0;
int minor=0;
unsigned int devno;
struct test_driver{
char buf[SIZE];
int irq;
struct cdev my_cdev;
int cur_size;
struct fasync_struct *fa;
};
struct test_driver mdr;
int test_fasync(int fd, struct file *flip, int on);
int test_open (struct inode *inod, struct file *filp)
{
printk("init test drivern");
struct test_driver *md=container_of(inod->i_cdev,struct test_driver,my_cdev);
filp->private_data=md;
return 0;
}
int test_close(struct inode *inod, struct file *filp)
{
printk("close test drivern");
filp->f_pos=0;
test_fasync(-1, filp, 0);
return 0;
}
ssize_t test_read (struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
remain=copy_to_user(buf,md->buf+filp->f_pos,count);
ret=count-remain;
md->cur_size-=ret;
return ret;
}
ssize_t test_write (struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
remain=copy_from_user(md->buf+filp->f_pos,buf,count);
ret=count-remain;
md->cur_size+=ret;
kill_fasync(&md->fa,SIGIO,POLL_IN);
return ret;
}
int test_fasync(int fd, struct file *flip, int on)
{
struct test_driver *md=flip->private_data;
return fasync_helper(fd,flip,on,&md->fa);
}
struct file_operations f_ops=
{
.open=test_open,
.release=test_close,
.read=test_read,
.write=test_write,
.fasync=test_fasync,
};
int init_test(void)
{
if(major){
devno=MKDEV(major,minor);
register_chrdev_region(devno,1,"new test driver");
}
else{
alloc_chrdev_region(&devno,0,1,"alloc devno");
major=MAJOR(devno);
minor=MINOR(devno);
printk("major is %d,minor is %dn",major,minor);
}
cdev_init(&mdr.my_cdev,&f_ops);
cdev_add(&mdr.my_cdev,devno,1);
PINFO("init test drivern");
return 0;
}
void exit_test(void)
{
cdev_del(&mdr.my_cdev);
unregister_chrdev_region(devno,1);
PINFO("exit test drivern");
}
module_init(init_test);
module_exit(exit_test);
然后让这个应用程序在后台运行:
#include
#include
#include
#include
#include
#include
int fd;
char buf[20]={0};
static int flag=0;
void handler(int num)
{
if(num==SIGIO)
{
int ret;
ret=read(fd,buf,5);
printf("app read,%d,%sn",ret,buf);
flag=1;
return;
}
}
int main(void)
{
struct sigaction sa;
fd=open("/dev/test",O_RDWR);
if(fd
#include
#include
#include
#include
#include
#include
#include
#include
#define PINFO(fmt,args...) printk(KERN_INFO "[%s]" fmt,__FUNCTION__ ##args)
#define SIZE 100
int major=0;
int minor=0;
unsigned int devno;
struct test_driver{
char buf[SIZE];
int irq;
struct cdev my_cdev;
int cur_size;
struct fasync_struct *fa;
};
struct test_driver mdr;
int test_fasync(int fd, struct file *flip, int on);
int test_open (struct inode *inod, struct file *filp)
{
printk("init test drivern");
struct test_driver *md=container_of(inod->i_cdev,struct test_driver,my_cdev);
filp->private_data=md;
return 0;
}
int test_close(struct inode *inod, struct file *filp)
{
printk("close test drivern");
filp->f_pos=0;
test_fasync(-1, filp, 0);
return 0;
}
ssize_t test_read (struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
remain=copy_to_user(buf,md->buf+filp->f_pos,count);
ret=count-remain;
md->cur_size-=ret;
return ret;
}
ssize_t test_write (struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
struct test_driver *md=filp->private_data;
int remain,ret;
remain=copy_from_user(md->buf+filp->f_pos,buf,count);
ret=count-remain;
md->cur_size+=ret;
kill_fasync(&md->fa,SIGIO,POLL_IN);
return ret;
}
int test_fasync(int fd, struct file *flip, int on)
{
struct test_driver *md=flip->private_data;
return fasync_helper(fd,flip,on,&md->fa);
}
struct file_operations f_ops=
{
.open=test_open,
.release=test_close,
.read=test_read,
.write=test_write,
.fasync=test_fasync,
};
int init_test(void)
{
if(major){
devno=MKDEV(major,minor);
register_chrdev_region(devno,1,"new test driver");
}
else{
alloc_chrdev_region(&devno,0,1,"alloc devno");
major=MAJOR(devno);
minor=MINOR(devno);
printk("major is %d,minor is %dn",major,minor);
}
cdev_init(&mdr.my_cdev,&f_ops);
cdev_add(&mdr.my_cdev,devno,1);
PINFO("init test drivern");
return 0;
}
void exit_test(void)
{
cdev_del(&mdr.my_cdev);
unregister_chrdev_region(devno,1);
PINFO("exit test drivern");
}
module_init(init_test);
module_exit(exit_test);
然后让这个应用程序在后台运行:
#include
#include
#include
#include
#include
#include
int fd;
char buf[20]={0};
static int flag=0;
void handler(int num)
{
if(num==SIGIO)
{
int ret;
ret=read(fd,buf,5);
printf("app read,%d,%sn",ret,buf);
flag=1;
return;
}
}
int main(void)
{
struct sigaction sa;
fd=open("/dev/test",O_RDWR);
if(fd