#include <linux/module.h>
#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/interrupt.h>
MODULE_LICENSE("GPL");
-static int major = 10;
-module_param_named(major, major, int, S_IRUGO | S_IWUSR);
+
+static dev_t devnum;
+static struct cdev *dev;
+static const char def_greeting[] = "Hello, World!\n";
+
+static DEFINE_SPINLOCK(hello_lock);
+
+static int hello_open(struct inode *ino, struct file *fp)
+{
+ return 0;
+}
+
+static void hello_world(unsigned long greeting)
+{
+ printk(KERN_INFO "%s", (char *) greeting);
+}
+
+static DECLARE_TASKLET(hello_tasklet, hello_world, def_greeting);
+
+static ssize_t hello_read(struct file *fp, char __user *buf, size_t sz,
+ loff_t *pos)
+{
+ printk(KERN_INFO "Scheduling tasklet...\n");
+ tasklet_schedule(&hello_tasklet);
+ return 0;
+}
+
+static ssize_t hello_write(struct file *fp, const char __user *buf, size_t sz,
+ loff_t *pos)
+{
+ return 0;
+}
+
+static int hello_release(struct inode *ino, struct file *fp)
+{
+ return 0;
+}
+
+static const struct file_operations hello_fops = {
+ .owner = THIS_MODULE,
+ .open = hello_open,
+ .release = hello_release,
+ .read = hello_read,
+ .write = hello_write,
+};
static int __init ch_init(void)
{
- int r;
- r = register_chrdev_region(MKDEV(major, 0), 256, "hello");
+ int r = 0;
+ r = alloc_chrdev_region(&devnum, 0, 256, "hello");
+ if (r)
+ goto reg_out;
+ dev = cdev_alloc();
+ if (!dev) {
+ r = -ENOMEM;
+ goto cdev_out;
+ }
+ dev->ops = &hello_fops;
+ r = cdev_add(dev, devnum, 256);
if (r)
- return 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);
+reg_out:
+ return r;
}
static void __exit ch_exit(void)
{
- unregister_chrdev_region(MKDEV(major, 0), 256);
+ tasklet_kill(&hello_tasklet);
+ cdev_del(dev);
+ unregister_chrdev_region(devnum, 256);
}
module_init(ch_init);