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 /* Idem for the MacPro */
211 "MacPro2", "Apple SMC Mac Pro (8-core)",
215 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
219 "MacBookAir1,1", "Apple SMC MacBook Air",
220 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
221 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
228 #undef ASMC_SMS_FUNCS
229 #undef ASMC_FAN_FUNCS
230 #undef ASMC_LIGHT_FUNCS
235 static device_method_t asmc_methods[] = {
236 DEVMETHOD(device_probe, asmc_probe),
237 DEVMETHOD(device_attach, asmc_attach),
238 DEVMETHOD(device_detach, asmc_detach),
243 static driver_t asmc_driver = {
246 sizeof(struct asmc_softc)
252 #define _COMPONENT ACPI_OEM
253 ACPI_MODULE_NAME("ASMC")
255 #define ASMC_DPRINTF(str) device_printf(dev, str)
257 #define ASMC_DPRINTF(str)
260 /* NB: can't be const */
261 static char *asmc_ids[] = { "APP0001", NULL };
263 static devclass_t asmc_devclass;
265 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
266 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
268 static struct asmc_model *
269 asmc_match(device_t dev)
274 model = getenv("smbios.system.product");
278 for (i = 0; asmc_models[i].smc_model; i++) {
279 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
281 return (&asmc_models[i]);
290 asmc_probe(device_t dev)
292 struct asmc_model *model;
294 if (resource_disabled("asmc", 0))
296 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
299 model = asmc_match(dev);
301 device_printf(dev, "model not recognized\n");
304 device_set_desc(dev, model->smc_desc);
306 return (BUS_PROBE_DEFAULT);
310 asmc_attach(device_t dev)
315 struct asmc_softc *sc = device_get_softc(dev);
316 struct sysctl_ctx_list *sysctlctx;
317 struct sysctl_oid *sysctlnode;
318 struct asmc_model *model;
320 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
321 &sc->sc_rid_port, RF_ACTIVE);
322 if (sc->sc_ioport == NULL) {
323 device_printf(dev, "unable to allocate IO port\n");
327 sysctlctx = device_get_sysctl_ctx(dev);
328 sysctlnode = device_get_sysctl_tree(dev);
330 model = asmc_match(dev);
332 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
334 sc->sc_model = model;
338 * dev.asmc.n.fan.* tree.
340 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
341 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
342 CTLFLAG_RD, 0, "Fan Root Tree");
344 for (i = 1; i <= sc->sc_nfan; i++) {
348 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
349 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
350 OID_AUTO, name, CTLFLAG_RD, 0,
353 SYSCTL_ADD_PROC(sysctlctx,
354 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
355 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
356 dev, j, model->smc_fan_speed, "I",
359 SYSCTL_ADD_PROC(sysctlctx,
360 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
361 OID_AUTO, "safespeed",
362 CTLTYPE_INT | CTLFLAG_RD,
363 dev, j, model->smc_fan_safespeed, "I",
364 "Fan safe speed in RPM");
366 SYSCTL_ADD_PROC(sysctlctx,
367 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
368 OID_AUTO, "minspeed",
369 CTLTYPE_INT | CTLFLAG_RD,
370 dev, j, model->smc_fan_minspeed, "I",
371 "Fan minimum speed in RPM");
373 SYSCTL_ADD_PROC(sysctlctx,
374 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
375 OID_AUTO, "maxspeed",
376 CTLTYPE_INT | CTLFLAG_RD,
377 dev, j, model->smc_fan_maxspeed, "I",
378 "Fan maximum speed in RPM");
380 SYSCTL_ADD_PROC(sysctlctx,
381 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
382 OID_AUTO, "targetspeed",
383 CTLTYPE_INT | CTLFLAG_RD,
384 dev, j, model->smc_fan_targetspeed, "I",
385 "Fan target speed in RPM");
389 * dev.asmc.n.temp tree.
391 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
392 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
393 CTLFLAG_RD, 0, "Temperature sensors");
395 for (i = 0; model->smc_temps[i]; i++) {
396 SYSCTL_ADD_PROC(sysctlctx,
397 SYSCTL_CHILDREN(sc->sc_temp_tree),
398 OID_AUTO, model->smc_tempnames[i],
399 CTLTYPE_INT | CTLFLAG_RD,
400 dev, i, asmc_temp_sysctl, "I",
401 model->smc_tempdescs[i]);
407 if (model->smc_light_left) {
408 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
409 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
410 CTLFLAG_RD, 0, "Keyboard backlight sensors");
412 SYSCTL_ADD_PROC(sysctlctx,
413 SYSCTL_CHILDREN(sc->sc_light_tree),
414 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
415 dev, 0, model->smc_light_left, "I",
416 "Keyboard backlight left sensor");
418 SYSCTL_ADD_PROC(sysctlctx,
419 SYSCTL_CHILDREN(sc->sc_light_tree),
420 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
421 dev, 0, model->smc_light_right, "I",
422 "Keyboard backlight right sensor");
424 SYSCTL_ADD_PROC(sysctlctx,
425 SYSCTL_CHILDREN(sc->sc_light_tree),
427 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
428 dev, 0, model->smc_light_control, "I",
429 "Keyboard backlight brightness control");
432 if (model->smc_sms_x == NULL)
436 * dev.asmc.n.sms tree.
438 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
439 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
440 CTLFLAG_RD, 0, "Sudden Motion Sensor");
442 SYSCTL_ADD_PROC(sysctlctx,
443 SYSCTL_CHILDREN(sc->sc_sms_tree),
444 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
445 dev, 0, model->smc_sms_x, "I",
446 "Sudden Motion Sensor X value");
448 SYSCTL_ADD_PROC(sysctlctx,
449 SYSCTL_CHILDREN(sc->sc_sms_tree),
450 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
451 dev, 0, model->smc_sms_y, "I",
452 "Sudden Motion Sensor Y value");
454 SYSCTL_ADD_PROC(sysctlctx,
455 SYSCTL_CHILDREN(sc->sc_sms_tree),
456 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
457 dev, 0, model->smc_sms_z, "I",
458 "Sudden Motion Sensor Z value");
461 * Need a taskqueue to send devctl_notify() events
462 * when the SMS interrupt us.
464 * PI_REALTIME is used due to the sensitivity of the
465 * interrupt. An interrupt from the SMS means that the
466 * disk heads should be turned off as quickly as possible.
468 * We only need to do this for the non INTR_FILTER case.
470 sc->sc_sms_tq = NULL;
472 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
473 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
474 taskqueue_thread_enqueue, &sc->sc_sms_tq);
475 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
476 device_get_nameunit(dev));
479 * Allocate an IRQ for the SMS.
482 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
483 &sc->sc_rid_irq, RF_ACTIVE);
484 if (sc->sc_irq == NULL) {
485 device_printf(dev, "unable to allocate IRQ resource\n");
490 ret = bus_setup_intr(dev, sc->sc_irq,
491 INTR_TYPE_MISC | INTR_MPSAFE,
493 asmc_sms_intrfast, asmc_sms_handler,
495 asmc_sms_intrfast, NULL,
497 dev, &sc->sc_cookie);
500 device_printf(dev, "unable to setup SMS IRQ\n");
506 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
508 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
510 mtx_destroy(&sc->sc_mtx);
512 taskqueue_free(sc->sc_sms_tq);
518 asmc_detach(device_t dev)
520 struct asmc_softc *sc = device_get_softc(dev);
523 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
524 taskqueue_free(sc->sc_sms_tq);
527 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
529 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
532 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
534 mtx_destroy(&sc->sc_mtx);
540 void asmc_dumpall(device_t dev)
544 /* XXX magic number */
545 for (i=0; i < 0x100; i++)
546 asmc_key_dump(dev, i);
551 asmc_init(device_t dev)
553 struct asmc_softc *sc = device_get_softc(dev);
557 if (sc->sc_model->smc_sms_x == NULL)
561 * We are ready to recieve interrupts from the SMS.
564 ASMC_DPRINTF(("intok key\n"));
565 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
569 * Initiate the polling intervals.
571 buf[0] = 20; /* msecs */
572 ASMC_DPRINTF(("low int key\n"));
573 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
576 buf[0] = 20; /* msecs */
577 ASMC_DPRINTF(("high int key\n"));
578 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
583 ASMC_DPRINTF(("sms low key\n"));
584 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
589 ASMC_DPRINTF(("sms high key\n"));
590 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
594 * I'm not sure what this key does, but it seems to be
598 ASMC_DPRINTF(("sms flag key\n"));
599 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
602 sc->sc_sms_intr_works = 0;
605 * Retry SMS initialization 1000 times
606 * (takes approx. 2 seconds in worst case)
608 for (i = 0; i < 1000; i++) {
609 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
610 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
612 sc->sc_sms_intr_works = 1;
615 buf[0] = ASMC_SMS_INIT1;
616 buf[1] = ASMC_SMS_INIT2;
617 ASMC_DPRINTF(("sms key\n"));
618 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
621 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
624 asmc_sms_calibrate(dev);
626 sc->sc_nfan = asmc_fan_count(dev);
627 if (sc->sc_nfan > ASMC_MAXFANS) {
628 device_printf(dev, "more than %d fans were detected. Please "
629 "report this.\n", ASMC_MAXFANS);
630 sc->sc_nfan = ASMC_MAXFANS;
635 * XXX: The number of keys is a 32 bit buffer, but
636 * right now Apple only uses the last 8 bit.
638 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
639 device_printf(dev, "number of keys: %d\n", buf[3]);
650 * We need to make sure that the SMC acks the byte sent.
651 * Just wait up to (amount * 10) ms.
654 asmc_wait_ack(device_t dev, uint8_t val, int amount)
656 struct asmc_softc *sc = device_get_softc(dev);
659 val = val & ASMC_STATUS_MASK;
661 for (i = 0; i < amount; i++) {
662 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
671 * We need to make sure that the SMC acks the byte sent.
672 * Just wait up to 100 ms.
675 asmc_wait(device_t dev, uint8_t val)
677 struct asmc_softc *sc;
679 if (asmc_wait_ack(dev, val, 1000) == 0)
682 sc = device_get_softc(dev);
683 val = val & ASMC_STATUS_MASK;
686 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
687 ASMC_CMDPORT_READ(sc));
693 * Send the given command, retrying up to 10 times if
694 * the acknowledgement fails.
697 asmc_command(device_t dev, uint8_t command) {
700 struct asmc_softc *sc = device_get_softc(dev);
702 for (i=0; i < 10; i++) {
703 ASMC_CMDPORT_WRITE(sc, command);
704 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
710 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
711 ASMC_CMDPORT_READ(sc));
717 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
719 int i, error = 1, try = 0;
720 struct asmc_softc *sc = device_get_softc(dev);
722 mtx_lock_spin(&sc->sc_mtx);
725 if (asmc_command(dev, ASMC_CMDREAD))
728 for (i = 0; i < 4; i++) {
729 ASMC_DATAPORT_WRITE(sc, key[i]);
730 if (asmc_wait(dev, 0x04))
734 ASMC_DATAPORT_WRITE(sc, len);
736 for (i = 0; i < len; i++) {
737 if (asmc_wait(dev, 0x05))
739 buf[i] = ASMC_DATAPORT_READ(sc);
745 if (++try < 10) goto begin;
746 device_printf(dev,"%s for key %s failed %d times, giving up\n",
750 mtx_unlock_spin(&sc->sc_mtx);
757 asmc_key_dump(device_t dev, int number)
759 struct asmc_softc *sc = device_get_softc(dev);
761 char type[7] = { 0 };
765 int i, error = 1, try = 0;
767 mtx_lock_spin(&sc->sc_mtx);
769 index[0] = (number >> 24) & 0xff;
770 index[1] = (number >> 16) & 0xff;
771 index[2] = (number >> 8) & 0xff;
772 index[3] = (number) & 0xff;
775 if (asmc_command(dev, 0x12))
778 for (i = 0; i < 4; i++) {
779 ASMC_DATAPORT_WRITE(sc, index[i]);
780 if (asmc_wait(dev, 0x04))
784 ASMC_DATAPORT_WRITE(sc, 4);
786 for (i = 0; i < 4; i++) {
787 if (asmc_wait(dev, 0x05))
789 key[i] = ASMC_DATAPORT_READ(sc);
793 if (asmc_command(dev, 0x13))
796 for (i = 0; i < 4; i++) {
797 ASMC_DATAPORT_WRITE(sc, key[i]);
798 if (asmc_wait(dev, 0x04))
802 ASMC_DATAPORT_WRITE(sc, 6);
804 for (i = 0; i < 6; i++) {
805 if (asmc_wait(dev, 0x05))
807 type[i] = ASMC_DATAPORT_READ(sc);
813 if (++try < 10) goto begin;
814 device_printf(dev,"%s for key %s failed %d times, giving up\n",
816 mtx_unlock_spin(&sc->sc_mtx);
821 mtx_unlock_spin(&sc->sc_mtx);
825 if (maxlen > sizeof(v)) {
827 "WARNING: cropping maxlen from %d to %zu\n",
831 for (i = 0; i < sizeof(v); i++) {
834 asmc_key_read(dev, key, v, maxlen);
835 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
836 "(len %d), data", number, key, type, maxlen);
837 for (i = 0; i < maxlen; i++) {
838 snprintf(buf2, sizeof(buf), " %02x", v[i]);
839 strlcat(buf, buf2, sizeof(buf));
841 strlcat(buf, " \n", sizeof(buf));
842 device_printf(dev, "%s", buf);
850 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
852 int i, error = -1, try = 0;
853 struct asmc_softc *sc = device_get_softc(dev);
855 mtx_lock_spin(&sc->sc_mtx);
858 ASMC_DPRINTF(("cmd port: cmd write\n"));
859 if (asmc_command(dev, ASMC_CMDWRITE))
862 ASMC_DPRINTF(("data port: key\n"));
863 for (i = 0; i < 4; i++) {
864 ASMC_DATAPORT_WRITE(sc, key[i]);
865 if (asmc_wait(dev, 0x04))
868 ASMC_DPRINTF(("data port: length\n"));
869 ASMC_DATAPORT_WRITE(sc, len);
871 ASMC_DPRINTF(("data port: buffer\n"));
872 for (i = 0; i < len; i++) {
873 if (asmc_wait(dev, 0x04))
875 ASMC_DATAPORT_WRITE(sc, buf[i]);
881 if (++try < 10) goto begin;
882 device_printf(dev,"%s for key %s failed %d times, giving up\n",
886 mtx_unlock_spin(&sc->sc_mtx);
893 * Fan control functions.
896 asmc_fan_count(device_t dev)
900 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0)
907 asmc_fan_getvalue(device_t dev, const char *key, int fan)
913 snprintf(fankey, sizeof(fankey), key, fan);
914 if (asmc_key_read(dev, fankey, buf, 2) < 0)
916 speed = (buf[0] << 6) | (buf[1] >> 2);
922 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
924 device_t dev = (device_t) arg1;
929 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
930 error = sysctl_handle_int(oidp, &v, 0, req);
936 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
938 device_t dev = (device_t) arg1;
943 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
944 error = sysctl_handle_int(oidp, &v, 0, req);
951 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
953 device_t dev = (device_t) arg1;
958 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
959 error = sysctl_handle_int(oidp, &v, 0, req);
965 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
967 device_t dev = (device_t) arg1;
972 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
973 error = sysctl_handle_int(oidp, &v, 0, req);
979 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
981 device_t dev = (device_t) arg1;
986 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
987 error = sysctl_handle_int(oidp, &v, 0, req);
993 * Temperature functions.
996 asmc_temp_getvalue(device_t dev, const char *key)
1001 * Check for invalid temperatures.
1003 if (asmc_key_read(dev, key, buf, 2) < 0)
1010 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1012 device_t dev = (device_t) arg1;
1013 struct asmc_softc *sc = device_get_softc(dev);
1016 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1017 error = sysctl_handle_int(oidp, &val, 0, req);
1023 * Sudden Motion Sensor functions.
1026 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1031 /* no need to do locking here as asmc_key_read() already does it */
1036 error = asmc_key_read(dev, key, buf, 2);
1039 device_printf(dev, "%s called with invalid argument %s\n",
1044 *val = ((int16_t)buf[0] << 8) | buf[1];
1050 asmc_sms_calibrate(device_t dev)
1052 struct asmc_softc *sc = device_get_softc(dev);
1054 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1055 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1056 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1060 asmc_sms_intrfast(void *arg)
1063 device_t dev = (device_t) arg;
1064 struct asmc_softc *sc = device_get_softc(dev);
1065 if (!sc->sc_sms_intr_works)
1066 return (FILTER_HANDLED);
1068 mtx_lock_spin(&sc->sc_mtx);
1069 type = ASMC_INTPORT_READ(sc);
1070 mtx_unlock_spin(&sc->sc_mtx);
1072 sc->sc_sms_intrtype = type;
1073 asmc_sms_printintr(dev, type);
1076 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1078 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1080 return (FILTER_HANDLED);
1085 asmc_sms_handler(void *arg)
1087 struct asmc_softc *sc = device_get_softc(arg);
1089 asmc_sms_task(sc, 0);
1095 asmc_sms_printintr(device_t dev, uint8_t type)
1099 case ASMC_SMS_INTFF:
1100 device_printf(dev, "WARNING: possible free fall!\n");
1102 case ASMC_SMS_INTHA:
1103 device_printf(dev, "WARNING: high acceleration detected!\n");
1105 case ASMC_SMS_INTSH:
1106 device_printf(dev, "WARNING: possible shock!\n");
1109 device_printf(dev, "%s unknown interrupt\n", __func__);
1114 asmc_sms_task(void *arg, int pending)
1116 struct asmc_softc *sc = (struct asmc_softc *)arg;
1120 switch (sc->sc_sms_intrtype) {
1121 case ASMC_SMS_INTFF:
1124 case ASMC_SMS_INTHA:
1127 case ASMC_SMS_INTSH:
1134 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1135 devctl_notify("ACPI", "asmc", "SMS", notify);
1139 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1141 device_t dev = (device_t) arg1;
1146 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1148 error = sysctl_handle_int(oidp, &v, 0, req);
1154 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1156 device_t dev = (device_t) arg1;
1161 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1163 error = sysctl_handle_int(oidp, &v, 0, req);
1169 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1171 device_t dev = (device_t) arg1;
1176 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1178 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1184 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1186 device_t dev = (device_t) arg1;
1191 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6);
1193 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1199 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1201 device_t dev = (device_t) arg1;
1206 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6);
1208 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1214 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1216 device_t dev = (device_t) arg1;
1222 error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1223 if (error == 0 && req->newptr != NULL) {
1224 level = *(unsigned int *)req->newptr;
1230 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);