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

76 lines
1.8 KiB
C
Raw Normal View History

2024-08-11 08:00:20 +00:00
#include <linux/miscdevice.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
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 = {
2024-08-11 07:49:35 +00:00
.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 08:00:20 +00:00
static struct miscdevice scream_misc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "scream",
.fops = &scream_fops,
.mode = S_IRUGO,
};
2024-08-11 07:49:35 +00:00
2024-08-11 08:00:20 +00:00
static int __init scream_module_init(void) {
2024-08-11 07:49:35 +00:00
printk(KERN_INFO "[scream] loading...\n");
2024-08-11 08:00:20 +00:00
int ret = misc_register(&scream_misc_device);
return ret;
2024-08-11 03:14:21 +00:00
}
2024-08-11 08:00:20 +00:00
static void __exit scream_module_exit(void) {
2024-08-11 07:49:35 +00:00
printk(KERN_INFO "[scream] exiting...\n");
2024-08-11 04:42:56 +00:00
2024-08-11 08:00:20 +00:00
misc_deregister(&scream_misc_device);
2024-08-11 04:07:11 +00:00
2024-08-11 07:49:35 +00:00
printk(KERN_INFO "[scream] done.\n");
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);