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>
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);
86 static void asmc_sms_printintr(device_t dev, uint8_t);
87 static void asmc_sms_task(void *arg, int pending);
89 void asmc_dumpall(device_t);
90 static int asmc_key_dump(device_t, int);
96 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
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_id)(SYSCTL_HANDLER_ARGS);
119 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
120 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
121 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
122 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
123 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
124 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
125 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
126 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
128 const char *smc_temps[ASMC_TEMP_MAX];
129 const char *smc_tempnames[ASMC_TEMP_MAX];
130 const char *smc_tempdescs[ASMC_TEMP_MAX];
133 static struct asmc_model *asmc_match(device_t dev);
135 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
138 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
140 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
141 asmc_mb_sysctl_fanminspeed, \
142 asmc_mb_sysctl_fanmaxspeed, \
143 asmc_mb_sysctl_fantargetspeed
145 #define ASMC_FAN_FUNCS2 asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
146 asmc_mb_sysctl_fanminspeed, \
147 asmc_mb_sysctl_fanmaxspeed, \
148 asmc_mb_sysctl_fantargetspeed
150 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
151 asmc_mbp_sysctl_light_right, \
152 asmc_mbp_sysctl_light_control
154 struct asmc_model asmc_models[] = {
156 "MacBook1,1", "Apple SMC MacBook Core Duo",
157 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
158 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
162 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
163 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
164 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
168 "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
169 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
170 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
174 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
175 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
176 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
180 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
181 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
182 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
186 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
187 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
188 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
192 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
193 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
194 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
198 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
199 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
200 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
204 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
205 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
206 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
210 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
211 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
212 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
216 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
217 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
218 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
222 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
223 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
224 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
228 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
229 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
230 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
234 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
235 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
236 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
239 /* The Mac Mini has no SMS */
241 "Macmini1,1", "Apple SMC Mac Mini",
245 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
248 /* The Mac Mini 3,1 has no SMS */
250 "Macmini3,1", "Apple SMC Mac Mini 3,1",
254 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
257 /* Idem for the MacPro */
259 "MacPro2", "Apple SMC Mac Pro (8-core)",
263 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
266 /* Idem for the MacPro 2010*/
268 "MacPro5,1", "Apple SMC MacPro (2010)",
272 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
276 "MacBookAir1,1", "Apple SMC MacBook Air",
277 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
278 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
282 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
283 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
284 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
288 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
289 ASMC_SMS_FUNCS_DISABLED,
292 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
296 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
297 ASMC_SMS_FUNCS_DISABLED,
300 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
307 #undef ASMC_SMS_FUNCS
308 #undef ASMC_SMS_FUNCS_DISABLED
309 #undef ASMC_FAN_FUNCS
310 #undef ASMC_FAN_FUNCS2
311 #undef ASMC_LIGHT_FUNCS
316 static device_method_t asmc_methods[] = {
317 DEVMETHOD(device_probe, asmc_probe),
318 DEVMETHOD(device_attach, asmc_attach),
319 DEVMETHOD(device_detach, asmc_detach),
320 DEVMETHOD(device_resume, asmc_resume),
325 static driver_t asmc_driver = {
328 sizeof(struct asmc_softc)
334 #define _COMPONENT ACPI_OEM
335 ACPI_MODULE_NAME("ASMC")
337 #define ASMC_DPRINTF(str) device_printf(dev, str)
339 #define ASMC_DPRINTF(str)
342 /* NB: can't be const */
343 static char *asmc_ids[] = { "APP0001", NULL };
345 static devclass_t asmc_devclass;
347 static unsigned int light_control = 0;
349 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
350 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
352 static struct asmc_model *
353 asmc_match(device_t dev)
358 model = kern_getenv("smbios.system.product");
362 for (i = 0; asmc_models[i].smc_model; i++) {
363 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
365 return (&asmc_models[i]);
374 asmc_probe(device_t dev)
376 struct asmc_model *model;
378 if (resource_disabled("asmc", 0))
380 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
383 model = asmc_match(dev);
385 device_printf(dev, "model not recognized\n");
388 device_set_desc(dev, model->smc_desc);
390 return (BUS_PROBE_DEFAULT);
394 asmc_attach(device_t dev)
399 struct asmc_softc *sc = device_get_softc(dev);
400 struct sysctl_ctx_list *sysctlctx;
401 struct sysctl_oid *sysctlnode;
402 struct asmc_model *model;
404 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
405 &sc->sc_rid_port, RF_ACTIVE);
406 if (sc->sc_ioport == NULL) {
407 device_printf(dev, "unable to allocate IO port\n");
411 sysctlctx = device_get_sysctl_ctx(dev);
412 sysctlnode = device_get_sysctl_tree(dev);
414 model = asmc_match(dev);
416 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
418 sc->sc_model = model;
422 * dev.asmc.n.fan.* tree.
424 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
425 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
426 CTLFLAG_RD, 0, "Fan Root Tree");
428 for (i = 1; i <= sc->sc_nfan; i++) {
432 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
433 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
434 OID_AUTO, name, CTLFLAG_RD, 0,
437 SYSCTL_ADD_PROC(sysctlctx,
438 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
439 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
440 dev, j, model->smc_fan_id, "I",
443 SYSCTL_ADD_PROC(sysctlctx,
444 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
445 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
446 dev, j, model->smc_fan_speed, "I",
449 SYSCTL_ADD_PROC(sysctlctx,
450 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
451 OID_AUTO, "safespeed",
452 CTLTYPE_INT | CTLFLAG_RD,
453 dev, j, model->smc_fan_safespeed, "I",
454 "Fan safe speed in RPM");
456 SYSCTL_ADD_PROC(sysctlctx,
457 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
458 OID_AUTO, "minspeed",
459 CTLTYPE_INT | CTLFLAG_RW,
460 dev, j, model->smc_fan_minspeed, "I",
461 "Fan minimum speed in RPM");
463 SYSCTL_ADD_PROC(sysctlctx,
464 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
465 OID_AUTO, "maxspeed",
466 CTLTYPE_INT | CTLFLAG_RW,
467 dev, j, model->smc_fan_maxspeed, "I",
468 "Fan maximum speed in RPM");
470 SYSCTL_ADD_PROC(sysctlctx,
471 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
472 OID_AUTO, "targetspeed",
473 CTLTYPE_INT | CTLFLAG_RW,
474 dev, j, model->smc_fan_targetspeed, "I",
475 "Fan target speed in RPM");
479 * dev.asmc.n.temp tree.
481 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
482 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
483 CTLFLAG_RD, 0, "Temperature sensors");
485 for (i = 0; model->smc_temps[i]; i++) {
486 SYSCTL_ADD_PROC(sysctlctx,
487 SYSCTL_CHILDREN(sc->sc_temp_tree),
488 OID_AUTO, model->smc_tempnames[i],
489 CTLTYPE_INT | CTLFLAG_RD,
490 dev, i, asmc_temp_sysctl, "I",
491 model->smc_tempdescs[i]);
497 if (model->smc_light_left) {
498 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
499 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
500 CTLFLAG_RD, 0, "Keyboard backlight sensors");
502 SYSCTL_ADD_PROC(sysctlctx,
503 SYSCTL_CHILDREN(sc->sc_light_tree),
504 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
505 dev, 0, model->smc_light_left, "I",
506 "Keyboard backlight left sensor");
508 SYSCTL_ADD_PROC(sysctlctx,
509 SYSCTL_CHILDREN(sc->sc_light_tree),
510 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
511 dev, 0, model->smc_light_right, "I",
512 "Keyboard backlight right sensor");
514 SYSCTL_ADD_PROC(sysctlctx,
515 SYSCTL_CHILDREN(sc->sc_light_tree),
517 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
518 dev, 0, model->smc_light_control, "I",
519 "Keyboard backlight brightness control");
522 if (model->smc_sms_x == NULL)
526 * dev.asmc.n.sms tree.
528 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
529 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
530 CTLFLAG_RD, 0, "Sudden Motion Sensor");
532 SYSCTL_ADD_PROC(sysctlctx,
533 SYSCTL_CHILDREN(sc->sc_sms_tree),
534 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
535 dev, 0, model->smc_sms_x, "I",
536 "Sudden Motion Sensor X value");
538 SYSCTL_ADD_PROC(sysctlctx,
539 SYSCTL_CHILDREN(sc->sc_sms_tree),
540 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
541 dev, 0, model->smc_sms_y, "I",
542 "Sudden Motion Sensor Y value");
544 SYSCTL_ADD_PROC(sysctlctx,
545 SYSCTL_CHILDREN(sc->sc_sms_tree),
546 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
547 dev, 0, model->smc_sms_z, "I",
548 "Sudden Motion Sensor Z value");
551 * Need a taskqueue to send devctl_notify() events
552 * when the SMS interrupt us.
554 * PI_REALTIME is used due to the sensitivity of the
555 * interrupt. An interrupt from the SMS means that the
556 * disk heads should be turned off as quickly as possible.
558 * We only need to do this for the non INTR_FILTER case.
560 sc->sc_sms_tq = NULL;
561 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
562 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
563 taskqueue_thread_enqueue, &sc->sc_sms_tq);
564 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
565 device_get_nameunit(dev));
567 * Allocate an IRQ for the SMS.
570 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
571 &sc->sc_rid_irq, RF_ACTIVE);
572 if (sc->sc_irq == NULL) {
573 device_printf(dev, "unable to allocate IRQ resource\n");
578 ret = bus_setup_intr(dev, sc->sc_irq,
579 INTR_TYPE_MISC | INTR_MPSAFE,
580 asmc_sms_intrfast, NULL,
581 dev, &sc->sc_cookie);
584 device_printf(dev, "unable to setup SMS IRQ\n");
590 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
592 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
594 mtx_destroy(&sc->sc_mtx);
596 taskqueue_free(sc->sc_sms_tq);
602 asmc_detach(device_t dev)
604 struct asmc_softc *sc = device_get_softc(dev);
607 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
608 taskqueue_free(sc->sc_sms_tq);
611 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
613 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
616 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
618 mtx_destroy(&sc->sc_mtx);
624 asmc_resume(device_t dev)
627 buf[0] = light_control;
629 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
635 void asmc_dumpall(device_t dev)
639 /* XXX magic number */
640 for (i=0; i < 0x100; i++)
641 asmc_key_dump(dev, i);
646 asmc_init(device_t dev)
648 struct asmc_softc *sc = device_get_softc(dev);
652 if (sc->sc_model->smc_sms_x == NULL)
656 * We are ready to receive interrupts from the SMS.
659 ASMC_DPRINTF(("intok key\n"));
660 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
664 * Initiate the polling intervals.
666 buf[0] = 20; /* msecs */
667 ASMC_DPRINTF(("low int key\n"));
668 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
671 buf[0] = 20; /* msecs */
672 ASMC_DPRINTF(("high int key\n"));
673 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
678 ASMC_DPRINTF(("sms low key\n"));
679 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
684 ASMC_DPRINTF(("sms high key\n"));
685 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
689 * I'm not sure what this key does, but it seems to be
693 ASMC_DPRINTF(("sms flag key\n"));
694 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
697 sc->sc_sms_intr_works = 0;
700 * Retry SMS initialization 1000 times
701 * (takes approx. 2 seconds in worst case)
703 for (i = 0; i < 1000; i++) {
704 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
705 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
707 sc->sc_sms_intr_works = 1;
710 buf[0] = ASMC_SMS_INIT1;
711 buf[1] = ASMC_SMS_INIT2;
712 ASMC_DPRINTF(("sms key\n"));
713 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
716 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
719 asmc_sms_calibrate(dev);
721 sc->sc_nfan = asmc_fan_count(dev);
722 if (sc->sc_nfan > ASMC_MAXFANS) {
723 device_printf(dev, "more than %d fans were detected. Please "
724 "report this.\n", ASMC_MAXFANS);
725 sc->sc_nfan = ASMC_MAXFANS;
730 * The number of keys is a 32 bit buffer
732 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
733 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
744 * We need to make sure that the SMC acks the byte sent.
745 * Just wait up to (amount * 10) ms.
748 asmc_wait_ack(device_t dev, uint8_t val, int amount)
750 struct asmc_softc *sc = device_get_softc(dev);
753 val = val & ASMC_STATUS_MASK;
755 for (i = 0; i < amount; i++) {
756 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
765 * We need to make sure that the SMC acks the byte sent.
766 * Just wait up to 100 ms.
769 asmc_wait(device_t dev, uint8_t val)
771 struct asmc_softc *sc;
773 if (asmc_wait_ack(dev, val, 1000) == 0)
776 sc = device_get_softc(dev);
777 val = val & ASMC_STATUS_MASK;
780 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
781 ASMC_CMDPORT_READ(sc));
787 * Send the given command, retrying up to 10 times if
788 * the acknowledgement fails.
791 asmc_command(device_t dev, uint8_t command) {
794 struct asmc_softc *sc = device_get_softc(dev);
796 for (i=0; i < 10; i++) {
797 ASMC_CMDPORT_WRITE(sc, command);
798 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
804 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
805 ASMC_CMDPORT_READ(sc));
811 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
813 int i, error = 1, try = 0;
814 struct asmc_softc *sc = device_get_softc(dev);
816 mtx_lock_spin(&sc->sc_mtx);
819 if (asmc_command(dev, ASMC_CMDREAD))
822 for (i = 0; i < 4; i++) {
823 ASMC_DATAPORT_WRITE(sc, key[i]);
824 if (asmc_wait(dev, 0x04))
828 ASMC_DATAPORT_WRITE(sc, len);
830 for (i = 0; i < len; i++) {
831 if (asmc_wait(dev, 0x05))
833 buf[i] = ASMC_DATAPORT_READ(sc);
839 if (++try < 10) goto begin;
840 device_printf(dev,"%s for key %s failed %d times, giving up\n",
844 mtx_unlock_spin(&sc->sc_mtx);
851 asmc_key_dump(device_t dev, int number)
853 struct asmc_softc *sc = device_get_softc(dev);
855 char type[7] = { 0 };
859 int i, error = 1, try = 0;
861 mtx_lock_spin(&sc->sc_mtx);
863 index[0] = (number >> 24) & 0xff;
864 index[1] = (number >> 16) & 0xff;
865 index[2] = (number >> 8) & 0xff;
866 index[3] = (number) & 0xff;
869 if (asmc_command(dev, 0x12))
872 for (i = 0; i < 4; i++) {
873 ASMC_DATAPORT_WRITE(sc, index[i]);
874 if (asmc_wait(dev, 0x04))
878 ASMC_DATAPORT_WRITE(sc, 4);
880 for (i = 0; i < 4; i++) {
881 if (asmc_wait(dev, 0x05))
883 key[i] = ASMC_DATAPORT_READ(sc);
887 if (asmc_command(dev, 0x13))
890 for (i = 0; i < 4; i++) {
891 ASMC_DATAPORT_WRITE(sc, key[i]);
892 if (asmc_wait(dev, 0x04))
896 ASMC_DATAPORT_WRITE(sc, 6);
898 for (i = 0; i < 6; i++) {
899 if (asmc_wait(dev, 0x05))
901 type[i] = ASMC_DATAPORT_READ(sc);
907 if (++try < 10) goto begin;
908 device_printf(dev,"%s for key %s failed %d times, giving up\n",
910 mtx_unlock_spin(&sc->sc_mtx);
915 mtx_unlock_spin(&sc->sc_mtx);
919 if (maxlen > sizeof(v)) {
921 "WARNING: cropping maxlen from %d to %zu\n",
925 for (i = 0; i < sizeof(v); i++) {
928 asmc_key_read(dev, key, v, maxlen);
929 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
930 "(len %d), data", number, key, type, maxlen);
931 for (i = 0; i < maxlen; i++) {
932 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
933 strlcat(buf, buf2, sizeof(buf));
935 strlcat(buf, " \n", sizeof(buf));
936 device_printf(dev, "%s", buf);
944 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
946 int i, error = -1, try = 0;
947 struct asmc_softc *sc = device_get_softc(dev);
949 mtx_lock_spin(&sc->sc_mtx);
952 ASMC_DPRINTF(("cmd port: cmd write\n"));
953 if (asmc_command(dev, ASMC_CMDWRITE))
956 ASMC_DPRINTF(("data port: key\n"));
957 for (i = 0; i < 4; i++) {
958 ASMC_DATAPORT_WRITE(sc, key[i]);
959 if (asmc_wait(dev, 0x04))
962 ASMC_DPRINTF(("data port: length\n"));
963 ASMC_DATAPORT_WRITE(sc, len);
965 ASMC_DPRINTF(("data port: buffer\n"));
966 for (i = 0; i < len; i++) {
967 if (asmc_wait(dev, 0x04))
969 ASMC_DATAPORT_WRITE(sc, buf[i]);
975 if (++try < 10) goto begin;
976 device_printf(dev,"%s for key %s failed %d times, giving up\n",
980 mtx_unlock_spin(&sc->sc_mtx);
987 * Fan control functions.
990 asmc_fan_count(device_t dev)
994 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1001 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1007 snprintf(fankey, sizeof(fankey), key, fan);
1008 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1010 speed = (buf[0] << 6) | (buf[1] >> 2);
1016 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1021 snprintf(fankey, sizeof(fankey), key, fan);
1022 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1030 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1040 snprintf(fankey, sizeof(fankey), key, fan);
1041 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1048 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1050 device_t dev = (device_t) arg1;
1055 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1056 error = sysctl_handle_int(oidp, &v, 0, req);
1062 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1065 device_t dev = (device_t) arg1;
1070 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1073 error = sysctl_handle_string(oidp, desc, 0, req);
1079 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1081 device_t dev = (device_t) arg1;
1086 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1087 error = sysctl_handle_int(oidp, &v, 0, req);
1094 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1096 device_t dev = (device_t) arg1;
1101 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1102 error = sysctl_handle_int(oidp, &v, 0, req);
1104 if (error == 0 && req->newptr != NULL) {
1105 unsigned int newspeed = v;
1106 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1113 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1115 device_t dev = (device_t) arg1;
1120 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1121 error = sysctl_handle_int(oidp, &v, 0, req);
1123 if (error == 0 && req->newptr != NULL) {
1124 unsigned int newspeed = v;
1125 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1132 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1134 device_t dev = (device_t) arg1;
1139 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1140 error = sysctl_handle_int(oidp, &v, 0, req);
1142 if (error == 0 && req->newptr != NULL) {
1143 unsigned int newspeed = v;
1144 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1151 * Temperature functions.
1154 asmc_temp_getvalue(device_t dev, const char *key)
1159 * Check for invalid temperatures.
1161 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1168 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1170 device_t dev = (device_t) arg1;
1171 struct asmc_softc *sc = device_get_softc(dev);
1174 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1175 error = sysctl_handle_int(oidp, &val, 0, req);
1181 * Sudden Motion Sensor functions.
1184 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1189 /* no need to do locking here as asmc_key_read() already does it */
1194 error = asmc_key_read(dev, key, buf, sizeof buf);
1197 device_printf(dev, "%s called with invalid argument %s\n",
1202 *val = ((int16_t)buf[0] << 8) | buf[1];
1208 asmc_sms_calibrate(device_t dev)
1210 struct asmc_softc *sc = device_get_softc(dev);
1212 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1213 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1214 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1218 asmc_sms_intrfast(void *arg)
1221 device_t dev = (device_t) arg;
1222 struct asmc_softc *sc = device_get_softc(dev);
1223 if (!sc->sc_sms_intr_works)
1224 return (FILTER_HANDLED);
1226 mtx_lock_spin(&sc->sc_mtx);
1227 type = ASMC_INTPORT_READ(sc);
1228 mtx_unlock_spin(&sc->sc_mtx);
1230 sc->sc_sms_intrtype = type;
1231 asmc_sms_printintr(dev, type);
1233 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1234 return (FILTER_HANDLED);
1240 asmc_sms_printintr(device_t dev, uint8_t type)
1244 case ASMC_SMS_INTFF:
1245 device_printf(dev, "WARNING: possible free fall!\n");
1247 case ASMC_SMS_INTHA:
1248 device_printf(dev, "WARNING: high acceleration detected!\n");
1250 case ASMC_SMS_INTSH:
1251 device_printf(dev, "WARNING: possible shock!\n");
1254 device_printf(dev, "%s unknown interrupt\n", __func__);
1259 asmc_sms_task(void *arg, int pending)
1261 struct asmc_softc *sc = (struct asmc_softc *)arg;
1265 switch (sc->sc_sms_intrtype) {
1266 case ASMC_SMS_INTFF:
1269 case ASMC_SMS_INTHA:
1272 case ASMC_SMS_INTSH:
1279 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1280 devctl_notify("ACPI", "asmc", "SMS", notify);
1284 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1286 device_t dev = (device_t) arg1;
1291 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1293 error = sysctl_handle_int(oidp, &v, 0, req);
1299 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1301 device_t dev = (device_t) arg1;
1306 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1308 error = sysctl_handle_int(oidp, &v, 0, req);
1314 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1316 device_t dev = (device_t) arg1;
1321 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1323 error = sysctl_handle_int(oidp, &v, 0, req);
1329 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1331 device_t dev = (device_t) arg1;
1336 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1338 error = sysctl_handle_int(oidp, &v, 0, req);
1344 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1346 device_t dev = (device_t) arg1;
1351 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1353 error = sysctl_handle_int(oidp, &v, 0, req);
1359 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1361 device_t dev = (device_t) arg1;
1367 error = sysctl_handle_int(oidp, &v, 0, req);
1369 if (error == 0 && req->newptr != NULL) {
1370 if (v < 0 || v > 255)
1373 buf[0] = light_control;
1375 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);