xref: /illumos-gate/usr/src/uts/common/sys/sensors.h (revision 74e12c43fe52f2c30f36e65a4d0fb0e8dfd7068a)
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 /*
13  * Copyright 2019, Joyent, Inc.
14  * Copyright 2020 Oxide Computer Company
15  */
16 
17 #ifndef _SYS_SENSORS_H
18 #define	_SYS_SENSORS_H
19 
20 /*
21  * Consolidated sensor ioctls for various parts of the operating system. These
22  * interfaces should not be relied on at all. They are evolving and will change
23  * as we add more to the system for this. This may eventually become a larger
24  * framework, though it's more likely we'll consolidate that in userland.
25  */
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /*
32  * List of different possible kinds of sensors.
33  */
34 #define	SENSOR_KIND_UNKNOWN		0x00
35 #define	SENSOR_KIND_TEMPERATURE		0x01
36 #define	SENSOR_KIND_VOLTAGE		0x02
37 #define	SENSOR_KIND_CURRENT		0x03
38 
39 /*
40  * Lists of units that senors may have.
41  */
42 #define	SENSOR_UNIT_UNKNOWN		0x00
43 #define	SENSOR_UNIT_CELSIUS		0x01
44 #define	SENSOR_UNIT_FAHRENHEIT		0x02
45 #define	SENSOR_UNIT_KELVIN		0x03
46 #define	SENSOR_UNIT_VOLTS		0x04
47 #define	SENSOR_UNIT_AMPS		0x05
48 
49 #define	SENSOR_IOCTL	(('s' << 24) | ('e' << 16) | ('n' << 8))
50 
51 /*
52  * Ask the sensor what kind of sensor it is.
53  */
54 #define	SENSOR_IOCTL_KIND	(SENSOR_IOCTL | 0x01)
55 
56 typedef struct sensor_ioctl_kind {
57 	uint64_t	sik_kind;
58 } sensor_ioctl_kind_t;
59 
60 /*
61  * Ask the sensor for a scalar measurement. The sensor is responsible for
62  * returning the units it's in.  A scalar measurement is broken down into a
63  * signed value and a notion of its granularity. The sit_gran member indicates
64  * the granularity: the number of increments per unit in the measurement (the
65  * sit_value member). sit_gran is signed and the sign indicates whether one
66  * needs to multiply or divide the granularity. The sit_prec member describes a
67  * +/- value (taking sit_gran into account) that describes the precision of the
68  * sensor.
69  *
70  * For example, consider a temperature sensor that set sit_gran to 10. This
71  * would mean that the value in sit_value was in 10ths of a degree and that to
72  * get the actual value in degrees, one would divide by 10. On the other hand, a
73  * negative value means that we effectively have to multiply to get there. For
74  * example, a value of -2 would indicate that each value in sit_value indicated
75  * two degrees and to get the temperature in degrees you would multiply
76  * sit_value * by two.
77  */
78 #define	SENSOR_IOCTL_SCALAR	(SENSOR_IOCTL | 0x02)
79 
80 typedef struct sensor_ioctl_scalar {
81 	uint32_t	sis_unit;
82 	int32_t		sis_gran;
83 	uint32_t	sis_prec;
84 	uint32_t	sis_pad;
85 	int64_t		sis_value;
86 } sensor_ioctl_scalar_t;
87 
88 #ifdef	_KERNEL
89 typedef int (*ksensor_kind_f)(void *, sensor_ioctl_kind_t *);
90 typedef int (*ksensor_scalar_f)(void *, sensor_ioctl_scalar_t *);
91 
92 typedef struct {
93 	ksensor_kind_f		kso_kind;
94 	ksensor_scalar_f	kso_scalar;
95 } ksensor_ops_t;
96 
97 extern int ksensor_kind_temperature(void *, sensor_ioctl_kind_t *);
98 extern int ksensor_kind_voltage(void *, sensor_ioctl_kind_t *);
99 extern int ksensor_kind_current(void *, sensor_ioctl_kind_t *);
100 
101 /*
102  * Create a sensor where the class and name is supplied.
103  */
104 extern int ksensor_create(dev_info_t *, const ksensor_ops_t *, void *,
105     const char *, const char *, id_t *);
106 
107 /*
108  * Create a scalar sensor for a PCI device. If this is not a device-wide
109  * (e.g. per-function) sensor, this should not be used.
110  */
111 extern int ksensor_create_scalar_pcidev(dev_info_t *, uint_t,
112     const ksensor_ops_t *, void *, const char *, id_t *);
113 
114 /*
115  * Remove a named or all sensors from this driver.
116  */
117 #define	KSENSOR_ALL_IDS	INT_MIN
118 extern int ksensor_remove(dev_info_t *, id_t);
119 
120 #endif
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* _SYS_SENSORS_H */
127