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