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