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