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