xref: /freebsd/sys/dev/asmc/asmc.c (revision a8f3c3b5d4d9dac1dafe4094fe35b30ffdf26178)
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, ASMC_LIGHT_FUNCS_DISABLED,
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, ASMC_LIGHT_FUNCS_DISABLED,
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, ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
301 	  ASMC_FAN_FUNCS,
302 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
319 	  ASMC_FAN_FUNCS,
320 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
338 	  ASMC_FAN_FUNCS2,
339 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
347 	  ASMC_FAN_FUNCS2,
348 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
357 	  ASMC_FAN_FUNCS2,
358 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
366 	  ASMC_FAN_FUNCS2,
367 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
375 	  ASMC_FAN_FUNCS2,
376 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
384 	  ASMC_FAN_FUNCS2,
385 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
393 	  ASMC_FAN_FUNCS,
394 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
402 	  ASMC_FAN_FUNCS,
403 	  ASMC_LIGHT_FUNCS_DISABLED,
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 	  ASMC_SMS_FUNCS_DISABLED,
411 	  ASMC_FAN_FUNCS,
412 	  ASMC_LIGHT_FUNCS_DISABLED,
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, ASMC_LIGHT_FUNCS_DISABLED,
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, ASMC_LIGHT_FUNCS_DISABLED,
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 *
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
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
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
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
831 asmc_resume(device_t dev)
832 {
833 	uint8_t buf[2];
834 
835 	buf[0] = light_control;
836 	buf[1] = 0x00;
837 	asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
838 
839 	return (0);
840 }
841 
842 #ifdef ASMC_DEBUG
843 void asmc_dumpall(device_t dev)
844 {
845 	struct asmc_softc *sc = device_get_softc(dev);
846 	int i;
847 
848 	if (sc->sc_nkeys == 0) {
849 		device_printf(dev, "asmc_dumpall: key count not available\n");
850 		return;
851 	}
852 
853 	device_printf(dev, "asmc_dumpall: dumping %d keys\n", sc->sc_nkeys);
854 	for (i = 0; i < sc->sc_nkeys; i++)
855 		asmc_key_dump(dev, i);
856 }
857 #endif
858 
859 static int
860 asmc_init(device_t dev)
861 {
862 	struct asmc_softc *sc = device_get_softc(dev);
863 	int i, error = 1;
864 	uint8_t buf[4];
865 
866 	if (sc->sc_model->smc_sms_x == NULL)
867 		goto nosms;
868 
869 	/*
870 	 * We are ready to receive interrupts from the SMS.
871 	 */
872 	buf[0] = 0x01;
873 	ASMC_DPRINTF(("intok key\n"));
874 	asmc_key_write(dev, ASMC_KEY_INTOK, buf, 1);
875 	DELAY(50);
876 
877 	/*
878 	 * Initiate the polling intervals.
879 	 */
880 	buf[0] = 20; /* msecs */
881 	ASMC_DPRINTF(("low int key\n"));
882 	asmc_key_write(dev, ASMC_KEY_SMS_LOW_INT, buf, 1);
883 	DELAY(200);
884 
885 	buf[0] = 20; /* msecs */
886 	ASMC_DPRINTF(("high int key\n"));
887 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH_INT, buf, 1);
888 	DELAY(200);
889 
890 	buf[0] = 0x00;
891 	buf[1] = 0x60;
892 	ASMC_DPRINTF(("sms low key\n"));
893 	asmc_key_write(dev, ASMC_KEY_SMS_LOW, buf, 2);
894 	DELAY(200);
895 
896 	buf[0] = 0x01;
897 	buf[1] = 0xc0;
898 	ASMC_DPRINTF(("sms high key\n"));
899 	asmc_key_write(dev, ASMC_KEY_SMS_HIGH, buf, 2);
900 	DELAY(200);
901 
902 	/*
903 	 * I'm not sure what this key does, but it seems to be
904 	 * required.
905 	 */
906 	buf[0] = 0x01;
907 	ASMC_DPRINTF(("sms flag key\n"));
908 	asmc_key_write(dev, ASMC_KEY_SMS_FLAG, buf, 1);
909 	DELAY(100);
910 
911 	sc->sc_sms_intr_works = 0;
912 
913 	/*
914 	 * Retry SMS initialization 1000 times
915 	 * (takes approx. 2 seconds in worst case)
916 	 */
917 	for (i = 0; i < 1000; i++) {
918 		if (asmc_key_read(dev, ASMC_KEY_SMS, buf, 2) == 0 &&
919 		    (buf[0] == ASMC_SMS_INIT1 && buf[1] == ASMC_SMS_INIT2)) {
920 			error = 0;
921 			sc->sc_sms_intr_works = 1;
922 			goto out;
923 		}
924 		buf[0] = ASMC_SMS_INIT1;
925 		buf[1] = ASMC_SMS_INIT2;
926 		ASMC_DPRINTF(("sms key\n"));
927 		asmc_key_write(dev, ASMC_KEY_SMS, buf, 2);
928 		DELAY(50);
929 	}
930 	device_printf(dev, "WARNING: Sudden Motion Sensor not initialized!\n");
931 
932 out:
933 	asmc_sms_calibrate(dev);
934 nosms:
935 	sc->sc_nfan = asmc_fan_count(dev);
936 	if (sc->sc_nfan > ASMC_MAXFANS) {
937 		device_printf(dev, "more than %d fans were detected. Please "
938 		    "report this.\n", ASMC_MAXFANS);
939 		sc->sc_nfan = ASMC_MAXFANS;
940 	}
941 
942 	/*
943 	 * Read and cache the number of SMC keys (32 bit buffer)
944 	 */
945 	if (asmc_key_read(dev, ASMC_NKEYS, buf, 4) == 0) {
946 		sc->sc_nkeys = be32dec(buf);
947 		if (bootverbose)
948 			device_printf(dev, "number of keys: %d\n", sc->sc_nkeys);
949 	} else {
950 		sc->sc_nkeys = 0;
951 	}
952 
953 #ifdef ASMC_DEBUG
954 	asmc_dumpall(dev);
955 #endif
956 
957 	return (error);
958 }
959 
960 /*
961  * We need to make sure that the SMC acks the byte sent.
962  * Just wait up to (amount * 10)  ms.
963  */
964 static int
965 asmc_wait_ack(device_t dev, uint8_t val, int amount)
966 {
967 	struct asmc_softc *sc = device_get_softc(dev);
968 	u_int i;
969 
970 	val = val & ASMC_STATUS_MASK;
971 
972 	for (i = 0; i < amount; i++) {
973 		if ((ASMC_CMDPORT_READ(sc) & ASMC_STATUS_MASK) == val)
974 			return (0);
975 		DELAY(10);
976 	}
977 
978 	return (1);
979 }
980 
981 /*
982  * We need to make sure that the SMC acks the byte sent.
983  * Just wait up to 100 ms.
984  */
985 static int
986 asmc_wait(device_t dev, uint8_t val)
987 {
988 #ifdef ASMC_DEBUG
989 	struct asmc_softc *sc;
990 #endif
991 
992 	if (asmc_wait_ack(dev, val, 1000) == 0)
993 		return (0);
994 
995 #ifdef ASMC_DEBUG
996 	sc = device_get_softc(dev);
997 #endif
998 	val = val & ASMC_STATUS_MASK;
999 
1000 #ifdef ASMC_DEBUG
1001 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, val,
1002 	    ASMC_CMDPORT_READ(sc));
1003 #endif
1004 	return (1);
1005 }
1006 
1007 /*
1008  * Send the given command, retrying up to 10 times if
1009  * the acknowledgement fails.
1010  */
1011 static int
1012 asmc_command(device_t dev, uint8_t command) {
1013 	int i;
1014 	struct asmc_softc *sc = device_get_softc(dev);
1015 
1016 	for (i=0; i < 10; i++) {
1017 		ASMC_CMDPORT_WRITE(sc, command);
1018 		if (asmc_wait_ack(dev, 0x0c, 100) == 0) {
1019 			return (0);
1020 		}
1021 	}
1022 
1023 #ifdef ASMC_DEBUG
1024 	device_printf(dev, "%s failed: 0x%x, 0x%x\n", __func__, command,
1025 	    ASMC_CMDPORT_READ(sc));
1026 #endif
1027 	return (1);
1028 }
1029 
1030 static int
1031 asmc_key_read(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1032 {
1033 	int i, error = 1, try = 0;
1034 	struct asmc_softc *sc = device_get_softc(dev);
1035 
1036 	mtx_lock_spin(&sc->sc_mtx);
1037 
1038 begin:
1039 	if (asmc_command(dev, ASMC_CMDREAD))
1040 		goto out;
1041 
1042 	for (i = 0; i < 4; i++) {
1043 		ASMC_DATAPORT_WRITE(sc, key[i]);
1044 		if (asmc_wait(dev, 0x04))
1045 			goto out;
1046 	}
1047 
1048 	ASMC_DATAPORT_WRITE(sc, len);
1049 
1050 	for (i = 0; i < len; i++) {
1051 		if (asmc_wait(dev, 0x05))
1052 			goto out;
1053 		buf[i] = ASMC_DATAPORT_READ(sc);
1054 	}
1055 
1056 	error = 0;
1057 out:
1058 	if (error) {
1059 		if (++try < 10) goto begin;
1060 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1061 			__func__, key, try);
1062 	}
1063 
1064 	mtx_unlock_spin(&sc->sc_mtx);
1065 
1066 	return (error);
1067 }
1068 
1069 #ifdef ASMC_DEBUG
1070 static int
1071 asmc_key_dump(device_t dev, int number)
1072 {
1073 	struct asmc_softc *sc = device_get_softc(dev);
1074 	char key[5] = { 0 };
1075 	char type[7] = { 0 };
1076 	uint8_t index[4];
1077 	uint8_t v[32];
1078 	uint8_t maxlen;
1079 	int i, error = 1, try = 0;
1080 
1081 	mtx_lock_spin(&sc->sc_mtx);
1082 
1083 	index[0] = (number >> 24) & 0xff;
1084 	index[1] = (number >> 16) & 0xff;
1085 	index[2] = (number >> 8) & 0xff;
1086 	index[3] = (number) & 0xff;
1087 
1088 begin:
1089 	if (asmc_command(dev, 0x12))
1090 		goto out;
1091 
1092 	for (i = 0; i < 4; i++) {
1093 		ASMC_DATAPORT_WRITE(sc, index[i]);
1094 		if (asmc_wait(dev, 0x04))
1095 			goto out;
1096 	}
1097 
1098 	ASMC_DATAPORT_WRITE(sc, 4);
1099 
1100 	for (i = 0; i < 4; i++) {
1101 		if (asmc_wait(dev, 0x05))
1102 			goto out;
1103 		key[i] = ASMC_DATAPORT_READ(sc);
1104 	}
1105 
1106 	/* get type */
1107 	if (asmc_command(dev, 0x13))
1108 		goto out;
1109 
1110 	for (i = 0; i < 4; i++) {
1111 		ASMC_DATAPORT_WRITE(sc, key[i]);
1112 		if (asmc_wait(dev, 0x04))
1113 			goto out;
1114 	}
1115 
1116 	ASMC_DATAPORT_WRITE(sc, 6);
1117 
1118 	for (i = 0; i < 6; i++) {
1119 		if (asmc_wait(dev, 0x05))
1120 			goto out;
1121 		type[i] = ASMC_DATAPORT_READ(sc);
1122 	}
1123 
1124 	error = 0;
1125 out:
1126 	if (error) {
1127 		if (++try < 10) goto begin;
1128 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1129 			__func__, key, try);
1130 		mtx_unlock_spin(&sc->sc_mtx);
1131 	}
1132 	else {
1133 		char buf[1024];
1134 		char buf2[8];
1135 		mtx_unlock_spin(&sc->sc_mtx);
1136 		maxlen = type[0];
1137 		type[0] = ' ';
1138 		type[5] = 0;
1139 		if (maxlen > sizeof(v)) {
1140 			device_printf(dev,
1141 			    "WARNING: cropping maxlen from %d to %zu\n",
1142 			    maxlen, sizeof(v));
1143 			maxlen = sizeof(v);
1144 		}
1145 		for (i = 0; i < sizeof(v); i++) {
1146 			v[i] = 0;
1147 		}
1148 		asmc_key_read(dev, key, v, maxlen);
1149 		snprintf(buf, sizeof(buf), "key %d is: %s, type %s "
1150 		    "(len %d), data", number, key, type, maxlen);
1151 		for (i = 0; i < maxlen; i++) {
1152 			snprintf(buf2, sizeof(buf2), " %02x", v[i]);
1153 			strlcat(buf, buf2, sizeof(buf));
1154 		}
1155 		strlcat(buf, " \n", sizeof(buf));
1156 		device_printf(dev, "%s", buf);
1157 	}
1158 
1159 	return (error);
1160 }
1161 #endif
1162 
1163 static int
1164 asmc_key_write(device_t dev, const char *key, uint8_t *buf, uint8_t len)
1165 {
1166 	int i, error = -1, try = 0;
1167 	struct asmc_softc *sc = device_get_softc(dev);
1168 
1169 	mtx_lock_spin(&sc->sc_mtx);
1170 
1171 begin:
1172 	ASMC_DPRINTF(("cmd port: cmd write\n"));
1173 	if (asmc_command(dev, ASMC_CMDWRITE))
1174 		goto out;
1175 
1176 	ASMC_DPRINTF(("data port: key\n"));
1177 	for (i = 0; i < 4; i++) {
1178 		ASMC_DATAPORT_WRITE(sc, key[i]);
1179 		if (asmc_wait(dev, 0x04))
1180 			goto out;
1181 	}
1182 	ASMC_DPRINTF(("data port: length\n"));
1183 	ASMC_DATAPORT_WRITE(sc, len);
1184 
1185 	ASMC_DPRINTF(("data port: buffer\n"));
1186 	for (i = 0; i < len; i++) {
1187 		if (asmc_wait(dev, 0x04))
1188 			goto out;
1189 		ASMC_DATAPORT_WRITE(sc, buf[i]);
1190 	}
1191 
1192 	error = 0;
1193 out:
1194 	if (error) {
1195 		if (++try < 10) goto begin;
1196 		device_printf(dev,"%s for key %s failed %d times, giving up\n",
1197 			__func__, key, try);
1198 	}
1199 
1200 	mtx_unlock_spin(&sc->sc_mtx);
1201 
1202 	return (error);
1203 
1204 }
1205 
1206 /*
1207  * Fan control functions.
1208  */
1209 static int
1210 asmc_fan_count(device_t dev)
1211 {
1212 	uint8_t buf[1];
1213 
1214 	if (asmc_key_read(dev, ASMC_KEY_FANCOUNT, buf, sizeof buf) != 0)
1215 		return (-1);
1216 
1217 	return (buf[0]);
1218 }
1219 
1220 static int
1221 asmc_fan_getvalue(device_t dev, const char *key, int fan)
1222 {
1223 	int speed;
1224 	uint8_t buf[2];
1225 	char fankey[5];
1226 
1227 	snprintf(fankey, sizeof(fankey), key, fan);
1228 	if (asmc_key_read(dev, fankey, buf, sizeof buf) != 0)
1229 		return (-1);
1230 	speed = (buf[0] << 6) | (buf[1] >> 2);
1231 
1232 	return (speed);
1233 }
1234 
1235 static char*
1236 asmc_fan_getstring(device_t dev, const char *key, int fan, uint8_t *buf, uint8_t buflen)
1237 {
1238 	char fankey[5];
1239 	char* desc;
1240 
1241 	snprintf(fankey, sizeof(fankey), key, fan);
1242 	if (asmc_key_read(dev, fankey, buf, buflen) != 0)
1243 		return (NULL);
1244 	desc = buf+4;
1245 
1246 	return (desc);
1247 }
1248 
1249 static int
1250 asmc_fan_setvalue(device_t dev, const char *key, int fan, int speed)
1251 {
1252 	uint8_t buf[2];
1253 	char fankey[5];
1254 
1255 	speed *= 4;
1256 
1257 	buf[0] = speed>>8;
1258 	buf[1] = speed;
1259 
1260 	snprintf(fankey, sizeof(fankey), key, fan);
1261 	if (asmc_key_write(dev, fankey, buf, sizeof buf) < 0)
1262 		return (-1);
1263 
1264 	return (0);
1265 }
1266 
1267 static int
1268 asmc_mb_sysctl_fanspeed(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_FANSPEED, fan);
1276 	error = sysctl_handle_int(oidp, &v, 0, req);
1277 
1278 	return (error);
1279 }
1280 
1281 static int
1282 asmc_mb_sysctl_fanid(SYSCTL_HANDLER_ARGS)
1283 {
1284 	uint8_t buf[16];
1285 	device_t dev = (device_t) arg1;
1286 	int fan = arg2;
1287 	int error = true;
1288 	char* desc;
1289 
1290 	desc = asmc_fan_getstring(dev, ASMC_KEY_FANID, fan, buf, sizeof(buf));
1291 
1292 	if (desc != NULL)
1293 		error = sysctl_handle_string(oidp, desc, 0, req);
1294 
1295 	return (error);
1296 }
1297 
1298 static int
1299 asmc_mb_sysctl_fansafespeed(SYSCTL_HANDLER_ARGS)
1300 {
1301 	device_t dev = (device_t) arg1;
1302 	int fan = arg2;
1303 	int error;
1304 	int32_t v;
1305 
1306 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANSAFESPEED, fan);
1307 	error = sysctl_handle_int(oidp, &v, 0, req);
1308 
1309 	return (error);
1310 }
1311 
1312 static int
1313 asmc_mb_sysctl_fanminspeed(SYSCTL_HANDLER_ARGS)
1314 {
1315 	device_t dev = (device_t) arg1;
1316 	int fan = arg2;
1317 	int error;
1318 	int32_t v;
1319 
1320 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMINSPEED, fan);
1321 	error = sysctl_handle_int(oidp, &v, 0, req);
1322 
1323 	if (error == 0 && req->newptr != NULL) {
1324 		unsigned int newspeed = v;
1325 		asmc_fan_setvalue(dev, ASMC_KEY_FANMINSPEED, fan, newspeed);
1326 	}
1327 
1328 	return (error);
1329 }
1330 
1331 static int
1332 asmc_mb_sysctl_fanmaxspeed(SYSCTL_HANDLER_ARGS)
1333 {
1334 	device_t dev = (device_t) arg1;
1335 	int fan = arg2;
1336 	int error;
1337 	int32_t v;
1338 
1339 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANMAXSPEED, fan);
1340 	error = sysctl_handle_int(oidp, &v, 0, req);
1341 
1342 	if (error == 0 && req->newptr != NULL) {
1343 		unsigned int newspeed = v;
1344 		asmc_fan_setvalue(dev, ASMC_KEY_FANMAXSPEED, fan, newspeed);
1345 	}
1346 
1347 	return (error);
1348 }
1349 
1350 static int
1351 asmc_mb_sysctl_fantargetspeed(SYSCTL_HANDLER_ARGS)
1352 {
1353 	device_t dev = (device_t) arg1;
1354 	int fan = arg2;
1355 	int error;
1356 	int32_t v;
1357 
1358 	v = asmc_fan_getvalue(dev, ASMC_KEY_FANTARGETSPEED, fan);
1359 	error = sysctl_handle_int(oidp, &v, 0, req);
1360 
1361 	if (error == 0 && req->newptr != NULL) {
1362 		unsigned int newspeed = v;
1363 		asmc_fan_setvalue(dev, ASMC_KEY_FANTARGETSPEED, fan, newspeed);
1364 	}
1365 
1366 	return (error);
1367 }
1368 
1369 static int
1370 asmc_mb_sysctl_fanmanual(SYSCTL_HANDLER_ARGS)
1371 {
1372 	device_t dev = (device_t) arg1;
1373 	int fan = arg2;
1374 	int error;
1375 	int32_t v;
1376 	uint8_t buf[2];
1377 	uint16_t val;
1378 
1379 	/* Read current FS! bitmask (asmc_key_read locks internally) */
1380 	error = asmc_key_read(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf));
1381 	if (error != 0)
1382 		return (error);
1383 
1384 	/* Extract manual bit for this fan (big-endian) */
1385 	val = (buf[0] << 8) | buf[1];
1386 	v = (val >> fan) & 0x01;
1387 
1388 	/* Let sysctl handle the value */
1389 	error = sysctl_handle_int(oidp, &v, 0, req);
1390 
1391 	if (error == 0 && req->newptr != NULL) {
1392 		/* Validate input (0 = auto, 1 = manual) */
1393 		if (v != 0 && v != 1)
1394 			return (EINVAL);
1395 		/* Read-modify-write of FS! bitmask */
1396 		error = asmc_key_read(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf));
1397 		if (error == 0) {
1398 			val = (buf[0] << 8) | buf[1];
1399 
1400 			/* Modify single bit */
1401 			if (v)
1402 				val |= (1 << fan);   /* Set to manual */
1403 			else
1404 				val &= ~(1 << fan);  /* Set to auto */
1405 
1406 			/* Write back */
1407 			buf[0] = val >> 8;
1408 			buf[1] = val & 0xff;
1409 			error = asmc_key_write(dev, ASMC_KEY_FANMANUAL, buf, sizeof(buf));
1410 		}
1411 	}
1412 
1413 	return (error);
1414 }
1415 
1416 /*
1417  * Temperature functions.
1418  */
1419 static int
1420 asmc_temp_getvalue(device_t dev, const char *key)
1421 {
1422 	uint8_t buf[2];
1423 
1424 	/*
1425 	 * Check for invalid temperatures.
1426 	 */
1427 	if (asmc_key_read(dev, key, buf, sizeof buf) != 0)
1428 		return (-1);
1429 
1430 	return (buf[0]);
1431 }
1432 
1433 static int
1434 asmc_temp_sysctl(SYSCTL_HANDLER_ARGS)
1435 {
1436 	device_t dev = (device_t) arg1;
1437 	struct asmc_softc *sc = device_get_softc(dev);
1438 	int error, val;
1439 
1440 	val = asmc_temp_getvalue(dev, sc->sc_model->smc_temps[arg2]);
1441 	error = sysctl_handle_int(oidp, &val, 0, req);
1442 
1443 	return (error);
1444 }
1445 
1446 /*
1447  * Sudden Motion Sensor functions.
1448  */
1449 static int
1450 asmc_sms_read(device_t dev, const char *key, int16_t *val)
1451 {
1452 	uint8_t buf[2];
1453 	int error;
1454 
1455 	/* no need to do locking here as asmc_key_read() already does it */
1456 	switch (key[3]) {
1457 	case 'X':
1458 	case 'Y':
1459 	case 'Z':
1460 		error =	asmc_key_read(dev, key, buf, sizeof buf);
1461 		break;
1462 	default:
1463 		device_printf(dev, "%s called with invalid argument %s\n",
1464 			      __func__, key);
1465 		error = 1;
1466 		goto out;
1467 	}
1468 	*val = ((int16_t)buf[0] << 8) | buf[1];
1469 out:
1470 	return (error);
1471 }
1472 
1473 static void
1474 asmc_sms_calibrate(device_t dev)
1475 {
1476 	struct asmc_softc *sc = device_get_softc(dev);
1477 
1478 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &sc->sms_rest_x);
1479 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &sc->sms_rest_y);
1480 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &sc->sms_rest_z);
1481 }
1482 
1483 static int
1484 asmc_sms_intrfast(void *arg)
1485 {
1486 	uint8_t type;
1487 	device_t dev = (device_t) arg;
1488 	struct asmc_softc *sc = device_get_softc(dev);
1489 	if (!sc->sc_sms_intr_works)
1490 		return (FILTER_HANDLED);
1491 
1492 	mtx_lock_spin(&sc->sc_mtx);
1493 	type = ASMC_INTPORT_READ(sc);
1494 	mtx_unlock_spin(&sc->sc_mtx);
1495 
1496 	sc->sc_sms_intrtype = type;
1497 	asmc_sms_printintr(dev, type);
1498 
1499 	taskqueue_enqueue(sc->sc_sms_tq, &sc->sc_sms_task);
1500 	return (FILTER_HANDLED);
1501 }
1502 
1503 static void
1504 asmc_sms_printintr(device_t dev, uint8_t type)
1505 {
1506 	struct asmc_softc *sc = device_get_softc(dev);
1507 
1508 	switch (type) {
1509 	case ASMC_SMS_INTFF:
1510 		device_printf(dev, "WARNING: possible free fall!\n");
1511 		break;
1512 	case ASMC_SMS_INTHA:
1513 		device_printf(dev, "WARNING: high acceleration detected!\n");
1514 		break;
1515 	case ASMC_SMS_INTSH:
1516 		device_printf(dev, "WARNING: possible shock!\n");
1517 		break;
1518 	case ASMC_ALSL_INT2A:
1519 		/*
1520 		 * This suppresses console and log messages for the ambient
1521 		 * light sensor for models known to generate this interrupt.
1522 		 */
1523 		if (strcmp(sc->sc_model->smc_model, "MacBookPro5,5") == 0 ||
1524 		    strcmp(sc->sc_model->smc_model, "MacBookPro6,2") == 0)
1525 			break;
1526 		/* FALLTHROUGH */
1527 	default:
1528 		device_printf(dev, "unknown interrupt: 0x%x\n", type);
1529 	}
1530 }
1531 
1532 static void
1533 asmc_sms_task(void *arg, int pending)
1534 {
1535 	struct asmc_softc *sc = (struct asmc_softc *)arg;
1536 	char notify[16];
1537 	int type;
1538 
1539 	switch (sc->sc_sms_intrtype) {
1540 	case ASMC_SMS_INTFF:
1541 		type = 2;
1542 		break;
1543 	case ASMC_SMS_INTHA:
1544 		type = 1;
1545 		break;
1546 	case ASMC_SMS_INTSH:
1547 		type = 0;
1548 		break;
1549 	default:
1550 		type = 255;
1551 	}
1552 
1553 	snprintf(notify, sizeof(notify), " notify=0x%x", type);
1554 	devctl_notify("ACPI", "asmc", "SMS", notify);
1555 }
1556 
1557 static int
1558 asmc_mb_sysctl_sms_x(SYSCTL_HANDLER_ARGS)
1559 {
1560 	device_t dev = (device_t) arg1;
1561 	int error;
1562 	int16_t val;
1563 	int32_t v;
1564 
1565 	asmc_sms_read(dev, ASMC_KEY_SMS_X, &val);
1566 	v = (int32_t) val;
1567 	error = sysctl_handle_int(oidp, &v, 0, req);
1568 
1569 	return (error);
1570 }
1571 
1572 static int
1573 asmc_mb_sysctl_sms_y(SYSCTL_HANDLER_ARGS)
1574 {
1575 	device_t dev = (device_t) arg1;
1576 	int error;
1577 	int16_t val;
1578 	int32_t v;
1579 
1580 	asmc_sms_read(dev, ASMC_KEY_SMS_Y, &val);
1581 	v = (int32_t) val;
1582 	error = sysctl_handle_int(oidp, &v, 0, req);
1583 
1584 	return (error);
1585 }
1586 
1587 static int
1588 asmc_mb_sysctl_sms_z(SYSCTL_HANDLER_ARGS)
1589 {
1590 	device_t dev = (device_t) arg1;
1591 	int error;
1592 	int16_t val;
1593 	int32_t v;
1594 
1595 	asmc_sms_read(dev, ASMC_KEY_SMS_Z, &val);
1596 	v = (int32_t) val;
1597 	error = sysctl_handle_int(oidp, &v, 0, req);
1598 
1599 	return (error);
1600 }
1601 
1602 static int
1603 asmc_mbp_sysctl_light_left(SYSCTL_HANDLER_ARGS)
1604 {
1605 	device_t dev = (device_t) arg1;
1606 	uint8_t buf[6];
1607 	int error;
1608 	int32_t v;
1609 
1610 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1611 	v = buf[2];
1612 	error = sysctl_handle_int(oidp, &v, 0, req);
1613 
1614 	return (error);
1615 }
1616 
1617 static int
1618 asmc_mbp_sysctl_light_right(SYSCTL_HANDLER_ARGS)
1619 {
1620 	device_t dev = (device_t) arg1;
1621 	uint8_t buf[6];
1622 	int error;
1623 	int32_t v;
1624 
1625 	asmc_key_read(dev, ASMC_KEY_LIGHTRIGHT, buf, sizeof buf);
1626 	v = buf[2];
1627 	error = sysctl_handle_int(oidp, &v, 0, req);
1628 
1629 	return (error);
1630 }
1631 
1632 static int
1633 asmc_mbp_sysctl_light_control(SYSCTL_HANDLER_ARGS)
1634 {
1635 	device_t dev = (device_t) arg1;
1636 	uint8_t buf[2];
1637 	int error;
1638 	int v;
1639 
1640 	v = light_control;
1641 	error = sysctl_handle_int(oidp, &v, 0, req);
1642 
1643 	if (error == 0 && req->newptr != NULL) {
1644 		if (v < 0 || v > 255)
1645 			return (EINVAL);
1646 		light_control = v;
1647 		buf[0] = light_control;
1648 		buf[1] = 0x00;
1649 		asmc_key_write(dev, ASMC_KEY_LIGHTVALUE, buf, sizeof buf);
1650 	}
1651 	return (error);
1652 }
1653 
1654 static int
1655 asmc_mbp_sysctl_light_left_10byte(SYSCTL_HANDLER_ARGS)
1656 {
1657 	device_t dev = (device_t) arg1;
1658 	uint8_t buf[10];
1659 	int error;
1660 	uint32_t v;
1661 
1662 	asmc_key_read(dev, ASMC_KEY_LIGHTLEFT, buf, sizeof buf);
1663 
1664 	/*
1665 	 * This seems to be a 32 bit big endian value from buf[6] -> buf[9].
1666 	 *
1667 	 * Extract it out manually here, then shift/clamp it.
1668 	 */
1669 	v = be32dec(&buf[6]);
1670 
1671 	/*
1672 	 * Shift out, clamp at 255; that way it looks like the
1673 	 * earlier SMC firmware version responses.
1674 	 */
1675 	v = v >> 8;
1676 	if (v > 255)
1677 		v = 255;
1678 
1679 	error = sysctl_handle_int(oidp, &v, 0, req);
1680 
1681 	return (error);
1682 }
1683