1. 程式人生 > >linux核心之class介紹(二)

linux核心之class介紹(二)

在最底層,在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驅動裡)