1 #include <linux/module.h>
2 /* Needed for struct file_operations and others */
4 /* Needed for struct cdev */
5 #include <linux/cdev.h>
6 /* Needed for copying to/from user space */
7 #include <asm/uaccess.h>
8 #include <linux/slab.h>
10 MODULE_LICENSE("GPL");
11 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
12 MODULE_DESCRIPTION("A hello world char device");
13 MODULE_VERSION("1.0.0");
15 #define BUFFER_SIZE 4096
16 #define DEVICE_NUMBER 2
23 /* Message buffer we send to upstream */
24 static struct message *hello_message[DEVICE_NUMBER];
26 static int helloc_open(struct inode *ino, struct file *filp)
29 printk(KERN_INFO "Opened file with minor %d\n", iminor(ino));
30 if (iminor(ino) >= DEVICE_NUMBER)
32 msg = hello_message[iminor(ino)];
33 filp->private_data = msg;
34 if (filp->f_flags & O_TRUNC) {
35 memset(msg->text, 0, msg->len);
37 printk(KERN_INFO "File opened with truncate flags\n");
39 if (filp->f_flags & O_APPEND) {
40 filp->f_pos = msg->len;
45 static int helloc_release(struct inode *ino, struct file *filp)
50 /* our read function writes our message to the user buffer */
51 static ssize_t helloc_read(struct file *filp, char __user *buf, size_t len,
55 struct message *msg = filp->private_data;
56 /* do not read pass through the size of the message */
58 /* return end of file */
60 /* if len is bigger than the rest of the message, clamp it */
61 if (len > msg->len - *pos)
62 len = msg->len - *pos;
63 /* copy message to user space and return error if it fails */
64 r = copy_to_user(buf, msg->text + *pos, len);
67 /* update the file position */
72 static ssize_t helloc_write(struct file *filp, const char __user *buf,
73 size_t len, loff_t *pos)
76 struct message *msg = filp->private_data;
77 /* do not read pass through the size of the message */
78 if (*pos >= BUFFER_SIZE)
79 /* return end of file */
81 /* if len is bigger than the rest of the message, clamp it */
82 if (len > BUFFER_SIZE - *pos)
83 len = BUFFER_SIZE - *pos;
85 /* copy message to user space and return error if it fails */
86 r = copy_from_user(msg->text + *pos, buf, len);
88 if (*pos + len > msg->len)
89 memset(msg->text + msg->len, 0, *pos + len - msg->len);
92 /* update the file position */
94 if (msg->len < *pos) {
95 printk(KERN_DEBUG "Updating size from %d to %lld\n", msg->len, *pos);
102 /* we only implement read */
103 static struct file_operations helloc_fops = {
104 .owner = THIS_MODULE,
106 .release = helloc_release,
108 .write = helloc_write,
111 /* the device number and the char device struct */
113 static struct cdev *cdev;
115 static void helloc_free(void)
118 for (i = 0; i < DEVICE_NUMBER; i++) {
119 if (hello_message[i])
120 kfree(hello_message[i]->text);
121 kfree(hello_message[i]);
125 static int __init helloc_alloc(void)
128 for (i = 0; i < DEVICE_NUMBER; i++) {
129 hello_message[i] = kzalloc(sizeof(struct message), GFP_KERNEL);
130 if (!hello_message[i])
132 hello_message[i]->text = kzalloc(BUFFER_SIZE, GFP_KERNEL);
133 if (!hello_message[i]->text)
138 for (; i >= 0; i--) {
139 if (hello_message[i])
140 kfree(hello_message[i]->text);
141 kfree(hello_message[i]);
146 static int __init helloc_init(void)
152 /* allocate any major number with only one minor */
153 r = alloc_chrdev_region(&dev, 0, DEVICE_NUMBER, "helloc");
157 /* print the major number allocated so we can create our device node */
158 printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev));
159 /* allocate the character device struct */
163 /* set the module owner and the file operations of our chardev */
164 cdev->owner = THIS_MODULE;
165 cdev->ops = &helloc_fops;
166 /* register the chardev to the system */
167 r = cdev_add(cdev, dev, DEVICE_NUMBER);
172 /* release memory allocated to the cdev device */
175 /* release the device number allocated */
176 unregister_chrdev_region(dev, DEVICE_NUMBER);
182 static void __exit helloc_exit(void)
184 /* remove the chardev from the system */
186 /* release the device number allocated */
187 unregister_chrdev_region(dev, DEVICE_NUMBER);
191 module_init(helloc_init);
192 module_exit(helloc_exit);