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