sysmon_envsys.c revision 1.38 1 /* $NetBSD: sysmon_envsys.c,v 1.38 2007/07/21 16:16:59 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.38 2007/07/21 16:16:59 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 edata->flags & ENVSYS_FDUPDESC) {
375 tred->validflags &= ~ENVSYS_FCURVALID;
376 tred->cur.data_us = tred->cur.data_s = 0;
377 }
378
379 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
380 __func__, edata->desc, tred->cur.data_s));
381 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
382 " tred->sensor=%d\n", __func__, tred->validflags,
383 tred->units, tred->sensor));
384 }
385 tred->sensor = oidx;
386 mutex_exit(&sme_mtx);
387
388 break;
389 }
390 case ENVSYS_GTREINFO:
391 {
392 struct envsys_basic_info *binfo = (void *)data;
393 envsys_data_t *edata = NULL;
394
395 binfo->validflags = 0;
396
397 sme = sysmon_envsys_find_40(binfo->sensor);
398 if (sme == NULL)
399 break;
400
401 mutex_enter(&sme_mtx);
402 oidx = binfo->sensor;
403 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
404
405 edata = &sme->sme_sensor_data[binfo->sensor];
406
407 binfo->validflags |= ENVSYS_FVALID;
408
409 if (binfo->sensor < sme->sme_nsensors) {
410 binfo->units = edata->units;
411 (void)strlcpy(binfo->desc, edata->desc,
412 sizeof(binfo->desc));
413 }
414
415 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
416 __func__, binfo->units, binfo->validflags));
417 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
418 __func__, binfo->desc, binfo->sensor));
419
420 binfo->sensor = oidx;
421 mutex_exit(&sme_mtx);
422
423 break;
424 }
425 #endif /* COMPAT_40 */
426 default:
427 error = ENOTTY;
428 break;
429 }
430
431 return error;
432 }
433
434 /*
435 * sysmon_envsys_register:
436 *
437 * + Register an envsys device.
438 * + Create device dictionary.
439 */
440 int
441 sysmon_envsys_register(struct sysmon_envsys *sme)
442 {
443 struct sysmon_envsys *lsme;
444 int error = 0;
445
446 /*
447 * sanity check 1, make sure the driver has initialized
448 * the sensors count or the value is not too high.
449 */
450 if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
451 return EINVAL;
452
453 /*
454 * sanity check 2, make sure that sme->sme_name and
455 * sme->sme_data are not NULL.
456 */
457 if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
458 return EINVAL;
459
460 /*
461 * sanity check 3, if SME_DISABLE_GTREDATA is not set
462 * the sme_gtredata function callback must be non NULL.
463 */
464 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
465 if (sme->sme_gtredata == NULL)
466 return EINVAL;
467 }
468
469 /*
470 * - check if requested sysmon_envsys device is valid
471 * and does not exist already in the list.
472 * - add device into the list.
473 * - create the plist structure.
474 */
475 mutex_enter(&sme_list_mtx);
476 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
477 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
478 mutex_exit(&sme_list_mtx);
479 error = EEXIST;
480 goto out;
481 }
482 }
483
484 /* initialize the singly linked list for sensor descriptions */
485 SLIST_INIT(&sme_names_list);
486
487 #ifdef COMPAT_40
488 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
489 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
490 #endif
491 mutex_exit(&sme_list_mtx);
492 error = sysmon_envsys_createplist(sme);
493 if (!error) {
494 mutex_enter(&sme_list_mtx);
495 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
496 mutex_exit(&sme_list_mtx);
497 }
498
499 out:
500 return error;
501 }
502
503 /*
504 * sysmon_envsys_unregister:
505 *
506 * + Unregister an envsys device.
507 * + Unregister all events associated with this device.
508 * + Remove device dictionary.
509 */
510 void
511 sysmon_envsys_unregister(struct sysmon_envsys *sme)
512 {
513 struct sme_sensor_names *snames;
514
515 KASSERT(sme != NULL);
516
517 mutex_enter(&sme_list_mtx);
518 while (sme->sme_flags & SME_FLAG_BUSY) {
519 sme->sme_flags |= SME_FLAG_WANTED;
520 cv_wait(&sme_list_cv, &sme_list_mtx);
521 }
522 prop_dictionary_remove(sme_propd, sme->sme_name);
523 /*
524 * Unregister all events associated with this device.
525 */
526 mutex_exit(&sme_list_mtx);
527 sme_event_unregister_all(sme);
528 mutex_enter(&sme_list_mtx);
529
530 /*
531 * Remove all elements from the sensor names singly linked list.
532 */
533 while (!SLIST_EMPTY(&sme_names_list)) {
534 snames = SLIST_FIRST(&sme_names_list);
535 SLIST_REMOVE_HEAD(&sme_names_list, sme_names);
536 mutex_exit(&sme_list_mtx);
537 kmem_free(snames, sizeof(*snames));
538 mutex_enter(&sme_list_mtx);
539 }
540 LIST_REMOVE(sme, sme_list);
541 mutex_exit(&sme_list_mtx);
542 }
543
544 /*
545 * sysmon_envsys_find:
546 *
547 * + Find an envsys device.
548 */
549 struct sysmon_envsys *
550 sysmon_envsys_find(const char *name)
551 {
552 struct sysmon_envsys *sme;
553
554 mutex_enter(&sme_list_mtx);
555 again:
556 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
557 sme = LIST_NEXT(sme, sme_list)) {
558 if (strcmp(sme->sme_name, name) == 0) {
559 if (sme->sme_flags & SME_FLAG_BUSY) {
560 sme->sme_flags |= SME_FLAG_WANTED;
561 cv_wait(&sme_list_cv, &sme_list_mtx);
562 goto again;
563 }
564 sme->sme_flags |= SME_FLAG_BUSY;
565 break;
566 }
567 }
568 mutex_exit(&sme_list_mtx);
569 return sme;
570 }
571
572 /*
573 * sysmon_envsys_release:
574 *
575 * + Release an envsys device.
576 */
577 void
578 sysmon_envsys_release(struct sysmon_envsys *sme)
579 {
580 mutex_enter(&sme_list_mtx);
581 if (sme->sme_flags & SME_FLAG_WANTED)
582 cv_broadcast(&sme_list_cv);
583 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
584 mutex_exit(&sme_list_mtx);
585 }
586
587 /* compatibility function */
588 #ifdef COMPAT_40
589 struct sysmon_envsys *
590 sysmon_envsys_find_40(u_int idx)
591 {
592 struct sysmon_envsys *sme;
593
594 mutex_enter(&sme_list_mtx);
595 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
596 sme = LIST_NEXT(sme, sme_list)) {
597 if (idx >= sme->sme_fsensor &&
598 idx < (sme->sme_fsensor + sme->sme_nsensors))
599 break;
600 }
601 mutex_exit(&sme_list_mtx);
602 return sme;
603 }
604 #endif
605
606 /*
607 * sysmon_envsys_createplist:
608 *
609 * + Create the property list structure for a device.
610 */
611 int
612 sysmon_envsys_createplist(struct sysmon_envsys *sme)
613 {
614 envsys_data_t *edata;
615 prop_array_t array;
616 int i, error = 0;
617
618 /* create the sysmon envsys device array. */
619 array = prop_array_create();
620 if (array == NULL)
621 return EINVAL;
622
623 /*
624 * <dict>
625 * <key>foo0</key>
626 * <array>
627 * ...
628 */
629 mutex_enter(&sme_mtx);
630 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
631 DPRINTF(("%s: prop_dictionary_set\n", __func__));
632 mutex_exit(&sme_mtx);
633 error = EINVAL;
634 goto out;
635 }
636 mutex_exit(&sme_mtx);
637
638 /*
639 * Iterate over all sensors and create a dictionary with all
640 * values specified by the sysmon envsys driver.
641 */
642 for (i = 0; i < sme->sme_nsensors; i++) {
643 edata = &sme->sme_sensor_data[i];
644 /*
645 * refresh sensor data via sme_gtredata only if the
646 * flag is not set.
647 */
648 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
649 mutex_enter(&sme_mtx);
650 error = (*sme->sme_gtredata)(sme, edata);
651 if (error) {
652 DPRINTF(("%s: sme->sme_gtredata[%d]\n",
653 __func__, i));
654 mutex_exit(&sme_mtx);
655 continue;
656 }
657 mutex_exit(&sme_mtx);
658 }
659
660 error = sme_make_dictionary(sme, array, edata);
661 if (error == EINVAL)
662 goto out;
663 else if (error == EEXIST) {
664 error = 0;
665 continue;
666 }
667 }
668
669 out:
670 prop_object_release(array);
671 return error;
672 }
673
674 /*
675 * sme_register_sensorname:
676 *
677 * + Registers a sensor name into the list maintained per device.
678 */
679 static int
680 sme_register_sensorname(envsys_data_t *edata)
681 {
682 struct sme_sensor_names *snames, *snames2 = NULL;
683
684 KASSERT(edata != NULL);
685
686 snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
687
688 mutex_enter(&sme_list_mtx);
689 SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
690 if (strcmp(snames2->desc, edata->desc) == 0)
691 if (snames2->assigned) {
692 edata->flags |= ENVSYS_FDUPDESC;
693 mutex_exit(&sme_list_mtx);
694 DPRINTF(("%s: duplicate name "
695 "(%s)\n", __func__, edata->desc));
696 kmem_free(snames, sizeof(*snames));
697 return EEXIST;
698 }
699 }
700
701 snames->assigned = true;
702 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
703 DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
704 SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
705 mutex_exit(&sme_list_mtx);
706
707 return 0;
708 }
709
710 /*
711 * sme_make_dictionary:
712 *
713 * + Create sensor's dictionary in device's dictionary array.
714 */
715 int
716 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
717 envsys_data_t *edata)
718 {
719 const struct sme_sensor_type *est = sme_sensor_type;
720 const struct sme_sensor_state *ess = sme_sensor_state;
721 const struct sme_sensor_state *esds = sme_sensor_drive_state;
722 sme_event_drv_t *sme_evdrv_t = NULL;
723 prop_dictionary_t dict;
724 int i, j, k, error;
725
726 i = j = k = error = 0;
727
728 /*
729 * Check if description was already assigned in other sensor.
730 */
731 error = sme_register_sensorname(edata);
732 if (error)
733 return error;
734
735 /*
736 * <array>
737 * <dict>
738 * ...
739 */
740 dict = prop_dictionary_create();
741 if (dict == NULL) {
742 DPRINTF(("%s: couldn't allocate a dictionary\n", __func__));
743 return EINVAL;
744 }
745
746 mutex_enter(&sme_mtx);
747 if (!prop_array_add(array, dict)) {
748 mutex_exit(&sme_mtx);
749 prop_object_release(dict);
750 DPRINTF(("%s: prop_array_add\n", __func__));
751 return EINVAL;
752 }
753
754 /* find the correct unit for this sensor. */
755 for (i = 0; est[i].type != -1; i++)
756 if (est[i].type == edata->units)
757 break;
758
759 /*
760 * ...
761 * <key>type</key>
762 * <string>foo</string>
763 * <key>description</key>
764 * <string>blah blah</string>
765 * ...
766 */
767 error = sme_sensor_upstring(dict, "type", est[i].desc);
768 if (error)
769 goto out;
770
771 error = sme_sensor_upstring(dict, "description", edata->desc);
772 if (error)
773 goto out;
774
775
776 /*
777 * Add sensor's state description.
778 *
779 * ...
780 * <key>state</key>
781 * <string>valid</string>
782 * ...
783 */
784 for (j = 0; ess[j].type != -1; j++)
785 if (ess[j].type == edata->state)
786 break;
787
788 error = sme_sensor_upstring(dict, "state", ess[j].desc);
789 if (error)
790 goto out;
791
792 /*
793 * add the percentage boolean object:
794 *
795 * ...
796 * <key>want-percentage</key>
797 * <true/>
798 * ...
799 */
800 if (edata->flags & ENVSYS_FPERCENT) {
801 error = sme_sensor_upbool(dict, "want-percentage", true);
802 if (error)
803 goto out;
804 }
805
806 /*
807 * Add the monitoring boolean object:
808 *
809 * ...
810 * <key>monitoring-supported</key>
811 * <true/>
812 * ...
813 *
814 * always false on Drive and Indicator types, they
815 * cannot be monitored.
816 *
817 */
818 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
819 (edata->units == ENVSYS_INDICATOR) ||
820 (edata->units == ENVSYS_DRIVE)) {
821 error = sme_sensor_upbool(dict, "monitoring-supported", false);
822 if (error)
823 goto out;
824 } else {
825 error = sme_sensor_upbool(dict, "monitoring-supported", true);
826 if (error)
827 goto out;
828 }
829
830 /*
831 * Add the drive-state object for drive sensors:
832 *
833 * ...
834 * <key>drive-state</key>
835 * <string>drive is online</string>
836 * ...
837 */
838 if (edata->units == ENVSYS_DRIVE) {
839 for (k = 0; esds[k].type != -1; k++)
840 if (esds[k].type == edata->value_cur)
841 break;
842
843 error = sme_sensor_upstring(dict, "drive-state", esds[k].desc);
844 if (error)
845 goto out;
846 }
847
848 mutex_exit(&sme_mtx);
849 /*
850 * Add a new event if a monitoring flag was set.
851 */
852 if (edata->monitor) {
853 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
854
855 sme_evdrv_t->sdict = dict;
856 sme_evdrv_t->edata = edata;
857 sme_evdrv_t->sme = sme;
858 sme_evdrv_t->powertype = est[i].crittype;
859
860 sysmon_task_queue_init();
861 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
862 }
863
864 mutex_enter(&sme_mtx);
865 /* if sensor is enabled, add the following properties... */
866 if (edata->state == ENVSYS_SVALID) {
867 /*
868 * ...
869 * <key>rpms</key>
870 * <integer>2500</integer>
871 * <key>rfact</key>
872 * <integer>10000</integer>
873 * <key>cur-value</key>
874 * <integer>1250</integer>
875 * <key>min-value</key>
876 * <integer>800</integer>
877 * <key>max-value</integer>
878 * <integer>3000</integer>
879 * <key>avg-value</integer>
880 * <integer>1400</integer>
881 * </dict>
882 */
883 if ((edata->units == ENVSYS_SFANRPM) && edata->rpms) {
884 error = sme_sensor_upuint32(dict, "rpms", edata->rpms);
885 if (error)
886 goto out;
887 }
888
889 if ((edata->units == ENVSYS_SVOLTS_AC ||
890 edata->units == ENVSYS_SVOLTS_DC) && edata->rfact) {
891 error = sme_sensor_upint32(dict, "rfact", edata->rfact);
892 if (error)
893 goto out;
894 }
895
896 if (edata->value_cur) {
897 error= sme_sensor_upint32(dict,
898 "cur-value",
899 edata->value_cur);
900 if (error)
901 goto out;
902 }
903
904 if ((edata->flags & ENVSYS_FVALID_MIN) && edata->value_min) {
905 error = sme_sensor_upint32(dict,
906 "min-value",
907 edata->value_min);
908 if (error)
909 goto out;
910 }
911
912 if ((edata->flags & ENVSYS_FVALID_MAX) && edata->value_max) {
913 error = sme_sensor_upint32(dict,
914 "max-value",
915 edata->value_max);
916 if (error)
917 goto out;
918 }
919
920 if ((edata->flags & ENVSYS_FVALID_AVG) && edata->value_avg) {
921 error = sme_sensor_upint32(dict,
922 "avg-value",
923 edata->value_avg);
924 if (error)
925 goto out;
926 }
927 }
928
929 /*
930 * ...
931 * </array>
932 */
933 out:
934 mutex_exit(&sme_mtx);
935 prop_object_release(dict);
936 return error;
937 }
938
939 /*
940 * sme_update_dictionary:
941 *
942 * + Update per-sensor dictionaries with new values if there were
943 * changes, otherwise the object in dictionary is untouched.
944 * + Send a critical event if any sensor is in a critical condition.
945 */
946 int
947 sme_update_dictionary(struct sysmon_envsys *sme)
948 {
949 const struct sme_sensor_state *ess = sme_sensor_state;
950 const struct sme_sensor_state *esds = sme_sensor_drive_state;
951 envsys_data_t *edata = NULL;
952 prop_object_t array, dict;
953 int i, j, error = 0;
954
955 array = dict = NULL;
956
957 /* retrieve the array of dictionaries in device. */
958 array = prop_dictionary_get(sme_propd, sme->sme_name);
959 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
960 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
961 return EINVAL;
962 }
963
964 /*
965 * - iterate over all sensors.
966 * - fetch new data.
967 * - check if data in dictionary is different than new data.
968 * - update dictionary if there were changes.
969 */
970 for (i = 0; i < sme->sme_nsensors; i++) {
971 edata = &sme->sme_sensor_data[i];
972 if (edata->flags & ENVSYS_FDUPDESC) {
973 --sme->sme_nsensors;
974 continue;
975 }
976
977 /*
978 * refresh sensor data via sme_gtredata only if the
979 * flag is not set.
980 */
981 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
982 error = (*sme->sme_gtredata)(sme, edata);
983 if (error) {
984 DPRINTF(("%s: gtredata[%d] failed\n",
985 __func__, i));
986 return error;
987 }
988 }
989
990 /* retrieve sensor's dictionary. */
991 dict = prop_array_get(array, i);
992 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
993 DPRINTF(("%s: not a dictionary (%d:%s)\n",
994 __func__, edata->sensor, sme->sme_name));
995 return EINVAL;
996 }
997
998 /* update state sensor. */
999 for (j = 0; ess[j].type != -1; j++)
1000 if (ess[j].type == edata->state)
1001 break;
1002
1003 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1004 "units=%d sensor=%d\n", __func__, ess[j].desc,
1005 ess[j].type, edata->flags, edata->units, edata->sensor));
1006
1007 /* update sensor state */
1008 error = sme_sensor_upstring(dict, "state", ess[j].desc);
1009 if (error)
1010 break;
1011
1012 /* update sensor current value */
1013 error = sme_sensor_upint32(dict,
1014 "cur-value",
1015 edata->value_cur);
1016 if (error)
1017 break;
1018
1019 /*
1020 * Integer and Indicator types do not the following
1021 * values, so skip them.
1022 */
1023 if (edata->units == ENVSYS_INTEGER ||
1024 edata->units == ENVSYS_INDICATOR)
1025 continue;
1026
1027 /* update sensor flags */
1028 if (edata->flags & ENVSYS_FPERCENT) {
1029 error = sme_sensor_upbool(dict,
1030 "want-percentage",
1031 true);
1032 if (error)
1033 break;
1034 }
1035
1036 if (edata->flags & ENVSYS_FVALID_MAX) {
1037 error = sme_sensor_upint32(dict,
1038 "max-value",
1039 edata->value_max);
1040 if (error)
1041 break;
1042 }
1043
1044 if (edata->flags & ENVSYS_FVALID_MIN) {
1045 error = sme_sensor_upint32(dict,
1046 "min-value",
1047 edata->value_min);
1048 if (error)
1049 break;
1050 }
1051
1052 if (edata->flags & ENVSYS_FVALID_AVG) {
1053 error = sme_sensor_upint32(dict,
1054 "avg-value",
1055 edata->value_avg);
1056 if (error)
1057 break;
1058 }
1059
1060 /* update 'rpms' only in ENVSYS_SFANRPM. */
1061 if (edata->units == ENVSYS_SFANRPM) {
1062 error = sme_sensor_upuint32(dict,
1063 "rpms",
1064 edata->rpms);
1065 if (error)
1066 break;
1067 }
1068
1069 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1070 if (edata->units == ENVSYS_SVOLTS_AC ||
1071 edata->units == ENVSYS_SVOLTS_DC) {
1072 error = sme_sensor_upint32(dict,
1073 "rfact",
1074 edata->rfact);
1075 if (error)
1076 break;
1077 }
1078
1079 /* update 'drive-state' only in ENVSYS_DRIVE. */
1080 if (edata->units == ENVSYS_DRIVE) {
1081 for (j = 0; esds[j].type != -1; j++)
1082 if (esds[j].type == edata->value_cur)
1083 break;
1084
1085 error = sme_sensor_upstring(dict,
1086 "drive-state",
1087 esds[j].desc);
1088 if (error)
1089 break;
1090 }
1091 }
1092
1093 return error;
1094 }
1095
1096 /*
1097 * sme_userset_dictionary:
1098 *
1099 * + Parses the userland dictionary and run the appropiate
1100 * tasks that were requested.
1101 */
1102 int
1103 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1104 prop_array_t array)
1105 {
1106 const struct sme_sensor_type *sst = sme_sensor_type;
1107 envsys_data_t *edata, *nedata;
1108 prop_dictionary_t dict;
1109 prop_object_t obj, obj1, obj2;
1110 int32_t critval;
1111 int i, error = 0;
1112 const char *blah, *sname;
1113 bool targetfound = false;
1114
1115 blah = sname = NULL;
1116
1117 /* get sensor's name from userland dictionary. */
1118 obj = prop_dictionary_get(udict, "sensor-name");
1119 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1120 DPRINTF(("%s: sensor-name failed\n", __func__));
1121 return EINVAL;
1122 }
1123
1124 /* iterate over the sensors to find the right one */
1125 for (i = 0; i < sme->sme_nsensors; i++) {
1126 edata = &sme->sme_sensor_data[i];
1127
1128 /* duplicate description? skip it */
1129 if (edata->flags & ENVSYS_FDUPDESC)
1130 continue;
1131
1132 dict = prop_array_get(array, i);
1133 obj1 = prop_dictionary_get(dict, "description");
1134
1135 /* is it our sensor? */
1136 if (!prop_string_equals(obj1, obj))
1137 continue;
1138
1139 /*
1140 * Check if a new description operation was
1141 * requested by the user and set new description.
1142 */
1143 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1144 targetfound = true;
1145 blah = prop_string_cstring_nocopy(obj2);
1146
1147 for (i = 0; i < sme->sme_nsensors; i++) {
1148 if (i == edata->sensor)
1149 continue;
1150
1151 nedata = &sme->sme_sensor_data[i];
1152 if (strcmp(blah, nedata->desc) == 0) {
1153 error = EEXIST;
1154 break;
1155 }
1156 }
1157
1158 if (error)
1159 break;
1160
1161 error = sme_sensor_upstring(dict,
1162 "description",
1163 blah);
1164 if (!error)
1165 (void)strlcpy(edata->desc,
1166 blah,
1167 sizeof(edata->desc));
1168
1169 break;
1170 }
1171
1172 /* did the user want to remove a critical capacity limit? */
1173 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1174 if (obj2 != NULL) {
1175 targetfound = true;
1176 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1177 (edata->flags & ENVSYS_FPERCENT) == 0) {
1178 error = ENOTSUP;
1179 break;
1180 }
1181
1182 sname = prop_string_cstring_nocopy(obj);
1183 error = sme_event_unregister(sname,
1184 PENVSYS_EVENT_BATT_USERCAP);
1185 if (error)
1186 break;
1187
1188 prop_dictionary_remove(dict, "critical-capacity");
1189 break;
1190 }
1191
1192 /* did the user want to remove a critical min limit? */
1193 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1194 if (obj2 != NULL) {
1195 targetfound = true;
1196 sname = prop_string_cstring_nocopy(obj);
1197 error = sme_event_unregister(sname,
1198 PENVSYS_EVENT_USER_CRITMIN);
1199 if (error)
1200 break;
1201
1202 prop_dictionary_remove(dict, "critical-min-limit");
1203 break;
1204 }
1205
1206 /* did the user want to remove a critical max limit? */
1207 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1208 if (obj2 != NULL) {
1209 targetfound = true;
1210 sname = prop_string_cstring_nocopy(obj);
1211 error = sme_event_unregister(sname,
1212 PENVSYS_EVENT_USER_CRITMAX);
1213 if (error)
1214 break;
1215
1216 prop_dictionary_remove(dict, "critical-max-limit");
1217 break;
1218 }
1219
1220 /* did the user want to change rfact? */
1221 obj2 = prop_dictionary_get(udict, "new-rfact");
1222 if (obj2 != NULL) {
1223 targetfound = true;
1224 if (edata->flags & ENVSYS_FCHANGERFACT)
1225 edata->rfact = prop_number_integer_value(obj2);
1226 else
1227 error = ENOTSUP;
1228
1229 break;
1230 }
1231
1232 for (i = 0; sst[i].type != -1; i++)
1233 if (sst[i].type == edata->units)
1234 break;
1235
1236 /* did the user want to set a critical capacity event? */
1237 obj2 = prop_dictionary_get(udict, "critical-capacity");
1238 if (obj2 != NULL) {
1239 targetfound = true;
1240 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1241 (edata->flags & ENVSYS_FPERCENT) == 0) {
1242 error = ENOTSUP;
1243 break;
1244 }
1245
1246 critval = prop_number_integer_value(obj2);
1247 error = sme_event_add(dict,
1248 edata,
1249 sme->sme_name,
1250 "critical-capacity",
1251 critval,
1252 PENVSYS_EVENT_BATT_USERCAP,
1253 sst[i].crittype);
1254 break;
1255 }
1256
1257 /* did the user want to set a critical max event? */
1258 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1259 if (obj2 != NULL) {
1260 targetfound = true;
1261 if (edata->units == ENVSYS_INDICATOR ||
1262 edata->flags & ENVSYS_FMONNOTSUPP) {
1263 error = ENOTSUP;
1264 break;
1265 }
1266
1267 critval = prop_number_integer_value(obj2);
1268 error = sme_event_add(dict,
1269 edata,
1270 sme->sme_name,
1271 "critical-max-limit",
1272 critval,
1273 PENVSYS_EVENT_USER_CRITMAX,
1274 sst[i].crittype);
1275 break;
1276 }
1277
1278 /* did the user want to set a critical min event? */
1279 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1280 if (obj2 != NULL) {
1281 targetfound = true;
1282 if (edata->units == ENVSYS_INDICATOR ||
1283 edata->flags & ENVSYS_FMONNOTSUPP) {
1284 error = ENOTSUP;
1285 break;
1286 }
1287
1288 critval = prop_number_integer_value(obj2);
1289 error = sme_event_add(dict,
1290 edata,
1291 sme->sme_name,
1292 "critical-min-limit",
1293 critval,
1294 PENVSYS_EVENT_USER_CRITMIN,
1295 sst[i].crittype);
1296 break;
1297 }
1298 }
1299
1300 /* invalid target? return the error */
1301 if (!targetfound)
1302 error = EINVAL;
1303
1304 return error;
1305 }
1306