Skip to content

Commit 06b7594

Browse files
authored
Fix incorrect major number registration in chardev (#77)
chardev2.c demonstrates the ioctl operation with static major number MAJOR_NUM, but there also exists "Major," the dynamic one, which results in registration and deregistration on different device. Once the module remove, it cannot insert again: $ sudo insmod chardev2.ko $ sudo rmmod chardev2 $ cat /proc/devices Character devices: ... 100 char_dev $ sudo insmod chardev2.ko insmod: ERROR: could not insert module chardev2.ko: Device or resource busy This patch removed the use of dynamic major number.
1 parent c97348d commit 06b7594

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

examples/chardev2.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ static char Message[BUF_LEN];
3030
*/
3131
static char *Message_Ptr;
3232

33-
/* Major number assigned to our device driver */
34-
static int Major;
3533
static struct class *cls;
3634

3735
/* This is called whenever a process attempts to open the device file */
@@ -201,10 +199,8 @@ static int __init chardev2_init(void)
201199
return ret_val;
202200
}
203201

204-
Major = ret_val;
205-
206202
cls = class_create(THIS_MODULE, DEVICE_FILE_NAME);
207-
device_create(cls, NULL, MKDEV(Major, MAJOR_NUM), NULL, DEVICE_FILE_NAME);
203+
device_create(cls, NULL, MKDEV(MAJOR_NUM, 0), NULL, DEVICE_FILE_NAME);
208204

209205
pr_info("Device created on /dev/%s\n", DEVICE_FILE_NAME);
210206

@@ -214,11 +210,11 @@ static int __init chardev2_init(void)
214210
/* Cleanup - unregister the appropriate file from /proc */
215211
static void __exit chardev2_exit(void)
216212
{
217-
device_destroy(cls, MKDEV(Major, 0));
213+
device_destroy(cls, MKDEV(MAJOR_NUM, 0));
218214
class_destroy(cls);
219215

220216
/* Unregister the device */
221-
unregister_chrdev(Major, DEVICE_NAME);
217+
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
222218
}
223219

224220
module_init(chardev2_init);

0 commit comments

Comments
 (0)