1 /* $NetBSD: sysmon_envsys.c,v 1.97 2010/03/14 18:03:15 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2008 Juan Romero Pardines. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /*- 29 * Copyright (c) 2000 Zembu Labs, Inc. 30 * All rights reserved. 31 * 32 * Author: Jason R. Thorpe <thorpej@zembu.com> 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 3. All advertising materials mentioning features or use of this software 43 * must display the following acknowledgement: 44 * This product includes software developed by Zembu Labs, Inc. 45 * 4. Neither the name of Zembu Labs nor the names of its employees may 46 * be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS 50 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR- 51 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS- 52 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 /* 62 * Environmental sensor framework for sysmon, exported to userland 63 * with proplib(3). 64 */ 65 66 #include <sys/cdefs.h> 67 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.97 2010/03/14 18:03:15 pgoyette Exp $"); 68 69 #include <sys/param.h> 70 #include <sys/types.h> 71 #include <sys/conf.h> 72 #include <sys/errno.h> 73 #include <sys/fcntl.h> 74 #include <sys/kernel.h> 75 #include <sys/systm.h> 76 #include <sys/proc.h> 77 #include <sys/mutex.h> 78 #include <sys/kmem.h> 79 80 /* #define ENVSYS_DEBUG */ 81 #include <dev/sysmon/sysmonvar.h> 82 #include <dev/sysmon/sysmon_envsysvar.h> 83 #include <dev/sysmon/sysmon_taskq.h> 84 85 kmutex_t sme_global_mtx; 86 87 static prop_dictionary_t sme_propd; 88 static uint32_t sysmon_envsys_next_sensor_index; 89 static struct sysmon_envsys *sysmon_envsys_find_40(u_int); 90 91 static void sysmon_envsys_destroy_plist(prop_array_t); 92 static void sme_remove_userprops(void); 93 static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t, 94 prop_dictionary_t); 95 static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *, 96 prop_array_t, prop_dictionary_t, envsys_data_t *); 97 static void sme_initial_refresh(void *); 98 static uint32_t sme_get_max_value(struct sysmon_envsys *, 99 bool (*)(const envsys_data_t*), bool); 100 101 /* 102 * sysmon_envsys_init: 103 * 104 * + Initialize global mutex, dictionary and the linked list. 105 */ 106 void 107 sysmon_envsys_init(void) 108 { 109 LIST_INIT(&sysmon_envsys_list); 110 mutex_init(&sme_global_mtx, MUTEX_DEFAULT, IPL_NONE); 111 sme_propd = prop_dictionary_create(); 112 } 113 114 /* 115 * sysmonopen_envsys: 116 * 117 * + Open the system monitor device. 118 */ 119 int 120 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l) 121 { 122 return 0; 123 } 124 125 /* 126 * sysmonclose_envsys: 127 * 128 * + Close the system monitor device. 129 */ 130 int 131 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l) 132 { 133 return 0; 134 } 135 136 /* 137 * sysmonioctl_envsys: 138 * 139 * + Perform a sysmon envsys control request. 140 */ 141 int 142 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 143 { 144 struct sysmon_envsys *sme = NULL; 145 int error = 0; 146 u_int oidx; 147 148 switch (cmd) { 149 /* 150 * To update the global dictionary with latest data from devices. 151 */ 152 case ENVSYS_GETDICTIONARY: 153 { 154 struct plistref *plist = (struct plistref *)data; 155 156 /* 157 * Update dictionaries on all sysmon envsys devices 158 * registered. 159 */ 160 mutex_enter(&sme_global_mtx); 161 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 162 sysmon_envsys_acquire(sme, false); 163 error = sme_update_dictionary(sme); 164 if (error) { 165 DPRINTF(("%s: sme_update_dictionary, " 166 "error=%d\n", __func__, error)); 167 sysmon_envsys_release(sme, false); 168 mutex_exit(&sme_global_mtx); 169 return error; 170 } 171 sysmon_envsys_release(sme, false); 172 } 173 mutex_exit(&sme_global_mtx); 174 /* 175 * Copy global dictionary to userland. 176 */ 177 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd); 178 break; 179 } 180 /* 181 * To set properties on multiple devices. 182 */ 183 case ENVSYS_SETDICTIONARY: 184 { 185 const struct plistref *plist = (const struct plistref *)data; 186 prop_dictionary_t udict; 187 prop_object_iterator_t iter, iter2; 188 prop_object_t obj, obj2; 189 prop_array_t array_u, array_k; 190 const char *devname = NULL; 191 192 if ((flag & FWRITE) == 0) 193 return EPERM; 194 195 /* 196 * Get dictionary from userland. 197 */ 198 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict); 199 if (error) { 200 DPRINTF(("%s: copyin_ioctl error=%d\n", 201 __func__, error)); 202 break; 203 } 204 205 iter = prop_dictionary_iterator(udict); 206 if (!iter) { 207 prop_object_release(udict); 208 return ENOMEM; 209 } 210 211 /* 212 * Iterate over the userland dictionary and process 213 * the list of devices. 214 */ 215 while ((obj = prop_object_iterator_next(iter))) { 216 array_u = prop_dictionary_get_keysym(udict, obj); 217 if (prop_object_type(array_u) != PROP_TYPE_ARRAY) { 218 prop_object_iterator_release(iter); 219 prop_object_release(udict); 220 return EINVAL; 221 } 222 223 devname = prop_dictionary_keysym_cstring_nocopy(obj); 224 DPRINTF(("%s: processing the '%s' array requests\n", 225 __func__, devname)); 226 227 /* 228 * find the correct sme device. 229 */ 230 sme = sysmon_envsys_find(devname); 231 if (!sme) { 232 DPRINTF(("%s: NULL sme\n", __func__)); 233 prop_object_iterator_release(iter); 234 prop_object_release(udict); 235 return EINVAL; 236 } 237 238 /* 239 * Find the correct array object with the string 240 * supplied by the userland dictionary. 241 */ 242 array_k = prop_dictionary_get(sme_propd, devname); 243 if (prop_object_type(array_k) != PROP_TYPE_ARRAY) { 244 DPRINTF(("%s: array device failed\n", 245 __func__)); 246 sysmon_envsys_release(sme, false); 247 prop_object_iterator_release(iter); 248 prop_object_release(udict); 249 return EINVAL; 250 } 251 252 iter2 = prop_array_iterator(array_u); 253 if (!iter2) { 254 sysmon_envsys_release(sme, false); 255 prop_object_iterator_release(iter); 256 prop_object_release(udict); 257 return ENOMEM; 258 } 259 260 /* 261 * Iterate over the array of dictionaries to 262 * process the list of sensors and properties. 263 */ 264 while ((obj2 = prop_object_iterator_next(iter2))) { 265 /* 266 * do the real work now. 267 */ 268 error = sme_userset_dictionary(sme, 269 obj2, 270 array_k); 271 if (error) { 272 sysmon_envsys_release(sme, false); 273 prop_object_iterator_release(iter2); 274 prop_object_iterator_release(iter); 275 prop_object_release(udict); 276 return error; 277 } 278 } 279 280 sysmon_envsys_release(sme, false); 281 prop_object_iterator_release(iter2); 282 } 283 284 prop_object_iterator_release(iter); 285 prop_object_release(udict); 286 break; 287 } 288 /* 289 * To remove all properties from all devices registered. 290 */ 291 case ENVSYS_REMOVEPROPS: 292 { 293 const struct plistref *plist = (const struct plistref *)data; 294 prop_dictionary_t udict; 295 prop_object_t obj; 296 297 if ((flag & FWRITE) == 0) 298 return EPERM; 299 300 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict); 301 if (error) { 302 DPRINTF(("%s: copyin_ioctl error=%d\n", 303 __func__, error)); 304 break; 305 } 306 307 obj = prop_dictionary_get(udict, "envsys-remove-props"); 308 if (!obj || !prop_bool_true(obj)) { 309 DPRINTF(("%s: invalid 'envsys-remove-props'\n", 310 __func__)); 311 return EINVAL; 312 } 313 314 prop_object_release(udict); 315 sme_remove_userprops(); 316 317 break; 318 } 319 /* 320 * Compatibility ioctls with the old interface, only implemented 321 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old 322 * applications work. 323 */ 324 case ENVSYS_GTREDATA: 325 { 326 struct envsys_tre_data *tred = (void *)data; 327 envsys_data_t *edata = NULL; 328 bool found = false; 329 330 tred->validflags = 0; 331 332 sme = sysmon_envsys_find_40(tred->sensor); 333 if (!sme) 334 break; 335 336 oidx = tred->sensor; 337 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor); 338 339 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n", 340 __func__, tred->sensor, oidx, sme->sme_name, 341 sme->sme_nsensors)); 342 343 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 344 if (edata->sensor == tred->sensor) { 345 found = true; 346 break; 347 } 348 } 349 350 if (!found) { 351 sysmon_envsys_release(sme, false); 352 error = ENODEV; 353 break; 354 } 355 356 if (tred->sensor < sme->sme_nsensors) { 357 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0 && 358 (sme->sme_flags & SME_POLL_ONLY) == 0) { 359 mutex_enter(&sme->sme_mtx); 360 (*sme->sme_refresh)(sme, edata); 361 mutex_exit(&sme->sme_mtx); 362 } 363 364 /* 365 * copy required values to the old interface. 366 */ 367 tred->sensor = edata->sensor; 368 tred->cur.data_us = edata->value_cur; 369 tred->cur.data_s = edata->value_cur; 370 tred->max.data_us = edata->value_max; 371 tred->max.data_s = edata->value_max; 372 tred->min.data_us = edata->value_min; 373 tred->min.data_s = edata->value_min; 374 tred->avg.data_us = edata->value_avg; 375 tred->avg.data_s = edata->value_avg; 376 if (edata->units == ENVSYS_BATTERY_CHARGE) 377 tred->units = ENVSYS_INDICATOR; 378 else 379 tred->units = edata->units; 380 381 tred->validflags |= ENVSYS_FVALID; 382 tred->validflags |= ENVSYS_FCURVALID; 383 384 if (edata->flags & ENVSYS_FPERCENT) { 385 tred->validflags |= ENVSYS_FMAXVALID; 386 tred->validflags |= ENVSYS_FFRACVALID; 387 } 388 389 if (edata->state == ENVSYS_SINVALID) { 390 tred->validflags &= ~ENVSYS_FCURVALID; 391 tred->cur.data_us = tred->cur.data_s = 0; 392 } 393 394 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n", 395 __func__, edata->desc, tred->cur.data_s)); 396 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d" 397 " tred->sensor=%d\n", __func__, tred->validflags, 398 tred->units, tred->sensor)); 399 } 400 tred->sensor = oidx; 401 sysmon_envsys_release(sme, false); 402 403 break; 404 } 405 case ENVSYS_GTREINFO: 406 { 407 struct envsys_basic_info *binfo = (void *)data; 408 envsys_data_t *edata = NULL; 409 bool found = false; 410 411 binfo->validflags = 0; 412 413 sme = sysmon_envsys_find_40(binfo->sensor); 414 if (!sme) 415 break; 416 417 oidx = binfo->sensor; 418 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor); 419 420 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 421 if (edata->sensor == binfo->sensor) { 422 found = true; 423 break; 424 } 425 } 426 427 if (!found) { 428 sysmon_envsys_release(sme, false); 429 error = ENODEV; 430 break; 431 } 432 433 binfo->validflags |= ENVSYS_FVALID; 434 435 if (binfo->sensor < sme->sme_nsensors) { 436 if (edata->units == ENVSYS_BATTERY_CHARGE) 437 binfo->units = ENVSYS_INDICATOR; 438 else 439 binfo->units = edata->units; 440 441 /* 442 * previously, the ACPI sensor names included the 443 * device name. Include that in compatibility code. 444 */ 445 if (strncmp(sme->sme_name, "acpi", 4) == 0) 446 (void)snprintf(binfo->desc, sizeof(binfo->desc), 447 "%s %s", sme->sme_name, edata->desc); 448 else 449 (void)strlcpy(binfo->desc, edata->desc, 450 sizeof(binfo->desc)); 451 } 452 453 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n", 454 __func__, binfo->units, binfo->validflags)); 455 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n", 456 __func__, binfo->desc, binfo->sensor)); 457 458 binfo->sensor = oidx; 459 sysmon_envsys_release(sme, false); 460 461 break; 462 } 463 default: 464 error = ENOTTY; 465 break; 466 } 467 468 return error; 469 } 470 471 /* 472 * sysmon_envsys_create: 473 * 474 * + Allocates a new sysmon_envsys object and initializes the 475 * stuff for sensors and events. 476 */ 477 struct sysmon_envsys * 478 sysmon_envsys_create(void) 479 { 480 struct sysmon_envsys *sme; 481 482 sme = kmem_zalloc(sizeof(*sme), KM_SLEEP); 483 TAILQ_INIT(&sme->sme_sensors_list); 484 LIST_INIT(&sme->sme_events_list); 485 mutex_init(&sme->sme_mtx, MUTEX_DEFAULT, IPL_NONE); 486 cv_init(&sme->sme_condvar, "sme_wait"); 487 488 return sme; 489 } 490 491 /* 492 * sysmon_envsys_destroy: 493 * 494 * + Removes all sensors from the tail queue, destroys the callout 495 * and frees the sysmon_envsys object. 496 */ 497 void 498 sysmon_envsys_destroy(struct sysmon_envsys *sme) 499 { 500 envsys_data_t *edata; 501 502 KASSERT(sme != NULL); 503 504 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) { 505 edata = TAILQ_FIRST(&sme->sme_sensors_list); 506 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 507 } 508 mutex_destroy(&sme->sme_mtx); 509 cv_destroy(&sme->sme_condvar); 510 kmem_free(sme, sizeof(*sme)); 511 } 512 513 /* 514 * sysmon_envsys_sensor_attach: 515 * 516 * + Attachs a sensor into a sysmon_envsys device checking that units 517 * is set to a valid type and description is unique and not empty. 518 */ 519 int 520 sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata) 521 { 522 const struct sme_description_table *sdt_units; 523 envsys_data_t *oedata; 524 int i; 525 526 KASSERT(sme != NULL || edata != NULL); 527 528 /* 529 * Find the correct units for this sensor. 530 */ 531 sdt_units = sme_get_description_table(SME_DESC_UNITS); 532 for (i = 0; sdt_units[i].type != -1; i++) 533 if (sdt_units[i].type == edata->units) 534 break; 535 536 if (strcmp(sdt_units[i].desc, "unknown") == 0) 537 return EINVAL; 538 539 /* 540 * Check that description is not empty or duplicate. 541 */ 542 if (strlen(edata->desc) == 0) 543 return EINVAL; 544 545 mutex_enter(&sme->sme_mtx); 546 sysmon_envsys_acquire(sme, true); 547 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) { 548 if (strcmp(oedata->desc, edata->desc) == 0) { 549 sysmon_envsys_release(sme, true); 550 mutex_exit(&sme->sme_mtx); 551 return EEXIST; 552 } 553 } 554 /* 555 * Ok, the sensor has been added into the device queue. 556 */ 557 TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head); 558 559 /* 560 * Give the sensor a index position. 561 */ 562 edata->sensor = sme->sme_nsensors; 563 sme->sme_nsensors++; 564 sysmon_envsys_release(sme, true); 565 mutex_exit(&sme->sme_mtx); 566 567 DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n", 568 __func__, edata->sensor, edata->desc, 569 sdt_units[i].type, sdt_units[i].desc)); 570 571 return 0; 572 } 573 574 /* 575 * sysmon_envsys_sensor_detach: 576 * 577 * + Detachs a sensor from a sysmon_envsys device and decrements the 578 * sensors count on success. 579 */ 580 int 581 sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata) 582 { 583 envsys_data_t *oedata; 584 bool found = false; 585 586 KASSERT(sme != NULL || edata != NULL); 587 588 /* 589 * Check the sensor is already on the list. 590 */ 591 mutex_enter(&sme->sme_mtx); 592 sysmon_envsys_acquire(sme, true); 593 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) { 594 if (oedata->sensor == edata->sensor) { 595 found = true; 596 break; 597 } 598 } 599 600 if (!found) { 601 sysmon_envsys_release(sme, true); 602 mutex_exit(&sme->sme_mtx); 603 return EINVAL; 604 } 605 606 /* 607 * remove it and decrement the sensors count. 608 */ 609 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 610 sme->sme_nsensors--; 611 sysmon_envsys_release(sme, true); 612 mutex_exit(&sme->sme_mtx); 613 614 return 0; 615 } 616 617 618 /* 619 * sysmon_envsys_register: 620 * 621 * + Register a sysmon envsys device. 622 * + Create array of dictionaries for a device. 623 */ 624 int 625 sysmon_envsys_register(struct sysmon_envsys *sme) 626 { 627 struct sme_evdrv { 628 SLIST_ENTRY(sme_evdrv) evdrv_head; 629 sme_event_drv_t *evdrv; 630 }; 631 SLIST_HEAD(, sme_evdrv) sme_evdrv_list; 632 struct sme_evdrv *evdv = NULL; 633 struct sysmon_envsys *lsme; 634 prop_array_t array = NULL; 635 prop_dictionary_t dict, dict2; 636 envsys_data_t *edata = NULL; 637 sme_event_drv_t *this_evdrv; 638 int nevent; 639 int error = 0; 640 641 KASSERT(sme != NULL); 642 KASSERT(sme->sme_name != NULL); 643 644 /* 645 * Check if requested sysmon_envsys device is valid 646 * and does not exist already in the list. 647 */ 648 mutex_enter(&sme_global_mtx); 649 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) { 650 if (strcmp(lsme->sme_name, sme->sme_name) == 0) { 651 mutex_exit(&sme_global_mtx); 652 return EEXIST; 653 } 654 } 655 mutex_exit(&sme_global_mtx); 656 657 /* 658 * sanity check: if SME_DISABLE_REFRESH is not set, 659 * the sme_refresh function callback must be non NULL. 660 */ 661 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 662 if (!sme->sme_refresh) 663 return EINVAL; 664 665 /* 666 * If the list of sensors is empty, there's no point to continue... 667 */ 668 if (TAILQ_EMPTY(&sme->sme_sensors_list)) { 669 DPRINTF(("%s: sensors list empty for %s\n", __func__, 670 sme->sme_name)); 671 return ENOTSUP; 672 } 673 674 /* 675 * Initialize the singly linked list for driver events. 676 */ 677 SLIST_INIT(&sme_evdrv_list); 678 679 array = prop_array_create(); 680 if (!array) 681 return ENOMEM; 682 683 /* 684 * Iterate over all sensors and create a dictionary per sensor. 685 * We must respect the order in which the sensors were added. 686 */ 687 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 688 dict = prop_dictionary_create(); 689 if (!dict) { 690 error = ENOMEM; 691 goto out2; 692 } 693 694 /* 695 * Create all objects in sensor's dictionary. 696 */ 697 this_evdrv = sme_add_sensor_dictionary(sme, array, 698 dict, edata); 699 if (this_evdrv) { 700 evdv = kmem_zalloc(sizeof(*evdv), KM_SLEEP); 701 evdv->evdrv = this_evdrv; 702 SLIST_INSERT_HEAD(&sme_evdrv_list, evdv, evdrv_head); 703 } 704 } 705 706 /* 707 * If the array does not contain any object (sensor), there's 708 * no need to attach the driver. 709 */ 710 if (prop_array_count(array) == 0) { 711 error = EINVAL; 712 DPRINTF(("%s: empty array for '%s'\n", __func__, 713 sme->sme_name)); 714 goto out; 715 } 716 717 /* 718 * Add the dictionary for the global properties of this device. 719 */ 720 dict2 = prop_dictionary_create(); 721 if (!dict2) { 722 error = ENOMEM; 723 goto out; 724 } 725 726 error = sme_add_property_dictionary(sme, array, dict2); 727 if (error) { 728 prop_object_release(dict2); 729 goto out; 730 } 731 732 /* 733 * Add the array into the global dictionary for the driver. 734 * 735 * <dict> 736 * <key>foo0</key> 737 * <array> 738 * ... 739 */ 740 mutex_enter(&sme_global_mtx); 741 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) { 742 error = EINVAL; 743 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__, 744 sme->sme_name)); 745 goto out; 746 } 747 748 /* 749 * Add the device into the list. 750 */ 751 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list); 752 sme->sme_fsensor = sysmon_envsys_next_sensor_index; 753 sysmon_envsys_next_sensor_index += sme->sme_nsensors; 754 mutex_exit(&sme_global_mtx); 755 756 out: 757 /* 758 * No errors? register the events that were set in the driver 759 * and make an initial data refresh if was requested. 760 */ 761 if (error == 0) { 762 nevent = 0; 763 sysmon_task_queue_init(); 764 SLIST_FOREACH(evdv, &sme_evdrv_list, evdrv_head) { 765 sysmon_task_queue_sched(0, 766 sme_event_drvadd, evdv->evdrv); 767 nevent++; 768 } 769 DPRINTF(("%s: driver '%s' registered (nsens=%d nevent=%d)\n", 770 __func__, sme->sme_name, sme->sme_nsensors, nevent)); 771 772 if (sme->sme_flags & SME_INIT_REFRESH) 773 sysmon_task_queue_sched(0, sme_initial_refresh, sme); 774 } 775 776 out2: 777 while (!SLIST_EMPTY(&sme_evdrv_list)) { 778 evdv = SLIST_FIRST(&sme_evdrv_list); 779 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head); 780 kmem_free(evdv, sizeof(*evdv)); 781 } 782 if (!error) 783 return 0; 784 785 /* 786 * Ugh... something wasn't right; unregister all events and sensors 787 * previously assigned and destroy the array with all its objects. 788 */ 789 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__, 790 sme->sme_name, error)); 791 792 sme_event_unregister_all(sme); 793 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) { 794 edata = TAILQ_FIRST(&sme->sme_sensors_list); 795 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 796 } 797 sysmon_envsys_destroy_plist(array); 798 return error; 799 } 800 801 /* 802 * sysmon_envsys_destroy_plist: 803 * 804 * + Remove all objects from the array of dictionaries that is 805 * created in a sysmon envsys device. 806 */ 807 static void 808 sysmon_envsys_destroy_plist(prop_array_t array) 809 { 810 prop_object_iterator_t iter, iter2; 811 prop_dictionary_t dict; 812 prop_object_t obj; 813 814 KASSERT(array != NULL); 815 KASSERT(prop_object_type(array) == PROP_TYPE_ARRAY); 816 817 DPRINTFOBJ(("%s: objects in array=%d\n", __func__, 818 prop_array_count(array))); 819 820 iter = prop_array_iterator(array); 821 if (!iter) 822 return; 823 824 while ((dict = prop_object_iterator_next(iter))) { 825 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY); 826 iter2 = prop_dictionary_iterator(dict); 827 if (!iter2) 828 goto out; 829 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__)); 830 while ((obj = prop_object_iterator_next(iter2)) != NULL) { 831 DPRINTFOBJ(("%s: obj=%s\n", __func__, 832 prop_dictionary_keysym_cstring_nocopy(obj))); 833 prop_dictionary_remove(dict, 834 prop_dictionary_keysym_cstring_nocopy(obj)); 835 prop_object_iterator_reset(iter2); 836 } 837 prop_object_iterator_release(iter2); 838 DPRINTFOBJ(("%s: objects in dictionary:%d\n", 839 __func__, prop_dictionary_count(dict))); 840 prop_object_release(dict); 841 } 842 843 out: 844 prop_object_iterator_release(iter); 845 prop_object_release(array); 846 } 847 848 /* 849 * sysmon_envsys_unregister: 850 * 851 * + Unregister a sysmon envsys device. 852 */ 853 void 854 sysmon_envsys_unregister(struct sysmon_envsys *sme) 855 { 856 prop_array_t array; 857 858 KASSERT(sme != NULL); 859 860 /* 861 * Unregister all events associated with device. 862 */ 863 sme_event_unregister_all(sme); 864 /* 865 * Decrement global sensors counter (only used for compatibility 866 * with previous API) and remove the device from the list. 867 */ 868 mutex_enter(&sme_global_mtx); 869 sysmon_envsys_next_sensor_index -= sme->sme_nsensors; 870 LIST_REMOVE(sme, sme_list); 871 mutex_exit(&sme_global_mtx); 872 873 /* 874 * Remove the device (and all its objects) from the global dictionary. 875 */ 876 array = prop_dictionary_get(sme_propd, sme->sme_name); 877 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) { 878 mutex_enter(&sme_global_mtx); 879 prop_dictionary_remove(sme_propd, sme->sme_name); 880 mutex_exit(&sme_global_mtx); 881 sysmon_envsys_destroy_plist(array); 882 } 883 /* 884 * And finally destroy the sysmon_envsys object. 885 */ 886 sysmon_envsys_destroy(sme); 887 } 888 889 /* 890 * sysmon_envsys_find: 891 * 892 * + Find a sysmon envsys device and mark it as busy 893 * once it's available. 894 */ 895 struct sysmon_envsys * 896 sysmon_envsys_find(const char *name) 897 { 898 struct sysmon_envsys *sme; 899 900 mutex_enter(&sme_global_mtx); 901 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 902 if (strcmp(sme->sme_name, name) == 0) { 903 sysmon_envsys_acquire(sme, false); 904 break; 905 } 906 } 907 mutex_exit(&sme_global_mtx); 908 909 return sme; 910 } 911 912 /* 913 * Compatibility function with the old API. 914 */ 915 struct sysmon_envsys * 916 sysmon_envsys_find_40(u_int idx) 917 { 918 struct sysmon_envsys *sme; 919 920 mutex_enter(&sme_global_mtx); 921 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 922 if (idx >= sme->sme_fsensor && 923 idx < (sme->sme_fsensor + sme->sme_nsensors)) { 924 sysmon_envsys_acquire(sme, false); 925 break; 926 } 927 } 928 mutex_exit(&sme_global_mtx); 929 930 return sme; 931 } 932 933 /* 934 * sysmon_envsys_acquire: 935 * 936 * + Wait until a sysmon envsys device is available and mark 937 * it as busy. 938 */ 939 void 940 sysmon_envsys_acquire(struct sysmon_envsys *sme, bool locked) 941 { 942 KASSERT(sme != NULL); 943 944 if (locked) { 945 while (sme->sme_flags & SME_FLAG_BUSY) 946 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 947 sme->sme_flags |= SME_FLAG_BUSY; 948 } else { 949 mutex_enter(&sme->sme_mtx); 950 while (sme->sme_flags & SME_FLAG_BUSY) 951 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 952 sme->sme_flags |= SME_FLAG_BUSY; 953 mutex_exit(&sme->sme_mtx); 954 } 955 } 956 957 /* 958 * sysmon_envsys_release: 959 * 960 * + Unmark a sysmon envsys device as busy, and notify 961 * waiters. 962 */ 963 void 964 sysmon_envsys_release(struct sysmon_envsys *sme, bool locked) 965 { 966 KASSERT(sme != NULL); 967 968 if (locked) { 969 sme->sme_flags &= ~SME_FLAG_BUSY; 970 cv_broadcast(&sme->sme_condvar); 971 } else { 972 mutex_enter(&sme->sme_mtx); 973 sme->sme_flags &= ~SME_FLAG_BUSY; 974 cv_broadcast(&sme->sme_condvar); 975 mutex_exit(&sme->sme_mtx); 976 } 977 } 978 979 /* 980 * sme_initial_refresh: 981 * 982 * + Do an initial refresh of the sensors in a device just after 983 * interrupts are enabled in the autoconf(9) process. 984 * 985 */ 986 static void 987 sme_initial_refresh(void *arg) 988 { 989 struct sysmon_envsys *sme = arg; 990 envsys_data_t *edata; 991 992 mutex_enter(&sme->sme_mtx); 993 sysmon_envsys_acquire(sme, true); 994 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) 995 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 996 (*sme->sme_refresh)(sme, edata); 997 sysmon_envsys_release(sme, true); 998 mutex_exit(&sme->sme_mtx); 999 } 1000 1001 /* 1002 * sme_sensor_dictionary_get: 1003 * 1004 * + Returns a dictionary of a device specified by its index 1005 * position. 1006 */ 1007 prop_dictionary_t 1008 sme_sensor_dictionary_get(prop_array_t array, const char *index) 1009 { 1010 prop_object_iterator_t iter; 1011 prop_dictionary_t dict; 1012 prop_object_t obj; 1013 1014 KASSERT(array != NULL || index != NULL); 1015 1016 iter = prop_array_iterator(array); 1017 if (!iter) 1018 return NULL; 1019 1020 while ((dict = prop_object_iterator_next(iter))) { 1021 obj = prop_dictionary_get(dict, "index"); 1022 if (prop_string_equals_cstring(obj, index)) 1023 break; 1024 } 1025 1026 prop_object_iterator_release(iter); 1027 return dict; 1028 } 1029 1030 /* 1031 * sme_remove_userprops: 1032 * 1033 * + Remove all properties from all devices that were set by 1034 * the ENVSYS_SETDICTIONARY ioctl. 1035 */ 1036 static void 1037 sme_remove_userprops(void) 1038 { 1039 struct sysmon_envsys *sme; 1040 prop_array_t array; 1041 prop_dictionary_t sdict; 1042 envsys_data_t *edata = NULL; 1043 char tmp[ENVSYS_DESCLEN]; 1044 int ptype; 1045 1046 mutex_enter(&sme_global_mtx); 1047 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1048 sysmon_envsys_acquire(sme, false); 1049 array = prop_dictionary_get(sme_propd, sme->sme_name); 1050 1051 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1052 (void)snprintf(tmp, sizeof(tmp), "sensor%d", 1053 edata->sensor); 1054 sdict = sme_sensor_dictionary_get(array, tmp); 1055 KASSERT(sdict != NULL); 1056 1057 ptype = 0; 1058 if (edata->upropset & PROP_BATTCAP) { 1059 prop_dictionary_remove(sdict, 1060 "critical-capacity"); 1061 ptype = PENVSYS_EVENT_CAPACITY; 1062 } 1063 1064 if (edata->upropset & PROP_BATTWARN) { 1065 prop_dictionary_remove(sdict, 1066 "warning-capacity"); 1067 ptype = PENVSYS_EVENT_CAPACITY; 1068 } 1069 1070 if (edata->upropset & PROP_BATTHIGH) { 1071 prop_dictionary_remove(sdict, 1072 "high-capacity"); 1073 ptype = PENVSYS_EVENT_CAPACITY; 1074 } 1075 1076 if (edata->upropset & PROP_BATTMAX) { 1077 prop_dictionary_remove(sdict, 1078 "maximum-capacity"); 1079 ptype = PENVSYS_EVENT_CAPACITY; 1080 } 1081 if (ptype != 0) 1082 sme_event_unregister(sme, edata->desc, ptype); 1083 1084 ptype = 0; 1085 if (edata->upropset & PROP_WARNMAX) { 1086 prop_dictionary_remove(sdict, "warning-max"); 1087 ptype = PENVSYS_EVENT_LIMITS; 1088 } 1089 1090 if (edata->upropset & PROP_WARNMIN) { 1091 prop_dictionary_remove(sdict, "warning-min"); 1092 ptype = PENVSYS_EVENT_LIMITS; 1093 } 1094 1095 if (edata->upropset & PROP_CRITMAX) { 1096 prop_dictionary_remove(sdict, "critical-max"); 1097 ptype = PENVSYS_EVENT_LIMITS; 1098 } 1099 1100 if (edata->upropset & PROP_CRITMIN) { 1101 prop_dictionary_remove(sdict, "critical-min"); 1102 ptype = PENVSYS_EVENT_LIMITS; 1103 } 1104 if (ptype != 0) 1105 sme_event_unregister(sme, edata->desc, ptype); 1106 1107 if (edata->upropset & PROP_RFACT) { 1108 (void)sme_sensor_upint32(sdict, "rfact", 0); 1109 edata->rfact = 0; 1110 } 1111 1112 if (edata->upropset & PROP_DESC) 1113 (void)sme_sensor_upstring(sdict, 1114 "description", edata->desc); 1115 1116 if (edata->upropset) 1117 edata->upropset = 0; 1118 } 1119 1120 /* 1121 * Restore default timeout value. 1122 */ 1123 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT; 1124 sysmon_envsys_release(sme, false); 1125 } 1126 mutex_exit(&sme_global_mtx); 1127 } 1128 1129 /* 1130 * sme_add_property_dictionary: 1131 * 1132 * + Add global properties into a device. 1133 */ 1134 static int 1135 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array, 1136 prop_dictionary_t dict) 1137 { 1138 prop_dictionary_t pdict; 1139 int error = 0; 1140 1141 pdict = prop_dictionary_create(); 1142 if (!pdict) 1143 return EINVAL; 1144 1145 /* 1146 * Add the 'refresh-timeout' object into the 'device-properties' 1147 * dictionary. We use by default 30 seconds. 1148 * 1149 * ... 1150 * <dict> 1151 * <key>device-properties</key> 1152 * <dict> 1153 * <key>refresh-timeout</key> 1154 * <integer>120</integer< 1155 * </dict< 1156 * </dict> 1157 * ... 1158 * 1159 */ 1160 if (!sme->sme_events_timeout) 1161 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT; 1162 1163 if (!prop_dictionary_set_uint64(pdict, "refresh-timeout", 1164 sme->sme_events_timeout)) { 1165 error = EINVAL; 1166 goto out; 1167 } 1168 1169 if (!prop_dictionary_set(dict, "device-properties", pdict)) { 1170 error = EINVAL; 1171 goto out; 1172 } 1173 1174 /* 1175 * Add the device dictionary into the sysmon envsys array. 1176 */ 1177 if (!prop_array_add(array, dict)) 1178 error = EINVAL; 1179 1180 out: 1181 prop_object_release(pdict); 1182 return error; 1183 } 1184 1185 /* 1186 * sme_add_sensor_dictionary: 1187 * 1188 * + Adds the sensor objects into the dictionary and returns a pointer 1189 * to a sme_event_drv_t object if a monitoring flag was set 1190 * (or NULL otherwise). 1191 */ 1192 static sme_event_drv_t * 1193 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, 1194 prop_dictionary_t dict, envsys_data_t *edata) 1195 { 1196 const struct sme_description_table *sdt, *sdt_units; 1197 sme_event_drv_t *sme_evdrv_t = NULL; 1198 int i, j; 1199 char indexstr[ENVSYS_DESCLEN]; 1200 1201 /* 1202 * Find the correct units for this sensor. 1203 */ 1204 sdt_units = sme_get_description_table(SME_DESC_UNITS); 1205 for (i = 0; sdt_units[i].type != -1; i++) 1206 if (sdt_units[i].type == edata->units) 1207 break; 1208 1209 /* 1210 * Add the index sensor string. 1211 * 1212 * ... 1213 * <key>index</eyr 1214 * <string>sensor0</string> 1215 * ... 1216 */ 1217 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor); 1218 if (sme_sensor_upstring(dict, "index", indexstr)) 1219 goto bad; 1220 1221 /* 1222 * ... 1223 * <key>type</key> 1224 * <string>foo</string> 1225 * <key>description</key> 1226 * <string>blah blah</string> 1227 * ... 1228 */ 1229 if (sme_sensor_upstring(dict, "type", sdt_units[i].desc)) 1230 goto bad; 1231 1232 if (sme_sensor_upstring(dict, "description", edata->desc)) 1233 goto bad; 1234 1235 /* 1236 * Add sensor's state description. 1237 * 1238 * ... 1239 * <key>state</key> 1240 * <string>valid</string> 1241 * ... 1242 */ 1243 sdt = sme_get_description_table(SME_DESC_STATES); 1244 for (j = 0; sdt[j].type != -1; j++) 1245 if (sdt[j].type == edata->state) 1246 break; 1247 1248 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n", 1249 __func__, edata->desc, edata->units, edata->state)); 1250 1251 if (sme_sensor_upstring(dict, "state", sdt[j].desc)) 1252 goto bad; 1253 1254 /* 1255 * Add the monitoring boolean object: 1256 * 1257 * ... 1258 * <key>monitoring-supported</key> 1259 * <true/> 1260 * ... 1261 * 1262 * always false on Battery {capacity,charge}, Drive and Indicator types. 1263 * They cannot be monitored. 1264 * 1265 */ 1266 if ((edata->flags & ENVSYS_FMONNOTSUPP) || 1267 (edata->units == ENVSYS_INDICATOR) || 1268 (edata->units == ENVSYS_DRIVE) || 1269 (edata->units == ENVSYS_BATTERY_CAPACITY) || 1270 (edata->units == ENVSYS_BATTERY_CHARGE)) { 1271 if (sme_sensor_upbool(dict, "monitoring-supported", false)) 1272 goto out; 1273 } else { 1274 if (sme_sensor_upbool(dict, "monitoring-supported", true)) 1275 goto out; 1276 } 1277 1278 /* 1279 * Add the percentage boolean object, true if ENVSYS_FPERCENT 1280 * is set or false otherwise. 1281 * 1282 * ... 1283 * <key>want-percentage</key> 1284 * <true/> 1285 * ... 1286 */ 1287 if (edata->flags & ENVSYS_FPERCENT) 1288 if (sme_sensor_upbool(dict, "want-percentage", true)) 1289 goto out; 1290 1291 /* 1292 * Add the allow-rfact boolean object, true if 1293 * ENVSYS_FCHANGERFACT if set or false otherwise. 1294 * 1295 * ... 1296 * <key>allow-rfact</key> 1297 * <true/> 1298 * ... 1299 */ 1300 if (edata->units == ENVSYS_SVOLTS_DC || 1301 edata->units == ENVSYS_SVOLTS_AC) { 1302 if (edata->flags & ENVSYS_FCHANGERFACT) { 1303 if (sme_sensor_upbool(dict, "allow-rfact", true)) 1304 goto out; 1305 } else { 1306 if (sme_sensor_upbool(dict, "allow-rfact", false)) 1307 goto out; 1308 } 1309 } 1310 1311 /* 1312 * Add the object for battery capacity sensors: 1313 * 1314 * ... 1315 * <key>battery-capacity</key> 1316 * <string>NORMAL</string> 1317 * ... 1318 */ 1319 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 1320 sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY); 1321 for (j = 0; sdt[j].type != -1; j++) 1322 if (sdt[j].type == edata->value_cur) 1323 break; 1324 1325 if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc)) 1326 goto out; 1327 } 1328 1329 /* 1330 * Add the drive-state object for drive sensors: 1331 * 1332 * ... 1333 * <key>drive-state</key> 1334 * <string>drive is online</string> 1335 * ... 1336 */ 1337 if (edata->units == ENVSYS_DRIVE) { 1338 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); 1339 for (j = 0; sdt[j].type != -1; j++) 1340 if (sdt[j].type == edata->value_cur) 1341 break; 1342 1343 if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc)) 1344 goto out; 1345 } 1346 1347 /* 1348 * Add the following objects if sensor is enabled... 1349 */ 1350 if (edata->state == ENVSYS_SVALID) { 1351 /* 1352 * Add the following objects: 1353 * 1354 * ... 1355 * <key>rpms</key> 1356 * <integer>2500</integer> 1357 * <key>rfact</key> 1358 * <integer>10000</integer> 1359 * <key>cur-value</key> 1360 * <integer>1250</integer> 1361 * <key>min-value</key> 1362 * <integer>800</integer> 1363 * <key>max-value</integer> 1364 * <integer>3000</integer> 1365 * <key>avg-value</integer> 1366 * <integer>1400</integer> 1367 * ... 1368 */ 1369 if (edata->units == ENVSYS_SFANRPM) 1370 if (sme_sensor_upuint32(dict, "rpms", edata->rpms)) 1371 goto out; 1372 1373 if (edata->units == ENVSYS_SVOLTS_AC || 1374 edata->units == ENVSYS_SVOLTS_DC) 1375 if (sme_sensor_upint32(dict, "rfact", edata->rfact)) 1376 goto out; 1377 1378 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur)) 1379 goto out; 1380 1381 if (edata->flags & ENVSYS_FVALID_MIN) { 1382 if (sme_sensor_upint32(dict, 1383 "min-value", 1384 edata->value_min)) 1385 goto out; 1386 } 1387 1388 if (edata->flags & ENVSYS_FVALID_MAX) { 1389 if (sme_sensor_upint32(dict, 1390 "max-value", 1391 edata->value_max)) 1392 goto out; 1393 } 1394 1395 if (edata->flags & ENVSYS_FVALID_AVG) { 1396 if (sme_sensor_upint32(dict, 1397 "avg-value", 1398 edata->value_avg)) 1399 goto out; 1400 } 1401 } 1402 1403 /* 1404 * ... 1405 * </dict> 1406 * 1407 * Add the dictionary into the array. 1408 * 1409 */ 1410 if (!prop_array_add(array, dict)) { 1411 DPRINTF(("%s: prop_array_add\n", __func__)); 1412 goto bad; 1413 } 1414 1415 /* 1416 * Register new event(s) if any monitoring flag was set. 1417 */ 1418 if (edata->flags & ENVSYS_FMONANY) { 1419 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP); 1420 sme_evdrv_t->sed_sdict = dict; 1421 sme_evdrv_t->sed_edata = edata; 1422 sme_evdrv_t->sed_sme = sme; 1423 sme_evdrv_t->sed_powertype = sdt_units[i].crittype; 1424 } 1425 1426 out: 1427 return sme_evdrv_t; 1428 1429 bad: 1430 prop_object_release(dict); 1431 return NULL; 1432 } 1433 1434 /* 1435 * Find the maximum of all currently reported values. 1436 * The provided callback decides wether a sensor is part of the 1437 * maximum calculation (by returning true) or ignored (callback 1438 * returns false). Example usage: callback selects temperature 1439 * sensors in a given thermal zone, the function calculates the 1440 * maximum currently reported temperature in this zone. 1441 * If the parameter "refresh" is true, new values will be aquired 1442 * from the hardware, if not, the last reported value will be used. 1443 */ 1444 uint32_t 1445 sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*), 1446 bool refresh) 1447 { 1448 struct sysmon_envsys *sme; 1449 uint32_t maxv, v; 1450 1451 maxv = 0; 1452 mutex_enter(&sme_global_mtx); 1453 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1454 sysmon_envsys_acquire(sme, false); 1455 v = sme_get_max_value(sme, predicate, refresh); 1456 sysmon_envsys_release(sme, false); 1457 if (v > maxv) 1458 maxv = v; 1459 } 1460 mutex_exit(&sme_global_mtx); 1461 return maxv; 1462 } 1463 1464 static uint32_t 1465 sme_get_max_value(struct sysmon_envsys *sme, 1466 bool (*predicate)(const envsys_data_t*), 1467 bool refresh) 1468 { 1469 envsys_data_t *edata; 1470 uint32_t maxv, v; 1471 1472 /* 1473 * Iterate over all sensors that match the predicate 1474 */ 1475 maxv = 0; 1476 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1477 if (!(*predicate)(edata)) 1478 continue; 1479 1480 /* 1481 * refresh sensor data via sme_refresh only if the 1482 * flag is not set. 1483 */ 1484 if (refresh && (sme->sme_flags & SME_DISABLE_REFRESH) == 0) { 1485 mutex_enter(&sme->sme_mtx); 1486 (*sme->sme_refresh)(sme, edata); 1487 mutex_exit(&sme->sme_mtx); 1488 } 1489 1490 v = edata->value_cur; 1491 if (v > maxv) 1492 maxv = v; 1493 1494 } 1495 1496 return maxv; 1497 } 1498 1499 /* 1500 * sme_update_dictionary: 1501 * 1502 * + Update per-sensor dictionaries with new values if there were 1503 * changes, otherwise the object in dictionary is untouched. 1504 */ 1505 int 1506 sme_update_dictionary(struct sysmon_envsys *sme) 1507 { 1508 const struct sme_description_table *sdt; 1509 envsys_data_t *edata; 1510 prop_object_t array, dict, obj, obj2; 1511 int j, error = 0; 1512 1513 /* 1514 * Retrieve the array of dictionaries in device. 1515 */ 1516 array = prop_dictionary_get(sme_propd, sme->sme_name); 1517 if (prop_object_type(array) != PROP_TYPE_ARRAY) { 1518 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name)); 1519 return EINVAL; 1520 } 1521 1522 /* 1523 * Get the last dictionary on the array, this contains the 1524 * 'device-properties' sub-dictionary. 1525 */ 1526 obj = prop_array_get(array, prop_array_count(array) - 1); 1527 if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) { 1528 DPRINTF(("%s: not a device-properties dictionary\n", __func__)); 1529 return EINVAL; 1530 } 1531 1532 obj2 = prop_dictionary_get(obj, "device-properties"); 1533 if (!obj2) 1534 return EINVAL; 1535 1536 /* 1537 * Update the 'refresh-timeout' property. 1538 */ 1539 if (!prop_dictionary_set_uint64(obj2, "refresh-timeout", 1540 sme->sme_events_timeout)) 1541 return EINVAL; 1542 1543 /* 1544 * - iterate over all sensors. 1545 * - fetch new data. 1546 * - check if data in dictionary is different than new data. 1547 * - update dictionary if there were changes. 1548 */ 1549 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__, 1550 sme->sme_name, sme->sme_nsensors)); 1551 1552 /* 1553 * Don't bother with locking when traversing the queue, 1554 * the device is already marked as busy; if a sensor 1555 * is going to be removed or added it will have to wait. 1556 */ 1557 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1558 /* 1559 * refresh sensor data via sme_refresh only if the 1560 * flag is not set. 1561 */ 1562 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) { 1563 mutex_enter(&sme->sme_mtx); 1564 (*sme->sme_refresh)(sme, edata); 1565 mutex_exit(&sme->sme_mtx); 1566 } 1567 1568 /* 1569 * retrieve sensor's dictionary. 1570 */ 1571 dict = prop_array_get(array, edata->sensor); 1572 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) { 1573 DPRINTF(("%s: not a dictionary (%d:%s)\n", 1574 __func__, edata->sensor, sme->sme_name)); 1575 return EINVAL; 1576 } 1577 1578 /* 1579 * update sensor's state. 1580 */ 1581 sdt = sme_get_description_table(SME_DESC_STATES); 1582 for (j = 0; sdt[j].type != -1; j++) 1583 if (sdt[j].type == edata->state) 1584 break; 1585 1586 DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", 1587 __func__, edata->sensor, sdt[j].type, sdt[j].desc, 1588 edata->flags)); 1589 1590 error = sme_sensor_upstring(dict, "state", sdt[j].desc); 1591 if (error) 1592 break; 1593 1594 /* 1595 * update sensor's type. 1596 */ 1597 sdt = sme_get_description_table(SME_DESC_UNITS); 1598 for (j = 0; sdt[j].type != -1; j++) 1599 if (sdt[j].type == edata->units) 1600 break; 1601 1602 DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", 1603 __func__, edata->sensor, sdt[j].type, sdt[j].desc)); 1604 1605 error = sme_sensor_upstring(dict, "type", sdt[j].desc); 1606 if (error) 1607 break; 1608 1609 /* 1610 * update sensor's current value. 1611 */ 1612 error = sme_sensor_upint32(dict, 1613 "cur-value", 1614 edata->value_cur); 1615 if (error) 1616 break; 1617 1618 /* 1619 * Battery charge, Integer and Indicator types do not 1620 * need the following objects, so skip them. 1621 */ 1622 if (edata->units == ENVSYS_INTEGER || 1623 edata->units == ENVSYS_INDICATOR || 1624 edata->units == ENVSYS_BATTERY_CHARGE) 1625 continue; 1626 1627 /* 1628 * update sensor flags. 1629 */ 1630 if (edata->flags & ENVSYS_FPERCENT) { 1631 error = sme_sensor_upbool(dict, 1632 "want-percentage", 1633 true); 1634 if (error) 1635 break; 1636 } 1637 1638 /* 1639 * update sensor's {avg,max,min}-value. 1640 */ 1641 if (edata->flags & ENVSYS_FVALID_MAX) { 1642 error = sme_sensor_upint32(dict, 1643 "max-value", 1644 edata->value_max); 1645 if (error) 1646 break; 1647 } 1648 1649 if (edata->flags & ENVSYS_FVALID_MIN) { 1650 error = sme_sensor_upint32(dict, 1651 "min-value", 1652 edata->value_min); 1653 if (error) 1654 break; 1655 } 1656 1657 if (edata->flags & ENVSYS_FVALID_AVG) { 1658 error = sme_sensor_upint32(dict, 1659 "avg-value", 1660 edata->value_avg); 1661 if (error) 1662 break; 1663 } 1664 1665 /* 1666 * update 'rpms' only for ENVSYS_SFANRPM sensors. 1667 */ 1668 if (edata->units == ENVSYS_SFANRPM) { 1669 error = sme_sensor_upuint32(dict, 1670 "rpms", 1671 edata->rpms); 1672 if (error) 1673 break; 1674 } 1675 1676 /* 1677 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors. 1678 */ 1679 if (edata->units == ENVSYS_SVOLTS_AC || 1680 edata->units == ENVSYS_SVOLTS_DC) { 1681 error = sme_sensor_upint32(dict, 1682 "rfact", 1683 edata->rfact); 1684 if (error) 1685 break; 1686 } 1687 1688 /* 1689 * update 'drive-state' only for ENVSYS_DRIVE sensors. 1690 */ 1691 if (edata->units == ENVSYS_DRIVE) { 1692 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); 1693 for (j = 0; sdt[j].type != -1; j++) 1694 if (sdt[j].type == edata->value_cur) 1695 break; 1696 1697 error = sme_sensor_upstring(dict, 1698 "drive-state", 1699 sdt[j].desc); 1700 if (error) 1701 break; 1702 } 1703 1704 /* 1705 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY 1706 * sensors. 1707 */ 1708 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 1709 sdt = 1710 sme_get_description_table(SME_DESC_BATTERY_CAPACITY); 1711 for (j = 0; sdt[j].type != -1; j++) 1712 if (sdt[j].type == edata->value_cur) 1713 break; 1714 1715 error = sme_sensor_upstring(dict, 1716 "battery-capacity", 1717 sdt[j].desc); 1718 if (error) 1719 break; 1720 } 1721 } 1722 1723 return error; 1724 } 1725 1726 /* 1727 * sme_userset_dictionary: 1728 * 1729 * + Parse the userland dictionary and run the appropiate tasks 1730 * that were specified. 1731 */ 1732 int 1733 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict, 1734 prop_array_t array) 1735 { 1736 const struct sme_description_table *sdt; 1737 envsys_data_t *edata; 1738 prop_dictionary_t dict, tdict = NULL; 1739 prop_object_t obj, obj1, obj2, tobj = NULL; 1740 uint32_t props; 1741 uint64_t refresh_timo = 0; 1742 sysmon_envsys_lim_t lims; 1743 int i, error = 0; 1744 const char *blah; 1745 bool targetfound = false; 1746 1747 /* 1748 * The user wanted to change the refresh timeout value for this 1749 * device. 1750 * 1751 * Get the 'device-properties' object from the userland dictionary. 1752 */ 1753 obj = prop_dictionary_get(udict, "device-properties"); 1754 if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) { 1755 /* 1756 * Get the 'refresh-timeout' property for this device. 1757 */ 1758 obj1 = prop_dictionary_get(obj, "refresh-timeout"); 1759 if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) { 1760 targetfound = true; 1761 refresh_timo = 1762 prop_number_unsigned_integer_value(obj1); 1763 if (refresh_timo < 1) 1764 error = EINVAL; 1765 else { 1766 mutex_enter(&sme->sme_mtx); 1767 sme->sme_events_timeout = refresh_timo; 1768 mutex_exit(&sme->sme_mtx); 1769 } 1770 } 1771 return error; 1772 1773 } else if (!obj) { 1774 /* 1775 * Get sensor's index from userland dictionary. 1776 */ 1777 obj = prop_dictionary_get(udict, "index"); 1778 if (!obj) 1779 return EINVAL; 1780 if (prop_object_type(obj) != PROP_TYPE_STRING) { 1781 DPRINTF(("%s: 'index' not a string\n", __func__)); 1782 return EINVAL; 1783 } 1784 } else 1785 return EINVAL; 1786 1787 /* 1788 * Don't bother with locking when traversing the queue, 1789 * the device is already marked as busy; if a sensor 1790 * is going to be removed or added it will have to wait. 1791 */ 1792 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1793 /* 1794 * Get a dictionary and check if it's our sensor by checking 1795 * at its index position. 1796 */ 1797 dict = prop_array_get(array, edata->sensor); 1798 obj1 = prop_dictionary_get(dict, "index"); 1799 1800 /* 1801 * is it our sensor? 1802 */ 1803 if (!prop_string_equals(obj1, obj)) 1804 continue; 1805 1806 props = 0; 1807 1808 /* 1809 * Check if a new description operation was 1810 * requested by the user and set new description. 1811 */ 1812 obj2 = prop_dictionary_get(udict, "description"); 1813 if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) { 1814 targetfound = true; 1815 blah = prop_string_cstring_nocopy(obj2); 1816 1817 /* 1818 * Check for duplicate description. 1819 */ 1820 for (i = 0; i < sme->sme_nsensors; i++) { 1821 if (i == edata->sensor) 1822 continue; 1823 tdict = prop_array_get(array, i); 1824 tobj = 1825 prop_dictionary_get(tdict, "description"); 1826 if (prop_string_equals(obj2, tobj)) { 1827 error = EEXIST; 1828 goto out; 1829 } 1830 } 1831 1832 /* 1833 * Update the object in dictionary. 1834 */ 1835 mutex_enter(&sme->sme_mtx); 1836 error = sme_sensor_upstring(dict, 1837 "description", 1838 blah); 1839 if (error) { 1840 mutex_exit(&sme->sme_mtx); 1841 goto out; 1842 } 1843 1844 DPRINTF(("%s: sensor%d changed desc to: %s\n", 1845 __func__, edata->sensor, blah)); 1846 edata->upropset |= PROP_DESC; 1847 mutex_exit(&sme->sme_mtx); 1848 } 1849 1850 /* 1851 * did the user want to change the rfact? 1852 */ 1853 obj2 = prop_dictionary_get(udict, "rfact"); 1854 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1855 targetfound = true; 1856 if (edata->flags & ENVSYS_FCHANGERFACT) { 1857 mutex_enter(&sme->sme_mtx); 1858 edata->rfact = prop_number_integer_value(obj2); 1859 edata->upropset |= PROP_RFACT; 1860 mutex_exit(&sme->sme_mtx); 1861 DPRINTF(("%s: sensor%d changed rfact to %d\n", 1862 __func__, edata->sensor, edata->rfact)); 1863 } else { 1864 error = ENOTSUP; 1865 goto out; 1866 } 1867 } 1868 1869 sdt = sme_get_description_table(SME_DESC_UNITS); 1870 for (i = 0; sdt[i].type != -1; i++) 1871 if (sdt[i].type == edata->units) 1872 break; 1873 1874 /* 1875 * did the user want to set a critical capacity event? 1876 */ 1877 obj2 = prop_dictionary_get(udict, "critical-capacity"); 1878 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1879 targetfound = true; 1880 lims.sel_critmin = prop_number_integer_value(obj2); 1881 props |= PROP_BATTCAP; 1882 } 1883 1884 /* 1885 * did the user want to set a warning capacity event? 1886 */ 1887 obj2 = prop_dictionary_get(udict, "warning-capacity"); 1888 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1889 targetfound = true; 1890 lims.sel_warnmin = prop_number_integer_value(obj2); 1891 props |= PROP_BATTWARN; 1892 } 1893 1894 /* 1895 * did the user want to set a high capacity event? 1896 */ 1897 obj2 = prop_dictionary_get(udict, "high-capacity"); 1898 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1899 targetfound = true; 1900 lims.sel_warnmin = prop_number_integer_value(obj2); 1901 props |= PROP_BATTHIGH; 1902 } 1903 1904 /* 1905 * did the user want to set a maximum capacity event? 1906 */ 1907 obj2 = prop_dictionary_get(udict, "maximum-capacity"); 1908 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1909 targetfound = true; 1910 lims.sel_warnmin = prop_number_integer_value(obj2); 1911 props |= PROP_BATTMAX; 1912 } 1913 1914 /* 1915 * did the user want to set a critical max event? 1916 */ 1917 obj2 = prop_dictionary_get(udict, "critical-max"); 1918 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1919 targetfound = true; 1920 lims.sel_critmax = prop_number_integer_value(obj2); 1921 props |= PROP_CRITMAX; 1922 } 1923 1924 /* 1925 * did the user want to set a warning max event? 1926 */ 1927 obj2 = prop_dictionary_get(udict, "warning-max"); 1928 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1929 targetfound = true; 1930 lims.sel_warnmax = prop_number_integer_value(obj2); 1931 props |= PROP_WARNMAX; 1932 } 1933 1934 /* 1935 * did the user want to set a critical min event? 1936 */ 1937 obj2 = prop_dictionary_get(udict, "critical-min"); 1938 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1939 targetfound = true; 1940 lims.sel_critmin = prop_number_integer_value(obj2); 1941 props |= PROP_CRITMIN; 1942 } 1943 1944 /* 1945 * did the user want to set a warning min event? 1946 */ 1947 obj2 = prop_dictionary_get(udict, "warning-min"); 1948 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1949 targetfound = true; 1950 lims.sel_warnmin = prop_number_integer_value(obj2); 1951 props |= PROP_WARNMIN; 1952 } 1953 1954 if (props) { 1955 if (edata->flags & ENVSYS_FMONNOTSUPP) { 1956 error = ENOTSUP; 1957 goto out; 1958 } 1959 error = sme_event_register(dict, edata, sme, &lims, 1960 props, 1961 (edata->flags & ENVSYS_FPERCENT)? 1962 PENVSYS_EVENT_CAPACITY: 1963 PENVSYS_EVENT_LIMITS, 1964 sdt[i].crittype); 1965 if (error == EEXIST) 1966 error = 0; 1967 if (error) 1968 goto out; 1969 } 1970 1971 /* 1972 * All objects in dictionary were processed. 1973 */ 1974 break; 1975 } 1976 1977 out: 1978 /* 1979 * invalid target? return the error. 1980 */ 1981 if (!targetfound) 1982 error = EINVAL; 1983 1984 return error; 1985 } 1986