1.函数存在的路径:kernel/drivers/base/core.c
2.函数原型:
struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)
{
va_list vargs;
struct device *dev;
va_start(vargs, fmt);
dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
va_end(vargs);
return dev;
}
3.先看参数
class: pointer to the struct class that this device should be registered to
the struct class 指针,必须在本函数调用之前先被创建
parent: pointer to the parent struct device of this new device
该设备的parent指针。
devt: the dev_t for the char device to be added
字符设备的设备号,如果dev_t不是0,0的话,1个”dev”文件将被创建。
drvdata:
被添加到该设备回调的数据。
fmt: string for the device's name
设备名字。
4.可以看下面的函数调用例子
例子1
device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );
如果成功,它将会在/dev目录下产生/dev/hello0设备。
例子2
led_cdev->dev = device_create_drvdata(leds_class, parent, 0, led_cdev,
"%s", led_cdev->name);
由于dev_t是0,所以它不会在/dev下产生设备文件。
led_cdev为传递给class的私有数据。
会把第6个参数的内容复制到第5个参数 “%s”,就像printf一样。
5.对于没有指定dev->parent的dev都将被添加到/sys/devices/virtual/tty/ 目录下,如果指定了dev->parent,那么同时该dev->class存在,同时parent->class存在,那么该dev->name目录将被添加到parent->class所在目录下
来看看linux2.6.25内核源码,是怎么做得.
device_create(tty_class,NULL, MKDEV(TTYAUX_MAJOR, 1),"console");
==>device_register
==>device_add
==>setup_parent
==>get_device_parent
==>dev->kobj.parent= kobj("/sys/devices/virtual/tty");//所以所有的文件添加都将指向该目录
6.
device_create ==>device_create_vargs ==>device_register ==>device_add==> kobj = get_device_parent(dev, parent)==>
if (parent == NULL)
parent_kobj = virtual_device_parent(dev); ==>
virtual_dir = kobject_create_and_add("virtual",
&devices_kset->kobj); ==>
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
==>由此得出的路径是/sys/devices/virtual