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