1 #include <linux/module.h>
3 #include <linux/cdev.h>
4 #include <asm/uaccess.h>
7 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
8 MODULE_DESCRIPTION("A hello world char device");
9 MODULE_VERSION("1.0.0");
11 static char helloc_message[] = "hello, world\n";
13 static ssize_t helloc_read(struct file *filp, char __user *buf, size_t len,
17 if (*pos >= sizeof(helloc_message))
19 if (len > sizeof(helloc_message) - *pos)
20 len = sizeof(helloc_message) - *pos;
21 r = copy_to_user(buf, helloc_message + *pos, len);
28 static struct file_operations helloc_fops = {
34 static struct cdev *cdev;
36 static int __init helloc_init(void)
39 r = alloc_chrdev_region(&dev, 0, 1, "helloc");
43 printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev));
47 cdev->owner = THIS_MODULE;
48 cdev->ops = &helloc_fops;
49 r = cdev_add(cdev, dev, 1);
54 kobject_put(&cdev->kobj);
56 unregister_chrdev_region(dev, 1);
61 static void __exit helloc_exit(void)
64 unregister_chrdev_region(dev, 1);
67 module_init(helloc_init);
68 module_exit(helloc_exit);