93 lines
2.5 KiB
C
93 lines
2.5 KiB
C
#include <linux/module.h>
|
|
#include <linux/device.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/gpio.h>
|
|
|
|
// Operation prototypes
|
|
static int dev_open(struct inode *, struct file *);
|
|
|
|
int major;
|
|
struct class *j_class;
|
|
struct device *j_device;
|
|
|
|
// The driver's file operations
|
|
static const struct file_operations fops = {
|
|
.owner = THIS_MODULE,
|
|
.open = dev_open
|
|
};
|
|
|
|
/**
|
|
* Driver initialization code.
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* This function is called when the module is unloaded.
|
|
*/
|
|
static void __exit mylab1_joystick_dev_exit(void) {
|
|
// TODO
|
|
// 1) Destroy the device
|
|
device_destroy(j_class,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");
|
|
}
|
|
|
|
/**
|
|
* Open operation
|
|
*/
|
|
static int dev_open(struct inode *inod, struct file *f) {
|
|
pr_info("mylab1_joystick: device opened\n");
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Joystick left IRQ handler
|
|
*/
|
|
/*
|
|
static irqreturn_t mylab_left_irq_handler(int irq, void *dev_id) {
|
|
// TODO
|
|
// - Operations to be done when the left position of the joystick is triggered
|
|
// - At minimum, the joystick state must be updated
|
|
return (irqreturn_t) IRQ_HANDLED; // Announce that the IRQ has been handled correctly
|
|
}
|
|
*/
|
|
|
|
module_init(mylab1_joystick_dev_init);
|
|
module_exit(mylab1_joystick_dev_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Jaime Coder <maxime.rohmer@hesge.ch>");
|
|
MODULE_DESCRIPTION("Module to drive the joystick on the myLab1 card");
|
|
MODULE_VERSION("0.1"); |