Implemented open/release with multiple devices.
[cascardo/kernel/samples/02.char/.git] / helloc.c
index 4d99fcb..8c085f3 100644 (file)
--- a/helloc.c
+++ b/helloc.c
 #include <linux/module.h>
+/* Needed for struct file_operations and others */
 #include <linux/fs.h>
+/* Needed for struct cdev */
 #include <linux/cdev.h>
+/* Needed for copying to/from user space */
 #include <asm/uaccess.h>
+#include <linux/slab.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
 MODULE_DESCRIPTION("A hello world char device");
 MODULE_VERSION("1.0.0");
 
-static char helloc_message[] = "hello, world\n";
+/* Message buffer we send to upstream */
+static char hello_message[] = "hello, world\n";
+static char goodbye_message[] = "goodbye, world\n";
 
+struct message {
+       char *text;
+       size_t len;
+};
+
+static int helloc_open(struct inode *ino, struct file *filp)
+{
+       struct message *msg;
+       printk(KERN_INFO "Opened file with minor %d\n", iminor(ino));
+       msg = kmalloc(sizeof(struct message), GFP_KERNEL);
+       if (!msg)
+               return -ENOMEM;
+       if (iminor(ino) == 0) {
+               msg->text = hello_message;
+               msg->len = sizeof(hello_message);
+       } else {
+               msg->text = goodbye_message;
+               msg->len = sizeof(goodbye_message);
+       }
+       filp->private_data = msg;
+       return 0;
+}
+
+static int helloc_release(struct inode *ino, struct file *filp)
+{
+       kfree(filp->private_data);
+       return 0;
+}
+
+/* our read function writes our message to the user buffer */
 static ssize_t helloc_read(struct file *filp, char __user *buf, size_t len,
                           loff_t *pos)
 {
        int r;
-       if (*pos >= sizeof(helloc_message))
+       struct message *msg = filp->private_data;
+       /* do not read pass through the size of the message */
+       if (*pos >= msg->len)
+       /* return end of file */
                return 0;
-       if (len > sizeof(helloc_message) - *pos)
-               len = sizeof(helloc_message) - *pos;
-       r = copy_to_user(buf, helloc_message + *pos, len);
+       /* if len is bigger than the rest of the message, clamp it */
+       if (len > msg->len - *pos)
+               len = msg->len - *pos;
+       /* copy message to user space and return error if it fails */
+       r = copy_to_user(buf, msg->text + *pos, len);
        if (r)
                return -EFAULT;
+       /* update the file position */
        *pos += len;
        return len;
 }
 
+/* we only implement read */
 static struct file_operations helloc_fops = {
        .owner = THIS_MODULE,
+       .open = helloc_open,
+       .release = helloc_release,
        .read = helloc_read,
 };
 
+/* the device number and the char device struct */
 static dev_t dev;
 static struct cdev *cdev;
 
 static int __init helloc_init(void)
 {
        int r;
-       r = alloc_chrdev_region(&dev, 0, 1, "helloc");
+       /* allocate any major number with only one minor */
+       r = alloc_chrdev_region(&dev, 0, 2, "helloc");
        if (r)
                goto out_region;
        r = -ENOMEM;
+       /* print the major number allocated so we can create our device node */
        printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev));
+       /* allocate the character device struct */
        cdev = cdev_alloc();
        if (!cdev)
                goto out_alloc;
+       /* set the module owner and the file operations of our chardev */
        cdev->owner = THIS_MODULE;
        cdev->ops = &helloc_fops;
-       r = cdev_add(cdev, dev, 1);
+       /* register the chardev to the system */
+       r = cdev_add(cdev, dev, 2);
        if (r)
                goto out_add;
        return 0;
 out_add:
-       kobject_put(&cdev->kobj);
+       /* release memory allocated to the cdev device */
+       kfree(cdev);
 out_alloc:
-       unregister_chrdev_region(dev, 1);
+       /* release the device number allocated */
+       unregister_chrdev_region(dev, 2);
 out_region:
        return r;
 }
 
 static void __exit helloc_exit(void)
 {
+       /* remove the chardev from the system */
        cdev_del(cdev);
-       unregister_chrdev_region(dev, 1);
+       /* release the device number allocated */
+       unregister_chrdev_region(dev, 2);
 }
 
 module_init(helloc_init);