在网上找了些自动创建设备节点的办法,但由于内核接口的变化,已经无法使用了。下面这个程序是可以在2.6.32内核上使用的:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
static struct cdev test_cdev;
static dev_t test_devno;
static struct class *test_class;
struct device *test_device;
static const struct file_operations test_fops =
{
.owner = THIS_MODULE,
};
static int test_init(void)
{
int ret;
ret = alloc_chrdev_region(&test_devno, 0, 255, "test");
if (ret) {
printk(KERN_INFO "alloc_chrdev_region failed, ret=%d\n", ret);
return ret;
}
cdev_init(&test_cdev, &test_fops);
test_cdev.owner = THIS_MODULE;
ret = cdev_add(&test_cdev, test_devno, 1);
if (ret) {
printk(KERN_INFO "cdev_add failed, ret=%d\n", ret);
goto free_devno;
}
test_class = class_create(THIS_MODULE, "test_class");
if (IS_ERR(test_class)) {
ret = PTR_ERR(test_class);
printk(KERN_INFO "class_create failed, ret=%d\n", ret);
goto free_cdev;
}
test_device = device_create(test_class, NULL, test_devno, NULL, "test");
if (IS_ERR(test_device)) {
ret = PTR_ERR(test_device);
printk(KERN_INFO "device_create failed, ret=%d\n", ret);
goto free_class;
}
return 0;
free_class:
class_destroy(test_class);
free_cdev:
cdev_del(&test_cdev);
free_devno:
unregister_chrdev_region(test_devno, 255);
return ret;
}
void test_exit(void)
{
device_destroy(test_class, test_devno);
class_destroy(test_class);
cdev_del(&test_cdev);
unregister_chrdev_region(test_devno, 255);
}
MODULE_LICENSE("GPL");
module_init (test_init);
module_exit (test_exit);
没有评论:
发表评论