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