Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / acpi / acpi_button.c
blob9a57a7f94df8d89f464619fec2ac48f0cd6322ba
1 /* $NetBSD: acpi_button.c,v 1.27 2009/08/04 14:20:40 jmcneill Exp $ */
3 /*
4 * Copyright 2001, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
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
11 * are met:
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
23 * written permission.
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.
39 * ACPI Button driver.
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[] = {
62 "PNP0C0C",
63 NULL
66 static const char * const sleep_button_hid[] = {
67 "PNP0C0E",
68 NULL
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 *);
83 * acpibut_match:
85 * Autoconfiguration `match' routine.
87 static int
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)
93 return 0;
95 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
96 return 1;
98 if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
99 return 1;
101 return 0;
105 * acpibut_attach:
107 * Autoconfiguration `attach' routine.
109 static void
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;
114 const char *desc;
115 ACPI_STATUS rv;
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;
121 desc = "Power";
122 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
123 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
124 desc = "Sleep";
125 } else {
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");
136 return;
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));
145 return;
148 #ifdef ACPI_BUT_DEBUG
149 /* Display the current state when it changes. */
150 sc->sc_flags = ACPIBUT_F_VERBOSE;
151 #endif
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.
162 static void
163 acpibut_pressed_event(void *arg)
165 device_t dv = 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.
179 static void
180 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
182 device_t dv = context;
183 ACPI_STATUS rv;
185 switch (notify) {
186 case ACPI_NOTIFY_S0PowerButtonPressed:
187 #if 0
188 case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
189 #endif
190 #ifdef ACPI_BUT_DEBUG
191 aprint_debug_dev(dv, "received ButtonPressed message\n");
192 #endif
193 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
194 acpibut_pressed_event, dv);
195 if (ACPI_FAILURE(rv))
196 aprint_error_dev(dv,
197 "WARNING: unable to queue button pressed callback: %s\n",
198 AcpiFormatException(rv));
199 break;
201 /* XXX ACPI_NOTIFY_DeviceWake?? */
203 default:
204 aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
205 notify);