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