2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 * Driver for Apple's System Management Console (SMC).
32 * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
34 * Inspired by the Linux applesmc driver.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
43 #include <sys/kernel.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>
53 #include <machine/resource.h>
55 #include <contrib/dev/acpica/include/acpi.h>
57 #include <dev/acpica/acpivar.h>
58 #include <dev/asmc/asmcvar.h>
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);
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,
77 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf,
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);
89 void asmc_dumpall(device_t);
90 static int asmc_key_dump(device_t, int);
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);
111 const char *smc_model; /* smbios.system.product env var. */
112 const char *smc_desc; /* driver description */
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);
128 const char *smc_temps[ASMC_TEMP_MAX];
129 const char *smc_tempnames[ASMC_TEMP_MAX];
130 const char *smc_tempdescs[ASMC_TEMP_MAX];
133 static struct asmc_model *asmc_match(device_t dev);
135 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
138 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
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
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
150 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
151 asmc_mbp_sysctl_light_right, \
152 asmc_mbp_sysctl_light_control
154 struct asmc_model asmc_models[] = {
156 "MacBook1,1", "Apple SMC MacBook Core Duo",
157 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
158 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
162 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
163 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
164 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
168 "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
169 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
170 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
174 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
175 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
176 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
180 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
181 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
182 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
186 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
187 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
188 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
192 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
193 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
194 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
198 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
199 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
200 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
204 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
205 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
206 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
210 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
211 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
212 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
216 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
217 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
218 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
222 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
223 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
224 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
228 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
229 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
230 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
234 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
235 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
236 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
239 /* The Mac Mini has no SMS */
241 "Macmini1,1", "Apple SMC Mac Mini",
245 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
248 /* The Mac Mini 3,1 has no SMS */
250 "Macmini3,1", "Apple SMC Mac Mini 3,1",
254 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
257 /* Idem for the MacPro */
259 "MacPro2", "Apple SMC Mac Pro (8-core)",
263 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
266 /* Idem for the MacPro 2010*/
268 "MacPro5,1", "Apple SMC MacPro (2010)",
272 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
276 "MacBookAir1,1", "Apple SMC MacBook Air",
277 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
278 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
282 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
283 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
284 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
288 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
289 ASMC_SMS_FUNCS_DISABLED,
292 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
296 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
297 ASMC_SMS_FUNCS_DISABLED,
300 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
307 #undef ASMC_SMS_FUNCS
308 #undef ASMC_SMS_FUNCS_DISABLED
309 #undef ASMC_FAN_FUNCS
310 #undef ASMC_FAN_FUNCS2
311 #undef ASMC_LIGHT_FUNCS
316 static device_method_t asmc_methods[] = {
317 DEVMETHOD(device_probe, asmc_probe),
318 DEVMETHOD(device_attach, asmc_attach),
319 DEVMETHOD(device_detach, asmc_detach),
320 DEVMETHOD(device_resume, asmc_resume),
325 static driver_t asmc_driver = {
328 sizeof(struct asmc_softc)
334 #define _COMPONENT ACPI_OEM
335 ACPI_MODULE_NAME("ASMC")
337 #define ASMC_DPRINTF(str) device_printf(dev, str)
339 #define ASMC_DPRINTF(str)
342 /* NB: can't be const */
343 static char *asmc_ids[] = { "APP0001", NULL };
345 static devclass_t asmc_devclass;
347 static unsigned int light_control = 0;
349 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
350 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
352 static struct asmc_model *
353 asmc_match(device_t dev)
358 model = kern_getenv("smbios.system.product");
362 for (i = 0; asmc_models[i].smc_model; i++) {
363 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
365 return (&asmc_models[i]);
374 asmc_probe(device_t dev)
376 struct asmc_model *model;
379 if (resource_disabled("asmc", 0))
381 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
385 model = asmc_match(dev);
387 device_printf(dev, "model not recognized\n");
390 device_set_desc(dev, model->smc_desc);
396 asmc_attach(device_t dev)
401 struct asmc_softc *sc = device_get_softc(dev);
402 struct sysctl_ctx_list *sysctlctx;
403 struct sysctl_oid *sysctlnode;
404 struct asmc_model *model;
406 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
407 &sc->sc_rid_port, RF_ACTIVE);
408 if (sc->sc_ioport == NULL) {
409 device_printf(dev, "unable to allocate IO port\n");
413 sysctlctx = device_get_sysctl_ctx(dev);
414 sysctlnode = device_get_sysctl_tree(dev);
416 model = asmc_match(dev);
418 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
420 sc->sc_model = model;
424 * dev.asmc.n.fan.* tree.
426 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
427 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
428 CTLFLAG_RD, 0, "Fan Root Tree");
430 for (i = 1; i <= sc->sc_nfan; i++) {
434 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
435 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
436 OID_AUTO, name, CTLFLAG_RD, 0,
439 SYSCTL_ADD_PROC(sysctlctx,
440 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
441 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
442 dev, j, model->smc_fan_id, "I",
445 SYSCTL_ADD_PROC(sysctlctx,
446 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
447 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
448 dev, j, model->smc_fan_speed, "I",
451 SYSCTL_ADD_PROC(sysctlctx,
452 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
453 OID_AUTO, "safespeed",
454 CTLTYPE_INT | CTLFLAG_RD,
455 dev, j, model->smc_fan_safespeed, "I",
456 "Fan safe speed in RPM");
458 SYSCTL_ADD_PROC(sysctlctx,
459 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
460 OID_AUTO, "minspeed",
461 CTLTYPE_INT | CTLFLAG_RW,
462 dev, j, model->smc_fan_minspeed, "I",
463 "Fan minimum speed in RPM");
465 SYSCTL_ADD_PROC(sysctlctx,
466 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
467 OID_AUTO, "maxspeed",
468 CTLTYPE_INT | CTLFLAG_RW,
469 dev, j, model->smc_fan_maxspeed, "I",
470 "Fan maximum speed in RPM");
472 SYSCTL_ADD_PROC(sysctlctx,
473 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
474 OID_AUTO, "targetspeed",
475 CTLTYPE_INT | CTLFLAG_RW,
476 dev, j, model->smc_fan_targetspeed, "I",
477 "Fan target speed in RPM");
481 * dev.asmc.n.temp tree.
483 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
484 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
485 CTLFLAG_RD, 0, "Temperature sensors");
487 for (i = 0; model->smc_temps[i]; i++) {
488 SYSCTL_ADD_PROC(sysctlctx,
489 SYSCTL_CHILDREN(sc->sc_temp_tree),
490 OID_AUTO, model->smc_tempnames[i],
491 CTLTYPE_INT | CTLFLAG_RD,
492 dev, i, asmc_temp_sysctl, "I",
493 model->smc_tempdescs[i]);
499 if (model->smc_light_left) {
500 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
501 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
502 CTLFLAG_RD, 0, "Keyboard backlight sensors");
504 SYSCTL_ADD_PROC(sysctlctx,
505 SYSCTL_CHILDREN(sc->sc_light_tree),
506 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
507 dev, 0, model->smc_light_left, "I",
508 "Keyboard backlight left sensor");
510 SYSCTL_ADD_PROC(sysctlctx,
511 SYSCTL_CHILDREN(sc->sc_light_tree),
512 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
513 dev, 0, model->smc_light_right, "I",
514 "Keyboard backlight right sensor");
516 SYSCTL_ADD_PROC(sysctlctx,
517 SYSCTL_CHILDREN(sc->sc_light_tree),
519 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
520 dev, 0, model->smc_light_control, "I",
521 "Keyboard backlight brightness control");
524 if (model->smc_sms_x == NULL)
528 * dev.asmc.n.sms tree.
530 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
531 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
532 CTLFLAG_RD, 0, "Sudden Motion Sensor");
534 SYSCTL_ADD_PROC(sysctlctx,
535 SYSCTL_CHILDREN(sc->sc_sms_tree),
536 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
537 dev, 0, model->smc_sms_x, "I",
538 "Sudden Motion Sensor X value");
540 SYSCTL_ADD_PROC(sysctlctx,
541 SYSCTL_CHILDREN(sc->sc_sms_tree),
542 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
543 dev, 0, model->smc_sms_y, "I",
544 "Sudden Motion Sensor Y value");
546 SYSCTL_ADD_PROC(sysctlctx,
547 SYSCTL_CHILDREN(sc->sc_sms_tree),
548 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
549 dev, 0, model->smc_sms_z, "I",
550 "Sudden Motion Sensor Z value");
553 * Need a taskqueue to send devctl_notify() events
554 * when the SMS interrupt us.
556 * PI_REALTIME is used due to the sensitivity of the
557 * interrupt. An interrupt from the SMS means that the
558 * disk heads should be turned off as quickly as possible.
560 * We only need to do this for the non INTR_FILTER case.
562 sc->sc_sms_tq = NULL;
563 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
564 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
565 taskqueue_thread_enqueue, &sc->sc_sms_tq);
566 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
567 device_get_nameunit(dev));
569 * Allocate an IRQ for the SMS.
572 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
573 &sc->sc_rid_irq, RF_ACTIVE);
574 if (sc->sc_irq == NULL) {
575 device_printf(dev, "unable to allocate IRQ resource\n");
580 ret = bus_setup_intr(dev, sc->sc_irq,
581 INTR_TYPE_MISC | INTR_MPSAFE,
582 asmc_sms_intrfast, NULL,
583 dev, &sc->sc_cookie);
586 device_printf(dev, "unable to setup SMS IRQ\n");
592 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
594 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
596 mtx_destroy(&sc->sc_mtx);
598 taskqueue_free(sc->sc_sms_tq);
604 asmc_detach(device_t dev)
606 struct asmc_softc *sc = device_get_softc(dev);
609 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
610 taskqueue_free(sc->sc_sms_tq);
613 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
615 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
618 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
620 mtx_destroy(&sc->sc_mtx);
626 asmc_resume(device_t dev)
629 buf[0] = light_control;
631 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
637 void asmc_dumpall(device_t dev)
641 /* XXX magic number */
642 for (i=0; i < 0x100; i++)
643 asmc_key_dump(dev, i);
648 asmc_init(device_t dev)
650 struct asmc_softc *sc = device_get_softc(dev);
654 if (sc->sc_model->smc_sms_x == NULL)
658 * We are ready to receive interrupts from the SMS.
661 ASMC_DPRINTF(("intok key\n"));
662 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
666 * Initiate the polling intervals.
668 buf[0] = 20; /* msecs */
669 ASMC_DPRINTF(("low int key\n"));
670 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
673 buf[0] = 20; /* msecs */
674 ASMC_DPRINTF(("high int key\n"));
675 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
680 ASMC_DPRINTF(("sms low key\n"));
681 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
686 ASMC_DPRINTF(("sms high key\n"));
687 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
691 * I'm not sure what this key does, but it seems to be
695 ASMC_DPRINTF(("sms flag key\n"));
696 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
699 sc->sc_sms_intr_works = 0;
702 * Retry SMS initialization 1000 times
703 * (takes approx. 2 seconds in worst case)
705 for (i = 0; i < 1000; i++) {
706 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
707 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
709 sc->sc_sms_intr_works = 1;
712 buf[0] = ASMC_SMS_INIT1;
713 buf[1] = ASMC_SMS_INIT2;
714 ASMC_DPRINTF(("sms key\n"));
715 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
718 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
721 asmc_sms_calibrate(dev);
723 sc->sc_nfan = asmc_fan_count(dev);
724 if (sc->sc_nfan > ASMC_MAXFANS) {
725 device_printf(dev, "more than %d fans were detected. Please "
726 "report this.\n", ASMC_MAXFANS);
727 sc->sc_nfan = ASMC_MAXFANS;
732 * The number of keys is a 32 bit buffer
734 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
735 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
746 * We need to make sure that the SMC acks the byte sent.
747 * Just wait up to (amount * 10) ms.
750 asmc_wait_ack(device_t dev, uint8_t val, int amount)
752 struct asmc_softc *sc = device_get_softc(dev);
755 val = val & ASMC_STATUS_MASK;
757 for (i = 0; i < amount; i++) {
758 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
767 * We need to make sure that the SMC acks the byte sent.
768 * Just wait up to 100 ms.
771 asmc_wait(device_t dev, uint8_t val)
773 struct asmc_softc *sc;
775 if (asmc_wait_ack(dev, val, 1000) == 0)
778 sc = device_get_softc(dev);
779 val = val & ASMC_STATUS_MASK;
782 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
783 ASMC_CMDPORT_READ(sc));
789 * Send the given command, retrying up to 10 times if
790 * the acknowledgement fails.
793 asmc_command(device_t dev, uint8_t command) {
796 struct asmc_softc *sc = device_get_softc(dev);
798 for (i=0; i < 10; i++) {
799 ASMC_CMDPORT_WRITE(sc, command);
800 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
806 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
807 ASMC_CMDPORT_READ(sc));
813 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
815 int i, error = 1, try = 0;
816 struct asmc_softc *sc = device_get_softc(dev);
818 mtx_lock_spin(&sc->sc_mtx);
821 if (asmc_command(dev, ASMC_CMDREAD))
824 for (i = 0; i < 4; i++) {
825 ASMC_DATAPORT_WRITE(sc, key[i]);
826 if (asmc_wait(dev, 0x04))
830 ASMC_DATAPORT_WRITE(sc, len);
832 for (i = 0; i < len; i++) {
833 if (asmc_wait(dev, 0x05))
835 buf[i] = ASMC_DATAPORT_READ(sc);
841 if (++try < 10) goto begin;
842 device_printf(dev,"%s for key %s failed %d times, giving up\n",
846 mtx_unlock_spin(&sc->sc_mtx);
853 asmc_key_dump(device_t dev, int number)
855 struct asmc_softc *sc = device_get_softc(dev);
857 char type[7] = { 0 };
861 int i, error = 1, try = 0;
863 mtx_lock_spin(&sc->sc_mtx);
865 index[0] = (number >> 24) & 0xff;
866 index[1] = (number >> 16) & 0xff;
867 index[2] = (number >> 8) & 0xff;
868 index[3] = (number) & 0xff;
871 if (asmc_command(dev, 0x12))
874 for (i = 0; i < 4; i++) {
875 ASMC_DATAPORT_WRITE(sc, index[i]);
876 if (asmc_wait(dev, 0x04))
880 ASMC_DATAPORT_WRITE(sc, 4);
882 for (i = 0; i < 4; i++) {
883 if (asmc_wait(dev, 0x05))
885 key[i] = ASMC_DATAPORT_READ(sc);
889 if (asmc_command(dev, 0x13))
892 for (i = 0; i < 4; i++) {
893 ASMC_DATAPORT_WRITE(sc, key[i]);
894 if (asmc_wait(dev, 0x04))
898 ASMC_DATAPORT_WRITE(sc, 6);
900 for (i = 0; i < 6; i++) {
901 if (asmc_wait(dev, 0x05))
903 type[i] = ASMC_DATAPORT_READ(sc);
909 if (++try < 10) goto begin;
910 device_printf(dev,"%s for key %s failed %d times, giving up\n",
912 mtx_unlock_spin(&sc->sc_mtx);
917 mtx_unlock_spin(&sc->sc_mtx);
921 if (maxlen > sizeof(v)) {
923 "WARNING: cropping maxlen from %d to %zu\n",
927 for (i = 0; i < sizeof(v); i++) {
930 asmc_key_read(dev, key, v, maxlen);
931 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
932 "(len %d), data", number, key, type, maxlen);
933 for (i = 0; i < maxlen; i++) {
934 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
935 strlcat(buf, buf2, sizeof(buf));
937 strlcat(buf, " \n", sizeof(buf));
938 device_printf(dev, "%s", buf);
946 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
948 int i, error = -1, try = 0;
949 struct asmc_softc *sc = device_get_softc(dev);
951 mtx_lock_spin(&sc->sc_mtx);
954 ASMC_DPRINTF(("cmd port: cmd write\n"));
955 if (asmc_command(dev, ASMC_CMDWRITE))
958 ASMC_DPRINTF(("data port: key\n"));
959 for (i = 0; i < 4; i++) {
960 ASMC_DATAPORT_WRITE(sc, key[i]);
961 if (asmc_wait(dev, 0x04))
964 ASMC_DPRINTF(("data port: length\n"));
965 ASMC_DATAPORT_WRITE(sc, len);
967 ASMC_DPRINTF(("data port: buffer\n"));
968 for (i = 0; i < len; i++) {
969 if (asmc_wait(dev, 0x04))
971 ASMC_DATAPORT_WRITE(sc, buf[i]);
977 if (++try < 10) goto begin;
978 device_printf(dev,"%s for key %s failed %d times, giving up\n",
982 mtx_unlock_spin(&sc->sc_mtx);
989 * Fan control functions.
992 asmc_fan_count(device_t dev)
996 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1003 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1009 snprintf(fankey, sizeof(fankey), key, fan);
1010 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1012 speed = (buf[0] << 6) | (buf[1] >> 2);
1018 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1023 snprintf(fankey, sizeof(fankey), key, fan);
1024 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1032 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1042 snprintf(fankey, sizeof(fankey), key, fan);
1043 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1050 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1052 device_t dev = (device_t) arg1;
1057 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1058 error = sysctl_handle_int(oidp, &v, 0, req);
1064 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1067 device_t dev = (device_t) arg1;
1072 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1075 error = sysctl_handle_string(oidp, desc, 0, req);
1081 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1083 device_t dev = (device_t) arg1;
1088 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1089 error = sysctl_handle_int(oidp, &v, 0, req);
1096 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1098 device_t dev = (device_t) arg1;
1103 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1104 error = sysctl_handle_int(oidp, &v, 0, req);
1106 if (error == 0 && req->newptr != NULL) {
1107 unsigned int newspeed = v;
1108 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1115 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1117 device_t dev = (device_t) arg1;
1122 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1123 error = sysctl_handle_int(oidp, &v, 0, req);
1125 if (error == 0 && req->newptr != NULL) {
1126 unsigned int newspeed = v;
1127 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1134 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1136 device_t dev = (device_t) arg1;
1141 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1142 error = sysctl_handle_int(oidp, &v, 0, req);
1144 if (error == 0 && req->newptr != NULL) {
1145 unsigned int newspeed = v;
1146 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1153 * Temperature functions.
1156 asmc_temp_getvalue(device_t dev, const char *key)
1161 * Check for invalid temperatures.
1163 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1170 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1172 device_t dev = (device_t) arg1;
1173 struct asmc_softc *sc = device_get_softc(dev);
1176 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1177 error = sysctl_handle_int(oidp, &val, 0, req);
1183 * Sudden Motion Sensor functions.
1186 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1191 /* no need to do locking here as asmc_key_read() already does it */
1196 error = asmc_key_read(dev, key, buf, sizeof buf);
1199 device_printf(dev, "%s called with invalid argument %s\n",
1204 *val = ((int16_t)buf[0] << 8) | buf[1];
1210 asmc_sms_calibrate(device_t dev)
1212 struct asmc_softc *sc = device_get_softc(dev);
1214 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1215 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1216 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1220 asmc_sms_intrfast(void *arg)
1223 device_t dev = (device_t) arg;
1224 struct asmc_softc *sc = device_get_softc(dev);
1225 if (!sc->sc_sms_intr_works)
1226 return (FILTER_HANDLED);
1228 mtx_lock_spin(&sc->sc_mtx);
1229 type = ASMC_INTPORT_READ(sc);
1230 mtx_unlock_spin(&sc->sc_mtx);
1232 sc->sc_sms_intrtype = type;
1233 asmc_sms_printintr(dev, type);
1235 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1236 return (FILTER_HANDLED);
1242 asmc_sms_printintr(device_t dev, uint8_t type)
1246 case ASMC_SMS_INTFF:
1247 device_printf(dev, "WARNING: possible free fall!\n");
1249 case ASMC_SMS_INTHA:
1250 device_printf(dev, "WARNING: high acceleration detected!\n");
1252 case ASMC_SMS_INTSH:
1253 device_printf(dev, "WARNING: possible shock!\n");
1256 device_printf(dev, "%s unknown interrupt\n", __func__);
1261 asmc_sms_task(void *arg, int pending)
1263 struct asmc_softc *sc = (struct asmc_softc *)arg;
1267 switch (sc->sc_sms_intrtype) {
1268 case ASMC_SMS_INTFF:
1271 case ASMC_SMS_INTHA:
1274 case ASMC_SMS_INTSH:
1281 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1282 devctl_notify("ACPI", "asmc", "SMS", notify);
1286 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1288 device_t dev = (device_t) arg1;
1293 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1295 error = sysctl_handle_int(oidp, &v, 0, req);
1301 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1303 device_t dev = (device_t) arg1;
1308 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1310 error = sysctl_handle_int(oidp, &v, 0, req);
1316 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1318 device_t dev = (device_t) arg1;
1323 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1325 error = sysctl_handle_int(oidp, &v, 0, req);
1331 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1333 device_t dev = (device_t) arg1;
1338 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1340 error = sysctl_handle_int(oidp, &v, 0, req);
1346 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1348 device_t dev = (device_t) arg1;
1353 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1355 error = sysctl_handle_int(oidp, &v, 0, req);
1361 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1363 device_t dev = (device_t) arg1;
1369 error = sysctl_handle_int(oidp, &v, 0, req);
1371 if (error == 0 && req->newptr != NULL) {
1372 if (v < 0 || v > 255)
1375 buf[0] = light_control;
1377 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);