1
0
Fork 0
mirror of https://github.com/yaakov-h/scream-driver.git synced 2024-10-16 07:40:02 +00:00
scream-driver/scream.c

98 lines
2.9 KiB
C
Raw Normal View History

2024-08-11 03:14:21 +00:00
#include <linux/module.h>
2024-08-11 04:07:11 +00:00
#include <linux/init.h>
2024-08-11 03:14:21 +00:00
#include <linux/fs.h>
2024-08-11 04:07:11 +00:00
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
2024-08-11 03:14:21 +00:00
MODULE_AUTHOR("Yaakov");
MODULE_DESCRIPTION("Scream Device");
MODULE_LICENSE("GPL");
2024-08-11 04:07:11 +00:00
#define DEVICE_NAME "scream_device"
#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_release(struct inode*, struct file*);
static ssize_t scream_device_read(struct file*, char*, size_t, loff_t*);
static struct file_operations fops = {
.open = scream_device_open,
.read = scream_device_read,
.release = scream_device_release,
};
2024-08-11 03:14:21 +00:00
static int __init scream_module_init(void) {
2024-08-11 04:07:11 +00:00
printk(KERN_INFO "[scream] loading...\n");
if (alloc_chrdev_region(&major_number, 0, 1, DEVICE_NAME) < 0) {
printk(KERN_ALERT "[scream] Failed to register a major number.\n");
return major_number;
}
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);
}
// Initialize the cdev structure and add it to the kernel
cdev_init(&scream_cdev, &fops);
int add_result = cdev_add(&scream_cdev, MKDEV(major_number, 0), 1);
if (add_result < 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", add_result);
return -1;
}
printk(KERN_INFO "[scream] loaded.\n");
return 0;
2024-08-11 03:14:21 +00:00
}
static void __exit scream_module_exit(void) {
2024-08-11 04:07:11 +00:00
printk(KERN_INFO "[scream] exiting...\n");
cdev_del(&scream_cdev);
device_destroy(scream_class, MKDEV(major_number, 0));
class_destroy(scream_class);
unregister_chrdev_region(major_number, 1);
printk(KERN_INFO "[scream] done.\n");
}
static int scream_device_open(struct inode* node, struct file* file)
{
printk(KERN_INFO "[scream] opening device.\n");
return 0;
}
static int scream_device_release(struct inode* node, struct file* file)
{
printk(KERN_INFO "[scream] releasing device.\n");
return 0;
}
static ssize_t scream_device_read(struct file* file, char* __user user_buffer, size_t size, loff_t* offset)
{
printk(KERN_INFO "[scream] reading from device.\n");
return 0;
2024-08-11 03:14:21 +00:00
}
module_init(scream_module_init);
module_exit(scream_module_exit);