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