xref: /dragonfly/sys/sys/sensors.h (revision 655933d6)
1 /* $OpenBSD: sensors.h,v 1.23 2007/03/22 16:55:31 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Alexander Yurchenko <grange@openbsd.org>
5  * Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_SENSORS_H_
30 #define _SYS_SENSORS_H_
31 
32 #include <sys/types.h>
33 
34 /* Sensor types */
35 enum sensor_type {
36 	SENSOR_TEMP,			/* temperature (muK) */
37 	SENSOR_FANRPM,			/* fan revolution speed */
38 	SENSOR_VOLTS_DC,		/* voltage (muV DC) */
39 	SENSOR_VOLTS_AC,		/* voltage (muV AC) */
40 	SENSOR_OHMS,			/* resistance */
41 	SENSOR_WATTS,			/* power */
42 	SENSOR_AMPS,			/* current (muA) */
43 	SENSOR_WATTHOUR,		/* power capacity */
44 	SENSOR_AMPHOUR,			/* power capacity */
45 	SENSOR_INDICATOR,		/* boolean indicator */
46 	SENSOR_INTEGER,			/* generic integer value */
47 	SENSOR_PERCENT,			/* percent */
48 	SENSOR_LUX,			/* illuminance (mulx) */
49 	SENSOR_DRIVE,			/* disk */
50 	SENSOR_TIMEDELTA,		/* system time error (nSec) */
51 	SENSOR_ECC,			/* memory ecc */
52 	SENSOR_FREQ,			/* frequency */
53 	SENSOR_RESERVED_B,		/* reserved */
54 	SENSOR_RESERVED_C,		/* reserved */
55 	SENSOR_RESERVED_D,		/* reserved */
56 	SENSOR_RESERVED_E,		/* reserved */
57 	SENSOR_RESERVED_F,		/* reserved */
58 	SENSOR_MAX_TYPES
59 };
60 
61 static const char * const sensor_type_s[SENSOR_MAX_TYPES + 1] = {
62 	"temp",
63 	"fan",
64 	"volt",
65 	"acvolt",
66 	"resistance",
67 	"power",
68 	"current",
69 	"watthour",
70 	"amphour",
71 	"indicator",
72 	"raw",
73 	"percent",
74 	"illuminance",
75 	"drive",
76 	"timedelta",
77 	"ecc",
78 	"freq",
79 	"reserved_b",
80 	"reserved_c",
81 	"reserved_d",
82 	"reserved_e",
83 	"reserved_f",
84 	"undefined"
85 };
86 
87 #define SENSOR_DRIVE_EMPTY	1
88 #define SENSOR_DRIVE_READY	2
89 #define SENSOR_DRIVE_POWERUP	3
90 #define SENSOR_DRIVE_ONLINE	4
91 #define SENSOR_DRIVE_IDLE	5
92 #define SENSOR_DRIVE_ACTIVE	6
93 #define SENSOR_DRIVE_REBUILD	7
94 #define SENSOR_DRIVE_POWERDOWN	8
95 #define SENSOR_DRIVE_FAIL	9
96 #define SENSOR_DRIVE_PFAIL	10
97 
98 /* Sensor states */
99 enum sensor_status {
100 	SENSOR_S_UNSPEC,		/* status is unspecified */
101 	SENSOR_S_OK,			/* status is ok */
102 	SENSOR_S_WARN,			/* status is warning */
103 	SENSOR_S_CRIT,			/* status is critical */
104 	SENSOR_S_UNKNOWN		/* status is unknown */
105 };
106 
107 /*
108  * Sensor data:
109  * New fields should be added at the end to encourage backwards compat
110  */
111 struct sensor {
112 	char desc[32];			/* sensor description, may be empty */
113 	struct timeval tv;		/* sensor value last change time */
114 	int64_t value;			/* current value */
115 	enum sensor_type type;		/* sensor type */
116 	enum sensor_status status;	/* sensor status */
117 	int numt;			/* sensor number of .type type */
118 	int flags;			/* sensor flags */
119 #define SENSOR_FINVALID		0x0001	/* sensor is invalid */
120 #define SENSOR_FUNKNOWN		0x0002	/* sensor value is unknown */
121 };
122 
123 /*
124  * Sensor device data:
125  * New fields should be added at the end to encourage backwards compat
126  */
127 struct sensordev {
128 	int num;			/* sensordev number */
129 	char xname[16];			/* unix device name */
130 	int maxnumt[SENSOR_MAX_TYPES];
131 	int sensors_count;
132 };
133 
134 #define MAXSENSORDEVICES 32
135 
136 #ifdef _KERNEL
137 
138 #include <sys/queue.h>
139 #include <sys/sysctl.h>
140 
141 /* Sensor data */
142 struct ksensor {
143 	SLIST_ENTRY(ksensor) list;	/* device-scope list */
144 	char desc[32];			/* sensor description, may be empty */
145 	struct timeval tv;		/* sensor value last change time */
146 	int64_t value;			/* current value */
147 	enum sensor_type type;		/* sensor type */
148 	enum sensor_status status;	/* sensor status */
149 	int numt;			/* sensor number of .type type */
150 	int flags;			/* sensor flags, ie. SENSOR_FINVALID */
151 	struct sysctl_oid *oid;
152 };
153 SLIST_HEAD(ksensors_head, ksensor);
154 
155 /* Sensor device data */
156 struct ksensordev {
157 	TAILQ_ENTRY(ksensordev)	list;
158 	int num;			/* sensordev number */
159 	char xname[16];			/* unix device name */
160 	int maxnumt[SENSOR_MAX_TYPES];
161 	int sensors_count;
162 	struct ksensors_head sensors_list;
163 	struct sysctl_oid *oid;
164 	struct sysctl_ctx_list clist;
165 };
166 
167 /* struct ksensordev */
168 void		sensordev_install(struct ksensordev *);
169 void		sensordev_deinstall(struct ksensordev *);
170 
171 /* struct ksensor */
172 void		sensor_attach(struct ksensordev *, struct ksensor *);
173 void		sensor_detach(struct ksensordev *, struct ksensor *);
174 
175 /*
176  * Task scheduling
177  * Deprecated; use sensor_task_{register,unregiser}2() instead.
178  */
179 void		sensor_task_register(void *, void (*)(void *), int);
180 void		sensor_task_unregister(void *);
181 
182 /*
183  * Task scheduling
184  */
185 struct sensor_task;
186 struct sensor_task *
187 		sensor_task_register2(void *, void (*)(void *),
188 		    int period, int cpu);
189 void		sensor_task_unregister2(struct sensor_task *);
190 
191 static __inline void
192 sensor_set_invalid(struct ksensor *sens)
193 {
194 	sens->status = SENSOR_S_UNSPEC;
195 	sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
196 	sens->flags |= SENSOR_FINVALID;
197 	sens->value = 0;
198 }
199 
200 static __inline void
201 sensor_set_unknown(struct ksensor *sens)
202 {
203 	sens->status = SENSOR_S_UNKNOWN;
204 	sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
205 	sens->flags |= SENSOR_FUNKNOWN;
206 	sens->value = 0;
207 }
208 
209 static __inline void
210 sensor_set(struct ksensor *sens, int64_t val, enum sensor_status status)
211 {
212 	sens->status = status;
213 	sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
214 	sens->value = val;
215 }
216 
217 static __inline void
218 sensor_set_temp_degc(struct ksensor *sens, int degc, enum sensor_status status)
219 {
220 	sensor_set(sens, (degc * 1000000) + 273150000, status);
221 }
222 
223 #endif	/* _KERNEL */
224 
225 #endif	/* !_SYS_SENSORS_H_ */
226