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