xref: /freebsd/sys/dev/asmc/asmc.c (revision 9c325393d5362fc4a9810c25810ab5ae5caf0216)
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 	{
344108e3076SAdrian Chadd 	  "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
345108e3076SAdrian Chadd 	  ASMC_SMS_FUNCS_DISABLED,
346108e3076SAdrian Chadd 	  ASMC_FAN_FUNCS2,
347108e3076SAdrian Chadd 	  ASMC_LIGHT_FUNCS,
348108e3076SAdrian Chadd 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
349108e3076SAdrian Chadd 	},
350108e3076SAdrian Chadd 
351108e3076SAdrian Chadd 	{
352108e3076SAdrian Chadd 	  "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
353108e3076SAdrian Chadd 	  ASMC_SMS_FUNCS_DISABLED,
354108e3076SAdrian Chadd 	  ASMC_FAN_FUNCS2,
355108e3076SAdrian Chadd 	  ASMC_LIGHT_FUNCS,
356108e3076SAdrian Chadd 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
357108e3076SAdrian Chadd 	},
358108e3076SAdrian Chadd 
359081954d3SDavid Bright 	{
360081954d3SDavid Bright 	  "MacBookAir7,1", "Apple SMC MacBook Air 11-inch (Early 2015)",
361081954d3SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED,
362081954d3SDavid Bright 	  ASMC_FAN_FUNCS2,
363081954d3SDavid Bright 	  ASMC_LIGHT_FUNCS,
364081954d3SDavid Bright 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
365081954d3SDavid Bright 	},
366081954d3SDavid Bright 
367081954d3SDavid Bright 	{
368081954d3SDavid Bright 	  "MacBookAir7,2", "Apple SMC MacBook Air 13-inch (Early 2015)",
369081954d3SDavid Bright 	  ASMC_SMS_FUNCS_DISABLED,
370081954d3SDavid Bright 	  ASMC_FAN_FUNCS2,
371081954d3SDavid Bright 	  ASMC_LIGHT_FUNCS,
372081954d3SDavid Bright 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
373081954d3SDavid Bright 	},
374d8246db0SRui Paulo 
37532a8088fSRui Paulo 	{ NULL, NULL }
37632a8088fSRui Paulo };
37732a8088fSRui Paulo 
37832a8088fSRui Paulo #undef ASMC_SMS_FUNCS
379108e3076SAdrian Chadd #undef ASMC_SMS_FUNCS_DISABLED
38032a8088fSRui Paulo #undef ASMC_FAN_FUNCS
381108e3076SAdrian Chadd #undef ASMC_FAN_FUNCS2
38232a8088fSRui Paulo #undef ASMC_LIGHT_FUNCS
38332a8088fSRui Paulo 
38432a8088fSRui Paulo /*
38532a8088fSRui Paulo  * Driver methods.
38632a8088fSRui Paulo  */
38732a8088fSRui Paulo static device_method_t	asmc_methods[] = {
38832a8088fSRui Paulo 	DEVMETHOD(device_probe,		asmc_probe),
38932a8088fSRui Paulo 	DEVMETHOD(device_attach,	asmc_attach),
39032a8088fSRui Paulo 	DEVMETHOD(device_detach,	asmc_detach),
391108e3076SAdrian Chadd 	DEVMETHOD(device_resume,	asmc_resume),
39232a8088fSRui Paulo 
39332a8088fSRui Paulo 	{ 0, 0 }
39432a8088fSRui Paulo };
39532a8088fSRui Paulo 
39632a8088fSRui Paulo static driver_t	asmc_driver = {
39732a8088fSRui Paulo 	"asmc",
39832a8088fSRui Paulo 	asmc_methods,
39932a8088fSRui Paulo 	sizeof(struct asmc_softc)
40032a8088fSRui Paulo };
40132a8088fSRui Paulo 
4024470f0f3SRui Paulo /*
4034470f0f3SRui Paulo  * Debugging
4044470f0f3SRui Paulo  */
4054470f0f3SRui Paulo #define	_COMPONENT	ACPI_OEM
4064470f0f3SRui Paulo ACPI_MODULE_NAME("ASMC")
4074470f0f3SRui Paulo #ifdef DEBUG
4084470f0f3SRui Paulo #define ASMC_DPRINTF(str)	device_printf(dev, str)
4094fb9bf66SRui Paulo #else
4104fb9bf66SRui Paulo #define ASMC_DPRINTF(str)
4114470f0f3SRui Paulo #endif
4124470f0f3SRui Paulo 
413be80e49aSRui Paulo /* NB: can't be const */
4144470f0f3SRui Paulo static char *asmc_ids[] = { "APP0001", NULL };
4154470f0f3SRui Paulo 
41632a8088fSRui Paulo static devclass_t asmc_devclass;
41732a8088fSRui Paulo 
418108e3076SAdrian Chadd static unsigned int light_control = 0;
419108e3076SAdrian Chadd 
4204470f0f3SRui Paulo DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
4214470f0f3SRui Paulo MODULE_DEPEND(asmc, acpi, 1, 1, 1);
42232a8088fSRui Paulo 
42332a8088fSRui Paulo static struct asmc_model *
42432a8088fSRui Paulo asmc_match(device_t dev)
42532a8088fSRui Paulo {
42632a8088fSRui Paulo 	int i;
42732a8088fSRui Paulo 	char *model;
42832a8088fSRui Paulo 
4292be111bfSDavide Italiano 	model = kern_getenv("smbios.system.product");
43047105877SRui Paulo 	if (model == NULL)
43147105877SRui Paulo 		return (NULL);
43247105877SRui Paulo 
43332a8088fSRui Paulo 	for (i = 0; asmc_models[i].smc_model; i++) {
43432a8088fSRui Paulo 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
43532a8088fSRui Paulo 			freeenv(model);
43632a8088fSRui Paulo 			return (&asmc_models[i]);
43732a8088fSRui Paulo 		}
43832a8088fSRui Paulo 	}
43932a8088fSRui Paulo 	freeenv(model);
44032a8088fSRui Paulo 
44132a8088fSRui Paulo 	return (NULL);
44232a8088fSRui Paulo }
44332a8088fSRui Paulo 
44432a8088fSRui Paulo static int
44532a8088fSRui Paulo asmc_probe(device_t dev)
44632a8088fSRui Paulo {
44732a8088fSRui Paulo 	struct asmc_model *model;
4485efca36fSTakanori Watanabe 	int rv;
44932a8088fSRui Paulo 
450a8de37b0SEitan Adler 	if (resource_disabled("asmc", 0))
451a8de37b0SEitan Adler 		return (ENXIO);
4525efca36fSTakanori Watanabe 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
4535efca36fSTakanori Watanabe 	if (rv > 0)
4545efca36fSTakanori Watanabe 		return (rv);
4554470f0f3SRui Paulo 
45632a8088fSRui Paulo 	model = asmc_match(dev);
4574470f0f3SRui Paulo 	if (!model) {
4584470f0f3SRui Paulo 		device_printf(dev, "model not recognized\n");
45932a8088fSRui Paulo 		return (ENXIO);
4604470f0f3SRui Paulo 	}
46132a8088fSRui Paulo 	device_set_desc(dev, model->smc_desc);
46232a8088fSRui Paulo 
4635efca36fSTakanori Watanabe 	return (rv);
46432a8088fSRui Paulo }
46532a8088fSRui Paulo 
46632a8088fSRui Paulo static int
46732a8088fSRui Paulo asmc_attach(device_t dev)
46832a8088fSRui Paulo {
46932a8088fSRui Paulo 	int i, j;
47032a8088fSRui Paulo 	int ret;
47132a8088fSRui Paulo 	char name[2];
47232a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
47332a8088fSRui Paulo 	struct sysctl_ctx_list *sysctlctx;
47432a8088fSRui Paulo 	struct sysctl_oid *sysctlnode;
47532a8088fSRui Paulo 	struct asmc_model *model;
47632a8088fSRui Paulo 
4774470f0f3SRui Paulo 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
4784470f0f3SRui Paulo 	    &sc->sc_rid_port, RF_ACTIVE);
4794470f0f3SRui Paulo 	if (sc->sc_ioport == NULL) {
4804470f0f3SRui Paulo 		device_printf(dev, "unable to allocate IO port\n");
4814470f0f3SRui Paulo 		return (ENOMEM);
4824470f0f3SRui Paulo 	}
4834470f0f3SRui Paulo 
48432a8088fSRui Paulo 	sysctlctx  = device_get_sysctl_ctx(dev);
48532a8088fSRui Paulo 	sysctlnode = device_get_sysctl_tree(dev);
48632a8088fSRui Paulo 
48732a8088fSRui Paulo 	model = asmc_match(dev);
48832a8088fSRui Paulo 
48932a8088fSRui Paulo 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
49032a8088fSRui Paulo 
49132a8088fSRui Paulo 	sc->sc_model = model;
49232a8088fSRui Paulo 	asmc_init(dev);
49332a8088fSRui Paulo 
49432a8088fSRui Paulo 	/*
49532a8088fSRui Paulo 	 * dev.asmc.n.fan.* tree.
49632a8088fSRui Paulo 	 */
49732a8088fSRui Paulo 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
49832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
4997029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Fan Root Tree");
50032a8088fSRui Paulo 
50132a8088fSRui Paulo 	for (i = 1; i <= sc->sc_nfan; i++) {
50232a8088fSRui Paulo 		j = i - 1;
50332a8088fSRui Paulo 		name[0] = '0' + j;
50432a8088fSRui Paulo 		name[1] = 0;
50532a8088fSRui Paulo 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
50632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
5077029da5cSPawel Biernacki 		    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
50832a8088fSRui Paulo 		    "Fan Subtree");
50932a8088fSRui Paulo 
51032a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
51132a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
5127029da5cSPawel Biernacki 		    OID_AUTO, "id",
5137029da5cSPawel Biernacki 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
514447666f0SRui Paulo 		    dev, j, model->smc_fan_id, "I",
515447666f0SRui Paulo 		    "Fan ID");
516447666f0SRui Paulo 
517447666f0SRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
518447666f0SRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
5197029da5cSPawel Biernacki 		    OID_AUTO, "speed",
5207029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
52132a8088fSRui Paulo 		    dev, j, model->smc_fan_speed, "I",
52232a8088fSRui Paulo 		    "Fan speed in RPM");
52332a8088fSRui Paulo 
52432a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
52532a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
52632a8088fSRui Paulo 		    OID_AUTO, "safespeed",
5277029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
52832a8088fSRui Paulo 		    dev, j, model->smc_fan_safespeed, "I",
52932a8088fSRui Paulo 		    "Fan safe speed in RPM");
53032a8088fSRui Paulo 
53132a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
53232a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
53332a8088fSRui Paulo 		    OID_AUTO, "minspeed",
5347029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
53532a8088fSRui Paulo 		    dev, j, model->smc_fan_minspeed, "I",
53632a8088fSRui Paulo 		    "Fan minimum speed in RPM");
53732a8088fSRui Paulo 
53832a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
53932a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
54032a8088fSRui Paulo 		    OID_AUTO, "maxspeed",
5417029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
54232a8088fSRui Paulo 		    dev, j, model->smc_fan_maxspeed, "I",
54332a8088fSRui Paulo 		    "Fan maximum speed in RPM");
54432a8088fSRui Paulo 
54532a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
54632a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
54732a8088fSRui Paulo 		    OID_AUTO, "targetspeed",
5487029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
54932a8088fSRui Paulo 		    dev, j, model->smc_fan_targetspeed, "I",
55032a8088fSRui Paulo 		    "Fan target speed in RPM");
55132a8088fSRui Paulo 	}
55232a8088fSRui Paulo 
55332a8088fSRui Paulo 	/*
55432a8088fSRui Paulo 	 * dev.asmc.n.temp tree.
55532a8088fSRui Paulo 	 */
55632a8088fSRui Paulo 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
55732a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
5587029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Temperature sensors");
55932a8088fSRui Paulo 
56032a8088fSRui Paulo 	for (i = 0; model->smc_temps[i]; i++) {
56132a8088fSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
56232a8088fSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
56332a8088fSRui Paulo 		    OID_AUTO, model->smc_tempnames[i],
5647029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
56532a8088fSRui Paulo 		    dev, i, asmc_temp_sysctl, "I",
56632a8088fSRui Paulo 		    model->smc_tempdescs[i]);
56732a8088fSRui Paulo 	}
56832a8088fSRui Paulo 
569be80e49aSRui Paulo 	/*
570be80e49aSRui Paulo 	 * dev.asmc.n.light
571be80e49aSRui Paulo 	 */
572be80e49aSRui Paulo 	if (model->smc_light_left) {
573be80e49aSRui Paulo 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
574be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
5757029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
5767029da5cSPawel Biernacki 		    "Keyboard backlight sensors");
577be80e49aSRui Paulo 
578be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
579be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
5807029da5cSPawel Biernacki 		    OID_AUTO, "left",
5817029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
582be80e49aSRui Paulo 		    dev, 0, model->smc_light_left, "I",
583be80e49aSRui Paulo 		    "Keyboard backlight left sensor");
584be80e49aSRui Paulo 
585be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
586be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
5877029da5cSPawel Biernacki 		    OID_AUTO, "right",
5887029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
589be80e49aSRui Paulo 		    dev, 0, model->smc_light_right, "I",
590be80e49aSRui Paulo 		    "Keyboard backlight right sensor");
591be80e49aSRui Paulo 
592be80e49aSRui Paulo 		SYSCTL_ADD_PROC(sysctlctx,
593be80e49aSRui Paulo 		    SYSCTL_CHILDREN(sc->sc_light_tree),
5943471c35dSRui Paulo 		    OID_AUTO, "control",
5957029da5cSPawel Biernacki 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY |
5967029da5cSPawel Biernacki 		    CTLFLAG_NEEDGIANT, dev, 0,
5977029da5cSPawel Biernacki 		    model->smc_light_control, "I",
598be80e49aSRui Paulo 		    "Keyboard backlight brightness control");
599be80e49aSRui Paulo 	}
600be80e49aSRui Paulo 
60132a8088fSRui Paulo 	if (model->smc_sms_x == NULL)
60232a8088fSRui Paulo 		goto nosms;
60332a8088fSRui Paulo 
60432a8088fSRui Paulo 	/*
60532a8088fSRui Paulo 	 * dev.asmc.n.sms tree.
60632a8088fSRui Paulo 	 */
60732a8088fSRui Paulo 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
60832a8088fSRui Paulo 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
6097029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Sudden Motion Sensor");
61032a8088fSRui Paulo 
61132a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
61232a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6137029da5cSPawel Biernacki 	    OID_AUTO, "x",
6147029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
61532a8088fSRui Paulo 	    dev, 0, model->smc_sms_x, "I",
61632a8088fSRui Paulo 	    "Sudden Motion Sensor X value");
61732a8088fSRui Paulo 
61832a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
61932a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6207029da5cSPawel Biernacki 	    OID_AUTO, "y",
6217029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
62232a8088fSRui Paulo 	    dev, 0, model->smc_sms_y, "I",
62332a8088fSRui Paulo 	    "Sudden Motion Sensor Y value");
62432a8088fSRui Paulo 
62532a8088fSRui Paulo 	SYSCTL_ADD_PROC(sysctlctx,
62632a8088fSRui Paulo 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
6277029da5cSPawel Biernacki 	    OID_AUTO, "z",
6287029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
62932a8088fSRui Paulo 	    dev, 0, model->smc_sms_z, "I",
63032a8088fSRui Paulo 	    "Sudden Motion Sensor Z value");
63132a8088fSRui Paulo 
63232a8088fSRui Paulo 	/*
63332a8088fSRui Paulo 	 * Need a taskqueue to send devctl_notify() events
63432a8088fSRui Paulo 	 * when the SMS interrupt us.
63532a8088fSRui Paulo 	 *
63632a8088fSRui Paulo 	 * PI_REALTIME is used due to the sensitivity of the
63732a8088fSRui Paulo 	 * interrupt. An interrupt from the SMS means that the
63832a8088fSRui Paulo 	 * disk heads should be turned off as quickly as possible.
63932a8088fSRui Paulo 	 *
64032a8088fSRui Paulo 	 * We only need to do this for the non INTR_FILTER case.
64132a8088fSRui Paulo 	 */
64232a8088fSRui Paulo 	sc->sc_sms_tq = NULL;
64332a8088fSRui Paulo 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
64432a8088fSRui Paulo 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
64532a8088fSRui Paulo 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
64632a8088fSRui Paulo 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
64732a8088fSRui Paulo 	    device_get_nameunit(dev));
64832a8088fSRui Paulo 	/*
64932a8088fSRui Paulo 	 * Allocate an IRQ for the SMS.
65032a8088fSRui Paulo 	 */
6514470f0f3SRui Paulo 	sc->sc_rid_irq = 0;
6524470f0f3SRui Paulo 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
6534470f0f3SRui Paulo 	    &sc->sc_rid_irq, RF_ACTIVE);
6544470f0f3SRui Paulo 	if (sc->sc_irq == NULL) {
65532a8088fSRui Paulo 		device_printf(dev, "unable to allocate IRQ resource\n");
65632a8088fSRui Paulo 		ret = ENXIO;
65732a8088fSRui Paulo 		goto err2;
65832a8088fSRui Paulo 	}
65932a8088fSRui Paulo 
6604470f0f3SRui Paulo 	ret = bus_setup_intr(dev, sc->sc_irq,
66132a8088fSRui Paulo 	          INTR_TYPE_MISC | INTR_MPSAFE,
66232a8088fSRui Paulo 	    asmc_sms_intrfast, NULL,
66332a8088fSRui Paulo 	    dev, &sc->sc_cookie);
66432a8088fSRui Paulo 
66532a8088fSRui Paulo 	if (ret) {
66632a8088fSRui Paulo 		device_printf(dev, "unable to setup SMS IRQ\n");
66732a8088fSRui Paulo 		goto err1;
66832a8088fSRui Paulo 	}
66932a8088fSRui Paulo nosms:
67032a8088fSRui Paulo 	return (0);
67132a8088fSRui Paulo err1:
6724470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
67332a8088fSRui Paulo err2:
6744470f0f3SRui Paulo 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
6754470f0f3SRui Paulo 	    sc->sc_ioport);
67632a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
67732a8088fSRui Paulo 	if (sc->sc_sms_tq)
67832a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
67932a8088fSRui Paulo 
68032a8088fSRui Paulo 	return (ret);
68132a8088fSRui Paulo }
68232a8088fSRui Paulo 
68332a8088fSRui Paulo static int
68432a8088fSRui Paulo asmc_detach(device_t dev)
68532a8088fSRui Paulo {
68632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
68732a8088fSRui Paulo 
68832a8088fSRui Paulo 	if (sc->sc_sms_tq) {
68932a8088fSRui Paulo 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
69032a8088fSRui Paulo 		taskqueue_free(sc->sc_sms_tq);
69132a8088fSRui Paulo 	}
69232a8088fSRui Paulo 	if (sc->sc_cookie)
6934470f0f3SRui Paulo 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
6944470f0f3SRui Paulo 	if (sc->sc_irq)
6954470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
6964470f0f3SRui Paulo 		    sc->sc_irq);
6974470f0f3SRui Paulo 	if (sc->sc_ioport)
6984470f0f3SRui Paulo 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
6994470f0f3SRui Paulo 		    sc->sc_ioport);
70032a8088fSRui Paulo 	mtx_destroy(&sc->sc_mtx);
70132a8088fSRui Paulo 
70232a8088fSRui Paulo 	return (0);
70332a8088fSRui Paulo }
70432a8088fSRui Paulo 
705108e3076SAdrian Chadd static int
706108e3076SAdrian Chadd asmc_resume(device_t dev)
707108e3076SAdrian Chadd {
708108e3076SAdrian Chadd     uint8_t buf[2];
709108e3076SAdrian Chadd     buf[0] = light_control;
710108e3076SAdrian Chadd     buf[1] = 0x00;
711108e3076SAdrian Chadd     asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
712108e3076SAdrian Chadd     return (0);
713108e3076SAdrian Chadd }
714108e3076SAdrian Chadd 
715108e3076SAdrian Chadd 
7161269f4d4SRui Paulo #ifdef DEBUG
7171269f4d4SRui Paulo void asmc_dumpall(device_t dev)
7181269f4d4SRui Paulo {
7191269f4d4SRui Paulo 	int i;
7201269f4d4SRui Paulo 
7211269f4d4SRui Paulo 	/* XXX magic number */
7221269f4d4SRui Paulo 	for (i=0; i < 0x100; i++)
7231269f4d4SRui Paulo 		asmc_key_dump(dev, i);
7241269f4d4SRui Paulo }
7251269f4d4SRui Paulo #endif
7261269f4d4SRui Paulo 
72732a8088fSRui Paulo static int
72832a8088fSRui Paulo asmc_init(device_t dev)
72932a8088fSRui Paulo {
73032a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
73132a8088fSRui Paulo 	int i, error = 1;
73232a8088fSRui Paulo 	uint8_t buf[4];
73332a8088fSRui Paulo 
73432a8088fSRui Paulo 	if (sc->sc_model->smc_sms_x == NULL)
73532a8088fSRui Paulo 		goto nosms;
73632a8088fSRui Paulo 
73732a8088fSRui Paulo 	/*
738453130d9SPedro F. Giffuni 	 * We are ready to receive interrupts from the SMS.
73932a8088fSRui Paulo 	 */
74032a8088fSRui Paulo 	buf[0] = 0x01;
7414470f0f3SRui Paulo 	ASMC_DPRINTF(("intok key\n"));
74232a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
74332a8088fSRui Paulo 	DELAY(50);
74432a8088fSRui Paulo 
74532a8088fSRui Paulo 	/*
74632a8088fSRui Paulo 	 * Initiate the polling intervals.
74732a8088fSRui Paulo 	 */
74832a8088fSRui Paulo 	buf[0] = 20; /* msecs */
7494470f0f3SRui Paulo 	ASMC_DPRINTF(("low int key\n"));
75032a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
75132a8088fSRui Paulo 	DELAY(200);
75232a8088fSRui Paulo 
75332a8088fSRui Paulo 	buf[0] = 20; /* msecs */
7544470f0f3SRui Paulo 	ASMC_DPRINTF(("high int key\n"));
75532a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
75632a8088fSRui Paulo 	DELAY(200);
75732a8088fSRui Paulo 
75832a8088fSRui Paulo 	buf[0] = 0x00;
75932a8088fSRui Paulo 	buf[1] = 0x60;
7604470f0f3SRui Paulo 	ASMC_DPRINTF(("sms low key\n"));
76132a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
76232a8088fSRui Paulo 	DELAY(200);
76332a8088fSRui Paulo 
76432a8088fSRui Paulo 	buf[0] = 0x01;
76532a8088fSRui Paulo 	buf[1] = 0xc0;
7664470f0f3SRui Paulo 	ASMC_DPRINTF(("sms high key\n"));
76732a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
76832a8088fSRui Paulo 	DELAY(200);
76932a8088fSRui Paulo 
77032a8088fSRui Paulo 	/*
77132a8088fSRui Paulo 	 * I'm not sure what this key does, but it seems to be
77232a8088fSRui Paulo 	 * required.
77332a8088fSRui Paulo 	 */
77432a8088fSRui Paulo 	buf[0] = 0x01;
7754470f0f3SRui Paulo 	ASMC_DPRINTF(("sms flag key\n"));
77632a8088fSRui Paulo 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
777b75dfbe8SRui Paulo 	DELAY(100);
77832a8088fSRui Paulo 
7791269f4d4SRui Paulo 	sc->sc_sms_intr_works = 0;
7801269f4d4SRui Paulo 
78132a8088fSRui Paulo 	/*
7821269f4d4SRui Paulo 	 * Retry SMS initialization 1000 times
7831269f4d4SRui Paulo 	 * (takes approx. 2 seconds in worst case)
78432a8088fSRui Paulo 	 */
7851269f4d4SRui Paulo 	for (i = 0; i < 1000; i++) {
78632a8088fSRui Paulo 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
7871269f4d4SRui Paulo 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
78832a8088fSRui Paulo 			error = 0;
7891269f4d4SRui Paulo 			sc->sc_sms_intr_works = 1;
7904fb9bf66SRui Paulo 			goto out;
79132a8088fSRui Paulo 		}
79232a8088fSRui Paulo 		buf[0] = ASMC_SMS_INIT1;
79332a8088fSRui Paulo 		buf[1] = ASMC_SMS_INIT2;
7944470f0f3SRui Paulo 		ASMC_DPRINTF(("sms key\n"));
79532a8088fSRui Paulo 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
79632a8088fSRui Paulo 		DELAY(50);
79732a8088fSRui Paulo 	}
7984fb9bf66SRui Paulo 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
79932a8088fSRui Paulo 
8004fb9bf66SRui Paulo out:
80132a8088fSRui Paulo 	asmc_sms_calibrate(dev);
80232a8088fSRui Paulo nosms:
80332a8088fSRui Paulo 	sc->sc_nfan = asmc_fan_count(dev);
80432a8088fSRui Paulo 	if (sc->sc_nfan > ASMC_MAXFANS) {
80532a8088fSRui Paulo 		device_printf(dev, "more than %d fans were detected. Please "
80632a8088fSRui Paulo 		    "report this.\n", ASMC_MAXFANS);
80732a8088fSRui Paulo 		sc->sc_nfan = ASMC_MAXFANS;
80832a8088fSRui Paulo 	}
80932a8088fSRui Paulo 
81032a8088fSRui Paulo 	if (bootverbose) {
81132a8088fSRui Paulo 		/*
812447666f0SRui Paulo 		 * The number of keys is a 32 bit buffer
81332a8088fSRui Paulo 		 */
81432a8088fSRui Paulo 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
815447666f0SRui Paulo 		device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
81632a8088fSRui Paulo 	}
81732a8088fSRui Paulo 
8181269f4d4SRui Paulo #ifdef DEBUG
8191269f4d4SRui Paulo 	asmc_dumpall(dev);
8201269f4d4SRui Paulo #endif
8211269f4d4SRui Paulo 
82232a8088fSRui Paulo 	return (error);
82332a8088fSRui Paulo }
82432a8088fSRui Paulo 
82532a8088fSRui Paulo /*
82632a8088fSRui Paulo  * We need to make sure that the SMC acks the byte sent.
827be80e49aSRui Paulo  * Just wait up to (amount * 10)  ms.
82832a8088fSRui Paulo  */
82932a8088fSRui Paulo static int
830be80e49aSRui Paulo asmc_wait_ack(device_t dev, uint8_t val, int amount)
83132a8088fSRui Paulo {
8324470f0f3SRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
83332a8088fSRui Paulo 	u_int i;
83432a8088fSRui Paulo 
83532a8088fSRui Paulo 	val = val & ASMC_STATUS_MASK;
83632a8088fSRui Paulo 
837be80e49aSRui Paulo 	for (i = 0; i < amount; i++) {
8384470f0f3SRui Paulo 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
83932a8088fSRui Paulo 			return (0);
84032a8088fSRui Paulo 		DELAY(10);
84132a8088fSRui Paulo 	}
84232a8088fSRui Paulo 
843be80e49aSRui Paulo 	return (1);
844be80e49aSRui Paulo }
845be80e49aSRui Paulo 
846be80e49aSRui Paulo /*
847be80e49aSRui Paulo  * We need to make sure that the SMC acks the byte sent.
848be80e49aSRui Paulo  * Just wait up to 100 ms.
849be80e49aSRui Paulo  */
850be80e49aSRui Paulo static int
851be80e49aSRui Paulo asmc_wait(device_t dev, uint8_t val)
852be80e49aSRui Paulo {
853be80e49aSRui Paulo 	struct asmc_softc *sc;
854be80e49aSRui Paulo 
855be80e49aSRui Paulo 	if (asmc_wait_ack(dev, val, 1000) == 0)
856be80e49aSRui Paulo 		return (0);
857be80e49aSRui Paulo 
858be80e49aSRui Paulo 	sc = device_get_softc(dev);
859be80e49aSRui Paulo 	val = val & ASMC_STATUS_MASK;
860be80e49aSRui Paulo 
861be80e49aSRui Paulo #ifdef DEBUG
86232a8088fSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
8634470f0f3SRui Paulo 	    ASMC_CMDPORT_READ(sc));
864be80e49aSRui Paulo #endif
865be80e49aSRui Paulo 	return (1);
866be80e49aSRui Paulo }
86732a8088fSRui Paulo 
868be80e49aSRui Paulo /*
869be80e49aSRui Paulo  * Send the given command, retrying up to 10 times if
870be80e49aSRui Paulo  * the acknowledgement fails.
871be80e49aSRui Paulo  */
872be80e49aSRui Paulo static int
873be80e49aSRui Paulo asmc_command(device_t dev, uint8_t command) {
874be80e49aSRui Paulo 
875be80e49aSRui Paulo 	int i;
876be80e49aSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
877be80e49aSRui Paulo 
878be80e49aSRui Paulo 	for (i=0; i < 10; i++) {
879be80e49aSRui Paulo 		ASMC_CMDPORT_WRITE(sc, command);
880be80e49aSRui Paulo 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
881be80e49aSRui Paulo 			return (0);
882be80e49aSRui Paulo 		}
883be80e49aSRui Paulo 	}
884be80e49aSRui Paulo 
885be80e49aSRui Paulo #ifdef DEBUG
886be80e49aSRui Paulo 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
887be80e49aSRui Paulo 	    ASMC_CMDPORT_READ(sc));
888be80e49aSRui Paulo #endif
88932a8088fSRui Paulo 	return (1);
89032a8088fSRui Paulo }
89132a8088fSRui Paulo 
89232a8088fSRui Paulo static int
89332a8088fSRui Paulo asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
89432a8088fSRui Paulo {
895be80e49aSRui Paulo 	int i, error = 1, try = 0;
89632a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
89732a8088fSRui Paulo 
89832a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
89932a8088fSRui Paulo 
900be80e49aSRui Paulo begin:
901be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDREAD))
90232a8088fSRui Paulo 		goto out;
90332a8088fSRui Paulo 
90432a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
9054470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
90632a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
90732a8088fSRui Paulo 			goto out;
90832a8088fSRui Paulo 	}
90932a8088fSRui Paulo 
9104470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
91132a8088fSRui Paulo 
91232a8088fSRui Paulo 	for (i = 0; i < len; i++) {
91332a8088fSRui Paulo 		if (asmc_wait(dev, 0x05))
91432a8088fSRui Paulo 			goto out;
9154470f0f3SRui Paulo 		buf[i] = ASMC_DATAPORT_READ(sc);
91632a8088fSRui Paulo 	}
91732a8088fSRui Paulo 
91832a8088fSRui Paulo 	error = 0;
91932a8088fSRui Paulo out:
920be80e49aSRui Paulo 	if (error) {
921be80e49aSRui Paulo 		if (++try < 10) goto begin;
922be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
923be80e49aSRui Paulo 			__func__, key, try);
924be80e49aSRui Paulo 	}
925be80e49aSRui Paulo 
92632a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
92732a8088fSRui Paulo 
92832a8088fSRui Paulo 	return (error);
92932a8088fSRui Paulo }
93032a8088fSRui Paulo 
9311269f4d4SRui Paulo #ifdef DEBUG
9321269f4d4SRui Paulo static int
9331269f4d4SRui Paulo asmc_key_dump(device_t dev, int number)
9341269f4d4SRui Paulo {
9351269f4d4SRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
9361269f4d4SRui Paulo 	char key[5] = { 0 };
9371269f4d4SRui Paulo 	char type[7] = { 0 };
9381269f4d4SRui Paulo 	uint8_t index[4];
9391269f4d4SRui Paulo 	uint8_t v[32];
9401269f4d4SRui Paulo 	uint8_t maxlen;
9411269f4d4SRui Paulo 	int i, error = 1, try = 0;
9421269f4d4SRui Paulo 
9431269f4d4SRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
9441269f4d4SRui Paulo 
9451269f4d4SRui Paulo 	index[0] = (number >> 24) & 0xff;
9461269f4d4SRui Paulo 	index[1] = (number >> 16) & 0xff;
9471269f4d4SRui Paulo 	index[2] = (number >> 8) & 0xff;
9481269f4d4SRui Paulo 	index[3] = (number) & 0xff;
9491269f4d4SRui Paulo 
9501269f4d4SRui Paulo begin:
9511269f4d4SRui Paulo 	if (asmc_command(dev, 0x12))
9521269f4d4SRui Paulo 		goto out;
9531269f4d4SRui Paulo 
9541269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9551269f4d4SRui Paulo 		ASMC_DATAPORT_WRITE(sc, index[i]);
9561269f4d4SRui Paulo 		if (asmc_wait(dev, 0x04))
9571269f4d4SRui Paulo 			goto out;
9581269f4d4SRui Paulo 	}
9591269f4d4SRui Paulo 
9601269f4d4SRui Paulo 	ASMC_DATAPORT_WRITE(sc, 4);
9611269f4d4SRui Paulo 
9621269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9631269f4d4SRui Paulo 		if (asmc_wait(dev, 0x05))
9641269f4d4SRui Paulo 			goto out;
9651269f4d4SRui Paulo 		key[i] = ASMC_DATAPORT_READ(sc);
9661269f4d4SRui Paulo 	}
9671269f4d4SRui Paulo 
9681269f4d4SRui Paulo 	/* get type */
9691269f4d4SRui Paulo 	if (asmc_command(dev, 0x13))
9701269f4d4SRui Paulo 		goto out;
9711269f4d4SRui Paulo 
9721269f4d4SRui Paulo 	for (i = 0; i < 4; i++) {
9731269f4d4SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
9741269f4d4SRui Paulo 		if (asmc_wait(dev, 0x04))
9751269f4d4SRui Paulo 			goto out;
9761269f4d4SRui Paulo 	}
9771269f4d4SRui Paulo 
9781269f4d4SRui Paulo 	ASMC_DATAPORT_WRITE(sc, 6);
9791269f4d4SRui Paulo 
9801269f4d4SRui Paulo 	for (i = 0; i < 6; i++) {
9811269f4d4SRui Paulo 		if (asmc_wait(dev, 0x05))
9821269f4d4SRui Paulo 			goto out;
9831269f4d4SRui Paulo 		type[i] = ASMC_DATAPORT_READ(sc);
9841269f4d4SRui Paulo 	}
9851269f4d4SRui Paulo 
9861269f4d4SRui Paulo 	error = 0;
9871269f4d4SRui Paulo out:
9881269f4d4SRui Paulo 	if (error) {
9891269f4d4SRui Paulo 		if (++try < 10) goto begin;
9901269f4d4SRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
9911269f4d4SRui Paulo 			__func__, key, try);
9921269f4d4SRui Paulo 		mtx_unlock_spin(&sc->sc_mtx);
9931269f4d4SRui Paulo 	}
9941269f4d4SRui Paulo 	else {
9951269f4d4SRui Paulo 		char buf[1024];
9961269f4d4SRui Paulo 		char buf2[8];
9971269f4d4SRui Paulo 		mtx_unlock_spin(&sc->sc_mtx);
9981269f4d4SRui Paulo 		maxlen = type[0];
9991269f4d4SRui Paulo 		type[0] = ' ';
10001269f4d4SRui Paulo 		type[5] = 0;
10011269f4d4SRui Paulo 		if (maxlen > sizeof(v)) {
1002f17bca82SRui Paulo 			device_printf(dev,
1003f17bca82SRui Paulo 			    "WARNING: cropping maxlen from %d to %zu\n",
1004f17bca82SRui Paulo 			    maxlen, sizeof(v));
10051269f4d4SRui Paulo 			maxlen = sizeof(v);
10061269f4d4SRui Paulo 		}
10071269f4d4SRui Paulo 		for (i = 0; i < sizeof(v); i++) {
10081269f4d4SRui Paulo 			v[i] = 0;
10091269f4d4SRui Paulo 		}
10101269f4d4SRui Paulo 		asmc_key_read(dev, key, v, maxlen);
10111269f4d4SRui Paulo 		snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
10121269f4d4SRui Paulo 		    "(len %d), data", number, key, type, maxlen);
10131269f4d4SRui Paulo 		for (i = 0; i < maxlen; i++) {
1014108e3076SAdrian Chadd 			snprintf(buf2, sizeof(buf2), " %02x", v[i]);
10151269f4d4SRui Paulo 			strlcat(buf, buf2, sizeof(buf));
10161269f4d4SRui Paulo 		}
10171269f4d4SRui Paulo 		strlcat(buf, " \n", sizeof(buf));
101846c76550SRoman Divacky 		device_printf(dev, "%s", buf);
10191269f4d4SRui Paulo 	}
10201269f4d4SRui Paulo 
10211269f4d4SRui Paulo 	return (error);
10221269f4d4SRui Paulo }
10231269f4d4SRui Paulo #endif
10241269f4d4SRui Paulo 
102532a8088fSRui Paulo static int
102632a8088fSRui Paulo asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
102732a8088fSRui Paulo {
1028be80e49aSRui Paulo 	int i, error = -1, try = 0;
102932a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
103032a8088fSRui Paulo 
103132a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
103232a8088fSRui Paulo 
1033be80e49aSRui Paulo begin:
10344470f0f3SRui Paulo 	ASMC_DPRINTF(("cmd port: cmd write\n"));
1035be80e49aSRui Paulo 	if (asmc_command(dev, ASMC_CMDWRITE))
103632a8088fSRui Paulo 		goto out;
103732a8088fSRui Paulo 
10384470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: key\n"));
103932a8088fSRui Paulo 	for (i = 0; i < 4; i++) {
10404470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, key[i]);
104132a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
104232a8088fSRui Paulo 			goto out;
104332a8088fSRui Paulo 	}
10444470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: length\n"));
10454470f0f3SRui Paulo 	ASMC_DATAPORT_WRITE(sc, len);
104632a8088fSRui Paulo 
10474470f0f3SRui Paulo 	ASMC_DPRINTF(("data port: buffer\n"));
104832a8088fSRui Paulo 	for (i = 0; i < len; i++) {
104932a8088fSRui Paulo 		if (asmc_wait(dev, 0x04))
105032a8088fSRui Paulo 			goto out;
10514470f0f3SRui Paulo 		ASMC_DATAPORT_WRITE(sc, buf[i]);
105232a8088fSRui Paulo 	}
105332a8088fSRui Paulo 
105432a8088fSRui Paulo 	error = 0;
105532a8088fSRui Paulo out:
1056be80e49aSRui Paulo 	if (error) {
1057be80e49aSRui Paulo 		if (++try < 10) goto begin;
1058be80e49aSRui Paulo 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1059be80e49aSRui Paulo 			__func__, key, try);
1060be80e49aSRui Paulo 	}
1061be80e49aSRui Paulo 
106232a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
106332a8088fSRui Paulo 
106432a8088fSRui Paulo 	return (error);
106532a8088fSRui Paulo 
106632a8088fSRui Paulo }
106732a8088fSRui Paulo 
106832a8088fSRui Paulo /*
106932a8088fSRui Paulo  * Fan control functions.
107032a8088fSRui Paulo  */
107132a8088fSRui Paulo static int
107232a8088fSRui Paulo asmc_fan_count(device_t dev)
107332a8088fSRui Paulo {
107432a8088fSRui Paulo 	uint8_t buf[1];
107532a8088fSRui Paulo 
1076*9c325393SMark Johnston 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) != 0)
107732a8088fSRui Paulo 		return (-1);
107832a8088fSRui Paulo 
107932a8088fSRui Paulo 	return (buf[0]);
108032a8088fSRui Paulo }
108132a8088fSRui Paulo 
108232a8088fSRui Paulo static int
108332a8088fSRui Paulo asmc_fan_getvalue(device_t dev, const char *key, int fan)
108432a8088fSRui Paulo {
108532a8088fSRui Paulo 	int speed;
108632a8088fSRui Paulo 	uint8_t buf[2];
108732a8088fSRui Paulo 	char fankey[5];
108832a8088fSRui Paulo 
108932a8088fSRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
1090*9c325393SMark Johnston 	if (asmc_key_read(dev, fankey, buf, sizeof buf) != 0)
109132a8088fSRui Paulo 		return (-1);
109232a8088fSRui Paulo 	speed = (buf[0] << 6) | (buf[1] >> 2);
109332a8088fSRui Paulo 
109432a8088fSRui Paulo 	return (speed);
109532a8088fSRui Paulo }
109632a8088fSRui Paulo 
1097447666f0SRui Paulo static char*
1098623534d6SUlrich Spörlein asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1099447666f0SRui Paulo {
1100447666f0SRui Paulo 	char fankey[5];
1101447666f0SRui Paulo 	char* desc;
1102447666f0SRui Paulo 
1103447666f0SRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
1104*9c325393SMark Johnston 	if (asmc_key_read(dev, fankey, buf, buflen) != 0)
1105447666f0SRui Paulo 		return (NULL);
1106447666f0SRui Paulo 	desc = buf+4;
1107447666f0SRui Paulo 
1108447666f0SRui Paulo 	return (desc);
1109447666f0SRui Paulo }
1110447666f0SRui Paulo 
1111447666f0SRui Paulo static int
1112447666f0SRui Paulo asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1113447666f0SRui Paulo {
1114447666f0SRui Paulo 	uint8_t buf[2];
1115447666f0SRui Paulo 	char fankey[5];
1116447666f0SRui Paulo 
1117447666f0SRui Paulo 	speed *= 4;
1118447666f0SRui Paulo 
1119447666f0SRui Paulo 	buf[0] = speed>>8;
1120447666f0SRui Paulo 	buf[1] = speed;
1121447666f0SRui Paulo 
1122447666f0SRui Paulo 	snprintf(fankey, sizeof(fankey), key, fan);
1123447666f0SRui Paulo 	if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1124447666f0SRui Paulo 		return (-1);
1125447666f0SRui Paulo 
1126447666f0SRui Paulo 	return (0);
1127447666f0SRui Paulo }
1128447666f0SRui Paulo 
112932a8088fSRui Paulo static int
113032a8088fSRui Paulo asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
113132a8088fSRui Paulo {
113232a8088fSRui Paulo 	device_t dev = (device_t) arg1;
113332a8088fSRui Paulo 	int fan = arg2;
113432a8088fSRui Paulo 	int error;
113532a8088fSRui Paulo 	int32_t v;
113632a8088fSRui Paulo 
113732a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
113832a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
113932a8088fSRui Paulo 
114032a8088fSRui Paulo 	return (error);
114132a8088fSRui Paulo }
114232a8088fSRui Paulo 
114332a8088fSRui Paulo static int
1144447666f0SRui Paulo asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1145447666f0SRui Paulo {
1146623534d6SUlrich Spörlein 	uint8_t buf[16];
1147447666f0SRui Paulo 	device_t dev = (device_t) arg1;
1148447666f0SRui Paulo 	int fan = arg2;
1149447666f0SRui Paulo 	int error = true;
1150447666f0SRui Paulo 	char* desc;
1151447666f0SRui Paulo 
1152623534d6SUlrich Spörlein 	desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1153447666f0SRui Paulo 
1154447666f0SRui Paulo 	if (desc != NULL)
1155447666f0SRui Paulo 		error = sysctl_handle_string(oidp, desc, 0, req);
1156447666f0SRui Paulo 
1157447666f0SRui Paulo 	return (error);
1158447666f0SRui Paulo }
1159447666f0SRui Paulo 
1160447666f0SRui Paulo static int
116132a8088fSRui Paulo asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
116232a8088fSRui Paulo {
116332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
116432a8088fSRui Paulo 	int fan = arg2;
116532a8088fSRui Paulo 	int error;
116632a8088fSRui Paulo 	int32_t v;
116732a8088fSRui Paulo 
116832a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
116932a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
117032a8088fSRui Paulo 
117132a8088fSRui Paulo 	return (error);
117232a8088fSRui Paulo }
117332a8088fSRui Paulo 
117432a8088fSRui Paulo 
117532a8088fSRui Paulo static int
117632a8088fSRui Paulo asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
117732a8088fSRui Paulo {
117832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
117932a8088fSRui Paulo 	int fan = arg2;
118032a8088fSRui Paulo 	int error;
118132a8088fSRui Paulo 	int32_t v;
118232a8088fSRui Paulo 
118332a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
118432a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
118532a8088fSRui Paulo 
1186447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
11870e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1188447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1189447666f0SRui Paulo 	}
1190447666f0SRui Paulo 
119132a8088fSRui Paulo 	return (error);
119232a8088fSRui Paulo }
119332a8088fSRui Paulo 
119432a8088fSRui Paulo static int
119532a8088fSRui Paulo asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
119632a8088fSRui Paulo {
119732a8088fSRui Paulo 	device_t dev = (device_t) arg1;
119832a8088fSRui Paulo 	int fan = arg2;
119932a8088fSRui Paulo 	int error;
120032a8088fSRui Paulo 	int32_t v;
120132a8088fSRui Paulo 
120232a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
120332a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
120432a8088fSRui Paulo 
1205447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
12060e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1207447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1208447666f0SRui Paulo 	}
1209447666f0SRui Paulo 
121032a8088fSRui Paulo 	return (error);
121132a8088fSRui Paulo }
121232a8088fSRui Paulo 
121332a8088fSRui Paulo static int
121432a8088fSRui Paulo asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
121532a8088fSRui Paulo {
121632a8088fSRui Paulo 	device_t dev = (device_t) arg1;
121732a8088fSRui Paulo 	int fan = arg2;
121832a8088fSRui Paulo 	int error;
121932a8088fSRui Paulo 	int32_t v;
122032a8088fSRui Paulo 
122132a8088fSRui Paulo 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
122232a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
122332a8088fSRui Paulo 
1224447666f0SRui Paulo 	if (error == 0 && req->newptr != NULL) {
12250e1152fcSHans Petter Selasky 		unsigned int newspeed = v;
1226447666f0SRui Paulo 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1227447666f0SRui Paulo 	}
1228447666f0SRui Paulo 
122932a8088fSRui Paulo 	return (error);
123032a8088fSRui Paulo }
123132a8088fSRui Paulo 
123232a8088fSRui Paulo /*
123332a8088fSRui Paulo  * Temperature functions.
123432a8088fSRui Paulo  */
123532a8088fSRui Paulo static int
123632a8088fSRui Paulo asmc_temp_getvalue(device_t dev, const char *key)
123732a8088fSRui Paulo {
123832a8088fSRui Paulo 	uint8_t buf[2];
123932a8088fSRui Paulo 
124032a8088fSRui Paulo 	/*
124132a8088fSRui Paulo 	 * Check for invalid temperatures.
124232a8088fSRui Paulo 	 */
1243*9c325393SMark Johnston 	if (asmc_key_read(dev, key, buf, sizeof buf) != 0)
124432a8088fSRui Paulo 		return (-1);
124532a8088fSRui Paulo 
124632a8088fSRui Paulo 	return (buf[0]);
124732a8088fSRui Paulo }
124832a8088fSRui Paulo 
124932a8088fSRui Paulo static int
125032a8088fSRui Paulo asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
125132a8088fSRui Paulo {
125232a8088fSRui Paulo 	device_t dev = (device_t) arg1;
125332a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
125432a8088fSRui Paulo 	int error, val;
125532a8088fSRui Paulo 
125632a8088fSRui Paulo 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
125732a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &val, 0, req);
125832a8088fSRui Paulo 
125932a8088fSRui Paulo 	return (error);
126032a8088fSRui Paulo }
126132a8088fSRui Paulo 
126232a8088fSRui Paulo /*
126332a8088fSRui Paulo  * Sudden Motion Sensor functions.
126432a8088fSRui Paulo  */
126532a8088fSRui Paulo static int
126632a8088fSRui Paulo asmc_sms_read(device_t dev, const char *key, int16_t *val)
126732a8088fSRui Paulo {
126832a8088fSRui Paulo 	uint8_t buf[2];
126932a8088fSRui Paulo 	int error;
127032a8088fSRui Paulo 
127132a8088fSRui Paulo 	/* no need to do locking here as asmc_key_read() already does it */
127232a8088fSRui Paulo 	switch (key[3]) {
127332a8088fSRui Paulo 	case 'X':
127432a8088fSRui Paulo 	case 'Y':
127532a8088fSRui Paulo 	case 'Z':
1276447666f0SRui Paulo 		error =	asmc_key_read(dev, key, buf, sizeof buf);
127732a8088fSRui Paulo 		break;
127832a8088fSRui Paulo 	default:
127932a8088fSRui Paulo 		device_printf(dev, "%s called with invalid argument %s\n",
128032a8088fSRui Paulo 			      __func__, key);
128132a8088fSRui Paulo 		error = 1;
128232a8088fSRui Paulo 		goto out;
128332a8088fSRui Paulo 	}
128432a8088fSRui Paulo 	*val = ((int16_t)buf[0] << 8) | buf[1];
128532a8088fSRui Paulo out:
128632a8088fSRui Paulo 	return (error);
128732a8088fSRui Paulo }
128832a8088fSRui Paulo 
128932a8088fSRui Paulo static void
129032a8088fSRui Paulo asmc_sms_calibrate(device_t dev)
129132a8088fSRui Paulo {
129232a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
129332a8088fSRui Paulo 
129432a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
129532a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
129632a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
129732a8088fSRui Paulo }
129832a8088fSRui Paulo 
129932a8088fSRui Paulo static int
130032a8088fSRui Paulo asmc_sms_intrfast(void *arg)
130132a8088fSRui Paulo {
130232a8088fSRui Paulo 	uint8_t type;
130332a8088fSRui Paulo 	device_t dev = (device_t) arg;
130432a8088fSRui Paulo 	struct asmc_softc *sc = device_get_softc(dev);
13051269f4d4SRui Paulo 	if (!sc->sc_sms_intr_works)
13061269f4d4SRui Paulo 		return (FILTER_HANDLED);
130732a8088fSRui Paulo 
130832a8088fSRui Paulo 	mtx_lock_spin(&sc->sc_mtx);
13094470f0f3SRui Paulo 	type = ASMC_INTPORT_READ(sc);
131032a8088fSRui Paulo 	mtx_unlock_spin(&sc->sc_mtx);
131132a8088fSRui Paulo 
131232a8088fSRui Paulo 	sc->sc_sms_intrtype = type;
131332a8088fSRui Paulo 	asmc_sms_printintr(dev, type);
131432a8088fSRui Paulo 
131532a8088fSRui Paulo 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
131632a8088fSRui Paulo 	return (FILTER_HANDLED);
131732a8088fSRui Paulo }
131832a8088fSRui Paulo 
131932a8088fSRui Paulo 
132032a8088fSRui Paulo 
132132a8088fSRui Paulo static void
132232a8088fSRui Paulo asmc_sms_printintr(device_t dev, uint8_t type)
132332a8088fSRui Paulo {
132432a8088fSRui Paulo 
132532a8088fSRui Paulo 	switch (type) {
132632a8088fSRui Paulo 	case ASMC_SMS_INTFF:
132732a8088fSRui Paulo 		device_printf(dev, "WARNING: possible free fall!\n");
132832a8088fSRui Paulo 		break;
132932a8088fSRui Paulo 	case ASMC_SMS_INTHA:
133032a8088fSRui Paulo 		device_printf(dev, "WARNING: high acceleration detected!\n");
133132a8088fSRui Paulo 		break;
133232a8088fSRui Paulo 	case ASMC_SMS_INTSH:
133332a8088fSRui Paulo 		device_printf(dev, "WARNING: possible shock!\n");
133432a8088fSRui Paulo 		break;
133532a8088fSRui Paulo 	default:
133632a8088fSRui Paulo 		device_printf(dev, "%s unknown interrupt\n", __func__);
133732a8088fSRui Paulo 	}
133832a8088fSRui Paulo }
133932a8088fSRui Paulo 
134032a8088fSRui Paulo static void
134132a8088fSRui Paulo asmc_sms_task(void *arg, int pending)
134232a8088fSRui Paulo {
134332a8088fSRui Paulo 	struct asmc_softc *sc = (struct asmc_softc *)arg;
134432a8088fSRui Paulo 	char notify[16];
134532a8088fSRui Paulo 	int type;
134632a8088fSRui Paulo 
134732a8088fSRui Paulo 	switch (sc->sc_sms_intrtype) {
134832a8088fSRui Paulo 	case ASMC_SMS_INTFF:
134932a8088fSRui Paulo 		type = 2;
135032a8088fSRui Paulo 		break;
135132a8088fSRui Paulo 	case ASMC_SMS_INTHA:
135232a8088fSRui Paulo 		type = 1;
135332a8088fSRui Paulo 		break;
135432a8088fSRui Paulo 	case ASMC_SMS_INTSH:
135532a8088fSRui Paulo 		type = 0;
135632a8088fSRui Paulo 		break;
135732a8088fSRui Paulo 	default:
135832a8088fSRui Paulo 		type = 255;
135932a8088fSRui Paulo 	}
136032a8088fSRui Paulo 
136132a8088fSRui Paulo 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
13624470f0f3SRui Paulo 	devctl_notify("ACPI", "asmc", "SMS", notify);
136332a8088fSRui Paulo }
136432a8088fSRui Paulo 
136532a8088fSRui Paulo static int
136632a8088fSRui Paulo asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
136732a8088fSRui Paulo {
136832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
136932a8088fSRui Paulo 	int error;
137032a8088fSRui Paulo 	int16_t val;
137132a8088fSRui Paulo 	int32_t v;
137232a8088fSRui Paulo 
137332a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
137432a8088fSRui Paulo 	v = (int32_t) val;
137532a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
137632a8088fSRui Paulo 
137732a8088fSRui Paulo 	return (error);
137832a8088fSRui Paulo }
137932a8088fSRui Paulo 
138032a8088fSRui Paulo static int
138132a8088fSRui Paulo asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
138232a8088fSRui Paulo {
138332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
138432a8088fSRui Paulo 	int error;
138532a8088fSRui Paulo 	int16_t val;
138632a8088fSRui Paulo 	int32_t v;
138732a8088fSRui Paulo 
138832a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
138932a8088fSRui Paulo 	v = (int32_t) val;
139032a8088fSRui Paulo 	error = sysctl_handle_int(oidp, &v, 0, req);
139132a8088fSRui Paulo 
139232a8088fSRui Paulo 	return (error);
139332a8088fSRui Paulo }
139432a8088fSRui Paulo 
139532a8088fSRui Paulo static int
139632a8088fSRui Paulo asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
139732a8088fSRui Paulo {
139832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
139932a8088fSRui Paulo 	int error;
140032a8088fSRui Paulo 	int16_t val;
140132a8088fSRui Paulo 	int32_t v;
140232a8088fSRui Paulo 
140332a8088fSRui Paulo 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
140432a8088fSRui Paulo 	v = (int32_t) val;
14050e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
140632a8088fSRui Paulo 
140732a8088fSRui Paulo 	return (error);
140832a8088fSRui Paulo }
140932a8088fSRui Paulo 
141032a8088fSRui Paulo static int
141132a8088fSRui Paulo asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
141232a8088fSRui Paulo {
141332a8088fSRui Paulo 	device_t dev = (device_t) arg1;
141432a8088fSRui Paulo 	uint8_t buf[6];
141532a8088fSRui Paulo 	int error;
141632a8088fSRui Paulo 	int32_t v;
141732a8088fSRui Paulo 
1418447666f0SRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
141932a8088fSRui Paulo 	v = buf[2];
14200e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
142132a8088fSRui Paulo 
142232a8088fSRui Paulo 	return (error);
142332a8088fSRui Paulo }
142432a8088fSRui Paulo 
142532a8088fSRui Paulo static int
142632a8088fSRui Paulo asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
142732a8088fSRui Paulo {
142832a8088fSRui Paulo 	device_t dev = (device_t) arg1;
142932a8088fSRui Paulo 	uint8_t buf[6];
143032a8088fSRui Paulo 	int error;
143132a8088fSRui Paulo 	int32_t v;
143232a8088fSRui Paulo 
1433447666f0SRui Paulo 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
143432a8088fSRui Paulo 	v = buf[2];
14350e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
1436be80e49aSRui Paulo 
1437be80e49aSRui Paulo 	return (error);
1438be80e49aSRui Paulo }
1439be80e49aSRui Paulo 
1440be80e49aSRui Paulo static int
1441be80e49aSRui Paulo asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1442be80e49aSRui Paulo {
1443be80e49aSRui Paulo 	device_t dev = (device_t) arg1;
1444be80e49aSRui Paulo 	uint8_t buf[2];
1445be80e49aSRui Paulo 	int error;
14460e1152fcSHans Petter Selasky 	int v;
1447be80e49aSRui Paulo 
1448108e3076SAdrian Chadd 	v = light_control;
14490e1152fcSHans Petter Selasky 	error = sysctl_handle_int(oidp, &v, 0, req);
14500e1152fcSHans Petter Selasky 
14510e1152fcSHans Petter Selasky 	if (error == 0 && req->newptr != NULL) {
14520e1152fcSHans Petter Selasky 		if (v < 0 || v > 255)
14530e1152fcSHans Petter Selasky 			return (EINVAL);
1454108e3076SAdrian Chadd 		light_control = v;
1455108e3076SAdrian Chadd 		buf[0] = light_control;
145632a8088fSRui Paulo 		buf[1] = 0x00;
1457447666f0SRui Paulo 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
145832a8088fSRui Paulo 	}
145932a8088fSRui Paulo 	return (error);
146032a8088fSRui Paulo }
1461