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