Linux设备子系统初始化
本文介绍的内容是基于Linux3.1源码,并参考了很多网上找来的资料
Linux内核的启动的流程如下:
start_kernel->rest_init->kernel_init->do_basic_setup->driver_init
由driver_init函数完成设备驱动子系统的初始化,这里重点分析driver_init函数,该函数通过调用一系列的初始化函数主要完成了设备驱动子系统的一个整体框架;
1 /** 2 * driver_init - initialize driver model. 3 * 4 * Call the driver model init functions to initialize their 5 * subsystems. Called early from init/main.c. 6 */ 7 void __init driver_init(void) 8 { 9 /* These are the core pieces */ 10 devtmpfs_init(); 11 devices_init(); 12 buses_init(); 13 classes_init(); 14 firmware_init(); 15 hypervisor_init(); 16 17 /* These are also core pieces, but must come after the 18 * core core pieces. 19 */ 20 platform_bus_init(); 21 system_bus_init(); 22 cpu_dev_init(); 23 memory_dev_init(); 24 }
这个函数完成驱动子系统的构建,实现了Linux设备驱动的一个整体框架,接下来就可以真正的添加设备了,接下来逐个介绍这些函数的作用。
1、devtmpfs_int函数:
该函数注册一个名为devtmpfs的文件系统dev_fs_type,这部分和设备驱动子系统的建立关系并不大,这里不多做介绍
1 /* 2 * Create devtmpfs instance, driver-core devices will add their device 3 * nodes here. 4 */ 5 int __init devtmpfs_init(void) 6 { 7 int err = register_filesystem(&dev_fs_type); 8 if (err) { 9 printk(KERN_ERR "devtmpfs: unable to register devtmpfs " 10 "type %i\n", err); 11 return err; 12 } 13 14 thread = kthread_run(devtmpfsd, &err, "kdevtmpfs"); 15 if (!IS_ERR(thread)) { 16 wait_for_completion(&setup_done); 17 } else { 18 err = PTR_ERR(thread); 19 thread = NULL; 20 } 21 22 if (err) { 23 printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err); 24 unregister_filesystem(&dev_fs_type); 25 return err; 26 } 27 28 printk(KERN_INFO "devtmpfs: initialized\n"); 29 return 0; 30 }