From fd89f87ee3d2721155124954dca4156866001562 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 11 Jul 2023 09:43:44 +0200 Subject: mod: move files to subdirectories --- mod/Kbuild | 10 +++ mod/Makefile | 17 ++++ mod/include/cyber.h | 31 +++++++ mod/include/cyber_device.h | 62 ++++++++++++++ mod/include/cyber_file.h | 35 ++++++++ mod/include/cyber_lifecycle.h | 33 ++++++++ mod/src/cyber.c | 29 +++++++ mod/src/cyber_device.c | 103 +++++++++++++++++++++++ mod/src/cyber_device_character_device.c | 53 ++++++++++++ mod/src/cyber_device_device_class.c | 85 +++++++++++++++++++ mod/src/cyber_device_kernel_device.c | 52 ++++++++++++ mod/src/cyber_file.c | 144 ++++++++++++++++++++++++++++++++ mod/src/cyber_lifecycle.c | 49 +++++++++++ 13 files changed, 703 insertions(+) create mode 100644 mod/Kbuild create mode 100644 mod/Makefile create mode 100644 mod/include/cyber.h create mode 100644 mod/include/cyber_device.h create mode 100644 mod/include/cyber_file.h create mode 100644 mod/include/cyber_lifecycle.h create mode 100644 mod/src/cyber.c create mode 100644 mod/src/cyber_device.c create mode 100644 mod/src/cyber_device_character_device.c create mode 100644 mod/src/cyber_device_device_class.c create mode 100644 mod/src/cyber_device_kernel_device.c create mode 100644 mod/src/cyber_file.c create mode 100644 mod/src/cyber_lifecycle.c (limited to 'mod') diff --git a/mod/Kbuild b/mod/Kbuild new file mode 100644 index 0000000..250db3e --- /dev/null +++ b/mod/Kbuild @@ -0,0 +1,10 @@ +obj-m := cyber.o +cyber-objs := src/cyber_device.o \ + src/cyber_device_character_device.o \ + src/cyber_device_device_class.o \ + src/cyber_device_kernel_device.o \ + src/cyber_file.o \ + src/cyber_lifecycle.o \ + src/cyber.o + +ccflags-y := -I$(src)/include diff --git a/mod/Makefile b/mod/Makefile new file mode 100644 index 0000000..0c25810 --- /dev/null +++ b/mod/Makefile @@ -0,0 +1,17 @@ +ifneq ($(KERNELRELEASE),) +include Kbuild +else +KDIR ?= /lib/modules/`uname -r`/build + +modules: + @$(MAKE) -C $(KDIR) M=$$PWD modules + +clean: + @$(MAKE) -C $(KDIR) M=$$PWD clean + +install: + @$(MAKE) -C $(KDIR) M=$$PWD modules_install + @depmod -a + +endif + diff --git a/mod/include/cyber.h b/mod/include/cyber.h new file mode 100644 index 0000000..f5ca28b --- /dev/null +++ b/mod/include/cyber.h @@ -0,0 +1,31 @@ +/* + * 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 . + */ + +#ifndef CYBER_H +#define CYBER_H + +/** + * The name of the CYBER device + */ +#define DEV_NAME "cyber" + +/** + * The class of the CYBER device + */ +#define CLS_NAME "cyber" + +#endif diff --git a/mod/include/cyber_device.h b/mod/include/cyber_device.h new file mode 100644 index 0000000..1da9e37 --- /dev/null +++ b/mod/include/cyber_device.h @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +#ifndef CYBER_DEVICE_H +#define CYBER_DEVICE_H + +#include +#include +#include + +/** + * This structure holds information about the CYBER device + */ +extern struct cyber_device +{ + /** + * The kernel character device structure + */ + struct cdev character; + + /** + * The kernel device structure + */ + struct device kernel; + + /** + * The kernel device class structure + */ + struct class class; + + /** + * The kernel device number + */ + dev_t number; + +} device; + +/** + * Initialize the CYBER device + */ +int cyber_device_init(void); + +/** + * Deinitialize the CYBER device + */ +void cyber_device_shutdown(void); + +#endif diff --git a/mod/include/cyber_file.h b/mod/include/cyber_file.h new file mode 100644 index 0000000..eb12cce --- /dev/null +++ b/mod/include/cyber_file.h @@ -0,0 +1,35 @@ +/* + * 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 . + */ + +#ifndef CYBER_FILE_H +#define CYBER_FILE_H + +#include + +/** + * Initialize the CYBER file + * + * @return zero on success, non-zero otherwise + */ +int cyber_file_init(void); + +/** + * Shutdown the CYBER file + */ +void cyber_file_shutdown(void); + +#endif diff --git a/mod/include/cyber_lifecycle.h b/mod/include/cyber_lifecycle.h new file mode 100644 index 0000000..ea2f6dd --- /dev/null +++ b/mod/include/cyber_lifecycle.h @@ -0,0 +1,33 @@ +/* + * 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 . + */ + +#ifndef CYBER_LIFECYCLE_H +#define CYBER_LIFECYCLE_H + +#include + +/** + * The CYBER driver intialization + */ +int __init cyber_driver_init(void); + +/** + * The CYBER driver deinitialization + */ +void __exit cyber_driver_exit(void); + +#endif diff --git a/mod/src/cyber.c b/mod/src/cyber.c new file mode 100644 index 0000000..6063136 --- /dev/null +++ b/mod/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 . + */ + +#include "cyber.h" +#include "cyber_device.h" +#include "cyber_lifecycle.h" + +#include +#include +#include + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Felix Morgner"); +MODULE_VERSION("42.1337.1939"); +MODULE_DESCRIPTION("Add CYBER to your system"); diff --git a/mod/src/cyber_device.c b/mod/src/cyber_device.c new file mode 100644 index 0000000..67b9695 --- /dev/null +++ b/mod/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 . + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include +#include +#include +#include + +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/mod/src/cyber_device_character_device.c b/mod/src/cyber_device_character_device.c new file mode 100644 index 0000000..f99e97e --- /dev/null +++ b/mod/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 . + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include +#include +#include + +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/mod/src/cyber_device_device_class.c b/mod/src/cyber_device_device_class.c new file mode 100644 index 0000000..b908da1 --- /dev/null +++ b/mod/src/cyber_device_device_class.c @@ -0,0 +1,85 @@ +/* + * 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 . + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include +#include +#include +#include +#include + +extern struct cyber_device device; + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,4,0) +static ssize_t available_show(struct class const * class, struct class_attribute const * attribute, char * __user buffer) +#else +static ssize_t available_show(struct class * class, struct class_attribute * attribute, char * __user buffer) +#endif + { + return sprintf(buffer, "infinite\n"); + } +static CLASS_ATTR_RO(available); + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,4,0) +static ssize_t storage_technology_show(struct class const * class, struct class_attribute const * attribute, char * __user buffer) +#else +static ssize_t storage_technology_show(struct class * class, struct class_attribute * attribute, char * __user buffer) +#endif + { + return sprintf(buffer, "condensed di-hydrogen-monoxide\n"); + } +static CLASS_ATTR_RO(storage_technology); + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,2,0) +static int cyber_device_handle_uevent(struct device const * kernelDevice, struct kobj_uevent_env * environment) +#else +static int cyber_device_handle_uevent(struct device * kernelDevice, struct kobj_uevent_env * environment) +#endif +{ + add_uevent_var(environment, "DEVMODE=%#o", 0666); + return 0; +} + +int cyber_device_init_device_class(void) +{ + int error = 0; + + device.class.name = CLS_NAME; +#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0) + device.class.owner = THIS_MODULE; +#endif + 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/mod/src/cyber_device_kernel_device.c b/mod/src/cyber_device_kernel_device.c new file mode 100644 index 0000000..6a2be4b --- /dev/null +++ b/mod/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 . + */ + +#include "cyber.h" +#include "cyber_device.h" + +#include +#include +#include + +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/mod/src/cyber_file.c b/mod/src/cyber_file.c new file mode 100644 index 0000000..4ff5581 --- /dev/null +++ b/mod/src/cyber_file.c @@ -0,0 +1,144 @@ +/* + * 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 . + */ + +#include "cyber.h" +#include "cyber_file.h" + +#include +#include +#include +#include +#include + +static unsigned char separator = ' '; +module_param(separator, byte, S_IRUGO); +MODULE_PARM_DESC(separator, "The string to separate the CYBER by"); + + +static char cyberPattern[] = {'!', 'C', 'Y', 'B', 'E', 'R', '!', ' '}; +static char * cyberSpace; + +/** + * Handler for CYBER device open events + * + * @param inode The kernel inode associated with the CYBER device + * @param file The kernel file associated with the CYBER device + * @return zero on success, non-zero otherwise + */ +static int cyber_file_open(struct inode * inode, struct file * file) +{ + return 0; +} + +/** + * Handler for CYBER device read events + * + * @param file The kernel file associated with the CYBER device + * @param buffer The user-space target buffer for all the CYBER + * @param size The size of the user-space buffer + * @param offset The offset into the CYBER + * @return The number of bytes that were read, negative on error + */ +static ssize_t cyber_file_read(struct file * file, char __user * buffer, size_t size, loff_t * offset) +{ + int const cyberChunks = (size + PAGE_SIZE - 1) / PAGE_SIZE; + int const cybersPerChunk = (size > PAGE_SIZE ? PAGE_SIZE : size) / 8; + int copiedCybers = 0; + + for(; copiedCybers < cyberChunks; ++copiedCybers) + { + if(raw_copy_to_user(buffer + copiedCybers * PAGE_SIZE, cyberSpace, cybersPerChunk * 8)) + { + return -EFAULT; + } + } + + return cyberChunks * cybersPerChunk * 8; +} + +static int cyber_file_mmap(struct file * file, struct vm_area_struct * vma) +{ + ssize_t vmaSize = vma->vm_end - vma->vm_start; + ssize_t offset = 0; + for(; vmaSize; vmaSize -= PAGE_SIZE, ++offset) + { + vm_insert_page(vma, vma->vm_start + offset * PAGE_SIZE, virt_to_page(cyberSpace)); + } + return 0; +} + +/** + * Handler for CYBER device write events + * + * @param file The kernel file associated with the CYBER device + * @param buffer The user-space source buffer for all the CYBER + * @param size The size of the user-space buffer + * @param offset The offset into the CYBER + * @param The number of bytes that were written, negative on error + */ +static ssize_t cyber_file_write(struct file * file, char __user const * buffer, size_t size, loff_t * offset) +{ + return size; +} + +/** + * Handler for CYBER device close events + * + * @param inode The kernel inode associated with the CYBER device + * @param file The kernel file associated with the CYBER device + * @param zero on success, non-zero otherwise + */ +static int cyber_file_close(struct inode * inode, struct file * file) +{ + return 0; +} + +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, + .mmap = cyber_file_mmap, +}; + +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; + } + + cyberPattern[7] = separator; + 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/mod/src/cyber_lifecycle.c b/mod/src/cyber_lifecycle.c new file mode 100644 index 0000000..02df37d --- /dev/null +++ b/mod/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 . + */ + +#include "cyber_device.h" +#include "cyber_file.h" +#include "cyber_lifecycle.h" + +#include + +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) -- cgit v1.2.3