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