Added len attribute.
[cascardo/kernel/samples/char2/.git] / hellochar.c
index a22677d..940bfeb 100644 (file)
@@ -23,6 +23,9 @@
 #include <linux/mutex.h>
 #include <asm/uaccess.h>
 #include <linux/circ_buf.h>
+#include <linux/sched.h>
+#include <linux/completion.h>
+#include <linux/device.h>
 
 MODULE_LICENSE("GPL");
 
@@ -34,6 +37,7 @@ static const char default_greeting[] = "Hello, World!\n";
 static struct circ_buf *hello;
 static struct circ_buf hello_instance;
 static DEFINE_MUTEX(hello_mtx);
+static DECLARE_COMPLETION(hello_wait);
 
 static int hello_open(struct inode *ino, struct file *fp)
 {
@@ -63,6 +67,7 @@ static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
        mutex_unlock(&hello_mtx);
        if (r)
                return -EFAULT;
+       complete(&hello_wait);
        return sz;
 }
 
@@ -76,6 +81,14 @@ static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
        if (mutex_lock_interruptible(&hello_mtx))
                return -ERESTARTSYS;
        len = CIRC_SPACE(hello->head, hello->tail, MAXLEN);
+       while (len == 0) {
+               mutex_unlock(&hello_mtx);
+               if (wait_for_completion_interruptible(&hello_wait))
+                       return -ERESTARTSYS;
+               if (mutex_lock_interruptible(&hello_mtx))
+                       return -ERESTARTSYS;
+               len = CIRC_SPACE(hello->head, hello->tail, MAXLEN);
+       }
        if (sz > len)
                sz = len;
        fsz = min(sz, CIRC_SPACE_TO_END(hello->head, hello->tail, MAXLEN));
@@ -104,6 +117,21 @@ static const struct file_operations hello_fops = {
        .write = hello_write,
 };
 
+static ssize_t hello_len_show(struct device *dev, struct device_attribute *attr,
+       char *buffer)
+{
+       size_t len = CIRC_CNT(hello->head, hello->tail, MAXLEN);
+       return sprintf(buffer, "%d\n", len);
+}
+
+static const struct device_attribute hello_len_attr = {
+       .attr = { .name = "len", .mode = 0444, },
+       .show = hello_len_show,
+};
+
+static struct class *hello_class;
+static struct device *hello_dev;
+
 static int __init ch_init(void)
 {
        int r = 0;
@@ -113,11 +141,24 @@ static int __init ch_init(void)
                r = -ENOMEM;
                goto out;
        }
+       hello_class = class_create(THIS_MODULE, "hello");
+       if (IS_ERR(hello_class)) {
+               r = PTR_ERR(hello_class);
+               goto class_out;
+       }
        memcpy(hello->buf, default_greeting, sizeof(default_greeting));
        hello->head = sizeof(default_greeting);
        r = alloc_chrdev_region(&devnum, 0, 256, "hello");
        if (r)
                goto reg_out;
+       hello_dev = device_create(hello_class, NULL, devnum, NULL, "hello");
+       if (!hello_dev) {
+               r = -ENODEV;
+               goto dev_out;
+       }
+       r = device_create_file(hello_dev, &hello_len_attr);
+       if (r)
+               goto file_out;
        dev = cdev_alloc();
        if (!dev) {
                r = -ENOMEM;
@@ -131,8 +172,14 @@ static int __init ch_init(void)
 add_out:
        kfree(dev);
 cdev_out:
+       device_remove_file(hello_dev, &hello_len_attr);
+file_out:
+       device_destroy(hello_class, devnum);
+dev_out:
        unregister_chrdev_region(devnum, 256);
 reg_out:
+       class_destroy(hello_class);
+class_out:
        kfree(hello);
 out:
        return r;
@@ -141,8 +188,11 @@ out:
 static void __exit ch_exit(void)
 {
        cdev_del(dev);
+       device_remove_file(hello_dev, &hello_len_attr);
+       device_destroy(hello_class, devnum);
        unregister_chrdev_region(devnum, 256);
        kfree(hello->buf);
+       class_destroy(hello_class);
 }
 
 module_init(ch_init);