当前位置: 技术问答>linux和unix
一个字符驱动的问题
来源: 互联网 发布时间:2016-04-11
本文导语: 我自己写了一个字符驱动~~~,就是按照LINUX 驱动开发第三版自己改写了的。 但是在用CAT输出字符的时候,却不停的输出停不下来。 请问是什么问题? 在线等~~~ #include #include #include #include #include #incl...
我自己写了一个字符驱动~~~,就是按照LINUX 驱动开发第三版自己改写了的。
但是在用CAT输出字符的时候,却不停的输出停不下来。
请问是什么问题?
在线等~~~
但是在用CAT输出字符的时候,却不停的输出停不下来。
请问是什么问题?
在线等~~~
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DATA_SIZE 1000
int pan_major=253;
module_param(pan_major,int,S_IRUGO);
int pan_minor=0;
module_param(pan_minor,int,S_IRUGO);
struct pan_dev{
void *data;
struct cdev cdev;
};
struct pan_dev *pan_device;
MODULE_LICENSE("Dual BSD/GPL");
ssize_t pan_read(struct file *filp,char __user *buf,
size_t count,loff_t *f_pos){
struct pan_dev *dev = filp->private_data;
ssize_t retval;
// if(count > DATA_SIZE){count = DATA_SIZE;}
//else{count = (long)*f_pos;}
//count = (long)*f_pos;
if(copy_to_user(buf,dev->data,count)){
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
return retval;
}
int pan_write(struct file *filp,
const char __user *buf,size_t count,
loff_t *f_pos){
struct pan_dev *dev = filp->private_data;
size_t retval;
//dev->data = kmalloc(DATA_SIZE*sizeof(char *),GFP_KERNEL);
// if(count > DATA_SIZE){count = DATA_SIZE;}
// else{count = (long)*f_pos;}
//count = (long)*f_pos;
memset(dev->data,0,DATA_SIZE*sizeof(char *));
if(copy_from_user(dev->data,buf,count)){
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
return retval;
}
int pan_open(struct inode *inode,struct file *filp){
struct pan_dev *dev;
dev = container_of(inode->i_cdev,struct pan_dev,cdev);
filp->private_data = dev;
return 0;
}
int pan_release(struct inode *inode,struct file *filp){
return 0;
}
struct file_operations pan_fops = {
.owner = THIS_MODULE,
.read = pan_read,
.write = pan_write,
.open = pan_open,
.release = pan_release,
};
static void pan_exit(void){
dev_t devno=MKDEV(pan_major,pan_minor);
kfree(pan_device);
unregister_chrdev_region(devno,1);
printk(KERN_ALERT"Pan char modules exit!n");
}
static int pan_init(void){
int result,err;
dev_t dev = 0;
if(pan_major){
dev = MKDEV(pan_major,pan_minor);
result = register_chrdev_region(dev,1,"pan");
}else{
result = alloc_chrdev_region(&dev,pan_minor,1,"pan");
pan_major = MAJOR(dev);
}
if(result data = kmalloc(DATA_SIZE*sizeof(char *),GFP_KERNEL);
memset(pan_device->data,0,DATA_SIZE*sizeof(char *));
//pan.data_size=DATA_SIZE;
cdev_init(&pan_device->cdev,&pan_fops);
err = cdev_add(&pan_device->cdev,dev,1);
if(err){
printk(KERN_ALERT"Error %d add pan device ",err);
goto fail;
}
return result;
fail:
pan_exit();
return result;
}
module_init(pan_init);
module_exit(pan_exit);
|
你在read、write函数中都没有判断是否到达文件末尾,所以执行cat命令时永远有数据可以读,当然会不停打印。