Create a class and a device.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 24 May 2010 13:07:49 +0000 (09:07 -0400)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Mon, 24 May 2010 13:07:49 +0000 (09:07 -0400)
hellochar.c

index a847392..e601e38 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/circ_buf.h>
 #include <linux/sched.h>
 #include <linux/completion.h>
+#include <linux/device.h>
 
 MODULE_LICENSE("GPL");
 
@@ -116,6 +117,9 @@ static const struct file_operations hello_fops = {
        .write = hello_write,
 };
 
+static struct class *hello_class;
+static struct device *hello_dev;
+
 static int __init ch_init(void)
 {
        int r = 0;
@@ -125,11 +129,21 @@ static int __init ch_init(void)
                r = -ENOMEM;
                goto out;
        }
+       hello_class = class_create(THIS_MODULE, "hello");
+       if (IS_ERR(hello_class)) {
+               r = PTR_ERR(hello_class);
+               goto class_out;
+       }
        memcpy(hello->buf, default_greeting, sizeof(default_greeting));
        hello->head = sizeof(default_greeting);
        r = alloc_chrdev_region(&devnum, 0, 256, "hello");
        if (r)
                goto reg_out;
+       hello_dev = device_create(hello_class, NULL, devnum, NULL, "hello");
+       if (!hello_dev) {
+               r = -ENODEV;
+               goto dev_out;
+       }
        dev = cdev_alloc();
        if (!dev) {
                r = -ENOMEM;
@@ -143,8 +157,12 @@ static int __init ch_init(void)
 add_out:
        kfree(dev);
 cdev_out:
+       device_destroy(hello_class, devnum);
+dev_out:
        unregister_chrdev_region(devnum, 256);
 reg_out:
+       class_destroy(hello_class);
+class_out:
        kfree(hello);
 out:
        return r;
@@ -153,8 +171,10 @@ out:
 static void __exit ch_exit(void)
 {
        cdev_del(dev);
+       device_destroy(hello_class, devnum);
        unregister_chrdev_region(devnum, 256);
        kfree(hello->buf);
+       class_destroy(hello_class);
 }
 
 module_init(ch_init);