xref: /freebsd/sys/dev/asmc/asmc.c (revision be80e49a01a84d1920236c2258b9ccb0df28062d)
132a8088fSRui Paulo /*-
24470f0f3SRui Paulo  * Copyright (c) 2007, 2008 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 <sys/rman.h>
504c061448SRui Paulo 
5132a8088fSRui Paulo #include <machine/resource.h>
52129d3046SJung-uk Kim 
53129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
54129d3046SJung-uk Kim 
554470f0f3SRui Paulo #include <dev/acpica/acpivar.h>
5632a8088fSRui Paulo #include <dev/asmc/asmcvar.h>
5732a8088fSRui Paulo 
584c061448SRui Paulo #include "opt_intr_filter.h"
594c061448SRui Paulo 
6032a8088fSRui Paulo /*
6132a8088fSRui Paulo  * Device interface.
6232a8088fSRui Paulo  */
6332a8088fSRui Paulo static int 	asmc_probe(device_t dev);
6432a8088fSRui Paulo static int 	asmc_attach(device_t dev);
6532a8088fSRui Paulo static int 	asmc_detach(device_t dev);
6632a8088fSRui Paulo 
6732a8088fSRui Paulo /*
6832a8088fSRui Paulo  * SMC functions.
6932a8088fSRui Paulo  */
7032a8088fSRui Paulo static int 	asmc_init(device_t dev);
71be80e49aSRui Paulo static int 	asmc_command(device_t dev, uint8_t command);
7232a8088fSRui Paulo static int 	asmc_wait(device_t dev, uint8_t val);
73be80e49aSRui Paulo static int 	asmc_wait_ack(device_t dev, uint8_t val, int amount);
7432a8088fSRui Paulo static int 	asmc_key_write(device_t dev, const char *key, uint8_t *buf,
7532a8088fSRui Paulo     uint8_t len);
7632a8088fSRui Paulo static int 	asmc_key_read(device_t dev, const char *key, uint8_t *buf,
7732a8088fSRui Paulo     uint8_t);
7832a8088fSRui Paulo static int 	asmc_fan_count(device_t dev);
7932a8088fSRui Paulo static int 	asmc_fan_getvalue(device_t dev, const char *key, int fan);
8032a8088fSRui Paulo static int 	asmc_temp_getvalue(device_t dev, const char *key);
8132a8088fSRui Paulo static int 	asmc_sms_read(device_t, const char *key, int16_t *val);
8232a8088fSRui Paulo static void 	asmc_sms_calibrate(device_t dev);
8332a8088fSRui Paulo static int 	asmc_sms_intrfast(void *arg);
8432a8088fSRui Paulo #ifdef INTR_FILTER
8532a8088fSRui Paulo static void 	asmc_sms_handler(void *arg);
8632a8088fSRui Paulo #endif
8732a8088fSRui Paulo static void 	asmc_sms_printintr(device_t dev, uint8_t);
8832a8088fSRui Paulo static void 	asmc_sms_task(void *arg, int pending);
8932a8088fSRui Paulo 
9032a8088fSRui Paulo /*
9132a8088fSRui Paulo  * Model functions.
9232a8088fSRui Paulo  */
9332a8088fSRui Paulo static int 	asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
9432a8088fSRui Paulo static int 	asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
9532a8088fSRui Paulo static int 	asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
9632a8088fSRui Paulo static int 	asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
9732a8088fSRui Paulo static int 	asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
9832a8088fSRui Paulo static int 	asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
9932a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
10032a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
10132a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
10232a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
10332a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
104be80e49aSRui Paulo static int 	asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
10532a8088fSRui Paulo 
10632a8088fSRui Paulo struct asmc_model {
10732a8088fSRui Paulo 	const char 	 *smc_model;	/* smbios.system.product env var. */
10832a8088fSRui Paulo 	const char 	 *smc_desc;	/* driver description */
10932a8088fSRui Paulo 
11032a8088fSRui Paulo 	/* Helper functions */
11132a8088fSRui Paulo 	int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
11232a8088fSRui Paulo 	int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
11332a8088fSRui Paulo 	int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
11432a8088fSRui Paulo 	int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
11532a8088fSRui Paulo 	int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
11632a8088fSRui Paulo 	int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
11732a8088fSRui Paulo 	int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
11832a8088fSRui Paulo 	int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
11932a8088fSRui Paulo 	int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
12032a8088fSRui Paulo 	int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
121be80e49aSRui Paulo 	int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
12232a8088fSRui Paulo 
123d8246db0SRui Paulo 	const char 	*smc_temps[ASMC_TEMP_MAX];
124d8246db0SRui Paulo 	const char 	*smc_tempnames[ASMC_TEMP_MAX];
125d8246db0SRui Paulo 	const char 	*smc_tempdescs[ASMC_TEMP_MAX];
12632a8088fSRui Paulo };
12732a8088fSRui Paulo 
12832a8088fSRui Paulo static struct asmc_model *asmc_match(device_t dev);
12932a8088fSRui Paulo 
13032a8088fSRui Paulo #define ASMC_SMS_FUNCS	asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
13132a8088fSRui Paulo 			asmc_mb_sysctl_sms_z
13232a8088fSRui Paulo 
13332a8088fSRui Paulo #define ASMC_FAN_FUNCS	asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
13432a8088fSRui Paulo 			asmc_mb_sysctl_fanminspeed, \
13532a8088fSRui Paulo 			asmc_mb_sysctl_fanmaxspeed, \
13632a8088fSRui Paulo 			asmc_mb_sysctl_fantargetspeed
13732a8088fSRui Paulo #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
138be80e49aSRui Paulo 			 asmc_mbp_sysctl_light_right, \
139be80e49aSRui Paulo 			 asmc_mbp_sysctl_light_control
14032a8088fSRui Paulo 
14132a8088fSRui Paulo struct asmc_model asmc_models[] = {
14232a8088fSRui Paulo 	{
14332a8088fSRui Paulo 	  "MacBook1,1", "Apple SMC MacBook Core Duo",
144be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
14532a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
14632a8088fSRui Paulo 	},
14732a8088fSRui Paulo 
14832a8088fSRui Paulo 	{
14932a8088fSRui Paulo 	  "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
150be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
15132a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
15232a8088fSRui Paulo 	},
15332a8088fSRui Paulo 
15432a8088fSRui Paulo 	{
15532a8088fSRui Paulo 	  "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
15632a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
15732a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
15832a8088fSRui Paulo 	},
15932a8088fSRui Paulo 
16032a8088fSRui Paulo 	{
16132a8088fSRui Paulo 	  "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
16232a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
16332a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
16432a8088fSRui Paulo 	},
16532a8088fSRui Paulo 
16632a8088fSRui Paulo 	{
16732a8088fSRui Paulo 	  "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
16832a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
16932a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
17032a8088fSRui Paulo 	},
17132a8088fSRui Paulo 
17232a8088fSRui Paulo 	{
17332a8088fSRui Paulo 	  "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
17432a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
17532a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
17632a8088fSRui Paulo 	},
17732a8088fSRui Paulo 
17832a8088fSRui Paulo 	{
17932a8088fSRui Paulo 	  "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
18032a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
18132a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
18232a8088fSRui Paulo 	},
18332a8088fSRui Paulo 
18432a8088fSRui Paulo 	{
18532a8088fSRui Paulo 	  "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
18632a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
18732a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
18832a8088fSRui Paulo 	},
18932a8088fSRui Paulo 
190be80e49aSRui Paulo 	{
191be80e49aSRui Paulo 	  "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
192be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
193be80e49aSRui Paulo 	  ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
194be80e49aSRui Paulo 	},
195be80e49aSRui Paulo 
19632a8088fSRui Paulo 	/* The Mac Mini has no SMS */
19732a8088fSRui Paulo 	{
19832a8088fSRui Paulo 	  "Macmini1,1", "Apple SMC Mac Mini",
19932a8088fSRui Paulo 	  NULL, NULL, NULL,
20032a8088fSRui Paulo 	  ASMC_FAN_FUNCS,
201be80e49aSRui Paulo 	  NULL, NULL, NULL,
20232a8088fSRui Paulo 	  ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
20332a8088fSRui Paulo 	},
20432a8088fSRui Paulo 
205d8246db0SRui Paulo 	/* Idem for the MacPro */
206d8246db0SRui Paulo 	{
207d8246db0SRui Paulo 	  "MacPro2", "Apple SMC Mac Pro (8-core)",
208d8246db0SRui Paulo 	  NULL, NULL, NULL,
209d8246db0SRui Paulo 	  ASMC_FAN_FUNCS,
210be80e49aSRui Paulo 	  NULL, NULL, NULL,
211d8246db0SRui Paulo 	  ASMC_MP_TEMPS, ASMC_MP_TEMPNAMES, ASMC_MP_TEMPDESCS
212d8246db0SRui Paulo 	},
213941f9f10SRui Paulo 
214941f9f10SRui Paulo 	{
215941f9f10SRui Paulo 	  "MacBookAir1,1", "Apple SMC MacBook Air",
216be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
217941f9f10SRui Paulo 	  ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
218941f9f10SRui Paulo 	},
219941f9f10SRui Paulo 
220d8246db0SRui Paulo 
22132a8088fSRui Paulo 	{ NULL, NULL }
22232a8088fSRui Paulo };
22332a8088fSRui Paulo 
22432a8088fSRui Paulo #undef ASMC_SMS_FUNCS
22532a8088fSRui Paulo #undef ASMC_FAN_FUNCS
22632a8088fSRui Paulo #undef ASMC_LIGHT_FUNCS
22732a8088fSRui Paulo 
22832a8088fSRui Paulo /*
22932a8088fSRui Paulo  * Driver methods.
23032a8088fSRui Paulo  */
23132a8088fSRui Paulo static device_method_t	asmc_methods[] = {
23232a8088fSRui Paulo 	DEVMETHOD(device_probe,		asmc_probe),
23332a8088fSRui Paulo 	DEVMETHOD(device_attach,	asmc_attach),
23432a8088fSRui Paulo 	DEVMETHOD(device_detach,	asmc_detach),
23532a8088fSRui Paulo 
23632a8088fSRui Paulo 	{ 0, 0 }
23732a8088fSRui Paulo };
23832a8088fSRui Paulo 
23932a8088fSRui Paulo static driver_t	asmc_driver = {
24032a8088fSRui Paulo 	"asmc",
24132a8088fSRui Paulo 	asmc_methods,
24232a8088fSRui Paulo 	sizeof(struct asmc_softc)
24332a8088fSRui Paulo };
24432a8088fSRui Paulo 
2454470f0f3SRui Paulo /*
2464470f0f3SRui Paulo  * Debugging
2474470f0f3SRui Paulo  */
2484470f0f3SRui Paulo #define	_COMPONENT	ACPI_OEM
2494470f0f3SRui Paulo ACPI_MODULE_NAME("ASMC")
2504470f0f3SRui Paulo #ifdef DEBUG
2514470f0f3SRui Paulo #define ASMC_DPRINTF(str)	device_printf(dev, str)
2524fb9bf66SRui Paulo #else
2534fb9bf66SRui Paulo #define ASMC_DPRINTF(str)
2544470f0f3SRui Paulo #endif
2554470f0f3SRui Paulo 
256be80e49aSRui Paulo /* NB: can't be const */
2574470f0f3SRui Paulo static char *asmc_ids[] = { "APP0001", NULL };
2584470f0f3SRui Paulo 
25932a8088fSRui Paulo static devclass_t asmc_devclass;
26032a8088fSRui Paulo 
2614470f0f3SRui Paulo DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
2624470f0f3SRui Paulo MODULE_DEPEND(asmc, acpi, 1, 1, 1);
26332a8088fSRui Paulo 
26432a8088fSRui Paulo static struct asmc_model *
26532a8088fSRui Paulo asmc_match(device_t dev)
26632a8088fSRui Paulo {
26732a8088fSRui Paulo 	int i;
26832a8088fSRui Paulo 	char *model;
26932a8088fSRui Paulo 
27032a8088fSRui Paulo 	model = getenv("smbios.system.product");
27147105877SRui Paulo 	if (model == NULL)
27247105877SRui Paulo 		return (NULL);
27347105877SRui Paulo 
27432a8088fSRui Paulo 	for (i = 0; asmc_models[i].smc_model; i++) {
27532a8088fSRui Paulo 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
27632a8088fSRui Paulo 			freeenv(model);
27732a8088fSRui Paulo 			return (&asmc_models[i]);
27832a8088fSRui Paulo 		}
27932a8088fSRui Paulo 	}
28032a8088fSRui Paulo 	freeenv(model);
28132a8088fSRui Paulo 
28232a8088fSRui Paulo 	return (NULL);
28332a8088fSRui Paulo }
28432a8088fSRui Paulo 
28532a8088fSRui Paulo static int
28632a8088fSRui Paulo asmc_probe(device_t dev)
28732a8088fSRui Paulo {
28832a8088fSRui Paulo 	struct asmc_model *model;
28932a8088fSRui Paulo 
2909cb3ef6eSRui Paulo 	if (resource_disabled("asmc", 0))
29132a8088fSRui Paulo 		return (ENXIO);
2924470f0f3SRui Paulo 	if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
2934470f0f3SRui Paulo 		return (ENXIO);
2944470f0f3SRui Paulo 
29532a8088fSRui Paulo 	model = asmc_match(dev);
2964470f0f3SRui Paulo 	if (!model) {
2974470f0f3SRui Paulo 		device_printf(dev, "model not recognized\n");
29832a8088fSRui Paulo 		return (ENXIO);
2994470f0f3SRui Paulo 	}
30032a8088fSRui Paulo 	device_set_desc(dev, model->smc_desc);
30132a8088fSRui Paulo 
30232a8088fSRui Paulo 	return (BUS_PROBE_DEFAULT);
30332a8088fSRui Paulo }
30432a8088fSRui Paulo 
30532a8088fSRui Paulo static int
30632a8088fSRui Paulo asmc_attach(device_t dev)
30732a8088fSRui Paulo {
30832a8088fSRui Paulo 	int i, j;
30932a8088fSRui Paulo 	int ret;
31032a8088fSRui Paulo 	char name[2];
31132a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
31232a8088fSRui Paulo 	struct sysctl_ctx_list *sysctlctx;
31332a8088fSRui Paulo 	struct sysctl_oid *sysctlnode;
31432a8088fSRui Paulo 	struct asmc_model *model;
31532a8088fSRui Paulo 
3164470f0f3SRui Paulo 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
3174470f0f3SRui Paulo 	    &sc->sc_rid_port, RF_ACTIVE);
3184470f0f3SRui Paulo 	if (sc->sc_ioport == NULL) {
3194470f0f3SRui Paulo 		device_printf(dev, "unable to allocate IO port\n");
3204470f0f3SRui Paulo 		return (ENOMEM);
3214470f0f3SRui Paulo 	}
3224470f0f3SRui Paulo 
32332a8088fSRui Paulo 	sysctlctx  = device_get_sysctl_ctx(dev);
32432a8088fSRui Paulo 	sysctlnode = device_get_sysctl_tree(dev);
32532a8088fSRui Paulo 
32632a8088fSRui Paulo 	model = asmc_match(dev);
32732a8088fSRui Paulo 
32832a8088fSRui Paulo 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
32932a8088fSRui Paulo 
33032a8088fSRui Paulo 	sc->sc_model = model;
33132a8088fSRui Paulo 	asmc_init(dev);
33232a8088fSRui Paulo 
33332a8088fSRui Paulo 	/*
33432a8088fSRui Paulo 	 * dev.asmc.n.fan.* tree.
33532a8088fSRui Paulo 	 */
33632a8088fSRui Paulo 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
33732a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
33832a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Fan Root Tree");
33932a8088fSRui Paulo 
34032a8088fSRui Paulo 	for (i = 1; i <= sc->sc_nfan; i++) {
34132a8088fSRui Paulo 		j = i - 1;
34232a8088fSRui Paulo 		name[0] = '0' + j;
34332a8088fSRui Paulo 		name[1] = 0;
34432a8088fSRui Paulo 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
34532a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
34632a8088fSRui Paulo 		    OID_AUTO, name, CTLFLAG_RD, 0,
34732a8088fSRui Paulo 		    "Fan Subtree");
34832a8088fSRui Paulo 
34932a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
35032a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
35132a8088fSRui Paulo 		    OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
35232a8088fSRui Paulo 		    dev, j, model->smc_fan_speed, "I",
35332a8088fSRui Paulo 		    "Fan speed in RPM");
35432a8088fSRui Paulo 
35532a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
35632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
35732a8088fSRui Paulo 		    OID_AUTO, "safespeed",
35832a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
35932a8088fSRui Paulo 		    dev, j, model->smc_fan_safespeed, "I",
36032a8088fSRui Paulo 		    "Fan safe speed in RPM");
36132a8088fSRui Paulo 
36232a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
36332a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
36432a8088fSRui Paulo 		    OID_AUTO, "minspeed",
36532a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
36632a8088fSRui Paulo 		    dev, j, model->smc_fan_minspeed, "I",
36732a8088fSRui Paulo 		    "Fan minimum speed in RPM");
36832a8088fSRui Paulo 
36932a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
37032a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
37132a8088fSRui Paulo 		    OID_AUTO, "maxspeed",
37232a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
37332a8088fSRui Paulo 		    dev, j, model->smc_fan_maxspeed, "I",
37432a8088fSRui Paulo 		    "Fan maximum speed in RPM");
37532a8088fSRui Paulo 
37632a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
37732a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
37832a8088fSRui Paulo 		    OID_AUTO, "targetspeed",
37932a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
38032a8088fSRui Paulo 		    dev, j, model->smc_fan_targetspeed, "I",
38132a8088fSRui Paulo 		    "Fan target speed in RPM");
38232a8088fSRui Paulo 	}
38332a8088fSRui Paulo 
38432a8088fSRui Paulo 	/*
38532a8088fSRui Paulo 	 * dev.asmc.n.temp tree.
38632a8088fSRui Paulo 	 */
38732a8088fSRui Paulo 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
38832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
38932a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Temperature sensors");
39032a8088fSRui Paulo 
39132a8088fSRui Paulo 	for (i = 0; model->smc_temps[i]; i++) {
39232a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
39332a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
39432a8088fSRui Paulo 		    OID_AUTO, model->smc_tempnames[i],
39532a8088fSRui Paulo 		    CTLTYPE_INT | CTLFLAG_RD,
39632a8088fSRui Paulo 		    dev, i, asmc_temp_sysctl, "I",
39732a8088fSRui Paulo 		    model->smc_tempdescs[i]);
39832a8088fSRui Paulo 	}
39932a8088fSRui Paulo 
400be80e49aSRui Paulo 	/*
401be80e49aSRui Paulo 	 * dev.asmc.n.light
402be80e49aSRui Paulo 	 */
403be80e49aSRui Paulo 	if (model->smc_light_left) {
404be80e49aSRui Paulo 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
405be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
406be80e49aSRui Paulo 		    CTLFLAG_RD, 0, "Keyboard backlight sensors");
407be80e49aSRui Paulo 
408be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
409be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
410be80e49aSRui Paulo 		    OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
411be80e49aSRui Paulo 		    dev, 0, model->smc_light_left, "I",
412be80e49aSRui Paulo 		    "Keyboard backlight left sensor");
413be80e49aSRui Paulo 
414be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
415be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
416be80e49aSRui Paulo 		    OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
417be80e49aSRui Paulo 		    dev, 0, model->smc_light_right, "I",
418be80e49aSRui Paulo 		    "Keyboard backlight right sensor");
419be80e49aSRui Paulo 
420be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
421be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
422be80e49aSRui Paulo 		    OID_AUTO, "control", CTLTYPE_INT | CTLFLAG_RW,
423be80e49aSRui Paulo 		    dev, 0, model->smc_light_control, "I",
424be80e49aSRui Paulo 		    "Keyboard backlight brightness control");
425be80e49aSRui Paulo 	}
426be80e49aSRui Paulo 
42732a8088fSRui Paulo 	if (model->smc_sms_x == NULL)
42832a8088fSRui Paulo 		goto nosms;
42932a8088fSRui Paulo 
43032a8088fSRui Paulo 	/*
43132a8088fSRui Paulo 	 * dev.asmc.n.sms tree.
43232a8088fSRui Paulo 	 */
43332a8088fSRui Paulo 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
43432a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
43532a8088fSRui Paulo 	    CTLFLAG_RD, 0, "Sudden Motion Sensor");
43632a8088fSRui Paulo 
43732a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
43832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
43932a8088fSRui Paulo 	    OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
44032a8088fSRui Paulo 	    dev, 0, model->smc_sms_x, "I",
44132a8088fSRui Paulo 	    "Sudden Motion Sensor X value");
44232a8088fSRui Paulo 
44332a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
44432a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
44532a8088fSRui Paulo 	    OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
44632a8088fSRui Paulo 	    dev, 0, model->smc_sms_y, "I",
44732a8088fSRui Paulo 	    "Sudden Motion Sensor Y value");
44832a8088fSRui Paulo 
44932a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
45032a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
45132a8088fSRui Paulo 	    OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
45232a8088fSRui Paulo 	    dev, 0, model->smc_sms_z, "I",
45332a8088fSRui Paulo 	    "Sudden Motion Sensor Z value");
45432a8088fSRui Paulo 
45532a8088fSRui Paulo 	/*
45632a8088fSRui Paulo 	 * Need a taskqueue to send devctl_notify() events
45732a8088fSRui Paulo 	 * when the SMS interrupt us.
45832a8088fSRui Paulo 	 *
45932a8088fSRui Paulo 	 * PI_REALTIME is used due to the sensitivity of the
46032a8088fSRui Paulo 	 * interrupt. An interrupt from the SMS means that the
46132a8088fSRui Paulo 	 * disk heads should be turned off as quickly as possible.
46232a8088fSRui Paulo 	 *
46332a8088fSRui Paulo 	 * We only need to do this for the non INTR_FILTER case.
46432a8088fSRui Paulo 	 */
46532a8088fSRui Paulo 	sc->sc_sms_tq = NULL;
46632a8088fSRui Paulo #ifndef INTR_FILTER
46732a8088fSRui Paulo 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
46832a8088fSRui Paulo 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
46932a8088fSRui Paulo 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
47032a8088fSRui Paulo 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
47132a8088fSRui Paulo 	    device_get_nameunit(dev));
47232a8088fSRui Paulo #endif
47332a8088fSRui Paulo 	/*
47432a8088fSRui Paulo 	 * Allocate an IRQ for the SMS.
47532a8088fSRui Paulo 	 */
4764470f0f3SRui Paulo 	sc->sc_rid_irq = 0;
4774470f0f3SRui Paulo 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
4784470f0f3SRui Paulo 	    &sc->sc_rid_irq, RF_ACTIVE);
4794470f0f3SRui Paulo 	if (sc->sc_irq == NULL) {
48032a8088fSRui Paulo 		device_printf(dev, "unable to allocate IRQ resource\n");
48132a8088fSRui Paulo 		ret = ENXIO;
48232a8088fSRui Paulo 		goto err2;
48332a8088fSRui Paulo 	}
48432a8088fSRui Paulo 
4854470f0f3SRui Paulo 	ret = bus_setup_intr(dev, sc->sc_irq,
48632a8088fSRui Paulo 	          INTR_TYPE_MISC | INTR_MPSAFE,
48732a8088fSRui Paulo #ifdef INTR_FILTER
48832a8088fSRui Paulo 	    asmc_sms_intrfast, asmc_sms_handler,
48932a8088fSRui Paulo #else
49032a8088fSRui Paulo 	    asmc_sms_intrfast, NULL,
49132a8088fSRui Paulo #endif
49232a8088fSRui Paulo 	    dev, &sc->sc_cookie);
49332a8088fSRui Paulo 
49432a8088fSRui Paulo 	if (ret) {
49532a8088fSRui Paulo 		device_printf(dev, "unable to setup SMS IRQ\n");
49632a8088fSRui Paulo 		goto err1;
49732a8088fSRui Paulo 	}
49832a8088fSRui Paulo nosms:
49932a8088fSRui Paulo 	return (0);
50032a8088fSRui Paulo err1:
5014470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
50232a8088fSRui Paulo err2:
5034470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
5044470f0f3SRui Paulo 	    sc->sc_ioport);
50532a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
50632a8088fSRui Paulo 	if (sc->sc_sms_tq)
50732a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
50832a8088fSRui Paulo 
50932a8088fSRui Paulo 	return (ret);
51032a8088fSRui Paulo }
51132a8088fSRui Paulo 
51232a8088fSRui Paulo static int
51332a8088fSRui Paulo asmc_detach(device_t dev)
51432a8088fSRui Paulo {
51532a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
51632a8088fSRui Paulo 
51732a8088fSRui Paulo 	if (sc->sc_sms_tq) {
51832a8088fSRui Paulo 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
51932a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
52032a8088fSRui Paulo 	}
52132a8088fSRui Paulo 	if (sc->sc_cookie)
5224470f0f3SRui Paulo 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
5234470f0f3SRui Paulo 	if (sc->sc_irq)
5244470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
5254470f0f3SRui Paulo 		    sc->sc_irq);
5264470f0f3SRui Paulo 	if (sc->sc_ioport)
5274470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
5284470f0f3SRui Paulo 		    sc->sc_ioport);
52932a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
53032a8088fSRui Paulo 
53132a8088fSRui Paulo 	return (0);
53232a8088fSRui Paulo }
53332a8088fSRui Paulo 
53432a8088fSRui Paulo static int
53532a8088fSRui Paulo asmc_init(device_t dev)
53632a8088fSRui Paulo {
53732a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
53832a8088fSRui Paulo 	int i, error = 1;
53932a8088fSRui Paulo 	uint8_t buf[4];
54032a8088fSRui Paulo 
54132a8088fSRui Paulo 	if (sc->sc_model->smc_sms_x == NULL)
54232a8088fSRui Paulo 		goto nosms;
54332a8088fSRui Paulo 
54432a8088fSRui Paulo 	/*
54532a8088fSRui Paulo 	 * We are ready to recieve interrupts from the SMS.
54632a8088fSRui Paulo 	 */
54732a8088fSRui Paulo 	buf[0] = 0x01;
5484470f0f3SRui Paulo 	ASMC_DPRINTF(("intok key\n"));
54932a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
55032a8088fSRui Paulo 	DELAY(50);
55132a8088fSRui Paulo 
55232a8088fSRui Paulo 	/*
55332a8088fSRui Paulo 	 * Initiate the polling intervals.
55432a8088fSRui Paulo 	 */
55532a8088fSRui Paulo 	buf[0] = 20; /* msecs */
5564470f0f3SRui Paulo 	ASMC_DPRINTF(("low int key\n"));
55732a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
55832a8088fSRui Paulo 	DELAY(200);
55932a8088fSRui Paulo 
56032a8088fSRui Paulo 	buf[0] = 20; /* msecs */
5614470f0f3SRui Paulo 	ASMC_DPRINTF(("high int key\n"));
56232a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
56332a8088fSRui Paulo 	DELAY(200);
56432a8088fSRui Paulo 
56532a8088fSRui Paulo 	buf[0] = 0x00;
56632a8088fSRui Paulo 	buf[1] = 0x60;
5674470f0f3SRui Paulo 	ASMC_DPRINTF(("sms low key\n"));
56832a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
56932a8088fSRui Paulo 	DELAY(200);
57032a8088fSRui Paulo 
57132a8088fSRui Paulo 	buf[0] = 0x01;
57232a8088fSRui Paulo 	buf[1] = 0xc0;
5734470f0f3SRui Paulo 	ASMC_DPRINTF(("sms high key\n"));
57432a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
57532a8088fSRui Paulo 	DELAY(200);
57632a8088fSRui Paulo 
57732a8088fSRui Paulo 	/*
57832a8088fSRui Paulo 	 * I'm not sure what this key does, but it seems to be
57932a8088fSRui Paulo 	 * required.
58032a8088fSRui Paulo 	 */
58132a8088fSRui Paulo 	buf[0] = 0x01;
5824470f0f3SRui Paulo 	ASMC_DPRINTF(("sms flag key\n"));
58332a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
584b75dfbe8SRui Paulo 	DELAY(100);
58532a8088fSRui Paulo 
58632a8088fSRui Paulo 	/*
58732a8088fSRui Paulo 	 * Wait up to 5 seconds for SMS initialization.
58832a8088fSRui Paulo 	 */
58932a8088fSRui Paulo 	for (i = 0; i < 10000; i++) {
59032a8088fSRui Paulo 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
59132a8088fSRui Paulo 		    (buf[0] != 0x00 || buf[1] != 0x00)) {
59232a8088fSRui Paulo 			error = 0;
5934fb9bf66SRui Paulo 			goto out;
59432a8088fSRui Paulo 		}
59532a8088fSRui Paulo 		buf[0] = ASMC_SMS_INIT1;
59632a8088fSRui Paulo 		buf[1] = ASMC_SMS_INIT2;
5974470f0f3SRui Paulo 		ASMC_DPRINTF(("sms key\n"));
59832a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
59932a8088fSRui Paulo 		DELAY(50);
60032a8088fSRui Paulo 	}
6014fb9bf66SRui Paulo 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
60232a8088fSRui Paulo 
6034fb9bf66SRui Paulo out:
60432a8088fSRui Paulo 	asmc_sms_calibrate(dev);
60532a8088fSRui Paulo nosms:
60632a8088fSRui Paulo 	sc->sc_nfan = asmc_fan_count(dev);
60732a8088fSRui Paulo 	if (sc->sc_nfan > ASMC_MAXFANS) {
60832a8088fSRui Paulo 		device_printf(dev, "more than %d fans were detected. Please "
60932a8088fSRui Paulo 		    "report this.\n", ASMC_MAXFANS);
61032a8088fSRui Paulo 		sc->sc_nfan = ASMC_MAXFANS;
61132a8088fSRui Paulo 	}
61232a8088fSRui Paulo 
61332a8088fSRui Paulo 	if (bootverbose) {
61432a8088fSRui Paulo 		/*
61532a8088fSRui Paulo 		 * XXX: The number of keys is a 32 bit buffer, but
61632a8088fSRui Paulo 		 * right now Apple only uses the last 8 bit.
61732a8088fSRui Paulo 		 */
61832a8088fSRui Paulo 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
61932a8088fSRui Paulo 		device_printf(dev, "number of keys: %d\n", buf[3]);
62032a8088fSRui Paulo 	}
62132a8088fSRui Paulo 
62232a8088fSRui Paulo 	return (error);
62332a8088fSRui Paulo }
62432a8088fSRui Paulo 
62532a8088fSRui Paulo /*
62632a8088fSRui Paulo  * We need to make sure that the SMC acks the byte sent.
627be80e49aSRui Paulo  * Just wait up to (amount * 10)  ms.
62832a8088fSRui Paulo  */
62932a8088fSRui Paulo static int
630be80e49aSRui Paulo asmc_wait_ack(device_t dev, uint8_t val, int amount)
63132a8088fSRui Paulo {
6324470f0f3SRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
63332a8088fSRui Paulo 	u_int i;
63432a8088fSRui Paulo 
63532a8088fSRui Paulo 	val = val & ASMC_STATUS_MASK;
63632a8088fSRui Paulo 
637be80e49aSRui Paulo 	for (i = 0; i < amount; i++) {
6384470f0f3SRui Paulo 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
63932a8088fSRui Paulo 			return (0);
64032a8088fSRui Paulo 		DELAY(10);
64132a8088fSRui Paulo 	}
64232a8088fSRui Paulo 
643be80e49aSRui Paulo 	return (1);
644be80e49aSRui Paulo }
645be80e49aSRui Paulo 
646be80e49aSRui Paulo /*
647be80e49aSRui Paulo  * We need to make sure that the SMC acks the byte sent.
648be80e49aSRui Paulo  * Just wait up to 100 ms.
649be80e49aSRui Paulo  */
650be80e49aSRui Paulo static int
651be80e49aSRui Paulo asmc_wait(device_t dev, uint8_t val)
652be80e49aSRui Paulo {
653be80e49aSRui Paulo 	struct asmc_softc *sc;
654be80e49aSRui Paulo 
655be80e49aSRui Paulo 	if (asmc_wait_ack(dev, val, 1000) == 0)
656be80e49aSRui Paulo 		return (0);
657be80e49aSRui Paulo 
658be80e49aSRui Paulo 	sc = device_get_softc(dev);
659be80e49aSRui Paulo 	val = val & ASMC_STATUS_MASK;
660be80e49aSRui Paulo 
661be80e49aSRui Paulo #ifdef DEBUG
66232a8088fSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
6634470f0f3SRui Paulo 	    ASMC_CMDPORT_READ(sc));
664be80e49aSRui Paulo #endif
665be80e49aSRui Paulo 	return (1);
666be80e49aSRui Paulo }
66732a8088fSRui Paulo 
668be80e49aSRui Paulo /*
669be80e49aSRui Paulo  * Send the given command, retrying up to 10 times if
670be80e49aSRui Paulo  * the acknowledgement fails.
671be80e49aSRui Paulo  */
672be80e49aSRui Paulo static int
673be80e49aSRui Paulo asmc_command(device_t dev, uint8_t command) {
674be80e49aSRui Paulo 
675be80e49aSRui Paulo 	int i;
676be80e49aSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
677be80e49aSRui Paulo 
678be80e49aSRui Paulo 	for (i=0; i < 10; i++) {
679be80e49aSRui Paulo 		ASMC_CMDPORT_WRITE(sc, command);
680be80e49aSRui Paulo 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
681be80e49aSRui Paulo 			return (0);
682be80e49aSRui Paulo 		}
683be80e49aSRui Paulo 	}
684be80e49aSRui Paulo 
685be80e49aSRui Paulo #ifdef DEBUG
686be80e49aSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
687be80e49aSRui Paulo 	    ASMC_CMDPORT_READ(sc));
688be80e49aSRui Paulo #endif
68932a8088fSRui Paulo 	return (1);
69032a8088fSRui Paulo }
69132a8088fSRui Paulo 
69232a8088fSRui Paulo static int
69332a8088fSRui Paulo asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
69432a8088fSRui Paulo {
695be80e49aSRui Paulo 	int i, error = 1, try = 0;
69632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
69732a8088fSRui Paulo 
69832a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
69932a8088fSRui Paulo 
700be80e49aSRui Paulo begin:
701be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDREAD))
70232a8088fSRui Paulo 		goto out;
70332a8088fSRui Paulo 
70432a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
7054470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
70632a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
70732a8088fSRui Paulo 			goto out;
70832a8088fSRui Paulo 	}
70932a8088fSRui Paulo 
7104470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
71132a8088fSRui Paulo 
71232a8088fSRui Paulo 	for (i = 0; i < len; i++) {
71332a8088fSRui Paulo 		if (asmc_wait(dev, 0x05))
71432a8088fSRui Paulo 			goto out;
7154470f0f3SRui Paulo 		buf[i] = ASMC_DATAPORT_READ(sc);
71632a8088fSRui Paulo 	}
71732a8088fSRui Paulo 
71832a8088fSRui Paulo 	error = 0;
71932a8088fSRui Paulo out:
720be80e49aSRui Paulo 	if (error) {
721be80e49aSRui Paulo 		if (++try < 10) goto begin;
722be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
723be80e49aSRui Paulo 			__func__, key, try);
724be80e49aSRui Paulo 	}
725be80e49aSRui Paulo 
72632a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
72732a8088fSRui Paulo 
72832a8088fSRui Paulo 	return (error);
72932a8088fSRui Paulo }
73032a8088fSRui Paulo 
73132a8088fSRui Paulo static int
73232a8088fSRui Paulo asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
73332a8088fSRui Paulo {
734be80e49aSRui Paulo 	int i, error = -1, try = 0;
73532a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
73632a8088fSRui Paulo 
73732a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
73832a8088fSRui Paulo 
739be80e49aSRui Paulo begin:
7404470f0f3SRui Paulo 	ASMC_DPRINTF(("cmd port: cmd write\n"));
741be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDWRITE))
74232a8088fSRui Paulo 		goto out;
74332a8088fSRui Paulo 
7444470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: key\n"));
74532a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
7464470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
74732a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
74832a8088fSRui Paulo 			goto out;
74932a8088fSRui Paulo 	}
7504470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: length\n"));
7514470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
75232a8088fSRui Paulo 
7534470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: buffer\n"));
75432a8088fSRui Paulo 	for (i = 0; i < len; i++) {
75532a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
75632a8088fSRui Paulo 			goto out;
7574470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, buf[i]);
75832a8088fSRui Paulo 	}
75932a8088fSRui Paulo 
76032a8088fSRui Paulo 	error = 0;
76132a8088fSRui Paulo out:
762be80e49aSRui Paulo 	if (error) {
763be80e49aSRui Paulo 		if (++try < 10) goto begin;
764be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
765be80e49aSRui Paulo 			__func__, key, try);
766be80e49aSRui Paulo 	}
767be80e49aSRui Paulo 
76832a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
76932a8088fSRui Paulo 
77032a8088fSRui Paulo 	return (error);
77132a8088fSRui Paulo 
77232a8088fSRui Paulo }
77332a8088fSRui Paulo 
77432a8088fSRui Paulo /*
77532a8088fSRui Paulo  * Fan control functions.
77632a8088fSRui Paulo  */
77732a8088fSRui Paulo static int
77832a8088fSRui Paulo asmc_fan_count(device_t dev)
77932a8088fSRui Paulo {
78032a8088fSRui Paulo 	uint8_t buf[1];
78132a8088fSRui Paulo 
78232a8088fSRui Paulo 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, 1) < 0)
78332a8088fSRui Paulo 		return (-1);
78432a8088fSRui Paulo 
78532a8088fSRui Paulo 	return (buf[0]);
78632a8088fSRui Paulo }
78732a8088fSRui Paulo 
78832a8088fSRui Paulo static int
78932a8088fSRui Paulo asmc_fan_getvalue(device_t dev, const char *key, int fan)
79032a8088fSRui Paulo {
79132a8088fSRui Paulo 	int speed;
79232a8088fSRui Paulo 	uint8_t buf[2];
79332a8088fSRui Paulo 	char fankey[5];
79432a8088fSRui Paulo 
79532a8088fSRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
79632a8088fSRui Paulo 	if (asmc_key_read(dev, fankey, buf, 2) < 0)
79732a8088fSRui Paulo 		return (-1);
79832a8088fSRui Paulo 	speed = (buf[0] << 6) | (buf[1] >> 2);
79932a8088fSRui Paulo 
80032a8088fSRui Paulo 	return (speed);
80132a8088fSRui Paulo }
80232a8088fSRui Paulo 
80332a8088fSRui Paulo static int
80432a8088fSRui Paulo asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
80532a8088fSRui Paulo {
80632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
80732a8088fSRui Paulo 	int fan = arg2;
80832a8088fSRui Paulo 	int error;
80932a8088fSRui Paulo 	int32_t v;
81032a8088fSRui Paulo 
81132a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
81232a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
81332a8088fSRui Paulo 
81432a8088fSRui Paulo 	return (error);
81532a8088fSRui Paulo }
81632a8088fSRui Paulo 
81732a8088fSRui Paulo static int
81832a8088fSRui Paulo asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
81932a8088fSRui Paulo {
82032a8088fSRui Paulo 	device_t dev = (device_t) arg1;
82132a8088fSRui Paulo 	int fan = arg2;
82232a8088fSRui Paulo 	int error;
82332a8088fSRui Paulo 	int32_t v;
82432a8088fSRui Paulo 
82532a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
82632a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
82732a8088fSRui Paulo 
82832a8088fSRui Paulo 	return (error);
82932a8088fSRui Paulo }
83032a8088fSRui Paulo 
83132a8088fSRui Paulo 
83232a8088fSRui Paulo static int
83332a8088fSRui Paulo asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
83432a8088fSRui Paulo {
83532a8088fSRui Paulo 	device_t dev = (device_t) arg1;
83632a8088fSRui Paulo 	int fan = arg2;
83732a8088fSRui Paulo 	int error;
83832a8088fSRui Paulo 	int32_t v;
83932a8088fSRui Paulo 
84032a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
84132a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
84232a8088fSRui Paulo 
84332a8088fSRui Paulo 	return (error);
84432a8088fSRui Paulo }
84532a8088fSRui Paulo 
84632a8088fSRui Paulo static int
84732a8088fSRui Paulo asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
84832a8088fSRui Paulo {
84932a8088fSRui Paulo 	device_t dev = (device_t) arg1;
85032a8088fSRui Paulo 	int fan = arg2;
85132a8088fSRui Paulo 	int error;
85232a8088fSRui Paulo 	int32_t v;
85332a8088fSRui Paulo 
85432a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
85532a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
85632a8088fSRui Paulo 
85732a8088fSRui Paulo 	return (error);
85832a8088fSRui Paulo }
85932a8088fSRui Paulo 
86032a8088fSRui Paulo static int
86132a8088fSRui Paulo asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
86232a8088fSRui Paulo {
86332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
86432a8088fSRui Paulo 	int fan = arg2;
86532a8088fSRui Paulo 	int error;
86632a8088fSRui Paulo 	int32_t v;
86732a8088fSRui Paulo 
86832a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
86932a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
87032a8088fSRui Paulo 
87132a8088fSRui Paulo 	return (error);
87232a8088fSRui Paulo }
87332a8088fSRui Paulo 
87432a8088fSRui Paulo /*
87532a8088fSRui Paulo  * Temperature functions.
87632a8088fSRui Paulo  */
87732a8088fSRui Paulo static int
87832a8088fSRui Paulo asmc_temp_getvalue(device_t dev, const char *key)
87932a8088fSRui Paulo {
88032a8088fSRui Paulo 	uint8_t buf[2];
88132a8088fSRui Paulo 
88232a8088fSRui Paulo 	/*
88332a8088fSRui Paulo 	 * Check for invalid temperatures.
88432a8088fSRui Paulo 	 */
88532a8088fSRui Paulo 	if (asmc_key_read(dev, key, buf, 2) < 0)
88632a8088fSRui Paulo 		return (-1);
88732a8088fSRui Paulo 
88832a8088fSRui Paulo 	return (buf[0]);
88932a8088fSRui Paulo }
89032a8088fSRui Paulo 
89132a8088fSRui Paulo static int
89232a8088fSRui Paulo asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
89332a8088fSRui Paulo {
89432a8088fSRui Paulo 	device_t dev = (device_t) arg1;
89532a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
89632a8088fSRui Paulo 	int error, val;
89732a8088fSRui Paulo 
89832a8088fSRui Paulo 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
89932a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &val, 0, req);
90032a8088fSRui Paulo 
90132a8088fSRui Paulo 	return (error);
90232a8088fSRui Paulo }
90332a8088fSRui Paulo 
90432a8088fSRui Paulo /*
90532a8088fSRui Paulo  * Sudden Motion Sensor functions.
90632a8088fSRui Paulo  */
90732a8088fSRui Paulo static int
90832a8088fSRui Paulo asmc_sms_read(device_t dev, const char *key, int16_t *val)
90932a8088fSRui Paulo {
91032a8088fSRui Paulo 	uint8_t buf[2];
91132a8088fSRui Paulo 	int error;
91232a8088fSRui Paulo 
91332a8088fSRui Paulo 	/* no need to do locking here as asmc_key_read() already does it */
91432a8088fSRui Paulo 	switch (key[3]) {
91532a8088fSRui Paulo 	case 'X':
91632a8088fSRui Paulo 	case 'Y':
91732a8088fSRui Paulo 	case 'Z':
91832a8088fSRui Paulo 		error =	asmc_key_read(dev, key, buf, 2);
91932a8088fSRui Paulo 		break;
92032a8088fSRui Paulo 	default:
92132a8088fSRui Paulo 		device_printf(dev, "%s called with invalid argument %s\n",
92232a8088fSRui Paulo 			      __func__, key);
92332a8088fSRui Paulo 		error = 1;
92432a8088fSRui Paulo 		goto out;
92532a8088fSRui Paulo 	}
92632a8088fSRui Paulo 	*val = ((int16_t)buf[0] << 8) | buf[1];
92732a8088fSRui Paulo out:
92832a8088fSRui Paulo 	return (error);
92932a8088fSRui Paulo }
93032a8088fSRui Paulo 
93132a8088fSRui Paulo static void
93232a8088fSRui Paulo asmc_sms_calibrate(device_t dev)
93332a8088fSRui Paulo {
93432a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
93532a8088fSRui Paulo 
93632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
93732a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
93832a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
93932a8088fSRui Paulo }
94032a8088fSRui Paulo 
94132a8088fSRui Paulo static int
94232a8088fSRui Paulo asmc_sms_intrfast(void *arg)
94332a8088fSRui Paulo {
94432a8088fSRui Paulo 	uint8_t type;
94532a8088fSRui Paulo 	device_t dev = (device_t) arg;
94632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
94732a8088fSRui Paulo 
94832a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
9494470f0f3SRui Paulo 	type = ASMC_INTPORT_READ(sc);
95032a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
95132a8088fSRui Paulo 
95232a8088fSRui Paulo 	sc->sc_sms_intrtype = type;
95332a8088fSRui Paulo 	asmc_sms_printintr(dev, type);
95432a8088fSRui Paulo 
95532a8088fSRui Paulo #ifdef INTR_FILTER
95632a8088fSRui Paulo 	return (FILTER_SCHEDULE_THREAD | FILTER_HANDLED);
95732a8088fSRui Paulo #else
95832a8088fSRui Paulo 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
95932a8088fSRui Paulo #endif
96032a8088fSRui Paulo 	return (FILTER_HANDLED);
96132a8088fSRui Paulo }
96232a8088fSRui Paulo 
96332a8088fSRui Paulo #ifdef INTR_FILTER
96432a8088fSRui Paulo static void
96532a8088fSRui Paulo asmc_sms_handler(void *arg)
96632a8088fSRui Paulo {
96732a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(arg);
96832a8088fSRui Paulo 
96932a8088fSRui Paulo 	asmc_sms_task(sc, 0);
97032a8088fSRui Paulo }
97132a8088fSRui Paulo #endif
97232a8088fSRui Paulo 
97332a8088fSRui Paulo 
97432a8088fSRui Paulo static void
97532a8088fSRui Paulo asmc_sms_printintr(device_t dev, uint8_t type)
97632a8088fSRui Paulo {
97732a8088fSRui Paulo 
97832a8088fSRui Paulo 	switch (type) {
97932a8088fSRui Paulo 	case ASMC_SMS_INTFF:
98032a8088fSRui Paulo 		device_printf(dev, "WARNING: possible free fall!\n");
98132a8088fSRui Paulo 		break;
98232a8088fSRui Paulo 	case ASMC_SMS_INTHA:
98332a8088fSRui Paulo 		device_printf(dev, "WARNING: high acceleration detected!\n");
98432a8088fSRui Paulo 		break;
98532a8088fSRui Paulo 	case ASMC_SMS_INTSH:
98632a8088fSRui Paulo 		device_printf(dev, "WARNING: possible shock!\n");
98732a8088fSRui Paulo 		break;
98832a8088fSRui Paulo 	default:
98932a8088fSRui Paulo 		device_printf(dev, "%s unknown interrupt\n", __func__);
99032a8088fSRui Paulo 	}
99132a8088fSRui Paulo }
99232a8088fSRui Paulo 
99332a8088fSRui Paulo static void
99432a8088fSRui Paulo asmc_sms_task(void *arg, int pending)
99532a8088fSRui Paulo {
99632a8088fSRui Paulo 	struct asmc_softc *sc = (struct asmc_softc *)arg;
99732a8088fSRui Paulo 	char notify[16];
99832a8088fSRui Paulo 	int type;
99932a8088fSRui Paulo 
100032a8088fSRui Paulo 	switch (sc->sc_sms_intrtype) {
100132a8088fSRui Paulo 	case ASMC_SMS_INTFF:
100232a8088fSRui Paulo 		type = 2;
100332a8088fSRui Paulo 		break;
100432a8088fSRui Paulo 	case ASMC_SMS_INTHA:
100532a8088fSRui Paulo 		type = 1;
100632a8088fSRui Paulo 		break;
100732a8088fSRui Paulo 	case ASMC_SMS_INTSH:
100832a8088fSRui Paulo 		type = 0;
100932a8088fSRui Paulo 		break;
101032a8088fSRui Paulo 	default:
101132a8088fSRui Paulo 		type = 255;
101232a8088fSRui Paulo 	}
101332a8088fSRui Paulo 
101432a8088fSRui Paulo 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
10154470f0f3SRui Paulo 	devctl_notify("ACPI", "asmc", "SMS", notify);
101632a8088fSRui Paulo }
101732a8088fSRui Paulo 
101832a8088fSRui Paulo static int
101932a8088fSRui Paulo asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
102032a8088fSRui Paulo {
102132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
102232a8088fSRui Paulo 	int error;
102332a8088fSRui Paulo 	int16_t val;
102432a8088fSRui Paulo 	int32_t v;
102532a8088fSRui Paulo 
102632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
102732a8088fSRui Paulo 	v = (int32_t) val;
102832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
102932a8088fSRui Paulo 
103032a8088fSRui Paulo 	return (error);
103132a8088fSRui Paulo }
103232a8088fSRui Paulo 
103332a8088fSRui Paulo static int
103432a8088fSRui Paulo asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
103532a8088fSRui Paulo {
103632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
103732a8088fSRui Paulo 	int error;
103832a8088fSRui Paulo 	int16_t val;
103932a8088fSRui Paulo 	int32_t v;
104032a8088fSRui Paulo 
104132a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
104232a8088fSRui Paulo 	v = (int32_t) val;
104332a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
104432a8088fSRui Paulo 
104532a8088fSRui Paulo 	return (error);
104632a8088fSRui Paulo }
104732a8088fSRui Paulo 
104832a8088fSRui Paulo static int
104932a8088fSRui Paulo asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
105032a8088fSRui Paulo {
105132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
105232a8088fSRui Paulo 	int error;
105332a8088fSRui Paulo 	int16_t val;
105432a8088fSRui Paulo 	int32_t v;
105532a8088fSRui Paulo 
105632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
105732a8088fSRui Paulo 	v = (int32_t) val;
105832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
105932a8088fSRui Paulo 
106032a8088fSRui Paulo 	return (error);
106132a8088fSRui Paulo }
106232a8088fSRui Paulo 
106332a8088fSRui Paulo static int
106432a8088fSRui Paulo asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
106532a8088fSRui Paulo {
106632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
106732a8088fSRui Paulo 	uint8_t buf[6];
106832a8088fSRui Paulo 	int error;
106932a8088fSRui Paulo 	int32_t v;
107032a8088fSRui Paulo 
107132a8088fSRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, 6);
107232a8088fSRui Paulo 	v = buf[2];
107332a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
107432a8088fSRui Paulo 
107532a8088fSRui Paulo 	return (error);
107632a8088fSRui Paulo }
107732a8088fSRui Paulo 
107832a8088fSRui Paulo static int
107932a8088fSRui Paulo asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
108032a8088fSRui Paulo {
108132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
108232a8088fSRui Paulo 	uint8_t buf[6];
108332a8088fSRui Paulo 	int error;
108432a8088fSRui Paulo 	int32_t v;
108532a8088fSRui Paulo 
108632a8088fSRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, 6);
108732a8088fSRui Paulo 	v = buf[2];
108832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
1089be80e49aSRui Paulo 
1090be80e49aSRui Paulo 	return (error);
1091be80e49aSRui Paulo }
1092be80e49aSRui Paulo 
1093be80e49aSRui Paulo static int
1094be80e49aSRui Paulo asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1095be80e49aSRui Paulo {
1096be80e49aSRui Paulo 	device_t dev = (device_t) arg1;
1097be80e49aSRui Paulo 	uint8_t buf[2];
1098be80e49aSRui Paulo 	int error;
1099be80e49aSRui Paulo 	unsigned int level;
1100be80e49aSRui Paulo 	static int32_t v;
1101be80e49aSRui Paulo 
1102be80e49aSRui Paulo 	error = sysctl_handle_int(oidp, &v, sizeof(v), req);
110332a8088fSRui Paulo 	if (error == 0 && req->newptr != NULL) {
110432a8088fSRui Paulo 		level = *(unsigned int *)req->newptr;
110532a8088fSRui Paulo 		if (level > 255)
110632a8088fSRui Paulo 			return (EINVAL);
1107be80e49aSRui Paulo 		v = level;
110832a8088fSRui Paulo 		buf[0] = level;
110932a8088fSRui Paulo 		buf[1] = 0x00;
111032a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, 2);
111132a8088fSRui Paulo 	}
111232a8088fSRui Paulo 
111332a8088fSRui Paulo 	return (error);
111432a8088fSRui Paulo }
1115