sysmon_envsys.c revision 1.124 1 /* $NetBSD: sysmon_envsys.c,v 1.124 2012/08/27 14:15:52 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.124 2012/08/27 14:15:52 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_POLL_ONLY) == 0) {
362 mutex_enter(&sme->sme_mtx);
363 sysmon_envsys_refresh_sensor(sme, edata);
364 mutex_exit(&sme->sme_mtx);
365 }
366
367 /*
368 * copy required values to the old interface.
369 */
370 tred->sensor = edata->sensor;
371 tred->cur.data_us = edata->value_cur;
372 tred->cur.data_s = edata->value_cur;
373 tred->max.data_us = edata->value_max;
374 tred->max.data_s = edata->value_max;
375 tred->min.data_us = edata->value_min;
376 tred->min.data_s = edata->value_min;
377 tred->avg.data_us = 0;
378 tred->avg.data_s = 0;
379 if (edata->units == ENVSYS_BATTERY_CHARGE)
380 tred->units = ENVSYS_INDICATOR;
381 else
382 tred->units = edata->units;
383
384 tred->validflags |= ENVSYS_FVALID;
385 tred->validflags |= ENVSYS_FCURVALID;
386
387 if (edata->flags & ENVSYS_FPERCENT) {
388 tred->validflags |= ENVSYS_FMAXVALID;
389 tred->validflags |= ENVSYS_FFRACVALID;
390 }
391
392 if (edata->state == ENVSYS_SINVALID) {
393 tred->validflags &= ~ENVSYS_FCURVALID;
394 tred->cur.data_us = tred->cur.data_s = 0;
395 }
396
397 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
398 __func__, edata->desc, tred->cur.data_s));
399 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
400 " tred->sensor=%d\n", __func__, tred->validflags,
401 tred->units, tred->sensor));
402 }
403 tred->sensor = oidx;
404 sysmon_envsys_release(sme, false);
405
406 break;
407 }
408 case ENVSYS_GTREINFO:
409 {
410 struct envsys_basic_info *binfo = (void *)data;
411 envsys_data_t *edata = NULL;
412 bool found = false;
413
414 binfo->validflags = 0;
415
416 sme = sysmon_envsys_find_40(binfo->sensor);
417 if (!sme)
418 break;
419
420 oidx = binfo->sensor;
421 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
422
423 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
424 if (edata->sensor == binfo->sensor) {
425 found = true;
426 break;
427 }
428 }
429
430 if (!found) {
431 sysmon_envsys_release(sme, false);
432 error = ENODEV;
433 break;
434 }
435
436 binfo->validflags |= ENVSYS_FVALID;
437
438 if (binfo->sensor < sme->sme_nsensors) {
439 if (edata->units == ENVSYS_BATTERY_CHARGE)
440 binfo->units = ENVSYS_INDICATOR;
441 else
442 binfo->units = edata->units;
443
444 /*
445 * previously, the ACPI sensor names included the
446 * device name. Include that in compatibility code.
447 */
448 if (strncmp(sme->sme_name, "acpi", 4) == 0)
449 (void)snprintf(binfo->desc, sizeof(binfo->desc),
450 "%s %s", sme->sme_name, edata->desc);
451 else
452 (void)strlcpy(binfo->desc, edata->desc,
453 sizeof(binfo->desc));
454 }
455
456 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
457 __func__, binfo->units, binfo->validflags));
458 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
459 __func__, binfo->desc, binfo->sensor));
460
461 binfo->sensor = oidx;
462 sysmon_envsys_release(sme, false);
463
464 break;
465 }
466 default:
467 error = ENOTTY;
468 break;
469 }
470
471 return error;
472 }
473
474 /*
475 * sysmon_envsys_create:
476 *
477 * + Allocates a new sysmon_envsys object and initializes the
478 * stuff for sensors and events.
479 */
480 struct sysmon_envsys *
481 sysmon_envsys_create(void)
482 {
483 struct sysmon_envsys *sme;
484
485 sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
486 TAILQ_INIT(&sme->sme_sensors_list);
487 LIST_INIT(&sme->sme_events_list);
488 mutex_init(&sme->sme_mtx, MUTEX_DEFAULT, IPL_NONE);
489 cv_init(&sme->sme_condvar, "sme_wait");
490
491 return sme;
492 }
493
494 /*
495 * sysmon_envsys_destroy:
496 *
497 * + Removes all sensors from the tail queue, destroys the callout
498 * and frees the sysmon_envsys object.
499 */
500 void
501 sysmon_envsys_destroy(struct sysmon_envsys *sme)
502 {
503 envsys_data_t *edata;
504
505 KASSERT(sme != NULL);
506
507 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
508 edata = TAILQ_FIRST(&sme->sme_sensors_list);
509 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
510 }
511 mutex_destroy(&sme->sme_mtx);
512 cv_destroy(&sme->sme_condvar);
513 kmem_free(sme, sizeof(*sme));
514 }
515
516 /*
517 * sysmon_envsys_sensor_attach:
518 *
519 * + Attaches a sensor into a sysmon_envsys device checking that units
520 * is set to a valid type and description is unique and not empty.
521 */
522 int
523 sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
524 {
525 const struct sme_descr_entry *sdt_units;
526 envsys_data_t *oedata;
527
528 KASSERT(sme != NULL || edata != NULL);
529
530 /*
531 * Find the correct units for this sensor.
532 */
533 sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units);
534 if (sdt_units->type == -1)
535 return EINVAL;
536
537 /*
538 * Check that description is not empty or duplicate.
539 */
540 if (strlen(edata->desc) == 0)
541 return EINVAL;
542
543 mutex_enter(&sme->sme_mtx);
544 sysmon_envsys_acquire(sme, true);
545 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
546 if (strcmp(oedata->desc, edata->desc) == 0) {
547 sysmon_envsys_release(sme, true);
548 mutex_exit(&sme->sme_mtx);
549 return EEXIST;
550 }
551 }
552 /*
553 * Ok, the sensor has been added into the device queue.
554 */
555 TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
556
557 /*
558 * Give the sensor an index position.
559 */
560 edata->sensor = sme->sme_nsensors;
561 sme->sme_nsensors++;
562 sysmon_envsys_release(sme, true);
563 mutex_exit(&sme->sme_mtx);
564
565 DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n",
566 __func__, edata->sensor, edata->desc,
567 sdt_units->type, sdt_units->desc));
568
569 return 0;
570 }
571
572 /*
573 * sysmon_envsys_sensor_detach:
574 *
575 * + Detachs a sensor from a sysmon_envsys device and decrements the
576 * sensors count on success.
577 */
578 int
579 sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
580 {
581 envsys_data_t *oedata;
582 bool found = false;
583
584 KASSERT(sme != NULL || edata != NULL);
585
586 /*
587 * Check the sensor is already on the list.
588 */
589 mutex_enter(&sme->sme_mtx);
590 sysmon_envsys_acquire(sme, true);
591 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
592 if (oedata->sensor == edata->sensor) {
593 found = true;
594 break;
595 }
596 }
597
598 if (!found) {
599 sysmon_envsys_release(sme, true);
600 mutex_exit(&sme->sme_mtx);
601 return EINVAL;
602 }
603
604 /*
605 * remove it, unhook from rnd(4), and decrement the sensors count.
606 */
607 sme_event_unregister_sensor(sme, edata);
608 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
609 sme->sme_nsensors--;
610 sysmon_envsys_release(sme, true);
611 mutex_exit(&sme->sme_mtx);
612
613 return 0;
614 }
615
616
617 /*
618 * sysmon_envsys_register:
619 *
620 * + Register a sysmon envsys device.
621 * + Create array of dictionaries for a device.
622 */
623 int
624 sysmon_envsys_register(struct sysmon_envsys *sme)
625 {
626 struct sme_evdrv {
627 SLIST_ENTRY(sme_evdrv) evdrv_head;
628 sme_event_drv_t *evdrv;
629 };
630 SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
631 struct sme_evdrv *evdv = NULL;
632 struct sysmon_envsys *lsme;
633 prop_array_t array = NULL;
634 prop_dictionary_t dict, dict2;
635 envsys_data_t *edata = NULL;
636 sme_event_drv_t *this_evdrv;
637 int nevent;
638 int error = 0;
639 char rnd_name[sizeof(edata->rnd_src.name)];
640
641 KASSERT(sme != NULL);
642 KASSERT(sme->sme_name != NULL);
643
644 /*
645 * Check if requested sysmon_envsys device is valid
646 * and does not exist already in the list.
647 */
648 mutex_enter(&sme_global_mtx);
649 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
650 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
651 mutex_exit(&sme_global_mtx);
652 return EEXIST;
653 }
654 }
655 mutex_exit(&sme_global_mtx);
656
657 /*
658 * sanity check: if SME_DISABLE_REFRESH is not set,
659 * the sme_refresh function callback must be non NULL.
660 */
661 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
662 if (!sme->sme_refresh)
663 return EINVAL;
664
665 /*
666 * If the list of sensors is empty, there's no point to continue...
667 */
668 if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
669 DPRINTF(("%s: sensors list empty for %s\n", __func__,
670 sme->sme_name));
671 return ENOTSUP;
672 }
673
674 /*
675 * Initialize the singly linked list for driver events.
676 */
677 SLIST_INIT(&sme_evdrv_list);
678
679 array = prop_array_create();
680 if (!array)
681 return ENOMEM;
682
683 /*
684 * Iterate over all sensors and create a dictionary per sensor.
685 * We must respect the order in which the sensors were added.
686 */
687 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
688 dict = prop_dictionary_create();
689 if (!dict) {
690 error = ENOMEM;
691 goto out2;
692 }
693
694 /*
695 * Create all objects in sensor's dictionary.
696 */
697 this_evdrv = sme_add_sensor_dictionary(sme, array,
698 dict, edata);
699 if (this_evdrv) {
700 evdv = kmem_zalloc(sizeof(*evdv), KM_SLEEP);
701 evdv->evdrv = this_evdrv;
702 SLIST_INSERT_HEAD(&sme_evdrv_list, evdv, evdrv_head);
703 }
704 }
705
706 /*
707 * If the array does not contain any object (sensor), there's
708 * no need to attach the driver.
709 */
710 if (prop_array_count(array) == 0) {
711 error = EINVAL;
712 DPRINTF(("%s: empty array for '%s'\n", __func__,
713 sme->sme_name));
714 goto out;
715 }
716
717 /*
718 * Add the dictionary for the global properties of this device.
719 */
720 dict2 = prop_dictionary_create();
721 if (!dict2) {
722 error = ENOMEM;
723 goto out;
724 }
725
726 error = sme_add_property_dictionary(sme, array, dict2);
727 if (error) {
728 prop_object_release(dict2);
729 goto out;
730 }
731
732 /*
733 * Add the array into the global dictionary for the driver.
734 *
735 * <dict>
736 * <key>foo0</key>
737 * <array>
738 * ...
739 */
740 mutex_enter(&sme_global_mtx);
741 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
742 error = EINVAL;
743 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
744 sme->sme_name));
745 goto out;
746 }
747
748 /*
749 * Add the device into the list.
750 */
751 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
752 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
753 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
754 mutex_exit(&sme_global_mtx);
755
756 out:
757 /*
758 * No errors? Make an initial data refresh if was requested,
759 * then register the events that were set in the driver. Do
760 * the refresh first in case it is needed to establish the
761 * limits or max_value needed by some events.
762 */
763 if (error == 0) {
764 nevent = 0;
765 sysmon_task_queue_init();
766
767 if (sme->sme_flags & SME_INIT_REFRESH) {
768 sysmon_task_queue_sched(0, sme_initial_refresh, sme);
769 DPRINTF(("%s: scheduled initial refresh for '%s'\n",
770 __func__, sme->sme_name));
771 }
772 SLIST_FOREACH(evdv, &sme_evdrv_list, evdrv_head) {
773 sysmon_task_queue_sched(0,
774 sme_event_drvadd, evdv->evdrv);
775 nevent++;
776 }
777 /*
778 * Hook the sensor into rnd(4) entropy pool if requested
779 */
780 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
781 if (edata->flags & ENVSYS_FHAS_ENTROPY) {
782 snprintf(rnd_name, sizeof(rnd_name), "%s-%s",
783 sme->sme_name, edata->desc);
784 rnd_attach_source(&edata->rnd_src, rnd_name,
785 RND_TYPE_ENV, 0);
786 }
787 }
788 DPRINTF(("%s: driver '%s' registered (nsens=%d nevent=%d)\n",
789 __func__, sme->sme_name, sme->sme_nsensors, nevent));
790 }
791
792 out2:
793 while (!SLIST_EMPTY(&sme_evdrv_list)) {
794 evdv = SLIST_FIRST(&sme_evdrv_list);
795 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
796 kmem_free(evdv, sizeof(*evdv));
797 }
798 if (!error)
799 return 0;
800
801 /*
802 * Ugh... something wasn't right; unregister all events and sensors
803 * previously assigned and destroy the array with all its objects.
804 */
805 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
806 sme->sme_name, error));
807
808 sme_event_unregister_all(sme);
809 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
810 edata = TAILQ_FIRST(&sme->sme_sensors_list);
811 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
812 }
813 sysmon_envsys_destroy_plist(array);
814 return error;
815 }
816
817 /*
818 * sysmon_envsys_destroy_plist:
819 *
820 * + Remove all objects from the array of dictionaries that is
821 * created in a sysmon envsys device.
822 */
823 static void
824 sysmon_envsys_destroy_plist(prop_array_t array)
825 {
826 prop_object_iterator_t iter, iter2;
827 prop_dictionary_t dict;
828 prop_object_t obj;
829
830 KASSERT(array != NULL);
831 KASSERT(prop_object_type(array) == PROP_TYPE_ARRAY);
832
833 DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
834 prop_array_count(array)));
835
836 iter = prop_array_iterator(array);
837 if (!iter)
838 return;
839
840 while ((dict = prop_object_iterator_next(iter))) {
841 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
842 iter2 = prop_dictionary_iterator(dict);
843 if (!iter2)
844 goto out;
845 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
846 while ((obj = prop_object_iterator_next(iter2)) != NULL) {
847 DPRINTFOBJ(("%s: obj=%s\n", __func__,
848 prop_dictionary_keysym_cstring_nocopy(obj)));
849 prop_dictionary_remove(dict,
850 prop_dictionary_keysym_cstring_nocopy(obj));
851 prop_object_iterator_reset(iter2);
852 }
853 prop_object_iterator_release(iter2);
854 DPRINTFOBJ(("%s: objects in dictionary:%d\n",
855 __func__, prop_dictionary_count(dict)));
856 prop_object_release(dict);
857 }
858
859 out:
860 prop_object_iterator_release(iter);
861 prop_object_release(array);
862 }
863
864 /*
865 * sysmon_envsys_unregister:
866 *
867 * + Unregister a sysmon envsys device.
868 */
869 void
870 sysmon_envsys_unregister(struct sysmon_envsys *sme)
871 {
872 prop_array_t array;
873 struct sysmon_envsys *osme;
874
875 KASSERT(sme != NULL);
876
877 /*
878 * Unregister all events associated with device.
879 */
880 sme_event_unregister_all(sme);
881 /*
882 * Decrement global sensors counter and the first_sensor index
883 * for remaining devices in the list (only used for compatibility
884 * with previous API), and remove the device from the list.
885 */
886 mutex_enter(&sme_global_mtx);
887 sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
888 LIST_FOREACH(osme, &sysmon_envsys_list, sme_list) {
889 if (osme->sme_fsensor >= sme->sme_fsensor)
890 osme->sme_fsensor -= sme->sme_nsensors;
891 }
892 LIST_REMOVE(sme, sme_list);
893 mutex_exit(&sme_global_mtx);
894
895 /*
896 * Remove the device (and all its objects) from the global dictionary.
897 */
898 array = prop_dictionary_get(sme_propd, sme->sme_name);
899 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
900 mutex_enter(&sme_global_mtx);
901 prop_dictionary_remove(sme_propd, sme->sme_name);
902 mutex_exit(&sme_global_mtx);
903 sysmon_envsys_destroy_plist(array);
904 }
905 /*
906 * And finally destroy the sysmon_envsys object.
907 */
908 sysmon_envsys_destroy(sme);
909 }
910
911 /*
912 * sysmon_envsys_find:
913 *
914 * + Find a sysmon envsys device and mark it as busy
915 * once it's available.
916 */
917 struct sysmon_envsys *
918 sysmon_envsys_find(const char *name)
919 {
920 struct sysmon_envsys *sme;
921
922 mutex_enter(&sme_global_mtx);
923 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
924 if (strcmp(sme->sme_name, name) == 0) {
925 sysmon_envsys_acquire(sme, false);
926 break;
927 }
928 }
929 mutex_exit(&sme_global_mtx);
930
931 return sme;
932 }
933
934 /*
935 * Compatibility function with the old API.
936 */
937 struct sysmon_envsys *
938 sysmon_envsys_find_40(u_int idx)
939 {
940 struct sysmon_envsys *sme;
941
942 mutex_enter(&sme_global_mtx);
943 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
944 if (idx >= sme->sme_fsensor &&
945 idx < (sme->sme_fsensor + sme->sme_nsensors)) {
946 sysmon_envsys_acquire(sme, false);
947 break;
948 }
949 }
950 mutex_exit(&sme_global_mtx);
951
952 return sme;
953 }
954
955 /*
956 * sysmon_envsys_acquire:
957 *
958 * + Wait until a sysmon envsys device is available and mark
959 * it as busy.
960 */
961 void
962 sysmon_envsys_acquire(struct sysmon_envsys *sme, bool locked)
963 {
964 KASSERT(sme != NULL);
965
966 if (locked) {
967 while (sme->sme_flags & SME_FLAG_BUSY)
968 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
969 sme->sme_flags |= SME_FLAG_BUSY;
970 } else {
971 mutex_enter(&sme->sme_mtx);
972 while (sme->sme_flags & SME_FLAG_BUSY)
973 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
974 sme->sme_flags |= SME_FLAG_BUSY;
975 mutex_exit(&sme->sme_mtx);
976 }
977 }
978
979 /*
980 * sysmon_envsys_release:
981 *
982 * + Unmark a sysmon envsys device as busy, and notify
983 * waiters.
984 */
985 void
986 sysmon_envsys_release(struct sysmon_envsys *sme, bool locked)
987 {
988 KASSERT(sme != NULL);
989
990 if (locked) {
991 sme->sme_flags &= ~SME_FLAG_BUSY;
992 cv_broadcast(&sme->sme_condvar);
993 } else {
994 mutex_enter(&sme->sme_mtx);
995 sme->sme_flags &= ~SME_FLAG_BUSY;
996 cv_broadcast(&sme->sme_condvar);
997 mutex_exit(&sme->sme_mtx);
998 }
999 }
1000
1001 /*
1002 * sme_initial_refresh:
1003 *
1004 * + Do an initial refresh of the sensors in a device just after
1005 * interrupts are enabled in the autoconf(9) process.
1006 *
1007 */
1008 static void
1009 sme_initial_refresh(void *arg)
1010 {
1011 struct sysmon_envsys *sme = arg;
1012 envsys_data_t *edata;
1013
1014 mutex_enter(&sme->sme_mtx);
1015 sysmon_envsys_acquire(sme, true);
1016 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head)
1017 sysmon_envsys_refresh_sensor(sme, edata);
1018 sysmon_envsys_release(sme, true);
1019 mutex_exit(&sme->sme_mtx);
1020 }
1021
1022 /*
1023 * sme_sensor_dictionary_get:
1024 *
1025 * + Returns a dictionary of a device specified by its index
1026 * position.
1027 */
1028 prop_dictionary_t
1029 sme_sensor_dictionary_get(prop_array_t array, const char *index)
1030 {
1031 prop_object_iterator_t iter;
1032 prop_dictionary_t dict;
1033 prop_object_t obj;
1034
1035 KASSERT(array != NULL || index != NULL);
1036
1037 iter = prop_array_iterator(array);
1038 if (!iter)
1039 return NULL;
1040
1041 while ((dict = prop_object_iterator_next(iter))) {
1042 obj = prop_dictionary_get(dict, "index");
1043 if (prop_string_equals_cstring(obj, index))
1044 break;
1045 }
1046
1047 prop_object_iterator_release(iter);
1048 return dict;
1049 }
1050
1051 /*
1052 * sme_remove_userprops:
1053 *
1054 * + Remove all properties from all devices that were set by
1055 * the ENVSYS_SETDICTIONARY ioctl.
1056 */
1057 static void
1058 sme_remove_userprops(void)
1059 {
1060 struct sysmon_envsys *sme;
1061 prop_array_t array;
1062 prop_dictionary_t sdict;
1063 envsys_data_t *edata = NULL;
1064 char tmp[ENVSYS_DESCLEN];
1065 char rnd_name[sizeof(edata->rnd_src.name)];
1066 sysmon_envsys_lim_t lims;
1067 const struct sme_descr_entry *sdt_units;
1068 uint32_t props;
1069 int ptype;
1070
1071 mutex_enter(&sme_global_mtx);
1072 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1073 sysmon_envsys_acquire(sme, false);
1074 array = prop_dictionary_get(sme_propd, sme->sme_name);
1075
1076 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1077 (void)snprintf(tmp, sizeof(tmp), "sensor%d",
1078 edata->sensor);
1079 sdict = sme_sensor_dictionary_get(array, tmp);
1080 KASSERT(sdict != NULL);
1081
1082 ptype = 0;
1083 if (edata->upropset & PROP_BATTCAP) {
1084 prop_dictionary_remove(sdict,
1085 "critical-capacity");
1086 ptype = PENVSYS_EVENT_CAPACITY;
1087 }
1088
1089 if (edata->upropset & PROP_BATTWARN) {
1090 prop_dictionary_remove(sdict,
1091 "warning-capacity");
1092 ptype = PENVSYS_EVENT_CAPACITY;
1093 }
1094
1095 if (edata->upropset & PROP_BATTHIGH) {
1096 prop_dictionary_remove(sdict,
1097 "high-capacity");
1098 ptype = PENVSYS_EVENT_CAPACITY;
1099 }
1100
1101 if (edata->upropset & PROP_BATTMAX) {
1102 prop_dictionary_remove(sdict,
1103 "maximum-capacity");
1104 ptype = PENVSYS_EVENT_CAPACITY;
1105 }
1106 if (edata->upropset & PROP_WARNMAX) {
1107 prop_dictionary_remove(sdict, "warning-max");
1108 ptype = PENVSYS_EVENT_LIMITS;
1109 }
1110
1111 if (edata->upropset & PROP_WARNMIN) {
1112 prop_dictionary_remove(sdict, "warning-min");
1113 ptype = PENVSYS_EVENT_LIMITS;
1114 }
1115
1116 if (edata->upropset & PROP_CRITMAX) {
1117 prop_dictionary_remove(sdict, "critical-max");
1118 ptype = PENVSYS_EVENT_LIMITS;
1119 }
1120
1121 if (edata->upropset & PROP_CRITMIN) {
1122 prop_dictionary_remove(sdict, "critical-min");
1123 ptype = PENVSYS_EVENT_LIMITS;
1124 }
1125 if (edata->upropset & PROP_RFACT) {
1126 (void)sme_sensor_upint32(sdict, "rfact", 0);
1127 edata->rfact = 0;
1128 }
1129
1130 if (edata->upropset & PROP_DESC)
1131 (void)sme_sensor_upstring(sdict,
1132 "description", edata->desc);
1133
1134 if (ptype == 0)
1135 continue;
1136
1137 /*
1138 * If there were any limit values removed, we
1139 * need to revert to initial limits.
1140 *
1141 * First, tell the driver that we need it to
1142 * restore any h/w limits which may have been
1143 * changed to stored, boot-time values.
1144 */
1145 if (sme->sme_set_limits) {
1146 DPRINTF(("%s: reset limits for %s %s\n",
1147 __func__, sme->sme_name, edata->desc));
1148 (*sme->sme_set_limits)(sme, edata, NULL, NULL);
1149 }
1150
1151 /*
1152 * Next, we need to retrieve those initial limits.
1153 */
1154 props = 0;
1155 edata->upropset &= ~PROP_LIMITS;
1156 if (sme->sme_get_limits) {
1157 DPRINTF(("%s: retrieve limits for %s %s\n",
1158 __func__, sme->sme_name, edata->desc));
1159 lims = edata->limits;
1160 (*sme->sme_get_limits)(sme, edata, &lims,
1161 &props);
1162 }
1163
1164 /*
1165 * Finally, remove any old limits event, then
1166 * install a new event (which will update the
1167 * dictionary)
1168 */
1169 sme_event_unregister(sme, edata->desc,
1170 PENVSYS_EVENT_LIMITS);
1171
1172 /*
1173 * Find the correct units for this sensor.
1174 */
1175 sdt_units = sme_find_table_entry(SME_DESC_UNITS,
1176 edata->units);
1177
1178 if (props & PROP_LIMITS) {
1179 DPRINTF(("%s: install limits for %s %s\n",
1180 __func__, sme->sme_name, edata->desc));
1181
1182 sme_event_register(sdict, edata, sme,
1183 &lims, props, PENVSYS_EVENT_LIMITS,
1184 sdt_units->crittype);
1185 }
1186 if (edata->flags & ENVSYS_FHAS_ENTROPY) {
1187 sme_event_register(sdict, edata, sme,
1188 &lims, props, PENVSYS_EVENT_NULL,
1189 sdt_units->crittype);
1190 snprintf(rnd_name, sizeof(rnd_name), "%s-%s",
1191 sme->sme_name, edata->desc);
1192 rnd_attach_source(&edata->rnd_src, rnd_name,
1193 RND_TYPE_ENV, 0);
1194 }
1195 }
1196
1197 /*
1198 * Restore default timeout value.
1199 */
1200 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1201 sme_schedule_callout(sme);
1202 sysmon_envsys_release(sme, false);
1203 }
1204 mutex_exit(&sme_global_mtx);
1205 }
1206
1207 /*
1208 * sme_add_property_dictionary:
1209 *
1210 * + Add global properties into a device.
1211 */
1212 static int
1213 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1214 prop_dictionary_t dict)
1215 {
1216 prop_dictionary_t pdict;
1217 const char *class;
1218 int error = 0;
1219
1220 pdict = prop_dictionary_create();
1221 if (!pdict)
1222 return EINVAL;
1223
1224 /*
1225 * Add the 'refresh-timeout' and 'dev-class' objects into the
1226 * 'device-properties' dictionary.
1227 *
1228 * ...
1229 * <dict>
1230 * <key>device-properties</key>
1231 * <dict>
1232 * <key>refresh-timeout</key>
1233 * <integer>120</integer<
1234 * <key>device-class</key>
1235 * <string>class_name</string>
1236 * </dict>
1237 * </dict>
1238 * ...
1239 *
1240 */
1241 if (sme->sme_events_timeout == 0) {
1242 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1243 sme_schedule_callout(sme);
1244 }
1245
1246 if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
1247 sme->sme_events_timeout)) {
1248 error = EINVAL;
1249 goto out;
1250 }
1251 if (sme->sme_class == SME_CLASS_BATTERY)
1252 class = "battery";
1253 else if (sme->sme_class == SME_CLASS_ACADAPTER)
1254 class = "ac-adapter";
1255 else
1256 class = "other";
1257 if (!prop_dictionary_set_cstring_nocopy(pdict, "device-class", class)) {
1258 error = EINVAL;
1259 goto out;
1260 }
1261
1262 if (!prop_dictionary_set(dict, "device-properties", pdict)) {
1263 error = EINVAL;
1264 goto out;
1265 }
1266
1267 /*
1268 * Add the device dictionary into the sysmon envsys array.
1269 */
1270 if (!prop_array_add(array, dict))
1271 error = EINVAL;
1272
1273 out:
1274 prop_object_release(pdict);
1275 return error;
1276 }
1277
1278 /*
1279 * sme_add_sensor_dictionary:
1280 *
1281 * + Adds the sensor objects into the dictionary and returns a pointer
1282 * to a sme_event_drv_t object if a monitoring flag was set
1283 * (or NULL otherwise).
1284 */
1285 static sme_event_drv_t *
1286 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1287 prop_dictionary_t dict, envsys_data_t *edata)
1288 {
1289 const struct sme_descr_entry *sdt;
1290 int error;
1291 sme_event_drv_t *sme_evdrv_t = NULL;
1292 char indexstr[ENVSYS_DESCLEN];
1293 bool mon_supported, allow_rfact;
1294
1295 /*
1296 * Add the index sensor string.
1297 *
1298 * ...
1299 * <key>index</eyr
1300 * <string>sensor0</string>
1301 * ...
1302 */
1303 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
1304 if (sme_sensor_upstring(dict, "index", indexstr))
1305 goto bad;
1306
1307 /*
1308 * ...
1309 * <key>description</key>
1310 * <string>blah blah</string>
1311 * ...
1312 */
1313 if (sme_sensor_upstring(dict, "description", edata->desc))
1314 goto bad;
1315
1316 /*
1317 * Add the monitoring boolean object:
1318 *
1319 * ...
1320 * <key>monitoring-supported</key>
1321 * <true/>
1322 * ...
1323 *
1324 * always false on Battery {capacity,charge}, Drive and Indicator types.
1325 * They cannot be monitored.
1326 *
1327 */
1328 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1329 (edata->units == ENVSYS_INDICATOR) ||
1330 (edata->units == ENVSYS_DRIVE) ||
1331 (edata->units == ENVSYS_BATTERY_CAPACITY) ||
1332 (edata->units == ENVSYS_BATTERY_CHARGE))
1333 mon_supported = false;
1334 else
1335 mon_supported = true;
1336 if (sme_sensor_upbool(dict, "monitoring-supported", mon_supported))
1337 goto out;
1338
1339 /*
1340 * Add the allow-rfact boolean object, true if
1341 * ENVSYS_FCHANGERFACT is set, false otherwise.
1342 *
1343 * ...
1344 * <key>allow-rfact</key>
1345 * <true/>
1346 * ...
1347 */
1348 if (edata->units == ENVSYS_SVOLTS_DC ||
1349 edata->units == ENVSYS_SVOLTS_AC) {
1350 if (edata->flags & ENVSYS_FCHANGERFACT)
1351 allow_rfact = true;
1352 else
1353 allow_rfact = false;
1354 if (sme_sensor_upbool(dict, "allow-rfact", allow_rfact))
1355 goto out;
1356 }
1357
1358 error = sme_update_sensor_dictionary(dict, edata,
1359 (edata->state == ENVSYS_SVALID));
1360 if (error < 0)
1361 goto bad;
1362 else if (error)
1363 goto out;
1364
1365 /*
1366 * ...
1367 * </dict>
1368 *
1369 * Add the dictionary into the array.
1370 *
1371 */
1372 if (!prop_array_add(array, dict)) {
1373 DPRINTF(("%s: prop_array_add\n", __func__));
1374 goto bad;
1375 }
1376
1377 /*
1378 * Register new event(s) if any monitoring flag was set or if
1379 * the sensor provides entropy for rnd(4).
1380 */
1381 if (edata->flags & (ENVSYS_FMONANY | ENVSYS_FHAS_ENTROPY)) {
1382 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
1383 sme_evdrv_t->sed_sdict = dict;
1384 sme_evdrv_t->sed_edata = edata;
1385 sme_evdrv_t->sed_sme = sme;
1386 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
1387 sme_evdrv_t->sed_powertype = sdt->crittype;
1388 }
1389
1390 out:
1391 return sme_evdrv_t;
1392
1393 bad:
1394 prop_object_release(dict);
1395 return NULL;
1396 }
1397
1398 /*
1399 * Find the maximum of all currently reported values.
1400 * The provided callback decides whether a sensor is part of the
1401 * maximum calculation (by returning true) or ignored (callback
1402 * returns false). Example usage: callback selects temperature
1403 * sensors in a given thermal zone, the function calculates the
1404 * maximum currently reported temperature in this zone.
1405 * If the parameter "refresh" is true, new values will be aquired
1406 * from the hardware, if not, the last reported value will be used.
1407 */
1408 uint32_t
1409 sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*),
1410 bool refresh)
1411 {
1412 struct sysmon_envsys *sme;
1413 uint32_t maxv, v;
1414
1415 maxv = 0;
1416 mutex_enter(&sme_global_mtx);
1417 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1418 sysmon_envsys_acquire(sme, false);
1419 v = sme_get_max_value(sme, predicate, refresh);
1420 sysmon_envsys_release(sme, false);
1421 if (v > maxv)
1422 maxv = v;
1423 }
1424 mutex_exit(&sme_global_mtx);
1425 return maxv;
1426 }
1427
1428 static uint32_t
1429 sme_get_max_value(struct sysmon_envsys *sme,
1430 bool (*predicate)(const envsys_data_t*),
1431 bool refresh)
1432 {
1433 envsys_data_t *edata;
1434 uint32_t maxv, v;
1435
1436 /*
1437 * Iterate over all sensors that match the predicate
1438 */
1439 maxv = 0;
1440 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1441 if (!(*predicate)(edata))
1442 continue;
1443
1444 /*
1445 * refresh sensor data
1446 */
1447 mutex_enter(&sme->sme_mtx);
1448 sysmon_envsys_refresh_sensor(sme, edata);
1449 mutex_exit(&sme->sme_mtx);
1450
1451 v = edata->value_cur;
1452 if (v > maxv)
1453 maxv = v;
1454
1455 }
1456
1457 return maxv;
1458 }
1459
1460 /*
1461 * sme_update_dictionary:
1462 *
1463 * + Update per-sensor dictionaries with new values if there were
1464 * changes, otherwise the object in dictionary is untouched.
1465 */
1466 int
1467 sme_update_dictionary(struct sysmon_envsys *sme)
1468 {
1469 envsys_data_t *edata;
1470 prop_object_t array, dict, obj, obj2;
1471 int error = 0;
1472
1473 /*
1474 * Retrieve the array of dictionaries in device.
1475 */
1476 array = prop_dictionary_get(sme_propd, sme->sme_name);
1477 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
1478 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
1479 return EINVAL;
1480 }
1481
1482 /*
1483 * Get the last dictionary on the array, this contains the
1484 * 'device-properties' sub-dictionary.
1485 */
1486 obj = prop_array_get(array, prop_array_count(array) - 1);
1487 if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
1488 DPRINTF(("%s: not a device-properties dictionary\n", __func__));
1489 return EINVAL;
1490 }
1491
1492 obj2 = prop_dictionary_get(obj, "device-properties");
1493 if (!obj2)
1494 return EINVAL;
1495
1496 /*
1497 * Update the 'refresh-timeout' property.
1498 */
1499 if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
1500 sme->sme_events_timeout))
1501 return EINVAL;
1502
1503 /*
1504 * - iterate over all sensors.
1505 * - fetch new data.
1506 * - check if data in dictionary is different than new data.
1507 * - update dictionary if there were changes.
1508 */
1509 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1510 sme->sme_name, sme->sme_nsensors));
1511
1512 /*
1513 * Don't bother with locking when traversing the queue,
1514 * the device is already marked as busy; if a sensor
1515 * is going to be removed or added it will have to wait.
1516 */
1517 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1518 /*
1519 * refresh sensor data via sme_envsys_refresh_sensor
1520 */
1521 mutex_enter(&sme->sme_mtx);
1522 sysmon_envsys_refresh_sensor(sme, edata);
1523 mutex_exit(&sme->sme_mtx);
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 && (edata->flags & ENVSYS_FMONNOTSUPP) != 0) {
1900 error = ENOTSUP;
1901 goto out;
1902 }
1903 if (props || (edata->flags & ENVSYS_FHAS_ENTROPY) != 0) {
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 mutex_enter(&sme->sme_mtx);
1953 sysmon_envsys_refresh_sensor(sme, sensor);
1954 mutex_exit(&sme->sme_mtx);
1955 }
1956 if (!(*func)(sme, sensor, arg))
1957 break;
1958 }
1959 sysmon_envsys_release(sme, false);
1960 }
1961 mutex_exit(&sme_global_mtx);
1962 }
1963
1964 /*
1965 * Call the sensor's refresh function, and collect/stir entropy
1966 */
1967 void
1968 sysmon_envsys_refresh_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
1969 {
1970
1971 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
1972 (*sme->sme_refresh)(sme, edata);
1973
1974 if (edata->flags & ENVSYS_FHAS_ENTROPY &&
1975 edata->state != ENVSYS_SINVALID &&
1976 edata->value_prev != edata->value_cur)
1977 rnd_add_uint32(&edata->rnd_src, edata->value_cur);
1978 edata->value_prev = edata->value_cur;
1979 }
1980