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