1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007, 2008 Rui Paulo <[email protected]>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * Driver for Apple's System Management Console (SMC).
32 * SMC can be found on the MacBook, MacBook Pro and Mac Mini.
33 *
34 * Inspired by the Linux applesmc driver.
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/conf.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/taskqueue.h>
51 #include <sys/rman.h>
52
53 #include <machine/resource.h>
54
55 #include <contrib/dev/acpica/include/acpi.h>
56
57 #include <dev/acpica/acpivar.h>
58 #include <dev/asmc/asmcvar.h>
59
60 /*
61 * Device interface.
62 */
63 static int asmc_probe(device_t dev);
64 static int asmc_attach(device_t dev);
65 static int asmc_detach(device_t dev);
66 static int asmc_resume(device_t dev);
67
68 /*
69 * SMC functions.
70 */
71 static int asmc_init(device_t dev);
72 static int asmc_command(device_t dev, uint8_t command);
73 static int asmc_wait(device_t dev, uint8_t val);
74 static int asmc_wait_ack(device_t dev, uint8_t val, int amount);
75 static int asmc_key_write(device_t dev, const char *key, uint8_t *buf,
76 uint8_t len);
77 static int asmc_key_read(device_t dev, const char *key, uint8_t *buf,
78 uint8_t);
79 static int asmc_fan_count(device_t dev);
80 static int asmc_fan_getvalue(device_t dev, const char *key, int fan);
81 static int asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed);
82 static int asmc_temp_getvalue(device_t dev, const char *key);
83 static int asmc_sms_read(device_t, const char *key, int16_t *val);
84 static void asmc_sms_calibrate(device_t dev);
85 static int asmc_sms_intrfast(void *arg);
86 static void asmc_sms_printintr(device_t dev, uint8_t);
87 static void asmc_sms_task(void *arg, int pending);
88 #ifdef DEBUG
89 void asmc_dumpall(device_t);
90 static int asmc_key_dump(device_t, int);
91 #endif
92
93 /*
94 * Model functions.
95 */
96 static int asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS);
97 static int asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS);
98 static int asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS);
99 static int asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS);
100 static int asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS);
101 static int asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS);
102 static int asmc_temp_sysctl(SYSCTL_HANDLER_ARGS);
103 static int asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS);
104 static int asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS);
105 static int asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS);
106 static int asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS);
107 static int asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS);
108 static int asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS);
109
110 struct asmc_model {
111 const char *smc_model; /* smbios.system.product env var. */
112 const char *smc_desc; /* driver description */
113
114 /* Helper functions */
115 int (*smc_sms_x)(SYSCTL_HANDLER_ARGS);
116 int (*smc_sms_y)(SYSCTL_HANDLER_ARGS);
117 int (*smc_sms_z)(SYSCTL_HANDLER_ARGS);
118 int (*smc_fan_id)(SYSCTL_HANDLER_ARGS);
119 int (*smc_fan_speed)(SYSCTL_HANDLER_ARGS);
120 int (*smc_fan_safespeed)(SYSCTL_HANDLER_ARGS);
121 int (*smc_fan_minspeed)(SYSCTL_HANDLER_ARGS);
122 int (*smc_fan_maxspeed)(SYSCTL_HANDLER_ARGS);
123 int (*smc_fan_targetspeed)(SYSCTL_HANDLER_ARGS);
124 int (*smc_light_left)(SYSCTL_HANDLER_ARGS);
125 int (*smc_light_right)(SYSCTL_HANDLER_ARGS);
126 int (*smc_light_control)(SYSCTL_HANDLER_ARGS);
127
128 const char *smc_temps[ASMC_TEMP_MAX];
129 const char *smc_tempnames[ASMC_TEMP_MAX];
130 const char *smc_tempdescs[ASMC_TEMP_MAX];
131 };
132
133 static struct asmc_model *asmc_match(device_t dev);
134
135 #define ASMC_SMS_FUNCS asmc_mb_sysctl_sms_x, asmc_mb_sysctl_sms_y, \
136 asmc_mb_sysctl_sms_z
137
138 #define ASMC_SMS_FUNCS_DISABLED NULL,NULL,NULL
139
140 #define ASMC_FAN_FUNCS asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, asmc_mb_sysctl_fansafespeed, \
141 asmc_mb_sysctl_fanminspeed, \
142 asmc_mb_sysctl_fanmaxspeed, \
143 asmc_mb_sysctl_fantargetspeed
144
145 #define ASMC_FAN_FUNCS2 asmc_mb_sysctl_fanid, asmc_mb_sysctl_fanspeed, NULL, \
146 asmc_mb_sysctl_fanminspeed, \
147 asmc_mb_sysctl_fanmaxspeed, \
148 asmc_mb_sysctl_fantargetspeed
149
150 #define ASMC_LIGHT_FUNCS asmc_mbp_sysctl_light_left, \
151 asmc_mbp_sysctl_light_right, \
152 asmc_mbp_sysctl_light_control
153
154 #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 *
asmc_match(device_t dev)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
asmc_probe(device_t dev)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
asmc_attach(device_t dev)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
asmc_detach(device_t dev)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
asmc_resume(device_t dev)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
asmc_dumpall(device_t dev)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
asmc_init(device_t dev)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
asmc_wait_ack(device_t dev,uint8_t val,int amount)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
asmc_wait(device_t dev,uint8_t val)848 asmc_wait(device_t dev, uint8_t val)
849 {
850 struct asmc_softc *sc;
851
852 if (asmc_wait_ack(dev, val, 1000) == 0)
853 return (0);
854
855 sc = device_get_softc(dev);
856 val = val & ASMC_STATUS_MASK;
857
858 #ifdef DEBUG
859 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
860 ASMC_CMDPORT_READ(sc));
861 #endif
862 return (1);
863 }
864
865 /*
866 * Send the given command, retrying up to 10 times if
867 * the acknowledgement fails.
868 */
869 static int
asmc_command(device_t dev,uint8_t command)870 asmc_command(device_t dev, uint8_t command) {
871 int i;
872 struct asmc_softc *sc = device_get_softc(dev);
873
874 for (i=0; i < 10; i++) {
875 ASMC_CMDPORT_WRITE(sc, command);
876 if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
877 return (0);
878 }
879 }
880
881 #ifdef DEBUG
882 device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
883 ASMC_CMDPORT_READ(sc));
884 #endif
885 return (1);
886 }
887
888 static int
asmc_key_read(device_t dev,const char * key,uint8_t * buf,uint8_t len)889 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
890 {
891 int i, error = 1, try = 0;
892 struct asmc_softc *sc = device_get_softc(dev);
893
894 mtx_lock_spin(&sc->sc_mtx);
895
896 begin:
897 if (asmc_command(dev, ASMC_CMDREAD))
898 goto out;
899
900 for (i = 0; i < 4; i++) {
901 ASMC_DATAPORT_WRITE(sc, key[i]);
902 if (asmc_wait(dev, 0x04))
903 goto out;
904 }
905
906 ASMC_DATAPORT_WRITE(sc, len);
907
908 for (i = 0; i < len; i++) {
909 if (asmc_wait(dev, 0x05))
910 goto out;
911 buf[i] = ASMC_DATAPORT_READ(sc);
912 }
913
914 error = 0;
915 out:
916 if (error) {
917 if (++try < 10) goto begin;
918 device_printf(dev,"%s for key %s failed %d times, giving up\n",
919 __func__, key, try);
920 }
921
922 mtx_unlock_spin(&sc->sc_mtx);
923
924 return (error);
925 }
926
927 #ifdef DEBUG
928 static int
asmc_key_dump(device_t dev,int number)929 asmc_key_dump(device_t dev, int number)
930 {
931 struct asmc_softc *sc = device_get_softc(dev);
932 char key[5] = { 0 };
933 char type[7] = { 0 };
934 uint8_t index[4];
935 uint8_t v[32];
936 uint8_t maxlen;
937 int i, error = 1, try = 0;
938
939 mtx_lock_spin(&sc->sc_mtx);
940
941 index[0] = (number >> 24) & 0xff;
942 index[1] = (number >> 16) & 0xff;
943 index[2] = (number >> 8) & 0xff;
944 index[3] = (number) & 0xff;
945
946 begin:
947 if (asmc_command(dev, 0x12))
948 goto out;
949
950 for (i = 0; i < 4; i++) {
951 ASMC_DATAPORT_WRITE(sc, index[i]);
952 if (asmc_wait(dev, 0x04))
953 goto out;
954 }
955
956 ASMC_DATAPORT_WRITE(sc, 4);
957
958 for (i = 0; i < 4; i++) {
959 if (asmc_wait(dev, 0x05))
960 goto out;
961 key[i] = ASMC_DATAPORT_READ(sc);
962 }
963
964 /* get type */
965 if (asmc_command(dev, 0x13))
966 goto out;
967
968 for (i = 0; i < 4; i++) {
969 ASMC_DATAPORT_WRITE(sc, key[i]);
970 if (asmc_wait(dev, 0x04))
971 goto out;
972 }
973
974 ASMC_DATAPORT_WRITE(sc, 6);
975
976 for (i = 0; i < 6; i++) {
977 if (asmc_wait(dev, 0x05))
978 goto out;
979 type[i] = ASMC_DATAPORT_READ(sc);
980 }
981
982 error = 0;
983 out:
984 if (error) {
985 if (++try < 10) goto begin;
986 device_printf(dev,"%s for key %s failed %d times, giving up\n",
987 __func__, key, try);
988 mtx_unlock_spin(&sc->sc_mtx);
989 }
990 else {
991 char buf[1024];
992 char buf2[8];
993 mtx_unlock_spin(&sc->sc_mtx);
994 maxlen = type[0];
995 type[0] = ' ';
996 type[5] = 0;
997 if (maxlen > sizeof(v)) {
998 device_printf(dev,
999 "WARNING: cropping maxlen from %d to %zu\n",
1000 maxlen, sizeof(v));
1001 maxlen = sizeof(v);
1002 }
1003 for (i = 0; i < sizeof(v); i++) {
1004 v[i] = 0;
1005 }
1006 asmc_key_read(dev, key, v, maxlen);
1007 snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
1008 "(len %d), data", number, key, type, maxlen);
1009 for (i = 0; i < maxlen; i++) {
1010 snprintf(buf2, sizeof(buf2), " %02x", v[i]);
1011 strlcat(buf, buf2, sizeof(buf));
1012 }
1013 strlcat(buf, " \n", sizeof(buf));
1014 device_printf(dev, "%s", buf);
1015 }
1016
1017 return (error);
1018 }
1019 #endif
1020
1021 static int
asmc_key_write(device_t dev,const char * key,uint8_t * buf,uint8_t len)1022 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1023 {
1024 int i, error = -1, try = 0;
1025 struct asmc_softc *sc = device_get_softc(dev);
1026
1027 mtx_lock_spin(&sc->sc_mtx);
1028
1029 begin:
1030 ASMC_DPRINTF(("cmd port: cmd write\n"));
1031 if (asmc_command(dev, ASMC_CMDWRITE))
1032 goto out;
1033
1034 ASMC_DPRINTF(("data port: key\n"));
1035 for (i = 0; i < 4; i++) {
1036 ASMC_DATAPORT_WRITE(sc, key[i]);
1037 if (asmc_wait(dev, 0x04))
1038 goto out;
1039 }
1040 ASMC_DPRINTF(("data port: length\n"));
1041 ASMC_DATAPORT_WRITE(sc, len);
1042
1043 ASMC_DPRINTF(("data port: buffer\n"));
1044 for (i = 0; i < len; i++) {
1045 if (asmc_wait(dev, 0x04))
1046 goto out;
1047 ASMC_DATAPORT_WRITE(sc, buf[i]);
1048 }
1049
1050 error = 0;
1051 out:
1052 if (error) {
1053 if (++try < 10) goto begin;
1054 device_printf(dev,"%s for key %s failed %d times, giving up\n",
1055 __func__, key, try);
1056 }
1057
1058 mtx_unlock_spin(&sc->sc_mtx);
1059
1060 return (error);
1061
1062 }
1063
1064 /*
1065 * Fan control functions.
1066 */
1067 static int
asmc_fan_count(device_t dev)1068 asmc_fan_count(device_t dev)
1069 {
1070 uint8_t buf[1];
1071
1072 if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) != 0)
1073 return (-1);
1074
1075 return (buf[0]);
1076 }
1077
1078 static int
asmc_fan_getvalue(device_t dev,const char * key,int fan)1079 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1080 {
1081 int speed;
1082 uint8_t buf[2];
1083 char fankey[5];
1084
1085 snprintf(fankey, sizeof(fankey), key, fan);
1086 if (asmc_key_read(dev, fankey, buf, sizeof buf) != 0)
1087 return (-1);
1088 speed = (buf[0] << 6) | (buf[1] >> 2);
1089
1090 return (speed);
1091 }
1092
1093 static char*
asmc_fan_getstring(device_t dev,const char * key,int fan,uint8_t * buf,uint8_t buflen)1094 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1095 {
1096 char fankey[5];
1097 char* desc;
1098
1099 snprintf(fankey, sizeof(fankey), key, fan);
1100 if (asmc_key_read(dev, fankey, buf, buflen) != 0)
1101 return (NULL);
1102 desc = buf+4;
1103
1104 return (desc);
1105 }
1106
1107 static int
asmc_fan_setvalue(device_t dev,const char * key,int fan,int speed)1108 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1109 {
1110 uint8_t buf[2];
1111 char fankey[5];
1112
1113 speed *= 4;
1114
1115 buf[0] = speed>>8;
1116 buf[1] = speed;
1117
1118 snprintf(fankey, sizeof(fankey), key, fan);
1119 if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1120 return (-1);
1121
1122 return (0);
1123 }
1124
1125 static int
asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)1126 asmc_mb_sysctl_fanspeed(SYSCTL_HANDLER_ARGS)
1127 {
1128 device_t dev = (device_t) arg1;
1129 int fan = arg2;
1130 int error;
1131 int32_t v;
1132
1133 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSPEED, fan);
1134 error = sysctl_handle_int(oidp, &v, 0, req);
1135
1136 return (error);
1137 }
1138
1139 static int
asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)1140 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1141 {
1142 uint8_t buf[16];
1143 device_t dev = (device_t) arg1;
1144 int fan = arg2;
1145 int error = true;
1146 char* desc;
1147
1148 desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1149
1150 if (desc != NULL)
1151 error = sysctl_handle_string(oidp, desc, 0, req);
1152
1153 return (error);
1154 }
1155
1156 static int
asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)1157 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1158 {
1159 device_t dev = (device_t) arg1;
1160 int fan = arg2;
1161 int error;
1162 int32_t v;
1163
1164 v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1165 error = sysctl_handle_int(oidp, &v, 0, req);
1166
1167 return (error);
1168 }
1169
1170 static int
asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)1171 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1172 {
1173 device_t dev = (device_t) arg1;
1174 int fan = arg2;
1175 int error;
1176 int32_t v;
1177
1178 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1179 error = sysctl_handle_int(oidp, &v, 0, req);
1180
1181 if (error == 0 && req->newptr != NULL) {
1182 unsigned int newspeed = v;
1183 asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1184 }
1185
1186 return (error);
1187 }
1188
1189 static int
asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)1190 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1191 {
1192 device_t dev = (device_t) arg1;
1193 int fan = arg2;
1194 int error;
1195 int32_t v;
1196
1197 v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1198 error = sysctl_handle_int(oidp, &v, 0, req);
1199
1200 if (error == 0 && req->newptr != NULL) {
1201 unsigned int newspeed = v;
1202 asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1203 }
1204
1205 return (error);
1206 }
1207
1208 static int
asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)1209 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1210 {
1211 device_t dev = (device_t) arg1;
1212 int fan = arg2;
1213 int error;
1214 int32_t v;
1215
1216 v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1217 error = sysctl_handle_int(oidp, &v, 0, req);
1218
1219 if (error == 0 && req->newptr != NULL) {
1220 unsigned int newspeed = v;
1221 asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1222 }
1223
1224 return (error);
1225 }
1226
1227 /*
1228 * Temperature functions.
1229 */
1230 static int
asmc_temp_getvalue(device_t dev,const char * key)1231 asmc_temp_getvalue(device_t dev, const char *key)
1232 {
1233 uint8_t buf[2];
1234
1235 /*
1236 * Check for invalid temperatures.
1237 */
1238 if (asmc_key_read(dev, key, buf, sizeof buf) != 0)
1239 return (-1);
1240
1241 return (buf[0]);
1242 }
1243
1244 static int
asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)1245 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1246 {
1247 device_t dev = (device_t) arg1;
1248 struct asmc_softc *sc = device_get_softc(dev);
1249 int error, val;
1250
1251 val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1252 error = sysctl_handle_int(oidp, &val, 0, req);
1253
1254 return (error);
1255 }
1256
1257 /*
1258 * Sudden Motion Sensor functions.
1259 */
1260 static int
asmc_sms_read(device_t dev,const char * key,int16_t * val)1261 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1262 {
1263 uint8_t buf[2];
1264 int error;
1265
1266 /* no need to do locking here as asmc_key_read() already does it */
1267 switch (key[3]) {
1268 case 'X':
1269 case 'Y':
1270 case 'Z':
1271 error = asmc_key_read(dev, key, buf, sizeof buf);
1272 break;
1273 default:
1274 device_printf(dev, "%s called with invalid argument %s\n",
1275 __func__, key);
1276 error = 1;
1277 goto out;
1278 }
1279 *val = ((int16_t)buf[0] << 8) | buf[1];
1280 out:
1281 return (error);
1282 }
1283
1284 static void
asmc_sms_calibrate(device_t dev)1285 asmc_sms_calibrate(device_t dev)
1286 {
1287 struct asmc_softc *sc = device_get_softc(dev);
1288
1289 asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1290 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1291 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1292 }
1293
1294 static int
asmc_sms_intrfast(void * arg)1295 asmc_sms_intrfast(void *arg)
1296 {
1297 uint8_t type;
1298 device_t dev = (device_t) arg;
1299 struct asmc_softc *sc = device_get_softc(dev);
1300 if (!sc->sc_sms_intr_works)
1301 return (FILTER_HANDLED);
1302
1303 mtx_lock_spin(&sc->sc_mtx);
1304 type = ASMC_INTPORT_READ(sc);
1305 mtx_unlock_spin(&sc->sc_mtx);
1306
1307 sc->sc_sms_intrtype = type;
1308 asmc_sms_printintr(dev, type);
1309
1310 taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1311 return (FILTER_HANDLED);
1312 }
1313
1314 static void
asmc_sms_printintr(device_t dev,uint8_t type)1315 asmc_sms_printintr(device_t dev, uint8_t type)
1316 {
1317
1318 switch (type) {
1319 case ASMC_SMS_INTFF:
1320 device_printf(dev, "WARNING: possible free fall!\n");
1321 break;
1322 case ASMC_SMS_INTHA:
1323 device_printf(dev, "WARNING: high acceleration detected!\n");
1324 break;
1325 case ASMC_SMS_INTSH:
1326 device_printf(dev, "WARNING: possible shock!\n");
1327 break;
1328 default:
1329 device_printf(dev, "%s unknown interrupt\n", __func__);
1330 }
1331 }
1332
1333 static void
asmc_sms_task(void * arg,int pending)1334 asmc_sms_task(void *arg, int pending)
1335 {
1336 struct asmc_softc *sc = (struct asmc_softc *)arg;
1337 char notify[16];
1338 int type;
1339
1340 switch (sc->sc_sms_intrtype) {
1341 case ASMC_SMS_INTFF:
1342 type = 2;
1343 break;
1344 case ASMC_SMS_INTHA:
1345 type = 1;
1346 break;
1347 case ASMC_SMS_INTSH:
1348 type = 0;
1349 break;
1350 default:
1351 type = 255;
1352 }
1353
1354 snprintf(notify, sizeof(notify), " notify=0x%x", type);
1355 devctl_notify("ACPI", "asmc", "SMS", notify);
1356 }
1357
1358 static int
asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)1359 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1360 {
1361 device_t dev = (device_t) arg1;
1362 int error;
1363 int16_t val;
1364 int32_t v;
1365
1366 asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1367 v = (int32_t) val;
1368 error = sysctl_handle_int(oidp, &v, 0, req);
1369
1370 return (error);
1371 }
1372
1373 static int
asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)1374 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1375 {
1376 device_t dev = (device_t) arg1;
1377 int error;
1378 int16_t val;
1379 int32_t v;
1380
1381 asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1382 v = (int32_t) val;
1383 error = sysctl_handle_int(oidp, &v, 0, req);
1384
1385 return (error);
1386 }
1387
1388 static int
asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)1389 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1390 {
1391 device_t dev = (device_t) arg1;
1392 int error;
1393 int16_t val;
1394 int32_t v;
1395
1396 asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1397 v = (int32_t) val;
1398 error = sysctl_handle_int(oidp, &v, 0, req);
1399
1400 return (error);
1401 }
1402
1403 static int
asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)1404 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1405 {
1406 device_t dev = (device_t) arg1;
1407 uint8_t buf[6];
1408 int error;
1409 int32_t v;
1410
1411 asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1412 v = buf[2];
1413 error = sysctl_handle_int(oidp, &v, 0, req);
1414
1415 return (error);
1416 }
1417
1418 static int
asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)1419 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1420 {
1421 device_t dev = (device_t) arg1;
1422 uint8_t buf[6];
1423 int error;
1424 int32_t v;
1425
1426 asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1427 v = buf[2];
1428 error = sysctl_handle_int(oidp, &v, 0, req);
1429
1430 return (error);
1431 }
1432
1433 static int
asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)1434 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1435 {
1436 device_t dev = (device_t) arg1;
1437 uint8_t buf[2];
1438 int error;
1439 int v;
1440
1441 v = light_control;
1442 error = sysctl_handle_int(oidp, &v, 0, req);
1443
1444 if (error == 0 && req->newptr != NULL) {
1445 if (v < 0 || v > 255)
1446 return (EINVAL);
1447 light_control = v;
1448 buf[0] = light_control;
1449 buf[1] = 0x00;
1450 asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
1451 }
1452 return (error);
1453 }
1454