Use tasklet to schedule printing our default greeting.
[cascardo/kernel/samples/char2/.git] / hellochar.c
index 85510f7..91e8d69 100644 (file)
 #include <linux/fs.h>
 #include <linux/cdev.h>
 #include <linux/slab.h>
-#include <linux/semaphore.h>
-#include <asm/uaccess.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
 
 MODULE_LICENSE("GPL");
 
-#define MAXLEN 4000
 static dev_t devnum;
 static struct cdev *dev;
-static const char default_greeting[] = "Hello, World!\n";
+static const char def_greeting[] = "Hello, World!\n";
 
-struct hello_buffer {
-       size_t len;
-       char buffer[0];
-};
-static struct hello_buffer *hello;
-static DECLARE_MUTEX(hello_mtx);
+static DEFINE_SPINLOCK(hello_lock);
 
 static int hello_open(struct inode *ino, struct file *fp)
 {
-       down(&hello_mtx);
-       if (fp->f_flags & O_TRUNC) {
-               memset(hello->buffer, 0, MAXLEN);
-               hello->len = 0;
-       }
-       if (fp->f_flags & O_APPEND)
-               fp->f_pos = hello->len;
-       up(&hello_mtx);
        return 0;
 }
 
+static void hello_world(unsigned long greeting)
+{
+       printk(KERN_INFO "%s", (char *) greeting);
+}
+
+static DECLARE_TASKLET(hello_tasklet, hello_world, def_greeting);
+
 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
        loff_t *pos)
 {
-       int r;
-       down(&hello_mtx);
-       if (sz + *pos > hello->len)
-               sz = hello->len - *pos;
-       r = copy_to_user(buf, hello->buffer + *pos, sz);
-       up(&hello_mtx);
-       if (r)
-               return -EFAULT;
-       *pos += sz;
-       return sz;
+       printk(KERN_INFO "Scheduling tasklet...\n");
+       tasklet_schedule(&hello_tasklet);
+       return 0;
 }
 
 static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
        loff_t *pos)
 {
-       int r;
-       down(&hello_mtx);
-       if (sz + *pos > MAXLEN)
-               sz = MAXLEN - *pos;
-       r = copy_from_user(hello->buffer + *pos, buf, sz);
-       if (r) {
-               up(&hello_mtx);
-               return -EFAULT;
-       }
-       *pos += sz;
-       if (hello->len < *pos)
-               hello->len = *pos;
-       up(&hello_mtx);
-       return sz;
+       return 0;
 }
 
 static int hello_release(struct inode *ino, struct file *fp)
@@ -100,13 +73,6 @@ static const struct file_operations hello_fops = {
 static int __init ch_init(void)
 {
        int r = 0;
-       hello = kzalloc(sizeof(*hello) + MAXLEN, GFP_KERNEL);
-       if (!hello) {
-               r = -ENOMEM;
-               goto out;
-       }
-       memcpy(hello->buffer, default_greeting, sizeof(default_greeting));
-       hello->len = sizeof(default_greeting);
        r = alloc_chrdev_region(&devnum, 0, 256, "hello");
        if (r)
                goto reg_out;
@@ -126,16 +92,14 @@ add_out:
 cdev_out:
        unregister_chrdev_region(devnum, 256);
 reg_out:
-       kfree(hello);
-out:
        return r;
 }
 
 static void __exit ch_exit(void)
 {
+       tasklet_kill(&hello_tasklet);
        cdev_del(dev);
        unregister_chrdev_region(devnum, 256);
-       kfree(hello);
 }
 
 module_init(ch_init);