2012-09-14 57 views
2

Chardevice主號碼啞chardevice LKM:這裏釋放

#include <linux/module.h> 
#include <linux/init.h> 
#include <linux/cdev.h> 
#include <linux/fs.h> 

MODULE_AUTHOR ("milo"); 
MODULE_DESCRIPTION ("alloc_chrdev_troubleshooting"); 
MODULE_LICENSE("GPL"); 
static int devmajor = 0; 
static char * devname = "chardev_alloc"; 

static int setup_chardevice(void) { 
    int result; 
    dev_t dev; 
    result = alloc_chrdev_region(&dev, 0, 1, devname); 
    if (result < 0) { 
     return result; 
    } 
    printk(KERN_INFO "major is %d", MAJOR(dev)); 
    devmajor = MAJOR(dev); 
    devmajor = result; 
    return 0; 
}; 

void delete_chardevice(void) { 
    unregister_chrdev_region(MKDEV(devmajor, 0), 1); 
}; 

static int __init test_init(void) { 
    int res; 
    res = setup_chardevice(); 
    if (res) { 
     printk(KERN_ERR "couldn't setup chardevice, goodbye."); 
     return res; 
    } 
    printk(KERN_WARNING "chardevice test driver is loaded"); 
    return 0; 
} 
static void __exit test_cleanup(void) { 
    delete_chardevice(); 
    printk(KERN_WARNING "chardevice test driver is unloading"); 
    return; 
} 

module_init(test_init); 
module_exit(test_cleanup); 

rmmod主設備號後不釋放,我可以看到它在/proc/devices。這實在是煩人... 這裏是Makefile快速測試:

KDIR ?= /lib/modules/`uname -r`/build 
obj-m += chartest.o 
chartest-objs := main.o 
default: all 
modules: 
    $(MAKE) -C $(KDIR) M=$(PWD) modules 
modules_clean: 
    $(MAKE) -C $(KDIR) M=$(PWD) clean 
all: modules 
clean: modules_clean 

我做了什麼錯?請提出建議。

回答

3
devmajor = MAJOR(dev); 
devmajor = result; 

您與alloc_chrdev_region的() alloc_chrdev_region()的返回值覆蓋devmajor變量成功返回0,而不是主號碼。

然後,您試圖釋放設備MKDEV(devmajor,0),如果alloc_chrdev_region成功,則可能是MKDEV(0,0),而不是您的設備。

+0

恥辱我!那真是個愚蠢的錯誤!非常感謝你的幫助! – milo