1 /*- 2 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 * 26 */ 27 28 /* 29 * Driver for Apple's System Management Console (SMC). 30 * SMC can be found on the MacBook, MacBook Pro and Mac Mini. 31 * 32 * Inspired by the Linux applesmc driver. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <sys/mutex.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 #include <sys/taskqueue.h> 49 #include <sys/rman.h> 50 51 #include <machine/resource.h> 52 53 #include <contrib/dev/acpica/include/acpi.h> 54 55 #include <dev/acpica/acpivar.h> 56 #include <dev/asmc/asmcvar.h> 57 58 #include "opt_intr_filter.h" 59 60 /* 61 * Device interface. 62 */ 63 static int asmc_probe(device_t dev); 64 static int asmc_attach(device_t dev); 65 static int asmc_detach(device_t dev); 66 67 /* 68 * SMC functions. 69 */ 70 static int asmc_init(device_t dev); 71 static int asmc_command(device_t dev, uint8_t command); 72 static int asmc_wait(device_t dev, uint8_t val); 73 static int asmc_wait_ack(device_t dev, uint8_t val, int amount); 74 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf, 75 uint8_t len); 76 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf, 77 uint8_t); 78 static int asmc_fan_count(device_t dev); 79 static int asmc_fan_getvalue(device_t dev, const char *key, int fan); 80 static int asmc_temp_getvalue(device_t dev, const char *key); 81 static int asmc_sms_read(device_t, const char *key, int16_t *val); 82 static void asmc_sms_calibrate(device_t dev); 83 static int asmc_sms_intrfast(void *arg); 84 #ifdef INTR_FILTER 85 static void asmc_sms_handler(void *arg); 86 #endif 87 static void asmc_sms_printintr(device_t dev, uint8_t); 88 static void asmc_sms_task(void *arg, int pending); 89 90 /* 91 * Model functions. 92 */ 93 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS); 94 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS); 95 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS); 96 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS); 97 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS); 98 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS); 99 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS); 100 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS); 101 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS); 102 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS); 103 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS); 104 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS); 105 106 struct asmc_model { 107 const char *smc_model; /* smbios.system.product env var. */ 108 const char *smc_desc; /* driver description */ 109 110 /* Helper functions */ 111 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS); 112 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS); 113 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS); 114 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS); 115 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS); 116 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS); 117 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS); 118 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS); 119 int (*smc_light_left)(SYSCTL_HANDLER_ARGS); 120 int (*smc_light_right)(SYSCTL_HANDLER_ARGS); 121 int (*smc_light_control)(SYSCTL_HANDLER_ARGS); 122 123 const char *smc_temps[ASMC_TEMP_MAX]; 124 const char *smc_tempnames[ASMC_TEMP_MAX]; 125 const char *smc_tempdescs[ASMC_TEMP_MAX]; 126 }; 127 128 static struct asmc_model *asmc_match(device_t dev); 129 130 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \ 131 asmc_mb_sysctl_sms_z 132 133 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \ 134 asmc_mb_sysctl_fanminspeed, \ 135 asmc_mb_sysctl_fanmaxspeed, \ 136 asmc_mb_sysctl_fantargetspeed 137 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \ 138 asmc_mbp_sysctl_light_right, \ 139 asmc_mbp_sysctl_light_control 140 141 struct asmc_model asmc_models[] = { 142 { 143 "MacBook1,1", "Apple SMC MacBook Core Duo", 144 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 145 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS 146 }, 147 148 { 149 "MacBook2,1", "Apple SMC MacBook Core 2 Duo", 150 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 151 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS 152 }, 153 154 { 155 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)", 156 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 157 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 158 }, 159 160 { 161 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)", 162 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 163 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 164 }, 165 166 { 167 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)", 168 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 169 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 170 }, 171 172 { 173 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)", 174 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 175 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 176 }, 177 178 { 179 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)", 180 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 181 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 182 }, 183 184 { 185 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)", 186 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 187 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 188 }, 189 190 { 191 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)", 192 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 193 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS 194 }, 195 196 /* The Mac Mini has no SMS */ 197 { 198 "Macmini1,1", "Apple SMC Mac Mini", 199 NULL, NULL, NULL, 200 ASMC_FAN_FUNCS, 201 NULL, NULL, NULL, 202 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS 203 }, 204 205 /* Idem for the MacPro */ 206 { 207 "MacPro2", "Apple SMC Mac Pro (8-core)", 208 NULL, NULL, NULL, 209 ASMC_FAN_FUNCS, 210 NULL, NULL, NULL, 211 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS 212 }, 213 214 { 215 "MacBookAir1,1", "Apple SMC MacBook Air", 216 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 217 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS 218 }, 219 220 221 { NULL, NULL } 222 }; 223 224 #undef ASMC_SMS_FUNCS 225 #undef ASMC_FAN_FUNCS 226 #undef ASMC_LIGHT_FUNCS 227 228 /* 229 * Driver methods. 230 */ 231 static device_method_t asmc_methods[] = { 232 DEVMETHOD(device_probe, asmc_probe), 233 DEVMETHOD(device_attach, asmc_attach), 234 DEVMETHOD(device_detach, asmc_detach), 235 236 { 0, 0 } 237 }; 238 239 static driver_t asmc_driver = { 240 "asmc", 241 asmc_methods, 242 sizeof(struct asmc_softc) 243 }; 244 245 /* 246 * Debugging 247 */ 248 #define _COMPONENT ACPI_OEM 249 ACPI_MODULE_NAME("ASMC") 250 #ifdef DEBUG 251 #define ASMC_DPRINTF(str) device_printf(dev, str) 252 #else 253 #define ASMC_DPRINTF(str) 254 #endif 255 256 /* NB: can't be const */ 257 static char *asmc_ids[] = { "APP0001", NULL }; 258 259 static devclass_t asmc_devclass; 260 261 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL); 262 MODULE_DEPEND(asmc, acpi, 1, 1, 1); 263 264 static struct asmc_model * 265 asmc_match(device_t dev) 266 { 267 int i; 268 char *model; 269 270 model = getenv("smbios.system.product"); 271 if (model == NULL) 272 return (NULL); 273 274 for (i = 0; asmc_models[i].smc_model; i++) { 275 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) { 276 freeenv(model); 277 return (&asmc_models[i]); 278 } 279 } 280 freeenv(model); 281 282 return (NULL); 283 } 284 285 static int 286 asmc_probe(device_t dev) 287 { 288 struct asmc_model *model; 289 290 if (resource_disabled("asmc", 0)) 291 return (ENXIO); 292 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL) 293 return (ENXIO); 294 295 model = asmc_match(dev); 296 if (!model) { 297 device_printf(dev, "model not recognized\n"); 298 return (ENXIO); 299 } 300 device_set_desc(dev, model->smc_desc); 301 302 return (BUS_PROBE_DEFAULT); 303 } 304 305 static int 306 asmc_attach(device_t dev) 307 { 308 int i, j; 309 int ret; 310 char name[2]; 311 struct asmc_softc *sc = device_get_softc(dev); 312 struct sysctl_ctx_list *sysctlctx; 313 struct sysctl_oid *sysctlnode; 314 struct asmc_model *model; 315 316 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 317 &sc->sc_rid_port, RF_ACTIVE); 318 if (sc->sc_ioport == NULL) { 319 device_printf(dev, "unable to allocate IO port\n"); 320 return (ENOMEM); 321 } 322 323 sysctlctx = device_get_sysctl_ctx(dev); 324 sysctlnode = device_get_sysctl_tree(dev); 325 326 model = asmc_match(dev); 327 328 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN); 329 330 sc->sc_model = model; 331 asmc_init(dev); 332 333 /* 334 * dev.asmc.n.fan.* tree. 335 */ 336 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx, 337 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan", 338 CTLFLAG_RD, 0, "Fan Root Tree"); 339 340 for (i = 1; i <= sc->sc_nfan; i++) { 341 j = i - 1; 342 name[0] = '0' + j; 343 name[1] = 0; 344 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx, 345 SYSCTL_CHILDREN(sc->sc_fan_tree[0]), 346 OID_AUTO, name, CTLFLAG_RD, 0, 347 "Fan Subtree"); 348 349 SYSCTL_ADD_PROC(sysctlctx, 350 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 351 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD, 352 dev, j, model->smc_fan_speed, "I", 353 "Fan speed in RPM"); 354 355 SYSCTL_ADD_PROC(sysctlctx, 356 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 357 OID_AUTO, "safespeed", 358 CTLTYPE_INT | CTLFLAG_RD, 359 dev, j, model->smc_fan_safespeed, "I", 360 "Fan safe speed in RPM"); 361 362 SYSCTL_ADD_PROC(sysctlctx, 363 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 364 OID_AUTO, "minspeed", 365 CTLTYPE_INT | CTLFLAG_RD, 366 dev, j, model->smc_fan_minspeed, "I", 367 "Fan minimum speed in RPM"); 368 369 SYSCTL_ADD_PROC(sysctlctx, 370 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 371 OID_AUTO, "maxspeed", 372 CTLTYPE_INT | CTLFLAG_RD, 373 dev, j, model->smc_fan_maxspeed, "I", 374 "Fan maximum speed in RPM"); 375 376 SYSCTL_ADD_PROC(sysctlctx, 377 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 378 OID_AUTO, "targetspeed", 379 CTLTYPE_INT | CTLFLAG_RD, 380 dev, j, model->smc_fan_targetspeed, "I", 381 "Fan target speed in RPM"); 382 } 383 384 /* 385 * dev.asmc.n.temp tree. 386 */ 387 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx, 388 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp", 389 CTLFLAG_RD, 0, "Temperature sensors"); 390 391 for (i = 0; model->smc_temps[i]; i++) { 392 SYSCTL_ADD_PROC(sysctlctx, 393 SYSCTL_CHILDREN(sc->sc_temp_tree), 394 OID_AUTO, model->smc_tempnames[i], 395 CTLTYPE_INT | CTLFLAG_RD, 396 dev, i, asmc_temp_sysctl, "I", 397 model->smc_tempdescs[i]); 398 } 399 400 /* 401 * dev.asmc.n.light 402 */ 403 if (model->smc_light_left) { 404 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx, 405 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light", 406 CTLFLAG_RD, 0, "Keyboard backlight sensors"); 407 408 SYSCTL_ADD_PROC(sysctlctx, 409 SYSCTL_CHILDREN(sc->sc_light_tree), 410 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD, 411 dev, 0, model->smc_light_left, "I", 412 "Keyboard backlight left sensor"); 413 414 SYSCTL_ADD_PROC(sysctlctx, 415 SYSCTL_CHILDREN(sc->sc_light_tree), 416 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD, 417 dev, 0, model->smc_light_right, "I", 418 "Keyboard backlight right sensor"); 419 420 SYSCTL_ADD_PROC(sysctlctx, 421 SYSCTL_CHILDREN(sc->sc_light_tree), 422 OID_AUTO, "control", 423 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY, 424 dev, 0, model->smc_light_control, "I", 425 "Keyboard backlight brightness control"); 426 } 427 428 if (model->smc_sms_x == NULL) 429 goto nosms; 430 431 /* 432 * dev.asmc.n.sms tree. 433 */ 434 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx, 435 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms", 436 CTLFLAG_RD, 0, "Sudden Motion Sensor"); 437 438 SYSCTL_ADD_PROC(sysctlctx, 439 SYSCTL_CHILDREN(sc->sc_sms_tree), 440 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD, 441 dev, 0, model->smc_sms_x, "I", 442 "Sudden Motion Sensor X value"); 443 444 SYSCTL_ADD_PROC(sysctlctx, 445 SYSCTL_CHILDREN(sc->sc_sms_tree), 446 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD, 447 dev, 0, model->smc_sms_y, "I", 448 "Sudden Motion Sensor Y value"); 449 450 SYSCTL_ADD_PROC(sysctlctx, 451 SYSCTL_CHILDREN(sc->sc_sms_tree), 452 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD, 453 dev, 0, model->smc_sms_z, "I", 454 "Sudden Motion Sensor Z value"); 455 456 /* 457 * Need a taskqueue to send devctl_notify() events 458 * when the SMS interrupt us. 459 * 460 * PI_REALTIME is used due to the sensitivity of the 461 * interrupt. An interrupt from the SMS means that the 462 * disk heads should be turned off as quickly as possible. 463 * 464 * We only need to do this for the non INTR_FILTER case. 465 */ 466 sc->sc_sms_tq = NULL; 467 #ifndef INTR_FILTER 468 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc); 469 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK, 470 taskqueue_thread_enqueue, &sc->sc_sms_tq); 471 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq", 472 device_get_nameunit(dev)); 473 #endif 474 /* 475 * Allocate an IRQ for the SMS. 476 */ 477 sc->sc_rid_irq = 0; 478 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 479 &sc->sc_rid_irq, RF_ACTIVE); 480 if (sc->sc_irq == NULL) { 481 device_printf(dev, "unable to allocate IRQ resource\n"); 482 ret = ENXIO; 483 goto err2; 484 } 485 486 ret = bus_setup_intr(dev, sc->sc_irq, 487 INTR_TYPE_MISC | INTR_MPSAFE, 488 #ifdef INTR_FILTER 489 asmc_sms_intrfast, asmc_sms_handler, 490 #else 491 asmc_sms_intrfast, NULL, 492 #endif 493 dev, &sc->sc_cookie); 494 495 if (ret) { 496 device_printf(dev, "unable to setup SMS IRQ\n"); 497 goto err1; 498 } 499 nosms: 500 return (0); 501 err1: 502 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq); 503 err2: 504 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port, 505 sc->sc_ioport); 506 mtx_destroy(&sc->sc_mtx); 507 if (sc->sc_sms_tq) 508 taskqueue_free(sc->sc_sms_tq); 509 510 return (ret); 511 } 512 513 static int 514 asmc_detach(device_t dev) 515 { 516 struct asmc_softc *sc = device_get_softc(dev); 517 518 if (sc->sc_sms_tq) { 519 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task); 520 taskqueue_free(sc->sc_sms_tq); 521 } 522 if (sc->sc_cookie) 523 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie); 524 if (sc->sc_irq) 525 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, 526 sc->sc_irq); 527 if (sc->sc_ioport) 528 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port, 529 sc->sc_ioport); 530 mtx_destroy(&sc->sc_mtx); 531 532 return (0); 533 } 534 535 static int 536 asmc_init(device_t dev) 537 { 538 struct asmc_softc *sc = device_get_softc(dev); 539 int i, error = 1; 540 uint8_t buf[4]; 541 542 if (sc->sc_model->smc_sms_x == NULL) 543 goto nosms; 544 545 /* 546 * We are ready to recieve interrupts from the SMS. 547 */ 548 buf[0] = 0x01; 549 ASMC_DPRINTF(("intok key\n")); 550 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1); 551 DELAY(50); 552 553 /* 554 * Initiate the polling intervals. 555 */ 556 buf[0] = 20; /* msecs */ 557 ASMC_DPRINTF(("low int key\n")); 558 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1); 559 DELAY(200); 560 561 buf[0] = 20; /* msecs */ 562 ASMC_DPRINTF(("high int key\n")); 563 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1); 564 DELAY(200); 565 566 buf[0] = 0x00; 567 buf[1] = 0x60; 568 ASMC_DPRINTF(("sms low key\n")); 569 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2); 570 DELAY(200); 571 572 buf[0] = 0x01; 573 buf[1] = 0xc0; 574 ASMC_DPRINTF(("sms high key\n")); 575 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2); 576 DELAY(200); 577 578 /* 579 * I'm not sure what this key does, but it seems to be 580 * required. 581 */ 582 buf[0] = 0x01; 583 ASMC_DPRINTF(("sms flag key\n")); 584 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1); 585 DELAY(100); 586 587 /* 588 * Wait up to 5 seconds for SMS initialization. 589 */ 590 for (i = 0; i < 10000; i++) { 591 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 && 592 (buf[0] != 0x00 || buf[1] != 0x00)) { 593 error = 0; 594 goto out; 595 } 596 buf[0] = ASMC_SMS_INIT1; 597 buf[1] = ASMC_SMS_INIT2; 598 ASMC_DPRINTF(("sms key\n")); 599 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2); 600 DELAY(50); 601 } 602 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n"); 603 604 out: 605 asmc_sms_calibrate(dev); 606 nosms: 607 sc->sc_nfan = asmc_fan_count(dev); 608 if (sc->sc_nfan > ASMC_MAXFANS) { 609 device_printf(dev, "more than %d fans were detected. Please " 610 "report this.\n", ASMC_MAXFANS); 611 sc->sc_nfan = ASMC_MAXFANS; 612 } 613 614 if (bootverbose) { 615 /* 616 * XXX: The number of keys is a 32 bit buffer, but 617 * right now Apple only uses the last 8 bit. 618 */ 619 asmc_key_read(dev, ASMC_NKEYS, buf, 4); 620 device_printf(dev, "number of keys: %d\n", buf[3]); 621 } 622 623 return (error); 624 } 625 626 /* 627 * We need to make sure that the SMC acks the byte sent. 628 * Just wait up to (amount * 10) ms. 629 */ 630 static int 631 asmc_wait_ack(device_t dev, uint8_t val, int amount) 632 { 633 struct asmc_softc *sc = device_get_softc(dev); 634 u_int i; 635 636 val = val & ASMC_STATUS_MASK; 637 638 for (i = 0; i < amount; i++) { 639 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val) 640 return (0); 641 DELAY(10); 642 } 643 644 return (1); 645 } 646 647 /* 648 * We need to make sure that the SMC acks the byte sent. 649 * Just wait up to 100 ms. 650 */ 651 static int 652 asmc_wait(device_t dev, uint8_t val) 653 { 654 struct asmc_softc *sc; 655 656 if (asmc_wait_ack(dev, val, 1000) == 0) 657 return (0); 658 659 sc = device_get_softc(dev); 660 val = val & ASMC_STATUS_MASK; 661 662 #ifdef DEBUG 663 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val, 664 ASMC_CMDPORT_READ(sc)); 665 #endif 666 return (1); 667 } 668 669 /* 670 * Send the given command, retrying up to 10 times if 671 * the acknowledgement fails. 672 */ 673 static int 674 asmc_command(device_t dev, uint8_t command) { 675 676 int i; 677 struct asmc_softc *sc = device_get_softc(dev); 678 679 for (i=0; i < 10; i++) { 680 ASMC_CMDPORT_WRITE(sc, command); 681 if (asmc_wait_ack(dev, 0x0c, 100) == 0) { 682 return (0); 683 } 684 } 685 686 #ifdef DEBUG 687 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command, 688 ASMC_CMDPORT_READ(sc)); 689 #endif 690 return (1); 691 } 692 693 static int 694 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len) 695 { 696 int i, error = 1, try = 0; 697 struct asmc_softc *sc = device_get_softc(dev); 698 699 mtx_lock_spin(&sc->sc_mtx); 700 701 begin: 702 if (asmc_command(dev, ASMC_CMDREAD)) 703 goto out; 704 705 for (i = 0; i < 4; i++) { 706 ASMC_DATAPORT_WRITE(sc, key[i]); 707 if (asmc_wait(dev, 0x04)) 708 goto out; 709 } 710 711 ASMC_DATAPORT_WRITE(sc, len); 712 713 for (i = 0; i < len; i++) { 714 if (asmc_wait(dev, 0x05)) 715 goto out; 716 buf[i] = ASMC_DATAPORT_READ(sc); 717 } 718 719 error = 0; 720 out: 721 if (error) { 722 if (++try < 10) goto begin; 723 device_printf(dev,"%s for key %s failed %d times, giving up\n", 724 __func__, key, try); 725 } 726 727 mtx_unlock_spin(&sc->sc_mtx); 728 729 return (error); 730 } 731 732 static int 733 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len) 734 { 735 int i, error = -1, try = 0; 736 struct asmc_softc *sc = device_get_softc(dev); 737 738 mtx_lock_spin(&sc->sc_mtx); 739 740 begin: 741 ASMC_DPRINTF(("cmd port: cmd write\n")); 742 if (asmc_command(dev, ASMC_CMDWRITE)) 743 goto out; 744 745 ASMC_DPRINTF(("data port: key\n")); 746 for (i = 0; i < 4; i++) { 747 ASMC_DATAPORT_WRITE(sc, key[i]); 748 if (asmc_wait(dev, 0x04)) 749 goto out; 750 } 751 ASMC_DPRINTF(("data port: length\n")); 752 ASMC_DATAPORT_WRITE(sc, len); 753 754 ASMC_DPRINTF(("data port: buffer\n")); 755 for (i = 0; i < len; i++) { 756 if (asmc_wait(dev, 0x04)) 757 goto out; 758 ASMC_DATAPORT_WRITE(sc, buf[i]); 759 } 760 761 error = 0; 762 out: 763 if (error) { 764 if (++try < 10) goto begin; 765 device_printf(dev,"%s for key %s failed %d times, giving up\n", 766 __func__, key, try); 767 } 768 769 mtx_unlock_spin(&sc->sc_mtx); 770 771 return (error); 772 773 } 774 775 /* 776 * Fan control functions. 777 */ 778 static int 779 asmc_fan_count(device_t dev) 780 { 781 uint8_t buf[1]; 782 783 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0) 784 return (-1); 785 786 return (buf[0]); 787 } 788 789 static int 790 asmc_fan_getvalue(device_t dev, const char *key, int fan) 791 { 792 int speed; 793 uint8_t buf[2]; 794 char fankey[5]; 795 796 snprintf(fankey, sizeof(fankey), key, fan); 797 if (asmc_key_read(dev, fankey, buf, 2) < 0) 798 return (-1); 799 speed = (buf[0] << 6) | (buf[1] >> 2); 800 801 return (speed); 802 } 803 804 static int 805 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS) 806 { 807 device_t dev = (device_t) arg1; 808 int fan = arg2; 809 int error; 810 int32_t v; 811 812 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan); 813 error = sysctl_handle_int(oidp, &v, 0, req); 814 815 return (error); 816 } 817 818 static int 819 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS) 820 { 821 device_t dev = (device_t) arg1; 822 int fan = arg2; 823 int error; 824 int32_t v; 825 826 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan); 827 error = sysctl_handle_int(oidp, &v, 0, req); 828 829 return (error); 830 } 831 832 833 static int 834 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS) 835 { 836 device_t dev = (device_t) arg1; 837 int fan = arg2; 838 int error; 839 int32_t v; 840 841 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan); 842 error = sysctl_handle_int(oidp, &v, 0, req); 843 844 return (error); 845 } 846 847 static int 848 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS) 849 { 850 device_t dev = (device_t) arg1; 851 int fan = arg2; 852 int error; 853 int32_t v; 854 855 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan); 856 error = sysctl_handle_int(oidp, &v, 0, req); 857 858 return (error); 859 } 860 861 static int 862 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS) 863 { 864 device_t dev = (device_t) arg1; 865 int fan = arg2; 866 int error; 867 int32_t v; 868 869 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan); 870 error = sysctl_handle_int(oidp, &v, 0, req); 871 872 return (error); 873 } 874 875 /* 876 * Temperature functions. 877 */ 878 static int 879 asmc_temp_getvalue(device_t dev, const char *key) 880 { 881 uint8_t buf[2]; 882 883 /* 884 * Check for invalid temperatures. 885 */ 886 if (asmc_key_read(dev, key, buf, 2) < 0) 887 return (-1); 888 889 return (buf[0]); 890 } 891 892 static int 893 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS) 894 { 895 device_t dev = (device_t) arg1; 896 struct asmc_softc *sc = device_get_softc(dev); 897 int error, val; 898 899 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]); 900 error = sysctl_handle_int(oidp, &val, 0, req); 901 902 return (error); 903 } 904 905 /* 906 * Sudden Motion Sensor functions. 907 */ 908 static int 909 asmc_sms_read(device_t dev, const char *key, int16_t *val) 910 { 911 uint8_t buf[2]; 912 int error; 913 914 /* no need to do locking here as asmc_key_read() already does it */ 915 switch (key[3]) { 916 case 'X': 917 case 'Y': 918 case 'Z': 919 error = asmc_key_read(dev, key, buf, 2); 920 break; 921 default: 922 device_printf(dev, "%s called with invalid argument %s\n", 923 __func__, key); 924 error = 1; 925 goto out; 926 } 927 *val = ((int16_t)buf[0] << 8) | buf[1]; 928 out: 929 return (error); 930 } 931 932 static void 933 asmc_sms_calibrate(device_t dev) 934 { 935 struct asmc_softc *sc = device_get_softc(dev); 936 937 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x); 938 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y); 939 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z); 940 } 941 942 static int 943 asmc_sms_intrfast(void *arg) 944 { 945 uint8_t type; 946 device_t dev = (device_t) arg; 947 struct asmc_softc *sc = device_get_softc(dev); 948 949 mtx_lock_spin(&sc->sc_mtx); 950 type = ASMC_INTPORT_READ(sc); 951 mtx_unlock_spin(&sc->sc_mtx); 952 953 sc->sc_sms_intrtype = type; 954 asmc_sms_printintr(dev, type); 955 956 #ifdef INTR_FILTER 957 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED); 958 #else 959 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task); 960 #endif 961 return (FILTER_HANDLED); 962 } 963 964 #ifdef INTR_FILTER 965 static void 966 asmc_sms_handler(void *arg) 967 { 968 struct asmc_softc *sc = device_get_softc(arg); 969 970 asmc_sms_task(sc, 0); 971 } 972 #endif 973 974 975 static void 976 asmc_sms_printintr(device_t dev, uint8_t type) 977 { 978 979 switch (type) { 980 case ASMC_SMS_INTFF: 981 device_printf(dev, "WARNING: possible free fall!\n"); 982 break; 983 case ASMC_SMS_INTHA: 984 device_printf(dev, "WARNING: high acceleration detected!\n"); 985 break; 986 case ASMC_SMS_INTSH: 987 device_printf(dev, "WARNING: possible shock!\n"); 988 break; 989 default: 990 device_printf(dev, "%s unknown interrupt\n", __func__); 991 } 992 } 993 994 static void 995 asmc_sms_task(void *arg, int pending) 996 { 997 struct asmc_softc *sc = (struct asmc_softc *)arg; 998 char notify[16]; 999 int type; 1000 1001 switch (sc->sc_sms_intrtype) { 1002 case ASMC_SMS_INTFF: 1003 type = 2; 1004 break; 1005 case ASMC_SMS_INTHA: 1006 type = 1; 1007 break; 1008 case ASMC_SMS_INTSH: 1009 type = 0; 1010 break; 1011 default: 1012 type = 255; 1013 } 1014 1015 snprintf(notify, sizeof(notify), " notify=0x%x", type); 1016 devctl_notify("ACPI", "asmc", "SMS", notify); 1017 } 1018 1019 static int 1020 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS) 1021 { 1022 device_t dev = (device_t) arg1; 1023 int error; 1024 int16_t val; 1025 int32_t v; 1026 1027 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val); 1028 v = (int32_t) val; 1029 error = sysctl_handle_int(oidp, &v, 0, req); 1030 1031 return (error); 1032 } 1033 1034 static int 1035 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS) 1036 { 1037 device_t dev = (device_t) arg1; 1038 int error; 1039 int16_t val; 1040 int32_t v; 1041 1042 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val); 1043 v = (int32_t) val; 1044 error = sysctl_handle_int(oidp, &v, 0, req); 1045 1046 return (error); 1047 } 1048 1049 static int 1050 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS) 1051 { 1052 device_t dev = (device_t) arg1; 1053 int error; 1054 int16_t val; 1055 int32_t v; 1056 1057 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val); 1058 v = (int32_t) val; 1059 error = sysctl_handle_int(oidp, &v, sizeof(v), req); 1060 1061 return (error); 1062 } 1063 1064 static int 1065 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS) 1066 { 1067 device_t dev = (device_t) arg1; 1068 uint8_t buf[6]; 1069 int error; 1070 int32_t v; 1071 1072 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6); 1073 v = buf[2]; 1074 error = sysctl_handle_int(oidp, &v, sizeof(v), req); 1075 1076 return (error); 1077 } 1078 1079 static int 1080 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS) 1081 { 1082 device_t dev = (device_t) arg1; 1083 uint8_t buf[6]; 1084 int error; 1085 int32_t v; 1086 1087 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6); 1088 v = buf[2]; 1089 error = sysctl_handle_int(oidp, &v, sizeof(v), req); 1090 1091 return (error); 1092 } 1093 1094 static int 1095 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS) 1096 { 1097 device_t dev = (device_t) arg1; 1098 uint8_t buf[2]; 1099 int error; 1100 unsigned int level; 1101 static int32_t v; 1102 1103 error = sysctl_handle_int(oidp, &v, sizeof(v), req); 1104 if (error == 0 && req->newptr != NULL) { 1105 level = *(unsigned int *)req->newptr; 1106 if (level > 255) 1107 return (EINVAL); 1108 v = level; 1109 buf[0] = level; 1110 buf[1] = 0x00; 1111 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2); 1112 } 1113 1114 return (error); 1115 } 1116