2 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
29 * Driver for Apple's System Management Console (SMC).
30 * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
32 * Inspired by the Linux applesmc driver.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
51 #include <machine/resource.h>
53 #include <contrib/dev/acpica/include/acpi.h>
55 #include <dev/acpica/acpivar.h>
56 #include <dev/asmc/asmcvar.h>
58 #include "opt_intr_filter.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);
70 static int asmc_init(device_t dev);
71 static int asmc_command(device_t dev, uint8_t command);
72 static int asmc_wait(device_t dev, uint8_t val);
73 static int asmc_wait_ack(device_t dev, uint8_t val, int amount);
74 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf,
76 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf,
78 static int asmc_fan_count(device_t dev);
79 static int asmc_fan_getvalue(device_t dev, const char *key, int fan);
80 static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
81 static int asmc_temp_getvalue(device_t dev, const char *key);
82 static int asmc_sms_read(device_t, const char *key, int16_t *val);
83 static void asmc_sms_calibrate(device_t dev);
84 static int asmc_sms_intrfast(void *arg);
86 static void asmc_sms_handler(void *arg);
88 static void asmc_sms_printintr(device_t dev, uint8_t);
89 static void asmc_sms_task(void *arg, int pending);
91 void asmc_dumpall(device_t);
92 static int asmc_key_dump(device_t, int);
98 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
99 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
100 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
101 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
102 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
103 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
104 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
105 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
106 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
107 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
108 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
109 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
110 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
113 const char *smc_model; /* smbios.system.product env var. */
114 const char *smc_desc; /* driver description */
116 /* Helper functions */
117 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
118 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
119 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
120 int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
121 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
122 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
123 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
124 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
125 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
126 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
127 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
128 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
130 const char *smc_temps[ASMC_TEMP_MAX];
131 const char *smc_tempnames[ASMC_TEMP_MAX];
132 const char *smc_tempdescs[ASMC_TEMP_MAX];
135 static struct asmc_model *asmc_match(device_t dev);
137 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
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 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
145 asmc_mbp_sysctl_light_right, \
146 asmc_mbp_sysctl_light_control
148 struct asmc_model asmc_models[] = {
150 "MacBook1,1", "Apple SMC MacBook Core Duo",
151 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
152 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
156 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
157 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
158 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
162 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
163 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
164 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
168 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
169 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
170 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
174 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
175 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
176 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
180 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
181 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
182 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
186 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
187 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
188 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
192 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
193 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
194 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
198 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
199 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
200 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
204 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
205 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
206 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
210 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
211 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
212 ASMC_MBP11_TEMPS, ASMC_MBP11_TEMPNAMES, ASMC_MBP11_TEMPDESCS
215 /* The Mac Mini has no SMS */
217 "Macmini1,1", "Apple SMC Mac Mini",
221 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
224 /* The Mac Mini 3,1 has no SMS */
226 "Macmini3,1", "Apple SMC Mac Mini 3,1",
230 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
233 /* Idem for the MacPro */
235 "MacPro2", "Apple SMC Mac Pro (8-core)",
239 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
242 /* Idem for the MacPro 2010*/
244 "MacPro5,1", "Apple SMC MacPro (2010)",
248 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
252 "MacBookAir1,1", "Apple SMC MacBook Air",
253 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
254 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
258 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
259 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
260 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
267 #undef ASMC_SMS_FUNCS
268 #undef ASMC_FAN_FUNCS
269 #undef ASMC_LIGHT_FUNCS
274 static device_method_t asmc_methods[] = {
275 DEVMETHOD(device_probe, asmc_probe),
276 DEVMETHOD(device_attach, asmc_attach),
277 DEVMETHOD(device_detach, asmc_detach),
282 static driver_t asmc_driver = {
285 sizeof(struct asmc_softc)
291 #define _COMPONENT ACPI_OEM
292 ACPI_MODULE_NAME("ASMC")
294 #define ASMC_DPRINTF(str) device_printf(dev, str)
296 #define ASMC_DPRINTF(str)
299 /* NB: can't be const */
300 static char *asmc_ids[] = { "APP0001", NULL };
302 static devclass_t asmc_devclass;
304 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
305 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
307 static struct asmc_model *
308 asmc_match(device_t dev)
313 model = getenv("smbios.system.product");
317 for (i = 0; asmc_models[i].smc_model; i++) {
318 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
320 return (&asmc_models[i]);
329 asmc_probe(device_t dev)
331 struct asmc_model *model;
333 if (resource_disabled("asmc", 0))
335 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
338 model = asmc_match(dev);
340 device_printf(dev, "model not recognized\n");
343 device_set_desc(dev, model->smc_desc);
345 return (BUS_PROBE_DEFAULT);
349 asmc_attach(device_t dev)
354 struct asmc_softc *sc = device_get_softc(dev);
355 struct sysctl_ctx_list *sysctlctx;
356 struct sysctl_oid *sysctlnode;
357 struct asmc_model *model;
359 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
360 &sc->sc_rid_port, RF_ACTIVE);
361 if (sc->sc_ioport == NULL) {
362 device_printf(dev, "unable to allocate IO port\n");
366 sysctlctx = device_get_sysctl_ctx(dev);
367 sysctlnode = device_get_sysctl_tree(dev);
369 model = asmc_match(dev);
371 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
373 sc->sc_model = model;
377 * dev.asmc.n.fan.* tree.
379 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
380 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
381 CTLFLAG_RD, 0, "Fan Root Tree");
383 for (i = 1; i <= sc->sc_nfan; i++) {
387 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
388 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
389 OID_AUTO, name, CTLFLAG_RD, 0,
392 SYSCTL_ADD_PROC(sysctlctx,
393 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
394 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
395 dev, j, model->smc_fan_id, "I",
398 SYSCTL_ADD_PROC(sysctlctx,
399 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
400 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
401 dev, j, model->smc_fan_speed, "I",
404 SYSCTL_ADD_PROC(sysctlctx,
405 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
406 OID_AUTO, "safespeed",
407 CTLTYPE_INT | CTLFLAG_RD,
408 dev, j, model->smc_fan_safespeed, "I",
409 "Fan safe speed in RPM");
411 SYSCTL_ADD_PROC(sysctlctx,
412 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
413 OID_AUTO, "minspeed",
414 CTLTYPE_INT | CTLFLAG_RW,
415 dev, j, model->smc_fan_minspeed, "I",
416 "Fan minimum speed in RPM");
418 SYSCTL_ADD_PROC(sysctlctx,
419 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
420 OID_AUTO, "maxspeed",
421 CTLTYPE_INT | CTLFLAG_RW,
422 dev, j, model->smc_fan_maxspeed, "I",
423 "Fan maximum speed in RPM");
425 SYSCTL_ADD_PROC(sysctlctx,
426 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
427 OID_AUTO, "targetspeed",
428 CTLTYPE_INT | CTLFLAG_RW,
429 dev, j, model->smc_fan_targetspeed, "I",
430 "Fan target speed in RPM");
434 * dev.asmc.n.temp tree.
436 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
437 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
438 CTLFLAG_RD, 0, "Temperature sensors");
440 for (i = 0; model->smc_temps[i]; i++) {
441 SYSCTL_ADD_PROC(sysctlctx,
442 SYSCTL_CHILDREN(sc->sc_temp_tree),
443 OID_AUTO, model->smc_tempnames[i],
444 CTLTYPE_INT | CTLFLAG_RD,
445 dev, i, asmc_temp_sysctl, "I",
446 model->smc_tempdescs[i]);
452 if (model->smc_light_left) {
453 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
454 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
455 CTLFLAG_RD, 0, "Keyboard backlight sensors");
457 SYSCTL_ADD_PROC(sysctlctx,
458 SYSCTL_CHILDREN(sc->sc_light_tree),
459 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
460 dev, 0, model->smc_light_left, "I",
461 "Keyboard backlight left sensor");
463 SYSCTL_ADD_PROC(sysctlctx,
464 SYSCTL_CHILDREN(sc->sc_light_tree),
465 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
466 dev, 0, model->smc_light_right, "I",
467 "Keyboard backlight right sensor");
469 SYSCTL_ADD_PROC(sysctlctx,
470 SYSCTL_CHILDREN(sc->sc_light_tree),
472 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
473 dev, 0, model->smc_light_control, "I",
474 "Keyboard backlight brightness control");
477 if (model->smc_sms_x == NULL)
481 * dev.asmc.n.sms tree.
483 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
484 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
485 CTLFLAG_RD, 0, "Sudden Motion Sensor");
487 SYSCTL_ADD_PROC(sysctlctx,
488 SYSCTL_CHILDREN(sc->sc_sms_tree),
489 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
490 dev, 0, model->smc_sms_x, "I",
491 "Sudden Motion Sensor X value");
493 SYSCTL_ADD_PROC(sysctlctx,
494 SYSCTL_CHILDREN(sc->sc_sms_tree),
495 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
496 dev, 0, model->smc_sms_y, "I",
497 "Sudden Motion Sensor Y value");
499 SYSCTL_ADD_PROC(sysctlctx,
500 SYSCTL_CHILDREN(sc->sc_sms_tree),
501 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
502 dev, 0, model->smc_sms_z, "I",
503 "Sudden Motion Sensor Z value");
506 * Need a taskqueue to send devctl_notify() events
507 * when the SMS interrupt us.
509 * PI_REALTIME is used due to the sensitivity of the
510 * interrupt. An interrupt from the SMS means that the
511 * disk heads should be turned off as quickly as possible.
513 * We only need to do this for the non INTR_FILTER case.
515 sc->sc_sms_tq = NULL;
517 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
518 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
519 taskqueue_thread_enqueue, &sc->sc_sms_tq);
520 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
521 device_get_nameunit(dev));
524 * Allocate an IRQ for the SMS.
527 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
528 &sc->sc_rid_irq, RF_ACTIVE);
529 if (sc->sc_irq == NULL) {
530 device_printf(dev, "unable to allocate IRQ resource\n");
535 ret = bus_setup_intr(dev, sc->sc_irq,
536 INTR_TYPE_MISC | INTR_MPSAFE,
538 asmc_sms_intrfast, asmc_sms_handler,
540 asmc_sms_intrfast, NULL,
542 dev, &sc->sc_cookie);
545 device_printf(dev, "unable to setup SMS IRQ\n");
551 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
553 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
555 mtx_destroy(&sc->sc_mtx);
557 taskqueue_free(sc->sc_sms_tq);
563 asmc_detach(device_t dev)
565 struct asmc_softc *sc = device_get_softc(dev);
568 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
569 taskqueue_free(sc->sc_sms_tq);
572 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
574 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
577 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
579 mtx_destroy(&sc->sc_mtx);
585 void asmc_dumpall(device_t dev)
589 /* XXX magic number */
590 for (i=0; i < 0x100; i++)
591 asmc_key_dump(dev, i);
596 asmc_init(device_t dev)
598 struct asmc_softc *sc = device_get_softc(dev);
602 if (sc->sc_model->smc_sms_x == NULL)
606 * We are ready to recieve interrupts from the SMS.
609 ASMC_DPRINTF(("intok key\n"));
610 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
614 * Initiate the polling intervals.
616 buf[0] = 20; /* msecs */
617 ASMC_DPRINTF(("low int key\n"));
618 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
621 buf[0] = 20; /* msecs */
622 ASMC_DPRINTF(("high int key\n"));
623 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
628 ASMC_DPRINTF(("sms low key\n"));
629 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
634 ASMC_DPRINTF(("sms high key\n"));
635 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
639 * I'm not sure what this key does, but it seems to be
643 ASMC_DPRINTF(("sms flag key\n"));
644 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
647 sc->sc_sms_intr_works = 0;
650 * Retry SMS initialization 1000 times
651 * (takes approx. 2 seconds in worst case)
653 for (i = 0; i < 1000; i++) {
654 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
655 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
657 sc->sc_sms_intr_works = 1;
660 buf[0] = ASMC_SMS_INIT1;
661 buf[1] = ASMC_SMS_INIT2;
662 ASMC_DPRINTF(("sms key\n"));
663 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
666 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
669 asmc_sms_calibrate(dev);
671 sc->sc_nfan = asmc_fan_count(dev);
672 if (sc->sc_nfan > ASMC_MAXFANS) {
673 device_printf(dev, "more than %d fans were detected. Please "
674 "report this.\n", ASMC_MAXFANS);
675 sc->sc_nfan = ASMC_MAXFANS;
680 * The number of keys is a 32 bit buffer
682 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
683 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
694 * We need to make sure that the SMC acks the byte sent.
695 * Just wait up to (amount * 10) ms.
698 asmc_wait_ack(device_t dev, uint8_t val, int amount)
700 struct asmc_softc *sc = device_get_softc(dev);
703 val = val & ASMC_STATUS_MASK;
705 for (i = 0; i < amount; i++) {
706 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
715 * We need to make sure that the SMC acks the byte sent.
716 * Just wait up to 100 ms.
719 asmc_wait(device_t dev, uint8_t val)
721 struct asmc_softc *sc;
723 if (asmc_wait_ack(dev, val, 1000) == 0)
726 sc = device_get_softc(dev);
727 val = val & ASMC_STATUS_MASK;
730 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
731 ASMC_CMDPORT_READ(sc));
737 * Send the given command, retrying up to 10 times if
738 * the acknowledgement fails.
741 asmc_command(device_t dev, uint8_t command) {
744 struct asmc_softc *sc = device_get_softc(dev);
746 for (i=0; i < 10; i++) {
747 ASMC_CMDPORT_WRITE(sc, command);
748 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
754 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
755 ASMC_CMDPORT_READ(sc));
761 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
763 int i, error = 1, try = 0;
764 struct asmc_softc *sc = device_get_softc(dev);
766 mtx_lock_spin(&sc->sc_mtx);
769 if (asmc_command(dev, ASMC_CMDREAD))
772 for (i = 0; i < 4; i++) {
773 ASMC_DATAPORT_WRITE(sc, key[i]);
774 if (asmc_wait(dev, 0x04))
778 ASMC_DATAPORT_WRITE(sc, len);
780 for (i = 0; i < len; i++) {
781 if (asmc_wait(dev, 0x05))
783 buf[i] = ASMC_DATAPORT_READ(sc);
789 if (++try < 10) goto begin;
790 device_printf(dev,"%s for key %s failed %d times, giving up\n",
794 mtx_unlock_spin(&sc->sc_mtx);
801 asmc_key_dump(device_t dev, int number)
803 struct asmc_softc *sc = device_get_softc(dev);
805 char type[7] = { 0 };
809 int i, error = 1, try = 0;
811 mtx_lock_spin(&sc->sc_mtx);
813 index[0] = (number >> 24) & 0xff;
814 index[1] = (number >> 16) & 0xff;
815 index[2] = (number >> 8) & 0xff;
816 index[3] = (number) & 0xff;
819 if (asmc_command(dev, 0x12))
822 for (i = 0; i < 4; i++) {
823 ASMC_DATAPORT_WRITE(sc, index[i]);
824 if (asmc_wait(dev, 0x04))
828 ASMC_DATAPORT_WRITE(sc, 4);
830 for (i = 0; i < 4; i++) {
831 if (asmc_wait(dev, 0x05))
833 key[i] = ASMC_DATAPORT_READ(sc);
837 if (asmc_command(dev, 0x13))
840 for (i = 0; i < 4; i++) {
841 ASMC_DATAPORT_WRITE(sc, key[i]);
842 if (asmc_wait(dev, 0x04))
846 ASMC_DATAPORT_WRITE(sc, 6);
848 for (i = 0; i < 6; i++) {
849 if (asmc_wait(dev, 0x05))
851 type[i] = ASMC_DATAPORT_READ(sc);
857 if (++try < 10) goto begin;
858 device_printf(dev,"%s for key %s failed %d times, giving up\n",
860 mtx_unlock_spin(&sc->sc_mtx);
865 mtx_unlock_spin(&sc->sc_mtx);
869 if (maxlen > sizeof(v)) {
871 "WARNING: cropping maxlen from %d to %zu\n",
875 for (i = 0; i < sizeof(v); i++) {
878 asmc_key_read(dev, key, v, maxlen);
879 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
880 "(len %d), data", number, key, type, maxlen);
881 for (i = 0; i < maxlen; i++) {
882 snprintf(buf2, sizeof(buf), " %02x", v[i]);
883 strlcat(buf, buf2, sizeof(buf));
885 strlcat(buf, " \n", sizeof(buf));
886 device_printf(dev, "%s", buf);
894 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
896 int i, error = -1, try = 0;
897 struct asmc_softc *sc = device_get_softc(dev);
899 mtx_lock_spin(&sc->sc_mtx);
902 ASMC_DPRINTF(("cmd port: cmd write\n"));
903 if (asmc_command(dev, ASMC_CMDWRITE))
906 ASMC_DPRINTF(("data port: key\n"));
907 for (i = 0; i < 4; i++) {
908 ASMC_DATAPORT_WRITE(sc, key[i]);
909 if (asmc_wait(dev, 0x04))
912 ASMC_DPRINTF(("data port: length\n"));
913 ASMC_DATAPORT_WRITE(sc, len);
915 ASMC_DPRINTF(("data port: buffer\n"));
916 for (i = 0; i < len; i++) {
917 if (asmc_wait(dev, 0x04))
919 ASMC_DATAPORT_WRITE(sc, buf[i]);
925 if (++try < 10) goto begin;
926 device_printf(dev,"%s for key %s failed %d times, giving up\n",
930 mtx_unlock_spin(&sc->sc_mtx);
937 * Fan control functions.
940 asmc_fan_count(device_t dev)
944 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
951 asmc_fan_getvalue(device_t dev, const char *key, int fan)
957 snprintf(fankey, sizeof(fankey), key, fan);
958 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
960 speed = (buf[0] << 6) | (buf[1] >> 2);
966 asmc_fan_getstring(device_t dev, const char *key, int fan)
972 snprintf(fankey, sizeof(fankey), key, fan);
973 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
981 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
991 snprintf(fankey, sizeof(fankey), key, fan);
992 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
999 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1001 device_t dev = (device_t) arg1;
1006 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1007 error = sysctl_handle_int(oidp, &v, 0, req);
1013 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1015 device_t dev = (device_t) arg1;
1020 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan);
1023 error = sysctl_handle_string(oidp, desc, 0, req);
1029 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1031 device_t dev = (device_t) arg1;
1036 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1037 error = sysctl_handle_int(oidp, &v, 0, req);
1044 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1046 device_t dev = (device_t) arg1;
1051 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1052 error = sysctl_handle_int(oidp, &v, 0, req);
1054 if (error == 0 && req->newptr != NULL) {
1055 unsigned int newspeed = v;
1056 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1063 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1065 device_t dev = (device_t) arg1;
1070 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1071 error = sysctl_handle_int(oidp, &v, 0, req);
1073 if (error == 0 && req->newptr != NULL) {
1074 unsigned int newspeed = v;
1075 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1082 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1084 device_t dev = (device_t) arg1;
1089 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1090 error = sysctl_handle_int(oidp, &v, 0, req);
1092 if (error == 0 && req->newptr != NULL) {
1093 unsigned int newspeed = v;
1094 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1101 * Temperature functions.
1104 asmc_temp_getvalue(device_t dev, const char *key)
1109 * Check for invalid temperatures.
1111 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1118 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1120 device_t dev = (device_t) arg1;
1121 struct asmc_softc *sc = device_get_softc(dev);
1124 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1125 error = sysctl_handle_int(oidp, &val, 0, req);
1131 * Sudden Motion Sensor functions.
1134 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1139 /* no need to do locking here as asmc_key_read() already does it */
1144 error = asmc_key_read(dev, key, buf, sizeof buf);
1147 device_printf(dev, "%s called with invalid argument %s\n",
1152 *val = ((int16_t)buf[0] << 8) | buf[1];
1158 asmc_sms_calibrate(device_t dev)
1160 struct asmc_softc *sc = device_get_softc(dev);
1162 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1163 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1164 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1168 asmc_sms_intrfast(void *arg)
1171 device_t dev = (device_t) arg;
1172 struct asmc_softc *sc = device_get_softc(dev);
1173 if (!sc->sc_sms_intr_works)
1174 return (FILTER_HANDLED);
1176 mtx_lock_spin(&sc->sc_mtx);
1177 type = ASMC_INTPORT_READ(sc);
1178 mtx_unlock_spin(&sc->sc_mtx);
1180 sc->sc_sms_intrtype = type;
1181 asmc_sms_printintr(dev, type);
1184 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1186 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1188 return (FILTER_HANDLED);
1193 asmc_sms_handler(void *arg)
1195 struct asmc_softc *sc = device_get_softc(arg);
1197 asmc_sms_task(sc, 0);
1203 asmc_sms_printintr(device_t dev, uint8_t type)
1207 case ASMC_SMS_INTFF:
1208 device_printf(dev, "WARNING: possible free fall!\n");
1210 case ASMC_SMS_INTHA:
1211 device_printf(dev, "WARNING: high acceleration detected!\n");
1213 case ASMC_SMS_INTSH:
1214 device_printf(dev, "WARNING: possible shock!\n");
1217 device_printf(dev, "%s unknown interrupt\n", __func__);
1222 asmc_sms_task(void *arg, int pending)
1224 struct asmc_softc *sc = (struct asmc_softc *)arg;
1228 switch (sc->sc_sms_intrtype) {
1229 case ASMC_SMS_INTFF:
1232 case ASMC_SMS_INTHA:
1235 case ASMC_SMS_INTSH:
1242 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1243 devctl_notify("ACPI", "asmc", "SMS", notify);
1247 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1249 device_t dev = (device_t) arg1;
1254 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1256 error = sysctl_handle_int(oidp, &v, 0, req);
1262 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1264 device_t dev = (device_t) arg1;
1269 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1271 error = sysctl_handle_int(oidp, &v, 0, req);
1277 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1279 device_t dev = (device_t) arg1;
1284 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1286 error = sysctl_handle_int(oidp, &v, 0, req);
1292 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1294 device_t dev = (device_t) arg1;
1299 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1301 error = sysctl_handle_int(oidp, &v, 0, req);
1307 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1309 device_t dev = (device_t) arg1;
1314 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1316 error = sysctl_handle_int(oidp, &v, 0, req);
1322 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1324 device_t dev = (device_t) arg1;
1327 static unsigned int level;
1331 error = sysctl_handle_int(oidp, &v, 0, req);
1333 if (error == 0 && req->newptr != NULL) {
1334 if (v < 0 || v > 255)
1339 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);