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