在最底层,在linux系统里每个设备由一个device结构体表示.device结构体包含了设备模型内核需要的信息
大部分subsystem,还会增加额外的信息.因此很少单独由一个device表示一个设备;而是像kobject一样嵌入到
一个更大的结构体来表示一个设备.
device结构体定义:
struct device {
struct device *parent;
struct device_private *p;
struct kobject kobj;
const char *init_name; /* initial name of the device */
const struct device_type *type;
struct mutex mutex; /* mutex to synchronize calls to
* its driver.
*/
struct bus_type *bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated this
device */
void *platform_data; /* Platform specific data, device
core doesn't touch it */
struct dev_pm_info power;
struct dev_pm_domain *pm_domain;
#ifdef CONFIG_NUMA
int numa_node; /* NUMA node this device is close to */
#endif
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;/* Like dma_mask, but for
alloc_coherent mappings as
not all hardware supports
64 bit addresses for consistent
allocations such descriptors. */
struct device_dma_parameters *dma_parms;
struct list_head dma_pools; /* dma pools (if dma'ble) */
struct dma_coherent_mem *dma_mem; /* internal for coherent mem
override */
/* arch specific additions */
struct dev_archdata archdata;
struct device_node *of_node; /* associated device tree node */
dev_t devt; /* dev_t, creates the sysfs "dev" */
spinlock_t devres_lock;
struct list_head devres_head;
struct klist_node knode_class;
struct class *class;
const struct attribute_group **groups; /* optional groups */
void (*release)(struct device *dev);
};
@parent: The device’s “parent” device, the device to which it is attached.
In most cases, a parent device is some sort of bus or host
controller. If parent is NULL, the device, is a top-level device,
which is not usually what you want.
该设备挂载的父设备,通常是某种总线或主控制器
@p: Holds the private data of the driver core portions of the device.
See the comment of the struct device_private for detail.
device结构体驱动内核部分的私有数据
@kobj: A top-level, abstract class from which other classes are derived.
这是一个顶层的抽象class,其它class从它那里派生
@init_name: Initial name of the device.
device的初始名
@type: The type of device.
This identifies the device type and carries type-specific
information.
device的类型信息,
结构体定义如下:
struct device_type {
const char *name;
const struct attribute_group **groups;
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
char *(*devnode)(struct device *dev, mode_t *mode);
void (*release)(struct device *dev);
const struct dev_pm_ops *pm;
};
class或bus可以包含不同类型的device,比如 “partitions” 和"disks", “mouse” 和 “event”.
该结构体定义了device类型和类型相关的信息.等同于kobject里的kobj_type.如果设置了
name成员,uevent将保存它到DEVTYPE变量,具体用法后面会再讲到
@mutex: Mutex to synchronize calls to its driver.
互斥锁用于同步调用它的驱动
@bus: Type of bus device is on.
该device所属的bus指针
@driver: Which driver has allocated this
指向分配给该device的驱动device_driver结构体
@platform_data: Platform data specific to the device.
关联到该device的平台数据,比如:
对于定制板上的device,典型的内嵌或SOC的硬件,linux通常用platform_data指向
板子相关的结构体,该结构体描述了devices以及它们的连接方法.可以包括可用端口,
不同芯片,以及gpio管脚的配置方式等等.这样就缩减了BSP和驱动里#ifdefs的用量
@power: For device power management.
See Documentation/power/devices.txt for details.
@pm_domain: Provide callbacks that are executed during system suspend,
hibernation, system resume and during runtime PM transitions
along with subsystem-level and driver-level callbacks.
@numa_node: NUMA node this device is close to.
@dma_mask: Dma mask (if dma’ble device).
@coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
hardware supports 64-bit addresses for consistent allocations
such descriptors.
@dma_parms: A low level driver may set these to teach IOMMU code about
segment limitations.
@dma_pools: Dma pools (if dma’ble device).
@dma_mem: Internal for coherent mem override.
@archdata: For arch-specific additions.
@of_node: Associated device tree node.
关联的设备树节点
@devt: For creating the sysfs “dev”.
用于创建sysfs文件系统的dev
@devres_lock: Spinlock to protect the resource of the device.
@devres_head: The resources list of the device.
@knode_class: The node used to add the device to the class list.
链表节点用于添加device到class链表
@class: The class of the device.
所属class
@groups: Optional attribute groups.
@release: Callback to free the device after all references have
gone away. This should be set by the allocator of the
device (i.e. the bus driver that discovered the device).
回调函数用于在所有对该设备的引用都消失后释放device.该函数应该在该device的配置器里设置
(比如发现该device的bus驱动里)