sysmon_envsys.c revision 1.44 1 /* $NetBSD: sysmon_envsys.c,v 1.44 2007/07/27 11:59:09 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.44 2007/07/27 11:59:09 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 mutex_enter(&sme_mtx);
640 /*
641 * XXX:
642 *
643 * workaround for LKMs. First sensor used in a LKM,
644 * gets the index in sensor from all edata structures
645 * allocated in kernel. It is unknown to me why this
646 * value is not 0 when using a LKM.
647 *
648 * For now, overwrite its index to 0.
649 */
650 if (i == 0)
651 sme->sme_sensor_data[0].sensor = 0;
652
653 edata = &sme->sme_sensor_data[i];
654 /*
655 * refresh sensor data via sme_gtredata only if the
656 * flag is not set.
657 */
658 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
659 if ((*sme->sme_gtredata)(sme, edata)) {
660 DPRINTF(("%s: sme->sme_gtredata[%d]\n",
661 __func__, i));
662 mutex_exit(&sme_mtx);
663 continue;
664 }
665 }
666 mutex_exit(&sme_mtx);
667 sme_make_dictionary(sme, array, edata);
668 }
669
670 if (prop_array_count(array) == 0) {
671 error = EINVAL;
672 prop_object_release(array);
673 return error;
674 }
675
676 /*
677 * <dict>
678 * <key>foo0</key>
679 * <array>
680 * ...
681 */
682 mutex_enter(&sme_mtx);
683 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
684 DPRINTF(("%s: prop_dictionary_set\n", __func__));
685 error = EINVAL;
686 }
687 mutex_exit(&sme_mtx);
688
689
690 prop_object_release(array);
691 mutex_enter(&sme_list_mtx);
692 return error;
693 }
694
695 /*
696 * sme_register_sensorname:
697 *
698 * + Registers a sensor name into the list maintained per device.
699 */
700 static int
701 sme_register_sensorname(envsys_data_t *edata)
702 {
703 struct sme_sensor_names *snames, *snames2 = NULL;
704
705 KASSERT(edata != NULL);
706
707 snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
708
709 mutex_enter(&sme_list_mtx);
710 SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
711 /*
712 * Match sensors with empty and duplicate description.
713 */
714 if (strlen(edata->desc) == 0 ||
715 strcmp(snames2->desc, edata->desc) == 0)
716 if (snames2->assigned) {
717 edata->flags |= ENVSYS_FNOTVALID;
718 mutex_exit(&sme_list_mtx);
719 DPRINTF(("%s: duplicate name "
720 "(%s)\n", __func__, edata->desc));
721 kmem_free(snames, sizeof(*snames));
722 return EEXIST;
723 }
724 }
725
726 snames->assigned = true;
727 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
728 DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
729 SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
730 sme_uniqsensors++;
731 mutex_exit(&sme_list_mtx);
732
733 return 0;
734 }
735
736 /*
737 * sme_make_dictionary:
738 *
739 * + Create sensor's dictionary in device's array.
740 */
741 void
742 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
743 envsys_data_t *edata)
744 {
745 const struct sme_sensor_type *est = sme_sensor_type;
746 const struct sme_sensor_state *ess = sme_sensor_state;
747 const struct sme_sensor_state *esds = sme_sensor_drive_state;
748 sme_event_drv_t *sme_evdrv_t = NULL;
749 prop_dictionary_t dict;
750 int i, j;
751
752 i = j = 0;
753
754
755 /*
756 * <array>
757 * <dict>
758 * ...
759 */
760 dict = prop_dictionary_create();
761 if (dict == NULL) {
762 DPRINTF(("%s: couldn't allocate a dictionary\n", __func__));
763 goto invalidate_sensor;
764 }
765
766 /* find the correct unit for this sensor. */
767 for (i = 0; est[i].type != -1; i++)
768 if (est[i].type == edata->units)
769 break;
770
771 if (strcmp(est[i].desc, "unknown") == 0) {
772 DPRINTF(("%s: invalid units type for sensor=%d\n",
773 __func__, edata->sensor));
774 goto invalidate_sensor;
775 }
776
777 /*
778 * ...
779 * <key>type</key>
780 * <string>foo</string>
781 * <key>description</key>
782 * <string>blah blah</string>
783 * ...
784 */
785 if (sme_sensor_upstring(dict, "type", est[i].desc))
786 goto invalidate_sensor;
787
788 if (strlen(edata->desc) == 0) {
789 DPRINTF(("%s: invalid description for sensor=%d\n",
790 __func__, edata->sensor));
791 goto invalidate_sensor;
792 }
793
794 /*
795 * Check if description was already assigned in other sensor.
796 */
797 if (sme_register_sensorname(edata))
798 goto out;
799
800 if (sme_sensor_upstring(dict, "description", edata->desc))
801 goto invalidate_sensor;
802
803 /*
804 * Add sensor's state description.
805 *
806 * ...
807 * <key>state</key>
808 * <string>valid</string>
809 * ...
810 */
811 for (j = 0; ess[j].type != -1; j++)
812 if (ess[j].type == edata->state)
813 break;
814
815 if (strcmp(ess[j].desc, "unknown") == 0) {
816 DPRINTF(("%s: invalid state for sensor=%d\n",
817 __func__, edata->sensor));
818 goto invalidate_sensor;
819 }
820
821 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
822 __func__, edata->desc, edata->units, edata->state));
823
824 if (sme_sensor_upstring(dict, "state", ess[j].desc))
825 goto invalidate_sensor;
826
827 /*
828 * add the percentage boolean object:
829 *
830 * ...
831 * <key>want-percentage</key>
832 * <true/>
833 * ...
834 */
835 if (edata->flags & ENVSYS_FPERCENT)
836 if (sme_sensor_upbool(dict, "want-percentage", true))
837 goto out;
838
839 /*
840 * Add the monitoring boolean object:
841 *
842 * ...
843 * <key>monitoring-supported</key>
844 * <true/>
845 * ...
846 *
847 * always false on Drive and Indicator types, they
848 * cannot be monitored.
849 *
850 */
851 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
852 (edata->units == ENVSYS_INDICATOR) ||
853 (edata->units == ENVSYS_DRIVE)) {
854 if (sme_sensor_upbool(dict, "monitoring-supported", false))
855 goto out;
856 } else {
857 if (sme_sensor_upbool(dict, "monitoring-supported", true))
858 goto out;
859 }
860
861 /*
862 * Add the drive-state object for drive sensors:
863 *
864 * ...
865 * <key>drive-state</key>
866 * <string>drive is online</string>
867 * ...
868 */
869 if (edata->units == ENVSYS_DRIVE) {
870 for (j = 0; esds[j].type != -1; j++)
871 if (esds[j].type == edata->value_cur)
872 break;
873
874 if (sme_sensor_upstring(dict, "drive-state", esds[j].desc))
875 goto out;
876 }
877
878 /* if sensor is enabled, add the following properties... */
879 if (edata->state == ENVSYS_SVALID) {
880 /*
881 * ...
882 * <key>rpms</key>
883 * <integer>2500</integer>
884 * <key>rfact</key>
885 * <integer>10000</integer>
886 * <key>cur-value</key>
887 * <integer>1250</integer>
888 * <key>min-value</key>
889 * <integer>800</integer>
890 * <key>max-value</integer>
891 * <integer>3000</integer>
892 * <key>avg-value</integer>
893 * <integer>1400</integer>
894 * </dict>
895 */
896 if (edata->units == ENVSYS_SFANRPM)
897 if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
898 goto out;
899
900 if (edata->units == ENVSYS_SVOLTS_AC ||
901 edata->units == ENVSYS_SVOLTS_DC)
902 if (sme_sensor_upint32(dict, "rfact", edata->rfact))
903 goto out;
904
905 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
906 goto out;
907
908 if (edata->flags & ENVSYS_FVALID_MIN) {
909 if (sme_sensor_upint32(dict,
910 "min-value",
911 edata->value_min))
912 goto out;
913 }
914
915 if (edata->flags & ENVSYS_FVALID_MAX) {
916 if (sme_sensor_upint32(dict,
917 "max-value",
918 edata->value_max))
919 goto out;
920 }
921
922 if (edata->flags & ENVSYS_FVALID_AVG) {
923 if (sme_sensor_upint32(dict,
924 "avg-value",
925 edata->value_avg))
926 goto out;
927 }
928 }
929
930 /*
931 * ...
932 * </array>
933 */
934
935 mutex_enter(&sme_mtx);
936 if (!prop_array_set(array, sme_uniqsensors - 1, dict)) {
937 mutex_exit(&sme_mtx);
938 DPRINTF(("%s: prop_array_add\n", __func__));
939 goto invalidate_sensor;
940 }
941 mutex_exit(&sme_mtx);
942
943 /*
944 * Add a new event if a monitoring flag was set.
945 */
946 if (edata->monitor) {
947 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
948
949 sme_evdrv_t->sdict = dict;
950 sme_evdrv_t->edata = edata;
951 sme_evdrv_t->sme = sme;
952 sme_evdrv_t->powertype = est[i].crittype;
953
954 sysmon_task_queue_init();
955 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
956 }
957
958 out:
959 prop_object_release(dict);
960 return;
961
962 invalidate_sensor:
963 mutex_enter(&sme_mtx);
964 edata->flags |= ENVSYS_FNOTVALID;
965 mutex_exit(&sme_mtx);
966 prop_object_release(dict);
967 }
968
969 /*
970 * sme_update_dictionary:
971 *
972 * + Update per-sensor dictionaries with new values if there were
973 * changes, otherwise the object in dictionary is untouched.
974 * + Send a critical event if any sensor is in a critical condition.
975 */
976 int
977 sme_update_dictionary(struct sysmon_envsys *sme)
978 {
979 const struct sme_sensor_state *ess = sme_sensor_state;
980 const struct sme_sensor_state *esds = sme_sensor_drive_state;
981 envsys_data_t *edata = NULL;
982 prop_object_t array, dict;
983 int i, j, error, invalid;
984
985 error = invalid = 0;
986 array = dict = NULL;
987
988 /* retrieve the array of dictionaries in device. */
989 array = prop_dictionary_get(sme_propd, sme->sme_name);
990 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
991 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
992 return EINVAL;
993 }
994
995 /*
996 * - iterate over all sensors.
997 * - fetch new data.
998 * - check if data in dictionary is different than new data.
999 * - update dictionary if there were changes.
1000 */
1001 for (i = 0; i < sme->sme_nsensors; i++) {
1002 edata = &sme->sme_sensor_data[i];
1003 /* skip invalid sensors */
1004 if (edata->flags & ENVSYS_FNOTVALID) {
1005 invalid++;
1006 continue;
1007 }
1008
1009 /*
1010 * refresh sensor data via sme_gtredata only if the
1011 * flag is not set.
1012 */
1013 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1014 error = (*sme->sme_gtredata)(sme, edata);
1015 if (error) {
1016 DPRINTF(("%s: gtredata[%d] failed\n",
1017 __func__, i));
1018 return error;
1019 }
1020 }
1021
1022 /* retrieve sensor's dictionary. */
1023 dict = prop_array_get(array, i - invalid);
1024 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1025 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1026 __func__, edata->sensor, sme->sme_name));
1027 return EINVAL;
1028 }
1029
1030 /* update state sensor. */
1031 for (j = 0; ess[j].type != -1; j++)
1032 if (ess[j].type == edata->state)
1033 break;
1034
1035 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1036 "units=%d sensor=%d\n", __func__, ess[j].desc,
1037 ess[j].type, edata->flags, edata->units, edata->sensor));
1038
1039 /* update sensor state */
1040 error = sme_sensor_upstring(dict, "state", ess[j].desc);
1041 if (error)
1042 break;
1043
1044 /* update sensor current value */
1045 error = sme_sensor_upint32(dict,
1046 "cur-value",
1047 edata->value_cur);
1048 if (error)
1049 break;
1050
1051 /*
1052 * Integer and Indicator types do not the following
1053 * values, so skip them.
1054 */
1055 if (edata->units == ENVSYS_INTEGER ||
1056 edata->units == ENVSYS_INDICATOR)
1057 continue;
1058
1059 /* update sensor flags */
1060 if (edata->flags & ENVSYS_FPERCENT) {
1061 error = sme_sensor_upbool(dict,
1062 "want-percentage",
1063 true);
1064 if (error)
1065 break;
1066 }
1067
1068 if (edata->flags & ENVSYS_FVALID_MAX) {
1069 error = sme_sensor_upint32(dict,
1070 "max-value",
1071 edata->value_max);
1072 if (error)
1073 break;
1074 }
1075
1076 if (edata->flags & ENVSYS_FVALID_MIN) {
1077 error = sme_sensor_upint32(dict,
1078 "min-value",
1079 edata->value_min);
1080 if (error)
1081 break;
1082 }
1083
1084 if (edata->flags & ENVSYS_FVALID_AVG) {
1085 error = sme_sensor_upint32(dict,
1086 "avg-value",
1087 edata->value_avg);
1088 if (error)
1089 break;
1090 }
1091
1092 /* update 'rpms' only in ENVSYS_SFANRPM. */
1093 if (edata->units == ENVSYS_SFANRPM) {
1094 error = sme_sensor_upuint32(dict,
1095 "rpms",
1096 edata->rpms);
1097 if (error)
1098 break;
1099 }
1100
1101 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1102 if (edata->units == ENVSYS_SVOLTS_AC ||
1103 edata->units == ENVSYS_SVOLTS_DC) {
1104 error = sme_sensor_upint32(dict,
1105 "rfact",
1106 edata->rfact);
1107 if (error)
1108 break;
1109 }
1110
1111 /* update 'drive-state' only in ENVSYS_DRIVE. */
1112 if (edata->units == ENVSYS_DRIVE) {
1113 for (j = 0; esds[j].type != -1; j++)
1114 if (esds[j].type == edata->value_cur)
1115 break;
1116
1117 error = sme_sensor_upstring(dict,
1118 "drive-state",
1119 esds[j].desc);
1120 if (error)
1121 break;
1122 }
1123 }
1124
1125 return error;
1126 }
1127
1128 /*
1129 * sme_userset_dictionary:
1130 *
1131 * + Parses the userland dictionary and run the appropiate
1132 * tasks that were requested.
1133 */
1134 int
1135 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1136 prop_array_t array)
1137 {
1138 const struct sme_sensor_type *sst = sme_sensor_type;
1139 envsys_data_t *edata, *nedata;
1140 prop_dictionary_t dict;
1141 prop_object_t obj, obj1, obj2;
1142 int32_t critval;
1143 int i, invalid, error;
1144 const char *blah, *sname;
1145 bool targetfound = false;
1146
1147 error = invalid = 0;
1148 blah = sname = NULL;
1149
1150 /* get sensor's name from userland dictionary. */
1151 obj = prop_dictionary_get(udict, "sensor-name");
1152 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1153 DPRINTF(("%s: sensor-name failed\n", __func__));
1154 return EINVAL;
1155 }
1156
1157 /* iterate over the sensors to find the right one */
1158 for (i = 0; i < sme->sme_nsensors; i++) {
1159 edata = &sme->sme_sensor_data[i];
1160 /* skip sensors with duplicate description */
1161 if (edata->flags & ENVSYS_FNOTVALID) {
1162 invalid++;
1163 continue;
1164 }
1165
1166 dict = prop_array_get(array, i - invalid);
1167 obj1 = prop_dictionary_get(dict, "description");
1168
1169 /* is it our sensor? */
1170 if (!prop_string_equals(obj1, obj))
1171 continue;
1172
1173 /*
1174 * Check if a new description operation was
1175 * requested by the user and set new description.
1176 */
1177 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1178 targetfound = true;
1179 blah = prop_string_cstring_nocopy(obj2);
1180
1181 for (i = 0; i < sme->sme_nsensors; i++) {
1182 if (i == edata->sensor)
1183 continue;
1184
1185 nedata = &sme->sme_sensor_data[i];
1186 if (strcmp(blah, nedata->desc) == 0) {
1187 error = EEXIST;
1188 break;
1189 }
1190 }
1191
1192 if (error)
1193 break;
1194
1195 error = sme_sensor_upstring(dict,
1196 "description",
1197 blah);
1198 if (!error)
1199 (void)strlcpy(edata->desc,
1200 blah,
1201 sizeof(edata->desc));
1202
1203 break;
1204 }
1205
1206 /* did the user want to remove a critical capacity limit? */
1207 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1208 if (obj2 != NULL) {
1209 targetfound = true;
1210 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1211 (edata->flags & ENVSYS_FPERCENT) == 0) {
1212 error = ENOTSUP;
1213 break;
1214 }
1215
1216 sname = prop_string_cstring_nocopy(obj);
1217 error = sme_event_unregister(sname,
1218 PENVSYS_EVENT_BATT_USERCAP);
1219 if (error)
1220 break;
1221
1222 prop_dictionary_remove(dict, "critical-capacity");
1223 break;
1224 }
1225
1226 /* did the user want to remove a critical min limit? */
1227 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1228 if (obj2 != NULL) {
1229 targetfound = true;
1230 sname = prop_string_cstring_nocopy(obj);
1231 error = sme_event_unregister(sname,
1232 PENVSYS_EVENT_USER_CRITMIN);
1233 if (error)
1234 break;
1235
1236 prop_dictionary_remove(dict, "critical-min-limit");
1237 break;
1238 }
1239
1240 /* did the user want to remove a critical max limit? */
1241 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1242 if (obj2 != NULL) {
1243 targetfound = true;
1244 sname = prop_string_cstring_nocopy(obj);
1245 error = sme_event_unregister(sname,
1246 PENVSYS_EVENT_USER_CRITMAX);
1247 if (error)
1248 break;
1249
1250 prop_dictionary_remove(dict, "critical-max-limit");
1251 break;
1252 }
1253
1254 /* did the user want to change rfact? */
1255 obj2 = prop_dictionary_get(udict, "new-rfact");
1256 if (obj2 != NULL) {
1257 targetfound = true;
1258 if (edata->flags & ENVSYS_FCHANGERFACT)
1259 edata->rfact = prop_number_integer_value(obj2);
1260 else
1261 error = ENOTSUP;
1262
1263 break;
1264 }
1265
1266 for (i = 0; sst[i].type != -1; i++)
1267 if (sst[i].type == edata->units)
1268 break;
1269
1270 /* did the user want to set a critical capacity event? */
1271 obj2 = prop_dictionary_get(udict, "critical-capacity");
1272 if (obj2 != NULL) {
1273 targetfound = true;
1274 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1275 (edata->flags & ENVSYS_FPERCENT) == 0) {
1276 error = ENOTSUP;
1277 break;
1278 }
1279
1280 critval = prop_number_integer_value(obj2);
1281 error = sme_event_add(dict,
1282 edata,
1283 sme->sme_name,
1284 "critical-capacity",
1285 critval,
1286 PENVSYS_EVENT_BATT_USERCAP,
1287 sst[i].crittype);
1288 break;
1289 }
1290
1291 /* did the user want to set a critical max event? */
1292 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1293 if (obj2 != NULL) {
1294 targetfound = true;
1295 if (edata->units == ENVSYS_INDICATOR ||
1296 edata->flags & ENVSYS_FMONNOTSUPP) {
1297 error = ENOTSUP;
1298 break;
1299 }
1300
1301 critval = prop_number_integer_value(obj2);
1302 error = sme_event_add(dict,
1303 edata,
1304 sme->sme_name,
1305 "critical-max-limit",
1306 critval,
1307 PENVSYS_EVENT_USER_CRITMAX,
1308 sst[i].crittype);
1309 break;
1310 }
1311
1312 /* did the user want to set a critical min event? */
1313 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1314 if (obj2 != NULL) {
1315 targetfound = true;
1316 if (edata->units == ENVSYS_INDICATOR ||
1317 edata->flags & ENVSYS_FMONNOTSUPP) {
1318 error = ENOTSUP;
1319 break;
1320 }
1321
1322 critval = prop_number_integer_value(obj2);
1323 error = sme_event_add(dict,
1324 edata,
1325 sme->sme_name,
1326 "critical-min-limit",
1327 critval,
1328 PENVSYS_EVENT_USER_CRITMIN,
1329 sst[i].crittype);
1330 break;
1331 }
1332 }
1333
1334 /* invalid target? return the error */
1335 if (!targetfound)
1336 error = EINVAL;
1337
1338 return error;
1339 }
1340