xref: /freebsd/sys/dev/asmc/asmc.c (revision 2774f206809b8fd3a4904fe945f029a414fbc642)
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 	{ NULL, NULL }
375 };
376 
377 #undef ASMC_SMS_FUNCS
378 #undef ASMC_SMS_FUNCS_DISABLED
379 #undef ASMC_FAN_FUNCS
380 #undef ASMC_FAN_FUNCS2
381 #undef ASMC_LIGHT_FUNCS
382 
383 /*
384  * Driver methods.
385  */
386 static device_method_t	asmc_methods[] = {
387 	DEVMETHOD(device_probe,		asmc_probe),
388 	DEVMETHOD(device_attach,	asmc_attach),
389 	DEVMETHOD(device_detach,	asmc_detach),
390 	DEVMETHOD(device_resume,	asmc_resume),
391 	{ 0, 0 }
392 };
393 
394 static driver_t	asmc_driver = {
395 	"asmc",
396 	asmc_methods,
397 	sizeof(struct asmc_softc)
398 };
399 
400 /*
401  * Debugging
402  */
403 #define	_COMPONENT	ACPI_OEM
404 ACPI_MODULE_NAME("ASMC")
405 #ifdef DEBUG
406 #define ASMC_DPRINTF(str)	device_printf(dev, str)
407 #else
408 #define ASMC_DPRINTF(str)
409 #endif
410 
411 /* NB: can't be const */
412 static char *asmc_ids[] = { "APP0001", NULL };
413 
414 static devclass_t asmc_devclass;
415 
416 static unsigned int light_control = 0;
417 
418 DRIVER_MODULE(asmc, acpi, asmc_driver, asmc_devclass, NULL, NULL);
419 MODULE_DEPEND(asmc, acpi, 1, 1, 1);
420 
421 static struct asmc_model *
422 asmc_match(device_t dev)
423 {
424 	int i;
425 	char *model;
426 
427 	model = kern_getenv("smbios.system.product");
428 	if (model == NULL)
429 		return (NULL);
430 
431 	for (i = 0; asmc_models[i].smc_model; i++) {
432 		if (!strncmp(model, asmc_models[i].smc_model, strlen(model))) {
433 			freeenv(model);
434 			return (&asmc_models[i]);
435 		}
436 	}
437 	freeenv(model);
438 
439 	return (NULL);
440 }
441 
442 static int
443 asmc_probe(device_t dev)
444 {
445 	struct asmc_model *model;
446 	int rv;
447 
448 	if (resource_disabled("asmc", 0))
449 		return (ENXIO);
450 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, asmc_ids, NULL);
451 	if (rv > 0)
452 		return (rv);
453 
454 	model = asmc_match(dev);
455 	if (!model) {
456 		device_printf(dev, "model not recognized\n");
457 		return (ENXIO);
458 	}
459 	device_set_desc(dev, model->smc_desc);
460 
461 	return (rv);
462 }
463 
464 static int
465 asmc_attach(device_t dev)
466 {
467 	int i, j;
468 	int ret;
469 	char name[2];
470 	struct asmc_softc *sc = device_get_softc(dev);
471 	struct sysctl_ctx_list *sysctlctx;
472 	struct sysctl_oid *sysctlnode;
473 	struct asmc_model *model;
474 
475 	sc->sc_ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
476 	    &sc->sc_rid_port, RF_ACTIVE);
477 	if (sc->sc_ioport == NULL) {
478 		device_printf(dev, "unable to allocate IO port\n");
479 		return (ENOMEM);
480 	}
481 
482 	sysctlctx  = device_get_sysctl_ctx(dev);
483 	sysctlnode = device_get_sysctl_tree(dev);
484 
485 	model = asmc_match(dev);
486 
487 	mtx_init(&sc->sc_mtx, "asmc", NULL, MTX_SPIN);
488 
489 	sc->sc_model = model;
490 	asmc_init(dev);
491 
492 	/*
493 	 * dev.asmc.n.fan.* tree.
494 	 */
495 	sc->sc_fan_tree[0] = SYSCTL_ADD_NODE(sysctlctx,
496 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "fan",
497 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Fan Root Tree");
498 
499 	for (i = 1; i <= sc->sc_nfan; i++) {
500 		j = i - 1;
501 		name[0] = '0' + j;
502 		name[1] = 0;
503 		sc->sc_fan_tree[i] = SYSCTL_ADD_NODE(sysctlctx,
504 		    SYSCTL_CHILDREN(sc->sc_fan_tree[0]),
505 		    OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
506 		    "Fan Subtree");
507 
508 		SYSCTL_ADD_PROC(sysctlctx,
509 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
510 		    OID_AUTO, "id",
511 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
512 		    dev, j, model->smc_fan_id, "I",
513 		    "Fan ID");
514 
515 		SYSCTL_ADD_PROC(sysctlctx,
516 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
517 		    OID_AUTO, "speed",
518 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
519 		    dev, j, model->smc_fan_speed, "I",
520 		    "Fan speed in RPM");
521 
522 		SYSCTL_ADD_PROC(sysctlctx,
523 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
524 		    OID_AUTO, "safespeed",
525 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
526 		    dev, j, model->smc_fan_safespeed, "I",
527 		    "Fan safe speed in RPM");
528 
529 		SYSCTL_ADD_PROC(sysctlctx,
530 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
531 		    OID_AUTO, "minspeed",
532 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
533 		    dev, j, model->smc_fan_minspeed, "I",
534 		    "Fan minimum speed in RPM");
535 
536 		SYSCTL_ADD_PROC(sysctlctx,
537 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
538 		    OID_AUTO, "maxspeed",
539 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
540 		    dev, j, model->smc_fan_maxspeed, "I",
541 		    "Fan maximum speed in RPM");
542 
543 		SYSCTL_ADD_PROC(sysctlctx,
544 		    SYSCTL_CHILDREN(sc->sc_fan_tree[i]),
545 		    OID_AUTO, "targetspeed",
546 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
547 		    dev, j, model->smc_fan_targetspeed, "I",
548 		    "Fan target speed in RPM");
549 	}
550 
551 	/*
552 	 * dev.asmc.n.temp tree.
553 	 */
554 	sc->sc_temp_tree = SYSCTL_ADD_NODE(sysctlctx,
555 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "temp",
556 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Temperature sensors");
557 
558 	for (i = 0; model->smc_temps[i]; i++) {
559 		SYSCTL_ADD_PROC(sysctlctx,
560 		    SYSCTL_CHILDREN(sc->sc_temp_tree),
561 		    OID_AUTO, model->smc_tempnames[i],
562 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
563 		    dev, i, asmc_temp_sysctl, "I",
564 		    model->smc_tempdescs[i]);
565 	}
566 
567 	/*
568 	 * dev.asmc.n.light
569 	 */
570 	if (model->smc_light_left) {
571 		sc->sc_light_tree = SYSCTL_ADD_NODE(sysctlctx,
572 		    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "light",
573 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
574 		    "Keyboard backlight sensors");
575 
576 		SYSCTL_ADD_PROC(sysctlctx,
577 		    SYSCTL_CHILDREN(sc->sc_light_tree),
578 		    OID_AUTO, "left",
579 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
580 		    dev, 0, model->smc_light_left, "I",
581 		    "Keyboard backlight left sensor");
582 
583 		SYSCTL_ADD_PROC(sysctlctx,
584 		    SYSCTL_CHILDREN(sc->sc_light_tree),
585 		    OID_AUTO, "right",
586 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
587 		    dev, 0, model->smc_light_right, "I",
588 		    "Keyboard backlight right sensor");
589 
590 		SYSCTL_ADD_PROC(sysctlctx,
591 		    SYSCTL_CHILDREN(sc->sc_light_tree),
592 		    OID_AUTO, "control",
593 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY |
594 		    CTLFLAG_NEEDGIANT, dev, 0,
595 		    model->smc_light_control, "I",
596 		    "Keyboard backlight brightness control");
597 	}
598 
599 	if (model->smc_sms_x == NULL)
600 		goto nosms;
601 
602 	/*
603 	 * dev.asmc.n.sms tree.
604 	 */
605 	sc->sc_sms_tree = SYSCTL_ADD_NODE(sysctlctx,
606 	    SYSCTL_CHILDREN(sysctlnode), OID_AUTO, "sms",
607 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Sudden Motion Sensor");
608 
609 	SYSCTL_ADD_PROC(sysctlctx,
610 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
611 	    OID_AUTO, "x",
612 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
613 	    dev, 0, model->smc_sms_x, "I",
614 	    "Sudden Motion Sensor X value");
615 
616 	SYSCTL_ADD_PROC(sysctlctx,
617 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
618 	    OID_AUTO, "y",
619 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
620 	    dev, 0, model->smc_sms_y, "I",
621 	    "Sudden Motion Sensor Y value");
622 
623 	SYSCTL_ADD_PROC(sysctlctx,
624 	    SYSCTL_CHILDREN(sc->sc_sms_tree),
625 	    OID_AUTO, "z",
626 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
627 	    dev, 0, model->smc_sms_z, "I",
628 	    "Sudden Motion Sensor Z value");
629 
630 	/*
631 	 * Need a taskqueue to send devctl_notify() events
632 	 * when the SMS interrupt us.
633 	 *
634 	 * PI_REALTIME is used due to the sensitivity of the
635 	 * interrupt. An interrupt from the SMS means that the
636 	 * disk heads should be turned off as quickly as possible.
637 	 *
638 	 * We only need to do this for the non INTR_FILTER case.
639 	 */
640 	sc->sc_sms_tq = NULL;
641 	TASK_INIT(&sc->sc_sms_task, 0, asmc_sms_task, sc);
642 	sc->sc_sms_tq = taskqueue_create_fast("asmc_taskq", M_WAITOK,
643 	    taskqueue_thread_enqueue, &sc->sc_sms_tq);
644 	taskqueue_start_threads(&sc->sc_sms_tq, 1, PI_REALTIME, "%s sms taskq",
645 	    device_get_nameunit(dev));
646 	/*
647 	 * Allocate an IRQ for the SMS.
648 	 */
649 	sc->sc_rid_irq = 0;
650 	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
651 	    &sc->sc_rid_irq, RF_ACTIVE);
652 	if (sc->sc_irq == NULL) {
653 		device_printf(dev, "unable to allocate IRQ resource\n");
654 		ret = ENXIO;
655 		goto err2;
656 	}
657 
658 	ret = bus_setup_intr(dev, sc->sc_irq,
659 	          INTR_TYPE_MISC | INTR_MPSAFE,
660 	    asmc_sms_intrfast, NULL,
661 	    dev, &sc->sc_cookie);
662 
663 	if (ret) {
664 		device_printf(dev, "unable to setup SMS IRQ\n");
665 		goto err1;
666 	}
667 nosms:
668 	return (0);
669 err1:
670 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq, sc->sc_irq);
671 err2:
672 	bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
673 	    sc->sc_ioport);
674 	mtx_destroy(&sc->sc_mtx);
675 	if (sc->sc_sms_tq)
676 		taskqueue_free(sc->sc_sms_tq);
677 
678 	return (ret);
679 }
680 
681 static int
682 asmc_detach(device_t dev)
683 {
684 	struct asmc_softc *sc = device_get_softc(dev);
685 
686 	if (sc->sc_sms_tq) {
687 		taskqueue_drain(sc->sc_sms_tq, &sc->sc_sms_task);
688 		taskqueue_free(sc->sc_sms_tq);
689 	}
690 	if (sc->sc_cookie)
691 		bus_teardown_intr(dev, sc->sc_irq, sc->sc_cookie);
692 	if (sc->sc_irq)
693 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_rid_irq,
694 		    sc->sc_irq);
695 	if (sc->sc_ioport)
696 		bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_rid_port,
697 		    sc->sc_ioport);
698 	mtx_destroy(&sc->sc_mtx);
699 
700 	return (0);
701 }
702 
703 static int
704 asmc_resume(device_t dev)
705 {
706     uint8_t buf[2];
707     buf[0] = light_control;
708     buf[1] = 0x00;
709     asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
710     return (0);
711 }
712 
713 #ifdef DEBUG
714 void asmc_dumpall(device_t dev)
715 {
716 	int i;
717 
718 	/* XXX magic number */
719 	for (i=0; i < 0x100; i++)
720 		asmc_key_dump(dev, i);
721 }
722 #endif
723 
724 static int
725 asmc_init(device_t dev)
726 {
727 	struct asmc_softc *sc = device_get_softc(dev);
728 	int i, error = 1;
729 	uint8_t buf[4];
730 
731 	if (sc->sc_model->smc_sms_x == NULL)
732 		goto nosms;
733 
734 	/*
735 	 * We are ready to receive interrupts from the SMS.
736 	 */
737 	buf[0] = 0x01;
738 	ASMC_DPRINTF(("intok key\n"));
739 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
740 	DELAY(50);
741 
742 	/*
743 	 * Initiate the polling intervals.
744 	 */
745 	buf[0] = 20; /* msecs */
746 	ASMC_DPRINTF(("low int key\n"));
747 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
748 	DELAY(200);
749 
750 	buf[0] = 20; /* msecs */
751 	ASMC_DPRINTF(("high int key\n"));
752 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
753 	DELAY(200);
754 
755 	buf[0] = 0x00;
756 	buf[1] = 0x60;
757 	ASMC_DPRINTF(("sms low key\n"));
758 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
759 	DELAY(200);
760 
761 	buf[0] = 0x01;
762 	buf[1] = 0xc0;
763 	ASMC_DPRINTF(("sms high key\n"));
764 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
765 	DELAY(200);
766 
767 	/*
768 	 * I'm not sure what this key does, but it seems to be
769 	 * required.
770 	 */
771 	buf[0] = 0x01;
772 	ASMC_DPRINTF(("sms flag key\n"));
773 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
774 	DELAY(100);
775 
776 	sc->sc_sms_intr_works = 0;
777 
778 	/*
779 	 * Retry SMS initialization 1000 times
780 	 * (takes approx. 2 seconds in worst case)
781 	 */
782 	for (i = 0; i < 1000; i++) {
783 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
784 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
785 			error = 0;
786 			sc->sc_sms_intr_works = 1;
787 			goto out;
788 		}
789 		buf[0] = ASMC_SMS_INIT1;
790 		buf[1] = ASMC_SMS_INIT2;
791 		ASMC_DPRINTF(("sms key\n"));
792 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
793 		DELAY(50);
794 	}
795 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
796 
797 out:
798 	asmc_sms_calibrate(dev);
799 nosms:
800 	sc->sc_nfan = asmc_fan_count(dev);
801 	if (sc->sc_nfan > ASMC_MAXFANS) {
802 		device_printf(dev, "more than %d fans were detected. Please "
803 		    "report this.\n", ASMC_MAXFANS);
804 		sc->sc_nfan = ASMC_MAXFANS;
805 	}
806 
807 	if (bootverbose) {
808 		/*
809 		 * The number of keys is a 32 bit buffer
810 		 */
811 		asmc_key_read(dev, ASMC_NKEYS, buf, 4);
812 		device_printf(dev, "number of keys: %d\n", ntohl(*(uint32_t*)buf));
813 	}
814 
815 #ifdef DEBUG
816 	asmc_dumpall(dev);
817 #endif
818 
819 	return (error);
820 }
821 
822 /*
823  * We need to make sure that the SMC acks the byte sent.
824  * Just wait up to (amount * 10)  ms.
825  */
826 static int
827 asmc_wait_ack(device_t dev, uint8_t val, int amount)
828 {
829 	struct asmc_softc *sc = device_get_softc(dev);
830 	u_int i;
831 
832 	val = val & ASMC_STATUS_MASK;
833 
834 	for (i = 0; i < amount; i++) {
835 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
836 			return (0);
837 		DELAY(10);
838 	}
839 
840 	return (1);
841 }
842 
843 /*
844  * We need to make sure that the SMC acks the byte sent.
845  * Just wait up to 100 ms.
846  */
847 static int
848 asmc_wait(device_t dev, uint8_t val)
849 {
850 #ifdef DEBUG
851 	struct asmc_softc *sc;
852 #endif
853 
854 	if (asmc_wait_ack(dev, val, 1000) == 0)
855 		return (0);
856 
857 #ifdef DEBUG
858 	sc = device_get_softc(dev);
859 #endif
860 	val = val & ASMC_STATUS_MASK;
861 
862 #ifdef DEBUG
863 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
864 	    ASMC_CMDPORT_READ(sc));
865 #endif
866 	return (1);
867 }
868 
869 /*
870  * Send the given command, retrying up to 10 times if
871  * the acknowledgement fails.
872  */
873 static int
874 asmc_command(device_t dev, uint8_t command) {
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 static int
1175 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1176 {
1177 	device_t dev = (device_t) arg1;
1178 	int fan = arg2;
1179 	int error;
1180 	int32_t v;
1181 
1182 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1183 	error = sysctl_handle_int(oidp, &v, 0, req);
1184 
1185 	if (error == 0 && req->newptr != NULL) {
1186 		unsigned int newspeed = v;
1187 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1188 	}
1189 
1190 	return (error);
1191 }
1192 
1193 static int
1194 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1195 {
1196 	device_t dev = (device_t) arg1;
1197 	int fan = arg2;
1198 	int error;
1199 	int32_t v;
1200 
1201 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1202 	error = sysctl_handle_int(oidp, &v, 0, req);
1203 
1204 	if (error == 0 && req->newptr != NULL) {
1205 		unsigned int newspeed = v;
1206 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1207 	}
1208 
1209 	return (error);
1210 }
1211 
1212 static int
1213 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1214 {
1215 	device_t dev = (device_t) arg1;
1216 	int fan = arg2;
1217 	int error;
1218 	int32_t v;
1219 
1220 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1221 	error = sysctl_handle_int(oidp, &v, 0, req);
1222 
1223 	if (error == 0 && req->newptr != NULL) {
1224 		unsigned int newspeed = v;
1225 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1226 	}
1227 
1228 	return (error);
1229 }
1230 
1231 /*
1232  * Temperature functions.
1233  */
1234 static int
1235 asmc_temp_getvalue(device_t dev, const char *key)
1236 {
1237 	uint8_t buf[2];
1238 
1239 	/*
1240 	 * Check for invalid temperatures.
1241 	 */
1242 	if (asmc_key_read(dev, key, buf, sizeof buf) != 0)
1243 		return (-1);
1244 
1245 	return (buf[0]);
1246 }
1247 
1248 static int
1249 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1250 {
1251 	device_t dev = (device_t) arg1;
1252 	struct asmc_softc *sc = device_get_softc(dev);
1253 	int error, val;
1254 
1255 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1256 	error = sysctl_handle_int(oidp, &val, 0, req);
1257 
1258 	return (error);
1259 }
1260 
1261 /*
1262  * Sudden Motion Sensor functions.
1263  */
1264 static int
1265 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1266 {
1267 	uint8_t buf[2];
1268 	int error;
1269 
1270 	/* no need to do locking here as asmc_key_read() already does it */
1271 	switch (key[3]) {
1272 	case 'X':
1273 	case 'Y':
1274 	case 'Z':
1275 		error =	asmc_key_read(dev, key, buf, sizeof buf);
1276 		break;
1277 	default:
1278 		device_printf(dev, "%s called with invalid argument %s\n",
1279 			      __func__, key);
1280 		error = 1;
1281 		goto out;
1282 	}
1283 	*val = ((int16_t)buf[0] << 8) | buf[1];
1284 out:
1285 	return (error);
1286 }
1287 
1288 static void
1289 asmc_sms_calibrate(device_t dev)
1290 {
1291 	struct asmc_softc *sc = device_get_softc(dev);
1292 
1293 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1294 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1295 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1296 }
1297 
1298 static int
1299 asmc_sms_intrfast(void *arg)
1300 {
1301 	uint8_t type;
1302 	device_t dev = (device_t) arg;
1303 	struct asmc_softc *sc = device_get_softc(dev);
1304 	if (!sc->sc_sms_intr_works)
1305 		return (FILTER_HANDLED);
1306 
1307 	mtx_lock_spin(&sc->sc_mtx);
1308 	type = ASMC_INTPORT_READ(sc);
1309 	mtx_unlock_spin(&sc->sc_mtx);
1310 
1311 	sc->sc_sms_intrtype = type;
1312 	asmc_sms_printintr(dev, type);
1313 
1314 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1315 	return (FILTER_HANDLED);
1316 }
1317 
1318 static void
1319 asmc_sms_printintr(device_t dev, uint8_t type)
1320 {
1321 
1322 	switch (type) {
1323 	case ASMC_SMS_INTFF:
1324 		device_printf(dev, "WARNING: possible free fall!\n");
1325 		break;
1326 	case ASMC_SMS_INTHA:
1327 		device_printf(dev, "WARNING: high acceleration detected!\n");
1328 		break;
1329 	case ASMC_SMS_INTSH:
1330 		device_printf(dev, "WARNING: possible shock!\n");
1331 		break;
1332 	default:
1333 		device_printf(dev, "%s unknown interrupt\n", __func__);
1334 	}
1335 }
1336 
1337 static void
1338 asmc_sms_task(void *arg, int pending)
1339 {
1340 	struct asmc_softc *sc = (struct asmc_softc *)arg;
1341 	char notify[16];
1342 	int type;
1343 
1344 	switch (sc->sc_sms_intrtype) {
1345 	case ASMC_SMS_INTFF:
1346 		type = 2;
1347 		break;
1348 	case ASMC_SMS_INTHA:
1349 		type = 1;
1350 		break;
1351 	case ASMC_SMS_INTSH:
1352 		type = 0;
1353 		break;
1354 	default:
1355 		type = 255;
1356 	}
1357 
1358 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
1359 	devctl_notify("ACPI", "asmc", "SMS", notify);
1360 }
1361 
1362 static int
1363 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1364 {
1365 	device_t dev = (device_t) arg1;
1366 	int error;
1367 	int16_t val;
1368 	int32_t v;
1369 
1370 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1371 	v = (int32_t) val;
1372 	error = sysctl_handle_int(oidp, &v, 0, req);
1373 
1374 	return (error);
1375 }
1376 
1377 static int
1378 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1379 {
1380 	device_t dev = (device_t) arg1;
1381 	int error;
1382 	int16_t val;
1383 	int32_t v;
1384 
1385 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1386 	v = (int32_t) val;
1387 	error = sysctl_handle_int(oidp, &v, 0, req);
1388 
1389 	return (error);
1390 }
1391 
1392 static int
1393 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1394 {
1395 	device_t dev = (device_t) arg1;
1396 	int error;
1397 	int16_t val;
1398 	int32_t v;
1399 
1400 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1401 	v = (int32_t) val;
1402 	error = sysctl_handle_int(oidp, &v, 0, req);
1403 
1404 	return (error);
1405 }
1406 
1407 static int
1408 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1409 {
1410 	device_t dev = (device_t) arg1;
1411 	uint8_t buf[6];
1412 	int error;
1413 	int32_t v;
1414 
1415 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1416 	v = buf[2];
1417 	error = sysctl_handle_int(oidp, &v, 0, req);
1418 
1419 	return (error);
1420 }
1421 
1422 static int
1423 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1424 {
1425 	device_t dev = (device_t) arg1;
1426 	uint8_t buf[6];
1427 	int error;
1428 	int32_t v;
1429 
1430 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1431 	v = buf[2];
1432 	error = sysctl_handle_int(oidp, &v, 0, req);
1433 
1434 	return (error);
1435 }
1436 
1437 static int
1438 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1439 {
1440 	device_t dev = (device_t) arg1;
1441 	uint8_t buf[2];
1442 	int error;
1443 	int v;
1444 
1445 	v = light_control;
1446 	error = sysctl_handle_int(oidp, &v, 0, req);
1447 
1448 	if (error == 0 && req->newptr != NULL) {
1449 		if (v < 0 || v > 255)
1450 			return (EINVAL);
1451 		light_control = v;
1452 		buf[0] = light_control;
1453 		buf[1] = 0x00;
1454 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
1455 	}
1456 	return (error);
1457 }
1458