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