Begin work on rust versions of modules
This commit is contained in:
parent
1f2cc50f26
commit
91f8ca7f49
23 changed files with 53 additions and 9 deletions
|
@ -1,7 +0,0 @@
|
|||
obj-m += test_module.o
|
||||
|
||||
all:
|
||||
make -C $(LINUX_MODULES_FOLDER)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
make -C $(LINUX_MODULES_FOLDER)/build M=$(PWD) clean
|
|
@ -1,31 +0,0 @@
|
|||
## Additionnal informations
|
||||
|
||||
To test character device, your need create the device.
|
||||
|
||||
Step 1: Get major number of your module device
|
||||
|
||||
```bash
|
||||
cat /proc/devices | grep flodev
|
||||
```
|
||||
|
||||
Step 2: Create device (as root)
|
||||
|
||||
```bash
|
||||
mknod /dev/[wanted name] -c <major_number> 0
|
||||
```
|
||||
|
||||
Exemple (as root):
|
||||
|
||||
```bash
|
||||
cat /proc/devices | grep flodev # => 236 flodev
|
||||
mknod /dev/flodev0 c 236 0
|
||||
echo "Salut" >> /dev/flodev0
|
||||
dmesg | tail # =>
|
||||
# flodev - Ouverture du périphérique
|
||||
# flodev - Message reçu: Salut
|
||||
# flodev - Fermeture du périphérique
|
||||
rm /dev/flodev0
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
#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");
|
Loading…
Add table
Add a link
Reference in a new issue