sysmon_envsys.c revision 1.37 1 /* $NetBSD: sysmon_envsys.c,v 1.37 2007/07/21 15:45:12 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.37 2007/07/21 15:45:12 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 --i;
975 continue;
976 }
977
978 /*
979 * refresh sensor data via sme_gtredata only if the
980 * flag is not set.
981 */
982 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
983 error = (*sme->sme_gtredata)(sme, edata);
984 if (error) {
985 DPRINTF(("%s: gtredata[%d] failed\n",
986 __func__, i));
987 return error;
988 }
989 }
990
991 /* retrieve sensor's dictionary. */
992 dict = prop_array_get(array, i);
993 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
994 DPRINTF(("%s: not a dictionary (%d:%s)\n",
995 __func__, edata->sensor, sme->sme_name));
996 return EINVAL;
997 }
998
999 /* update state sensor. */
1000 for (j = 0; ess[j].type != -1; j++)
1001 if (ess[j].type == edata->state)
1002 break;
1003
1004 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1005 "units=%d sensor=%d\n", __func__, ess[j].desc,
1006 ess[j].type, edata->flags, edata->units, edata->sensor));
1007
1008 /* update sensor state */
1009 error = sme_sensor_upstring(dict, "state", ess[j].desc);
1010 if (error)
1011 break;
1012
1013 /* update sensor current value */
1014 error = sme_sensor_upint32(dict,
1015 "cur-value",
1016 edata->value_cur);
1017 if (error)
1018 break;
1019
1020 /*
1021 * Integer and Indicator types do not the following
1022 * values, so skip them.
1023 */
1024 if (edata->units == ENVSYS_INTEGER ||
1025 edata->units == ENVSYS_INDICATOR)
1026 continue;
1027
1028 /* update sensor flags */
1029 if (edata->flags & ENVSYS_FPERCENT) {
1030 error = sme_sensor_upbool(dict,
1031 "want-percentage",
1032 true);
1033 if (error)
1034 break;
1035 }
1036
1037 if (edata->flags & ENVSYS_FVALID_MAX) {
1038 error = sme_sensor_upint32(dict,
1039 "max-value",
1040 edata->value_max);
1041 if (error)
1042 break;
1043 }
1044
1045 if (edata->flags & ENVSYS_FVALID_MIN) {
1046 error = sme_sensor_upint32(dict,
1047 "min-value",
1048 edata->value_min);
1049 if (error)
1050 break;
1051 }
1052
1053 if (edata->flags & ENVSYS_FVALID_AVG) {
1054 error = sme_sensor_upint32(dict,
1055 "avg-value",
1056 edata->value_avg);
1057 if (error)
1058 break;
1059 }
1060
1061 /* update 'rpms' only in ENVSYS_SFANRPM. */
1062 if (edata->units == ENVSYS_SFANRPM) {
1063 error = sme_sensor_upuint32(dict,
1064 "rpms",
1065 edata->rpms);
1066 if (error)
1067 break;
1068 }
1069
1070 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1071 if (edata->units == ENVSYS_SVOLTS_AC ||
1072 edata->units == ENVSYS_SVOLTS_DC) {
1073 error = sme_sensor_upint32(dict,
1074 "rfact",
1075 edata->rfact);
1076 if (error)
1077 break;
1078 }
1079
1080 /* update 'drive-state' only in ENVSYS_DRIVE. */
1081 if (edata->units == ENVSYS_DRIVE) {
1082 for (j = 0; esds[j].type != -1; j++)
1083 if (esds[j].type == edata->value_cur)
1084 break;
1085
1086 error = sme_sensor_upstring(dict,
1087 "drive-state",
1088 esds[j].desc);
1089 if (error)
1090 break;
1091 }
1092 }
1093
1094 return error;
1095 }
1096
1097 /*
1098 * sme_userset_dictionary:
1099 *
1100 * + Parses the userland dictionary and run the appropiate
1101 * tasks that were requested.
1102 */
1103 int
1104 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1105 prop_array_t array)
1106 {
1107 const struct sme_sensor_type *sst = sme_sensor_type;
1108 envsys_data_t *edata, *nedata;
1109 prop_dictionary_t dict;
1110 prop_object_t obj, obj1, obj2;
1111 int32_t critval;
1112 int i, error = 0;
1113 const char *blah, *sname;
1114 bool targetfound = false;
1115
1116 blah = sname = NULL;
1117
1118 /* get sensor's name from userland dictionary. */
1119 obj = prop_dictionary_get(udict, "sensor-name");
1120 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1121 DPRINTF(("%s: sensor-name failed\n", __func__));
1122 return EINVAL;
1123 }
1124
1125 /* iterate over the sensors to find the right one */
1126 for (i = 0; i < sme->sme_nsensors; i++) {
1127 edata = &sme->sme_sensor_data[i];
1128
1129 /* duplicate description? skip it */
1130 if (edata->flags & ENVSYS_FDUPDESC)
1131 continue;
1132
1133 dict = prop_array_get(array, i);
1134 obj1 = prop_dictionary_get(dict, "description");
1135
1136 /* is it our sensor? */
1137 if (!prop_string_equals(obj1, obj))
1138 continue;
1139
1140 /*
1141 * Check if a new description operation was
1142 * requested by the user and set new description.
1143 */
1144 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1145 targetfound = true;
1146 blah = prop_string_cstring_nocopy(obj2);
1147
1148 for (i = 0; i < sme->sme_nsensors; i++) {
1149 if (i == edata->sensor)
1150 continue;
1151
1152 nedata = &sme->sme_sensor_data[i];
1153 if (strcmp(blah, nedata->desc) == 0) {
1154 error = EEXIST;
1155 break;
1156 }
1157 }
1158
1159 if (error)
1160 break;
1161
1162 error = sme_sensor_upstring(dict,
1163 "description",
1164 blah);
1165 if (!error)
1166 (void)strlcpy(edata->desc,
1167 blah,
1168 sizeof(edata->desc));
1169
1170 break;
1171 }
1172
1173 /* did the user want to remove a critical capacity limit? */
1174 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1175 if (obj2 != NULL) {
1176 targetfound = true;
1177 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1178 (edata->flags & ENVSYS_FPERCENT) == 0) {
1179 error = ENOTSUP;
1180 break;
1181 }
1182
1183 sname = prop_string_cstring_nocopy(obj);
1184 error = sme_event_unregister(sname,
1185 PENVSYS_EVENT_BATT_USERCAP);
1186 if (error)
1187 break;
1188
1189 prop_dictionary_remove(dict, "critical-capacity");
1190 break;
1191 }
1192
1193 /* did the user want to remove a critical min limit? */
1194 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1195 if (obj2 != NULL) {
1196 targetfound = true;
1197 sname = prop_string_cstring_nocopy(obj);
1198 error = sme_event_unregister(sname,
1199 PENVSYS_EVENT_USER_CRITMIN);
1200 if (error)
1201 break;
1202
1203 prop_dictionary_remove(dict, "critical-min-limit");
1204 break;
1205 }
1206
1207 /* did the user want to remove a critical max limit? */
1208 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1209 if (obj2 != NULL) {
1210 targetfound = true;
1211 sname = prop_string_cstring_nocopy(obj);
1212 error = sme_event_unregister(sname,
1213 PENVSYS_EVENT_USER_CRITMAX);
1214 if (error)
1215 break;
1216
1217 prop_dictionary_remove(dict, "critical-max-limit");
1218 break;
1219 }
1220
1221 /* did the user want to change rfact? */
1222 obj2 = prop_dictionary_get(udict, "new-rfact");
1223 if (obj2 != NULL) {
1224 targetfound = true;
1225 if (edata->flags & ENVSYS_FCHANGERFACT)
1226 edata->rfact = prop_number_integer_value(obj2);
1227 else
1228 error = ENOTSUP;
1229
1230 break;
1231 }
1232
1233 for (i = 0; sst[i].type != -1; i++)
1234 if (sst[i].type == edata->units)
1235 break;
1236
1237 /* did the user want to set a critical capacity event? */
1238 obj2 = prop_dictionary_get(udict, "critical-capacity");
1239 if (obj2 != NULL) {
1240 targetfound = true;
1241 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1242 (edata->flags & ENVSYS_FPERCENT) == 0) {
1243 error = ENOTSUP;
1244 break;
1245 }
1246
1247 critval = prop_number_integer_value(obj2);
1248 error = sme_event_add(dict,
1249 edata,
1250 sme->sme_name,
1251 "critical-capacity",
1252 critval,
1253 PENVSYS_EVENT_BATT_USERCAP,
1254 sst[i].crittype);
1255 break;
1256 }
1257
1258 /* did the user want to set a critical max event? */
1259 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1260 if (obj2 != NULL) {
1261 targetfound = true;
1262 if (edata->units == ENVSYS_INDICATOR ||
1263 edata->flags & ENVSYS_FMONNOTSUPP) {
1264 error = ENOTSUP;
1265 break;
1266 }
1267
1268 critval = prop_number_integer_value(obj2);
1269 error = sme_event_add(dict,
1270 edata,
1271 sme->sme_name,
1272 "critical-max-limit",
1273 critval,
1274 PENVSYS_EVENT_USER_CRITMAX,
1275 sst[i].crittype);
1276 break;
1277 }
1278
1279 /* did the user want to set a critical min event? */
1280 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1281 if (obj2 != NULL) {
1282 targetfound = true;
1283 if (edata->units == ENVSYS_INDICATOR ||
1284 edata->flags & ENVSYS_FMONNOTSUPP) {
1285 error = ENOTSUP;
1286 break;
1287 }
1288
1289 critval = prop_number_integer_value(obj2);
1290 error = sme_event_add(dict,
1291 edata,
1292 sme->sme_name,
1293 "critical-min-limit",
1294 critval,
1295 PENVSYS_EVENT_USER_CRITMIN,
1296 sst[i].crittype);
1297 break;
1298 }
1299 }
1300
1301 /* invalid target? return the error */
1302 if (!targetfound)
1303 error = EINVAL;
1304
1305 return error;
1306 }
1307