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