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 /* Needed for using misc device stuff */
9 #include <linux/miscdevice.h>
11 MODULE_LICENSE("GPL");
12 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
13 MODULE_DESCRIPTION("A hello world char device");
14 MODULE_VERSION("1.0.0");
16 /* Message buffer we send to upstream */
17 static char helloc_message[] = "hello, world\n";
19 /* our read function writes our message to the user buffer */
20 static ssize_t helloc_read(struct file *filp, char __user *buf, size_t len,
24 /* do not read pass through the size of the message */
25 if (*pos >= sizeof(helloc_message))
26 /* return end of file */
28 /* if len is bigger than the rest of the message, clamp it */
29 if (len > sizeof(helloc_message) - *pos)
30 len = sizeof(helloc_message) - *pos;
31 /* copy message to user space and return error if it fails */
32 r = copy_to_user(buf, helloc_message + *pos, len);
35 /* update the file position */
40 /* we only implement read */
41 static struct file_operations helloc_fops = {
46 static struct miscdevice hello_misc = {
47 .minor = MISC_DYNAMIC_MINOR,
52 static int __init helloc_init(void)
55 r = misc_register(&hello_misc);
59 static void __exit helloc_exit(void)
61 misc_deregister(&hello_misc);
64 module_init(helloc_init);
65 module_exit(helloc_exit);