Use misc device. misc
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 8 Dec 2009 11:27:04 +0000 (09:27 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Tue, 8 Dec 2009 11:27:04 +0000 (09:27 -0200)
helloc.c

index 0db07e3..8103f60 100644 (file)
--- a/helloc.c
+++ b/helloc.c
@@ -5,6 +5,8 @@
 #include <linux/cdev.h>
 /* Needed for copying to/from user space */
 #include <asm/uaccess.h>
+/* Needed for using misc device stuff */
+#include <linux/miscdevice.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
@@ -41,48 +43,22 @@ static struct file_operations helloc_fops = {
        .read = helloc_read,
 };
 
-/* the device number and the char device struct */
-static dev_t dev;
-static struct cdev *cdev;
+static struct miscdevice hello_misc = {
+       .minor = MISC_DYNAMIC_MINOR,
+       .name = "helloc",
+       .fops = &helloc_fops,
+};
 
 static int __init helloc_init(void)
 {
        int r;
-       /* allocate any major number with only one minor */
-       r = alloc_chrdev_region(&dev, 0, 1, "helloc");
-       if (r)
-               goto out_region;
-       r = -ENOMEM;
-       /* print the major number allocated so we can create our device node */
-       printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev));
-       /* allocate the character device struct */
-       cdev = cdev_alloc();
-       if (!cdev)
-               goto out_alloc;
-       /* set the module owner and the file operations of our chardev */
-       cdev->owner = THIS_MODULE;
-       cdev->ops = &helloc_fops;
-       /* register the chardev to the system */
-       r = cdev_add(cdev, dev, 1);
-       if (r)
-               goto out_add;
-       return 0;
-out_add:
-       /* release memory allocated to the cdev device */
-       kfree(cdev);
-out_alloc:
-       /* release the device number allocated */
-       unregister_chrdev_region(dev, 1);
-out_region:
+       r = misc_register(&hello_misc);
        return r;
 }
 
 static void __exit helloc_exit(void)
 {
-       /* remove the chardev from the system */
-       cdev_del(cdev);
-       /* release the device number allocated */
-       unregister_chrdev_region(dev, 1);
+       misc_deregister(&hello_misc);
 }
 
 module_init(helloc_init);