sysmon_envsys.c revision 1.22 1 /* $NetBSD: sysmon_envsys.c,v 1.22 2007/07/17 15:43:08 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.22 2007/07/17 15:43:08 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) {
632 DPRINTF(("%s: sme_make_dictionary[%d]\n", __func__, i));
633 goto out;
634 }
635 }
636
637 out:
638 prop_object_release(array);
639 return error;
640 }
641
642 /*
643 * sme_make_dictionary:
644 *
645 * + Create sensor's dictionary in device's dictionary array.
646 */
647 int
648 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
649 envsys_data_t *edata)
650 {
651 const struct sme_sensor_type *est = sme_sensor_type;
652 const struct sme_sensor_state *ess = sme_sensor_state;
653 const struct sme_sensor_state *esds = sme_sensor_drive_state;
654 sme_event_drv_t *sme_evdrv_t = NULL;
655 prop_dictionary_t dict;
656 int i, j, k;
657
658 i = j = k = 0;
659
660 /*
661 * <array>
662 * <dict>
663 * ...
664 */
665 dict = prop_dictionary_create();
666
667 mutex_enter(&sme_mtx);
668 if (!prop_array_add(array, dict)) {
669 mutex_exit(&sme_mtx);
670 DPRINTF(("%s: prop_array_add\n", __func__));
671 return EINVAL;
672 }
673
674 /* find the correct unit for this sensor. */
675 for (i = 0; est[i].type != -1; i++)
676 if (est[i].type == edata->units)
677 break;
678
679 /*
680 * ...
681 * <key>type</key>
682 * <string>foo</string>
683 * <key>description</key>
684 * <string>blah blah</string>
685 * ...
686 */
687 SENSOR_SSTRING(dict, "type", est[i].desc);
688 SENSOR_SSTRING(dict, "description", edata->desc);
689
690 /*
691 * Add sensor's state description.
692 *
693 * ...
694 * <key>state</key>
695 * <string>valid</string>
696 * ...
697 */
698 for (j = 0; ess[j].type != -1; j++)
699 if (ess[j].type == edata->state)
700 break;
701
702 SENSOR_SSTRING(dict, "state", ess[j].desc);
703
704 /*
705 * add the percentage boolean object:
706 *
707 * ...
708 * <key>want-percentage</key>
709 * <true/>
710 * ...
711 */
712 if (edata->flags & ENVSYS_FPERCENT)
713 SENSOR_SBOOL(dict, "want-percentage", true);
714
715 /*
716 * Add the monitoring boolean object:
717 *
718 * ...
719 * <key>monitoring-supported</key>
720 * <true/>
721 * ...
722 *
723 * always false on Drive and Indicator types, they
724 * cannot be monitored.
725 *
726 */
727 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
728 (edata->units == ENVSYS_INDICATOR) ||
729 (edata->units == ENVSYS_DRIVE)) {
730 SENSOR_SBOOL(dict, "monitoring-supported", false);
731 } else {
732 SENSOR_SBOOL(dict, "monitoring-supported", true);
733 }
734
735 /*
736 * Add the drive-state object for drive sensors:
737 *
738 * ...
739 * <key>drive-state</key>
740 * <string>drive is online</string>
741 * ...
742 */
743 if (edata->units == ENVSYS_DRIVE) {
744 for (k = 0; esds[k].type != -1; k++)
745 if (esds[k].type == edata->value_cur)
746 break;
747 SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
748 }
749
750 mutex_exit(&sme_mtx);
751 /*
752 * Add a new event if a monitoring flag was set.
753 */
754 if (edata->monitor) {
755 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
756
757 sme_evdrv_t->sdict = dict;
758 sme_evdrv_t->edata = edata;
759 sme_evdrv_t->sme = sme;
760 sme_evdrv_t->powertype = est[i].crittype;
761
762 sysmon_task_queue_init();
763 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
764 }
765
766 mutex_enter(&sme_mtx);
767 /* if sensor is enabled, add the following properties... */
768 if (edata->state == ENVSYS_SVALID) {
769 /*
770 * ...
771 * <key>rpms</key>
772 * <integer>2500</integer>
773 * <key>rfact</key>
774 * <integer>10000</integer>
775 * <key>cur-value</key>
776 * <integer>1250</integer>
777 * <key>min-value</key>
778 * <integer>800</integer>
779 * <key>max-value</integer>
780 * <integer>3000</integer>
781 * <key>avg-value</integer>
782 * <integer>1400</integer>
783 * </dict>
784 */
785 if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
786 SENSOR_SUINT32(dict, "rpms", edata->rpms);
787
788 if ((edata->units == ENVSYS_SVOLTS_AC ||
789 edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
790 SENSOR_SINT32(dict, "rfact", edata->rfact);
791
792 if (edata->value_cur)
793 SENSOR_SINT32(dict, "cur-value", edata->value_cur);
794
795 if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
796 SENSOR_SINT32(dict, "min-value", edata->value_min);
797
798 if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
799 SENSOR_SINT32(dict, "max-value", edata->value_max);
800
801 if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
802 SENSOR_SINT32(dict, "avg-value", edata->value_avg);
803 }
804
805 /*
806 * ...
807 * </array>
808 */
809 out:
810 mutex_exit(&sme_mtx);
811 prop_object_release(dict);
812 return 0;
813 }
814
815 /*
816 * sme_update_dictionary:
817 *
818 * + Update per-sensor dictionaries with new values if there were
819 * changes, otherwise the object in dictionary is untouched.
820 * + Send a critical event if any sensor is in a critical condition.
821 */
822 int
823 sme_update_dictionary(struct sysmon_envsys *sme)
824 {
825 const struct sme_sensor_state *ess = sme_sensor_state;
826 const struct sme_sensor_state *esds = sme_sensor_drive_state;
827 envsys_data_t *edata = NULL;
828 prop_object_t array, obj, dict = NULL;
829 int i, j, error = 0;
830
831 /* retrieve the array of dictionaries in device. */
832 array = prop_dictionary_get(sme_propd, sme->sme_name);
833 if (prop_object_type(array) != PROP_TYPE_ARRAY)
834 return EINVAL;
835
836 /*
837 * - iterate over all sensors.
838 * - fetch new data.
839 * - check if data in dictionary is different than new data.
840 * - update dictionary if there were changes.
841 */
842 for (i = 0; i < sme->sme_nsensors; i++) {
843 edata = &sme->sme_sensor_data[i];
844
845 /*
846 * refresh sensor data via sme_gtredata only if the
847 * flag is not set.
848 */
849 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
850 error = (*sme->sme_gtredata)(sme, edata);
851 if (error) {
852 DPRINTF(("%s: gtredata[%d] failed\n",
853 __func__, i));
854 return error;
855 }
856 }
857
858 /* retrieve sensor's dictionary. */
859 dict = prop_array_get(array, i);
860 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY)
861 return EINVAL;
862
863 /* update state sensor. */
864 for (j = 0; ess[j].type != -1; j++)
865 if (ess[j].type == edata->state)
866 break;
867
868 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
869 "units=%d sensor=%d\n", __func__, ess[j].desc,
870 ess[j].type, edata->flags, edata->units, edata->sensor));
871
872 /* update sensor state */
873 SENSOR_UPSTRING(dict, "state", ess[j].desc);
874
875 /* update sensor current value */
876 SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
877
878 /*
879 * Integer and Indicator types do not the following
880 * values, so skip them.
881 */
882 if (edata->units == ENVSYS_INTEGER ||
883 edata->units == ENVSYS_INDICATOR)
884 continue;
885
886 /* update sensor flags */
887 if (edata->flags & ENVSYS_FPERCENT)
888 SENSOR_SBOOL(dict, "want-percentage", true);
889 else {
890 obj = prop_dictionary_get(dict, "want-percentage");
891 if (obj)
892 SENSOR_SBOOL(dict, "want-percentage", false);
893 }
894
895 if (edata->flags & ENVSYS_FVALID_MAX)
896 SENSOR_UPINT32(dict, "max-value", edata->value_max);
897
898 if (edata->flags & ENVSYS_FVALID_MIN)
899 SENSOR_UPINT32(dict, "min-value", edata->value_min);
900
901 if (edata->flags & ENVSYS_FVALID_AVG)
902 SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
903
904 /* update 'rpms' only in ENVSYS_SFANRPM. */
905 if (edata->units == ENVSYS_SFANRPM)
906 SENSOR_UPUINT32(dict, "rpms", edata->rpms);
907
908 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
909 if (edata->units == ENVSYS_SVOLTS_AC ||
910 edata->units == ENVSYS_SVOLTS_DC) {
911 SENSOR_UPINT32(dict, "rfact", edata->rfact);
912 }
913
914 /* update 'drive-state' only in ENVSYS_DRIVE. */
915 if (edata->units == ENVSYS_DRIVE) {
916 for (j = 0; esds[j].type != -1; j++)
917 if (esds[j].type == edata->value_cur)
918 break;
919
920 SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
921 }
922 }
923
924 out:
925 return error;
926 }
927
928 /*
929 * sme_userset_dictionary:
930 *
931 * + Parses the userland dictionary and run the appropiate
932 * tasks that were requested.
933 */
934 int
935 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
936 prop_array_t array)
937 {
938 const struct sme_sensor_type *sst = sme_sensor_type;
939 envsys_data_t *edata;
940 prop_dictionary_t dict;
941 prop_object_t obj, obj1, obj2;
942 int32_t critval;
943 int i, error = 0;
944 const char *blah, *sname;
945 bool targetfound = false;
946
947 blah = sname = NULL;
948
949 /* get sensor's name from userland dictionary. */
950 obj = prop_dictionary_get(udict, "sensor-name");
951 if (prop_object_type(obj) != PROP_TYPE_STRING) {
952 DPRINTF(("%s: sensor-name failed\n", __func__));
953 return EINVAL;
954 }
955
956 /* iterate over the sensors to find the right one */
957 for (i = 0; i < sme->sme_nsensors; i++) {
958 edata = &sme->sme_sensor_data[i];
959 dict = prop_array_get(array, i);
960 obj1 = prop_dictionary_get(dict, "description");
961
962 /* is it our sensor? */
963 if (!prop_string_equals(obj1, obj))
964 continue;
965
966 /*
967 * Check if a new description operation was
968 * requested by the user and set new description.
969 */
970 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
971 targetfound = true;
972 blah = prop_string_cstring_nocopy(obj2);
973 SENSOR_UPSTRING(dict, "description", blah);
974 break;
975 }
976
977 /* did the user want to remove a critical capacity limit? */
978 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
979 if (obj2 != NULL) {
980 targetfound = true;
981 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
982 (edata->flags & ENVSYS_FPERCENT) == 0) {
983 error = ENOTSUP;
984 break;
985 }
986
987 sname = prop_string_cstring_nocopy(obj);
988 error = sme_event_unregister(sname,
989 PENVSYS_EVENT_BATT_USERCAP);
990 if (error)
991 break;
992
993 prop_dictionary_remove(dict, "critical-capacity");
994 break;
995 }
996
997 /* did the user want to remove a critical min limit? */
998 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
999 if (obj2 != NULL) {
1000 targetfound = true;
1001 sname = prop_string_cstring_nocopy(obj);
1002 error = sme_event_unregister(sname,
1003 PENVSYS_EVENT_USER_CRITMIN);
1004 if (error)
1005 break;
1006
1007 prop_dictionary_remove(dict, "critical-min-limit");
1008 break;
1009 }
1010
1011 /* did the user want to remove a critical max limit? */
1012 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1013 if (obj2 != NULL) {
1014 targetfound = true;
1015 sname = prop_string_cstring_nocopy(obj);
1016 error = sme_event_unregister(sname,
1017 PENVSYS_EVENT_USER_CRITMAX);
1018 if (error)
1019 break;
1020
1021 prop_dictionary_remove(dict, "critical-max-limit");
1022 break;
1023 }
1024
1025 /* did the user want to change rfact? */
1026 obj2 = prop_dictionary_get(udict, "new-rfact");
1027 if (obj2 != NULL) {
1028 targetfound = true;
1029 if (edata->flags & ENVSYS_FCHANGERFACT)
1030 edata->rfact = prop_number_integer_value(obj2);
1031 else
1032 error = ENOTSUP;
1033
1034 break;
1035 }
1036
1037 for (i = 0; sst[i].type != -1; i++)
1038 if (sst[i].type == edata->units)
1039 break;
1040
1041 /* did the user want to set a critical capacity event? */
1042 obj2 = prop_dictionary_get(udict, "critical-capacity");
1043 if (obj2 != NULL) {
1044 targetfound = true;
1045 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1046 (edata->flags & ENVSYS_FPERCENT) == 0) {
1047 error = ENOTSUP;
1048 break;
1049 }
1050
1051 critval = prop_number_integer_value(obj2);
1052 error = sme_event_add(dict,
1053 edata,
1054 sme->sme_name,
1055 "critical-capacity",
1056 critval,
1057 PENVSYS_EVENT_BATT_USERCAP,
1058 sst[i].crittype);
1059 break;
1060 }
1061
1062 /* did the user want to set a critical max event? */
1063 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1064 if (obj2 != NULL) {
1065 targetfound = true;
1066 if (edata->units == ENVSYS_INDICATOR ||
1067 edata->flags & ENVSYS_FMONNOTSUPP) {
1068 error = ENOTSUP;
1069 break;
1070 }
1071
1072 critval = prop_number_integer_value(obj2);
1073 error = sme_event_add(dict,
1074 edata,
1075 sme->sme_name,
1076 "critical-max-limit",
1077 critval,
1078 PENVSYS_EVENT_USER_CRITMAX,
1079 sst[i].crittype);
1080 break;
1081 }
1082
1083 /* did the user want to set a critical min event? */
1084 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1085 if (obj2 != NULL) {
1086 targetfound = true;
1087 if (edata->units == ENVSYS_INDICATOR ||
1088 edata->flags & ENVSYS_FMONNOTSUPP) {
1089 error = ENOTSUP;
1090 break;
1091 }
1092
1093 critval = prop_number_integer_value(obj2);
1094 error = sme_event_add(dict,
1095 edata,
1096 sme->sme_name,
1097 "critical-min-limit",
1098 critval,
1099 PENVSYS_EVENT_USER_CRITMIN,
1100 sst[i].crittype);
1101 break;
1102 }
1103 }
1104
1105 /* invalid target? return the error */
1106 if (!targetfound)
1107 error = EINVAL;
1108
1109 out:
1110 return error;
1111 }
1112