Using udelay to busy wait for about 10 seconds.
[cascardo/kernel/samples/char2/.git] / hellochar.c
index c95100e..7f659f5 100644 (file)
@@ -20,6 +20,8 @@
 #include <linux/fs.h>
 #include <linux/cdev.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/sched.h>
 
 MODULE_LICENSE("GPL");
 
@@ -27,29 +29,30 @@ static dev_t devnum;
 static struct cdev *dev;
 static const char default_greeting[] = "Hello, World!\n";
 
-struct hello_buffer {
-       size_t len;
-       char buffer[0];
-};
+static DEFINE_SPINLOCK(hello_lock);
 
 static int hello_open(struct inode *ino, struct file *fp)
 {
-       struct hello_buffer *hello = kmalloc(sizeof(*hello) + 4000, GFP_KERNEL);
-       if (!hello)
-               return -ENOMEM;
-       hello->private_data = hello;
        return 0;
 }
 
 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
        loff_t *pos)
+{
+       int i = 1 << 20;
+       while (i--)
+               udelay(10);
+       return 0;
+}
+
+static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
+       loff_t *pos)
 {
        return 0;
 }
 
 static int hello_release(struct inode *ino, struct file *fp)
 {
-       kfree(fp->private_data);
        return 0;
 }
 
@@ -58,6 +61,7 @@ static const struct file_operations hello_fops = {
        .open = hello_open,
        .release = hello_release,
        .read = hello_read,
+       .write = hello_write,
 };
 
 static int __init ch_init(void)
@@ -65,7 +69,7 @@ static int __init ch_init(void)
        int r = 0;
        r = alloc_chrdev_region(&devnum, 0, 256, "hello");
        if (r)
-               goto out;
+               goto reg_out;
        dev = cdev_alloc();
        if (!dev) {
                r = -ENOMEM;
@@ -81,7 +85,7 @@ add_out:
        kfree(dev);
 cdev_out:
        unregister_chrdev_region(devnum, 256);
-out:
+reg_out:
        return r;
 }