[linux]misc简单驱动

/*
*file name: misc_device.c
*/

#include #include #include #include #include #include #include #include #include #include #include #include

#define MISC_MINOR 255

static int hello_open(struct inode *inode, struct file *filp)
{
printk(“misc open!\n”);
return 0;
}

static ssize_t hello_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char data[16];

printk(“misc write\n”);
ret = copy_from_user(data, buf, cnt);
if(ret != 0)
printk(“hello misc write failed\n”);

return ret;
}

static ssize_t hello_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char data[4] = {0x12, 0x03, 0x56, 0x11};

printk(“misc read\n”);

ret = copy_to_user(buf, &data, sizeof(data));
if(ret != 0)
printk(“hello misc read failed\n”);

return ret;
}

/*
* file operations
*/
struct file_operations hello_fops = {

.owner = THIS_MODULE,
.write = hello_write,
.read = hello_read,
.open = hello_open,
};

/*
* misc description
*/
struct miscdevice hello_misc = {
.minor = MISC_MINOR,
.name = “hello-misc”,
.fops = &hello_fops,
};

static int hello_init(void)
{
int ret = 0;

printk(” Hello World enter\n”);

ret = misc_register(&hello_misc);
if(ret != 0)
printk(“hello misc register failed!”);

return 0;
}

static void hello_exit(void)
{
printk(” Hello World exit\n “);
misc_deregister(&hello_misc);
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE(“Dual BSD/GPL”);
MODULE_DESCRIPTION(“A simple Hello World Module”);

发表评论

邮箱地址不会被公开。 必填项已用*标注