Serialize access to global structure using semaphore.
[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/semaphore.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 DECLARE_MUTEX(hello_mtx);
39
40 static int hello_open(struct inode *ino, struct file *fp)
41 {
42         down(&hello_mtx);
43         if (fp->f_flags & O_TRUNC) {
44                 memset(hello->buffer, 0, MAXLEN);
45                 hello->len = 0;
46         }
47         if (fp->f_flags & O_APPEND)
48                 fp->f_pos = hello->len;
49         up(&hello_mtx);
50         return 0;
51 }
52
53 static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
54         loff_t *pos)
55 {
56         int r;
57         down(&hello_mtx);
58         if (sz + *pos > hello->len)
59                 sz = hello->len - *pos;
60         r = copy_to_user(buf, hello->buffer + *pos, sz);
61         up(&hello_mtx);
62         if (r)
63                 return -EFAULT;
64         *pos += sz;
65         return sz;
66 }
67
68 static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
69         loff_t *pos)
70 {
71         int r;
72         down(&hello_mtx);
73         if (sz + *pos > MAXLEN)
74                 sz = MAXLEN - *pos;
75         r = copy_from_user(hello->buffer + *pos, buf, sz);
76         if (r) {
77                 up(&hello_mtx);
78                 return -EFAULT;
79         }
80         *pos += sz;
81         if (hello->len < *pos)
82                 hello->len = *pos;
83         up(&hello_mtx);
84         return sz;
85 }
86
87 static int hello_release(struct inode *ino, struct file *fp)
88 {
89         return 0;
90 }
91
92 static const struct file_operations hello_fops = {
93         .owner = THIS_MODULE,
94         .open = hello_open,
95         .release = hello_release,
96         .read = hello_read,
97         .write = hello_write,
98 };
99
100 static int __init ch_init(void)
101 {
102         int r = 0;
103         hello = kzalloc(sizeof(*hello) + MAXLEN, GFP_KERNEL);
104         if (!hello) {
105                 r = -ENOMEM;
106                 goto out;
107         }
108         memcpy(hello->buffer, default_greeting, sizeof(default_greeting));
109         hello->len = sizeof(default_greeting);
110         r = alloc_chrdev_region(&devnum, 0, 256, "hello");
111         if (r)
112                 goto reg_out;
113         dev = cdev_alloc();
114         if (!dev) {
115                 r = -ENOMEM;
116                 goto cdev_out;
117         }
118         dev->ops = &hello_fops;
119         r = cdev_add(dev, devnum, 256);
120         if (r)
121                 goto add_out;
122         printk(KERN_DEBUG "Allocate major %d\n", MAJOR(devnum));
123         return 0;
124 add_out:
125         kfree(dev);
126 cdev_out:
127         unregister_chrdev_region(devnum, 256);
128 reg_out:
129         kfree(hello);
130 out:
131         return r;
132 }
133
134 static void __exit ch_exit(void)
135 {
136         cdev_del(dev);
137         unregister_chrdev_region(devnum, 256);
138         kfree(hello);
139 }
140
141 module_init(ch_init);
142 module_exit(ch_exit);