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