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