Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160626
  • 博文数量: 12
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 900
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-19 21:27
个人简介

对于我们的阶级敌人 只有仇恨 没有宽恕

文章分类
文章存档

2014年(6)

2013年(1)

2012年(5)

我的朋友

分类: LINUX

2012-12-26 19:03:39


Bus Types 
总线类型

Definition
定义
~~~~~~~~~~
See the kerneldoc for the struct bus_type.

  1. int bus_register(struct bus_type * bus);


Declaration
声明
~~~~~~~~~~~

Each bus type in the kernel (PCI, USB, etc) should declare one static object of this type. They must initialize the name field, and may optionally initialize the match callback.
内核中每个总线类型(PCI、USB 等等)都应该声明一个此类型静态对象。它们必须初始化该对象的 name 字段,然后可选的初始化 match 回调函数。

  1. struct bus_type pci_bus_type = {
  2.     .name = "pci",
  3.     .match = pci_bus_match,
  4. };

The structure should be exported to drivers in a header file:
这个结构体应该在头文件中向驱动程序导出:

  1. extern struct bus_type pci_bus_type;


Registration
注册
~~~~~~~~~~~~

When a bus driver is initialized, it calls bus_register. This initializes the rest of the fields in the bus object and inserts it into a global list of bus types. Once the bus object is registered, the fields in it are usable by the bus driver. 
当初始化一个总线驱动时,将会调用 bus_register。这时这个总线对象剩下的字段将被初始化,然后这个对象会被插入到总线类型的一个局列表里去。一旦完成一个总线对象的注册,那么对于总线驱动来说它里面的字段就已经可用了。


Callbacks
回调函数
~~~~~~~~~

match(): Attaching Drivers to Devices
match():给设备加载驱动
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The format of device ID structures and the semantics for comparing them are inherently bus-specific. Drivers typically declare an array of device IDs of devices they support that reside in a bus-specific driver structure. 
设备 ID 的结构和比较这些 ID 的语义本质上讲都是总线定制的。驱动通常会定义个它所支持的设备 ID 的数组,并将这个数组驻留在总线定制的驱动结构中。

The purpose of the match callback is provide the bus an opportunity to determine if a particular driver supports a particular device by comparing the device IDs the driver supports with the device ID of a particular device, without sacrificing bus-specific functionality or type-safety. 
回调函数 match 的目的就是在不牺牲总线定制功能或类型安全的情况下,通过比较驱动所支持的 ID 与特定设备的 ID,给总线提供一个确定特定驱动是否支持特定设备的机会。

When a driver is registered with the bus, the bus's list of devices is iterated over, and the match callback is called for each device that does not have a driver associated with it. 
当一个驱动注册到总线后,将会遍历总线的设备列表,然后会为每个还没有和任何一个驱动关联的设备调用回调函数 match。



Device and Driver Lists
设备和驱动列表
~~~~~~~~~~~~~~~~~~~~~~~

The lists of devices and drivers are intended to replace the local lists that many buses keep. They are lists of struct devices and struct device_drivers, respectively. Bus drivers are free to use the lists as they please, but conversion to the bus-specific type may be necessary. 
设备和驱动列表是为了取代那些总线自己保存的本地列表。它们分别是 struct devices 和 struct device_drivers 的列表。总线驱动可以随意的使用这些列表,但有可能需要将其转换成总线定制类型。

The LDM core provides helper functions for iterating over each list.
LDM 核心提供了遍历各个列表的辅助函数。

  1. int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
  2.                      int (*fn)(struct device *, void *));

  3. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  4.                      void * data, int (*fn)(struct device_driver *, void *));

These helpers iterate over the respective list, and call the callback for each device or driver in the list. All list accesses are synchronized by taking the bus's lock (read currently). The reference count on each object in the list is incremented before the callback is called; it is decremented after the next object has been obtained. The lock is not held when calling the callback. 
这些辅助函数迭代各个列表,然后为这些列表里的设备或驱动调用回调函数。所有列表的访问都是通过取得总线锁(实时读取)而保持同步的。列表中每个对象的引用计数都会在调用回调函数前递增;并且会在取得下一个对象之后递减。总线锁并不在回调函数调用期间继续保持。


sysfs
~~~~~~~~
There is a top-level directory named 'bus'.
这里有一个顶层目录:‘bus’。

Each bus gets a directory in the bus directory, along with two default
directories:
每个总线在 bus 目录下都有自己的目录,其中有两个默认目录:

  1. /sys/bus/pci/
  2. |-- devices
  3. `-- drivers

Drivers registered with the bus get a directory in the bus's drivers
directory:
在总线注册的驱动会得到一个该总线 drivers 目录的子目录:

  1. /sys/bus/pci/
  2. |-- devices
  3. `-- drivers
  4.    |-- Intel ICH
  5.    |-- Intel ICH Joystick
  6.    |-- agpgart
  7.    `-- e100

Each device that is discovered on a bus of that type gets a symlink in the bus's devices directory to the device's directory in the physical hierarchy:
在这种类型总线上发现的每个设备都会在总线的 deivces 目录下获得一个符号链接,该链接指向这个设备在物理层次中的目录。

  1. /sys/bus/pci/
  2. |-- devices
  3. |  |-- 00:00.0 -> ../../../root/pci0/00:00.0
  4. |  |-- 00:01.0 -> ../../../root/pci0/00:01.0
  5. |  `-- 00:02.0 -> ../../../root/pci0/00:02.0
  6. `-- drivers

Exporting Attributes
属性导出
~~~~~~~~~~~~~~~~~~~~
  1. struct bus_attribute {
  2.     struct attribute    attr;
  3.     ssize_t (*show)(struct bus_type *, char * buf);
  4.     ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  5. };

Bus drivers can export attributes using the BUS_ATTR macro that works similarly to the DEVICE_ATTR macro for devices. For example, a definition like this:
总线驱动可以使用宏 BUS_ATTR 导出属性,它的工作方式就像 device 里的 DEVICE_ATTR 一样。例如,像这样定义:

  1. static BUS_ATTR(debug,0644,show_debug,store_debug);

is equivalent to declaring:
这等于是定义一个这样的东西:

  1. static bus_attribute bus_attr_debug;

This can then be used to add and remove the attribute from the bus's
sysfs directory using:
使用以下函数,可以用来从总线的 sysfs 目录中添加或删除属性:

  1. int bus_create_file(struct bus_type *, struct bus_attribute *);
  2. void bus_remove_file(struct bus_type *, struct bus_attribute *);

阅读(4228) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~