2019-10-15 14:43:14 +08:00
|
|
|
/**
|
|
|
|
* @file demo_misc.c
|
|
|
|
* @author your name (you@domain.com)
|
|
|
|
* @brief
|
|
|
|
* @version 0.1
|
|
|
|
* @date 2019-10-15
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019
|
|
|
|
*
|
|
|
|
*/
|
2019-10-15 14:38:02 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
|
2019-10-21 09:17:47 +08:00
|
|
|
MODULE_AUTHOR("Rick Chan");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2019-10-15 14:38:02 +08:00
|
|
|
static int demo_open(struct inode *inode, struct file* filp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int demo_release(struct inode *inode, struct file* filp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static loff_t demo_llseek(struct file *filp, loff_t offset, int origin)
|
|
|
|
{
|
|
|
|
return filp->f_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t demo_read(struct file *filp, char __user *buffer, size_t count, loff_t *offset)
|
|
|
|
{
|
|
|
|
const char __user *p = buffer;
|
|
|
|
return p-buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t demo_write(struct file *filp, const char __user *buffer, size_t count, loff_t *offset)
|
|
|
|
{
|
|
|
|
const char __user *p = buffer;
|
|
|
|
return p-buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long demo_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int demo_mmap(struct file* filp, struct vm_area_struct* vma)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_operations demo_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = demo_open,
|
|
|
|
.release = demo_release,
|
|
|
|
.llseek = demo_llseek,
|
|
|
|
.read = demo_read,
|
|
|
|
.write = demo_write,
|
|
|
|
.unlocked_ioctl = demo_ioctl,
|
|
|
|
.mmap = demo_mmap
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct miscdevice demo_dev = {
|
2019-10-15 14:43:14 +08:00
|
|
|
.minor = MISC_DYNAMIC_MINOR, // Dynamically allocate minor.
|
|
|
|
.name = "demo_misc",
|
|
|
|
.fops = &demo_fops,
|
2019-10-15 14:38:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init demo_init(void)
|
|
|
|
{
|
|
|
|
misc_register(&demo_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit demo_exit(void)
|
|
|
|
{
|
|
|
|
misc_deregister(&demo_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(demo_init);
|
|
|
|
module_exit(demo_exit);
|