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