sysmon_envsys.c revision 1.14 1 1.14 xtraeme /* $NetBSD: sysmon_envsys.c,v 1.14 2007/02/18 23:38:11 xtraeme Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2000 Zembu Labs, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Author: Jason R. Thorpe <thorpej (at) zembu.com>
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed by Zembu Labs, Inc.
20 1.1 thorpej * 4. Neither the name of Zembu Labs nor the names of its employees may
21 1.1 thorpej * be used to endorse or promote products derived from this software
22 1.1 thorpej * without specific prior written permission.
23 1.1 thorpej *
24 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26 1.1 thorpej * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27 1.1 thorpej * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 thorpej */
35 1.1 thorpej
36 1.1 thorpej /*
37 1.1 thorpej * Environmental sensor framework for sysmon. Hardware monitors
38 1.1 thorpej * such as the LM78 and VIA 82C686A (or even ACPI, eventually) can
39 1.1 thorpej * register themselves to provide backplane fan and temperature
40 1.1 thorpej * information, etc.
41 1.1 thorpej */
42 1.2 lukem
43 1.2 lukem #include <sys/cdefs.h>
44 1.14 xtraeme __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.14 2007/02/18 23:38:11 xtraeme Exp $");
45 1.1 thorpej
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <sys/conf.h>
48 1.1 thorpej #include <sys/errno.h>
49 1.1 thorpej #include <sys/fcntl.h>
50 1.1 thorpej #include <sys/lock.h>
51 1.1 thorpej #include <sys/kernel.h>
52 1.1 thorpej #include <sys/systm.h>
53 1.1 thorpej #include <sys/proc.h>
54 1.14 xtraeme #include <sys/mutex.h>
55 1.14 xtraeme #include <sys/condvar.h>
56 1.1 thorpej
57 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
58 1.1 thorpej
59 1.1 thorpej /*
60 1.1 thorpej * We run at ENVSYS version 1.
61 1.1 thorpej */
62 1.1 thorpej #define SYSMON_ENVSYS_VERSION (1 * 1000)
63 1.1 thorpej
64 1.14 xtraeme kmutex_t sysmon_envsys_mtx;
65 1.1 thorpej
66 1.1 thorpej LIST_HEAD(, sysmon_envsys) sysmon_envsys_list =
67 1.1 thorpej LIST_HEAD_INITIALIZER(&sysmon_envsys_list);
68 1.14 xtraeme kmutex_t sysmon_envsys_list_mtx;
69 1.14 xtraeme kcondvar_t sysmon_envsys_cv;
70 1.1 thorpej u_int sysmon_envsys_next_sensor_index;
71 1.1 thorpej
72 1.1 thorpej int sysmon_envsys_initialized;
73 1.14 xtraeme kmutex_t sysmon_envsys_initialized_mtx;
74 1.1 thorpej
75 1.3 jdolecek #define SYSMON_ENVSYS_LOCK() \
76 1.14 xtraeme mutex_enter(&sysmon_envsys_mtx)
77 1.3 jdolecek
78 1.3 jdolecek #define SYSMON_ENVSYS_UNLOCK() \
79 1.14 xtraeme mutex_exit(&sysmon_envsys_mtx)
80 1.1 thorpej
81 1.1 thorpej struct sysmon_envsys *sysmon_envsys_find(u_int);
82 1.1 thorpej void sysmon_envsys_release(struct sysmon_envsys *);
83 1.1 thorpej
84 1.1 thorpej /*
85 1.1 thorpej * sysmonopen_envsys:
86 1.1 thorpej *
87 1.1 thorpej * Open the system monitor device.
88 1.1 thorpej */
89 1.1 thorpej int
90 1.13 christos sysmonopen_envsys(dev_t dev, int flag, int mode,
91 1.13 christos struct lwp *l)
92 1.1 thorpej {
93 1.14 xtraeme mutex_enter(&sysmon_envsys_initialized_mtx);
94 1.3 jdolecek if (sysmon_envsys_initialized == 0) {
95 1.14 xtraeme mutex_init(&sysmon_envsys_mtx, MUTEX_DRIVER, IPL_NONE);
96 1.3 jdolecek sysmon_envsys_initialized = 1;
97 1.3 jdolecek }
98 1.14 xtraeme mutex_exit(&sysmon_envsys_initialized_mtx);
99 1.1 thorpej
100 1.3 jdolecek return (0);
101 1.1 thorpej }
102 1.1 thorpej
103 1.1 thorpej /*
104 1.1 thorpej * sysmonclose_envsys:
105 1.1 thorpej *
106 1.1 thorpej * Close the system monitor device.
107 1.1 thorpej */
108 1.1 thorpej int
109 1.13 christos sysmonclose_envsys(dev_t dev, int flag, int mode,
110 1.13 christos struct lwp *l)
111 1.1 thorpej {
112 1.1 thorpej
113 1.3 jdolecek /* Nothing to do */
114 1.1 thorpej return (0);
115 1.1 thorpej }
116 1.1 thorpej
117 1.1 thorpej /*
118 1.1 thorpej * sysmonioctl_envsys:
119 1.1 thorpej *
120 1.1 thorpej * Perform an envsys control request.
121 1.1 thorpej */
122 1.1 thorpej int
123 1.13 christos sysmonioctl_envsys(dev_t dev, u_long cmd, caddr_t data,
124 1.13 christos int flag, struct lwp *l)
125 1.1 thorpej {
126 1.1 thorpej struct sysmon_envsys *sme;
127 1.1 thorpej int error = 0;
128 1.1 thorpej u_int oidx;
129 1.1 thorpej
130 1.1 thorpej switch (cmd) {
131 1.1 thorpej /*
132 1.1 thorpej * For ENVSYS commands, we translate the absolute sensor index
133 1.1 thorpej * to a device-relative sensor index.
134 1.1 thorpej */
135 1.1 thorpej case ENVSYS_VERSION:
136 1.1 thorpej *(int32_t *)data = SYSMON_ENVSYS_VERSION;
137 1.1 thorpej break;
138 1.1 thorpej
139 1.1 thorpej case ENVSYS_GRANGE:
140 1.1 thorpej {
141 1.1 thorpej struct envsys_range *rng = (void *) data;
142 1.4 explorer int i;
143 1.4 explorer
144 1.4 explorer
145 1.4 explorer /* Return empty range unless we find something better */
146 1.4 explorer rng->low = 1;
147 1.4 explorer rng->high = 0;
148 1.4 explorer
149 1.4 explorer if (rng->units == -1) {
150 1.4 explorer rng->low = 0;
151 1.4 explorer rng->high = sysmon_envsys_next_sensor_index;
152 1.4 explorer break;
153 1.4 explorer }
154 1.1 thorpej
155 1.1 thorpej sme = sysmon_envsys_find(0); /* XXX */
156 1.1 thorpej if (sme == NULL) {
157 1.1 thorpej /* Return empty range for `no sensors'. */
158 1.1 thorpej break;
159 1.1 thorpej }
160 1.4 explorer for (i = 0;
161 1.4 explorer sme->sme_ranges[i].low <= sme->sme_ranges[i].high;
162 1.4 explorer i++) {
163 1.4 explorer if (sme->sme_ranges[i].units == rng->units) {
164 1.4 explorer *rng = sme->sme_ranges[i];
165 1.4 explorer break;
166 1.4 explorer }
167 1.1 thorpej }
168 1.1 thorpej sysmon_envsys_release(sme);
169 1.1 thorpej break;
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej case ENVSYS_GTREDATA:
173 1.1 thorpej {
174 1.1 thorpej struct envsys_tre_data *tred = (void *) data;
175 1.1 thorpej
176 1.1 thorpej tred->validflags = 0;
177 1.1 thorpej
178 1.1 thorpej sme = sysmon_envsys_find(tred->sensor);
179 1.1 thorpej if (sme == NULL)
180 1.1 thorpej break;
181 1.1 thorpej oidx = tred->sensor;
182 1.1 thorpej tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
183 1.10 lukem if (tred->sensor < sme->sme_nsensors
184 1.10 lukem && sme->sme_gtredata != NULL) {
185 1.3 jdolecek SYSMON_ENVSYS_LOCK();
186 1.1 thorpej error = (*sme->sme_gtredata)(sme, tred);
187 1.3 jdolecek SYSMON_ENVSYS_UNLOCK();
188 1.3 jdolecek }
189 1.1 thorpej tred->sensor = oidx;
190 1.1 thorpej sysmon_envsys_release(sme);
191 1.1 thorpej break;
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej case ENVSYS_STREINFO:
195 1.1 thorpej {
196 1.1 thorpej struct envsys_basic_info *binfo = (void *) data;
197 1.1 thorpej
198 1.1 thorpej sme = sysmon_envsys_find(binfo->sensor);
199 1.1 thorpej if (sme == NULL) {
200 1.1 thorpej binfo->validflags = 0;
201 1.1 thorpej break;
202 1.1 thorpej }
203 1.1 thorpej oidx = binfo->sensor;
204 1.1 thorpej binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
205 1.10 lukem if (binfo->sensor < sme->sme_nsensors
206 1.10 lukem && sme->sme_streinfo != NULL) {
207 1.3 jdolecek SYSMON_ENVSYS_LOCK();
208 1.1 thorpej error = (*sme->sme_streinfo)(sme, binfo);
209 1.3 jdolecek SYSMON_ENVSYS_UNLOCK();
210 1.3 jdolecek } else
211 1.1 thorpej binfo->validflags = 0;
212 1.1 thorpej binfo->sensor = oidx;
213 1.1 thorpej sysmon_envsys_release(sme);
214 1.1 thorpej break;
215 1.1 thorpej }
216 1.1 thorpej
217 1.1 thorpej case ENVSYS_GTREINFO:
218 1.1 thorpej {
219 1.1 thorpej struct envsys_basic_info *binfo = (void *) data;
220 1.1 thorpej
221 1.1 thorpej binfo->validflags = 0;
222 1.1 thorpej
223 1.1 thorpej sme = sysmon_envsys_find(binfo->sensor);
224 1.1 thorpej if (sme == NULL)
225 1.1 thorpej break;
226 1.1 thorpej oidx = binfo->sensor;
227 1.1 thorpej binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
228 1.1 thorpej if (binfo->sensor < sme->sme_nsensors)
229 1.1 thorpej *binfo = sme->sme_sensor_info[binfo->sensor];
230 1.1 thorpej binfo->sensor = oidx;
231 1.1 thorpej sysmon_envsys_release(sme);
232 1.1 thorpej break;
233 1.1 thorpej }
234 1.1 thorpej
235 1.1 thorpej default:
236 1.1 thorpej error = ENOTTY;
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej return (error);
240 1.1 thorpej }
241 1.1 thorpej
242 1.1 thorpej /*
243 1.1 thorpej * sysmon_envsys_register:
244 1.1 thorpej *
245 1.1 thorpej * Register an ENVSYS device.
246 1.1 thorpej */
247 1.1 thorpej int
248 1.1 thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
249 1.1 thorpej {
250 1.1 thorpej int error = 0;
251 1.1 thorpej
252 1.14 xtraeme mutex_init(&sysmon_envsys_list_mtx, MUTEX_DRIVER, IPL_NONE);
253 1.14 xtraeme mutex_init(&sysmon_envsys_initialized_mtx, MUTEX_DRIVER, IPL_NONE);
254 1.14 xtraeme cv_init(&sysmon_envsys_cv, "smestate");
255 1.14 xtraeme
256 1.8 yamt KASSERT((sme->sme_flags & (SME_FLAG_BUSY | SME_FLAG_WANTED)) == 0);
257 1.14 xtraeme mutex_enter(&sysmon_envsys_list_mtx);
258 1.1 thorpej
259 1.1 thorpej if (sme->sme_envsys_version != SYSMON_ENVSYS_VERSION) {
260 1.1 thorpej error = EINVAL;
261 1.1 thorpej goto out;
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej sme->sme_fsensor = sysmon_envsys_next_sensor_index;
265 1.1 thorpej sysmon_envsys_next_sensor_index += sme->sme_nsensors;
266 1.1 thorpej LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
267 1.1 thorpej
268 1.1 thorpej out:
269 1.14 xtraeme mutex_exit(&sysmon_envsys_list_mtx);
270 1.1 thorpej return (error);
271 1.1 thorpej }
272 1.1 thorpej
273 1.1 thorpej /*
274 1.1 thorpej * sysmon_envsys_unregister:
275 1.1 thorpej *
276 1.1 thorpej * Unregister an ENVSYS device.
277 1.1 thorpej */
278 1.1 thorpej void
279 1.1 thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
280 1.1 thorpej {
281 1.1 thorpej
282 1.14 xtraeme mutex_enter(&sysmon_envsys_list_mtx);
283 1.7 yamt while (sme->sme_flags & SME_FLAG_BUSY) {
284 1.8 yamt sme->sme_flags |= SME_FLAG_WANTED;
285 1.14 xtraeme cv_wait(&sysmon_envsys_cv, &sysmon_envsys_list_mtx);
286 1.7 yamt }
287 1.1 thorpej LIST_REMOVE(sme, sme_list);
288 1.14 xtraeme mutex_exit(&sysmon_envsys_list_mtx);
289 1.1 thorpej }
290 1.1 thorpej
291 1.1 thorpej /*
292 1.1 thorpej * sysmon_envsys_find:
293 1.1 thorpej *
294 1.7 yamt * Find an ENVSYS device.
295 1.7 yamt * the found device should be sysmon_envsys_release'ed by the caller.
296 1.1 thorpej */
297 1.1 thorpej struct sysmon_envsys *
298 1.1 thorpej sysmon_envsys_find(u_int idx)
299 1.1 thorpej {
300 1.1 thorpej struct sysmon_envsys *sme;
301 1.1 thorpej
302 1.14 xtraeme mutex_enter(&sysmon_envsys_list_mtx);
303 1.7 yamt again:
304 1.1 thorpej for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
305 1.1 thorpej sme = LIST_NEXT(sme, sme_list)) {
306 1.1 thorpej if (idx >= sme->sme_fsensor &&
307 1.7 yamt idx < (sme->sme_fsensor + sme->sme_nsensors)) {
308 1.7 yamt if (sme->sme_flags & SME_FLAG_BUSY) {
309 1.8 yamt sme->sme_flags |= SME_FLAG_WANTED;
310 1.14 xtraeme cv_wait(&sysmon_envsys_cv,
311 1.14 xtraeme &sysmon_envsys_list_mtx);
312 1.7 yamt goto again;
313 1.7 yamt }
314 1.7 yamt sme->sme_flags |= SME_FLAG_BUSY;
315 1.7 yamt break;
316 1.7 yamt }
317 1.1 thorpej }
318 1.1 thorpej
319 1.14 xtraeme mutex_exit(&sysmon_envsys_list_mtx);
320 1.7 yamt return sme;
321 1.1 thorpej }
322 1.1 thorpej
323 1.1 thorpej /*
324 1.1 thorpej * sysmon_envsys_release:
325 1.1 thorpej *
326 1.1 thorpej * Release an ENVSYS device.
327 1.1 thorpej */
328 1.1 thorpej /* ARGSUSED */
329 1.1 thorpej void
330 1.1 thorpej sysmon_envsys_release(struct sysmon_envsys *sme)
331 1.1 thorpej {
332 1.1 thorpej
333 1.7 yamt KASSERT(sme->sme_flags & SME_FLAG_BUSY);
334 1.7 yamt
335 1.14 xtraeme mutex_enter(&sysmon_envsys_list_mtx);
336 1.8 yamt if (sme->sme_flags & SME_FLAG_WANTED)
337 1.14 xtraeme cv_broadcast(&sysmon_envsys_cv);
338 1.8 yamt sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
339 1.14 xtraeme mutex_exit(&sysmon_envsys_list_mtx);
340 1.1 thorpej }
341