1
0
Fork 0
mirror of https://github.com/yaakov-h/scream-driver.git synced 2024-11-22 09:24:49 +00:00

simplify by switching to miscdevice

This commit is contained in:
Yaakov 2024-08-11 18:00:20 +10:00
parent 5c61c12fdb
commit 2d516e87aa

View file

@ -5,6 +5,8 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/stat.h>
MODULE_AUTHOR("Yaakov"); MODULE_AUTHOR("Yaakov");
MODULE_DESCRIPTION("Scream Device"); MODULE_DESCRIPTION("Scream Device");
@ -13,11 +15,6 @@ MODULE_LICENSE("GPL");
#define DEVICE_NAME "scream" #define DEVICE_NAME "scream"
#define CLASS_NAME "scream_class" #define CLASS_NAME "scream_class"
static int major_number;
static struct class* scream_class = NULL;
static struct device* scream_device = NULL;
static struct cdev scream_cdev;
static int scream_device_open(struct inode*, struct file*); static int scream_device_open(struct inode*, struct file*);
static int scream_device_release(struct inode*, struct file*); static int scream_device_release(struct inode*, struct file*);
static ssize_t scream_device_read(struct file*, char*, size_t, loff_t*); static ssize_t scream_device_read(struct file*, char*, size_t, loff_t*);
@ -29,62 +26,23 @@ static const struct file_operations scream_fops = {
.release = scream_device_release, .release = scream_device_release,
}; };
static struct miscdevice scream_misc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "scream",
.fops = &scream_fops,
.mode = S_IRUGO,
};
static int __init scream_module_init(void) { static int __init scream_module_init(void) {
printk(KERN_INFO "[scream] loading...\n"); printk(KERN_INFO "[scream] loading...\n");
int ret = misc_register(&scream_misc_device);
int err; return ret;
dev_t dev;
err = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
if (err) {
printk(KERN_ALERT "[scream] Failed to register a major number.\n");
return err;
}
major_number = MAJOR(dev);
scream_class = class_create(CLASS_NAME);
if (IS_ERR(scream_class)) {
unregister_chrdev_region(major_number, 1);
printk(KERN_ALERT "[scream] Failed to register device class\n");
return PTR_ERR(scream_class);
}
scream_device = device_create(scream_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(scream_device)) {
class_destroy(scream_class);
unregister_chrdev_region(major_number, 1);
printk(KERN_ALERT "[scream] Failed to create the device\n");
return PTR_ERR(scream_device);
}
printk(KERN_INFO "[scream] %s/%s device registered.", CLASS_NAME, DEVICE_NAME);
cdev_init(&scream_cdev, &scream_fops);
err = cdev_add(&scream_cdev, MKDEV(major_number, 0), 1);
if (err < 0) {
device_destroy(scream_class, MKDEV(major_number, 0));
class_destroy(scream_class);
unregister_chrdev_region(major_number, 1);
printk(KERN_ALERT "[scream] Failed to add cdev: %d\n", err);
return err;
}
printk(KERN_INFO "[scream] loaded.\n");
return 0;
} }
static void __exit scream_module_exit(void) static void __exit scream_module_exit(void) {
{
printk(KERN_INFO "[scream] exiting...\n"); printk(KERN_INFO "[scream] exiting...\n");
cdev_del(&scream_cdev); misc_deregister(&scream_misc_device);
device_destroy(scream_class, MKDEV(major_number, 0));
class_destroy(scream_class);
unregister_chrdev_region(major_number, 1);
printk(KERN_INFO "[scream] done.\n"); printk(KERN_INFO "[scream] done.\n");
} }