sysmon_envsys.c revision 1.14 1 /* $NetBSD: sysmon_envsys.c,v 1.14 2007/02/18 23:38:11 xtraeme 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 * Environmental sensor framework for sysmon. Hardware monitors
38 * such as the LM78 and VIA 82C686A (or even ACPI, eventually) can
39 * register themselves to provide backplane fan and temperature
40 * information, etc.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.14 2007/02/18 23:38:11 xtraeme Exp $");
45
46 #include <sys/param.h>
47 #include <sys/conf.h>
48 #include <sys/errno.h>
49 #include <sys/fcntl.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56
57 #include <dev/sysmon/sysmonvar.h>
58
59 /*
60 * We run at ENVSYS version 1.
61 */
62 #define SYSMON_ENVSYS_VERSION (1 * 1000)
63
64 kmutex_t sysmon_envsys_mtx;
65
66 LIST_HEAD(, sysmon_envsys) sysmon_envsys_list =
67 LIST_HEAD_INITIALIZER(&sysmon_envsys_list);
68 kmutex_t sysmon_envsys_list_mtx;
69 kcondvar_t sysmon_envsys_cv;
70 u_int sysmon_envsys_next_sensor_index;
71
72 int sysmon_envsys_initialized;
73 kmutex_t sysmon_envsys_initialized_mtx;
74
75 #define SYSMON_ENVSYS_LOCK() \
76 mutex_enter(&sysmon_envsys_mtx)
77
78 #define SYSMON_ENVSYS_UNLOCK() \
79 mutex_exit(&sysmon_envsys_mtx)
80
81 struct sysmon_envsys *sysmon_envsys_find(u_int);
82 void sysmon_envsys_release(struct sysmon_envsys *);
83
84 /*
85 * sysmonopen_envsys:
86 *
87 * Open the system monitor device.
88 */
89 int
90 sysmonopen_envsys(dev_t dev, int flag, int mode,
91 struct lwp *l)
92 {
93 mutex_enter(&sysmon_envsys_initialized_mtx);
94 if (sysmon_envsys_initialized == 0) {
95 mutex_init(&sysmon_envsys_mtx, MUTEX_DRIVER, IPL_NONE);
96 sysmon_envsys_initialized = 1;
97 }
98 mutex_exit(&sysmon_envsys_initialized_mtx);
99
100 return (0);
101 }
102
103 /*
104 * sysmonclose_envsys:
105 *
106 * Close the system monitor device.
107 */
108 int
109 sysmonclose_envsys(dev_t dev, int flag, int mode,
110 struct lwp *l)
111 {
112
113 /* Nothing to do */
114 return (0);
115 }
116
117 /*
118 * sysmonioctl_envsys:
119 *
120 * Perform an envsys control request.
121 */
122 int
123 sysmonioctl_envsys(dev_t dev, u_long cmd, caddr_t data,
124 int flag, struct lwp *l)
125 {
126 struct sysmon_envsys *sme;
127 int error = 0;
128 u_int oidx;
129
130 switch (cmd) {
131 /*
132 * For ENVSYS commands, we translate the absolute sensor index
133 * to a device-relative sensor index.
134 */
135 case ENVSYS_VERSION:
136 *(int32_t *)data = SYSMON_ENVSYS_VERSION;
137 break;
138
139 case ENVSYS_GRANGE:
140 {
141 struct envsys_range *rng = (void *) data;
142 int i;
143
144
145 /* Return empty range unless we find something better */
146 rng->low = 1;
147 rng->high = 0;
148
149 if (rng->units == -1) {
150 rng->low = 0;
151 rng->high = sysmon_envsys_next_sensor_index;
152 break;
153 }
154
155 sme = sysmon_envsys_find(0); /* XXX */
156 if (sme == NULL) {
157 /* Return empty range for `no sensors'. */
158 break;
159 }
160 for (i = 0;
161 sme->sme_ranges[i].low <= sme->sme_ranges[i].high;
162 i++) {
163 if (sme->sme_ranges[i].units == rng->units) {
164 *rng = sme->sme_ranges[i];
165 break;
166 }
167 }
168 sysmon_envsys_release(sme);
169 break;
170 }
171
172 case ENVSYS_GTREDATA:
173 {
174 struct envsys_tre_data *tred = (void *) data;
175
176 tred->validflags = 0;
177
178 sme = sysmon_envsys_find(tred->sensor);
179 if (sme == NULL)
180 break;
181 oidx = tred->sensor;
182 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
183 if (tred->sensor < sme->sme_nsensors
184 && sme->sme_gtredata != NULL) {
185 SYSMON_ENVSYS_LOCK();
186 error = (*sme->sme_gtredata)(sme, tred);
187 SYSMON_ENVSYS_UNLOCK();
188 }
189 tred->sensor = oidx;
190 sysmon_envsys_release(sme);
191 break;
192 }
193
194 case ENVSYS_STREINFO:
195 {
196 struct envsys_basic_info *binfo = (void *) data;
197
198 sme = sysmon_envsys_find(binfo->sensor);
199 if (sme == NULL) {
200 binfo->validflags = 0;
201 break;
202 }
203 oidx = binfo->sensor;
204 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
205 if (binfo->sensor < sme->sme_nsensors
206 && sme->sme_streinfo != NULL) {
207 SYSMON_ENVSYS_LOCK();
208 error = (*sme->sme_streinfo)(sme, binfo);
209 SYSMON_ENVSYS_UNLOCK();
210 } else
211 binfo->validflags = 0;
212 binfo->sensor = oidx;
213 sysmon_envsys_release(sme);
214 break;
215 }
216
217 case ENVSYS_GTREINFO:
218 {
219 struct envsys_basic_info *binfo = (void *) data;
220
221 binfo->validflags = 0;
222
223 sme = sysmon_envsys_find(binfo->sensor);
224 if (sme == NULL)
225 break;
226 oidx = binfo->sensor;
227 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
228 if (binfo->sensor < sme->sme_nsensors)
229 *binfo = sme->sme_sensor_info[binfo->sensor];
230 binfo->sensor = oidx;
231 sysmon_envsys_release(sme);
232 break;
233 }
234
235 default:
236 error = ENOTTY;
237 }
238
239 return (error);
240 }
241
242 /*
243 * sysmon_envsys_register:
244 *
245 * Register an ENVSYS device.
246 */
247 int
248 sysmon_envsys_register(struct sysmon_envsys *sme)
249 {
250 int error = 0;
251
252 mutex_init(&sysmon_envsys_list_mtx, MUTEX_DRIVER, IPL_NONE);
253 mutex_init(&sysmon_envsys_initialized_mtx, MUTEX_DRIVER, IPL_NONE);
254 cv_init(&sysmon_envsys_cv, "smestate");
255
256 KASSERT((sme->sme_flags & (SME_FLAG_BUSY | SME_FLAG_WANTED)) == 0);
257 mutex_enter(&sysmon_envsys_list_mtx);
258
259 if (sme->sme_envsys_version != SYSMON_ENVSYS_VERSION) {
260 error = EINVAL;
261 goto out;
262 }
263
264 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
265 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
266 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
267
268 out:
269 mutex_exit(&sysmon_envsys_list_mtx);
270 return (error);
271 }
272
273 /*
274 * sysmon_envsys_unregister:
275 *
276 * Unregister an ENVSYS device.
277 */
278 void
279 sysmon_envsys_unregister(struct sysmon_envsys *sme)
280 {
281
282 mutex_enter(&sysmon_envsys_list_mtx);
283 while (sme->sme_flags & SME_FLAG_BUSY) {
284 sme->sme_flags |= SME_FLAG_WANTED;
285 cv_wait(&sysmon_envsys_cv, &sysmon_envsys_list_mtx);
286 }
287 LIST_REMOVE(sme, sme_list);
288 mutex_exit(&sysmon_envsys_list_mtx);
289 }
290
291 /*
292 * sysmon_envsys_find:
293 *
294 * Find an ENVSYS device.
295 * the found device should be sysmon_envsys_release'ed by the caller.
296 */
297 struct sysmon_envsys *
298 sysmon_envsys_find(u_int idx)
299 {
300 struct sysmon_envsys *sme;
301
302 mutex_enter(&sysmon_envsys_list_mtx);
303 again:
304 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
305 sme = LIST_NEXT(sme, sme_list)) {
306 if (idx >= sme->sme_fsensor &&
307 idx < (sme->sme_fsensor + sme->sme_nsensors)) {
308 if (sme->sme_flags & SME_FLAG_BUSY) {
309 sme->sme_flags |= SME_FLAG_WANTED;
310 cv_wait(&sysmon_envsys_cv,
311 &sysmon_envsys_list_mtx);
312 goto again;
313 }
314 sme->sme_flags |= SME_FLAG_BUSY;
315 break;
316 }
317 }
318
319 mutex_exit(&sysmon_envsys_list_mtx);
320 return sme;
321 }
322
323 /*
324 * sysmon_envsys_release:
325 *
326 * Release an ENVSYS device.
327 */
328 /* ARGSUSED */
329 void
330 sysmon_envsys_release(struct sysmon_envsys *sme)
331 {
332
333 KASSERT(sme->sme_flags & SME_FLAG_BUSY);
334
335 mutex_enter(&sysmon_envsys_list_mtx);
336 if (sme->sme_flags & SME_FLAG_WANTED)
337 cv_broadcast(&sysmon_envsys_cv);
338 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
339 mutex_exit(&sysmon_envsys_list_mtx);
340 }
341