xref: /freebsd/sys/dev/asmc/asmc.c (revision 32a8088feff95f2a7b35ff5ac78887cdb963c3e8)
132a8088fSRui Paulo /*-
232a8088fSRui Paulo  * Copyright (c) 2007 Rui Paulo <rpaulo@FreeBSD.org>
332a8088fSRui Paulo  * All rights reserved.
432a8088fSRui Paulo  *
532a8088fSRui Paulo  * Redistribution and use in source and binary forms, with or without
632a8088fSRui Paulo  * modification, are permitted provided that the following conditions
732a8088fSRui Paulo  * are met:
832a8088fSRui Paulo  * 1. Redistributions of source code must retain the above copyright
932a8088fSRui Paulo  *    notice, this list of conditions and the following disclaimer.
1032a8088fSRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1132a8088fSRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1232a8088fSRui Paulo  *    documentation and/or other materials provided with the distribution.
1332a8088fSRui Paulo  *
1432a8088fSRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532a8088fSRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1632a8088fSRui Paulo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1732a8088fSRui Paulo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
1832a8088fSRui Paulo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1932a8088fSRui Paulo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2032a8088fSRui Paulo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2132a8088fSRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2232a8088fSRui Paulo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2332a8088fSRui Paulo  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2432a8088fSRui Paulo  * POSSIBILITY OF SUCH DAMAGE.
2532a8088fSRui Paulo  *
2632a8088fSRui Paulo  */
2732a8088fSRui Paulo 
2832a8088fSRui Paulo /*
2932a8088fSRui Paulo  * Driver for Apple's System Management Console (SMC).
3032a8088fSRui Paulo  * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
3132a8088fSRui Paulo  *
3232a8088fSRui Paulo  * Inspired by the Linux applesmc driver.
3332a8088fSRui Paulo  */
3432a8088fSRui Paulo 
3532a8088fSRui Paulo #include <sys/cdefs.h>
3632a8088fSRui Paulo __FBSDID("$FreeBSD$");
3732a8088fSRui Paulo 
3832a8088fSRui Paulo #include <sys/param.h>
3932a8088fSRui Paulo #include <sys/bus.h>
4032a8088fSRui Paulo #include <sys/conf.h>
4132a8088fSRui Paulo #include <sys/kernel.h>
4232a8088fSRui Paulo #include <sys/lock.h>
4332a8088fSRui Paulo #include <sys/malloc.h>
4432a8088fSRui Paulo #include <sys/module.h>
4532a8088fSRui Paulo #include <sys/mutex.h>
4632a8088fSRui Paulo #include <sys/sysctl.h>
4732a8088fSRui Paulo #include <sys/systm.h>
4832a8088fSRui Paulo #include <sys/taskqueue.h>
4932a8088fSRui Paulo #include <isa/isavar.h>
5032a8088fSRui Paulo #include <machine/bus.h>
5132a8088fSRui Paulo #include <sys/rman.h>
5232a8088fSRui Paulo #include <machine/resource.h>
5332a8088fSRui Paulo 
5432a8088fSRui Paulo #include <dev/asmc/asmcvar.h>
5532a8088fSRui Paulo 
5632a8088fSRui Paulo /*
5732a8088fSRui Paulo  * Device interface.
5832a8088fSRui Paulo  */
5932a8088fSRui Paulo static void 	asmc_identify(driver_t *driver, device_t parent);
6032a8088fSRui Paulo static int 	asmc_probe(device_t dev);
6132a8088fSRui Paulo static int 	asmc_attach(device_t dev);
6232a8088fSRui Paulo static int 	asmc_detach(device_t dev);
6332a8088fSRui Paulo 
6432a8088fSRui Paulo /*
6532a8088fSRui Paulo  * SMC functions.
6632a8088fSRui Paulo  */
6732a8088fSRui Paulo static int 	asmc_init(device_t dev);
6832a8088fSRui Paulo static int 	asmc_wait(device_t dev, uint8_t val);
6932a8088fSRui Paulo static int 	asmc_key_write(device_t dev, const char *key, uint8_t *buf,
7032a8088fSRui Paulo     uint8_t len);
7132a8088fSRui Paulo static int 	asmc_key_read(device_t dev, const char *key, uint8_t *buf,
7232a8088fSRui Paulo     uint8_t);
7332a8088fSRui Paulo static int 	asmc_fan_count(device_t dev);
7432a8088fSRui Paulo static int 	asmc_fan_getvalue(device_t dev, const char *key, int fan);
7532a8088fSRui Paulo static int 	asmc_temp_getvalue(device_t dev, const char *key);
7632a8088fSRui Paulo static int 	asmc_sms_read(device_t, const char *key, int16_t *val);
7732a8088fSRui Paulo static void 	asmc_sms_calibrate(device_t dev);
7832a8088fSRui Paulo static int 	asmc_sms_intrfast(void *arg);
7932a8088fSRui Paulo #ifdef INTR_FILTER
8032a8088fSRui Paulo static void 	asmc_sms_handler(void *arg);
8132a8088fSRui Paulo #endif
8232a8088fSRui Paulo static void 	asmc_sms_printintr(device_t dev, uint8_t);
8332a8088fSRui Paulo static void 	asmc_sms_task(void *arg, int pending);
8432a8088fSRui Paulo 
8532a8088fSRui Paulo /*
8632a8088fSRui Paulo  * Model functions.
8732a8088fSRui Paulo  */
8832a8088fSRui Paulo static int 	asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
8932a8088fSRui Paulo static int 	asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
9032a8088fSRui Paulo static int 	asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
9132a8088fSRui Paulo static int 	asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
9232a8088fSRui Paulo static int 	asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
9332a8088fSRui Paulo static int 	asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
9432a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
9532a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
9632a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
9732a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
9832a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
9932a8088fSRui Paulo 
10032a8088fSRui Paulo struct asmc_model {
10132a8088fSRui Paulo 	const char 	 *smc_model;	/* smbios.system.product env var. */
10232a8088fSRui Paulo 	const char 	 *smc_desc;	/* driver description */
10332a8088fSRui Paulo 
10432a8088fSRui Paulo 	/* Helper functions */
10532a8088fSRui Paulo 	int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
10632a8088fSRui Paulo 	int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
10732a8088fSRui Paulo 	int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
10832a8088fSRui Paulo 	int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
10932a8088fSRui Paulo 	int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
11032a8088fSRui Paulo 	int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
11132a8088fSRui Paulo 	int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
11232a8088fSRui Paulo 	int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
11332a8088fSRui Paulo 	int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
11432a8088fSRui Paulo 	int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
11532a8088fSRui Paulo 
11632a8088fSRui Paulo 	const char 	*smc_temps[8];
11732a8088fSRui Paulo 	const char 	*smc_tempnames[8];
11832a8088fSRui Paulo 	const char 	*smc_tempdescs[8];
11932a8088fSRui Paulo };
12032a8088fSRui Paulo 
12132a8088fSRui Paulo static struct asmc_model *asmc_match(device_t dev);
12232a8088fSRui Paulo 
12332a8088fSRui Paulo #define ASMC_SMS_FUNCS	asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
12432a8088fSRui Paulo 			asmc_mb_sysctl_sms_z
12532a8088fSRui Paulo 
12632a8088fSRui Paulo #define ASMC_FAN_FUNCS	asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
12732a8088fSRui Paulo 			asmc_mb_sysctl_fanminspeed, \
12832a8088fSRui Paulo 			asmc_mb_sysctl_fanmaxspeed, \
12932a8088fSRui Paulo 			asmc_mb_sysctl_fantargetspeed
13032a8088fSRui Paulo #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
13132a8088fSRui Paulo 			 asmc_mbp_sysctl_light_right
13232a8088fSRui Paulo 
13332a8088fSRui Paulo struct asmc_model asmc_models[] = {
13432a8088fSRui Paulo 	{
13532a8088fSRui Paulo 	  "MacBook1,1", "Apple SMC MacBook Core Duo",
13632a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL,
13732a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
13832a8088fSRui Paulo 	},
13932a8088fSRui Paulo 
14032a8088fSRui Paulo 	{
14132a8088fSRui Paulo 	  "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
14232a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL,
14332a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
14432a8088fSRui Paulo 	},
14532a8088fSRui Paulo 
14632a8088fSRui Paulo 	{
14732a8088fSRui Paulo 	  "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
14832a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
14932a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
15032a8088fSRui Paulo 	},
15132a8088fSRui Paulo 
15232a8088fSRui Paulo 	{
15332a8088fSRui Paulo 	  "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
15432a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
15532a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
15632a8088fSRui Paulo 	},
15732a8088fSRui Paulo 
15832a8088fSRui Paulo 	{
15932a8088fSRui Paulo 	  "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
16032a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
16132a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
16232a8088fSRui Paulo 	},
16332a8088fSRui Paulo 
16432a8088fSRui Paulo 	{
16532a8088fSRui Paulo 	  "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
16632a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
16732a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
16832a8088fSRui Paulo 	},
16932a8088fSRui Paulo 
17032a8088fSRui Paulo 	{
17132a8088fSRui Paulo 	  "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
17232a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
17332a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
17432a8088fSRui Paulo 	},
17532a8088fSRui Paulo 
17632a8088fSRui Paulo 	{
17732a8088fSRui Paulo 	  "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
17832a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
17932a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
18032a8088fSRui Paulo 	},
18132a8088fSRui Paulo 
18232a8088fSRui Paulo 	/* The Mac Mini has no SMS */
18332a8088fSRui Paulo 	{
18432a8088fSRui Paulo 	  "Macmini1,1", "Apple SMC Mac Mini",
18532a8088fSRui Paulo 	  NULL, NULL, NULL,
18632a8088fSRui Paulo 	  NULL, NULL,
18732a8088fSRui Paulo 	  ASMC_FAN_FUNCS,
18832a8088fSRui Paulo 	  ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
18932a8088fSRui Paulo 	},
19032a8088fSRui Paulo 
19132a8088fSRui Paulo 	{ NULL, NULL }
19232a8088fSRui Paulo };
19332a8088fSRui Paulo 
19432a8088fSRui Paulo #undef ASMC_SMS_FUNCS
19532a8088fSRui Paulo #undef ASMC_FAN_FUNCS
19632a8088fSRui Paulo #undef ASMC_LIGHT_FUNCS
19732a8088fSRui Paulo 
19832a8088fSRui Paulo /*
19932a8088fSRui Paulo  * Driver methods.
20032a8088fSRui Paulo  */
20132a8088fSRui Paulo static device_method_t	asmc_methods[] = {
20232a8088fSRui Paulo 	DEVMETHOD(device_identify,	asmc_identify),
20332a8088fSRui Paulo 	DEVMETHOD(device_probe,		asmc_probe),
20432a8088fSRui Paulo 	DEVMETHOD(device_attach,	asmc_attach),
20532a8088fSRui Paulo 	DEVMETHOD(device_detach,	asmc_detach),
20632a8088fSRui Paulo 
20732a8088fSRui Paulo 	{ 0, 0 }
20832a8088fSRui Paulo };
20932a8088fSRui Paulo 
21032a8088fSRui Paulo static driver_t	asmc_driver = {
21132a8088fSRui Paulo 	"asmc",
21232a8088fSRui Paulo 	asmc_methods,
21332a8088fSRui Paulo 	sizeof(struct asmc_softc)
21432a8088fSRui Paulo };
21532a8088fSRui Paulo 
21632a8088fSRui Paulo static devclass_t asmc_devclass;
21732a8088fSRui Paulo 
21832a8088fSRui Paulo DRIVER_MODULE(asmc, isa, asmc_driver, asmc_devclass, NULL, NULL);
21932a8088fSRui Paulo 
22032a8088fSRui Paulo static void
22132a8088fSRui Paulo asmc_identify(driver_t *driver, device_t parent)
22232a8088fSRui Paulo {
22332a8088fSRui Paulo 	if (device_find_child(parent, "asmc", -1) == NULL &&
22432a8088fSRui Paulo 	    asmc_match(parent))
22532a8088fSRui Paulo 		BUS_ADD_CHILD(parent, 0, "asmc", -1);
22632a8088fSRui Paulo }
22732a8088fSRui Paulo 
22832a8088fSRui Paulo static struct asmc_model *
22932a8088fSRui Paulo asmc_match(device_t dev)
23032a8088fSRui Paulo {
23132a8088fSRui Paulo 	int i;
23232a8088fSRui Paulo 	char *model;
23332a8088fSRui Paulo 
23432a8088fSRui Paulo 	model = getenv("smbios.system.product");
23532a8088fSRui Paulo 	for (i = 0; asmc_models[i].smc_model; i++) {
23632a8088fSRui Paulo 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
23732a8088fSRui Paulo 			freeenv(model);
23832a8088fSRui Paulo 			return (&asmc_models[i]);
23932a8088fSRui Paulo 		}
24032a8088fSRui Paulo 	}
24132a8088fSRui Paulo 	freeenv(model);
24232a8088fSRui Paulo 
24332a8088fSRui Paulo 	return (NULL);
24432a8088fSRui Paulo }
24532a8088fSRui Paulo 
24632a8088fSRui Paulo static int
24732a8088fSRui Paulo asmc_probe(device_t dev)
24832a8088fSRui Paulo {
24932a8088fSRui Paulo 	struct asmc_model *model;
25032a8088fSRui Paulo 
25132a8088fSRui Paulo 	if (resource_disabled("asmc", 0))
25232a8088fSRui Paulo 		return (ENXIO);
25332a8088fSRui Paulo 	model = asmc_match(dev);
25432a8088fSRui Paulo 	if (!model)
25532a8088fSRui Paulo 		return (ENXIO);
25632a8088fSRui Paulo 	if (isa_get_irq(dev) == -1)
25732a8088fSRui Paulo 		bus_set_resource(dev, SYS_RES_IRQ, 0, ASMC_IRQ, 1);
25832a8088fSRui Paulo 	device_set_desc(dev, model->smc_desc);
25932a8088fSRui Paulo 
26032a8088fSRui Paulo 	return (BUS_PROBE_DEFAULT);
26132a8088fSRui Paulo }
26232a8088fSRui Paulo 
26332a8088fSRui Paulo static int
26432a8088fSRui Paulo asmc_attach(device_t dev)
26532a8088fSRui Paulo {
26632a8088fSRui Paulo 	int i, j;
26732a8088fSRui Paulo 	int ret;
26832a8088fSRui Paulo 	char name[2];
26932a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
27032a8088fSRui Paulo 	struct sysctl_ctx_list *sysctlctx;
27132a8088fSRui Paulo 	struct sysctl_oid *sysctlnode;
27232a8088fSRui Paulo 	struct asmc_model *model;
27332a8088fSRui Paulo 
27432a8088fSRui Paulo 	sysctlctx  = device_get_sysctl_ctx(dev);
27532a8088fSRui Paulo 	sysctlnode = device_get_sysctl_tree(dev);
27632a8088fSRui Paulo 
27732a8088fSRui Paulo 	model = asmc_match(dev);
27832a8088fSRui Paulo 
27932a8088fSRui Paulo 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
28032a8088fSRui Paulo 
28132a8088fSRui Paulo 	sc->sc_model = model;
28232a8088fSRui Paulo 	asmc_init(dev);
28332a8088fSRui Paulo 
28432a8088fSRui Paulo 	/*
28532a8088fSRui Paulo 	 * dev.asmc.n.fan.* tree.
28632a8088fSRui Paulo 	 */
28732a8088fSRui Paulo 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
28832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
28932a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Fan Root Tree");
29032a8088fSRui Paulo 
29132a8088fSRui Paulo 	for (i = 1; i <= sc->sc_nfan; i++) {
29232a8088fSRui Paulo 		j = i - 1;
29332a8088fSRui Paulo 		name[0] = '0' + j;
29432a8088fSRui Paulo 		name[1] = 0;
29532a8088fSRui Paulo 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
29632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
29732a8088fSRui Paulo 		    OID_AUTO, name, CTLFLAG_RD, 0,
29832a8088fSRui Paulo 		    "Fan Subtree");
29932a8088fSRui Paulo 
30032a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
30132a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
30232a8088fSRui Paulo 		    OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
30332a8088fSRui Paulo 		    dev, j, model->smc_fan_speed, "I",
30432a8088fSRui Paulo 		    "Fan speed in RPM");
30532a8088fSRui Paulo 
30632a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
30732a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
30832a8088fSRui Paulo 		    OID_AUTO, "safespeed",
30932a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
31032a8088fSRui Paulo 		    dev, j, model->smc_fan_safespeed, "I",
31132a8088fSRui Paulo 		    "Fan safe speed in RPM");
31232a8088fSRui Paulo 
31332a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
31432a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
31532a8088fSRui Paulo 		    OID_AUTO, "minspeed",
31632a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
31732a8088fSRui Paulo 		    dev, j, model->smc_fan_minspeed, "I",
31832a8088fSRui Paulo 		    "Fan minimum speed in RPM");
31932a8088fSRui Paulo 
32032a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
32132a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
32232a8088fSRui Paulo 		    OID_AUTO, "maxspeed",
32332a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
32432a8088fSRui Paulo 		    dev, j, model->smc_fan_maxspeed, "I",
32532a8088fSRui Paulo 		    "Fan maximum speed in RPM");
32632a8088fSRui Paulo 
32732a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
32832a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
32932a8088fSRui Paulo 		    OID_AUTO, "targetspeed",
33032a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
33132a8088fSRui Paulo 		    dev, j, model->smc_fan_targetspeed, "I",
33232a8088fSRui Paulo 		    "Fan target speed in RPM");
33332a8088fSRui Paulo 	}
33432a8088fSRui Paulo 
33532a8088fSRui Paulo 	/*
33632a8088fSRui Paulo 	 * dev.asmc.n.temp tree.
33732a8088fSRui Paulo 	 */
33832a8088fSRui Paulo 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
33932a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
34032a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Temperature sensors");
34132a8088fSRui Paulo 
34232a8088fSRui Paulo 	for (i = 0; model->smc_temps[i]; i++) {
34332a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
34432a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
34532a8088fSRui Paulo 		    OID_AUTO, model->smc_tempnames[i],
34632a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
34732a8088fSRui Paulo 		    dev, i, asmc_temp_sysctl, "I",
34832a8088fSRui Paulo 		    model->smc_tempdescs[i]);
34932a8088fSRui Paulo 	}
35032a8088fSRui Paulo 
35132a8088fSRui Paulo 	if (model->smc_sms_x == NULL)
35232a8088fSRui Paulo 		goto nosms;
35332a8088fSRui Paulo 
35432a8088fSRui Paulo 	/*
35532a8088fSRui Paulo 	 * dev.asmc.n.sms tree.
35632a8088fSRui Paulo 	 */
35732a8088fSRui Paulo 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
35832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
35932a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Sudden Motion Sensor");
36032a8088fSRui Paulo 
36132a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
36232a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
36332a8088fSRui Paulo 	    OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
36432a8088fSRui Paulo 	    dev, 0, model->smc_sms_x, "I",
36532a8088fSRui Paulo 	    "Sudden Motion Sensor X value");
36632a8088fSRui Paulo 
36732a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
36832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
36932a8088fSRui Paulo 	    OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
37032a8088fSRui Paulo 	    dev, 0, model->smc_sms_y, "I",
37132a8088fSRui Paulo 	    "Sudden Motion Sensor Y value");
37232a8088fSRui Paulo 
37332a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
37432a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
37532a8088fSRui Paulo 	    OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
37632a8088fSRui Paulo 	    dev, 0, model->smc_sms_z, "I",
37732a8088fSRui Paulo 	    "Sudden Motion Sensor Z value");
37832a8088fSRui Paulo 
37932a8088fSRui Paulo 	/*
38032a8088fSRui Paulo 	 * dev.asmc.n.light
38132a8088fSRui Paulo 	 */
38232a8088fSRui Paulo 	if (model->smc_light_left) {
38332a8088fSRui Paulo 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
38432a8088fSRui Paulo 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
38532a8088fSRui Paulo 		    CTLFLAG_RD, 0, "Keyboard backlight sensors");
38632a8088fSRui Paulo 
38732a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
38832a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
38932a8088fSRui Paulo 		    OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RW,
39032a8088fSRui Paulo 		    dev, 0, model->smc_light_left, "I",
39132a8088fSRui Paulo 		    "Keyboard backlight left sensor");
39232a8088fSRui Paulo 
39332a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
39432a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
39532a8088fSRui Paulo 		    OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RW,
39632a8088fSRui Paulo 		    dev, 0, model->smc_light_right, "I",
39732a8088fSRui Paulo 		    "Keyboard backlight right sensor");
39832a8088fSRui Paulo 	}
39932a8088fSRui Paulo 
40032a8088fSRui Paulo 	/*
40132a8088fSRui Paulo 	 * Need a taskqueue to send devctl_notify() events
40232a8088fSRui Paulo 	 * when the SMS interrupt us.
40332a8088fSRui Paulo 	 *
40432a8088fSRui Paulo 	 * PI_REALTIME is used due to the sensitivity of the
40532a8088fSRui Paulo 	 * interrupt. An interrupt from the SMS means that the
40632a8088fSRui Paulo 	 * disk heads should be turned off as quickly as possible.
40732a8088fSRui Paulo 	 *
40832a8088fSRui Paulo 	 * We only need to do this for the non INTR_FILTER case.
40932a8088fSRui Paulo 	 */
41032a8088fSRui Paulo 	sc->sc_sms_tq = NULL;
41132a8088fSRui Paulo #ifndef INTR_FILTER
41232a8088fSRui Paulo 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
41332a8088fSRui Paulo 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
41432a8088fSRui Paulo 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
41532a8088fSRui Paulo 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
41632a8088fSRui Paulo 	    device_get_nameunit(dev));
41732a8088fSRui Paulo #endif
41832a8088fSRui Paulo 	/*
41932a8088fSRui Paulo 	 * Allocate an IRQ for the SMS.
42032a8088fSRui Paulo 	 */
42132a8088fSRui Paulo 	sc->sc_rid = 0;
42232a8088fSRui Paulo 	sc->sc_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_rid,
42332a8088fSRui Paulo 	    ASMC_IRQ, ASMC_IRQ, 1, RF_ACTIVE);
42432a8088fSRui Paulo 	if (sc->sc_res == NULL) {
42532a8088fSRui Paulo 		device_printf(dev, "unable to allocate IRQ resource\n");
42632a8088fSRui Paulo 		ret = ENXIO;
42732a8088fSRui Paulo 		goto err2;
42832a8088fSRui Paulo 	}
42932a8088fSRui Paulo 
43032a8088fSRui Paulo 	ret = bus_setup_intr(dev, sc->sc_res,
43132a8088fSRui Paulo 	          INTR_TYPE_MISC | INTR_MPSAFE,
43232a8088fSRui Paulo #ifdef INTR_FILTER
43332a8088fSRui Paulo 	    asmc_sms_intrfast, asmc_sms_handler,
43432a8088fSRui Paulo #else
43532a8088fSRui Paulo 	    asmc_sms_intrfast, NULL,
43632a8088fSRui Paulo #endif
43732a8088fSRui Paulo 	    dev, &sc->sc_cookie);
43832a8088fSRui Paulo 
43932a8088fSRui Paulo 	if (ret) {
44032a8088fSRui Paulo 		device_printf(dev, "unable to setup SMS IRQ\n");
44132a8088fSRui Paulo 		goto err1;
44232a8088fSRui Paulo 	}
44332a8088fSRui Paulo nosms:
44432a8088fSRui Paulo 	return (0);
44532a8088fSRui Paulo err1:
44632a8088fSRui Paulo 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid, sc->sc_res);
44732a8088fSRui Paulo err2:
44832a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
44932a8088fSRui Paulo 	if (sc->sc_sms_tq)
45032a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
45132a8088fSRui Paulo 
45232a8088fSRui Paulo 	return (ret);
45332a8088fSRui Paulo }
45432a8088fSRui Paulo 
45532a8088fSRui Paulo static int
45632a8088fSRui Paulo asmc_detach(device_t dev)
45732a8088fSRui Paulo {
45832a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
45932a8088fSRui Paulo 
46032a8088fSRui Paulo 	if (sc->sc_sms_tq) {
46132a8088fSRui Paulo 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
46232a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
46332a8088fSRui Paulo 	}
46432a8088fSRui Paulo 	if (sc->sc_cookie)
46532a8088fSRui Paulo 		bus_teardown_intr(dev, sc->sc_res, sc->sc_cookie);
46632a8088fSRui Paulo 	if (sc->sc_res)
46732a8088fSRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid, sc->sc_res);
46832a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
46932a8088fSRui Paulo 
47032a8088fSRui Paulo 	return (0);
47132a8088fSRui Paulo }
47232a8088fSRui Paulo 
47332a8088fSRui Paulo static int
47432a8088fSRui Paulo asmc_init(device_t dev)
47532a8088fSRui Paulo {
47632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
47732a8088fSRui Paulo 	int i, error = 1;
47832a8088fSRui Paulo 	uint8_t buf[4];
47932a8088fSRui Paulo 
48032a8088fSRui Paulo 	if (sc->sc_model->smc_sms_x == NULL)
48132a8088fSRui Paulo 		goto nosms;
48232a8088fSRui Paulo 
48332a8088fSRui Paulo 	/*
48432a8088fSRui Paulo 	 * We are ready to recieve interrupts from the SMS.
48532a8088fSRui Paulo 	 */
48632a8088fSRui Paulo 	buf[0] = 0x01;
48732a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
48832a8088fSRui Paulo 	DELAY(50);
48932a8088fSRui Paulo 
49032a8088fSRui Paulo 	/*
49132a8088fSRui Paulo 	 * Initiate the polling intervals.
49232a8088fSRui Paulo 	 */
49332a8088fSRui Paulo 	buf[0] = 20; /* msecs */
49432a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
49532a8088fSRui Paulo 	DELAY(200);
49632a8088fSRui Paulo 
49732a8088fSRui Paulo 	buf[0] = 20; /* msecs */
49832a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
49932a8088fSRui Paulo 	DELAY(200);
50032a8088fSRui Paulo 
50132a8088fSRui Paulo 	buf[0] = 0x00;
50232a8088fSRui Paulo 	buf[1] = 0x60;
50332a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
50432a8088fSRui Paulo 	DELAY(200);
50532a8088fSRui Paulo 
50632a8088fSRui Paulo 	buf[0] = 0x01;
50732a8088fSRui Paulo 	buf[1] = 0xc0;
50832a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
50932a8088fSRui Paulo 	DELAY(200);
51032a8088fSRui Paulo 
51132a8088fSRui Paulo 	/*
51232a8088fSRui Paulo 	 * I'm not sure what this key does, but it seems to be
51332a8088fSRui Paulo 	 * required.
51432a8088fSRui Paulo 	 */
51532a8088fSRui Paulo 	buf[0] = 0x01;
51632a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
51732a8088fSRui Paulo 	DELAY(50);
51832a8088fSRui Paulo 
51932a8088fSRui Paulo 	/*
52032a8088fSRui Paulo 	 * Wait up to 5 seconds for SMS initialization.
52132a8088fSRui Paulo 	 */
52232a8088fSRui Paulo 	for (i = 0; i < 10000; i++) {
52332a8088fSRui Paulo 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
52432a8088fSRui Paulo 		    (buf[0] != 0x00 || buf[1] != 0x00)) {
52532a8088fSRui Paulo 			error = 0;
52632a8088fSRui Paulo 			goto nosms;
52732a8088fSRui Paulo 		}
52832a8088fSRui Paulo 
52932a8088fSRui Paulo 		buf[0] = ASMC_SMS_INIT1;
53032a8088fSRui Paulo 		buf[1] = ASMC_SMS_INIT2;
53132a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
53232a8088fSRui Paulo 		DELAY(50);
53332a8088fSRui Paulo 	}
53432a8088fSRui Paulo 
53532a8088fSRui Paulo 	asmc_sms_calibrate(dev);
53632a8088fSRui Paulo nosms:
53732a8088fSRui Paulo 	sc->sc_nfan = asmc_fan_count(dev);
53832a8088fSRui Paulo 	if (sc->sc_nfan > ASMC_MAXFANS) {
53932a8088fSRui Paulo 		device_printf(dev, "more than %d fans were detected. Please "
54032a8088fSRui Paulo 		    "report this.\n", ASMC_MAXFANS);
54132a8088fSRui Paulo 		sc->sc_nfan = ASMC_MAXFANS;
54232a8088fSRui Paulo 	}
54332a8088fSRui Paulo 
54432a8088fSRui Paulo 	if (bootverbose) {
54532a8088fSRui Paulo 		/*
54632a8088fSRui Paulo 		 * XXX: The number of keys is a 32 bit buffer, but
54732a8088fSRui Paulo 		 * right now Apple only uses the last 8 bit.
54832a8088fSRui Paulo 		 */
54932a8088fSRui Paulo 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
55032a8088fSRui Paulo 		device_printf(dev, "number of keys: %d\n", buf[3]);
55132a8088fSRui Paulo 	}
55232a8088fSRui Paulo 
55332a8088fSRui Paulo 	return (error);
55432a8088fSRui Paulo }
55532a8088fSRui Paulo 
55632a8088fSRui Paulo /*
55732a8088fSRui Paulo  * We need to make sure that the SMC acks the byte sent.
55832a8088fSRui Paulo  * Just wait up to 100 ms.
55932a8088fSRui Paulo  */
56032a8088fSRui Paulo static int
56132a8088fSRui Paulo asmc_wait(device_t dev, uint8_t val)
56232a8088fSRui Paulo {
56332a8088fSRui Paulo 	u_int i;
56432a8088fSRui Paulo 
56532a8088fSRui Paulo 	val = val & ASMC_STATUS_MASK;
56632a8088fSRui Paulo 
56732a8088fSRui Paulo 	for (i = 0; i < 1000; i++) {
56832a8088fSRui Paulo 		if ((inb(ASMC_CMDPORT) & ASMC_STATUS_MASK) == val)
56932a8088fSRui Paulo 			return (0);
57032a8088fSRui Paulo 		DELAY(10);
57132a8088fSRui Paulo 	}
57232a8088fSRui Paulo 
57332a8088fSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
57432a8088fSRui Paulo 		      inb(ASMC_CMDPORT));
57532a8088fSRui Paulo 
57632a8088fSRui Paulo 	return (1);
57732a8088fSRui Paulo }
57832a8088fSRui Paulo 
57932a8088fSRui Paulo static int
58032a8088fSRui Paulo asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
58132a8088fSRui Paulo {
58232a8088fSRui Paulo 	int i, error = 1;
58332a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
58432a8088fSRui Paulo 
58532a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
58632a8088fSRui Paulo 
58732a8088fSRui Paulo 	outb(ASMC_CMDPORT, ASMC_CMDREAD);
58832a8088fSRui Paulo 	if (asmc_wait(dev, 0x0c))
58932a8088fSRui Paulo 		goto out;
59032a8088fSRui Paulo 
59132a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
59232a8088fSRui Paulo 		outb(ASMC_DATAPORT, key[i]);
59332a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
59432a8088fSRui Paulo 			goto out;
59532a8088fSRui Paulo 	}
59632a8088fSRui Paulo 
59732a8088fSRui Paulo 	outb(ASMC_DATAPORT, len);
59832a8088fSRui Paulo 
59932a8088fSRui Paulo 	for (i = 0; i < len; i++) {
60032a8088fSRui Paulo 		if (asmc_wait(dev, 0x05))
60132a8088fSRui Paulo 			goto out;
60232a8088fSRui Paulo 		buf[i] = inb(ASMC_DATAPORT);
60332a8088fSRui Paulo 	}
60432a8088fSRui Paulo 
60532a8088fSRui Paulo 	error = 0;
60632a8088fSRui Paulo out:
60732a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
60832a8088fSRui Paulo 
60932a8088fSRui Paulo 	return (error);
61032a8088fSRui Paulo }
61132a8088fSRui Paulo 
61232a8088fSRui Paulo static int
61332a8088fSRui Paulo asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
61432a8088fSRui Paulo {
61532a8088fSRui Paulo 	int i, error = -1;
61632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
61732a8088fSRui Paulo 
61832a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
61932a8088fSRui Paulo 
62032a8088fSRui Paulo 	outb(ASMC_CMDPORT, ASMC_CMDWRITE);
62132a8088fSRui Paulo 	if (asmc_wait(dev, 0x0c))
62232a8088fSRui Paulo 		goto out;
62332a8088fSRui Paulo 
62432a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
62532a8088fSRui Paulo 		outb(ASMC_DATAPORT, key[i]);
62632a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
62732a8088fSRui Paulo 			goto out;
62832a8088fSRui Paulo 	}
62932a8088fSRui Paulo 
63032a8088fSRui Paulo 	outb(ASMC_DATAPORT, len);
63132a8088fSRui Paulo 
63232a8088fSRui Paulo 	for (i = 0; i < len; i++) {
63332a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
63432a8088fSRui Paulo 			goto out;
63532a8088fSRui Paulo 		outb(ASMC_DATAPORT, buf[i]);
63632a8088fSRui Paulo 	}
63732a8088fSRui Paulo 
63832a8088fSRui Paulo 	error = 0;
63932a8088fSRui Paulo out:
64032a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
64132a8088fSRui Paulo 
64232a8088fSRui Paulo 	return (error);
64332a8088fSRui Paulo 
64432a8088fSRui Paulo }
64532a8088fSRui Paulo 
64632a8088fSRui Paulo /*
64732a8088fSRui Paulo  * Fan control functions.
64832a8088fSRui Paulo  */
64932a8088fSRui Paulo static int
65032a8088fSRui Paulo asmc_fan_count(device_t dev)
65132a8088fSRui Paulo {
65232a8088fSRui Paulo 	uint8_t buf[1];
65332a8088fSRui Paulo 
65432a8088fSRui Paulo 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0)
65532a8088fSRui Paulo 		return (-1);
65632a8088fSRui Paulo 
65732a8088fSRui Paulo 	return (buf[0]);
65832a8088fSRui Paulo }
65932a8088fSRui Paulo 
66032a8088fSRui Paulo static int
66132a8088fSRui Paulo asmc_fan_getvalue(device_t dev, const char *key, int fan)
66232a8088fSRui Paulo {
66332a8088fSRui Paulo 	int speed;
66432a8088fSRui Paulo 	uint8_t buf[2];
66532a8088fSRui Paulo 	char fankey[5];
66632a8088fSRui Paulo 
66732a8088fSRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
66832a8088fSRui Paulo 	if (asmc_key_read(dev, fankey, buf, 2) < 0)
66932a8088fSRui Paulo 		return (-1);
67032a8088fSRui Paulo 	speed = (buf[0] << 6) | (buf[1] >> 2);
67132a8088fSRui Paulo 
67232a8088fSRui Paulo 	return (speed);
67332a8088fSRui Paulo }
67432a8088fSRui Paulo 
67532a8088fSRui Paulo static int
67632a8088fSRui Paulo asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
67732a8088fSRui Paulo {
67832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
67932a8088fSRui Paulo 	int fan = arg2;
68032a8088fSRui Paulo 	int error;
68132a8088fSRui Paulo 	int32_t v;
68232a8088fSRui Paulo 
68332a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
68432a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
68532a8088fSRui Paulo 
68632a8088fSRui Paulo 	return (error);
68732a8088fSRui Paulo }
68832a8088fSRui Paulo 
68932a8088fSRui Paulo static int
69032a8088fSRui Paulo asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
69132a8088fSRui Paulo {
69232a8088fSRui Paulo 	device_t dev = (device_t) arg1;
69332a8088fSRui Paulo 	int fan = arg2;
69432a8088fSRui Paulo 	int error;
69532a8088fSRui Paulo 	int32_t v;
69632a8088fSRui Paulo 
69732a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
69832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
69932a8088fSRui Paulo 
70032a8088fSRui Paulo 	return (error);
70132a8088fSRui Paulo }
70232a8088fSRui Paulo 
70332a8088fSRui Paulo 
70432a8088fSRui Paulo static int
70532a8088fSRui Paulo asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
70632a8088fSRui Paulo {
70732a8088fSRui Paulo 	device_t dev = (device_t) arg1;
70832a8088fSRui Paulo 	int fan = arg2;
70932a8088fSRui Paulo 	int error;
71032a8088fSRui Paulo 	int32_t v;
71132a8088fSRui Paulo 
71232a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
71332a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
71432a8088fSRui Paulo 
71532a8088fSRui Paulo 	return (error);
71632a8088fSRui Paulo }
71732a8088fSRui Paulo 
71832a8088fSRui Paulo static int
71932a8088fSRui Paulo asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
72032a8088fSRui Paulo {
72132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
72232a8088fSRui Paulo 	int fan = arg2;
72332a8088fSRui Paulo 	int error;
72432a8088fSRui Paulo 	int32_t v;
72532a8088fSRui Paulo 
72632a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
72732a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
72832a8088fSRui Paulo 
72932a8088fSRui Paulo 	return (error);
73032a8088fSRui Paulo }
73132a8088fSRui Paulo 
73232a8088fSRui Paulo static int
73332a8088fSRui Paulo asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
73432a8088fSRui Paulo {
73532a8088fSRui Paulo 	device_t dev = (device_t) arg1;
73632a8088fSRui Paulo 	int fan = arg2;
73732a8088fSRui Paulo 	int error;
73832a8088fSRui Paulo 	int32_t v;
73932a8088fSRui Paulo 
74032a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
74132a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
74232a8088fSRui Paulo 
74332a8088fSRui Paulo 	return (error);
74432a8088fSRui Paulo }
74532a8088fSRui Paulo 
74632a8088fSRui Paulo /*
74732a8088fSRui Paulo  * Temperature functions.
74832a8088fSRui Paulo  */
74932a8088fSRui Paulo static int
75032a8088fSRui Paulo asmc_temp_getvalue(device_t dev, const char *key)
75132a8088fSRui Paulo {
75232a8088fSRui Paulo 	uint8_t buf[2];
75332a8088fSRui Paulo 
75432a8088fSRui Paulo 	/*
75532a8088fSRui Paulo 	 * Check for invalid temperatures.
75632a8088fSRui Paulo 	 */
75732a8088fSRui Paulo 	if (asmc_key_read(dev, key, buf, 2) < 0)
75832a8088fSRui Paulo 		return (-1);
75932a8088fSRui Paulo 
76032a8088fSRui Paulo 	return (buf[0]);
76132a8088fSRui Paulo }
76232a8088fSRui Paulo 
76332a8088fSRui Paulo static int
76432a8088fSRui Paulo asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
76532a8088fSRui Paulo {
76632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
76732a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
76832a8088fSRui Paulo 	int error, val;
76932a8088fSRui Paulo 
77032a8088fSRui Paulo 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
77132a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &val, 0, req);
77232a8088fSRui Paulo 
77332a8088fSRui Paulo 	return (error);
77432a8088fSRui Paulo }
77532a8088fSRui Paulo 
77632a8088fSRui Paulo /*
77732a8088fSRui Paulo  * Sudden Motion Sensor functions.
77832a8088fSRui Paulo  */
77932a8088fSRui Paulo static int
78032a8088fSRui Paulo asmc_sms_read(device_t dev, const char *key, int16_t *val)
78132a8088fSRui Paulo {
78232a8088fSRui Paulo 	uint8_t buf[2];
78332a8088fSRui Paulo 	int error;
78432a8088fSRui Paulo 
78532a8088fSRui Paulo 	/* no need to do locking here as asmc_key_read() already does it */
78632a8088fSRui Paulo 	switch (key[3]) {
78732a8088fSRui Paulo 	case 'X':
78832a8088fSRui Paulo 	case 'Y':
78932a8088fSRui Paulo 	case 'Z':
79032a8088fSRui Paulo 		error =	asmc_key_read(dev, key, buf, 2);
79132a8088fSRui Paulo 		break;
79232a8088fSRui Paulo 	default:
79332a8088fSRui Paulo 		device_printf(dev, "%s called with invalid argument %s\n",
79432a8088fSRui Paulo 			      __func__, key);
79532a8088fSRui Paulo 		error = 1;
79632a8088fSRui Paulo 		goto out;
79732a8088fSRui Paulo 	}
79832a8088fSRui Paulo 	*val = ((int16_t)buf[0] << 8) | buf[1];
79932a8088fSRui Paulo out:
80032a8088fSRui Paulo 	return (error);
80132a8088fSRui Paulo }
80232a8088fSRui Paulo 
80332a8088fSRui Paulo static void
80432a8088fSRui Paulo asmc_sms_calibrate(device_t dev)
80532a8088fSRui Paulo {
80632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
80732a8088fSRui Paulo 
80832a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
80932a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
81032a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
81132a8088fSRui Paulo }
81232a8088fSRui Paulo 
81332a8088fSRui Paulo static int
81432a8088fSRui Paulo asmc_sms_intrfast(void *arg)
81532a8088fSRui Paulo {
81632a8088fSRui Paulo 	uint8_t type;
81732a8088fSRui Paulo 	device_t dev = (device_t) arg;
81832a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
81932a8088fSRui Paulo 
82032a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
82132a8088fSRui Paulo 	type = inb(ASMC_INTPORT);
82232a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
82332a8088fSRui Paulo 
82432a8088fSRui Paulo 	sc->sc_sms_intrtype = type;
82532a8088fSRui Paulo 	asmc_sms_printintr(dev, type);
82632a8088fSRui Paulo 
82732a8088fSRui Paulo #ifdef INTR_FILTER
82832a8088fSRui Paulo 	return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
82932a8088fSRui Paulo #else
83032a8088fSRui Paulo 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
83132a8088fSRui Paulo #endif
83232a8088fSRui Paulo 	return (FILTER_HANDLED);
83332a8088fSRui Paulo }
83432a8088fSRui Paulo 
83532a8088fSRui Paulo #ifdef INTR_FILTER
83632a8088fSRui Paulo static void
83732a8088fSRui Paulo asmc_sms_handler(void *arg)
83832a8088fSRui Paulo {
83932a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(arg);
84032a8088fSRui Paulo 
84132a8088fSRui Paulo 	asmc_sms_task(sc, 0);
84232a8088fSRui Paulo }
84332a8088fSRui Paulo #endif
84432a8088fSRui Paulo 
84532a8088fSRui Paulo 
84632a8088fSRui Paulo static void
84732a8088fSRui Paulo asmc_sms_printintr(device_t dev, uint8_t type)
84832a8088fSRui Paulo {
84932a8088fSRui Paulo 
85032a8088fSRui Paulo 	switch (type) {
85132a8088fSRui Paulo 	case ASMC_SMS_INTFF:
85232a8088fSRui Paulo 		device_printf(dev, "WARNING: possible free fall!\n");
85332a8088fSRui Paulo 		break;
85432a8088fSRui Paulo 	case ASMC_SMS_INTHA:
85532a8088fSRui Paulo 		device_printf(dev, "WARNING: high acceleration detected!\n");
85632a8088fSRui Paulo 		break;
85732a8088fSRui Paulo 	case ASMC_SMS_INTSH:
85832a8088fSRui Paulo 		device_printf(dev, "WARNING: possible shock!\n");
85932a8088fSRui Paulo 		break;
86032a8088fSRui Paulo 	default:
86132a8088fSRui Paulo 		device_printf(dev, "%s unknown interrupt\n", __func__);
86232a8088fSRui Paulo 	}
86332a8088fSRui Paulo }
86432a8088fSRui Paulo 
86532a8088fSRui Paulo static void
86632a8088fSRui Paulo asmc_sms_task(void *arg, int pending)
86732a8088fSRui Paulo {
86832a8088fSRui Paulo 	struct asmc_softc *sc = (struct asmc_softc *)arg;
86932a8088fSRui Paulo 	char notify[16];
87032a8088fSRui Paulo 	int type;
87132a8088fSRui Paulo 
87232a8088fSRui Paulo 	switch (sc->sc_sms_intrtype) {
87332a8088fSRui Paulo 	case ASMC_SMS_INTFF:
87432a8088fSRui Paulo 		type = 2;
87532a8088fSRui Paulo 		break;
87632a8088fSRui Paulo 	case ASMC_SMS_INTHA:
87732a8088fSRui Paulo 		type = 1;
87832a8088fSRui Paulo 		break;
87932a8088fSRui Paulo 	case ASMC_SMS_INTSH:
88032a8088fSRui Paulo 		type = 0;
88132a8088fSRui Paulo 		break;
88232a8088fSRui Paulo 	default:
88332a8088fSRui Paulo 		type = 255;
88432a8088fSRui Paulo 	}
88532a8088fSRui Paulo 
88632a8088fSRui Paulo 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
88732a8088fSRui Paulo 	devctl_notify("ISA", "asmc", "SMS", notify);
88832a8088fSRui Paulo }
88932a8088fSRui Paulo 
89032a8088fSRui Paulo static int
89132a8088fSRui Paulo asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
89232a8088fSRui Paulo {
89332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
89432a8088fSRui Paulo 	int error;
89532a8088fSRui Paulo 	int16_t val;
89632a8088fSRui Paulo 	int32_t v;
89732a8088fSRui Paulo 
89832a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
89932a8088fSRui Paulo 	v = (int32_t) val;
90032a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
90132a8088fSRui Paulo 
90232a8088fSRui Paulo 	return (error);
90332a8088fSRui Paulo }
90432a8088fSRui Paulo 
90532a8088fSRui Paulo static int
90632a8088fSRui Paulo asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
90732a8088fSRui Paulo {
90832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
90932a8088fSRui Paulo 	int error;
91032a8088fSRui Paulo 	int16_t val;
91132a8088fSRui Paulo 	int32_t v;
91232a8088fSRui Paulo 
91332a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
91432a8088fSRui Paulo 	v = (int32_t) val;
91532a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
91632a8088fSRui Paulo 
91732a8088fSRui Paulo 	return (error);
91832a8088fSRui Paulo }
91932a8088fSRui Paulo 
92032a8088fSRui Paulo static int
92132a8088fSRui Paulo asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
92232a8088fSRui Paulo {
92332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
92432a8088fSRui Paulo 	int error;
92532a8088fSRui Paulo 	int16_t val;
92632a8088fSRui Paulo 	int32_t v;
92732a8088fSRui Paulo 
92832a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
92932a8088fSRui Paulo 	v = (int32_t) val;
93032a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
93132a8088fSRui Paulo 
93232a8088fSRui Paulo 	return (error);
93332a8088fSRui Paulo }
93432a8088fSRui Paulo 
93532a8088fSRui Paulo static int
93632a8088fSRui Paulo asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
93732a8088fSRui Paulo {
93832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
93932a8088fSRui Paulo 	uint8_t buf[6];
94032a8088fSRui Paulo 	int error;
94132a8088fSRui Paulo 	unsigned int level;
94232a8088fSRui Paulo 	int32_t v;
94332a8088fSRui Paulo 
94432a8088fSRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6);
94532a8088fSRui Paulo 	v = buf[2];
94632a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
94732a8088fSRui Paulo 	if (error == 0 && req->newptr != NULL) {
94832a8088fSRui Paulo 		level = *(unsigned int *)req->newptr;
94932a8088fSRui Paulo 		if (level > 255)
95032a8088fSRui Paulo 			return (EINVAL);
95132a8088fSRui Paulo 		buf[0] = level;
95232a8088fSRui Paulo 		buf[1] = 0x00;
95332a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);
95432a8088fSRui Paulo 	}
95532a8088fSRui Paulo 
95632a8088fSRui Paulo 	return (error);
95732a8088fSRui Paulo }
95832a8088fSRui Paulo 
95932a8088fSRui Paulo static int
96032a8088fSRui Paulo asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
96132a8088fSRui Paulo {
96232a8088fSRui Paulo 	device_t dev = (device_t) arg1;
96332a8088fSRui Paulo 	uint8_t buf[6];
96432a8088fSRui Paulo 	int error;
96532a8088fSRui Paulo 	unsigned int level;
96632a8088fSRui Paulo 	int32_t v;
96732a8088fSRui Paulo 
96832a8088fSRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6);
96932a8088fSRui Paulo 	v = buf[2];
97032a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
97132a8088fSRui Paulo 	if (error == 0 && req->newptr != NULL) {
97232a8088fSRui Paulo 		level = *(unsigned int *)req->newptr;
97332a8088fSRui Paulo 		if (level > 255)
97432a8088fSRui Paulo 			return (EINVAL);
97532a8088fSRui Paulo 		buf[0] = level;
97632a8088fSRui Paulo 		buf[1] = 0x00;
97732a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);
97832a8088fSRui Paulo 	}
97932a8088fSRui Paulo 
98032a8088fSRui Paulo 	return (error);
98132a8088fSRui Paulo }
982