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,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
232 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
233 ASMC_MBP11_TEMPS, ASMC_MBP11_TEMPNAMES, ASMC_MBP11_TEMPDESCS
236 /* The Mac Mini has no SMS */
238 "Macmini1,1", "Apple SMC Mac Mini",
242 ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
245 /* The Mac Mini 3,1 has no SMS */
247 "Macmini3,1", "Apple SMC Mac Mini 3,1",
251 ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
254 /* Idem for the MacPro */
256 "MacPro2", "Apple SMC Mac Pro (8-core)",
260 ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
263 /* Idem for the MacPro 2010*/
265 "MacPro5,1", "Apple SMC MacPro (2010)",
269 ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
273 "MacBookAir1,1", "Apple SMC MacBook Air",
274 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
275 ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
279 "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
280 ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
281 ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
285 "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
286 ASMC_SMS_FUNCS_DISABLED,
289 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
293 "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
294 ASMC_SMS_FUNCS_DISABLED,
297 ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
304 #undef ASMC_SMS_FUNCS
305 #undef ASMC_SMS_FUNCS_DISABLED
306 #undef ASMC_FAN_FUNCS
307 #undef ASMC_FAN_FUNCS2
308 #undef ASMC_LIGHT_FUNCS
313 static device_method_t asmc_methods[] = {
314 DEVMETHOD(device_probe, asmc_probe),
315 DEVMETHOD(device_attach, asmc_attach),
316 DEVMETHOD(device_detach, asmc_detach),
317 DEVMETHOD(device_resume, asmc_resume),
322 static driver_t asmc_driver = {
325 sizeof(struct asmc_softc)
331 #define _COMPONENT ACPI_OEM
332 ACPI_MODULE_NAME("ASMC")
334 #define ASMC_DPRINTF(str) device_printf(dev, str)
336 #define ASMC_DPRINTF(str)
339 /* NB: can't be const */
340 static char *asmc_ids[] = { "APP0001", NULL };
342 static devclass_t asmc_devclass;
344 static unsigned int light_control = 0;
346 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
347 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
349 static struct asmc_model *
350 asmc_match(device_t dev)
355 model = kern_getenv("smbios.system.product");
359 for (i = 0; asmc_models[i].smc_model; i++) {
360 if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
362 return (&asmc_models[i]);
371 asmc_probe(device_t dev)
373 struct asmc_model *model;
375 if (resource_disabled("asmc", 0))
377 if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
380 model = asmc_match(dev);
382 device_printf(dev, "model not recognized\n");
385 device_set_desc(dev, model->smc_desc);
387 return (BUS_PROBE_DEFAULT);
391 asmc_attach(device_t dev)
396 struct asmc_softc *sc = device_get_softc(dev);
397 struct sysctl_ctx_list *sysctlctx;
398 struct sysctl_oid *sysctlnode;
399 struct asmc_model *model;
401 sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
402 &sc->sc_rid_port, RF_ACTIVE);
403 if (sc->sc_ioport == NULL) {
404 device_printf(dev, "unable to allocate IO port\n");
408 sysctlctx = device_get_sysctl_ctx(dev);
409 sysctlnode = device_get_sysctl_tree(dev);
411 model = asmc_match(dev);
413 mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
415 sc->sc_model = model;
419 * dev.asmc.n.fan.* tree.
421 sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
422 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
423 CTLFLAG_RD, 0, "Fan Root Tree");
425 for (i = 1; i <= sc->sc_nfan; i++) {
429 sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
430 SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
431 OID_AUTO, name, CTLFLAG_RD, 0,
434 SYSCTL_ADD_PROC(sysctlctx,
435 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
436 OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
437 dev, j, model->smc_fan_id, "I",
440 SYSCTL_ADD_PROC(sysctlctx,
441 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
442 OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
443 dev, j, model->smc_fan_speed, "I",
446 SYSCTL_ADD_PROC(sysctlctx,
447 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
448 OID_AUTO, "safespeed",
449 CTLTYPE_INT | CTLFLAG_RD,
450 dev, j, model->smc_fan_safespeed, "I",
451 "Fan safe speed in RPM");
453 SYSCTL_ADD_PROC(sysctlctx,
454 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
455 OID_AUTO, "minspeed",
456 CTLTYPE_INT | CTLFLAG_RW,
457 dev, j, model->smc_fan_minspeed, "I",
458 "Fan minimum speed in RPM");
460 SYSCTL_ADD_PROC(sysctlctx,
461 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
462 OID_AUTO, "maxspeed",
463 CTLTYPE_INT | CTLFLAG_RW,
464 dev, j, model->smc_fan_maxspeed, "I",
465 "Fan maximum speed in RPM");
467 SYSCTL_ADD_PROC(sysctlctx,
468 SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
469 OID_AUTO, "targetspeed",
470 CTLTYPE_INT | CTLFLAG_RW,
471 dev, j, model->smc_fan_targetspeed, "I",
472 "Fan target speed in RPM");
476 * dev.asmc.n.temp tree.
478 sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
479 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
480 CTLFLAG_RD, 0, "Temperature sensors");
482 for (i = 0; model->smc_temps[i]; i++) {
483 SYSCTL_ADD_PROC(sysctlctx,
484 SYSCTL_CHILDREN(sc->sc_temp_tree),
485 OID_AUTO, model->smc_tempnames[i],
486 CTLTYPE_INT | CTLFLAG_RD,
487 dev, i, asmc_temp_sysctl, "I",
488 model->smc_tempdescs[i]);
494 if (model->smc_light_left) {
495 sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
496 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
497 CTLFLAG_RD, 0, "Keyboard backlight sensors");
499 SYSCTL_ADD_PROC(sysctlctx,
500 SYSCTL_CHILDREN(sc->sc_light_tree),
501 OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
502 dev, 0, model->smc_light_left, "I",
503 "Keyboard backlight left sensor");
505 SYSCTL_ADD_PROC(sysctlctx,
506 SYSCTL_CHILDREN(sc->sc_light_tree),
507 OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
508 dev, 0, model->smc_light_right, "I",
509 "Keyboard backlight right sensor");
511 SYSCTL_ADD_PROC(sysctlctx,
512 SYSCTL_CHILDREN(sc->sc_light_tree),
514 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
515 dev, 0, model->smc_light_control, "I",
516 "Keyboard backlight brightness control");
519 if (model->smc_sms_x == NULL)
523 * dev.asmc.n.sms tree.
525 sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
526 SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
527 CTLFLAG_RD, 0, "Sudden Motion Sensor");
529 SYSCTL_ADD_PROC(sysctlctx,
530 SYSCTL_CHILDREN(sc->sc_sms_tree),
531 OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
532 dev, 0, model->smc_sms_x, "I",
533 "Sudden Motion Sensor X value");
535 SYSCTL_ADD_PROC(sysctlctx,
536 SYSCTL_CHILDREN(sc->sc_sms_tree),
537 OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
538 dev, 0, model->smc_sms_y, "I",
539 "Sudden Motion Sensor Y value");
541 SYSCTL_ADD_PROC(sysctlctx,
542 SYSCTL_CHILDREN(sc->sc_sms_tree),
543 OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
544 dev, 0, model->smc_sms_z, "I",
545 "Sudden Motion Sensor Z value");
548 * Need a taskqueue to send devctl_notify() events
549 * when the SMS interrupt us.
551 * PI_REALTIME is used due to the sensitivity of the
552 * interrupt. An interrupt from the SMS means that the
553 * disk heads should be turned off as quickly as possible.
555 * We only need to do this for the non INTR_FILTER case.
557 sc->sc_sms_tq = NULL;
559 TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
560 sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
561 taskqueue_thread_enqueue, &sc->sc_sms_tq);
562 taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
563 device_get_nameunit(dev));
566 * Allocate an IRQ for the SMS.
569 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
570 &sc->sc_rid_irq, RF_ACTIVE);
571 if (sc->sc_irq == NULL) {
572 device_printf(dev, "unable to allocate IRQ resource\n");
577 ret = bus_setup_intr(dev, sc->sc_irq,
578 INTR_TYPE_MISC | INTR_MPSAFE,
580 asmc_sms_intrfast, asmc_sms_handler,
582 asmc_sms_intrfast, NULL,
584 dev, &sc->sc_cookie);
587 device_printf(dev, "unable to setup SMS IRQ\n");
593 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
595 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
597 mtx_destroy(&sc->sc_mtx);
599 taskqueue_free(sc->sc_sms_tq);
605 asmc_detach(device_t dev)
607 struct asmc_softc *sc = device_get_softc(dev);
610 taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
611 taskqueue_free(sc->sc_sms_tq);
614 bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
616 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
619 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
621 mtx_destroy(&sc->sc_mtx);
627 asmc_resume(device_t dev)
630 buf[0] = light_control;
632 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
638 void asmc_dumpall(device_t dev)
642 /* XXX magic number */
643 for (i=0; i < 0x100; i++)
644 asmc_key_dump(dev, i);
649 asmc_init(device_t dev)
651 struct asmc_softc *sc = device_get_softc(dev);
655 if (sc->sc_model->smc_sms_x == NULL)
659 * We are ready to receive interrupts from the SMS.
662 ASMC_DPRINTF(("intok key\n"));
663 asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
667 * Initiate the polling intervals.
669 buf[0] = 20; /* msecs */
670 ASMC_DPRINTF(("low int key\n"));
671 asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
674 buf[0] = 20; /* msecs */
675 ASMC_DPRINTF(("high int key\n"));
676 asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
681 ASMC_DPRINTF(("sms low key\n"));
682 asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
687 ASMC_DPRINTF(("sms high key\n"));
688 asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
692 * I'm not sure what this key does, but it seems to be
696 ASMC_DPRINTF(("sms flag key\n"));
697 asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
700 sc->sc_sms_intr_works = 0;
703 * Retry SMS initialization 1000 times
704 * (takes approx. 2 seconds in worst case)
706 for (i = 0; i < 1000; i++) {
707 if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
708 (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
710 sc->sc_sms_intr_works = 1;
713 buf[0] = ASMC_SMS_INIT1;
714 buf[1] = ASMC_SMS_INIT2;
715 ASMC_DPRINTF(("sms key\n"));
716 asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
719 device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
722 asmc_sms_calibrate(dev);
724 sc->sc_nfan = asmc_fan_count(dev);
725 if (sc->sc_nfan > ASMC_MAXFANS) {
726 device_printf(dev, "more than %d fans were detected. Please "
727 "report this.\n", ASMC_MAXFANS);
728 sc->sc_nfan = ASMC_MAXFANS;
733 * The number of keys is a 32 bit buffer
735 asmc_key_read(dev, ASMC_NKEYS, buf, 4);
736 device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
747 * We need to make sure that the SMC acks the byte sent.
748 * Just wait up to (amount * 10) ms.
751 asmc_wait_ack(device_t dev, uint8_t val, int amount)
753 struct asmc_softc *sc = device_get_softc(dev);
756 val = val & ASMC_STATUS_MASK;
758 for (i = 0; i < amount; i++) {
759 if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
768 * We need to make sure that the SMC acks the byte sent.
769 * Just wait up to 100 ms.
772 asmc_wait(device_t dev, uint8_t val)
774 struct asmc_softc *sc;
776 if (asmc_wait_ack(dev, val, 1000) == 0)
779 sc = device_get_softc(dev);
780 val = val & ASMC_STATUS_MASK;
783 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
784 ASMC_CMDPORT_READ(sc));
790 * Send the given command, retrying up to 10 times if
791 * the acknowledgement fails.
794 asmc_command(device_t dev, uint8_t command) {
797 struct asmc_softc *sc = device_get_softc(dev);
799 for (i=0; i < 10; i++) {
800 ASMC_CMDPORT_WRITE(sc, command);
801 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
807 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
808 ASMC_CMDPORT_READ(sc));
814 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
816 int i, error = 1, try = 0;
817 struct asmc_softc *sc = device_get_softc(dev);
819 mtx_lock_spin(&sc->sc_mtx);
822 if (asmc_command(dev, ASMC_CMDREAD))
825 for (i = 0; i < 4; i++) {
826 ASMC_DATAPORT_WRITE(sc, key[i]);
827 if (asmc_wait(dev, 0x04))
831 ASMC_DATAPORT_WRITE(sc, len);
833 for (i = 0; i < len; i++) {
834 if (asmc_wait(dev, 0x05))
836 buf[i] = ASMC_DATAPORT_READ(sc);
842 if (++try < 10) goto begin;
843 device_printf(dev,"%s for key %s failed %d times, giving up\n",
847 mtx_unlock_spin(&sc->sc_mtx);
854 asmc_key_dump(device_t dev, int number)
856 struct asmc_softc *sc = device_get_softc(dev);
858 char type[7] = { 0 };
862 int i, error = 1, try = 0;
864 mtx_lock_spin(&sc->sc_mtx);
866 index[0] = (number >> 24) & 0xff;
867 index[1] = (number >> 16) & 0xff;
868 index[2] = (number >> 8) & 0xff;
869 index[3] = (number) & 0xff;
872 if (asmc_command(dev, 0x12))
875 for (i = 0; i < 4; i++) {
876 ASMC_DATAPORT_WRITE(sc, index[i]);
877 if (asmc_wait(dev, 0x04))
881 ASMC_DATAPORT_WRITE(sc, 4);
883 for (i = 0; i < 4; i++) {
884 if (asmc_wait(dev, 0x05))
886 key[i] = ASMC_DATAPORT_READ(sc);
890 if (asmc_command(dev, 0x13))
893 for (i = 0; i < 4; i++) {
894 ASMC_DATAPORT_WRITE(sc, key[i]);
895 if (asmc_wait(dev, 0x04))
899 ASMC_DATAPORT_WRITE(sc, 6);
901 for (i = 0; i < 6; i++) {
902 if (asmc_wait(dev, 0x05))
904 type[i] = ASMC_DATAPORT_READ(sc);
910 if (++try < 10) goto begin;
911 device_printf(dev,"%s for key %s failed %d times, giving up\n",
913 mtx_unlock_spin(&sc->sc_mtx);
918 mtx_unlock_spin(&sc->sc_mtx);
922 if (maxlen > sizeof(v)) {
924 "WARNING: cropping maxlen from %d to %zu\n",
928 for (i = 0; i < sizeof(v); i++) {
931 asmc_key_read(dev, key, v, maxlen);
932 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
933 "(len %d), data", number, key, type, maxlen);
934 for (i = 0; i < maxlen; i++) {
935 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
936 strlcat(buf, buf2, sizeof(buf));
938 strlcat(buf, " \n", sizeof(buf));
939 device_printf(dev, "%s", buf);
947 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
949 int i, error = -1, try = 0;
950 struct asmc_softc *sc = device_get_softc(dev);
952 mtx_lock_spin(&sc->sc_mtx);
955 ASMC_DPRINTF(("cmd port: cmd write\n"));
956 if (asmc_command(dev, ASMC_CMDWRITE))
959 ASMC_DPRINTF(("data port: key\n"));
960 for (i = 0; i < 4; i++) {
961 ASMC_DATAPORT_WRITE(sc, key[i]);
962 if (asmc_wait(dev, 0x04))
965 ASMC_DPRINTF(("data port: length\n"));
966 ASMC_DATAPORT_WRITE(sc, len);
968 ASMC_DPRINTF(("data port: buffer\n"));
969 for (i = 0; i < len; i++) {
970 if (asmc_wait(dev, 0x04))
972 ASMC_DATAPORT_WRITE(sc, buf[i]);
978 if (++try < 10) goto begin;
979 device_printf(dev,"%s for key %s failed %d times, giving up\n",
983 mtx_unlock_spin(&sc->sc_mtx);
990 * Fan control functions.
993 asmc_fan_count(device_t dev)
997 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1004 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1010 snprintf(fankey, sizeof(fankey), key, fan);
1011 if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1013 speed = (buf[0] << 6) | (buf[1] >> 2);
1019 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1024 snprintf(fankey, sizeof(fankey), key, fan);
1025 if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1033 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1043 snprintf(fankey, sizeof(fankey), key, fan);
1044 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1051 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1053 device_t dev = (device_t) arg1;
1058 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1059 error = sysctl_handle_int(oidp, &v, 0, req);
1065 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1068 device_t dev = (device_t) arg1;
1073 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1076 error = sysctl_handle_string(oidp, desc, 0, req);
1082 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1084 device_t dev = (device_t) arg1;
1089 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1090 error = sysctl_handle_int(oidp, &v, 0, req);
1097 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1099 device_t dev = (device_t) arg1;
1104 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1105 error = sysctl_handle_int(oidp, &v, 0, req);
1107 if (error == 0 && req->newptr != NULL) {
1108 unsigned int newspeed = v;
1109 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1116 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1118 device_t dev = (device_t) arg1;
1123 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1124 error = sysctl_handle_int(oidp, &v, 0, req);
1126 if (error == 0 && req->newptr != NULL) {
1127 unsigned int newspeed = v;
1128 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1135 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1137 device_t dev = (device_t) arg1;
1142 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1143 error = sysctl_handle_int(oidp, &v, 0, req);
1145 if (error == 0 && req->newptr != NULL) {
1146 unsigned int newspeed = v;
1147 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1154 * Temperature functions.
1157 asmc_temp_getvalue(device_t dev, const char *key)
1162 * Check for invalid temperatures.
1164 if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1171 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1173 device_t dev = (device_t) arg1;
1174 struct asmc_softc *sc = device_get_softc(dev);
1177 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1178 error = sysctl_handle_int(oidp, &val, 0, req);
1184 * Sudden Motion Sensor functions.
1187 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1192 /* no need to do locking here as asmc_key_read() already does it */
1197 error = asmc_key_read(dev, key, buf, sizeof buf);
1200 device_printf(dev, "%s called with invalid argument %s\n",
1205 *val = ((int16_t)buf[0] << 8) | buf[1];
1211 asmc_sms_calibrate(device_t dev)
1213 struct asmc_softc *sc = device_get_softc(dev);
1215 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1216 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1217 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1221 asmc_sms_intrfast(void *arg)
1224 device_t dev = (device_t) arg;
1225 struct asmc_softc *sc = device_get_softc(dev);
1226 if (!sc->sc_sms_intr_works)
1227 return (FILTER_HANDLED);
1229 mtx_lock_spin(&sc->sc_mtx);
1230 type = ASMC_INTPORT_READ(sc);
1231 mtx_unlock_spin(&sc->sc_mtx);
1233 sc->sc_sms_intrtype = type;
1234 asmc_sms_printintr(dev, type);
1237 return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
1239 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1241 return (FILTER_HANDLED);
1246 asmc_sms_handler(void *arg)
1248 struct asmc_softc *sc = device_get_softc(arg);
1250 asmc_sms_task(sc, 0);
1256 asmc_sms_printintr(device_t dev, uint8_t type)
1260 case ASMC_SMS_INTFF:
1261 device_printf(dev, "WARNING: possible free fall!\n");
1263 case ASMC_SMS_INTHA:
1264 device_printf(dev, "WARNING: high acceleration detected!\n");
1266 case ASMC_SMS_INTSH:
1267 device_printf(dev, "WARNING: possible shock!\n");
1270 device_printf(dev, "%s unknown interrupt\n", __func__);
1275 asmc_sms_task(void *arg, int pending)
1277 struct asmc_softc *sc = (struct asmc_softc *)arg;
1281 switch (sc->sc_sms_intrtype) {
1282 case ASMC_SMS_INTFF:
1285 case ASMC_SMS_INTHA:
1288 case ASMC_SMS_INTSH:
1295 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1296 devctl_notify("ACPI", "asmc", "SMS", notify);
1300 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1302 device_t dev = (device_t) arg1;
1307 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1309 error = sysctl_handle_int(oidp, &v, 0, req);
1315 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1317 device_t dev = (device_t) arg1;
1322 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1324 error = sysctl_handle_int(oidp, &v, 0, req);
1330 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1332 device_t dev = (device_t) arg1;
1337 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1339 error = sysctl_handle_int(oidp, &v, 0, req);
1345 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1347 device_t dev = (device_t) arg1;
1352 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1354 error = sysctl_handle_int(oidp, &v, 0, req);
1360 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1362 device_t dev = (device_t) arg1;
1367 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1369 error = sysctl_handle_int(oidp, &v, 0, req);
1375 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1377 device_t dev = (device_t) arg1;
1383 error = sysctl_handle_int(oidp, &v, 0, req);
1385 if (error == 0 && req->newptr != NULL) {
1386 if (v < 0 || v > 255)
1389 buf[0] = light_control;
1391 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);