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