教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 政务民生 >

linux设备模型深探(9)

来源:网络收集 时间:2026-08-05
导读: 即对应sysfs中的/sys/devices device_ktype 中对属性的读写操作同bus中的类似,被回溯到了struct device_attribute中的show 和store. 接着往下看device_add()的实现.这个函数比较长,分段分析如下: int device_add(st

即对应sysfs中的/sys/devices

device_ktype 中对属性的读写操作同bus中的类似,被回溯到了struct device_attribute中的show 和store.

接着往下看device_add()的实现.这个函数比较长,分段分析如下:

int device_add(struct device *dev) {

struct device *parent = NULL;

struct class_interface *class_intf;

int error;

dev = get_device(dev);

if (!dev || !strlen(dev->bus_id)) {

error = -EINVAL;

goto Done;

}

pr_debug(\

parent = get_device(dev->parent);

setup_parent(dev, parent);

/* first, register with generic layer. */

error = kobject_add(&dev->kobj, dev->kobj.parent, \

if (error)

goto Error;

如果注册device的时候,没有指定父结点,在kobject_add将会在/sys/device/下建立相同名称的目录

/* notify platform of device entry */

if (platform_notify)

platform_notify(dev);

/* notify clients of device entry (new way) */

if (dev->bus)

blocking_notifier_call_chain(&dev->bus->p->bus_notifier,

BUS_NOTIFY_ADD_DEVICE, dev);

忽略notify部份,这部份不会影响本函数的流程

error = device_create_file(dev, &uevent_attr);

if (error)

goto attrError;

if (MAJOR(dev->devt)) {

error = device_create_file(dev, &devt_attr);

if (error)

goto ueventattrError;

}

建立属性为uevent_attr的属性文件,如果device中指定了设备号,则建立属性为devt_attr的属性文件

error = device_add_class_symlinks(dev);

if (error)

goto SymlinkError;

error = device_add_attrs(dev);

if (error)

goto AttrsError;

error = dpm_sysfs_add(dev);

if (error)

goto PMError;

device_pm_add(dev);

在这里,不打算讨论class的部份,dpm pm是选择编译部份,不讨论. device_add_attrs中涉及到了group的部分,暂不讨论

error = bus_add_device(dev);

if (error)

goto BusError;

kobject_uevent(&dev->kobj, KOBJ_ADD);

bus_attach_device(dev);

if (parent)

klist_add_tail(&dev->knode_parent, &parent->klist_children);

if (dev->class) {

down(&dev->class->sem);

/* tie the class to the device */

list_add_tail(&dev->node, &dev->class->devices);

/* notify any interfaces that the device is here */

list_for_each_entry(class_intf, &dev->class->interfaces, node)

if (class_intf->add_dev)

class_intf->add_dev(dev, class_intf);

up(&dev->class->sem);

}

bus_add_device()会在对应总线代表目录的device目录下创建几个到device的链接.然后产生一个add事件,再调用bus_attach_device()去匹配已经注册到总线的驱动程序.全部做完之后,将设备挂到父结点的子链表.

Done:

put_device(dev);

return error;

BusError:

device_pm_remove(dev);

PMError:

if (dev->bus)

blocking_notifier_call_chain(&dev->bus->p->bus_notifier,

BUS_NOTIFY_DEL_DEVICE, dev);

device_remove_attrs(dev);

AttrsError:

device_remove_class_symlinks(dev);

SymlinkError:

if (MAJOR(dev->devt))

device_remove_file(dev, &devt_attr);

ueventattrError:

device_remove_file(dev, &uevent_attr);

attrError:

kobject_uevent(&dev->kobj, KOBJ_REMOVE);

kobject_del(&dev->kobj);

Error:

cleanup_device_parent(dev);

if (parent)

put_device(parent);

goto Done; }

出错处理部份.

bus_attach_device()是一个很重要的函数。它将设备自动与挂在总线上面的驱动进行匹配。

…… 此处隐藏:213字,全部文档内容请下载后查看。喜欢就下载吧 ……
linux设备模型深探(9).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/448127.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)