#include<linux/module.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/kdev_t.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
#define FTIRST_DRIVE 111
struct cdev_first{
struct cdev f_dev;
};
struct cdev_first *devp;
static ssize_t first_read (struct file *filp, char __user *buf, size_t len, loff_t *ppos)
{
char k_buf[20] = "jqwdaddda aeee!!";
if(copy_to_user(buf,&k_buf,sizeof(k_buf)))
return -1;
return sizeof(k_buf);
}
static ssize_t first_write (struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
{
printk(KERN_WARNING "do noting");
return 0;
}
static const struct file_operations first_fops = {
.owner = THIS_MODULE,
.read = first_read,
.write = first_write,
};
static void first_setup_cdev(struct cdev_first *dev, int index)
{
int error;
dev_t devno = MKDEV(FTIRST_DRIVE,0);
cdev_init(&dev->f_dev, &first_fops);
dev->f_dev.owner = THIS_MODULE;
error = cdev_add (&dev->f_dev, devno, 1);
if(error)
printk(KERN_WARNING "add dev error");
}
static int __init first_init(void)
{
int error;
dev_t devn;
devn = MKDEV(FTIRST_DRIVE,0);
if(1){
error = register_chrdev_region(devn,1,"first");
}
else{
error = alloc_chrdev_region(&devn,0,1,"first");
}
if(error < 0)
goto deal_error;
first_setup_cdev(devp,0);
return 0;
deal_error:
unregister_chrdev_region(devn,1);
printk(KERN_WARNING "register error!");
return -1;
}
static void __exit first_exit(void)
{
int devno = MKDEV(FTIRST_DRIVE,0);
cdev_del(&devp->f_dev);
unregister_chrdev_region(devno,1);
}
module_init(first_init);
module_exit(first_exit);
MODULE_AUTHOR("LIN");
MODULE_LICENSE("GPL");
以上是代码,是不是初始化哪里出错了,求指点 |