在Linux 2.6内核中,devfs被认为是过时的方法,并最终被抛弃,udev取代了它。Devfs的一个很重要的特点就是可以动态创建设备结点。那我们现在如何通过udev和sys文件系统动态创建设备结点呢?为什么udev要取代devfs? 这是生产关系适应生产力的需要,udev好,devfs坏,用好的不用坏的。
udev是硬件平台无关的,属于user space的进程,它脱离驱动层的关联而建立在操作系统之上,基于这种设计实现,我们可以随时修改及删除/dev下的设备文件名称和指向,随心所欲地按照我们的愿望安排和管理设备文件系统,而完成如此灵活的功能只需要简单地修改udev的配置文件即可,无需重新启动操作系统。udev 已经使得我们对设备的管理如探囊取物般轻松自如。
用udev在/dev/下动态生成设备文件,这样用户就不用手工调用mknod了。
利用的kernel API:
class_create : 创建class
class_destroy : 销毁class
class_device_create : 创建device
class_device_destroy : 销毁device
注意,这些API是2.6.13开始有的,在2.6.13之前,应当使用
class_simple_create
class_simple_destroy
class_simple_device_add
class_simple_device_remove
这一系列,也就是ldd3第14章描述的。 详见:
Output:
===========================================
[root@localhost dynamic_dev_node]# insmod ./dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: character special (250/0)
[root@localhost dynamic_dev_node]# rmmod dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: ERROR: cannot open `/dev/dummy_dev0' (No such file or directory)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define THIS_DESCRIPTION "\
This module is a dummy device driver, it register\n\
\t\ta char device, and utilize udev to create/destroy \n\
\t\tdevice node under /dev/ dynamicallly."
MODULE_LICENSE("GPL");
MODULE_AUTHOR("albcamus <>");
MODULE_DESCRIPTION(THIS_DESCRIPTION);
#define DUMMY_MAJOR 250
#define DUMMY_MINOR 0
#define DUMMY_NAME "dummy_dev"
/**
* the open routine of 'dummy_dev'
*/
static int dummy_open(struct inode *inode, struct file *file)
{
printk("Open OK\n");
return 0;
}
/**
* the write routine of 'dummy_dev'
*/
static ssize_t dummy_write(struct file *filp, const char *bp, size_t count, loff_t *ppos)
{
printk("Don't Write!\n");
return 0;
}
/**
* the read routine of 'dummy_dev'
*/
static ssize_t dummy_read(struct file *filp, char *bp, size_t count, loff_t *ppos)
{
return 0;
}
/**
* the ioctl routine of 'dummy_dev'
*/
static int dummy_ioctl(struct inode *inode, struct file *filep,
unsigned int cmd, unsigned long arg)
{
return 0;
}
/**
* file_operations of 'dummy_dev'
*/
static struct file_operations dummy_dev_ops = {
.owner = THIS_MODULE,
.open = dummy_open,
.read = dummy_read,
.write = dummy_write,
.ioctl = dummy_ioctl,
};
/**
* struct cdev of 'dummy_dev'
*/
struct cdev *my_cdev;
struct class *my_class;
static int __init my_init(void)
{
int err, devno = MKDEV(DUMMY_MAJOR, DUMMY_MINOR);
/* register the 'dummy_dev' char device */
my_cdev = cdev_alloc();
cdev_init(my_cdev, &dummy_dev_ops);
my_cdev->owner = THIS_MODULE;
err = cdev_add(my_cdev, devno, 1);
if (err != 0)
printk("dummy pci device register failed!\n");
/* creating your own class */
my_class = class_create(THIS_MODULE, "dummy_class");
if(IS_ERR(my_class)) {
printk("Err: failed in creating class.\n");
return -1;
}
/* register your own device in sysfs, and this will cause udevd to create corresponding device node */
class_device_create(my_class, NULL, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), NULL, DUMMY_NAME "%d", DUMMY_MINOR );
return 0;
}
static void __exit my_fini(void)
{
printk("bye\n");
cdev_del(my_cdev);
//kfree(my_cdev); no use. because that cdev_del() will call kfree if neccessary.
class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR));
class_destroy(my_class);
}
module_init(my_init);
module_exit(my_fini);
原来2.6.15中的函数:
class_device_create();
class_device_destroy();
在2.6.27中变为:
device_create()
device_destroy()
阅读(1214) | 评论(0) | 转发(1) |