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 #define ASMC_LIGHT_FUNCS_DISABLED NULL, NULL, NULL
156 struct asmc_model asmc_models[] = {
158 "MacBook1,1", "Apple SMC MacBook Core Duo",
159 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
160 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
164 "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
165 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
166 ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
170 "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
171 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
172 ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
176 "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
177 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
178 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
182 "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
183 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
184 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
188 "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
189 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
190 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
194 "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
195 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
196 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
200 "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
201 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
202 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
206 "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
207 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
208 ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
212 "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
213 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
214 ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
218 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
219 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
220 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
224 "MacBookPro8,1", "Apple SMC MacBook Pro (early 2011, 13-inch)",
225 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
226 ASMC_MBP81_TEMPS, ASMC_MBP81_TEMPNAMES, ASMC_MBP81_TEMPDESCS
230 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
231 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
232 ASMC_MBP82_TEMPS, ASMC_MBP82_TEMPNAMES, ASMC_MBP82_TEMPDESCS
236 "MacBookPro9,2", "Apple SMC MacBook Pro (mid 2012)",
237 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
238 ASMC_MBP9_TEMPS, ASMC_MBP9_TEMPNAMES, ASMC_MBP9_TEMPDESCS
242 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
243 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
244 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
248 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
249 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
250 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
253 /* The Mac Mini has no SMS */
255 "Macmini1,1", "Apple SMC Mac Mini",
259 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
262 /* The Mac Mini 3,1 has no SMS */
264 "Macmini3,1", "Apple SMC Mac Mini 3,1",
268 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
271 /* The Mac Mini 4,1 (Mid-2010) has no SMS */
273 "Macmini4,1", "Apple SMC Mac mini 4,1 (Mid-2010)",
274 ASMC_SMS_FUNCS_DISABLED,
276 ASMC_LIGHT_FUNCS_DISABLED,
277 ASMC_MM41_TEMPS, ASMC_MM41_TEMPNAMES, ASMC_MM41_TEMPDESCS
280 /* The Mac Mini 5,2 has no SMS */
282 "Macmini5,2", "Apple SMC Mac Mini 5,2",
286 ASMC_MM52_TEMPS, ASMC_MM52_TEMPNAMES, ASMC_MM52_TEMPDESCS
289 /* Idem for the Mac Pro "Quad Core" (original) */
291 "MacPro1,1", "Apple SMC Mac Pro (Quad Core)",
295 ASMC_MP1_TEMPS, ASMC_MP1_TEMPNAMES, ASMC_MP1_TEMPDESCS
298 /* Idem for the Mac Pro (8-core) */
300 "MacPro2", "Apple SMC Mac Pro (8-core)",
304 ASMC_MP2_TEMPS, ASMC_MP2_TEMPNAMES, ASMC_MP2_TEMPDESCS
307 /* Idem for the MacPro 2010*/
309 "MacPro5,1", "Apple SMC MacPro (2010)",
313 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
317 "MacBookAir1,1", "Apple SMC MacBook Air",
318 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
319 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
323 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
324 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
325 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
329 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
330 ASMC_SMS_FUNCS_DISABLED,
333 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
337 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
338 ASMC_SMS_FUNCS_DISABLED,
341 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
345 "MacBookAir7,1", "Apple SMC MacBook Air 11-inch (Early 2015)",
346 ASMC_SMS_FUNCS_DISABLED,
349 ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
353 "MacBookAir7,2", "Apple SMC MacBook Air 13-inch (Early 2015)",
354 ASMC_SMS_FUNCS_DISABLED,
357 ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
363 #undef ASMC_SMS_FUNCS
364 #undef ASMC_SMS_FUNCS_DISABLED
365 #undef ASMC_FAN_FUNCS
366 #undef ASMC_FAN_FUNCS2
367 #undef ASMC_LIGHT_FUNCS
372 static device_method_t asmc_methods[] = {
373 DEVMETHOD(device_probe, asmc_probe),
374 DEVMETHOD(device_attach, asmc_attach),
375 DEVMETHOD(device_detach, asmc_detach),
376 DEVMETHOD(device_resume, asmc_resume),
381 static driver_t asmc_driver = {
384 sizeof(struct asmc_softc)
390 #define _COMPONENT ACPI_OEM
391 ACPI_MODULE_NAME("ASMC")
393 #define ASMC_DPRINTF(str) device_printf(dev, str)
395 #define ASMC_DPRINTF(str)
398 /* NB: can't be const */
399 static char *asmc_ids[] = { "APP0001", NULL };
401 static devclass_t asmc_devclass;
403 static unsigned int light_control = 0;
405 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
406 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
408 static struct asmc_model *
409 asmc_match(device_t dev)
414 model = kern_getenv("smbios.system.product");
418 for (i = 0; asmc_models[i].smc_model; i++) {
419 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
421 return (&asmc_models[i]);
430 asmc_probe(device_t dev)
432 struct asmc_model *model;
435 if (resource_disabled("asmc", 0))
437 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
441 model = asmc_match(dev);
443 device_printf(dev, "model not recognized\n");
446 device_set_desc(dev, model->smc_desc);
452 asmc_attach(device_t dev)
457 struct asmc_softc *sc = device_get_softc(dev);
458 struct sysctl_ctx_list *sysctlctx;
459 struct sysctl_oid *sysctlnode;
460 struct asmc_model *model;
462 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
463 &sc->sc_rid_port, RF_ACTIVE);
464 if (sc->sc_ioport == NULL) {
465 device_printf(dev, "unable to allocate IO port\n");
469 sysctlctx = device_get_sysctl_ctx(dev);
470 sysctlnode = device_get_sysctl_tree(dev);
472 model = asmc_match(dev);
474 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
476 sc->sc_model = model;
480 * dev.asmc.n.fan.* tree.
482 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
483 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
484 CTLFLAG_RD, 0, "Fan Root Tree");
486 for (i = 1; i <= sc->sc_nfan; i++) {
490 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
491 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
492 OID_AUTO, name, CTLFLAG_RD, 0,
495 SYSCTL_ADD_PROC(sysctlctx,
496 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
497 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
498 dev, j, model->smc_fan_id, "I",
501 SYSCTL_ADD_PROC(sysctlctx,
502 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
503 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
504 dev, j, model->smc_fan_speed, "I",
507 SYSCTL_ADD_PROC(sysctlctx,
508 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
509 OID_AUTO, "safespeed",
510 CTLTYPE_INT | CTLFLAG_RD,
511 dev, j, model->smc_fan_safespeed, "I",
512 "Fan safe speed in RPM");
514 SYSCTL_ADD_PROC(sysctlctx,
515 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
516 OID_AUTO, "minspeed",
517 CTLTYPE_INT | CTLFLAG_RW,
518 dev, j, model->smc_fan_minspeed, "I",
519 "Fan minimum speed in RPM");
521 SYSCTL_ADD_PROC(sysctlctx,
522 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
523 OID_AUTO, "maxspeed",
524 CTLTYPE_INT | CTLFLAG_RW,
525 dev, j, model->smc_fan_maxspeed, "I",
526 "Fan maximum speed in RPM");
528 SYSCTL_ADD_PROC(sysctlctx,
529 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
530 OID_AUTO, "targetspeed",
531 CTLTYPE_INT | CTLFLAG_RW,
532 dev, j, model->smc_fan_targetspeed, "I",
533 "Fan target speed in RPM");
537 * dev.asmc.n.temp tree.
539 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
540 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
541 CTLFLAG_RD, 0, "Temperature sensors");
543 for (i = 0; model->smc_temps[i]; i++) {
544 SYSCTL_ADD_PROC(sysctlctx,
545 SYSCTL_CHILDREN(sc->sc_temp_tree),
546 OID_AUTO, model->smc_tempnames[i],
547 CTLTYPE_INT | CTLFLAG_RD,
548 dev, i, asmc_temp_sysctl, "I",
549 model->smc_tempdescs[i]);
555 if (model->smc_light_left) {
556 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
557 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
558 CTLFLAG_RD, 0, "Keyboard backlight sensors");
560 SYSCTL_ADD_PROC(sysctlctx,
561 SYSCTL_CHILDREN(sc->sc_light_tree),
562 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
563 dev, 0, model->smc_light_left, "I",
564 "Keyboard backlight left sensor");
566 SYSCTL_ADD_PROC(sysctlctx,
567 SYSCTL_CHILDREN(sc->sc_light_tree),
568 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
569 dev, 0, model->smc_light_right, "I",
570 "Keyboard backlight right sensor");
572 SYSCTL_ADD_PROC(sysctlctx,
573 SYSCTL_CHILDREN(sc->sc_light_tree),
575 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
576 dev, 0, model->smc_light_control, "I",
577 "Keyboard backlight brightness control");
580 if (model->smc_sms_x == NULL)
584 * dev.asmc.n.sms tree.
586 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
587 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
588 CTLFLAG_RD, 0, "Sudden Motion Sensor");
590 SYSCTL_ADD_PROC(sysctlctx,
591 SYSCTL_CHILDREN(sc->sc_sms_tree),
592 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
593 dev, 0, model->smc_sms_x, "I",
594 "Sudden Motion Sensor X value");
596 SYSCTL_ADD_PROC(sysctlctx,
597 SYSCTL_CHILDREN(sc->sc_sms_tree),
598 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
599 dev, 0, model->smc_sms_y, "I",
600 "Sudden Motion Sensor Y value");
602 SYSCTL_ADD_PROC(sysctlctx,
603 SYSCTL_CHILDREN(sc->sc_sms_tree),
604 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
605 dev, 0, model->smc_sms_z, "I",
606 "Sudden Motion Sensor Z value");
609 * Need a taskqueue to send devctl_notify() events
610 * when the SMS interrupt us.
612 * PI_REALTIME is used due to the sensitivity of the
613 * interrupt. An interrupt from the SMS means that the
614 * disk heads should be turned off as quickly as possible.
616 * We only need to do this for the non INTR_FILTER case.
618 sc->sc_sms_tq = NULL;
619 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
620 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
621 taskqueue_thread_enqueue, &sc->sc_sms_tq);
622 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
623 device_get_nameunit(dev));
625 * Allocate an IRQ for the SMS.
628 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
629 &sc->sc_rid_irq, RF_ACTIVE);
630 if (sc->sc_irq == NULL) {
631 device_printf(dev, "unable to allocate IRQ resource\n");
636 ret = bus_setup_intr(dev, sc->sc_irq,
637 INTR_TYPE_MISC | INTR_MPSAFE,
638 asmc_sms_intrfast, NULL,
639 dev, &sc->sc_cookie);
642 device_printf(dev, "unable to setup SMS IRQ\n");
648 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
650 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
652 mtx_destroy(&sc->sc_mtx);
654 taskqueue_free(sc->sc_sms_tq);
660 asmc_detach(device_t dev)
662 struct asmc_softc *sc = device_get_softc(dev);
665 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
666 taskqueue_free(sc->sc_sms_tq);
669 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
671 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
674 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
676 mtx_destroy(&sc->sc_mtx);
682 asmc_resume(device_t dev)
685 buf[0] = light_control;
687 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
693 void asmc_dumpall(device_t dev)
697 /* XXX magic number */
698 for (i=0; i < 0x100; i++)
699 asmc_key_dump(dev, i);
704 asmc_init(device_t dev)
706 struct asmc_softc *sc = device_get_softc(dev);
710 if (sc->sc_model->smc_sms_x == NULL)
714 * We are ready to receive interrupts from the SMS.
717 ASMC_DPRINTF(("intok key\n"));
718 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
722 * Initiate the polling intervals.
724 buf[0] = 20; /* msecs */
725 ASMC_DPRINTF(("low int key\n"));
726 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
729 buf[0] = 20; /* msecs */
730 ASMC_DPRINTF(("high int key\n"));
731 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
736 ASMC_DPRINTF(("sms low key\n"));
737 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
742 ASMC_DPRINTF(("sms high key\n"));
743 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
747 * I'm not sure what this key does, but it seems to be
751 ASMC_DPRINTF(("sms flag key\n"));
752 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
755 sc->sc_sms_intr_works = 0;
758 * Retry SMS initialization 1000 times
759 * (takes approx. 2 seconds in worst case)
761 for (i = 0; i < 1000; i++) {
762 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
763 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
765 sc->sc_sms_intr_works = 1;
768 buf[0] = ASMC_SMS_INIT1;
769 buf[1] = ASMC_SMS_INIT2;
770 ASMC_DPRINTF(("sms key\n"));
771 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
774 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
777 asmc_sms_calibrate(dev);
779 sc->sc_nfan = asmc_fan_count(dev);
780 if (sc->sc_nfan > ASMC_MAXFANS) {
781 device_printf(dev, "more than %d fans were detected. Please "
782 "report this.\n", ASMC_MAXFANS);
783 sc->sc_nfan = ASMC_MAXFANS;
788 * The number of keys is a 32 bit buffer
790 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
791 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
802 * We need to make sure that the SMC acks the byte sent.
803 * Just wait up to (amount * 10) ms.
806 asmc_wait_ack(device_t dev, uint8_t val, int amount)
808 struct asmc_softc *sc = device_get_softc(dev);
811 val = val & ASMC_STATUS_MASK;
813 for (i = 0; i < amount; i++) {
814 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
823 * We need to make sure that the SMC acks the byte sent.
824 * Just wait up to 100 ms.
827 asmc_wait(device_t dev, uint8_t val)
829 struct asmc_softc *sc;
831 if (asmc_wait_ack(dev, val, 1000) == 0)
834 sc = device_get_softc(dev);
835 val = val & ASMC_STATUS_MASK;
838 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
839 ASMC_CMDPORT_READ(sc));
845 * Send the given command, retrying up to 10 times if
846 * the acknowledgement fails.
849 asmc_command(device_t dev, uint8_t command) {
852 struct asmc_softc *sc = device_get_softc(dev);
854 for (i=0; i < 10; i++) {
855 ASMC_CMDPORT_WRITE(sc, command);
856 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
862 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
863 ASMC_CMDPORT_READ(sc));
869 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
871 int i, error = 1, try = 0;
872 struct asmc_softc *sc = device_get_softc(dev);
874 mtx_lock_spin(&sc->sc_mtx);
877 if (asmc_command(dev, ASMC_CMDREAD))
880 for (i = 0; i < 4; i++) {
881 ASMC_DATAPORT_WRITE(sc, key[i]);
882 if (asmc_wait(dev, 0x04))
886 ASMC_DATAPORT_WRITE(sc, len);
888 for (i = 0; i < len; i++) {
889 if (asmc_wait(dev, 0x05))
891 buf[i] = ASMC_DATAPORT_READ(sc);
897 if (++try < 10) goto begin;
898 device_printf(dev,"%s for key %s failed %d times, giving up\n",
902 mtx_unlock_spin(&sc->sc_mtx);
909 asmc_key_dump(device_t dev, int number)
911 struct asmc_softc *sc = device_get_softc(dev);
913 char type[7] = { 0 };
917 int i, error = 1, try = 0;
919 mtx_lock_spin(&sc->sc_mtx);
921 index[0] = (number >> 24) & 0xff;
922 index[1] = (number >> 16) & 0xff;
923 index[2] = (number >> 8) & 0xff;
924 index[3] = (number) & 0xff;
927 if (asmc_command(dev, 0x12))
930 for (i = 0; i < 4; i++) {
931 ASMC_DATAPORT_WRITE(sc, index[i]);
932 if (asmc_wait(dev, 0x04))
936 ASMC_DATAPORT_WRITE(sc, 4);
938 for (i = 0; i < 4; i++) {
939 if (asmc_wait(dev, 0x05))
941 key[i] = ASMC_DATAPORT_READ(sc);
945 if (asmc_command(dev, 0x13))
948 for (i = 0; i < 4; i++) {
949 ASMC_DATAPORT_WRITE(sc, key[i]);
950 if (asmc_wait(dev, 0x04))
954 ASMC_DATAPORT_WRITE(sc, 6);
956 for (i = 0; i < 6; i++) {
957 if (asmc_wait(dev, 0x05))
959 type[i] = ASMC_DATAPORT_READ(sc);
965 if (++try < 10) goto begin;
966 device_printf(dev,"%s for key %s failed %d times, giving up\n",
968 mtx_unlock_spin(&sc->sc_mtx);
973 mtx_unlock_spin(&sc->sc_mtx);
977 if (maxlen > sizeof(v)) {
979 "WARNING: cropping maxlen from %d to %zu\n",
983 for (i = 0; i < sizeof(v); i++) {
986 asmc_key_read(dev, key, v, maxlen);
987 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
988 "(len %d), data", number, key, type, maxlen);
989 for (i = 0; i < maxlen; i++) {
990 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
991 strlcat(buf, buf2, sizeof(buf));
993 strlcat(buf, " \n", sizeof(buf));
994 device_printf(dev, "%s", buf);
1002 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1004 int i, error = -1, try = 0;
1005 struct asmc_softc *sc = device_get_softc(dev);
1007 mtx_lock_spin(&sc->sc_mtx);
1010 ASMC_DPRINTF(("cmd port: cmd write\n"));
1011 if (asmc_command(dev, ASMC_CMDWRITE))
1014 ASMC_DPRINTF(("data port: key\n"));
1015 for (i = 0; i < 4; i++) {
1016 ASMC_DATAPORT_WRITE(sc, key[i]);
1017 if (asmc_wait(dev, 0x04))
1020 ASMC_DPRINTF(("data port: length\n"));
1021 ASMC_DATAPORT_WRITE(sc, len);
1023 ASMC_DPRINTF(("data port: buffer\n"));
1024 for (i = 0; i < len; i++) {
1025 if (asmc_wait(dev, 0x04))
1027 ASMC_DATAPORT_WRITE(sc, buf[i]);
1033 if (++try < 10) goto begin;
1034 device_printf(dev,"%s for key %s failed %d times, giving up\n",
1035 __func__, key, try);
1038 mtx_unlock_spin(&sc->sc_mtx);
1045 * Fan control functions.
1048 asmc_fan_count(device_t dev)
1052 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1059 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1065 snprintf(fankey, sizeof(fankey), key, fan);
1066 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1068 speed = (buf[0] << 6) | (buf[1] >> 2);
1074 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1079 snprintf(fankey, sizeof(fankey), key, fan);
1080 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1088 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1098 snprintf(fankey, sizeof(fankey), key, fan);
1099 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1106 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1108 device_t dev = (device_t) arg1;
1113 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1114 error = sysctl_handle_int(oidp, &v, 0, req);
1120 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1123 device_t dev = (device_t) arg1;
1128 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1131 error = sysctl_handle_string(oidp, desc, 0, req);
1137 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1139 device_t dev = (device_t) arg1;
1144 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1145 error = sysctl_handle_int(oidp, &v, 0, req);
1152 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1154 device_t dev = (device_t) arg1;
1159 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1160 error = sysctl_handle_int(oidp, &v, 0, req);
1162 if (error == 0 && req->newptr != NULL) {
1163 unsigned int newspeed = v;
1164 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1171 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1173 device_t dev = (device_t) arg1;
1178 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1179 error = sysctl_handle_int(oidp, &v, 0, req);
1181 if (error == 0 && req->newptr != NULL) {
1182 unsigned int newspeed = v;
1183 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1190 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1192 device_t dev = (device_t) arg1;
1197 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1198 error = sysctl_handle_int(oidp, &v, 0, req);
1200 if (error == 0 && req->newptr != NULL) {
1201 unsigned int newspeed = v;
1202 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1209 * Temperature functions.
1212 asmc_temp_getvalue(device_t dev, const char *key)
1217 * Check for invalid temperatures.
1219 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1226 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1228 device_t dev = (device_t) arg1;
1229 struct asmc_softc *sc = device_get_softc(dev);
1232 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1233 error = sysctl_handle_int(oidp, &val, 0, req);
1239 * Sudden Motion Sensor functions.
1242 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1247 /* no need to do locking here as asmc_key_read() already does it */
1252 error = asmc_key_read(dev, key, buf, sizeof buf);
1255 device_printf(dev, "%s called with invalid argument %s\n",
1260 *val = ((int16_t)buf[0] << 8) | buf[1];
1266 asmc_sms_calibrate(device_t dev)
1268 struct asmc_softc *sc = device_get_softc(dev);
1270 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1271 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1272 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1276 asmc_sms_intrfast(void *arg)
1279 device_t dev = (device_t) arg;
1280 struct asmc_softc *sc = device_get_softc(dev);
1281 if (!sc->sc_sms_intr_works)
1282 return (FILTER_HANDLED);
1284 mtx_lock_spin(&sc->sc_mtx);
1285 type = ASMC_INTPORT_READ(sc);
1286 mtx_unlock_spin(&sc->sc_mtx);
1288 sc->sc_sms_intrtype = type;
1289 asmc_sms_printintr(dev, type);
1291 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1292 return (FILTER_HANDLED);
1298 asmc_sms_printintr(device_t dev, uint8_t type)
1302 case ASMC_SMS_INTFF:
1303 device_printf(dev, "WARNING: possible free fall!\n");
1305 case ASMC_SMS_INTHA:
1306 device_printf(dev, "WARNING: high acceleration detected!\n");
1308 case ASMC_SMS_INTSH:
1309 device_printf(dev, "WARNING: possible shock!\n");
1312 device_printf(dev, "%s unknown interrupt\n", __func__);
1317 asmc_sms_task(void *arg, int pending)
1319 struct asmc_softc *sc = (struct asmc_softc *)arg;
1323 switch (sc->sc_sms_intrtype) {
1324 case ASMC_SMS_INTFF:
1327 case ASMC_SMS_INTHA:
1330 case ASMC_SMS_INTSH:
1337 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1338 devctl_notify("ACPI", "asmc", "SMS", notify);
1342 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1344 device_t dev = (device_t) arg1;
1349 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1351 error = sysctl_handle_int(oidp, &v, 0, req);
1357 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1359 device_t dev = (device_t) arg1;
1364 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1366 error = sysctl_handle_int(oidp, &v, 0, req);
1372 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1374 device_t dev = (device_t) arg1;
1379 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1381 error = sysctl_handle_int(oidp, &v, 0, req);
1387 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1389 device_t dev = (device_t) arg1;
1394 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1396 error = sysctl_handle_int(oidp, &v, 0, req);
1402 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1404 device_t dev = (device_t) arg1;
1409 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1411 error = sysctl_handle_int(oidp, &v, 0, req);
1417 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1419 device_t dev = (device_t) arg1;
1425 error = sysctl_handle_int(oidp, &v, 0, req);
1427 if (error == 0 && req->newptr != NULL) {
1428 if (v < 0 || v > 255)
1431 buf[0] = light_control;
1433 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);