jordy90
jordy90

Reputation: 61

removing files created by device_create(), class_create() and alloc_chrdev_region()

After creating a character device driver using alloc_chrdev_region(), cdev_init(), cdev_add(), class_create() and device_create() I am unable to successfully clean all the entries made by the functions above in the module_exit(). When I use rmmod it just says "Killed". When I check the /proc/devices/, /sys/class/ and /dev/ directories my created files are still present. How can I make a clean exit of my module? The code I am using for exit:

static void char_exit(void)
{
    printk(KERN_ALERT "leaving the kernel.");
    cdev_del(my_chardev);

    device_destroy(myclass,first);

    class_destroy(myclass);

    unregister_chrdev_region(first,1);
}

For some reason it's not working.

Upvotes: 3

Views: 2678

Answers (1)

Himanshu Gupta
Himanshu Gupta

Reputation: 11

I can see there is an issue in order in which you are trying to destroy/del the class, device and cdev.

I tried on my system with sample code. Snapshot attached below:

[root@dhcp-10-123-181-110 own_char]# find / -name sample_cdev0

/dev/sample_cdev0

/sys/devices/virtual/sample/sample_cdev0

/sys/class/sample/sample_cdev0

[root@dhcp-10-123-181-110 own_char]#

[root@dhcp-10-123-181-110 own_char]#

[root@dhcp-10-123-181-110 own_char]# rmmod himschar

[root@dhcp-10-123-181-110 own_char]# find / -name sample_cdev0

[root@dhcp-10-123-181-110 own_char]# cat /proc/devices | grep -i sample

[root@dhcp-10-123-181-110 own_char]#

========================================================================

The order you need to follow is:

    device_destroy(sample_class, sample_dev_t);
    class_destroy(sample_class);
    cdev_del(sample_cdev);
    unregister_chrdev_region(sample_dev_t, 1);

Upvotes: 1

Related Questions