Skip to content
Snippets Groups Projects
Commit fd13f03a authored by valentin.acevedoa's avatar valentin.acevedoa
Browse files

master commit

parent 9e55a316
No related branches found
No related tags found
No related merge requests found
abcd.c 0 → 100644
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/types.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Valentin Acevedo <valentin.acevedo-arroyo@etu.hesge.ch>");
MODULE_DESCRIPTION("Alphabet buffer");
MODULE_VERSION("1.0");
// Buffer
static int buff_size = 26;
static char *buff;
static dev_t major;
struct mydevice {
char *string_data;
int int_data;
struct cdev mycdev;
};
static struct mydevice peripherique;
static ssize_t custom_write(struct file *file, const char *buff, size_t len, loff_t *ppos) {
int real_buff_size = 0, write_buff_size = 0;
if(len <= buff_size - *ppos) {
real_buff_size = len;
} else {
real_buff_size = buff_size - *ppos;
}
if(real_buff_size) {
write_buff_size = copy_from_user((void*)buff + *ppos, buff, real_buff_size);
}
if(buff_size <*ppos + real_buff_size) {
*ppos = 0;
} else {
*ppos += real_buff_size;
}
return real_buff_size - write_buff_size;
}
static ssize_t custom_read(struct file *file, char *buff, size_t len, loff_t *ppos) {
int real_buff_size = 0, read_buff_size = 0;
if(buff_size - *ppos > len) {
real_buff_size = len;
} else {
real_buff_size = buff_size - *ppos;
}
if(real_buff_size) {
read_buff_size = copy_to_user(buff, buff + *ppos, real_buff_size);
}
if(buff_size < *ppos + real_buff_size) {
*ppos = 0;
} else {
*ppos += real_buff_size;
}
return real_buff_size - read_buff_size;
}
//Map used functions to custom created functions
static struct file_operations custom_operations = {
read : custom_read,
write : custom_write
};
static int __init custom_init(void) {
int val;
buff = kmalloc(buff_size * sizeof(char), GFP_KERNEL);
memcpy(buff, "abcdefghijklmnopqrstuvwxyz", buff_size * sizeof(char));
val = alloc_chrdev_region(&major, 0, 1, "abcd");
if(val < 0) {
printk(KERN_WARNING "Module loading failure\n");
return val;
}
cdev_init(&(peripherique.mycdev), &custom_operations);
cdev_add(&(peripherique.mycdev), major, 1);
printk(KERN_DEBUG "Debug");
return 0;
}
static void __exit custom_exit(void) {
//Freeing buffer
unregister_chrdev_region(major, 1);
kfree(buff), buff = NULL;
cdev_del(&(peripherique.mycdev));
}
module_init(custom_init);
module_exit(custom_exit);
\ No newline at end of file
#include <linux/module.h>
#define INCLUDE_VERMAGIC
#include <linux/build-salt.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
BUILD_SALT;
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__section(".gnu.linkonce.this_module") = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef CONFIG_RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
MODULE_INFO(depends, "");
MODULE_INFO(srcversion, "907842194018E903497EAD0");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment