sysmon_envsys.c revision 1.26 1 /* $NetBSD: sysmon_envsys.c,v 1.26 2007/07/18 20:31:41 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.26 2007/07/18 20:31:41 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 kcondvar_t sme_event_cv;
162
163 /*
164 * sysmon_envsys_init:
165 *
166 * + Initialize global mutexes, dictionary and the linked lists.
167 */
168 void
169 sysmon_envsys_init(void)
170 {
171 LIST_INIT(&sysmon_envsys_list);
172 LIST_INIT(&sme_events_list);
173 mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
174 mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
175 mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
176 mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
177 cv_init(&sme_list_cv, "smefind");
178 cv_init(&sme_event_cv, "smeevent");
179 sme_propd = prop_dictionary_create();
180 }
181
182 /*
183 * sysmonopen_envsys:
184 *
185 * + Open the system monitor device.
186 */
187 int
188 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
189 {
190 return 0;
191 }
192
193 /*
194 * sysmonclose_envsys:
195 *
196 * + Close the system monitor device.
197 */
198 int
199 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
200 {
201 /* Nothing to do */
202 return 0;
203 }
204
205 /*
206 * sysmonioctl_envsys:
207 *
208 * + Perform an envsys control request.
209 */
210 int
211 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
212 {
213 struct sysmon_envsys *sme = NULL;
214 int error = 0;
215 #ifdef COMPAT_40
216 u_int oidx;
217 #endif
218
219 switch (cmd) {
220 case ENVSYS_GETDICTIONARY:
221 {
222 struct plistref *plist = (struct plistref *)data;
223
224 /*
225 * Update all sysmon envsys devices dictionaries with
226 * new data if it's different than we have currently
227 * in the dictionary.
228 */
229 mutex_enter(&sme_mtx);
230 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
231 if (sme == NULL)
232 continue;
233
234 error = sme_update_dictionary(sme);
235 if (error) {
236 DPRINTF(("%s: sme_update_dictionary, "
237 "error=%d\n", __func__, error));
238 mutex_exit(&sme_mtx);
239 return error;
240 }
241 }
242 mutex_exit(&sme_mtx);
243 /*
244 * Copy global dictionary to userland.
245 */
246 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
247 break;
248 }
249 case ENVSYS_SETDICTIONARY:
250 {
251 const struct plistref *plist = (const struct plistref *)data;
252 prop_dictionary_t udict;
253 prop_object_t obj;
254 const char *devname = NULL;
255
256 if ((flag & FWRITE) == 0)
257 return EPERM;
258
259 /*
260 * Get dictionary from userland.
261 */
262 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
263 DPRINTF(("%s: copyin_ioctl error=%d\n", __func__, error));
264 if (error)
265 break;
266
267 /*
268 * Parse "driver-name" key to obtain the driver we
269 * are searching for.
270 */
271 obj = prop_dictionary_get(udict, "driver-name");
272 if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
273 DPRINTF(("%s: driver-name failed\n", __func__));
274 prop_object_release(udict);
275 error = EINVAL;
276 break;
277 }
278
279 /* driver name */
280 devname = prop_string_cstring_nocopy(obj);
281
282 /* find the correct sme device */
283 sme = sysmon_envsys_find(devname);
284 if (sme == NULL) {
285 DPRINTF(("%s: NULL sme\n", __func__));
286 prop_object_release(udict);
287 error = EINVAL;
288 break;
289 }
290
291 /*
292 * Find the correct array object with the string supplied
293 * by the userland dictionary.
294 */
295 obj = prop_dictionary_get(sme_propd, devname);
296 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
297 DPRINTF(("%s: array device failed\n", __func__));
298 prop_object_release(udict);
299 error = EINVAL;
300 break;
301 }
302
303 /* do the real work now */
304 error = sme_userset_dictionary(sme, udict, obj);
305 sysmon_envsys_release(sme);
306 break;
307 }
308 /*
309 * Compatibility functions with the old interface, only
310 * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
311 * to make old applications work.
312 */
313 #ifdef COMPAT_40
314 case ENVSYS_GTREDATA:
315 {
316 struct envsys_tre_data *tred = (void *)data;
317 envsys_data_t *edata = NULL;
318
319 tred->validflags = 0;
320
321 sme = sysmon_envsys_find_40(tred->sensor);
322 if (sme == NULL)
323 break;
324
325 mutex_enter(&sme_mtx);
326 oidx = tred->sensor;
327 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
328
329 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
330 __func__, tred->sensor, oidx, sme->sme_name,
331 sme->sme_nsensors));
332
333 edata = &sme->sme_sensor_data[tred->sensor];
334
335 if (tred->sensor < sme->sme_nsensors) {
336 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
337 error = (*sme->sme_gtredata)(sme, edata);
338 if (error) {
339 DPRINTF(("%s: sme_gtredata failed\n",
340 __func__));
341 mutex_exit(&sme_mtx);
342 return error;
343 }
344 }
345
346 /* copy required values to the old interface */
347 tred->sensor = edata->sensor;
348 tred->cur.data_us = edata->value_cur;
349 tred->cur.data_s = edata->value_cur;
350 tred->max.data_us = edata->value_max;
351 tred->max.data_s = edata->value_max;
352 tred->min.data_us = edata->value_min;
353 tred->min.data_s = edata->value_min;
354 tred->avg.data_us = edata->value_avg;
355 tred->avg.data_s = edata->value_avg;
356 tred->units = edata->units;
357
358 tred->validflags |= ENVSYS_FVALID;
359 tred->validflags |= ENVSYS_FCURVALID;
360
361 if (edata->flags & ENVSYS_FPERCENT) {
362 tred->validflags |= ENVSYS_FMAXVALID;
363 tred->validflags |= ENVSYS_FFRACVALID;
364 }
365
366 if (edata->state == ENVSYS_SINVALID) {
367 tred->validflags &= ~ENVSYS_FCURVALID;
368 tred->cur.data_us = tred->cur.data_s = 0;
369 }
370
371 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
372 __func__, edata->desc, tred->cur.data_s));
373 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
374 " tred->sensor=%d\n", __func__, tred->validflags,
375 tred->units, tred->sensor));
376 }
377 tred->sensor = oidx;
378 mutex_exit(&sme_mtx);
379
380 break;
381 }
382 case ENVSYS_GTREINFO:
383 {
384 struct envsys_basic_info *binfo = (void *)data;
385 envsys_data_t *edata = NULL;
386
387 binfo->validflags = 0;
388
389 sme = sysmon_envsys_find_40(binfo->sensor);
390 if (sme == NULL)
391 break;
392
393 mutex_enter(&sme_mtx);
394 oidx = binfo->sensor;
395 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
396
397 edata = &sme->sme_sensor_data[binfo->sensor];
398
399 binfo->validflags |= ENVSYS_FVALID;
400
401 if (binfo->sensor < sme->sme_nsensors) {
402 binfo->units = edata->units;
403 (void)strlcpy(binfo->desc, edata->desc,
404 sizeof(binfo->desc));
405 }
406
407 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
408 __func__, binfo->units, binfo->validflags));
409 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
410 __func__, binfo->desc, binfo->sensor));
411
412 binfo->sensor = oidx;
413 mutex_exit(&sme_mtx);
414
415 break;
416 }
417 #endif /* COMPAT_40 */
418 default:
419 error = ENOTTY;
420 break;
421 }
422
423 return error;
424 }
425
426 /*
427 * sysmon_envsys_register:
428 *
429 * + Register an envsys device.
430 * + Create device dictionary.
431 */
432 int
433 sysmon_envsys_register(struct sysmon_envsys *sme)
434 {
435 struct sysmon_envsys *lsme;
436 int error = 0;
437
438 /*
439 * sanity check 1, make sure the driver has initialized
440 * the sensors count or the value is not too high.
441 */
442 if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
443 return EINVAL;
444
445 /*
446 * sanity check 2, make sure that sme->sme_name and
447 * sme->sme_data are not NULL.
448 */
449 if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
450 return EINVAL;
451
452 /*
453 * sanity check 3, if SME_DISABLE_GTREDATA is not set
454 * the sme_gtredata function callback must be non NULL.
455 */
456 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
457 if (sme->sme_gtredata == NULL)
458 return EINVAL;
459 }
460
461 /*
462 * - check if requested sysmon_envsys device is valid
463 * and does not exist already in the list.
464 * - add device into the list.
465 * - create the plist structure.
466 */
467 mutex_enter(&sme_list_mtx);
468 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
469 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
470 mutex_exit(&sme_list_mtx);
471 error = EEXIST;
472 goto out;
473 }
474 }
475 #ifdef COMPAT_40
476 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
477 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
478 #endif
479 mutex_exit(&sme_list_mtx);
480 error = sysmon_envsys_createplist(sme);
481 if (!error) {
482 mutex_enter(&sme_list_mtx);
483 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
484 mutex_exit(&sme_list_mtx);
485 }
486
487 out:
488 return error;
489 }
490
491 /*
492 * sysmon_envsys_unregister:
493 *
494 * + Unregister an envsys device.
495 * + Unregister all events associated with this device.
496 * + Remove device dictionary.
497 */
498 void
499 sysmon_envsys_unregister(struct sysmon_envsys *sme)
500 {
501 sme_event_t *see;
502
503 mutex_enter(&sme_list_mtx);
504 while (sme->sme_flags & SME_FLAG_BUSY) {
505 sme->sme_flags |= SME_FLAG_WANTED;
506 cv_wait(&sme_list_cv, &sme_list_mtx);
507 }
508 prop_dictionary_remove(sme_propd, sme->sme_name);
509 LIST_FOREACH(see, &sme_events_list, see_list) {
510 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
511 sme_event_unregister(see->pes.pes_sensname,
512 see->type);
513 }
514 LIST_REMOVE(sme, sme_list);
515 mutex_exit(&sme_list_mtx);
516 }
517
518 /*
519 * sysmon_envsys_find:
520 *
521 * + Find an envsys device.
522 */
523 struct sysmon_envsys *
524 sysmon_envsys_find(const char *name)
525 {
526 struct sysmon_envsys *sme;
527
528 mutex_enter(&sme_list_mtx);
529 again:
530 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
531 sme = LIST_NEXT(sme, sme_list)) {
532 if (strcmp(sme->sme_name, name) == 0) {
533 if (sme->sme_flags & SME_FLAG_BUSY) {
534 sme->sme_flags |= SME_FLAG_WANTED;
535 cv_wait(&sme_list_cv, &sme_list_mtx);
536 goto again;
537 }
538 sme->sme_flags |= SME_FLAG_BUSY;
539 break;
540 }
541 }
542 mutex_exit(&sme_list_mtx);
543 return sme;
544 }
545
546 /*
547 * sysmon_envsys_release:
548 *
549 * + Release an envsys device.
550 */
551 void
552 sysmon_envsys_release(struct sysmon_envsys *sme)
553 {
554 mutex_enter(&sme_list_mtx);
555 if (sme->sme_flags & SME_FLAG_WANTED)
556 cv_broadcast(&sme_list_cv);
557 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
558 mutex_exit(&sme_list_mtx);
559 }
560
561 /* compatibility function */
562 #ifdef COMPAT_40
563 struct sysmon_envsys *
564 sysmon_envsys_find_40(u_int idx)
565 {
566 struct sysmon_envsys *sme;
567
568 mutex_enter(&sme_list_mtx);
569 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
570 sme = LIST_NEXT(sme, sme_list)) {
571 if (idx >= sme->sme_fsensor &&
572 idx < (sme->sme_fsensor + sme->sme_nsensors))
573 break;
574 }
575 mutex_exit(&sme_list_mtx);
576 return sme;
577 }
578 #endif
579
580 /*
581 * sysmon_envsys_createplist:
582 *
583 * + Create the property list structure for a device.
584 */
585 int
586 sysmon_envsys_createplist(struct sysmon_envsys *sme)
587 {
588 envsys_data_t *edata;
589 prop_array_t array;
590 int i, error = 0;
591
592 /* create the sysmon envsys device array. */
593 array = prop_array_create();
594
595 /*
596 * <dict>
597 * <key>foo0</key>
598 * <array>
599 * ...
600 */
601 mutex_enter(&sme_mtx);
602 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
603 DPRINTF(("%s: prop_dictionary_set\n", __func__));
604 mutex_exit(&sme_mtx);
605 error = EINVAL;
606 goto out;
607 }
608 mutex_exit(&sme_mtx);
609
610 /*
611 * Iterate over all sensors and create a dictionary with all
612 * values specified by the sysmon envsys driver.
613 */
614 for (i = 0; i < sme->sme_nsensors; i++) {
615 edata = &sme->sme_sensor_data[i];
616 /*
617 * refresh sensor data via sme_gtredata only if the
618 * flag is not set.
619 */
620 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
621 mutex_enter(&sme_mtx);
622 error = (*sme->sme_gtredata)(sme, edata);
623 if (error) {
624 DPRINTF(("%s: sme->sme_gtredata[%d]\n",
625 __func__, i));
626 mutex_exit(&sme_mtx);
627 continue;
628 }
629 mutex_exit(&sme_mtx);
630 }
631
632 error = sme_make_dictionary(sme, array, edata);
633 if (error && error != EEXIST)
634 goto out;
635 else {
636 error = 0;
637 continue;
638 }
639 }
640
641 out:
642 prop_object_release(array);
643 return error;
644 }
645
646 /*
647 * sme_make_dictionary:
648 *
649 * + Create sensor's dictionary in device's dictionary array.
650 */
651 int
652 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
653 envsys_data_t *edata)
654 {
655 const struct sme_sensor_type *est = sme_sensor_type;
656 const struct sme_sensor_state *ess = sme_sensor_state;
657 const struct sme_sensor_state *esds = sme_sensor_drive_state;
658 sme_event_drv_t *sme_evdrv_t = NULL;
659 prop_dictionary_t dict;
660 envsys_data_t *nedata = NULL;
661 int i, j, k, l;
662
663 i = j = k = l = 0;
664
665 /*
666 * <array>
667 * <dict>
668 * ...
669 */
670 dict = prop_dictionary_create();
671
672 mutex_enter(&sme_mtx);
673 if (!prop_array_add(array, dict)) {
674 mutex_exit(&sme_mtx);
675 prop_object_release(dict);
676 DPRINTF(("%s: prop_array_add\n", __func__));
677 return EINVAL;
678 }
679
680 /* find the correct unit for this sensor. */
681 for (i = 0; est[i].type != -1; i++)
682 if (est[i].type == edata->units)
683 break;
684
685 /*
686 * ...
687 * <key>type</key>
688 * <string>foo</string>
689 * <key>description</key>
690 * <string>blah blah</string>
691 * ...
692 */
693 SENSOR_SSTRING(dict, "type", est[i].desc);
694
695 /*
696 * Check if description was already assigned in other sensor.
697 */
698 for (l = 0; l < sme->sme_nsensors; l++) {
699 /* skip the same sensor */
700 if (l == edata->sensor)
701 continue;
702 nedata = &sme->sme_sensor_data[l];
703 if (strcmp(edata->desc, nedata->desc) == 0) {
704 edata->flags |= ENVSYS_FDUPDESC;
705 mutex_exit(&sme_mtx);
706 DPRINTF(("%s: dupdesc=%s sensor=%d dev=%s\n",
707 __func__, edata->desc, edata->sensor,
708 sme->sme_name));
709 return EEXIST;
710 }
711 }
712
713 SENSOR_SSTRING(dict, "description", edata->desc);
714
715 /*
716 * Add sensor's state description.
717 *
718 * ...
719 * <key>state</key>
720 * <string>valid</string>
721 * ...
722 */
723 for (j = 0; ess[j].type != -1; j++)
724 if (ess[j].type == edata->state)
725 break;
726
727 SENSOR_SSTRING(dict, "state", ess[j].desc);
728
729 /*
730 * add the percentage boolean object:
731 *
732 * ...
733 * <key>want-percentage</key>
734 * <true/>
735 * ...
736 */
737 if (edata->flags & ENVSYS_FPERCENT)
738 SENSOR_SBOOL(dict, "want-percentage", true);
739
740 /*
741 * Add the monitoring boolean object:
742 *
743 * ...
744 * <key>monitoring-supported</key>
745 * <true/>
746 * ...
747 *
748 * always false on Drive and Indicator types, they
749 * cannot be monitored.
750 *
751 */
752 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
753 (edata->units == ENVSYS_INDICATOR) ||
754 (edata->units == ENVSYS_DRIVE)) {
755 SENSOR_SBOOL(dict, "monitoring-supported", false);
756 } else {
757 SENSOR_SBOOL(dict, "monitoring-supported", true);
758 }
759
760 /*
761 * Add the drive-state object for drive sensors:
762 *
763 * ...
764 * <key>drive-state</key>
765 * <string>drive is online</string>
766 * ...
767 */
768 if (edata->units == ENVSYS_DRIVE) {
769 for (k = 0; esds[k].type != -1; k++)
770 if (esds[k].type == edata->value_cur)
771 break;
772 SENSOR_SSTRING(dict, "drive-state", esds[k].desc);
773 }
774
775 mutex_exit(&sme_mtx);
776 /*
777 * Add a new event if a monitoring flag was set.
778 */
779 if (edata->monitor) {
780 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
781
782 sme_evdrv_t->sdict = dict;
783 sme_evdrv_t->edata = edata;
784 sme_evdrv_t->sme = sme;
785 sme_evdrv_t->powertype = est[i].crittype;
786
787 sysmon_task_queue_init();
788 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
789 }
790
791 mutex_enter(&sme_mtx);
792 /* if sensor is enabled, add the following properties... */
793 if (edata->state == ENVSYS_SVALID) {
794 /*
795 * ...
796 * <key>rpms</key>
797 * <integer>2500</integer>
798 * <key>rfact</key>
799 * <integer>10000</integer>
800 * <key>cur-value</key>
801 * <integer>1250</integer>
802 * <key>min-value</key>
803 * <integer>800</integer>
804 * <key>max-value</integer>
805 * <integer>3000</integer>
806 * <key>avg-value</integer>
807 * <integer>1400</integer>
808 * </dict>
809 */
810 if ((edata->units == ENVSYS_SFANRPM) && edata->rpms)
811 SENSOR_SUINT32(dict, "rpms", edata->rpms);
812
813 if ((edata->units == ENVSYS_SVOLTS_AC ||
814 edata->units == ENVSYS_SVOLTS_DC) && edata->rfact)
815 SENSOR_SINT32(dict, "rfact", edata->rfact);
816
817 if (edata->value_cur)
818 SENSOR_SINT32(dict, "cur-value", edata->value_cur);
819
820 if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min)
821 SENSOR_SINT32(dict, "min-value", edata->value_min);
822
823 if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max)
824 SENSOR_SINT32(dict, "max-value", edata->value_max);
825
826 if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg)
827 SENSOR_SINT32(dict, "avg-value", edata->value_avg);
828 }
829
830 /*
831 * ...
832 * </array>
833 */
834 out:
835 mutex_exit(&sme_mtx);
836 prop_object_release(dict);
837 return 0;
838 }
839
840 /*
841 * sme_update_dictionary:
842 *
843 * + Update per-sensor dictionaries with new values if there were
844 * changes, otherwise the object in dictionary is untouched.
845 * + Send a critical event if any sensor is in a critical condition.
846 */
847 int
848 sme_update_dictionary(struct sysmon_envsys *sme)
849 {
850 const struct sme_sensor_state *ess = sme_sensor_state;
851 const struct sme_sensor_state *esds = sme_sensor_drive_state;
852 envsys_data_t *edata = NULL;
853 prop_object_t array, obj, dict = NULL;
854 int i, j, error = 0;
855
856 /* retrieve the array of dictionaries in device. */
857 array = prop_dictionary_get(sme_propd, sme->sme_name);
858 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
859 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
860 return EINVAL;
861 }
862
863 /*
864 * - iterate over all sensors.
865 * - fetch new data.
866 * - check if data in dictionary is different than new data.
867 * - update dictionary if there were changes.
868 */
869 for (i = 0; i < sme->sme_nsensors; i++) {
870 edata = &sme->sme_sensor_data[i];
871
872 /* skip sensors with a duplicate description */
873 if (edata->flags & ENVSYS_FDUPDESC) {
874 DPRINTF(("%s: dupdesc=%s sensor=%d dev=%s\n",
875 __func__, edata->desc, edata->sensor,
876 sme->sme_name));
877 continue;
878 }
879
880 /*
881 * refresh sensor data via sme_gtredata only if the
882 * flag is not set.
883 */
884 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
885 error = (*sme->sme_gtredata)(sme, edata);
886 if (error) {
887 DPRINTF(("%s: gtredata[%d] failed\n",
888 __func__, i));
889 return error;
890 }
891 }
892
893 /* retrieve sensor's dictionary. */
894 dict = prop_array_get(array, i);
895 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
896 DPRINTF(("%s: not a dictionary (%d:%s)\n",
897 __func__, edata->sensor, sme->sme_name));
898 return EINVAL;
899 }
900
901 /* update state sensor. */
902 for (j = 0; ess[j].type != -1; j++)
903 if (ess[j].type == edata->state)
904 break;
905
906 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
907 "units=%d sensor=%d\n", __func__, ess[j].desc,
908 ess[j].type, edata->flags, edata->units, edata->sensor));
909
910 /* update sensor state */
911 SENSOR_UPSTRING(dict, "state", ess[j].desc);
912
913 /* update sensor current value */
914 SENSOR_UPINT32(dict, "cur-value", edata->value_cur);
915
916 /*
917 * Integer and Indicator types do not the following
918 * values, so skip them.
919 */
920 if (edata->units == ENVSYS_INTEGER ||
921 edata->units == ENVSYS_INDICATOR)
922 continue;
923
924 /* update sensor flags */
925 if (edata->flags & ENVSYS_FPERCENT)
926 SENSOR_SBOOL(dict, "want-percentage", true);
927 else {
928 obj = prop_dictionary_get(dict, "want-percentage");
929 if (obj)
930 SENSOR_SBOOL(dict, "want-percentage", false);
931 }
932
933 if (edata->flags & ENVSYS_FVALID_MAX)
934 SENSOR_UPINT32(dict, "max-value", edata->value_max);
935
936 if (edata->flags & ENVSYS_FVALID_MIN)
937 SENSOR_UPINT32(dict, "min-value", edata->value_min);
938
939 if (edata->flags & ENVSYS_FVALID_AVG)
940 SENSOR_UPINT32(dict, "avg-value", edata->value_avg);
941
942 /* update 'rpms' only in ENVSYS_SFANRPM. */
943 if (edata->units == ENVSYS_SFANRPM)
944 SENSOR_UPUINT32(dict, "rpms", edata->rpms);
945
946 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
947 if (edata->units == ENVSYS_SVOLTS_AC ||
948 edata->units == ENVSYS_SVOLTS_DC) {
949 SENSOR_UPINT32(dict, "rfact", edata->rfact);
950 }
951
952 /* update 'drive-state' only in ENVSYS_DRIVE. */
953 if (edata->units == ENVSYS_DRIVE) {
954 for (j = 0; esds[j].type != -1; j++)
955 if (esds[j].type == edata->value_cur)
956 break;
957
958 SENSOR_UPSTRING(dict, "drive-state", esds[j].desc);
959 }
960 }
961
962 out:
963 return error;
964 }
965
966 /*
967 * sme_userset_dictionary:
968 *
969 * + Parses the userland dictionary and run the appropiate
970 * tasks that were requested.
971 */
972 int
973 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
974 prop_array_t array)
975 {
976 const struct sme_sensor_type *sst = sme_sensor_type;
977 envsys_data_t *edata, *nedata;
978 prop_dictionary_t dict;
979 prop_object_t obj, obj1, obj2;
980 int32_t critval;
981 int i, error = 0;
982 const char *blah, *sname;
983 bool targetfound = false;
984
985 blah = sname = NULL;
986
987 /* get sensor's name from userland dictionary. */
988 obj = prop_dictionary_get(udict, "sensor-name");
989 if (prop_object_type(obj) != PROP_TYPE_STRING) {
990 DPRINTF(("%s: sensor-name failed\n", __func__));
991 return EINVAL;
992 }
993
994 /* iterate over the sensors to find the right one */
995 for (i = 0; i < sme->sme_nsensors; i++) {
996 edata = &sme->sme_sensor_data[i];
997 dict = prop_array_get(array, i);
998 obj1 = prop_dictionary_get(dict, "description");
999
1000 /* is it our sensor? */
1001 if (!prop_string_equals(obj1, obj))
1002 continue;
1003
1004 /*
1005 * Check if a new description operation was
1006 * requested by the user and set new description.
1007 */
1008 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1009 targetfound = true;
1010 blah = prop_string_cstring_nocopy(obj2);
1011
1012 for (i = 0; i < sme->sme_nsensors; i++) {
1013 if (i == edata->sensor)
1014 continue;
1015
1016 nedata = &sme->sme_sensor_data[i];
1017 if (strcmp(blah, nedata->desc) == 0) {
1018 error = EEXIST;
1019 goto out;
1020 }
1021 }
1022
1023 SENSOR_UPSTRING(dict, "description", blah);
1024 (void)strlcpy(edata->desc, blah, sizeof(edata->desc));
1025
1026 break;
1027 }
1028
1029 /* did the user want to remove a critical capacity limit? */
1030 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1031 if (obj2 != NULL) {
1032 targetfound = true;
1033 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1034 (edata->flags & ENVSYS_FPERCENT) == 0) {
1035 error = ENOTSUP;
1036 break;
1037 }
1038
1039 sname = prop_string_cstring_nocopy(obj);
1040 error = sme_event_unregister(sname,
1041 PENVSYS_EVENT_BATT_USERCAP);
1042 if (error)
1043 break;
1044
1045 prop_dictionary_remove(dict, "critical-capacity");
1046 break;
1047 }
1048
1049 /* did the user want to remove a critical min limit? */
1050 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1051 if (obj2 != NULL) {
1052 targetfound = true;
1053 sname = prop_string_cstring_nocopy(obj);
1054 error = sme_event_unregister(sname,
1055 PENVSYS_EVENT_USER_CRITMIN);
1056 if (error)
1057 break;
1058
1059 prop_dictionary_remove(dict, "critical-min-limit");
1060 break;
1061 }
1062
1063 /* did the user want to remove a critical max limit? */
1064 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1065 if (obj2 != NULL) {
1066 targetfound = true;
1067 sname = prop_string_cstring_nocopy(obj);
1068 error = sme_event_unregister(sname,
1069 PENVSYS_EVENT_USER_CRITMAX);
1070 if (error)
1071 break;
1072
1073 prop_dictionary_remove(dict, "critical-max-limit");
1074 break;
1075 }
1076
1077 /* did the user want to change rfact? */
1078 obj2 = prop_dictionary_get(udict, "new-rfact");
1079 if (obj2 != NULL) {
1080 targetfound = true;
1081 if (edata->flags & ENVSYS_FCHANGERFACT)
1082 edata->rfact = prop_number_integer_value(obj2);
1083 else
1084 error = ENOTSUP;
1085
1086 break;
1087 }
1088
1089 for (i = 0; sst[i].type != -1; i++)
1090 if (sst[i].type == edata->units)
1091 break;
1092
1093 /* did the user want to set a critical capacity event? */
1094 obj2 = prop_dictionary_get(udict, "critical-capacity");
1095 if (obj2 != NULL) {
1096 targetfound = true;
1097 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1098 (edata->flags & ENVSYS_FPERCENT) == 0) {
1099 error = ENOTSUP;
1100 break;
1101 }
1102
1103 critval = prop_number_integer_value(obj2);
1104 error = sme_event_add(dict,
1105 edata,
1106 sme->sme_name,
1107 "critical-capacity",
1108 critval,
1109 PENVSYS_EVENT_BATT_USERCAP,
1110 sst[i].crittype);
1111 break;
1112 }
1113
1114 /* did the user want to set a critical max event? */
1115 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1116 if (obj2 != NULL) {
1117 targetfound = true;
1118 if (edata->units == ENVSYS_INDICATOR ||
1119 edata->flags & ENVSYS_FMONNOTSUPP) {
1120 error = ENOTSUP;
1121 break;
1122 }
1123
1124 critval = prop_number_integer_value(obj2);
1125 error = sme_event_add(dict,
1126 edata,
1127 sme->sme_name,
1128 "critical-max-limit",
1129 critval,
1130 PENVSYS_EVENT_USER_CRITMAX,
1131 sst[i].crittype);
1132 break;
1133 }
1134
1135 /* did the user want to set a critical min event? */
1136 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1137 if (obj2 != NULL) {
1138 targetfound = true;
1139 if (edata->units == ENVSYS_INDICATOR ||
1140 edata->flags & ENVSYS_FMONNOTSUPP) {
1141 error = ENOTSUP;
1142 break;
1143 }
1144
1145 critval = prop_number_integer_value(obj2);
1146 error = sme_event_add(dict,
1147 edata,
1148 sme->sme_name,
1149 "critical-min-limit",
1150 critval,
1151 PENVSYS_EVENT_USER_CRITMIN,
1152 sst[i].crittype);
1153 break;
1154 }
1155 }
1156
1157 /* invalid target? return the error */
1158 if (!targetfound)
1159 error = EINVAL;
1160
1161 out:
1162 return error;
1163 }
1164