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
我做了什麼錯?請提出建議。
恥辱我!那真是個愚蠢的錯誤!非常感謝你的幫助! – milo