当前位置: 技术问答>linux和unix
怎么这个驱动刚加载就说被杀死了?
来源: 互联网 发布时间:2016-11-16
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define LEN 1024 struct scull{ wait_queue_head_t r_que; wait_queue_head_t w_que; struct semaphore sem; struct cdev c...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LEN 1024
struct scull{
wait_queue_head_t r_que;
wait_queue_head_t w_que;
struct semaphore sem;
struct cdev cdev;
char buffer[LEN];
dev_t devno;
int count;
int major;
int minor;
};
struct scull *device;
static int com_read(struct file *filp,char __user *buf,size_t count,loff_t *pos)
{
wait_event_interruptible(device->r_que,device->countsem);
copy_to_user(buf,device->buffer,count);
device->count=0;
up(&device->sem);
}
static int com_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos)
{
down_interruptible(&device->sem);
if(device->count>=LEN)
wake_up_interruptible(&device->r_que);
device->count+=count;
copy_from_user(device->buffer+device->count,buf,count);
up(&device->sem);
return count;
}
static int com_open(struct inode *inode,struct file *filp)
{
return 0;
}
static int com_release(struct inode *inode,struct file *filp)
{
return 0;
}
struct file_operations f_ops=
{
.owner=THIS_MODULE,
.read=com_read,
.write=com_write,
.open=com_open,
.release=com_release,
};
void set_cdev(void)
{
int err;
cdev_init(&device->cdev,&f_ops);
device->cdev.owner=THIS_MODULE;
device->cdev.ops=&f_ops;
err=cdev_add(&device->cdev,device->devno,1);
if(err)
printk(KERN_INFO"Error %d adding dev n",err);
}
static int init(void)
{
int ret;
ret=alloc_chrdev_region(&device->devno,0,1,"block_file");
if(retmajor=MAJOR(device->devno);
device->minor=MINOR(device->devno);
init_waitqueue_head(&device->r_que);
init_waitqueue_head(&device->w_que);
init_MUTEX(&device->sem);
device->count=0;
set_cdev();
return 0;
}
static int exit(void)
{
kfree(device);
unregister_chrdev_region(device->devno,1);
cdev_del(&device->cdev);
return 0;
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init);
module_exit(exit);
|
你都还没为你的device指针分配内存就拿到alloc_chrdev_region这个函数里用,能不挂掉吗