Use mutex.
[cascardo/kernel/samples/char2/.git] / hellochar.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <linux/cdev.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <asm/uaccess.h>
25
26 MODULE_LICENSE("GPL");
27
28 #define MAXLEN 4000
29 static dev_t devnum;
30 static struct cdev *dev;
31 static const char default_greeting[] = "Hello, World!\n";
32
33 struct hello_buffer {
34         size_t len;
35         char buffer[0];
36 };
37 static struct hello_buffer *hello;
38 static DEFINE_MUTEX(hello_mtx);
39
40 static int hello_open(struct inode *ino, struct file *fp)
41 {
42         if (mutex_lock_interruptible(&hello_mtx))
43                 return -ERESTARTSYS;
44         if (fp->f_flags & O_TRUNC) {
45                 memset(hello->buffer, 0, MAXLEN);
46                 hello->len = 0;
47         }
48         if (fp->f_flags & O_APPEND)
49                 fp->f_pos = hello->len;
50         mutex_unlock(&hello_mtx);
51         return 0;
52 }
53
54 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
55         loff_t *pos)
56 {
57         int r;
58         if (mutex_lock_interruptible(&hello_mtx))
59                 return -ERESTARTSYS;
60         if (sz + *pos > hello->len)
61                 sz = hello->len - *pos;
62         r = copy_to_user(buf, hello->buffer + *pos, sz);
63         mutex_unlock(&hello_mtx);
64         if (r)
65                 return -EFAULT;
66         *pos += sz;
67         return sz;
68 }
69
70 static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
71         loff_t *pos)
72 {
73         int r;
74         if (mutex_lock_interruptible(&hello_mtx))
75                 return -ERESTARTSYS;
76         if (sz + *pos > MAXLEN)
77                 sz = MAXLEN - *pos;
78         r = copy_from_user(hello->buffer + *pos, buf, sz);
79         if (r) {
80                 mutex_unlock(&hello_mtx);
81                 return -EFAULT;
82         }
83         *pos += sz;
84         if (hello->len < *pos)
85                 hello->len = *pos;
86         mutex_unlock(&hello_mtx);
87         return sz;
88 }
89
90 static int hello_release(struct inode *ino, struct file *fp)
91 {
92         return 0;
93 }
94
95 static const struct file_operations hello_fops = {
96         .owner = THIS_MODULE,
97         .open = hello_open,
98         .release = hello_release,
99         .read = hello_read,
100         .write = hello_write,
101 };
102
103 static int __init ch_init(void)
104 {
105         int r = 0;
106         hello = kzalloc(sizeof(*hello) + MAXLEN, GFP_KERNEL);
107         if (!hello) {
108                 r = -ENOMEM;
109                 goto out;
110         }
111         memcpy(hello->buffer, default_greeting, sizeof(default_greeting));
112         hello->len = sizeof(default_greeting);
113         r = alloc_chrdev_region(&devnum, 0, 256, "hello");
114         if (r)
115                 goto reg_out;
116         dev = cdev_alloc();
117         if (!dev) {
118                 r = -ENOMEM;
119                 goto cdev_out;
120         }
121         dev->ops = &hello_fops;
122         r = cdev_add(dev, devnum, 256);
123         if (r)
124                 goto add_out;
125         printk(KERN_DEBUG "Allocate major %d\n", MAJOR(devnum));
126         return 0;
127 add_out:
128         kfree(dev);
129 cdev_out:
130         unregister_chrdev_region(devnum, 256);
131 reg_out:
132         kfree(hello);
133 out:
134         return r;
135 }
136
137 static void __exit ch_exit(void)
138 {
139         cdev_del(dev);
140         unregister_chrdev_region(devnum, 256);
141         kfree(hello);
142 }
143
144 module_init(ch_init);
145 module_exit(ch_exit);