Skip to content

Commit

Permalink
miscdevice: Add helper macro for misc device boilerplate
Browse files Browse the repository at this point in the history
Many modules call misc_register and misc_deregister in its module init
and exit methods without any additional code. This ends up being
boilerplate. This patch adds helper macro module_misc_device(), that
replaces module_init()/ module_exit() with template functions.

This patch also converts drivers to use new macro.

Change since v1:
Add device.h include in miscdevice.h as module_driver macro was not
available from other include files in some architectures.

Signed-off-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
PrasannaKumar Muralidharan authored and Greg Kroah-Hartman committed Aug 31, 2016
1 parent 832c823 commit ca75d60
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 80 deletions.
14 changes: 1 addition & 13 deletions arch/arm/common/bL_switcher_dummy_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,4 @@ static struct miscdevice bL_switcher_device = {
"b.L_switcher",
&bL_switcher_fops
};

static int __init bL_switcher_dummy_if_init(void)
{
return misc_register(&bL_switcher_device);
}

static void __exit bL_switcher_dummy_if_exit(void)
{
misc_deregister(&bL_switcher_device);
}

module_init(bL_switcher_dummy_if_init);
module_exit(bL_switcher_dummy_if_exit);
module_misc_device(bL_switcher_device);
13 changes: 1 addition & 12 deletions arch/blackfin/mach-bf561/coreb.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,7 @@ static struct miscdevice coreb_dev = {
.name = "coreb",
.fops = &coreb_fops,
};

static int __init bf561_coreb_init(void)
{
return misc_register(&coreb_dev);
}
module_init(bf561_coreb_init);

static void __exit bf561_coreb_exit(void)
{
misc_deregister(&coreb_dev);
}
module_exit(bf561_coreb_exit);
module_misc_device(coreb_dev);

MODULE_AUTHOR("Bas Vermeulen <bvermeul@blackstar.xs4all.nl>");
MODULE_DESCRIPTION("BF561 Core B Support");
Expand Down
13 changes: 1 addition & 12 deletions drivers/hid/uhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,19 +779,8 @@ static struct miscdevice uhid_misc = {
.minor = UHID_MINOR,
.name = UHID_NAME,
};
module_misc_device(uhid_misc);

static int __init uhid_init(void)
{
return misc_register(&uhid_misc);
}

static void __exit uhid_exit(void)
{
misc_deregister(&uhid_misc);
}

module_init(uhid_init);
module_exit(uhid_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
Expand Down
15 changes: 2 additions & 13 deletions drivers/input/misc/uinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,23 +1013,12 @@ static struct miscdevice uinput_misc = {
.minor = UINPUT_MINOR,
.name = UINPUT_NAME,
};
module_misc_device(uinput_misc);

MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
MODULE_ALIAS("devname:" UINPUT_NAME);

static int __init uinput_init(void)
{
return misc_register(&uinput_misc);
}

static void __exit uinput_exit(void)
{
misc_deregister(&uinput_misc);
}

MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
MODULE_DESCRIPTION("User level driver support for input subsystem");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.3");

module_init(uinput_init);
module_exit(uinput_exit);
19 changes: 1 addition & 18 deletions drivers/s390/char/sclp_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,4 @@ static struct miscdevice sclp_ctl_device = {
.name = "sclp",
.fops = &sclp_ctl_fops,
};

/*
* Register sclp_ctl misc device
*/
static int __init sclp_ctl_init(void)
{
return misc_register(&sclp_ctl_device);
}
module_init(sclp_ctl_init);

/*
* Deregister sclp_ctl misc device
*/
static void __exit sclp_ctl_exit(void)
{
misc_deregister(&sclp_ctl_device);
}
module_exit(sclp_ctl_exit);
module_misc_device(sclp_ctl_device);
13 changes: 1 addition & 12 deletions drivers/vhost/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,7 @@ static struct miscdevice vhost_test_misc = {
"vhost-test",
&vhost_test_fops,
};

static int vhost_test_init(void)
{
return misc_register(&vhost_test_misc);
}
module_init(vhost_test_init);

static void vhost_test_exit(void)
{
misc_deregister(&vhost_test_misc);
}
module_exit(vhost_test_exit);
module_misc_device(vhost_test_misc);

MODULE_VERSION("0.0.1");
MODULE_LICENSE("GPL v2");
Expand Down
8 changes: 8 additions & 0 deletions include/linux/miscdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <linux/major.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/device.h>

/*
* These allocations are managed by device@lanana.org. If you use an
Expand Down Expand Up @@ -70,6 +71,13 @@ struct miscdevice {
extern int misc_register(struct miscdevice *misc);
extern void misc_deregister(struct miscdevice *misc);

/*
* Helper macro for drivers that don't do anything special in module init / exit
* call. This helps in eleminating of boilerplate code.
*/
#define module_misc_device(__misc_device) \
module_driver(__misc_device, misc_register, misc_deregister)

#define MODULE_ALIAS_MISCDEV(minor) \
MODULE_ALIAS("char-major-" __stringify(MISC_MAJOR) \
"-" __stringify(minor))
Expand Down

0 comments on commit ca75d60

Please sign in to comment.