Table of Contents
1 Overview" >1 Overview
2. Data structure description" >2. Data structure description
2.1 struct class" >2.1 struct class
2.2 struct class_interface" >2.2 struct class_interface
3. 功能及内部逻辑解析" >3. 功能及内部逻辑解析
3.1 class的功能" >3.1 class的功能
3.2 class的注册" >3.2 class的注册
3.3 Class-related actions during device registration" >3.3 Class-related actions during device registration
4 Conclusion" >4 Conclusion
Home System Tutorial LINUX Detailed explanation of Linux device model (7)_Class

Detailed explanation of Linux device model (7)_Class

Feb 13, 2024 pm 10:39 PM
linux linux tutorial linux system linux command shell script overflow embeddedlinux Getting started with linux linux learning

1 Overview

In the device model, Bus, Device, Device driver, etc. are relatively easy to understand, because they all correspond to real things, and all logic is centered around these entities. However, the Class to be described in this article is somewhat different, because it is virtual, just to abstract the commonality of the device.

Detailed explanation of Linux device model (7)_Class

For example, if some people of similar age and need to acquire similar knowledge gather together to study, they form a class. This class can have its own name (such as "295"), but it has no meaning without the students (Devices) that make up it. In addition, what is the greatest significance of the existence of classes? Every course is taught by a teacher! Because the teacher only needs to speak once, all students in a class can hear it. If every student is studying at home, a teacher will be hired for each student to teach them this way. Most of the content is the same, which is a huge waste.

Class in the device model provides similar functions. For example, some similar Devices (students) need to provide similar interfaces (courses) to user space. If each device driver has to be implemented once, it will lead to a large amount of redundant code in the kernel, which is a huge waste. Therefore, Class said: "Let me help you implement it, as long as you know how to use it."

This is the function of Class in the device model. Combined with the kernel's comments: A class is a higher-level view of a device that abstracts out low-level implementation details (include/linux/device.h line326), it is easy to understand.

2. Data structure description

2.1 struct class

struct class is the abstraction of class, its definition is as follows:

   1: /* include/linux/device.h, line 332 */
   2: struct class {
   3:         const char              *name;
   4:         struct module           *owner;
   5:  
   6:         struct class_attribute          *class_attrs;
   7:         struct device_attribute         *dev_attrs;
   8:         struct bin_attribute            *dev_bin_attrs;
   9:         struct kobject                  *dev_kobj;
  10:  
  11:         int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  12:         char *(*devnode)(struct device *dev, umode_t *mode);
  13:  
  14:         void (*class_release)(struct class *class);
  15:         void (*dev_release)(struct device *dev);
  16:  
  17:         int (*suspend)(struct device *dev, pm_message_t state);
  18:         int (*resume)(struct device *dev);
  19:  
  20:         const struct kobj_ns_type_operations *ns_type;
  21:         const void *(*namespace)(struct device *dev);
  22:  
  23:         const struct dev_pm_ops *pm;
  24:  
  25:         struct subsys_private *p;
  26: };
Copy after login

In fact, struct class and struct bus are very similar, explained as follows:

name, the name of the class, will be reflected in the "/sys/class/" directory.

class_atrrs, the default attribute of the class, will automatically create the corresponding attribute file under "/sys/class/xxx_class" when the class is registered in the kernel.

dev_attrs, the attributes of each device under this class will automatically create the corresponding attribute file in the sysfs directory of the device when the device is registered to the kernel.

dev_bin_attrs, similar to dev_attrs, is just a binary type attribute.

dev_kobj, indicates the directory under /sys/dev/ of the device under this class. Currently, there are generally two types: char and block. If dev_kobj is NULL, char is selected by default.

dev_uevent, when a device under this class changes, the class's uevent callback function will be called.

class_release, the callback function used for release itself.

dev_release, used as a callback function for devices within the release class. In the device_release interface, the Device, Device Type and the class where the Device is located will be checked in order to see whether the release interface is registered. If so, the corresponding release interface will be called to release the device pointer.

p, is the same as the struct bus structure in "Linux Device Model (6)_Bus" and will not be explained again.

2.2 struct class_interface

struct class_interface is a structure that allows the class driver to call preset callback functions (add_dev and remove_dev) when a device is added or removed under the class. So what do you do with calling them? You can do whatever you want (such as changing the name of the device), and it is implemented by the specific class driver.

The structure is defined as follows:

   1: /* include/linux/device.h, line 434 */
   2: struct class_interface {
   3:         struct list_head        node;
   4:         struct class            *class;
   5:  
   6:         int (*add_dev)          (struct device *, struct class_interface *);
   7:         void (*remove_dev)      (struct device *, struct class_interface *);
   8: };
Copy after login

3. 功能及内部逻辑解析

3.1 class的功能

看完上面的东西,蜗蜗依旧糊里糊涂的,class到底提供了什么功能?怎么使用呢?让我们先看一下现有Linux系统中有关class的状况(这里以input class为例):

root@android:/ # ls /sys/class/input/ -l
lrwxrwxrwx root root 2014-04-23 03:39 event0 -> ../../devices/platform/i2c-gpio.17/i2c-17/17-0066/max77693-muic/input/input0/event0
lrwxrwxrwx root root 2014-04-23 03:39 event1 -> ../../devices/platform/gpio-keys.0/input/input1/event1
lrwxrwxrwx root root 2014-04-23 03:39 event10 -> ../../devices/virtual/input/input10/event10
lrwxrwxrwx root root 2014-04-23 03:39 event2 -> ../../devices/platform/s3c2440-i2c.3/i2c-3/3-0048/input/input2/event2

lrwxrwxrwx root root 2014-04-23 03:39 event8 -> ../../devices/platform/soc-audio/sound/card0/input8/event8
lrwxrwxrwx root root 2014-04-23 03:39 event9 -> ../../devices/platform/i2c-gpio.8/i2c-8/8-0020/input/input9/event9
lrwxrwxrwx root root 2014-04-23 03:39 input0 -> ../../devices/platform/i2c-gpio.17/i2c-17/17-0066/max77693-muic/input/input0

lrwxrwxrwx root root 2014-04-23 03:39 mice -> ../../devices/virtual/input/mice

root@android:/ # ls /sys/devices/platform/s3c2440-i2c.3/i2c-3/3-0048/input/input2/event2/ -l

-r–r–r– root root 4096 2014-04-23 04:08 dev
lrwxrwxrwx root root 2014-04-23 04:08 device -> ../../input2
drwxr-xr-x root root 2014-04-23 04:08 power
lrwxrwxrwx root root 2014-04-23 04:08 subsystem -> ../../../../../../../../class/input
-rw-r–r– root root 4096 2014-04-23 04:08 uevent

root@android:/ # ls /sys/devices/virtual/input/mice/ -l
-r–r–r– root root 4096 2014-04-23 03:57 dev
drwxr-xr-x root root 2014-04-23 03:57 power
lrwxrwxrwx root root 2014-04-23 03:57 subsystem -> ../../../../class/input
-rw-r–r– root root 4096 2014-04-23 03:57 uevent

看上面的例子,发现input class也没做什么实实在在的事儿,它(input class)的功能,仅仅是:

  • 在/sys/class/目录下,创建一个本class的目录(input)
  • 在本目录下,创建每一个属于该class的设备的符号链接(如,把“sys/devices/platform/s3c2440-i2c.3/i2c-3/3-0048/input/input2/event2”设备链接到”/sys/class/input/event2”),这样就可以在本class目录下,访问该设备的所有特性(即attribute)
  • 另外,device在sysfs的目录下,也会创建一个subsystem的符号链接,链接到本class的目录

算了,我们还是先分析一下Class的核心逻辑都做了哪些事情,至于class到底有什么用处,可以在后续具体的子系统里面(如input子系统),更为细致的探讨。

3.2 class的注册

class的注册,是由__class_register接口(它的实现位于”drivers/base/class.c, line 609″)实现的,它的处理逻辑和bus的注册类似,主要包括:

  • Allocate space for the struct subsys_private type pointer (cp) in the class structure, and initialize the fields in it, including cp->subsys.kobj.kset, cp->subsys.kobj.ktype, etc.
  • Call kset_register to register the class (recall the description in "Linux Device Model (6)_Bus", a class is a subsystem, so registering a class is also a registration subsystem). After the process is completed, a directory corresponding to the class (subsystem) will be created in the /sys/class/ directory
  • Call the add_class_attrs interface to add the attribute pointed to by the class_attrs pointer in the class structure to the kernel. After execution, you will see the files corresponding to these attributes in the /sys/class/xxx_class/ directory

In "Linux Device Model (5)_device and device driver", we have talked about the two data structures struct device and struct device_driver. The struct device structure will contain a struct class pointer (this explains the class from the side) It is a collection of devices, and even class can be the driver of the device). When a class driver registers a class with the kernel, it needs to use the device of the class by pointing its own class pointer to the class. The kernel handles the rest when registering the device.

In this section, we will talk about the class-related actions when registering the device:

Device registration is ultimately implemented by the device_add interface (drivers/base/core.c). The class-related actions in this interface include:

  • Call the device_add_class_symlinks interface to create various symbolic links described in Section 3.1, that is: in the directory of the corresponding class, create a symbolic link pointing to the device; in the directory of the device, create a symbolic link named subsystem pointing to the corresponding class directory
  • Call device_add_attrs to add attributes specified by class (class->dev_attrs)
  • If there is an add_dev callback function corresponding to the class, call the callback function

4 Conclusion

In fact, after the end of this article, Wowo still has not figured out how classes are used in the kernel. But it doesn’t matter. In the subsequent analysis of subsystems (such as input subsystem, RTC subsystem, etc.), we will see many use cases of classes. When the time comes, it will be very clear when we look back and summarize.

The above is the detailed content of Detailed explanation of Linux device model (7)_Class. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Android TV Box gets unofficial Ubuntu 24.04 upgrade Android TV Box gets unofficial Ubuntu 24.04 upgrade Sep 05, 2024 am 06:33 AM

For many users, hacking an Android TV box sounds daunting. However, developer Murray R. Van Luyn faced the challenge of looking for suitable alternatives to the Raspberry Pi during the Broadcom chip shortage. His collaborative efforts with the Armbia

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BitPie Bitpie wallet app download address BitPie Bitpie wallet app download address Sep 10, 2024 pm 12:10 PM

How to download BitPie Bitpie Wallet App? The steps are as follows: Search for "BitPie Bitpie Wallet" in the AppStore (Apple devices) or Google Play Store (Android devices). Click the "Get" or "Install" button to download the app. For the computer version, visit the official BitPie wallet website and download the corresponding software package.

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Detailed explanation: Shell script variable judgment parameter command Detailed explanation: Shell script variable judgment parameter command Sep 02, 2024 pm 03:25 PM

The system variable $n is the parameter passed to the script or function. n is a number indicating the number of parameters. For example, the first parameter is $1, and the second parameter is $2$? The exit status of the previous command, or the return value of the function. Returns 0 on success, 1 on failure $#Number of parameters passed to the script or function $* All these parameters are enclosed in double quotes. If a script receives two parameters, $* is equal to $1$2$0The name of the command being executed. For shell scripts, this is the path to the activated command. When $@ is enclosed in double quotes (""), it is slightly different from $*. If a script receives two parameters, $@ is equivalent to $1$2$$the process number of the current shell. For a shell script, this is the process I when it is executing

Zabbix 3.4 Source code compilation installation Zabbix 3.4 Source code compilation installation Sep 04, 2024 am 07:32 AM

1. Installation environment (Hyper-V virtual machine): $hostnamectlStatichostname:localhost.localdomainIconname:computer-vmChassis:vmMachineID:renwoles1d8743989a40cb81db696400BootID:renwoles272f4aa59935dcdd0d456501Virtualization:microsoftOperatingSystem:CentOS Linux7(Core)CPEOSName:cpe:

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

See all articles