sysmon_envsys.c revision 1.24 1 /* $NetBSD: sysmon_envsys.c,v 1.24 2007/07/17 17:56:04 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Juan Romero Pardines
21 * for the NetBSD Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 2000 Zembu Labs, Inc.
41 * All rights reserved.
42 *
43 * Author: Jason R. Thorpe <thorpej (at) zembu.com>
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Zembu Labs, Inc.
56 * 4. Neither the name of Zembu Labs nor the names of its employees may
57 * be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
61 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
62 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
63 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 /*
73 * Environmental sensor framework for sysmon, exported to userland
74 * with proplib(3).
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.24 2007/07/17 17:56:04 xtraeme Exp $");
79
80 #include <sys/param.h>
81 #include <sys/types.h>
82 #include <sys/conf.h>
83 #include <sys/errno.h>
84 #include <sys/fcntl.h>
85 #include <sys/kernel.h>
86 #include <sys/systm.h>
87 #include <sys/proc.h>
88 #include <sys/mutex.h>
89 #include <sys/kmem.h>
90
91 #include <dev/sysmon/sysmonvar.h>
92 #include <dev/sysmon/sysmon_envsysvar.h>
93 #include <dev/sysmon/sysmon_taskq.h>
94
95 #include "opt_compat_netbsd.h"
96
97 struct sme_sensor_type {
98 int type;
99 int crittype;
100 const char *desc;
101 };
102
103 static const struct sme_sensor_type sme_sensor_type[] = {
104 { ENVSYS_STEMP, PENVSYS_TYPE_TEMP, "Temperature" },
105 { ENVSYS_SFANRPM, PENVSYS_TYPE_FAN, "Fan" },
106 { ENVSYS_SVOLTS_AC, PENVSYS_TYPE_VOLTAGE, "Voltage AC" },
107 { ENVSYS_SVOLTS_DC, PENVSYS_TYPE_VOLTAGE, "Voltage DC" },
108 { ENVSYS_SOHMS, PENVSYS_TYPE_RESISTANCE,"Ohms" },
109 { ENVSYS_SWATTS, PENVSYS_TYPE_POWER, "Watts" },
110 { ENVSYS_SAMPS, PENVSYS_TYPE_POWER, "Ampere" },
111 { ENVSYS_SWATTHOUR, PENVSYS_TYPE_BATTERY, "Watt hour" },
112 { ENVSYS_SAMPHOUR, PENVSYS_TYPE_BATTERY, "Ampere hour" },
113 { ENVSYS_INDICATOR, PENVSYS_TYPE_INDICATOR, "Indicator" },
114 { ENVSYS_INTEGER, PENVSYS_TYPE_INDICATOR, "Integer" },
115 { ENVSYS_DRIVE, PENVSYS_TYPE_DRIVE, "Drive" },
116 { -1, -1, NULL }
117 };
118
119 struct sme_sensor_state {
120 int type;
121 const char *desc;
122 };
123
124 static const struct sme_sensor_state sme_sensor_state[] = {
125 { ENVSYS_SVALID, "valid" },
126 { ENVSYS_SINVALID, "invalid" },
127 { ENVSYS_SCRITICAL, "critical" },
128 { ENVSYS_SCRITUNDER, "critical-under" },
129 { ENVSYS_SCRITOVER, "critical-over" },
130 { ENVSYS_SWARNUNDER, "warning-under" },
131 { ENVSYS_SWARNOVER, "warning-over" },
132 { -1, "unknown" }
133 };
134
135 static const struct sme_sensor_state sme_sensor_drive_state[] = {
136 { ENVSYS_DRIVE_EMPTY, "drive state is unknown" },
137 { ENVSYS_DRIVE_READY, "drive is ready" },
138 { ENVSYS_DRIVE_POWERUP, "drive is powering up" },
139 { ENVSYS_DRIVE_ONLINE, "drive is online" },
140 { ENVSYS_DRIVE_IDLE, "drive is idle" },
141 { ENVSYS_DRIVE_ACTIVE, "drive is active" },
142 { ENVSYS_DRIVE_REBUILD, "drive is rebuilding" },
143 { ENVSYS_DRIVE_POWERDOWN, "drive is powering down" },
144 { ENVSYS_DRIVE_FAIL, "drive failed" },
145 { ENVSYS_DRIVE_PFAIL, "drive degraded" },
146 { -1, "unknown" }
147 };
148
149 static prop_dictionary_t sme_propd;
150 static kmutex_t sme_list_mtx;
151 static kcondvar_t sme_list_cv;
152
153 #ifdef COMPAT_40
154 static u_int sysmon_envsys_next_sensor_index = 0;
155 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
156 #endif
157
158 static void sysmon_envsys_release(struct sysmon_envsys *);
159
160 kmutex_t sme_mtx, sme_event_mtx;
161
162 /*
163 * sysmon_envsys_init:
164 *
165 * + Initialize global mutexes, dictionary and the linked lists.
166 */
167 void
168 sysmon_envsys_init(void)
169 {
170 LIST_INIT(&sysmon_envsys_list);
171 LIST_INIT(&sme_events_list);
172 mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
173 mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
174 mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
175 mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
176 cv_init(&sme_list_cv, "smefind");
177 sme_propd = prop_dictionary_create();
178 }
179
180 /*
181 * sysmonopen_envsys:
182 *
183 * + Open the system monitor device.
184 */
185 int
186 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
187 {
188 return 0;
189 }
190
191 /*
192 * sysmonclose_envsys:
193 *
194 * + Close the system monitor device.
195 */
196 int
197 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
198 {
199 /* Nothing to do */
200 return 0;
201 }
202
203 /*
204 * sysmonioctl_envsys:
205 *
206 * + Perform an envsys control request.
207 */
208 int
209 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
210 {
211 struct sysmon_envsys *sme = NULL;
212 int error = 0;
213 #ifdef COMPAT_40
214 u_int oidx;
215 #endif
216
217 switch (cmd) {
218 case ENVSYS_GETDICTIONARY:
219 {
220 struct plistref *plist = (struct plistref *)data;
221
222 /*
223 * Update all sysmon envsys devices dictionaries with
224 * new data if it's different than we have currently
225 * in the dictionary.
226 */
227 mutex_enter(&sme_mtx);
228 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
229 if (sme == NULL)
230 continue;
231
232 error = sme_update_dictionary(sme);
233 if (error) {
234 DPRINTF(("%s: sme_update_dictionary, "
235 "error=%d\n", __func__, error));
236 mutex_exit(&sme_mtx);
237 return error;
238 }
239 }
240 mutex_exit(&sme_mtx);
241 /*
242 * Copy global dictionary to userland.
243 */
244 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
245 break;
246 }
247 case ENVSYS_SETDICTIONARY:
248 {
249 const struct plistref *plist = (const struct plistref *)data;
250 prop_dictionary_t udict;
251 prop_object_t obj;
252 const char *devname = NULL;
253
254 if ((flag & FWRITE) == 0)
255 return EPERM;
256
257 /*
258 * Get dictionary from userland.
259 */
260 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
261 DPRINTF(("%s: copyin_ioctl error=%d\n", __func__, error));
262 if (error)
263 break;
264
265 /*
266 * Parse "driver-name" key to obtain the driver we
267 * are searching for.
268 */
269 obj = prop_dictionary_get(udict, "driver-name");
270 if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
271 DPRINTF(("%s: driver-name failed\n", __func__));
272 prop_object_release(udict);
273 error = EINVAL;
274 break;
275 }
276
277 /* driver name */
278 devname = prop_string_cstring_nocopy(obj);
279
280 /* find the correct sme device */
281 sme = sysmon_envsys_find(devname);
282 if (sme == NULL) {
283 DPRINTF(("%s: NULL sme\n", __func__));
284 prop_object_release(udict);
285 error = EINVAL;
286 break;
287 }
288
289 /*
290 * Find the correct array object with the string supplied
291 * by the userland dictionary.
292 */
293 obj = prop_dictionary_get(sme_propd, devname);
294 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
295 DPRINTF(("%s: array device failed\n", __func__));
296 prop_object_release(udict);
297 error = EINVAL;
298 break;
299 }
300
301 /* do the real work now */
302 error = sme_userset_dictionary(sme, udict, obj);
303 sysmon_envsys_release(sme);
304 break;
305 }
306 /*
307 * Compatibility functions with the old interface, only
308 * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
309 * to make old applications work.
310 */
311 #ifdef COMPAT_40
312 case ENVSYS_GTREDATA:
313 {
314 struct envsys_tre_data *tred = (void *)data;
315 envsys_data_t *edata = NULL;
316
317 tred->validflags = 0;
318
319 sme = sysmon_envsys_find_40(tred->sensor);
320 if (sme == NULL)
321 break;
322
323 mutex_enter(&sme_mtx);
324 oidx = tred->sensor;
325 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
326
327 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
328 __func__, tred->sensor, oidx, sme->sme_name,
329 sme->sme_nsensors));
330
331 edata = &sme->sme_sensor_data[tred->sensor];
332
333 if (tred->sensor < sme->sme_nsensors) {
334 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
335 error = (*sme->sme_gtredata)(sme, edata);
336 if (error) {
337 DPRINTF(("%s: sme_gtredata failed\n",
338 __func__));
339 mutex_exit(&sme_mtx);
340 return error;
341 }
342 }
343
344 /* copy required values to the old interface */
345 tred->sensor = edata->sensor;
346 tred->cur.data_us = edata->value_cur;
347 tred->cur.data_s = edata->value_cur;
348 tred->max.data_us = edata->value_max;
349 tred->max.data_s = edata->value_max;
350 tred->min.data_us = edata->value_min;
351 tred->min.data_s = edata->value_min;
352 tred->avg.data_us = edata->value_avg;
353 tred->avg.data_s = edata->value_avg;
354 tred->units = edata->units;
355
356 tred->validflags |= ENVSYS_FVALID;
357 tred->validflags |= ENVSYS_FCURVALID;
358
359 if (edata->flags & ENVSYS_FPERCENT) {
360 tred->validflags |= ENVSYS_FMAXVALID;
361 tred->validflags |= ENVSYS_FFRACVALID;
362 }
363
364 if (edata->state == ENVSYS_SINVALID) {
365 tred->validflags &= ~ENVSYS_FCURVALID;
366 tred->cur.data_us = tred->cur.data_s = 0;
367 }
368
369 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
370 __func__, edata->desc, tred->cur.data_s));
371 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
372 " tred->sensor=%d\n", __func__, tred->validflags,
373 tred->units, tred->sensor));
374 }
375 tred->sensor = oidx;
376 mutex_exit(&sme_mtx);
377
378 break;
379 }
380 case ENVSYS_GTREINFO:
381 {
382 struct envsys_basic_info *binfo = (void *)data;
383 envsys_data_t *edata = NULL;
384
385 binfo->validflags = 0;
386
387 sme = sysmon_envsys_find_40(binfo->sensor);
388 if (sme == NULL)
389 break;
390
391 mutex_enter(&sme_mtx);
392 oidx = binfo->sensor;
393 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
394
395 edata = &sme->sme_sensor_data[binfo->sensor];
396
397 binfo->validflags |= ENVSYS_FVALID;
398
399 if (binfo->sensor < sme->sme_nsensors) {
400 binfo->units = edata->units;
401 (void)strlcpy(binfo->desc, edata->desc,
402 sizeof(binfo->desc));
403 }
404
405 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
406 __func__, binfo->units, binfo->validflags));
407 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
408 __func__, binfo->desc, binfo->sensor));
409
410 binfo->sensor = oidx;
411 mutex_exit(&sme_mtx);
412
413 break;
414 }
415 #endif /* COMPAT_40 */
416 default:
417 error = ENOTTY;
418 break;
419 }
420
421 return error;
422 }
423
424 /*
425 * sysmon_envsys_register:
426 *
427 * + Register an envsys device.
428 * + Create device dictionary.
429 */
430 int
431 sysmon_envsys_register(struct sysmon_envsys *sme)
432 {
433 struct sysmon_envsys *lsme;
434 int error = 0;
435
436 /*
437 * sanity check 1, make sure the driver has initialized
438 * the sensors count or the value is not too high.
439 */
440 if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
441 return EINVAL;
442
443 /*
444 * sanity check 2, make sure that sme->sme_name and
445 * sme->sme_data are not NULL.
446 */
447 if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
448 return EINVAL;
449
450 /*
451 * sanity check 3, if SME_DISABLE_GTREDATA is not set
452 * the sme_gtredata function callback must be non NULL.
453 */
454 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
455 if (sme->sme_gtredata == NULL)
456 return EINVAL;
457 }
458
459 /*
460 * - check if requested sysmon_envsys device is valid
461 * and does not exist already in the list.
462 * - add device into the list.
463 * - create the plist structure.
464 */
465 mutex_enter(&sme_list_mtx);
466 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
467 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
468 mutex_exit(&sme_list_mtx);
469 error = EEXIST;
470 goto out;
471 }
472 }
473 #ifdef COMPAT_40
474 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
475 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
476 #endif
477 mutex_exit(&sme_list_mtx);
478 error = sysmon_envsys_createplist(sme);
479 if (!error) {
480 mutex_enter(&sme_list_mtx);
481 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
482 mutex_exit(&sme_list_mtx);
483 }
484
485 out:
486 return error;
487 }
488
489 /*
490 * sysmon_envsys_unregister:
491 *
492 * + Unregister an envsys device.
493 * + Unregister all events associated with this device.
494 * + Remove device dictionary.
495 */
496 void
497 sysmon_envsys_unregister(struct sysmon_envsys *sme)
498 {
499 sme_event_t *see;
500
501 mutex_enter(&sme_list_mtx);
502 while (sme->sme_flags & SME_FLAG_BUSY) {
503 sme->sme_flags |= SME_FLAG_WANTED;
504 cv_wait(&sme_list_cv, &sme_list_mtx);
505 }
506 prop_dictionary_remove(sme_propd, sme->sme_name);
507 LIST_FOREACH(see, &sme_events_list, see_list) {
508 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
509 sme_event_unregister(see->pes.pes_sensname,
510 see->type);
511 }
512 LIST_REMOVE(sme, sme_list);
513 mutex_exit(&sme_list_mtx);
514 }
515
516 /*
517 * sysmon_envsys_find:
518 *
519 * + Find an envsys device.
520 */
521 struct sysmon_envsys *
522 sysmon_envsys_find(const char *name)
523 {
524 struct sysmon_envsys *sme;
525
526 mutex_enter(&sme_list_mtx);
527 again:
528 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
529 sme = LIST_NEXT(sme, sme_list)) {
530 if (strcmp(sme->sme_name, name) == 0) {
531 if (sme->sme_flags & SME_FLAG_BUSY) {
532 sme->sme_flags |= SME_FLAG_WANTED;
533 cv_wait(&sme_list_cv, &sme_list_mtx);
534 goto again;
535 }
536 sme->sme_flags |= SME_FLAG_BUSY;
537 break;
538 }
539 }
540 mutex_exit(&sme_list_mtx);
541 return sme;
542 }
543
544 /*
545 * sysmon_envsys_release:
546 *
547 * + Release an envsys device.
548 */
549 void
550 sysmon_envsys_release(struct sysmon_envsys *sme)
551 {
552 mutex_enter(&sme_list_mtx);
553 if (sme->sme_flags & SME_FLAG_WANTED)
554 cv_broadcast(&sme_list_cv);
555 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
556 mutex_exit(&sme_list_mtx);
557 }
558
559 /* compatibility function */
560 #ifdef COMPAT_40
561 struct sysmon_envsys *
562 sysmon_envsys_find_40(u_int idx)
563 {
564 struct sysmon_envsys *sme;
565
566 mutex_enter(&sme_list_mtx);
567 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
568 sme = LIST_NEXT(sme, sme_list)) {
569 if (idx >= sme->sme_fsensor &&
570 idx < (sme->sme_fsensor + sme->sme_nsensors))
571 break;
572 }
573 mutex_exit(&sme_list_mtx);
574 return sme;
575 }
576 #endif
577
578 /*
579 * sysmon_envsys_createplist:
580 *
581 * + Create the property list structure for a device.
582 */
583 int
584 sysmon_envsys_createplist(struct sysmon_envsys *sme)
585 {
586 envsys_data_t *edata;
587 prop_array_t array;
588 int i, error = 0;
589
590 /* create the sysmon envsys device array. */
591 array = prop_array_create();
592
593 /*
594 * <dict>
595 * <key>foo0</key>
596 * <array>
597 * ...
598 */
599 mutex_enter(&sme_mtx);
600 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
601 DPRINTF(("%s: prop_dictionary_set\n", __func__));
602 mutex_exit(&sme_mtx);
603 error = EINVAL;
604 goto out;
605 }
606 mutex_exit(&sme_mtx);
607
608 /*
609 * Iterate over all sensors and create a dictionary with all
610 * values specified by the sysmon envsys driver.
611 */
612 for (i = 0; i < sme->sme_nsensors; i++) {
613 edata = &sme->sme_sensor_data[i];
614 /*
615 * refresh sensor data via sme_gtredata only if the
616 * flag is not set.
617 */
618 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
619 mutex_enter(&sme_mtx);
620 error = (*sme->sme_gtredata)(sme, edata);
621 if (error) {
622 DPRINTF(("%s: sme->sme_gtredata[%d]\n",
623 __func__, i));
624 mutex_exit(&sme_mtx);
625 continue;
626 }
627 mutex_exit(&sme_mtx);
628 }
629
630 error = sme_make_dictionary(sme, array, edata);
631 if (error && error != EEXIST)
632 goto out;
633 else {
634 error = 0;
635 continue;
636 }
637 }
638
639 out:
640 prop_object_release(array);
641 return error;
642 }
643
644 /*
645 * sme_make_dictionary:
646 *
647 * + Create sensor's dictionary in device's dictionary array.
648 */
649 int
650 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
651 envsys_data_t *edata)
652 {
653 const struct sme_sensor_type *est = sme_sensor_type;
654 const struct sme_sensor_state *ess = sme_sensor_state;
655 const struct sme_sensor_state *esds = sme_sensor_drive_state;
656 sme_event_drv_t *sme_evdrv_t = NULL;
657 prop_dictionary_t dict;
658 envsys_data_t *nedata = NULL;
659 int i, j, k, l;
660
661 i = j = k = l = 0;
662
663 /*
664 * <array>
665 * <dict>
666 * ...
667 */
668 dict = prop_dictionary_create();
669
670 mutex_enter(&sme_mtx);
671 if (!prop_array_add(array, dict)) {
672 mutex_exit(&sme_mtx);
673 DPRINTF(("%s: prop_array_add\n", __func__));
674 return EINVAL;
675 }
676
677 /* find the correct unit for this sensor. */
678 for (i = 0; est[i].type != -1; i++)
679 if (est[i].type == edata->units)
680 break;
681
682 /*
683 * ...
684 * <key>type</key>
685 * <string>foo</string>
686 * <key>description</key>
687 * <string>blah blah</string>
688 * ...
689 */
690 SENSOR_SSTRING(dict, "type", est[i].desc);
691
692 /*
693 * Check if description was already assigned in other sensor.
694 */
695 for (l = 0; l < sme->sme_nsensors; l++) {
696 /* skip the same sensor */
697 if (l == edata->sensor)
698 continue;
699 nedata = &sme->sme_sensor_data[l];
700 if (strcmp(edata->desc, nedata->desc) == 0) {
701 edata->flags |= ENVSYS_FDUPDESC;
702 mutex_exit(&sme_mtx);
703 DPRINTF(("%s: dupdesc=%s sensor=%d dev=%s\n",
704 __func__, edata->desc, edata->sensor,
705 sme->sme_name));
706 return EEXIST;
707 }
708 }
709
710 SENSOR_SSTRING(dict, "description", edata->desc);
711
712 /*
713 * Add sensor's state description.
714 *
715 * ...
716 * <key>state</key>
717 * <string>valid</string>
718 * ...
719 */
720 for (j = 0; ess[j].type != -1; j++)
721 if (ess[j].type == edata->state)
722 break;
723
724 SENSOR_SSTRING(dict, "state", ess[j].desc);
725
726 /*
727 * add the percentage boolean object:
728 *
729 * ...
730 * <key>want-percentage</key>
731 * <true/>
732 * ...
733 */
734 if (edata->flags & ENVSYS_FPERCENT)
735 SENSOR_SBOOL(dict, "want-percentage", true);
736
737 /*
738 * Add the monitoring boolean object:
739 *
740 * ...
741 * <key>monitoring-supported</key>
742 * <true/>
743 * ...
744 *
745 * always false on Drive and Indicator types, they
746 * cannot be monitored.
747 *
748 */
749 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
750 (edata->units == ENVSYS_INDICATOR) ||
751 (edata->units == ENVSYS_DRIVE)) {
752 SENSOR_SBOOL(dict, "monitoring-supported", false);
753 } else {
754 SENSOR_SBOOL(dict, "monitoring-supported", true);
755 }
756
757 /*
758 * Add the drive-state object for drive sensors:
759 *
760 * ...
761 * <key>drive-state</key>
762 * <string>drive is online</string>
763 * ...
764 */
765 if (edata->units == ENVSYS_DRIVE) {
766 for (k = 0; esds[k].type != -1; k++)
767 if (esds[k].type == edata->value_cur)
768 break;
769 SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
770 }
771
772 mutex_exit(&sme_mtx);
773 /*
774 * Add a new event if a monitoring flag was set.
775 */
776 if (edata->monitor) {
777 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
778
779 sme_evdrv_t->sdict = dict;
780 sme_evdrv_t->edata = edata;
781 sme_evdrv_t->sme = sme;
782 sme_evdrv_t->powertype = est[i].crittype;
783
784 sysmon_task_queue_init();
785 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
786 }
787
788 mutex_enter(&sme_mtx);
789 /* if sensor is enabled, add the following properties... */
790 if (edata->state == ENVSYS_SVALID) {
791 /*
792 * ...
793 * <key>rpms</key>
794 * <integer>2500</integer>
795 * <key>rfact</key>
796 * <integer>10000</integer>
797 * <key>cur-value</key>
798 * <integer>1250</integer>
799 * <key>min-value</key>
800 * <integer>800</integer>
801 * <key>max-value</integer>
802 * <integer>3000</integer>
803 * <key>avg-value</integer>
804 * <integer>1400</integer>
805 * </dict>
806 */
807 if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
808 SENSOR_SUINT32(dict, "rpms", edata->rpms);
809
810 if ((edata->units == ENVSYS_SVOLTS_AC ||
811 edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
812 SENSOR_SINT32(dict, "rfact", edata->rfact);
813
814 if (edata->value_cur)
815 SENSOR_SINT32(dict, "cur-value", edata->value_cur);
816
817 if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
818 SENSOR_SINT32(dict, "min-value", edata->value_min);
819
820 if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
821 SENSOR_SINT32(dict, "max-value", edata->value_max);
822
823 if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
824 SENSOR_SINT32(dict, "avg-value", edata->value_avg);
825 }
826
827 /*
828 * ...
829 * </array>
830 */
831 out:
832 mutex_exit(&sme_mtx);
833 prop_object_release(dict);
834 return 0;
835 }
836
837 /*
838 * sme_update_dictionary:
839 *
840 * + Update per-sensor dictionaries with new values if there were
841 * changes, otherwise the object in dictionary is untouched.
842 * + Send a critical event if any sensor is in a critical condition.
843 */
844 int
845 sme_update_dictionary(struct sysmon_envsys *sme)
846 {
847 const struct sme_sensor_state *ess = sme_sensor_state;
848 const struct sme_sensor_state *esds = sme_sensor_drive_state;
849 envsys_data_t *edata = NULL;
850 prop_object_t array, obj, dict = NULL;
851 int i, j, error = 0;
852
853 /* retrieve the array of dictionaries in device. */
854 array = prop_dictionary_get(sme_propd, sme->sme_name);
855 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
856 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
857 return EINVAL;
858 }
859
860 /*
861 * - iterate over all sensors.
862 * - fetch new data.
863 * - check if data in dictionary is different than new data.
864 * - update dictionary if there were changes.
865 */
866 for (i = 0; i < sme->sme_nsensors; i++) {
867 edata = &sme->sme_sensor_data[i];
868
869 /* skip sensors with a duplicate description */
870 if (edata->flags & ENVSYS_FDUPDESC) {
871 DPRINTF(("%s: dupdesc=%s sensor=%d dev=%s\n",
872 __func__, edata->desc, edata->sensor,
873 sme->sme_name));
874 continue;
875 }
876
877 /*
878 * refresh sensor data via sme_gtredata only if the
879 * flag is not set.
880 */
881 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
882 error = (*sme->sme_gtredata)(sme, edata);
883 if (error) {
884 DPRINTF(("%s: gtredata[%d] failed\n",
885 __func__, i));
886 return error;
887 }
888 }
889
890 /* retrieve sensor's dictionary. */
891 dict = prop_array_get(array, i);
892 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
893 DPRINTF(("%s: not a dictionary (%d:%s)\n",
894 __func__, edata->sensor, sme->sme_name));
895 return EINVAL;
896 }
897
898 /* update state sensor. */
899 for (j = 0; ess[j].type != -1; j++)
900 if (ess[j].type == edata->state)
901 break;
902
903 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
904 "units=%d sensor=%d\n", __func__, ess[j].desc,
905 ess[j].type, edata->flags, edata->units, edata->sensor));
906
907 /* update sensor state */
908 SENSOR_UPSTRING(dict, "state", ess[j].desc);
909
910 /* update sensor current value */
911 SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
912
913 /*
914 * Integer and Indicator types do not the following
915 * values, so skip them.
916 */
917 if (edata->units == ENVSYS_INTEGER ||
918 edata->units == ENVSYS_INDICATOR)
919 continue;
920
921 /* update sensor flags */
922 if (edata->flags & ENVSYS_FPERCENT)
923 SENSOR_SBOOL(dict, "want-percentage", true);
924 else {
925 obj = prop_dictionary_get(dict, "want-percentage");
926 if (obj)
927 SENSOR_SBOOL(dict, "want-percentage", false);
928 }
929
930 if (edata->flags & ENVSYS_FVALID_MAX)
931 SENSOR_UPINT32(dict, "max-value", edata->value_max);
932
933 if (edata->flags & ENVSYS_FVALID_MIN)
934 SENSOR_UPINT32(dict, "min-value", edata->value_min);
935
936 if (edata->flags & ENVSYS_FVALID_AVG)
937 SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
938
939 /* update 'rpms' only in ENVSYS_SFANRPM. */
940 if (edata->units == ENVSYS_SFANRPM)
941 SENSOR_UPUINT32(dict, "rpms", edata->rpms);
942
943 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
944 if (edata->units == ENVSYS_SVOLTS_AC ||
945 edata->units == ENVSYS_SVOLTS_DC) {
946 SENSOR_UPINT32(dict, "rfact", edata->rfact);
947 }
948
949 /* update 'drive-state' only in ENVSYS_DRIVE. */
950 if (edata->units == ENVSYS_DRIVE) {
951 for (j = 0; esds[j].type != -1; j++)
952 if (esds[j].type == edata->value_cur)
953 break;
954
955 SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
956 }
957 }
958
959 out:
960 return error;
961 }
962
963 /*
964 * sme_userset_dictionary:
965 *
966 * + Parses the userland dictionary and run the appropiate
967 * tasks that were requested.
968 */
969 int
970 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
971 prop_array_t array)
972 {
973 const struct sme_sensor_type *sst = sme_sensor_type;
974 envsys_data_t *edata, *nedata;
975 prop_dictionary_t dict;
976 prop_object_t obj, obj1, obj2;
977 int32_t critval;
978 int i, error = 0;
979 const char *blah, *sname;
980 bool targetfound = false;
981
982 blah = sname = NULL;
983
984 /* get sensor's name from userland dictionary. */
985 obj = prop_dictionary_get(udict, "sensor-name");
986 if (prop_object_type(obj) != PROP_TYPE_STRING) {
987 DPRINTF(("%s: sensor-name failed\n", __func__));
988 return EINVAL;
989 }
990
991 /* iterate over the sensors to find the right one */
992 for (i = 0; i < sme->sme_nsensors; i++) {
993 edata = &sme->sme_sensor_data[i];
994 dict = prop_array_get(array, i);
995 obj1 = prop_dictionary_get(dict, "description");
996
997 /* is it our sensor? */
998 if (!prop_string_equals(obj1, obj))
999 continue;
1000
1001 /*
1002 * Check if a new description operation was
1003 * requested by the user and set new description.
1004 */
1005 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1006 targetfound = true;
1007 blah = prop_string_cstring_nocopy(obj2);
1008
1009 for (i = 0; i < sme->sme_nsensors; i++) {
1010 if (i == edata->sensor)
1011 continue;
1012
1013 nedata = &sme->sme_sensor_data[i];
1014 if (strcmp(blah, nedata->desc) == 0) {
1015 error = EEXIST;
1016 goto out;
1017 }
1018 }
1019
1020 SENSOR_UPSTRING(dict, "description", blah);
1021 (void)strlcpy(edata->desc, blah, sizeof(edata->desc));
1022
1023 break;
1024 }
1025
1026 /* did the user want to remove a critical capacity limit? */
1027 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1028 if (obj2 != NULL) {
1029 targetfound = true;
1030 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1031 (edata->flags & ENVSYS_FPERCENT) == 0) {
1032 error = ENOTSUP;
1033 break;
1034 }
1035
1036 sname = prop_string_cstring_nocopy(obj);
1037 error = sme_event_unregister(sname,
1038 PENVSYS_EVENT_BATT_USERCAP);
1039 if (error)
1040 break;
1041
1042 prop_dictionary_remove(dict, "critical-capacity");
1043 break;
1044 }
1045
1046 /* did the user want to remove a critical min limit? */
1047 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1048 if (obj2 != NULL) {
1049 targetfound = true;
1050 sname = prop_string_cstring_nocopy(obj);
1051 error = sme_event_unregister(sname,
1052 PENVSYS_EVENT_USER_CRITMIN);
1053 if (error)
1054 break;
1055
1056 prop_dictionary_remove(dict, "critical-min-limit");
1057 break;
1058 }
1059
1060 /* did the user want to remove a critical max limit? */
1061 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1062 if (obj2 != NULL) {
1063 targetfound = true;
1064 sname = prop_string_cstring_nocopy(obj);
1065 error = sme_event_unregister(sname,
1066 PENVSYS_EVENT_USER_CRITMAX);
1067 if (error)
1068 break;
1069
1070 prop_dictionary_remove(dict, "critical-max-limit");
1071 break;
1072 }
1073
1074 /* did the user want to change rfact? */
1075 obj2 = prop_dictionary_get(udict, "new-rfact");
1076 if (obj2 != NULL) {
1077 targetfound = true;
1078 if (edata->flags & ENVSYS_FCHANGERFACT)
1079 edata->rfact = prop_number_integer_value(obj2);
1080 else
1081 error = ENOTSUP;
1082
1083 break;
1084 }
1085
1086 for (i = 0; sst[i].type != -1; i++)
1087 if (sst[i].type == edata->units)
1088 break;
1089
1090 /* did the user want to set a critical capacity event? */
1091 obj2 = prop_dictionary_get(udict, "critical-capacity");
1092 if (obj2 != NULL) {
1093 targetfound = true;
1094 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1095 (edata->flags & ENVSYS_FPERCENT) == 0) {
1096 error = ENOTSUP;
1097 break;
1098 }
1099
1100 critval = prop_number_integer_value(obj2);
1101 error = sme_event_add(dict,
1102 edata,
1103 sme->sme_name,
1104 "critical-capacity",
1105 critval,
1106 PENVSYS_EVENT_BATT_USERCAP,
1107 sst[i].crittype);
1108 break;
1109 }
1110
1111 /* did the user want to set a critical max event? */
1112 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1113 if (obj2 != NULL) {
1114 targetfound = true;
1115 if (edata->units == ENVSYS_INDICATOR ||
1116 edata->flags & ENVSYS_FMONNOTSUPP) {
1117 error = ENOTSUP;
1118 break;
1119 }
1120
1121 critval = prop_number_integer_value(obj2);
1122 error = sme_event_add(dict,
1123 edata,
1124 sme->sme_name,
1125 "critical-max-limit",
1126 critval,
1127 PENVSYS_EVENT_USER_CRITMAX,
1128 sst[i].crittype);
1129 break;
1130 }
1131
1132 /* did the user want to set a critical min event? */
1133 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1134 if (obj2 != NULL) {
1135 targetfound = true;
1136 if (edata->units == ENVSYS_INDICATOR ||
1137 edata->flags & ENVSYS_FMONNOTSUPP) {
1138 error = ENOTSUP;
1139 break;
1140 }
1141
1142 critval = prop_number_integer_value(obj2);
1143 error = sme_event_add(dict,
1144 edata,
1145 sme->sme_name,
1146 "critical-min-limit",
1147 critval,
1148 PENVSYS_EVENT_USER_CRITMIN,
1149 sst[i].crittype);
1150 break;
1151 }
1152 }
1153
1154 /* invalid target? return the error */
1155 if (!targetfound)
1156 error = EINVAL;
1157
1158 out:
1159 return error;
1160 }
1161