你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux设备驱动编程模型之设备篇

[日期:2012-02-26] 来源:Linux社区  作者:bullbat [字体:大 中 小]

Hypervisor初始化,该功能需要内核编译选项支持,该函数需要定义CONFIG_SYS_HYPERVISOR宏,在我机器上没有编译此功能。

[cpp]
  1. int __init hypervisor_init(void)  
  2. {  
  3.     /*在sys文件夹下创建hypervisor文件夹*/  
  4.     hypervisor_kobj = kobject_create_and_add("hypervisor", NULL);  
  5.     if (!hypervisor_kobj)  
  6.         return -ENOMEM;  
  7.     return 0;  
  8. }  

Platform_bus初始化,下面会详细讲解

[cpp]
  1. int __init platform_bus_init(void)  
  2. {  
  3.     int error;  
  4.     /*清空early_platform_device_list链表*/  
  5.     early_platform_cleanup();  
  6.     /*注册platform_bus*/  
  7.     error = device_register(&platform_bus);  
  8.     if (error)  
  9.         return error;  
  10.     /*bus注册*/  
  11.     error =  bus_register(&platform_bus_type);  
  12.     if (error)  
  13.         device_unregister(&platform_bus);  
  14.     return error;  
  15. }  
[cpp]
  1. struct device platform_bus = {  
  2.     /*这个名称将变成"/sys/devices/platform"*/  
  3.     .init_name  = "platform",  
  4. };  

注册platform_bus设备

platform_bus_init()->device_register()

[cpp]
  1. int device_register(struct device *dev)  
  2. {  
  3.     device_initialize(dev);  
  4.     return device_add(dev);  
  5. }  

设备初始化

[cpp]
  1. /** 
  2.  * device_initialize - init device structure. 
  3.  * @dev: device. 
  4.  * 
  5.  * This prepares the device for use by other layers by initializing 
  6.  * its fields. 
  7.  * It is the first half of device_register(), if called by 
  8.  * that function, though it can also be called separately, so one 
  9.  * may use @dev's fields. In particular, get_device()/put_device() 
  10.  * may be used for reference counting of @dev after calling this 
  11.  * function. 
  12.  * 
  13.  * NOTE: Use put_device() to give up your reference instead of freeing 
  14.  * @dev directly once you have called this function. 
  15.  */  
  16.  /*初始化device结构*/  
  17. void device_initialize(struct device *dev)  
  18. {  
  19.     /*所属为devices_kset,所以他的父kobj为/sys/devices/*/  
  20.     dev->kobj.kset = devices_kset;  
  21.     kobject_init(&dev->kobj, &device_ktype);  
  22.     INIT_LIST_HEAD(&dev->dma_pools);  
  23.     init_MUTEX(&dev->sem);  
  24.     spin_lock_init(&dev->devres_lock);  
  25.     INIT_LIST_HEAD(&dev->devres_head);  
  26.     device_init_wakeup(dev, 0);/*设置power结构的wake相关两个属性为假*/  
  27.     device_pm_init(dev);/*初始化dev的power属性*/  
  28.     set_dev_node(dev, -1);/*CONFIG_NUMA结构下才有意义*/  
  29. }  

添加设备到设备树中,我们结合实际的实现结果分析

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);

linux
相关资讯       Linux驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款