90 lines
2.3 KiB
C
90 lines
2.3 KiB
C
|
|
#include <linux/types.h>
|
||
|
|
#include <linux/init.h>
|
||
|
|
#include <linux/interrupt.h>
|
||
|
|
#include <linux/mm.h>
|
||
|
|
#include <linux/slab.h>
|
||
|
|
#include <linux/spinlock.h>
|
||
|
|
#include <linux/module.h>
|
||
|
|
#include <linux/device.h>
|
||
|
|
#include <linux/cdev.h>
|
||
|
|
|
||
|
|
dev_t hello_dev_id;
|
||
|
|
struct cdev hello_cdev;
|
||
|
|
int hello_major = 0;
|
||
|
|
int hello_minor;
|
||
|
|
|
||
|
|
uint8_t kernel_buffer[1024] = { 0 };
|
||
|
|
static struct class *hello_driver_class;
|
||
|
|
|
||
|
|
static int hello_driver_open(struct inode *inode, struct file *file)
|
||
|
|
{
|
||
|
|
printk("hello_driver_open\r\n");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int hello_driver_release(struct inode *inode, struct file *file)
|
||
|
|
{
|
||
|
|
printk("hello_driver_release\r\n");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static ssize_t hello_driver_read(struct file *file, char __user *buffer, size_t size, loff_t *ppos)
|
||
|
|
{
|
||
|
|
printk("hello_driver_read size=%ld\r\n", size);
|
||
|
|
copy_to_user(buffer, kernel_buffer, size);
|
||
|
|
return size;
|
||
|
|
}
|
||
|
|
|
||
|
|
static ssize_t hello_driver_write(struct file *file, const char __user *buffer, size_t size, loff_t *ppos)
|
||
|
|
{
|
||
|
|
printk("hello_driver_write size=%ld\r\n", size);
|
||
|
|
copy_from_user(kernel_buffer, buffer, size);
|
||
|
|
return size;
|
||
|
|
}
|
||
|
|
|
||
|
|
static const struct file_operations hello_driver_fops = {
|
||
|
|
.owner = THIS_MODULE,
|
||
|
|
.open = hello_driver_open,
|
||
|
|
.release = hello_driver_release,
|
||
|
|
.read = hello_driver_read,
|
||
|
|
.write = hello_driver_write,
|
||
|
|
};
|
||
|
|
|
||
|
|
static int __init hello_driver_init(void)
|
||
|
|
{
|
||
|
|
printk("hello_driver_init\r\n");
|
||
|
|
|
||
|
|
/* automatically apply for device number */
|
||
|
|
alloc_chrdev_region(&hello_dev_id, 0, 1, "hello_driver");
|
||
|
|
hello_major = MAJOR(hello_dev_id);
|
||
|
|
hello_minor = MINOR(hello_dev_id);
|
||
|
|
printk("hello_driver: major=%d, minor=%d\r\n", hello_major, hello_minor);
|
||
|
|
|
||
|
|
hello_cdev.owner = THIS_MODULE;
|
||
|
|
cdev_init(&hello_cdev, &hello_driver_fops);
|
||
|
|
cdev_add(&hello_cdev, hello_dev_id, 1);
|
||
|
|
// ret = register_chrdev(CHRDEVBASE_MAJOR, "hello_driver", &hello_driver_fops);
|
||
|
|
|
||
|
|
/* automatically add device node file */
|
||
|
|
hello_driver_class = class_create("hello_driver_class");
|
||
|
|
device_create(hello_driver_class, NULL, hello_dev_id, NULL, "hello_driver_device");
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void __exit hello_driver_exit(void)
|
||
|
|
{
|
||
|
|
printk("hello_driver_exit\r\n");
|
||
|
|
|
||
|
|
cdev_del(&hello_cdev);
|
||
|
|
unregister_chrdev_region(hello_dev_id, 1);
|
||
|
|
|
||
|
|
device_destroy(hello_driver_class, hello_dev_id);
|
||
|
|
class_destroy(hello_driver_class);
|
||
|
|
// unregister_chrdev(CHRDEVBASE_MAJOR, "hello_driver");
|
||
|
|
}
|
||
|
|
|
||
|
|
module_init(hello_driver_init);
|
||
|
|
module_exit(hello_driver_exit);
|
||
|
|
MODULE_LICENSE("GPL");
|