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 "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
220 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
221 ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
225 "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
226 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
227 ASMC_MBP8_TEMPS, ASMC_MBP8_TEMPNAMES, ASMC_MBP8_TEMPDESCS
231 "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
232 ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
233 ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
237 "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
238 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
239 ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
242 /* The Mac Mini has no SMS */
244 "Macmini1,1", "Apple SMC Mac Mini",
248 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
251 /* The Mac Mini 3,1 has no SMS */
253 "Macmini3,1", "Apple SMC Mac Mini 3,1",
257 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
260 /* Idem for the MacPro */
262 "MacPro2", "Apple SMC Mac Pro (8-core)",
266 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
269 /* Idem for the MacPro 2010*/
271 "MacPro5,1", "Apple SMC MacPro (2010)",
275 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
279 "MacBookAir1,1", "Apple SMC MacBook Air",
280 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
281 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
285 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
286 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
287 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
291 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
292 ASMC_SMS_FUNCS_DISABLED,
295 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
299 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
300 ASMC_SMS_FUNCS_DISABLED,
303 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
310 #undef ASMC_SMS_FUNCS
311 #undef ASMC_SMS_FUNCS_DISABLED
312 #undef ASMC_FAN_FUNCS
313 #undef ASMC_FAN_FUNCS2
314 #undef ASMC_LIGHT_FUNCS
319 static device_method_t asmc_methods[] = {
320 DEVMETHOD(device_probe, asmc_probe),
321 DEVMETHOD(device_attach, asmc_attach),
322 DEVMETHOD(device_detach, asmc_detach),
323 DEVMETHOD(device_resume, asmc_resume),
328 static driver_t asmc_driver = {
331 sizeof(struct asmc_softc)
337 #define _COMPONENT ACPI_OEM
338 ACPI_MODULE_NAME("ASMC")
340 #define ASMC_DPRINTF(str) device_printf(dev, str)
342 #define ASMC_DPRINTF(str)
345 /* NB: can't be const */
346 static char *asmc_ids[] = { "APP0001", NULL };
348 static devclass_t asmc_devclass;
350 static unsigned int light_control = 0;
352 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
353 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
355 static struct asmc_model *
356 asmc_match(device_t dev)
361 model = kern_getenv("smbios.system.product");
365 for (i = 0; asmc_models[i].smc_model; i++) {
366 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
368 return (&asmc_models[i]);
377 asmc_probe(device_t dev)
379 struct asmc_model *model;
381 if (resource_disabled("asmc", 0))
383 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
386 model = asmc_match(dev);
388 device_printf(dev, "model not recognized\n");
391 device_set_desc(dev, model->smc_desc);
393 return (BUS_PROBE_DEFAULT);
397 asmc_attach(device_t dev)
402 struct asmc_softc *sc = device_get_softc(dev);
403 struct sysctl_ctx_list *sysctlctx;
404 struct sysctl_oid *sysctlnode;
405 struct asmc_model *model;
407 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
408 &sc->sc_rid_port, RF_ACTIVE);
409 if (sc->sc_ioport == NULL) {
410 device_printf(dev, "unable to allocate IO port\n");
414 sysctlctx = device_get_sysctl_ctx(dev);
415 sysctlnode = device_get_sysctl_tree(dev);
417 model = asmc_match(dev);
419 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
421 sc->sc_model = model;
425 * dev.asmc.n.fan.* tree.
427 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
428 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
429 CTLFLAG_RD, 0, "Fan Root Tree");
431 for (i = 1; i <= sc->sc_nfan; i++) {
435 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
436 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
437 OID_AUTO, name, CTLFLAG_RD, 0,
440 SYSCTL_ADD_PROC(sysctlctx,
441 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
442 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
443 dev, j, model->smc_fan_id, "I",
446 SYSCTL_ADD_PROC(sysctlctx,
447 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
448 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
449 dev, j, model->smc_fan_speed, "I",
452 SYSCTL_ADD_PROC(sysctlctx,
453 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
454 OID_AUTO, "safespeed",
455 CTLTYPE_INT | CTLFLAG_RD,
456 dev, j, model->smc_fan_safespeed, "I",
457 "Fan safe speed in RPM");
459 SYSCTL_ADD_PROC(sysctlctx,
460 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
461 OID_AUTO, "minspeed",
462 CTLTYPE_INT | CTLFLAG_RW,
463 dev, j, model->smc_fan_minspeed, "I",
464 "Fan minimum speed in RPM");
466 SYSCTL_ADD_PROC(sysctlctx,
467 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
468 OID_AUTO, "maxspeed",
469 CTLTYPE_INT | CTLFLAG_RW,
470 dev, j, model->smc_fan_maxspeed, "I",
471 "Fan maximum speed in RPM");
473 SYSCTL_ADD_PROC(sysctlctx,
474 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
475 OID_AUTO, "targetspeed",
476 CTLTYPE_INT | CTLFLAG_RW,
477 dev, j, model->smc_fan_targetspeed, "I",
478 "Fan target speed in RPM");
482 * dev.asmc.n.temp tree.
484 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
485 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
486 CTLFLAG_RD, 0, "Temperature sensors");
488 for (i = 0; model->smc_temps[i]; i++) {
489 SYSCTL_ADD_PROC(sysctlctx,
490 SYSCTL_CHILDREN(sc->sc_temp_tree),
491 OID_AUTO, model->smc_tempnames[i],
492 CTLTYPE_INT | CTLFLAG_RD,
493 dev, i, asmc_temp_sysctl, "I",
494 model->smc_tempdescs[i]);
500 if (model->smc_light_left) {
501 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
502 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
503 CTLFLAG_RD, 0, "Keyboard backlight sensors");
505 SYSCTL_ADD_PROC(sysctlctx,
506 SYSCTL_CHILDREN(sc->sc_light_tree),
507 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
508 dev, 0, model->smc_light_left, "I",
509 "Keyboard backlight left sensor");
511 SYSCTL_ADD_PROC(sysctlctx,
512 SYSCTL_CHILDREN(sc->sc_light_tree),
513 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
514 dev, 0, model->smc_light_right, "I",
515 "Keyboard backlight right sensor");
517 SYSCTL_ADD_PROC(sysctlctx,
518 SYSCTL_CHILDREN(sc->sc_light_tree),
520 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
521 dev, 0, model->smc_light_control, "I",
522 "Keyboard backlight brightness control");
525 if (model->smc_sms_x == NULL)
529 * dev.asmc.n.sms tree.
531 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
532 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
533 CTLFLAG_RD, 0, "Sudden Motion Sensor");
535 SYSCTL_ADD_PROC(sysctlctx,
536 SYSCTL_CHILDREN(sc->sc_sms_tree),
537 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
538 dev, 0, model->smc_sms_x, "I",
539 "Sudden Motion Sensor X value");
541 SYSCTL_ADD_PROC(sysctlctx,
542 SYSCTL_CHILDREN(sc->sc_sms_tree),
543 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
544 dev, 0, model->smc_sms_y, "I",
545 "Sudden Motion Sensor Y value");
547 SYSCTL_ADD_PROC(sysctlctx,
548 SYSCTL_CHILDREN(sc->sc_sms_tree),
549 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
550 dev, 0, model->smc_sms_z, "I",
551 "Sudden Motion Sensor Z value");
554 * Need a taskqueue to send devctl_notify() events
555 * when the SMS interrupt us.
557 * PI_REALTIME is used due to the sensitivity of the
558 * interrupt. An interrupt from the SMS means that the
559 * disk heads should be turned off as quickly as possible.
561 * We only need to do this for the non INTR_FILTER case.
563 sc->sc_sms_tq = NULL;
565 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
566 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
567 taskqueue_thread_enqueue, &sc->sc_sms_tq);
568 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
569 device_get_nameunit(dev));
572 * Allocate an IRQ for the SMS.
575 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
576 &sc->sc_rid_irq, RF_ACTIVE);
577 if (sc->sc_irq == NULL) {
578 device_printf(dev, "unable to allocate IRQ resource\n");
583 ret = bus_setup_intr(dev, sc->sc_irq,
584 INTR_TYPE_MISC | INTR_MPSAFE,
586 asmc_sms_intrfast, asmc_sms_handler,
588 asmc_sms_intrfast, NULL,
590 dev, &sc->sc_cookie);
593 device_printf(dev, "unable to setup SMS IRQ\n");
599 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
601 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
603 mtx_destroy(&sc->sc_mtx);
605 taskqueue_free(sc->sc_sms_tq);
611 asmc_detach(device_t dev)
613 struct asmc_softc *sc = device_get_softc(dev);
616 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
617 taskqueue_free(sc->sc_sms_tq);
620 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
622 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
625 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
627 mtx_destroy(&sc->sc_mtx);
633 asmc_resume(device_t dev)
636 buf[0] = light_control;
638 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
644 void asmc_dumpall(device_t dev)
648 /* XXX magic number */
649 for (i=0; i < 0x100; i++)
650 asmc_key_dump(dev, i);
655 asmc_init(device_t dev)
657 struct asmc_softc *sc = device_get_softc(dev);
661 if (sc->sc_model->smc_sms_x == NULL)
665 * We are ready to receive interrupts from the SMS.
668 ASMC_DPRINTF(("intok key\n"));
669 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
673 * Initiate the polling intervals.
675 buf[0] = 20; /* msecs */
676 ASMC_DPRINTF(("low int key\n"));
677 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
680 buf[0] = 20; /* msecs */
681 ASMC_DPRINTF(("high int key\n"));
682 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
687 ASMC_DPRINTF(("sms low key\n"));
688 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
693 ASMC_DPRINTF(("sms high key\n"));
694 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
698 * I'm not sure what this key does, but it seems to be
702 ASMC_DPRINTF(("sms flag key\n"));
703 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
706 sc->sc_sms_intr_works = 0;
709 * Retry SMS initialization 1000 times
710 * (takes approx. 2 seconds in worst case)
712 for (i = 0; i < 1000; i++) {
713 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
714 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
716 sc->sc_sms_intr_works = 1;
719 buf[0] = ASMC_SMS_INIT1;
720 buf[1] = ASMC_SMS_INIT2;
721 ASMC_DPRINTF(("sms key\n"));
722 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
725 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
728 asmc_sms_calibrate(dev);
730 sc->sc_nfan = asmc_fan_count(dev);
731 if (sc->sc_nfan > ASMC_MAXFANS) {
732 device_printf(dev, "more than %d fans were detected. Please "
733 "report this.\n", ASMC_MAXFANS);
734 sc->sc_nfan = ASMC_MAXFANS;
739 * The number of keys is a 32 bit buffer
741 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
742 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
753 * We need to make sure that the SMC acks the byte sent.
754 * Just wait up to (amount * 10) ms.
757 asmc_wait_ack(device_t dev, uint8_t val, int amount)
759 struct asmc_softc *sc = device_get_softc(dev);
762 val = val & ASMC_STATUS_MASK;
764 for (i = 0; i < amount; i++) {
765 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
774 * We need to make sure that the SMC acks the byte sent.
775 * Just wait up to 100 ms.
778 asmc_wait(device_t dev, uint8_t val)
780 struct asmc_softc *sc;
782 if (asmc_wait_ack(dev, val, 1000) == 0)
785 sc = device_get_softc(dev);
786 val = val & ASMC_STATUS_MASK;
789 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
790 ASMC_CMDPORT_READ(sc));
796 * Send the given command, retrying up to 10 times if
797 * the acknowledgement fails.
800 asmc_command(device_t dev, uint8_t command) {
803 struct asmc_softc *sc = device_get_softc(dev);
805 for (i=0; i < 10; i++) {
806 ASMC_CMDPORT_WRITE(sc, command);
807 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
813 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
814 ASMC_CMDPORT_READ(sc));
820 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
822 int i, error = 1, try = 0;
823 struct asmc_softc *sc = device_get_softc(dev);
825 mtx_lock_spin(&sc->sc_mtx);
828 if (asmc_command(dev, ASMC_CMDREAD))
831 for (i = 0; i < 4; i++) {
832 ASMC_DATAPORT_WRITE(sc, key[i]);
833 if (asmc_wait(dev, 0x04))
837 ASMC_DATAPORT_WRITE(sc, len);
839 for (i = 0; i < len; i++) {
840 if (asmc_wait(dev, 0x05))
842 buf[i] = ASMC_DATAPORT_READ(sc);
848 if (++try < 10) goto begin;
849 device_printf(dev,"%s for key %s failed %d times, giving up\n",
853 mtx_unlock_spin(&sc->sc_mtx);
860 asmc_key_dump(device_t dev, int number)
862 struct asmc_softc *sc = device_get_softc(dev);
864 char type[7] = { 0 };
868 int i, error = 1, try = 0;
870 mtx_lock_spin(&sc->sc_mtx);
872 index[0] = (number >> 24) & 0xff;
873 index[1] = (number >> 16) & 0xff;
874 index[2] = (number >> 8) & 0xff;
875 index[3] = (number) & 0xff;
878 if (asmc_command(dev, 0x12))
881 for (i = 0; i < 4; i++) {
882 ASMC_DATAPORT_WRITE(sc, index[i]);
883 if (asmc_wait(dev, 0x04))
887 ASMC_DATAPORT_WRITE(sc, 4);
889 for (i = 0; i < 4; i++) {
890 if (asmc_wait(dev, 0x05))
892 key[i] = ASMC_DATAPORT_READ(sc);
896 if (asmc_command(dev, 0x13))
899 for (i = 0; i < 4; i++) {
900 ASMC_DATAPORT_WRITE(sc, key[i]);
901 if (asmc_wait(dev, 0x04))
905 ASMC_DATAPORT_WRITE(sc, 6);
907 for (i = 0; i < 6; i++) {
908 if (asmc_wait(dev, 0x05))
910 type[i] = ASMC_DATAPORT_READ(sc);
916 if (++try < 10) goto begin;
917 device_printf(dev,"%s for key %s failed %d times, giving up\n",
919 mtx_unlock_spin(&sc->sc_mtx);
924 mtx_unlock_spin(&sc->sc_mtx);
928 if (maxlen > sizeof(v)) {
930 "WARNING: cropping maxlen from %d to %zu\n",
934 for (i = 0; i < sizeof(v); i++) {
937 asmc_key_read(dev, key, v, maxlen);
938 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
939 "(len %d), data", number, key, type, maxlen);
940 for (i = 0; i < maxlen; i++) {
941 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
942 strlcat(buf, buf2, sizeof(buf));
944 strlcat(buf, " \n", sizeof(buf));
945 device_printf(dev, "%s", buf);
953 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
955 int i, error = -1, try = 0;
956 struct asmc_softc *sc = device_get_softc(dev);
958 mtx_lock_spin(&sc->sc_mtx);
961 ASMC_DPRINTF(("cmd port: cmd write\n"));
962 if (asmc_command(dev, ASMC_CMDWRITE))
965 ASMC_DPRINTF(("data port: key\n"));
966 for (i = 0; i < 4; i++) {
967 ASMC_DATAPORT_WRITE(sc, key[i]);
968 if (asmc_wait(dev, 0x04))
971 ASMC_DPRINTF(("data port: length\n"));
972 ASMC_DATAPORT_WRITE(sc, len);
974 ASMC_DPRINTF(("data port: buffer\n"));
975 for (i = 0; i < len; i++) {
976 if (asmc_wait(dev, 0x04))
978 ASMC_DATAPORT_WRITE(sc, buf[i]);
984 if (++try < 10) goto begin;
985 device_printf(dev,"%s for key %s failed %d times, giving up\n",
989 mtx_unlock_spin(&sc->sc_mtx);
996 * Fan control functions.
999 asmc_fan_count(device_t dev)
1003 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1010 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1016 snprintf(fankey, sizeof(fankey), key, fan);
1017 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1019 speed = (buf[0] << 6) | (buf[1] >> 2);
1025 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1030 snprintf(fankey, sizeof(fankey), key, fan);
1031 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1039 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1049 snprintf(fankey, sizeof(fankey), key, fan);
1050 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1057 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1059 device_t dev = (device_t) arg1;
1064 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1065 error = sysctl_handle_int(oidp, &v, 0, req);
1071 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1074 device_t dev = (device_t) arg1;
1079 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1082 error = sysctl_handle_string(oidp, desc, 0, req);
1088 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1090 device_t dev = (device_t) arg1;
1095 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1096 error = sysctl_handle_int(oidp, &v, 0, req);
1103 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1105 device_t dev = (device_t) arg1;
1110 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1111 error = sysctl_handle_int(oidp, &v, 0, req);
1113 if (error == 0 && req->newptr != NULL) {
1114 unsigned int newspeed = v;
1115 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1122 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1124 device_t dev = (device_t) arg1;
1129 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1130 error = sysctl_handle_int(oidp, &v, 0, req);
1132 if (error == 0 && req->newptr != NULL) {
1133 unsigned int newspeed = v;
1134 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1141 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1143 device_t dev = (device_t) arg1;
1148 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1149 error = sysctl_handle_int(oidp, &v, 0, req);
1151 if (error == 0 && req->newptr != NULL) {
1152 unsigned int newspeed = v;
1153 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1160 * Temperature functions.
1163 asmc_temp_getvalue(device_t dev, const char *key)
1168 * Check for invalid temperatures.
1170 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1177 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1179 device_t dev = (device_t) arg1;
1180 struct asmc_softc *sc = device_get_softc(dev);
1183 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1184 error = sysctl_handle_int(oidp, &val, 0, req);
1190 * Sudden Motion Sensor functions.
1193 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1198 /* no need to do locking here as asmc_key_read() already does it */
1203 error = asmc_key_read(dev, key, buf, sizeof buf);
1206 device_printf(dev, "%s called with invalid argument %s\n",
1211 *val = ((int16_t)buf[0] << 8) | buf[1];
1217 asmc_sms_calibrate(device_t dev)
1219 struct asmc_softc *sc = device_get_softc(dev);
1221 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1222 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1223 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1227 asmc_sms_intrfast(void *arg)
1230 device_t dev = (device_t) arg;
1231 struct asmc_softc *sc = device_get_softc(dev);
1232 if (!sc->sc_sms_intr_works)
1233 return (FILTER_HANDLED);
1235 mtx_lock_spin(&sc->sc_mtx);
1236 type = ASMC_INTPORT_READ(sc);
1237 mtx_unlock_spin(&sc->sc_mtx);
1239 sc->sc_sms_intrtype = type;
1240 asmc_sms_printintr(dev, type);
1243 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1245 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1247 return (FILTER_HANDLED);
1252 asmc_sms_handler(void *arg)
1254 struct asmc_softc *sc = device_get_softc(arg);
1256 asmc_sms_task(sc, 0);
1262 asmc_sms_printintr(device_t dev, uint8_t type)
1266 case ASMC_SMS_INTFF:
1267 device_printf(dev, "WARNING: possible free fall!\n");
1269 case ASMC_SMS_INTHA:
1270 device_printf(dev, "WARNING: high acceleration detected!\n");
1272 case ASMC_SMS_INTSH:
1273 device_printf(dev, "WARNING: possible shock!\n");
1276 device_printf(dev, "%s unknown interrupt\n", __func__);
1281 asmc_sms_task(void *arg, int pending)
1283 struct asmc_softc *sc = (struct asmc_softc *)arg;
1287 switch (sc->sc_sms_intrtype) {
1288 case ASMC_SMS_INTFF:
1291 case ASMC_SMS_INTHA:
1294 case ASMC_SMS_INTSH:
1301 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1302 devctl_notify("ACPI", "asmc", "SMS", notify);
1306 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1308 device_t dev = (device_t) arg1;
1313 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1315 error = sysctl_handle_int(oidp, &v, 0, req);
1321 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1323 device_t dev = (device_t) arg1;
1328 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1330 error = sysctl_handle_int(oidp, &v, 0, req);
1336 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1338 device_t dev = (device_t) arg1;
1343 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1345 error = sysctl_handle_int(oidp, &v, 0, req);
1351 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1353 device_t dev = (device_t) arg1;
1358 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1360 error = sysctl_handle_int(oidp, &v, 0, req);
1366 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1368 device_t dev = (device_t) arg1;
1373 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1375 error = sysctl_handle_int(oidp, &v, 0, req);
1381 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1383 device_t dev = (device_t) arg1;
1389 error = sysctl_handle_int(oidp, &v, 0, req);
1391 if (error == 0 && req->newptr != NULL) {
1392 if (v < 0 || v > 255)
1395 buf[0] = light_control;
1397 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);