diff options
author | Kyle K <kylek389@gmail.com> | 2012-12-23 01:41:33 +0000 |
---|---|---|
committer | Kyle Kaminski <kyle@kkaminsk.com> | 2012-12-23 01:41:33 +0000 |
commit | f0531d88e958b5fa77f8dc9bb34e697e872aeba9 (patch) | |
tree | 873915769ff30f32953d58519131b3956190a584 | |
parent | adbfb7c71fe1e958a2a2397c7b88c3061043c54e (diff) | |
download | kernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.tar.gz kernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.tar.bz2 kernelhello-f0531d88e958b5fa77f8dc9bb34e697e872aeba9.zip |
register char device and prepare mknod script
-rwxr-xr-x | load.sh | 24 | ||||
-rw-r--r-- | main.c | 8 |
2 files changed, 32 insertions, 0 deletions
@@ -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 + @@ -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"); } |