← Documents Documentation/hwmon/hwmon-kernel-api.rst GitHub 원문 ↗

Linux 6.18.37 · Hardware Monitoring

The Linux Hardware Monitoring kernel API

hwmon 장치 등록, 센서 채널 설명, 속성 콜백, 추가 sysfs 속성을 구현하는 커널 API 계약입니다.

Source pathDocumentation/hwmon/hwmon-kernel-api.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

hwmon-kernel-api.rst:1-381

이 문서는 hwmon 드라이버가 코어에 장치를 등록하고 센서 형식·지원 속성·콜백을 선언하는 전체 계약을 설명합니다. 새 드라이버는 장치 관리형 등록과 `HWMON_CHANNEL_INFO()`를 우선 사용하고, 표준 속성은 코어에 맡기며 비표준 속성만 직접 선언하는 흐름이 핵심입니다.

문서 개요
항목
SourceDocumentation/hwmon/hwmon-kernel-api.rst
분량381 source lines
등록[devm_]hwmon_device_register_with_info()
채널hwmon_channel_info / HWMON_CHANNEL_INFO()
콜백is_visible / read / write
추가 속성SENSOR_DEVICE_ATTR 계열

원문 분량과 구현 시 확인할 핵심 인터페이스입니다.

hwmon 드라이버 구현 흐름
센서 형식과 지원 비트 정의is_visible·read·write 구현hwmon_chip_info 구성devm 등록 함수 호출코어가 표준 sysfs 속성 제공

칩 설명에서 사용자에게 보이는 속성까지의 경로입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 The Linux Hardware Monitoring kernel API
2 ========================================
3
4 Guenter Roeck
5
6 Introduction
7 ------------
8
9 This document describes the API that can be used by hardware monitoring
10 drivers that want to use the hardware monitoring framework.
11
12 This document does not describe what a hardware monitoring (hwmon) Driver or
13 Device is. It also does not describe the API which can be used by user space
14 to communicate with a hardware monitoring device. If you want to know this
15 then please read the following file: Documentation/hwmon/sysfs-interface.rst.
16
17 For additional guidelines on how to write and improve hwmon drivers, please
18 also read Documentation/hwmon/submitting-patches.rst.
19
20 The API
21 -------
22 Each hardware monitoring driver must #include <linux/hwmon.h> and, in some
23 cases, <linux/hwmon-sysfs.h>. linux/hwmon.h declares the following
24 register/unregister functions::
25
26 struct device *
27 hwmon_device_register_with_info(struct device *dev,
28 const char *name, void *drvdata,
29 const struct hwmon_chip_info *info,
30 const struct attribute_group **extra_groups);
31
32 struct device *
33 devm_hwmon_device_register_with_info(struct device *dev,
34 const char *name,
35 void *drvdata,
36 const struct hwmon_chip_info *info,
37 const struct attribute_group **extra_groups);
38
39 void hwmon_device_unregister(struct device *dev);
40
41 char *hwmon_sanitize_name(const char *name);
42
43 char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
44
45 void hwmon_lock(struct device *dev);
46 void hwmon_unlock(struct device *dev);
47
48 hwmon_device_register_with_info registers a hardware monitoring device.
49 It creates the standard sysfs attributes in the hardware monitoring core,
50 letting the driver focus on reading from and writing to the chip instead
51 of having to bother with sysfs attributes. The parent device parameter
52 as well as the chip parameter must not be NULL. Its parameters are described
53 in more detail below.
54
55 devm_hwmon_device_register_with_info is similar to
56 hwmon_device_register_with_info. However, it is device managed, meaning the
57 hwmon device does not have to be removed explicitly by the removal function.
58
59 All other hardware monitoring device registration functions are deprecated
60 and must not be used in new drivers.
61
62 hwmon_device_unregister deregisters a registered hardware monitoring device.
63 The parameter of this function is the pointer to the registered hardware
64 monitoring device structure. This function must be called from the driver
65 remove function if the hardware monitoring device was registered with
66 hwmon_device_register_with_info.
67
68 All supported hwmon device registration functions only accept valid device
69 names. Device names including invalid characters (whitespace, '*', or '-')
70 will be rejected. If NULL is passed as name parameter, the hardware monitoring
71 device name will be derived from the parent device name.
72
73 If the driver doesn't use a static device name (for example it uses
74 dev_name()), and therefore cannot make sure the name only contains valid
75 characters, hwmon_sanitize_name can be used. This convenience function
76 will duplicate the string and replace any invalid characters with an
77 underscore. It will allocate memory for the new string and it is the
78 responsibility of the caller to release the memory when the device is
79 removed.
80
81 devm_hwmon_sanitize_name is the resource managed version of
82 hwmon_sanitize_name; the memory will be freed automatically on device
83 removal.
84
85 When using ``[devm_]hwmon_device_register_with_info()`` to register the
86 hardware monitoring device, accesses using the associated access functions
87 are serialised by the hardware monitoring core. If a driver needs locking
88 for other functions such as interrupt handlers or for attributes which are
89 fully implemented in the driver, hwmon_lock() and hwmon_unlock() can be used
90 to ensure that calls to those functions are serialized.
91
92 Using devm_hwmon_device_register_with_info()
93 --------------------------------------------
94
95 hwmon_device_register_with_info() registers a hardware monitoring device.
96 The parameters to this function are
97
98 =============================================== ===============================================
99 `struct device *dev` Pointer to parent device
100 `const char *name` Device name
101 `void *drvdata` Driver private data
102 `const struct hwmon_chip_info *info` Pointer to chip description.
103 `const struct attribute_group **extra_groups` Null-terminated list of additional non-standard
104 sysfs attribute groups.
105 =============================================== ===============================================
106
107 This function returns a pointer to the created hardware monitoring device
108 on success and a negative error code for failure.
109
110 The hwmon_chip_info structure looks as follows::
111
112 struct hwmon_chip_info {
113 const struct hwmon_ops *ops;
114 const struct hwmon_channel_info * const *info;
115 };
116
117 It contains the following fields:
118
119 * ops:
120 Pointer to device operations.
121 * info:
122 NULL-terminated list of device channel descriptors.
123
124 The list of hwmon operations is defined as::
125
126 struct hwmon_ops {
127 umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
128 u32 attr, int);
129 int (*read)(struct device *, enum hwmon_sensor_types type,
130 u32 attr, int, long *);
131 int (*write)(struct device *, enum hwmon_sensor_types type,
132 u32 attr, int, long);
133 };
134
135 It defines the following operations.
136
137 * is_visible:
138 Pointer to a function to return the file mode for each supported
139 attribute. This function is mandatory.
140
141 * read:
142 Pointer to a function for reading a value from the chip. This function
143 is optional, but must be provided if any readable attributes exist.
144
145 * write:
146 Pointer to a function for writing a value to the chip. This function is
147 optional, but must be provided if any writeable attributes exist.
148
149 Each sensor channel is described with struct hwmon_channel_info, which is
150 defined as follows::
151
152 struct hwmon_channel_info {
153 enum hwmon_sensor_types type;
154 u32 *config;
155 };
156
157 It contains following fields:
158
159 * type:
160 The hardware monitoring sensor type.
161
162 Supported sensor types are
163
164 ================== ==================================================
165 hwmon_chip A virtual sensor type, used to describe attributes
166 which are not bound to a specific input or output
167 hwmon_temp Temperature sensor
168 hwmon_in Voltage sensor
169 hwmon_curr Current sensor
170 hwmon_power Power sensor
171 hwmon_energy Energy sensor
172 hwmon_energy64 Energy sensor, reported as 64-bit signed value
173 hwmon_humidity Humidity sensor
174 hwmon_fan Fan speed sensor
175 hwmon_pwm PWM control
176 ================== ==================================================
177
178 * config:
179 Pointer to a 0-terminated list of configuration values for each
180 sensor of the given type. Each value is a combination of bit values
181 describing the attributes supposed by a single sensor.
182
183 As an example, here is the complete description file for a LM75 compatible
184 sensor chip. The chip has a single temperature sensor. The driver wants to
185 register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
186 the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
187 reading the temperature (HWMON_T_INPUT), it has a maximum temperature
188 register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
189 (HWMON_T_MAX_HYST)::
190
191 static const u32 lm75_chip_config[] = {
192 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
193 0
194 };
195
196 static const struct hwmon_channel_info lm75_chip = {
197 .type = hwmon_chip,
198 .config = lm75_chip_config,
199 };
200
201 static const u32 lm75_temp_config[] = {
202 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
203 0
204 };
205
206 static const struct hwmon_channel_info lm75_temp = {
207 .type = hwmon_temp,
208 .config = lm75_temp_config,
209 };
210
211 static const struct hwmon_channel_info * const lm75_info[] = {
212 &lm75_chip,
213 &lm75_temp,
214 NULL
215 };
216
217 The HWMON_CHANNEL_INFO() macro can and should be used when possible.
218 With this macro, the above example can be simplified to
219
220 static const struct hwmon_channel_info * const lm75_info[] = {
221 HWMON_CHANNEL_INFO(chip,
222 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
223 HWMON_CHANNEL_INFO(temp,
224 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
225 NULL
226 };
227
228 The remaining declarations are as follows.
229
230 static const struct hwmon_ops lm75_hwmon_ops = {
231 .is_visible = lm75_is_visible,
232 .read = lm75_read,
233 .write = lm75_write,
234 };
235
236 static const struct hwmon_chip_info lm75_chip_info = {
237 .ops = &lm75_hwmon_ops,
238 .info = lm75_info,
239 };
240
241 A complete list of bit values indicating individual attribute support
242 is defined in include/linux/hwmon.h. Definition prefixes are as follows.
243
244 =============== =================================================
245 HWMON_C_xxxx Chip attributes, for use with hwmon_chip.
246 HWMON_T_xxxx Temperature attributes, for use with hwmon_temp.
247 HWMON_I_xxxx Voltage attributes, for use with hwmon_in.
248 HWMON_C_xxxx Current attributes, for use with hwmon_curr.
249 Notice the prefix overlap with chip attributes.
250 HWMON_P_xxxx Power attributes, for use with hwmon_power.
251 HWMON_E_xxxx Energy attributes, for use with hwmon_energy.
252 HWMON_H_xxxx Humidity attributes, for use with hwmon_humidity.
253 HWMON_F_xxxx Fan speed attributes, for use with hwmon_fan.
254 HWMON_PWM_xxxx PWM control attributes, for use with hwmon_pwm.
255 =============== =================================================
256
257 Driver callback functions
258 -------------------------
259
260 Each driver provides is_visible, read, and write functions. Parameters
261 and return values for those functions are as follows::
262
263 umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
264 u32 attr, int channel)
265
266 Parameters:
267 data:
268 Pointer to device private data structure.
269 type:
270 The sensor type.
271 attr:
272 Attribute identifier associated with a specific attribute.
273 For example, the attribute value for HWMON_T_INPUT would be
274 hwmon_temp_input. For complete mappings of bit fields to
275 attribute values please see include/linux/hwmon.h.
276 channel:
277 The sensor channel number.
278
279 Return value:
280 The file mode for this attribute. Typically, this will be 0 (the
281 attribute will not be created), 0444, or 0644.
282
283 ::
284
285 int read_func(struct device *dev, enum hwmon_sensor_types type,
286 u32 attr, int channel, long *val)
287
288 Parameters:
289 dev:
290 Pointer to the hardware monitoring device.
291 type:
292 The sensor type.
293 attr:
294 Attribute identifier associated with a specific attribute.
295 For example, the attribute value for HWMON_T_INPUT would be
296 hwmon_temp_input. For complete mappings please see
297 include/linux/hwmon.h.
298 channel:
299 The sensor channel number.
300 val:
301 Pointer to attribute value.
302 For hwmon_energy64, `'val`' is passed as `long *` but needs
303 a typecast to `s64 *`.
304
305 Return value:
306 0 on success, a negative error number otherwise.
307
308 ::
309
310 int write_func(struct device *dev, enum hwmon_sensor_types type,
311 u32 attr, int channel, long val)
312
313 Parameters:
314 dev:
315 Pointer to the hardware monitoring device.
316 type:
317 The sensor type.
318 attr:
319 Attribute identifier associated with a specific attribute.
320 For example, the attribute value for HWMON_T_INPUT would be
321 hwmon_temp_input. For complete mappings please see
322 include/linux/hwmon.h.
323 channel:
324 The sensor channel number.
325 val:
326 The value to write to the chip.
327
328 Return value:
329 0 on success, a negative error number otherwise.
330
331
332 Driver-provided sysfs attributes
333 --------------------------------
334
335 In most situations it should not be necessary for a driver to provide sysfs
336 attributes since the hardware monitoring core creates those internally.
337 Only additional non-standard sysfs attributes need to be provided.
338
339 The header file linux/hwmon-sysfs.h provides a number of useful macros to
340 declare and use hardware monitoring sysfs attributes.
341
342 In many cases, you can use the existing define DEVICE_ATTR or its variants
343 DEVICE_ATTR_{RW,RO,WO} to declare such attributes. This is feasible if an
344 attribute has no additional context. However, in many cases there will be
345 additional information such as a sensor index which will need to be passed
346 to the sysfs attribute handling function.
347
348 SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
349 which need such additional context information. SENSOR_DEVICE_ATTR requires
350 one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
351
352 Simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 are available
353 and should be used if standard attribute permissions and function names are
354 feasible. Standard permissions are 0644 for SENSOR_DEVICE_ATTR[_2]_RW,
355 0444 for SENSOR_DEVICE_ATTR[_2]_RO, and 0200 for SENSOR_DEVICE_ATTR[_2]_WO.
356 Standard functions, similar to DEVICE_ATTR_{RW,RO,WO}, have _show and _store
357 appended to the provided function name.
358
359 SENSOR_DEVICE_ATTR and its variants define a struct sensor_device_attribute
360 variable. This structure has the following fields::
361
362 struct sensor_device_attribute {
363 struct device_attribute dev_attr;
364 int index;
365 };
366
367 You can use to_sensor_dev_attr to get the pointer to this structure from the
368 attribute read or write function. Its parameter is the device to which the
369 attribute is attached.
370
371 SENSOR_DEVICE_ATTR_2 and its variants define a struct sensor_device_attribute_2
372 variable, which is defined as follows::
373
374 struct sensor_device_attribute_2 {
375 struct device_attribute dev_attr;
376 u8 index;
377 u8 nr;
378 };
379
380 Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
381 is the device to which the attribute is attached.
382

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Linux hwmon 커널 API 개요와 선언

1-47

Guenter Roeck가 작성한 이 문서는 하드웨어 모니터링 프레임워크를 사용하는 hwmon 드라이버용 커널 API를 설명합니다. hwmon 드라이버나 장치의 개념, 사용자 공간이 장치와 통신하는 인터페이스는 이 문서의 범위가 아닙니다. 그 내용은 `Documentation/hwmon/sysfs-interface.rst`를, 드라이버 작성과 개선 지침은 `Documentation/hwmon/submitting-patches.rst`를 읽어야 합니다.

모든 하드웨어 모니터링 드라이버는 `<linux/hwmon.h>`를 포함해야 하며, 경우에 따라 `<linux/hwmon-sysfs.h>`도 포함합니다. `linux/hwmon.h`는 `hwmon_device_register_with_info()`, 장치 관리형인 `devm_hwmon_device_register_with_info()`, `hwmon_device_unregister()`를 선언합니다.

같은 헤더는 이름을 정규화하는 `hwmon_sanitize_name()`과 `devm_hwmon_sanitize_name()`, hwmon 코어의 직렬화 잠금을 공유하는 `hwmon_lock()`과 `hwmon_unlock()`도 선언합니다. 각 함수의 원형, 인수 타입, `const` 한정자, 반환 타입은 아래 원문 코드 블록과 동일하게 보존됩니다.

핵심 hwmon API
함수역할수명 관리
hwmon_device_register_with_info()hwmon 장치와 표준 속성 등록드라이버가 해제
devm_hwmon_device_register_with_info()장치 관리형 hwmon 등록코어가 자동 해제
hwmon_device_unregister()수동 등록 장치 해제명시적 호출
hwmon_sanitize_name()유효하지 않은 이름 문자를 `_`로 치환호출자가 메모리 해제
devm_hwmon_sanitize_name()장치 관리형 이름 정규화장치 제거 시 자동 해제
hwmon_lock() / hwmon_unlock()hwmon 코어 직렬화 잠금 공유호출 범위에서 사용

등록, 이름 처리, 잠금 기능을 역할별로 정리했습니다.

드라이버 진입점
linux/hwmon.h 포함칩 정보와 연산 정의hwmon 장치 등록표준 sysfs 속성 생성필요 시 잠금과 해제 수행

드라이버가 hwmon 프레임워크에 연결되는 기본 순서입니다.

The Linux Hardware Monitoring kernel API
========================================

Guenter Roeck

Introduction
------------

This document describes the API that can be used by hardware monitoring
drivers that want to use the hardware monitoring framework.

This document does not describe what a hardware monitoring (hwmon) Driver or
Device is. It also does not describe the API which can be used by user space
to communicate with a hardware monitoring device. If you want to know this
then please read the following file: Documentation/hwmon/sysfs-interface.rst.

For additional guidelines on how to write and improve hwmon drivers, please
also read Documentation/hwmon/submitting-patches.rst.

The API
-------
Each hardware monitoring driver must #include <linux/hwmon.h> and, in some
cases, <linux/hwmon-sysfs.h>. linux/hwmon.h declares the following
register/unregister functions::

  struct device *
  hwmon_device_register_with_info(struct device *dev,
                                  const char *name, void *drvdata,
                                  const struct hwmon_chip_info *info,
                                  const struct attribute_group **extra_groups);

  struct device *
  devm_hwmon_device_register_with_info(struct device *dev,
                                       const char *name,
                                       void *drvdata,
                                       const struct hwmon_chip_info *info,
                                       const struct attribute_group **extra_groups);

  void hwmon_device_unregister(struct device *dev);

  char *hwmon_sanitize_name(const char *name);

  char *devm_hwmon_sanitize_name(struct device *dev, const char *name);

  void hwmon_lock(struct device *dev);
  void hwmon_unlock(struct device *dev);

장치 등록, 이름, 잠금의 수명 주기

48-90

`hwmon_device_register_with_info()`는 하드웨어 모니터링 장치를 등록하고 hwmon 코어 안에 표준 sysfs 속성을 만듭니다. 덕분에 드라이버는 sysfs 속성 구현보다 칩에서 값을 읽고 쓰는 일에 집중할 수 있습니다. 부모 장치 인수와 칩 정보 인수는 `NULL`이면 안 됩니다.

`devm_hwmon_device_register_with_info()`는 같은 등록 작업을 장치 관리형으로 수행하므로 드라이버의 제거 함수가 hwmon 장치를 직접 제거할 필요가 없습니다. 그 밖의 hwmon 장치 등록 함수는 모두 폐기 예정이므로 새 드라이버에서 사용하면 안 됩니다.

수동 등록 함수를 사용했다면 드라이버 제거 함수에서 `hwmon_device_unregister()`를 호출해야 합니다. 이 함수에는 등록 과정에서 반환된 하드웨어 모니터링 장치 구조체 포인터를 넘깁니다.

지원되는 등록 함수는 유효한 장치 이름만 허용합니다. 공백, `*`, `-`가 들어간 이름은 거부됩니다. 이름 인수로 `NULL`을 전달하면 부모 장치 이름에서 hwmon 장치 이름을 만듭니다. `dev_name()`처럼 정적 이름을 쓰지 않아 문자를 보장할 수 없다면 `hwmon_sanitize_name()`으로 문자열을 복제하고 잘못된 문자를 밑줄로 바꿀 수 있습니다. 이때 생긴 메모리는 호출자가 장치 제거 시 해제해야 합니다. `devm_hwmon_sanitize_name()`은 그 메모리를 자동으로 해제하는 장치 관리형 버전입니다.

`[devm_]hwmon_device_register_with_info()`와 연결된 접근 함수는 hwmon 코어가 직렬화합니다. 인터럽트 처리기나 드라이버가 완전히 구현한 별도 속성처럼 다른 경로에도 같은 직렬화가 필요하면 `hwmon_lock()`과 `hwmon_unlock()`으로 해당 호출들을 보호할 수 있습니다.

등록 방식 선택
방식제거 책임새 드라이버
hwmon_device_register_with_info()remove에서 unregister 호출사용 가능
devm_hwmon_device_register_with_info()장치 관리 계층이 자동 처리권장
기타 기존 등록 함수함수별 상이사용 금지, deprecated
이름 인수 NULL부모 장치 이름에서 파생유효
동적 이름sanitize 후 메모리 수명 관리문자 검증 필요

등록 방식에 따른 제거 책임과 사용 조건입니다.

등록부터 제거까지
유효한 이름 또는 정규화된 이름 준비부모 장치와 칩 정보 전달코어가 표준 속성과 직렬화 제공드라이버 동작수동 등록은 unregister, devm 등록은 자동 해제

수동 등록과 장치 관리형 등록의 수명 주기를 비교합니다.

hwmon_device_register_with_info registers a hardware monitoring device.
It creates the standard sysfs attributes in the hardware monitoring core,
letting the driver focus on reading from and writing to the chip instead
of having to bother with sysfs attributes. The parent device parameter
as well as the chip parameter must not be NULL. Its parameters are described
in more detail below.

devm_hwmon_device_register_with_info is similar to
hwmon_device_register_with_info. However, it is device managed, meaning the
hwmon device does not have to be removed explicitly by the removal function.

All other hardware monitoring device registration functions are deprecated
and must not be used in new drivers.

hwmon_device_unregister deregisters a registered hardware monitoring device.
The parameter of this function is the pointer to the registered hardware
monitoring device structure. This function must be called from the driver
remove function if the hardware monitoring device was registered with
hwmon_device_register_with_info.

All supported hwmon device registration functions only accept valid device
names. Device names including invalid characters (whitespace, '*', or '-')
will be rejected. If NULL is passed as name parameter, the hardware monitoring
device name will be derived from the parent device name.

If the driver doesn't use a static device name (for example it uses
dev_name()), and therefore cannot make sure the name only contains valid
characters, hwmon_sanitize_name can be used. This convenience function
will duplicate the string and replace any invalid characters with an
underscore. It will allocate memory for the new string and it is the
responsibility of the caller to release the memory when the device is
removed.

devm_hwmon_sanitize_name is the resource managed version of
hwmon_sanitize_name; the memory will be freed automatically on device
removal.

When using ``[devm_]hwmon_device_register_with_info()`` to register the
hardware monitoring device, accesses using the associated access functions
are serialised by the hardware monitoring core. If a driver needs locking
for other functions such as interrupt handlers or for attributes which are
fully implemented in the driver, hwmon_lock() and hwmon_unlock() can be used
to ensure that calls to those functions are serialized.

등록 인수와 hwmon_chip_info 연산

91-147

`hwmon_device_register_with_info()`에 전달하는 인수는 부모 장치 포인터 `dev`, 장치 이름 `name`, 드라이버 전용 데이터 `drvdata`, 칩 설명 `info`, 그리고 비표준 sysfs 속성 그룹의 `NULL` 종료 목록 `extra_groups`입니다. 성공하면 생성된 hwmon 장치 포인터를 반환하고, 실패하면 음수 오류 코드를 반환합니다.

`struct hwmon_chip_info`는 장치 연산을 가리키는 `ops`와 장치 채널 설명자의 `NULL` 종료 목록을 가리키는 `info`로 구성됩니다. 이 두 필드가 드라이버의 동작 계약과 센서 구성을 코어에 연결합니다.

`struct hwmon_ops`의 `is_visible` 콜백은 지원하는 각 속성의 파일 모드를 반환하며 필수입니다. `read`는 칩에서 값을 읽는 선택 콜백이지만 읽을 수 있는 속성이 하나라도 있으면 반드시 제공해야 합니다. `write`도 선택 콜백이지만 쓸 수 있는 속성이 존재하면 반드시 제공해야 합니다.

등록 함수 인수
인수의미
struct device *dev부모 장치 포인터
const char *name장치 이름
void *drvdata드라이버 전용 데이터
const struct hwmon_chip_info *info칩 설명 포인터
const struct attribute_group **extra_groups추가 비표준 sysfs 속성 그룹의 NULL 종료 목록

원문의 인수 표를 한국어로 구조화했습니다.

hwmon_ops 계약
콜백필수 여부역할
is_visible항상 필수지원 속성별 파일 모드 반환
read읽기 속성이 있으면 필수칩에서 값을 읽음
write쓰기 속성이 있으면 필수칩에 값을 기록

속성의 가시성, 읽기, 쓰기 콜백 제공 조건입니다.

칩 설명 연결
부모 장치와 drvdata 준비hwmon_ops 콜백 정의채널 설명자 목록 정의hwmon_chip_info에 ops와 info 연결register_with_info로 코어에 전달

등록 정보가 sysfs 접근 콜백으로 이어지는 구조입니다.


Using devm_hwmon_device_register_with_info()
--------------------------------------------

hwmon_device_register_with_info() registers a hardware monitoring device.
The parameters to this function are

=============================================== ===============================================
`struct device *dev`                                Pointer to parent device
`const char *name`                                Device name
`void *drvdata`                                        Driver private data
`const struct hwmon_chip_info *info`                Pointer to chip description.
`const struct attribute_group **extra_groups`         Null-terminated list of additional non-standard
                                                sysfs attribute groups.
=============================================== ===============================================

This function returns a pointer to the created hardware monitoring device
on success and a negative error code for failure.

The hwmon_chip_info structure looks as follows::

        struct hwmon_chip_info {
                const struct hwmon_ops *ops;
                const struct hwmon_channel_info * const *info;
        };

It contains the following fields:

* ops:
        Pointer to device operations.
* info:
        NULL-terminated list of device channel descriptors.

The list of hwmon operations is defined as::

  struct hwmon_ops {
        umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
                              u32 attr, int);
        int (*read)(struct device *, enum hwmon_sensor_types type,
                    u32 attr, int, long *);
        int (*write)(struct device *, enum hwmon_sensor_types type,
                     u32 attr, int, long);
  };

It defines the following operations.

* is_visible:
    Pointer to a function to return the file mode for each supported
    attribute. This function is mandatory.

* read:
    Pointer to a function for reading a value from the chip. This function
    is optional, but must be provided if any readable attributes exist.

* write:
    Pointer to a function for writing a value to the chip. This function is
    optional, but must be provided if any writeable attributes exist.

센서 채널 형식과 구성 비트

148-182

각 센서 채널은 `struct hwmon_channel_info`로 설명합니다. `type`은 하드웨어 모니터링 센서 형식이고, `config`는 해당 형식의 각 센서가 지원하는 속성을 나타내는 구성값의 포인터입니다.

지원 형식에는 특정 입출력에 묶이지 않은 칩 속성을 위한 가상 형식 `hwmon_chip`, 온도 `hwmon_temp`, 전압 `hwmon_in`, 전류 `hwmon_curr`, 전력 `hwmon_power`, 에너지 `hwmon_energy`, 64비트 부호 있는 값으로 보고하는 에너지 `hwmon_energy64`, 습도 `hwmon_humidity`, 팬 속도 `hwmon_fan`, PWM 제어 `hwmon_pwm`가 있습니다.

`config`는 주어진 형식의 센서별 구성값을 나열하고 마지막을 0으로 끝냅니다. 각 값은 단일 센서가 제공하는 속성을 나타내는 비트값들의 조합입니다. 원문에서 `attributes supposed by a single sensor`라고 적힌 부분은 문맥상 단일 센서가 지원하도록 지정된 속성을 뜻합니다.

hwmon 센서 형식
열거값설명
hwmon_chip특정 입력·출력에 묶이지 않은 칩 속성용 가상 형식
hwmon_temp온도 센서
hwmon_in전압 센서
hwmon_curr전류 센서
hwmon_power전력 센서
hwmon_energy에너지 센서
hwmon_energy6464비트 부호 있는 값으로 보고하는 에너지 센서
hwmon_humidity습도 센서
hwmon_fan팬 속도 센서
hwmon_pwmPWM 제어

원문의 센서 형식 표를 의미별로 옮겼습니다.

채널 구성 해석
센서 형식 선택센서별 속성 비트 조합config 목록 끝에 0 배치hwmon_channel_info에 type과 config 연결채널 목록을 hwmon_chip_info에 등록

센서 형식과 속성 비트가 채널 설명자로 결합됩니다.


Each sensor channel is described with struct hwmon_channel_info, which is
defined as follows::

        struct hwmon_channel_info {
                enum hwmon_sensor_types type;
                u32 *config;
        };

It contains following fields:

* type:
    The hardware monitoring sensor type.

    Supported sensor types are

     ================== ==================================================
     hwmon_chip                A virtual sensor type, used to describe attributes
                        which are not bound to a specific input or output
     hwmon_temp                Temperature sensor
     hwmon_in                Voltage sensor
     hwmon_curr                Current sensor
     hwmon_power                Power sensor
     hwmon_energy        Energy sensor
     hwmon_energy64        Energy sensor, reported as 64-bit signed value
     hwmon_humidity        Humidity sensor
     hwmon_fan                Fan speed sensor
     hwmon_pwm                PWM control
     ================== ==================================================

* config:
    Pointer to a 0-terminated list of configuration values for each
    sensor of the given type. Each value is a combination of bit values
    describing the attributes supposed by a single sensor.

LM75 구성 예제와 속성 접두사

183-255

LM75 호환 칩은 온도 센서 하나를 가진 예제로 제시됩니다. 드라이버는 `HWMON_C_REGISTER_TZ`로 thermal 하위 시스템에 등록하고 `HWMON_C_UPDATE_INTERVAL` 속성을 지원합니다. 온도 센서는 `HWMON_T_INPUT`으로 현재 온도를 읽고, `HWMON_T_MAX` 최대 온도 레지스터와 `HWMON_T_MAX_HYST` 최대 온도 히스테리시스 레지스터를 제공합니다.

긴 형식에서는 `lm75_chip_config`와 `lm75_temp_config`를 각각 0으로 끝내고, 이를 `hwmon_chip` 및 `hwmon_temp` 형식의 `struct hwmon_channel_info`에 연결합니다. `lm75_info`는 두 채널 설명자 포인터 뒤에 `NULL`을 둡니다.

가능하면 `HWMON_CHANNEL_INFO()` 매크로를 사용해야 합니다. 이 매크로는 별도의 구성 배열과 채널 구조체를 간결한 채널 선언으로 바꿉니다. 이후 `lm75_hwmon_ops`에 `lm75_is_visible`, `lm75_read`, `lm75_write`를 연결하고, `lm75_chip_info`에 연산과 채널 목록을 지정합니다.

개별 속성 지원 비트의 전체 목록은 `include/linux/hwmon.h`에 있습니다. 칩과 전류 속성이 모두 `HWMON_C_xxxx` 접두사를 사용하므로 형식 문맥을 구분해야 합니다. 나머지는 온도 `HWMON_T_xxxx`, 전압 `HWMON_I_xxxx`, 전력 `HWMON_P_xxxx`, 에너지 `HWMON_E_xxxx`, 습도 `HWMON_H_xxxx`, 팬 `HWMON_F_xxxx`, PWM `HWMON_PWM_xxxx`입니다.

속성 지원 비트 접두사
접두사대상
HWMON_C_xxxxhwmon_chip의 칩 속성
HWMON_T_xxxxhwmon_temp의 온도 속성
HWMON_I_xxxxhwmon_in의 전압 속성
HWMON_C_xxxxhwmon_curr의 전류 속성, 칩 속성과 접두사 중복
HWMON_P_xxxxhwmon_power의 전력 속성
HWMON_E_xxxxhwmon_energy의 에너지 속성
HWMON_H_xxxxhwmon_humidity의 습도 속성
HWMON_F_xxxxhwmon_fan의 팬 속도 속성
HWMON_PWM_xxxxhwmon_pwm의 PWM 제어 속성

센서 형식별 정의 접두사와 주의점을 정리했습니다.

LM75 설명자 구성
칩 속성 비트 선택온도 속성 비트 선택HWMON_CHANNEL_INFO로 채널 선언is_visible·read·write 연산 연결hwmon_chip_info 완성

지원 비트를 채널과 칩 정보로 묶는 예제의 순서입니다.

As an example, here is the complete description file for a LM75 compatible
sensor chip. The chip has a single temperature sensor. The driver wants to
register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
reading the temperature (HWMON_T_INPUT), it has a maximum temperature
register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
(HWMON_T_MAX_HYST)::

        static const u32 lm75_chip_config[] = {
                HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
                0
        };

        static const struct hwmon_channel_info lm75_chip = {
                .type = hwmon_chip,
                .config = lm75_chip_config,
        };

        static const u32 lm75_temp_config[] = {
                HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
                0
        };

        static const struct hwmon_channel_info lm75_temp = {
                .type = hwmon_temp,
                .config = lm75_temp_config,
        };

        static const struct hwmon_channel_info * const lm75_info[] = {
                &lm75_chip,
                &lm75_temp,
                NULL
        };

        The HWMON_CHANNEL_INFO() macro can and should be used when possible.
        With this macro, the above example can be simplified to

        static const struct hwmon_channel_info * const lm75_info[] = {
                HWMON_CHANNEL_INFO(chip,
                                HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
                HWMON_CHANNEL_INFO(temp,
                                HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
                NULL
        };

        The remaining declarations are as follows.

        static const struct hwmon_ops lm75_hwmon_ops = {
                .is_visible = lm75_is_visible,
                .read = lm75_read,
                .write = lm75_write,
        };

        static const struct hwmon_chip_info lm75_chip_info = {
                .ops = &lm75_hwmon_ops,
                .info = lm75_info,
        };

A complete list of bit values indicating individual attribute support
is defined in include/linux/hwmon.h. Definition prefixes are as follows.

=============== =================================================
HWMON_C_xxxx        Chip attributes, for use with hwmon_chip.
HWMON_T_xxxx        Temperature attributes, for use with hwmon_temp.
HWMON_I_xxxx        Voltage attributes, for use with hwmon_in.
HWMON_C_xxxx        Current attributes, for use with hwmon_curr.
                Notice the prefix overlap with chip attributes.
HWMON_P_xxxx        Power attributes, for use with hwmon_power.
HWMON_E_xxxx        Energy attributes, for use with hwmon_energy.
HWMON_H_xxxx        Humidity attributes, for use with hwmon_humidity.
HWMON_F_xxxx        Fan speed attributes, for use with hwmon_fan.
HWMON_PWM_xxxx        PWM control attributes, for use with hwmon_pwm.
=============== =================================================

드라이버 콜백의 인수와 반환값

256-330

각 드라이버는 `is_visible`, `read`, `write` 함수를 제공합니다. `is_visible_func()`의 `data`는 장치 전용 데이터 구조체 포인터, `type`은 센서 형식, `attr`은 특정 속성의 식별자, `channel`은 센서 채널 번호입니다. 예를 들어 `HWMON_T_INPUT` 비트에 대응하는 속성값은 `hwmon_temp_input`이며 전체 매핑은 `include/linux/hwmon.h`에 있습니다.

`is_visible_func()`는 해당 속성의 파일 모드를 반환합니다. 일반적으로 0이면 속성을 만들지 않고, `0444`이면 읽기 전용, `0644`이면 소유자 쓰기와 전체 읽기를 허용합니다.

`read_func()`에는 hwmon 장치 포인터 `dev`, 센서 형식 `type`, 속성 식별자 `attr`, 채널 번호 `channel`, 값을 돌려줄 `long *val`을 전달합니다. `hwmon_energy64`에서는 `val`이 `long *`로 전달되지만 `s64 *`로 형변환해야 합니다. 성공 시 0, 실패 시 음수 오류 번호를 반환합니다.

`write_func()`는 같은 장치, 형식, 속성, 채널 인수와 칩에 기록할 `long val`을 받습니다. 이 함수도 성공하면 0, 실패하면 음수 오류 번호를 반환합니다.

콜백 인수와 결과
콜백고유 인수·결과성공
is_visibleconst void *data, 파일 모드 반환0·0444·0644 등
readstruct device *dev, long *val0
writestruct device *dev, long val0
read(hwmon_energy64)long *val을 s64 *로 형변환0
read/write 오류음수 오류 번호해당 없음

세 콜백의 핵심 계약을 한 표로 정리했습니다.

속성 접근 콜백
type·attr·channel로 속성 식별is_visible에서 파일 모드 결정읽기는 read와 val 포인터 사용쓰기는 write와 val 값 사용0 또는 음수 오류 번호 반환

코어가 속성 접근을 드라이버 콜백으로 전달하는 과정입니다.


Driver callback functions
-------------------------

Each driver provides is_visible, read, and write functions. Parameters
and return values for those functions are as follows::

  umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
                          u32 attr, int channel)

Parameters:
        data:
                Pointer to device private data structure.
        type:
                The sensor type.
        attr:
                Attribute identifier associated with a specific attribute.
                For example, the attribute value for HWMON_T_INPUT would be
                hwmon_temp_input. For complete mappings of bit fields to
                attribute values please see include/linux/hwmon.h.
        channel:
                The sensor channel number.

Return value:
        The file mode for this attribute. Typically, this will be 0 (the
        attribute will not be created), 0444, or 0644.

::

        int read_func(struct device *dev, enum hwmon_sensor_types type,
                      u32 attr, int channel, long *val)

Parameters:
        dev:
                Pointer to the hardware monitoring device.
        type:
                The sensor type.
        attr:
                Attribute identifier associated with a specific attribute.
                For example, the attribute value for HWMON_T_INPUT would be
                hwmon_temp_input. For complete mappings please see
                include/linux/hwmon.h.
        channel:
                The sensor channel number.
        val:
                Pointer to attribute value.
                For hwmon_energy64, `'val`' is passed as `long *` but needs
                a typecast to `s64 *`.

Return value:
        0 on success, a negative error number otherwise.

::

        int write_func(struct device *dev, enum hwmon_sensor_types type,
                       u32 attr, int channel, long val)

Parameters:
        dev:
                Pointer to the hardware monitoring device.
        type:
                The sensor type.
        attr:
                Attribute identifier associated with a specific attribute.
                For example, the attribute value for HWMON_T_INPUT would be
                hwmon_temp_input. For complete mappings please see
                include/linux/hwmon.h.
        channel:
                The sensor channel number.
        val:
                The value to write to the chip.

Return value:
        0 on success, a negative error number otherwise.

드라이버 제공 추가 sysfs 속성

331-381

대부분의 경우 hwmon 코어가 표준 sysfs 속성을 내부에서 만들기 때문에 드라이버가 직접 속성을 제공할 필요가 없습니다. 드라이버가 추가해야 하는 것은 비표준 sysfs 속성뿐입니다. `<linux/hwmon-sysfs.h>`는 이런 속성을 선언하고 사용하는 여러 매크로를 제공합니다.

추가 문맥이 없는 속성은 기존 `DEVICE_ATTR` 또는 `DEVICE_ATTR_{RW,RO,WO}` 변형으로 선언할 수 있습니다. 하지만 센서 인덱스처럼 sysfs 처리 함수에 넘겨야 할 정보가 있으면 `SENSOR_DEVICE_ATTR` 또는 `SENSOR_DEVICE_ATTR_2`를 사용합니다. 전자는 추가 인수 하나, 후자는 두 개를 전달합니다.

표준 권한과 함수 이름을 사용할 수 있다면 단순화된 변형을 사용해야 합니다. `SENSOR_DEVICE_ATTR[_2]_RW`의 표준 권한은 `0644`, `SENSOR_DEVICE_ATTR[_2]_RO`는 `0444`, `SENSOR_DEVICE_ATTR[_2]_WO`는 `0200`입니다. `DEVICE_ATTR_{RW,RO,WO}`와 비슷하게 제공한 함수 이름 뒤에 `_show`와 `_store`가 붙어 표준 함수 이름이 됩니다.

`SENSOR_DEVICE_ATTR` 계열은 `struct device_attribute dev_attr`과 `int index`를 가진 `struct sensor_device_attribute` 변수를 정의합니다. 속성 읽기·쓰기 함수에서 속성이 붙은 장치를 `to_sensor_dev_attr()`에 전달하면 이 구조체 포인터를 얻습니다.

`SENSOR_DEVICE_ATTR_2` 계열은 `struct device_attribute dev_attr`, `u8 index`, `u8 nr`을 가진 `struct sensor_device_attribute_2` 변수를 정의합니다. `to_sensor_dev_attr_2()`에 속성이 붙은 장치를 전달하면 이 구조체 포인터를 얻습니다.

추가 sysfs 속성 매크로
매크로추가 문맥표준 권한
DEVICE_ATTR_{RW,RO,WO}없음변형에 따름
SENSOR_DEVICE_ATTR인수 1개명시 또는 단순화 변형
SENSOR_DEVICE_ATTR_2인수 2개명시 또는 단순화 변형
SENSOR_DEVICE_ATTR[_2]_RWindex 또는 index+nr0644
SENSOR_DEVICE_ATTR[_2]_ROindex 또는 index+nr0444
SENSOR_DEVICE_ATTR[_2]_WOindex 또는 index+nr0200

문맥 인수 수와 표준 권한에 따라 사용할 매크로를 고릅니다.

비표준 속성 선언
표준 속성인지 먼저 확인비표준 속성만 드라이버에서 선언추가 문맥 0·1·2개 판별DEVICE_ATTR 또는 SENSOR_DEVICE_ATTR 계열 선택to_sensor_dev_attr 계열로 문맥 복원

추가 문맥의 수에 맞춰 속성 구조체와 변환 도우미를 선택합니다.


Driver-provided sysfs attributes
--------------------------------

In most situations it should not be necessary for a driver to provide sysfs
attributes since the hardware monitoring core creates those internally.
Only additional non-standard sysfs attributes need to be provided.

The header file linux/hwmon-sysfs.h provides a number of useful macros to
declare and use hardware monitoring sysfs attributes.

In many cases, you can use the existing define DEVICE_ATTR or its variants
DEVICE_ATTR_{RW,RO,WO} to declare such attributes. This is feasible if an
attribute has no additional context. However, in many cases there will be
additional information such as a sensor index which will need to be passed
to the sysfs attribute handling function.

SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
which need such additional context information. SENSOR_DEVICE_ATTR requires
one additional argument, SENSOR_DEVICE_ATTR_2 requires two.

Simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 are available
and should be used if standard attribute permissions and function names are
feasible. Standard permissions are 0644 for SENSOR_DEVICE_ATTR[_2]_RW,
0444 for SENSOR_DEVICE_ATTR[_2]_RO, and 0200 for SENSOR_DEVICE_ATTR[_2]_WO.
Standard functions, similar to DEVICE_ATTR_{RW,RO,WO}, have _show and _store
appended to the provided function name.

SENSOR_DEVICE_ATTR and its variants define a struct sensor_device_attribute
variable. This structure has the following fields::

        struct sensor_device_attribute {
                struct device_attribute dev_attr;
                int index;
        };

You can use to_sensor_dev_attr to get the pointer to this structure from the
attribute read or write function. Its parameter is the device to which the
attribute is attached.

SENSOR_DEVICE_ATTR_2 and its variants define a struct sensor_device_attribute_2
variable, which is defined as follows::

        struct sensor_device_attribute_2 {
                struct device_attribute dev_attr;
                u8 index;
                u8 nr;
        };

Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
is the device to which the attribute is attached.