Reputation: 151
I am writting a simple char driver. I create node in sysfs with device_create() and it is created properly. I get my node in /dev automatically as well. My problem is that class_distroy() and device_destroy() don't clean the /sys/devices/virtual/tdmcdev/tdm/ directory crated on init. My init and close code below
...
/* Node in the /dev/ */
tdm->dev_major = 0; //for dynamic major
tdm_dev = MKDEV(tdm->dev_major, 0);
tdm->dev_major = MAJOR(tdm_dev);
err = alloc_chrdev_region(&tdm_dev, 0, 1, "tdm"); //One node to read/write data frame
if (err) {
printk("can't alloc minor for /dev/tdm\n");
return -ENODEV;
}
cdev_init(&(tdm->cdev), &tdm_dev_fops);
tdm->cdev.owner = THIS_MODULE;
err = cdev_add(&(tdm->cdev), tdm_dev, 1);
if (err) {
printk("cdev_add() failed for /dev/tdm\n");
unregister_chrdev_region(tdm_dev, 1);
return -ENODEV;
}
/* Node /sys/devices/virtual/tdmcdev/tdm/ */
tdm->dev_class = class_create(THIS_MODULE, "tdmcdev");
if (IS_ERR(device_create(tdm->dev_class, NULL, tdm_dev, NULL, "tdm"))) {
printk("device_create() failed for the tdm device\n");
class_destroy(tdm->dev_class);
cdev_del(&(tdm->cdev));
unregister_chrdev_region(tdm_dev, 1);
return -ENOMEM;
}
...
my close code
dev_t tdm_dev = MKDEV(tdm->dev_major, 0);
device_destroy(tdm->dev_class, tdm_dev);
class_destroy(tdm->dev_class);
cdev_del(&(tdm->cdev));
unregister_chrdev_region(tdm_dev, 1);
...
It is on Linux OpenWrt 3.10.49 on MIPS CPU. Anyone seeing something not in order?
Thanks. Dimitar
Upvotes: 3
Views: 703
Reputation: 151
It appeared a stupid one. I am using dynamic major so start from value 0. I have to move tdm->dev_major = MAJOR(tdm_dev) after the alloc_chrdev_region() of course so get the proper major which I can clean afterwords
Thanks Dimitar
Upvotes: 2