From: Thadeu Lima de Souza Cascardo Date: Tue, 8 Dec 2009 11:27:04 +0000 (-0200) Subject: Use misc device. X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fkernel%2Fsamples%2F02.char%2F.git;a=commitdiff_plain;h=25d2ba26a1d460609aeb150a8c56c446791486f6 Use misc device. --- diff --git a/helloc.c b/helloc.c index 0db07e3..8103f60 100644 --- a/helloc.c +++ b/helloc.c @@ -5,6 +5,8 @@ #include /* Needed for copying to/from user space */ #include +/* Needed for using misc device stuff */ +#include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Thadeu Lima de Souza Cascardo "); @@ -41,48 +43,22 @@ static struct file_operations helloc_fops = { .read = helloc_read, }; -/* the device number and the char device struct */ -static dev_t dev; -static struct cdev *cdev; +static struct miscdevice hello_misc = { + .minor = MISC_DYNAMIC_MINOR, + .name = "helloc", + .fops = &helloc_fops, +}; static int __init helloc_init(void) { int r; - /* allocate any major number with only one minor */ - r = alloc_chrdev_region(&dev, 0, 1, "helloc"); - if (r) - goto out_region; - r = -ENOMEM; - /* print the major number allocated so we can create our device node */ - printk(KERN_INFO "Allocated major number %d\n", MAJOR(dev)); - /* allocate the character device struct */ - cdev = cdev_alloc(); - if (!cdev) - goto out_alloc; - /* set the module owner and the file operations of our chardev */ - cdev->owner = THIS_MODULE; - cdev->ops = &helloc_fops; - /* register the chardev to the system */ - r = cdev_add(cdev, dev, 1); - if (r) - goto out_add; - return 0; -out_add: - /* release memory allocated to the cdev device */ - kfree(cdev); -out_alloc: - /* release the device number allocated */ - unregister_chrdev_region(dev, 1); -out_region: + r = misc_register(&hello_misc); return r; } static void __exit helloc_exit(void) { - /* remove the chardev from the system */ - cdev_del(cdev); - /* release the device number allocated */ - unregister_chrdev_region(dev, 1); + misc_deregister(&hello_misc); } module_init(helloc_init);