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