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