Skip to content

Commit b442fbc

Browse files
committed
Subject: automatic creation of device node
Description: - Device node '/dev/testCharDevice' will be automatically created on module loading - BUG: major number gets reduced by one each time the module is inserted
1 parent 63ae0a1 commit b442fbc

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

test_char_device/testCharDevice.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include<linux/semaphore.h> /* used acces to semaphore, process management
1717
syncronization behaviour */
1818
#include<asm/uaccess.h> /* copy_to_user;copy_from_user */
19+
#include<linux/device.h>
1920

2021
/* (1)create structure for fake char device */
2122
struct fake_device
@@ -39,7 +40,7 @@ int ret; /* will hold return value of the
3940
dev_t dev_num; /* will hold the device number that
4041
the kernel gives; name appears in
4142
/proc/devices */
42-
43+
static struct class *cl; // Global variable for the device class
4344
#define DEVICE_NAME "testCharDevice"
4445

4546
/* (7) called on device_open
@@ -126,13 +127,11 @@ static int driver_entry(void) {
126127
major_number = MAJOR(dev_num); /* extract major number from dev_num
127128
and store it in our variable */
128129
minor_number = MINOR(dev_num); /* minor number */
130+
printk(KERN_INFO "testCharDevice: module loaded, device is: /dev/%s",
131+
DEVICE_NAME);
129132
printk(KERN_INFO "testCharDevice: major number is: %d, minor number is: %d",
130133
major_number,
131134
minor_number);
132-
printk(KERN_INFO "testCharDevice: Please execute 'sudo mknod /dev/%s c %d %d'",
133-
DEVICE_NAME,
134-
major_number,
135-
minor_number); /* dmesg */
136135

137136
/* ----------(2)---------- */
138137
mcdev = cdev_alloc(); /* create our cdev structure;
@@ -141,6 +140,20 @@ static int driver_entry(void) {
141140
mcdev->ops = &fops; /* struct file operation */
142141
mcdev->owner = THIS_MODULE;
143142

143+
/* populate sysfs entries under /sys/class/chardev/ */
144+
if ((cl = class_create(THIS_MODULE, "chardev")) == NULL)
145+
{
146+
unregister_chrdev_region(dev_num, 1);
147+
return -1;
148+
}
149+
/* create /dev/testCharDevice node */
150+
if (device_create(cl, NULL, dev_num, NULL, DEVICE_NAME) == NULL)
151+
{
152+
class_destroy(cl);
153+
unregister_chrdev_region(dev_num, 1);
154+
return -1;
155+
}
156+
144157
/* now that we have created our cdev, we have to add it to kernel */
145158
/* int cdev_add(struct cdev*, dev, dev_t num, unsigned int count) */
146159
ret = cdev_add(mcdev, dev_num, 1);
@@ -152,18 +165,18 @@ static int driver_entry(void) {
152165

153166
/* (4) Initialize our semaphore */
154167
sema_init(&virtual_device.sem,1); /* initial value of one */
155-
156168
return 0;
157169
}
158170

159171
static void driver_exit(void) {
160172
/* (5) unregister everything in reverse order */
161173
// (a)
162174
cdev_del(mcdev);
163-
175+
device_destroy(cl, dev_num); /* delete node /dev/testCharDevice */
176+
class_destroy(cl); /* delete sysfs */
164177
// (b)
165178
unregister_chrdev_region(dev_num, 1);
166-
printk(KERN_INFO "testCharDevice: unloaded module");
179+
printk(KERN_INFO "testCharDevice: module unloaded");
167180

168181
}
169182

0 commit comments

Comments
 (0)