Linux device models

Source: Internet
Author: User

An important feature of the Linux 2.6 kernel is the provision of a unified kernel device model. With the continuous advancement of technology, the topology structure of the system becomes more and more complex, and the requirements for intelligent power management, hot swapping, and support for plug and play are also increasing. The 2.4 kernel is hard to meet these requirements. To meet the needs of this situation, the 2.6 kernel has developed a new device model.
1. sysfs File System
Sysfs is a special file system similar to the proc file system. It organizes devices in the system into hierarchies and provides detailed kernel data structure information to user mode programs. Its top-level directories mainly include:
Block Directory: contains all Block devices
Devices Directory: contains all the devices in the system, and is organized into hierarchies according to the bus type attached to the device.
Bus Directory: contains all bus types in the system
DRIVERS directory: includes all registered device drivers in the kernel
Class Directory: the types of devices in the system (such as NIC devices and sound card devices)
2. Key data structures of the kernel object mechanism
2.1 kobject Kernel Object
Kobject is a new device management mechanism introduced in Linux 2.6. It is represented by struct kobject in the kernel. Through this data structure, all devices have a unified interface at the underlying layer. kobject provides basic object management, which is the core structure of the Linux 2.6 device model. It is closely associated with the sysfs file system, each kobject object registered in the kernel corresponds to a directory in the sysfs file system.
The kobject structure is defined:
Struct kobject {
Char * k_name; pointer to device name
Char name [kobj_name_len]; device name
Struct kref; object reference count
Struct list_head entry; it is attached to the Unit in the kset
Struct kobject * parent; pointer to parent object
Struct kset * kset; pointer to the kset
Struct kobj_type * ktype; pointer to its object type descriptor
Struct dentry * dentry; file Node path pointer corresponding to this object in sysfs File System
};
The kref field indicates the reference count of the object. The kernel manages the reference count of the object through kref. The Kernel provides two functions, kobject_get () and kobject_put (), to increase and decrease the reference count respectively, when the reference count is 0, all resources used by this object will be released.
The ktype field is a pointer to the kobj_type structure, indicating the type of the object. The kobj_type data structure contains three fields: A release method is used to release resources occupied by kobject; A sysfs_ops Pointer Points to the sysfs operation table and a default attribute list of sysfs file systems. The sysfs operation table includes two functions: store () and show (). When the user State reads the attribute, the show () function is called. The Function Code specifies that the attribute value is stored in the buffer and returned to the user State. The store () function is used to store the attribute value passed in the user State.
2.2 kset kernel object set
Kobject is usually organized into a hierarchical structure through kset. kset is a set of kobject of the same type. It is expressed in the kernel using the kset data structure and is defined:
Struct kset {
Struct subsystem * subsys; pointer to the subsystem
Struct kobj_type * ktype; pointer to the kset object type descriptor
Struct list_head list; used to connect the linked list headers of all kobject in the kset
Struct kobject kobj; embedded kobject
Struct kset_hotplug_ops * hotplug_ops; pointer to hot swapping operation table
};
All the kobject contained in the kset is organized into a bidirectional cyclic linked list. The list field is the header of the linked list. The ktype field points to a kobj_type structure, which is shared by all the kobject in the kset, indicating the type of these objects. The kset data structure is also embedded with a kobject object (represented by the kobj domain). The parent fields of all the kobject objects belonging to this kset point to this embedded object. In addition, kset depends on kobj to maintain reference count: kset reference count is actually the reference count of the embedded kobject object.
2.3 subsystem Kernel Object Subsystem
The subsystem is a collection of ksets that describe a kind of device subsystems in the system. For example, block_subsys indicates all Block devices and corresponds to the block directory in the sysfs file system. Similarly, devices_subsys corresponds to the devices directory in sysfs and describes all devices in the system. The subsystem is described by the struct subsystem data structure and is defined:
Struct subsystem {
Struct kset; embedded kset object
Struct rw_semaphore rwsem; mutex access semaphore
};
Each kset must belong to a subsystem. You can add a kset to the subsystem by setting the subsys domain in the kset structure to point to the specified subsystem. All ksets attached to the same subsystem share the same rwsem semaphore, which is used to synchronously access the linked list in the kset.

 

3. Main functions related to the kernel object mechanism
For different data structures of kernel objects, the Linux 2.6 kernel defines a series of operation functions, which are defined in the LIB/kobject. c file.
3.1 kobject related functions
Void kobject_init (struct kobject * kobj );
Kobject initialization function. Set the reference count of kobject to 1, the entry field to itself, and the kset reference count to which it belongs plus 1.
Int kobject_set_name (struct kobject * kobj, const char * format ,...);
Set the name of the specified kobject.
Void kobject_cleanup (struct kobject * kobj) and void kobject_release (struct kref * kref );
The kobject clearing function. When its reference count is 0, the resources occupied by the released object are released.
Struct kobject * kobject_get (struct kobject * kobj );
Add the reference count of the kobj object to 1 and return the pointer to the object.
Void kobject_put (struct kobject * kobj );
Reduce the reference count of the kobj object by 1. If the reference count is reduced to 0, call kobject_release () to release the kobject object.
Int kobject_add (struct kobject * kobj );
Add the kobj object to the Linux device level. Mount the kobject object to the list chain of the kset, increase the reference count of kobject at all levels in the parent directory, create a file node under the directory to which the parent points, and start the hotplug function of the kernel object of this type.
Int kobject_register (struct kobject * kobj );
Kobject registers a function. Call kobject_init () to initialize kobj, and then call kobject_add () to register the kernel object.
Void kobject_del (struct kobject * kobj );
Delete the kobj object from the hierarchy of a Linux device.
Void kobject_unregister (struct kobject * kobj );
The kobject deregister function. Unlike kobject_register (), it first calls kobject_del to delete the object from the device level, and then calls kobject_put () to reduce the reference count of the object. If the reference count is reduced to 0, then the kobject object is released.
3.2 kset related functions
Similar to kobject, kset_init () initializes the specified kset, and kset_get () and kset_put () increase and decrease the reference count of the kset object respectively. The kset_add () and kset_del () functions add the specified keset object to the device level and delete it from the device level respectively. The kset_register () function completes kset registration and kset_unregister () the function is used to cancel the kset operation.
3.3 subsystem Functions
Subsystem has a set of functions similar to the following:
Void subsystem_init (struct subsystem * subsys );
Int subsystem_register (struct subsystem * subsys );
Void subsystem_unregister (struct subsystem * subsys );
Struct subsystem * subsys_get (struct subsystem * subsys)
Void subsys_put (struct subsystem * subsys );
4. Device Model Components
Based on the above kernel object mechanism, the Linux device model is built on several key components. The following describes these components in detail.
4.1 Devices
Any device in the system is described by a device object in the device model. The corresponding data structure struct device is defined:
Struct device {
Struct list_head g_list;
Struct list_head node;
Struct list_head bus_list;
Struct list_head driver_list;
Struct list_head children;
Struct device * parent;
Struct kobject kobj;
Char bus_id [bus_id_size];
Struct bus_type * bus;
Struct device_driver * driver;
Void * driver_data;
/* Several fields omitted */
};
G_list mounts the device object to the global device linked list. All Device objects are contained in devices_subsys and organized into hierarchies. The node domain mounts the object to the linked list of its brother object, while the bus_list is used to organize the devices connected to the same bus into a linked list, driver_list organizes all devices managed by the same driver into a chain table. In addition, the children field points to the sub-object linked list header of the device object, and the parent field points to the parent object. The device object is also embedded with a kobject object, which is used to reference counting management and implement the device hierarchy. The driver domain points to the driver object that manages the device, while driver_data is the data provided to the driver. The bus field describes the type of the bus to which the slave node is connected.
The kernel provides functions to operate Device objects. The device_register () function inserts a new device object into the device model and automatically creates a corresponding directory under/sys/devices. Device_unregister () to cancel the device object. Get_device () and put_device () increase and decrease the reference count of the device object respectively. Generally, the device structure is not used independently, but included in a larger structure as a sub-structure. For example, the struct pci_dev describing the PCI device, where the dev field is a device object.
4.2 drivers
Each driver in the system is described by a device_driver object. The corresponding data structure is defined:
Struct device_driver {
Char * Name; Name of the device driver
Struct bus_type * bus; type of the bus to which the driver is attached
Struct kobject kobj; embedded kobject object
Struct list_head devices; device linked list header managed by the driver
INT (* probe) (struct device * Dev); points to the device detection function to detect whether the device can be managed by the driver.
INT (* remove) (struct device * Dev); function used to delete a device
/* Some fields omitted */
};
Similar to the device structure, the device_driver object relies on the embedded kobject object to implement reference counting management and hierarchical organization. The kernel provides similar functions to operate the device_driver object, such as get_driver () to increase the reference count, and driver_register () to insert a new driver pair to the device model, create a directory in the sysfs file system. The device_driver () structure also includes several functions for hot plugging, plug-and-play, and power management events.
4.3 buses
In the system, the bus is described by struct bus_type and defined:
Struct bus_type {
Char * Name; Name of the Bus Type
Struct subsystem subsys; the subsystem associated with the bus
Struct kset drivers; a collection of all drivers related to the bus
Struct kset devices; a collection of all devices attached to the bus
Struct bus_attribute * bus_attrs; bus attribute
Struct device_attribute * dev_attrs; device attribute
Struct driver_attribute * drv_attrs; driver attributes
INT (* match) (struct device * Dev, struct device_driver * DRV );
INT (* hotplug) (struct device * Dev, char ** envp, int num_envp, char * buffer, int buffer_size );
INT (* suspend) (struct device * Dev, u32 State );
INT (* resume) (struct device * Dev );
};
Each bus_type object is embedded with a subsystem object, which is a subsystem object of all bus types in the bus_subsys object management system. Each bus_type object corresponds to a sub-directory under the/sys/bus directory. For example, the PCI bus type corresponds to/sys/bus/PCI. There are two sub-directories in each of these directories: devices and drivers (corresponding to the devices and drivers fields in the bus_type structure respectively ). The devices subdirectory describes all devices connected to the bus, and the DRIVERS directory describes all drivers associated with the bus. Similar to the device_driver object, the bus_type structure also contains several functions (such as match () and hotplug () to handle hot swapping, Plug and Play, and power management events.
4.4 classes
The device class in the system is described by struct class, indicating a certain type of device. All class objects belong to the class_subsys subsystem and correspond to the/sys/class directory in the sysfs file system. Each class object includes a class_device linked list. Each class_device object represents a logical device and is associated with a physical device through the dev field (a pointer to the struct device) in the struct class_device. In this way, a logical device always corresponds to one physical device, but a physical device may correspond to multiple logical devices. In addition, the class structure includes functions used to handle hot swapping, Plug and Play, and power management events, similar to the device object and driver object.

 

 

Device Model

The device model has three main data structures: kobject, kset, and subsystem.

 

Related Data Structure:

Struct kobject {
 Char  * K_name;
 Char  Name [kobj_name_len];
 Struct kref Kref;
 Struct list_headEntry;
 Struct kobject * Parent;
 Struct kset * Kset;
 Struct kobj_type* Ktype;
 Struct dentry * Dentry;
};

Struct kset {
 Struct Subsystem* Subsys;
 Struct kobj_type* Ktype;
 Struct list_headList;
 Spinlock_t List_lock;
 Struct kobject Kobj;
 Struct kset_hotplug_ops* Hotplug_ops;
};
 
Struct subsystem {
 Struct kset Kset;
 Struct rw_semaphoreRwsem;
};

They are the skeleton of the entire device model (in an object-oriented tone: This is called a base class (not a chicken ribs ^ _')).

All other structures, such as cdev and bus, are built on these frameworks.

Kobject is the most basic. Many structures have an embedded kobject.
The reference counting function of kobject is the most important.

Kset can be considered as a set of kojbect, while subsystem can be considered as a set of several ksets.

 

Multiple ksets can point to a subsystem through the subsys pointer. However, the subsystem only contains an embedded kset.
This unidirectional relationship means that it is impossible to find all the ksets of the subsystem through a subsystem struct.

Their contact information is as follows:
                                                        
                                                        
 /----------------------------->   Kset   <----------------------------/
 |                            + ------------ +                           |
 |                            | Subsys |                           |
 |      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.