xref: /freebsd/sys/dev/asmc/asmc.c (revision 308340cce2f02f06855f3ed355a2936fbc5c16bf)
132a8088fSRui Paulo /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
44470f0f3SRui Paulo  * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
532a8088fSRui Paulo  * All rights reserved.
632a8088fSRui Paulo  *
732a8088fSRui Paulo  * Redistribution and use in source and binary forms, with or without
832a8088fSRui Paulo  * modification, are permitted provided that the following conditions
932a8088fSRui Paulo  * are met:
1032a8088fSRui Paulo  * 1. Redistributions of source code must retain the above copyright
1132a8088fSRui Paulo  *    notice, this list of conditions and the following disclaimer.
1232a8088fSRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1332a8088fSRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1432a8088fSRui Paulo  *    documentation and/or other materials provided with the distribution.
1532a8088fSRui Paulo  *
1632a8088fSRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1732a8088fSRui Paulo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1832a8088fSRui Paulo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1932a8088fSRui Paulo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2032a8088fSRui Paulo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2132a8088fSRui Paulo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2232a8088fSRui Paulo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2332a8088fSRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2432a8088fSRui Paulo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2532a8088fSRui Paulo  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2632a8088fSRui Paulo  * POSSIBILITY OF SUCH DAMAGE.
2732a8088fSRui Paulo  *
2832a8088fSRui Paulo  */
2932a8088fSRui Paulo 
3032a8088fSRui Paulo /*
3132a8088fSRui Paulo  * Driver for Apple's System Management Console (SMC).
3232a8088fSRui Paulo  * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
3332a8088fSRui Paulo  *
3432a8088fSRui Paulo  * Inspired by the Linux applesmc driver.
3532a8088fSRui Paulo  */
3632a8088fSRui Paulo 
3732a8088fSRui Paulo #include <sys/cdefs.h>
3832a8088fSRui Paulo __FBSDID("$FreeBSD$");
3932a8088fSRui Paulo 
4032a8088fSRui Paulo #include <sys/param.h>
4132a8088fSRui Paulo #include <sys/bus.h>
4232a8088fSRui Paulo #include <sys/conf.h>
4332a8088fSRui Paulo #include <sys/kernel.h>
4432a8088fSRui Paulo #include <sys/lock.h>
4532a8088fSRui Paulo #include <sys/malloc.h>
4632a8088fSRui Paulo #include <sys/module.h>
4732a8088fSRui Paulo #include <sys/mutex.h>
4832a8088fSRui Paulo #include <sys/sysctl.h>
4932a8088fSRui Paulo #include <sys/systm.h>
5032a8088fSRui Paulo #include <sys/taskqueue.h>
5132a8088fSRui Paulo #include <sys/rman.h>
524c061448SRui Paulo 
5332a8088fSRui Paulo #include <machine/resource.h>
54129d3046SJung-uk Kim 
55129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
56129d3046SJung-uk Kim 
574470f0f3SRui Paulo #include <dev/acpica/acpivar.h>
5832a8088fSRui Paulo #include <dev/asmc/asmcvar.h>
5932a8088fSRui 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);
66108e3076SAdrian Chadd static int 	asmc_resume(device_t dev);
6732a8088fSRui Paulo 
6832a8088fSRui Paulo /*
6932a8088fSRui Paulo  * SMC functions.
7032a8088fSRui Paulo  */
7132a8088fSRui Paulo static int 	asmc_init(device_t dev);
72be80e49aSRui Paulo static int 	asmc_command(device_t dev, uint8_t command);
7332a8088fSRui Paulo static int 	asmc_wait(device_t dev, uint8_t val);
74be80e49aSRui Paulo static int 	asmc_wait_ack(device_t dev, uint8_t val, int amount);
7532a8088fSRui Paulo static int 	asmc_key_write(device_t dev, const char *key, uint8_t *buf,
7632a8088fSRui Paulo     uint8_t len);
7732a8088fSRui Paulo static int 	asmc_key_read(device_t dev, const char *key, uint8_t *buf,
7832a8088fSRui Paulo     uint8_t);
7932a8088fSRui Paulo static int 	asmc_fan_count(device_t dev);
8032a8088fSRui Paulo static int 	asmc_fan_getvalue(device_t dev, const char *key, int fan);
81447666f0SRui Paulo static int 	asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
8232a8088fSRui Paulo static int 	asmc_temp_getvalue(device_t dev, const char *key);
8332a8088fSRui Paulo static int 	asmc_sms_read(device_t, const char *key, int16_t *val);
8432a8088fSRui Paulo static void 	asmc_sms_calibrate(device_t dev);
8532a8088fSRui Paulo static int 	asmc_sms_intrfast(void *arg);
8632a8088fSRui Paulo static void 	asmc_sms_printintr(device_t dev, uint8_t);
8732a8088fSRui Paulo static void 	asmc_sms_task(void *arg, int pending);
881269f4d4SRui Paulo #ifdef DEBUG
891269f4d4SRui Paulo void		asmc_dumpall(device_t);
901269f4d4SRui Paulo static int	asmc_key_dump(device_t, int);
911269f4d4SRui Paulo #endif
9232a8088fSRui Paulo 
9332a8088fSRui Paulo /*
9432a8088fSRui Paulo  * Model functions.
9532a8088fSRui Paulo  */
96447666f0SRui Paulo static int 	asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
9732a8088fSRui Paulo static int 	asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
9832a8088fSRui Paulo static int 	asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
9932a8088fSRui Paulo static int 	asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
10032a8088fSRui Paulo static int 	asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
10132a8088fSRui Paulo static int 	asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
10232a8088fSRui Paulo static int 	asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
10332a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
10432a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
10532a8088fSRui Paulo static int 	asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
10632a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
10732a8088fSRui Paulo static int 	asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
108be80e49aSRui Paulo static int 	asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
10932a8088fSRui Paulo 
11032a8088fSRui Paulo struct asmc_model {
11132a8088fSRui Paulo 	const char 	 *smc_model;	/* smbios.system.product env var. */
11232a8088fSRui Paulo 	const char 	 *smc_desc;	/* driver description */
11332a8088fSRui Paulo 
11432a8088fSRui Paulo 	/* Helper functions */
11532a8088fSRui Paulo 	int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
11632a8088fSRui Paulo 	int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
11732a8088fSRui Paulo 	int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
118447666f0SRui Paulo 	int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
11932a8088fSRui Paulo 	int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
12032a8088fSRui Paulo 	int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
12132a8088fSRui Paulo 	int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
12232a8088fSRui Paulo 	int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
12332a8088fSRui Paulo 	int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
12432a8088fSRui Paulo 	int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
12532a8088fSRui Paulo 	int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
126be80e49aSRui Paulo 	int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
12732a8088fSRui Paulo 
128d8246db0SRui Paulo 	const char 	*smc_temps[ASMC_TEMP_MAX];
129d8246db0SRui Paulo 	const char 	*smc_tempnames[ASMC_TEMP_MAX];
130d8246db0SRui Paulo 	const char 	*smc_tempdescs[ASMC_TEMP_MAX];
13132a8088fSRui Paulo };
13232a8088fSRui Paulo 
13332a8088fSRui Paulo static struct asmc_model *asmc_match(device_t dev);
13432a8088fSRui Paulo 
13532a8088fSRui Paulo #define ASMC_SMS_FUNCS	asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
13632a8088fSRui Paulo 			asmc_mb_sysctl_sms_z
13732a8088fSRui Paulo 
138108e3076SAdrian Chadd #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
139108e3076SAdrian Chadd 
140447666f0SRui Paulo #define ASMC_FAN_FUNCS	asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
14132a8088fSRui Paulo 			asmc_mb_sysctl_fanminspeed, \
14232a8088fSRui Paulo 			asmc_mb_sysctl_fanmaxspeed, \
14332a8088fSRui Paulo 			asmc_mb_sysctl_fantargetspeed
144108e3076SAdrian Chadd 
145108e3076SAdrian Chadd #define ASMC_FAN_FUNCS2	asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
146108e3076SAdrian Chadd 			asmc_mb_sysctl_fanminspeed, \
147108e3076SAdrian Chadd 			asmc_mb_sysctl_fanmaxspeed, \
148108e3076SAdrian Chadd 			asmc_mb_sysctl_fantargetspeed
149108e3076SAdrian Chadd 
15032a8088fSRui Paulo #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
151be80e49aSRui Paulo 			 asmc_mbp_sysctl_light_right, \
152be80e49aSRui Paulo 			 asmc_mbp_sysctl_light_control
15332a8088fSRui Paulo 
1549f0bfc51SDavid Bright #define ASMC_LIGHT_FUNCS_DISABLED NULL, NULL, NULL
1559f0bfc51SDavid Bright 
15632a8088fSRui Paulo struct asmc_model asmc_models[] = {
15732a8088fSRui Paulo 	{
15832a8088fSRui Paulo 	  "MacBook1,1", "Apple SMC MacBook Core Duo",
159be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
16032a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
16132a8088fSRui Paulo 	},
16232a8088fSRui Paulo 
16332a8088fSRui Paulo 	{
16432a8088fSRui Paulo 	  "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
165be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
16632a8088fSRui Paulo 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
16732a8088fSRui Paulo 	},
16832a8088fSRui Paulo 
16932a8088fSRui Paulo 	{
170108e3076SAdrian Chadd 	  "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
171108e3076SAdrian Chadd 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
172108e3076SAdrian Chadd 	  ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
173108e3076SAdrian Chadd 	},
174108e3076SAdrian Chadd 
175108e3076SAdrian Chadd 	{
176e4a14ce7SMark Johnston 	  "MacBook7,1", "Apple SMC MacBook Core 2 Duo (mid 2010)",
177e4a14ce7SMark Johnston 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS_DISABLED,
178e4a14ce7SMark Johnston 	  ASMC_MB71_TEMPS, ASMC_MB71_TEMPNAMES, ASMC_MB71_TEMPDESCS
179e4a14ce7SMark Johnston 	},
180e4a14ce7SMark Johnston 
181e4a14ce7SMark Johnston 	{
18232a8088fSRui Paulo 	  "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
18332a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
18432a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
18532a8088fSRui Paulo 	},
18632a8088fSRui Paulo 
18732a8088fSRui Paulo 	{
18832a8088fSRui Paulo 	  "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
18932a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
19032a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
19132a8088fSRui Paulo 	},
19232a8088fSRui Paulo 
19332a8088fSRui Paulo 	{
19432a8088fSRui Paulo 	  "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
19532a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
19632a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
19732a8088fSRui Paulo 	},
19832a8088fSRui Paulo 
19932a8088fSRui Paulo 	{
20032a8088fSRui Paulo 	  "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
20132a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
20232a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
20332a8088fSRui Paulo 	},
20432a8088fSRui Paulo 
20532a8088fSRui Paulo 	{
20632a8088fSRui Paulo 	  "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
20732a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
20832a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
20932a8088fSRui Paulo 	},
21032a8088fSRui Paulo 
21132a8088fSRui Paulo 	{
21232a8088fSRui Paulo 	  "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
21332a8088fSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
21432a8088fSRui Paulo 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
21532a8088fSRui Paulo 	},
21632a8088fSRui Paulo 
217be80e49aSRui Paulo 	{
218be80e49aSRui Paulo 	  "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
219be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
220be80e49aSRui Paulo 	  ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
221be80e49aSRui Paulo 	},
222be80e49aSRui Paulo 
223447666f0SRui Paulo 	{
22431ae3b07SAdrian Chadd 	  "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
22531ae3b07SAdrian Chadd 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
22631ae3b07SAdrian Chadd 	  ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
22731ae3b07SAdrian Chadd 	},
22831ae3b07SAdrian Chadd 
22931ae3b07SAdrian Chadd 	{
23009ff71d3SDavid Bright 	  "MacBookPro8,1", "Apple SMC MacBook Pro (early 2011, 13-inch)",
23109ff71d3SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
23209ff71d3SDavid Bright 	  ASMC_MBP81_TEMPS, ASMC_MBP81_TEMPNAMES, ASMC_MBP81_TEMPDESCS
23309ff71d3SDavid Bright 	},
23409ff71d3SDavid Bright 
23509ff71d3SDavid Bright 	{
236447666f0SRui Paulo 	  "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
237447666f0SRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
23809ff71d3SDavid Bright 	  ASMC_MBP82_TEMPS, ASMC_MBP82_TEMPNAMES, ASMC_MBP82_TEMPDESCS
239447666f0SRui Paulo 	},
240447666f0SRui Paulo 
241447666f0SRui Paulo 	{
24298ae4866SDavid Bright 	 "MacBookPro9,2", "Apple SMC MacBook Pro (mid 2012)",
24398ae4866SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
24498ae4866SDavid Bright 	  ASMC_MBP9_TEMPS, ASMC_MBP9_TEMPNAMES, ASMC_MBP9_TEMPDESCS
24598ae4866SDavid Bright 	},
24698ae4866SDavid Bright 
24798ae4866SDavid Bright 	{
248109e2d29SAdrian Chadd 	  "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
249109e2d29SAdrian Chadd 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
250109e2d29SAdrian Chadd 	  ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
251109e2d29SAdrian Chadd 	},
252109e2d29SAdrian Chadd 
253109e2d29SAdrian Chadd 	{
254447666f0SRui Paulo 	  "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
255447666f0SRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
256109e2d29SAdrian Chadd 	  ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
257447666f0SRui Paulo 	},
258447666f0SRui Paulo 
25932a8088fSRui Paulo 	/* The Mac Mini has no SMS */
26032a8088fSRui Paulo 	{
26132a8088fSRui Paulo 	  "Macmini1,1", "Apple SMC Mac Mini",
26232a8088fSRui Paulo 	  NULL, NULL, NULL,
26332a8088fSRui Paulo 	  ASMC_FAN_FUNCS,
264be80e49aSRui Paulo 	  NULL, NULL, NULL,
26532a8088fSRui Paulo 	  ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
26632a8088fSRui Paulo 	},
26732a8088fSRui Paulo 
2689e33968bSDavid Bright         /* The Mac Mini 2,1 has no SMS */
2699e33968bSDavid Bright         {
2709e33968bSDavid Bright           "Macmini2,1", "Apple SMC Mac Mini 2,1",
2719e33968bSDavid Bright           ASMC_SMS_FUNCS_DISABLED,
2729e33968bSDavid Bright           ASMC_FAN_FUNCS,
2739e33968bSDavid Bright           ASMC_LIGHT_FUNCS_DISABLED,
2749e33968bSDavid Bright           ASMC_MM21_TEMPS, ASMC_MM21_TEMPNAMES, ASMC_MM21_TEMPDESCS
2759e33968bSDavid Bright         },
2769e33968bSDavid Bright 
277764442e0SGavin Atkinson 	/* The Mac Mini 3,1 has no SMS */
278764442e0SGavin Atkinson 	{
279764442e0SGavin Atkinson 	  "Macmini3,1", "Apple SMC Mac Mini 3,1",
280764442e0SGavin Atkinson 	  NULL, NULL, NULL,
281764442e0SGavin Atkinson 	  ASMC_FAN_FUNCS,
282764442e0SGavin Atkinson 	  NULL, NULL, NULL,
283764442e0SGavin Atkinson 	  ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
284764442e0SGavin Atkinson 	},
285764442e0SGavin Atkinson 
2869f0bfc51SDavid Bright 	/* The Mac Mini 4,1 (Mid-2010) has no SMS */
2879f0bfc51SDavid Bright 	{
2889f0bfc51SDavid Bright 	  "Macmini4,1", "Apple SMC Mac mini 4,1 (Mid-2010)",
2899f0bfc51SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED,
2909f0bfc51SDavid Bright 	  ASMC_FAN_FUNCS,
2919f0bfc51SDavid Bright 	  ASMC_LIGHT_FUNCS_DISABLED,
2929f0bfc51SDavid Bright 	  ASMC_MM41_TEMPS, ASMC_MM41_TEMPNAMES, ASMC_MM41_TEMPDESCS
2939f0bfc51SDavid Bright 	},
2949f0bfc51SDavid Bright 
29571b475e7SDavid Bright 	/* The Mac Mini 5,2 has no SMS */
29671b475e7SDavid Bright 	{
29771b475e7SDavid Bright 	  "Macmini5,2", "Apple SMC Mac Mini 5,2",
29871b475e7SDavid Bright 	  NULL, NULL, NULL,
29971b475e7SDavid Bright 	  ASMC_FAN_FUNCS2,
30071b475e7SDavid Bright 	  NULL, NULL, NULL,
30171b475e7SDavid Bright 	  ASMC_MM52_TEMPS, ASMC_MM52_TEMPNAMES, ASMC_MM52_TEMPDESCS
30271b475e7SDavid Bright 	},
30371b475e7SDavid Bright 
30439a8ee13SDavid Bright 	/* Idem for the Mac Pro "Quad Core" (original) */
30539a8ee13SDavid Bright 	{
30639a8ee13SDavid Bright 	  "MacPro1,1", "Apple SMC Mac Pro (Quad Core)",
30739a8ee13SDavid Bright 	  NULL, NULL, NULL,
30839a8ee13SDavid Bright 	  ASMC_FAN_FUNCS,
30939a8ee13SDavid Bright 	  NULL, NULL, NULL,
31039a8ee13SDavid Bright 	  ASMC_MP1_TEMPS, ASMC_MP1_TEMPNAMES, ASMC_MP1_TEMPDESCS
31139a8ee13SDavid Bright 	},
31239a8ee13SDavid Bright 
31339a8ee13SDavid Bright 	/* Idem for the Mac Pro (8-core) */
314d8246db0SRui Paulo 	{
315d8246db0SRui Paulo 	  "MacPro2", "Apple SMC Mac Pro (8-core)",
316d8246db0SRui Paulo 	  NULL, NULL, NULL,
317d8246db0SRui Paulo 	  ASMC_FAN_FUNCS,
318be80e49aSRui Paulo 	  NULL, NULL, NULL,
31939a8ee13SDavid Bright 	  ASMC_MP2_TEMPS, ASMC_MP2_TEMPNAMES, ASMC_MP2_TEMPDESCS
320d8246db0SRui Paulo 	},
321941f9f10SRui Paulo 
322447666f0SRui Paulo 	/* Idem for the MacPro  2010*/
323447666f0SRui Paulo 	{
324447666f0SRui Paulo 	  "MacPro5,1", "Apple SMC MacPro (2010)",
325447666f0SRui Paulo 	  NULL, NULL, NULL,
326447666f0SRui Paulo 	  ASMC_FAN_FUNCS,
327447666f0SRui Paulo 	  NULL, NULL, NULL,
328447666f0SRui Paulo 	  ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
329447666f0SRui Paulo 	},
330447666f0SRui Paulo 
331941f9f10SRui Paulo 	{
332941f9f10SRui Paulo 	  "MacBookAir1,1", "Apple SMC MacBook Air",
333be80e49aSRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
334941f9f10SRui Paulo 	  ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
335941f9f10SRui Paulo 	},
336941f9f10SRui Paulo 
337447666f0SRui Paulo 	{
338447666f0SRui Paulo 	  "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
339447666f0SRui Paulo 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
340447666f0SRui Paulo 	  ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
341447666f0SRui Paulo 	},
342447666f0SRui Paulo 
343108e3076SAdrian Chadd 	{
344*308340ccSMark Johnston 	  "MacBookAir4,1", "Apple SMC Macbook Air 11-inch (Mid 2011)",
345*308340ccSMark Johnston 	  ASMC_SMS_FUNCS_DISABLED,
346*308340ccSMark Johnston 	  ASMC_FAN_FUNCS2,
347*308340ccSMark Johnston 	  ASMC_LIGHT_FUNCS,
348*308340ccSMark Johnston 	  ASMC_MBA4_TEMPS, ASMC_MBA4_TEMPNAMES, ASMC_MBA4_TEMPDESCS
349*308340ccSMark Johnston 	},
350*308340ccSMark Johnston 
351*308340ccSMark Johnston 	{
352*308340ccSMark Johnston 	  "MacBookAir4,2", "Apple SMC Macbook Air 13-inch (Mid 2011)",
353*308340ccSMark Johnston 	  ASMC_SMS_FUNCS_DISABLED,
354*308340ccSMark Johnston 	  ASMC_FAN_FUNCS2,
355*308340ccSMark Johnston 	  ASMC_LIGHT_FUNCS,
356*308340ccSMark Johnston 	  ASMC_MBA4_TEMPS, ASMC_MBA4_TEMPNAMES, ASMC_MBA4_TEMPDESCS
357*308340ccSMark Johnston 	},
358*308340ccSMark Johnston 
359*308340ccSMark Johnston 	{
360108e3076SAdrian Chadd 	  "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
361108e3076SAdrian Chadd 	  ASMC_SMS_FUNCS_DISABLED,
362108e3076SAdrian Chadd 	  ASMC_FAN_FUNCS2,
363108e3076SAdrian Chadd 	  ASMC_LIGHT_FUNCS,
364108e3076SAdrian Chadd 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
365108e3076SAdrian Chadd 	},
366108e3076SAdrian Chadd 
367108e3076SAdrian Chadd 	{
368108e3076SAdrian Chadd 	  "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
369108e3076SAdrian Chadd 	  ASMC_SMS_FUNCS_DISABLED,
370108e3076SAdrian Chadd 	  ASMC_FAN_FUNCS2,
371108e3076SAdrian Chadd 	  ASMC_LIGHT_FUNCS,
372108e3076SAdrian Chadd 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
373108e3076SAdrian Chadd 	},
374108e3076SAdrian Chadd 
375081954d3SDavid Bright 	{
376081954d3SDavid Bright 	  "MacBookAir7,1", "Apple SMC MacBook Air 11-inch (Early 2015)",
377081954d3SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED,
378081954d3SDavid Bright 	  ASMC_FAN_FUNCS2,
379081954d3SDavid Bright 	  ASMC_LIGHT_FUNCS,
380081954d3SDavid Bright 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
381081954d3SDavid Bright 	},
382081954d3SDavid Bright 
383081954d3SDavid Bright 	{
384081954d3SDavid Bright 	  "MacBookAir7,2", "Apple SMC MacBook Air 13-inch (Early 2015)",
385081954d3SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED,
386081954d3SDavid Bright 	  ASMC_FAN_FUNCS2,
387081954d3SDavid Bright 	  ASMC_LIGHT_FUNCS,
388081954d3SDavid Bright 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
389081954d3SDavid Bright 	},
39032a8088fSRui Paulo 	{ NULL, NULL }
39132a8088fSRui Paulo };
39232a8088fSRui Paulo 
39332a8088fSRui Paulo #undef ASMC_SMS_FUNCS
394108e3076SAdrian Chadd #undef ASMC_SMS_FUNCS_DISABLED
39532a8088fSRui Paulo #undef ASMC_FAN_FUNCS
396108e3076SAdrian Chadd #undef ASMC_FAN_FUNCS2
39732a8088fSRui Paulo #undef ASMC_LIGHT_FUNCS
39832a8088fSRui Paulo 
39932a8088fSRui Paulo /*
40032a8088fSRui Paulo  * Driver methods.
40132a8088fSRui Paulo  */
40232a8088fSRui Paulo static device_method_t	asmc_methods[] = {
40332a8088fSRui Paulo 	DEVMETHOD(device_probe,		asmc_probe),
40432a8088fSRui Paulo 	DEVMETHOD(device_attach,	asmc_attach),
40532a8088fSRui Paulo 	DEVMETHOD(device_detach,	asmc_detach),
406108e3076SAdrian Chadd 	DEVMETHOD(device_resume,	asmc_resume),
40732a8088fSRui Paulo 	{ 0, 0 }
40832a8088fSRui Paulo };
40932a8088fSRui Paulo 
41032a8088fSRui Paulo static driver_t	asmc_driver = {
41132a8088fSRui Paulo 	"asmc",
41232a8088fSRui Paulo 	asmc_methods,
41332a8088fSRui Paulo 	sizeof(struct asmc_softc)
41432a8088fSRui Paulo };
41532a8088fSRui Paulo 
4164470f0f3SRui Paulo /*
4174470f0f3SRui Paulo  * Debugging
4184470f0f3SRui Paulo  */
4194470f0f3SRui Paulo #define	_COMPONENT	ACPI_OEM
4204470f0f3SRui Paulo ACPI_MODULE_NAME("ASMC")
4214470f0f3SRui Paulo #ifdef DEBUG
4224470f0f3SRui Paulo #define ASMC_DPRINTF(str)	device_printf(dev, str)
4234fb9bf66SRui Paulo #else
4244fb9bf66SRui Paulo #define ASMC_DPRINTF(str)
4254470f0f3SRui Paulo #endif
4264470f0f3SRui Paulo 
427be80e49aSRui Paulo /* NB: can't be const */
4284470f0f3SRui Paulo static char *asmc_ids[] = { "APP0001", NULL };
4294470f0f3SRui Paulo 
43032a8088fSRui Paulo static devclass_t asmc_devclass;
43132a8088fSRui Paulo 
432108e3076SAdrian Chadd static unsigned int light_control = 0;
433108e3076SAdrian Chadd 
4344470f0f3SRui Paulo DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
4354470f0f3SRui Paulo MODULE_DEPEND(asmc, acpi, 1, 1, 1);
43632a8088fSRui Paulo 
43732a8088fSRui Paulo static struct asmc_model *
43832a8088fSRui Paulo asmc_match(device_t dev)
43932a8088fSRui Paulo {
44032a8088fSRui Paulo 	int i;
44132a8088fSRui Paulo 	char *model;
44232a8088fSRui Paulo 
4432be111bfSDavide Italiano 	model = kern_getenv("smbios.system.product");
44447105877SRui Paulo 	if (model == NULL)
44547105877SRui Paulo 		return (NULL);
44647105877SRui Paulo 
44732a8088fSRui Paulo 	for (i = 0; asmc_models[i].smc_model; i++) {
44832a8088fSRui Paulo 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
44932a8088fSRui Paulo 			freeenv(model);
45032a8088fSRui Paulo 			return (&asmc_models[i]);
45132a8088fSRui Paulo 		}
45232a8088fSRui Paulo 	}
45332a8088fSRui Paulo 	freeenv(model);
45432a8088fSRui Paulo 
45532a8088fSRui Paulo 	return (NULL);
45632a8088fSRui Paulo }
45732a8088fSRui Paulo 
45832a8088fSRui Paulo static int
45932a8088fSRui Paulo asmc_probe(device_t dev)
46032a8088fSRui Paulo {
46132a8088fSRui Paulo 	struct asmc_model *model;
4625efca36fSTakanori Watanabe 	int rv;
46332a8088fSRui Paulo 
464a8de37b0SEitan Adler 	if (resource_disabled("asmc", 0))
465a8de37b0SEitan Adler 		return (ENXIO);
4665efca36fSTakanori Watanabe 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
4675efca36fSTakanori Watanabe 	if (rv > 0)
4685efca36fSTakanori Watanabe 		return (rv);
4694470f0f3SRui Paulo 
47032a8088fSRui Paulo 	model = asmc_match(dev);
4714470f0f3SRui Paulo 	if (!model) {
4724470f0f3SRui Paulo 		device_printf(dev, "model not recognized\n");
47332a8088fSRui Paulo 		return (ENXIO);
4744470f0f3SRui Paulo 	}
47532a8088fSRui Paulo 	device_set_desc(dev, model->smc_desc);
47632a8088fSRui Paulo 
4775efca36fSTakanori Watanabe 	return (rv);
47832a8088fSRui Paulo }
47932a8088fSRui Paulo 
48032a8088fSRui Paulo static int
48132a8088fSRui Paulo asmc_attach(device_t dev)
48232a8088fSRui Paulo {
48332a8088fSRui Paulo 	int i, j;
48432a8088fSRui Paulo 	int ret;
48532a8088fSRui Paulo 	char name[2];
48632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
48732a8088fSRui Paulo 	struct sysctl_ctx_list *sysctlctx;
48832a8088fSRui Paulo 	struct sysctl_oid *sysctlnode;
48932a8088fSRui Paulo 	struct asmc_model *model;
49032a8088fSRui Paulo 
4914470f0f3SRui Paulo 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
4924470f0f3SRui Paulo 	    &sc->sc_rid_port, RF_ACTIVE);
4934470f0f3SRui Paulo 	if (sc->sc_ioport == NULL) {
4944470f0f3SRui Paulo 		device_printf(dev, "unable to allocate IO port\n");
4954470f0f3SRui Paulo 		return (ENOMEM);
4964470f0f3SRui Paulo 	}
4974470f0f3SRui Paulo 
49832a8088fSRui Paulo 	sysctlctx  = device_get_sysctl_ctx(dev);
49932a8088fSRui Paulo 	sysctlnode = device_get_sysctl_tree(dev);
50032a8088fSRui Paulo 
50132a8088fSRui Paulo 	model = asmc_match(dev);
50232a8088fSRui Paulo 
50332a8088fSRui Paulo 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
50432a8088fSRui Paulo 
50532a8088fSRui Paulo 	sc->sc_model = model;
50632a8088fSRui Paulo 	asmc_init(dev);
50732a8088fSRui Paulo 
50832a8088fSRui Paulo 	/*
50932a8088fSRui Paulo 	 * dev.asmc.n.fan.* tree.
51032a8088fSRui Paulo 	 */
51132a8088fSRui Paulo 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
51232a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
5137029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Fan Root Tree");
51432a8088fSRui Paulo 
51532a8088fSRui Paulo 	for (i = 1; i <= sc->sc_nfan; i++) {
51632a8088fSRui Paulo 		j = i - 1;
51732a8088fSRui Paulo 		name[0] = '0' + j;
51832a8088fSRui Paulo 		name[1] = 0;
51932a8088fSRui Paulo 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
52032a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
5217029da5cSPawel Biernacki 		    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
52232a8088fSRui Paulo 		    "Fan Subtree");
52332a8088fSRui Paulo 
52432a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
52532a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
5267029da5cSPawel Biernacki 		    OID_AUTO, "id",
5277029da5cSPawel Biernacki 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
528447666f0SRui Paulo 		    dev, j, model->smc_fan_id, "I",
529447666f0SRui Paulo 		    "Fan ID");
530447666f0SRui Paulo 
531447666f0SRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
532447666f0SRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
5337029da5cSPawel Biernacki 		    OID_AUTO, "speed",
5347029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
53532a8088fSRui Paulo 		    dev, j, model->smc_fan_speed, "I",
53632a8088fSRui Paulo 		    "Fan speed in RPM");
53732a8088fSRui Paulo 
53832a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
53932a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
54032a8088fSRui Paulo 		    OID_AUTO, "safespeed",
5417029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
54232a8088fSRui Paulo 		    dev, j, model->smc_fan_safespeed, "I",
54332a8088fSRui Paulo 		    "Fan safe speed in RPM");
54432a8088fSRui Paulo 
54532a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
54632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
54732a8088fSRui Paulo 		    OID_AUTO, "minspeed",
5487029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
54932a8088fSRui Paulo 		    dev, j, model->smc_fan_minspeed, "I",
55032a8088fSRui Paulo 		    "Fan minimum speed in RPM");
55132a8088fSRui Paulo 
55232a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
55332a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
55432a8088fSRui Paulo 		    OID_AUTO, "maxspeed",
5557029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
55632a8088fSRui Paulo 		    dev, j, model->smc_fan_maxspeed, "I",
55732a8088fSRui Paulo 		    "Fan maximum speed in RPM");
55832a8088fSRui Paulo 
55932a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
56032a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
56132a8088fSRui Paulo 		    OID_AUTO, "targetspeed",
5627029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
56332a8088fSRui Paulo 		    dev, j, model->smc_fan_targetspeed, "I",
56432a8088fSRui Paulo 		    "Fan target speed in RPM");
56532a8088fSRui Paulo 	}
56632a8088fSRui Paulo 
56732a8088fSRui Paulo 	/*
56832a8088fSRui Paulo 	 * dev.asmc.n.temp tree.
56932a8088fSRui Paulo 	 */
57032a8088fSRui Paulo 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
57132a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
5727029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Temperature sensors");
57332a8088fSRui Paulo 
57432a8088fSRui Paulo 	for (i = 0; model->smc_temps[i]; i++) {
57532a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
57632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
57732a8088fSRui Paulo 		    OID_AUTO, model->smc_tempnames[i],
5787029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
57932a8088fSRui Paulo 		    dev, i, asmc_temp_sysctl, "I",
58032a8088fSRui Paulo 		    model->smc_tempdescs[i]);
58132a8088fSRui Paulo 	}
58232a8088fSRui Paulo 
583be80e49aSRui Paulo 	/*
584be80e49aSRui Paulo 	 * dev.asmc.n.light
585be80e49aSRui Paulo 	 */
586be80e49aSRui Paulo 	if (model->smc_light_left) {
587be80e49aSRui Paulo 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
588be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
5897029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
5907029da5cSPawel Biernacki 		    "Keyboard backlight sensors");
591be80e49aSRui Paulo 
592be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
593be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
5947029da5cSPawel Biernacki 		    OID_AUTO, "left",
5957029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
596be80e49aSRui Paulo 		    dev, 0, model->smc_light_left, "I",
597be80e49aSRui Paulo 		    "Keyboard backlight left sensor");
598be80e49aSRui Paulo 
599be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
600be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
6017029da5cSPawel Biernacki 		    OID_AUTO, "right",
6027029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
603be80e49aSRui Paulo 		    dev, 0, model->smc_light_right, "I",
604be80e49aSRui Paulo 		    "Keyboard backlight right sensor");
605be80e49aSRui Paulo 
606be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
607be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
6083471c35dSRui Paulo 		    OID_AUTO, "control",
6097029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY |
6107029da5cSPawel Biernacki 		    CTLFLAG_NEEDGIANT, dev, 0,
6117029da5cSPawel Biernacki 		    model->smc_light_control, "I",
612be80e49aSRui Paulo 		    "Keyboard backlight brightness control");
613be80e49aSRui Paulo 	}
614be80e49aSRui Paulo 
61532a8088fSRui Paulo 	if (model->smc_sms_x == NULL)
61632a8088fSRui Paulo 		goto nosms;
61732a8088fSRui Paulo 
61832a8088fSRui Paulo 	/*
61932a8088fSRui Paulo 	 * dev.asmc.n.sms tree.
62032a8088fSRui Paulo 	 */
62132a8088fSRui Paulo 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
62232a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
6237029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Sudden Motion Sensor");
62432a8088fSRui Paulo 
62532a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
62632a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6277029da5cSPawel Biernacki 	    OID_AUTO, "x",
6287029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
62932a8088fSRui Paulo 	    dev, 0, model->smc_sms_x, "I",
63032a8088fSRui Paulo 	    "Sudden Motion Sensor X value");
63132a8088fSRui Paulo 
63232a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
63332a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6347029da5cSPawel Biernacki 	    OID_AUTO, "y",
6357029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
63632a8088fSRui Paulo 	    dev, 0, model->smc_sms_y, "I",
63732a8088fSRui Paulo 	    "Sudden Motion Sensor Y value");
63832a8088fSRui Paulo 
63932a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
64032a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6417029da5cSPawel Biernacki 	    OID_AUTO, "z",
6427029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
64332a8088fSRui Paulo 	    dev, 0, model->smc_sms_z, "I",
64432a8088fSRui Paulo 	    "Sudden Motion Sensor Z value");
64532a8088fSRui Paulo 
64632a8088fSRui Paulo 	/*
64732a8088fSRui Paulo 	 * Need a taskqueue to send devctl_notify() events
64832a8088fSRui Paulo 	 * when the SMS interrupt us.
64932a8088fSRui Paulo 	 *
65032a8088fSRui Paulo 	 * PI_REALTIME is used due to the sensitivity of the
65132a8088fSRui Paulo 	 * interrupt. An interrupt from the SMS means that the
65232a8088fSRui Paulo 	 * disk heads should be turned off as quickly as possible.
65332a8088fSRui Paulo 	 *
65432a8088fSRui Paulo 	 * We only need to do this for the non INTR_FILTER case.
65532a8088fSRui Paulo 	 */
65632a8088fSRui Paulo 	sc->sc_sms_tq = NULL;
65732a8088fSRui Paulo 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
65832a8088fSRui Paulo 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
65932a8088fSRui Paulo 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
66032a8088fSRui Paulo 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
66132a8088fSRui Paulo 	    device_get_nameunit(dev));
66232a8088fSRui Paulo 	/*
66332a8088fSRui Paulo 	 * Allocate an IRQ for the SMS.
66432a8088fSRui Paulo 	 */
6654470f0f3SRui Paulo 	sc->sc_rid_irq = 0;
6664470f0f3SRui Paulo 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
6674470f0f3SRui Paulo 	    &sc->sc_rid_irq, RF_ACTIVE);
6684470f0f3SRui Paulo 	if (sc->sc_irq == NULL) {
66932a8088fSRui Paulo 		device_printf(dev, "unable to allocate IRQ resource\n");
67032a8088fSRui Paulo 		ret = ENXIO;
67132a8088fSRui Paulo 		goto err2;
67232a8088fSRui Paulo 	}
67332a8088fSRui Paulo 
6744470f0f3SRui Paulo 	ret = bus_setup_intr(dev, sc->sc_irq,
67532a8088fSRui Paulo 	          INTR_TYPE_MISC | INTR_MPSAFE,
67632a8088fSRui Paulo 	    asmc_sms_intrfast, NULL,
67732a8088fSRui Paulo 	    dev, &sc->sc_cookie);
67832a8088fSRui Paulo 
67932a8088fSRui Paulo 	if (ret) {
68032a8088fSRui Paulo 		device_printf(dev, "unable to setup SMS IRQ\n");
68132a8088fSRui Paulo 		goto err1;
68232a8088fSRui Paulo 	}
68332a8088fSRui Paulo nosms:
68432a8088fSRui Paulo 	return (0);
68532a8088fSRui Paulo err1:
6864470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
68732a8088fSRui Paulo err2:
6884470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
6894470f0f3SRui Paulo 	    sc->sc_ioport);
69032a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
69132a8088fSRui Paulo 	if (sc->sc_sms_tq)
69232a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
69332a8088fSRui Paulo 
69432a8088fSRui Paulo 	return (ret);
69532a8088fSRui Paulo }
69632a8088fSRui Paulo 
69732a8088fSRui Paulo static int
69832a8088fSRui Paulo asmc_detach(device_t dev)
69932a8088fSRui Paulo {
70032a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
70132a8088fSRui Paulo 
70232a8088fSRui Paulo 	if (sc->sc_sms_tq) {
70332a8088fSRui Paulo 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
70432a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
70532a8088fSRui Paulo 	}
70632a8088fSRui Paulo 	if (sc->sc_cookie)
7074470f0f3SRui Paulo 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
7084470f0f3SRui Paulo 	if (sc->sc_irq)
7094470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
7104470f0f3SRui Paulo 		    sc->sc_irq);
7114470f0f3SRui Paulo 	if (sc->sc_ioport)
7124470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
7134470f0f3SRui Paulo 		    sc->sc_ioport);
71432a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
71532a8088fSRui Paulo 
71632a8088fSRui Paulo 	return (0);
71732a8088fSRui Paulo }
71832a8088fSRui Paulo 
719108e3076SAdrian Chadd static int
720108e3076SAdrian Chadd asmc_resume(device_t dev)
721108e3076SAdrian Chadd {
722108e3076SAdrian Chadd     uint8_t buf[2];
723108e3076SAdrian Chadd     buf[0] = light_control;
724108e3076SAdrian Chadd     buf[1] = 0x00;
725108e3076SAdrian Chadd     asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
726108e3076SAdrian Chadd     return (0);
727108e3076SAdrian Chadd }
728108e3076SAdrian Chadd 
7291269f4d4SRui Paulo #ifdef DEBUG
7301269f4d4SRui Paulo void asmc_dumpall(device_t dev)
7311269f4d4SRui Paulo {
7321269f4d4SRui Paulo 	int i;
7331269f4d4SRui Paulo 
7341269f4d4SRui Paulo 	/* XXX magic number */
7351269f4d4SRui Paulo 	for (i=0; i < 0x100; i++)
7361269f4d4SRui Paulo 		asmc_key_dump(dev, i);
7371269f4d4SRui Paulo }
7381269f4d4SRui Paulo #endif
7391269f4d4SRui Paulo 
74032a8088fSRui Paulo static int
74132a8088fSRui Paulo asmc_init(device_t dev)
74232a8088fSRui Paulo {
74332a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
74432a8088fSRui Paulo 	int i, error = 1;
74532a8088fSRui Paulo 	uint8_t buf[4];
74632a8088fSRui Paulo 
74732a8088fSRui Paulo 	if (sc->sc_model->smc_sms_x == NULL)
74832a8088fSRui Paulo 		goto nosms;
74932a8088fSRui Paulo 
75032a8088fSRui Paulo 	/*
751453130d9SPedro F. Giffuni 	 * We are ready to receive interrupts from the SMS.
75232a8088fSRui Paulo 	 */
75332a8088fSRui Paulo 	buf[0] = 0x01;
7544470f0f3SRui Paulo 	ASMC_DPRINTF(("intok key\n"));
75532a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
75632a8088fSRui Paulo 	DELAY(50);
75732a8088fSRui Paulo 
75832a8088fSRui Paulo 	/*
75932a8088fSRui Paulo 	 * Initiate the polling intervals.
76032a8088fSRui Paulo 	 */
76132a8088fSRui Paulo 	buf[0] = 20; /* msecs */
7624470f0f3SRui Paulo 	ASMC_DPRINTF(("low int key\n"));
76332a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
76432a8088fSRui Paulo 	DELAY(200);
76532a8088fSRui Paulo 
76632a8088fSRui Paulo 	buf[0] = 20; /* msecs */
7674470f0f3SRui Paulo 	ASMC_DPRINTF(("high int key\n"));
76832a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
76932a8088fSRui Paulo 	DELAY(200);
77032a8088fSRui Paulo 
77132a8088fSRui Paulo 	buf[0] = 0x00;
77232a8088fSRui Paulo 	buf[1] = 0x60;
7734470f0f3SRui Paulo 	ASMC_DPRINTF(("sms low key\n"));
77432a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
77532a8088fSRui Paulo 	DELAY(200);
77632a8088fSRui Paulo 
77732a8088fSRui Paulo 	buf[0] = 0x01;
77832a8088fSRui Paulo 	buf[1] = 0xc0;
7794470f0f3SRui Paulo 	ASMC_DPRINTF(("sms high key\n"));
78032a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
78132a8088fSRui Paulo 	DELAY(200);
78232a8088fSRui Paulo 
78332a8088fSRui Paulo 	/*
78432a8088fSRui Paulo 	 * I'm not sure what this key does, but it seems to be
78532a8088fSRui Paulo 	 * required.
78632a8088fSRui Paulo 	 */
78732a8088fSRui Paulo 	buf[0] = 0x01;
7884470f0f3SRui Paulo 	ASMC_DPRINTF(("sms flag key\n"));
78932a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
790b75dfbe8SRui Paulo 	DELAY(100);
79132a8088fSRui Paulo 
7921269f4d4SRui Paulo 	sc->sc_sms_intr_works = 0;
7931269f4d4SRui Paulo 
79432a8088fSRui Paulo 	/*
7951269f4d4SRui Paulo 	 * Retry SMS initialization 1000 times
7961269f4d4SRui Paulo 	 * (takes approx. 2 seconds in worst case)
79732a8088fSRui Paulo 	 */
7981269f4d4SRui Paulo 	for (i = 0; i < 1000; i++) {
79932a8088fSRui Paulo 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
8001269f4d4SRui Paulo 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
80132a8088fSRui Paulo 			error = 0;
8021269f4d4SRui Paulo 			sc->sc_sms_intr_works = 1;
8034fb9bf66SRui Paulo 			goto out;
80432a8088fSRui Paulo 		}
80532a8088fSRui Paulo 		buf[0] = ASMC_SMS_INIT1;
80632a8088fSRui Paulo 		buf[1] = ASMC_SMS_INIT2;
8074470f0f3SRui Paulo 		ASMC_DPRINTF(("sms key\n"));
80832a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
80932a8088fSRui Paulo 		DELAY(50);
81032a8088fSRui Paulo 	}
8114fb9bf66SRui Paulo 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
81232a8088fSRui Paulo 
8134fb9bf66SRui Paulo out:
81432a8088fSRui Paulo 	asmc_sms_calibrate(dev);
81532a8088fSRui Paulo nosms:
81632a8088fSRui Paulo 	sc->sc_nfan = asmc_fan_count(dev);
81732a8088fSRui Paulo 	if (sc->sc_nfan > ASMC_MAXFANS) {
81832a8088fSRui Paulo 		device_printf(dev, "more than %d fans were detected. Please "
81932a8088fSRui Paulo 		    "report this.\n", ASMC_MAXFANS);
82032a8088fSRui Paulo 		sc->sc_nfan = ASMC_MAXFANS;
82132a8088fSRui Paulo 	}
82232a8088fSRui Paulo 
82332a8088fSRui Paulo 	if (bootverbose) {
82432a8088fSRui Paulo 		/*
825447666f0SRui Paulo 		 * The number of keys is a 32 bit buffer
82632a8088fSRui Paulo 		 */
82732a8088fSRui Paulo 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
828447666f0SRui Paulo 		device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
82932a8088fSRui Paulo 	}
83032a8088fSRui Paulo 
8311269f4d4SRui Paulo #ifdef DEBUG
8321269f4d4SRui Paulo 	asmc_dumpall(dev);
8331269f4d4SRui Paulo #endif
8341269f4d4SRui Paulo 
83532a8088fSRui Paulo 	return (error);
83632a8088fSRui Paulo }
83732a8088fSRui Paulo 
83832a8088fSRui Paulo /*
83932a8088fSRui Paulo  * We need to make sure that the SMC acks the byte sent.
840be80e49aSRui Paulo  * Just wait up to (amount * 10)  ms.
84132a8088fSRui Paulo  */
84232a8088fSRui Paulo static int
843be80e49aSRui Paulo asmc_wait_ack(device_t dev, uint8_t val, int amount)
84432a8088fSRui Paulo {
8454470f0f3SRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
84632a8088fSRui Paulo 	u_int i;
84732a8088fSRui Paulo 
84832a8088fSRui Paulo 	val = val & ASMC_STATUS_MASK;
84932a8088fSRui Paulo 
850be80e49aSRui Paulo 	for (i = 0; i < amount; i++) {
8514470f0f3SRui Paulo 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
85232a8088fSRui Paulo 			return (0);
85332a8088fSRui Paulo 		DELAY(10);
85432a8088fSRui Paulo 	}
85532a8088fSRui Paulo 
856be80e49aSRui Paulo 	return (1);
857be80e49aSRui Paulo }
858be80e49aSRui Paulo 
859be80e49aSRui Paulo /*
860be80e49aSRui Paulo  * We need to make sure that the SMC acks the byte sent.
861be80e49aSRui Paulo  * Just wait up to 100 ms.
862be80e49aSRui Paulo  */
863be80e49aSRui Paulo static int
864be80e49aSRui Paulo asmc_wait(device_t dev, uint8_t val)
865be80e49aSRui Paulo {
866e02d0cabSMateusz Guzik #ifdef DEBUG
867be80e49aSRui Paulo 	struct asmc_softc *sc;
868e02d0cabSMateusz Guzik #endif
869be80e49aSRui Paulo 
870be80e49aSRui Paulo 	if (asmc_wait_ack(dev, val, 1000) == 0)
871be80e49aSRui Paulo 		return (0);
872be80e49aSRui Paulo 
873e02d0cabSMateusz Guzik #ifdef DEBUG
874be80e49aSRui Paulo 	sc = device_get_softc(dev);
875e02d0cabSMateusz Guzik #endif
876be80e49aSRui Paulo 	val = val & ASMC_STATUS_MASK;
877be80e49aSRui Paulo 
878be80e49aSRui Paulo #ifdef DEBUG
87932a8088fSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
8804470f0f3SRui Paulo 	    ASMC_CMDPORT_READ(sc));
881be80e49aSRui Paulo #endif
882be80e49aSRui Paulo 	return (1);
883be80e49aSRui Paulo }
88432a8088fSRui Paulo 
885be80e49aSRui Paulo /*
886be80e49aSRui Paulo  * Send the given command, retrying up to 10 times if
887be80e49aSRui Paulo  * the acknowledgement fails.
888be80e49aSRui Paulo  */
889be80e49aSRui Paulo static int
890be80e49aSRui Paulo asmc_command(device_t dev, uint8_t command) {
891be80e49aSRui Paulo 	int i;
892be80e49aSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
893be80e49aSRui Paulo 
894be80e49aSRui Paulo 	for (i=0; i < 10; i++) {
895be80e49aSRui Paulo 		ASMC_CMDPORT_WRITE(sc, command);
896be80e49aSRui Paulo 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
897be80e49aSRui Paulo 			return (0);
898be80e49aSRui Paulo 		}
899be80e49aSRui Paulo 	}
900be80e49aSRui Paulo 
901be80e49aSRui Paulo #ifdef DEBUG
902be80e49aSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
903be80e49aSRui Paulo 	    ASMC_CMDPORT_READ(sc));
904be80e49aSRui Paulo #endif
90532a8088fSRui Paulo 	return (1);
90632a8088fSRui Paulo }
90732a8088fSRui Paulo 
90832a8088fSRui Paulo static int
90932a8088fSRui Paulo asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
91032a8088fSRui Paulo {
911be80e49aSRui Paulo 	int i, error = 1, try = 0;
91232a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
91332a8088fSRui Paulo 
91432a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
91532a8088fSRui Paulo 
916be80e49aSRui Paulo begin:
917be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDREAD))
91832a8088fSRui Paulo 		goto out;
91932a8088fSRui Paulo 
92032a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
9214470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
92232a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
92332a8088fSRui Paulo 			goto out;
92432a8088fSRui Paulo 	}
92532a8088fSRui Paulo 
9264470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
92732a8088fSRui Paulo 
92832a8088fSRui Paulo 	for (i = 0; i < len; i++) {
92932a8088fSRui Paulo 		if (asmc_wait(dev, 0x05))
93032a8088fSRui Paulo 			goto out;
9314470f0f3SRui Paulo 		buf[i] = ASMC_DATAPORT_READ(sc);
93232a8088fSRui Paulo 	}
93332a8088fSRui Paulo 
93432a8088fSRui Paulo 	error = 0;
93532a8088fSRui Paulo out:
936be80e49aSRui Paulo 	if (error) {
937be80e49aSRui Paulo 		if (++try < 10) goto begin;
938be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
939be80e49aSRui Paulo 			__func__, key, try);
940be80e49aSRui Paulo 	}
941be80e49aSRui Paulo 
94232a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
94332a8088fSRui Paulo 
94432a8088fSRui Paulo 	return (error);
94532a8088fSRui Paulo }
94632a8088fSRui Paulo 
9471269f4d4SRui Paulo #ifdef DEBUG
9481269f4d4SRui Paulo static int
9491269f4d4SRui Paulo asmc_key_dump(device_t dev, int number)
9501269f4d4SRui Paulo {
9511269f4d4SRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
9521269f4d4SRui Paulo 	char key[5] = { 0 };
9531269f4d4SRui Paulo 	char type[7] = { 0 };
9541269f4d4SRui Paulo 	uint8_t index[4];
9551269f4d4SRui Paulo 	uint8_t v[32];
9561269f4d4SRui Paulo 	uint8_t maxlen;
9571269f4d4SRui Paulo 	int i, error = 1, try = 0;
9581269f4d4SRui Paulo 
9591269f4d4SRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
9601269f4d4SRui Paulo 
9611269f4d4SRui Paulo 	index[0] = (number >> 24) & 0xff;
9621269f4d4SRui Paulo 	index[1] = (number >> 16) & 0xff;
9631269f4d4SRui Paulo 	index[2] = (number >> 8) & 0xff;
9641269f4d4SRui Paulo 	index[3] = (number) & 0xff;
9651269f4d4SRui Paulo 
9661269f4d4SRui Paulo begin:
9671269f4d4SRui Paulo 	if (asmc_command(dev, 0x12))
9681269f4d4SRui Paulo 		goto out;
9691269f4d4SRui Paulo 
9701269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9711269f4d4SRui Paulo 		ASMC_DATAPORT_WRITE(sc, index[i]);
9721269f4d4SRui Paulo 		if (asmc_wait(dev, 0x04))
9731269f4d4SRui Paulo 			goto out;
9741269f4d4SRui Paulo 	}
9751269f4d4SRui Paulo 
9761269f4d4SRui Paulo 	ASMC_DATAPORT_WRITE(sc, 4);
9771269f4d4SRui Paulo 
9781269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9791269f4d4SRui Paulo 		if (asmc_wait(dev, 0x05))
9801269f4d4SRui Paulo 			goto out;
9811269f4d4SRui Paulo 		key[i] = ASMC_DATAPORT_READ(sc);
9821269f4d4SRui Paulo 	}
9831269f4d4SRui Paulo 
9841269f4d4SRui Paulo 	/* get type */
9851269f4d4SRui Paulo 	if (asmc_command(dev, 0x13))
9861269f4d4SRui Paulo 		goto out;
9871269f4d4SRui Paulo 
9881269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9891269f4d4SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
9901269f4d4SRui Paulo 		if (asmc_wait(dev, 0x04))
9911269f4d4SRui Paulo 			goto out;
9921269f4d4SRui Paulo 	}
9931269f4d4SRui Paulo 
9941269f4d4SRui Paulo 	ASMC_DATAPORT_WRITE(sc, 6);
9951269f4d4SRui Paulo 
9961269f4d4SRui Paulo 	for (i = 0; i < 6; i++) {
9971269f4d4SRui Paulo 		if (asmc_wait(dev, 0x05))
9981269f4d4SRui Paulo 			goto out;
9991269f4d4SRui Paulo 		type[i] = ASMC_DATAPORT_READ(sc);
10001269f4d4SRui Paulo 	}
10011269f4d4SRui Paulo 
10021269f4d4SRui Paulo 	error = 0;
10031269f4d4SRui Paulo out:
10041269f4d4SRui Paulo 	if (error) {
10051269f4d4SRui Paulo 		if (++try < 10) goto begin;
10061269f4d4SRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
10071269f4d4SRui Paulo 			__func__, key, try);
10081269f4d4SRui Paulo 		mtx_unlock_spin(&sc->sc_mtx);
10091269f4d4SRui Paulo 	}
10101269f4d4SRui Paulo 	else {
10111269f4d4SRui Paulo 		char buf[1024];
10121269f4d4SRui Paulo 		char buf2[8];
10131269f4d4SRui Paulo 		mtx_unlock_spin(&sc->sc_mtx);
10141269f4d4SRui Paulo 		maxlen = type[0];
10151269f4d4SRui Paulo 		type[0] = ' ';
10161269f4d4SRui Paulo 		type[5] = 0;
10171269f4d4SRui Paulo 		if (maxlen > sizeof(v)) {
1018f17bca82SRui Paulo 			device_printf(dev,
1019f17bca82SRui Paulo 			    "WARNING: cropping maxlen from %d to %zu\n",
1020f17bca82SRui Paulo 			    maxlen, sizeof(v));
10211269f4d4SRui Paulo 			maxlen = sizeof(v);
10221269f4d4SRui Paulo 		}
10231269f4d4SRui Paulo 		for (i = 0; i < sizeof(v); i++) {
10241269f4d4SRui Paulo 			v[i] = 0;
10251269f4d4SRui Paulo 		}
10261269f4d4SRui Paulo 		asmc_key_read(dev, key, v, maxlen);
10271269f4d4SRui Paulo 		snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
10281269f4d4SRui Paulo 		    "(len %d), data", number, key, type, maxlen);
10291269f4d4SRui Paulo 		for (i = 0; i < maxlen; i++) {
1030108e3076SAdrian Chadd 			snprintf(buf2, sizeof(buf2), " %02x", v[i]);
10311269f4d4SRui Paulo 			strlcat(buf, buf2, sizeof(buf));
10321269f4d4SRui Paulo 		}
10331269f4d4SRui Paulo 		strlcat(buf, " \n", sizeof(buf));
103446c76550SRoman Divacky 		device_printf(dev, "%s", buf);
10351269f4d4SRui Paulo 	}
10361269f4d4SRui Paulo 
10371269f4d4SRui Paulo 	return (error);
10381269f4d4SRui Paulo }
10391269f4d4SRui Paulo #endif
10401269f4d4SRui Paulo 
104132a8088fSRui Paulo static int
104232a8088fSRui Paulo asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
104332a8088fSRui Paulo {
1044be80e49aSRui Paulo 	int i, error = -1, try = 0;
104532a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
104632a8088fSRui Paulo 
104732a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
104832a8088fSRui Paulo 
1049be80e49aSRui Paulo begin:
10504470f0f3SRui Paulo 	ASMC_DPRINTF(("cmd port: cmd write\n"));
1051be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDWRITE))
105232a8088fSRui Paulo 		goto out;
105332a8088fSRui Paulo 
10544470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: key\n"));
105532a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
10564470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
105732a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
105832a8088fSRui Paulo 			goto out;
105932a8088fSRui Paulo 	}
10604470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: length\n"));
10614470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
106232a8088fSRui Paulo 
10634470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: buffer\n"));
106432a8088fSRui Paulo 	for (i = 0; i < len; i++) {
106532a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
106632a8088fSRui Paulo 			goto out;
10674470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, buf[i]);
106832a8088fSRui Paulo 	}
106932a8088fSRui Paulo 
107032a8088fSRui Paulo 	error = 0;
107132a8088fSRui Paulo out:
1072be80e49aSRui Paulo 	if (error) {
1073be80e49aSRui Paulo 		if (++try < 10) goto begin;
1074be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1075be80e49aSRui Paulo 			__func__, key, try);
1076be80e49aSRui Paulo 	}
1077be80e49aSRui Paulo 
107832a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
107932a8088fSRui Paulo 
108032a8088fSRui Paulo 	return (error);
108132a8088fSRui Paulo 
108232a8088fSRui Paulo }
108332a8088fSRui Paulo 
108432a8088fSRui Paulo /*
108532a8088fSRui Paulo  * Fan control functions.
108632a8088fSRui Paulo  */
108732a8088fSRui Paulo static int
108832a8088fSRui Paulo asmc_fan_count(device_t dev)
108932a8088fSRui Paulo {
109032a8088fSRui Paulo 	uint8_t buf[1];
109132a8088fSRui Paulo 
10929c325393SMark Johnston 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) != 0)
109332a8088fSRui Paulo 		return (-1);
109432a8088fSRui Paulo 
109532a8088fSRui Paulo 	return (buf[0]);
109632a8088fSRui Paulo }
109732a8088fSRui Paulo 
109832a8088fSRui Paulo static int
109932a8088fSRui Paulo asmc_fan_getvalue(device_t dev, const char *key, int fan)
110032a8088fSRui Paulo {
110132a8088fSRui Paulo 	int speed;
110232a8088fSRui Paulo 	uint8_t buf[2];
110332a8088fSRui Paulo 	char fankey[5];
110432a8088fSRui Paulo 
110532a8088fSRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
11069c325393SMark Johnston 	if (asmc_key_read(dev, fankey, buf, sizeof buf) != 0)
110732a8088fSRui Paulo 		return (-1);
110832a8088fSRui Paulo 	speed = (buf[0] << 6) | (buf[1] >> 2);
110932a8088fSRui Paulo 
111032a8088fSRui Paulo 	return (speed);
111132a8088fSRui Paulo }
111232a8088fSRui Paulo 
1113447666f0SRui Paulo static char*
1114623534d6SUlrich Spörlein asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1115447666f0SRui Paulo {
1116447666f0SRui Paulo 	char fankey[5];
1117447666f0SRui Paulo 	char* desc;
1118447666f0SRui Paulo 
1119447666f0SRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
11209c325393SMark Johnston 	if (asmc_key_read(dev, fankey, buf, buflen) != 0)
1121447666f0SRui Paulo 		return (NULL);
1122447666f0SRui Paulo 	desc = buf+4;
1123447666f0SRui Paulo 
1124447666f0SRui Paulo 	return (desc);
1125447666f0SRui Paulo }
1126447666f0SRui Paulo 
1127447666f0SRui Paulo static int
1128447666f0SRui Paulo asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1129447666f0SRui Paulo {
1130447666f0SRui Paulo 	uint8_t buf[2];
1131447666f0SRui Paulo 	char fankey[5];
1132447666f0SRui Paulo 
1133447666f0SRui Paulo 	speed *= 4;
1134447666f0SRui Paulo 
1135447666f0SRui Paulo 	buf[0] = speed>>8;
1136447666f0SRui Paulo 	buf[1] = speed;
1137447666f0SRui Paulo 
1138447666f0SRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
1139447666f0SRui Paulo 	if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1140447666f0SRui Paulo 		return (-1);
1141447666f0SRui Paulo 
1142447666f0SRui Paulo 	return (0);
1143447666f0SRui Paulo }
1144447666f0SRui Paulo 
114532a8088fSRui Paulo static int
114632a8088fSRui Paulo asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
114732a8088fSRui Paulo {
114832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
114932a8088fSRui Paulo 	int fan = arg2;
115032a8088fSRui Paulo 	int error;
115132a8088fSRui Paulo 	int32_t v;
115232a8088fSRui Paulo 
115332a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
115432a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
115532a8088fSRui Paulo 
115632a8088fSRui Paulo 	return (error);
115732a8088fSRui Paulo }
115832a8088fSRui Paulo 
115932a8088fSRui Paulo static int
1160447666f0SRui Paulo asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1161447666f0SRui Paulo {
1162623534d6SUlrich Spörlein 	uint8_t buf[16];
1163447666f0SRui Paulo 	device_t dev = (device_t) arg1;
1164447666f0SRui Paulo 	int fan = arg2;
1165447666f0SRui Paulo 	int error = true;
1166447666f0SRui Paulo 	char* desc;
1167447666f0SRui Paulo 
1168623534d6SUlrich Spörlein 	desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1169447666f0SRui Paulo 
1170447666f0SRui Paulo 	if (desc != NULL)
1171447666f0SRui Paulo 		error = sysctl_handle_string(oidp, desc, 0, req);
1172447666f0SRui Paulo 
1173447666f0SRui Paulo 	return (error);
1174447666f0SRui Paulo }
1175447666f0SRui Paulo 
1176447666f0SRui Paulo static int
117732a8088fSRui Paulo asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
117832a8088fSRui Paulo {
117932a8088fSRui Paulo 	device_t dev = (device_t) arg1;
118032a8088fSRui Paulo 	int fan = arg2;
118132a8088fSRui Paulo 	int error;
118232a8088fSRui Paulo 	int32_t v;
118332a8088fSRui Paulo 
118432a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
118532a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
118632a8088fSRui Paulo 
118732a8088fSRui Paulo 	return (error);
118832a8088fSRui Paulo }
118932a8088fSRui Paulo 
119032a8088fSRui Paulo static int
119132a8088fSRui Paulo asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
119232a8088fSRui Paulo {
119332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
119432a8088fSRui Paulo 	int fan = arg2;
119532a8088fSRui Paulo 	int error;
119632a8088fSRui Paulo 	int32_t v;
119732a8088fSRui Paulo 
119832a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
119932a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
120032a8088fSRui Paulo 
1201447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
12020e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1203447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1204447666f0SRui Paulo 	}
1205447666f0SRui Paulo 
120632a8088fSRui Paulo 	return (error);
120732a8088fSRui Paulo }
120832a8088fSRui Paulo 
120932a8088fSRui Paulo static int
121032a8088fSRui Paulo asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
121132a8088fSRui Paulo {
121232a8088fSRui Paulo 	device_t dev = (device_t) arg1;
121332a8088fSRui Paulo 	int fan = arg2;
121432a8088fSRui Paulo 	int error;
121532a8088fSRui Paulo 	int32_t v;
121632a8088fSRui Paulo 
121732a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
121832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
121932a8088fSRui Paulo 
1220447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
12210e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1222447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1223447666f0SRui Paulo 	}
1224447666f0SRui Paulo 
122532a8088fSRui Paulo 	return (error);
122632a8088fSRui Paulo }
122732a8088fSRui Paulo 
122832a8088fSRui Paulo static int
122932a8088fSRui Paulo asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
123032a8088fSRui Paulo {
123132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
123232a8088fSRui Paulo 	int fan = arg2;
123332a8088fSRui Paulo 	int error;
123432a8088fSRui Paulo 	int32_t v;
123532a8088fSRui Paulo 
123632a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
123732a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
123832a8088fSRui Paulo 
1239447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
12400e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1241447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1242447666f0SRui Paulo 	}
1243447666f0SRui Paulo 
124432a8088fSRui Paulo 	return (error);
124532a8088fSRui Paulo }
124632a8088fSRui Paulo 
124732a8088fSRui Paulo /*
124832a8088fSRui Paulo  * Temperature functions.
124932a8088fSRui Paulo  */
125032a8088fSRui Paulo static int
125132a8088fSRui Paulo asmc_temp_getvalue(device_t dev, const char *key)
125232a8088fSRui Paulo {
125332a8088fSRui Paulo 	uint8_t buf[2];
125432a8088fSRui Paulo 
125532a8088fSRui Paulo 	/*
125632a8088fSRui Paulo 	 * Check for invalid temperatures.
125732a8088fSRui Paulo 	 */
12589c325393SMark Johnston 	if (asmc_key_read(dev, key, buf, sizeof buf) != 0)
125932a8088fSRui Paulo 		return (-1);
126032a8088fSRui Paulo 
126132a8088fSRui Paulo 	return (buf[0]);
126232a8088fSRui Paulo }
126332a8088fSRui Paulo 
126432a8088fSRui Paulo static int
126532a8088fSRui Paulo asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
126632a8088fSRui Paulo {
126732a8088fSRui Paulo 	device_t dev = (device_t) arg1;
126832a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
126932a8088fSRui Paulo 	int error, val;
127032a8088fSRui Paulo 
127132a8088fSRui Paulo 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
127232a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &val, 0, req);
127332a8088fSRui Paulo 
127432a8088fSRui Paulo 	return (error);
127532a8088fSRui Paulo }
127632a8088fSRui Paulo 
127732a8088fSRui Paulo /*
127832a8088fSRui Paulo  * Sudden Motion Sensor functions.
127932a8088fSRui Paulo  */
128032a8088fSRui Paulo static int
128132a8088fSRui Paulo asmc_sms_read(device_t dev, const char *key, int16_t *val)
128232a8088fSRui Paulo {
128332a8088fSRui Paulo 	uint8_t buf[2];
128432a8088fSRui Paulo 	int error;
128532a8088fSRui Paulo 
128632a8088fSRui Paulo 	/* no need to do locking here as asmc_key_read() already does it */
128732a8088fSRui Paulo 	switch (key[3]) {
128832a8088fSRui Paulo 	case 'X':
128932a8088fSRui Paulo 	case 'Y':
129032a8088fSRui Paulo 	case 'Z':
1291447666f0SRui Paulo 		error =	asmc_key_read(dev, key, buf, sizeof buf);
129232a8088fSRui Paulo 		break;
129332a8088fSRui Paulo 	default:
129432a8088fSRui Paulo 		device_printf(dev, "%s called with invalid argument %s\n",
129532a8088fSRui Paulo 			      __func__, key);
129632a8088fSRui Paulo 		error = 1;
129732a8088fSRui Paulo 		goto out;
129832a8088fSRui Paulo 	}
129932a8088fSRui Paulo 	*val = ((int16_t)buf[0] << 8) | buf[1];
130032a8088fSRui Paulo out:
130132a8088fSRui Paulo 	return (error);
130232a8088fSRui Paulo }
130332a8088fSRui Paulo 
130432a8088fSRui Paulo static void
130532a8088fSRui Paulo asmc_sms_calibrate(device_t dev)
130632a8088fSRui Paulo {
130732a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
130832a8088fSRui Paulo 
130932a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
131032a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
131132a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
131232a8088fSRui Paulo }
131332a8088fSRui Paulo 
131432a8088fSRui Paulo static int
131532a8088fSRui Paulo asmc_sms_intrfast(void *arg)
131632a8088fSRui Paulo {
131732a8088fSRui Paulo 	uint8_t type;
131832a8088fSRui Paulo 	device_t dev = (device_t) arg;
131932a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
13201269f4d4SRui Paulo 	if (!sc->sc_sms_intr_works)
13211269f4d4SRui Paulo 		return (FILTER_HANDLED);
132232a8088fSRui Paulo 
132332a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
13244470f0f3SRui Paulo 	type = ASMC_INTPORT_READ(sc);
132532a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
132632a8088fSRui Paulo 
132732a8088fSRui Paulo 	sc->sc_sms_intrtype = type;
132832a8088fSRui Paulo 	asmc_sms_printintr(dev, type);
132932a8088fSRui Paulo 
133032a8088fSRui Paulo 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
133132a8088fSRui Paulo 	return (FILTER_HANDLED);
133232a8088fSRui Paulo }
133332a8088fSRui Paulo 
133432a8088fSRui Paulo static void
133532a8088fSRui Paulo asmc_sms_printintr(device_t dev, uint8_t type)
133632a8088fSRui Paulo {
133732a8088fSRui Paulo 
133832a8088fSRui Paulo 	switch (type) {
133932a8088fSRui Paulo 	case ASMC_SMS_INTFF:
134032a8088fSRui Paulo 		device_printf(dev, "WARNING: possible free fall!\n");
134132a8088fSRui Paulo 		break;
134232a8088fSRui Paulo 	case ASMC_SMS_INTHA:
134332a8088fSRui Paulo 		device_printf(dev, "WARNING: high acceleration detected!\n");
134432a8088fSRui Paulo 		break;
134532a8088fSRui Paulo 	case ASMC_SMS_INTSH:
134632a8088fSRui Paulo 		device_printf(dev, "WARNING: possible shock!\n");
134732a8088fSRui Paulo 		break;
134832a8088fSRui Paulo 	default:
134932a8088fSRui Paulo 		device_printf(dev, "%s unknown interrupt\n", __func__);
135032a8088fSRui Paulo 	}
135132a8088fSRui Paulo }
135232a8088fSRui Paulo 
135332a8088fSRui Paulo static void
135432a8088fSRui Paulo asmc_sms_task(void *arg, int pending)
135532a8088fSRui Paulo {
135632a8088fSRui Paulo 	struct asmc_softc *sc = (struct asmc_softc *)arg;
135732a8088fSRui Paulo 	char notify[16];
135832a8088fSRui Paulo 	int type;
135932a8088fSRui Paulo 
136032a8088fSRui Paulo 	switch (sc->sc_sms_intrtype) {
136132a8088fSRui Paulo 	case ASMC_SMS_INTFF:
136232a8088fSRui Paulo 		type = 2;
136332a8088fSRui Paulo 		break;
136432a8088fSRui Paulo 	case ASMC_SMS_INTHA:
136532a8088fSRui Paulo 		type = 1;
136632a8088fSRui Paulo 		break;
136732a8088fSRui Paulo 	case ASMC_SMS_INTSH:
136832a8088fSRui Paulo 		type = 0;
136932a8088fSRui Paulo 		break;
137032a8088fSRui Paulo 	default:
137132a8088fSRui Paulo 		type = 255;
137232a8088fSRui Paulo 	}
137332a8088fSRui Paulo 
137432a8088fSRui Paulo 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
13754470f0f3SRui Paulo 	devctl_notify("ACPI", "asmc", "SMS", notify);
137632a8088fSRui Paulo }
137732a8088fSRui Paulo 
137832a8088fSRui Paulo static int
137932a8088fSRui Paulo asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
138032a8088fSRui Paulo {
138132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
138232a8088fSRui Paulo 	int error;
138332a8088fSRui Paulo 	int16_t val;
138432a8088fSRui Paulo 	int32_t v;
138532a8088fSRui Paulo 
138632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
138732a8088fSRui Paulo 	v = (int32_t) val;
138832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
138932a8088fSRui Paulo 
139032a8088fSRui Paulo 	return (error);
139132a8088fSRui Paulo }
139232a8088fSRui Paulo 
139332a8088fSRui Paulo static int
139432a8088fSRui Paulo asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
139532a8088fSRui Paulo {
139632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
139732a8088fSRui Paulo 	int error;
139832a8088fSRui Paulo 	int16_t val;
139932a8088fSRui Paulo 	int32_t v;
140032a8088fSRui Paulo 
140132a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
140232a8088fSRui Paulo 	v = (int32_t) val;
140332a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
140432a8088fSRui Paulo 
140532a8088fSRui Paulo 	return (error);
140632a8088fSRui Paulo }
140732a8088fSRui Paulo 
140832a8088fSRui Paulo static int
140932a8088fSRui Paulo asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
141032a8088fSRui Paulo {
141132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
141232a8088fSRui Paulo 	int error;
141332a8088fSRui Paulo 	int16_t val;
141432a8088fSRui Paulo 	int32_t v;
141532a8088fSRui Paulo 
141632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
141732a8088fSRui Paulo 	v = (int32_t) val;
14180e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
141932a8088fSRui Paulo 
142032a8088fSRui Paulo 	return (error);
142132a8088fSRui Paulo }
142232a8088fSRui Paulo 
142332a8088fSRui Paulo static int
142432a8088fSRui Paulo asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
142532a8088fSRui Paulo {
142632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
142732a8088fSRui Paulo 	uint8_t buf[6];
142832a8088fSRui Paulo 	int error;
142932a8088fSRui Paulo 	int32_t v;
143032a8088fSRui Paulo 
1431447666f0SRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
143232a8088fSRui Paulo 	v = buf[2];
14330e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
143432a8088fSRui Paulo 
143532a8088fSRui Paulo 	return (error);
143632a8088fSRui Paulo }
143732a8088fSRui Paulo 
143832a8088fSRui Paulo static int
143932a8088fSRui Paulo asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
144032a8088fSRui Paulo {
144132a8088fSRui Paulo 	device_t dev = (device_t) arg1;
144232a8088fSRui Paulo 	uint8_t buf[6];
144332a8088fSRui Paulo 	int error;
144432a8088fSRui Paulo 	int32_t v;
144532a8088fSRui Paulo 
1446447666f0SRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
144732a8088fSRui Paulo 	v = buf[2];
14480e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
1449be80e49aSRui Paulo 
1450be80e49aSRui Paulo 	return (error);
1451be80e49aSRui Paulo }
1452be80e49aSRui Paulo 
1453be80e49aSRui Paulo static int
1454be80e49aSRui Paulo asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1455be80e49aSRui Paulo {
1456be80e49aSRui Paulo 	device_t dev = (device_t) arg1;
1457be80e49aSRui Paulo 	uint8_t buf[2];
1458be80e49aSRui Paulo 	int error;
14590e1152fcSHans Petter Selasky 	int v;
1460be80e49aSRui Paulo 
1461108e3076SAdrian Chadd 	v = light_control;
14620e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
14630e1152fcSHans Petter Selasky 
14640e1152fcSHans Petter Selasky 	if (error == 0 && req->newptr != NULL) {
14650e1152fcSHans Petter Selasky 		if (v < 0 || v > 255)
14660e1152fcSHans Petter Selasky 			return (EINVAL);
1467108e3076SAdrian Chadd 		light_control = v;
1468108e3076SAdrian Chadd 		buf[0] = light_control;
146932a8088fSRui Paulo 		buf[1] = 0x00;
1470447666f0SRui Paulo 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
147132a8088fSRui Paulo 	}
147232a8088fSRui Paulo 	return (error);
147332a8088fSRui Paulo }
1474