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