一、device定义
device结构体定义在include/linux/device.h文件中:
我们来看一下device结构体的几个重要成员变量。
parent,device的父设备,即device依附的设备,通常父设备是某种类型的bus或host controller。如果parent的值是NULL,那么device将是一个顶层设备,这通常不是我们想要的。
p,device的私有数据,它是struct device_private结构体变量,这个结构体定义在drivers/base/base.h文件中,其内容如下:
- 55
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 73struct device_private {
- 74 struct klist klist_children;
- 75 struct klist_node knode_parent;
- 76 struct klist_node knode_driver;
- 77 struct klist_node knode_bus;
- 78 struct list_head deferred_probe;
- 79 void *driver_data;
- 80 struct device *device;
- 81};
kobj,device对应的kobject。
init_name,device的名字。
type,device类型。
bus,device所属的bus。
driver,device所关联的device_driver。
platform_data,平台相关数据。
二、devics相关sysfs结构初始化
Linux设备模型中,device相关的sysfs结构的初始化是由devices_init 函数完成的,该函数定义在drivers/base/core.c文件中,其内容如下:
- 1395int __init devices_init(void)
- 1396{
- 1397 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
- 1398 if (!devices_kset)
- 1399 return -ENOMEM;
- 1400 dev_kobj = kobject_create_and_add("dev", NULL);
- 1401 if (!dev_kobj)
- 1402 goto dev_kobj_err;
- 1403 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
- 1404 if (!sysfs_dev_block_kobj)
- 1405 goto block_kobj_err;
- 1406 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
- 1407 if (!sysfs_dev_char_kobj)
- 1408 goto char_kobj_err;
- 1409
- 1410 return 0;
- 1411
- 1412 char_kobj_err:
- 1413 kobject_put(sysfs_dev_block_kobj);
- 1414 block_kobj_err:
- 1415 kobject_put(dev_kobj);
- 1416 dev_kobj_err:
- 1417 kset_unregister(devices_kset);
- 1418 return -ENOMEM;
- 1419}
1397行,创建devices_kset,它是所有device的集合,它的名字是“devices”,对应/sys/devices目录。
1400行,创建dev_kobj,它的名字是“dev”,对应/sys/dev目录。
1403行,创建sysfs_dev_block_kobj,它的名字是“block”,对应/sys/dev/block目录。
1406行,创建sysfs_dev_char_kobj,它的名字是“char”,对应/sys/dev/char目录。
三、device的注册
注册一个device是通过调用device_register函数完成的,该函数定义在drivers/base/core.c文件中,其内容如下:
- 1147
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1165int device_register(struct device *dev)
- 1166{
- 1167 device_initialize(dev);
- 1168 return device_add(dev);
- 1169}
1167行,调用device_initialize函数对device结构体进行初始化。该函数定义在drivers/base/core.c文件中,其内容如下:
- 675
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 695void device_initialize(struct device *dev)
- 696{
- 697 dev->kobj.kset = devices_kset;
- 698 kobject_init(&dev->kobj, &device_ktype);
- 699 INIT_LIST_HEAD(&dev->dma_pools);
- 700 mutex_init(&dev->mutex);
- 701 lockdep_set_novalidate_class(&dev->mutex);
- 702 spin_lock_init(&dev->devres_lock);
- 703 INIT_LIST_HEAD(&dev->devres_head);
- 704 device_pm_init(dev);
- 705 set_dev_node(dev, -1);
- 706}
该函数对device结构体的部分成员进行了初始化。
1168行,调用device_add函数,将device添加到系统中,该函数定义在drivers/base/core.c文件中,其内容如下:
- 980
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1002int device_add(struct device *dev)
- 1003{
- 1004 struct device *parent = NULL;
- 1005 struct kobject *kobj;
- 1006 struct class_interface *class_intf;
- 1007 int error = -EINVAL;
- 1008
- 1009 dev = get_device(dev);
- 1010 if (!dev)
- 1011 goto done;
- 1012
- 1013 if (!dev->p) {
- 1014 error = device_private_init(dev);
- 1015 if (error)
- 1016 goto done;
- 1017 }
- 1018
- 1019
-
-
-
-
- 1024 if (dev->init_name) {
- 1025 dev_set_name(dev, "%s", dev->init_name);
- 1026 dev->init_name = NULL;
- 1027 }
- 1028
- 1029
- 1030 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
- 1031 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
- 1032
- 1033 if (!dev_name(dev)) {
- 1034 error = -EINVAL;
- 1035 goto name_error;
- 1036 }
- 1037
- 1038 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
- 1039
- 1040 parent = get_device(dev->parent);
- 1041 kobj = get_device_parent(dev, parent);
- 1042 if (kobj)
- 1043 dev->kobj.parent = kobj;
- 1044
- 1045
- 1046 if (parent)
- 1047 set_dev_node(dev, dev_to_node(parent));
- 1048
- 1049
- 1050
- 1051 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
- 1052 if (error)
- 1053 goto Error;
- 1054
- 1055
- 1056 if (platform_notify)
- 1057 platform_notify(dev);
- 1058
- 1059 error = device_create_file(dev, &uevent_attr);
- 1060 if (error)
- 1061 goto attrError;
- 1062
- 1063 if (MAJOR(dev->devt)) {
- 1064 error = device_create_file(dev, &devt_attr);
- 1065 if (error)
- 1066 goto ueventattrError;
- 1067
- 1068 error = device_create_sys_dev_entry(dev);
- 1069 if (error)
- 1070 goto devtattrError;
- 1071
- 1072 devtmpfs_create_node(dev);
- 1073 }
- 1074
- 1075 error = device_add_class_symlinks(dev);
- 1076 if (error)
- 1077 goto SymlinkError;
- 1078 error = device_add_attrs(dev);
- 1079 if (error)
- 1080 goto AttrsError;
- 1081 error = bus_add_device(dev);
- 1082 if (error)
- 1083 goto BusError;
- 1084 error = dpm_sysfs_add(dev);
- 1085 if (error)
- 1086 goto DPMError;
- 1087 device_pm_add(dev);
- 1088
- 1089
-
-
- 1092 if (dev->bus)
- 1093 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
- 1094 BUS_NOTIFY_ADD_DEVICE, dev);
- 1095
- 1096 kobject_uevent(&dev->kobj, KOBJ_ADD);
- 1097 bus_probe_device(dev);
- 1098 if (parent)
- 1099 klist_add_tail(&dev->p->knode_parent,
- 1100 &parent->p->klist_children);
- 1101
- 1102 if (dev->class) {
- 1103 mutex_lock(&dev->class->p->mutex);
- 1104
- 1105 klist_add_tail(&dev->knode_class,
- 1106 &dev->class->p->klist_devices);
- 1107
- 1108
- 1109 list_for_each_entry(class_intf,
- 1110 &dev->class->p->interfaces, node)
- 1111 if (class_intf->add_dev)
- 1112 class_intf->add_dev(dev, class_intf);
- 1113 mutex_unlock(&dev->class->p->mutex);
- 1114 }
- 1115done:
- 1116 put_device(dev);
- 1117 return error;
- 1118 DPMError:
- 1119 bus_remove_device(dev);
- 1120 BusError:
- 1121 device_remove_attrs(dev);
- 1122 AttrsError:
- 1123 device_remove_class_symlinks(dev);
- 1124 SymlinkError:
- 1125 if (MAJOR(dev->devt))
- 1126 devtmpfs_delete_node(dev);
- 1127 if (MAJOR(dev->devt))
- 1128 device_remove_sys_dev_entry(dev);
- 1129 devtattrError:
- 1130 if (MAJOR(dev->devt))
- 1131 device_remove_file(dev, &devt_attr);
- 1132 ueventattrError:
- 1133 device_remove_file(dev, &uevent_attr);
- 1134 attrError:
- 1135 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
- 1136 kobject_del(&dev->kobj);
- 1137 Error:
- 1138 cleanup_device_parent(dev);
- 1139 if (parent)
- 1140 put_device(parent);
- 1141name_error:
- 1142 kfree(dev->p);
- 1143 dev->p = NULL;
- 1144 goto done;
- 1145}
1009行,调用get_device增加device(其实是device.kobj)的引用计数。
1013-1017行,如果dev->p是NULL,则调用device_private_init函数为dev->p分配内存空间并进行初始化,该函数定义在drivers/base/core.c文件中,其内容如下:
- 968int device_private_init(struct device *dev)
- 969{
- 970 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
- 971 if (!dev->p)
- 972 return -ENOMEM;
- 973 dev->p->device = dev;
- 974 klist_init(&dev->p->klist_children, klist_children_get,
- 975 klist_children_put);
- 976 INIT_LIST_HEAD(&dev->p->deferred_probe);
- 977 return 0;
- 978}
1024-1027行,如果dev->init_name不为空,则调用dev_set_name设置device的名字(其实是设置device.kobj的名字)。
1030-1031行,如果device的名字为空,并且dev->bus->dev_name不为空,则用dev->bus->dev_name作为device的名字。
1033-1036行,如果device的名字仍然为空,则出错退出。
1040行,取得dev->parent,保存在parent变量中。
1041行,调用get_device_parent函数,该函数的作用是取得dev->kobj.parent,即device对应的kobject的parent kobject。该函数定义在drivers/base/core.c文件中,其内容如下:
- 769static struct kobject *get_device_parent(struct device *dev,
- 770 struct device *parent)
- 771{
- 772 if (dev->class) {
- 773 static DEFINE_MUTEX(gdp_mutex);
- 774 struct kobject *kobj = NULL;
- 775 struct kobject *parent_kobj;
- 776 struct kobject *k;
- 777
- 778#ifdef CONFIG_BLOCK
- 779
- 780 if (sysfs_deprecated && dev->class == &block_class) {
- 781 if (parent && parent->class == &block_class)
- 782 return &parent->kobj;
- 783 return &block_class.p->subsys.kobj;
- 784 }
- 785#endif
- 786
- 787
-
-
-
-
- 792 if (parent == NULL)
- 793 parent_kobj = virtual_device_parent(dev);
- 794 else if (parent->class && !dev->class->ns_type)
- 795 return &parent->kobj;
- 796 else
- 797 parent_kobj = &parent->kobj;
- 798
- 799 mutex_lock(&gdp_mutex);
- 800
- 801
- 802 spin_lock(&dev->class->p->glue_dirs.list_lock);
- 803 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
- 804 if (k->parent == parent_kobj) {
- 805 kobj = kobject_get(k);
- 806 break;
- 807 }
- 808 spin_unlock(&dev->class->p->glue_dirs.list_lock);
- 809 if (kobj) {
- 810 mutex_unlock(&gdp_mutex);
- 811 return kobj;
- 812 }
- 813
- 814
- 815 k = class_dir_create_and_add(dev->class, parent_kobj);
- 816
- 817 mutex_unlock(&gdp_mutex);
- 818 return k;
- 819 }
- 820
- 821
- 822 if (!parent && dev->bus && dev->bus->dev_root)
- 823 return &dev->bus->dev_root->kobj;
- 824
- 825 if (parent)
- 826 return &parent->kobj;
- 827 return NULL;
- 828}
这个函数功能如下:
1、如果dev->class不为NULL,并且dev->parent为NULL
这种情况下系统将为dev->kobj.parent建立一个虚拟上层对象“virtual”,如此,将dev对象加入系统将会在/sys/devices/virtual中产生一个新的目录/sys/devices/virtual/dev->init_name。
2、如果dev->class不为NULL,并且dev->parent也不为NULL
这种情况下,要看dev->parent->class是否为空,如果不为空,则dev->kobj.parent为dev->parent->kobj,即dev父设备的内嵌kobject。
如果dev->parent->class为空,则内核需要在dev->class->p->class_dirs.list中查找是否有满足如下条件的kobject对象k:k->parent等于&parent->kobj。如果找到满足条件的k,那么dev->kobj.parent就是dev->parent->kobj,即dev父设备的内嵌kobject。如果没有满足条件的k,那么就需要调用class_dir_create_and_add函数,重新生成一个kobject对象作为dev->kobj的父kobject。
3、如果dev->class为NULL,并且dev->parent不为NULL
这种情况下,dev->kobj.parent为dev->parent->kobj,即dev父设备的内嵌kobject。也就是说dev对象对应的目录将建立在dev->parent->kobj对应的目录下。
4、如果dev->class为NULL,并且dev->parent也为NULL
这种情况下,如果dev->bus->dev_root不为NULL,则指定dev->kobj.parent为dev->bus->dev_root->kobj。
我们回到device_add函数:
1042-1043行,如果通过get_device_parent函数取得了父kobject,则用它来设置dev->kobj.parent。
1051行,调用kobject_add将dev->kobj注册到sysfs系统中。
1059行,调用device_create_file创建uevent属性文件。uevent_attr定义在drivers/base/core.c文件中,其内容如下:
- 403static struct device_attribute uevent_attr =
- 404 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
device_attribute结构体定义在include/linux/device.h文件中,其内容如下:
- 478
- 479struct device_attribute {
- 480 struct attribute attr;
- 481 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
- 482 char *buf);
- 483 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
- 484 const char *buf, size_t count);
- 485};
device_create_file定义在drivers/base/core.c文件中,其内容如下:
- 563
-
-
-
-
- 568int device_create_file(struct device *dev,
- 569 const struct device_attribute *attr)
- 570{
- 571 int error = 0;
- 572
- 573 if (dev) {
- 574 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
- 575 "Attribute %s: write permission without 'store'\n",
- 576 attr->attr.name);
- 577 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
- 578 "Attribute %s: read permission without 'show'\n",
- 579 attr->attr.name);
- 580 error = sysfs_create_file(&dev->kobj, &attr->attr);
- 581 }
- 582
- 583 return error;
- 584}
1063行,通过MAJOR(dev->devt)取得设备对应的主设备号。
1064行,如果设备的主设备号不为0,调用device_create_file创建dev属性文件。devt_attr定义在drivers/base/core.c文件中,其内容如下:
- 557static struct device_attribute devt_attr =
- 558 __ATTR(dev, S_IRUGO, show_dev, NULL);
1068行,调用device_create_sys_dev_entry(dev)函数,该函数定义在drivers/base/core.c文件中,其内容如下:
- 943static int device_create_sys_dev_entry(struct device *dev)
- 944{
- 945 struct kobject *kobj = device_to_dev_kobj(dev);
- 946 int error = 0;
- 947 char devt_str[15];
- 948
- 949 if (kobj) {
- 950 format_dev_t(devt_str, dev->devt);
- 951 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
- 952 }
- 953
- 954 return error;
- 955}
945行,调用device_to_dev_kobj(dev),该函数定义在drivers/base/core.c文件中,其内容如下:
- 920
-
-
-
-
-
-
-
-
-
-
- 931static struct kobject *device_to_dev_kobj(struct device *dev)
- 932{
- 933 struct kobject *kobj;
- 934
- 935 if (dev->class)
- 936 kobj = dev->class->dev_kobj;
- 937 else
- 938 kobj = sysfs_dev_char_kobj;
- 939
- 940 return kobj;
- 941}
从该函数的注释可以看出,该函数的作用是返回device对应的/sys/dev/下的一个目录。该函数的返回值是一个kobject,一个kobject在sysfs系统中对应一个目录。如果dev->class不为NULL,该函数返回dev->class->dev_kobj;如果dev->class为NULL,该函数返回sysfs_dev_char_kobj,它对应/sys/dev/char目录。
回到device_create_sys_dev_entry函数:
950行,调用format_dev_t宏,该宏定义在include/linux/kdev_t.h文件中,其内容如下:
- 16#define format_dev_t(buffer, dev) \
- 17 ({ \
- 18 sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev)); \
- 19 buffer; \
- 20 })
可以看到,该宏的作用是把“主设备号:次设备号”字符串写入buffer变量中。
951行,调用sysfs_create_link(kobj, &dev->kobj, devt_str)创建一个链接。该函数定义在fs/sysfs/symlink.c文件中,其内容如下:
- 117
-
-
-
-
-
- 123int sysfs_create_link(struct kobject *kobj, struct kobject *target,
- 124 const char *name)
- 125{
- 126 return sysfs_do_create_link(kobj, target, name, 1);
- 127}
由注释可以看出,第一个参数是我们将要创建的链接所在的目录,第二个参数是链接指向的源,第三个参数是链接的名字。
回到device_create_sys_dev_entry函数,这里我们举个例子,如果dev的设备号major=20, minor=1,设备名为dev->init_name,那么,
如果dev->class为NULL,则产生的链接文件为/sys/dev/char/20:1,链接指向的文件为/sys/devices/dev->init_name。
如果dev->class不为NULL,则产生的链接文件将位于dev->class->dev_kobj所对应的目录下,链接指向的文件为/sys/devices/virual/dev->init_name。
回到device_add函数:
1072行,如果device主设备号不为0,还会调用devtmpfs_create_node(dev)函数在/dev目录下自动生成一个设备节点。在Linux早期,/dev下的设备节点是通过mknod命令手动添加的,现在通过devtmpfs文件系统,就可以在调用device_register函数注册设备时自动向/dev目录下添加设备节点,该节点的名字就是dev->init_name。
1075行,调用device_add_class_symlinks(dev)函数,该函数定义在drivers/base/core.c文件中,创建dev->class相关的链接。这个函数我们不仔细分析了。
1078行,调用device_add_attrs(dev)函数,该函数定义在drivers/base/core.c文件中,该函数创建与dev->class相关的一些属性文件,这个函数我们不仔细分析了。
1081行,调用bus_add_device(dev)函数,该函数定义在drivers/base/bus.c文件中,该函数在/sys/bus/devices目录下创建一个链接文件,指向/sys/devices/dev->init_name。
1097行,调用bus_probe_device(dev)函数为新添加的device探测匹配的device_driver。该函数定义在drivers/base/bus.c文件中,其内容如下:
- 523
-
-
-
-
-
- 529void bus_probe_device(struct device *dev)
- 530{
- 531 struct bus_type *bus = dev->bus;
- 532 struct subsys_interface *sif;
- 533 int ret;
- 534
- 535 if (!bus)
- 536 return;
- 537
- 538 if (bus->p->drivers_autoprobe) {
- 539 ret = device_attach(dev);
- 540 WARN_ON(ret < 0);
- 541 }
- 542
- 543 mutex_lock(&bus->p->mutex);
- 544 list_for_each_entry(sif, &bus->p->interfaces, node)
- 545 if (sif->add_dev)
- 546 sif->add_dev(dev, sif);
- 547 mutex_unlock(&bus->p->mutex);
- 548}
531行,取得device所依附的bus。
538-539行,如果bus->p->drivers_autoprobe值为1,调用device_attach(dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
- 398
-
-
-
-
-
-
-
-
-
-
-
-
-
- 412int device_attach(struct device *dev)
- 413{
- 414 int ret = 0;
- 415
- 416 device_lock(dev);
- 417 if (dev->driver) {
- 418 if (klist_node_attached(&dev->p->knode_driver)) {
- 419 ret = 1;
- 420 goto out_unlock;
- 421 }
- 422 ret = device_bind_driver(dev);
- 423 if (ret == 0)
- 424 ret = 1;
- 425 else {
- 426 dev->driver = NULL;
- 427 ret = 0;
- 428 }
- 429 } else {
- 430 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
- 431 pm_request_idle(dev);
- 432 }
- 433out_unlock:
- 434 device_unlock(dev);
- 435 return ret;
- 436}
417-429行,如果dev->driver不为空,表明device已经与相应的device_driver进行了绑定,这种情况下只需要调用device_bind_driver函数在sysfs文件系统中建立device与其device_driver的相互关联。
430行,调用bus_for_each_drv函数,该函数定义在drivers/base/bus.c文件中,其内容如下:
- 417
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 436int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
- 437 void *data, int (*fn)(struct device_driver *, void *))
- 438{
- 439 struct klist_iter i;
- 440 struct device_driver *drv;
- 441 int error = 0;
- 442
- 443 if (!bus)
- 444 return -EINVAL;
- 445
- 446 klist_iter_init_node(&bus->p->klist_drivers, &i,
- 447 start ? &start->p->knode_bus : NULL);
- 448 while ((drv = next_driver(&i)) && !error)
- 449 error = fn(drv, data);
- 450 klist_iter_exit(&i);
- 451 return error;
- 452}
448-449行,这个while循环依次遍历bus->p->klist_drivers中的所有device_driver,对于每个device_driver,调用fn(drv, data)函数,看该device_driver是否支持该device。如果匹配成功,fn函数返回非0值。参数传递过来的fn函数是__device_attach,该函数定义在drivers/base/dd.c文件中,其内容如下:
- 388static int __device_attach(struct device_driver *drv, void *data)
- 389{
- 390 struct device *dev = data;
- 391
- 392 if (!driver_match_device(drv, dev))
- 393 return 0;
- 394
- 395 return driver_probe_device(drv, dev);
- 396}
392行,调用driver_match_device(drv, dev)函数,检查device_driver是否支持该device,该函数定义在drivers/base/base.h文件中,其内容如下:
- 116static inline int driver_match_device(struct device_driver *drv,
- 117 struct device *dev)
- 118{
- 119 return drv->bus->match ? drv->bus->match(dev, drv) : 1;
- 120}
如果定义了drv->bus->match函数,则调用该函数,否则返回1。
回到__device_attach函数:
395行,调用driver_probe_device(drv, dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
- 360
-
-
-
-
-
-
-
-
-
-
- 371int driver_probe_device(struct device_driver *drv, struct device *dev)
- 372{
- 373 int ret = 0;
- 374
- 375 if (!device_is_registered(dev))
- 376 return -ENODEV;
- 377
- 378 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
- 379 drv->bus->name, __func__, dev_name(dev), drv->name);
- 380
- 381 pm_runtime_barrier(dev);
- 382 ret = really_probe(dev, drv);
- 383 pm_request_idle(dev);
- 384
- 385 return ret;
- 386}
375行,调用device_is_registered函数判断device是否已经在sysfs系统中注册过,如果还没有注册过,则返回ENODEV,退出。该函数定义在include/linux/device.h文件中,其内容如下:
- 787static inline int device_is_registered(struct device *dev)
- 788{
- 789 return dev->kobj.state_in_sysfs;
- 790}
382行,调用really_probe(dev, drv)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
- 265static int really_probe(struct device *dev, struct device_driver *drv)
- 266{
- 267 int ret = 0;
- 268
- 269 atomic_inc(&probe_count);
- 270 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
- 271 drv->bus->name, __func__, drv->name, dev_name(dev));
- 272 WARN_ON(!list_empty(&dev->devres_head));
- 273
- 274 dev->driver = drv;
- 275
- 276
- 277 ret = pinctrl_bind_pins(dev);
- 278 if (ret)
- 279 goto probe_failed;
- 280
- 281 if (driver_sysfs_add(dev)) {
- 282 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
- 283 __func__, dev_name(dev));
- 284 goto probe_failed;
- 285 }
- 286
- 287 if (dev->bus->probe) {
- 288 ret = dev->bus->probe(dev);
- 289 if (ret)
- 290 goto probe_failed;
- 291 } else if (drv->probe) {
- 292 ret = drv->probe(dev);
- 293 if (ret)
- 294 goto probe_failed;
- 295 }
- 296
- 297 driver_bound(dev);
- 298 ret = 1;
- 299 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
- 300 drv->bus->name, __func__, dev_name(dev), drv->name);
- 301 goto done;
- 302
- 303probe_failed:
- 304 devres_release_all(dev);
- 305 driver_sysfs_remove(dev);
- 306 dev->driver = NULL;
- 307 dev_set_drvdata(dev, NULL);
- 308
- 309 if (ret == -EPROBE_DEFER) {
- 310
- 311 dev_info(dev, "Driver %s requests probe deferral\n", drv->name);
- 312 driver_deferred_probe_add(dev);
- 313 } else if (ret != -ENODEV && ret != -ENXIO) {
- 314
- 315 printk(KERN_WARNING
- 316 "%s: probe of %s failed with error %d\n",
- 317 drv->name, dev_name(dev), ret);
- 318 } else {
- 319 pr_debug("%s: probe of %s rejects match %d\n",
- 320 drv->name, dev_name(dev), ret);
- 321 }
- 322
-
-
-
- 326 ret = 0;
- 327done:
- 328 atomic_dec(&probe_count);
- 329 wake_up(&probe_waitqueue);
- 330 return ret;
- 331}
287-295行,如果定义了dev->bus->probe函数,则调用该函数;如果没有定义dev->bus->probe函数,但是定义了drv->probe函数,则调用drv->probe函数。这里,我们一般写Linux驱动程序时都要实现的probe函数就会被调用了。
297行,调用driver_bound(dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
- 182static void driver_bound(struct device *dev)
- 183{
- 184 if (klist_node_attached(&dev->p->knode_driver)) {
- 185 printk(KERN_WARNING "%s: device %s already bound\n",
- 186 __func__, kobject_name(&dev->kobj));
- 187 return;
- 188 }
- 189
- 190 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
- 191 __func__, dev->driver->name);
- 192
- 193 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
- 194
- 195
-
-
-
- 199 driver_deferred_probe_del(dev);
- 200 driver_deferred_probe_trigger();
- 201
- 202 if (dev->bus)
- 203 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
- 204 BUS_NOTIFY_BOUND_DRIVER, dev);
- 205}
193行,调用klist_add_tail函数将device加入到device_driver的driver->p->klist_devices链表中。
至此,我们一步一步回退driver_bound->really_probe ->driver_probe_device->__device_attach->device_attach->bus_probe_device->device_add->device_register
这样,device_register函数我们就分析完了,也就清楚了device的注册过程。