1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org> 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 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 /* 31 * Driver for Apple's System Management Console (SMC). 32 * SMC can be found on the MacBook, MacBook Pro and Mac Mini. 33 * 34 * Inspired by the Linux applesmc driver. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/conf.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/module.h> 47 #include <sys/mutex.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 #include <sys/taskqueue.h> 51 #include <sys/rman.h> 52 53 #include <machine/resource.h> 54 55 #include <contrib/dev/acpica/include/acpi.h> 56 57 #include <dev/acpica/acpivar.h> 58 #include <dev/asmc/asmcvar.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 static int asmc_resume(device_t dev); 67 68 /* 69 * SMC functions. 70 */ 71 static int asmc_init(device_t dev); 72 static int asmc_command(device_t dev, uint8_t command); 73 static int asmc_wait(device_t dev, uint8_t val); 74 static int asmc_wait_ack(device_t dev, uint8_t val, int amount); 75 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf, 76 uint8_t len); 77 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf, 78 uint8_t); 79 static int asmc_fan_count(device_t dev); 80 static int asmc_fan_getvalue(device_t dev, const char *key, int fan); 81 static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed); 82 static int asmc_temp_getvalue(device_t dev, const char *key); 83 static int asmc_sms_read(device_t, const char *key, int16_t *val); 84 static void asmc_sms_calibrate(device_t dev); 85 static int asmc_sms_intrfast(void *arg); 86 static void asmc_sms_printintr(device_t dev, uint8_t); 87 static void asmc_sms_task(void *arg, int pending); 88 #ifdef DEBUG 89 void asmc_dumpall(device_t); 90 static int asmc_key_dump(device_t, int); 91 #endif 92 93 /* 94 * Model functions. 95 */ 96 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS); 97 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS); 98 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS); 99 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS); 100 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS); 101 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS); 102 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS); 103 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS); 104 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS); 105 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS); 106 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS); 107 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS); 108 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS); 109 110 struct asmc_model { 111 const char *smc_model; /* smbios.system.product env var. */ 112 const char *smc_desc; /* driver description */ 113 114 /* Helper functions */ 115 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS); 116 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS); 117 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS); 118 int (*smc_fan_id)(SYSCTL_HANDLER_ARGS); 119 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS); 120 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS); 121 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS); 122 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS); 123 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS); 124 int (*smc_light_left)(SYSCTL_HANDLER_ARGS); 125 int (*smc_light_right)(SYSCTL_HANDLER_ARGS); 126 int (*smc_light_control)(SYSCTL_HANDLER_ARGS); 127 128 const char *smc_temps[ASMC_TEMP_MAX]; 129 const char *smc_tempnames[ASMC_TEMP_MAX]; 130 const char *smc_tempdescs[ASMC_TEMP_MAX]; 131 }; 132 133 static struct asmc_model *asmc_match(device_t dev); 134 135 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \ 136 asmc_mb_sysctl_sms_z 137 138 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL 139 140 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \ 141 asmc_mb_sysctl_fanminspeed, \ 142 asmc_mb_sysctl_fanmaxspeed, \ 143 asmc_mb_sysctl_fantargetspeed 144 145 #define ASMC_FAN_FUNCS2 asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \ 146 asmc_mb_sysctl_fanminspeed, \ 147 asmc_mb_sysctl_fanmaxspeed, \ 148 asmc_mb_sysctl_fantargetspeed 149 150 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \ 151 asmc_mbp_sysctl_light_right, \ 152 asmc_mbp_sysctl_light_control 153 154 struct asmc_model asmc_models[] = { 155 { 156 "MacBook1,1", "Apple SMC MacBook Core Duo", 157 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 158 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS 159 }, 160 161 { 162 "MacBook2,1", "Apple SMC MacBook Core 2 Duo", 163 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 164 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS 165 }, 166 167 { 168 "MacBook3,1", "Apple SMC MacBook Core 2 Duo", 169 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 170 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS 171 }, 172 173 { 174 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)", 175 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 176 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 177 }, 178 179 { 180 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)", 181 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 182 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 183 }, 184 185 { 186 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)", 187 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 188 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 189 }, 190 191 { 192 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)", 193 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 194 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 195 }, 196 197 { 198 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)", 199 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 200 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 201 }, 202 203 { 204 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)", 205 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 206 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS 207 }, 208 209 { 210 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)", 211 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 212 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS 213 }, 214 215 { 216 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)", 217 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 218 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS 219 }, 220 221 { 222 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)", 223 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 224 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS 225 }, 226 227 { 228 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)", 229 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS, 230 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS 231 }, 232 233 { 234 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)", 235 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS, 236 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS 237 }, 238 239 /* The Mac Mini has no SMS */ 240 { 241 "Macmini1,1", "Apple SMC Mac Mini", 242 NULL, NULL, NULL, 243 ASMC_FAN_FUNCS, 244 NULL, NULL, NULL, 245 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS 246 }, 247 248 /* The Mac Mini 3,1 has no SMS */ 249 { 250 "Macmini3,1", "Apple SMC Mac Mini 3,1", 251 NULL, NULL, NULL, 252 ASMC_FAN_FUNCS, 253 NULL, NULL, NULL, 254 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS 255 }, 256 257 /* Idem for the MacPro */ 258 { 259 "MacPro2", "Apple SMC Mac Pro (8-core)", 260 NULL, NULL, NULL, 261 ASMC_FAN_FUNCS, 262 NULL, NULL, NULL, 263 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS 264 }, 265 266 /* Idem for the MacPro 2010*/ 267 { 268 "MacPro5,1", "Apple SMC MacPro (2010)", 269 NULL, NULL, NULL, 270 ASMC_FAN_FUNCS, 271 NULL, NULL, NULL, 272 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS 273 }, 274 275 { 276 "MacBookAir1,1", "Apple SMC MacBook Air", 277 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 278 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS 279 }, 280 281 { 282 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)", 283 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL, 284 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS 285 }, 286 287 { 288 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)", 289 ASMC_SMS_FUNCS_DISABLED, 290 ASMC_FAN_FUNCS2, 291 ASMC_LIGHT_FUNCS, 292 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS 293 }, 294 295 { 296 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)", 297 ASMC_SMS_FUNCS_DISABLED, 298 ASMC_FAN_FUNCS2, 299 ASMC_LIGHT_FUNCS, 300 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS 301 }, 302 303 { 304 "MacBookAir7,1", "Apple SMC MacBook Air 11-inch (Early 2015)", 305 ASMC_SMS_FUNCS_DISABLED, 306 ASMC_FAN_FUNCS2, 307 ASMC_LIGHT_FUNCS, 308 ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS 309 }, 310 311 { 312 "MacBookAir7,2", "Apple SMC MacBook Air 13-inch (Early 2015)", 313 ASMC_SMS_FUNCS_DISABLED, 314 ASMC_FAN_FUNCS2, 315 ASMC_LIGHT_FUNCS, 316 ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS 317 }, 318 319 { NULL, NULL } 320 }; 321 322 #undef ASMC_SMS_FUNCS 323 #undef ASMC_SMS_FUNCS_DISABLED 324 #undef ASMC_FAN_FUNCS 325 #undef ASMC_FAN_FUNCS2 326 #undef ASMC_LIGHT_FUNCS 327 328 /* 329 * Driver methods. 330 */ 331 static device_method_t asmc_methods[] = { 332 DEVMETHOD(device_probe, asmc_probe), 333 DEVMETHOD(device_attach, asmc_attach), 334 DEVMETHOD(device_detach, asmc_detach), 335 DEVMETHOD(device_resume, asmc_resume), 336 337 { 0, 0 } 338 }; 339 340 static driver_t asmc_driver = { 341 "asmc", 342 asmc_methods, 343 sizeof(struct asmc_softc) 344 }; 345 346 /* 347 * Debugging 348 */ 349 #define _COMPONENT ACPI_OEM 350 ACPI_MODULE_NAME("ASMC") 351 #ifdef DEBUG 352 #define ASMC_DPRINTF(str) device_printf(dev, str) 353 #else 354 #define ASMC_DPRINTF(str) 355 #endif 356 357 /* NB: can't be const */ 358 static char *asmc_ids[] = { "APP0001", NULL }; 359 360 static devclass_t asmc_devclass; 361 362 static unsigned int light_control = 0; 363 364 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL); 365 MODULE_DEPEND(asmc, acpi, 1, 1, 1); 366 367 static struct asmc_model * 368 asmc_match(device_t dev) 369 { 370 int i; 371 char *model; 372 373 model = kern_getenv("smbios.system.product"); 374 if (model == NULL) 375 return (NULL); 376 377 for (i = 0; asmc_models[i].smc_model; i++) { 378 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) { 379 freeenv(model); 380 return (&asmc_models[i]); 381 } 382 } 383 freeenv(model); 384 385 return (NULL); 386 } 387 388 static int 389 asmc_probe(device_t dev) 390 { 391 struct asmc_model *model; 392 int rv; 393 394 if (resource_disabled("asmc", 0)) 395 return (ENXIO); 396 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL); 397 if (rv > 0) 398 return (rv); 399 400 model = asmc_match(dev); 401 if (!model) { 402 device_printf(dev, "model not recognized\n"); 403 return (ENXIO); 404 } 405 device_set_desc(dev, model->smc_desc); 406 407 return (rv); 408 } 409 410 static int 411 asmc_attach(device_t dev) 412 { 413 int i, j; 414 int ret; 415 char name[2]; 416 struct asmc_softc *sc = device_get_softc(dev); 417 struct sysctl_ctx_list *sysctlctx; 418 struct sysctl_oid *sysctlnode; 419 struct asmc_model *model; 420 421 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 422 &sc->sc_rid_port, RF_ACTIVE); 423 if (sc->sc_ioport == NULL) { 424 device_printf(dev, "unable to allocate IO port\n"); 425 return (ENOMEM); 426 } 427 428 sysctlctx = device_get_sysctl_ctx(dev); 429 sysctlnode = device_get_sysctl_tree(dev); 430 431 model = asmc_match(dev); 432 433 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN); 434 435 sc->sc_model = model; 436 asmc_init(dev); 437 438 /* 439 * dev.asmc.n.fan.* tree. 440 */ 441 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx, 442 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan", 443 CTLFLAG_RD, 0, "Fan Root Tree"); 444 445 for (i = 1; i <= sc->sc_nfan; i++) { 446 j = i - 1; 447 name[0] = '0' + j; 448 name[1] = 0; 449 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx, 450 SYSCTL_CHILDREN(sc->sc_fan_tree[0]), 451 OID_AUTO, name, CTLFLAG_RD, 0, 452 "Fan Subtree"); 453 454 SYSCTL_ADD_PROC(sysctlctx, 455 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 456 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD, 457 dev, j, model->smc_fan_id, "I", 458 "Fan ID"); 459 460 SYSCTL_ADD_PROC(sysctlctx, 461 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 462 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD, 463 dev, j, model->smc_fan_speed, "I", 464 "Fan speed in RPM"); 465 466 SYSCTL_ADD_PROC(sysctlctx, 467 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 468 OID_AUTO, "safespeed", 469 CTLTYPE_INT | CTLFLAG_RD, 470 dev, j, model->smc_fan_safespeed, "I", 471 "Fan safe speed in RPM"); 472 473 SYSCTL_ADD_PROC(sysctlctx, 474 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 475 OID_AUTO, "minspeed", 476 CTLTYPE_INT | CTLFLAG_RW, 477 dev, j, model->smc_fan_minspeed, "I", 478 "Fan minimum speed in RPM"); 479 480 SYSCTL_ADD_PROC(sysctlctx, 481 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 482 OID_AUTO, "maxspeed", 483 CTLTYPE_INT | CTLFLAG_RW, 484 dev, j, model->smc_fan_maxspeed, "I", 485 "Fan maximum speed in RPM"); 486 487 SYSCTL_ADD_PROC(sysctlctx, 488 SYSCTL_CHILDREN(sc->sc_fan_tree[i]), 489 OID_AUTO, "targetspeed", 490 CTLTYPE_INT | CTLFLAG_RW, 491 dev, j, model->smc_fan_targetspeed, "I", 492 "Fan target speed in RPM"); 493 } 494 495 /* 496 * dev.asmc.n.temp tree. 497 */ 498 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx, 499 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp", 500 CTLFLAG_RD, 0, "Temperature sensors"); 501 502 for (i = 0; model->smc_temps[i]; i++) { 503 SYSCTL_ADD_PROC(sysctlctx, 504 SYSCTL_CHILDREN(sc->sc_temp_tree), 505 OID_AUTO, model->smc_tempnames[i], 506 CTLTYPE_INT | CTLFLAG_RD, 507 dev, i, asmc_temp_sysctl, "I", 508 model->smc_tempdescs[i]); 509 } 510 511 /* 512 * dev.asmc.n.light 513 */ 514 if (model->smc_light_left) { 515 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx, 516 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light", 517 CTLFLAG_RD, 0, "Keyboard backlight sensors"); 518 519 SYSCTL_ADD_PROC(sysctlctx, 520 SYSCTL_CHILDREN(sc->sc_light_tree), 521 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD, 522 dev, 0, model->smc_light_left, "I", 523 "Keyboard backlight left sensor"); 524 525 SYSCTL_ADD_PROC(sysctlctx, 526 SYSCTL_CHILDREN(sc->sc_light_tree), 527 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD, 528 dev, 0, model->smc_light_right, "I", 529 "Keyboard backlight right sensor"); 530 531 SYSCTL_ADD_PROC(sysctlctx, 532 SYSCTL_CHILDREN(sc->sc_light_tree), 533 OID_AUTO, "control", 534 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY, 535 dev, 0, model->smc_light_control, "I", 536 "Keyboard backlight brightness control"); 537 } 538 539 if (model->smc_sms_x == NULL) 540 goto nosms; 541 542 /* 543 * dev.asmc.n.sms tree. 544 */ 545 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx, 546 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms", 547 CTLFLAG_RD, 0, "Sudden Motion Sensor"); 548 549 SYSCTL_ADD_PROC(sysctlctx, 550 SYSCTL_CHILDREN(sc->sc_sms_tree), 551 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD, 552 dev, 0, model->smc_sms_x, "I", 553 "Sudden Motion Sensor X value"); 554 555 SYSCTL_ADD_PROC(sysctlctx, 556 SYSCTL_CHILDREN(sc->sc_sms_tree), 557 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD, 558 dev, 0, model->smc_sms_y, "I", 559 "Sudden Motion Sensor Y value"); 560 561 SYSCTL_ADD_PROC(sysctlctx, 562 SYSCTL_CHILDREN(sc->sc_sms_tree), 563 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD, 564 dev, 0, model->smc_sms_z, "I", 565 "Sudden Motion Sensor Z value"); 566 567 /* 568 * Need a taskqueue to send devctl_notify() events 569 * when the SMS interrupt us. 570 * 571 * PI_REALTIME is used due to the sensitivity of the 572 * interrupt. An interrupt from the SMS means that the 573 * disk heads should be turned off as quickly as possible. 574 * 575 * We only need to do this for the non INTR_FILTER case. 576 */ 577 sc->sc_sms_tq = NULL; 578 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc); 579 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK, 580 taskqueue_thread_enqueue, &sc->sc_sms_tq); 581 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq", 582 device_get_nameunit(dev)); 583 /* 584 * Allocate an IRQ for the SMS. 585 */ 586 sc->sc_rid_irq = 0; 587 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 588 &sc->sc_rid_irq, RF_ACTIVE); 589 if (sc->sc_irq == NULL) { 590 device_printf(dev, "unable to allocate IRQ resource\n"); 591 ret = ENXIO; 592 goto err2; 593 } 594 595 ret = bus_setup_intr(dev, sc->sc_irq, 596 INTR_TYPE_MISC | INTR_MPSAFE, 597 asmc_sms_intrfast, NULL, 598 dev, &sc->sc_cookie); 599 600 if (ret) { 601 device_printf(dev, "unable to setup SMS IRQ\n"); 602 goto err1; 603 } 604 nosms: 605 return (0); 606 err1: 607 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq); 608 err2: 609 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port, 610 sc->sc_ioport); 611 mtx_destroy(&sc->sc_mtx); 612 if (sc->sc_sms_tq) 613 taskqueue_free(sc->sc_sms_tq); 614 615 return (ret); 616 } 617 618 static int 619 asmc_detach(device_t dev) 620 { 621 struct asmc_softc *sc = device_get_softc(dev); 622 623 if (sc->sc_sms_tq) { 624 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task); 625 taskqueue_free(sc->sc_sms_tq); 626 } 627 if (sc->sc_cookie) 628 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie); 629 if (sc->sc_irq) 630 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, 631 sc->sc_irq); 632 if (sc->sc_ioport) 633 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port, 634 sc->sc_ioport); 635 mtx_destroy(&sc->sc_mtx); 636 637 return (0); 638 } 639 640 static int 641 asmc_resume(device_t dev) 642 { 643 uint8_t buf[2]; 644 buf[0] = light_control; 645 buf[1] = 0x00; 646 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf); 647 return (0); 648 } 649 650 651 #ifdef DEBUG 652 void asmc_dumpall(device_t dev) 653 { 654 int i; 655 656 /* XXX magic number */ 657 for (i=0; i < 0x100; i++) 658 asmc_key_dump(dev, i); 659 } 660 #endif 661 662 static int 663 asmc_init(device_t dev) 664 { 665 struct asmc_softc *sc = device_get_softc(dev); 666 int i, error = 1; 667 uint8_t buf[4]; 668 669 if (sc->sc_model->smc_sms_x == NULL) 670 goto nosms; 671 672 /* 673 * We are ready to receive interrupts from the SMS. 674 */ 675 buf[0] = 0x01; 676 ASMC_DPRINTF(("intok key\n")); 677 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1); 678 DELAY(50); 679 680 /* 681 * Initiate the polling intervals. 682 */ 683 buf[0] = 20; /* msecs */ 684 ASMC_DPRINTF(("low int key\n")); 685 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1); 686 DELAY(200); 687 688 buf[0] = 20; /* msecs */ 689 ASMC_DPRINTF(("high int key\n")); 690 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1); 691 DELAY(200); 692 693 buf[0] = 0x00; 694 buf[1] = 0x60; 695 ASMC_DPRINTF(("sms low key\n")); 696 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2); 697 DELAY(200); 698 699 buf[0] = 0x01; 700 buf[1] = 0xc0; 701 ASMC_DPRINTF(("sms high key\n")); 702 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2); 703 DELAY(200); 704 705 /* 706 * I'm not sure what this key does, but it seems to be 707 * required. 708 */ 709 buf[0] = 0x01; 710 ASMC_DPRINTF(("sms flag key\n")); 711 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1); 712 DELAY(100); 713 714 sc->sc_sms_intr_works = 0; 715 716 /* 717 * Retry SMS initialization 1000 times 718 * (takes approx. 2 seconds in worst case) 719 */ 720 for (i = 0; i < 1000; i++) { 721 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 && 722 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) { 723 error = 0; 724 sc->sc_sms_intr_works = 1; 725 goto out; 726 } 727 buf[0] = ASMC_SMS_INIT1; 728 buf[1] = ASMC_SMS_INIT2; 729 ASMC_DPRINTF(("sms key\n")); 730 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2); 731 DELAY(50); 732 } 733 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n"); 734 735 out: 736 asmc_sms_calibrate(dev); 737 nosms: 738 sc->sc_nfan = asmc_fan_count(dev); 739 if (sc->sc_nfan > ASMC_MAXFANS) { 740 device_printf(dev, "more than %d fans were detected. Please " 741 "report this.\n", ASMC_MAXFANS); 742 sc->sc_nfan = ASMC_MAXFANS; 743 } 744 745 if (bootverbose) { 746 /* 747 * The number of keys is a 32 bit buffer 748 */ 749 asmc_key_read(dev, ASMC_NKEYS, buf, 4); 750 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf)); 751 } 752 753 #ifdef DEBUG 754 asmc_dumpall(dev); 755 #endif 756 757 return (error); 758 } 759 760 /* 761 * We need to make sure that the SMC acks the byte sent. 762 * Just wait up to (amount * 10) ms. 763 */ 764 static int 765 asmc_wait_ack(device_t dev, uint8_t val, int amount) 766 { 767 struct asmc_softc *sc = device_get_softc(dev); 768 u_int i; 769 770 val = val & ASMC_STATUS_MASK; 771 772 for (i = 0; i < amount; i++) { 773 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val) 774 return (0); 775 DELAY(10); 776 } 777 778 return (1); 779 } 780 781 /* 782 * We need to make sure that the SMC acks the byte sent. 783 * Just wait up to 100 ms. 784 */ 785 static int 786 asmc_wait(device_t dev, uint8_t val) 787 { 788 struct asmc_softc *sc; 789 790 if (asmc_wait_ack(dev, val, 1000) == 0) 791 return (0); 792 793 sc = device_get_softc(dev); 794 val = val & ASMC_STATUS_MASK; 795 796 #ifdef DEBUG 797 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val, 798 ASMC_CMDPORT_READ(sc)); 799 #endif 800 return (1); 801 } 802 803 /* 804 * Send the given command, retrying up to 10 times if 805 * the acknowledgement fails. 806 */ 807 static int 808 asmc_command(device_t dev, uint8_t command) { 809 810 int i; 811 struct asmc_softc *sc = device_get_softc(dev); 812 813 for (i=0; i < 10; i++) { 814 ASMC_CMDPORT_WRITE(sc, command); 815 if (asmc_wait_ack(dev, 0x0c, 100) == 0) { 816 return (0); 817 } 818 } 819 820 #ifdef DEBUG 821 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command, 822 ASMC_CMDPORT_READ(sc)); 823 #endif 824 return (1); 825 } 826 827 static int 828 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len) 829 { 830 int i, error = 1, try = 0; 831 struct asmc_softc *sc = device_get_softc(dev); 832 833 mtx_lock_spin(&sc->sc_mtx); 834 835 begin: 836 if (asmc_command(dev, ASMC_CMDREAD)) 837 goto out; 838 839 for (i = 0; i < 4; i++) { 840 ASMC_DATAPORT_WRITE(sc, key[i]); 841 if (asmc_wait(dev, 0x04)) 842 goto out; 843 } 844 845 ASMC_DATAPORT_WRITE(sc, len); 846 847 for (i = 0; i < len; i++) { 848 if (asmc_wait(dev, 0x05)) 849 goto out; 850 buf[i] = ASMC_DATAPORT_READ(sc); 851 } 852 853 error = 0; 854 out: 855 if (error) { 856 if (++try < 10) goto begin; 857 device_printf(dev,"%s for key %s failed %d times, giving up\n", 858 __func__, key, try); 859 } 860 861 mtx_unlock_spin(&sc->sc_mtx); 862 863 return (error); 864 } 865 866 #ifdef DEBUG 867 static int 868 asmc_key_dump(device_t dev, int number) 869 { 870 struct asmc_softc *sc = device_get_softc(dev); 871 char key[5] = { 0 }; 872 char type[7] = { 0 }; 873 uint8_t index[4]; 874 uint8_t v[32]; 875 uint8_t maxlen; 876 int i, error = 1, try = 0; 877 878 mtx_lock_spin(&sc->sc_mtx); 879 880 index[0] = (number >> 24) & 0xff; 881 index[1] = (number >> 16) & 0xff; 882 index[2] = (number >> 8) & 0xff; 883 index[3] = (number) & 0xff; 884 885 begin: 886 if (asmc_command(dev, 0x12)) 887 goto out; 888 889 for (i = 0; i < 4; i++) { 890 ASMC_DATAPORT_WRITE(sc, index[i]); 891 if (asmc_wait(dev, 0x04)) 892 goto out; 893 } 894 895 ASMC_DATAPORT_WRITE(sc, 4); 896 897 for (i = 0; i < 4; i++) { 898 if (asmc_wait(dev, 0x05)) 899 goto out; 900 key[i] = ASMC_DATAPORT_READ(sc); 901 } 902 903 /* get type */ 904 if (asmc_command(dev, 0x13)) 905 goto out; 906 907 for (i = 0; i < 4; i++) { 908 ASMC_DATAPORT_WRITE(sc, key[i]); 909 if (asmc_wait(dev, 0x04)) 910 goto out; 911 } 912 913 ASMC_DATAPORT_WRITE(sc, 6); 914 915 for (i = 0; i < 6; i++) { 916 if (asmc_wait(dev, 0x05)) 917 goto out; 918 type[i] = ASMC_DATAPORT_READ(sc); 919 } 920 921 error = 0; 922 out: 923 if (error) { 924 if (++try < 10) goto begin; 925 device_printf(dev,"%s for key %s failed %d times, giving up\n", 926 __func__, key, try); 927 mtx_unlock_spin(&sc->sc_mtx); 928 } 929 else { 930 char buf[1024]; 931 char buf2[8]; 932 mtx_unlock_spin(&sc->sc_mtx); 933 maxlen = type[0]; 934 type[0] = ' '; 935 type[5] = 0; 936 if (maxlen > sizeof(v)) { 937 device_printf(dev, 938 "WARNING: cropping maxlen from %d to %zu\n", 939 maxlen, sizeof(v)); 940 maxlen = sizeof(v); 941 } 942 for (i = 0; i < sizeof(v); i++) { 943 v[i] = 0; 944 } 945 asmc_key_read(dev, key, v, maxlen); 946 snprintf(buf, sizeof(buf), "key %d is: %s, type %s " 947 "(len %d), data", number, key, type, maxlen); 948 for (i = 0; i < maxlen; i++) { 949 snprintf(buf2, sizeof(buf2), " %02x", v[i]); 950 strlcat(buf, buf2, sizeof(buf)); 951 } 952 strlcat(buf, " \n", sizeof(buf)); 953 device_printf(dev, "%s", buf); 954 } 955 956 return (error); 957 } 958 #endif 959 960 static int 961 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len) 962 { 963 int i, error = -1, try = 0; 964 struct asmc_softc *sc = device_get_softc(dev); 965 966 mtx_lock_spin(&sc->sc_mtx); 967 968 begin: 969 ASMC_DPRINTF(("cmd port: cmd write\n")); 970 if (asmc_command(dev, ASMC_CMDWRITE)) 971 goto out; 972 973 ASMC_DPRINTF(("data port: key\n")); 974 for (i = 0; i < 4; i++) { 975 ASMC_DATAPORT_WRITE(sc, key[i]); 976 if (asmc_wait(dev, 0x04)) 977 goto out; 978 } 979 ASMC_DPRINTF(("data port: length\n")); 980 ASMC_DATAPORT_WRITE(sc, len); 981 982 ASMC_DPRINTF(("data port: buffer\n")); 983 for (i = 0; i < len; i++) { 984 if (asmc_wait(dev, 0x04)) 985 goto out; 986 ASMC_DATAPORT_WRITE(sc, buf[i]); 987 } 988 989 error = 0; 990 out: 991 if (error) { 992 if (++try < 10) goto begin; 993 device_printf(dev,"%s for key %s failed %d times, giving up\n", 994 __func__, key, try); 995 } 996 997 mtx_unlock_spin(&sc->sc_mtx); 998 999 return (error); 1000 1001 } 1002 1003 /* 1004 * Fan control functions. 1005 */ 1006 static int 1007 asmc_fan_count(device_t dev) 1008 { 1009 uint8_t buf[1]; 1010 1011 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0) 1012 return (-1); 1013 1014 return (buf[0]); 1015 } 1016 1017 static int 1018 asmc_fan_getvalue(device_t dev, const char *key, int fan) 1019 { 1020 int speed; 1021 uint8_t buf[2]; 1022 char fankey[5]; 1023 1024 snprintf(fankey, sizeof(fankey), key, fan); 1025 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0) 1026 return (-1); 1027 speed = (buf[0] << 6) | (buf[1] >> 2); 1028 1029 return (speed); 1030 } 1031 1032 static char* 1033 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen) 1034 { 1035 char fankey[5]; 1036 char* desc; 1037 1038 snprintf(fankey, sizeof(fankey), key, fan); 1039 if (asmc_key_read(dev, fankey, buf, buflen) < 0) 1040 return (NULL); 1041 desc = buf+4; 1042 1043 return (desc); 1044 } 1045 1046 static int 1047 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed) 1048 { 1049 uint8_t buf[2]; 1050 char fankey[5]; 1051 1052 speed *= 4; 1053 1054 buf[0] = speed>>8; 1055 buf[1] = speed; 1056 1057 snprintf(fankey, sizeof(fankey), key, fan); 1058 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0) 1059 return (-1); 1060 1061 return (0); 1062 } 1063 1064 static int 1065 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS) 1066 { 1067 device_t dev = (device_t) arg1; 1068 int fan = arg2; 1069 int error; 1070 int32_t v; 1071 1072 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan); 1073 error = sysctl_handle_int(oidp, &v, 0, req); 1074 1075 return (error); 1076 } 1077 1078 static int 1079 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS) 1080 { 1081 uint8_t buf[16]; 1082 device_t dev = (device_t) arg1; 1083 int fan = arg2; 1084 int error = true; 1085 char* desc; 1086 1087 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf)); 1088 1089 if (desc != NULL) 1090 error = sysctl_handle_string(oidp, desc, 0, req); 1091 1092 return (error); 1093 } 1094 1095 static int 1096 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS) 1097 { 1098 device_t dev = (device_t) arg1; 1099 int fan = arg2; 1100 int error; 1101 int32_t v; 1102 1103 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan); 1104 error = sysctl_handle_int(oidp, &v, 0, req); 1105 1106 return (error); 1107 } 1108 1109 1110 static int 1111 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS) 1112 { 1113 device_t dev = (device_t) arg1; 1114 int fan = arg2; 1115 int error; 1116 int32_t v; 1117 1118 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan); 1119 error = sysctl_handle_int(oidp, &v, 0, req); 1120 1121 if (error == 0 && req->newptr != NULL) { 1122 unsigned int newspeed = v; 1123 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed); 1124 } 1125 1126 return (error); 1127 } 1128 1129 static int 1130 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS) 1131 { 1132 device_t dev = (device_t) arg1; 1133 int fan = arg2; 1134 int error; 1135 int32_t v; 1136 1137 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan); 1138 error = sysctl_handle_int(oidp, &v, 0, req); 1139 1140 if (error == 0 && req->newptr != NULL) { 1141 unsigned int newspeed = v; 1142 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed); 1143 } 1144 1145 return (error); 1146 } 1147 1148 static int 1149 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS) 1150 { 1151 device_t dev = (device_t) arg1; 1152 int fan = arg2; 1153 int error; 1154 int32_t v; 1155 1156 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan); 1157 error = sysctl_handle_int(oidp, &v, 0, req); 1158 1159 if (error == 0 && req->newptr != NULL) { 1160 unsigned int newspeed = v; 1161 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed); 1162 } 1163 1164 return (error); 1165 } 1166 1167 /* 1168 * Temperature functions. 1169 */ 1170 static int 1171 asmc_temp_getvalue(device_t dev, const char *key) 1172 { 1173 uint8_t buf[2]; 1174 1175 /* 1176 * Check for invalid temperatures. 1177 */ 1178 if (asmc_key_read(dev, key, buf, sizeof buf) < 0) 1179 return (-1); 1180 1181 return (buf[0]); 1182 } 1183 1184 static int 1185 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS) 1186 { 1187 device_t dev = (device_t) arg1; 1188 struct asmc_softc *sc = device_get_softc(dev); 1189 int error, val; 1190 1191 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]); 1192 error = sysctl_handle_int(oidp, &val, 0, req); 1193 1194 return (error); 1195 } 1196 1197 /* 1198 * Sudden Motion Sensor functions. 1199 */ 1200 static int 1201 asmc_sms_read(device_t dev, const char *key, int16_t *val) 1202 { 1203 uint8_t buf[2]; 1204 int error; 1205 1206 /* no need to do locking here as asmc_key_read() already does it */ 1207 switch (key[3]) { 1208 case 'X': 1209 case 'Y': 1210 case 'Z': 1211 error = asmc_key_read(dev, key, buf, sizeof buf); 1212 break; 1213 default: 1214 device_printf(dev, "%s called with invalid argument %s\n", 1215 __func__, key); 1216 error = 1; 1217 goto out; 1218 } 1219 *val = ((int16_t)buf[0] << 8) | buf[1]; 1220 out: 1221 return (error); 1222 } 1223 1224 static void 1225 asmc_sms_calibrate(device_t dev) 1226 { 1227 struct asmc_softc *sc = device_get_softc(dev); 1228 1229 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x); 1230 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y); 1231 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z); 1232 } 1233 1234 static int 1235 asmc_sms_intrfast(void *arg) 1236 { 1237 uint8_t type; 1238 device_t dev = (device_t) arg; 1239 struct asmc_softc *sc = device_get_softc(dev); 1240 if (!sc->sc_sms_intr_works) 1241 return (FILTER_HANDLED); 1242 1243 mtx_lock_spin(&sc->sc_mtx); 1244 type = ASMC_INTPORT_READ(sc); 1245 mtx_unlock_spin(&sc->sc_mtx); 1246 1247 sc->sc_sms_intrtype = type; 1248 asmc_sms_printintr(dev, type); 1249 1250 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task); 1251 return (FILTER_HANDLED); 1252 } 1253 1254 1255 1256 static void 1257 asmc_sms_printintr(device_t dev, uint8_t type) 1258 { 1259 1260 switch (type) { 1261 case ASMC_SMS_INTFF: 1262 device_printf(dev, "WARNING: possible free fall!\n"); 1263 break; 1264 case ASMC_SMS_INTHA: 1265 device_printf(dev, "WARNING: high acceleration detected!\n"); 1266 break; 1267 case ASMC_SMS_INTSH: 1268 device_printf(dev, "WARNING: possible shock!\n"); 1269 break; 1270 default: 1271 device_printf(dev, "%s unknown interrupt\n", __func__); 1272 } 1273 } 1274 1275 static void 1276 asmc_sms_task(void *arg, int pending) 1277 { 1278 struct asmc_softc *sc = (struct asmc_softc *)arg; 1279 char notify[16]; 1280 int type; 1281 1282 switch (sc->sc_sms_intrtype) { 1283 case ASMC_SMS_INTFF: 1284 type = 2; 1285 break; 1286 case ASMC_SMS_INTHA: 1287 type = 1; 1288 break; 1289 case ASMC_SMS_INTSH: 1290 type = 0; 1291 break; 1292 default: 1293 type = 255; 1294 } 1295 1296 snprintf(notify, sizeof(notify), " notify=0x%x", type); 1297 devctl_notify("ACPI", "asmc", "SMS", notify); 1298 } 1299 1300 static int 1301 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS) 1302 { 1303 device_t dev = (device_t) arg1; 1304 int error; 1305 int16_t val; 1306 int32_t v; 1307 1308 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val); 1309 v = (int32_t) val; 1310 error = sysctl_handle_int(oidp, &v, 0, req); 1311 1312 return (error); 1313 } 1314 1315 static int 1316 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS) 1317 { 1318 device_t dev = (device_t) arg1; 1319 int error; 1320 int16_t val; 1321 int32_t v; 1322 1323 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val); 1324 v = (int32_t) val; 1325 error = sysctl_handle_int(oidp, &v, 0, req); 1326 1327 return (error); 1328 } 1329 1330 static int 1331 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS) 1332 { 1333 device_t dev = (device_t) arg1; 1334 int error; 1335 int16_t val; 1336 int32_t v; 1337 1338 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val); 1339 v = (int32_t) val; 1340 error = sysctl_handle_int(oidp, &v, 0, req); 1341 1342 return (error); 1343 } 1344 1345 static int 1346 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS) 1347 { 1348 device_t dev = (device_t) arg1; 1349 uint8_t buf[6]; 1350 int error; 1351 int32_t v; 1352 1353 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf); 1354 v = buf[2]; 1355 error = sysctl_handle_int(oidp, &v, 0, req); 1356 1357 return (error); 1358 } 1359 1360 static int 1361 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS) 1362 { 1363 device_t dev = (device_t) arg1; 1364 uint8_t buf[6]; 1365 int error; 1366 int32_t v; 1367 1368 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf); 1369 v = buf[2]; 1370 error = sysctl_handle_int(oidp, &v, 0, req); 1371 1372 return (error); 1373 } 1374 1375 static int 1376 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS) 1377 { 1378 device_t dev = (device_t) arg1; 1379 uint8_t buf[2]; 1380 int error; 1381 int v; 1382 1383 v = light_control; 1384 error = sysctl_handle_int(oidp, &v, 0, req); 1385 1386 if (error == 0 && req->newptr != NULL) { 1387 if (v < 0 || v > 255) 1388 return (EINVAL); 1389 light_control = v; 1390 buf[0] = light_control; 1391 buf[1] = 0x00; 1392 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf); 1393 } 1394 return (error); 1395 } 1396