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