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