作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
内核版本:3.10.1
http://blog.csdn.net/liuhaoyutz/article/details/14228761
一、device定义
device结构体定义在include/linux/device.h文件中:
- 598/**
- 599 * struct device - The basic device structure
- 600 * @parent: The device's "parent" device, the device to which it is attached.
- 601 * In most cases, a parent device is some sort of bus or host
- 602 * controller. If parent is NULL, the device, is a top-level device,
- 603 * which is not usually what you want.
- 604 * @p: Holds the private data of the driver core portions of the device.
- 605 * See the comment of the struct device_private for detail.
- 606 * @kobj: A top-level, abstract class from which other classes are derived.
- 607 * @init_name: Initial name of the device.
- 608 * @type: The type of device.
- 609 * This identifies the device type and carries type-specific
- 610 * information.
- 611 * @mutex: Mutex to synchronize calls to its driver.
- 612 * @bus: Type of bus device is on.
- 613 * @driver: Which driver has allocated this
- 614 * @platform_data: Platform data specific to the device.
- 615 * Example: For devices on custom boards, as typical of embedded
- 616 * and SOC based hardware, Linux often uses platform_data to point
- 617 * to board-specific structures describing devices and how they
- 618 * are wired. That can include what ports are available, chip
- 619 * variants, which GPIO pins act in what additional roles, and so
- 620 * on. This shrinks the "Board Support Packages" (BSPs) and
- 621 * minimizes board-specific #ifdefs in drivers.
- 622 * @power: For device power management.
- 623 * See Documentation/power/devices.txt for details.
- 624 * @pm_domain: Provide callbacks that are executed during system suspend,
- 625 * hibernation, system resume and during runtime PM transitions
- 626 * along with subsystem-level and driver-level callbacks.
- 627 * @pins: For device pin management.
- 628 * See Documentation/pinctrl.txt for details.
- 629 * @numa_node: NUMA node this device is close to.
- 630 * @dma_mask: Dma mask (if dma'ble device).
- 631 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
- 632 * hardware supports 64-bit addresses for consistent allocations
- 633 * such descriptors.
- 634 * @dma_parms: A low level driver may set these to teach IOMMU code about
- 635 * segment limitations.
- 636 * @dma_pools: Dma pools (if dma'ble device).
- 637 * @dma_mem: Internal for coherent mem override.
- 638 * @archdata: For arch-specific additions.
- 639 * @of_node: Associated device tree node.
- 640 * @acpi_node: Associated ACPI device node.
- 641 * @devt: For creating the sysfs "dev".
- 642 * @id: device instance
- 643 * @devres_lock: Spinlock to protect the resource of the device.
- 644 * @devres_head: The resources list of the device.
- 645 * @knode_class: The node used to add the device to the class list.
- 646 * @class: The class of the device.
- 647 * @groups: Optional attribute groups.
- 648 * @release: Callback to free the device after all references have
- 649 * gone away. This should be set by the allocator of the
- 650 * device (i.e. the bus driver that discovered the device).
- 651 *
- 652 * At the lowest level, every device in a Linux system is represented by an
- 653 * instance of struct device. The device structure contains the information
- 654 * that the device model core needs to model the system. Most subsystems,
- 655 * however, track additional information about the devices they host. As a
- 656 * result, it is rare for devices to be represented by bare device structures;
- 657 * instead, that structure, like kobject structures, is usually embedded within
- 658 * a higher-level representation of the device.
- 659 */
- 660struct device {
- 661 struct device *parent;
- 662
- 663 struct device_private *p;
- 664
- 665 struct kobject kobj;
- 666 const char *init_name; /* initial name of the device */
- 667 const struct device_type *type;
- 668
- 669 struct mutex mutex; /* mutex to synchronize calls to
- 670 * its driver.
- 671 */
- 672
- 673 struct bus_type *bus; /* type of bus device is on */
- 674 struct device_driver *driver; /* which driver has allocated this
- 675 device */
- 676 void *platform_data; /* Platform specific data, device
- 677 core doesn't touch it */
- 678 struct dev_pm_info power;
- 679 struct dev_pm_domain *pm_domain;
- 680
- 681#ifdef CONFIG_PINCTRL
- 682 struct dev_pin_info *pins;
- 683#endif
- 684
- 685#ifdef CONFIG_NUMA
- 686 int numa_node; /* NUMA node this device is close to */
- 687#endif
- 688 u64 *dma_mask; /* dma mask (if dma'able device) */
- 689 u64 coherent_dma_mask;/* Like dma_mask, but for
- 690 alloc_coherent mappings as
- 691 not all hardware supports
- 692 64 bit addresses for consistent
- 693 allocations such descriptors. */
- 694
- 695 struct device_dma_parameters *dma_parms;
- 696
- 697 struct list_head dma_pools; /* dma pools (if dma'ble) */
- 698
- 699 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
- 700 override */
- 701#ifdef CONFIG_CMA
- 702 struct cma *cma_area; /* contiguous memory area for dma
- 703 allocations */
- 704#endif
- 705 /* arch specific additions */
- 706 struct dev_archdata archdata;
- 707
- 708 struct device_node *of_node; /* associated device tree node */
- 709 struct acpi_dev_node acpi_node; /* associated ACPI device node */
- 710
- 711 dev_t devt; /* dev_t, creates the sysfs "dev" */
- 712 u32 id; /* device instance */
- 713
- 714 spinlock_t devres_lock;
- 715 struct list_head devres_head;
- 716
- 717 struct klist_node knode_class;
- 718 struct class *class;
- 719 const struct attribute_group **groups; /* optional groups */
- 720
- 721 void (*release)(struct device *dev);
- 722 struct iommu_group *iommu_group;
- 723};
598/** 599 * struct device - The basic device structure 600 * @parent: The device's "parent" device, the device to which it is attached. 601 * In most cases, a parent device is some sort of bus or host 602 * controller. If parent is NULL, the device, is a top-level device, 603 * which is not usually what you want. 604 * @p: Holds the private data of the driver core portions of the device. 605 * See the comment of the struct device_private for detail. 606 * @kobj: A top-level, abstract class from which other classes are derived. 607 * @init_name: Initial name of the device. 608 * @type: The type of device. 609 * This identifies the device type and carries type-specific 610 * information. 611 * @mutex: Mutex to synchronize calls to its driver. 612 * @bus: Type of bus device is on. 613 * @driver: Which driver has allocated this 614 * @platform_data: Platform data specific to the device. 615 * Example: For devices on custom boards, as typical of embedded 616 * and SOC based hardware, Linux often uses platform_data to point 617 * to board-specific structures describing devices and how they 618 * are wired. That can include what ports are available, chip 619 * variants, which GPIO pins act in what additional roles, and so 620 * on. This shrinks the "Board Support Packages" (BSPs) and 621 * minimizes board-specific #ifdefs in drivers. 622 * @power: For device power management. 623 * See Documentation/power/devices.txt for details. 624 * @pm_domain: Provide callbacks that are executed during system suspend, 625 * hibernation, system resume and during runtime PM transitions 626 * along with subsystem-level and driver-level callbacks. 627 * @pins: For device pin management. 628 * See Documentation/pinctrl.txt for details. 629 * @numa_node: NUMA node this device is close to. 630 * @dma_mask: Dma mask (if dma'ble device). 631 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 632 * hardware supports 64-bit addresses for consistent allocations 633 * such descriptors. 634 * @dma_parms: A low level driver may set these to teach IOMMU code about 635 * segment limitations. 636 * @dma_pools: Dma pools (if dma'ble device). 637 * @dma_mem: Internal for coherent mem override. 638 * @archdata: For arch-specific additions. 639 * @of_node: Associated device tree node. 640 * @acpi_node: Associated ACPI device node. 641 * @devt: For creating the sysfs "dev". 642 * @id: device instance 643 * @devres_lock: Spinlock to protect the resource of the device. 644 * @devres_head: The resources list of the device. 645 * @knode_class: The node used to add the device to the class list. 646 * @class: The class of the device. 647 * @groups: Optional attribute groups. 648 * @release: Callback to free the device after all references have 649 * gone away. This should be set by the allocator of the 650 * device (i.e. the bus driver that discovered the device). 651 * 652 * At the lowest level, every device in a Linux system is represented by an 653 * instance of struct device. The device structure contains the information 654 * that the device model core needs to model the system. Most subsystems, 655 * however, track additional information about the devices they host. As a 656 * result, it is rare for devices to be represented by bare device structures; 657 * instead, that structure, like kobject structures, is usually embedded within 658 * a higher-level representation of the device. 659 */ 660struct device { 661 struct device *parent; 662 663 struct device_private *p; 664 665 struct kobject kobj; 666 const char *init_name; /* initial name of the device */ 667 const struct device_type *type; 668 669 struct mutex mutex; /* mutex to synchronize calls to 670 * its driver. 671 */ 672 673 struct bus_type *bus; /* type of bus device is on */ 674 struct device_driver *driver; /* which driver has allocated this 675 device */ 676 void *platform_data; /* Platform specific data, device 677 core doesn't touch it */ 678 struct dev_pm_info power; 679 struct dev_pm_domain *pm_domain; 680 681#ifdef CONFIG_PINCTRL 682 struct dev_pin_info *pins; 683#endif 684 685#ifdef CONFIG_NUMA 686 int numa_node; /* NUMA node this device is close to */ 687#endif 688 u64 *dma_mask; /* dma mask (if dma'able device) */ 689 u64 coherent_dma_mask;/* Like dma_mask, but for 690 alloc_coherent mappings as 691 not all hardware supports 692 64 bit addresses for consistent 693 allocations such descriptors. */ 694 695 struct device_dma_parameters *dma_parms; 696 697 struct list_head dma_pools; /* dma pools (if dma'ble) */ 698 699 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 700 override */ 701#ifdef CONFIG_CMA 702 struct cma *cma_area; /* contiguous memory area for dma 703 allocations */ 704#endif 705 /* arch specific additions */ 706 struct dev_archdata archdata; 707 708 struct device_node *of_node; /* associated device tree node */ 709 struct acpi_dev_node acpi_node; /* associated ACPI device node */ 710 711 dev_t devt; /* dev_t, creates the sysfs "dev" */ 712 u32 id; /* device instance */ 713 714 spinlock_t devres_lock; 715 struct list_head devres_head; 716 717 struct klist_node knode_class; 718 struct class *class; 719 const struct attribute_group **groups; /* optional groups */ 720 721 void (*release)(struct device *dev); 722 struct iommu_group *iommu_group; 723};
我们来看一下device结构体的几个重要成员变量。
parent,device的父设备,即device依附的设备,通常父设备是某种类型的bus或host controller。如果parent的值是NULL,那么device将是一个顶层设备,这通常不是我们想要的。
p,device的私有数据,它是struct device_private结构体变量,这个结构体定义在drivers/base/base.h文件中,其内容如下:
- 55/**
- 56 * struct device_private - structure to hold the private to the driver core portions of the device structure.
- 57 *
- 58 * @klist_children - klist containing all children of this device
- 59 * @knode_parent - node in sibling list
- 60 * @knode_driver - node in driver list
- 61 * @knode_bus - node in bus list
- 62 * @deferred_probe - entry in deferred_probe_list which is used to retry the
- 63 * binding of drivers which were unable to get all the resources needed by
- 64 * the device; typically because it depends on another driver getting
- 65 * probed first.
- 66 * @driver_data - private pointer for driver specific info. Will turn into a
- 67 * list soon.
- 68 * @device - pointer back to the struct class that this structure is
- 69 * associated with.
- 70 *
- 71 * Nothing outside of the driver core should ever touch these fields.
- 72 */
- 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};
55/** 56 * struct device_private - structure to hold the private to the driver core portions of the device structure. 57 * 58 * @klist_children - klist containing all children of this device 59 * @knode_parent - node in sibling list 60 * @knode_driver - node in driver list 61 * @knode_bus - node in bus list 62 * @deferred_probe - entry in deferred_probe_list which is used to retry the 63 * binding of drivers which were unable to get all the resources needed by 64 * the device; typically because it depends on another driver getting 65 * probed first. 66 * @driver_data - private pointer for driver specific info. Will turn into a 67 * list soon. 68 * @device - pointer back to the struct class that this structure is 69 * associated with. 70 * 71 * Nothing outside of the driver core should ever touch these fields. 72 */ 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}
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/**
- 1148 * device_register - register a device with the system.
- 1149 * @dev: pointer to the device structure
- 1150 *
- 1151 * This happens in two clean steps - initialize the device
- 1152 * and add it to the system. The two steps can be called
- 1153 * separately, but this is the easiest and most common.
- 1154 * I.e. you should only call the two helpers separately if
- 1155 * have a clearly defined need to use and refcount the device
- 1156 * before it is added to the hierarchy.
- 1157 *
- 1158 * For more information, see the kerneldoc for device_initialize()
- 1159 * and device_add().
- 1160 *
- 1161 * NOTE: _Never_ directly free @dev after calling this function, even
- 1162 * if it returned an error! Always use put_device() to give up the
- 1163 * reference initialized in this function instead.
- 1164 */
- 1165int device_register(struct device *dev)
- 1166{
- 1167 device_initialize(dev);
- 1168 return device_add(dev);
- 1169}
1147/** 1148 * device_register - register a device with the system. 1149 * @dev: pointer to the device structure 1150 * 1151 * This happens in two clean steps - initialize the device 1152 * and add it to the system. The two steps can be called 1153 * separately, but this is the easiest and most common. 1154 * I.e. you should only call the two helpers separately if 1155 * have a clearly defined need to use and refcount the device 1156 * before it is added to the hierarchy. 1157 * 1158 * For more information, see the kerneldoc for device_initialize() 1159 * and device_add(). 1160 * 1161 * NOTE: _Never_ directly free @dev after calling this function, even 1162 * if it returned an error! Always use put_device() to give up the 1163 * reference initialized in this function instead. 1164 */ 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/**
- 676 * device_initialize - init device structure.
- 677 * @dev: device.
- 678 *
- 679 * This prepares the device for use by other layers by initializing
- 680 * its fields.
- 681 * It is the first half of device_register(), if called by
- 682 * that function, though it can also be called separately, so one
- 683 * may use @dev's fields. In particular, get_device()/put_device()
- 684 * may be used for reference counting of @dev after calling this
- 685 * function.
- 686 *
- 687 * All fields in @dev must be initialized by the caller to 0, except
- 688 * for those explicitly set to some other value. The simplest
- 689 * approach is to use kzalloc() to allocate the structure containing
- 690 * @dev.
- 691 *
- 692 * NOTE: Use put_device() to give up your reference instead of freeing
- 693 * @dev directly once you have called this function.
- 694 */
- 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}
675/** 676 * device_initialize - init device structure. 677 * @dev: device. 678 * 679 * This prepares the device for use by other layers by initializing 680 * its fields. 681 * It is the first half of device_register(), if called by 682 * that function, though it can also be called separately, so one 683 * may use @dev's fields. In particular, get_device()/put_device() 684 * may be used for reference counting of @dev after calling this 685 * function. 686 * 687 * All fields in @dev must be initialized by the caller to 0, except 688 * for those explicitly set to some other value. The simplest 689 * approach is to use kzalloc() to allocate the structure containing 690 * @dev. 691 * 692 * NOTE: Use put_device() to give up your reference instead of freeing 693 * @dev directly once you have called this function. 694 */ 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/**
- 981 * device_add - add device to device hierarchy.
- 982 * @dev: device.
- 983 *
- 984 * This is part 2 of device_register(), though may be called
- 985 * separately _iff_ device_initialize() has been called separately.
- 986 *
- 987 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
- 988 * to the global and sibling lists for the device, then
- 989 * adds it to the other relevant subsystems of the driver model.
- 990 *
- 991 * Do not call this routine or device_register() more than once for
- 992 * any device structure. The driver model core is not designed to work
- 993 * with devices that get unregistered and then spring back to life.
- 994 * (Among other things, it's very hard to guarantee that all references
- 995 * to the previous incarnation of @dev have been dropped.) Allocate
- 996 * and register a fresh new struct device instead.
- 997 *
- 998 * NOTE: _Never_ directly free @dev after calling this function, even
- 999 * if it returned an error! Always use put_device() to give up your
- 1000 * reference instead.
- 1001 */
- 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 /*
- 1020 * for statically allocated devices, which should all be converted
- 1021 * some day, we need to initialize the name. We prevent reading back
- 1022 * the name, and force the use of dev_name()
- 1023 */
- 1024 if (dev->init_name) {
- 1025 dev_set_name(dev, "%s", dev->init_name);
- 1026 dev->init_name = NULL;
- 1027 }
- 1028
- 1029 /* subsystems can specify simple device enumeration */
- 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 /* use parent numa_node */
- 1046 if (parent)
- 1047 set_dev_node(dev, dev_to_node(parent));
- 1048
- 1049 /* first, register with generic layer. */
- 1050 /* we require the name to be set before, and pass NULL */
- 1051 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
- 1052 if (error)
- 1053 goto Error;
- 1054
- 1055 /* notify platform of device entry */
- 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 /* Notify clients of device addition. This call must come
- 1090 * after dpm_sysfs_add() and before kobject_uevent().
- 1091 */
- 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 /* tie the class to the device */
- 1105 klist_add_tail(&dev->knode_class,
- 1106 &dev->class->p->klist_devices);
- 1107
- 1108 /* notify any interfaces that the device is here */
- 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}
980/** 981 * device_add - add device to device hierarchy. 982 * @dev: device. 983 * 984 * This is part 2 of device_register(), though may be called 985 * separately _iff_ device_initialize() has been called separately. 986 * 987 * This adds @dev to the kobject hierarchy via kobject_add(), adds it 988 * to the global and sibling lists for the device, then 989 * adds it to the other relevant subsystems of the driver model. 990 * 991 * Do not call this routine or device_register() more than once for 992 * any device structure. The driver model core is not designed to work 993 * with devices that get unregistered and then spring back to life. 994 * (Among other things, it's very hard to guarantee that all references 995 * to the previous incarnation of @dev have been dropped.) Allocate 996 * and register a fresh new struct device instead. 997 * 998 * NOTE: _Never_ directly free @dev after calling this function, even 999 * if it returned an error! Always use put_device() to give up your 1000 * reference instead. 1001 */ 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 /* 1020 * for statically allocated devices, which should all be converted 1021 * some day, we need to initialize the name. We prevent reading back 1022 * the name, and force the use of dev_name() 1023 */ 1024 if (dev->init_name) { 1025 dev_set_name(dev, "%s", dev->init_name); 1026 dev->init_name = NULL; 1027 } 1028 1029 /* subsystems can specify simple device enumeration */ 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 /* use parent numa_node */ 1046 if (parent) 1047 set_dev_node(dev, dev_to_node(parent)); 1048 1049 /* first, register with generic layer. */ 1050 /* we require the name to be set before, and pass NULL */ 1051 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL); 1052 if (error) 1053 goto Error; 1054 1055 /* notify platform of device entry */ 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 /* Notify clients of device addition. This call must come 1090 * after dpm_sysfs_add() and before kobject_uevent(). 1091 */ 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 /* tie the class to the device */ 1105 klist_add_tail(&dev->knode_class, 1106 &dev->class->p->klist_devices); 1107 1108 /* notify any interfaces that the device is here */ 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}
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 /* block disks show up in /sys/block */
- 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 /*
- 788 * If we have no parent, we live in "virtual".
- 789 * Class-devices with a non class-device as parent, live
- 790 * in a "glue" directory to prevent namespace collisions.
- 791 */
- 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 /* find our class-directory at the parent and reference it */
- 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 /* or create a new class-directory at the parent device */
- 815 k = class_dir_create_and_add(dev->class, parent_kobj);
- 816 /* do not emit an uevent for this simple "glue" directory */
- 817 mutex_unlock(&gdp_mutex);
- 818 return k;
- 819 }
- 820
- 821 /* subsystems can specify a default root directory for their devices */
- 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}
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 /* block disks show up in /sys/block */ 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 /* 788 * If we have no parent, we live in "virtual". 789 * Class-devices with a non class-device as parent, live 790 * in a "glue" directory to prevent namespace collisions. 791 */ 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 /* find our class-directory at the parent and reference it */ 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 /* or create a new class-directory at the parent device */ 815 k = class_dir_create_and_add(dev->class, parent_kobj); 816 /* do not emit an uevent for this simple "glue" directory */ 817 mutex_unlock(&gdp_mutex); 818 return k; 819 } 820 821 /* subsystems can specify a default root directory for their devices */ 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);
403static struct device_attribute uevent_attr = 404 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
device_attribute结构体定义在include/linux/device.h文件中,其内容如下:
- 478/* interface for exporting device attributes */
- 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};
478/* interface for exporting device attributes */ 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/**
- 564 * device_create_file - create sysfs attribute file for device.
- 565 * @dev: device.
- 566 * @attr: device attribute descriptor.
- 567 */
- 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}
563/** 564 * device_create_file - create sysfs attribute file for device. 565 * @dev: device. 566 * @attr: device attribute descriptor. 567 */ 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);
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}
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/**
- 921 * device_to_dev_kobj - select a /sys/dev/ directory for the device
- 922 * @dev: device
- 923 *
- 924 * By default we select char/ for new entries. Setting class->dev_obj
- 925 * to NULL prevents an entry from being created. class->dev_kobj must
- 926 * be set (or cleared) before any devices are registered to the class
- 927 * otherwise device_create_sys_dev_entry() and
- 928 * device_remove_sys_dev_entry() will disagree about the presence of
- 929 * the link.
- 930 */
- 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}
920/** 921 * device_to_dev_kobj - select a /sys/dev/ directory for the device 922 * @dev: device 923 * 924 * By default we select char/ for new entries. Setting class->dev_obj 925 * to NULL prevents an entry from being created. class->dev_kobj must 926 * be set (or cleared) before any devices are registered to the class 927 * otherwise device_create_sys_dev_entry() and 928 * device_remove_sys_dev_entry() will disagree about the presence of 929 * the link. 930 */ 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 })
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/**
- 118 * sysfs_create_link - create symlink between two objects.
- 119 * @kobj: object whose directory we're creating the link in.
- 120 * @target: object we're pointing to.
- 121 * @name: name of the symlink.
- 122 */
- 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}
117/** 118 * sysfs_create_link - create symlink between two objects. 119 * @kobj: object whose directory we're creating the link in. 120 * @target: object we're pointing to. 121 * @name: name of the symlink. 122 */ 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/**
- 524 * bus_probe_device - probe drivers for a new device
- 525 * @dev: device to probe
- 526 *
- 527 * - Automatically probe for a driver if the bus allows it.
- 528 */
- 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}
523/** 524 * bus_probe_device - probe drivers for a new device 525 * @dev: device to probe 526 * 527 * - Automatically probe for a driver if the bus allows it. 528 */ 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/**
- 399 * device_attach - try to attach device to a driver.
- 400 * @dev: device.
- 401 *
- 402 * Walk the list of drivers that the bus has and call
- 403 * driver_probe_device() for each pair. If a compatible
- 404 * pair is found, break out and return.
- 405 *
- 406 * Returns 1 if the device was bound to a driver;
- 407 * 0 if no matching driver was found;
- 408 * -ENODEV if the device is not registered.
- 409 *
- 410 * When called for a USB interface, @dev->parent lock must be held.
- 411 */
- 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}
398/** 399 * device_attach - try to attach device to a driver. 400 * @dev: device. 401 * 402 * Walk the list of drivers that the bus has and call 403 * driver_probe_device() for each pair. If a compatible 404 * pair is found, break out and return. 405 * 406 * Returns 1 if the device was bound to a driver; 407 * 0 if no matching driver was found; 408 * -ENODEV if the device is not registered. 409 * 410 * When called for a USB interface, @dev->parent lock must be held. 411 */ 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/**
- 418 * bus_for_each_drv - driver iterator
- 419 * @bus: bus we're dealing with.
- 420 * @start: driver to start iterating on.
- 421 * @data: data to pass to the callback.
- 422 * @fn: function to call for each driver.
- 423 *
- 424 * This is nearly identical to the device iterator above.
- 425 * We iterate over each driver that belongs to @bus, and call
- 426 * @fn for each. If @fn returns anything but 0, we break out
- 427 * and return it. If @start is not NULL, we use it as the head
- 428 * of the list.
- 429 *
- 430 * NOTE: we don't return the driver that returns a non-zero
- 431 * value, nor do we leave the reference count incremented for that
- 432 * driver. If the caller needs to know that info, it must set it
- 433 * in the callback. It must also be sure to increment the refcount
- 434 * so it doesn't disappear before returning to the caller.
- 435 */
- 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}
417/** 418 * bus_for_each_drv - driver iterator 419 * @bus: bus we're dealing with. 420 * @start: driver to start iterating on. 421 * @data: data to pass to the callback. 422 * @fn: function to call for each driver. 423 * 424 * This is nearly identical to the device iterator above. 425 * We iterate over each driver that belongs to @bus, and call 426 * @fn for each. If @fn returns anything but 0, we break out 427 * and return it. If @start is not NULL, we use it as the head 428 * of the list. 429 * 430 * NOTE: we don't return the driver that returns a non-zero 431 * value, nor do we leave the reference count incremented for that 432 * driver. If the caller needs to know that info, it must set it 433 * in the callback. It must also be sure to increment the refcount 434 * so it doesn't disappear before returning to the caller. 435 */ 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}
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}
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/**
- 361 * driver_probe_device - attempt to bind device & driver together
- 362 * @drv: driver to bind a device to
- 363 * @dev: device to try to bind to the driver
- 364 *
- 365 * This function returns -ENODEV if the device is not registered,
- 366 * 1 if the device is bound successfully and 0 otherwise.
- 367 *
- 368 * This function must be called with @dev lock held. When called for a
- 369 * USB interface, @dev->parent lock must be held as well.
- 370 */
- 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}
360/** 361 * driver_probe_device - attempt to bind device & driver together 362 * @drv: driver to bind a device to 363 * @dev: device to try to bind to the driver 364 * 365 * This function returns -ENODEV if the device is not registered, 366 * 1 if the device is bound successfully and 0 otherwise. 367 * 368 * This function must be called with @dev lock held. When called for a 369 * USB interface, @dev->parent lock must be held as well. 370 */ 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}
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 /* If using pinctrl, bind pins now before probing */
- 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 /* Driver requested deferred probing */
- 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 /* driver matched but the probe failed */
- 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 /*
- 323 * Ignore errors returned by ->probe so that the next driver can try
- 324 * its luck.
- 325 */
- 326 ret = 0;
- 327done:
- 328 atomic_dec(&probe_count);
- 329 wake_up(&probe_waitqueue);
- 330 return ret;
- 331}
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 /* If using pinctrl, bind pins now before probing */ 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 /* Driver requested deferred probing */ 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 /* driver matched but the probe failed */ 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 /* 323 * Ignore errors returned by ->probe so that the next driver can try 324 * its luck. 325 */ 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 /*
- 196 * Make sure the device is no longer in one of the deferred lists and
- 197 * kick off retrying all pending devices
- 198 */
- 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}
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 /* 196 * Make sure the device is no longer in one of the deferred lists and 197 * kick off retrying all pending devices 198 */ 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的注册过程。