sysmon_envsys.c revision 1.17.4.1 1 1.17.4.1 mjf /* $NetBSD: sysmon_envsys.c,v 1.17.4.1 2007/07/11 20:08:24 mjf Exp $ */
2 1.17.4.1 mjf
3 1.17.4.1 mjf /*-
4 1.17.4.1 mjf * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.17.4.1 mjf * All rights reserved.
6 1.17.4.1 mjf *
7 1.17.4.1 mjf * This code is derived from software contributed to The NetBSD Foundation
8 1.17.4.1 mjf * by Juan Romero Pardines.
9 1.17.4.1 mjf *
10 1.17.4.1 mjf * Redistribution and use in source and binary forms, with or without
11 1.17.4.1 mjf * modification, are permitted provided that the following conditions
12 1.17.4.1 mjf * are met:
13 1.17.4.1 mjf * 1. Redistributions of source code must retain the above copyright
14 1.17.4.1 mjf * notice, this list of conditions and the following disclaimer.
15 1.17.4.1 mjf * 2. Redistributions in binary form must reproduce the above copyright
16 1.17.4.1 mjf * notice, this list of conditions and the following disclaimer in the
17 1.17.4.1 mjf * documentation and/or other materials provided with the distribution.
18 1.17.4.1 mjf * 3. All advertising materials mentioning features or use of this software
19 1.17.4.1 mjf * must display the following acknowledgement:
20 1.17.4.1 mjf * This product includes software developed by Juan Romero Pardines
21 1.17.4.1 mjf * for the NetBSD Foundation, Inc. and its contributors.
22 1.17.4.1 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.17.4.1 mjf * contributors may be used to endorse or promote products derived
24 1.17.4.1 mjf * from this software without specific prior written permission.
25 1.17.4.1 mjf *
26 1.17.4.1 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.17.4.1 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.17.4.1 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.17.4.1 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.17.4.1 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.17.4.1 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.17.4.1 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.17.4.1 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.17.4.1 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.17.4.1 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.17.4.1 mjf * POSSIBILITY OF SUCH DAMAGE.
37 1.17.4.1 mjf */
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.17.4.1 mjf * Environmental sensor framework for sysmon, exported to userland
74 1.17.4.1 mjf * with proplib(3).
75 1.1 thorpej */
76 1.2 lukem
77 1.2 lukem #include <sys/cdefs.h>
78 1.17.4.1 mjf __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.17.4.1 2007/07/11 20:08:24 mjf Exp $");
79 1.1 thorpej
80 1.1 thorpej #include <sys/param.h>
81 1.17.4.1 mjf #include <sys/types.h>
82 1.1 thorpej #include <sys/conf.h>
83 1.1 thorpej #include <sys/errno.h>
84 1.1 thorpej #include <sys/kernel.h>
85 1.1 thorpej #include <sys/systm.h>
86 1.1 thorpej #include <sys/proc.h>
87 1.17.4.1 mjf #include <sys/mutex.h>
88 1.17.4.1 mjf #include <sys/kmem.h>
89 1.1 thorpej
90 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
91 1.17.4.1 mjf #include <dev/sysmon/sysmon_envsysvar.h>
92 1.17.4.1 mjf #include <dev/sysmon/sysmon_taskq.h>
93 1.1 thorpej
94 1.17.4.1 mjf #include "opt_compat_netbsd.h"
95 1.1 thorpej
96 1.17.4.1 mjf struct sme_sensor_type {
97 1.17.4.1 mjf int type;
98 1.17.4.1 mjf int crittype;
99 1.17.4.1 mjf const char *desc;
100 1.17.4.1 mjf };
101 1.17.4.1 mjf
102 1.17.4.1 mjf static const struct sme_sensor_type sme_sensor_type[] = {
103 1.17.4.1 mjf { ENVSYS_STEMP, PENVSYS_TYPE_TEMP, "Temperature" },
104 1.17.4.1 mjf { ENVSYS_SFANRPM, PENVSYS_TYPE_FAN, "Fan" },
105 1.17.4.1 mjf { ENVSYS_SVOLTS_AC, PENVSYS_TYPE_VOLTAGE, "Voltage AC" },
106 1.17.4.1 mjf { ENVSYS_SVOLTS_DC, PENVSYS_TYPE_VOLTAGE, "Voltage DC" },
107 1.17.4.1 mjf { ENVSYS_SOHMS, PENVSYS_TYPE_RESISTANCE,"Ohms" },
108 1.17.4.1 mjf { ENVSYS_SWATTS, PENVSYS_TYPE_POWER, "Watts" },
109 1.17.4.1 mjf { ENVSYS_SAMPS, PENVSYS_TYPE_POWER, "Ampere" },
110 1.17.4.1 mjf { ENVSYS_SWATTHOUR, PENVSYS_TYPE_BATTERY, "Watt hour" },
111 1.17.4.1 mjf { ENVSYS_SAMPHOUR, PENVSYS_TYPE_BATTERY, "Ampere hour" },
112 1.17.4.1 mjf { ENVSYS_INDICATOR, PENVSYS_TYPE_INDICATOR, "Indicator" },
113 1.17.4.1 mjf { ENVSYS_INTEGER, -1, "Integer" },
114 1.17.4.1 mjf { ENVSYS_DRIVE, PENVSYS_TYPE_DRIVE, "Drive" },
115 1.17.4.1 mjf { -1, -1, NULL }
116 1.17.4.1 mjf };
117 1.17.4.1 mjf
118 1.17.4.1 mjf struct sme_sensor_state {
119 1.17.4.1 mjf int type;
120 1.17.4.1 mjf const char *desc;
121 1.17.4.1 mjf };
122 1.17.4.1 mjf
123 1.17.4.1 mjf static const struct sme_sensor_state sme_sensor_state[] = {
124 1.17.4.1 mjf { ENVSYS_SVALID, "valid" },
125 1.17.4.1 mjf { ENVSYS_SINVALID, "invalid" },
126 1.17.4.1 mjf { ENVSYS_SCRITICAL, "critical" },
127 1.17.4.1 mjf { ENVSYS_SCRITUNDER, "critical-under" },
128 1.17.4.1 mjf { ENVSYS_SCRITOVER, "critical-over" },
129 1.17.4.1 mjf { ENVSYS_SWARNUNDER, "warning-under" },
130 1.17.4.1 mjf { ENVSYS_SWARNOVER, "warning-over" },
131 1.17.4.1 mjf { -1, "unknown" }
132 1.17.4.1 mjf };
133 1.17.4.1 mjf
134 1.17.4.1 mjf static const struct sme_sensor_state sme_sensor_drive_state[] = {
135 1.17.4.1 mjf { ENVSYS_DRIVE_EMPTY, "drive state is unknown" },
136 1.17.4.1 mjf { ENVSYS_DRIVE_READY, "drive is ready" },
137 1.17.4.1 mjf { ENVSYS_DRIVE_POWERUP, "drive is powering up" },
138 1.17.4.1 mjf { ENVSYS_DRIVE_ONLINE, "drive is online" },
139 1.17.4.1 mjf { ENVSYS_DRIVE_IDLE, "drive is idle" },
140 1.17.4.1 mjf { ENVSYS_DRIVE_ACTIVE, "drive is active" },
141 1.17.4.1 mjf { ENVSYS_DRIVE_REBUILD, "drive is rebuilding" },
142 1.17.4.1 mjf { ENVSYS_DRIVE_POWERDOWN, "drive is powering down" },
143 1.17.4.1 mjf { ENVSYS_DRIVE_FAIL, "drive failed" },
144 1.17.4.1 mjf { ENVSYS_DRIVE_PFAIL, "drive degraded" },
145 1.17.4.1 mjf { -1, "unknown" }
146 1.17.4.1 mjf };
147 1.17.4.1 mjf
148 1.17.4.1 mjf static prop_dictionary_t sme_propd;
149 1.17.4.1 mjf static kmutex_t sme_list_mtx;
150 1.17.4.1 mjf static kcondvar_t sme_list_cv;
151 1.17.4.1 mjf
152 1.17.4.1 mjf #ifdef COMPAT_40
153 1.17.4.1 mjf static u_int sysmon_envsys_next_sensor_index = 0;
154 1.17.4.1 mjf static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
155 1.17.4.1 mjf #endif
156 1.16 ad
157 1.17.4.1 mjf static void sysmon_envsys_release(struct sysmon_envsys *);
158 1.16 ad
159 1.17.4.1 mjf kmutex_t sme_mtx, sme_event_mtx;
160 1.1 thorpej
161 1.17.4.1 mjf /*
162 1.17.4.1 mjf * sysmon_envsys_init:
163 1.17.4.1 mjf *
164 1.17.4.1 mjf * + Initialize global mutexes, dictionary and the linked lists.
165 1.17.4.1 mjf */
166 1.17.4.1 mjf void
167 1.17.4.1 mjf sysmon_envsys_init(void)
168 1.17.4.1 mjf {
169 1.17.4.1 mjf LIST_INIT(&sysmon_envsys_list);
170 1.17.4.1 mjf LIST_INIT(&sme_events_list);
171 1.17.4.1 mjf mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
172 1.17.4.1 mjf mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
173 1.17.4.1 mjf mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
174 1.17.4.1 mjf mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
175 1.17.4.1 mjf cv_init(&sme_list_cv, "smefind");
176 1.17.4.1 mjf sme_propd = prop_dictionary_create();
177 1.17.4.1 mjf }
178 1.1 thorpej
179 1.1 thorpej /*
180 1.1 thorpej * sysmonopen_envsys:
181 1.1 thorpej *
182 1.17.4.1 mjf * + Open the system monitor device.
183 1.1 thorpej */
184 1.1 thorpej int
185 1.17.4.1 mjf sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
186 1.1 thorpej {
187 1.17.4.1 mjf return 0;
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej /*
191 1.1 thorpej * sysmonclose_envsys:
192 1.1 thorpej *
193 1.17.4.1 mjf * + Close the system monitor device.
194 1.1 thorpej */
195 1.1 thorpej int
196 1.17.4.1 mjf sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
197 1.1 thorpej {
198 1.3 jdolecek /* Nothing to do */
199 1.17.4.1 mjf return 0;
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * sysmonioctl_envsys:
204 1.1 thorpej *
205 1.17.4.1 mjf * + Perform an envsys control request.
206 1.1 thorpej */
207 1.1 thorpej int
208 1.17.4.1 mjf sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
209 1.1 thorpej {
210 1.17.4.1 mjf struct sysmon_envsys *sme = NULL;
211 1.1 thorpej int error = 0;
212 1.17.4.1 mjf #ifdef COMPAT_40
213 1.1 thorpej u_int oidx;
214 1.17.4.1 mjf #endif
215 1.1 thorpej
216 1.1 thorpej switch (cmd) {
217 1.17.4.1 mjf case ENVSYS_GETDICTIONARY:
218 1.17.4.1 mjf {
219 1.17.4.1 mjf struct plistref *plist = (struct plistref *)data;
220 1.17.4.1 mjf
221 1.17.4.1 mjf /*
222 1.17.4.1 mjf * Update all sysmon envsys devices dictionaries with
223 1.17.4.1 mjf * new data if it's different than we have currently
224 1.17.4.1 mjf * in the dictionary.
225 1.17.4.1 mjf */
226 1.17.4.1 mjf mutex_enter(&sme_mtx);
227 1.17.4.1 mjf LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
228 1.17.4.1 mjf if (sme == NULL)
229 1.17.4.1 mjf continue;
230 1.17.4.1 mjf
231 1.17.4.1 mjf error = sme_update_dictionary(sme);
232 1.17.4.1 mjf if (error) {
233 1.17.4.1 mjf DPRINTF(("%s: sme_update_dictionary, "
234 1.17.4.1 mjf "error=%d\n", __func__, error));
235 1.17.4.1 mjf mutex_exit(&sme_mtx);
236 1.17.4.1 mjf return error;
237 1.17.4.1 mjf }
238 1.17.4.1 mjf }
239 1.17.4.1 mjf mutex_exit(&sme_mtx);
240 1.17.4.1 mjf /*
241 1.17.4.1 mjf * Copy global dictionary to userland.
242 1.17.4.1 mjf */
243 1.17.4.1 mjf error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
244 1.1 thorpej break;
245 1.17.4.1 mjf }
246 1.17.4.1 mjf case ENVSYS_SETDICTIONARY:
247 1.1 thorpej {
248 1.17.4.1 mjf const struct plistref *plist = (const struct plistref *)data;
249 1.17.4.1 mjf prop_dictionary_t udict;
250 1.17.4.1 mjf prop_object_t obj;
251 1.17.4.1 mjf const char *devname = NULL;
252 1.17.4.1 mjf
253 1.17.4.1 mjf /*
254 1.17.4.1 mjf * Get dictionary from userland.
255 1.17.4.1 mjf */
256 1.17.4.1 mjf error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
257 1.17.4.1 mjf DPRINTF(("%s: copyin_ioctl error=%d\n", __func__, error));
258 1.17.4.1 mjf if (error)
259 1.17.4.1 mjf break;
260 1.4 explorer
261 1.17.4.1 mjf /*
262 1.17.4.1 mjf * Parse "driver-name" key to obtain the driver we
263 1.17.4.1 mjf * are searching for.
264 1.17.4.1 mjf */
265 1.17.4.1 mjf obj = prop_dictionary_get(udict, "driver-name");
266 1.17.4.1 mjf if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
267 1.17.4.1 mjf DPRINTF(("%s: driver-name failed\n", __func__));
268 1.17.4.1 mjf prop_object_release(udict);
269 1.17.4.1 mjf error = EINVAL;
270 1.4 explorer break;
271 1.4 explorer }
272 1.1 thorpej
273 1.17.4.1 mjf /* driver name */
274 1.17.4.1 mjf devname = prop_string_cstring_nocopy(obj);
275 1.17.4.1 mjf
276 1.17.4.1 mjf /* find the correct sme device */
277 1.17.4.1 mjf sme = sysmon_envsys_find(devname);
278 1.1 thorpej if (sme == NULL) {
279 1.17.4.1 mjf DPRINTF(("%s: NULL sme\n", __func__));
280 1.17.4.1 mjf prop_object_release(udict);
281 1.17.4.1 mjf error = EINVAL;
282 1.1 thorpej break;
283 1.1 thorpej }
284 1.17.4.1 mjf
285 1.17.4.1 mjf /*
286 1.17.4.1 mjf * Find the correct array object with the string supplied
287 1.17.4.1 mjf * by the userland dictionary.
288 1.17.4.1 mjf */
289 1.17.4.1 mjf obj = prop_dictionary_get(sme_propd, devname);
290 1.17.4.1 mjf if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
291 1.17.4.1 mjf DPRINTF(("%s: array device failed\n", __func__));
292 1.17.4.1 mjf prop_object_release(udict);
293 1.17.4.1 mjf error = EINVAL;
294 1.17.4.1 mjf break;
295 1.1 thorpej }
296 1.17.4.1 mjf
297 1.17.4.1 mjf /* do the real work now */
298 1.17.4.1 mjf error = sme_userset_dictionary(sme, udict, obj);
299 1.1 thorpej sysmon_envsys_release(sme);
300 1.1 thorpej break;
301 1.1 thorpej }
302 1.17.4.1 mjf /*
303 1.17.4.1 mjf * Compatibility functions with the old interface, only
304 1.17.4.1 mjf * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
305 1.17.4.1 mjf * to make old applications work.
306 1.17.4.1 mjf */
307 1.17.4.1 mjf #ifdef COMPAT_40
308 1.1 thorpej case ENVSYS_GTREDATA:
309 1.1 thorpej {
310 1.17.4.1 mjf struct envsys_tre_data *tred = (void *)data;
311 1.17.4.1 mjf envsys_data_t *edata = NULL;
312 1.1 thorpej
313 1.1 thorpej tred->validflags = 0;
314 1.1 thorpej
315 1.17.4.1 mjf sme = sysmon_envsys_find_40(tred->sensor);
316 1.1 thorpej if (sme == NULL)
317 1.1 thorpej break;
318 1.17.4.1 mjf
319 1.17.4.1 mjf mutex_enter(&sme_mtx);
320 1.1 thorpej oidx = tred->sensor;
321 1.1 thorpej tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
322 1.1 thorpej
323 1.17.4.1 mjf DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
324 1.17.4.1 mjf __func__, tred->sensor, oidx, sme->sme_name,
325 1.17.4.1 mjf sme->sme_nsensors));
326 1.17.4.1 mjf
327 1.17.4.1 mjf edata = &sme->sme_sensor_data[tred->sensor];
328 1.17.4.1 mjf
329 1.17.4.1 mjf if (tred->sensor < sme->sme_nsensors) {
330 1.17.4.1 mjf if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
331 1.17.4.1 mjf error = (*sme->sme_gtredata)(sme, edata);
332 1.17.4.1 mjf if (error) {
333 1.17.4.1 mjf DPRINTF(("%s: sme_gtredata failed\n",
334 1.17.4.1 mjf __func__));
335 1.17.4.1 mjf mutex_exit(&sme_mtx);
336 1.17.4.1 mjf return error;
337 1.17.4.1 mjf }
338 1.17.4.1 mjf }
339 1.1 thorpej
340 1.17.4.1 mjf /* copy required values to the old interface */
341 1.17.4.1 mjf tred->sensor = edata->sensor;
342 1.17.4.1 mjf tred->cur.data_us = edata->value_cur;
343 1.17.4.1 mjf tred->cur.data_s = edata->value_cur;
344 1.17.4.1 mjf tred->max.data_us = edata->value_max;
345 1.17.4.1 mjf tred->max.data_s = edata->value_max;
346 1.17.4.1 mjf tred->min.data_us = edata->value_min;
347 1.17.4.1 mjf tred->min.data_s = edata->value_min;
348 1.17.4.1 mjf tred->avg.data_us = edata->value_avg;
349 1.17.4.1 mjf tred->avg.data_s = edata->value_avg;
350 1.17.4.1 mjf tred->units = edata->units;
351 1.17.4.1 mjf
352 1.17.4.1 mjf tred->validflags |= ENVSYS_FVALID;
353 1.17.4.1 mjf tred->validflags |= ENVSYS_FCURVALID;
354 1.17.4.1 mjf
355 1.17.4.1 mjf if (edata->flags & ENVSYS_FPERCENT) {
356 1.17.4.1 mjf tred->validflags |= ENVSYS_FMAXVALID;
357 1.17.4.1 mjf tred->validflags |= ENVSYS_FFRACVALID;
358 1.17.4.1 mjf }
359 1.17.4.1 mjf
360 1.17.4.1 mjf if (edata->state == ENVSYS_SINVALID) {
361 1.17.4.1 mjf tred->validflags &= ~ENVSYS_FCURVALID;
362 1.17.4.1 mjf tred->cur.data_us = tred->cur.data_s = 0;
363 1.17.4.1 mjf }
364 1.17.4.1 mjf
365 1.17.4.1 mjf DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
366 1.17.4.1 mjf __func__, edata->desc, tred->cur.data_s));
367 1.17.4.1 mjf DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
368 1.17.4.1 mjf " tred->sensor=%d\n", __func__, tred->validflags,
369 1.17.4.1 mjf tred->units, tred->sensor));
370 1.1 thorpej }
371 1.17.4.1 mjf tred->sensor = oidx;
372 1.17.4.1 mjf mutex_exit(&sme_mtx);
373 1.17.4.1 mjf
374 1.1 thorpej break;
375 1.1 thorpej }
376 1.1 thorpej case ENVSYS_GTREINFO:
377 1.1 thorpej {
378 1.17.4.1 mjf struct envsys_basic_info *binfo = (void *)data;
379 1.17.4.1 mjf envsys_data_t *edata = NULL;
380 1.1 thorpej
381 1.1 thorpej binfo->validflags = 0;
382 1.1 thorpej
383 1.17.4.1 mjf sme = sysmon_envsys_find_40(binfo->sensor);
384 1.1 thorpej if (sme == NULL)
385 1.1 thorpej break;
386 1.17.4.1 mjf
387 1.17.4.1 mjf mutex_enter(&sme_mtx);
388 1.1 thorpej oidx = binfo->sensor;
389 1.1 thorpej binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
390 1.17.4.1 mjf
391 1.17.4.1 mjf edata = &sme->sme_sensor_data[binfo->sensor];
392 1.17.4.1 mjf
393 1.17.4.1 mjf binfo->validflags |= ENVSYS_FVALID;
394 1.17.4.1 mjf
395 1.17.4.1 mjf if (binfo->sensor < sme->sme_nsensors) {
396 1.17.4.1 mjf binfo->units = edata->units;
397 1.17.4.1 mjf (void)strlcpy(binfo->desc, edata->desc,
398 1.17.4.1 mjf sizeof(binfo->desc));
399 1.17.4.1 mjf }
400 1.17.4.1 mjf
401 1.17.4.1 mjf DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
402 1.17.4.1 mjf __func__, binfo->units, binfo->validflags));
403 1.17.4.1 mjf DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
404 1.17.4.1 mjf __func__, binfo->desc, binfo->sensor));
405 1.17.4.1 mjf
406 1.1 thorpej binfo->sensor = oidx;
407 1.17.4.1 mjf mutex_exit(&sme_mtx);
408 1.17.4.1 mjf
409 1.1 thorpej break;
410 1.1 thorpej }
411 1.17.4.1 mjf #endif /* COMPAT_40 */
412 1.1 thorpej default:
413 1.1 thorpej error = ENOTTY;
414 1.17.4.1 mjf break;
415 1.1 thorpej }
416 1.1 thorpej
417 1.17.4.1 mjf return error;
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej /*
421 1.1 thorpej * sysmon_envsys_register:
422 1.1 thorpej *
423 1.17.4.1 mjf * + Register an envsys device.
424 1.17.4.1 mjf * + Create device dictionary.
425 1.1 thorpej */
426 1.1 thorpej int
427 1.1 thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
428 1.1 thorpej {
429 1.17.4.1 mjf struct sysmon_envsys *lsme;
430 1.1 thorpej int error = 0;
431 1.1 thorpej
432 1.17.4.1 mjf /*
433 1.17.4.1 mjf * sanity check 1, make sure the driver has initialized
434 1.17.4.1 mjf * the sensors count or the value is not too high.
435 1.17.4.1 mjf */
436 1.17.4.1 mjf if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
437 1.17.4.1 mjf return EINVAL;
438 1.1 thorpej
439 1.17.4.1 mjf /*
440 1.17.4.1 mjf * sanity check 2, make sure that sme->sme_name and
441 1.17.4.1 mjf * sme->sme_data are not NULL.
442 1.17.4.1 mjf */
443 1.17.4.1 mjf if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
444 1.17.4.1 mjf return EINVAL;
445 1.17.4.1 mjf
446 1.17.4.1 mjf /*
447 1.17.4.1 mjf * sanity check 3, if SME_DISABLE_GTREDATA is not set
448 1.17.4.1 mjf * the sme_gtredata function callback must be non NULL.
449 1.17.4.1 mjf */
450 1.17.4.1 mjf if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
451 1.17.4.1 mjf if (sme->sme_gtredata == NULL)
452 1.17.4.1 mjf return EINVAL;
453 1.1 thorpej }
454 1.1 thorpej
455 1.17.4.1 mjf /*
456 1.17.4.1 mjf * - check if requested sysmon_envsys device is valid
457 1.17.4.1 mjf * and does not exist already in the list.
458 1.17.4.1 mjf * - add device into the list.
459 1.17.4.1 mjf * - create the plist structure.
460 1.17.4.1 mjf */
461 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
462 1.17.4.1 mjf LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
463 1.17.4.1 mjf if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
464 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
465 1.17.4.1 mjf error = EEXIST;
466 1.17.4.1 mjf goto out;
467 1.17.4.1 mjf }
468 1.17.4.1 mjf }
469 1.17.4.1 mjf #ifdef COMPAT_40
470 1.1 thorpej sme->sme_fsensor = sysmon_envsys_next_sensor_index;
471 1.1 thorpej sysmon_envsys_next_sensor_index += sme->sme_nsensors;
472 1.17.4.1 mjf #endif
473 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
474 1.17.4.1 mjf error = sysmon_envsys_createplist(sme);
475 1.17.4.1 mjf if (!error) {
476 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
477 1.17.4.1 mjf LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
478 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
479 1.17.4.1 mjf }
480 1.1 thorpej
481 1.17.4.1 mjf out:
482 1.17.4.1 mjf return error;
483 1.1 thorpej }
484 1.1 thorpej
485 1.1 thorpej /*
486 1.1 thorpej * sysmon_envsys_unregister:
487 1.1 thorpej *
488 1.17.4.1 mjf * + Unregister an envsys device.
489 1.17.4.1 mjf * + Unregister all events associated with this device.
490 1.17.4.1 mjf * + Remove device dictionary.
491 1.1 thorpej */
492 1.1 thorpej void
493 1.1 thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
494 1.1 thorpej {
495 1.17.4.1 mjf sme_event_t *see;
496 1.1 thorpej
497 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
498 1.7 yamt while (sme->sme_flags & SME_FLAG_BUSY) {
499 1.8 yamt sme->sme_flags |= SME_FLAG_WANTED;
500 1.17.4.1 mjf cv_wait(&sme_list_cv, &sme_list_mtx);
501 1.17.4.1 mjf }
502 1.17.4.1 mjf prop_dictionary_remove(sme_propd, sme->sme_name);
503 1.17.4.1 mjf LIST_FOREACH(see, &sme_events_list, see_list) {
504 1.17.4.1 mjf if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
505 1.17.4.1 mjf sme_event_unregister(see->pes.pes_sensname,
506 1.17.4.1 mjf see->type);
507 1.7 yamt }
508 1.1 thorpej LIST_REMOVE(sme, sme_list);
509 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
510 1.1 thorpej }
511 1.1 thorpej
512 1.1 thorpej /*
513 1.1 thorpej * sysmon_envsys_find:
514 1.1 thorpej *
515 1.17.4.1 mjf * + Find an envsys device.
516 1.1 thorpej */
517 1.1 thorpej struct sysmon_envsys *
518 1.17.4.1 mjf sysmon_envsys_find(const char *name)
519 1.1 thorpej {
520 1.1 thorpej struct sysmon_envsys *sme;
521 1.1 thorpej
522 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
523 1.7 yamt again:
524 1.1 thorpej for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
525 1.1 thorpej sme = LIST_NEXT(sme, sme_list)) {
526 1.17.4.1 mjf if (strcmp(sme->sme_name, name) == 0) {
527 1.17.4.1 mjf if (sme->sme_flags & SME_FLAG_BUSY) {
528 1.17.4.1 mjf sme->sme_flags |= SME_FLAG_WANTED;
529 1.17.4.1 mjf cv_wait(&sme_list_cv, &sme_list_mtx);
530 1.17.4.1 mjf goto again;
531 1.17.4.1 mjf }
532 1.17.4.1 mjf sme->sme_flags |= SME_FLAG_BUSY;
533 1.17.4.1 mjf break;
534 1.7 yamt }
535 1.1 thorpej }
536 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
537 1.7 yamt return sme;
538 1.1 thorpej }
539 1.1 thorpej
540 1.1 thorpej /*
541 1.1 thorpej * sysmon_envsys_release:
542 1.1 thorpej *
543 1.17.4.1 mjf * + Release an envsys device.
544 1.1 thorpej */
545 1.1 thorpej void
546 1.1 thorpej sysmon_envsys_release(struct sysmon_envsys *sme)
547 1.1 thorpej {
548 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
549 1.8 yamt if (sme->sme_flags & SME_FLAG_WANTED)
550 1.17.4.1 mjf cv_broadcast(&sme_list_cv);
551 1.8 yamt sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
552 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
553 1.17.4.1 mjf }
554 1.17.4.1 mjf
555 1.17.4.1 mjf /* compatibility function */
556 1.17.4.1 mjf #ifdef COMPAT_40
557 1.17.4.1 mjf struct sysmon_envsys *
558 1.17.4.1 mjf sysmon_envsys_find_40(u_int idx)
559 1.17.4.1 mjf {
560 1.17.4.1 mjf struct sysmon_envsys *sme;
561 1.17.4.1 mjf
562 1.17.4.1 mjf mutex_enter(&sme_list_mtx);
563 1.17.4.1 mjf for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
564 1.17.4.1 mjf sme = LIST_NEXT(sme, sme_list)) {
565 1.17.4.1 mjf if (idx >= sme->sme_fsensor &&
566 1.17.4.1 mjf idx < (sme->sme_fsensor + sme->sme_nsensors))
567 1.17.4.1 mjf break;
568 1.17.4.1 mjf }
569 1.17.4.1 mjf mutex_exit(&sme_list_mtx);
570 1.17.4.1 mjf return sme;
571 1.17.4.1 mjf }
572 1.17.4.1 mjf #endif
573 1.17.4.1 mjf
574 1.17.4.1 mjf /*
575 1.17.4.1 mjf * sysmon_envsys_createplist:
576 1.17.4.1 mjf *
577 1.17.4.1 mjf * + Create the property list structure for a device.
578 1.17.4.1 mjf */
579 1.17.4.1 mjf int
580 1.17.4.1 mjf sysmon_envsys_createplist(struct sysmon_envsys *sme)
581 1.17.4.1 mjf {
582 1.17.4.1 mjf envsys_data_t *edata;
583 1.17.4.1 mjf prop_array_t array;
584 1.17.4.1 mjf int i, error = 0;
585 1.17.4.1 mjf
586 1.17.4.1 mjf /* create the sysmon envsys device array. */
587 1.17.4.1 mjf array = prop_array_create();
588 1.17.4.1 mjf
589 1.17.4.1 mjf /*
590 1.17.4.1 mjf * <dict>
591 1.17.4.1 mjf * <key>foo0</key>
592 1.17.4.1 mjf * <array>
593 1.17.4.1 mjf * ...
594 1.17.4.1 mjf */
595 1.17.4.1 mjf mutex_enter(&sme_mtx);
596 1.17.4.1 mjf if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
597 1.17.4.1 mjf DPRINTF(("%s: prop_dictionary_set\n", __func__));
598 1.17.4.1 mjf mutex_exit(&sme_mtx);
599 1.17.4.1 mjf error = EINVAL;
600 1.17.4.1 mjf goto out;
601 1.17.4.1 mjf }
602 1.17.4.1 mjf mutex_exit(&sme_mtx);
603 1.17.4.1 mjf
604 1.17.4.1 mjf /*
605 1.17.4.1 mjf * Iterate over all sensors and create a dictionary with all
606 1.17.4.1 mjf * values specified by the sysmon envsys driver.
607 1.17.4.1 mjf */
608 1.17.4.1 mjf for (i = 0; i < sme->sme_nsensors; i++) {
609 1.17.4.1 mjf edata = &sme->sme_sensor_data[i];
610 1.17.4.1 mjf /*
611 1.17.4.1 mjf * refresh sensor data via sme_gtredata only if the
612 1.17.4.1 mjf * flag is not set.
613 1.17.4.1 mjf */
614 1.17.4.1 mjf if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
615 1.17.4.1 mjf mutex_enter(&sme_mtx);
616 1.17.4.1 mjf error = (*sme->sme_gtredata)(sme, edata);
617 1.17.4.1 mjf if (error) {
618 1.17.4.1 mjf DPRINTF(("%s: sme->sme_gtredata[%d]\n",
619 1.17.4.1 mjf __func__, i));
620 1.17.4.1 mjf mutex_exit(&sme_mtx);
621 1.17.4.1 mjf continue;
622 1.17.4.1 mjf }
623 1.17.4.1 mjf mutex_exit(&sme_mtx);
624 1.17.4.1 mjf }
625 1.17.4.1 mjf
626 1.17.4.1 mjf error = sme_make_dictionary(sme, array, edata);
627 1.17.4.1 mjf if (error) {
628 1.17.4.1 mjf DPRINTF(("%s: sme_make_dictionary[%d]\n", __func__, i));
629 1.17.4.1 mjf goto out;
630 1.17.4.1 mjf }
631 1.17.4.1 mjf }
632 1.17.4.1 mjf
633 1.17.4.1 mjf out:
634 1.17.4.1 mjf prop_object_release(array);
635 1.17.4.1 mjf return error;
636 1.17.4.1 mjf }
637 1.17.4.1 mjf
638 1.17.4.1 mjf /*
639 1.17.4.1 mjf * sme_make_dictionary:
640 1.17.4.1 mjf *
641 1.17.4.1 mjf * + Create sensor's dictionary in device's dictionary array.
642 1.17.4.1 mjf */
643 1.17.4.1 mjf int
644 1.17.4.1 mjf sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
645 1.17.4.1 mjf envsys_data_t *edata)
646 1.17.4.1 mjf {
647 1.17.4.1 mjf const struct sme_sensor_type *est = sme_sensor_type;
648 1.17.4.1 mjf const struct sme_sensor_state *ess = sme_sensor_state;
649 1.17.4.1 mjf const struct sme_sensor_state *esds = sme_sensor_drive_state;
650 1.17.4.1 mjf sme_event_drv_t *sme_evdrv_t = NULL;
651 1.17.4.1 mjf prop_dictionary_t dict;
652 1.17.4.1 mjf int i, j, k;
653 1.17.4.1 mjf
654 1.17.4.1 mjf i = j = k = 0;
655 1.17.4.1 mjf
656 1.17.4.1 mjf /*
657 1.17.4.1 mjf * <array>
658 1.17.4.1 mjf * <dict>
659 1.17.4.1 mjf * ...
660 1.17.4.1 mjf */
661 1.17.4.1 mjf dict = prop_dictionary_create();
662 1.17.4.1 mjf
663 1.17.4.1 mjf mutex_enter(&sme_mtx);
664 1.17.4.1 mjf if (!prop_array_add(array, dict)) {
665 1.17.4.1 mjf mutex_exit(&sme_mtx);
666 1.17.4.1 mjf DPRINTF(("%s: prop_array_add\n", __func__));
667 1.17.4.1 mjf return EINVAL;
668 1.17.4.1 mjf }
669 1.17.4.1 mjf
670 1.17.4.1 mjf /* find the correct unit for this sensor. */
671 1.17.4.1 mjf for (i = 0; est[i].type != -1; i++)
672 1.17.4.1 mjf if (est[i].type == edata->units)
673 1.17.4.1 mjf break;
674 1.17.4.1 mjf
675 1.17.4.1 mjf /*
676 1.17.4.1 mjf * ...
677 1.17.4.1 mjf * <key>type</key>
678 1.17.4.1 mjf * <string>foo</string>
679 1.17.4.1 mjf * <key>description</key>
680 1.17.4.1 mjf * <string>blah blah</string>
681 1.17.4.1 mjf * ...
682 1.17.4.1 mjf */
683 1.17.4.1 mjf SENSOR_SSTRING(dict, "type", est[i].desc);
684 1.17.4.1 mjf SENSOR_SSTRING(dict, "description", edata->desc);
685 1.17.4.1 mjf
686 1.17.4.1 mjf /*
687 1.17.4.1 mjf * Add sensor's state description.
688 1.17.4.1 mjf *
689 1.17.4.1 mjf * ...
690 1.17.4.1 mjf * <key>state</key>
691 1.17.4.1 mjf * <string>valid</string>
692 1.17.4.1 mjf * ...
693 1.17.4.1 mjf */
694 1.17.4.1 mjf for (j = 0; ess[j].type != -1; j++)
695 1.17.4.1 mjf if (ess[j].type == edata->state)
696 1.17.4.1 mjf break;
697 1.17.4.1 mjf
698 1.17.4.1 mjf SENSOR_SSTRING(dict, "state", ess[j].desc);
699 1.17.4.1 mjf
700 1.17.4.1 mjf /*
701 1.17.4.1 mjf * add the percentage boolean object:
702 1.17.4.1 mjf *
703 1.17.4.1 mjf * ...
704 1.17.4.1 mjf * <key>want-percentage</key>
705 1.17.4.1 mjf * <true/>
706 1.17.4.1 mjf * ...
707 1.17.4.1 mjf */
708 1.17.4.1 mjf if (edata->flags & ENVSYS_FPERCENT)
709 1.17.4.1 mjf SENSOR_SBOOL(dict, "want-percentage", true);
710 1.17.4.1 mjf
711 1.17.4.1 mjf /*
712 1.17.4.1 mjf * Add the monitoring boolean object:
713 1.17.4.1 mjf *
714 1.17.4.1 mjf * ...
715 1.17.4.1 mjf * <key>monitoring-supported</key>
716 1.17.4.1 mjf * <true/>
717 1.17.4.1 mjf * ...
718 1.17.4.1 mjf *
719 1.17.4.1 mjf * always false on Drive, Integer and Indicator types, they
720 1.17.4.1 mjf * cannot be monitored.
721 1.17.4.1 mjf *
722 1.17.4.1 mjf */
723 1.17.4.1 mjf if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
724 1.17.4.1 mjf (edata->units == ENVSYS_INDICATOR) ||
725 1.17.4.1 mjf (edata->units == ENVSYS_INTEGER) ||
726 1.17.4.1 mjf (edata->units == ENVSYS_DRIVE)) {
727 1.17.4.1 mjf SENSOR_SBOOL(dict, "monitoring-supported", false);
728 1.17.4.1 mjf } else {
729 1.17.4.1 mjf SENSOR_SBOOL(dict, "monitoring-supported", true);
730 1.17.4.1 mjf }
731 1.17.4.1 mjf
732 1.17.4.1 mjf /*
733 1.17.4.1 mjf * Add the drive-state object for drive sensors:
734 1.17.4.1 mjf *
735 1.17.4.1 mjf * ...
736 1.17.4.1 mjf * <key>drive-state</key>
737 1.17.4.1 mjf * <string>drive is online</string>
738 1.17.4.1 mjf * ...
739 1.17.4.1 mjf */
740 1.17.4.1 mjf if (edata->units == ENVSYS_DRIVE) {
741 1.17.4.1 mjf for (k = 0; esds[k].type != -1; k++)
742 1.17.4.1 mjf if (esds[k].type == edata->value_cur)
743 1.17.4.1 mjf break;
744 1.17.4.1 mjf SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
745 1.17.4.1 mjf }
746 1.17.4.1 mjf
747 1.17.4.1 mjf mutex_exit(&sme_mtx);
748 1.17.4.1 mjf /*
749 1.17.4.1 mjf * Add a new event if a monitoring flag was set.
750 1.17.4.1 mjf */
751 1.17.4.1 mjf if (edata->monitor && edata->units != ENVSYS_INTEGER) {
752 1.17.4.1 mjf sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
753 1.17.4.1 mjf
754 1.17.4.1 mjf sme_evdrv_t->sdict = dict;
755 1.17.4.1 mjf sme_evdrv_t->edata = edata;
756 1.17.4.1 mjf sme_evdrv_t->sme = sme;
757 1.17.4.1 mjf sme_evdrv_t->powertype = est[i].crittype;
758 1.17.4.1 mjf
759 1.17.4.1 mjf sysmon_task_queue_init();
760 1.17.4.1 mjf sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
761 1.17.4.1 mjf }
762 1.17.4.1 mjf
763 1.17.4.1 mjf mutex_enter(&sme_mtx);
764 1.17.4.1 mjf /* if sensor is enabled, add the following properties... */
765 1.17.4.1 mjf if (edata->state == ENVSYS_SVALID) {
766 1.17.4.1 mjf /*
767 1.17.4.1 mjf * ...
768 1.17.4.1 mjf * <key>rpms</key>
769 1.17.4.1 mjf * <integer>2500</integer>
770 1.17.4.1 mjf * <key>rfact</key>
771 1.17.4.1 mjf * <integer>10000</integer>
772 1.17.4.1 mjf * <key>cur-value</key>
773 1.17.4.1 mjf * <integer>1250</integer>
774 1.17.4.1 mjf * <key>min-value</key>
775 1.17.4.1 mjf * <integer>800</integer>
776 1.17.4.1 mjf * <key>max-value</integer>
777 1.17.4.1 mjf * <integer>3000</integer>
778 1.17.4.1 mjf * <key>avg-value</integer>
779 1.17.4.1 mjf * <integer>1400</integer>
780 1.17.4.1 mjf * </dict>
781 1.17.4.1 mjf */
782 1.17.4.1 mjf if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
783 1.17.4.1 mjf SENSOR_SUINT32(dict, "rpms", edata->rpms);
784 1.17.4.1 mjf
785 1.17.4.1 mjf if ((edata->units == ENVSYS_SVOLTS_AC ||
786 1.17.4.1 mjf edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
787 1.17.4.1 mjf SENSOR_SINT32(dict, "rfact", edata->rfact);
788 1.17.4.1 mjf
789 1.17.4.1 mjf if (edata->value_cur)
790 1.17.4.1 mjf SENSOR_SINT32(dict, "cur-value", edata->value_cur);
791 1.17.4.1 mjf
792 1.17.4.1 mjf if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
793 1.17.4.1 mjf SENSOR_SINT32(dict, "min-value", edata->value_min);
794 1.17.4.1 mjf
795 1.17.4.1 mjf if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
796 1.17.4.1 mjf SENSOR_SINT32(dict, "max-value", edata->value_max);
797 1.17.4.1 mjf
798 1.17.4.1 mjf if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
799 1.17.4.1 mjf SENSOR_SINT32(dict, "avg-value", edata->value_avg);
800 1.17.4.1 mjf }
801 1.17.4.1 mjf
802 1.17.4.1 mjf /*
803 1.17.4.1 mjf * ...
804 1.17.4.1 mjf * </array>
805 1.17.4.1 mjf */
806 1.17.4.1 mjf out:
807 1.17.4.1 mjf mutex_exit(&sme_mtx);
808 1.17.4.1 mjf prop_object_release(dict);
809 1.17.4.1 mjf return 0;
810 1.17.4.1 mjf }
811 1.17.4.1 mjf
812 1.17.4.1 mjf /*
813 1.17.4.1 mjf * sme_update_dictionary:
814 1.17.4.1 mjf *
815 1.17.4.1 mjf * + Update per-sensor dictionaries with new values if there were
816 1.17.4.1 mjf * changes, otherwise the object in dictionary is untouched.
817 1.17.4.1 mjf * + Send a critical event if any sensor is in a critical condition.
818 1.17.4.1 mjf */
819 1.17.4.1 mjf int
820 1.17.4.1 mjf sme_update_dictionary(struct sysmon_envsys *sme)
821 1.17.4.1 mjf {
822 1.17.4.1 mjf const struct sme_sensor_state *ess = sme_sensor_state;
823 1.17.4.1 mjf const struct sme_sensor_state *esds = sme_sensor_drive_state;
824 1.17.4.1 mjf envsys_data_t *edata = NULL;
825 1.17.4.1 mjf prop_object_t array, obj, dict = NULL;
826 1.17.4.1 mjf int i, j, error = 0;
827 1.17.4.1 mjf
828 1.17.4.1 mjf /* retrieve the array of dictionaries in device. */
829 1.17.4.1 mjf array = prop_dictionary_get(sme_propd, sme->sme_name);
830 1.17.4.1 mjf if (prop_object_type(array) != PROP_TYPE_ARRAY)
831 1.17.4.1 mjf return EINVAL;
832 1.17.4.1 mjf
833 1.17.4.1 mjf /*
834 1.17.4.1 mjf * - iterate over all sensors.
835 1.17.4.1 mjf * - fetch new data.
836 1.17.4.1 mjf * - check if data in dictionary is different than new data.
837 1.17.4.1 mjf * - update dictionary if there were changes.
838 1.17.4.1 mjf */
839 1.17.4.1 mjf for (i = 0; i < sme->sme_nsensors; i++) {
840 1.17.4.1 mjf edata = &sme->sme_sensor_data[i];
841 1.17.4.1 mjf
842 1.17.4.1 mjf /*
843 1.17.4.1 mjf * refresh sensor data via sme_gtredata only if the
844 1.17.4.1 mjf * flag is not set.
845 1.17.4.1 mjf */
846 1.17.4.1 mjf if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
847 1.17.4.1 mjf error = (*sme->sme_gtredata)(sme, edata);
848 1.17.4.1 mjf if (error) {
849 1.17.4.1 mjf DPRINTF(("%s: gtredata[%d] failed\n",
850 1.17.4.1 mjf __func__, i));
851 1.17.4.1 mjf return error;
852 1.17.4.1 mjf }
853 1.17.4.1 mjf }
854 1.17.4.1 mjf
855 1.17.4.1 mjf /* retrieve sensor's dictionary. */
856 1.17.4.1 mjf dict = prop_array_get(array, i);
857 1.17.4.1 mjf if (prop_object_type(dict) != PROP_TYPE_DICTIONARY)
858 1.17.4.1 mjf return EINVAL;
859 1.17.4.1 mjf
860 1.17.4.1 mjf /* update state sensor. */
861 1.17.4.1 mjf for (j = 0; ess[j].type != -1; j++)
862 1.17.4.1 mjf if (ess[j].type == edata->state)
863 1.17.4.1 mjf break;
864 1.17.4.1 mjf
865 1.17.4.1 mjf DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
866 1.17.4.1 mjf "units=%d sensor=%d\n", __func__, ess[j].desc,
867 1.17.4.1 mjf ess[j].type, edata->flags, edata->units, edata->sensor));
868 1.17.4.1 mjf
869 1.17.4.1 mjf /* update sensor state */
870 1.17.4.1 mjf SENSOR_UPSTRING(dict, "state", ess[j].desc);
871 1.17.4.1 mjf
872 1.17.4.1 mjf /* update sensor current value */
873 1.17.4.1 mjf SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
874 1.17.4.1 mjf
875 1.17.4.1 mjf /*
876 1.17.4.1 mjf * Integer and Indicator types do not the following
877 1.17.4.1 mjf * values, so skip them.
878 1.17.4.1 mjf */
879 1.17.4.1 mjf if (edata->units == ENVSYS_INTEGER ||
880 1.17.4.1 mjf edata->units == ENVSYS_INDICATOR)
881 1.17.4.1 mjf continue;
882 1.17.4.1 mjf
883 1.17.4.1 mjf /* update sensor flags */
884 1.17.4.1 mjf if (edata->flags & ENVSYS_FPERCENT)
885 1.17.4.1 mjf SENSOR_SBOOL(dict, "want-percentage", true);
886 1.17.4.1 mjf else {
887 1.17.4.1 mjf obj = prop_dictionary_get(dict, "want-percentage");
888 1.17.4.1 mjf if (obj)
889 1.17.4.1 mjf SENSOR_SBOOL(dict, "want-percentage", false);
890 1.17.4.1 mjf }
891 1.17.4.1 mjf
892 1.17.4.1 mjf if (edata->flags & ENVSYS_FVALID_MAX)
893 1.17.4.1 mjf SENSOR_UPINT32(dict, "max-value", edata->value_max);
894 1.17.4.1 mjf
895 1.17.4.1 mjf if (edata->flags & ENVSYS_FVALID_MIN)
896 1.17.4.1 mjf SENSOR_UPINT32(dict, "min-value", edata->value_min);
897 1.17.4.1 mjf
898 1.17.4.1 mjf if (edata->flags & ENVSYS_FVALID_AVG)
899 1.17.4.1 mjf SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
900 1.17.4.1 mjf
901 1.17.4.1 mjf /* update 'rpms' only in ENVSYS_SFANRPM. */
902 1.17.4.1 mjf if (edata->units == ENVSYS_SFANRPM)
903 1.17.4.1 mjf SENSOR_UPUINT32(dict, "rpms", edata->rpms);
904 1.17.4.1 mjf
905 1.17.4.1 mjf /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
906 1.17.4.1 mjf if (edata->units == ENVSYS_SVOLTS_AC ||
907 1.17.4.1 mjf edata->units == ENVSYS_SVOLTS_DC) {
908 1.17.4.1 mjf SENSOR_UPINT32(dict, "rfact", edata->rfact);
909 1.17.4.1 mjf }
910 1.17.4.1 mjf
911 1.17.4.1 mjf /* update 'drive-state' only in ENVSYS_DRIVE. */
912 1.17.4.1 mjf if (edata->units == ENVSYS_DRIVE) {
913 1.17.4.1 mjf for (j = 0; esds[j].type != -1; j++)
914 1.17.4.1 mjf if (esds[j].type == edata->value_cur)
915 1.17.4.1 mjf break;
916 1.17.4.1 mjf
917 1.17.4.1 mjf SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
918 1.17.4.1 mjf }
919 1.17.4.1 mjf }
920 1.17.4.1 mjf
921 1.17.4.1 mjf out:
922 1.17.4.1 mjf return error;
923 1.17.4.1 mjf }
924 1.17.4.1 mjf
925 1.17.4.1 mjf /*
926 1.17.4.1 mjf * sme_userset_dictionary:
927 1.17.4.1 mjf *
928 1.17.4.1 mjf * + Parses the userland dictionary and run the appropiate
929 1.17.4.1 mjf * tasks that were requested.
930 1.17.4.1 mjf */
931 1.17.4.1 mjf int
932 1.17.4.1 mjf sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
933 1.17.4.1 mjf prop_array_t array)
934 1.17.4.1 mjf {
935 1.17.4.1 mjf const struct sme_sensor_type *sst = sme_sensor_type;
936 1.17.4.1 mjf envsys_data_t *edata;
937 1.17.4.1 mjf prop_dictionary_t dict;
938 1.17.4.1 mjf prop_object_t obj, obj1, obj2;
939 1.17.4.1 mjf int32_t critval;
940 1.17.4.1 mjf int i, error = 0;
941 1.17.4.1 mjf const char *blah, *sname;
942 1.17.4.1 mjf bool targetfound = false;
943 1.17.4.1 mjf
944 1.17.4.1 mjf blah = sname = NULL;
945 1.17.4.1 mjf
946 1.17.4.1 mjf /* get sensor's name from userland dictionary. */
947 1.17.4.1 mjf obj = prop_dictionary_get(udict, "sensor-name");
948 1.17.4.1 mjf if (prop_object_type(obj) != PROP_TYPE_STRING) {
949 1.17.4.1 mjf DPRINTF(("%s: sensor-name failed\n", __func__));
950 1.17.4.1 mjf return EINVAL;
951 1.17.4.1 mjf }
952 1.17.4.1 mjf
953 1.17.4.1 mjf /* iterate over the sensors to find the right one */
954 1.17.4.1 mjf for (i = 0; i < sme->sme_nsensors; i++) {
955 1.17.4.1 mjf edata = &sme->sme_sensor_data[i];
956 1.17.4.1 mjf dict = prop_array_get(array, i);
957 1.17.4.1 mjf obj1 = prop_dictionary_get(dict, "description");
958 1.17.4.1 mjf
959 1.17.4.1 mjf /* is it our sensor? */
960 1.17.4.1 mjf if (!prop_string_equals(obj1, obj))
961 1.17.4.1 mjf continue;
962 1.17.4.1 mjf
963 1.17.4.1 mjf /*
964 1.17.4.1 mjf * Check if a new description operation was
965 1.17.4.1 mjf * requested by the user and set new description.
966 1.17.4.1 mjf */
967 1.17.4.1 mjf if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
968 1.17.4.1 mjf targetfound = true;
969 1.17.4.1 mjf blah = prop_string_cstring_nocopy(obj2);
970 1.17.4.1 mjf SENSOR_UPSTRING(dict, "description", blah);
971 1.17.4.1 mjf break;
972 1.17.4.1 mjf }
973 1.17.4.1 mjf
974 1.17.4.1 mjf /* did the user want to remove a critical capacity limit? */
975 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "remove-critical-cap");
976 1.17.4.1 mjf if (obj2 != NULL) {
977 1.17.4.1 mjf targetfound = true;
978 1.17.4.1 mjf if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
979 1.17.4.1 mjf (edata->flags & ENVSYS_FPERCENT) == 0) {
980 1.17.4.1 mjf error = ENOTSUP;
981 1.17.4.1 mjf break;
982 1.17.4.1 mjf }
983 1.17.4.1 mjf
984 1.17.4.1 mjf sname = prop_string_cstring_nocopy(obj);
985 1.17.4.1 mjf error = sme_event_unregister(sname,
986 1.17.4.1 mjf PENVSYS_EVENT_BATT_USERCAP);
987 1.17.4.1 mjf if (error)
988 1.17.4.1 mjf break;
989 1.17.4.1 mjf
990 1.17.4.1 mjf prop_dictionary_remove(dict, "critical-capacity");
991 1.17.4.1 mjf break;
992 1.17.4.1 mjf }
993 1.17.4.1 mjf
994 1.17.4.1 mjf /* did the user want to remove a critical min limit? */
995 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
996 1.17.4.1 mjf if (obj2 != NULL) {
997 1.17.4.1 mjf targetfound = true;
998 1.17.4.1 mjf sname = prop_string_cstring_nocopy(obj);
999 1.17.4.1 mjf error = sme_event_unregister(sname,
1000 1.17.4.1 mjf PENVSYS_EVENT_USER_CRITMIN);
1001 1.17.4.1 mjf if (error)
1002 1.17.4.1 mjf break;
1003 1.17.4.1 mjf
1004 1.17.4.1 mjf prop_dictionary_remove(dict, "critical-min-limit");
1005 1.17.4.1 mjf break;
1006 1.17.4.1 mjf }
1007 1.17.4.1 mjf
1008 1.17.4.1 mjf /* did the user want to remove a critical max limit? */
1009 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1010 1.17.4.1 mjf if (obj2 != NULL) {
1011 1.17.4.1 mjf targetfound = true;
1012 1.17.4.1 mjf sname = prop_string_cstring_nocopy(obj);
1013 1.17.4.1 mjf error = sme_event_unregister(sname,
1014 1.17.4.1 mjf PENVSYS_EVENT_USER_CRITMAX);
1015 1.17.4.1 mjf if (error)
1016 1.17.4.1 mjf break;
1017 1.17.4.1 mjf
1018 1.17.4.1 mjf prop_dictionary_remove(dict, "critical-max-limit");
1019 1.17.4.1 mjf break;
1020 1.17.4.1 mjf }
1021 1.17.4.1 mjf
1022 1.17.4.1 mjf /* did the user want to change rfact? */
1023 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "new-rfact");
1024 1.17.4.1 mjf if (obj2 != NULL) {
1025 1.17.4.1 mjf targetfound = true;
1026 1.17.4.1 mjf if (edata->flags & ENVSYS_FCHANGERFACT)
1027 1.17.4.1 mjf edata->rfact = prop_number_integer_value(obj2);
1028 1.17.4.1 mjf else
1029 1.17.4.1 mjf error = ENOTSUP;
1030 1.17.4.1 mjf
1031 1.17.4.1 mjf break;
1032 1.17.4.1 mjf }
1033 1.17.4.1 mjf
1034 1.17.4.1 mjf for (i = 0; sst[i].type != -1; i++)
1035 1.17.4.1 mjf if (sst[i].type == edata->units)
1036 1.17.4.1 mjf break;
1037 1.17.4.1 mjf
1038 1.17.4.1 mjf /* did the user want to set a critical capacity event? */
1039 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "critical-capacity");
1040 1.17.4.1 mjf if (obj2 != NULL) {
1041 1.17.4.1 mjf targetfound = true;
1042 1.17.4.1 mjf if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1043 1.17.4.1 mjf (edata->flags & ENVSYS_FPERCENT) == 0) {
1044 1.17.4.1 mjf error = ENOTSUP;
1045 1.17.4.1 mjf break;
1046 1.17.4.1 mjf }
1047 1.17.4.1 mjf
1048 1.17.4.1 mjf critval = prop_number_integer_value(obj2);
1049 1.17.4.1 mjf error = sme_event_add(dict,
1050 1.17.4.1 mjf edata,
1051 1.17.4.1 mjf sme->sme_name,
1052 1.17.4.1 mjf "critical-capacity",
1053 1.17.4.1 mjf critval,
1054 1.17.4.1 mjf PENVSYS_EVENT_BATT_USERCAP,
1055 1.17.4.1 mjf sst[i].crittype);
1056 1.17.4.1 mjf break;
1057 1.17.4.1 mjf }
1058 1.17.4.1 mjf
1059 1.17.4.1 mjf /* did the user want to set a critical max event? */
1060 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "critical-max-limit");
1061 1.17.4.1 mjf if (obj2 != NULL) {
1062 1.17.4.1 mjf targetfound = true;
1063 1.17.4.1 mjf if (edata->units == ENVSYS_INDICATOR ||
1064 1.17.4.1 mjf edata->units == ENVSYS_INTEGER ||
1065 1.17.4.1 mjf edata->flags & ENVSYS_FMONNOTSUPP) {
1066 1.17.4.1 mjf error = ENOTSUP;
1067 1.17.4.1 mjf break;
1068 1.17.4.1 mjf }
1069 1.17.4.1 mjf
1070 1.17.4.1 mjf critval = prop_number_integer_value(obj2);
1071 1.17.4.1 mjf error = sme_event_add(dict,
1072 1.17.4.1 mjf edata,
1073 1.17.4.1 mjf sme->sme_name,
1074 1.17.4.1 mjf "critical-max-limit",
1075 1.17.4.1 mjf critval,
1076 1.17.4.1 mjf PENVSYS_EVENT_USER_CRITMAX,
1077 1.17.4.1 mjf sst[i].crittype);
1078 1.17.4.1 mjf break;
1079 1.17.4.1 mjf }
1080 1.17.4.1 mjf
1081 1.17.4.1 mjf /* did the user want to set a critical min event? */
1082 1.17.4.1 mjf obj2 = prop_dictionary_get(udict, "critical-min-limit");
1083 1.17.4.1 mjf if (obj2 != NULL) {
1084 1.17.4.1 mjf targetfound = true;
1085 1.17.4.1 mjf if (edata->units == ENVSYS_INDICATOR ||
1086 1.17.4.1 mjf edata->units == ENVSYS_INTEGER ||
1087 1.17.4.1 mjf edata->flags & ENVSYS_FMONNOTSUPP) {
1088 1.17.4.1 mjf error = ENOTSUP;
1089 1.17.4.1 mjf break;
1090 1.17.4.1 mjf }
1091 1.17.4.1 mjf
1092 1.17.4.1 mjf critval = prop_number_integer_value(obj2);
1093 1.17.4.1 mjf error = sme_event_add(dict,
1094 1.17.4.1 mjf edata,
1095 1.17.4.1 mjf sme->sme_name,
1096 1.17.4.1 mjf "critical-min-limit",
1097 1.17.4.1 mjf critval,
1098 1.17.4.1 mjf PENVSYS_EVENT_USER_CRITMIN,
1099 1.17.4.1 mjf sst[i].crittype);
1100 1.17.4.1 mjf break;
1101 1.17.4.1 mjf }
1102 1.17.4.1 mjf }
1103 1.17.4.1 mjf
1104 1.17.4.1 mjf /* invalid target? return the error */
1105 1.17.4.1 mjf if (!targetfound)
1106 1.17.4.1 mjf error = EINVAL;
1107 1.17.4.1 mjf
1108 1.17.4.1 mjf out:
1109 1.17.4.1 mjf return error;
1110 1.1 thorpej }
1111