linux裝置:初始化
阿新 • • 發佈:2019-02-09
本文轉載自 http://blog.china unix.net/uid-24631445-id-3419097.html
不過本人在此將原始碼修改為3.14.3的原始碼了
當按下開機鍵後,電腦經過自檢,執行載入程式,核心初始化,然後建立了核心執行緒init執行緒,init執行緒呼叫do_basic_setup()來初始化外部裝置,載入驅動程式.... (這一段實在其他地方看來的,並不表示我看過核心初始化的程式碼)
檔案/init/main.c
static void __init do_basic_setup(void)
{
cpuset_init_smp();
usermodehelper_init();
shmem_init();
driver_init();
init_irq_proc();
do_ctors();
usermodehelper_enable();
do_initcalls();
random_int_secret_init();
}
下面是driver_init()函式
檔案/drivers/base/init.c
void __init driver_init(void)
{
devtmpfs_init();
devices_init();
buses_init();
classes_init();
firmware_init();
hypervisor_init();
platform_bus_init();
cpu_dev_init();
memory_dev_init();
container_dev_init();
}
它又呼叫了devices_init()、buses_init()、chasses_init();
下面是devices_init()
檔案/drivers/base/core.c
int __init devices_init(void)
{
devices_kset
= kset_create_and_add("devices", &device_uevent_ops, NULL);
if
(!devices_kset)
return
-ENOMEM;
dev_kobj
= kobject_create_and_add("dev", NULL);
if
(!dev_kobj)
goto
dev_kobj_err;
sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj);
if
(!sysfs_dev_block_kobj)
goto
block_kobj_err;
sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj);
if
(!sysfs_dev_char_kobj)
goto
char_kobj_err;
return
0;
char_kobj_err:
kobject_put(sysfs_dev_block_kobj);
block_kobj_err:
kobject_put(dev_kobj);
dev_kobj_err:
kset_unregister(devices_kset);
return
-ENOMEM;
}
因此devices_init()就是呼叫kset_create_and_add和kobject_create_and_add函式來建立kset和kobject物件。
devices_kset = kset_create_and_add("devices",&device_uevent_ops,NULL);
dev_kobj = kobject_create_and_add("dev",NULL);
sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
下面的buses_init,classes_init,firmware_init,hypervisor_init與此類似,下面列出這些函式建立的kset和kobject物件。在建立這些物件的時候也會在/sys目錄下建立相應的目錄。
接著是函式:platform_bus_init(), 檔案/drivers/base/platform.c
-
buses_init:
-
bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
- system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
-
classes_init:
-
class_kset = kset_create_and_add("class", NULL, NULL);
-
firmware_init:
-
firmware_kobj = kobject_create_and_add("firmware", NULL);
-
hypervisor_init:
- hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);
接著是函式:platform_bus_init(), 檔案/drivers/base/platform.c
-
int __init platform_bus_init(void)
-
{
-
int error;
-
early_platform_cleanup();
-
error = device_register(&platform_bus);
-
if(error) return error;
-
error = bus_register(&platform_bus_type);
- if(error)
-
device_unregister(&platform_bus);
-
return error;
- }
- struct device platform_bus = {
- .init_name = "platform",
- };
- struct bus_type platform_bus_type = {
- .name = "platform",
- .dev_groups = platform_dev_groups,
- .match = platform_match,
- .uevent = platform_uevent,
- .pm = &platform_dev_pm_ops
- };