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