/*
*file name: misc_device.c
*/
#include #define MISC_MINOR 255 static int hello_open(struct inode *inode, struct file *filp) static ssize_t hello_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt) printk(“misc write\n”); return ret; static ssize_t hello_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt) printk(“misc read\n”); ret = copy_to_user(buf, &data, sizeof(data)); return ret; /* .owner = THIS_MODULE, /* static int hello_init(void) printk(” Hello World enter\n”); ret = misc_register(&hello_misc); return 0; static void hello_exit(void) module_init(hello_init);
{
printk(“misc open!\n”);
return 0;
}
{
int ret = 0;
unsigned char data[16];
ret = copy_from_user(data, buf, cnt);
if(ret != 0)
printk(“hello misc write failed\n”);
}
{
int ret = 0;
unsigned char data[4] = {0x12, 0x03, 0x56, 0x11};
if(ret != 0)
printk(“hello misc read failed\n”);
}
* file operations
*/
struct file_operations hello_fops = {
.write = hello_write,
.read = hello_read,
.open = hello_open,
};
* misc description
*/
struct miscdevice hello_misc = {
.minor = MISC_MINOR,
.name = “hello-misc”,
.fops = &hello_fops,
};
{
int ret = 0;
if(ret != 0)
printk(“hello misc register failed!”);
}
{
printk(” Hello World exit\n “);
misc_deregister(&hello_misc);
}
module_exit(hello_exit);
MODULE_LICENSE(“Dual BSD/GPL”);
MODULE_DESCRIPTION(“A simple Hello World Module”);