继续上面的例子,在这里为在/sys/dev/char/下生成1:8/文件夹链接到/sys/devices/virtual/mem/random目录:

if (error)
goto devtattrError;
devtmpfs_create_node(dev);
}
/*添加链接文件,dev的class存在时有效,在后面详细讲*/
error = device_add_class_symlinks(dev);
if (error)
goto SymlinkError;
/*实际负责device中的属性添加。也是几个部分的集合,
包括class中的dev_attrs,device_type中的groups,还有device本
身的groups。*/
error = device_add_attrs(dev);
if (error)
goto AttrsError;
/*add device to bus*/
/*添加device到bus,建立bus和device之间的联系,后面详细将*/
error = bus_add_device(dev);
if (error)
goto BusError;
/*在dev->kobj目录下生成power目录,用于电源管理*/
error = dpm_sysfs_add(dev);
在dev->kobj下会生成一个power文件夹用于电源的管理,例如我们这里会在platform下

if (error)
goto DPMError;
/*添加设备到PM链表*/
device_pm_add(dev);
/* Notify clients of device addition. This call must come
* after dpm_sysf_add() and before kobject_uevent().
*/
if (dev->bus)
blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
BUS_NOTIFY_ADD_DEVICE, dev);
kobject_uevent(&dev->kobj, KOBJ_ADD);
/*将驱动与设备关联,在后面详细讲解*/
bus_probe_device(dev);
/*下面为添加到系统中的各种链表中*/
if (parent)
klist_add_tail(&dev->p->knode_parent,
&parent->p->klist_children);
if (dev->class) {
mutex_lock(&dev->class->p->class_mutex);
/* tie the class to the device */
klist_add_tail(&dev->knode_class,
&dev->class->p->class_devices);
/* notify any interfaces that the device is here */
list_for_each_entry(class_intf,
&dev->class->p->class_interfaces, node)
if (class_intf->add_dev)
class_intf->add_dev(dev, class_intf);
mutex_unlock(&dev->class->p->class_mutex);
}
……
}
