xref: /illumos-gate/usr/src/man/man9e/ksensor_ops.9e (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1.\"
2.\" This file and its contents are supplied under the terms of the
3.\" Common Development and Distribution License ("CDDL"), version 1.0.
4.\" You may only use this file in accordance with the terms of version
5.\" 1.0 of the CDDL.
6.\"
7.\" A full copy of the text of the CDDL should have accompanied this
8.\" source.  A copy of the CDDL is also available via the Internet at
9.\" http://www.illumos.org/license/CDDL.
10.\"
11.\"
12.\" Copyright 2024 Oxide Computer Company
13.\"
14.Dd May 10, 2024
15.Dt KSENSOR_OPS 9E
16.Os
17.Sh NAME
18.Nm ksensor_ops ,
19.Nm kso_kind ,
20.Nm kso_scalar
21.Nd ksensor entry points
22.Sh SYNOPSIS
23.In sys/sensors.h
24.Ft int
25.Fo kso_kind
26.Fa "void *driver"
27.Fa "sensor_ioctl_kind_t *kind"
28.Fc
29.Ft int
30.Fo kso_scalar
31.Fa "void *driver"
32.Fa "sensor_ioctl_scalar_t *scalar"
33.Fc
34.Sh INTERFACE LEVEL
35.Sy Volatile -
36This interface is still evolving in illumos.
37API and ABI stability is not guaranteed.
38.Sh PARAMETERS
39.Bl -tag -width Fa
40.It Fa driver
41A pointer to the driver's private data that was passed as an argument to
42.Xr ksensor_create 9F .
43.It Fa kind
44A pointer to a structure the driver will fill out to answer what kind of
45sensor it is.
46.It Fa scalar
47A pointer to a structure that the driver will fill out to answer the
48current value of the sensor.
49.El
50.Sh DESCRIPTION
51The
52.Xr ksensor 9E
53framework requires that device drivers provide an operations vector when
54registering a sensor with
55.Xr ksensor_create 9F .
56The operations vector uses the
57.Vt ksensor_ops_t
58structure and is implemented in terms of two entry points
59.Fn kso_kind
60and
61.Fn kso_scalar ,
62both of which are required.
63.Pp
64In all entry points, the driver will be passed back
65.Fa driver ,
66which is the argument registered when the sensor was created.
67This provides the driver a direct means to determine which sensor the
68framework is asking about and allows the same operations vector to serve
69multiple instances of a sensor.
70.Pp
71The ksensor framework does not serialize calls to the operations
72vectors as part of its contract to sensor providers.
73Drivers must assume that the various entry points will be called in
74parallel from multiple threads and that if any locking is required, it
75is the driver's responsibility.
76.Ss ksensor kind
77The
78.Fn kso_kind
79entry point is used to answer the question of what kind of sensor
80something is.
81A ksensor's kind indicates to the user what type of physical phenomenon
82the sensor manages such as temperature, voltage, etc.
83Some sensors are synthesized from physical phenomena, but don't
84represent one themselves.
85These sensors use the
86.Fn kso_kind
87entry point to indicate that and what they're derived from.
88For many drivers, they can use one of the stock implementations that the
89kernel provides such as
90.Xr ksensor_kind_temperature 9F ,
91.Xr ksensor_kind_voltage 9F ,
92or
93.Xr ksensor_kind_current 9F
94if they're a stock temperature, voltage, or current sensor.
95.Pp
96For drivers that must implement this themselves, they should fill out
97the members of the
98.Ft sensor_ioctl_kind_t
99structure as follows:
100.Bl -tag -width Fa
101.It Fa sik_kind
102This member should be filled in with the kind of the sensor from the list in
103.Xr ksensor 9E .
104The driver should not use
105.Dv SENSOR_KIND_UNKNOWN .
106If the driver uses
107.Dv SENSOR_KIND_SYNTHETIC
108then it should fill in
109.Fa sik_derive .
110.It Fa sik_derive
111If the driver did not set
112.Fa sik_kind
113to
114.Dv SENSOR_KIND_SYNTHETIC ,
115then this member should not be set and left at its default value that
116the framework provides.
117Otherwise, if the type that it is derived from is known, then it should
118be set to one of the kind values other than
119.Dv SENSOR_KIND_UNKNOWN
120and
121.Dv SENSOR_KIND_SYNTHETIC .
122.El
123.Ss ksensor scalar
124The
125.Fn kso_scalar
126entry point is used to return information about a scalar value read from
127a sensor.
128This is the primary interface by which a value is read from a device.
129For more information on scalar sensors and the intended semantics, see
130the
131.Sy Sensor Types, Kinds, Units, and Naming
132section of
133.Xr ksensor 9E .
134.Pp
135When this entry point is called, the driver should fill out the members
136of the
137.Fa sensor_ioctl_scalar_t
138structure as follows:
139.Bl -tag -width Fa
140.It Fa sis_unit
141A
142.Vt uint32_t
143that indicates the unit that the sensor is in.
144This should be one of the units from the list in
145.Xr ksensor 9E
146and should not be
147.Dv SENSOR_UNIT_UNKNOWN .
148.Dv SENSOR_UNIT_NONE
149should only be used if the sensor's kind is
150.Dv SENSOR_KIND_SYNTHETIC .
151.It Fa sis_gran
152An
153.Vt int32_t
154that indicates the granularity or resolution of the sensor.
155The granularity indicates the number of increments per unit in the
156measurement.
157A value such as 10 indicates that the value is in 10ths of the unit.
158If this was a temperature sensor, one would need to divide
159.Fa sit_value
160by 10 to obtain degrees.
161On the other hand a negative granularity indicates one would need to multiply
162.Fa sit_value
163to get the actual base unit.
164For example, a value of -2 would indicate that actual number of degrees, you'd
165need to multiply the value by two.
166.It Fa sis_prec
167A
168.Vt uint32_t
169that represents the accuracy of the sensor itself and is measured in
170units of the granularity.
171For example, a temperature sensor that has a granularity of 1, meaning the value
172read from the sensor is in degrees, and is accurate to +/-5 degrees would set
173the precision to 5.
174Conversely, a temperature sensor that measured in 0.5 degree increments has a
175granularity of 2.
176If the sensor was accurate to +/-1 degree, then it'd have a precision of 2.
177If the precision is unknown, it should be left at zero.
178.It Fa sis_value
179A
180.Vt int64_t
181that represents the value read from the sensor.
182It is in units of the granularity and is a signed quantity.
183.El
184.Sh CONTEXT
185The
186.Fn kso_kind
187and
188.Fn kso_scalar
189functions are generally called from
190.Sy kernel
191context.
192While these functions may be called from
193.Sy user
194context, the driver must not assume that and should not be copying any
195data into or out of a user process.
196.Sh RETURN VALUES
197Upon successful completion, the device driver should have filled out the
198corresponding structure into
199.Fa kind
200or
201.Fa scalar
202and return
203.Sy 0 .
204Otherwise, a positive error number should be returned to indicate the
205failure.
206.Sh EXAMPLES
207.Sy Example 1
208Example PCI-based Implementation
209.Pp
210The following example shows what this might look like for PCI-based
211device driver that has a temperature sensor in configure space.
212This example assumes the sensor measures in 0.5 degree increments and
213is accurate to +/-1 degree .
214.Bd -literal
215#include <sys/ddi.h>
216#include <sys/sunddi.h>
217#include <sys/sensors.h>
218
219/*
220 * Our temperature sensor in configuration space. It returns an unsigned
221 * 32-bit value in 0.5 degree increments that indicates the current
222 * temperature in degrees C.
223 */
224#define	EX_SENSOR	0x200
225
226/*
227 * Our granularity is 0.5 degrees. Our precision is +/-1 degree, which
228 * is 2 units of our granularity, hence we define it as 2.
229 */
230#define	EX_SENSOR_GRAN	2
231#define	EX_SENSOR_PREC	2
232
233/*
234 * Driver structure that is registered with ksensor_create(9F). The
235 * ex_cfg member comes from a call to pci_config_setup() during
236 * attach(9E).
237 */
238typedef struct ex {
239	...
240	ddi_acc_handle_t ex_cfg;
241	...
242} ex_t;
243
244static int
245ex_sensor_temp_read(void *arg, sensor_ioctl_scalar_t *scalar)
246{
247	uint32_t reg;
248	ex_t *ex = arg;
249
250	reg = pci_config_get32(ex->ex_cfg, EX_SENSOR);
251	scalar->sis_unit = SENSOR_UNIT_CELSIUS;
252	scalar->sis_gran = EX_SENSOR_GRAN;
253	scalar->sis_prec = EX_SENSOR_PREC;
254	scalar->sis_value = reg;
255	return (0);
256}
257
258static const ksensor_ops_t ex_sensor_temp_ops = {
259	.kso_kind = ksensor_kind_temperature,
260	.kso_scalar = ex_sensor_temp_read
261};
262.Ed
263.Sh ERRORS
264The device driver may return one of the following errors, but is not
265limited to this set if there is a more accurate error based on the
266situation.
267.Bl -tag -width Er
268.It Er EIO
269This error should be used when the driver fails to communicate with the
270device to read the current sensor value.
271.El
272.Sh SEE ALSO
273.Xr ksensor 9E ,
274.Xr ksensor_create 9F ,
275.Xr ksensor_kind 9F
276