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