sysmon_envsys.c revision 1.121 1 /* $NetBSD: sysmon_envsys.c,v 1.121 2012/07/16 13:55:01 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.121 2012/07/16 13:55:01 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 * + Attachs 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 a 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 sysmon_envsys_lim_t lims;
1066 const struct sme_descr_entry *sdt_units;
1067 uint32_t props;
1068 int ptype;
1069
1070 mutex_enter(&sme_global_mtx);
1071 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1072 sysmon_envsys_acquire(sme, false);
1073 array = prop_dictionary_get(sme_propd, sme->sme_name);
1074
1075 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1076 (void)snprintf(tmp, sizeof(tmp), "sensor%d",
1077 edata->sensor);
1078 sdict = sme_sensor_dictionary_get(array, tmp);
1079 KASSERT(sdict != NULL);
1080
1081 ptype = 0;
1082 if (edata->upropset & PROP_BATTCAP) {
1083 prop_dictionary_remove(sdict,
1084 "critical-capacity");
1085 ptype = PENVSYS_EVENT_CAPACITY;
1086 }
1087
1088 if (edata->upropset & PROP_BATTWARN) {
1089 prop_dictionary_remove(sdict,
1090 "warning-capacity");
1091 ptype = PENVSYS_EVENT_CAPACITY;
1092 }
1093
1094 if (edata->upropset & PROP_BATTHIGH) {
1095 prop_dictionary_remove(sdict,
1096 "high-capacity");
1097 ptype = PENVSYS_EVENT_CAPACITY;
1098 }
1099
1100 if (edata->upropset & PROP_BATTMAX) {
1101 prop_dictionary_remove(sdict,
1102 "maximum-capacity");
1103 ptype = PENVSYS_EVENT_CAPACITY;
1104 }
1105 if (edata->upropset & PROP_WARNMAX) {
1106 prop_dictionary_remove(sdict, "warning-max");
1107 ptype = PENVSYS_EVENT_LIMITS;
1108 }
1109
1110 if (edata->upropset & PROP_WARNMIN) {
1111 prop_dictionary_remove(sdict, "warning-min");
1112 ptype = PENVSYS_EVENT_LIMITS;
1113 }
1114
1115 if (edata->upropset & PROP_CRITMAX) {
1116 prop_dictionary_remove(sdict, "critical-max");
1117 ptype = PENVSYS_EVENT_LIMITS;
1118 }
1119
1120 if (edata->upropset & PROP_CRITMIN) {
1121 prop_dictionary_remove(sdict, "critical-min");
1122 ptype = PENVSYS_EVENT_LIMITS;
1123 }
1124 if (edata->upropset & PROP_RFACT) {
1125 (void)sme_sensor_upint32(sdict, "rfact", 0);
1126 edata->rfact = 0;
1127 }
1128
1129 if (edata->upropset & PROP_DESC)
1130 (void)sme_sensor_upstring(sdict,
1131 "description", edata->desc);
1132
1133 if (ptype == 0)
1134 continue;
1135
1136 /*
1137 * If there were any limit values removed, we
1138 * need to revert to initial limits.
1139 *
1140 * First, tell the driver that we need it to
1141 * restore any h/w limits which may have been
1142 * changed to stored, boot-time values.
1143 */
1144 if (sme->sme_set_limits) {
1145 DPRINTF(("%s: reset limits for %s %s\n",
1146 __func__, sme->sme_name, edata->desc));
1147 (*sme->sme_set_limits)(sme, edata, NULL, NULL);
1148 }
1149
1150 /*
1151 * Next, we need to retrieve those initial limits.
1152 */
1153 props = 0;
1154 edata->upropset &= ~PROP_LIMITS;
1155 if (sme->sme_get_limits) {
1156 DPRINTF(("%s: retrieve limits for %s %s\n",
1157 __func__, sme->sme_name, edata->desc));
1158 lims = edata->limits;
1159 (*sme->sme_get_limits)(sme, edata, &lims,
1160 &props);
1161 }
1162
1163 /*
1164 * Finally, remove any old limits event, then
1165 * install a new event (which will update the
1166 * dictionary)
1167 */
1168 sme_event_unregister(sme, edata->desc,
1169 PENVSYS_EVENT_LIMITS);
1170
1171 if (props & PROP_LIMITS) {
1172 DPRINTF(("%s: install limits for %s %s\n",
1173 __func__, sme->sme_name, edata->desc));
1174
1175
1176 /*
1177 * Find the correct units for this sensor.
1178 */
1179 sdt_units = sme_find_table_entry(SME_DESC_UNITS,
1180 edata->units);
1181
1182 sme_event_register(sdict, edata, sme,
1183 &lims, props, PENVSYS_EVENT_LIMITS,
1184 sdt_units->crittype);
1185 }
1186 }
1187
1188 /*
1189 * Restore default timeout value.
1190 */
1191 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1192 sme_schedule_callout(sme);
1193 sysmon_envsys_release(sme, false);
1194 }
1195 mutex_exit(&sme_global_mtx);
1196 }
1197
1198 /*
1199 * sme_add_property_dictionary:
1200 *
1201 * + Add global properties into a device.
1202 */
1203 static int
1204 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1205 prop_dictionary_t dict)
1206 {
1207 prop_dictionary_t pdict;
1208 const char *class;
1209 int error = 0;
1210
1211 pdict = prop_dictionary_create();
1212 if (!pdict)
1213 return EINVAL;
1214
1215 /*
1216 * Add the 'refresh-timeout' and 'dev-class' objects into the
1217 * 'device-properties' dictionary.
1218 *
1219 * ...
1220 * <dict>
1221 * <key>device-properties</key>
1222 * <dict>
1223 * <key>refresh-timeout</key>
1224 * <integer>120</integer<
1225 * <key>device-class</key>
1226 * <string>class_name</string>
1227 * </dict>
1228 * </dict>
1229 * ...
1230 *
1231 */
1232 if (sme->sme_events_timeout == 0) {
1233 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
1234 sme_schedule_callout(sme);
1235 }
1236
1237 if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
1238 sme->sme_events_timeout)) {
1239 error = EINVAL;
1240 goto out;
1241 }
1242 if (sme->sme_class == SME_CLASS_BATTERY)
1243 class = "battery";
1244 else if (sme->sme_class == SME_CLASS_ACADAPTER)
1245 class = "ac-adapter";
1246 else
1247 class = "other";
1248 if (!prop_dictionary_set_cstring_nocopy(pdict, "device-class", class)) {
1249 error = EINVAL;
1250 goto out;
1251 }
1252
1253 if (!prop_dictionary_set(dict, "device-properties", pdict)) {
1254 error = EINVAL;
1255 goto out;
1256 }
1257
1258 /*
1259 * Add the device dictionary into the sysmon envsys array.
1260 */
1261 if (!prop_array_add(array, dict))
1262 error = EINVAL;
1263
1264 out:
1265 prop_object_release(pdict);
1266 return error;
1267 }
1268
1269 /*
1270 * sme_add_sensor_dictionary:
1271 *
1272 * + Adds the sensor objects into the dictionary and returns a pointer
1273 * to a sme_event_drv_t object if a monitoring flag was set
1274 * (or NULL otherwise).
1275 */
1276 static sme_event_drv_t *
1277 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
1278 prop_dictionary_t dict, envsys_data_t *edata)
1279 {
1280 const struct sme_descr_entry *sdt;
1281 int error;
1282 sme_event_drv_t *sme_evdrv_t = NULL;
1283 char indexstr[ENVSYS_DESCLEN];
1284
1285 /*
1286 * Add the index sensor string.
1287 *
1288 * ...
1289 * <key>index</eyr
1290 * <string>sensor0</string>
1291 * ...
1292 */
1293 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
1294 if (sme_sensor_upstring(dict, "index", indexstr))
1295 goto bad;
1296
1297 /*
1298 * ...
1299 * <key>description</key>
1300 * <string>blah blah</string>
1301 * ...
1302 */
1303 if (sme_sensor_upstring(dict, "description", edata->desc))
1304 goto bad;
1305
1306 /*
1307 * Add the monitoring boolean object:
1308 *
1309 * ...
1310 * <key>monitoring-supported</key>
1311 * <true/>
1312 * ...
1313 *
1314 * always false on Battery {capacity,charge}, Drive and Indicator types.
1315 * They cannot be monitored.
1316 *
1317 */
1318 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1319 (edata->units == ENVSYS_INDICATOR) ||
1320 (edata->units == ENVSYS_DRIVE) ||
1321 (edata->units == ENVSYS_BATTERY_CAPACITY) ||
1322 (edata->units == ENVSYS_BATTERY_CHARGE)) {
1323 if (sme_sensor_upbool(dict, "monitoring-supported", false))
1324 goto out;
1325 } else {
1326 if (sme_sensor_upbool(dict, "monitoring-supported", true))
1327 goto out;
1328 }
1329
1330 /*
1331 * Add the allow-rfact boolean object, true if
1332 * ENVSYS_FCHANGERFACT is set, false otherwise.
1333 *
1334 * ...
1335 * <key>allow-rfact</key>
1336 * <true/>
1337 * ...
1338 */
1339 if (edata->units == ENVSYS_SVOLTS_DC ||
1340 edata->units == ENVSYS_SVOLTS_AC) {
1341 if (edata->flags & ENVSYS_FCHANGERFACT) {
1342 if (sme_sensor_upbool(dict, "allow-rfact", true))
1343 goto out;
1344 } else {
1345 if (sme_sensor_upbool(dict, "allow-rfact", false))
1346 goto out;
1347 }
1348 }
1349
1350 error = sme_update_sensor_dictionary(dict, edata,
1351 (edata->state == ENVSYS_SVALID));
1352 if (error < 0)
1353 goto bad;
1354 else if (error)
1355 goto out;
1356
1357 /*
1358 * ...
1359 * </dict>
1360 *
1361 * Add the dictionary into the array.
1362 *
1363 */
1364 if (!prop_array_add(array, dict)) {
1365 DPRINTF(("%s: prop_array_add\n", __func__));
1366 goto bad;
1367 }
1368
1369 /*
1370 * Register new event(s) if any monitoring flag was set or if
1371 * the sensor provides entropy for rnd(4).
1372 */
1373 if (edata->flags & (ENVSYS_FMONANY | ENVSYS_FHAS_ENTROPY)) {
1374 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
1375 sme_evdrv_t->sed_sdict = dict;
1376 sme_evdrv_t->sed_edata = edata;
1377 sme_evdrv_t->sed_sme = sme;
1378 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
1379 sme_evdrv_t->sed_powertype = sdt->crittype;
1380 }
1381
1382 out:
1383 return sme_evdrv_t;
1384
1385 bad:
1386 prop_object_release(dict);
1387 return NULL;
1388 }
1389
1390 /*
1391 * Find the maximum of all currently reported values.
1392 * The provided callback decides whether a sensor is part of the
1393 * maximum calculation (by returning true) or ignored (callback
1394 * returns false). Example usage: callback selects temperature
1395 * sensors in a given thermal zone, the function calculates the
1396 * maximum currently reported temperature in this zone.
1397 * If the parameter "refresh" is true, new values will be aquired
1398 * from the hardware, if not, the last reported value will be used.
1399 */
1400 uint32_t
1401 sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*),
1402 bool refresh)
1403 {
1404 struct sysmon_envsys *sme;
1405 uint32_t maxv, v;
1406
1407 maxv = 0;
1408 mutex_enter(&sme_global_mtx);
1409 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1410 sysmon_envsys_acquire(sme, false);
1411 v = sme_get_max_value(sme, predicate, refresh);
1412 sysmon_envsys_release(sme, false);
1413 if (v > maxv)
1414 maxv = v;
1415 }
1416 mutex_exit(&sme_global_mtx);
1417 return maxv;
1418 }
1419
1420 static uint32_t
1421 sme_get_max_value(struct sysmon_envsys *sme,
1422 bool (*predicate)(const envsys_data_t*),
1423 bool refresh)
1424 {
1425 envsys_data_t *edata;
1426 uint32_t maxv, v;
1427
1428 /*
1429 * Iterate over all sensors that match the predicate
1430 */
1431 maxv = 0;
1432 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1433 if (!(*predicate)(edata))
1434 continue;
1435
1436 /*
1437 * refresh sensor data
1438 */
1439 mutex_enter(&sme->sme_mtx);
1440 sysmon_envsys_refresh_sensor(sme, edata);
1441 mutex_exit(&sme->sme_mtx);
1442
1443 v = edata->value_cur;
1444 if (v > maxv)
1445 maxv = v;
1446
1447 }
1448
1449 return maxv;
1450 }
1451
1452 /*
1453 * sme_update_dictionary:
1454 *
1455 * + Update per-sensor dictionaries with new values if there were
1456 * changes, otherwise the object in dictionary is untouched.
1457 */
1458 int
1459 sme_update_dictionary(struct sysmon_envsys *sme)
1460 {
1461 envsys_data_t *edata;
1462 prop_object_t array, dict, obj, obj2;
1463 int error = 0;
1464
1465 /*
1466 * Retrieve the array of dictionaries in device.
1467 */
1468 array = prop_dictionary_get(sme_propd, sme->sme_name);
1469 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
1470 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
1471 return EINVAL;
1472 }
1473
1474 /*
1475 * Get the last dictionary on the array, this contains the
1476 * 'device-properties' sub-dictionary.
1477 */
1478 obj = prop_array_get(array, prop_array_count(array) - 1);
1479 if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
1480 DPRINTF(("%s: not a device-properties dictionary\n", __func__));
1481 return EINVAL;
1482 }
1483
1484 obj2 = prop_dictionary_get(obj, "device-properties");
1485 if (!obj2)
1486 return EINVAL;
1487
1488 /*
1489 * Update the 'refresh-timeout' property.
1490 */
1491 if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
1492 sme->sme_events_timeout))
1493 return EINVAL;
1494
1495 /*
1496 * - iterate over all sensors.
1497 * - fetch new data.
1498 * - check if data in dictionary is different than new data.
1499 * - update dictionary if there were changes.
1500 */
1501 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1502 sme->sme_name, sme->sme_nsensors));
1503
1504 /*
1505 * Don't bother with locking when traversing the queue,
1506 * the device is already marked as busy; if a sensor
1507 * is going to be removed or added it will have to wait.
1508 */
1509 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1510 /*
1511 * refresh sensor data via sme_envsys_refresh_sensor
1512 */
1513 mutex_enter(&sme->sme_mtx);
1514 sysmon_envsys_refresh_sensor(sme, edata);
1515 mutex_exit(&sme->sme_mtx);
1516
1517 /*
1518 * retrieve sensor's dictionary.
1519 */
1520 dict = prop_array_get(array, edata->sensor);
1521 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1522 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1523 __func__, edata->sensor, sme->sme_name));
1524 return EINVAL;
1525 }
1526
1527 /*
1528 * update sensor's state.
1529 */
1530 error = sme_update_sensor_dictionary(dict, edata, true);
1531
1532 if (error)
1533 break;
1534 }
1535
1536 return error;
1537 }
1538
1539 int
1540 sme_update_sensor_dictionary(prop_object_t dict, envsys_data_t *edata,
1541 bool value_update)
1542 {
1543 const struct sme_descr_entry *sdt;
1544 int error = 0;
1545
1546 sdt = sme_find_table_entry(SME_DESC_STATES, edata->state);
1547 if (sdt == NULL) {
1548 printf("sme_update_sensor_dictionary: can not update sensor "
1549 "state %d unknown\n", edata->state);
1550 return EINVAL;
1551 }
1552
1553 DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", __func__,
1554 edata->sensor, sdt->type, sdt->desc, edata->flags));
1555
1556 error = sme_sensor_upstring(dict, "state", sdt->desc);
1557 if (error)
1558 return (-error);
1559
1560 /*
1561 * update sensor's type.
1562 */
1563 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
1564
1565 DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", __func__, edata->sensor,
1566 sdt->type, sdt->desc));
1567
1568 error = sme_sensor_upstring(dict, "type", sdt->desc);
1569 if (error)
1570 return (-error);
1571
1572 if (value_update) {
1573 /*
1574 * update sensor's current value.
1575 */
1576 error = sme_sensor_upint32(dict, "cur-value", edata->value_cur);
1577 if (error)
1578 return error;
1579 }
1580
1581 /*
1582 * Battery charge and Indicator types do not
1583 * need the remaining objects, so skip them.
1584 */
1585 if (edata->units == ENVSYS_INDICATOR ||
1586 edata->units == ENVSYS_BATTERY_CHARGE)
1587 return error;
1588
1589 /*
1590 * update sensor flags.
1591 */
1592 if (edata->flags & ENVSYS_FPERCENT) {
1593 error = sme_sensor_upbool(dict, "want-percentage", true);
1594 if (error)
1595 return error;
1596 }
1597
1598 if (value_update) {
1599 /*
1600 * update sensor's {max,min}-value.
1601 */
1602 if (edata->flags & ENVSYS_FVALID_MAX) {
1603 error = sme_sensor_upint32(dict, "max-value",
1604 edata->value_max);
1605 if (error)
1606 return error;
1607 }
1608
1609 if (edata->flags & ENVSYS_FVALID_MIN) {
1610 error = sme_sensor_upint32(dict, "min-value",
1611 edata->value_min);
1612 if (error)
1613 return error;
1614 }
1615
1616 /*
1617 * update 'rpms' only for ENVSYS_SFANRPM sensors.
1618 */
1619 if (edata->units == ENVSYS_SFANRPM) {
1620 error = sme_sensor_upuint32(dict, "rpms", edata->rpms);
1621 if (error)
1622 return error;
1623 }
1624
1625 /*
1626 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
1627 */
1628 if (edata->units == ENVSYS_SVOLTS_AC ||
1629 edata->units == ENVSYS_SVOLTS_DC) {
1630 error = sme_sensor_upint32(dict, "rfact", edata->rfact);
1631 if (error)
1632 return error;
1633 }
1634 }
1635
1636 /*
1637 * update 'drive-state' only for ENVSYS_DRIVE sensors.
1638 */
1639 if (edata->units == ENVSYS_DRIVE) {
1640 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
1641 edata->value_cur);
1642 error = sme_sensor_upstring(dict, "drive-state", sdt->desc);
1643 if (error)
1644 return error;
1645 }
1646
1647 /*
1648 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
1649 * sensors.
1650 */
1651 if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1652 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
1653 edata->value_cur);
1654 error = sme_sensor_upstring(dict, "battery-capacity",
1655 sdt->desc);
1656 if (error)
1657 return error;
1658 }
1659
1660 return error;
1661 }
1662
1663 /*
1664 * sme_userset_dictionary:
1665 *
1666 * + Parse the userland dictionary and run the appropiate tasks
1667 * that were specified.
1668 */
1669 int
1670 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1671 prop_array_t array)
1672 {
1673 const struct sme_descr_entry *sdt;
1674 envsys_data_t *edata;
1675 prop_dictionary_t dict, tdict = NULL;
1676 prop_object_t obj, obj1, obj2, tobj = NULL;
1677 uint32_t props;
1678 uint64_t refresh_timo = 0;
1679 sysmon_envsys_lim_t lims;
1680 int i, error = 0;
1681 const char *blah;
1682 bool targetfound = false;
1683
1684 /*
1685 * The user wanted to change the refresh timeout value for this
1686 * device.
1687 *
1688 * Get the 'device-properties' object from the userland dictionary.
1689 */
1690 obj = prop_dictionary_get(udict, "device-properties");
1691 if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
1692 /*
1693 * Get the 'refresh-timeout' property for this device.
1694 */
1695 obj1 = prop_dictionary_get(obj, "refresh-timeout");
1696 if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
1697 targetfound = true;
1698 refresh_timo =
1699 prop_number_unsigned_integer_value(obj1);
1700 if (refresh_timo < 1)
1701 error = EINVAL;
1702 else {
1703 mutex_enter(&sme->sme_mtx);
1704 if (sme->sme_events_timeout != refresh_timo) {
1705 sme->sme_events_timeout = refresh_timo;
1706 sme_schedule_callout(sme);
1707 }
1708 mutex_exit(&sme->sme_mtx);
1709 }
1710 }
1711 return error;
1712
1713 } else if (!obj) {
1714 /*
1715 * Get sensor's index from userland dictionary.
1716 */
1717 obj = prop_dictionary_get(udict, "index");
1718 if (!obj)
1719 return EINVAL;
1720 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1721 DPRINTF(("%s: 'index' not a string\n", __func__));
1722 return EINVAL;
1723 }
1724 } else
1725 return EINVAL;
1726
1727 /*
1728 * Don't bother with locking when traversing the queue,
1729 * the device is already marked as busy; if a sensor
1730 * is going to be removed or added it will have to wait.
1731 */
1732 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1733 /*
1734 * Get a dictionary and check if it's our sensor by checking
1735 * at its index position.
1736 */
1737 dict = prop_array_get(array, edata->sensor);
1738 obj1 = prop_dictionary_get(dict, "index");
1739
1740 /*
1741 * is it our sensor?
1742 */
1743 if (!prop_string_equals(obj1, obj))
1744 continue;
1745
1746 props = 0;
1747
1748 /*
1749 * Check if a new description operation was
1750 * requested by the user and set new description.
1751 */
1752 obj2 = prop_dictionary_get(udict, "description");
1753 if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
1754 targetfound = true;
1755 blah = prop_string_cstring_nocopy(obj2);
1756
1757 /*
1758 * Check for duplicate description.
1759 */
1760 for (i = 0; i < sme->sme_nsensors; i++) {
1761 if (i == edata->sensor)
1762 continue;
1763 tdict = prop_array_get(array, i);
1764 tobj =
1765 prop_dictionary_get(tdict, "description");
1766 if (prop_string_equals(obj2, tobj)) {
1767 error = EEXIST;
1768 goto out;
1769 }
1770 }
1771
1772 /*
1773 * Update the object in dictionary.
1774 */
1775 mutex_enter(&sme->sme_mtx);
1776 error = sme_sensor_upstring(dict,
1777 "description",
1778 blah);
1779 if (error) {
1780 mutex_exit(&sme->sme_mtx);
1781 goto out;
1782 }
1783
1784 DPRINTF(("%s: sensor%d changed desc to: %s\n",
1785 __func__, edata->sensor, blah));
1786 edata->upropset |= PROP_DESC;
1787 mutex_exit(&sme->sme_mtx);
1788 }
1789
1790 /*
1791 * did the user want to change the rfact?
1792 */
1793 obj2 = prop_dictionary_get(udict, "rfact");
1794 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1795 targetfound = true;
1796 if (edata->flags & ENVSYS_FCHANGERFACT) {
1797 mutex_enter(&sme->sme_mtx);
1798 edata->rfact = prop_number_integer_value(obj2);
1799 edata->upropset |= PROP_RFACT;
1800 mutex_exit(&sme->sme_mtx);
1801 DPRINTF(("%s: sensor%d changed rfact to %d\n",
1802 __func__, edata->sensor, edata->rfact));
1803 } else {
1804 error = ENOTSUP;
1805 goto out;
1806 }
1807 }
1808
1809 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
1810
1811 /*
1812 * did the user want to set a critical capacity event?
1813 */
1814 obj2 = prop_dictionary_get(udict, "critical-capacity");
1815 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1816 targetfound = true;
1817 lims.sel_critmin = prop_number_integer_value(obj2);
1818 props |= PROP_BATTCAP;
1819 }
1820
1821 /*
1822 * did the user want to set a warning capacity event?
1823 */
1824 obj2 = prop_dictionary_get(udict, "warning-capacity");
1825 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1826 targetfound = true;
1827 lims.sel_warnmin = prop_number_integer_value(obj2);
1828 props |= PROP_BATTWARN;
1829 }
1830
1831 /*
1832 * did the user want to set a high capacity event?
1833 */
1834 obj2 = prop_dictionary_get(udict, "high-capacity");
1835 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1836 targetfound = true;
1837 lims.sel_warnmin = prop_number_integer_value(obj2);
1838 props |= PROP_BATTHIGH;
1839 }
1840
1841 /*
1842 * did the user want to set a maximum capacity event?
1843 */
1844 obj2 = prop_dictionary_get(udict, "maximum-capacity");
1845 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1846 targetfound = true;
1847 lims.sel_warnmin = prop_number_integer_value(obj2);
1848 props |= PROP_BATTMAX;
1849 }
1850
1851 /*
1852 * did the user want to set a critical max event?
1853 */
1854 obj2 = prop_dictionary_get(udict, "critical-max");
1855 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1856 targetfound = true;
1857 lims.sel_critmax = prop_number_integer_value(obj2);
1858 props |= PROP_CRITMAX;
1859 }
1860
1861 /*
1862 * did the user want to set a warning max event?
1863 */
1864 obj2 = prop_dictionary_get(udict, "warning-max");
1865 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1866 targetfound = true;
1867 lims.sel_warnmax = prop_number_integer_value(obj2);
1868 props |= PROP_WARNMAX;
1869 }
1870
1871 /*
1872 * did the user want to set a critical min event?
1873 */
1874 obj2 = prop_dictionary_get(udict, "critical-min");
1875 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1876 targetfound = true;
1877 lims.sel_critmin = prop_number_integer_value(obj2);
1878 props |= PROP_CRITMIN;
1879 }
1880
1881 /*
1882 * did the user want to set a warning min event?
1883 */
1884 obj2 = prop_dictionary_get(udict, "warning-min");
1885 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
1886 targetfound = true;
1887 lims.sel_warnmin = prop_number_integer_value(obj2);
1888 props |= PROP_WARNMIN;
1889 }
1890
1891 if (props && (edata->flags & ENVSYS_FMONNOTSUPP) != 0) {
1892 error = ENOTSUP;
1893 goto out;
1894 }
1895 if (props || (edata->flags & ENVSYS_FHAS_ENTROPY) != 0) {
1896 error = sme_event_register(dict, edata, sme, &lims,
1897 props,
1898 (edata->flags & ENVSYS_FPERCENT)?
1899 PENVSYS_EVENT_CAPACITY:
1900 PENVSYS_EVENT_LIMITS,
1901 sdt->crittype);
1902 if (error == EEXIST)
1903 error = 0;
1904 if (error)
1905 goto out;
1906 }
1907
1908 /*
1909 * All objects in dictionary were processed.
1910 */
1911 break;
1912 }
1913
1914 out:
1915 /*
1916 * invalid target? return the error.
1917 */
1918 if (!targetfound)
1919 error = EINVAL;
1920
1921 return error;
1922 }
1923
1924 /*
1925 * + sysmon_envsys_foreach_sensor
1926 *
1927 * Walk through the devices' sensor lists and execute the callback.
1928 * If the callback returns false, the remainder of the current
1929 * device's sensors are skipped.
1930 */
1931 void
1932 sysmon_envsys_foreach_sensor(sysmon_envsys_callback_t func, void *arg,
1933 bool refresh)
1934 {
1935 struct sysmon_envsys *sme;
1936 envsys_data_t *sensor;
1937
1938 mutex_enter(&sme_global_mtx);
1939 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1940
1941 sysmon_envsys_acquire(sme, false);
1942 TAILQ_FOREACH(sensor, &sme->sme_sensors_list, sensors_head) {
1943 if (refresh) {
1944 mutex_enter(&sme->sme_mtx);
1945 sysmon_envsys_refresh_sensor(sme, sensor);
1946 mutex_exit(&sme->sme_mtx);
1947 }
1948 if (!(*func)(sme, sensor, arg))
1949 break;
1950 }
1951 sysmon_envsys_release(sme, false);
1952 }
1953 mutex_exit(&sme_global_mtx);
1954 }
1955
1956 /*
1957 * Call the sensor's refresh function, and collect/stir entropy
1958 */
1959 void
1960 sysmon_envsys_refresh_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
1961 {
1962
1963 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
1964 (*sme->sme_refresh)(sme, edata);
1965
1966 if (edata->flags & ENVSYS_FHAS_ENTROPY &&
1967 edata->state != ENVSYS_SINVALID &&
1968 edata->value_prev != edata->value_cur)
1969 rnd_add_uint32(&edata->rnd_src, edata->value_cur);
1970 edata->value_prev = edata->value_cur;
1971 }
1972