sysmon_envsys.c revision 1.109 1 1.109 pgoyette /* $NetBSD: sysmon_envsys.c,v 1.109 2010/12/11 15:17:15 pgoyette Exp $ */
2 1.18 xtraeme
3 1.18 xtraeme /*-
4 1.81 xtraeme * Copyright (c) 2007, 2008 Juan Romero Pardines.
5 1.18 xtraeme * All rights reserved.
6 1.18 xtraeme *
7 1.18 xtraeme * Redistribution and use in source and binary forms, with or without
8 1.18 xtraeme * modification, are permitted provided that the following conditions
9 1.18 xtraeme * are met:
10 1.18 xtraeme * 1. Redistributions of source code must retain the above copyright
11 1.18 xtraeme * notice, this list of conditions and the following disclaimer.
12 1.18 xtraeme * 2. Redistributions in binary form must reproduce the above copyright
13 1.18 xtraeme * notice, this list of conditions and the following disclaimer in the
14 1.18 xtraeme * documentation and/or other materials provided with the distribution.
15 1.69 xtraeme *
16 1.69 xtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.69 xtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.69 xtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.69 xtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.69 xtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.69 xtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.69 xtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.69 xtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.69 xtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.69 xtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.18 xtraeme */
27 1.1 thorpej
28 1.1 thorpej /*-
29 1.1 thorpej * Copyright (c) 2000 Zembu Labs, Inc.
30 1.1 thorpej * All rights reserved.
31 1.1 thorpej *
32 1.1 thorpej * Author: Jason R. Thorpe <thorpej (at) zembu.com>
33 1.1 thorpej *
34 1.1 thorpej * Redistribution and use in source and binary forms, with or without
35 1.1 thorpej * modification, are permitted provided that the following conditions
36 1.1 thorpej * are met:
37 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
38 1.1 thorpej * notice, this list of conditions and the following disclaimer.
39 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
40 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
41 1.1 thorpej * documentation and/or other materials provided with the distribution.
42 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
43 1.1 thorpej * must display the following acknowledgement:
44 1.1 thorpej * This product includes software developed by Zembu Labs, Inc.
45 1.1 thorpej * 4. Neither the name of Zembu Labs nor the names of its employees may
46 1.1 thorpej * be used to endorse or promote products derived from this software
47 1.1 thorpej * without specific prior written permission.
48 1.1 thorpej *
49 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
50 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
51 1.1 thorpej * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
52 1.1 thorpej * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 1.1 thorpej */
60 1.1 thorpej
61 1.1 thorpej /*
62 1.18 xtraeme * Environmental sensor framework for sysmon, exported to userland
63 1.18 xtraeme * with proplib(3).
64 1.1 thorpej */
65 1.2 lukem
66 1.2 lukem #include <sys/cdefs.h>
67 1.109 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.109 2010/12/11 15:17:15 pgoyette Exp $");
68 1.1 thorpej
69 1.1 thorpej #include <sys/param.h>
70 1.18 xtraeme #include <sys/types.h>
71 1.1 thorpej #include <sys/conf.h>
72 1.1 thorpej #include <sys/errno.h>
73 1.22 xtraeme #include <sys/fcntl.h>
74 1.1 thorpej #include <sys/kernel.h>
75 1.1 thorpej #include <sys/systm.h>
76 1.1 thorpej #include <sys/proc.h>
77 1.18 xtraeme #include <sys/mutex.h>
78 1.18 xtraeme #include <sys/kmem.h>
79 1.1 thorpej
80 1.60 xtraeme /* #define ENVSYS_DEBUG */
81 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
82 1.18 xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
83 1.18 xtraeme #include <dev/sysmon/sysmon_taskq.h>
84 1.1 thorpej
85 1.82 xtraeme kmutex_t sme_global_mtx;
86 1.41 xtraeme
87 1.104 pgoyette prop_dictionary_t sme_propd;
88 1.104 pgoyette
89 1.82 xtraeme static uint32_t sysmon_envsys_next_sensor_index;
90 1.18 xtraeme static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
91 1.1 thorpej
92 1.53 xtraeme static void sysmon_envsys_destroy_plist(prop_array_t);
93 1.69 xtraeme static void sme_remove_userprops(void);
94 1.72 xtraeme static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t,
95 1.72 xtraeme prop_dictionary_t);
96 1.97 pgoyette static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *,
97 1.97 pgoyette prop_array_t, prop_dictionary_t, envsys_data_t *);
98 1.81 xtraeme static void sme_initial_refresh(void *);
99 1.92 martin static uint32_t sme_get_max_value(struct sysmon_envsys *,
100 1.92 martin bool (*)(const envsys_data_t*), bool);
101 1.1 thorpej
102 1.18 xtraeme /*
103 1.18 xtraeme * sysmon_envsys_init:
104 1.18 xtraeme *
105 1.82 xtraeme * + Initialize global mutex, dictionary and the linked list.
106 1.18 xtraeme */
107 1.18 xtraeme void
108 1.18 xtraeme sysmon_envsys_init(void)
109 1.18 xtraeme {
110 1.18 xtraeme LIST_INIT(&sysmon_envsys_list);
111 1.82 xtraeme mutex_init(&sme_global_mtx, MUTEX_DEFAULT, IPL_NONE);
112 1.18 xtraeme sme_propd = prop_dictionary_create();
113 1.18 xtraeme }
114 1.1 thorpej
115 1.1 thorpej /*
116 1.1 thorpej * sysmonopen_envsys:
117 1.1 thorpej *
118 1.18 xtraeme * + Open the system monitor device.
119 1.1 thorpej */
120 1.1 thorpej int
121 1.18 xtraeme sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
122 1.1 thorpej {
123 1.18 xtraeme return 0;
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej /*
127 1.1 thorpej * sysmonclose_envsys:
128 1.1 thorpej *
129 1.18 xtraeme * + Close the system monitor device.
130 1.1 thorpej */
131 1.1 thorpej int
132 1.18 xtraeme sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
133 1.1 thorpej {
134 1.18 xtraeme return 0;
135 1.1 thorpej }
136 1.1 thorpej
137 1.1 thorpej /*
138 1.1 thorpej * sysmonioctl_envsys:
139 1.1 thorpej *
140 1.52 xtraeme * + Perform a sysmon envsys control request.
141 1.1 thorpej */
142 1.1 thorpej int
143 1.18 xtraeme sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
144 1.1 thorpej {
145 1.18 xtraeme struct sysmon_envsys *sme = NULL;
146 1.1 thorpej int error = 0;
147 1.1 thorpej u_int oidx;
148 1.1 thorpej
149 1.1 thorpej switch (cmd) {
150 1.72 xtraeme /*
151 1.72 xtraeme * To update the global dictionary with latest data from devices.
152 1.72 xtraeme */
153 1.18 xtraeme case ENVSYS_GETDICTIONARY:
154 1.18 xtraeme {
155 1.18 xtraeme struct plistref *plist = (struct plistref *)data;
156 1.56 xtraeme
157 1.18 xtraeme /*
158 1.72 xtraeme * Update dictionaries on all sysmon envsys devices
159 1.72 xtraeme * registered.
160 1.18 xtraeme */
161 1.82 xtraeme mutex_enter(&sme_global_mtx);
162 1.18 xtraeme LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
163 1.82 xtraeme sysmon_envsys_acquire(sme, false);
164 1.18 xtraeme error = sme_update_dictionary(sme);
165 1.18 xtraeme if (error) {
166 1.18 xtraeme DPRINTF(("%s: sme_update_dictionary, "
167 1.18 xtraeme "error=%d\n", __func__, error));
168 1.82 xtraeme sysmon_envsys_release(sme, false);
169 1.82 xtraeme mutex_exit(&sme_global_mtx);
170 1.18 xtraeme return error;
171 1.18 xtraeme }
172 1.82 xtraeme sysmon_envsys_release(sme, false);
173 1.18 xtraeme }
174 1.82 xtraeme mutex_exit(&sme_global_mtx);
175 1.18 xtraeme /*
176 1.18 xtraeme * Copy global dictionary to userland.
177 1.18 xtraeme */
178 1.18 xtraeme error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
179 1.1 thorpej break;
180 1.18 xtraeme }
181 1.72 xtraeme /*
182 1.72 xtraeme * To set properties on multiple devices.
183 1.72 xtraeme */
184 1.18 xtraeme case ENVSYS_SETDICTIONARY:
185 1.1 thorpej {
186 1.18 xtraeme const struct plistref *plist = (const struct plistref *)data;
187 1.18 xtraeme prop_dictionary_t udict;
188 1.69 xtraeme prop_object_iterator_t iter, iter2;
189 1.69 xtraeme prop_object_t obj, obj2;
190 1.69 xtraeme prop_array_t array_u, array_k;
191 1.18 xtraeme const char *devname = NULL;
192 1.18 xtraeme
193 1.22 xtraeme if ((flag & FWRITE) == 0)
194 1.22 xtraeme return EPERM;
195 1.22 xtraeme
196 1.18 xtraeme /*
197 1.18 xtraeme * Get dictionary from userland.
198 1.18 xtraeme */
199 1.18 xtraeme error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
200 1.41 xtraeme if (error) {
201 1.41 xtraeme DPRINTF(("%s: copyin_ioctl error=%d\n",
202 1.41 xtraeme __func__, error));
203 1.18 xtraeme break;
204 1.41 xtraeme }
205 1.4 explorer
206 1.69 xtraeme iter = prop_dictionary_iterator(udict);
207 1.69 xtraeme if (!iter) {
208 1.69 xtraeme prop_object_release(udict);
209 1.69 xtraeme return ENOMEM;
210 1.69 xtraeme }
211 1.69 xtraeme
212 1.18 xtraeme /*
213 1.69 xtraeme * Iterate over the userland dictionary and process
214 1.69 xtraeme * the list of devices.
215 1.18 xtraeme */
216 1.69 xtraeme while ((obj = prop_object_iterator_next(iter))) {
217 1.69 xtraeme array_u = prop_dictionary_get_keysym(udict, obj);
218 1.69 xtraeme if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
219 1.69 xtraeme prop_object_iterator_release(iter);
220 1.69 xtraeme prop_object_release(udict);
221 1.69 xtraeme return EINVAL;
222 1.69 xtraeme }
223 1.69 xtraeme
224 1.69 xtraeme devname = prop_dictionary_keysym_cstring_nocopy(obj);
225 1.69 xtraeme DPRINTF(("%s: processing the '%s' array requests\n",
226 1.69 xtraeme __func__, devname));
227 1.69 xtraeme
228 1.69 xtraeme /*
229 1.69 xtraeme * find the correct sme device.
230 1.69 xtraeme */
231 1.69 xtraeme sme = sysmon_envsys_find(devname);
232 1.69 xtraeme if (!sme) {
233 1.69 xtraeme DPRINTF(("%s: NULL sme\n", __func__));
234 1.69 xtraeme prop_object_iterator_release(iter);
235 1.69 xtraeme prop_object_release(udict);
236 1.69 xtraeme return EINVAL;
237 1.69 xtraeme }
238 1.69 xtraeme
239 1.69 xtraeme /*
240 1.69 xtraeme * Find the correct array object with the string
241 1.69 xtraeme * supplied by the userland dictionary.
242 1.69 xtraeme */
243 1.69 xtraeme array_k = prop_dictionary_get(sme_propd, devname);
244 1.69 xtraeme if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
245 1.69 xtraeme DPRINTF(("%s: array device failed\n",
246 1.69 xtraeme __func__));
247 1.82 xtraeme sysmon_envsys_release(sme, false);
248 1.69 xtraeme prop_object_iterator_release(iter);
249 1.69 xtraeme prop_object_release(udict);
250 1.69 xtraeme return EINVAL;
251 1.69 xtraeme }
252 1.69 xtraeme
253 1.69 xtraeme iter2 = prop_array_iterator(array_u);
254 1.69 xtraeme if (!iter2) {
255 1.82 xtraeme sysmon_envsys_release(sme, false);
256 1.69 xtraeme prop_object_iterator_release(iter);
257 1.69 xtraeme prop_object_release(udict);
258 1.69 xtraeme return ENOMEM;
259 1.69 xtraeme }
260 1.69 xtraeme
261 1.69 xtraeme /*
262 1.69 xtraeme * Iterate over the array of dictionaries to
263 1.72 xtraeme * process the list of sensors and properties.
264 1.69 xtraeme */
265 1.69 xtraeme while ((obj2 = prop_object_iterator_next(iter2))) {
266 1.72 xtraeme /*
267 1.72 xtraeme * do the real work now.
268 1.72 xtraeme */
269 1.69 xtraeme error = sme_userset_dictionary(sme,
270 1.69 xtraeme obj2,
271 1.69 xtraeme array_k);
272 1.69 xtraeme if (error) {
273 1.82 xtraeme sysmon_envsys_release(sme, false);
274 1.69 xtraeme prop_object_iterator_release(iter2);
275 1.69 xtraeme prop_object_iterator_release(iter);
276 1.69 xtraeme prop_object_release(udict);
277 1.72 xtraeme return error;
278 1.69 xtraeme }
279 1.69 xtraeme }
280 1.69 xtraeme
281 1.82 xtraeme sysmon_envsys_release(sme, false);
282 1.69 xtraeme prop_object_iterator_release(iter2);
283 1.18 xtraeme }
284 1.4 explorer
285 1.69 xtraeme prop_object_iterator_release(iter);
286 1.69 xtraeme prop_object_release(udict);
287 1.69 xtraeme break;
288 1.69 xtraeme }
289 1.72 xtraeme /*
290 1.72 xtraeme * To remove all properties from all devices registered.
291 1.72 xtraeme */
292 1.69 xtraeme case ENVSYS_REMOVEPROPS:
293 1.69 xtraeme {
294 1.69 xtraeme const struct plistref *plist = (const struct plistref *)data;
295 1.69 xtraeme prop_dictionary_t udict;
296 1.69 xtraeme prop_object_t obj;
297 1.69 xtraeme
298 1.69 xtraeme if ((flag & FWRITE) == 0)
299 1.69 xtraeme return EPERM;
300 1.4 explorer
301 1.69 xtraeme error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
302 1.69 xtraeme if (error) {
303 1.69 xtraeme DPRINTF(("%s: copyin_ioctl error=%d\n",
304 1.69 xtraeme __func__, error));
305 1.4 explorer break;
306 1.4 explorer }
307 1.1 thorpej
308 1.69 xtraeme obj = prop_dictionary_get(udict, "envsys-remove-props");
309 1.69 xtraeme if (!obj || !prop_bool_true(obj)) {
310 1.69 xtraeme DPRINTF(("%s: invalid 'envsys-remove-props'\n",
311 1.69 xtraeme __func__));
312 1.69 xtraeme return EINVAL;
313 1.1 thorpej }
314 1.18 xtraeme
315 1.72 xtraeme prop_object_release(udict);
316 1.69 xtraeme sme_remove_userprops();
317 1.69 xtraeme
318 1.1 thorpej break;
319 1.1 thorpej }
320 1.72 xtraeme /*
321 1.72 xtraeme * Compatibility ioctls with the old interface, only implemented
322 1.72 xtraeme * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
323 1.72 xtraeme * applications work.
324 1.72 xtraeme */
325 1.1 thorpej case ENVSYS_GTREDATA:
326 1.1 thorpej {
327 1.18 xtraeme struct envsys_tre_data *tred = (void *)data;
328 1.18 xtraeme envsys_data_t *edata = NULL;
329 1.72 xtraeme bool found = false;
330 1.1 thorpej
331 1.1 thorpej tred->validflags = 0;
332 1.1 thorpej
333 1.18 xtraeme sme = sysmon_envsys_find_40(tred->sensor);
334 1.82 xtraeme if (!sme)
335 1.1 thorpej break;
336 1.18 xtraeme
337 1.1 thorpej oidx = tred->sensor;
338 1.1 thorpej tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
339 1.18 xtraeme
340 1.18 xtraeme DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
341 1.18 xtraeme __func__, tred->sensor, oidx, sme->sme_name,
342 1.18 xtraeme sme->sme_nsensors));
343 1.18 xtraeme
344 1.72 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
345 1.72 xtraeme if (edata->sensor == tred->sensor) {
346 1.72 xtraeme found = true;
347 1.72 xtraeme break;
348 1.72 xtraeme }
349 1.72 xtraeme }
350 1.72 xtraeme
351 1.72 xtraeme if (!found) {
352 1.82 xtraeme sysmon_envsys_release(sme, false);
353 1.72 xtraeme error = ENODEV;
354 1.72 xtraeme break;
355 1.72 xtraeme }
356 1.18 xtraeme
357 1.18 xtraeme if (tred->sensor < sme->sme_nsensors) {
358 1.84 jmcneill if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0 &&
359 1.84 jmcneill (sme->sme_flags & SME_POLL_ONLY) == 0) {
360 1.82 xtraeme mutex_enter(&sme->sme_mtx);
361 1.72 xtraeme (*sme->sme_refresh)(sme, edata);
362 1.82 xtraeme mutex_exit(&sme->sme_mtx);
363 1.82 xtraeme }
364 1.18 xtraeme
365 1.72 xtraeme /*
366 1.72 xtraeme * copy required values to the old interface.
367 1.72 xtraeme */
368 1.18 xtraeme tred->sensor = edata->sensor;
369 1.18 xtraeme tred->cur.data_us = edata->value_cur;
370 1.18 xtraeme tred->cur.data_s = edata->value_cur;
371 1.18 xtraeme tred->max.data_us = edata->value_max;
372 1.18 xtraeme tred->max.data_s = edata->value_max;
373 1.18 xtraeme tred->min.data_us = edata->value_min;
374 1.18 xtraeme tred->min.data_s = edata->value_min;
375 1.18 xtraeme tred->avg.data_us = edata->value_avg;
376 1.18 xtraeme tred->avg.data_s = edata->value_avg;
377 1.71 xtraeme if (edata->units == ENVSYS_BATTERY_CHARGE)
378 1.71 xtraeme tred->units = ENVSYS_INDICATOR;
379 1.71 xtraeme else
380 1.72 xtraeme tred->units = edata->units;
381 1.18 xtraeme
382 1.20 xtraeme tred->validflags |= ENVSYS_FVALID;
383 1.20 xtraeme tred->validflags |= ENVSYS_FCURVALID;
384 1.20 xtraeme
385 1.20 xtraeme if (edata->flags & ENVSYS_FPERCENT) {
386 1.20 xtraeme tred->validflags |= ENVSYS_FMAXVALID;
387 1.20 xtraeme tred->validflags |= ENVSYS_FFRACVALID;
388 1.20 xtraeme }
389 1.20 xtraeme
390 1.72 xtraeme if (edata->state == ENVSYS_SINVALID) {
391 1.20 xtraeme tred->validflags &= ~ENVSYS_FCURVALID;
392 1.18 xtraeme tred->cur.data_us = tred->cur.data_s = 0;
393 1.18 xtraeme }
394 1.18 xtraeme
395 1.18 xtraeme DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
396 1.18 xtraeme __func__, edata->desc, tred->cur.data_s));
397 1.18 xtraeme DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
398 1.18 xtraeme " tred->sensor=%d\n", __func__, tred->validflags,
399 1.18 xtraeme tred->units, tred->sensor));
400 1.3 jdolecek }
401 1.1 thorpej tred->sensor = oidx;
402 1.82 xtraeme sysmon_envsys_release(sme, false);
403 1.1 thorpej
404 1.1 thorpej break;
405 1.1 thorpej }
406 1.1 thorpej case ENVSYS_GTREINFO:
407 1.1 thorpej {
408 1.18 xtraeme struct envsys_basic_info *binfo = (void *)data;
409 1.18 xtraeme envsys_data_t *edata = NULL;
410 1.72 xtraeme bool found = false;
411 1.1 thorpej
412 1.1 thorpej binfo->validflags = 0;
413 1.1 thorpej
414 1.18 xtraeme sme = sysmon_envsys_find_40(binfo->sensor);
415 1.82 xtraeme if (!sme)
416 1.1 thorpej break;
417 1.18 xtraeme
418 1.1 thorpej oidx = binfo->sensor;
419 1.1 thorpej binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
420 1.18 xtraeme
421 1.72 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
422 1.72 xtraeme if (edata->sensor == binfo->sensor) {
423 1.72 xtraeme found = true;
424 1.72 xtraeme break;
425 1.72 xtraeme }
426 1.72 xtraeme }
427 1.72 xtraeme
428 1.72 xtraeme if (!found) {
429 1.82 xtraeme sysmon_envsys_release(sme, false);
430 1.72 xtraeme error = ENODEV;
431 1.72 xtraeme break;
432 1.72 xtraeme }
433 1.18 xtraeme
434 1.20 xtraeme binfo->validflags |= ENVSYS_FVALID;
435 1.18 xtraeme
436 1.18 xtraeme if (binfo->sensor < sme->sme_nsensors) {
437 1.71 xtraeme if (edata->units == ENVSYS_BATTERY_CHARGE)
438 1.71 xtraeme binfo->units = ENVSYS_INDICATOR;
439 1.71 xtraeme else
440 1.72 xtraeme binfo->units = edata->units;
441 1.70 plunky
442 1.70 plunky /*
443 1.70 plunky * previously, the ACPI sensor names included the
444 1.70 plunky * device name. Include that in compatibility code.
445 1.70 plunky */
446 1.70 plunky if (strncmp(sme->sme_name, "acpi", 4) == 0)
447 1.70 plunky (void)snprintf(binfo->desc, sizeof(binfo->desc),
448 1.70 plunky "%s %s", sme->sme_name, edata->desc);
449 1.70 plunky else
450 1.70 plunky (void)strlcpy(binfo->desc, edata->desc,
451 1.70 plunky sizeof(binfo->desc));
452 1.18 xtraeme }
453 1.18 xtraeme
454 1.18 xtraeme DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
455 1.18 xtraeme __func__, binfo->units, binfo->validflags));
456 1.18 xtraeme DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
457 1.18 xtraeme __func__, binfo->desc, binfo->sensor));
458 1.18 xtraeme
459 1.1 thorpej binfo->sensor = oidx;
460 1.82 xtraeme sysmon_envsys_release(sme, false);
461 1.18 xtraeme
462 1.1 thorpej break;
463 1.1 thorpej }
464 1.1 thorpej default:
465 1.1 thorpej error = ENOTTY;
466 1.18 xtraeme break;
467 1.1 thorpej }
468 1.1 thorpej
469 1.18 xtraeme return error;
470 1.1 thorpej }
471 1.1 thorpej
472 1.1 thorpej /*
473 1.72 xtraeme * sysmon_envsys_create:
474 1.72 xtraeme *
475 1.72 xtraeme * + Allocates a new sysmon_envsys object and initializes the
476 1.72 xtraeme * stuff for sensors and events.
477 1.72 xtraeme */
478 1.72 xtraeme struct sysmon_envsys *
479 1.72 xtraeme sysmon_envsys_create(void)
480 1.72 xtraeme {
481 1.72 xtraeme struct sysmon_envsys *sme;
482 1.72 xtraeme
483 1.72 xtraeme sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
484 1.72 xtraeme TAILQ_INIT(&sme->sme_sensors_list);
485 1.72 xtraeme LIST_INIT(&sme->sme_events_list);
486 1.82 xtraeme mutex_init(&sme->sme_mtx, MUTEX_DEFAULT, IPL_NONE);
487 1.82 xtraeme cv_init(&sme->sme_condvar, "sme_wait");
488 1.72 xtraeme
489 1.72 xtraeme return sme;
490 1.72 xtraeme }
491 1.72 xtraeme
492 1.72 xtraeme /*
493 1.72 xtraeme * sysmon_envsys_destroy:
494 1.72 xtraeme *
495 1.80 xtraeme * + Removes all sensors from the tail queue, destroys the callout
496 1.80 xtraeme * and frees the sysmon_envsys object.
497 1.72 xtraeme */
498 1.72 xtraeme void
499 1.72 xtraeme sysmon_envsys_destroy(struct sysmon_envsys *sme)
500 1.72 xtraeme {
501 1.72 xtraeme envsys_data_t *edata;
502 1.72 xtraeme
503 1.72 xtraeme KASSERT(sme != NULL);
504 1.72 xtraeme
505 1.72 xtraeme while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
506 1.72 xtraeme edata = TAILQ_FIRST(&sme->sme_sensors_list);
507 1.72 xtraeme TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
508 1.72 xtraeme }
509 1.82 xtraeme mutex_destroy(&sme->sme_mtx);
510 1.82 xtraeme cv_destroy(&sme->sme_condvar);
511 1.72 xtraeme kmem_free(sme, sizeof(*sme));
512 1.72 xtraeme }
513 1.72 xtraeme
514 1.72 xtraeme /*
515 1.72 xtraeme * sysmon_envsys_sensor_attach:
516 1.72 xtraeme *
517 1.72 xtraeme * + Attachs a sensor into a sysmon_envsys device checking that units
518 1.72 xtraeme * is set to a valid type and description is unique and not empty.
519 1.72 xtraeme */
520 1.72 xtraeme int
521 1.72 xtraeme sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
522 1.72 xtraeme {
523 1.72 xtraeme const struct sme_description_table *sdt_units;
524 1.72 xtraeme envsys_data_t *oedata;
525 1.72 xtraeme int i;
526 1.72 xtraeme
527 1.72 xtraeme KASSERT(sme != NULL || edata != NULL);
528 1.72 xtraeme
529 1.72 xtraeme /*
530 1.72 xtraeme * Find the correct units for this sensor.
531 1.72 xtraeme */
532 1.72 xtraeme sdt_units = sme_get_description_table(SME_DESC_UNITS);
533 1.72 xtraeme for (i = 0; sdt_units[i].type != -1; i++)
534 1.72 xtraeme if (sdt_units[i].type == edata->units)
535 1.72 xtraeme break;
536 1.72 xtraeme
537 1.72 xtraeme if (strcmp(sdt_units[i].desc, "unknown") == 0)
538 1.72 xtraeme return EINVAL;
539 1.72 xtraeme
540 1.72 xtraeme /*
541 1.72 xtraeme * Check that description is not empty or duplicate.
542 1.72 xtraeme */
543 1.72 xtraeme if (strlen(edata->desc) == 0)
544 1.72 xtraeme return EINVAL;
545 1.72 xtraeme
546 1.82 xtraeme mutex_enter(&sme->sme_mtx);
547 1.82 xtraeme sysmon_envsys_acquire(sme, true);
548 1.72 xtraeme TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
549 1.72 xtraeme if (strcmp(oedata->desc, edata->desc) == 0) {
550 1.82 xtraeme sysmon_envsys_release(sme, true);
551 1.82 xtraeme mutex_exit(&sme->sme_mtx);
552 1.72 xtraeme return EEXIST;
553 1.72 xtraeme }
554 1.72 xtraeme }
555 1.72 xtraeme /*
556 1.72 xtraeme * Ok, the sensor has been added into the device queue.
557 1.72 xtraeme */
558 1.72 xtraeme TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
559 1.72 xtraeme
560 1.72 xtraeme /*
561 1.72 xtraeme * Give the sensor a index position.
562 1.72 xtraeme */
563 1.72 xtraeme edata->sensor = sme->sme_nsensors;
564 1.72 xtraeme sme->sme_nsensors++;
565 1.82 xtraeme sysmon_envsys_release(sme, true);
566 1.82 xtraeme mutex_exit(&sme->sme_mtx);
567 1.72 xtraeme
568 1.89 pgoyette DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n",
569 1.89 pgoyette __func__, edata->sensor, edata->desc,
570 1.87 pgoyette sdt_units[i].type, sdt_units[i].desc));
571 1.87 pgoyette
572 1.72 xtraeme return 0;
573 1.72 xtraeme }
574 1.72 xtraeme
575 1.72 xtraeme /*
576 1.72 xtraeme * sysmon_envsys_sensor_detach:
577 1.72 xtraeme *
578 1.72 xtraeme * + Detachs a sensor from a sysmon_envsys device and decrements the
579 1.72 xtraeme * sensors count on success.
580 1.72 xtraeme */
581 1.72 xtraeme int
582 1.72 xtraeme sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
583 1.72 xtraeme {
584 1.72 xtraeme envsys_data_t *oedata;
585 1.72 xtraeme bool found = false;
586 1.72 xtraeme
587 1.72 xtraeme KASSERT(sme != NULL || edata != NULL);
588 1.72 xtraeme
589 1.72 xtraeme /*
590 1.72 xtraeme * Check the sensor is already on the list.
591 1.72 xtraeme */
592 1.82 xtraeme mutex_enter(&sme->sme_mtx);
593 1.82 xtraeme sysmon_envsys_acquire(sme, true);
594 1.72 xtraeme TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
595 1.72 xtraeme if (oedata->sensor == edata->sensor) {
596 1.72 xtraeme found = true;
597 1.72 xtraeme break;
598 1.72 xtraeme }
599 1.72 xtraeme }
600 1.72 xtraeme
601 1.72 xtraeme if (!found) {
602 1.82 xtraeme sysmon_envsys_release(sme, true);
603 1.82 xtraeme mutex_exit(&sme->sme_mtx);
604 1.72 xtraeme return EINVAL;
605 1.72 xtraeme }
606 1.72 xtraeme
607 1.72 xtraeme /*
608 1.72 xtraeme * remove it and decrement the sensors count.
609 1.72 xtraeme */
610 1.72 xtraeme TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
611 1.72 xtraeme sme->sme_nsensors--;
612 1.82 xtraeme sysmon_envsys_release(sme, true);
613 1.82 xtraeme mutex_exit(&sme->sme_mtx);
614 1.72 xtraeme
615 1.72 xtraeme return 0;
616 1.72 xtraeme }
617 1.72 xtraeme
618 1.72 xtraeme
619 1.72 xtraeme /*
620 1.1 thorpej * sysmon_envsys_register:
621 1.1 thorpej *
622 1.52 xtraeme * + Register a sysmon envsys device.
623 1.52 xtraeme * + Create array of dictionaries for a device.
624 1.1 thorpej */
625 1.1 thorpej int
626 1.1 thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
627 1.1 thorpej {
628 1.58 xtraeme struct sme_evdrv {
629 1.58 xtraeme SLIST_ENTRY(sme_evdrv) evdrv_head;
630 1.58 xtraeme sme_event_drv_t *evdrv;
631 1.58 xtraeme };
632 1.58 xtraeme SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
633 1.82 xtraeme struct sme_evdrv *evdv = NULL;
634 1.18 xtraeme struct sysmon_envsys *lsme;
635 1.82 xtraeme prop_array_t array = NULL;
636 1.72 xtraeme prop_dictionary_t dict, dict2;
637 1.47 xtraeme envsys_data_t *edata = NULL;
638 1.86 pgoyette sme_event_drv_t *this_evdrv;
639 1.97 pgoyette int nevent;
640 1.81 xtraeme int error = 0;
641 1.1 thorpej
642 1.47 xtraeme KASSERT(sme != NULL);
643 1.47 xtraeme KASSERT(sme->sme_name != NULL);
644 1.18 xtraeme
645 1.47 xtraeme /*
646 1.82 xtraeme * Check if requested sysmon_envsys device is valid
647 1.82 xtraeme * and does not exist already in the list.
648 1.82 xtraeme */
649 1.82 xtraeme mutex_enter(&sme_global_mtx);
650 1.82 xtraeme LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
651 1.82 xtraeme if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
652 1.82 xtraeme mutex_exit(&sme_global_mtx);
653 1.82 xtraeme return EEXIST;
654 1.82 xtraeme }
655 1.82 xtraeme }
656 1.82 xtraeme mutex_exit(&sme_global_mtx);
657 1.82 xtraeme
658 1.82 xtraeme /*
659 1.72 xtraeme * sanity check: if SME_DISABLE_REFRESH is not set,
660 1.72 xtraeme * the sme_refresh function callback must be non NULL.
661 1.18 xtraeme */
662 1.72 xtraeme if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
663 1.72 xtraeme if (!sme->sme_refresh)
664 1.72 xtraeme return EINVAL;
665 1.72 xtraeme
666 1.18 xtraeme /*
667 1.72 xtraeme * If the list of sensors is empty, there's no point to continue...
668 1.18 xtraeme */
669 1.72 xtraeme if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
670 1.72 xtraeme DPRINTF(("%s: sensors list empty for %s\n", __func__,
671 1.72 xtraeme sme->sme_name));
672 1.72 xtraeme return ENOTSUP;
673 1.1 thorpej }
674 1.1 thorpej
675 1.72 xtraeme /*
676 1.82 xtraeme * Initialize the singly linked list for driver events.
677 1.72 xtraeme */
678 1.82 xtraeme SLIST_INIT(&sme_evdrv_list);
679 1.82 xtraeme
680 1.47 xtraeme array = prop_array_create();
681 1.69 xtraeme if (!array)
682 1.47 xtraeme return ENOMEM;
683 1.47 xtraeme
684 1.58 xtraeme /*
685 1.72 xtraeme * Iterate over all sensors and create a dictionary per sensor.
686 1.72 xtraeme * We must respect the order in which the sensors were added.
687 1.47 xtraeme */
688 1.76 dyoung TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
689 1.53 xtraeme dict = prop_dictionary_create();
690 1.69 xtraeme if (!dict) {
691 1.53 xtraeme error = ENOMEM;
692 1.53 xtraeme goto out2;
693 1.53 xtraeme }
694 1.53 xtraeme
695 1.48 xtraeme /*
696 1.48 xtraeme * Create all objects in sensor's dictionary.
697 1.48 xtraeme */
698 1.86 pgoyette this_evdrv = sme_add_sensor_dictionary(sme, array,
699 1.86 pgoyette dict, edata);
700 1.86 pgoyette if (this_evdrv) {
701 1.86 pgoyette evdv = kmem_zalloc(sizeof(*evdv), KM_SLEEP);
702 1.86 pgoyette evdv->evdrv = this_evdrv;
703 1.82 xtraeme SLIST_INSERT_HEAD(&sme_evdrv_list, evdv, evdrv_head);
704 1.86 pgoyette }
705 1.47 xtraeme }
706 1.27 xtraeme
707 1.47 xtraeme /*
708 1.47 xtraeme * If the array does not contain any object (sensor), there's
709 1.47 xtraeme * no need to attach the driver.
710 1.47 xtraeme */
711 1.47 xtraeme if (prop_array_count(array) == 0) {
712 1.47 xtraeme error = EINVAL;
713 1.47 xtraeme DPRINTF(("%s: empty array for '%s'\n", __func__,
714 1.47 xtraeme sme->sme_name));
715 1.47 xtraeme goto out;
716 1.47 xtraeme }
717 1.72 xtraeme
718 1.72 xtraeme /*
719 1.72 xtraeme * Add the dictionary for the global properties of this device.
720 1.72 xtraeme */
721 1.72 xtraeme dict2 = prop_dictionary_create();
722 1.72 xtraeme if (!dict2) {
723 1.72 xtraeme error = ENOMEM;
724 1.72 xtraeme goto out;
725 1.72 xtraeme }
726 1.72 xtraeme
727 1.72 xtraeme error = sme_add_property_dictionary(sme, array, dict2);
728 1.72 xtraeme if (error) {
729 1.72 xtraeme prop_object_release(dict2);
730 1.72 xtraeme goto out;
731 1.72 xtraeme }
732 1.72 xtraeme
733 1.47 xtraeme /*
734 1.47 xtraeme * Add the array into the global dictionary for the driver.
735 1.47 xtraeme *
736 1.47 xtraeme * <dict>
737 1.47 xtraeme * <key>foo0</key>
738 1.47 xtraeme * <array>
739 1.47 xtraeme * ...
740 1.47 xtraeme */
741 1.82 xtraeme mutex_enter(&sme_global_mtx);
742 1.47 xtraeme if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
743 1.53 xtraeme error = EINVAL;
744 1.47 xtraeme DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
745 1.47 xtraeme sme->sme_name));
746 1.47 xtraeme goto out;
747 1.47 xtraeme }
748 1.82 xtraeme
749 1.47 xtraeme /*
750 1.48 xtraeme * Add the device into the list.
751 1.47 xtraeme */
752 1.47 xtraeme LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
753 1.58 xtraeme sme->sme_fsensor = sysmon_envsys_next_sensor_index;
754 1.58 xtraeme sysmon_envsys_next_sensor_index += sme->sme_nsensors;
755 1.82 xtraeme mutex_exit(&sme_global_mtx);
756 1.82 xtraeme
757 1.18 xtraeme out:
758 1.72 xtraeme /*
759 1.98 pgoyette * No errors? Make an initial data refresh if was requested,
760 1.98 pgoyette * then register the events that were set in the driver. Do
761 1.98 pgoyette * the refresh first in case it is needed to establish the
762 1.98 pgoyette * limits or max_value needed by some events.
763 1.72 xtraeme */
764 1.56 xtraeme if (error == 0) {
765 1.97 pgoyette nevent = 0;
766 1.81 xtraeme sysmon_task_queue_init();
767 1.98 pgoyette
768 1.98 pgoyette if (sme->sme_flags & SME_INIT_REFRESH) {
769 1.98 pgoyette sysmon_task_queue_sched(0, sme_initial_refresh, sme);
770 1.98 pgoyette DPRINTF(("%s: scheduled initial refresh for '%s'\n",
771 1.98 pgoyette __func__, sme->sme_name));
772 1.98 pgoyette }
773 1.82 xtraeme SLIST_FOREACH(evdv, &sme_evdrv_list, evdrv_head) {
774 1.58 xtraeme sysmon_task_queue_sched(0,
775 1.82 xtraeme sme_event_drvadd, evdv->evdrv);
776 1.97 pgoyette nevent++;
777 1.58 xtraeme }
778 1.97 pgoyette DPRINTF(("%s: driver '%s' registered (nsens=%d nevent=%d)\n",
779 1.97 pgoyette __func__, sme->sme_name, sme->sme_nsensors, nevent));
780 1.56 xtraeme }
781 1.62 xtraeme
782 1.47 xtraeme out2:
783 1.58 xtraeme while (!SLIST_EMPTY(&sme_evdrv_list)) {
784 1.82 xtraeme evdv = SLIST_FIRST(&sme_evdrv_list);
785 1.58 xtraeme SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
786 1.82 xtraeme kmem_free(evdv, sizeof(*evdv));
787 1.58 xtraeme }
788 1.69 xtraeme if (!error)
789 1.62 xtraeme return 0;
790 1.62 xtraeme
791 1.72 xtraeme /*
792 1.72 xtraeme * Ugh... something wasn't right; unregister all events and sensors
793 1.72 xtraeme * previously assigned and destroy the array with all its objects.
794 1.72 xtraeme */
795 1.62 xtraeme DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
796 1.62 xtraeme sme->sme_name, error));
797 1.82 xtraeme
798 1.82 xtraeme sme_event_unregister_all(sme);
799 1.82 xtraeme while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
800 1.82 xtraeme edata = TAILQ_FIRST(&sme->sme_sensors_list);
801 1.82 xtraeme TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
802 1.59 xtraeme }
803 1.53 xtraeme sysmon_envsys_destroy_plist(array);
804 1.18 xtraeme return error;
805 1.1 thorpej }
806 1.1 thorpej
807 1.1 thorpej /*
808 1.53 xtraeme * sysmon_envsys_destroy_plist:
809 1.53 xtraeme *
810 1.53 xtraeme * + Remove all objects from the array of dictionaries that is
811 1.53 xtraeme * created in a sysmon envsys device.
812 1.53 xtraeme */
813 1.53 xtraeme static void
814 1.53 xtraeme sysmon_envsys_destroy_plist(prop_array_t array)
815 1.53 xtraeme {
816 1.63 xtraeme prop_object_iterator_t iter, iter2;
817 1.53 xtraeme prop_dictionary_t dict;
818 1.53 xtraeme prop_object_t obj;
819 1.53 xtraeme
820 1.53 xtraeme KASSERT(array != NULL);
821 1.82 xtraeme KASSERT(prop_object_type(array) == PROP_TYPE_ARRAY);
822 1.53 xtraeme
823 1.67 xtraeme DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
824 1.63 xtraeme prop_array_count(array)));
825 1.63 xtraeme
826 1.53 xtraeme iter = prop_array_iterator(array);
827 1.69 xtraeme if (!iter)
828 1.53 xtraeme return;
829 1.53 xtraeme
830 1.69 xtraeme while ((dict = prop_object_iterator_next(iter))) {
831 1.63 xtraeme KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
832 1.53 xtraeme iter2 = prop_dictionary_iterator(dict);
833 1.69 xtraeme if (!iter2)
834 1.63 xtraeme goto out;
835 1.67 xtraeme DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
836 1.63 xtraeme while ((obj = prop_object_iterator_next(iter2)) != NULL) {
837 1.67 xtraeme DPRINTFOBJ(("%s: obj=%s\n", __func__,
838 1.65 xtraeme prop_dictionary_keysym_cstring_nocopy(obj)));
839 1.63 xtraeme prop_dictionary_remove(dict,
840 1.63 xtraeme prop_dictionary_keysym_cstring_nocopy(obj));
841 1.63 xtraeme prop_object_iterator_reset(iter2);
842 1.63 xtraeme }
843 1.63 xtraeme prop_object_iterator_release(iter2);
844 1.67 xtraeme DPRINTFOBJ(("%s: objects in dictionary:%d\n",
845 1.63 xtraeme __func__, prop_dictionary_count(dict)));
846 1.53 xtraeme prop_object_release(dict);
847 1.53 xtraeme }
848 1.53 xtraeme
849 1.63 xtraeme out:
850 1.53 xtraeme prop_object_iterator_release(iter);
851 1.63 xtraeme prop_object_release(array);
852 1.53 xtraeme }
853 1.53 xtraeme
854 1.53 xtraeme /*
855 1.1 thorpej * sysmon_envsys_unregister:
856 1.1 thorpej *
857 1.52 xtraeme * + Unregister a sysmon envsys device.
858 1.1 thorpej */
859 1.1 thorpej void
860 1.1 thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
861 1.1 thorpej {
862 1.53 xtraeme prop_array_t array;
863 1.105 pgoyette struct sysmon_envsys *osme;
864 1.35 xtraeme
865 1.35 xtraeme KASSERT(sme != NULL);
866 1.1 thorpej
867 1.72 xtraeme /*
868 1.83 xtraeme * Unregister all events associated with device.
869 1.72 xtraeme */
870 1.82 xtraeme sme_event_unregister_all(sme);
871 1.72 xtraeme /*
872 1.105 pgoyette * Decrement global sensors counter and the first_sensor index
873 1.105 pgoyette * for remaining devices in the list (only used for compatibility
874 1.105 pgoyette * with previous API), and remove the device from the list.
875 1.27 xtraeme */
876 1.82 xtraeme mutex_enter(&sme_global_mtx);
877 1.72 xtraeme sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
878 1.105 pgoyette LIST_FOREACH(osme, &sysmon_envsys_list, sme_list) {
879 1.105 pgoyette if (osme->sme_fsensor >= sme->sme_fsensor)
880 1.105 pgoyette osme->sme_fsensor -= sme->sme_nsensors;
881 1.105 pgoyette }
882 1.58 xtraeme LIST_REMOVE(sme, sme_list);
883 1.82 xtraeme mutex_exit(&sme_global_mtx);
884 1.82 xtraeme
885 1.47 xtraeme /*
886 1.53 xtraeme * Remove the device (and all its objects) from the global dictionary.
887 1.47 xtraeme */
888 1.53 xtraeme array = prop_dictionary_get(sme_propd, sme->sme_name);
889 1.63 xtraeme if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
890 1.82 xtraeme mutex_enter(&sme_global_mtx);
891 1.63 xtraeme prop_dictionary_remove(sme_propd, sme->sme_name);
892 1.82 xtraeme mutex_exit(&sme_global_mtx);
893 1.53 xtraeme sysmon_envsys_destroy_plist(array);
894 1.53 xtraeme }
895 1.72 xtraeme /*
896 1.72 xtraeme * And finally destroy the sysmon_envsys object.
897 1.72 xtraeme */
898 1.72 xtraeme sysmon_envsys_destroy(sme);
899 1.1 thorpej }
900 1.1 thorpej
901 1.1 thorpej /*
902 1.1 thorpej * sysmon_envsys_find:
903 1.1 thorpej *
904 1.82 xtraeme * + Find a sysmon envsys device and mark it as busy
905 1.82 xtraeme * once it's available.
906 1.1 thorpej */
907 1.1 thorpej struct sysmon_envsys *
908 1.18 xtraeme sysmon_envsys_find(const char *name)
909 1.1 thorpej {
910 1.1 thorpej struct sysmon_envsys *sme;
911 1.1 thorpej
912 1.82 xtraeme mutex_enter(&sme_global_mtx);
913 1.77 dyoung LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
914 1.79 xtraeme if (strcmp(sme->sme_name, name) == 0) {
915 1.82 xtraeme sysmon_envsys_acquire(sme, false);
916 1.79 xtraeme break;
917 1.79 xtraeme }
918 1.18 xtraeme }
919 1.82 xtraeme mutex_exit(&sme_global_mtx);
920 1.82 xtraeme
921 1.18 xtraeme return sme;
922 1.18 xtraeme }
923 1.18 xtraeme
924 1.18 xtraeme /*
925 1.82 xtraeme * Compatibility function with the old API.
926 1.72 xtraeme */
927 1.82 xtraeme struct sysmon_envsys *
928 1.82 xtraeme sysmon_envsys_find_40(u_int idx)
929 1.72 xtraeme {
930 1.82 xtraeme struct sysmon_envsys *sme;
931 1.72 xtraeme
932 1.82 xtraeme mutex_enter(&sme_global_mtx);
933 1.82 xtraeme LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
934 1.82 xtraeme if (idx >= sme->sme_fsensor &&
935 1.82 xtraeme idx < (sme->sme_fsensor + sme->sme_nsensors)) {
936 1.82 xtraeme sysmon_envsys_acquire(sme, false);
937 1.82 xtraeme break;
938 1.82 xtraeme }
939 1.82 xtraeme }
940 1.82 xtraeme mutex_exit(&sme_global_mtx);
941 1.72 xtraeme
942 1.82 xtraeme return sme;
943 1.72 xtraeme }
944 1.72 xtraeme
945 1.72 xtraeme /*
946 1.82 xtraeme * sysmon_envsys_acquire:
947 1.18 xtraeme *
948 1.82 xtraeme * + Wait until a sysmon envsys device is available and mark
949 1.82 xtraeme * it as busy.
950 1.18 xtraeme */
951 1.18 xtraeme void
952 1.82 xtraeme sysmon_envsys_acquire(struct sysmon_envsys *sme, bool locked)
953 1.18 xtraeme {
954 1.82 xtraeme KASSERT(sme != NULL);
955 1.72 xtraeme
956 1.82 xtraeme if (locked) {
957 1.82 xtraeme while (sme->sme_flags & SME_FLAG_BUSY)
958 1.82 xtraeme cv_wait(&sme->sme_condvar, &sme->sme_mtx);
959 1.82 xtraeme sme->sme_flags |= SME_FLAG_BUSY;
960 1.82 xtraeme } else {
961 1.82 xtraeme mutex_enter(&sme->sme_mtx);
962 1.82 xtraeme while (sme->sme_flags & SME_FLAG_BUSY)
963 1.82 xtraeme cv_wait(&sme->sme_condvar, &sme->sme_mtx);
964 1.82 xtraeme sme->sme_flags |= SME_FLAG_BUSY;
965 1.82 xtraeme mutex_exit(&sme->sme_mtx);
966 1.82 xtraeme }
967 1.18 xtraeme }
968 1.18 xtraeme
969 1.82 xtraeme /*
970 1.82 xtraeme * sysmon_envsys_release:
971 1.82 xtraeme *
972 1.82 xtraeme * + Unmark a sysmon envsys device as busy, and notify
973 1.82 xtraeme * waiters.
974 1.82 xtraeme */
975 1.82 xtraeme void
976 1.82 xtraeme sysmon_envsys_release(struct sysmon_envsys *sme, bool locked)
977 1.18 xtraeme {
978 1.82 xtraeme KASSERT(sme != NULL);
979 1.18 xtraeme
980 1.82 xtraeme if (locked) {
981 1.82 xtraeme sme->sme_flags &= ~SME_FLAG_BUSY;
982 1.82 xtraeme cv_broadcast(&sme->sme_condvar);
983 1.82 xtraeme } else {
984 1.82 xtraeme mutex_enter(&sme->sme_mtx);
985 1.82 xtraeme sme->sme_flags &= ~SME_FLAG_BUSY;
986 1.82 xtraeme cv_broadcast(&sme->sme_condvar);
987 1.82 xtraeme mutex_exit(&sme->sme_mtx);
988 1.18 xtraeme }
989 1.18 xtraeme }
990 1.18 xtraeme
991 1.18 xtraeme /*
992 1.81 xtraeme * sme_initial_refresh:
993 1.81 xtraeme *
994 1.81 xtraeme * + Do an initial refresh of the sensors in a device just after
995 1.81 xtraeme * interrupts are enabled in the autoconf(9) process.
996 1.81 xtraeme *
997 1.81 xtraeme */
998 1.81 xtraeme static void
999 1.81 xtraeme sme_initial_refresh(void *arg)
1000 1.81 xtraeme {
1001 1.81 xtraeme struct sysmon_envsys *sme = arg;
1002 1.81 xtraeme envsys_data_t *edata;
1003 1.81 xtraeme
1004 1.82 xtraeme mutex_enter(&sme->sme_mtx);
1005 1.82 xtraeme sysmon_envsys_acquire(sme, true);
1006 1.81 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head)
1007 1.94 pgoyette if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
1008 1.94 pgoyette (*sme->sme_refresh)(sme, edata);
1009 1.82 xtraeme sysmon_envsys_release(sme, true);
1010 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1011 1.81 xtraeme }
1012 1.81 xtraeme
1013 1.81 xtraeme /*
1014 1.69 xtraeme * sme_sensor_dictionary_get:
1015 1.69 xtraeme *
1016 1.72 xtraeme * + Returns a dictionary of a device specified by its index
1017 1.72 xtraeme * position.
1018 1.69 xtraeme */
1019 1.69 xtraeme prop_dictionary_t
1020 1.69 xtraeme sme_sensor_dictionary_get(prop_array_t array, const char *index)
1021 1.69 xtraeme {
1022 1.69 xtraeme prop_object_iterator_t iter;
1023 1.69 xtraeme prop_dictionary_t dict;
1024 1.69 xtraeme prop_object_t obj;
1025 1.69 xtraeme
1026 1.69 xtraeme KASSERT(array != NULL || index != NULL);
1027 1.69 xtraeme
1028 1.69 xtraeme iter = prop_array_iterator(array);
1029 1.69 xtraeme if (!iter)
1030 1.69 xtraeme return NULL;
1031 1.69 xtraeme
1032 1.69 xtraeme while ((dict = prop_object_iterator_next(iter))) {
1033 1.69 xtraeme obj = prop_dictionary_get(dict, "index");
1034 1.69 xtraeme if (prop_string_equals_cstring(obj, index))
1035 1.69 xtraeme break;
1036 1.69 xtraeme }
1037 1.69 xtraeme
1038 1.69 xtraeme prop_object_iterator_release(iter);
1039 1.69 xtraeme return dict;
1040 1.69 xtraeme }
1041 1.69 xtraeme
1042 1.69 xtraeme /*
1043 1.69 xtraeme * sme_remove_userprops:
1044 1.69 xtraeme *
1045 1.69 xtraeme * + Remove all properties from all devices that were set by
1046 1.72 xtraeme * the ENVSYS_SETDICTIONARY ioctl.
1047 1.69 xtraeme */
1048 1.69 xtraeme static void
1049 1.69 xtraeme sme_remove_userprops(void)
1050 1.69 xtraeme {
1051 1.69 xtraeme struct sysmon_envsys *sme;
1052 1.69 xtraeme prop_array_t array;
1053 1.69 xtraeme prop_dictionary_t sdict;
1054 1.69 xtraeme envsys_data_t *edata = NULL;
1055 1.69 xtraeme char tmp[ENVSYS_DESCLEN];
1056 1.106 pgoyette sysmon_envsys_lim_t lims;
1057 1.109 pgoyette const struct sme_description_table *sdt_units;
1058 1.109 pgoyette uint32_t props;
1059 1.109 pgoyette int ptype, i;
1060 1.69 xtraeme
1061 1.82 xtraeme mutex_enter(&sme_global_mtx);
1062 1.69 xtraeme LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1063 1.82 xtraeme sysmon_envsys_acquire(sme, false);
1064 1.69 xtraeme array = prop_dictionary_get(sme_propd, sme->sme_name);
1065 1.69 xtraeme
1066 1.72 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1067 1.69 xtraeme (void)snprintf(tmp, sizeof(tmp), "sensor%d",
1068 1.69 xtraeme edata->sensor);
1069 1.69 xtraeme sdict = sme_sensor_dictionary_get(array, tmp);
1070 1.72 xtraeme KASSERT(sdict != NULL);
1071 1.69 xtraeme
1072 1.85 pgoyette ptype = 0;
1073 1.87 pgoyette if (edata->upropset & PROP_BATTCAP) {
1074 1.69 xtraeme prop_dictionary_remove(sdict,
1075 1.69 xtraeme "critical-capacity");
1076 1.87 pgoyette ptype = PENVSYS_EVENT_CAPACITY;
1077 1.85 pgoyette }
1078 1.85 pgoyette
1079 1.87 pgoyette if (edata->upropset & PROP_BATTWARN) {
1080 1.85 pgoyette prop_dictionary_remove(sdict,
1081 1.85 pgoyette "warning-capacity");
1082 1.87 pgoyette ptype = PENVSYS_EVENT_CAPACITY;
1083 1.85 pgoyette }
1084 1.96 pgoyette
1085 1.96 pgoyette if (edata->upropset & PROP_BATTHIGH) {
1086 1.96 pgoyette prop_dictionary_remove(sdict,
1087 1.96 pgoyette "high-capacity");
1088 1.96 pgoyette ptype = PENVSYS_EVENT_CAPACITY;
1089 1.96 pgoyette }
1090 1.96 pgoyette
1091 1.96 pgoyette if (edata->upropset & PROP_BATTMAX) {
1092 1.96 pgoyette prop_dictionary_remove(sdict,
1093 1.96 pgoyette "maximum-capacity");
1094 1.96 pgoyette ptype = PENVSYS_EVENT_CAPACITY;
1095 1.96 pgoyette }
1096 1.87 pgoyette if (edata->upropset & PROP_WARNMAX) {
1097 1.87 pgoyette prop_dictionary_remove(sdict, "warning-max");
1098 1.87 pgoyette ptype = PENVSYS_EVENT_LIMITS;
1099 1.85 pgoyette }
1100 1.85 pgoyette
1101 1.87 pgoyette if (edata->upropset & PROP_WARNMIN) {
1102 1.87 pgoyette prop_dictionary_remove(sdict, "warning-min");
1103 1.87 pgoyette ptype = PENVSYS_EVENT_LIMITS;
1104 1.69 xtraeme }
1105 1.69 xtraeme
1106 1.87 pgoyette if (edata->upropset & PROP_CRITMAX) {
1107 1.87 pgoyette prop_dictionary_remove(sdict, "critical-max");
1108 1.87 pgoyette ptype = PENVSYS_EVENT_LIMITS;
1109 1.69 xtraeme }
1110 1.69 xtraeme
1111 1.87 pgoyette if (edata->upropset & PROP_CRITMIN) {
1112 1.87 pgoyette prop_dictionary_remove(sdict, "critical-min");
1113 1.87 pgoyette ptype = PENVSYS_EVENT_LIMITS;
1114 1.85 pgoyette }
1115 1.87 pgoyette if (edata->upropset & PROP_RFACT) {
1116 1.69 xtraeme (void)sme_sensor_upint32(sdict, "rfact", 0);
1117 1.69 xtraeme edata->rfact = 0;
1118 1.69 xtraeme }
1119 1.69 xtraeme
1120 1.87 pgoyette if (edata->upropset & PROP_DESC)
1121 1.69 xtraeme (void)sme_sensor_upstring(sdict,
1122 1.69 xtraeme "description", edata->desc);
1123 1.72 xtraeme
1124 1.106 pgoyette if (ptype == 0)
1125 1.106 pgoyette continue;
1126 1.106 pgoyette
1127 1.106 pgoyette /*
1128 1.106 pgoyette * If there were any limit values removed, we
1129 1.106 pgoyette * need to revert to initial limits.
1130 1.106 pgoyette *
1131 1.106 pgoyette * First, tell the driver that we need it to
1132 1.106 pgoyette * restore any h/w limits which may have been
1133 1.109 pgoyette * changed to stored, boot-time values.
1134 1.106 pgoyette */
1135 1.106 pgoyette if (sme->sme_set_limits) {
1136 1.106 pgoyette DPRINTF(("%s: reset limits for %s %s\n",
1137 1.106 pgoyette __func__, sme->sme_name, edata->desc));
1138 1.106 pgoyette (*sme->sme_set_limits)(sme, edata, NULL, NULL);
1139 1.106 pgoyette }
1140 1.109 pgoyette
1141 1.109 pgoyette /*
1142 1.109 pgoyette * Next, we need to retrieve those initial limits.
1143 1.109 pgoyette */
1144 1.109 pgoyette edata->upropset &= ~PROP_LIMITS;
1145 1.106 pgoyette if (sme->sme_get_limits) {
1146 1.106 pgoyette DPRINTF(("%s: retrieve limits for %s %s\n",
1147 1.106 pgoyette __func__, sme->sme_name, edata->desc));
1148 1.106 pgoyette lims = edata->limits;
1149 1.106 pgoyette (*sme->sme_get_limits)(sme, edata, &lims,
1150 1.109 pgoyette &props);
1151 1.109 pgoyette }
1152 1.108 pgoyette
1153 1.109 pgoyette /*
1154 1.109 pgoyette * Finally, remove any old limits event, then
1155 1.109 pgoyette * install a new event (which will update the
1156 1.109 pgoyette * dictionary)
1157 1.109 pgoyette */
1158 1.109 pgoyette sme_event_unregister(sme, edata->desc,
1159 1.109 pgoyette PENVSYS_EVENT_LIMITS);
1160 1.109 pgoyette
1161 1.109 pgoyette if (props & PROP_LIMITS) {
1162 1.106 pgoyette DPRINTF(("%s: install limits for %s %s\n",
1163 1.106 pgoyette __func__, sme->sme_name, edata->desc));
1164 1.109 pgoyette
1165 1.109 pgoyette
1166 1.109 pgoyette /*
1167 1.109 pgoyette * Find the correct units for this sensor.
1168 1.109 pgoyette */
1169 1.109 pgoyette sdt_units =
1170 1.109 pgoyette sme_get_description_table(SME_DESC_UNITS);
1171 1.109 pgoyette for (i = 0; sdt_units[i].type != -1; i++)
1172 1.109 pgoyette if (sdt_units[i].type == edata->units)
1173 1.109 pgoyette break;
1174 1.109 pgoyette
1175 1.109 pgoyette sme_event_register(sdict, edata, sme,
1176 1.109 pgoyette &lims, props, PENVSYS_EVENT_LIMITS,
1177 1.109 pgoyette sdt_units[i].crittype);
1178 1.106 pgoyette }
1179 1.69 xtraeme }
1180 1.69 xtraeme
1181 1.72 xtraeme /*
1182 1.72 xtraeme * Restore default timeout value.
1183 1.72 xtraeme */
1184 1.72 xtraeme sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1185 1.82 xtraeme sysmon_envsys_release(sme, false);
1186 1.69 xtraeme }
1187 1.82 xtraeme mutex_exit(&sme_global_mtx);
1188 1.69 xtraeme }
1189 1.72 xtraeme
1190 1.69 xtraeme /*
1191 1.72 xtraeme * sme_add_property_dictionary:
1192 1.72 xtraeme *
1193 1.72 xtraeme * + Add global properties into a device.
1194 1.27 xtraeme */
1195 1.27 xtraeme static int
1196 1.72 xtraeme sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1197 1.72 xtraeme prop_dictionary_t dict)
1198 1.27 xtraeme {
1199 1.72 xtraeme prop_dictionary_t pdict;
1200 1.99 pgoyette const char *class;
1201 1.72 xtraeme int error = 0;
1202 1.72 xtraeme
1203 1.72 xtraeme pdict = prop_dictionary_create();
1204 1.72 xtraeme if (!pdict)
1205 1.72 xtraeme return EINVAL;
1206 1.27 xtraeme
1207 1.72 xtraeme /*
1208 1.99 pgoyette * Add the 'refresh-timeout' and 'dev-class' objects into the
1209 1.99 pgoyette * 'device-properties' dictionary.
1210 1.72 xtraeme *
1211 1.72 xtraeme * ...
1212 1.72 xtraeme * <dict>
1213 1.72 xtraeme * <key>device-properties</key>
1214 1.72 xtraeme * <dict>
1215 1.72 xtraeme * <key>refresh-timeout</key>
1216 1.72 xtraeme * <integer>120</integer<
1217 1.99 pgoyette * <key>device-class</key>
1218 1.99 pgoyette * <string>class_name</string>
1219 1.99 pgoyette * </dict>
1220 1.72 xtraeme * </dict>
1221 1.72 xtraeme * ...
1222 1.72 xtraeme *
1223 1.72 xtraeme */
1224 1.72 xtraeme if (!sme->sme_events_timeout)
1225 1.72 xtraeme sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1226 1.35 xtraeme
1227 1.72 xtraeme if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
1228 1.72 xtraeme sme->sme_events_timeout)) {
1229 1.72 xtraeme error = EINVAL;
1230 1.72 xtraeme goto out;
1231 1.27 xtraeme }
1232 1.99 pgoyette if (sme->sme_class == SME_CLASS_BATTERY)
1233 1.99 pgoyette class = "battery";
1234 1.99 pgoyette else if (sme->sme_class == SME_CLASS_ACADAPTER)
1235 1.99 pgoyette class = "ac-adapter";
1236 1.99 pgoyette else
1237 1.99 pgoyette class = "other";
1238 1.99 pgoyette if (!prop_dictionary_set_cstring_nocopy(pdict, "device-class", class)) {
1239 1.99 pgoyette error = EINVAL;
1240 1.99 pgoyette goto out;
1241 1.99 pgoyette }
1242 1.27 xtraeme
1243 1.72 xtraeme if (!prop_dictionary_set(dict, "device-properties", pdict)) {
1244 1.72 xtraeme error = EINVAL;
1245 1.72 xtraeme goto out;
1246 1.72 xtraeme }
1247 1.47 xtraeme
1248 1.72 xtraeme /*
1249 1.72 xtraeme * Add the device dictionary into the sysmon envsys array.
1250 1.72 xtraeme */
1251 1.72 xtraeme if (!prop_array_add(array, dict))
1252 1.72 xtraeme error = EINVAL;
1253 1.27 xtraeme
1254 1.72 xtraeme out:
1255 1.72 xtraeme prop_object_release(pdict);
1256 1.72 xtraeme return error;
1257 1.27 xtraeme }
1258 1.27 xtraeme
1259 1.27 xtraeme /*
1260 1.52 xtraeme * sme_add_sensor_dictionary:
1261 1.18 xtraeme *
1262 1.72 xtraeme * + Adds the sensor objects into the dictionary and returns a pointer
1263 1.72 xtraeme * to a sme_event_drv_t object if a monitoring flag was set
1264 1.72 xtraeme * (or NULL otherwise).
1265 1.18 xtraeme */
1266 1.97 pgoyette static sme_event_drv_t *
1267 1.47 xtraeme sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1268 1.47 xtraeme prop_dictionary_t dict, envsys_data_t *edata)
1269 1.18 xtraeme {
1270 1.55 xtraeme const struct sme_description_table *sdt, *sdt_units;
1271 1.18 xtraeme sme_event_drv_t *sme_evdrv_t = NULL;
1272 1.41 xtraeme int i, j;
1273 1.69 xtraeme char indexstr[ENVSYS_DESCLEN];
1274 1.18 xtraeme
1275 1.72 xtraeme /*
1276 1.72 xtraeme * Find the correct units for this sensor.
1277 1.72 xtraeme */
1278 1.55 xtraeme sdt_units = sme_get_description_table(SME_DESC_UNITS);
1279 1.55 xtraeme for (i = 0; sdt_units[i].type != -1; i++)
1280 1.55 xtraeme if (sdt_units[i].type == edata->units)
1281 1.18 xtraeme break;
1282 1.18 xtraeme
1283 1.18 xtraeme /*
1284 1.69 xtraeme * Add the index sensor string.
1285 1.69 xtraeme *
1286 1.69 xtraeme * ...
1287 1.72 xtraeme * <key>index</eyr
1288 1.69 xtraeme * <string>sensor0</string>
1289 1.69 xtraeme * ...
1290 1.69 xtraeme */
1291 1.69 xtraeme (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
1292 1.69 xtraeme if (sme_sensor_upstring(dict, "index", indexstr))
1293 1.72 xtraeme goto bad;
1294 1.69 xtraeme
1295 1.69 xtraeme /*
1296 1.18 xtraeme * ...
1297 1.18 xtraeme * <key>type</key>
1298 1.18 xtraeme * <string>foo</string>
1299 1.18 xtraeme * <key>description</key>
1300 1.18 xtraeme * <string>blah blah</string>
1301 1.18 xtraeme * ...
1302 1.18 xtraeme */
1303 1.55 xtraeme if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
1304 1.72 xtraeme goto bad;
1305 1.30 xtraeme
1306 1.41 xtraeme if (sme_sensor_upstring(dict, "description", edata->desc))
1307 1.72 xtraeme goto bad;
1308 1.18 xtraeme
1309 1.18 xtraeme /*
1310 1.18 xtraeme * Add sensor's state description.
1311 1.18 xtraeme *
1312 1.18 xtraeme * ...
1313 1.18 xtraeme * <key>state</key>
1314 1.18 xtraeme * <string>valid</string>
1315 1.18 xtraeme * ...
1316 1.18 xtraeme */
1317 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_STATES);
1318 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1319 1.55 xtraeme if (sdt[j].type == edata->state)
1320 1.7 yamt break;
1321 1.18 xtraeme
1322 1.41 xtraeme DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
1323 1.41 xtraeme __func__, edata->desc, edata->units, edata->state));
1324 1.41 xtraeme
1325 1.55 xtraeme if (sme_sensor_upstring(dict, "state", sdt[j].desc))
1326 1.72 xtraeme goto bad;
1327 1.18 xtraeme
1328 1.18 xtraeme /*
1329 1.18 xtraeme * Add the monitoring boolean object:
1330 1.18 xtraeme *
1331 1.18 xtraeme * ...
1332 1.18 xtraeme * <key>monitoring-supported</key>
1333 1.18 xtraeme * <true/>
1334 1.18 xtraeme * ...
1335 1.18 xtraeme *
1336 1.71 xtraeme * always false on Battery {capacity,charge}, Drive and Indicator types.
1337 1.54 xtraeme * They cannot be monitored.
1338 1.18 xtraeme *
1339 1.18 xtraeme */
1340 1.18 xtraeme if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1341 1.18 xtraeme (edata->units == ENVSYS_INDICATOR) ||
1342 1.54 xtraeme (edata->units == ENVSYS_DRIVE) ||
1343 1.71 xtraeme (edata->units == ENVSYS_BATTERY_CAPACITY) ||
1344 1.71 xtraeme (edata->units == ENVSYS_BATTERY_CHARGE)) {
1345 1.41 xtraeme if (sme_sensor_upbool(dict, "monitoring-supported", false))
1346 1.58 xtraeme goto out;
1347 1.18 xtraeme } else {
1348 1.41 xtraeme if (sme_sensor_upbool(dict, "monitoring-supported", true))
1349 1.58 xtraeme goto out;
1350 1.18 xtraeme }
1351 1.18 xtraeme
1352 1.18 xtraeme /*
1353 1.69 xtraeme * Add the percentage boolean object, true if ENVSYS_FPERCENT
1354 1.69 xtraeme * is set or false otherwise.
1355 1.54 xtraeme *
1356 1.54 xtraeme * ...
1357 1.55 xtraeme * <key>want-percentage</key>
1358 1.55 xtraeme * <true/>
1359 1.54 xtraeme * ...
1360 1.54 xtraeme */
1361 1.55 xtraeme if (edata->flags & ENVSYS_FPERCENT)
1362 1.55 xtraeme if (sme_sensor_upbool(dict, "want-percentage", true))
1363 1.58 xtraeme goto out;
1364 1.54 xtraeme
1365 1.54 xtraeme /*
1366 1.69 xtraeme * Add the allow-rfact boolean object, true if
1367 1.69 xtraeme * ENVSYS_FCHANGERFACT if set or false otherwise.
1368 1.69 xtraeme *
1369 1.69 xtraeme * ...
1370 1.69 xtraeme * <key>allow-rfact</key>
1371 1.69 xtraeme * <true/>
1372 1.69 xtraeme * ...
1373 1.69 xtraeme */
1374 1.69 xtraeme if (edata->units == ENVSYS_SVOLTS_DC ||
1375 1.69 xtraeme edata->units == ENVSYS_SVOLTS_AC) {
1376 1.69 xtraeme if (edata->flags & ENVSYS_FCHANGERFACT) {
1377 1.69 xtraeme if (sme_sensor_upbool(dict, "allow-rfact", true))
1378 1.69 xtraeme goto out;
1379 1.69 xtraeme } else {
1380 1.69 xtraeme if (sme_sensor_upbool(dict, "allow-rfact", false))
1381 1.69 xtraeme goto out;
1382 1.69 xtraeme }
1383 1.69 xtraeme }
1384 1.69 xtraeme
1385 1.69 xtraeme /*
1386 1.71 xtraeme * Add the object for battery capacity sensors:
1387 1.54 xtraeme *
1388 1.54 xtraeme * ...
1389 1.71 xtraeme * <key>battery-capacity</key>
1390 1.55 xtraeme * <string>NORMAL</string>
1391 1.54 xtraeme * ...
1392 1.54 xtraeme */
1393 1.71 xtraeme if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1394 1.71 xtraeme sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
1395 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1396 1.55 xtraeme if (sdt[j].type == edata->value_cur)
1397 1.55 xtraeme break;
1398 1.55 xtraeme
1399 1.71 xtraeme if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc))
1400 1.58 xtraeme goto out;
1401 1.55 xtraeme }
1402 1.54 xtraeme
1403 1.54 xtraeme /*
1404 1.18 xtraeme * Add the drive-state object for drive sensors:
1405 1.18 xtraeme *
1406 1.18 xtraeme * ...
1407 1.18 xtraeme * <key>drive-state</key>
1408 1.18 xtraeme * <string>drive is online</string>
1409 1.18 xtraeme * ...
1410 1.18 xtraeme */
1411 1.18 xtraeme if (edata->units == ENVSYS_DRIVE) {
1412 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1413 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1414 1.55 xtraeme if (sdt[j].type == edata->value_cur)
1415 1.18 xtraeme break;
1416 1.30 xtraeme
1417 1.55 xtraeme if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
1418 1.58 xtraeme goto out;
1419 1.18 xtraeme }
1420 1.18 xtraeme
1421 1.72 xtraeme /*
1422 1.72 xtraeme * Add the following objects if sensor is enabled...
1423 1.54 xtraeme */
1424 1.18 xtraeme if (edata->state == ENVSYS_SVALID) {
1425 1.18 xtraeme /*
1426 1.72 xtraeme * Add the following objects:
1427 1.72 xtraeme *
1428 1.18 xtraeme * ...
1429 1.18 xtraeme * <key>rpms</key>
1430 1.18 xtraeme * <integer>2500</integer>
1431 1.18 xtraeme * <key>rfact</key>
1432 1.18 xtraeme * <integer>10000</integer>
1433 1.18 xtraeme * <key>cur-value</key>
1434 1.72 xtraeme * <integer>1250</integer>
1435 1.72 xtraeme * <key>min-value</key>
1436 1.72 xtraeme * <integer>800</integer>
1437 1.72 xtraeme * <key>max-value</integer>
1438 1.72 xtraeme * <integer>3000</integer>
1439 1.72 xtraeme * <key>avg-value</integer>
1440 1.72 xtraeme * <integer>1400</integer>
1441 1.72 xtraeme * ...
1442 1.72 xtraeme */
1443 1.41 xtraeme if (edata->units == ENVSYS_SFANRPM)
1444 1.41 xtraeme if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
1445 1.58 xtraeme goto out;
1446 1.18 xtraeme
1447 1.41 xtraeme if (edata->units == ENVSYS_SVOLTS_AC ||
1448 1.72 xtraeme edata->units == ENVSYS_SVOLTS_DC)
1449 1.41 xtraeme if (sme_sensor_upint32(dict, "rfact", edata->rfact))
1450 1.58 xtraeme goto out;
1451 1.18 xtraeme
1452 1.41 xtraeme if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
1453 1.58 xtraeme goto out;
1454 1.30 xtraeme
1455 1.41 xtraeme if (edata->flags & ENVSYS_FVALID_MIN) {
1456 1.41 xtraeme if (sme_sensor_upint32(dict,
1457 1.41 xtraeme "min-value",
1458 1.41 xtraeme edata->value_min))
1459 1.72 xtraeme goto out;
1460 1.30 xtraeme }
1461 1.30 xtraeme
1462 1.41 xtraeme if (edata->flags & ENVSYS_FVALID_MAX) {
1463 1.41 xtraeme if (sme_sensor_upint32(dict,
1464 1.41 xtraeme "max-value",
1465 1.41 xtraeme edata->value_max))
1466 1.72 xtraeme goto out;
1467 1.30 xtraeme }
1468 1.30 xtraeme
1469 1.41 xtraeme if (edata->flags & ENVSYS_FVALID_AVG) {
1470 1.41 xtraeme if (sme_sensor_upint32(dict,
1471 1.41 xtraeme "avg-value",
1472 1.72 xtraeme edata->value_avg))
1473 1.72 xtraeme goto out;
1474 1.30 xtraeme }
1475 1.18 xtraeme }
1476 1.18 xtraeme
1477 1.18 xtraeme /*
1478 1.18 xtraeme * ...
1479 1.72 xtraeme * </dict>
1480 1.47 xtraeme *
1481 1.47 xtraeme * Add the dictionary into the array.
1482 1.47 xtraeme *
1483 1.18 xtraeme */
1484 1.72 xtraeme if (!prop_array_add(array, dict)) {
1485 1.41 xtraeme DPRINTF(("%s: prop_array_add\n", __func__));
1486 1.72 xtraeme goto bad;
1487 1.41 xtraeme }
1488 1.41 xtraeme
1489 1.41 xtraeme /*
1490 1.97 pgoyette * Register new event(s) if any monitoring flag was set.
1491 1.41 xtraeme */
1492 1.97 pgoyette if (edata->flags & ENVSYS_FMONANY) {
1493 1.58 xtraeme sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
1494 1.72 xtraeme sme_evdrv_t->sed_sdict = dict;
1495 1.72 xtraeme sme_evdrv_t->sed_edata = edata;
1496 1.72 xtraeme sme_evdrv_t->sed_sme = sme;
1497 1.72 xtraeme sme_evdrv_t->sed_powertype = sdt_units[i].crittype;
1498 1.41 xtraeme }
1499 1.41 xtraeme
1500 1.58 xtraeme out:
1501 1.58 xtraeme return sme_evdrv_t;
1502 1.41 xtraeme
1503 1.72 xtraeme bad:
1504 1.68 xtraeme prop_object_release(dict);
1505 1.72 xtraeme return NULL;
1506 1.18 xtraeme }
1507 1.18 xtraeme
1508 1.18 xtraeme /*
1509 1.92 martin * Find the maximum of all currently reported values.
1510 1.92 martin * The provided callback decides wether a sensor is part of the
1511 1.92 martin * maximum calculation (by returning true) or ignored (callback
1512 1.92 martin * returns false). Example usage: callback selects temperature
1513 1.92 martin * sensors in a given thermal zone, the function calculates the
1514 1.92 martin * maximum currently reported temperature in this zone.
1515 1.92 martin * If the parameter "refresh" is true, new values will be aquired
1516 1.92 martin * from the hardware, if not, the last reported value will be used.
1517 1.92 martin */
1518 1.92 martin uint32_t
1519 1.92 martin sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*),
1520 1.92 martin bool refresh)
1521 1.92 martin {
1522 1.92 martin struct sysmon_envsys *sme;
1523 1.92 martin uint32_t maxv, v;
1524 1.92 martin
1525 1.92 martin maxv = 0;
1526 1.92 martin mutex_enter(&sme_global_mtx);
1527 1.92 martin LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1528 1.92 martin sysmon_envsys_acquire(sme, false);
1529 1.92 martin v = sme_get_max_value(sme, predicate, refresh);
1530 1.92 martin sysmon_envsys_release(sme, false);
1531 1.92 martin if (v > maxv)
1532 1.92 martin maxv = v;
1533 1.92 martin }
1534 1.92 martin mutex_exit(&sme_global_mtx);
1535 1.92 martin return maxv;
1536 1.92 martin }
1537 1.92 martin
1538 1.92 martin static uint32_t
1539 1.92 martin sme_get_max_value(struct sysmon_envsys *sme,
1540 1.92 martin bool (*predicate)(const envsys_data_t*),
1541 1.92 martin bool refresh)
1542 1.92 martin {
1543 1.92 martin envsys_data_t *edata;
1544 1.92 martin uint32_t maxv, v;
1545 1.92 martin
1546 1.92 martin /*
1547 1.93 martin * Iterate over all sensors that match the predicate
1548 1.92 martin */
1549 1.92 martin maxv = 0;
1550 1.92 martin TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1551 1.92 martin if (!(*predicate)(edata))
1552 1.92 martin continue;
1553 1.92 martin
1554 1.92 martin /*
1555 1.92 martin * refresh sensor data via sme_refresh only if the
1556 1.92 martin * flag is not set.
1557 1.92 martin */
1558 1.92 martin if (refresh && (sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
1559 1.92 martin mutex_enter(&sme->sme_mtx);
1560 1.92 martin (*sme->sme_refresh)(sme, edata);
1561 1.92 martin mutex_exit(&sme->sme_mtx);
1562 1.92 martin }
1563 1.92 martin
1564 1.92 martin v = edata->value_cur;
1565 1.92 martin if (v > maxv)
1566 1.92 martin maxv = v;
1567 1.92 martin
1568 1.92 martin }
1569 1.92 martin
1570 1.92 martin return maxv;
1571 1.92 martin }
1572 1.92 martin
1573 1.92 martin /*
1574 1.18 xtraeme * sme_update_dictionary:
1575 1.18 xtraeme *
1576 1.18 xtraeme * + Update per-sensor dictionaries with new values if there were
1577 1.18 xtraeme * changes, otherwise the object in dictionary is untouched.
1578 1.18 xtraeme */
1579 1.18 xtraeme int
1580 1.18 xtraeme sme_update_dictionary(struct sysmon_envsys *sme)
1581 1.18 xtraeme {
1582 1.55 xtraeme const struct sme_description_table *sdt;
1583 1.55 xtraeme envsys_data_t *edata;
1584 1.72 xtraeme prop_object_t array, dict, obj, obj2;
1585 1.72 xtraeme int j, error = 0;
1586 1.18 xtraeme
1587 1.72 xtraeme /*
1588 1.72 xtraeme * Retrieve the array of dictionaries in device.
1589 1.72 xtraeme */
1590 1.18 xtraeme array = prop_dictionary_get(sme_propd, sme->sme_name);
1591 1.24 xtraeme if (prop_object_type(array) != PROP_TYPE_ARRAY) {
1592 1.24 xtraeme DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
1593 1.18 xtraeme return EINVAL;
1594 1.24 xtraeme }
1595 1.18 xtraeme
1596 1.72 xtraeme /*
1597 1.72 xtraeme * Get the last dictionary on the array, this contains the
1598 1.72 xtraeme * 'device-properties' sub-dictionary.
1599 1.72 xtraeme */
1600 1.72 xtraeme obj = prop_array_get(array, prop_array_count(array) - 1);
1601 1.72 xtraeme if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
1602 1.72 xtraeme DPRINTF(("%s: not a device-properties dictionary\n", __func__));
1603 1.72 xtraeme return EINVAL;
1604 1.72 xtraeme }
1605 1.72 xtraeme
1606 1.72 xtraeme obj2 = prop_dictionary_get(obj, "device-properties");
1607 1.72 xtraeme if (!obj2)
1608 1.72 xtraeme return EINVAL;
1609 1.72 xtraeme
1610 1.72 xtraeme /*
1611 1.72 xtraeme * Update the 'refresh-timeout' property.
1612 1.72 xtraeme */
1613 1.72 xtraeme if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
1614 1.72 xtraeme sme->sme_events_timeout))
1615 1.72 xtraeme return EINVAL;
1616 1.72 xtraeme
1617 1.18 xtraeme /*
1618 1.18 xtraeme * - iterate over all sensors.
1619 1.18 xtraeme * - fetch new data.
1620 1.18 xtraeme * - check if data in dictionary is different than new data.
1621 1.18 xtraeme * - update dictionary if there were changes.
1622 1.18 xtraeme */
1623 1.56 xtraeme DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1624 1.56 xtraeme sme->sme_name, sme->sme_nsensors));
1625 1.56 xtraeme
1626 1.82 xtraeme /*
1627 1.82 xtraeme * Don't bother with locking when traversing the queue,
1628 1.82 xtraeme * the device is already marked as busy; if a sensor
1629 1.82 xtraeme * is going to be removed or added it will have to wait.
1630 1.82 xtraeme */
1631 1.72 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1632 1.18 xtraeme /*
1633 1.72 xtraeme * refresh sensor data via sme_refresh only if the
1634 1.18 xtraeme * flag is not set.
1635 1.18 xtraeme */
1636 1.82 xtraeme if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
1637 1.82 xtraeme mutex_enter(&sme->sme_mtx);
1638 1.72 xtraeme (*sme->sme_refresh)(sme, edata);
1639 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1640 1.82 xtraeme }
1641 1.18 xtraeme
1642 1.72 xtraeme /*
1643 1.72 xtraeme * retrieve sensor's dictionary.
1644 1.72 xtraeme */
1645 1.72 xtraeme dict = prop_array_get(array, edata->sensor);
1646 1.24 xtraeme if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1647 1.24 xtraeme DPRINTF(("%s: not a dictionary (%d:%s)\n",
1648 1.24 xtraeme __func__, edata->sensor, sme->sme_name));
1649 1.18 xtraeme return EINVAL;
1650 1.24 xtraeme }
1651 1.18 xtraeme
1652 1.72 xtraeme /*
1653 1.72 xtraeme * update sensor's state.
1654 1.72 xtraeme */
1655 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_STATES);
1656 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1657 1.55 xtraeme if (sdt[j].type == edata->state)
1658 1.18 xtraeme break;
1659 1.18 xtraeme
1660 1.87 pgoyette DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n",
1661 1.87 pgoyette __func__, edata->sensor, sdt[j].type, sdt[j].desc,
1662 1.87 pgoyette edata->flags));
1663 1.18 xtraeme
1664 1.55 xtraeme error = sme_sensor_upstring(dict, "state", sdt[j].desc);
1665 1.30 xtraeme if (error)
1666 1.30 xtraeme break;
1667 1.18 xtraeme
1668 1.72 xtraeme /*
1669 1.72 xtraeme * update sensor's type.
1670 1.72 xtraeme */
1671 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_UNITS);
1672 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1673 1.55 xtraeme if (sdt[j].type == edata->units)
1674 1.45 xtraeme break;
1675 1.45 xtraeme
1676 1.87 pgoyette DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n",
1677 1.87 pgoyette __func__, edata->sensor, sdt[j].type, sdt[j].desc));
1678 1.87 pgoyette
1679 1.55 xtraeme error = sme_sensor_upstring(dict, "type", sdt[j].desc);
1680 1.45 xtraeme if (error)
1681 1.45 xtraeme break;
1682 1.45 xtraeme
1683 1.72 xtraeme /*
1684 1.72 xtraeme * update sensor's current value.
1685 1.72 xtraeme */
1686 1.34 xtraeme error = sme_sensor_upint32(dict,
1687 1.30 xtraeme "cur-value",
1688 1.30 xtraeme edata->value_cur);
1689 1.30 xtraeme if (error)
1690 1.30 xtraeme break;
1691 1.18 xtraeme
1692 1.18 xtraeme /*
1693 1.71 xtraeme * Battery charge, Integer and Indicator types do not
1694 1.71 xtraeme * need the following objects, so skip them.
1695 1.18 xtraeme */
1696 1.18 xtraeme if (edata->units == ENVSYS_INTEGER ||
1697 1.71 xtraeme edata->units == ENVSYS_INDICATOR ||
1698 1.71 xtraeme edata->units == ENVSYS_BATTERY_CHARGE)
1699 1.18 xtraeme continue;
1700 1.18 xtraeme
1701 1.72 xtraeme /*
1702 1.72 xtraeme * update sensor flags.
1703 1.72 xtraeme */
1704 1.30 xtraeme if (edata->flags & ENVSYS_FPERCENT) {
1705 1.34 xtraeme error = sme_sensor_upbool(dict,
1706 1.30 xtraeme "want-percentage",
1707 1.30 xtraeme true);
1708 1.30 xtraeme if (error)
1709 1.30 xtraeme break;
1710 1.18 xtraeme }
1711 1.18 xtraeme
1712 1.72 xtraeme /*
1713 1.72 xtraeme * update sensor's {avg,max,min}-value.
1714 1.72 xtraeme */
1715 1.30 xtraeme if (edata->flags & ENVSYS_FVALID_MAX) {
1716 1.34 xtraeme error = sme_sensor_upint32(dict,
1717 1.30 xtraeme "max-value",
1718 1.30 xtraeme edata->value_max);
1719 1.30 xtraeme if (error)
1720 1.30 xtraeme break;
1721 1.30 xtraeme }
1722 1.30 xtraeme
1723 1.30 xtraeme if (edata->flags & ENVSYS_FVALID_MIN) {
1724 1.34 xtraeme error = sme_sensor_upint32(dict,
1725 1.30 xtraeme "min-value",
1726 1.30 xtraeme edata->value_min);
1727 1.30 xtraeme if (error)
1728 1.30 xtraeme break;
1729 1.30 xtraeme }
1730 1.18 xtraeme
1731 1.30 xtraeme if (edata->flags & ENVSYS_FVALID_AVG) {
1732 1.34 xtraeme error = sme_sensor_upint32(dict,
1733 1.30 xtraeme "avg-value",
1734 1.30 xtraeme edata->value_avg);
1735 1.30 xtraeme if (error)
1736 1.30 xtraeme break;
1737 1.30 xtraeme }
1738 1.18 xtraeme
1739 1.72 xtraeme /*
1740 1.72 xtraeme * update 'rpms' only for ENVSYS_SFANRPM sensors.
1741 1.72 xtraeme */
1742 1.30 xtraeme if (edata->units == ENVSYS_SFANRPM) {
1743 1.34 xtraeme error = sme_sensor_upuint32(dict,
1744 1.30 xtraeme "rpms",
1745 1.30 xtraeme edata->rpms);
1746 1.30 xtraeme if (error)
1747 1.30 xtraeme break;
1748 1.30 xtraeme }
1749 1.18 xtraeme
1750 1.72 xtraeme /*
1751 1.72 xtraeme * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
1752 1.72 xtraeme */
1753 1.18 xtraeme if (edata->units == ENVSYS_SVOLTS_AC ||
1754 1.18 xtraeme edata->units == ENVSYS_SVOLTS_DC) {
1755 1.34 xtraeme error = sme_sensor_upint32(dict,
1756 1.30 xtraeme "rfact",
1757 1.30 xtraeme edata->rfact);
1758 1.30 xtraeme if (error)
1759 1.30 xtraeme break;
1760 1.18 xtraeme }
1761 1.18 xtraeme
1762 1.72 xtraeme /*
1763 1.72 xtraeme * update 'drive-state' only for ENVSYS_DRIVE sensors.
1764 1.72 xtraeme */
1765 1.18 xtraeme if (edata->units == ENVSYS_DRIVE) {
1766 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1767 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1768 1.55 xtraeme if (sdt[j].type == edata->value_cur)
1769 1.18 xtraeme break;
1770 1.18 xtraeme
1771 1.34 xtraeme error = sme_sensor_upstring(dict,
1772 1.30 xtraeme "drive-state",
1773 1.55 xtraeme sdt[j].desc);
1774 1.55 xtraeme if (error)
1775 1.55 xtraeme break;
1776 1.55 xtraeme }
1777 1.55 xtraeme
1778 1.72 xtraeme /*
1779 1.72 xtraeme * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
1780 1.72 xtraeme * sensors.
1781 1.72 xtraeme */
1782 1.71 xtraeme if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1783 1.55 xtraeme sdt =
1784 1.72 xtraeme sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
1785 1.55 xtraeme for (j = 0; sdt[j].type != -1; j++)
1786 1.55 xtraeme if (sdt[j].type == edata->value_cur)
1787 1.55 xtraeme break;
1788 1.55 xtraeme
1789 1.55 xtraeme error = sme_sensor_upstring(dict,
1790 1.71 xtraeme "battery-capacity",
1791 1.55 xtraeme sdt[j].desc);
1792 1.32 xtraeme if (error)
1793 1.32 xtraeme break;
1794 1.7 yamt }
1795 1.1 thorpej }
1796 1.1 thorpej
1797 1.18 xtraeme return error;
1798 1.1 thorpej }
1799 1.1 thorpej
1800 1.1 thorpej /*
1801 1.18 xtraeme * sme_userset_dictionary:
1802 1.1 thorpej *
1803 1.72 xtraeme * + Parse the userland dictionary and run the appropiate tasks
1804 1.72 xtraeme * that were specified.
1805 1.1 thorpej */
1806 1.18 xtraeme int
1807 1.18 xtraeme sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1808 1.18 xtraeme prop_array_t array)
1809 1.1 thorpej {
1810 1.55 xtraeme const struct sme_description_table *sdt;
1811 1.72 xtraeme envsys_data_t *edata;
1812 1.72 xtraeme prop_dictionary_t dict, tdict = NULL;
1813 1.72 xtraeme prop_object_t obj, obj1, obj2, tobj = NULL;
1814 1.95 pgoyette uint32_t props;
1815 1.72 xtraeme uint64_t refresh_timo = 0;
1816 1.88 pgoyette sysmon_envsys_lim_t lims;
1817 1.72 xtraeme int i, error = 0;
1818 1.72 xtraeme const char *blah;
1819 1.18 xtraeme bool targetfound = false;
1820 1.18 xtraeme
1821 1.72 xtraeme /*
1822 1.72 xtraeme * The user wanted to change the refresh timeout value for this
1823 1.72 xtraeme * device.
1824 1.72 xtraeme *
1825 1.72 xtraeme * Get the 'device-properties' object from the userland dictionary.
1826 1.72 xtraeme */
1827 1.72 xtraeme obj = prop_dictionary_get(udict, "device-properties");
1828 1.72 xtraeme if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
1829 1.72 xtraeme /*
1830 1.72 xtraeme * Get the 'refresh-timeout' property for this device.
1831 1.72 xtraeme */
1832 1.72 xtraeme obj1 = prop_dictionary_get(obj, "refresh-timeout");
1833 1.72 xtraeme if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
1834 1.72 xtraeme targetfound = true;
1835 1.72 xtraeme refresh_timo =
1836 1.72 xtraeme prop_number_unsigned_integer_value(obj1);
1837 1.72 xtraeme if (refresh_timo < 1)
1838 1.72 xtraeme error = EINVAL;
1839 1.82 xtraeme else {
1840 1.82 xtraeme mutex_enter(&sme->sme_mtx);
1841 1.72 xtraeme sme->sme_events_timeout = refresh_timo;
1842 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1843 1.82 xtraeme }
1844 1.72 xtraeme }
1845 1.82 xtraeme return error;
1846 1.1 thorpej
1847 1.72 xtraeme } else if (!obj) {
1848 1.69 xtraeme /*
1849 1.72 xtraeme * Get sensor's index from userland dictionary.
1850 1.69 xtraeme */
1851 1.72 xtraeme obj = prop_dictionary_get(udict, "index");
1852 1.72 xtraeme if (!obj)
1853 1.82 xtraeme return EINVAL;
1854 1.72 xtraeme if (prop_object_type(obj) != PROP_TYPE_STRING) {
1855 1.72 xtraeme DPRINTF(("%s: 'index' not a string\n", __func__));
1856 1.72 xtraeme return EINVAL;
1857 1.40 xtraeme }
1858 1.72 xtraeme } else
1859 1.72 xtraeme return EINVAL;
1860 1.27 xtraeme
1861 1.82 xtraeme /*
1862 1.82 xtraeme * Don't bother with locking when traversing the queue,
1863 1.82 xtraeme * the device is already marked as busy; if a sensor
1864 1.82 xtraeme * is going to be removed or added it will have to wait.
1865 1.72 xtraeme */
1866 1.72 xtraeme TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1867 1.72 xtraeme /*
1868 1.72 xtraeme * Get a dictionary and check if it's our sensor by checking
1869 1.72 xtraeme * at its index position.
1870 1.72 xtraeme */
1871 1.72 xtraeme dict = prop_array_get(array, edata->sensor);
1872 1.69 xtraeme obj1 = prop_dictionary_get(dict, "index");
1873 1.18 xtraeme
1874 1.72 xtraeme /*
1875 1.72 xtraeme * is it our sensor?
1876 1.72 xtraeme */
1877 1.18 xtraeme if (!prop_string_equals(obj1, obj))
1878 1.18 xtraeme continue;
1879 1.18 xtraeme
1880 1.95 pgoyette props = 0;
1881 1.88 pgoyette
1882 1.18 xtraeme /*
1883 1.18 xtraeme * Check if a new description operation was
1884 1.18 xtraeme * requested by the user and set new description.
1885 1.18 xtraeme */
1886 1.72 xtraeme obj2 = prop_dictionary_get(udict, "description");
1887 1.72 xtraeme if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
1888 1.18 xtraeme targetfound = true;
1889 1.18 xtraeme blah = prop_string_cstring_nocopy(obj2);
1890 1.72 xtraeme
1891 1.72 xtraeme /*
1892 1.72 xtraeme * Check for duplicate description.
1893 1.72 xtraeme */
1894 1.23 xtraeme for (i = 0; i < sme->sme_nsensors; i++) {
1895 1.23 xtraeme if (i == edata->sensor)
1896 1.23 xtraeme continue;
1897 1.72 xtraeme tdict = prop_array_get(array, i);
1898 1.72 xtraeme tobj =
1899 1.72 xtraeme prop_dictionary_get(tdict, "description");
1900 1.82 xtraeme if (prop_string_equals(obj2, tobj)) {
1901 1.82 xtraeme error = EEXIST;
1902 1.82 xtraeme goto out;
1903 1.82 xtraeme }
1904 1.23 xtraeme }
1905 1.23 xtraeme
1906 1.72 xtraeme /*
1907 1.72 xtraeme * Update the object in dictionary.
1908 1.72 xtraeme */
1909 1.82 xtraeme mutex_enter(&sme->sme_mtx);
1910 1.34 xtraeme error = sme_sensor_upstring(dict,
1911 1.30 xtraeme "description",
1912 1.30 xtraeme blah);
1913 1.82 xtraeme if (error) {
1914 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1915 1.82 xtraeme goto out;
1916 1.82 xtraeme }
1917 1.18 xtraeme
1918 1.72 xtraeme DPRINTF(("%s: sensor%d changed desc to: %s\n",
1919 1.72 xtraeme __func__, edata->sensor, blah));
1920 1.87 pgoyette edata->upropset |= PROP_DESC;
1921 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1922 1.18 xtraeme }
1923 1.18 xtraeme
1924 1.69 xtraeme /*
1925 1.69 xtraeme * did the user want to change the rfact?
1926 1.69 xtraeme */
1927 1.69 xtraeme obj2 = prop_dictionary_get(udict, "rfact");
1928 1.72 xtraeme if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1929 1.18 xtraeme targetfound = true;
1930 1.69 xtraeme if (edata->flags & ENVSYS_FCHANGERFACT) {
1931 1.82 xtraeme mutex_enter(&sme->sme_mtx);
1932 1.18 xtraeme edata->rfact = prop_number_integer_value(obj2);
1933 1.87 pgoyette edata->upropset |= PROP_RFACT;
1934 1.82 xtraeme mutex_exit(&sme->sme_mtx);
1935 1.72 xtraeme DPRINTF(("%s: sensor%d changed rfact to %d\n",
1936 1.72 xtraeme __func__, edata->sensor, edata->rfact));
1937 1.82 xtraeme } else {
1938 1.82 xtraeme error = ENOTSUP;
1939 1.82 xtraeme goto out;
1940 1.82 xtraeme }
1941 1.18 xtraeme }
1942 1.18 xtraeme
1943 1.55 xtraeme sdt = sme_get_description_table(SME_DESC_UNITS);
1944 1.55 xtraeme for (i = 0; sdt[i].type != -1; i++)
1945 1.55 xtraeme if (sdt[i].type == edata->units)
1946 1.18 xtraeme break;
1947 1.18 xtraeme
1948 1.69 xtraeme /*
1949 1.69 xtraeme * did the user want to set a critical capacity event?
1950 1.69 xtraeme */
1951 1.18 xtraeme obj2 = prop_dictionary_get(udict, "critical-capacity");
1952 1.72 xtraeme if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1953 1.18 xtraeme targetfound = true;
1954 1.88 pgoyette lims.sel_critmin = prop_number_integer_value(obj2);
1955 1.95 pgoyette props |= PROP_BATTCAP;
1956 1.18 xtraeme }
1957 1.18 xtraeme
1958 1.69 xtraeme /*
1959 1.85 pgoyette * did the user want to set a warning capacity event?
1960 1.85 pgoyette */
1961 1.85 pgoyette obj2 = prop_dictionary_get(udict, "warning-capacity");
1962 1.85 pgoyette if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1963 1.85 pgoyette targetfound = true;
1964 1.88 pgoyette lims.sel_warnmin = prop_number_integer_value(obj2);
1965 1.95 pgoyette props |= PROP_BATTWARN;
1966 1.85 pgoyette }
1967 1.85 pgoyette
1968 1.85 pgoyette /*
1969 1.96 pgoyette * did the user want to set a high capacity event?
1970 1.96 pgoyette */
1971 1.96 pgoyette obj2 = prop_dictionary_get(udict, "high-capacity");
1972 1.96 pgoyette if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1973 1.96 pgoyette targetfound = true;
1974 1.96 pgoyette lims.sel_warnmin = prop_number_integer_value(obj2);
1975 1.96 pgoyette props |= PROP_BATTHIGH;
1976 1.96 pgoyette }
1977 1.96 pgoyette
1978 1.96 pgoyette /*
1979 1.96 pgoyette * did the user want to set a maximum capacity event?
1980 1.96 pgoyette */
1981 1.96 pgoyette obj2 = prop_dictionary_get(udict, "maximum-capacity");
1982 1.96 pgoyette if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1983 1.96 pgoyette targetfound = true;
1984 1.96 pgoyette lims.sel_warnmin = prop_number_integer_value(obj2);
1985 1.96 pgoyette props |= PROP_BATTMAX;
1986 1.96 pgoyette }
1987 1.96 pgoyette
1988 1.96 pgoyette /*
1989 1.69 xtraeme * did the user want to set a critical max event?
1990 1.69 xtraeme */
1991 1.69 xtraeme obj2 = prop_dictionary_get(udict, "critical-max");
1992 1.72 xtraeme if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1993 1.18 xtraeme targetfound = true;
1994 1.88 pgoyette lims.sel_critmax = prop_number_integer_value(obj2);
1995 1.95 pgoyette props |= PROP_CRITMAX;
1996 1.18 xtraeme }
1997 1.18 xtraeme
1998 1.69 xtraeme /*
1999 1.85 pgoyette * did the user want to set a warning max event?
2000 1.85 pgoyette */
2001 1.85 pgoyette obj2 = prop_dictionary_get(udict, "warning-max");
2002 1.85 pgoyette if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
2003 1.85 pgoyette targetfound = true;
2004 1.88 pgoyette lims.sel_warnmax = prop_number_integer_value(obj2);
2005 1.95 pgoyette props |= PROP_WARNMAX;
2006 1.85 pgoyette }
2007 1.85 pgoyette
2008 1.85 pgoyette /*
2009 1.69 xtraeme * did the user want to set a critical min event?
2010 1.69 xtraeme */
2011 1.69 xtraeme obj2 = prop_dictionary_get(udict, "critical-min");
2012 1.72 xtraeme if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
2013 1.18 xtraeme targetfound = true;
2014 1.88 pgoyette lims.sel_critmin = prop_number_integer_value(obj2);
2015 1.95 pgoyette props |= PROP_CRITMIN;
2016 1.18 xtraeme }
2017 1.69 xtraeme
2018 1.85 pgoyette /*
2019 1.85 pgoyette * did the user want to set a warning min event?
2020 1.85 pgoyette */
2021 1.85 pgoyette obj2 = prop_dictionary_get(udict, "warning-min");
2022 1.85 pgoyette if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
2023 1.85 pgoyette targetfound = true;
2024 1.88 pgoyette lims.sel_warnmin = prop_number_integer_value(obj2);
2025 1.95 pgoyette props |= PROP_WARNMIN;
2026 1.87 pgoyette }
2027 1.87 pgoyette
2028 1.95 pgoyette if (props) {
2029 1.91 pgoyette if (edata->flags & ENVSYS_FMONNOTSUPP) {
2030 1.91 pgoyette error = ENOTSUP;
2031 1.91 pgoyette goto out;
2032 1.91 pgoyette }
2033 1.88 pgoyette error = sme_event_register(dict, edata, sme, &lims,
2034 1.95 pgoyette props,
2035 1.95 pgoyette (edata->flags & ENVSYS_FPERCENT)?
2036 1.87 pgoyette PENVSYS_EVENT_CAPACITY:
2037 1.87 pgoyette PENVSYS_EVENT_LIMITS,
2038 1.95 pgoyette sdt[i].crittype);
2039 1.85 pgoyette if (error == EEXIST)
2040 1.85 pgoyette error = 0;
2041 1.85 pgoyette if (error)
2042 1.85 pgoyette goto out;
2043 1.85 pgoyette }
2044 1.85 pgoyette
2045 1.69 xtraeme /*
2046 1.69 xtraeme * All objects in dictionary were processed.
2047 1.69 xtraeme */
2048 1.69 xtraeme break;
2049 1.18 xtraeme }
2050 1.18 xtraeme
2051 1.72 xtraeme out:
2052 1.72 xtraeme /*
2053 1.72 xtraeme * invalid target? return the error.
2054 1.72 xtraeme */
2055 1.18 xtraeme if (!targetfound)
2056 1.18 xtraeme error = EINVAL;
2057 1.18 xtraeme
2058 1.18 xtraeme return error;
2059 1.1 thorpej }
2060 1.100 pgoyette
2061 1.100 pgoyette /*
2062 1.100 pgoyette * + sysmon_envsys_foreach_sensor
2063 1.100 pgoyette *
2064 1.100 pgoyette * Walk through the devices' sensor lists and execute the callback.
2065 1.100 pgoyette * If the callback returns false, the remainder of the current
2066 1.100 pgoyette * device's sensors are skipped.
2067 1.100 pgoyette */
2068 1.100 pgoyette void
2069 1.107 pgoyette sysmon_envsys_foreach_sensor(sysmon_envsys_callback_t func, void *arg,
2070 1.107 pgoyette bool refresh)
2071 1.100 pgoyette {
2072 1.100 pgoyette struct sysmon_envsys *sme;
2073 1.100 pgoyette envsys_data_t *sensor;
2074 1.100 pgoyette
2075 1.100 pgoyette mutex_enter(&sme_global_mtx);
2076 1.100 pgoyette LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
2077 1.100 pgoyette
2078 1.102 pgoyette sysmon_envsys_acquire(sme, false);
2079 1.100 pgoyette TAILQ_FOREACH(sensor, &sme->sme_sensors_list, sensors_head) {
2080 1.102 pgoyette if (refresh &&
2081 1.102 pgoyette (sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
2082 1.102 pgoyette mutex_enter(&sme->sme_mtx);
2083 1.102 pgoyette (*sme->sme_refresh)(sme, sensor);
2084 1.102 pgoyette mutex_exit(&sme->sme_mtx);
2085 1.102 pgoyette }
2086 1.101 pgoyette if (!(*func)(sme, sensor, arg))
2087 1.100 pgoyette break;
2088 1.100 pgoyette }
2089 1.102 pgoyette sysmon_envsys_release(sme, false);
2090 1.100 pgoyette }
2091 1.100 pgoyette mutex_exit(&sme_global_mtx);
2092 1.100 pgoyette }
2093