Hypervisor初始化,该功能需要内核编译选项支持,该函数需要定义CONFIG_SYS_HYPERVISOR宏,在我机器上没有编译此功能。
- int __init hypervisor_init(void)
- {
- /*在sys文件夹下创建hypervisor文件夹*/
- hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);
- if (!hypervisor_kobj)
- return -ENOMEM;
- return 0;
- }
Platform_bus初始化,下面会详细讲解
- int __init platform_bus_init(void)
- {
- int error;
- /*清空early_platform_device_list链表*/
- early_platform_cleanup();
- /*注册platform_bus*/
- error = device_register(&platform_bus);
- if (error)
- return error;
- /*bus注册*/
- error = bus_register(&platform_bus_type);
- if (error)
- device_unregister(&platform_bus);
- return error;
- }
- struct device platform_bus = {
- /*这个名称将变成"/sys/devices/platform"*/
- .init_name = "platform",
- };
注册platform_bus设备
platform_bus_init()->device_register()
- int device_register(struct device *dev)
- {
- device_initialize(dev);
- return device_add(dev);
- }
设备初始化
- /**
- * device_initialize - init device structure.
- * @dev: device.
- *
- * This prepares the device for use by other layers by initializing
- * its fields.
- * It is the first half of device_register(), if called by
- * that function, though it can also be called separately, so one
- * may use @dev's fields. In particular, get_device()/put_device()
- * may be used for reference counting of @dev after calling this
- * function.
- *
- * NOTE: Use put_device() to give up your reference instead of freeing
- * @dev directly once you have called this function.
- */
- /*初始化device结构*/
- void device_initialize(struct device *dev)
- {
- /*所属为devices_kset,所以他的父kobj为/sys/devices/*/
- dev->kobj.kset = devices_kset;
- kobject_init(&dev->kobj, &device_ktype);
- INIT_LIST_HEAD(&dev->dma_pools);
- init_MUTEX(&dev->sem);
- spin_lock_init(&dev->devres_lock);
- INIT_LIST_HEAD(&dev->devres_head);
- device_init_wakeup(dev, 0);/*设置power结构的wake相关两个属性为假*/
- device_pm_init(dev);/*初始化dev的power属性*/
- set_dev_node(dev, -1);/*CONFIG_NUMA结构下才有意义*/
- }
添加设备到设备树中,我们结合实际的实现结果分析
int device_add(struct device *dev)
{
struct device *parent = NULL;
struct class_interface *class_intf;/*class接口*/
int error = -EINVAL;
dev = get_device(dev);/*增加dev中kobject的引用计数*/
if (!dev)
goto done;
if (!dev->p) {/*如果设备对象的私有对象不存在,分配并初始化*/
error = device_private_init(dev);
if (error)
goto done;
}
/*
* for statically allocated devices, which should all be converted
* some day, we need to initialize the name. We prevent reading back
* the name, and force the use of dev_name()
*/
if (dev->init_name) {/*将名称保存到kobj中*/
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}
if (!dev_name(dev))/*从kobj中获取上面设置的名称*/
goto name_error;
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
/*得到设备的父设备,并增加其引用计数*/
parent = get_device(dev->parent);
/*设置设备的obj对象树,下面详细讲*/
setup_parent(dev, parent);
/* use parent numa_node */
if (parent)
set_dev_node(dev, dev_to_node(parent));
/* first, register with generic layer. */
/* we require the name to be set before, and pass NULL */
/*将obj对象添加到对象树中,对于这里的意思即platform添加到sys目录下*/
error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
实现效果如下图中的/sys/devices/目录下生成platform文件夹:

if (error)
goto Error;
/* notify platform of device entry */
if (platform_notify)
platform_notify(dev);
/*为文件创建dient对象(其实就是属性),并加入到dient对象树中*/
/*在对应设备下面创建uevent文件*/
error = device_create_file(dev, &uevent_attr);
如下图,在/sys/devices/platform下生成uevent文件

if (error)
goto attrError;
/*如果存在主设备号,在我们的情形中没有主设备号*/
if (MAJOR(dev->devt)) {
/*创建dient对象,用指定的属性,即生成一个dev文件*/
error = device_create_file(dev, &devt_attr);
如果有主设备号,即在dev->kobj下生成dev文件,例如,/sys/devices/virtual/mem/random具有的主设备号为1,次设备号为8,在/sys/devices/virtual/mem/random目录下生成dev文件:

其中dev文件为文本文件,保存内容为major:minor,例子中为1:8:

if (error)
goto ueventattrError;
/*创建/dev符号链接(就是在dient树种创建符号链接树)*/
/*根据设备类型,在sys/dev/char或block或其属性指定的dev_obj目录下生成链接文件,其名字的样式为major:minor,如1:8,指向dev主目录*/
error = device_create_sys_dev_entry(dev);
