1 /* $NetBSD: acpi_button.c,v 1.27 2009/08/04 14:20:40 jmcneill Exp $ */
4 * Copyright 2001, 2003 Wasabi Systems, Inc.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.27 2009/08/04 14:20:40 jmcneill Exp $");
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
49 #include <dev/acpi/acpica.h>
50 #include <dev/acpi/acpireg.h>
51 #include <dev/acpi/acpivar.h>
53 #include <dev/sysmon/sysmonvar.h>
55 struct acpibut_softc
{
56 struct acpi_devnode
*sc_node
; /* our ACPI devnode */
57 struct sysmon_pswitch sc_smpsw
; /* our sysmon glue */
58 int sc_flags
; /* see below */
61 static const char * const power_button_hid
[] = {
66 static const char * const sleep_button_hid
[] = {
71 #define ACPIBUT_F_VERBOSE 0x01 /* verbose events */
73 static int acpibut_match(device_t
, cfdata_t
, void *);
74 static void acpibut_attach(device_t
, device_t
, void *);
76 CFATTACH_DECL_NEW(acpibut
, sizeof(struct acpibut_softc
),
77 acpibut_match
, acpibut_attach
, NULL
, NULL
);
79 static void acpibut_pressed_event(void *);
80 static void acpibut_notify_handler(ACPI_HANDLE
, UINT32
, void *);
85 * Autoconfiguration `match' routine.
88 acpibut_match(device_t parent
, cfdata_t match
, void *aux
)
90 struct acpi_attach_args
*aa
= aux
;
92 if (aa
->aa_node
->ad_type
!= ACPI_TYPE_DEVICE
)
95 if (acpi_match_hid(aa
->aa_node
->ad_devinfo
, power_button_hid
))
98 if (acpi_match_hid(aa
->aa_node
->ad_devinfo
, sleep_button_hid
))
107 * Autoconfiguration `attach' routine.
110 acpibut_attach(device_t parent
, device_t self
, void *aux
)
112 struct acpibut_softc
*sc
= device_private(self
);
113 struct acpi_attach_args
*aa
= aux
;
117 sc
->sc_smpsw
.smpsw_name
= device_xname(self
);
119 if (acpi_match_hid(aa
->aa_node
->ad_devinfo
, power_button_hid
)) {
120 sc
->sc_smpsw
.smpsw_type
= PSWITCH_TYPE_POWER
;
122 } else if (acpi_match_hid(aa
->aa_node
->ad_devinfo
, sleep_button_hid
)) {
123 sc
->sc_smpsw
.smpsw_type
= PSWITCH_TYPE_SLEEP
;
126 panic("acpibut_attach: impossible");
129 aprint_naive(": ACPI %s Button\n", desc
);
130 aprint_normal(": ACPI %s Button\n", desc
);
132 sc
->sc_node
= aa
->aa_node
;
134 if (sysmon_pswitch_register(&sc
->sc_smpsw
) != 0) {
135 aprint_error_dev(self
, "unable to register with sysmon\n");
139 rv
= AcpiInstallNotifyHandler(sc
->sc_node
->ad_handle
,
140 ACPI_DEVICE_NOTIFY
, acpibut_notify_handler
, self
);
141 if (ACPI_FAILURE(rv
)) {
142 aprint_error_dev(self
,
143 "unable to register DEVICE NOTIFY handler: %s\n",
144 AcpiFormatException(rv
));
148 #ifdef ACPI_BUT_DEBUG
149 /* Display the current state when it changes. */
150 sc
->sc_flags
= ACPIBUT_F_VERBOSE
;
153 if (!pmf_device_register(self
, NULL
, NULL
))
154 aprint_error_dev(self
, "couldn't establish power handler\n");
158 * acpibut_pressed_event:
160 * Deal with a button being pressed.
163 acpibut_pressed_event(void *arg
)
166 struct acpibut_softc
*sc
= device_private(dv
);
168 if (sc
->sc_flags
& ACPIBUT_F_VERBOSE
)
169 aprint_verbose_dev(dv
, "button pressed\n");
171 sysmon_pswitch_event(&sc
->sc_smpsw
, PSWITCH_EVENT_PRESSED
);
175 * acpibut_notify_handler:
177 * Callback from ACPI interrupt handler to notify us of an event.
180 acpibut_notify_handler(ACPI_HANDLE handle
, UINT32 notify
, void *context
)
182 device_t dv
= context
;
186 case ACPI_NOTIFY_S0PowerButtonPressed
:
188 case ACPI_NOTIFY_S0SleepButtonPressed
: /* same as above */
190 #ifdef ACPI_BUT_DEBUG
191 aprint_debug_dev(dv
, "received ButtonPressed message\n");
193 rv
= AcpiOsExecute(OSL_NOTIFY_HANDLER
,
194 acpibut_pressed_event
, dv
);
195 if (ACPI_FAILURE(rv
))
197 "WARNING: unable to queue button pressed callback: %s\n",
198 AcpiFormatException(rv
));
201 /* XXX ACPI_NOTIFY_DeviceWake?? */
204 aprint_error_dev(dv
, "received unknown notify message: 0x%x\n",