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