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