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