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