diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2017-02-25 15:24:32 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2017-02-25 15:39:50 +0100 |
| commit | 8466ba397d04f24e97b06f4117e8f0ab2c1e0e48 (patch) | |
| tree | 3de8a10c86773832a574e9f1ce6736bc5892b078 /src | |
| download | dev-cyber-8466ba397d04f24e97b06f4117e8f0ab2c1e0e48.tar.xz dev-cyber-8466ba397d04f24e97b06f4117e8f0ab2c1e0e48.zip | |
cyber: initial releasev42.1337
Diffstat (limited to 'src')
| -rw-r--r-- | src/cyber.c | 29 | ||||
| -rw-r--r-- | src/cyber_device.c | 103 | ||||
| -rw-r--r-- | src/cyber_device_character_device.c | 53 | ||||
| -rw-r--r-- | src/cyber_device_device_class.c | 77 | ||||
| -rw-r--r-- | src/cyber_device_kernel_device.c | 52 | ||||
| -rw-r--r-- | src/cyber_file.c | 84 | ||||
| -rw-r--r-- | src/cyber_lifecycle.c | 49 |
7 files changed, 447 insertions, 0 deletions
diff --git a/src/cyber.c b/src/cyber.c new file mode 100644 index 0000000..3f454af --- /dev/null +++ b/src/cyber.c @@ -0,0 +1,29 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_device.h" +#include "cyber_lifecycle.h" + +#include <linux/device.h> +#include <linux/init.h> +#include <linux/module.h> + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Felix Morgner"); +MODULE_VERSION("42.1337"); +MODULE_DESCRIPTION("Add CYBER to your system"); diff --git a/src/cyber_device.c b/src/cyber_device.c new file mode 100644 index 0000000..67b9695 --- /dev/null +++ b/src/cyber_device.c @@ -0,0 +1,103 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> + +struct cyber_device device = {}; + +/** + * Initialize the sysfs CYBER device class + * + * @return zero on success, non-zero otherwise + */ +int cyber_device_init_device_class(void); + +/** + * Shutdown the sysfs CYBER device class + */ +void cyber_device_shutdown_device_class(void); + +/** + * Initialize the CYBER character device + * + * @return zero on success, non-zero otherwise + */ +int cyber_device_init_character_device(void); + +/** + * Shutdown the CYBER character device + */ +void cyber_device_shutdown_character_device(void); + +/** + * Initialize the CYBER kernel device + * + * @return zero on success, non-zero otherwise + */ +int cyber_device_init_kernel_device(void); + +/** + * Shutdown the CYBER kernel device + */ +void cyber_device_shutdown_kernel_device(void); + +int cyber_device_init(void) +{ + int error = 0; + + printk(KERN_INFO DEV_NAME ": Initializing CYBER device\n"); + + if((error = cyber_device_init_character_device())) + { + return error; + } + + if((error = cyber_device_init_device_class())) + { + cyber_device_shutdown_character_device(); + return error; + } + + if((error = cyber_device_init_kernel_device())) + { + cyber_device_shutdown_character_device(); + cyber_device_shutdown_device_class(); + return error; + } + + printk(KERN_INFO DEV_NAME ": New CYBER device with major %d minor %d\n", MAJOR(device.number), MINOR(device.number)); + return error; +} + +void cyber_device_shutdown(void) +{ + printk(KERN_INFO DEV_NAME ": Shutting down CYBER device\n"); + cyber_device_shutdown_kernel_device(); + cyber_device_shutdown_device_class(); + cyber_device_shutdown_character_device(); +} + +void cyber_device_release(struct device * device) +{ + printk(KERN_INFO DEV_NAME ": Closing kernel CYBER device\n"); +} diff --git a/src/cyber_device_character_device.c b/src/cyber_device_character_device.c new file mode 100644 index 0000000..f99e97e --- /dev/null +++ b/src/cyber_device_character_device.c @@ -0,0 +1,53 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/module.h> + +extern struct cyber_device device; +extern struct file_operations const cyber_operations; + +int cyber_device_init_character_device(void) +{ + int error = 0; + + if((error = alloc_chrdev_region(&device.number, 0, 1, DEV_NAME))) + { + printk(KERN_ALERT DEV_NAME ": Failed to allocate character device!\n"); + } + + cdev_init(&device.character, &cyber_operations); + device.character.owner = THIS_MODULE; + device.character.ops = &cyber_operations; + + if((error = cdev_add(&device.character, device.number, 1))) + { + printk(KERN_ALERT DEV_NAME ": Failed to add character device (Error: %d)!\n", error); + } + + return error; +} + +void cyber_device_shutdown_character_device(void) +{ + cdev_del(&device.character); + unregister_chrdev_region(device.number, 1); +} diff --git a/src/cyber_device_device_class.c b/src/cyber_device_device_class.c new file mode 100644 index 0000000..c0d108b --- /dev/null +++ b/src/cyber_device_device_class.c @@ -0,0 +1,77 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/string.h> + +extern struct cyber_device device; + +static ssize_t cyber_device_show_attribute(struct class * class, struct class_attribute * attribute, char __user * buffer) +{ + char const * const name = attribute->attr.name; + + if(!strcmp(name, "available")) + { + return sprintf(buffer, "infinite\n"); + } + else if(!strcmp(name, "storage_technology")) + { + return sprintf(buffer, "condensed di-hydrogen-monoxide\n"); + } + + return -EINVAL; +} + +CLASS_ATTR(available, 0444, cyber_device_show_attribute, NULL); +CLASS_ATTR(storage_technology, 0444, cyber_device_show_attribute, NULL); + +static int cyber_device_handle_uevent(struct device * kernelDevice, struct kobj_uevent_env * environment) +{ + add_uevent_var(environment, "DEVMODE=%#o", 0666); + return 0; +} + +int cyber_device_init_device_class(void) +{ + int error = 0; + + device.class.name = CLS_NAME; + device.class.owner = THIS_MODULE; + device.class.dev_uevent = cyber_device_handle_uevent; + if((error = class_register(&device.class))) + { + printk(KERN_ALERT DEV_NAME ": Failed to register CYBER device class!\n"); + } + + if((error = class_create_file(&device.class, &class_attr_available)) || + (error = class_create_file(&device.class, &class_attr_storage_technology))) + { + printk(KERN_ALERT DEV_NAME ": Failed to register sysfs CYBER attributes!\n"); + } + + return error; +} + +void cyber_device_shutdown_device_class(void) +{ + class_unregister(&device.class); +} diff --git a/src/cyber_device_kernel_device.c b/src/cyber_device_kernel_device.c new file mode 100644 index 0000000..6a2be4b --- /dev/null +++ b/src/cyber_device_kernel_device.c @@ -0,0 +1,52 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include <linux/device.h> +#include <linux/kernel.h> +#include <linux/module.h> + +extern struct cyber_device device; + +static void cyber_device_close_kernel_device(struct device * device) +{ + printk(KERN_INFO DEV_NAME ": Closing kernel CYBER device\n"); +} + +int cyber_device_init_kernel_device(void) +{ + int error = 0; + + device.kernel.class = &device.class; + device.kernel.devt = device.number; + device.kernel.init_name = DEV_NAME; + device.kernel.release = cyber_device_close_kernel_device; + + if((error = device_register(&device.kernel))) + { + printk(KERN_ALERT DEV_NAME ": Failed to create CYBER device!\n"); + } + + return error; +} + +void cyber_device_shutdown_kernel_device(void) +{ + device_unregister(&device.kernel); +} diff --git a/src/cyber_file.c b/src/cyber_file.c new file mode 100644 index 0000000..d7264c1 --- /dev/null +++ b/src/cyber_file.c @@ -0,0 +1,84 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber.h" +#include "cyber_file.h" + +#include <asm/uaccess.h> +#include <linux/module.h> + +struct file_operations const cyber_operations = { + .owner = THIS_MODULE, + .open = cyber_file_open, + .read = cyber_file_read, + .write = cyber_file_write, + .release = cyber_file_close, +}; + +static char const * cyberPattern = "!CYBER! "; +static char * cyberSpace; + +int cyber_file_open(struct inode * inode, struct file * file) +{ + return 0; +} + +ssize_t cyber_file_read(struct file * file, char __user * buffer, size_t size, loff_t * offset) +{ + int const cybers = (size > PAGE_SIZE ? PAGE_SIZE : size) / 8; + if(copy_to_user(buffer, cyberSpace, cybers * 8)) + { + return -EFAULT; + } + return cybers * 8; +} + +ssize_t cyber_file_write(struct file * file, char __user const * buffer, size_t size, loff_t * offset) +{ + return size; +} + +int cyber_file_close(struct inode * inode, struct file * file) +{ + return 0; +} + +int cyber_file_init(void) +{ + int i; + + printk(KERN_INFO DEV_NAME ": Initializing CYBER space\n"); + cyberSpace = (char *)__get_free_page(GFP_KERNEL); + if(!cyberSpace) + { + printk(KERN_ALERT DEV_NAME ": Out of CYBER space!\n"); + return ENOMEM; + } + + for(i = 0; i < PAGE_SIZE; ++i) + { + cyberSpace[i] = cyberPattern[i % 8]; + } + + return 0; +} + +void cyber_file_shutdown(void) +{ + printk(KERN_INFO DEV_NAME ": Releasing CYBER space\n"); + free_page((unsigned long)cyberSpace); +} diff --git a/src/cyber_lifecycle.c b/src/cyber_lifecycle.c new file mode 100644 index 0000000..02df37d --- /dev/null +++ b/src/cyber_lifecycle.c @@ -0,0 +1,49 @@ +/* + * cyber - Add CYBER to your system + * Copyright (C) 2017 Felix Morgner + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "cyber_device.h" +#include "cyber_file.h" +#include "cyber_lifecycle.h" + +#include <linux/module.h> + +int cyber_driver_init(void) +{ + int error; + + if((error = cyber_file_init())) + { + return error; + } + + if((error = cyber_device_init()) < 0) + { + cyber_file_shutdown(); + return error; + } + + return 0; +} + +void cyber_driver_exit(void) +{ + cyber_device_shutdown(); + cyber_file_shutdown(); +} + +module_init(cyber_driver_init) +module_exit(cyber_driver_exit) |
