xref: /freebsd-12.1/sys/dev/asmc/asmc.c (revision 112e6ea2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007, 2008 Rui Paulo <[email protected]>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 /*
31  * Driver for Apple's System Management Console (SMC).
32  * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
33  *
34  * Inspired by the Linux applesmc driver.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/taskqueue.h>
51 #include <sys/rman.h>
52 
53 #include <machine/resource.h>
54 
55 #include <contrib/dev/acpica/include/acpi.h>
56 
57 #include <dev/acpica/acpivar.h>
58 #include <dev/asmc/asmcvar.h>
59 
60 /*
61  * Device interface.
62  */
63 static int 	asmc_probe(device_t dev);
64 static int 	asmc_attach(device_t dev);
65 static int 	asmc_detach(device_t dev);
66 static int 	asmc_resume(device_t dev);
67 
68 /*
69  * SMC functions.
70  */
71 static int 	asmc_init(device_t dev);
72 static int 	asmc_command(device_t dev, uint8_t command);
73 static int 	asmc_wait(device_t dev, uint8_t val);
74 static int 	asmc_wait_ack(device_t dev, uint8_t val, int amount);
75 static int 	asmc_key_write(device_t dev, const char *key, uint8_t *buf,
76     uint8_t len);
77 static int 	asmc_key_read(device_t dev, const char *key, uint8_t *buf,
78     uint8_t);
79 static int 	asmc_fan_count(device_t dev);
80 static int 	asmc_fan_getvalue(device_t dev, const char *key, int fan);
81 static int 	asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
82 static int 	asmc_temp_getvalue(device_t dev, const char *key);
83 static int 	asmc_sms_read(device_t, const char *key, int16_t *val);
84 static void 	asmc_sms_calibrate(device_t dev);
85 static int 	asmc_sms_intrfast(void *arg);
86 static void 	asmc_sms_printintr(device_t dev, uint8_t);
87 static void 	asmc_sms_task(void *arg, int pending);
88 #ifdef DEBUG
89 void		asmc_dumpall(device_t);
90 static int	asmc_key_dump(device_t, int);
91 #endif
92 
93 /*
94  * Model functions.
95  */
96 static int 	asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
97 static int 	asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
98 static int 	asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
99 static int 	asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
100 static int 	asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
101 static int 	asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
102 static int 	asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
103 static int 	asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
104 static int 	asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
105 static int 	asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
106 static int 	asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
107 static int 	asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
108 static int 	asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
109 
110 struct asmc_model {
111 	const char 	 *smc_model;	/* smbios.system.product env var. */
112 	const char 	 *smc_desc;	/* driver description */
113 
114 	/* Helper functions */
115 	int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
116 	int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
117 	int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
118 	int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
119 	int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
120 	int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
121 	int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
122 	int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
123 	int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
124 	int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
125 	int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
126 	int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
127 
128 	const char 	*smc_temps[ASMC_TEMP_MAX];
129 	const char 	*smc_tempnames[ASMC_TEMP_MAX];
130 	const char 	*smc_tempdescs[ASMC_TEMP_MAX];
131 };
132 
133 static struct asmc_model *asmc_match(device_t dev);
134 
135 #define ASMC_SMS_FUNCS	asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
136 			asmc_mb_sysctl_sms_z
137 
138 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
139 
140 #define ASMC_FAN_FUNCS	asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
141 			asmc_mb_sysctl_fanminspeed, \
142 			asmc_mb_sysctl_fanmaxspeed, \
143 			asmc_mb_sysctl_fantargetspeed
144 
145 #define ASMC_FAN_FUNCS2	asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
146 			asmc_mb_sysctl_fanminspeed, \
147 			asmc_mb_sysctl_fanmaxspeed, \
148 			asmc_mb_sysctl_fantargetspeed
149 
150 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
151 			 asmc_mbp_sysctl_light_right, \
152 			 asmc_mbp_sysctl_light_control
153 
154 struct asmc_model asmc_models[] = {
155 	{
156 	  "MacBook1,1", "Apple SMC MacBook Core Duo",
157 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
158 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
159 	},
160 
161 	{
162 	  "MacBook2,1", "Apple SMC MacBook Core 2 Duo",
163 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
164 	  ASMC_MB_TEMPS, ASMC_MB_TEMPNAMES, ASMC_MB_TEMPDESCS
165 	},
166 
167 	{
168 	  "MacBook3,1", "Apple SMC MacBook Core 2 Duo",
169 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, NULL, NULL, NULL,
170 	  ASMC_MB31_TEMPS, ASMC_MB31_TEMPNAMES, ASMC_MB31_TEMPDESCS
171 	},
172 
173 	{
174 	  "MacBookPro1,1", "Apple SMC MacBook Pro Core Duo (15-inch)",
175 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
176 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
177 	},
178 
179 	{
180 	  "MacBookPro1,2", "Apple SMC MacBook Pro Core Duo (17-inch)",
181 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
182 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
183 	},
184 
185 	{
186 	  "MacBookPro2,1", "Apple SMC MacBook Pro Core 2 Duo (17-inch)",
187 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
188 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
189 	},
190 
191 	{
192 	  "MacBookPro2,2", "Apple SMC MacBook Pro Core 2 Duo (15-inch)",
193 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
194 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
195 	},
196 
197 	{
198 	  "MacBookPro3,1", "Apple SMC MacBook Pro Core 2 Duo (15-inch LED)",
199 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
200 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
201 	},
202 
203 	{
204 	  "MacBookPro3,2", "Apple SMC MacBook Pro Core 2 Duo (17-inch HD)",
205 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
206 	  ASMC_MBP_TEMPS, ASMC_MBP_TEMPNAMES, ASMC_MBP_TEMPDESCS
207 	},
208 
209 	{
210 	  "MacBookPro4,1", "Apple SMC MacBook Pro Core 2 Duo (Penryn)",
211 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
212 	  ASMC_MBP4_TEMPS, ASMC_MBP4_TEMPNAMES, ASMC_MBP4_TEMPDESCS
213 	},
214 
215 	{
216 	  "MacBookPro5,1", "Apple SMC MacBook Pro Core 2 Duo (2008/2009)",
217 	  ASMC_SMS_FUNCS, ASMC_FAN_FUNCS, ASMC_LIGHT_FUNCS,
218 	  ASMC_MBP5_TEMPS, ASMC_MBP5_TEMPNAMES, ASMC_MBP5_TEMPDESCS
219 	},
220 
221 	{
222 	  "MacBookPro8,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 
417 	if (resource_disabled("asmc", 0))
418 		return (ENXIO);
419 	if (ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids) == NULL)
420 		return (ENXIO);
421 
422 	model = asmc_match(dev);
423 	if (!model) {
424 		device_printf(dev, "model not recognized\n");
425 		return (ENXIO);
426 	}
427 	device_set_desc(dev, model->smc_desc);
428 
429 	return (BUS_PROBE_DEFAULT);
430 }
431 
432 static int
433 asmc_attach(device_t dev)
434 {
435 	int i, j;
436 	int ret;
437 	char name[2];
438 	struct asmc_softc *sc = device_get_softc(dev);
439 	struct sysctl_ctx_list *sysctlctx;
440 	struct sysctl_oid *sysctlnode;
441 	struct asmc_model *model;
442 
443 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
444 	    &sc->sc_rid_port, RF_ACTIVE);
445 	if (sc->sc_ioport == NULL) {
446 		device_printf(dev, "unable to allocate IO port\n");
447 		return (ENOMEM);
448 	}
449 
450 	sysctlctx  = device_get_sysctl_ctx(dev);
451 	sysctlnode = device_get_sysctl_tree(dev);
452 
453 	model = asmc_match(dev);
454 
455 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
456 
457 	sc->sc_model = model;
458 	asmc_init(dev);
459 
460 	/*
461 	 * dev.asmc.n.fan.* tree.
462 	 */
463 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
464 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
465 	    CTLFLAG_RD, 0, "Fan Root Tree");
466 
467 	for (i = 1; i <= sc->sc_nfan; i++) {
468 		j = i - 1;
469 		name[0] = '0' + j;
470 		name[1] = 0;
471 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
472 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
473 		    OID_AUTO, name, CTLFLAG_RD, 0,
474 		    "Fan Subtree");
475 
476 		SYSCTL_ADD_PROC(sysctlctx,
477 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
478 		    OID_AUTO, "id", CTLTYPE_STRING | CTLFLAG_RD,
479 		    dev, j, model->smc_fan_id, "I",
480 		    "Fan ID");
481 
482 		SYSCTL_ADD_PROC(sysctlctx,
483 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
484 		    OID_AUTO, "speed", CTLTYPE_INT | CTLFLAG_RD,
485 		    dev, j, model->smc_fan_speed, "I",
486 		    "Fan speed in RPM");
487 
488 		SYSCTL_ADD_PROC(sysctlctx,
489 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
490 		    OID_AUTO, "safespeed",
491 		    CTLTYPE_INT | CTLFLAG_RD,
492 		    dev, j, model->smc_fan_safespeed, "I",
493 		    "Fan safe speed in RPM");
494 
495 		SYSCTL_ADD_PROC(sysctlctx,
496 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
497 		    OID_AUTO, "minspeed",
498 		    CTLTYPE_INT | CTLFLAG_RW,
499 		    dev, j, model->smc_fan_minspeed, "I",
500 		    "Fan minimum speed in RPM");
501 
502 		SYSCTL_ADD_PROC(sysctlctx,
503 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
504 		    OID_AUTO, "maxspeed",
505 		    CTLTYPE_INT | CTLFLAG_RW,
506 		    dev, j, model->smc_fan_maxspeed, "I",
507 		    "Fan maximum speed in RPM");
508 
509 		SYSCTL_ADD_PROC(sysctlctx,
510 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
511 		    OID_AUTO, "targetspeed",
512 		    CTLTYPE_INT | CTLFLAG_RW,
513 		    dev, j, model->smc_fan_targetspeed, "I",
514 		    "Fan target speed in RPM");
515 	}
516 
517 	/*
518 	 * dev.asmc.n.temp tree.
519 	 */
520 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
521 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
522 	    CTLFLAG_RD, 0, "Temperature sensors");
523 
524 	for (i = 0; model->smc_temps[i]; i++) {
525 		SYSCTL_ADD_PROC(sysctlctx,
526 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
527 		    OID_AUTO, model->smc_tempnames[i],
528 		    CTLTYPE_INT | CTLFLAG_RD,
529 		    dev, i, asmc_temp_sysctl, "I",
530 		    model->smc_tempdescs[i]);
531 	}
532 
533 	/*
534 	 * dev.asmc.n.light
535 	 */
536 	if (model->smc_light_left) {
537 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
538 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
539 		    CTLFLAG_RD, 0, "Keyboard backlight sensors");
540 
541 		SYSCTL_ADD_PROC(sysctlctx,
542 		    SYSCTL_CHILDREN(sc->sc_light_tree),
543 		    OID_AUTO, "left", CTLTYPE_INT | CTLFLAG_RD,
544 		    dev, 0, model->smc_light_left, "I",
545 		    "Keyboard backlight left sensor");
546 
547 		SYSCTL_ADD_PROC(sysctlctx,
548 		    SYSCTL_CHILDREN(sc->sc_light_tree),
549 		    OID_AUTO, "right", CTLTYPE_INT | CTLFLAG_RD,
550 		    dev, 0, model->smc_light_right, "I",
551 		    "Keyboard backlight right sensor");
552 
553 		SYSCTL_ADD_PROC(sysctlctx,
554 		    SYSCTL_CHILDREN(sc->sc_light_tree),
555 		    OID_AUTO, "control",
556 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
557 		    dev, 0, model->smc_light_control, "I",
558 		    "Keyboard backlight brightness control");
559 	}
560 
561 	if (model->smc_sms_x == NULL)
562 		goto nosms;
563 
564 	/*
565 	 * dev.asmc.n.sms tree.
566 	 */
567 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
568 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
569 	    CTLFLAG_RD, 0, "Sudden Motion Sensor");
570 
571 	SYSCTL_ADD_PROC(sysctlctx,
572 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
573 	    OID_AUTO, "x", CTLTYPE_INT | CTLFLAG_RD,
574 	    dev, 0, model->smc_sms_x, "I",
575 	    "Sudden Motion Sensor X value");
576 
577 	SYSCTL_ADD_PROC(sysctlctx,
578 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
579 	    OID_AUTO, "y", CTLTYPE_INT | CTLFLAG_RD,
580 	    dev, 0, model->smc_sms_y, "I",
581 	    "Sudden Motion Sensor Y value");
582 
583 	SYSCTL_ADD_PROC(sysctlctx,
584 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
585 	    OID_AUTO, "z", CTLTYPE_INT | CTLFLAG_RD,
586 	    dev, 0, model->smc_sms_z, "I",
587 	    "Sudden Motion Sensor Z value");
588 
589 	/*
590 	 * Need a taskqueue to send devctl_notify() events
591 	 * when the SMS interrupt us.
592 	 *
593 	 * PI_REALTIME is used due to the sensitivity of the
594 	 * interrupt. An interrupt from the SMS means that the
595 	 * disk heads should be turned off as quickly as possible.
596 	 *
597 	 * We only need to do this for the non INTR_FILTER case.
598 	 */
599 	sc->sc_sms_tq = NULL;
600 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
601 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
602 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
603 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
604 	    device_get_nameunit(dev));
605 	/*
606 	 * Allocate an IRQ for the SMS.
607 	 */
608 	sc->sc_rid_irq = 0;
609 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
610 	    &sc->sc_rid_irq, RF_ACTIVE);
611 	if (sc->sc_irq == NULL) {
612 		device_printf(dev, "unable to allocate IRQ resource\n");
613 		ret = ENXIO;
614 		goto err2;
615 	}
616 
617 	ret = bus_setup_intr(dev, sc->sc_irq,
618 	          INTR_TYPE_MISC | INTR_MPSAFE,
619 	    asmc_sms_intrfast, NULL,
620 	    dev, &sc->sc_cookie);
621 
622 	if (ret) {
623 		device_printf(dev, "unable to setup SMS IRQ\n");
624 		goto err1;
625 	}
626 nosms:
627 	return (0);
628 err1:
629 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
630 err2:
631 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
632 	    sc->sc_ioport);
633 	mtx_destroy(&sc->sc_mtx);
634 	if (sc->sc_sms_tq)
635 		taskqueue_free(sc->sc_sms_tq);
636 
637 	return (ret);
638 }
639 
640 static int
641 asmc_detach(device_t dev)
642 {
643 	struct asmc_softc *sc = device_get_softc(dev);
644 
645 	if (sc->sc_sms_tq) {
646 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
647 		taskqueue_free(sc->sc_sms_tq);
648 	}
649 	if (sc->sc_cookie)
650 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
651 	if (sc->sc_irq)
652 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
653 		    sc->sc_irq);
654 	if (sc->sc_ioport)
655 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
656 		    sc->sc_ioport);
657 	mtx_destroy(&sc->sc_mtx);
658 
659 	return (0);
660 }
661 
662 static int
663 asmc_resume(device_t dev)
664 {
665     uint8_t buf[2];
666     buf[0] = light_control;
667     buf[1] = 0x00;
668     asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
669     return (0);
670 }
671 
672 
673 #ifdef DEBUG
674 void asmc_dumpall(device_t dev)
675 {
676 	int i;
677 
678 	/* XXX magic number */
679 	for (i=0; i < 0x100; i++)
680 		asmc_key_dump(dev, i);
681 }
682 #endif
683 
684 static int
685 asmc_init(device_t dev)
686 {
687 	struct asmc_softc *sc = device_get_softc(dev);
688 	int i, error = 1;
689 	uint8_t buf[4];
690 
691 	if (sc->sc_model->smc_sms_x == NULL)
692 		goto nosms;
693 
694 	/*
695 	 * We are ready to receive interrupts from the SMS.
696 	 */
697 	buf[0] = 0x01;
698 	ASMC_DPRINTF(("intok key\n"));
699 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
700 	DELAY(50);
701 
702 	/*
703 	 * Initiate the polling intervals.
704 	 */
705 	buf[0] = 20; /* msecs */
706 	ASMC_DPRINTF(("low int key\n"));
707 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
708 	DELAY(200);
709 
710 	buf[0] = 20; /* msecs */
711 	ASMC_DPRINTF(("high int key\n"));
712 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
713 	DELAY(200);
714 
715 	buf[0] = 0x00;
716 	buf[1] = 0x60;
717 	ASMC_DPRINTF(("sms low key\n"));
718 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
719 	DELAY(200);
720 
721 	buf[0] = 0x01;
722 	buf[1] = 0xc0;
723 	ASMC_DPRINTF(("sms high key\n"));
724 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
725 	DELAY(200);
726 
727 	/*
728 	 * I'm not sure what this key does, but it seems to be
729 	 * required.
730 	 */
731 	buf[0] = 0x01;
732 	ASMC_DPRINTF(("sms flag key\n"));
733 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
734 	DELAY(100);
735 
736 	sc->sc_sms_intr_works = 0;
737 
738 	/*
739 	 * Retry SMS initialization 1000 times
740 	 * (takes approx. 2 seconds in worst case)
741 	 */
742 	for (i = 0; i < 1000; i++) {
743 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
744 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
745 			error = 0;
746 			sc->sc_sms_intr_works = 1;
747 			goto out;
748 		}
749 		buf[0] = ASMC_SMS_INIT1;
750 		buf[1] = ASMC_SMS_INIT2;
751 		ASMC_DPRINTF(("sms key\n"));
752 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
753 		DELAY(50);
754 	}
755 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
756 
757 out:
758 	asmc_sms_calibrate(dev);
759 nosms:
760 	sc->sc_nfan = asmc_fan_count(dev);
761 	if (sc->sc_nfan > ASMC_MAXFANS) {
762 		device_printf(dev, "more than %d fans were detected. Please "
763 		    "report this.\n", ASMC_MAXFANS);
764 		sc->sc_nfan = ASMC_MAXFANS;
765 	}
766 
767 	if (bootverbose) {
768 		/*
769 		 * The number of keys is a 32 bit buffer
770 		 */
771 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
772 		device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
773 	}
774 
775 #ifdef DEBUG
776 	asmc_dumpall(dev);
777 #endif
778 
779 	return (error);
780 }
781 
782 /*
783  * We need to make sure that the SMC acks the byte sent.
784  * Just wait up to (amount * 10)  ms.
785  */
786 static int
787 asmc_wait_ack(device_t dev, uint8_t val, int amount)
788 {
789 	struct asmc_softc *sc = device_get_softc(dev);
790 	u_int i;
791 
792 	val = val & ASMC_STATUS_MASK;
793 
794 	for (i = 0; i < amount; i++) {
795 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
796 			return (0);
797 		DELAY(10);
798 	}
799 
800 	return (1);
801 }
802 
803 /*
804  * We need to make sure that the SMC acks the byte sent.
805  * Just wait up to 100 ms.
806  */
807 static int
808 asmc_wait(device_t dev, uint8_t val)
809 {
810 	struct asmc_softc *sc;
811 
812 	if (asmc_wait_ack(dev, val, 1000) == 0)
813 		return (0);
814 
815 	sc = device_get_softc(dev);
816 	val = val & ASMC_STATUS_MASK;
817 
818 #ifdef DEBUG
819 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
820 	    ASMC_CMDPORT_READ(sc));
821 #endif
822 	return (1);
823 }
824 
825 /*
826  * Send the given command, retrying up to 10 times if
827  * the acknowledgement fails.
828  */
829 static int
830 asmc_command(device_t dev, uint8_t command) {
831 
832 	int i;
833 	struct asmc_softc *sc = device_get_softc(dev);
834 
835 	for (i=0; i < 10; i++) {
836 		ASMC_CMDPORT_WRITE(sc, command);
837 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
838 			return (0);
839 		}
840 	}
841 
842 #ifdef DEBUG
843 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
844 	    ASMC_CMDPORT_READ(sc));
845 #endif
846 	return (1);
847 }
848 
849 static int
850 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
851 {
852 	int i, error = 1, try = 0;
853 	struct asmc_softc *sc = device_get_softc(dev);
854 
855 	mtx_lock_spin(&sc->sc_mtx);
856 
857 begin:
858 	if (asmc_command(dev, ASMC_CMDREAD))
859 		goto out;
860 
861 	for (i = 0; i < 4; i++) {
862 		ASMC_DATAPORT_WRITE(sc, key[i]);
863 		if (asmc_wait(dev, 0x04))
864 			goto out;
865 	}
866 
867 	ASMC_DATAPORT_WRITE(sc, len);
868 
869 	for (i = 0; i < len; i++) {
870 		if (asmc_wait(dev, 0x05))
871 			goto out;
872 		buf[i] = ASMC_DATAPORT_READ(sc);
873 	}
874 
875 	error = 0;
876 out:
877 	if (error) {
878 		if (++try < 10) goto begin;
879 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
880 			__func__, key, try);
881 	}
882 
883 	mtx_unlock_spin(&sc->sc_mtx);
884 
885 	return (error);
886 }
887 
888 #ifdef DEBUG
889 static int
890 asmc_key_dump(device_t dev, int number)
891 {
892 	struct asmc_softc *sc = device_get_softc(dev);
893 	char key[5] = { 0 };
894 	char type[7] = { 0 };
895 	uint8_t index[4];
896 	uint8_t v[32];
897 	uint8_t maxlen;
898 	int i, error = 1, try = 0;
899 
900 	mtx_lock_spin(&sc->sc_mtx);
901 
902 	index[0] = (number >> 24) & 0xff;
903 	index[1] = (number >> 16) & 0xff;
904 	index[2] = (number >> 8) & 0xff;
905 	index[3] = (number) & 0xff;
906 
907 begin:
908 	if (asmc_command(dev, 0x12))
909 		goto out;
910 
911 	for (i = 0; i < 4; i++) {
912 		ASMC_DATAPORT_WRITE(sc, index[i]);
913 		if (asmc_wait(dev, 0x04))
914 			goto out;
915 	}
916 
917 	ASMC_DATAPORT_WRITE(sc, 4);
918 
919 	for (i = 0; i < 4; i++) {
920 		if (asmc_wait(dev, 0x05))
921 			goto out;
922 		key[i] = ASMC_DATAPORT_READ(sc);
923 	}
924 
925 	/* get type */
926 	if (asmc_command(dev, 0x13))
927 		goto out;
928 
929 	for (i = 0; i < 4; i++) {
930 		ASMC_DATAPORT_WRITE(sc, key[i]);
931 		if (asmc_wait(dev, 0x04))
932 			goto out;
933 	}
934 
935 	ASMC_DATAPORT_WRITE(sc, 6);
936 
937 	for (i = 0; i < 6; i++) {
938 		if (asmc_wait(dev, 0x05))
939 			goto out;
940 		type[i] = ASMC_DATAPORT_READ(sc);
941 	}
942 
943 	error = 0;
944 out:
945 	if (error) {
946 		if (++try < 10) goto begin;
947 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
948 			__func__, key, try);
949 		mtx_unlock_spin(&sc->sc_mtx);
950 	}
951 	else {
952 		char buf[1024];
953 		char buf2[8];
954 		mtx_unlock_spin(&sc->sc_mtx);
955 		maxlen = type[0];
956 		type[0] = ' ';
957 		type[5] = 0;
958 		if (maxlen > sizeof(v)) {
959 			device_printf(dev,
960 			    "WARNING: cropping maxlen from %d to %zu\n",
961 			    maxlen, sizeof(v));
962 			maxlen = sizeof(v);
963 		}
964 		for (i = 0; i < sizeof(v); i++) {
965 			v[i] = 0;
966 		}
967 		asmc_key_read(dev, key, v, maxlen);
968 		snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
969 		    "(len %d), data", number, key, type, maxlen);
970 		for (i = 0; i < maxlen; i++) {
971 			snprintf(buf2, sizeof(buf2), " %02x", v[i]);
972 			strlcat(buf, buf2, sizeof(buf));
973 		}
974 		strlcat(buf, " \n", sizeof(buf));
975 		device_printf(dev, "%s", buf);
976 	}
977 
978 	return (error);
979 }
980 #endif
981 
982 static int
983 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
984 {
985 	int i, error = -1, try = 0;
986 	struct asmc_softc *sc = device_get_softc(dev);
987 
988 	mtx_lock_spin(&sc->sc_mtx);
989 
990 begin:
991 	ASMC_DPRINTF(("cmd port: cmd write\n"));
992 	if (asmc_command(dev, ASMC_CMDWRITE))
993 		goto out;
994 
995 	ASMC_DPRINTF(("data port: key\n"));
996 	for (i = 0; i < 4; i++) {
997 		ASMC_DATAPORT_WRITE(sc, key[i]);
998 		if (asmc_wait(dev, 0x04))
999 			goto out;
1000 	}
1001 	ASMC_DPRINTF(("data port: length\n"));
1002 	ASMC_DATAPORT_WRITE(sc, len);
1003 
1004 	ASMC_DPRINTF(("data port: buffer\n"));
1005 	for (i = 0; i < len; i++) {
1006 		if (asmc_wait(dev, 0x04))
1007 			goto out;
1008 		ASMC_DATAPORT_WRITE(sc, buf[i]);
1009 	}
1010 
1011 	error = 0;
1012 out:
1013 	if (error) {
1014 		if (++try < 10) goto begin;
1015 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1016 			__func__, key, try);
1017 	}
1018 
1019 	mtx_unlock_spin(&sc->sc_mtx);
1020 
1021 	return (error);
1022 
1023 }
1024 
1025 /*
1026  * Fan control functions.
1027  */
1028 static int
1029 asmc_fan_count(device_t dev)
1030 {
1031 	uint8_t buf[1];
1032 
1033 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) < 0)
1034 		return (-1);
1035 
1036 	return (buf[0]);
1037 }
1038 
1039 static int
1040 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1041 {
1042 	int speed;
1043 	uint8_t buf[2];
1044 	char fankey[5];
1045 
1046 	snprintf(fankey, sizeof(fankey), key, fan);
1047 	if (asmc_key_read(dev, fankey, buf, sizeof buf) < 0)
1048 		return (-1);
1049 	speed = (buf[0] << 6) | (buf[1] >> 2);
1050 
1051 	return (speed);
1052 }
1053 
1054 static char*
1055 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1056 {
1057 	char fankey[5];
1058 	char* desc;
1059 
1060 	snprintf(fankey, sizeof(fankey), key, fan);
1061 	if (asmc_key_read(dev, fankey, buf, buflen) < 0)
1062 		return (NULL);
1063 	desc = buf+4;
1064 
1065 	return (desc);
1066 }
1067 
1068 static int
1069 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1070 {
1071 	uint8_t buf[2];
1072 	char fankey[5];
1073 
1074 	speed *= 4;
1075 
1076 	buf[0] = speed>>8;
1077 	buf[1] = speed;
1078 
1079 	snprintf(fankey, sizeof(fankey), key, fan);
1080 	if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1081 		return (-1);
1082 
1083 	return (0);
1084 }
1085 
1086 static int
1087 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1088 {
1089 	device_t dev = (device_t) arg1;
1090 	int fan = arg2;
1091 	int error;
1092 	int32_t v;
1093 
1094 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1095 	error = sysctl_handle_int(oidp, &v, 0, req);
1096 
1097 	return (error);
1098 }
1099 
1100 static int
1101 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1102 {
1103 	uint8_t buf[16];
1104 	device_t dev = (device_t) arg1;
1105 	int fan = arg2;
1106 	int error = true;
1107 	char* desc;
1108 
1109 	desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1110 
1111 	if (desc != NULL)
1112 		error = sysctl_handle_string(oidp, desc, 0, req);
1113 
1114 	return (error);
1115 }
1116 
1117 static int
1118 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1119 {
1120 	device_t dev = (device_t) arg1;
1121 	int fan = arg2;
1122 	int error;
1123 	int32_t v;
1124 
1125 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1126 	error = sysctl_handle_int(oidp, &v, 0, req);
1127 
1128 	return (error);
1129 }
1130 
1131 
1132 static int
1133 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1134 {
1135 	device_t dev = (device_t) arg1;
1136 	int fan = arg2;
1137 	int error;
1138 	int32_t v;
1139 
1140 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1141 	error = sysctl_handle_int(oidp, &v, 0, req);
1142 
1143 	if (error == 0 && req->newptr != NULL) {
1144 		unsigned int newspeed = v;
1145 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1146 	}
1147 
1148 	return (error);
1149 }
1150 
1151 static int
1152 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1153 {
1154 	device_t dev = (device_t) arg1;
1155 	int fan = arg2;
1156 	int error;
1157 	int32_t v;
1158 
1159 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1160 	error = sysctl_handle_int(oidp, &v, 0, req);
1161 
1162 	if (error == 0 && req->newptr != NULL) {
1163 		unsigned int newspeed = v;
1164 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1165 	}
1166 
1167 	return (error);
1168 }
1169 
1170 static int
1171 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1172 {
1173 	device_t dev = (device_t) arg1;
1174 	int fan = arg2;
1175 	int error;
1176 	int32_t v;
1177 
1178 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1179 	error = sysctl_handle_int(oidp, &v, 0, req);
1180 
1181 	if (error == 0 && req->newptr != NULL) {
1182 		unsigned int newspeed = v;
1183 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1184 	}
1185 
1186 	return (error);
1187 }
1188 
1189 /*
1190  * Temperature functions.
1191  */
1192 static int
1193 asmc_temp_getvalue(device_t dev, const char *key)
1194 {
1195 	uint8_t buf[2];
1196 
1197 	/*
1198 	 * Check for invalid temperatures.
1199 	 */
1200 	if (asmc_key_read(dev, key, buf, sizeof buf) < 0)
1201 		return (-1);
1202 
1203 	return (buf[0]);
1204 }
1205 
1206 static int
1207 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1208 {
1209 	device_t dev = (device_t) arg1;
1210 	struct asmc_softc *sc = device_get_softc(dev);
1211 	int error, val;
1212 
1213 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1214 	error = sysctl_handle_int(oidp, &val, 0, req);
1215 
1216 	return (error);
1217 }
1218 
1219 /*
1220  * Sudden Motion Sensor functions.
1221  */
1222 static int
1223 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1224 {
1225 	uint8_t buf[2];
1226 	int error;
1227 
1228 	/* no need to do locking here as asmc_key_read() already does it */
1229 	switch (key[3]) {
1230 	case 'X':
1231 	case 'Y':
1232 	case 'Z':
1233 		error =	asmc_key_read(dev, key, buf, sizeof buf);
1234 		break;
1235 	default:
1236 		device_printf(dev, "%s called with invalid argument %s\n",
1237 			      __func__, key);
1238 		error = 1;
1239 		goto out;
1240 	}
1241 	*val = ((int16_t)buf[0] << 8) | buf[1];
1242 out:
1243 	return (error);
1244 }
1245 
1246 static void
1247 asmc_sms_calibrate(device_t dev)
1248 {
1249 	struct asmc_softc *sc = device_get_softc(dev);
1250 
1251 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1252 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1253 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1254 }
1255 
1256 static int
1257 asmc_sms_intrfast(void *arg)
1258 {
1259 	uint8_t type;
1260 	device_t dev = (device_t) arg;
1261 	struct asmc_softc *sc = device_get_softc(dev);
1262 	if (!sc->sc_sms_intr_works)
1263 		return (FILTER_HANDLED);
1264 
1265 	mtx_lock_spin(&sc->sc_mtx);
1266 	type = ASMC_INTPORT_READ(sc);
1267 	mtx_unlock_spin(&sc->sc_mtx);
1268 
1269 	sc->sc_sms_intrtype = type;
1270 	asmc_sms_printintr(dev, type);
1271 
1272 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1273 	return (FILTER_HANDLED);
1274 }
1275 
1276 
1277 
1278 static void
1279 asmc_sms_printintr(device_t dev, uint8_t type)
1280 {
1281 
1282 	switch (type) {
1283 	case ASMC_SMS_INTFF:
1284 		device_printf(dev, "WARNING: possible free fall!\n");
1285 		break;
1286 	case ASMC_SMS_INTHA:
1287 		device_printf(dev, "WARNING: high acceleration detected!\n");
1288 		break;
1289 	case ASMC_SMS_INTSH:
1290 		device_printf(dev, "WARNING: possible shock!\n");
1291 		break;
1292 	default:
1293 		device_printf(dev, "%s unknown interrupt\n", __func__);
1294 	}
1295 }
1296 
1297 static void
1298 asmc_sms_task(void *arg, int pending)
1299 {
1300 	struct asmc_softc *sc = (struct asmc_softc *)arg;
1301 	char notify[16];
1302 	int type;
1303 
1304 	switch (sc->sc_sms_intrtype) {
1305 	case ASMC_SMS_INTFF:
1306 		type = 2;
1307 		break;
1308 	case ASMC_SMS_INTHA:
1309 		type = 1;
1310 		break;
1311 	case ASMC_SMS_INTSH:
1312 		type = 0;
1313 		break;
1314 	default:
1315 		type = 255;
1316 	}
1317 
1318 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
1319 	devctl_notify("ACPI", "asmc", "SMS", notify);
1320 }
1321 
1322 static int
1323 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1324 {
1325 	device_t dev = (device_t) arg1;
1326 	int error;
1327 	int16_t val;
1328 	int32_t v;
1329 
1330 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1331 	v = (int32_t) val;
1332 	error = sysctl_handle_int(oidp, &v, 0, req);
1333 
1334 	return (error);
1335 }
1336 
1337 static int
1338 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1339 {
1340 	device_t dev = (device_t) arg1;
1341 	int error;
1342 	int16_t val;
1343 	int32_t v;
1344 
1345 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1346 	v = (int32_t) val;
1347 	error = sysctl_handle_int(oidp, &v, 0, req);
1348 
1349 	return (error);
1350 }
1351 
1352 static int
1353 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1354 {
1355 	device_t dev = (device_t) arg1;
1356 	int error;
1357 	int16_t val;
1358 	int32_t v;
1359 
1360 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1361 	v = (int32_t) val;
1362 	error = sysctl_handle_int(oidp, &v, 0, req);
1363 
1364 	return (error);
1365 }
1366 
1367 static int
1368 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1369 {
1370 	device_t dev = (device_t) arg1;
1371 	uint8_t buf[6];
1372 	int error;
1373 	int32_t v;
1374 
1375 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1376 	v = buf[2];
1377 	error = sysctl_handle_int(oidp, &v, 0, req);
1378 
1379 	return (error);
1380 }
1381 
1382 static int
1383 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1384 {
1385 	device_t dev = (device_t) arg1;
1386 	uint8_t buf[6];
1387 	int error;
1388 	int32_t v;
1389 
1390 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1391 	v = buf[2];
1392 	error = sysctl_handle_int(oidp, &v, 0, req);
1393 
1394 	return (error);
1395 }
1396 
1397 static int
1398 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1399 {
1400 	device_t dev = (device_t) arg1;
1401 	uint8_t buf[2];
1402 	int error;
1403 	int v;
1404 
1405 	v = light_control;
1406 	error = sysctl_handle_int(oidp, &v, 0, req);
1407 
1408 	if (error == 0 && req->newptr != NULL) {
1409 		if (v < 0 || v > 255)
1410 			return (EINVAL);
1411 		light_control = v;
1412 		buf[0] = light_control;
1413 		buf[1] = 0x00;
1414 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
1415 	}
1416 	return (error);
1417 }
1418