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