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);
90 void asmc_dumpall(device_t);
91 static int asmc_key_dump(device_t, int);
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_speed)(SYSCTL_HANDLER_ARGS);
119 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
120 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
121 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
122 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
123 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
124 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
125 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
127 const char *smc_temps[ASMC_TEMP_MAX];
128 const char *smc_tempnames[ASMC_TEMP_MAX];
129 const char *smc_tempdescs[ASMC_TEMP_MAX];
132 static struct asmc_model *asmc_match(device_t dev);
134 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
137 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
138 asmc_mb_sysctl_fanminspeed, \
139 asmc_mb_sysctl_fanmaxspeed, \
140 asmc_mb_sysctl_fantargetspeed
141 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
142 asmc_mbp_sysctl_light_right, \
143 asmc_mbp_sysctl_light_control
145 struct asmc_model asmc_models[] = {
147 "MacBook1,1", "Apple SMC MacBook Core Duo",
148 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
149 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
153 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
154 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
155 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
159 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
160 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
161 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
165 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
166 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
167 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
171 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
172 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
173 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
177 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
178 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
179 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
183 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
184 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
185 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
189 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
190 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
191 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
195 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
196 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
197 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
200 /* The Mac Mini has no SMS */
202 "Macmini1,1", "Apple SMC Mac Mini",
206 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
209 /* The Mac Mini 3,1 has no SMS */
211 "Macmini3,1", "Apple SMC Mac Mini 3,1",
215 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
218 /* Idem for the MacPro */
220 "MacPro2", "Apple SMC Mac Pro (8-core)",
224 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
228 "MacBookAir1,1", "Apple SMC MacBook Air",
229 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
230 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
237 #undef ASMC_SMS_FUNCS
238 #undef ASMC_FAN_FUNCS
239 #undef ASMC_LIGHT_FUNCS
244 static device_method_t asmc_methods[] = {
245 DEVMETHOD(device_probe, asmc_probe),
246 DEVMETHOD(device_attach, asmc_attach),
247 DEVMETHOD(device_detach, asmc_detach),
252 static driver_t asmc_driver = {
255 sizeof(struct asmc_softc)
261 #define _COMPONENT ACPI_OEM
262 ACPI_MODULE_NAME("ASMC")
264 #define ASMC_DPRINTF(str) device_printf(dev, str)
266 #define ASMC_DPRINTF(str)
269 /* NB: can't be const */
270 static char *asmc_ids[] = { "APP0001", NULL };
272 static devclass_t asmc_devclass;
274 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
275 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
277 static struct asmc_model *
278 asmc_match(device_t dev)
283 model = getenv("smbios.system.product");
287 for (i = 0; asmc_models[i].smc_model; i++) {
288 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
290 return (&asmc_models[i]);
299 asmc_probe(device_t dev)
301 struct asmc_model *model;
303 if (resource_disabled("asmc", 0))
305 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
308 model = asmc_match(dev);
310 device_printf(dev, "model not recognized\n");
313 device_set_desc(dev, model->smc_desc);
315 return (BUS_PROBE_DEFAULT);
319 asmc_attach(device_t dev)
324 struct asmc_softc *sc = device_get_softc(dev);
325 struct sysctl_ctx_list *sysctlctx;
326 struct sysctl_oid *sysctlnode;
327 struct asmc_model *model;
329 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
330 &sc->sc_rid_port, RF_ACTIVE);
331 if (sc->sc_ioport == NULL) {
332 device_printf(dev, "unable to allocate IO port\n");
336 sysctlctx = device_get_sysctl_ctx(dev);
337 sysctlnode = device_get_sysctl_tree(dev);
339 model = asmc_match(dev);
341 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
343 sc->sc_model = model;
347 * dev.asmc.n.fan.* tree.
349 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
350 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
351 CTLFLAG_RD, 0, "Fan Root Tree");
353 for (i = 1; i <= sc->sc_nfan; i++) {
357 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
358 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
359 OID_AUTO, name, CTLFLAG_RD, 0,
362 SYSCTL_ADD_PROC(sysctlctx,
363 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
364 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
365 dev, j, model->smc_fan_speed, "I",
368 SYSCTL_ADD_PROC(sysctlctx,
369 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
370 OID_AUTO, "safespeed",
371 CTLTYPE_INT | CTLFLAG_RD,
372 dev, j, model->smc_fan_safespeed, "I",
373 "Fan safe speed in RPM");
375 SYSCTL_ADD_PROC(sysctlctx,
376 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
377 OID_AUTO, "minspeed",
378 CTLTYPE_INT | CTLFLAG_RD,
379 dev, j, model->smc_fan_minspeed, "I",
380 "Fan minimum speed in RPM");
382 SYSCTL_ADD_PROC(sysctlctx,
383 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
384 OID_AUTO, "maxspeed",
385 CTLTYPE_INT | CTLFLAG_RD,
386 dev, j, model->smc_fan_maxspeed, "I",
387 "Fan maximum speed in RPM");
389 SYSCTL_ADD_PROC(sysctlctx,
390 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
391 OID_AUTO, "targetspeed",
392 CTLTYPE_INT | CTLFLAG_RD,
393 dev, j, model->smc_fan_targetspeed, "I",
394 "Fan target speed in RPM");
398 * dev.asmc.n.temp tree.
400 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
401 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
402 CTLFLAG_RD, 0, "Temperature sensors");
404 for (i = 0; model->smc_temps[i]; i++) {
405 SYSCTL_ADD_PROC(sysctlctx,
406 SYSCTL_CHILDREN(sc->sc_temp_tree),
407 OID_AUTO, model->smc_tempnames[i],
408 CTLTYPE_INT | CTLFLAG_RD,
409 dev, i, asmc_temp_sysctl, "I",
410 model->smc_tempdescs[i]);
416 if (model->smc_light_left) {
417 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
418 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
419 CTLFLAG_RD, 0, "Keyboard backlight sensors");
421 SYSCTL_ADD_PROC(sysctlctx,
422 SYSCTL_CHILDREN(sc->sc_light_tree),
423 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
424 dev, 0, model->smc_light_left, "I",
425 "Keyboard backlight left sensor");
427 SYSCTL_ADD_PROC(sysctlctx,
428 SYSCTL_CHILDREN(sc->sc_light_tree),
429 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
430 dev, 0, model->smc_light_right, "I",
431 "Keyboard backlight right sensor");
433 SYSCTL_ADD_PROC(sysctlctx,
434 SYSCTL_CHILDREN(sc->sc_light_tree),
436 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
437 dev, 0, model->smc_light_control, "I",
438 "Keyboard backlight brightness control");
441 if (model->smc_sms_x == NULL)
445 * dev.asmc.n.sms tree.
447 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
448 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
449 CTLFLAG_RD, 0, "Sudden Motion Sensor");
451 SYSCTL_ADD_PROC(sysctlctx,
452 SYSCTL_CHILDREN(sc->sc_sms_tree),
453 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
454 dev, 0, model->smc_sms_x, "I",
455 "Sudden Motion Sensor X value");
457 SYSCTL_ADD_PROC(sysctlctx,
458 SYSCTL_CHILDREN(sc->sc_sms_tree),
459 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
460 dev, 0, model->smc_sms_y, "I",
461 "Sudden Motion Sensor Y value");
463 SYSCTL_ADD_PROC(sysctlctx,
464 SYSCTL_CHILDREN(sc->sc_sms_tree),
465 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
466 dev, 0, model->smc_sms_z, "I",
467 "Sudden Motion Sensor Z value");
470 * Need a taskqueue to send devctl_notify() events
471 * when the SMS interrupt us.
473 * PI_REALTIME is used due to the sensitivity of the
474 * interrupt. An interrupt from the SMS means that the
475 * disk heads should be turned off as quickly as possible.
477 * We only need to do this for the non INTR_FILTER case.
479 sc->sc_sms_tq = NULL;
481 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
482 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
483 taskqueue_thread_enqueue, &sc->sc_sms_tq);
484 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
485 device_get_nameunit(dev));
488 * Allocate an IRQ for the SMS.
491 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
492 &sc->sc_rid_irq, RF_ACTIVE);
493 if (sc->sc_irq == NULL) {
494 device_printf(dev, "unable to allocate IRQ resource\n");
499 ret = bus_setup_intr(dev, sc->sc_irq,
500 INTR_TYPE_MISC | INTR_MPSAFE,
502 asmc_sms_intrfast, asmc_sms_handler,
504 asmc_sms_intrfast, NULL,
506 dev, &sc->sc_cookie);
509 device_printf(dev, "unable to setup SMS IRQ\n");
515 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
517 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
519 mtx_destroy(&sc->sc_mtx);
521 taskqueue_free(sc->sc_sms_tq);
527 asmc_detach(device_t dev)
529 struct asmc_softc *sc = device_get_softc(dev);
532 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
533 taskqueue_free(sc->sc_sms_tq);
536 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
538 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
541 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
543 mtx_destroy(&sc->sc_mtx);
549 void asmc_dumpall(device_t dev)
553 /* XXX magic number */
554 for (i=0; i < 0x100; i++)
555 asmc_key_dump(dev, i);
560 asmc_init(device_t dev)
562 struct asmc_softc *sc = device_get_softc(dev);
566 if (sc->sc_model->smc_sms_x == NULL)
570 * We are ready to recieve interrupts from the SMS.
573 ASMC_DPRINTF(("intok key\n"));
574 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
578 * Initiate the polling intervals.
580 buf[0] = 20; /* msecs */
581 ASMC_DPRINTF(("low int key\n"));
582 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
585 buf[0] = 20; /* msecs */
586 ASMC_DPRINTF(("high int key\n"));
587 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
592 ASMC_DPRINTF(("sms low key\n"));
593 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
598 ASMC_DPRINTF(("sms high key\n"));
599 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
603 * I'm not sure what this key does, but it seems to be
607 ASMC_DPRINTF(("sms flag key\n"));
608 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
611 sc->sc_sms_intr_works = 0;
614 * Retry SMS initialization 1000 times
615 * (takes approx. 2 seconds in worst case)
617 for (i = 0; i < 1000; i++) {
618 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
619 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
621 sc->sc_sms_intr_works = 1;
624 buf[0] = ASMC_SMS_INIT1;
625 buf[1] = ASMC_SMS_INIT2;
626 ASMC_DPRINTF(("sms key\n"));
627 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
630 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
633 asmc_sms_calibrate(dev);
635 sc->sc_nfan = asmc_fan_count(dev);
636 if (sc->sc_nfan > ASMC_MAXFANS) {
637 device_printf(dev, "more than %d fans were detected. Please "
638 "report this.\n", ASMC_MAXFANS);
639 sc->sc_nfan = ASMC_MAXFANS;
644 * XXX: The number of keys is a 32 bit buffer, but
645 * right now Apple only uses the last 8 bit.
647 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
648 device_printf(dev, "number of keys: %d\n", buf[3]);
659 * We need to make sure that the SMC acks the byte sent.
660 * Just wait up to (amount * 10) ms.
663 asmc_wait_ack(device_t dev, uint8_t val, int amount)
665 struct asmc_softc *sc = device_get_softc(dev);
668 val = val & ASMC_STATUS_MASK;
670 for (i = 0; i < amount; i++) {
671 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
680 * We need to make sure that the SMC acks the byte sent.
681 * Just wait up to 100 ms.
684 asmc_wait(device_t dev, uint8_t val)
686 struct asmc_softc *sc;
688 if (asmc_wait_ack(dev, val, 1000) == 0)
691 sc = device_get_softc(dev);
692 val = val & ASMC_STATUS_MASK;
695 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
696 ASMC_CMDPORT_READ(sc));
702 * Send the given command, retrying up to 10 times if
703 * the acknowledgement fails.
706 asmc_command(device_t dev, uint8_t command) {
709 struct asmc_softc *sc = device_get_softc(dev);
711 for (i=0; i < 10; i++) {
712 ASMC_CMDPORT_WRITE(sc, command);
713 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
719 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
720 ASMC_CMDPORT_READ(sc));
726 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
728 int i, error = 1, try = 0;
729 struct asmc_softc *sc = device_get_softc(dev);
731 mtx_lock_spin(&sc->sc_mtx);
734 if (asmc_command(dev, ASMC_CMDREAD))
737 for (i = 0; i < 4; i++) {
738 ASMC_DATAPORT_WRITE(sc, key[i]);
739 if (asmc_wait(dev, 0x04))
743 ASMC_DATAPORT_WRITE(sc, len);
745 for (i = 0; i < len; i++) {
746 if (asmc_wait(dev, 0x05))
748 buf[i] = ASMC_DATAPORT_READ(sc);
754 if (++try < 10) goto begin;
755 device_printf(dev,"%s for key %s failed %d times, giving up\n",
759 mtx_unlock_spin(&sc->sc_mtx);
766 asmc_key_dump(device_t dev, int number)
768 struct asmc_softc *sc = device_get_softc(dev);
770 char type[7] = { 0 };
774 int i, error = 1, try = 0;
776 mtx_lock_spin(&sc->sc_mtx);
778 index[0] = (number >> 24) & 0xff;
779 index[1] = (number >> 16) & 0xff;
780 index[2] = (number >> 8) & 0xff;
781 index[3] = (number) & 0xff;
784 if (asmc_command(dev, 0x12))
787 for (i = 0; i < 4; i++) {
788 ASMC_DATAPORT_WRITE(sc, index[i]);
789 if (asmc_wait(dev, 0x04))
793 ASMC_DATAPORT_WRITE(sc, 4);
795 for (i = 0; i < 4; i++) {
796 if (asmc_wait(dev, 0x05))
798 key[i] = ASMC_DATAPORT_READ(sc);
802 if (asmc_command(dev, 0x13))
805 for (i = 0; i < 4; i++) {
806 ASMC_DATAPORT_WRITE(sc, key[i]);
807 if (asmc_wait(dev, 0x04))
811 ASMC_DATAPORT_WRITE(sc, 6);
813 for (i = 0; i < 6; i++) {
814 if (asmc_wait(dev, 0x05))
816 type[i] = ASMC_DATAPORT_READ(sc);
822 if (++try < 10) goto begin;
823 device_printf(dev,"%s for key %s failed %d times, giving up\n",
825 mtx_unlock_spin(&sc->sc_mtx);
830 mtx_unlock_spin(&sc->sc_mtx);
834 if (maxlen > sizeof(v)) {
836 "WARNING: cropping maxlen from %d to %zu\n",
840 for (i = 0; i < sizeof(v); i++) {
843 asmc_key_read(dev, key, v, maxlen);
844 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
845 "(len %d), data", number, key, type, maxlen);
846 for (i = 0; i < maxlen; i++) {
847 snprintf(buf2, sizeof(buf), " %02x", v[i]);
848 strlcat(buf, buf2, sizeof(buf));
850 strlcat(buf, " \n", sizeof(buf));
851 device_printf(dev, "%s", buf);
859 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
861 int i, error = -1, try = 0;
862 struct asmc_softc *sc = device_get_softc(dev);
864 mtx_lock_spin(&sc->sc_mtx);
867 ASMC_DPRINTF(("cmd port: cmd write\n"));
868 if (asmc_command(dev, ASMC_CMDWRITE))
871 ASMC_DPRINTF(("data port: key\n"));
872 for (i = 0; i < 4; i++) {
873 ASMC_DATAPORT_WRITE(sc, key[i]);
874 if (asmc_wait(dev, 0x04))
877 ASMC_DPRINTF(("data port: length\n"));
878 ASMC_DATAPORT_WRITE(sc, len);
880 ASMC_DPRINTF(("data port: buffer\n"));
881 for (i = 0; i < len; i++) {
882 if (asmc_wait(dev, 0x04))
884 ASMC_DATAPORT_WRITE(sc, buf[i]);
890 if (++try < 10) goto begin;
891 device_printf(dev,"%s for key %s failed %d times, giving up\n",
895 mtx_unlock_spin(&sc->sc_mtx);
902 * Fan control functions.
905 asmc_fan_count(device_t dev)
909 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0)
916 asmc_fan_getvalue(device_t dev, const char *key, int fan)
922 snprintf(fankey, sizeof(fankey), key, fan);
923 if (asmc_key_read(dev, fankey, buf, 2) < 0)
925 speed = (buf[0] << 6) | (buf[1] >> 2);
931 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
933 device_t dev = (device_t) arg1;
938 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
939 error = sysctl_handle_int(oidp, &v, 0, req);
945 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
947 device_t dev = (device_t) arg1;
952 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
953 error = sysctl_handle_int(oidp, &v, 0, req);
960 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
962 device_t dev = (device_t) arg1;
967 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
968 error = sysctl_handle_int(oidp, &v, 0, req);
974 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
976 device_t dev = (device_t) arg1;
981 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
982 error = sysctl_handle_int(oidp, &v, 0, req);
988 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
990 device_t dev = (device_t) arg1;
995 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
996 error = sysctl_handle_int(oidp, &v, 0, req);
1002 * Temperature functions.
1005 asmc_temp_getvalue(device_t dev, const char *key)
1010 * Check for invalid temperatures.
1012 if (asmc_key_read(dev, key, buf, 2) < 0)
1019 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1021 device_t dev = (device_t) arg1;
1022 struct asmc_softc *sc = device_get_softc(dev);
1025 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1026 error = sysctl_handle_int(oidp, &val, 0, req);
1032 * Sudden Motion Sensor functions.
1035 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1040 /* no need to do locking here as asmc_key_read() already does it */
1045 error = asmc_key_read(dev, key, buf, 2);
1048 device_printf(dev, "%s called with invalid argument %s\n",
1053 *val = ((int16_t)buf[0] << 8) | buf[1];
1059 asmc_sms_calibrate(device_t dev)
1061 struct asmc_softc *sc = device_get_softc(dev);
1063 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1064 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1065 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1069 asmc_sms_intrfast(void *arg)
1072 device_t dev = (device_t) arg;
1073 struct asmc_softc *sc = device_get_softc(dev);
1074 if (!sc->sc_sms_intr_works)
1075 return (FILTER_HANDLED);
1077 mtx_lock_spin(&sc->sc_mtx);
1078 type = ASMC_INTPORT_READ(sc);
1079 mtx_unlock_spin(&sc->sc_mtx);
1081 sc->sc_sms_intrtype = type;
1082 asmc_sms_printintr(dev, type);
1085 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1087 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1089 return (FILTER_HANDLED);
1094 asmc_sms_handler(void *arg)
1096 struct asmc_softc *sc = device_get_softc(arg);
1098 asmc_sms_task(sc, 0);
1104 asmc_sms_printintr(device_t dev, uint8_t type)
1108 case ASMC_SMS_INTFF:
1109 device_printf(dev, "WARNING: possible free fall!\n");
1111 case ASMC_SMS_INTHA:
1112 device_printf(dev, "WARNING: high acceleration detected!\n");
1114 case ASMC_SMS_INTSH:
1115 device_printf(dev, "WARNING: possible shock!\n");
1118 device_printf(dev, "%s unknown interrupt\n", __func__);
1123 asmc_sms_task(void *arg, int pending)
1125 struct asmc_softc *sc = (struct asmc_softc *)arg;
1129 switch (sc->sc_sms_intrtype) {
1130 case ASMC_SMS_INTFF:
1133 case ASMC_SMS_INTHA:
1136 case ASMC_SMS_INTSH:
1143 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1144 devctl_notify("ACPI", "asmc", "SMS", notify);
1148 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1150 device_t dev = (device_t) arg1;
1155 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1157 error = sysctl_handle_int(oidp, &v, 0, req);
1163 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1165 device_t dev = (device_t) arg1;
1170 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1172 error = sysctl_handle_int(oidp, &v, 0, req);
1178 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1180 device_t dev = (device_t) arg1;
1185 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1187 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1193 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1195 device_t dev = (device_t) arg1;
1200 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6);
1202 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1208 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1210 device_t dev = (device_t) arg1;
1215 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6);
1217 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1223 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1225 device_t dev = (device_t) arg1;
1231 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1232 if (error == 0 && req->newptr != NULL) {
1233 level = *(unsigned int *)req->newptr;
1239 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);