83 lines
2.3 KiB
C
83 lines
2.3 KiB
C
#include <linux/fs.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/uaccess.h>
|
|
|
|
#define DEVICE_NAME "flodev"
|
|
#define CLASS_NAME "chardrv"
|
|
|
|
static int major_number;
|
|
static char msg[256] = {0};
|
|
static short size_of_msg;
|
|
static int device_open(struct inode *, struct file *);
|
|
static int device_release(struct inode *, struct file *);
|
|
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
|
|
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
|
|
|
|
static struct file_operations fops = {
|
|
.read = device_read,
|
|
.write = device_write,
|
|
.open = device_open,
|
|
.release = device_release,
|
|
};
|
|
|
|
static int __init basic_module_init(void) {
|
|
pr_info("Bonjour! Le module est chargé.\n");
|
|
major_number = register_chrdev(0, DEVICE_NAME, &fops);
|
|
if (major_number < 0) {
|
|
pr_info("Erreur lors de l'enregistrement du périphérique de caractère\n");
|
|
return major_number;
|
|
}
|
|
pr_info("Périphérique de caractère enregistré avec le numéro de majeur %d\n", major_number);
|
|
return 0;
|
|
}
|
|
|
|
static void __exit basic_module_exit(void) {
|
|
unregister_chrdev(major_number, DEVICE_NAME);
|
|
pr_info("Au revoir! Le module est déchargé.\n");
|
|
}
|
|
|
|
static int device_open(struct inode *inode, struct file *file) {
|
|
pr_info("flodev - Ouverture du périphérique\n");
|
|
return 0;
|
|
}
|
|
|
|
static int device_release(struct inode *inode, struct file *file) {
|
|
pr_info("flodev - Fermeture du périphérique\n");
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset) {
|
|
int bytes_read = 0;
|
|
if (*offset >= size_of_msg) {
|
|
return 0;
|
|
}
|
|
if (*offset + length > size_of_msg) {
|
|
length = size_of_msg - *offset;
|
|
}
|
|
if (copy_to_user(buffer, msg + *offset, length)) {
|
|
return -EFAULT;
|
|
}
|
|
*offset += length;
|
|
bytes_read = length;
|
|
pr_info("flodev - Lecture de %d bytes\n", bytes_read);
|
|
return bytes_read;
|
|
}
|
|
|
|
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
|
|
if (copy_from_user(msg, buff, len)) {
|
|
return -EFAULT;
|
|
}
|
|
size_of_msg = len;
|
|
pr_info("flodev - Message reçu: %s\n", msg);
|
|
return len;
|
|
}
|
|
|
|
module_init(basic_module_init);
|
|
module_exit(basic_module_exit);
|
|
|
|
MODULE_LICENSE("MIT License");
|
|
MODULE_AUTHOR("Florian RICHER");
|
|
MODULE_DESCRIPTION("Un module noyau avec un périphérique de caractère");
|
|
MODULE_VERSION("1.0");
|