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