acpi_button.c revision 1.35
1/*	$NetBSD: acpi_button.c,v 1.35 2010/04/15 07:02:24 jruoho Exp $	*/
2
3/*
4 * Copyright 2001, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
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.
24 *
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.
36 */
37
38/*
39 * ACPI Button driver.
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.35 2010/04/15 07:02:24 jruoho Exp $");
44
45#include <sys/param.h>
46#include <sys/device.h>
47#include <sys/module.h>
48#include <sys/systm.h>
49
50#include <dev/acpi/acpireg.h>
51#include <dev/acpi/acpivar.h>
52
53#define _COMPONENT		 ACPI_BUTTON_COMPONENT
54ACPI_MODULE_NAME		 ("acpi_button")
55
56struct acpibut_softc {
57	struct acpi_devnode	*sc_node;
58	struct sysmon_pswitch	 sc_smpsw;
59};
60
61static const char * const power_button_hid[] = {
62	"PNP0C0C",
63	NULL
64};
65
66static const char * const sleep_button_hid[] = {
67	"PNP0C0E",
68	NULL
69};
70
71static int	acpibut_match(device_t, cfdata_t, void *);
72static void	acpibut_attach(device_t, device_t, void *);
73static int	acpibut_detach(device_t, int);
74static void	acpibut_pressed_event(void *);
75static void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
76
77CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
78    acpibut_match, acpibut_attach, acpibut_detach, NULL);
79
80/*
81 * acpibut_match:
82 *
83 *	Autoconfiguration `match' routine.
84 */
85static int
86acpibut_match(device_t parent, cfdata_t match, void *aux)
87{
88	struct acpi_attach_args *aa = aux;
89
90	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
91		return 0;
92
93	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
94		return 1;
95
96	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
97		return 1;
98
99	return 0;
100}
101
102/*
103 * acpibut_attach:
104 *
105 *	Autoconfiguration `attach' routine.
106 */
107static void
108acpibut_attach(device_t parent, device_t self, void *aux)
109{
110	struct acpibut_softc *sc = device_private(self);
111	struct acpi_attach_args *aa = aux;
112	const char *desc;
113
114	sc->sc_smpsw.smpsw_name = device_xname(self);
115
116	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
117		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
118		desc = "Power";
119	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
120		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
121		desc = "Sleep";
122	} else
123		panic("%s: impossible", __func__);
124
125	aprint_naive(": ACPI %s Button\n", desc);
126	aprint_normal(": ACPI %s Button\n", desc);
127
128	sc->sc_node = aa->aa_node;
129
130	(void)pmf_device_register(self, NULL, NULL);
131	(void)sysmon_pswitch_register(&sc->sc_smpsw);
132	(void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
133}
134
135/*
136 * acpibut_detach:
137 *
138 *	Autoconfiguration `detach' routine.
139 */
140static int
141acpibut_detach(device_t self, int flags)
142{
143	struct acpibut_softc *sc = device_private(self);
144
145	pmf_device_deregister(self);
146	acpi_deregister_notify(sc->sc_node);
147	sysmon_pswitch_unregister(&sc->sc_smpsw);
148
149	return 0;
150}
151
152/*
153 * acpibut_pressed_event:
154 *
155 *	Deal with a button being pressed.
156 */
157static void
158acpibut_pressed_event(void *arg)
159{
160	device_t dv = arg;
161	struct acpibut_softc *sc = device_private(dv);
162
163	aprint_debug_dev(dv, "button pressed\n");
164	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
165}
166
167/*
168 * acpibut_notify_handler:
169 *
170 *	Callback from ACPI interrupt handler to notify us of an event.
171 */
172static void
173acpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
174{
175	static const int handler = OSL_NOTIFY_HANDLER;
176	device_t dv = context;
177
178	switch (notify) {
179
180     /* case ACPI_NOTIFY_S0SleepButtonPressed: */
181	case ACPI_NOTIFY_S0PowerButtonPressed:
182		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
183		break;
184
185	default:
186		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
187	}
188}
189
190#ifdef _MODULE
191
192MODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
193CFDRIVER_DECL(acpibut, DV_DULL, NULL);
194
195static int acpibutloc[] = { -1 };
196extern struct cfattach acpibut_ca;
197
198static struct cfparent acpiparent = {
199	"acpinodebus", NULL, DVUNIT_ANY
200};
201
202static struct cfdata acpibut_cfdata[] = {
203	{
204		.cf_name = "acpibut",
205		.cf_atname = "acpibut",
206		.cf_unit = 0,
207		.cf_fstate = FSTATE_STAR,
208		.cf_loc = acpibutloc,
209		.cf_flags = 0,
210		.cf_pspec = &acpiparent,
211	},
212
213	{ NULL }
214};
215
216static int
217acpibut_modcmd(modcmd_t cmd, void *context)
218{
219	int err;
220
221	switch (cmd) {
222
223	case MODULE_CMD_INIT:
224
225		err = config_cfdriver_attach(&acpibut_cd);
226
227		if (err != 0)
228			return err;
229
230		err = config_cfattach_attach("acpibut", &acpibut_ca);
231
232		if (err != 0) {
233			config_cfdriver_detach(&acpibut_cd);
234			return err;
235		}
236
237		err = config_cfdata_attach(acpibut_cfdata, 1);
238
239		if (err != 0) {
240			config_cfattach_detach("acpibut", &acpibut_ca);
241			config_cfdriver_detach(&acpibut_cd);
242			return err;
243		}
244
245		return 0;
246
247	case MODULE_CMD_FINI:
248
249		err = config_cfdata_detach(acpibut_cfdata);
250
251		if (err != 0)
252			return err;
253
254		config_cfattach_detach("acpibut", &acpibut_ca);
255		config_cfdriver_detach(&acpibut_cd);
256
257		return 0;
258
259	default:
260		return ENOTTY;
261	}
262}
263
264#endif	/* _MODULE */
265