Skip to content

Commit ca75d60

Browse files
prasannatsmgregkh
authored andcommitted
miscdevice: Add helper macro for misc device boilerplate
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>
1 parent 832c823 commit ca75d60

7 files changed

Lines changed: 15 additions & 80 deletions

File tree

‎arch/arm/common/bL_switcher_dummy_if.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,4 @@ static struct miscdevice bL_switcher_device = {
5656
"b.L_switcher",
5757
&bL_switcher_fops
5858
};
59-
60-
static int __init bL_switcher_dummy_if_init(void)
61-
{
62-
return misc_register(&bL_switcher_device);
63-
}
64-
65-
static void __exit bL_switcher_dummy_if_exit(void)
66-
{
67-
misc_deregister(&bL_switcher_device);
68-
}
69-
70-
module_init(bL_switcher_dummy_if_init);
71-
module_exit(bL_switcher_dummy_if_exit);
59+
module_misc_device(bL_switcher_device);

‎arch/blackfin/mach-bf561/coreb.c‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,7 @@ static struct miscdevice coreb_dev = {
5959
.name = "coreb",
6060
.fops = &coreb_fops,
6161
};
62-
63-
static int __init bf561_coreb_init(void)
64-
{
65-
return misc_register(&coreb_dev);
66-
}
67-
module_init(bf561_coreb_init);
68-
69-
static void __exit bf561_coreb_exit(void)
70-
{
71-
misc_deregister(&coreb_dev);
72-
}
73-
module_exit(bf561_coreb_exit);
62+
module_misc_device(coreb_dev);
7463

7564
MODULE_AUTHOR("Bas Vermeulen <bvermeul@blackstar.xs4all.nl>");
7665
MODULE_DESCRIPTION("BF561 Core B Support");

‎drivers/hid/uhid.c‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -779,19 +779,8 @@ static struct miscdevice uhid_misc = {
779779
.minor = UHID_MINOR,
780780
.name = UHID_NAME,
781781
};
782+
module_misc_device(uhid_misc);
782783

783-
static int __init uhid_init(void)
784-
{
785-
return misc_register(&uhid_misc);
786-
}
787-
788-
static void __exit uhid_exit(void)
789-
{
790-
misc_deregister(&uhid_misc);
791-
}
792-
793-
module_init(uhid_init);
794-
module_exit(uhid_exit);
795784
MODULE_LICENSE("GPL");
796785
MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
797786
MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");

‎drivers/input/misc/uinput.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,23 +1013,12 @@ static struct miscdevice uinput_misc = {
10131013
.minor = UINPUT_MINOR,
10141014
.name = UINPUT_NAME,
10151015
};
1016+
module_misc_device(uinput_misc);
1017+
10161018
MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
10171019
MODULE_ALIAS("devname:" UINPUT_NAME);
10181020

1019-
static int __init uinput_init(void)
1020-
{
1021-
return misc_register(&uinput_misc);
1022-
}
1023-
1024-
static void __exit uinput_exit(void)
1025-
{
1026-
misc_deregister(&uinput_misc);
1027-
}
1028-
10291021
MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
10301022
MODULE_DESCRIPTION("User level driver support for input subsystem");
10311023
MODULE_LICENSE("GPL");
10321024
MODULE_VERSION("0.3");
1033-
1034-
module_init(uinput_init);
1035-
module_exit(uinput_exit);

‎drivers/s390/char/sclp_ctl.c‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,4 @@ static struct miscdevice sclp_ctl_device = {
126126
.name = "sclp",
127127
.fops = &sclp_ctl_fops,
128128
};
129-
130-
/*
131-
* Register sclp_ctl misc device
132-
*/
133-
static int __init sclp_ctl_init(void)
134-
{
135-
return misc_register(&sclp_ctl_device);
136-
}
137-
module_init(sclp_ctl_init);
138-
139-
/*
140-
* Deregister sclp_ctl misc device
141-
*/
142-
static void __exit sclp_ctl_exit(void)
143-
{
144-
misc_deregister(&sclp_ctl_device);
145-
}
146-
module_exit(sclp_ctl_exit);
129+
module_misc_device(sclp_ctl_device);

‎drivers/vhost/test.c‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,7 @@ static struct miscdevice vhost_test_misc = {
322322
"vhost-test",
323323
&vhost_test_fops,
324324
};
325-
326-
static int vhost_test_init(void)
327-
{
328-
return misc_register(&vhost_test_misc);
329-
}
330-
module_init(vhost_test_init);
331-
332-
static void vhost_test_exit(void)
333-
{
334-
misc_deregister(&vhost_test_misc);
335-
}
336-
module_exit(vhost_test_exit);
325+
module_misc_device(vhost_test_misc);
337326

338327
MODULE_VERSION("0.0.1");
339328
MODULE_LICENSE("GPL v2");

‎include/linux/miscdevice.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/major.h>
44
#include <linux/list.h>
55
#include <linux/types.h>
6+
#include <linux/device.h>
67

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

74+
/*
75+
* Helper macro for drivers that don't do anything special in module init / exit
76+
* call. This helps in eleminating of boilerplate code.
77+
*/
78+
#define module_misc_device(__misc_device) \
79+
module_driver(__misc_device, misc_register, misc_deregister)
80+
7381
#define MODULE_ALIAS_MISCDEV(minor) \
7482
MODULE_ALIAS("char-major-" __stringify(MISC_MAJOR) \
7583
"-" __stringify(minor))

0 commit comments

Comments
 (0)