sys/dev/apple/smc/smc_sysctl.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | /* * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2026 Abdelkader Boudih <dragonflybsd@seuros.com> * * Ported from FreeBSD's asmc(4) driver. * * DragonFlyBSD port: adapted for kmalloc/kfree, lockmgr 2-arg form, * taskqueue_start_threads ncpu arg, sys/bus_resource.h, acpica paths. * Added MMIO (T2) backend support. */ /* * Apple SMC — sysctl handlers. * * All SYSCTL_HANDLER_ARGS callbacks: fan speed, temperature, SMS axes, * keyboard backlight, battery charge limit, system state keys, and * the optional raw-key debug interface. */ #include "smc.h" /* * Module-scope cached keyboard backlight level. Shared across all device * instances so suspend/resume can restore the last requested brightness even * though the resume path only has a device_t and not sysctl request context. */ static unsigned int light_control = 0; /* Retrieve the cached module-level brightness value for resume restore. */ unsigned int apple_smc_get_light_control(void) { return (light_control); } int apple_smc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; int fan = arg2; int32_t v; v = apple_smc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan); return (sysctl_handle_int(oidp, &v, 0, req)); } int apple_smc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS) { uint8_t buf[16]; device_t dev = (device_t)arg1; int fan = arg2; char *desc; desc = apple_smc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf)); if (desc == NULL) { return (EIO); } return (sysctl_handle_string(oidp, desc, 0, req)); } int apple_smc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; int fan = arg2; int32_t v; v = apple_smc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan); return (sysctl_handle_int(oidp, &v, 0, req)); } /* * Fan speed keys that support both read and write. * Index into this table is packed in arg2 bits [15:8]. */ static const char *apple_smc_fan_rw_keys[] = { ASMC_KEY_FANMINSPEED, /* 0 */ ASMC_KEY_FANMAXSPEED, /* 1 */ ASMC_KEY_FANTARGETSPEED, /* 2 */ }; int apple_smc_mb_sysctl_fanrw(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; int key_idx = (arg2 >> 8) & 0xFF; int fan = arg2 & 0xFF; const char *key; int error; int32_t v; if (key_idx >= (int)nitems(apple_smc_fan_rw_keys)) return (EINVAL); key = apple_smc_fan_rw_keys[key_idx]; v = apple_smc_fan_getvalue(dev, key, fan); error = sysctl_handle_int(oidp, &v, 0, req); if (error == 0 && req->newptr != NULL) { error = apple_smc_fan_setvalue(dev, key, fan, v); } return (error); } int apple_smc_mb_sysctl_fanmanual(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); int fan = arg2; int error; int32_t v; uint8_t buf[2]; uint16_t val; char fmkey[5]; ksnprintf(fmkey, sizeof(fmkey), ASMC_KEY_FANMANUAL_T2, fan); if (sc->sc_is_t2 && apple_smc_key_getinfo(dev, fmkey, NULL, NULL) == 0) { error = apple_smc_key_read(dev, fmkey, buf, 1); if (error != 0) { return (error); } v = buf[0] ? 1 : 0; error = sysctl_handle_int(oidp, &v, 0, req); if (error == 0 && req->newptr != NULL) { if (v != 0 && v != 1) { return (EINVAL); } buf[0] = (uint8_t)v; error = apple_smc_key_write(dev, fmkey, buf, 1); } return (error); } error = apple_smc_key_read(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf)); if (error != 0) { return (error); } val = (buf[0] << 8) | buf[1]; v = (val >> fan) & 0x01; error = sysctl_handle_int(oidp, &v, 0, req); if (error == 0 && req->newptr != NULL) { if (v != 0 && v != 1) { return (EINVAL); } error = apple_smc_key_read(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf)); if (error == 0) { val = (buf[0] << 8) | buf[1]; if (v) { val |= (1 << fan); } else { val &= ~(1 << fan); } buf[0] = val >> 8; buf[1] = val & 0xff; error = apple_smc_key_write(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf)); } } return (error); } int apple_smc_temp_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); int error; int val; if (arg2 < 0 || arg2 >= sc->sc_temp_count) { return (EINVAL); } error = apple_smc_sensor_read(dev, sc->sc_temp_sensors[arg2], &val); if (error != 0) { return (error); } return (sysctl_handle_int(oidp, &val, 0, req)); } int apple_smc_dts_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); int error, val; if (arg2 < 0 || arg2 >= sc->sc_dts_count) return (EINVAL); error = apple_smc_sensor_read(dev, sc->sc_dts_sensors[arg2], &val); if (error != 0) return (error); /* Sentinel check: a disconnected key can return 0x8x00 at runtime too. */ if (val <= -120000) return (ENODEV); /* DTS value is a negative offset from Tj,max; convert to absolute. */ val = sc->sc_tj_max * 1000 + val; return (sysctl_handle_int(oidp, &val, 0, req)); } int apple_smc_sensor_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); int error, val; int sensor_type = (arg2 >> 8) & 0xFF; int sensor_idx = arg2 & 0xFF; const char *key = NULL; switch (sensor_type) { case 'V': if (sensor_idx < sc->sc_voltage_count) { key = sc->sc_voltage_sensors[sensor_idx]; } break; case 'I': if (sensor_idx < sc->sc_current_count) { key = sc->sc_current_sensors[sensor_idx]; } break; case 'P': if (sensor_idx < sc->sc_power_count) { key = sc->sc_power_sensors[sensor_idx]; } break; case 'L': if (sensor_idx < sc->sc_light_count) { key = sc->sc_light_sensors[sensor_idx]; } break; default: return (EINVAL); } if (key == NULL) { return (ENOENT); } error = apple_smc_sensor_read(dev, key, &val); if (error != 0) { return (error); } return (sysctl_handle_int(oidp, &val, 0, req)); } static int apple_smc_sms_axis(device_t dev, const char *key, struct sysctl_oid *oidp, struct sysctl_req *req) { int16_t val; int32_t v; int error; error = apple_smc_sms_read(dev, key, &val); if (error != 0) { return (error); } v = (int32_t)val; return (sysctl_handle_int(oidp, &v, 0, req)); } static const char *apple_smc_sms_keys[] = { ASMC_KEY_SMS_X, /* 0 */ ASMC_KEY_SMS_Y, /* 1 */ ASMC_KEY_SMS_Z, /* 2 */ }; int apple_smc_mb_sysctl_sms(SYSCTL_HANDLER_ARGS) { if (arg2 < 0 || arg2 >= (int)nitems(apple_smc_sms_keys)) return (EINVAL); return (apple_smc_sms_axis((device_t)arg1, apple_smc_sms_keys[arg2], oidp, req)); } static int apple_smc_light_sensor(device_t dev, const char *key, struct sysctl_oid *oidp, struct sysctl_req *req) { uint8_t buf[6]; int error; int32_t v; error = apple_smc_key_read(dev, key, buf, sizeof(buf)); if (error != 0) { return (error); } v = buf[2]; return (sysctl_handle_int(oidp, &v, 0, req)); } static const char *apple_smc_light_keys[] = { ASMC_KEY_LIGHTLEFT, /* 0 */ ASMC_KEY_LIGHTRIGHT, /* 1 */ }; int apple_smc_mbp_sysctl_light(SYSCTL_HANDLER_ARGS) { if (arg2 < 0 || arg2 >= (int)nitems(apple_smc_light_keys)) return (EINVAL); return (apple_smc_light_sensor((device_t)arg1, apple_smc_light_keys[arg2], oidp, req)); } int apple_smc_mbp_sysctl_light_left_10byte(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t buf[10]; uint32_t v; int error; error = apple_smc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof(buf)); if (error != 0) { return (error); } v = be32dec(&buf[6]); v = v >> 8; if (v > 255) { v = 255; } return (sysctl_handle_int(oidp, &v, 0, req)); } int apple_smc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); uint8_t buf[2]; int error, v; v = light_control; error = sysctl_handle_int(oidp, &v, 0, req); if (error == 0 && req->newptr != NULL) { if (v < 0 || v > 255) { return (EINVAL); } light_control = v; sc->sc_kbd_bkl_level = v * 100 / 255; buf[0] = light_control; buf[1] = 0x00; error = apple_smc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof(buf)); } return (error); } int apple_smc_aupo_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t aupo; int val, error; if (apple_smc_key_read(dev, ASMC_KEY_AUPO, &aupo, 1) != 0) { return (EIO); } val = (aupo != 0) ? 1 : 0; error = sysctl_handle_int(oidp, &val, 0, req); if (error != 0 || req->newptr == NULL) { return (error); } aupo = (val != 0) ? 1 : 0; return (apple_smc_key_write(dev, ASMC_KEY_AUPO, &aupo, 1) != 0 ? EIO : 0); } int apple_smc_bclm_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t bclm; int val, error; if (apple_smc_key_read(dev, ASMC_KEY_BCLM, &bclm, 1) != 0) { return (EIO); } val = (int)bclm; error = sysctl_handle_int(oidp, &val, 0, req); if (error != 0 || req->newptr == NULL) { return (error); } if (val < 0 || val > 100) { return (EINVAL); } bclm = (uint8_t)val; return (apple_smc_key_write(dev, ASMC_KEY_BCLM, &bclm, 1) != 0 ? EIO : 0); } static const char * apple_smc_cause_str(int8_t cause) { switch (cause) { case -128: return "power-loss"; case -127: return "software-powerdown"; case -125: return "thermtrip"; case -50: return "overtemp-shutdown"; case -20: return "watchdog"; case -15: return "battery-low"; case 0: return "unknown"; case 1: return "overtemp-sleep"; case 3: return "power-button"; case 5: return "good-shutdown"; default: return NULL; } } static const char *apple_smc_cause_keys[] = { ASMC_KEY_MSSD, /* 0 */ ASMC_KEY_MSSP, /* 1 */ }; int apple_smc_cause_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; const char *key, *desc; int8_t cause; char buf[48]; if (arg2 < 0 || arg2 >= (int)nitems(apple_smc_cause_keys)) return (EINVAL); key = apple_smc_cause_keys[arg2]; if (apple_smc_key_read(dev, key, (uint8_t *)&cause, 1) != 0) return (EIO); desc = apple_smc_cause_str(cause); if (desc) ksnprintf(buf, sizeof(buf), "%d (%s)", (int)cause, desc); else ksnprintf(buf, sizeof(buf), "%d", (int)cause); return (sysctl_handle_string(oidp, buf, sizeof(buf), req)); } int apple_smc_msal_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t msal; char buf[80]; if (apple_smc_key_read(dev, ASMC_KEY_MSAL, &msal, 1) != 0) { return (EIO); } /* * Bits 0x04, 0x10 and 0x20 are present in observed hardware values but * are not documented here yet, so report the raw byte and decode only the * known flags. */ ksnprintf(buf, sizeof(buf), "0x%02x (tss=%d therm_valid=%d calib_valid=%d prochot=%d plimits=%d)", msal, (msal & 0x01) != 0, (msal & 0x02) != 0, (msal & 0x08) != 0, (msal & 0x40) != 0, (msal & 0x80) != 0); return (sysctl_handle_string(oidp, buf, sizeof(buf), req)); } int apple_smc_clkt_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t buf[4]; uint32_t secs; if (apple_smc_key_read(dev, ASMC_KEY_CLKT, buf, 4) != 0) { return (EIO); } secs = be32dec(buf); return (sysctl_handle_32(oidp, &secs, 0, req)); } int apple_smc_msps_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t buf[2]; uint32_t state; if (apple_smc_key_read(dev, ASMC_KEY_MSPS, buf, 2) != 0) { return (EIO); } state = ((uint32_t)buf[0] << 8) | buf[1]; return (sysctl_handle_32(oidp, &state, 0, req)); } int apple_smc_rplt_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t buf[9]; char name[9]; memset(buf, 0, sizeof(buf)); if (apple_smc_key_read(dev, ASMC_KEY_RPLT, buf, 8) != 0) { return (EIO); } memcpy(name, buf, 8); name[8] = '\0'; return (sysctl_handle_string(oidp, name, sizeof(name), req)); } int apple_smc_rgen_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; uint8_t gen; uint32_t val; if (apple_smc_key_read(dev, ASMC_KEY_RGEN, &gen, 1) != 0) { return (EIO); } val = gen; return (sysctl_handle_32(oidp, &val, 0, req)); } #ifdef APPLE_SMC_DEBUG int apple_smc_raw_key_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); char newkey[ASMC_KEYLEN + 1]; uint8_t keylen; int error; strlcpy(newkey, sc->sc_rawkey, sizeof(newkey)); error = sysctl_handle_string(oidp, newkey, sizeof(newkey), req); if (error || req->newptr == NULL) { return (error); } if (strlen(newkey) != ASMC_KEYLEN) { return (EINVAL); } if (apple_smc_key_getinfo(dev, newkey, &keylen, sc->sc_rawtype) != 0) { return (ENOENT); } if (keylen > ASMC_MAXVAL) { keylen = ASMC_MAXVAL; } strlcpy(sc->sc_rawkey, newkey, sizeof(sc->sc_rawkey)); sc->sc_rawlen = keylen; memset(sc->sc_rawval, 0, sizeof(sc->sc_rawval)); error = apple_smc_key_read(dev, sc->sc_rawkey, sc->sc_rawval, sc->sc_rawlen); if (error != 0) { return (error); } return (0); } int apple_smc_raw_value_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); char hexbuf[ASMC_MAXVAL * 2 + 1]; int error, i; if (sc->sc_rawkey[0] != '\0') { error = apple_smc_key_read(dev, sc->sc_rawkey, sc->sc_rawval, sc->sc_rawlen > 0 ? sc->sc_rawlen : ASMC_MAXVAL); if (error != 0) { return (error); } } for (i = 0; i < sc->sc_rawlen && i < ASMC_MAXVAL; i++) { ksnprintf(hexbuf + i * 2, 3, "%02x", sc->sc_rawval[i]); } hexbuf[i * 2] = '\0'; error = sysctl_handle_string(oidp, hexbuf, sizeof(hexbuf), req); if (error || req->newptr == NULL) { return (error); } if (sc->sc_rawkey[0] == '\0') { return (EINVAL); } memset(sc->sc_rawval, 0, sizeof(sc->sc_rawval)); for (i = 0; i < sc->sc_rawlen && hexbuf[i*2] && hexbuf[i*2+1]; i++) { unsigned int val; char tmp[3] = { hexbuf[i*2], hexbuf[i*2+1], 0 }; if (ksscanf(tmp, "%02x", &val) == 1) { sc->sc_rawval[i] = (uint8_t)val; } } return (apple_smc_key_write(dev, sc->sc_rawkey, sc->sc_rawval, sc->sc_rawlen) != 0 ? EIO : 0); } int apple_smc_raw_len_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); int v = sc->sc_rawlen; return (sysctl_handle_int(oidp, &v, 0, req)); } int apple_smc_raw_type_sysctl(SYSCTL_HANDLER_ARGS) { device_t dev = (device_t)arg1; struct apple_smc_softc *sc = device_get_softc(dev); return (sysctl_handle_string(oidp, sc->sc_rawtype, sizeof(sc->sc_rawtype), req)); } #endif /* APPLE_SMC_DEBUG */ |