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