Started work on the kernel module
This commit is contained in:
@@ -381,3 +381,77 @@ clean:
|
|||||||
|
|
||||||
### Développement d’un module noyau
|
### Développement d’un module noyau
|
||||||
|
|
||||||
|
On crée un repertoire sur la VM de developpement sous /tp/module
|
||||||
|
|
||||||
|
On va prendre le squelette du module sur le git
|
||||||
|
|
||||||
|
On va y aller étape par étape :
|
||||||
|
|
||||||
|
#### 1 initialisation du driver
|
||||||
|
|
||||||
|
Les étapes pour bien initialiser notre driver sont :
|
||||||
|
|
||||||
|
1. Enregistrer notre module en obtenant un numéro majeur
|
||||||
|
2. Créer la classe
|
||||||
|
3. Créer le module dans /dev
|
||||||
|
|
||||||
|
Les trois étapes ci dessus ont pu ête implémentées avec l'aide des slides du cours.
|
||||||
|
|
||||||
|
4. Demander les différents GPIOS
|
||||||
|
5. Enregistrer les différentes interruptions pour chaque GPIO
|
||||||
|
|
||||||
|
```c
|
||||||
|
int major;
|
||||||
|
struct class *j_class;
|
||||||
|
struct device *j_device;
|
||||||
|
//mapping des fonctions classiques d'un char device
|
||||||
|
static const struct file_operations fops = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = dev_open
|
||||||
|
};
|
||||||
|
static int __init mylab1_joystick_dev_init(void) {
|
||||||
|
// TODO
|
||||||
|
// 1) Register the device by dynamically obtaining a major number
|
||||||
|
major = register_chrdev(0, "mylab1_joystick", fops);
|
||||||
|
if(major < 0){
|
||||||
|
pr_info("mylab1_joystick: could not get a major number, initialisation failed\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// 2) Create the class
|
||||||
|
j_class = class_create("mylab1_joystick");
|
||||||
|
if (IS_ERR(j_class)){
|
||||||
|
pr_info("mylab1_joystick: could not create class, initialisation failed\n");
|
||||||
|
return -1:
|
||||||
|
}
|
||||||
|
// 3) Create the device in /dev
|
||||||
|
j_device = device_create(j_class, NULL, MKDEV(major, 0), NULL, "mylab1_joystick");
|
||||||
|
if (IS_ERR(j_device)){
|
||||||
|
pr_info("mylab1_joystick: could not create device, initialisation failed\n");
|
||||||
|
return -1:
|
||||||
|
}
|
||||||
|
// 6) Request the necessary GPIOs
|
||||||
|
// 7) Register an IRQ handler per GPIO
|
||||||
|
pr_info("mylab1_joystick: driver initialized\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
par la même occasion avec le code ci dessus on peut déja préparer la sortie du module :
|
||||||
|
|
||||||
|
```c
|
||||||
|
static void __exit mylab1_joystick_dev_exit(void) {
|
||||||
|
// TODO
|
||||||
|
// 1) Destroy the device
|
||||||
|
device_destroy(j_device,MKDEV(major, 0));
|
||||||
|
// 2) Destroy the class
|
||||||
|
class_destroy(j_class);
|
||||||
|
// 4) Unregister the device
|
||||||
|
unregister_chrdev(major,"mylab1_joystick");
|
||||||
|
// 5) Free the IRQs
|
||||||
|
// 6) Free the GPIOs
|
||||||
|
pr_info("mylab1_joystick: driver destroyed\n");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Normalement avec le squelette modifié de la sorte on devrait avoir un driver minimal qui ne sert à rien mais qui se créée correctement dans le kernel linux.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user