aboutsummaryrefslogtreecommitdiff
path: root/mod
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2023-07-13 10:25:14 +0200
committerFelix Morgner <felix.morgner@gmail.com>2023-07-13 10:25:14 +0200
commit006399dc8d5983037d1ac5a7b9d81a0bdc117143 (patch)
tree19afa4ef3624a007ce709b1e3f93e99a6f585645 /mod
parentfd89f87ee3d2721155124954dca4156866001562 (diff)
downloaddev-cyber-master.tar.xz
dev-cyber-master.zip
mod: restructure module layoutHEADmasterfmorgner/re-layout
Diffstat (limited to 'mod')
-rw-r--r--mod/Kbuild11
-rw-r--r--mod/include/cyber_device.h13
-rw-r--r--mod/include/cyber_file.h2
-rw-r--r--mod/src/cyber_device.c176
-rw-r--r--mod/src/cyber_device_character_device.c53
-rw-r--r--mod/src/cyber_device_device_class.c85
-rw-r--r--mod/src/cyber_device_kernel_device.c52
-rw-r--r--mod/src/cyber_file.c94
-rw-r--r--mod/src/cyber_lifecycle.c10
9 files changed, 201 insertions, 295 deletions
diff --git a/mod/Kbuild b/mod/Kbuild
index 250db3e..d71d187 100644
--- a/mod/Kbuild
+++ b/mod/Kbuild
@@ -1,10 +1,7 @@
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
+cyber-objs := src/cyber_device.o \
+ src/cyber_file.o \
+ src/cyber_lifecycle.o \
+ src/cyber.o
ccflags-y := -I$(src)/include
diff --git a/mod/include/cyber_device.h b/mod/include/cyber_device.h
index 1da9e37..884b54a 100644
--- a/mod/include/cyber_device.h
+++ b/mod/include/cyber_device.h
@@ -25,7 +25,7 @@
/**
* This structure holds information about the CYBER device
*/
-extern struct cyber_device
+typedef struct
{
/**
* The kernel character device structure
@@ -47,16 +47,21 @@ extern struct cyber_device
*/
dev_t number;
-} device;
+ /**
+ * Cyber space!
+ */
+ char * space;
+
+} cyber_device;
/**
* Initialize the CYBER device
*/
-int cyber_device_init(void);
+int cyber_device_init(cyber_device * device, struct file_operations const * file_ops);
/**
* Deinitialize the CYBER device
*/
-void cyber_device_shutdown(void);
+void cyber_device_shutdown(cyber_device * device);
#endif
diff --git a/mod/include/cyber_file.h b/mod/include/cyber_file.h
index eb12cce..c4eea70 100644
--- a/mod/include/cyber_file.h
+++ b/mod/include/cyber_file.h
@@ -25,7 +25,7 @@
*
* @return zero on success, non-zero otherwise
*/
-int cyber_file_init(void);
+int cyber_file_init(struct file_operations * file_ops);
/**
* Shutdown the CYBER file
diff --git a/mod/src/cyber_device.c b/mod/src/cyber_device.c
index 67b9695..a75a67c 100644
--- a/mod/src/cyber_device.c
+++ b/mod/src/cyber_device.c
@@ -20,81 +20,173 @@
#include <linux/device.h>
#include <linux/kernel.h>
+#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/string.h>
+#include <linux/fs.h>
-struct cyber_device device = {};
-
-/**
- * Initialize the sysfs CYBER device class
- *
- * @return zero on success, non-zero otherwise
+/*
+ * UDEV
*/
-int cyber_device_init_device_class(void);
-/**
- * Shutdown the sysfs CYBER device class
- */
-void cyber_device_shutdown_device_class(void);
+typedef struct device *device_ptr;
+typedef struct device const *const_device_ptr;
+typedef struct kobj_uevent_env *uevent_env_ptr;
+typedef struct kobj_uevent_env const *const_uevent_env_ptr;
-/**
- * Initialize the CYBER character device
- *
- * @return zero on success, non-zero otherwise
- */
-int cyber_device_init_character_device(void);
+static int _handle_uevent(const_device_ptr, uevent_env_ptr environment)
+{
+ return add_uevent_var(environment, "DEVMODE=%#o", 0666);
+}
-/**
- * Shutdown the CYBER character device
+/*
+ * Device Class
*/
-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);
+typedef struct class *class_ptr;
+typedef struct class const *const_class_ptr;
+typedef struct class_attribute *attribute_ptr;
+typedef struct class_attribute const *const_attribute_ptr;
-/**
- * Shutdown the CYBER kernel device
- */
-void cyber_device_shutdown_kernel_device(void);
+/// Get the amount of available cyber.
+static ssize_t available_show(const_class_ptr, const_attribute_ptr, char * __user output_buffer)
+{
+ return sprintf(output_buffer, "infinite\n");
+}
+
+/// Get the storage technology in use.
+static ssize_t storage_technology_show(const_class_ptr, const_attribute_ptr, char * __user output_buffer)
+{
+ return sprintf(output_buffer, "condensed di-hydrogen-monoxide\n");
+}
+
+static CLASS_ATTR_RO(available);
+static CLASS_ATTR_RO(storage_technology);
+
+static int _init_device_class(cyber_device * device)
+{
+ int error = 0;
+
+ device->class.name = CLS_NAME;
+ device->class.dev_uevent = _handle_uevent;
+
+ if((error = class_register(&device->class)))
+ {
+ printk(KERN_ALERT DEV_NAME ": Failed to register CYBER device class!\n");
+ return error;
+ }
+
+ 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;
+ }
+
+ return 0;
+}
+
+static void _shutdown_device_class(cyber_device * device)
+{
+ class_unregister(&device->class);
+}
+
+/// Character Device
+
+typedef struct file_operations *file_operations_ptr;
+typedef struct file_operations const *const_file_operations_ptr;
+
+static int _init_character_device(cyber_device * device, const_file_operations_ptr file_ops)
+{
+ 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, file_ops);
+ device->character.owner = THIS_MODULE;
+ device->character.ops = file_ops;
+
+ 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;
+}
+
+static void _shutdown_character_device(cyber_device * device)
+{
+ cdev_del(&device->character);
+ unregister_chrdev_region(device->number, 1);
+}
+
+/// Kernel Device
+
+static void _close_kernel_device(struct device * device)
+{
+ printk(KERN_INFO DEV_NAME ": Closing kernel CYBER device\n");
+}
+
+static int _init_kernel_device(cyber_device * device)
+{
+ int error = 0;
+
+ device->kernel.class = &device->class;
+ device->kernel.devt = device->number;
+ device->kernel.init_name = DEV_NAME;
+ device->kernel.release = _close_kernel_device;
+
+ if((error = device_register(&device->kernel)))
+ {
+ printk(KERN_ALERT DEV_NAME ": Failed to create CYBER device!\n");
+ }
+
+ return error;
+}
+
+static void _shutdown_kernel_device(cyber_device * device)
+{
+ device_unregister(&device->kernel);
+}
+
+/// Device
-int cyber_device_init(void)
+int cyber_device_init(cyber_device * device, const_file_operations_ptr file_ops)
{
int error = 0;
printk(KERN_INFO DEV_NAME ": Initializing CYBER device\n");
- if((error = cyber_device_init_character_device()))
+ if((error = _init_character_device(device, file_ops)))
{
return error;
}
- if((error = cyber_device_init_device_class()))
+ if((error = _init_device_class(device)))
{
- cyber_device_shutdown_character_device();
+ _shutdown_character_device(device);
return error;
}
- if((error = cyber_device_init_kernel_device()))
+ if((error = _init_kernel_device(device)))
{
- cyber_device_shutdown_character_device();
- cyber_device_shutdown_device_class();
+ _shutdown_character_device(device);
+ _shutdown_device_class(device);
return error;
}
- printk(KERN_INFO DEV_NAME ": New CYBER device with major %d minor %d\n", MAJOR(device.number), MINOR(device.number));
+ 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)
+void cyber_device_shutdown(cyber_device * device)
{
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();
+ _shutdown_kernel_device(device);
+ _shutdown_device_class(device);
+ _shutdown_character_device(device);
}
void cyber_device_release(struct device * device)
diff --git a/mod/src/cyber_device_character_device.c b/mod/src/cyber_device_character_device.c
deleted file mode 100644
index f99e97e..0000000
--- a/mod/src/cyber_device_character_device.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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/mod/src/cyber_device_device_class.c b/mod/src/cyber_device_device_class.c
deleted file mode 100644
index b908da1..0000000
--- a/mod/src/cyber_device_device_class.c
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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>
-#include <linux/version.h>
-
-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
deleted file mode 100644
index 6a2be4b..0000000
--- a/mod/src/cyber_device_kernel_device.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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/mod/src/cyber_file.c b/mod/src/cyber_file.c
index 4ff5581..16a7d02 100644
--- a/mod/src/cyber_file.c
+++ b/mod/src/cyber_file.c
@@ -17,6 +17,7 @@
#include "cyber.h"
#include "cyber_file.h"
+#include "cyber_device.h"
#include <asm/uaccess.h>
#include <linux/module.h>
@@ -28,10 +29,6 @@ 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
*
@@ -39,8 +36,36 @@ static char * cyberSpace;
* @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)
+static int cyber_file_open(struct inode *inode, struct file *file)
+{
+ char pattern[] = {'!', 'C', 'Y', 'B', 'E', 'R', '!', ' '};
+
+ printk(KERN_DEBUG DEV_NAME ": Opening CYBER space\n");
+
+ if(!(file->private_data = (char *)__get_free_page(GFP_KERNEL)))
+ {
+ return -ENOMEM;
+ }
+
+ pattern[sizeof(pattern) - 1] = separator;
+ for (int byte = 0; byte < PAGE_SIZE; ++byte)
+ {
+ ((char *)file->private_data)[byte] = pattern[byte % 8];
+ }
+
+ return 0;
+}
+
+/**
+ * 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)
{
+ free_page((unsigned long)file->private_data);
return 0;
}
@@ -53,15 +78,16 @@ static int cyber_file_open(struct inode * inode, struct file * file)
* @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)
+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;
+ char * cyberSpace = file->private_data;
- for(; copiedCybers < cyberChunks; ++copiedCybers)
+ for (; copiedCybers < cyberChunks; ++copiedCybers)
{
- if(raw_copy_to_user(buffer + copiedCybers * PAGE_SIZE, cyberSpace, cybersPerChunk * 8))
+ if (raw_copy_to_user(buffer + copiedCybers * PAGE_SIZE, cyberSpace, cybersPerChunk * 8))
{
return -EFAULT;
}
@@ -70,11 +96,13 @@ static ssize_t cyber_file_read(struct file * file, char __user * buffer, size_t
return cyberChunks * cybersPerChunk * 8;
}
-static int cyber_file_mmap(struct file * file, struct vm_area_struct * vma)
+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)
+ char * cyberSpace = file->private_data;
+
+ for (; vmaSize; vmaSize -= PAGE_SIZE, ++offset)
{
vm_insert_page(vma, vma->vm_start + offset * PAGE_SIZE, virt_to_page(cyberSpace));
}
@@ -90,49 +118,20 @@ static int cyber_file_mmap(struct file * file, struct vm_area_struct * vma)
* @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)
+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 cyber_file_init(struct file_operations *file_ops)
{
- 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];
- }
+ file_ops->open = cyber_file_open;
+ file_ops->read = cyber_file_read;
+ file_ops->write = cyber_file_write;
+ file_ops->release = cyber_file_close;
+ file_ops->mmap = cyber_file_mmap;
return 0;
}
@@ -140,5 +139,4 @@ int cyber_file_init(void)
void cyber_file_shutdown(void)
{
printk(KERN_INFO DEV_NAME ": Releasing CYBER space\n");
- free_page((unsigned long)cyberSpace);
-}
+} \ No newline at end of file
diff --git a/mod/src/cyber_lifecycle.c b/mod/src/cyber_lifecycle.c
index 02df37d..b5f1fab 100644
--- a/mod/src/cyber_lifecycle.c
+++ b/mod/src/cyber_lifecycle.c
@@ -20,17 +20,21 @@
#include "cyber_lifecycle.h"
#include <linux/module.h>
+#include <linux/fs.h>
+
+cyber_device device = {};
+struct file_operations file_ops = {.owner = THIS_MODULE};
int cyber_driver_init(void)
{
int error;
- if((error = cyber_file_init()))
+ if((error = cyber_file_init(&file_ops)))
{
return error;
}
- if((error = cyber_device_init()) < 0)
+ if((error = cyber_device_init(&device, &file_ops)) < 0)
{
cyber_file_shutdown();
return error;
@@ -41,7 +45,7 @@ int cyber_driver_init(void)
void cyber_driver_exit(void)
{
- cyber_device_shutdown();
+ cyber_device_shutdown(&device);
cyber_file_shutdown();
}