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