diff --git a/kernel/kmod.c b/kernel/kmod.c index cb13babf2e53a3abfe4efb7bbbc4c031100b50c4..e29d2387f6a1d7d4b718c3bcbf9a98870f4be22a 100644 --- a/kernel/kmod.c +++ b/kernel/kmod.c @@ -4,17 +4,23 @@ #include <linux/cdev.h> #include <linux/vmalloc.h> #include <linux/uaccess.h> +#include <linux/hrtimer.h> #include "../common/include/message.h" #define DEVICE_NAME "amogus" #define CLASS_NAME "amogus_class" +#define SLOT_INTERVAL_NS 250000 + static int major_number; static struct class* class = NULL; static struct cdev mycdev; static message_t* message; +static struct hrtimer timer; +static ktime_t interval; + static int device_open(struct inode *inode, struct file *file) { return 0; @@ -48,6 +54,11 @@ static const struct file_operations fops = { .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) { dev_t dev; @@ -93,6 +104,12 @@ static int __init lkm_init(void) } 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; } @@ -104,6 +121,8 @@ static void __exit lkm_exit(void) class_destroy(class); unregister_chrdev_region(MKDEV(major_number, 0), 1); printk(KERN_INFO "lkm: device removed successfully\n"); + + hrtimer_cancel(&timer); } module_init(lkm_init);