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_temp_getvalue(device_t dev, const char *key);
81 static int asmc_sms_read(device_t, const char *key, int16_t *val);
82 static void asmc_sms_calibrate(device_t dev);
83 static int asmc_sms_intrfast(void *arg);
85 static void asmc_sms_handler(void *arg);
87 static void asmc_sms_printintr(device_t dev, uint8_t);
88 static void asmc_sms_task(void *arg, int pending);
93 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
94 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
95 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
96 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
97 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
98 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
99 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
100 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
101 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
102 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
103 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
104 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
107 const char *smc_model; /* smbios.system.product env var. */
108 const char *smc_desc; /* driver description */
110 /* Helper functions */
111 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
112 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
113 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
114 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
115 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
116 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
117 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
118 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
119 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
120 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
121 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
123 const char *smc_temps[ASMC_TEMP_MAX];
124 const char *smc_tempnames[ASMC_TEMP_MAX];
125 const char *smc_tempdescs[ASMC_TEMP_MAX];
128 static struct asmc_model *asmc_match(device_t dev);
130 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
133 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
134 asmc_mb_sysctl_fanminspeed, \
135 asmc_mb_sysctl_fanmaxspeed, \
136 asmc_mb_sysctl_fantargetspeed
137 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
138 asmc_mbp_sysctl_light_right, \
139 asmc_mbp_sysctl_light_control
141 struct asmc_model asmc_models[] = {
143 "MacBook1,1", "Apple SMC MacBook Core Duo",
144 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
145 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
149 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
150 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
151 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
155 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
156 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
157 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
161 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
162 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
163 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
167 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
168 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
169 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
173 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
174 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
175 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
179 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
180 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
181 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
185 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
186 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
187 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
191 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
192 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
193 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
196 /* The Mac Mini has no SMS */
198 "Macmini1,1", "Apple SMC Mac Mini",
202 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
205 /* Idem for the MacPro */
207 "MacPro2", "Apple SMC Mac Pro (8-core)",
211 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
215 "MacBookAir1,1", "Apple SMC MacBook Air",
216 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
217 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
224 #undef ASMC_SMS_FUNCS
225 #undef ASMC_FAN_FUNCS
226 #undef ASMC_LIGHT_FUNCS
231 static device_method_t asmc_methods[] = {
232 DEVMETHOD(device_probe, asmc_probe),
233 DEVMETHOD(device_attach, asmc_attach),
234 DEVMETHOD(device_detach, asmc_detach),
239 static driver_t asmc_driver = {
242 sizeof(struct asmc_softc)
248 #define _COMPONENT ACPI_OEM
249 ACPI_MODULE_NAME("ASMC")
251 #define ASMC_DPRINTF(str) device_printf(dev, str)
253 #define ASMC_DPRINTF(str)
256 /* NB: can't be const */
257 static char *asmc_ids[] = { "APP0001", NULL };
259 static devclass_t asmc_devclass;
261 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
262 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
264 static struct asmc_model *
265 asmc_match(device_t dev)
270 model = getenv("smbios.system.product");
274 for (i = 0; asmc_models[i].smc_model; i++) {
275 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
277 return (&asmc_models[i]);
286 asmc_probe(device_t dev)
288 struct asmc_model *model;
290 if (resource_disabled("asmc", 0))
292 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
295 model = asmc_match(dev);
297 device_printf(dev, "model not recognized\n");
300 device_set_desc(dev, model->smc_desc);
302 return (BUS_PROBE_DEFAULT);
306 asmc_attach(device_t dev)
311 struct asmc_softc *sc = device_get_softc(dev);
312 struct sysctl_ctx_list *sysctlctx;
313 struct sysctl_oid *sysctlnode;
314 struct asmc_model *model;
316 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
317 &sc->sc_rid_port, RF_ACTIVE);
318 if (sc->sc_ioport == NULL) {
319 device_printf(dev, "unable to allocate IO port\n");
323 sysctlctx = device_get_sysctl_ctx(dev);
324 sysctlnode = device_get_sysctl_tree(dev);
326 model = asmc_match(dev);
328 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
330 sc->sc_model = model;
334 * dev.asmc.n.fan.* tree.
336 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
337 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
338 CTLFLAG_RD, 0, "Fan Root Tree");
340 for (i = 1; i <= sc->sc_nfan; i++) {
344 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
345 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
346 OID_AUTO, name, CTLFLAG_RD, 0,
349 SYSCTL_ADD_PROC(sysctlctx,
350 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
351 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
352 dev, j, model->smc_fan_speed, "I",
355 SYSCTL_ADD_PROC(sysctlctx,
356 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
357 OID_AUTO, "safespeed",
358 CTLTYPE_INT | CTLFLAG_RD,
359 dev, j, model->smc_fan_safespeed, "I",
360 "Fan safe speed in RPM");
362 SYSCTL_ADD_PROC(sysctlctx,
363 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
364 OID_AUTO, "minspeed",
365 CTLTYPE_INT | CTLFLAG_RD,
366 dev, j, model->smc_fan_minspeed, "I",
367 "Fan minimum speed in RPM");
369 SYSCTL_ADD_PROC(sysctlctx,
370 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
371 OID_AUTO, "maxspeed",
372 CTLTYPE_INT | CTLFLAG_RD,
373 dev, j, model->smc_fan_maxspeed, "I",
374 "Fan maximum speed in RPM");
376 SYSCTL_ADD_PROC(sysctlctx,
377 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
378 OID_AUTO, "targetspeed",
379 CTLTYPE_INT | CTLFLAG_RD,
380 dev, j, model->smc_fan_targetspeed, "I",
381 "Fan target speed in RPM");
385 * dev.asmc.n.temp tree.
387 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
388 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
389 CTLFLAG_RD, 0, "Temperature sensors");
391 for (i = 0; model->smc_temps[i]; i++) {
392 SYSCTL_ADD_PROC(sysctlctx,
393 SYSCTL_CHILDREN(sc->sc_temp_tree),
394 OID_AUTO, model->smc_tempnames[i],
395 CTLTYPE_INT | CTLFLAG_RD,
396 dev, i, asmc_temp_sysctl, "I",
397 model->smc_tempdescs[i]);
403 if (model->smc_light_left) {
404 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
405 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
406 CTLFLAG_RD, 0, "Keyboard backlight sensors");
408 SYSCTL_ADD_PROC(sysctlctx,
409 SYSCTL_CHILDREN(sc->sc_light_tree),
410 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
411 dev, 0, model->smc_light_left, "I",
412 "Keyboard backlight left sensor");
414 SYSCTL_ADD_PROC(sysctlctx,
415 SYSCTL_CHILDREN(sc->sc_light_tree),
416 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
417 dev, 0, model->smc_light_right, "I",
418 "Keyboard backlight right sensor");
420 SYSCTL_ADD_PROC(sysctlctx,
421 SYSCTL_CHILDREN(sc->sc_light_tree),
422 OID_AUTO, "control", CTLTYPE_INT | CTLFLAG_RW,
423 dev, 0, model->smc_light_control, "I",
424 "Keyboard backlight brightness control");
427 if (model->smc_sms_x == NULL)
431 * dev.asmc.n.sms tree.
433 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
434 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
435 CTLFLAG_RD, 0, "Sudden Motion Sensor");
437 SYSCTL_ADD_PROC(sysctlctx,
438 SYSCTL_CHILDREN(sc->sc_sms_tree),
439 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
440 dev, 0, model->smc_sms_x, "I",
441 "Sudden Motion Sensor X value");
443 SYSCTL_ADD_PROC(sysctlctx,
444 SYSCTL_CHILDREN(sc->sc_sms_tree),
445 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
446 dev, 0, model->smc_sms_y, "I",
447 "Sudden Motion Sensor Y value");
449 SYSCTL_ADD_PROC(sysctlctx,
450 SYSCTL_CHILDREN(sc->sc_sms_tree),
451 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
452 dev, 0, model->smc_sms_z, "I",
453 "Sudden Motion Sensor Z value");
456 * Need a taskqueue to send devctl_notify() events
457 * when the SMS interrupt us.
459 * PI_REALTIME is used due to the sensitivity of the
460 * interrupt. An interrupt from the SMS means that the
461 * disk heads should be turned off as quickly as possible.
463 * We only need to do this for the non INTR_FILTER case.
465 sc->sc_sms_tq = NULL;
467 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
468 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
469 taskqueue_thread_enqueue, &sc->sc_sms_tq);
470 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
471 device_get_nameunit(dev));
474 * Allocate an IRQ for the SMS.
477 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
478 &sc->sc_rid_irq, RF_ACTIVE);
479 if (sc->sc_irq == NULL) {
480 device_printf(dev, "unable to allocate IRQ resource\n");
485 ret = bus_setup_intr(dev, sc->sc_irq,
486 INTR_TYPE_MISC | INTR_MPSAFE,
488 asmc_sms_intrfast, asmc_sms_handler,
490 asmc_sms_intrfast, NULL,
492 dev, &sc->sc_cookie);
495 device_printf(dev, "unable to setup SMS IRQ\n");
501 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
503 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
505 mtx_destroy(&sc->sc_mtx);
507 taskqueue_free(sc->sc_sms_tq);
513 asmc_detach(device_t dev)
515 struct asmc_softc *sc = device_get_softc(dev);
518 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
519 taskqueue_free(sc->sc_sms_tq);
522 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
524 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
527 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
529 mtx_destroy(&sc->sc_mtx);
535 asmc_init(device_t dev)
537 struct asmc_softc *sc = device_get_softc(dev);
541 if (sc->sc_model->smc_sms_x == NULL)
545 * We are ready to recieve interrupts from the SMS.
548 ASMC_DPRINTF(("intok key\n"));
549 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
553 * Initiate the polling intervals.
555 buf[0] = 20; /* msecs */
556 ASMC_DPRINTF(("low int key\n"));
557 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
560 buf[0] = 20; /* msecs */
561 ASMC_DPRINTF(("high int key\n"));
562 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
567 ASMC_DPRINTF(("sms low key\n"));
568 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
573 ASMC_DPRINTF(("sms high key\n"));
574 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
578 * I'm not sure what this key does, but it seems to be
582 ASMC_DPRINTF(("sms flag key\n"));
583 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
587 * Wait up to 5 seconds for SMS initialization.
589 for (i = 0; i < 10000; i++) {
590 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
591 (buf[0] != 0x00 || buf[1] != 0x00)) {
595 buf[0] = ASMC_SMS_INIT1;
596 buf[1] = ASMC_SMS_INIT2;
597 ASMC_DPRINTF(("sms key\n"));
598 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
601 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
604 asmc_sms_calibrate(dev);
606 sc->sc_nfan = asmc_fan_count(dev);
607 if (sc->sc_nfan > ASMC_MAXFANS) {
608 device_printf(dev, "more than %d fans were detected. Please "
609 "report this.\n", ASMC_MAXFANS);
610 sc->sc_nfan = ASMC_MAXFANS;
615 * XXX: The number of keys is a 32 bit buffer, but
616 * right now Apple only uses the last 8 bit.
618 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
619 device_printf(dev, "number of keys: %d\n", buf[3]);
626 * We need to make sure that the SMC acks the byte sent.
627 * Just wait up to (amount * 10) ms.
630 asmc_wait_ack(device_t dev, uint8_t val, int amount)
632 struct asmc_softc *sc = device_get_softc(dev);
635 val = val & ASMC_STATUS_MASK;
637 for (i = 0; i < amount; i++) {
638 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
647 * We need to make sure that the SMC acks the byte sent.
648 * Just wait up to 100 ms.
651 asmc_wait(device_t dev, uint8_t val)
653 struct asmc_softc *sc;
655 if (asmc_wait_ack(dev, val, 1000) == 0)
658 sc = device_get_softc(dev);
659 val = val & ASMC_STATUS_MASK;
662 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
663 ASMC_CMDPORT_READ(sc));
669 * Send the given command, retrying up to 10 times if
670 * the acknowledgement fails.
673 asmc_command(device_t dev, uint8_t command) {
676 struct asmc_softc *sc = device_get_softc(dev);
678 for (i=0; i < 10; i++) {
679 ASMC_CMDPORT_WRITE(sc, command);
680 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
686 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
687 ASMC_CMDPORT_READ(sc));
693 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
695 int i, error = 1, try = 0;
696 struct asmc_softc *sc = device_get_softc(dev);
698 mtx_lock_spin(&sc->sc_mtx);
701 if (asmc_command(dev, ASMC_CMDREAD))
704 for (i = 0; i < 4; i++) {
705 ASMC_DATAPORT_WRITE(sc, key[i]);
706 if (asmc_wait(dev, 0x04))
710 ASMC_DATAPORT_WRITE(sc, len);
712 for (i = 0; i < len; i++) {
713 if (asmc_wait(dev, 0x05))
715 buf[i] = ASMC_DATAPORT_READ(sc);
721 if (++try < 10) goto begin;
722 device_printf(dev,"%s for key %s failed %d times, giving up\n",
726 mtx_unlock_spin(&sc->sc_mtx);
732 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
734 int i, error = -1, try = 0;
735 struct asmc_softc *sc = device_get_softc(dev);
737 mtx_lock_spin(&sc->sc_mtx);
740 ASMC_DPRINTF(("cmd port: cmd write\n"));
741 if (asmc_command(dev, ASMC_CMDWRITE))
744 ASMC_DPRINTF(("data port: key\n"));
745 for (i = 0; i < 4; i++) {
746 ASMC_DATAPORT_WRITE(sc, key[i]);
747 if (asmc_wait(dev, 0x04))
750 ASMC_DPRINTF(("data port: length\n"));
751 ASMC_DATAPORT_WRITE(sc, len);
753 ASMC_DPRINTF(("data port: buffer\n"));
754 for (i = 0; i < len; i++) {
755 if (asmc_wait(dev, 0x04))
757 ASMC_DATAPORT_WRITE(sc, buf[i]);
763 if (++try < 10) goto begin;
764 device_printf(dev,"%s for key %s failed %d times, giving up\n",
768 mtx_unlock_spin(&sc->sc_mtx);
775 * Fan control functions.
778 asmc_fan_count(device_t dev)
782 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0)
789 asmc_fan_getvalue(device_t dev, const char *key, int fan)
795 snprintf(fankey, sizeof(fankey), key, fan);
796 if (asmc_key_read(dev, fankey, buf, 2) < 0)
798 speed = (buf[0] << 6) | (buf[1] >> 2);
804 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
806 device_t dev = (device_t) arg1;
811 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
812 error = sysctl_handle_int(oidp, &v, 0, req);
818 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
820 device_t dev = (device_t) arg1;
825 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
826 error = sysctl_handle_int(oidp, &v, 0, req);
833 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
835 device_t dev = (device_t) arg1;
840 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
841 error = sysctl_handle_int(oidp, &v, 0, req);
847 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
849 device_t dev = (device_t) arg1;
854 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
855 error = sysctl_handle_int(oidp, &v, 0, req);
861 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
863 device_t dev = (device_t) arg1;
868 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
869 error = sysctl_handle_int(oidp, &v, 0, req);
875 * Temperature functions.
878 asmc_temp_getvalue(device_t dev, const char *key)
883 * Check for invalid temperatures.
885 if (asmc_key_read(dev, key, buf, 2) < 0)
892 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
894 device_t dev = (device_t) arg1;
895 struct asmc_softc *sc = device_get_softc(dev);
898 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
899 error = sysctl_handle_int(oidp, &val, 0, req);
905 * Sudden Motion Sensor functions.
908 asmc_sms_read(device_t dev, const char *key, int16_t *val)
913 /* no need to do locking here as asmc_key_read() already does it */
918 error = asmc_key_read(dev, key, buf, 2);
921 device_printf(dev, "%s called with invalid argument %s\n",
926 *val = ((int16_t)buf[0] << 8) | buf[1];
932 asmc_sms_calibrate(device_t dev)
934 struct asmc_softc *sc = device_get_softc(dev);
936 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
937 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
938 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
942 asmc_sms_intrfast(void *arg)
945 device_t dev = (device_t) arg;
946 struct asmc_softc *sc = device_get_softc(dev);
948 mtx_lock_spin(&sc->sc_mtx);
949 type = ASMC_INTPORT_READ(sc);
950 mtx_unlock_spin(&sc->sc_mtx);
952 sc->sc_sms_intrtype = type;
953 asmc_sms_printintr(dev, type);
956 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
958 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
960 return (FILTER_HANDLED);
965 asmc_sms_handler(void *arg)
967 struct asmc_softc *sc = device_get_softc(arg);
969 asmc_sms_task(sc, 0);
975 asmc_sms_printintr(device_t dev, uint8_t type)
980 device_printf(dev, "WARNING: possible free fall!\n");
983 device_printf(dev, "WARNING: high acceleration detected!\n");
986 device_printf(dev, "WARNING: possible shock!\n");
989 device_printf(dev, "%s unknown interrupt\n", __func__);
994 asmc_sms_task(void *arg, int pending)
996 struct asmc_softc *sc = (struct asmc_softc *)arg;
1000 switch (sc->sc_sms_intrtype) {
1001 case ASMC_SMS_INTFF:
1004 case ASMC_SMS_INTHA:
1007 case ASMC_SMS_INTSH:
1014 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1015 devctl_notify("ACPI", "asmc", "SMS", notify);
1019 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1021 device_t dev = (device_t) arg1;
1026 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1028 error = sysctl_handle_int(oidp, &v, 0, req);
1034 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1036 device_t dev = (device_t) arg1;
1041 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1043 error = sysctl_handle_int(oidp, &v, 0, req);
1049 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1051 device_t dev = (device_t) arg1;
1056 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1058 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1064 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1066 device_t dev = (device_t) arg1;
1071 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6);
1073 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1079 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1081 device_t dev = (device_t) arg1;
1086 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6);
1088 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1094 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1096 device_t dev = (device_t) arg1;
1102 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1103 if (error == 0 && req->newptr != NULL) {
1104 level = *(unsigned int *)req->newptr;
1110 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);