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