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