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