日期:2014-05-16  浏览次数:20730 次

Linux设备模型分析之bus(基于3.10.1内核)

作者:刘昊昱 

博客:http://blog.csdn.net/liuhaoyutz

内核版本:3.10.1

 
一、bus定义
Linux设备驱动模型中的bus,即可以是物理总线(如PCI、I2C总线)的抽象,也可以是出于设备驱动模型架构需要而定义的虚拟的“platform”总线。一个符合Linux设备驱动模型的device或device_driver必须挂靠在一个bus上,无论这个bus是物理的还是虚拟的。
Linux内核使用bus_type结构体来描述bus,该结构体定义在include/linux/device.h文件中,其内容如下:
  56/**
  57 * struct bus_type - The bus type of the device
  58 *
  59 * @name:   The name of the bus.
  60 * @dev_name:   Used for subsystems to enumerate devices like ("foo%u", dev->id).
  61 * @dev_root:   Default device to use as the parent.
  62 * @bus_attrs:  Default attributes of the bus.
  63 * @dev_attrs:  Default attributes of the devices on the bus.
  64 * @drv_attrs:  Default attributes of the device drivers on the bus.
  65 * @match:  Called, perhaps multiple times, whenever a new device or driver
  66 *      is added for this bus. It should return a nonzero value if the
  67 *      given device can be handled by the given driver.
  68 * @uevent: Called when a device is added, removed, or a few other things
  69 *      that generate uevents to add the environment variables.
  70 * @probe:  Called when a new device or driver add to this bus, and callback
  71 *      the specific driver's probe to initial the matched device.
  72 * @remove: Called when a device removed from this bus.
  73 * @shutdown:   Called at shut-down time to quiesce the device.
  74 * @suspend:    Called when a device on this bus wants to go to sleep mode.
  75 * @resume: Called to bring a device on this bus out of sleep mode.
  76 * @pm:     Power management operations of this bus, callback the specific
  77 *      device driver's pm-ops.
  78 * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
  79 *              driver implementations to a bus and allow the driver to do
  80 *              bus-specific setup
  81 * @p:      The private data of the driver core, only the driver core can
  82 *      touch this.
  83 *
  84 * A bus is a channel between the processor and one or more devices. For the
  85 * purposes of the device model, all devices are connected via a bus, even if
  86 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
  87 * A USB controller is usually a PCI device, for example. The device model
  88 * represents the actual connections between buses and the devices they control.
  89 * A bus is represented by the bus_type structure. It contains the name, the
  90 * default attributes, the bus' methods, PM operations, and the driver core's
  91 * private data.
  92 */
  93struct bus_type {
  94    const char      *name;
  95    const char      *dev_name;
  96    struct device       *dev_root;
  97    struct bus_attribute    *bus_attrs;
  98    struct device_attribute *dev_attrs;
  99    struct driver_attribute *drv_attrs;
 100
 101    int (*match)(struct device *dev, struct device_driver *drv);
 102    int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
 103    int (*probe)(struct device *dev);
 104    int (*remove)(struct device *dev);
 105    void (*shutdown)(struct device *dev);
 106
 107    int (*suspend)(struct device *dev, pm_message_t state);
 108    int (*resume)(struct device *dev);
 109
 110    const struct dev_pm_ops *pm;
 111
 112    struct iommu_ops *iommu_ops;
 113
 114    struct subsys_private *p;
 115    struct lock_class_key lock_key;
 116};


下面我们来看一下bus_type各个成员的作用:
name,代表bus的名字。
dev_name,用来遍历bus上的device。
dev_root,bus上device的根节点。
bus_attrs,bus的属性。
dev_attrs,bus上device的属性。
drv_attrs,bus上device_driver的属性。
match,当一个新的device或device driver被加入到该bus时,match函数会被调用进行匹配操作。更详细的说,当一个device被加入时,会和bus上的所有device_driver进行匹配操作,如果有device_driver能支持这个device,则匹配成功,match返回非0值。当一个device_driver被加入到