mirror of
https://github.com/yaakov-h/scream-driver.git
synced 2024-11-22 17:34:47 +00:00
works now, clean me up pls
This commit is contained in:
parent
252c5eaf2a
commit
0b2fa3282f
1 changed files with 44 additions and 51 deletions
121
scream.c
121
scream.c
|
@ -3,6 +3,7 @@
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <linux/cdev.h>
|
#include <linux/cdev.h>
|
||||||
#include <linux/device.h>
|
#include <linux/device.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
#include <linux/uaccess.h>
|
#include <linux/uaccess.h>
|
||||||
|
|
||||||
MODULE_AUTHOR("Yaakov");
|
MODULE_AUTHOR("Yaakov");
|
||||||
|
@ -20,87 +21,79 @@ 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*);
|
||||||
static ssize_t scream_device_write(struct file*, const char*, size_t, loff_t*);
|
|
||||||
|
|
||||||
static struct file_operations fops = {
|
static const struct file_operations scream_fops = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
.open = scream_device_open,
|
.open = scream_device_open,
|
||||||
.read = scream_device_read,
|
.read = scream_device_read,
|
||||||
.write = scream_device_write,
|
|
||||||
.release = scream_device_release,
|
.release = scream_device_release,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int __init scream_module_init(void) {
|
static int __init scream_module_init(void)
|
||||||
printk(KERN_INFO "[scream] loading...\n");
|
{
|
||||||
|
int err;
|
||||||
|
dev_t dev;
|
||||||
|
|
||||||
if (alloc_chrdev_region(&major_number, 0, 1, DEVICE_NAME) < 0) {
|
err = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
|
||||||
printk(KERN_ALERT "[scream] Failed to register a major number.\n");
|
|
||||||
return major_number;
|
major_number = MAJOR(dev);
|
||||||
}
|
|
||||||
|
|
||||||
scream_class = class_create(CLASS_NAME);
|
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);
|
cdev_init(&scream_cdev, &scream_fops);
|
||||||
if (IS_ERR(scream_device)) {
|
scream_cdev.owner = THIS_MODULE;
|
||||||
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_add(&scream_cdev, MKDEV(major_number, 0), 1);
|
||||||
|
|
||||||
cdev_init(&scream_cdev, &fops);
|
device_create(scream_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
|
||||||
int add_result = cdev_add(&scream_cdev, MKDEV(major_number, 0), 1);
|
|
||||||
if (add_result < 0) {
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __exit scream_module_exit(void)
|
||||||
|
{
|
||||||
device_destroy(scream_class, MKDEV(major_number, 0));
|
device_destroy(scream_class, MKDEV(major_number, 0));
|
||||||
|
|
||||||
|
class_unregister(scream_class);
|
||||||
class_destroy(scream_class);
|
class_destroy(scream_class);
|
||||||
unregister_chrdev_region(major_number, 1);
|
|
||||||
printk(KERN_ALERT "[scream] Failed to add cdev: %d\n", add_result);
|
unregister_chrdev_region(MKDEV(major_number, 0), MINORMASK);
|
||||||
return -1;
|
}
|
||||||
|
|
||||||
|
static int scream_device_open(struct inode *inode, struct file *file)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "[scream] Device open\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int scream_device_release(struct inode *inode, struct file *file)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "[scream] Device close\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t scream_device_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
|
||||||
|
{
|
||||||
|
uint8_t rand;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
get_random_bytes(&rand, sizeof(rand));
|
||||||
|
|
||||||
|
char value;
|
||||||
|
|
||||||
|
if (rand > 200) {
|
||||||
|
value = 'A';
|
||||||
|
} else {
|
||||||
|
value = 'a';
|
||||||
}
|
}
|
||||||
|
|
||||||
printk(KERN_INFO "[scream] loaded.\n");
|
if (copy_to_user(buf + i, &value, 1)) {
|
||||||
return 0;
|
return -EFAULT;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void __exit scream_module_exit(void) {
|
return count;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ssize_t scream_device_write(struct file* file, const char* __user user_buffer, size_t size, loff_t* offset)
|
|
||||||
{
|
|
||||||
printk(KERN_INFO "[scream] writing to device.\n");
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(scream_module_init);
|
module_init(scream_module_init);
|
||||||
|
|
Loading…
Reference in a new issue