summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-12-23 01:41:33 +0000
committerKyle Kaminski <kyle@kkaminsk.com>2012-12-23 01:41:33 +0000
commitf0531d88e958b5fa77f8dc9bb34e697e872aeba9 (patch)
tree873915769ff30f32953d58519131b3956190a584
parentadbfb7c71fe1e958a2a2397c7b88c3061043c54e (diff)
downloadkernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.tar.gz
kernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.tar.bz2
kernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.zip
register char device and prepare mknod script
-rwxr-xr-xload.sh24
-rw-r--r--main.c8
2 files changed, 32 insertions, 0 deletions
diff --git a/load.sh b/load.sh
new file mode 100755
index 0000000..41266bc
--- /dev/null
+++ b/load.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+MODULE=hello
+DEVICE=skull
+MODE=664
+
+# invoke insmod with all arguments and use a pathname
+# as newer modutils don't look into . by default
+/usr/bin/insmod ./${MODULE}.ko $* || exit 1
+
+# remove stale nodes
+rm -f /dev/${DEVICE}[0]
+
+MAJOR=$(awk "\$2 == \"$DEVICE\" {print \$1}" /proc/devices)
+
+mknod /dev/${DEVICE}0 c $MAJOR 0
+
+# give appriopriate permissions
+GROUP=staff
+grep -q '^staff' /etc/group || GROUP=wheel
+
+chgrp $GROUP /dev/${DEVICE}0
+chmod $MODE /dev/${DEVICE}0
+
diff --git a/main.c b/main.c
index 6a65f3c..b6975a4 100644
--- a/main.c
+++ b/main.c
@@ -5,6 +5,7 @@
#include <linux/string.h>
/* used for kmalloc */
#include <linux/slab.h>
+#include <linux/fs.h>
#include <debug.h>
@@ -12,6 +13,8 @@ char *debugmsg = "Tesla coil is a hi freq transformer";
struct dentry *debug_dir = NULL;
struct dentry *debug_file = NULL;
+dev_t devnum;
+
/* __init is a hint that the func is only used at initialization time, then module
* loader drops the function after module is loaded making its memory available for other uses
*/
@@ -25,6 +28,10 @@ static int __init hello_init(void)
printk("%s\n", msg);
kfree(msg);
+ /* device numbers, major will be picked for us */
+ err = alloc_chrdev_region(&devnum, 0 /* first minor */, 1, "skull");
+ printk("major: %d, minor: %d\n", MAJOR(devnum), MINOR(devnum));
+
/* modifies passed in ptrs */
err = debug_init(&debug_dir, &debug_file);
if (err)
@@ -35,6 +42,7 @@ static int __init hello_init(void)
static void __exit hello_exit(void)
{
+ unregister_chrdev_region(devnum, 1);
debug_destroy(debug_dir, debug_file);
printk("module named hello removed\n");
}