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