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