Basic Structures
Linux Device Model provides a number of structures to ensure the interaction between a hardware device and a device driver. The whole model is based on kobject structure. Hierarchies are built using this structure and the following structures are implemented:
struct bus_type
struct device
struct device_driver
The kobject structure
A kobject structure does not perform a single function. This structure is usually integrated into a larger one. A kobject structure actually incorporates a set of features that will be offered to a higher abstraction object in the Linux Device Model hierarchy.
For example, the cdev structure has the following definition:
struct cdev {
struct kobject kob;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
Note that this structure includes a kobject structure field.
A kobject structure is defined as follows:
struct kobject {
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct sysfs_dirent *sd;
struct kref kref;
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
As we can see, the kobject structures are in a hierarchy: an object has a parent and holds a kset member, which contains objects on the same level.
Working with the structure involves initializing it with the kobject_init() function. Also in the initialization process it is necessary to set the name of the kobject structure, which will appear in sysfs, using the kobject_set_name() function.
Any operation on a kobject is done by incrementing its internal counter using kobject_get(), or decrementing if it is no longer used using kobject_put(). Thus, a kobject object will only be released when its internal counter reaches 0. A method of notifying this is needed so that the resources associated with the device structure which included the kobject structure are released (for example, cdev). The method is called release and is associated with the object via the ktype field (struct kobj_type).
The kobject structure is the basic structure of the Linux Device Model. The structures in the higher levels of the model are struct bus_type, struct device and struct device_driver.
下回介绍Buses、device、driver、classes