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