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);
66 static int asmc_resume(device_t dev);
71 static int asmc_init(device_t dev);
72 static int asmc_command(device_t dev, uint8_t command);
73 static int asmc_wait(device_t dev, uint8_t val);
74 static int asmc_wait_ack(device_t dev, uint8_t val, int amount);
75 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf,
77 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf,
79 static int asmc_fan_count(device_t dev);
80 static int asmc_fan_getvalue(device_t dev, const char *key, int fan);
81 static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
82 static int asmc_temp_getvalue(device_t dev, const char *key);
83 static int asmc_sms_read(device_t, const char *key, int16_t *val);
84 static void asmc_sms_calibrate(device_t dev);
85 static int asmc_sms_intrfast(void *arg);
87 static void asmc_sms_handler(void *arg);
89 static void asmc_sms_printintr(device_t dev, uint8_t);
90 static void asmc_sms_task(void *arg, int pending);
92 void asmc_dumpall(device_t);
93 static int asmc_key_dump(device_t, int);
99 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
100 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
101 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
102 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
103 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
104 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
105 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
106 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
107 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
108 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
109 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
110 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
111 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
114 const char *smc_model; /* smbios.system.product env var. */
115 const char *smc_desc; /* driver description */
117 /* Helper functions */
118 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
119 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
120 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
121 int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
122 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
123 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
124 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
125 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
126 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
127 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
128 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
129 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
131 const char *smc_temps[ASMC_TEMP_MAX];
132 const char *smc_tempnames[ASMC_TEMP_MAX];
133 const char *smc_tempdescs[ASMC_TEMP_MAX];
136 static struct asmc_model *asmc_match(device_t dev);
138 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
141 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
143 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
144 asmc_mb_sysctl_fanminspeed, \
145 asmc_mb_sysctl_fanmaxspeed, \
146 asmc_mb_sysctl_fantargetspeed
148 #define ASMC_FAN_FUNCS2 asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
149 asmc_mb_sysctl_fanminspeed, \
150 asmc_mb_sysctl_fanmaxspeed, \
151 asmc_mb_sysctl_fantargetspeed
153 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
154 asmc_mbp_sysctl_light_right, \
155 asmc_mbp_sysctl_light_control
157 struct asmc_model asmc_models[] = {
159 "MacBook1,1", "Apple SMC MacBook Core Duo",
160 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
161 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
165 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
166 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
167 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
171 "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
172 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
173 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
177 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
178 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
179 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
183 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
184 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
185 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
189 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
190 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
191 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
195 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
196 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
197 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
201 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
202 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
203 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
207 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
208 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
209 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
213 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
214 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
215 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
219 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
220 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
221 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
225 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
226 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
227 ASMC_MBP11_TEMPS, ASMC_MBP11_TEMPNAMES, ASMC_MBP11_TEMPDESCS
230 /* The Mac Mini has no SMS */
232 "Macmini1,1", "Apple SMC Mac Mini",
236 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
239 /* The Mac Mini 3,1 has no SMS */
241 "Macmini3,1", "Apple SMC Mac Mini 3,1",
245 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
248 /* Idem for the MacPro */
250 "MacPro2", "Apple SMC Mac Pro (8-core)",
254 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
257 /* Idem for the MacPro 2010*/
259 "MacPro5,1", "Apple SMC MacPro (2010)",
263 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
267 "MacBookAir1,1", "Apple SMC MacBook Air",
268 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
269 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
273 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
274 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
275 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
279 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
280 ASMC_SMS_FUNCS_DISABLED,
283 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
287 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
288 ASMC_SMS_FUNCS_DISABLED,
291 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
298 #undef ASMC_SMS_FUNCS
299 #undef ASMC_SMS_FUNCS_DISABLED
300 #undef ASMC_FAN_FUNCS
301 #undef ASMC_FAN_FUNCS2
302 #undef ASMC_LIGHT_FUNCS
307 static device_method_t asmc_methods[] = {
308 DEVMETHOD(device_probe, asmc_probe),
309 DEVMETHOD(device_attach, asmc_attach),
310 DEVMETHOD(device_detach, asmc_detach),
311 DEVMETHOD(device_resume, asmc_resume),
316 static driver_t asmc_driver = {
319 sizeof(struct asmc_softc)
325 #define _COMPONENT ACPI_OEM
326 ACPI_MODULE_NAME("ASMC")
328 #define ASMC_DPRINTF(str) device_printf(dev, str)
330 #define ASMC_DPRINTF(str)
333 /* NB: can't be const */
334 static char *asmc_ids[] = { "APP0001", NULL };
336 static devclass_t asmc_devclass;
338 static unsigned int light_control = 0;
340 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
341 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
343 static struct asmc_model *
344 asmc_match(device_t dev)
349 model = kern_getenv("smbios.system.product");
353 for (i = 0; asmc_models[i].smc_model; i++) {
354 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
356 return (&asmc_models[i]);
365 asmc_probe(device_t dev)
367 struct asmc_model *model;
369 if (resource_disabled("asmc", 0))
371 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
374 model = asmc_match(dev);
376 device_printf(dev, "model not recognized\n");
379 device_set_desc(dev, model->smc_desc);
381 return (BUS_PROBE_DEFAULT);
385 asmc_attach(device_t dev)
390 struct asmc_softc *sc = device_get_softc(dev);
391 struct sysctl_ctx_list *sysctlctx;
392 struct sysctl_oid *sysctlnode;
393 struct asmc_model *model;
395 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
396 &sc->sc_rid_port, RF_ACTIVE);
397 if (sc->sc_ioport == NULL) {
398 device_printf(dev, "unable to allocate IO port\n");
402 sysctlctx = device_get_sysctl_ctx(dev);
403 sysctlnode = device_get_sysctl_tree(dev);
405 model = asmc_match(dev);
407 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
409 sc->sc_model = model;
413 * dev.asmc.n.fan.* tree.
415 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
416 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
417 CTLFLAG_RD, 0, "Fan Root Tree");
419 for (i = 1; i <= sc->sc_nfan; i++) {
423 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
424 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
425 OID_AUTO, name, CTLFLAG_RD, 0,
428 SYSCTL_ADD_PROC(sysctlctx,
429 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
430 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
431 dev, j, model->smc_fan_id, "I",
434 SYSCTL_ADD_PROC(sysctlctx,
435 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
436 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
437 dev, j, model->smc_fan_speed, "I",
440 SYSCTL_ADD_PROC(sysctlctx,
441 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
442 OID_AUTO, "safespeed",
443 CTLTYPE_INT | CTLFLAG_RD,
444 dev, j, model->smc_fan_safespeed, "I",
445 "Fan safe speed in RPM");
447 SYSCTL_ADD_PROC(sysctlctx,
448 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
449 OID_AUTO, "minspeed",
450 CTLTYPE_INT | CTLFLAG_RW,
451 dev, j, model->smc_fan_minspeed, "I",
452 "Fan minimum speed in RPM");
454 SYSCTL_ADD_PROC(sysctlctx,
455 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
456 OID_AUTO, "maxspeed",
457 CTLTYPE_INT | CTLFLAG_RW,
458 dev, j, model->smc_fan_maxspeed, "I",
459 "Fan maximum speed in RPM");
461 SYSCTL_ADD_PROC(sysctlctx,
462 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
463 OID_AUTO, "targetspeed",
464 CTLTYPE_INT | CTLFLAG_RW,
465 dev, j, model->smc_fan_targetspeed, "I",
466 "Fan target speed in RPM");
470 * dev.asmc.n.temp tree.
472 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
473 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
474 CTLFLAG_RD, 0, "Temperature sensors");
476 for (i = 0; model->smc_temps[i]; i++) {
477 SYSCTL_ADD_PROC(sysctlctx,
478 SYSCTL_CHILDREN(sc->sc_temp_tree),
479 OID_AUTO, model->smc_tempnames[i],
480 CTLTYPE_INT | CTLFLAG_RD,
481 dev, i, asmc_temp_sysctl, "I",
482 model->smc_tempdescs[i]);
488 if (model->smc_light_left) {
489 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
490 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
491 CTLFLAG_RD, 0, "Keyboard backlight sensors");
493 SYSCTL_ADD_PROC(sysctlctx,
494 SYSCTL_CHILDREN(sc->sc_light_tree),
495 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
496 dev, 0, model->smc_light_left, "I",
497 "Keyboard backlight left sensor");
499 SYSCTL_ADD_PROC(sysctlctx,
500 SYSCTL_CHILDREN(sc->sc_light_tree),
501 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
502 dev, 0, model->smc_light_right, "I",
503 "Keyboard backlight right sensor");
505 SYSCTL_ADD_PROC(sysctlctx,
506 SYSCTL_CHILDREN(sc->sc_light_tree),
508 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
509 dev, 0, model->smc_light_control, "I",
510 "Keyboard backlight brightness control");
513 if (model->smc_sms_x == NULL)
517 * dev.asmc.n.sms tree.
519 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
520 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
521 CTLFLAG_RD, 0, "Sudden Motion Sensor");
523 SYSCTL_ADD_PROC(sysctlctx,
524 SYSCTL_CHILDREN(sc->sc_sms_tree),
525 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
526 dev, 0, model->smc_sms_x, "I",
527 "Sudden Motion Sensor X value");
529 SYSCTL_ADD_PROC(sysctlctx,
530 SYSCTL_CHILDREN(sc->sc_sms_tree),
531 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
532 dev, 0, model->smc_sms_y, "I",
533 "Sudden Motion Sensor Y value");
535 SYSCTL_ADD_PROC(sysctlctx,
536 SYSCTL_CHILDREN(sc->sc_sms_tree),
537 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
538 dev, 0, model->smc_sms_z, "I",
539 "Sudden Motion Sensor Z value");
542 * Need a taskqueue to send devctl_notify() events
543 * when the SMS interrupt us.
545 * PI_REALTIME is used due to the sensitivity of the
546 * interrupt. An interrupt from the SMS means that the
547 * disk heads should be turned off as quickly as possible.
549 * We only need to do this for the non INTR_FILTER case.
551 sc->sc_sms_tq = NULL;
553 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
554 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
555 taskqueue_thread_enqueue, &sc->sc_sms_tq);
556 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
557 device_get_nameunit(dev));
560 * Allocate an IRQ for the SMS.
563 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
564 &sc->sc_rid_irq, RF_ACTIVE);
565 if (sc->sc_irq == NULL) {
566 device_printf(dev, "unable to allocate IRQ resource\n");
571 ret = bus_setup_intr(dev, sc->sc_irq,
572 INTR_TYPE_MISC | INTR_MPSAFE,
574 asmc_sms_intrfast, asmc_sms_handler,
576 asmc_sms_intrfast, NULL,
578 dev, &sc->sc_cookie);
581 device_printf(dev, "unable to setup SMS IRQ\n");
587 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
589 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
591 mtx_destroy(&sc->sc_mtx);
593 taskqueue_free(sc->sc_sms_tq);
599 asmc_detach(device_t dev)
601 struct asmc_softc *sc = device_get_softc(dev);
604 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
605 taskqueue_free(sc->sc_sms_tq);
608 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
610 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
613 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
615 mtx_destroy(&sc->sc_mtx);
621 asmc_resume(device_t dev)
624 buf[0] = light_control;
626 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
632 void asmc_dumpall(device_t dev)
636 /* XXX magic number */
637 for (i=0; i < 0x100; i++)
638 asmc_key_dump(dev, i);
643 asmc_init(device_t dev)
645 struct asmc_softc *sc = device_get_softc(dev);
649 if (sc->sc_model->smc_sms_x == NULL)
653 * We are ready to recieve interrupts from the SMS.
656 ASMC_DPRINTF(("intok key\n"));
657 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
661 * Initiate the polling intervals.
663 buf[0] = 20; /* msecs */
664 ASMC_DPRINTF(("low int key\n"));
665 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
668 buf[0] = 20; /* msecs */
669 ASMC_DPRINTF(("high int key\n"));
670 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
675 ASMC_DPRINTF(("sms low key\n"));
676 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
681 ASMC_DPRINTF(("sms high key\n"));
682 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
686 * I'm not sure what this key does, but it seems to be
690 ASMC_DPRINTF(("sms flag key\n"));
691 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
694 sc->sc_sms_intr_works = 0;
697 * Retry SMS initialization 1000 times
698 * (takes approx. 2 seconds in worst case)
700 for (i = 0; i < 1000; i++) {
701 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
702 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
704 sc->sc_sms_intr_works = 1;
707 buf[0] = ASMC_SMS_INIT1;
708 buf[1] = ASMC_SMS_INIT2;
709 ASMC_DPRINTF(("sms key\n"));
710 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
713 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
716 asmc_sms_calibrate(dev);
718 sc->sc_nfan = asmc_fan_count(dev);
719 if (sc->sc_nfan > ASMC_MAXFANS) {
720 device_printf(dev, "more than %d fans were detected. Please "
721 "report this.\n", ASMC_MAXFANS);
722 sc->sc_nfan = ASMC_MAXFANS;
727 * The number of keys is a 32 bit buffer
729 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
730 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
741 * We need to make sure that the SMC acks the byte sent.
742 * Just wait up to (amount * 10) ms.
745 asmc_wait_ack(device_t dev, uint8_t val, int amount)
747 struct asmc_softc *sc = device_get_softc(dev);
750 val = val & ASMC_STATUS_MASK;
752 for (i = 0; i < amount; i++) {
753 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
762 * We need to make sure that the SMC acks the byte sent.
763 * Just wait up to 100 ms.
766 asmc_wait(device_t dev, uint8_t val)
768 struct asmc_softc *sc;
770 if (asmc_wait_ack(dev, val, 1000) == 0)
773 sc = device_get_softc(dev);
774 val = val & ASMC_STATUS_MASK;
777 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
778 ASMC_CMDPORT_READ(sc));
784 * Send the given command, retrying up to 10 times if
785 * the acknowledgement fails.
788 asmc_command(device_t dev, uint8_t command) {
791 struct asmc_softc *sc = device_get_softc(dev);
793 for (i=0; i < 10; i++) {
794 ASMC_CMDPORT_WRITE(sc, command);
795 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
801 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
802 ASMC_CMDPORT_READ(sc));
808 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
810 int i, error = 1, try = 0;
811 struct asmc_softc *sc = device_get_softc(dev);
813 mtx_lock_spin(&sc->sc_mtx);
816 if (asmc_command(dev, ASMC_CMDREAD))
819 for (i = 0; i < 4; i++) {
820 ASMC_DATAPORT_WRITE(sc, key[i]);
821 if (asmc_wait(dev, 0x04))
825 ASMC_DATAPORT_WRITE(sc, len);
827 for (i = 0; i < len; i++) {
828 if (asmc_wait(dev, 0x05))
830 buf[i] = ASMC_DATAPORT_READ(sc);
836 if (++try < 10) goto begin;
837 device_printf(dev,"%s for key %s failed %d times, giving up\n",
841 mtx_unlock_spin(&sc->sc_mtx);
848 asmc_key_dump(device_t dev, int number)
850 struct asmc_softc *sc = device_get_softc(dev);
852 char type[7] = { 0 };
856 int i, error = 1, try = 0;
858 mtx_lock_spin(&sc->sc_mtx);
860 index[0] = (number >> 24) & 0xff;
861 index[1] = (number >> 16) & 0xff;
862 index[2] = (number >> 8) & 0xff;
863 index[3] = (number) & 0xff;
866 if (asmc_command(dev, 0x12))
869 for (i = 0; i < 4; i++) {
870 ASMC_DATAPORT_WRITE(sc, index[i]);
871 if (asmc_wait(dev, 0x04))
875 ASMC_DATAPORT_WRITE(sc, 4);
877 for (i = 0; i < 4; i++) {
878 if (asmc_wait(dev, 0x05))
880 key[i] = ASMC_DATAPORT_READ(sc);
884 if (asmc_command(dev, 0x13))
887 for (i = 0; i < 4; i++) {
888 ASMC_DATAPORT_WRITE(sc, key[i]);
889 if (asmc_wait(dev, 0x04))
893 ASMC_DATAPORT_WRITE(sc, 6);
895 for (i = 0; i < 6; i++) {
896 if (asmc_wait(dev, 0x05))
898 type[i] = ASMC_DATAPORT_READ(sc);
904 if (++try < 10) goto begin;
905 device_printf(dev,"%s for key %s failed %d times, giving up\n",
907 mtx_unlock_spin(&sc->sc_mtx);
912 mtx_unlock_spin(&sc->sc_mtx);
916 if (maxlen > sizeof(v)) {
918 "WARNING: cropping maxlen from %d to %zu\n",
922 for (i = 0; i < sizeof(v); i++) {
925 asmc_key_read(dev, key, v, maxlen);
926 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
927 "(len %d), data", number, key, type, maxlen);
928 for (i = 0; i < maxlen; i++) {
929 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
930 strlcat(buf, buf2, sizeof(buf));
932 strlcat(buf, " \n", sizeof(buf));
933 device_printf(dev, "%s", buf);
941 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
943 int i, error = -1, try = 0;
944 struct asmc_softc *sc = device_get_softc(dev);
946 mtx_lock_spin(&sc->sc_mtx);
949 ASMC_DPRINTF(("cmd port: cmd write\n"));
950 if (asmc_command(dev, ASMC_CMDWRITE))
953 ASMC_DPRINTF(("data port: key\n"));
954 for (i = 0; i < 4; i++) {
955 ASMC_DATAPORT_WRITE(sc, key[i]);
956 if (asmc_wait(dev, 0x04))
959 ASMC_DPRINTF(("data port: length\n"));
960 ASMC_DATAPORT_WRITE(sc, len);
962 ASMC_DPRINTF(("data port: buffer\n"));
963 for (i = 0; i < len; i++) {
964 if (asmc_wait(dev, 0x04))
966 ASMC_DATAPORT_WRITE(sc, buf[i]);
972 if (++try < 10) goto begin;
973 device_printf(dev,"%s for key %s failed %d times, giving up\n",
977 mtx_unlock_spin(&sc->sc_mtx);
984 * Fan control functions.
987 asmc_fan_count(device_t dev)
991 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
998 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1004 snprintf(fankey, sizeof(fankey), key, fan);
1005 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1007 speed = (buf[0] << 6) | (buf[1] >> 2);
1013 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1018 snprintf(fankey, sizeof(fankey), key, fan);
1019 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1027 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1037 snprintf(fankey, sizeof(fankey), key, fan);
1038 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1045 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1047 device_t dev = (device_t) arg1;
1052 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1053 error = sysctl_handle_int(oidp, &v, 0, req);
1059 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1062 device_t dev = (device_t) arg1;
1067 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1070 error = sysctl_handle_string(oidp, desc, 0, req);
1076 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1078 device_t dev = (device_t) arg1;
1083 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1084 error = sysctl_handle_int(oidp, &v, 0, req);
1091 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1093 device_t dev = (device_t) arg1;
1098 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1099 error = sysctl_handle_int(oidp, &v, 0, req);
1101 if (error == 0 && req->newptr != NULL) {
1102 unsigned int newspeed = v;
1103 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1110 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1112 device_t dev = (device_t) arg1;
1117 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1118 error = sysctl_handle_int(oidp, &v, 0, req);
1120 if (error == 0 && req->newptr != NULL) {
1121 unsigned int newspeed = v;
1122 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1129 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1131 device_t dev = (device_t) arg1;
1136 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1137 error = sysctl_handle_int(oidp, &v, 0, req);
1139 if (error == 0 && req->newptr != NULL) {
1140 unsigned int newspeed = v;
1141 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1148 * Temperature functions.
1151 asmc_temp_getvalue(device_t dev, const char *key)
1156 * Check for invalid temperatures.
1158 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1165 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1167 device_t dev = (device_t) arg1;
1168 struct asmc_softc *sc = device_get_softc(dev);
1171 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1172 error = sysctl_handle_int(oidp, &val, 0, req);
1178 * Sudden Motion Sensor functions.
1181 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1186 /* no need to do locking here as asmc_key_read() already does it */
1191 error = asmc_key_read(dev, key, buf, sizeof buf);
1194 device_printf(dev, "%s called with invalid argument %s\n",
1199 *val = ((int16_t)buf[0] << 8) | buf[1];
1205 asmc_sms_calibrate(device_t dev)
1207 struct asmc_softc *sc = device_get_softc(dev);
1209 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1210 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1211 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1215 asmc_sms_intrfast(void *arg)
1218 device_t dev = (device_t) arg;
1219 struct asmc_softc *sc = device_get_softc(dev);
1220 if (!sc->sc_sms_intr_works)
1221 return (FILTER_HANDLED);
1223 mtx_lock_spin(&sc->sc_mtx);
1224 type = ASMC_INTPORT_READ(sc);
1225 mtx_unlock_spin(&sc->sc_mtx);
1227 sc->sc_sms_intrtype = type;
1228 asmc_sms_printintr(dev, type);
1231 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1233 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1235 return (FILTER_HANDLED);
1240 asmc_sms_handler(void *arg)
1242 struct asmc_softc *sc = device_get_softc(arg);
1244 asmc_sms_task(sc, 0);
1250 asmc_sms_printintr(device_t dev, uint8_t type)
1254 case ASMC_SMS_INTFF:
1255 device_printf(dev, "WARNING: possible free fall!\n");
1257 case ASMC_SMS_INTHA:
1258 device_printf(dev, "WARNING: high acceleration detected!\n");
1260 case ASMC_SMS_INTSH:
1261 device_printf(dev, "WARNING: possible shock!\n");
1264 device_printf(dev, "%s unknown interrupt\n", __func__);
1269 asmc_sms_task(void *arg, int pending)
1271 struct asmc_softc *sc = (struct asmc_softc *)arg;
1275 switch (sc->sc_sms_intrtype) {
1276 case ASMC_SMS_INTFF:
1279 case ASMC_SMS_INTHA:
1282 case ASMC_SMS_INTSH:
1289 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1290 devctl_notify("ACPI", "asmc", "SMS", notify);
1294 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1296 device_t dev = (device_t) arg1;
1301 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1303 error = sysctl_handle_int(oidp, &v, 0, req);
1309 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1311 device_t dev = (device_t) arg1;
1316 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1318 error = sysctl_handle_int(oidp, &v, 0, req);
1324 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1326 device_t dev = (device_t) arg1;
1331 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1333 error = sysctl_handle_int(oidp, &v, 0, req);
1339 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1341 device_t dev = (device_t) arg1;
1346 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1348 error = sysctl_handle_int(oidp, &v, 0, req);
1354 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1356 device_t dev = (device_t) arg1;
1361 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1363 error = sysctl_handle_int(oidp, &v, 0, req);
1369 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1371 device_t dev = (device_t) arg1;
1377 error = sysctl_handle_int(oidp, &v, 0, req);
1379 if (error == 0 && req->newptr != NULL) {
1380 if (v < 0 || v > 255)
1383 buf[0] = light_control;
1385 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);