sysmon.c revision 1.2.2.2 1 1.2.2.2 bouyer /* $NetBSD: sysmon.c,v 1.2.2.2 2000/07/30 17:54:16 bouyer Exp $ */
2 1.2.2.2 bouyer
3 1.2.2.2 bouyer /*-
4 1.2.2.2 bouyer * Copyright (c) 2000 Zembu Labs, Inc.
5 1.2.2.2 bouyer * All rights reserved.
6 1.2.2.2 bouyer *
7 1.2.2.2 bouyer * Author: Jason R. Thorpe <thorpej (at) zembu.com>
8 1.2.2.2 bouyer *
9 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
11 1.2.2.2 bouyer * are met:
12 1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
17 1.2.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
18 1.2.2.2 bouyer * must display the following acknowledgement:
19 1.2.2.2 bouyer * This product includes software developed by Zembu Labs, Inc.
20 1.2.2.2 bouyer * 4. Neither the name of Zembu Labs nor the names of its employees may
21 1.2.2.2 bouyer * be used to endorse or promote products derived from this software
22 1.2.2.2 bouyer * without specific prior written permission.
23 1.2.2.2 bouyer *
24 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25 1.2.2.2 bouyer * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26 1.2.2.2 bouyer * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27 1.2.2.2 bouyer * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.2.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.2.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.2.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.2.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.2.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.2.2.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.2.2.2 bouyer */
35 1.2.2.2 bouyer
36 1.2.2.2 bouyer /*
37 1.2.2.2 bouyer * Clearing house for system monitoring hardware. This pseudo-device
38 1.2.2.2 bouyer * is a place where hardware monitors such as the LM78 and VIA 82C686A
39 1.2.2.2 bouyer * (or even ACPI, eventually) can register themselves to provide
40 1.2.2.2 bouyer * backplane fan and temperature information, etc.
41 1.2.2.2 bouyer *
42 1.2.2.2 bouyer * Eventually, we will also provide a way for a hardware watchdog timer
43 1.2.2.2 bouyer * to hook itself up here (with an option to fall back on a software
44 1.2.2.2 bouyer * watchdog timer if hardware is not available).
45 1.2.2.2 bouyer */
46 1.2.2.2 bouyer
47 1.2.2.2 bouyer #include <sys/param.h>
48 1.2.2.2 bouyer #include <sys/conf.h>
49 1.2.2.2 bouyer #include <sys/errno.h>
50 1.2.2.2 bouyer #include <sys/fcntl.h>
51 1.2.2.2 bouyer #include <sys/lock.h>
52 1.2.2.2 bouyer
53 1.2.2.2 bouyer #include <dev/sysmon/sysmonvar.h>
54 1.2.2.2 bouyer
55 1.2.2.2 bouyer /*
56 1.2.2.2 bouyer * We run at ENVSYS version 1.
57 1.2.2.2 bouyer */
58 1.2.2.2 bouyer #define SYSMON_ENVSYS_VERSION (1 * 1000)
59 1.2.2.2 bouyer
60 1.2.2.2 bouyer struct lock sysmon_lock;
61 1.2.2.2 bouyer
62 1.2.2.2 bouyer LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
63 1.2.2.2 bouyer struct simplelock sysmon_envsys_list_slock = SIMPLELOCK_INITIALIZER;
64 1.2.2.2 bouyer u_int sysmon_envsys_next_sensor_index;
65 1.2.2.2 bouyer
66 1.2.2.2 bouyer int sysmon_initialized;
67 1.2.2.2 bouyer struct simplelock sysmon_initialized_slock = SIMPLELOCK_INITIALIZER;
68 1.2.2.2 bouyer
69 1.2.2.2 bouyer cdev_decl(sysmon);
70 1.2.2.2 bouyer
71 1.2.2.2 bouyer void sysmon_init(void);
72 1.2.2.2 bouyer
73 1.2.2.2 bouyer struct sysmon_envsys *sysmon_envsys_find(u_int);
74 1.2.2.2 bouyer void sysmon_envsys_release(struct sysmon_envsys *);
75 1.2.2.2 bouyer
76 1.2.2.2 bouyer /*
77 1.2.2.2 bouyer * sysmon_init:
78 1.2.2.2 bouyer *
79 1.2.2.2 bouyer * Initialize the system monitor.
80 1.2.2.2 bouyer */
81 1.2.2.2 bouyer void
82 1.2.2.2 bouyer sysmon_init(void)
83 1.2.2.2 bouyer {
84 1.2.2.2 bouyer
85 1.2.2.2 bouyer lockinit(&sysmon_lock, PWAIT, "sysmon", 0, 0);
86 1.2.2.2 bouyer sysmon_initialized = 1;
87 1.2.2.2 bouyer }
88 1.2.2.2 bouyer
89 1.2.2.2 bouyer /*
90 1.2.2.2 bouyer * sysmonopen:
91 1.2.2.2 bouyer *
92 1.2.2.2 bouyer * Open the system monitor device.
93 1.2.2.2 bouyer */
94 1.2.2.2 bouyer int
95 1.2.2.2 bouyer sysmonopen(dev_t dev, int flag, int mode, struct proc *p)
96 1.2.2.2 bouyer {
97 1.2.2.2 bouyer int error;
98 1.2.2.2 bouyer
99 1.2.2.2 bouyer simple_lock(&sysmon_initialized_slock);
100 1.2.2.2 bouyer if (sysmon_initialized == 0)
101 1.2.2.2 bouyer sysmon_init();
102 1.2.2.2 bouyer
103 1.2.2.2 bouyer error = lockmgr(&sysmon_lock,
104 1.2.2.2 bouyer LK_EXCLUSIVE | LK_INTERLOCK |
105 1.2.2.2 bouyer ((flag & O_NONBLOCK) ? LK_NOWAIT : 0), &sysmon_initialized_slock);
106 1.2.2.2 bouyer
107 1.2.2.2 bouyer return (error);
108 1.2.2.2 bouyer }
109 1.2.2.2 bouyer
110 1.2.2.2 bouyer /*
111 1.2.2.2 bouyer * sysmonclose:
112 1.2.2.2 bouyer *
113 1.2.2.2 bouyer * Close the system monitor device.
114 1.2.2.2 bouyer */
115 1.2.2.2 bouyer int
116 1.2.2.2 bouyer sysmonclose(dev_t dev, int flag, int mode, struct proc *p)
117 1.2.2.2 bouyer {
118 1.2.2.2 bouyer
119 1.2.2.2 bouyer (void) lockmgr(&sysmon_lock, LK_RELEASE, NULL);
120 1.2.2.2 bouyer return (0);
121 1.2.2.2 bouyer }
122 1.2.2.2 bouyer
123 1.2.2.2 bouyer /*
124 1.2.2.2 bouyer * sysmonioctl:
125 1.2.2.2 bouyer *
126 1.2.2.2 bouyer * Perform a control request.
127 1.2.2.2 bouyer */
128 1.2.2.2 bouyer int
129 1.2.2.2 bouyer sysmonioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
130 1.2.2.2 bouyer {
131 1.2.2.2 bouyer struct sysmon_envsys *sme;
132 1.2.2.2 bouyer int error = 0;
133 1.2.2.2 bouyer u_int oidx;
134 1.2.2.2 bouyer
135 1.2.2.2 bouyer switch (cmd) {
136 1.2.2.2 bouyer /*
137 1.2.2.2 bouyer * For ENVSYS commands, we translate the absolute sensor index
138 1.2.2.2 bouyer * to a device-relative sensor index.
139 1.2.2.2 bouyer */
140 1.2.2.2 bouyer case ENVSYS_VERSION:
141 1.2.2.2 bouyer *(int32_t *)data = SYSMON_ENVSYS_VERSION;
142 1.2.2.2 bouyer break;
143 1.2.2.2 bouyer
144 1.2.2.2 bouyer case ENVSYS_GRANGE:
145 1.2.2.2 bouyer {
146 1.2.2.2 bouyer struct envsys_range *rng = (void *) data;
147 1.2.2.2 bouyer
148 1.2.2.2 bouyer sme = sysmon_envsys_find(0); /* XXX */
149 1.2.2.2 bouyer if (sme == NULL) {
150 1.2.2.2 bouyer /* Return empty range for `no sensors'. */
151 1.2.2.2 bouyer rng->low = 1;
152 1.2.2.2 bouyer rng->high = 0;
153 1.2.2.2 bouyer break;
154 1.2.2.2 bouyer }
155 1.2.2.2 bouyer
156 1.2.2.2 bouyer if (rng->units < ENVSYS_NSENSORS)
157 1.2.2.2 bouyer *rng = sme->sme_ranges[rng->units];
158 1.2.2.2 bouyer else {
159 1.2.2.2 bouyer /* Return empty range for unsupported sensor types. */
160 1.2.2.2 bouyer rng->low = 1;
161 1.2.2.2 bouyer rng->high = 0;
162 1.2.2.2 bouyer }
163 1.2.2.2 bouyer sysmon_envsys_release(sme);
164 1.2.2.2 bouyer break;
165 1.2.2.2 bouyer }
166 1.2.2.2 bouyer
167 1.2.2.2 bouyer case ENVSYS_GTREDATA:
168 1.2.2.2 bouyer {
169 1.2.2.2 bouyer struct envsys_tre_data *tred = (void *) data;
170 1.2.2.2 bouyer
171 1.2.2.2 bouyer sme = sysmon_envsys_find(tred->sensor);
172 1.2.2.2 bouyer if (sme == NULL)
173 1.2.2.2 bouyer break;
174 1.2.2.2 bouyer oidx = tred->sensor;
175 1.2.2.2 bouyer tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
176 1.2.2.2 bouyer if (tred->sensor < sme->sme_nsensors)
177 1.2.2.2 bouyer error = (*sme->sme_gtredata)(sme, tred);
178 1.2.2.2 bouyer else
179 1.2.2.2 bouyer tred->validflags = 0;
180 1.2.2.2 bouyer tred->sensor = oidx;
181 1.2.2.2 bouyer sysmon_envsys_release(sme);
182 1.2.2.2 bouyer break;
183 1.2.2.2 bouyer }
184 1.2.2.2 bouyer
185 1.2.2.2 bouyer case ENVSYS_STREINFO:
186 1.2.2.2 bouyer {
187 1.2.2.2 bouyer struct envsys_basic_info *binfo = (void *) data;
188 1.2.2.2 bouyer
189 1.2.2.2 bouyer sme = sysmon_envsys_find(binfo->sensor);
190 1.2.2.2 bouyer oidx = binfo->sensor;
191 1.2.2.2 bouyer binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
192 1.2.2.2 bouyer if (binfo->sensor < sme->sme_nsensors)
193 1.2.2.2 bouyer error = (*sme->sme_streinfo)(sme, binfo);
194 1.2.2.2 bouyer else
195 1.2.2.2 bouyer binfo->validflags = 0;
196 1.2.2.2 bouyer binfo->sensor = oidx;
197 1.2.2.2 bouyer sysmon_envsys_release(sme);
198 1.2.2.2 bouyer break;
199 1.2.2.2 bouyer }
200 1.2.2.2 bouyer
201 1.2.2.2 bouyer case ENVSYS_GTREINFO:
202 1.2.2.2 bouyer {
203 1.2.2.2 bouyer struct envsys_basic_info *binfo = (void *) data;
204 1.2.2.2 bouyer
205 1.2.2.2 bouyer sme = sysmon_envsys_find(binfo->sensor);
206 1.2.2.2 bouyer oidx = binfo->sensor;
207 1.2.2.2 bouyer binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
208 1.2.2.2 bouyer if (binfo->sensor < sme->sme_nsensors)
209 1.2.2.2 bouyer *binfo = sme->sme_sensor_info[binfo->sensor];
210 1.2.2.2 bouyer else
211 1.2.2.2 bouyer binfo->validflags = 0;
212 1.2.2.2 bouyer binfo->sensor = oidx;
213 1.2.2.2 bouyer sysmon_envsys_release(sme);
214 1.2.2.2 bouyer break;
215 1.2.2.2 bouyer }
216 1.2.2.2 bouyer
217 1.2.2.2 bouyer default:
218 1.2.2.2 bouyer error = ENOTTY;
219 1.2.2.2 bouyer }
220 1.2.2.2 bouyer
221 1.2.2.2 bouyer return (error);
222 1.2.2.2 bouyer }
223 1.2.2.2 bouyer
224 1.2.2.2 bouyer /*
225 1.2.2.2 bouyer * sysmon_envsys_register:
226 1.2.2.2 bouyer *
227 1.2.2.2 bouyer * Register an ENVSYS device.
228 1.2.2.2 bouyer */
229 1.2.2.2 bouyer int
230 1.2.2.2 bouyer sysmon_envsys_register(struct sysmon_envsys *sme)
231 1.2.2.2 bouyer {
232 1.2.2.2 bouyer int error = 0;
233 1.2.2.2 bouyer
234 1.2.2.2 bouyer simple_lock(&sysmon_envsys_list_slock);
235 1.2.2.2 bouyer
236 1.2.2.2 bouyer /* XXX Only get to register one, for now. */
237 1.2.2.2 bouyer if (LIST_FIRST(&sysmon_envsys_list) != NULL) {
238 1.2.2.2 bouyer error = EEXIST;
239 1.2.2.2 bouyer goto out;
240 1.2.2.2 bouyer }
241 1.2.2.2 bouyer
242 1.2.2.2 bouyer if (sme->sme_envsys_version != SYSMON_ENVSYS_VERSION) {
243 1.2.2.2 bouyer error = EINVAL;
244 1.2.2.2 bouyer goto out;
245 1.2.2.2 bouyer }
246 1.2.2.2 bouyer
247 1.2.2.2 bouyer sme->sme_fsensor = sysmon_envsys_next_sensor_index;
248 1.2.2.2 bouyer sysmon_envsys_next_sensor_index += sme->sme_nsensors;
249 1.2.2.2 bouyer LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
250 1.2.2.2 bouyer
251 1.2.2.2 bouyer out:
252 1.2.2.2 bouyer simple_unlock(&sysmon_envsys_list_slock);
253 1.2.2.2 bouyer return (error);
254 1.2.2.2 bouyer }
255 1.2.2.2 bouyer
256 1.2.2.2 bouyer /*
257 1.2.2.2 bouyer * sysmon_envsys_unregister:
258 1.2.2.2 bouyer *
259 1.2.2.2 bouyer * Unregister an ENVSYS device.
260 1.2.2.2 bouyer */
261 1.2.2.2 bouyer void
262 1.2.2.2 bouyer sysmon_envsys_unregister(struct sysmon_envsys *sme)
263 1.2.2.2 bouyer {
264 1.2.2.2 bouyer
265 1.2.2.2 bouyer simple_lock(&sysmon_envsys_list_slock);
266 1.2.2.2 bouyer LIST_REMOVE(sme, sme_list);
267 1.2.2.2 bouyer simple_unlock(&sysmon_envsys_list_slock);
268 1.2.2.2 bouyer }
269 1.2.2.2 bouyer
270 1.2.2.2 bouyer /*
271 1.2.2.2 bouyer * sysmon_envsys_find:
272 1.2.2.2 bouyer *
273 1.2.2.2 bouyer * Find an ENVSYS device. The list remains locked upon
274 1.2.2.2 bouyer * a match.
275 1.2.2.2 bouyer */
276 1.2.2.2 bouyer struct sysmon_envsys *
277 1.2.2.2 bouyer sysmon_envsys_find(u_int idx)
278 1.2.2.2 bouyer {
279 1.2.2.2 bouyer struct sysmon_envsys *sme;
280 1.2.2.2 bouyer
281 1.2.2.2 bouyer simple_lock(&sysmon_envsys_list_slock);
282 1.2.2.2 bouyer
283 1.2.2.2 bouyer for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
284 1.2.2.2 bouyer sme = LIST_NEXT(sme, sme_list)) {
285 1.2.2.2 bouyer if (idx >= sme->sme_fsensor &&
286 1.2.2.2 bouyer idx < (sme->sme_fsensor + sme->sme_nsensors))
287 1.2.2.2 bouyer return (sme);
288 1.2.2.2 bouyer }
289 1.2.2.2 bouyer
290 1.2.2.2 bouyer simple_unlock(&sysmon_envsys_list_slock);
291 1.2.2.2 bouyer return (NULL);
292 1.2.2.2 bouyer }
293 1.2.2.2 bouyer
294 1.2.2.2 bouyer /*
295 1.2.2.2 bouyer * sysmon_envsys_release:
296 1.2.2.2 bouyer *
297 1.2.2.2 bouyer * Release an ENVSYS device.
298 1.2.2.2 bouyer */
299 1.2.2.2 bouyer /* ARGSUSED */
300 1.2.2.2 bouyer void
301 1.2.2.2 bouyer sysmon_envsys_release(struct sysmon_envsys *sme)
302 1.2.2.2 bouyer {
303 1.2.2.2 bouyer
304 1.2.2.2 bouyer simple_unlock(&sysmon_envsys_list_slock);
305 1.2.2.2 bouyer }
306