Commits

Anonymous committed 50ea20a

allocate and unregister character device

Comments (0)

Files changed (3)

misc-modules/Makefile

 
 else
     # called from kernel build system: just declare what our modules are
-    obj-m := hello.o
+    obj-m := hello.o hello-dev.o
 endif

misc-modules/hello-dev.c

+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/version.h>
+
+int hello_major;
+int hello_minor;
+
+static int __init hello_init( void ) {
+    int result;
+    dev_t dev = 0;
+
+    result = alloc_chrdev_region( &dev, 0, 1, "hello-dev" );
+    hello_major = MAJOR( dev );
+    hello_minor = MINOR( dev );
+    if ( result < 0 ) {
+        printk( KERN_WARNING "hello-dev: can't get major\n" );
+        return result;
+    }
+
+    printk( KERN_INFO "Got major %i and minor %i\n", hello_major,
+            hello_minor );
+
+    return 0;
+}
+
+static void __exit hello_exit( void ) {
+    dev_t dev = MKDEV( hello_major, hello_minor );
+
+    unregister_chrdev_region( dev, 1 );
+
+    printk( KERN_INFO "Goodbye world!\n" );
+}
+
+module_init( hello_init );
+module_exit( hello_exit );
+
+MODULE_LICENSE( "GPL" );
+MODULE_AUTHOR( "Alexander Dahl <post@lespocky.de>" );
+MODULE_DESCRIPTION( "Hello Device!" );
+MODULE_VERSION( "1.0" );

misc-modules/hello.c

 MODULE_LICENSE( "GPL" );
 MODULE_AUTHOR( "Alexander Dahl <post@lespocky.de>" );
 MODULE_DESCRIPTION( "Hello World as described in book 'Linux Device Drivers'" );
+MODULE_VERSION( "1.0" );