2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
31 * Driver for Apple's System Management Console (SMC).
32 * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
34 * Inspired by the Linux applesmc driver.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/taskqueue.h>
53 #include <machine/resource.h>
55 #include <contrib/dev/acpica/include/acpi.h>
57 #include <dev/acpica/acpivar.h>
58 #include <dev/asmc/asmcvar.h>
60 #include "opt_intr_filter.h"
65 static int asmc_probe(device_t dev);
66 static int asmc_attach(device_t dev);
67 static int asmc_detach(device_t dev);
68 static int asmc_resume(device_t dev);
73 static int asmc_init(device_t dev);
74 static int asmc_command(device_t dev, uint8_t command);
75 static int asmc_wait(device_t dev, uint8_t val);
76 static int asmc_wait_ack(device_t dev, uint8_t val, int amount);
77 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf,
79 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf,
81 static int asmc_fan_count(device_t dev);
82 static int asmc_fan_getvalue(device_t dev, const char *key, int fan);
83 static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
84 static int asmc_temp_getvalue(device_t dev, const char *key);
85 static int asmc_sms_read(device_t, const char *key, int16_t *val);
86 static void asmc_sms_calibrate(device_t dev);
87 static int asmc_sms_intrfast(void *arg);
89 static void asmc_sms_handler(void *arg);
91 static void asmc_sms_printintr(device_t dev, uint8_t);
92 static void asmc_sms_task(void *arg, int pending);
94 void asmc_dumpall(device_t);
95 static int asmc_key_dump(device_t, int);
101 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
102 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
103 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
104 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
105 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
106 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
107 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
108 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
109 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
110 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
111 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
112 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
113 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
116 const char *smc_model; /* smbios.system.product env var. */
117 const char *smc_desc; /* driver description */
119 /* Helper functions */
120 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
121 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
122 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
123 int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
124 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
125 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
126 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
127 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
128 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
129 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
130 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
131 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
133 const char *smc_temps[ASMC_TEMP_MAX];
134 const char *smc_tempnames[ASMC_TEMP_MAX];
135 const char *smc_tempdescs[ASMC_TEMP_MAX];
138 static struct asmc_model *asmc_match(device_t dev);
140 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
143 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
145 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
146 asmc_mb_sysctl_fanminspeed, \
147 asmc_mb_sysctl_fanmaxspeed, \
148 asmc_mb_sysctl_fantargetspeed
150 #define ASMC_FAN_FUNCS2 asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
151 asmc_mb_sysctl_fanminspeed, \
152 asmc_mb_sysctl_fanmaxspeed, \
153 asmc_mb_sysctl_fantargetspeed
155 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
156 asmc_mbp_sysctl_light_right, \
157 asmc_mbp_sysctl_light_control
159 struct asmc_model asmc_models[] = {
161 "MacBook1,1", "Apple SMC MacBook Core Duo",
162 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
163 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
167 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
168 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
169 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
173 "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
174 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
175 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
179 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
180 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
181 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
185 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
186 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
187 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
191 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
192 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
193 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
197 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
198 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
199 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
203 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
204 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
205 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
209 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
210 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
211 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
215 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
216 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
217 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
221 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
222 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
223 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
227 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
228 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
229 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
233 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
234 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
235 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
239 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
240 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
241 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
244 /* The Mac Mini has no SMS */
246 "Macmini1,1", "Apple SMC Mac Mini",
250 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
253 /* The Mac Mini 3,1 has no SMS */
255 "Macmini3,1", "Apple SMC Mac Mini 3,1",
259 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
262 /* Idem for the MacPro */
264 "MacPro2", "Apple SMC Mac Pro (8-core)",
268 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
271 /* Idem for the MacPro 2010*/
273 "MacPro5,1", "Apple SMC MacPro (2010)",
277 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
281 "MacBookAir1,1", "Apple SMC MacBook Air",
282 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
283 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
287 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
288 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
289 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
293 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
294 ASMC_SMS_FUNCS_DISABLED,
297 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
301 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
302 ASMC_SMS_FUNCS_DISABLED,
305 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
312 #undef ASMC_SMS_FUNCS
313 #undef ASMC_SMS_FUNCS_DISABLED
314 #undef ASMC_FAN_FUNCS
315 #undef ASMC_FAN_FUNCS2
316 #undef ASMC_LIGHT_FUNCS
321 static device_method_t asmc_methods[] = {
322 DEVMETHOD(device_probe, asmc_probe),
323 DEVMETHOD(device_attach, asmc_attach),
324 DEVMETHOD(device_detach, asmc_detach),
325 DEVMETHOD(device_resume, asmc_resume),
330 static driver_t asmc_driver = {
333 sizeof(struct asmc_softc)
339 #define _COMPONENT ACPI_OEM
340 ACPI_MODULE_NAME("ASMC")
342 #define ASMC_DPRINTF(str) device_printf(dev, str)
344 #define ASMC_DPRINTF(str)
347 /* NB: can't be const */
348 static char *asmc_ids[] = { "APP0001", NULL };
350 static devclass_t asmc_devclass;
352 static unsigned int light_control = 0;
354 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
355 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
357 static struct asmc_model *
358 asmc_match(device_t dev)
363 model = kern_getenv("smbios.system.product");
367 for (i = 0; asmc_models[i].smc_model; i++) {
368 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
370 return (&asmc_models[i]);
379 asmc_probe(device_t dev)
381 struct asmc_model *model;
383 if (resource_disabled("asmc", 0))
385 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
388 model = asmc_match(dev);
390 device_printf(dev, "model not recognized\n");
393 device_set_desc(dev, model->smc_desc);
395 return (BUS_PROBE_DEFAULT);
399 asmc_attach(device_t dev)
404 struct asmc_softc *sc = device_get_softc(dev);
405 struct sysctl_ctx_list *sysctlctx;
406 struct sysctl_oid *sysctlnode;
407 struct asmc_model *model;
409 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
410 &sc->sc_rid_port, RF_ACTIVE);
411 if (sc->sc_ioport == NULL) {
412 device_printf(dev, "unable to allocate IO port\n");
416 sysctlctx = device_get_sysctl_ctx(dev);
417 sysctlnode = device_get_sysctl_tree(dev);
419 model = asmc_match(dev);
421 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
423 sc->sc_model = model;
427 * dev.asmc.n.fan.* tree.
429 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
430 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
431 CTLFLAG_RD, 0, "Fan Root Tree");
433 for (i = 1; i <= sc->sc_nfan; i++) {
437 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
438 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
439 OID_AUTO, name, CTLFLAG_RD, 0,
442 SYSCTL_ADD_PROC(sysctlctx,
443 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
444 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
445 dev, j, model->smc_fan_id, "I",
448 SYSCTL_ADD_PROC(sysctlctx,
449 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
450 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
451 dev, j, model->smc_fan_speed, "I",
454 SYSCTL_ADD_PROC(sysctlctx,
455 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
456 OID_AUTO, "safespeed",
457 CTLTYPE_INT | CTLFLAG_RD,
458 dev, j, model->smc_fan_safespeed, "I",
459 "Fan safe speed in RPM");
461 SYSCTL_ADD_PROC(sysctlctx,
462 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
463 OID_AUTO, "minspeed",
464 CTLTYPE_INT | CTLFLAG_RW,
465 dev, j, model->smc_fan_minspeed, "I",
466 "Fan minimum speed in RPM");
468 SYSCTL_ADD_PROC(sysctlctx,
469 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
470 OID_AUTO, "maxspeed",
471 CTLTYPE_INT | CTLFLAG_RW,
472 dev, j, model->smc_fan_maxspeed, "I",
473 "Fan maximum speed in RPM");
475 SYSCTL_ADD_PROC(sysctlctx,
476 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
477 OID_AUTO, "targetspeed",
478 CTLTYPE_INT | CTLFLAG_RW,
479 dev, j, model->smc_fan_targetspeed, "I",
480 "Fan target speed in RPM");
484 * dev.asmc.n.temp tree.
486 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
487 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
488 CTLFLAG_RD, 0, "Temperature sensors");
490 for (i = 0; model->smc_temps[i]; i++) {
491 SYSCTL_ADD_PROC(sysctlctx,
492 SYSCTL_CHILDREN(sc->sc_temp_tree),
493 OID_AUTO, model->smc_tempnames[i],
494 CTLTYPE_INT | CTLFLAG_RD,
495 dev, i, asmc_temp_sysctl, "I",
496 model->smc_tempdescs[i]);
502 if (model->smc_light_left) {
503 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
504 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
505 CTLFLAG_RD, 0, "Keyboard backlight sensors");
507 SYSCTL_ADD_PROC(sysctlctx,
508 SYSCTL_CHILDREN(sc->sc_light_tree),
509 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
510 dev, 0, model->smc_light_left, "I",
511 "Keyboard backlight left sensor");
513 SYSCTL_ADD_PROC(sysctlctx,
514 SYSCTL_CHILDREN(sc->sc_light_tree),
515 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
516 dev, 0, model->smc_light_right, "I",
517 "Keyboard backlight right sensor");
519 SYSCTL_ADD_PROC(sysctlctx,
520 SYSCTL_CHILDREN(sc->sc_light_tree),
522 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
523 dev, 0, model->smc_light_control, "I",
524 "Keyboard backlight brightness control");
527 if (model->smc_sms_x == NULL)
531 * dev.asmc.n.sms tree.
533 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
534 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
535 CTLFLAG_RD, 0, "Sudden Motion Sensor");
537 SYSCTL_ADD_PROC(sysctlctx,
538 SYSCTL_CHILDREN(sc->sc_sms_tree),
539 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
540 dev, 0, model->smc_sms_x, "I",
541 "Sudden Motion Sensor X value");
543 SYSCTL_ADD_PROC(sysctlctx,
544 SYSCTL_CHILDREN(sc->sc_sms_tree),
545 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
546 dev, 0, model->smc_sms_y, "I",
547 "Sudden Motion Sensor Y value");
549 SYSCTL_ADD_PROC(sysctlctx,
550 SYSCTL_CHILDREN(sc->sc_sms_tree),
551 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
552 dev, 0, model->smc_sms_z, "I",
553 "Sudden Motion Sensor Z value");
556 * Need a taskqueue to send devctl_notify() events
557 * when the SMS interrupt us.
559 * PI_REALTIME is used due to the sensitivity of the
560 * interrupt. An interrupt from the SMS means that the
561 * disk heads should be turned off as quickly as possible.
563 * We only need to do this for the non INTR_FILTER case.
565 sc->sc_sms_tq = NULL;
567 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
568 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
569 taskqueue_thread_enqueue, &sc->sc_sms_tq);
570 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
571 device_get_nameunit(dev));
574 * Allocate an IRQ for the SMS.
577 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
578 &sc->sc_rid_irq, RF_ACTIVE);
579 if (sc->sc_irq == NULL) {
580 device_printf(dev, "unable to allocate IRQ resource\n");
585 ret = bus_setup_intr(dev, sc->sc_irq,
586 INTR_TYPE_MISC | INTR_MPSAFE,
588 asmc_sms_intrfast, asmc_sms_handler,
590 asmc_sms_intrfast, NULL,
592 dev, &sc->sc_cookie);
595 device_printf(dev, "unable to setup SMS IRQ\n");
601 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
603 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
605 mtx_destroy(&sc->sc_mtx);
607 taskqueue_free(sc->sc_sms_tq);
613 asmc_detach(device_t dev)
615 struct asmc_softc *sc = device_get_softc(dev);
618 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
619 taskqueue_free(sc->sc_sms_tq);
622 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
624 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
627 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
629 mtx_destroy(&sc->sc_mtx);
635 asmc_resume(device_t dev)
638 buf[0] = light_control;
640 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
646 void asmc_dumpall(device_t dev)
650 /* XXX magic number */
651 for (i=0; i < 0x100; i++)
652 asmc_key_dump(dev, i);
657 asmc_init(device_t dev)
659 struct asmc_softc *sc = device_get_softc(dev);
663 if (sc->sc_model->smc_sms_x == NULL)
667 * We are ready to receive interrupts from the SMS.
670 ASMC_DPRINTF(("intok key\n"));
671 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
675 * Initiate the polling intervals.
677 buf[0] = 20; /* msecs */
678 ASMC_DPRINTF(("low int key\n"));
679 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
682 buf[0] = 20; /* msecs */
683 ASMC_DPRINTF(("high int key\n"));
684 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
689 ASMC_DPRINTF(("sms low key\n"));
690 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
695 ASMC_DPRINTF(("sms high key\n"));
696 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
700 * I'm not sure what this key does, but it seems to be
704 ASMC_DPRINTF(("sms flag key\n"));
705 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
708 sc->sc_sms_intr_works = 0;
711 * Retry SMS initialization 1000 times
712 * (takes approx. 2 seconds in worst case)
714 for (i = 0; i < 1000; i++) {
715 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
716 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
718 sc->sc_sms_intr_works = 1;
721 buf[0] = ASMC_SMS_INIT1;
722 buf[1] = ASMC_SMS_INIT2;
723 ASMC_DPRINTF(("sms key\n"));
724 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
727 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
730 asmc_sms_calibrate(dev);
732 sc->sc_nfan = asmc_fan_count(dev);
733 if (sc->sc_nfan > ASMC_MAXFANS) {
734 device_printf(dev, "more than %d fans were detected. Please "
735 "report this.\n", ASMC_MAXFANS);
736 sc->sc_nfan = ASMC_MAXFANS;
741 * The number of keys is a 32 bit buffer
743 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
744 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
755 * We need to make sure that the SMC acks the byte sent.
756 * Just wait up to (amount * 10) ms.
759 asmc_wait_ack(device_t dev, uint8_t val, int amount)
761 struct asmc_softc *sc = device_get_softc(dev);
764 val = val & ASMC_STATUS_MASK;
766 for (i = 0; i < amount; i++) {
767 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
776 * We need to make sure that the SMC acks the byte sent.
777 * Just wait up to 100 ms.
780 asmc_wait(device_t dev, uint8_t val)
782 struct asmc_softc *sc;
784 if (asmc_wait_ack(dev, val, 1000) == 0)
787 sc = device_get_softc(dev);
788 val = val & ASMC_STATUS_MASK;
791 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
792 ASMC_CMDPORT_READ(sc));
798 * Send the given command, retrying up to 10 times if
799 * the acknowledgement fails.
802 asmc_command(device_t dev, uint8_t command) {
805 struct asmc_softc *sc = device_get_softc(dev);
807 for (i=0; i < 10; i++) {
808 ASMC_CMDPORT_WRITE(sc, command);
809 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
815 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
816 ASMC_CMDPORT_READ(sc));
822 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
824 int i, error = 1, try = 0;
825 struct asmc_softc *sc = device_get_softc(dev);
827 mtx_lock_spin(&sc->sc_mtx);
830 if (asmc_command(dev, ASMC_CMDREAD))
833 for (i = 0; i < 4; i++) {
834 ASMC_DATAPORT_WRITE(sc, key[i]);
835 if (asmc_wait(dev, 0x04))
839 ASMC_DATAPORT_WRITE(sc, len);
841 for (i = 0; i < len; i++) {
842 if (asmc_wait(dev, 0x05))
844 buf[i] = ASMC_DATAPORT_READ(sc);
850 if (++try < 10) goto begin;
851 device_printf(dev,"%s for key %s failed %d times, giving up\n",
855 mtx_unlock_spin(&sc->sc_mtx);
862 asmc_key_dump(device_t dev, int number)
864 struct asmc_softc *sc = device_get_softc(dev);
866 char type[7] = { 0 };
870 int i, error = 1, try = 0;
872 mtx_lock_spin(&sc->sc_mtx);
874 index[0] = (number >> 24) & 0xff;
875 index[1] = (number >> 16) & 0xff;
876 index[2] = (number >> 8) & 0xff;
877 index[3] = (number) & 0xff;
880 if (asmc_command(dev, 0x12))
883 for (i = 0; i < 4; i++) {
884 ASMC_DATAPORT_WRITE(sc, index[i]);
885 if (asmc_wait(dev, 0x04))
889 ASMC_DATAPORT_WRITE(sc, 4);
891 for (i = 0; i < 4; i++) {
892 if (asmc_wait(dev, 0x05))
894 key[i] = ASMC_DATAPORT_READ(sc);
898 if (asmc_command(dev, 0x13))
901 for (i = 0; i < 4; i++) {
902 ASMC_DATAPORT_WRITE(sc, key[i]);
903 if (asmc_wait(dev, 0x04))
907 ASMC_DATAPORT_WRITE(sc, 6);
909 for (i = 0; i < 6; i++) {
910 if (asmc_wait(dev, 0x05))
912 type[i] = ASMC_DATAPORT_READ(sc);
918 if (++try < 10) goto begin;
919 device_printf(dev,"%s for key %s failed %d times, giving up\n",
921 mtx_unlock_spin(&sc->sc_mtx);
926 mtx_unlock_spin(&sc->sc_mtx);
930 if (maxlen > sizeof(v)) {
932 "WARNING: cropping maxlen from %d to %zu\n",
936 for (i = 0; i < sizeof(v); i++) {
939 asmc_key_read(dev, key, v, maxlen);
940 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
941 "(len %d), data", number, key, type, maxlen);
942 for (i = 0; i < maxlen; i++) {
943 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
944 strlcat(buf, buf2, sizeof(buf));
946 strlcat(buf, " \n", sizeof(buf));
947 device_printf(dev, "%s", buf);
955 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
957 int i, error = -1, try = 0;
958 struct asmc_softc *sc = device_get_softc(dev);
960 mtx_lock_spin(&sc->sc_mtx);
963 ASMC_DPRINTF(("cmd port: cmd write\n"));
964 if (asmc_command(dev, ASMC_CMDWRITE))
967 ASMC_DPRINTF(("data port: key\n"));
968 for (i = 0; i < 4; i++) {
969 ASMC_DATAPORT_WRITE(sc, key[i]);
970 if (asmc_wait(dev, 0x04))
973 ASMC_DPRINTF(("data port: length\n"));
974 ASMC_DATAPORT_WRITE(sc, len);
976 ASMC_DPRINTF(("data port: buffer\n"));
977 for (i = 0; i < len; i++) {
978 if (asmc_wait(dev, 0x04))
980 ASMC_DATAPORT_WRITE(sc, buf[i]);
986 if (++try < 10) goto begin;
987 device_printf(dev,"%s for key %s failed %d times, giving up\n",
991 mtx_unlock_spin(&sc->sc_mtx);
998 * Fan control functions.
1001 asmc_fan_count(device_t dev)
1005 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1012 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1018 snprintf(fankey, sizeof(fankey), key, fan);
1019 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1021 speed = (buf[0] << 6) | (buf[1] >> 2);
1027 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1032 snprintf(fankey, sizeof(fankey), key, fan);
1033 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1041 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1051 snprintf(fankey, sizeof(fankey), key, fan);
1052 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1059 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1061 device_t dev = (device_t) arg1;
1066 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1067 error = sysctl_handle_int(oidp, &v, 0, req);
1073 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1076 device_t dev = (device_t) arg1;
1081 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1084 error = sysctl_handle_string(oidp, desc, 0, req);
1090 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1092 device_t dev = (device_t) arg1;
1097 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1098 error = sysctl_handle_int(oidp, &v, 0, req);
1105 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1107 device_t dev = (device_t) arg1;
1112 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1113 error = sysctl_handle_int(oidp, &v, 0, req);
1115 if (error == 0 && req->newptr != NULL) {
1116 unsigned int newspeed = v;
1117 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1124 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1126 device_t dev = (device_t) arg1;
1131 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1132 error = sysctl_handle_int(oidp, &v, 0, req);
1134 if (error == 0 && req->newptr != NULL) {
1135 unsigned int newspeed = v;
1136 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1143 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1145 device_t dev = (device_t) arg1;
1150 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1151 error = sysctl_handle_int(oidp, &v, 0, req);
1153 if (error == 0 && req->newptr != NULL) {
1154 unsigned int newspeed = v;
1155 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1162 * Temperature functions.
1165 asmc_temp_getvalue(device_t dev, const char *key)
1170 * Check for invalid temperatures.
1172 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1179 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1181 device_t dev = (device_t) arg1;
1182 struct asmc_softc *sc = device_get_softc(dev);
1185 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1186 error = sysctl_handle_int(oidp, &val, 0, req);
1192 * Sudden Motion Sensor functions.
1195 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1200 /* no need to do locking here as asmc_key_read() already does it */
1205 error = asmc_key_read(dev, key, buf, sizeof buf);
1208 device_printf(dev, "%s called with invalid argument %s\n",
1213 *val = ((int16_t)buf[0] << 8) | buf[1];
1219 asmc_sms_calibrate(device_t dev)
1221 struct asmc_softc *sc = device_get_softc(dev);
1223 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1224 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1225 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1229 asmc_sms_intrfast(void *arg)
1232 device_t dev = (device_t) arg;
1233 struct asmc_softc *sc = device_get_softc(dev);
1234 if (!sc->sc_sms_intr_works)
1235 return (FILTER_HANDLED);
1237 mtx_lock_spin(&sc->sc_mtx);
1238 type = ASMC_INTPORT_READ(sc);
1239 mtx_unlock_spin(&sc->sc_mtx);
1241 sc->sc_sms_intrtype = type;
1242 asmc_sms_printintr(dev, type);
1245 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1247 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1249 return (FILTER_HANDLED);
1254 asmc_sms_handler(void *arg)
1256 struct asmc_softc *sc = device_get_softc(arg);
1258 asmc_sms_task(sc, 0);
1264 asmc_sms_printintr(device_t dev, uint8_t type)
1268 case ASMC_SMS_INTFF:
1269 device_printf(dev, "WARNING: possible free fall!\n");
1271 case ASMC_SMS_INTHA:
1272 device_printf(dev, "WARNING: high acceleration detected!\n");
1274 case ASMC_SMS_INTSH:
1275 device_printf(dev, "WARNING: possible shock!\n");
1278 device_printf(dev, "%s unknown interrupt\n", __func__);
1283 asmc_sms_task(void *arg, int pending)
1285 struct asmc_softc *sc = (struct asmc_softc *)arg;
1289 switch (sc->sc_sms_intrtype) {
1290 case ASMC_SMS_INTFF:
1293 case ASMC_SMS_INTHA:
1296 case ASMC_SMS_INTSH:
1303 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1304 devctl_notify("ACPI", "asmc", "SMS", notify);
1308 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1310 device_t dev = (device_t) arg1;
1315 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1317 error = sysctl_handle_int(oidp, &v, 0, req);
1323 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1325 device_t dev = (device_t) arg1;
1330 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1332 error = sysctl_handle_int(oidp, &v, 0, req);
1338 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1340 device_t dev = (device_t) arg1;
1345 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1347 error = sysctl_handle_int(oidp, &v, 0, req);
1353 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1355 device_t dev = (device_t) arg1;
1360 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1362 error = sysctl_handle_int(oidp, &v, 0, req);
1368 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1370 device_t dev = (device_t) arg1;
1375 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1377 error = sysctl_handle_int(oidp, &v, 0, req);
1383 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1385 device_t dev = (device_t) arg1;
1391 error = sysctl_handle_int(oidp, &v, 0, req);
1393 if (error == 0 && req->newptr != NULL) {
1394 if (v < 0 || v > 255)
1397 buf[0] = light_control;
1399 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);