Use timer to print our default greeting.
[cascardo/kernel/samples/char2/.git] / hellochar.c
index 3830a47..938b32f 100644 (file)
@@ -21,7 +21,7 @@
 #include <linux/cdev.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/sched.h>
+#include <linux/timer.h>
 
 MODULE_LICENSE("GPL");
 
@@ -36,14 +36,21 @@ static int hello_open(struct inode *ino, struct file *fp)
        return 0;
 }
 
+static void hello_world(unsigned long greeting)
+{
+       printk(KERN_INFO "%s", (char *) greeting);
+}
+
+static struct timer_list hello_timer;
 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
        loff_t *pos)
 {
-       unsigned long timeout = 5 * HZ;
-       while (timeout > 0) {
-               set_current_state(TASK_INTERRUPTIBLE);
-               timeout = schedule_timeout(timeout);
-       }
+       printk(KERN_INFO "Adding timer...\n");
+       init_timer(&hello_timer);
+       hello_timer.expires = jiffies + 5 * HZ;
+       hello_timer.function = hello_world;
+       hello_timer.data = (unsigned long) default_greeting;
+       add_timer(&hello_timer);
        return 0;
 }