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