sensor: Factor out helper functions.
[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_MAX_TYPES
53 };
54
55 static const char * const sensor_type_s[SENSOR_MAX_TYPES + 1] = {
56         "temp",
57         "fan",
58         "volt",
59         "acvolt",
60         "resistance",
61         "power",
62         "current",
63         "watthour",
64         "amphour",
65         "indicator",
66         "raw",
67         "percent",
68         "illuminance",
69         "drive",
70         "timedelta",
71         "undefined"
72 };
73
74 #define SENSOR_DRIVE_EMPTY      1
75 #define SENSOR_DRIVE_READY      2
76 #define SENSOR_DRIVE_POWERUP    3
77 #define SENSOR_DRIVE_ONLINE     4
78 #define SENSOR_DRIVE_IDLE       5
79 #define SENSOR_DRIVE_ACTIVE     6
80 #define SENSOR_DRIVE_REBUILD    7
81 #define SENSOR_DRIVE_POWERDOWN  8
82 #define SENSOR_DRIVE_FAIL       9
83 #define SENSOR_DRIVE_PFAIL      10
84
85 /* Sensor states */
86 enum sensor_status {
87         SENSOR_S_UNSPEC,                /* status is unspecified */
88         SENSOR_S_OK,                    /* status is ok */
89         SENSOR_S_WARN,                  /* status is warning */
90         SENSOR_S_CRIT,                  /* status is critical */
91         SENSOR_S_UNKNOWN                /* status is unknown */
92 };
93
94 /*
95  * Sensor data:
96  * New fields should be added at the end to encourage backwards compat
97  */
98 struct sensor {
99         char desc[32];                  /* sensor description, may be empty */
100         struct timeval tv;              /* sensor value last change time */
101         int64_t value;                  /* current value */
102         enum sensor_type type;          /* sensor type */
103         enum sensor_status status;      /* sensor status */
104         int numt;                       /* sensor number of .type type */
105         int flags;                      /* sensor flags */
106 #define SENSOR_FINVALID         0x0001  /* sensor is invalid */
107 #define SENSOR_FUNKNOWN         0x0002  /* sensor value is unknown */
108 };
109
110 /*
111  * Sensor device data:
112  * New fields should be added at the end to encourage backwards compat
113  */
114 struct sensordev {
115         int num;                        /* sensordev number */
116         char xname[16];                 /* unix device name */
117         int maxnumt[SENSOR_MAX_TYPES];
118         int sensors_count;
119 };
120
121 #define MAXSENSORDEVICES 32
122
123 #ifdef _KERNEL
124
125 #include <sys/queue.h>
126 #include <sys/sysctl.h>
127
128 /* Sensor data */
129 struct ksensor {
130         SLIST_ENTRY(ksensor) list;      /* device-scope list */
131         char desc[32];                  /* sensor description, may be empty */
132         struct timeval tv;              /* sensor value last change time */
133         int64_t value;                  /* current value */
134         enum sensor_type type;          /* sensor type */
135         enum sensor_status status;      /* sensor status */
136         int numt;                       /* sensor number of .type type */
137         int flags;                      /* sensor flags, ie. SENSOR_FINVALID */
138         struct sysctl_oid *oid;
139 };
140 SLIST_HEAD(ksensors_head, ksensor);
141
142 /* Sensor device data */
143 struct ksensordev {
144         TAILQ_ENTRY(ksensordev) list;
145         int num;                        /* sensordev number */
146         char xname[16];                 /* unix device name */
147         int maxnumt[SENSOR_MAX_TYPES];
148         int sensors_count;
149         struct ksensors_head sensors_list;
150         struct sysctl_oid *oid;
151         struct sysctl_ctx_list clist;
152 };
153
154 /* struct ksensordev */
155 void            sensordev_install(struct ksensordev *);
156 void            sensordev_deinstall(struct ksensordev *);
157
158 /* struct ksensor */
159 void            sensor_attach(struct ksensordev *, struct ksensor *);
160 void            sensor_detach(struct ksensordev *, struct ksensor *);
161
162 /* task scheduling */
163 void            sensor_task_register(void *, void (*)(void *), int);
164 void            sensor_task_unregister(void *);
165
166 static __inline void
167 sensor_set_invalid(struct ksensor *sens)
168 {
169         sens->status = SENSOR_S_UNSPEC;
170         sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
171         sens->flags |= SENSOR_FINVALID;
172         sens->value = 0;
173 }
174
175 static __inline void
176 sensor_set_unknown(struct ksensor *sens)
177 {
178         sens->status = SENSOR_S_UNKNOWN;
179         sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
180         sens->flags |= SENSOR_FUNKNOWN;
181         sens->value = 0;
182 }
183
184 static __inline void
185 sensor_set(struct ksensor *sens, int val, enum sensor_status status)
186 {
187         sens->status = status;
188         sens->flags &= ~(SENSOR_FUNKNOWN | SENSOR_FINVALID);
189         sens->value = val;
190 }
191
192 static __inline void
193 sensor_set_temp_degc(struct ksensor *sens, int degc, enum sensor_status status)
194 {
195         sensor_set(sens, (degc * 1000000) + 273150000, status);
196 }
197
198 #endif  /* _KERNEL */
199
200 #endif  /* !_SYS_SENSORS_H_ */