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