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