Skip to content
Snippets Groups Projects
Commit 15b1b4d0 authored by Leon Bohnwagner's avatar Leon Bohnwagner :crab:
Browse files

feat: add hrtimer to kernel module

parent 6c8c0a3e
No related branches found
No related tags found
No related merge requests found
...@@ -4,17 +4,23 @@ ...@@ -4,17 +4,23 @@
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/hrtimer.h>
#include "../common/include/message.h" #include "../common/include/message.h"
#define DEVICE_NAME "amogus" #define DEVICE_NAME "amogus"
#define CLASS_NAME "amogus_class" #define CLASS_NAME "amogus_class"
#define SLOT_INTERVAL_NS 250000
static int major_number; static int major_number;
static struct class* class = NULL; static struct class* class = NULL;
static struct cdev mycdev; static struct cdev mycdev;
static message_t* message; static message_t* message;
static struct hrtimer timer;
static ktime_t interval;
static int device_open(struct inode *inode, struct file *file) static int device_open(struct inode *inode, struct file *file)
{ {
return 0; return 0;
...@@ -48,6 +54,11 @@ static const struct file_operations fops = { ...@@ -48,6 +54,11 @@ static const struct file_operations fops = {
.release = device_release .release = device_release
}; };
static enum hrtimer_restart timer_callback(struct hrtimer *timer) {
hrtimer_forward_now(timer, interval);
return HRTIMER_RESTART;
}
static int __init lkm_init(void) static int __init lkm_init(void)
{ {
dev_t dev; dev_t dev;
...@@ -93,6 +104,12 @@ static int __init lkm_init(void) ...@@ -93,6 +104,12 @@ static int __init lkm_init(void)
} }
printk(KERN_INFO "lkm: device created successfully\n"); printk(KERN_INFO "lkm: device created successfully\n");
interval = ktime_set(0, SLOT_INTERVAL_NS);
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = timer_callback;
hrtimer_start(&timer, interval, HRTIMER_MODE_REL);
return 0; return 0;
} }
...@@ -104,6 +121,8 @@ static void __exit lkm_exit(void) ...@@ -104,6 +121,8 @@ static void __exit lkm_exit(void)
class_destroy(class); class_destroy(class);
unregister_chrdev_region(MKDEV(major_number, 0), 1); unregister_chrdev_region(MKDEV(major_number, 0), 1);
printk(KERN_INFO "lkm: device removed successfully\n"); printk(KERN_INFO "lkm: device removed successfully\n");
hrtimer_cancel(&timer);
} }
module_init(lkm_init); module_init(lkm_init);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment