#include <linux/module.h>
#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <linux/slab.h>
MODULE_LICENSE("GPL");
+
static dev_t devnum;
+static struct cdev *dev;
+
+static int hello_open(struct inode *ino, struct file *fp)
+{
+ printk(KERN_DEBUG "Hello, World!\n");
+ return 0;
+}
+
+static const struct file_operations hello_fops = {
+ .owner = THIS_MODULE,
+ .open = hello_open,
+};
static int __init ch_init(void)
{
- int r;
+ int r = 0;
r = alloc_chrdev_region(&devnum, 0, 256, "hello");
if (r)
- return r;
+ goto out;
+ dev = cdev_alloc();
+ if (!dev) {
+ r = -ENOMEM;
+ goto cdev_out;
+ }
+ dev->ops = &hello_fops;
+ r = cdev_add(dev, devnum, 256);
+ if (r)
+ goto add_out;
printk(KERN_DEBUG "Allocate major %d\n", MAJOR(devnum));
return 0;
+add_out:
+ kfree(dev);
+cdev_out:
+ unregister_chrdev_region(devnum, 256);
+out:
+ return r;
}
static void __exit ch_exit(void)
{
+ cdev_del(dev);
+ kfree(dev);
unregister_chrdev_region(devnum, 256);
}