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

100 lines
2.3 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>
2024-08-11 04:42:56 +00:00
#include <linux/kernel.h>
2024-08-11 04:07:11 +00:00
#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:43:28 +00:00
#define DEVICE_NAME "scream"
2024-08-11 04:07:11 +00:00
#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*);
2024-08-11 04:42:56 +00:00
static const struct file_operations scream_fops = {
.owner = THIS_MODULE,
2024-08-11 04:07:11 +00:00
.open = scream_device_open,
.read = scream_device_read,
.release = scream_device_release,
};
2024-08-11 04:42:56 +00:00
static int __init scream_module_init(void)
{
int err;
dev_t dev;
2024-08-11 04:07:11 +00:00
2024-08-11 04:42:56 +00:00
err = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
major_number = MAJOR(dev);
2024-08-11 04:07:11 +00:00
scream_class = class_create(CLASS_NAME);
2024-08-11 04:42:56 +00:00
cdev_init(&scream_cdev, &scream_fops);
scream_cdev.owner = THIS_MODULE;
2024-08-11 04:22:51 +00:00
2024-08-11 04:42:56 +00:00
cdev_add(&scream_cdev, MKDEV(major_number, 0), 1);
device_create(scream_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
2024-08-11 04:07:11 +00:00
return 0;
2024-08-11 03:14:21 +00:00
}
2024-08-11 04:42:56 +00:00
static void __exit scream_module_exit(void)
{
2024-08-11 04:07:11 +00:00
device_destroy(scream_class, MKDEV(major_number, 0));
2024-08-11 04:42:56 +00:00
class_unregister(scream_class);
2024-08-11 04:07:11 +00:00
class_destroy(scream_class);
2024-08-11 04:42:56 +00:00
unregister_chrdev_region(MKDEV(major_number, 0), MINORMASK);
2024-08-11 04:07:11 +00:00
}
2024-08-11 04:42:56 +00:00
static int scream_device_open(struct inode *inode, struct file *file)
2024-08-11 04:07:11 +00:00
{
2024-08-11 04:42:56 +00:00
printk(KERN_INFO "[scream] Device open\n");
2024-08-11 04:07:11 +00:00
return 0;
}
2024-08-11 04:42:56 +00:00
static int scream_device_release(struct inode *inode, struct file *file)
2024-08-11 04:07:11 +00:00
{
2024-08-11 04:42:56 +00:00
printk(KERN_INFO "[scream] Device close\n");
2024-08-11 04:07:11 +00:00
return 0;
}
2024-08-11 04:42:56 +00:00
static ssize_t scream_device_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2024-08-11 04:07:11 +00:00
{
2024-08-11 04:42:56 +00:00
uint8_t rand;
2024-08-11 03:14:21 +00:00
2024-08-11 04:42:56 +00:00
for (size_t i = 0; i < count; i++)
{
get_random_bytes(&rand, sizeof(rand));
char value;
if (rand > 200) {
value = 'A';
} else {
value = 'a';
}
if (copy_to_user(buf + i, &value, 1)) {
return -EFAULT;
}
}
return count;
2024-08-11 04:22:51 +00:00
}
2024-08-11 03:14:21 +00:00
module_init(scream_module_init);
module_exit(scream_module_exit);