xref: /freebsd/sys/dev/asmc/asmc.c (revision 94db365042d35ff7e3ee7365a87a89bab1560030)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 /*
31  * Driver for Apple's System Management Console (SMC).
32  * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
33  *
34  * Inspired by the Linux applesmc driver.
35  */
36 
37 #include "opt_asmc.h"
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/endian.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/taskqueue.h>
51 #include <sys/rman.h>
52 
53 #include <machine/resource.h>
54 
55 #include <contrib/dev/acpica/include/acpi.h>
56 
57 #include <dev/acpica/acpivar.h>
58 #include <dev/asmc/asmcvar.h>
59 
60 /*
61  * Device interface.
62  */
63 static int 	asmc_probe(device_t dev);
64 static int 	asmc_attach(device_t dev);
65 static int 	asmc_detach(device_t dev);
66 static int 	asmc_resume(device_t dev);
67 
68 /*
69  * SMC functions.
70  */
71 static int 	asmc_init(device_t dev);
72 static int 	asmc_command(device_t dev, uint8_t command);
73 static int 	asmc_wait(device_t dev, uint8_t val);
74 static int 	asmc_wait_ack(device_t dev, uint8_t val, int amount);
75 static int 	asmc_key_write(device_t dev, const char *key, uint8_t *buf,
76     uint8_t len);
77 static int 	asmc_key_read(device_t dev, const char *key, uint8_t *buf,
78     uint8_t);
79 static int 	asmc_fan_count(device_t dev);
80 static int 	asmc_fan_getvalue(device_t dev, const char *key, int fan);
81 static int 	asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
82 static int 	asmc_temp_getvalue(device_t dev, const char *key);
83 static int 	asmc_sms_read(device_t, const char *key, int16_t *val);
84 static void 	asmc_sms_calibrate(device_t dev);
85 static int 	asmc_sms_intrfast(void *arg);
86 static void 	asmc_sms_printintr(device_t dev, uint8_t);
87 static void 	asmc_sms_task(void *arg, int pending);
88 #ifdef ASMC_DEBUG
89 void		asmc_dumpall(device_t);
90 static int	asmc_key_dump(device_t, int);
91 #endif
92 
93 /*
94  * Model functions.
95  */
96 static int 	asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
97 static int 	asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
98 static int 	asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
99 static int 	asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
100 static int 	asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
101 static int 	asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
102 static int 	asmc_mb_sysctl_fanmanual(SYSCTL_HANDLER_ARGS);
103 static int 	asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
104 static int 	asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
105 static int 	asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
106 static int 	asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
107 static int 	asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
108 static int 	asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
109 static int 	asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
110 static int 	asmc_mbp_sysctl_light_left_10byte(SYSCTL_HANDLER_ARGS);
111 static int	asmc_wol_sysctl(SYSCTL_HANDLER_ARGS);
112 
113 struct asmc_model {
114 	const char *smc_model; /* smbios.system.product env var. */
115 	const char *smc_desc;  /* driver description */
116 
117 	/* Helper functions */
118 	int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
119 	int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
120 	int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
121 	int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
122 	int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
123 	int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
124 	int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
125 	int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
126 	int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
127 	int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
128 	int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
129 	int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
130 
131 	const char 	*smc_temps[ASMC_TEMP_MAX];
132 	const char 	*smc_tempnames[ASMC_TEMP_MAX];
133 	const char 	*smc_tempdescs[ASMC_TEMP_MAX];
134 };
135 
136 static const struct asmc_model *asmc_match(device_t dev);
137 
138 #define ASMC_SMS_FUNCS						\
139 			.smc_sms_x = asmc_mb_sysctl_sms_x,	\
140 			.smc_sms_y = asmc_mb_sysctl_sms_y,	\
141 			.smc_sms_z = asmc_mb_sysctl_sms_z
142 
143 #define ASMC_SMS_FUNCS_DISABLED			\
144 			.smc_sms_x = NULL,	\
145 			.smc_sms_y = NULL,	\
146 			.smc_sms_z = NULL
147 
148 #define ASMC_FAN_FUNCS	\
149 			.smc_fan_id = asmc_mb_sysctl_fanid, \
150 			.smc_fan_speed = asmc_mb_sysctl_fanspeed, \
151 			.smc_fan_safespeed = asmc_mb_sysctl_fansafespeed, \
152 			.smc_fan_minspeed = asmc_mb_sysctl_fanminspeed, \
153 			.smc_fan_maxspeed = asmc_mb_sysctl_fanmaxspeed, \
154 			.smc_fan_targetspeed = asmc_mb_sysctl_fantargetspeed
155 
156 #define ASMC_FAN_FUNCS2	\
157 			.smc_fan_id = asmc_mb_sysctl_fanid, \
158 			.smc_fan_speed = asmc_mb_sysctl_fanspeed, \
159 			.smc_fan_safespeed = NULL, \
160 			.smc_fan_minspeed = asmc_mb_sysctl_fanminspeed, \
161 			.smc_fan_maxspeed = asmc_mb_sysctl_fanmaxspeed, \
162 			.smc_fan_targetspeed = asmc_mb_sysctl_fantargetspeed
163 
164 #define ASMC_LIGHT_FUNCS \
165 			 .smc_light_left = asmc_mbp_sysctl_light_left, \
166 			 .smc_light_right = asmc_mbp_sysctl_light_right, \
167 			 .smc_light_control = asmc_mbp_sysctl_light_control
168 
169 #define ASMC_LIGHT_FUNCS_10BYTE \
170 			 .smc_light_left = asmc_mbp_sysctl_light_left_10byte, \
171 			 .smc_light_right = NULL, \
172 			 .smc_light_control = asmc_mbp_sysctl_light_control
173 
174 #define ASMC_LIGHT_FUNCS_DISABLED \
175 			 .smc_light_left = NULL, \
176 			 .smc_light_right = NULL, \
177 			 .smc_light_control = NULL
178 
179 static const struct asmc_model asmc_models[] = {
180 	{
181 	  "MacBook1,1", "Apple SMC MacBook Core Duo",
182 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS_DISABLED,
183 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
184 	},
185 
186 	{
187 	  "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
188 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS_DISABLED,
189 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
190 	},
191 
192 	{
193 	  "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
194 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS_DISABLED,
195 	  ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
196 	},
197 
198 	{
199 	  "MacBook7,1", "Apple SMC MacBook Core 2 Duo (mid 2010)",
200 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS_DISABLED,
201 	  ASMC_MB71_TEMPS, ASMC_MB71_TEMPNAMES, ASMC_MB71_TEMPDESCS
202 	},
203 
204 	{
205 	  "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
206 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
207 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
208 	},
209 
210 	{
211 	  "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
212 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
213 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
214 	},
215 
216 	{
217 	  "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
218 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
219 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
220 	},
221 
222 	{
223 	  "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
224 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
225 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
226 	},
227 
228 	{
229 	  "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
230 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
231 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
232 	},
233 
234 	{
235 	  "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
236 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
237 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
238 	},
239 
240 	{
241 	  "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
242 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
243 	  ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
244 	},
245 
246 	{
247 	  "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
248 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
249 	  ASMC_MBP51_TEMPS, ASMC_MBP51_TEMPNAMES, ASMC_MBP51_TEMPDESCS
250 	},
251 
252 	{
253 	  "MacBookPro5,5", "Apple SMC MacBook Pro Core 2 Duo (Mid 2009)",
254 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
255 	  ASMC_MBP55_TEMPS, ASMC_MBP55_TEMPNAMES, ASMC_MBP55_TEMPDESCS
256 	},
257 
258 	{
259 	  "MacBookPro6,2", "Apple SMC MacBook Pro (Mid 2010, 15-inch)",
260 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
261 	  ASMC_MBP62_TEMPS, ASMC_MBP62_TEMPNAMES, ASMC_MBP62_TEMPDESCS
262 	},
263 
264 	{
265 	  "MacBookPro8,1", "Apple SMC MacBook Pro (early 2011, 13-inch)",
266 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
267 	  ASMC_MBP81_TEMPS, ASMC_MBP81_TEMPNAMES, ASMC_MBP81_TEMPDESCS
268 	},
269 
270 	{
271 	  "MacBookPro8,2", "Apple SMC MacBook Pro (early 2011)",
272 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
273 	  ASMC_MBP82_TEMPS, ASMC_MBP82_TEMPNAMES, ASMC_MBP82_TEMPDESCS
274 	},
275 
276 	{
277 	  "MacBookPro9,1", "Apple SMC MacBook Pro (mid 2012, 15-inch)",
278 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
279 	  ASMC_MBP91_TEMPS, ASMC_MBP91_TEMPNAMES, ASMC_MBP91_TEMPDESCS
280 	},
281 
282 	{
283 	 "MacBookPro9,2", "Apple SMC MacBook Pro (mid 2012, 13-inch)",
284 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
285 	  ASMC_MBP92_TEMPS, ASMC_MBP92_TEMPNAMES, ASMC_MBP92_TEMPDESCS
286 	},
287 
288 	{
289 	  "MacBookPro11,2", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
290 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
291 	  ASMC_MBP112_TEMPS, ASMC_MBP112_TEMPNAMES, ASMC_MBP112_TEMPDESCS
292 	},
293 
294 	{
295 	  "MacBookPro11,3", "Apple SMC MacBook Pro Retina Core i7 (2013/2014)",
296 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
297 	  ASMC_MBP113_TEMPS, ASMC_MBP113_TEMPNAMES, ASMC_MBP113_TEMPDESCS
298 	},
299 
300 	{
301 	  "MacBookPro11,4", "Apple SMC MacBook Pro Retina Core i7 (mid 2015, 15-inch)",
302 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
303 	  ASMC_MBP114_TEMPS, ASMC_MBP114_TEMPNAMES, ASMC_MBP114_TEMPDESCS
304 	},
305 
306 	{
307 	  "MacBookPro11,5",
308 	  "Apple SMC MacBook Pro Retina Core i7 (mid 2015, 15-inch, AMD GPU)",
309 	  ASMC_SMS_FUNCS_DISABLED, ASMC_FAN_FUNCS2, ASMC_LIGHT_FUNCS,
310 	  ASMC_MBP115_TEMPS, ASMC_MBP115_TEMPNAMES, ASMC_MBP115_TEMPDESCS
311 	},
312 
313 	/* The Mac Mini has no SMS */
314 	{
315 	  "Macmini1,1", "Apple SMC Mac Mini",
316 	  ASMC_SMS_FUNCS_DISABLED,
317 	  ASMC_FAN_FUNCS,
318 	  ASMC_LIGHT_FUNCS_DISABLED,
319 	  ASMC_MM_TEMPS, ASMC_MM_TEMPNAMES, ASMC_MM_TEMPDESCS
320 	},
321 
322 	/* The Mac Mini 2,1 has no SMS */
323 	{
324 	  "Macmini2,1", "Apple SMC Mac Mini 2,1",
325 	  ASMC_SMS_FUNCS_DISABLED,
326 	  ASMC_FAN_FUNCS,
327 	  ASMC_LIGHT_FUNCS_DISABLED,
328 	  ASMC_MM21_TEMPS, ASMC_MM21_TEMPNAMES, ASMC_MM21_TEMPDESCS
329 	},
330 
331 	/* The Mac Mini 3,1 has no SMS */
332 	{
333 	  "Macmini3,1", "Apple SMC Mac Mini 3,1",
334 	  ASMC_SMS_FUNCS_DISABLED,
335 	  ASMC_FAN_FUNCS,
336 	  ASMC_LIGHT_FUNCS_DISABLED,
337 	  ASMC_MM31_TEMPS, ASMC_MM31_TEMPNAMES, ASMC_MM31_TEMPDESCS
338 	},
339 
340 	/* The Mac Mini 4,1 (Mid-2010) has no SMS */
341 	{
342 	  "Macmini4,1", "Apple SMC Mac mini 4,1 (Mid-2010)",
343 	  ASMC_SMS_FUNCS_DISABLED,
344 	  ASMC_FAN_FUNCS2,
345 	  ASMC_LIGHT_FUNCS_DISABLED,
346 	  ASMC_MM41_TEMPS, ASMC_MM41_TEMPNAMES, ASMC_MM41_TEMPDESCS
347 	},
348 
349 	/* The Mac Mini 5,1 has no SMS */
350 	/* - same sensors as Mac Mini 5,2 */
351 	{
352 	  "Macmini5,1", "Apple SMC Mac Mini 5,1",
353 	  ASMC_SMS_FUNCS_DISABLED,
354 	  ASMC_FAN_FUNCS2,
355 	  ASMC_LIGHT_FUNCS_DISABLED,
356 	  ASMC_MM52_TEMPS, ASMC_MM52_TEMPNAMES, ASMC_MM52_TEMPDESCS
357 	},
358 
359 	/* The Mac Mini 5,2 has no SMS */
360 	{
361 	  "Macmini5,2", "Apple SMC Mac Mini 5,2",
362 	  ASMC_SMS_FUNCS_DISABLED,
363 	  ASMC_FAN_FUNCS2,
364 	  ASMC_LIGHT_FUNCS_DISABLED,
365 	  ASMC_MM52_TEMPS, ASMC_MM52_TEMPNAMES, ASMC_MM52_TEMPDESCS
366 	},
367 
368 	/* The Mac Mini 5,3 has no SMS */
369 	/* - same sensors as Mac Mini 5,2 */
370 	{
371 	  "Macmini5,3", "Apple SMC Mac Mini 5,3",
372 	  ASMC_SMS_FUNCS_DISABLED,
373 	  ASMC_FAN_FUNCS2,
374 	  ASMC_LIGHT_FUNCS_DISABLED,
375 	  ASMC_MM52_TEMPS, ASMC_MM52_TEMPNAMES, ASMC_MM52_TEMPDESCS
376 	},
377 
378 	/* The Mac Mini 6,1 has no SMS */
379 	{
380 	  "Macmini6,1", "Apple SMC Mac Mini 6,1",
381 	  ASMC_SMS_FUNCS_DISABLED,
382 	  ASMC_FAN_FUNCS2,
383 	  ASMC_LIGHT_FUNCS_DISABLED,
384 	  ASMC_MM61_TEMPS, ASMC_MM61_TEMPNAMES, ASMC_MM61_TEMPDESCS
385 	},
386 
387 	/* The Mac Mini 6,2 has no SMS */
388 	{
389 	  "Macmini6,2", "Apple SMC Mac Mini 6,2",
390 	  ASMC_SMS_FUNCS_DISABLED,
391 	  ASMC_FAN_FUNCS2,
392 	  ASMC_LIGHT_FUNCS_DISABLED,
393 	  ASMC_MM62_TEMPS, ASMC_MM62_TEMPNAMES, ASMC_MM62_TEMPDESCS
394 	},
395 
396 	/* The Mac Mini 7,1 has no SMS */
397 	{
398 	  "Macmini7,1", "Apple SMC Mac Mini 7,1",
399 	  ASMC_SMS_FUNCS_DISABLED,
400 	  ASMC_FAN_FUNCS2,
401 	  ASMC_LIGHT_FUNCS_DISABLED,
402 	  ASMC_MM71_TEMPS, ASMC_MM71_TEMPNAMES, ASMC_MM71_TEMPDESCS
403 	},
404 
405 	/* Idem for the Mac Pro "Quad Core" (original) */
406 	{
407 	  "MacPro1,1", "Apple SMC Mac Pro (Quad Core)",
408 	  ASMC_SMS_FUNCS_DISABLED,
409 	  ASMC_FAN_FUNCS,
410 	  ASMC_LIGHT_FUNCS_DISABLED,
411 	  ASMC_MP1_TEMPS, ASMC_MP1_TEMPNAMES, ASMC_MP1_TEMPDESCS
412 	},
413 
414 	/* Idem for the Mac Pro (Early 2008) */
415 	{
416 	  "MacPro3,1", "Apple SMC Mac Pro (Early 2008)",
417 	  ASMC_SMS_FUNCS_DISABLED,
418 	  ASMC_FAN_FUNCS,
419 	  ASMC_LIGHT_FUNCS_DISABLED,
420 	  ASMC_MP31_TEMPS, ASMC_MP31_TEMPNAMES, ASMC_MP31_TEMPDESCS
421 	},
422 
423 	/* Idem for the Mac Pro (8-core) */
424 	{
425 	  "MacPro2", "Apple SMC Mac Pro (8-core)",
426 	  ASMC_SMS_FUNCS_DISABLED,
427 	  ASMC_FAN_FUNCS,
428 	  ASMC_LIGHT_FUNCS_DISABLED,
429 	  ASMC_MP2_TEMPS, ASMC_MP2_TEMPNAMES, ASMC_MP2_TEMPDESCS
430 	},
431 
432 	/* Idem for the MacPro  2010*/
433 	{
434 	  "MacPro5,1", "Apple SMC MacPro (2010)",
435 	  ASMC_SMS_FUNCS_DISABLED,
436 	  ASMC_FAN_FUNCS,
437 	  ASMC_LIGHT_FUNCS_DISABLED,
438 	  ASMC_MP5_TEMPS, ASMC_MP5_TEMPNAMES, ASMC_MP5_TEMPDESCS
439 	},
440 
441 	/* Idem for the Mac Pro 2013 (cylinder) */
442 	{
443 	  "MacPro6,1", "Apple SMC Mac Pro (2013)",
444 	  ASMC_SMS_FUNCS_DISABLED,
445 	  ASMC_FAN_FUNCS2,
446 	  ASMC_LIGHT_FUNCS_DISABLED,
447 	  ASMC_MP6_TEMPS, ASMC_MP6_TEMPNAMES, ASMC_MP6_TEMPDESCS
448 	},
449 
450 	{
451 	  "MacBookAir1,1", "Apple SMC MacBook Air",
452 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS_DISABLED,
453 	  ASMC_MBA_TEMPS, ASMC_MBA_TEMPNAMES, ASMC_MBA_TEMPDESCS
454 	},
455 
456 	{
457 	  "MacBookAir3,1", "Apple SMC MacBook Air Core 2 Duo (Late 2010)",
458 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS_DISABLED,
459 	  ASMC_MBA3_TEMPS, ASMC_MBA3_TEMPNAMES, ASMC_MBA3_TEMPDESCS
460 	},
461 
462 	{
463 	  "MacBookAir4,1", "Apple SMC Macbook Air 11-inch (Mid 2011)",
464 	  ASMC_SMS_FUNCS_DISABLED,
465 	  ASMC_FAN_FUNCS2,
466 	  ASMC_LIGHT_FUNCS,
467 	  ASMC_MBA4_TEMPS, ASMC_MBA4_TEMPNAMES, ASMC_MBA4_TEMPDESCS
468 	},
469 
470 	{
471 	  "MacBookAir4,2", "Apple SMC Macbook Air 13-inch (Mid 2011)",
472 	  ASMC_SMS_FUNCS_DISABLED,
473 	  ASMC_FAN_FUNCS2,
474 	  ASMC_LIGHT_FUNCS,
475 	  ASMC_MBA4_TEMPS, ASMC_MBA4_TEMPNAMES, ASMC_MBA4_TEMPDESCS
476 	},
477 
478 	{
479 	  "MacBookAir5,1", "Apple SMC MacBook Air 11-inch (Mid 2012)",
480 	  ASMC_SMS_FUNCS_DISABLED,
481 	  ASMC_FAN_FUNCS2,
482 	  ASMC_LIGHT_FUNCS,
483 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
484 	},
485 
486 	{
487 	  "MacBookAir5,2", "Apple SMC MacBook Air 13-inch (Mid 2012)",
488 	  ASMC_SMS_FUNCS_DISABLED,
489 	  ASMC_FAN_FUNCS2,
490 	  ASMC_LIGHT_FUNCS,
491 	  ASMC_MBA5_TEMPS, ASMC_MBA5_TEMPNAMES, ASMC_MBA5_TEMPDESCS
492 	},
493 	{
494 	  "MacBookAir6,1", "Apple SMC MacBook Air 11-inch (Early 2013)",
495 	  ASMC_SMS_FUNCS_DISABLED,
496 	  ASMC_FAN_FUNCS2,
497 	  ASMC_LIGHT_FUNCS_10BYTE,
498 	  ASMC_MBA6_TEMPS, ASMC_MBA6_TEMPNAMES, ASMC_MBA6_TEMPDESCS
499 	},
500 	{
501 	  "MacBookAir6,2", "Apple SMC MacBook Air 13-inch (Early 2013)",
502 	  ASMC_SMS_FUNCS_DISABLED,
503 	  ASMC_FAN_FUNCS2,
504 	  ASMC_LIGHT_FUNCS_10BYTE,
505 	  ASMC_MBA6_TEMPS, ASMC_MBA6_TEMPNAMES, ASMC_MBA6_TEMPDESCS
506 	},
507 	{
508 	  "MacBookAir7,1", "Apple SMC MacBook Air 11-inch (Early 2015)",
509 	  ASMC_SMS_FUNCS_DISABLED,
510 	  ASMC_FAN_FUNCS2,
511 	  ASMC_LIGHT_FUNCS,
512 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
513 	},
514 	{
515 	  "MacBookAir7,2", "Apple SMC MacBook Air 13-inch (Early 2015)",
516 	  ASMC_SMS_FUNCS_DISABLED,
517 	  ASMC_FAN_FUNCS2,
518 	  ASMC_LIGHT_FUNCS,
519 	  ASMC_MBA7_TEMPS, ASMC_MBA7_TEMPNAMES, ASMC_MBA7_TEMPDESCS
520 	},
521 	{ NULL, NULL }
522 };
523 
524 #undef ASMC_SMS_FUNCS
525 #undef ASMC_SMS_FUNCS_DISABLED
526 #undef ASMC_FAN_FUNCS
527 #undef ASMC_FAN_FUNCS2
528 #undef ASMC_LIGHT_FUNCS
529 
530 /*
531  * Driver methods.
532  */
533 static device_method_t	asmc_methods[] = {
534 	DEVMETHOD(device_probe,		asmc_probe),
535 	DEVMETHOD(device_attach,	asmc_attach),
536 	DEVMETHOD(device_detach,	asmc_detach),
537 	DEVMETHOD(device_resume,	asmc_resume),
538 	DEVMETHOD_END
539 };
540 
541 static driver_t	asmc_driver = {
542 	"asmc",
543 	asmc_methods,
544 	sizeof(struct asmc_softc)
545 };
546 
547 /*
548  * Debugging
549  */
550 #define	_COMPONENT	ACPI_OEM
551 ACPI_MODULE_NAME("ASMC")
552 #ifdef ASMC_DEBUG
553 #define ASMC_DPRINTF(str, ...)	device_printf(dev, str, ##__VA_ARGS__)
554 #else
555 #define ASMC_DPRINTF(str, ...)
556 #endif
557 
558 /* NB: can't be const */
559 static char *asmc_ids[] = { "APP0001", NULL };
560 
561 static unsigned int light_control = 0;
562 
563 DRIVER_MODULE(asmc, acpi, asmc_driver, NULL, NULL);
564 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
565 
566 static const struct asmc_model *
567 asmc_match(device_t dev)
568 {
569 	int i;
570 	char *model;
571 
572 	model = kern_getenv("smbios.system.product");
573 	if (model == NULL)
574 		return (NULL);
575 
576 	for (i = 0; asmc_models[i].smc_model; i++) {
577 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
578 			freeenv(model);
579 			return (&asmc_models[i]);
580 		}
581 	}
582 	freeenv(model);
583 
584 	return (NULL);
585 }
586 
587 static int
588 asmc_probe(device_t dev)
589 {
590 	const struct asmc_model *model;
591 	int rv;
592 
593 	if (resource_disabled("asmc", 0))
594 		return (ENXIO);
595 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
596 	if (rv > 0)
597 		return (rv);
598 
599 	model = asmc_match(dev);
600 	if (!model) {
601 		device_printf(dev, "model not recognized\n");
602 		return (ENXIO);
603 	}
604 	device_set_desc(dev, model->smc_desc);
605 
606 	return (rv);
607 }
608 
609 static int
610 asmc_attach(device_t dev)
611 {
612 	int i, j;
613 	int ret;
614 	char name[2];
615 	struct asmc_softc *sc = device_get_softc(dev);
616 	struct sysctl_ctx_list *sysctlctx;
617 	struct sysctl_oid *sysctlnode;
618 	const struct asmc_model *model;
619 
620 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
621 	    &sc->sc_rid_port, RF_ACTIVE);
622 	if (sc->sc_ioport == NULL) {
623 		device_printf(dev, "unable to allocate IO port\n");
624 		return (ENOMEM);
625 	}
626 
627 	sysctlctx = device_get_sysctl_ctx(dev);
628 	sysctlnode = device_get_sysctl_tree(dev);
629 
630 	model = asmc_match(dev);
631 
632 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
633 
634 	sc->sc_model = model;
635 	asmc_init(dev);
636 
637 	/*
638 	 * dev.asmc.n.fan.* tree.
639 	 */
640 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
641 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
642 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Fan Root Tree");
643 
644 	for (i = 1; i <= sc->sc_nfan; i++) {
645 		j = i - 1;
646 		name[0] = '0' + j;
647 		name[1] = 0;
648 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
649 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]), OID_AUTO, name,
650 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Fan Subtree");
651 
652 		SYSCTL_ADD_PROC(sysctlctx,
653 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
654 		    OID_AUTO, "id",
655 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, j,
656 		    model->smc_fan_id, "I", "Fan ID");
657 
658 		SYSCTL_ADD_PROC(sysctlctx,
659 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
660 		    OID_AUTO, "speed",
661 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, j,
662 		    model->smc_fan_speed, "I", "Fan speed in RPM");
663 
664 		SYSCTL_ADD_PROC(sysctlctx,
665 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
666 		    OID_AUTO, "safespeed",
667 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, j,
668 		    model->smc_fan_safespeed, "I", "Fan safe speed in RPM");
669 
670 		SYSCTL_ADD_PROC(sysctlctx,
671 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
672 		    OID_AUTO, "minspeed",
673 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, j,
674 		    model->smc_fan_minspeed, "I", "Fan minimum speed in RPM");
675 
676 		SYSCTL_ADD_PROC(sysctlctx,
677 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
678 		    OID_AUTO, "maxspeed",
679 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, j,
680 		    model->smc_fan_maxspeed, "I", "Fan maximum speed in RPM");
681 
682 		SYSCTL_ADD_PROC(sysctlctx,
683 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
684 		    OID_AUTO, "targetspeed",
685 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, j,
686 		    model->smc_fan_targetspeed, "I", "Fan target speed in RPM");
687 
688 		SYSCTL_ADD_PROC(sysctlctx,
689 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
690 		    OID_AUTO, "manual",
691 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, j,
692 		    asmc_mb_sysctl_fanmanual, "I",
693 		    "Fan manual mode (0=auto, 1=manual)");
694 	}
695 
696 	/*
697 	 * dev.asmc.n.temp tree.
698 	 */
699 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
700 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
701 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Temperature sensors");
702 
703 	for (i = 0; model->smc_temps[i]; i++) {
704 		SYSCTL_ADD_PROC(sysctlctx,
705 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
706 		    OID_AUTO, model->smc_tempnames[i],
707 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, i,
708 		    asmc_temp_sysctl, "I",
709 		    model->smc_tempdescs[i]);
710 	}
711 
712 	/*
713 	 * dev.asmc.n.light
714 	 */
715 	if (model->smc_light_left) {
716 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
717 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
718 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
719 		    "Keyboard backlight sensors");
720 
721 		SYSCTL_ADD_PROC(sysctlctx,
722 		    SYSCTL_CHILDREN(sc->sc_light_tree),
723 		    OID_AUTO, "left",
724 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
725 		    dev, 0, model->smc_light_left, "I",
726 		    "Keyboard backlight left sensor");
727 
728 		SYSCTL_ADD_PROC(sysctlctx,
729 		    SYSCTL_CHILDREN(sc->sc_light_tree),
730 		    OID_AUTO, "right",
731 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
732 		    model->smc_light_right, "I",
733 		    "Keyboard backlight right sensor");
734 
735 		SYSCTL_ADD_PROC(sysctlctx,
736 		    SYSCTL_CHILDREN(sc->sc_light_tree),
737 		    OID_AUTO, "control",
738 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
739 		    dev, 0, model->smc_light_control, "I",
740 		    "Keyboard backlight brightness control");
741 	}
742 
743 	if (model->smc_sms_x == NULL)
744 		goto nosms;
745 
746 	/*
747 	 * dev.asmc.n.sms tree.
748 	 */
749 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
750 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
751 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Sudden Motion Sensor");
752 
753 	SYSCTL_ADD_PROC(sysctlctx,
754 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
755 	    OID_AUTO, "x",
756 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
757 	    dev, 0, model->smc_sms_x, "I",
758 	    "Sudden Motion Sensor X value");
759 
760 	SYSCTL_ADD_PROC(sysctlctx,
761 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
762 	    OID_AUTO, "y",
763 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
764 	    dev, 0, model->smc_sms_y, "I",
765 	    "Sudden Motion Sensor Y value");
766 
767 	SYSCTL_ADD_PROC(sysctlctx,
768 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
769 	    OID_AUTO, "z",
770 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
771 	    dev, 0, model->smc_sms_z, "I",
772 	    "Sudden Motion Sensor Z value");
773 
774 	/*
775 	 * Need a taskqueue to send devctl_notify() events
776 	 * when the SMS interrupt us.
777 	 *
778 	 * PI_REALTIME is used due to the sensitivity of the
779 	 * interrupt. An interrupt from the SMS means that the
780 	 * disk heads should be turned off as quickly as possible.
781 	 *
782 	 * We only need to do this for the non INTR_FILTER case.
783 	 */
784 	sc->sc_sms_tq = NULL;
785 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
786 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
787 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
788 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
789 	    device_get_nameunit(dev));
790 	/*
791 	 * Allocate an IRQ for the SMS.
792 	 */
793 	sc->sc_rid_irq = 0;
794 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_rid_irq,
795 	    RF_ACTIVE);
796 	if (sc->sc_irq == NULL) {
797 		device_printf(dev, "unable to allocate IRQ resource\n");
798 		ret = ENXIO;
799 		goto err;
800 	}
801 
802 	ret = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE,
803 	    asmc_sms_intrfast, NULL, dev, &sc->sc_cookie);
804 	if (ret) {
805 		device_printf(dev, "unable to setup SMS IRQ\n");
806 		goto err;
807 	}
808 
809 nosms:
810 	return (0);
811 
812 err:
813 	asmc_detach(dev);
814 
815 	return (ret);
816 }
817 
818 static int
819 asmc_detach(device_t dev)
820 {
821 	struct asmc_softc *sc = device_get_softc(dev);
822 
823 	if (sc->sc_sms_tq) {
824 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
825 		taskqueue_free(sc->sc_sms_tq);
826 		sc->sc_sms_tq = NULL;
827 	}
828 	if (sc->sc_cookie) {
829 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
830 		sc->sc_cookie = NULL;
831 	}
832 	if (sc->sc_irq) {
833 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
834 		    sc->sc_irq);
835 		sc->sc_irq = NULL;
836 	}
837 	if (sc->sc_ioport) {
838 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
839 		    sc->sc_ioport);
840 		sc->sc_ioport = NULL;
841 	}
842 	if (mtx_initialized(&sc->sc_mtx)) {
843 		mtx_destroy(&sc->sc_mtx);
844 	}
845 
846 	return (0);
847 }
848 
849 static int
850 asmc_resume(device_t dev)
851 {
852 	uint8_t buf[2];
853 
854 	buf[0] = light_control;
855 	buf[1] = 0x00;
856 	asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof(buf));
857 
858 	return (0);
859 }
860 
861 #ifdef ASMC_DEBUG
862 void
863 asmc_dumpall(device_t dev)
864 {
865 	struct asmc_softc *sc = device_get_softc(dev);
866 	int i;
867 
868 	if (sc->sc_nkeys == 0) {
869 		device_printf(dev, "asmc_dumpall: key count not available\n");
870 		return;
871 	}
872 
873 	device_printf(dev, "asmc_dumpall: dumping %d keys\n", sc->sc_nkeys);
874 	for (i = 0; i < sc->sc_nkeys; i++)
875 		asmc_key_dump(dev, i);
876 }
877 #endif
878 
879 static int
880 asmc_init(device_t dev)
881 {
882 	struct asmc_softc *sc = device_get_softc(dev);
883 	struct sysctl_ctx_list *sysctlctx;
884 	int i, error = 1;
885 	uint8_t buf[4];
886 
887 	sysctlctx = device_get_sysctl_ctx(dev);
888 
889 	if (sc->sc_model->smc_sms_x == NULL)
890 		goto nosms;
891 
892 	/*
893 	 * We are ready to receive interrupts from the SMS.
894 	 */
895 	buf[0] = 0x01;
896 	ASMC_DPRINTF(("intok key\n"));
897 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
898 	DELAY(50);
899 
900 	/*
901 	 * Initiate the polling intervals.
902 	 */
903 	buf[0] = 20; /* msecs */
904 	ASMC_DPRINTF(("low int key\n"));
905 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
906 	DELAY(200);
907 
908 	buf[0] = 20; /* msecs */
909 	ASMC_DPRINTF(("high int key\n"));
910 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
911 	DELAY(200);
912 
913 	buf[0] = 0x00;
914 	buf[1] = 0x60;
915 	ASMC_DPRINTF(("sms low key\n"));
916 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
917 	DELAY(200);
918 
919 	buf[0] = 0x01;
920 	buf[1] = 0xc0;
921 	ASMC_DPRINTF(("sms high key\n"));
922 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
923 	DELAY(200);
924 
925 	/*
926 	 * I'm not sure what this key does, but it seems to be
927 	 * required.
928 	 */
929 	buf[0] = 0x01;
930 	ASMC_DPRINTF(("sms flag key\n"));
931 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
932 	DELAY(100);
933 
934 	sc->sc_sms_intr_works = 0;
935 
936 	/*
937 	 * Retry SMS initialization 1000 times
938 	 * (takes approx. 2 seconds in worst case)
939 	 */
940 	for (i = 0; i < 1000; i++) {
941 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
942 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
943 			error = 0;
944 			sc->sc_sms_intr_works = 1;
945 			goto out;
946 		}
947 		buf[0] = ASMC_SMS_INIT1;
948 		buf[1] = ASMC_SMS_INIT2;
949 		ASMC_DPRINTF(("sms key\n"));
950 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
951 		DELAY(50);
952 	}
953 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
954 
955 out:
956 	asmc_sms_calibrate(dev);
957 nosms:
958 	/* Wake-on-LAN convenience sysctl */
959 	if (asmc_key_read(dev, ASMC_KEY_AUPO, buf, 1) == 0) {
960 		SYSCTL_ADD_PROC(sysctlctx,
961 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
962 		    OID_AUTO, "wol",
963 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
964 		    dev, 0, asmc_wol_sysctl, "I",
965 		    "Wake-on-LAN enable (0=off, 1=on)");
966 	}
967 
968 	sc->sc_nfan = asmc_fan_count(dev);
969 	if (sc->sc_nfan > ASMC_MAXFANS) {
970 		device_printf(dev,
971 		    "more than %d fans were detected. Please report this.\n",
972 		    ASMC_MAXFANS);
973 		sc->sc_nfan = ASMC_MAXFANS;
974 	}
975 
976 	/*
977 	 * Read and cache the number of SMC keys (32 bit buffer)
978 	 */
979 	if (asmc_key_read(dev, ASMC_NKEYS, buf, 4) == 0) {
980 		sc->sc_nkeys = be32dec(buf);
981 		if (bootverbose)
982 			device_printf(dev, "number of keys: %d\n",
983 			    sc->sc_nkeys);
984 	} else {
985 		sc->sc_nkeys = 0;
986 	}
987 
988 #ifdef ASMC_DEBUG
989 	asmc_dumpall(dev);
990 #endif
991 
992 	return (error);
993 }
994 
995 /*
996  * We need to make sure that the SMC acks the byte sent.
997  * Just wait up to (amount * 10)  ms.
998  */
999 static int
1000 asmc_wait_ack(device_t dev, uint8_t val, int amount)
1001 {
1002 	struct asmc_softc *sc = device_get_softc(dev);
1003 	u_int i;
1004 
1005 	val = val & ASMC_STATUS_MASK;
1006 
1007 	for (i = 0; i < amount; i++) {
1008 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
1009 			return (0);
1010 		DELAY(10);
1011 	}
1012 
1013 	return (1);
1014 }
1015 
1016 /*
1017  * We need to make sure that the SMC acks the byte sent.
1018  * Just wait up to 100 ms.
1019  */
1020 static int
1021 asmc_wait(device_t dev, uint8_t val)
1022 {
1023 #ifdef ASMC_DEBUG
1024 	struct asmc_softc *sc;
1025 #endif
1026 
1027 	if (asmc_wait_ack(dev, val, 1000) == 0)
1028 		return (0);
1029 
1030 #ifdef ASMC_DEBUG
1031 	sc = device_get_softc(dev);
1032 
1033 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__,
1034 	    val & ASMC_STATUS_MASK, ASMC_CMDPORT_READ(sc));
1035 #endif
1036 	return (1);
1037 }
1038 
1039 /*
1040  * Send the given command, retrying up to 10 times if
1041  * the acknowledgement fails.
1042  */
1043 static int
1044 asmc_command(device_t dev, uint8_t command)
1045 {
1046 	int i;
1047 	struct asmc_softc *sc = device_get_softc(dev);
1048 
1049 	for (i = 0; i < 10; i++) {
1050 		ASMC_CMDPORT_WRITE(sc, command);
1051 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
1052 			return (0);
1053 		}
1054 	}
1055 
1056 #ifdef ASMC_DEBUG
1057 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
1058 	    ASMC_CMDPORT_READ(sc));
1059 #endif
1060 	return (1);
1061 }
1062 
1063 static int
1064 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1065 {
1066 	int i, error = 1, try = 0;
1067 	struct asmc_softc *sc = device_get_softc(dev);
1068 
1069 	mtx_lock_spin(&sc->sc_mtx);
1070 
1071 begin:
1072 	if (asmc_command(dev, ASMC_CMDREAD))
1073 		goto out;
1074 
1075 	for (i = 0; i < 4; i++) {
1076 		ASMC_DATAPORT_WRITE(sc, key[i]);
1077 		if (asmc_wait(dev, 0x04))
1078 			goto out;
1079 	}
1080 
1081 	ASMC_DATAPORT_WRITE(sc, len);
1082 
1083 	for (i = 0; i < len; i++) {
1084 		if (asmc_wait(dev, 0x05))
1085 			goto out;
1086 		buf[i] = ASMC_DATAPORT_READ(sc);
1087 	}
1088 
1089 	error = 0;
1090 out:
1091 	if (error) {
1092 		if (++try < 10)
1093 			goto begin;
1094 		device_printf(dev, "%s for key %s failed %d times, giving up\n",
1095 		    __func__, key, try);
1096 	}
1097 
1098 	mtx_unlock_spin(&sc->sc_mtx);
1099 
1100 	return (error);
1101 }
1102 
1103 #ifdef ASMC_DEBUG
1104 static int
1105 asmc_key_dump(device_t dev, int number)
1106 {
1107 	struct asmc_softc *sc = device_get_softc(dev);
1108 	char key[5] = { 0 };
1109 	char type[7] = { 0 };
1110 	uint8_t index[4];
1111 	uint8_t v[32];
1112 	uint8_t maxlen;
1113 	int i, error = 1, try = 0;
1114 
1115 	mtx_lock_spin(&sc->sc_mtx);
1116 
1117 	index[0] = (number >> 24) & 0xff;
1118 	index[1] = (number >> 16) & 0xff;
1119 	index[2] = (number >> 8) & 0xff;
1120 	index[3] = (number) & 0xff;
1121 
1122 begin:
1123 	if (asmc_command(dev, 0x12))
1124 		goto out;
1125 
1126 	for (i = 0; i < 4; i++) {
1127 		ASMC_DATAPORT_WRITE(sc, index[i]);
1128 		if (asmc_wait(dev, 0x04))
1129 			goto out;
1130 	}
1131 
1132 	ASMC_DATAPORT_WRITE(sc, 4);
1133 
1134 	for (i = 0; i < 4; i++) {
1135 		if (asmc_wait(dev, 0x05))
1136 			goto out;
1137 		key[i] = ASMC_DATAPORT_READ(sc);
1138 	}
1139 
1140 	/* get type */
1141 	if (asmc_command(dev, 0x13))
1142 		goto out;
1143 
1144 	for (i = 0; i < 4; i++) {
1145 		ASMC_DATAPORT_WRITE(sc, key[i]);
1146 		if (asmc_wait(dev, 0x04))
1147 			goto out;
1148 	}
1149 
1150 	ASMC_DATAPORT_WRITE(sc, 6);
1151 
1152 	for (i = 0; i < 6; i++) {
1153 		if (asmc_wait(dev, 0x05))
1154 			goto out;
1155 		type[i] = ASMC_DATAPORT_READ(sc);
1156 	}
1157 
1158 	error = 0;
1159 out:
1160 	if (error) {
1161 		if (++try < 10)
1162 			goto begin;
1163 		device_printf(dev, "%s for key %s failed %d times, giving up\n",
1164 		    __func__, key, try);
1165 		mtx_unlock_spin(&sc->sc_mtx);
1166 	} else {
1167 		char buf[1024];
1168 		char buf2[8];
1169 		mtx_unlock_spin(&sc->sc_mtx);
1170 		maxlen = type[0];
1171 		type[0] = ' ';
1172 		type[5] = 0;
1173 		if (maxlen > sizeof(v)) {
1174 			device_printf(dev,
1175 			    "WARNING: cropping maxlen from %d to %zu\n", maxlen,
1176 			    sizeof(v));
1177 			maxlen = sizeof(v);
1178 		}
1179 		for (i = 0; i < sizeof(v); i++) {
1180 			v[i] = 0;
1181 		}
1182 		asmc_key_read(dev, key, v, maxlen);
1183 		snprintf(buf, sizeof(buf),
1184 		    "key %d is: %s, type %s (len %d), data",
1185 		    number, key, type, maxlen);
1186 		for (i = 0; i < maxlen; i++) {
1187 			snprintf(buf2, sizeof(buf2), " %02x", v[i]);
1188 			strlcat(buf, buf2, sizeof(buf));
1189 		}
1190 		strlcat(buf, " \n", sizeof(buf));
1191 		device_printf(dev, "%s", buf);
1192 	}
1193 
1194 	return (error);
1195 }
1196 #endif
1197 
1198 static int
1199 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1200 {
1201 	int i, error = -1, try = 0;
1202 	struct asmc_softc *sc = device_get_softc(dev);
1203 
1204 	mtx_lock_spin(&sc->sc_mtx);
1205 
1206 begin:
1207 	ASMC_DPRINTF(("cmd port: cmd write\n"));
1208 	if (asmc_command(dev, ASMC_CMDWRITE))
1209 		goto out;
1210 
1211 	ASMC_DPRINTF(("data port: key\n"));
1212 	for (i = 0; i < 4; i++) {
1213 		ASMC_DATAPORT_WRITE(sc, key[i]);
1214 		if (asmc_wait(dev, 0x04))
1215 			goto out;
1216 	}
1217 	ASMC_DPRINTF(("data port: length\n"));
1218 	ASMC_DATAPORT_WRITE(sc, len);
1219 
1220 	ASMC_DPRINTF(("data port: buffer\n"));
1221 	for (i = 0; i < len; i++) {
1222 		if (asmc_wait(dev, 0x04))
1223 			goto out;
1224 		ASMC_DATAPORT_WRITE(sc, buf[i]);
1225 	}
1226 
1227 	error = 0;
1228 out:
1229 	if (error) {
1230 		if (++try < 10)
1231 			goto begin;
1232 		device_printf(dev, "%s for key %s failed %d times, giving up\n",
1233 		    __func__, key, try);
1234 	}
1235 
1236 	mtx_unlock_spin(&sc->sc_mtx);
1237 
1238 	return (error);
1239 }
1240 
1241 /*
1242  * Fan control functions.
1243  */
1244 static int
1245 asmc_fan_count(device_t dev)
1246 {
1247 	uint8_t buf[1];
1248 
1249 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof(buf)) != 0)
1250 		return (-1);
1251 
1252 	return (buf[0]);
1253 }
1254 
1255 static int
1256 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1257 {
1258 	int speed;
1259 	uint8_t buf[2];
1260 	char fankey[5];
1261 
1262 	snprintf(fankey, sizeof(fankey), key, fan);
1263 	if (asmc_key_read(dev, fankey, buf, sizeof(buf)) != 0)
1264 		return (-1);
1265 	speed = (buf[0] << 6) | (buf[1] >> 2);
1266 
1267 	return (speed);
1268 }
1269 
1270 static char *
1271 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf,
1272     uint8_t buflen)
1273 {
1274 	char fankey[5];
1275 	char *desc;
1276 
1277 	snprintf(fankey, sizeof(fankey), key, fan);
1278 	if (asmc_key_read(dev, fankey, buf, buflen) != 0)
1279 		return (NULL);
1280 	desc = buf + 4;
1281 
1282 	return (desc);
1283 }
1284 
1285 static int
1286 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1287 {
1288 	uint8_t buf[2];
1289 	char fankey[5];
1290 
1291 	speed *= 4;
1292 
1293 	buf[0] = speed >> 8;
1294 	buf[1] = speed;
1295 
1296 	snprintf(fankey, sizeof(fankey), key, fan);
1297 	if (asmc_key_write(dev, fankey, buf, sizeof(buf)) < 0)
1298 		return (-1);
1299 
1300 	return (0);
1301 }
1302 
1303 static int
1304 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1305 {
1306 	device_t dev = (device_t)arg1;
1307 	int fan = arg2;
1308 	int error;
1309 	int32_t v;
1310 
1311 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1312 	error = sysctl_handle_int(oidp, &v, 0, req);
1313 
1314 	return (error);
1315 }
1316 
1317 static int
1318 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1319 {
1320 	uint8_t buf[16];
1321 	device_t dev = (device_t)arg1;
1322 	int fan = arg2;
1323 	int error = true;
1324 	char *desc;
1325 
1326 	desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1327 
1328 	if (desc != NULL)
1329 		error = sysctl_handle_string(oidp, desc, 0, req);
1330 
1331 	return (error);
1332 }
1333 
1334 static int
1335 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1336 {
1337 	device_t dev = (device_t)arg1;
1338 	int fan = arg2;
1339 	int error;
1340 	int32_t v;
1341 
1342 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1343 	error = sysctl_handle_int(oidp, &v, 0, req);
1344 
1345 	return (error);
1346 }
1347 
1348 static int
1349 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1350 {
1351 	device_t dev = (device_t)arg1;
1352 	int fan = arg2;
1353 	int error;
1354 	int32_t v;
1355 
1356 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1357 	error = sysctl_handle_int(oidp, &v, 0, req);
1358 
1359 	if (error == 0 && req->newptr != NULL) {
1360 		unsigned int newspeed = v;
1361 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1362 	}
1363 
1364 	return (error);
1365 }
1366 
1367 static int
1368 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1369 {
1370 	device_t dev = (device_t)arg1;
1371 	int fan = arg2;
1372 	int error;
1373 	int32_t v;
1374 
1375 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1376 	error = sysctl_handle_int(oidp, &v, 0, req);
1377 
1378 	if (error == 0 && req->newptr != NULL) {
1379 		unsigned int newspeed = v;
1380 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1381 	}
1382 
1383 	return (error);
1384 }
1385 
1386 static int
1387 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1388 {
1389 	device_t dev = (device_t)arg1;
1390 	int fan = arg2;
1391 	int error;
1392 	int32_t v;
1393 
1394 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1395 	error = sysctl_handle_int(oidp, &v, 0, req);
1396 
1397 	if (error == 0 && req->newptr != NULL) {
1398 		unsigned int newspeed = v;
1399 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1400 	}
1401 
1402 	return (error);
1403 }
1404 
1405 static int
1406 asmc_mb_sysctl_fanmanual(SYSCTL_HANDLER_ARGS)
1407 {
1408 	device_t dev = (device_t)arg1;
1409 	int fan = arg2;
1410 	int error;
1411 	int32_t v;
1412 	uint8_t buf[2];
1413 	uint16_t val;
1414 
1415 	/* Read current FS! bitmask (asmc_key_read locks internally) */
1416 	error = asmc_key_read(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf));
1417 	if (error != 0)
1418 		return (error);
1419 
1420 	/* Extract manual bit for this fan (big-endian) */
1421 	val = (buf[0] << 8) | buf[1];
1422 	v = (val >> fan) & 0x01;
1423 
1424 	/* Let sysctl handle the value */
1425 	error = sysctl_handle_int(oidp, &v, 0, req);
1426 
1427 	if (error == 0 && req->newptr != NULL) {
1428 		/* Validate input (0 = auto, 1 = manual) */
1429 		if (v != 0 && v != 1)
1430 			return (EINVAL);
1431 		/* Read-modify-write of FS! bitmask */
1432 		error = asmc_key_read(dev, ASMC_KEY_FANMANUAL, buf,
1433 		    sizeof(buf));
1434 		if (error == 0) {
1435 			val = (buf[0] << 8) | buf[1];
1436 
1437 			/* Modify single bit */
1438 			if (v)
1439 				val |= (1 << fan);   /* Set to manual */
1440 			else
1441 				val &= ~(1 << fan);  /* Set to auto */
1442 
1443 			/* Write back */
1444 			buf[0] = val >> 8;
1445 			buf[1] = val & 0xff;
1446 			error = asmc_key_write(dev, ASMC_KEY_FANMANUAL, buf,
1447 			    sizeof(buf));
1448 		}
1449 	}
1450 
1451 	return (error);
1452 }
1453 
1454 /*
1455  * Temperature functions.
1456  */
1457 static int
1458 asmc_temp_getvalue(device_t dev, const char *key)
1459 {
1460 	uint8_t buf[2];
1461 
1462 	/*
1463 	 * Check for invalid temperatures.
1464 	 */
1465 	if (asmc_key_read(dev, key, buf, sizeof(buf)) != 0)
1466 		return (-1);
1467 
1468 	return (buf[0]);
1469 }
1470 
1471 static int
1472 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1473 {
1474 	device_t dev = (device_t)arg1;
1475 	struct asmc_softc *sc = device_get_softc(dev);
1476 	int error, val;
1477 
1478 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1479 	error = sysctl_handle_int(oidp, &val, 0, req);
1480 
1481 	return (error);
1482 }
1483 
1484 /*
1485  * Sudden Motion Sensor functions.
1486  */
1487 static int
1488 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1489 {
1490 	uint8_t buf[2];
1491 	int error;
1492 
1493 	/* no need to do locking here as asmc_key_read() already does it */
1494 	switch (key[3]) {
1495 	case 'X':
1496 	case 'Y':
1497 	case 'Z':
1498 		error = asmc_key_read(dev, key, buf, sizeof(buf));
1499 		break;
1500 	default:
1501 		device_printf(dev, "%s called with invalid argument %s\n",
1502 		    __func__, key);
1503 		error = EINVAL;
1504 		goto out;
1505 	}
1506 	*val = ((int16_t)buf[0] << 8) | buf[1];
1507 out:
1508 	return (error);
1509 }
1510 
1511 static void
1512 asmc_sms_calibrate(device_t dev)
1513 {
1514 	struct asmc_softc *sc = device_get_softc(dev);
1515 
1516 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1517 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1518 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1519 }
1520 
1521 static int
1522 asmc_sms_intrfast(void *arg)
1523 {
1524 	uint8_t type;
1525 	device_t dev = (device_t)arg;
1526 	struct asmc_softc *sc = device_get_softc(dev);
1527 	if (!sc->sc_sms_intr_works)
1528 		return (FILTER_HANDLED);
1529 
1530 	mtx_lock_spin(&sc->sc_mtx);
1531 	type = ASMC_INTPORT_READ(sc);
1532 	mtx_unlock_spin(&sc->sc_mtx);
1533 
1534 	sc->sc_sms_intrtype = type;
1535 	asmc_sms_printintr(dev, type);
1536 
1537 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1538 	return (FILTER_HANDLED);
1539 }
1540 
1541 static void
1542 asmc_sms_printintr(device_t dev, uint8_t type)
1543 {
1544 	struct asmc_softc *sc = device_get_softc(dev);
1545 
1546 	switch (type) {
1547 	case ASMC_SMS_INTFF:
1548 		device_printf(dev, "WARNING: possible free fall!\n");
1549 		break;
1550 	case ASMC_SMS_INTHA:
1551 		device_printf(dev, "WARNING: high acceleration detected!\n");
1552 		break;
1553 	case ASMC_SMS_INTSH:
1554 		device_printf(dev, "WARNING: possible shock!\n");
1555 		break;
1556 	case ASMC_ALSL_INT2A:
1557 		/*
1558 		 * This suppresses console and log messages for the ambient
1559 		 * light sensor for models known to generate this interrupt.
1560 		 */
1561 		if (strcmp(sc->sc_model->smc_model, "MacBookPro5,5") == 0 ||
1562 		    strcmp(sc->sc_model->smc_model, "MacBookPro6,2") == 0)
1563 			break;
1564 		/* FALLTHROUGH */
1565 	default:
1566 		device_printf(dev, "unknown interrupt: 0x%x\n", type);
1567 	}
1568 }
1569 
1570 static void
1571 asmc_sms_task(void *arg, int pending)
1572 {
1573 	struct asmc_softc *sc = (struct asmc_softc *)arg;
1574 	char notify[16];
1575 	int type;
1576 
1577 	switch (sc->sc_sms_intrtype) {
1578 	case ASMC_SMS_INTFF:
1579 		type = 2;
1580 		break;
1581 	case ASMC_SMS_INTHA:
1582 		type = 1;
1583 		break;
1584 	case ASMC_SMS_INTSH:
1585 		type = 0;
1586 		break;
1587 	default:
1588 		type = 255;
1589 	}
1590 
1591 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
1592 	devctl_notify("ACPI", "asmc", "SMS", notify);
1593 }
1594 
1595 static int
1596 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1597 {
1598 	device_t dev = (device_t)arg1;
1599 	int error;
1600 	int16_t val;
1601 	int32_t v;
1602 
1603 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1604 	v = (int32_t)val;
1605 	error = sysctl_handle_int(oidp, &v, 0, req);
1606 
1607 	return (error);
1608 }
1609 
1610 static int
1611 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1612 {
1613 	device_t dev = (device_t)arg1;
1614 	int error;
1615 	int16_t val;
1616 	int32_t v;
1617 
1618 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1619 	v = (int32_t)val;
1620 	error = sysctl_handle_int(oidp, &v, 0, req);
1621 
1622 	return (error);
1623 }
1624 
1625 static int
1626 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1627 {
1628 	device_t dev = (device_t)arg1;
1629 	int error;
1630 	int16_t val;
1631 	int32_t v;
1632 
1633 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1634 	v = (int32_t)val;
1635 	error = sysctl_handle_int(oidp, &v, 0, req);
1636 
1637 	return (error);
1638 }
1639 
1640 static int
1641 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1642 {
1643 	device_t dev = (device_t)arg1;
1644 	uint8_t buf[6];
1645 	int error;
1646 	int32_t v;
1647 
1648 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof(buf));
1649 	v = buf[2];
1650 	error = sysctl_handle_int(oidp, &v, 0, req);
1651 
1652 	return (error);
1653 }
1654 
1655 static int
1656 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1657 {
1658 	device_t dev = (device_t)arg1;
1659 	uint8_t buf[6];
1660 	int error;
1661 	int32_t v;
1662 
1663 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof(buf));
1664 	v = buf[2];
1665 	error = sysctl_handle_int(oidp, &v, 0, req);
1666 
1667 	return (error);
1668 }
1669 
1670 static int
1671 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1672 {
1673 	device_t dev = (device_t)arg1;
1674 	uint8_t buf[2];
1675 	int error;
1676 	int v;
1677 
1678 	v = light_control;
1679 	error = sysctl_handle_int(oidp, &v, 0, req);
1680 
1681 	if (error == 0 && req->newptr != NULL) {
1682 		if (v < 0 || v > 255)
1683 			return (EINVAL);
1684 		light_control = v;
1685 		buf[0] = light_control;
1686 		buf[1] = 0x00;
1687 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof(buf));
1688 	}
1689 	return (error);
1690 }
1691 
1692 static int
1693 asmc_mbp_sysctl_light_left_10byte(SYSCTL_HANDLER_ARGS)
1694 {
1695 	device_t dev = (device_t)arg1;
1696 	uint8_t buf[10];
1697 	int error;
1698 	uint32_t v;
1699 
1700 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof(buf));
1701 
1702 	/*
1703 	 * This seems to be a 32 bit big endian value from buf[6] -> buf[9].
1704 	 *
1705 	 * Extract it out manually here, then shift/clamp it.
1706 	 */
1707 	v = be32dec(&buf[6]);
1708 
1709 	/*
1710 	 * Shift out, clamp at 255; that way it looks like the
1711 	 * earlier SMC firmware version responses.
1712 	 */
1713 	v = v >> 8;
1714 	if (v > 255)
1715 		v = 255;
1716 
1717 	error = sysctl_handle_int(oidp, &v, 0, req);
1718 
1719 	return (error);
1720 }
1721 
1722 /*
1723  * Wake-on-LAN convenience sysctl.
1724  * Reading returns 1 if WoL is enabled, 0 if disabled.
1725  * Writing 1 enables WoL, 0 disables it.
1726  */
1727 static int
1728 asmc_wol_sysctl(SYSCTL_HANDLER_ARGS)
1729 {
1730 	device_t dev = (device_t)arg1;
1731 	uint8_t aupo;
1732 	int val, error;
1733 
1734 	/* Read current AUPO value */
1735 	if (asmc_key_read(dev, ASMC_KEY_AUPO, &aupo, 1) != 0)
1736 		return (EIO);
1737 
1738 	val = (aupo != 0) ? 1 : 0;
1739 	error = sysctl_handle_int(oidp, &val, 0, req);
1740 	if (error != 0 || req->newptr == NULL)
1741 		return (error);
1742 
1743 	/* Clamp to 0 or 1 */
1744 	aupo = (val != 0) ? 1 : 0;
1745 
1746 	/* Write AUPO */
1747 	if (asmc_key_write(dev, ASMC_KEY_AUPO, &aupo, 1) != 0)
1748 		return (EIO);
1749 
1750 	return (0);
1751 }
1752