acpi_lid.c revision 1.25
1/*	$NetBSD: acpi_lid.c,v 1.25 2008/03/26 15:31:59 xtraeme 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 Lid Switch driver.
40 */
41
42#include <sys/cdefs.h>
43__KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.25 2008/03/26 15:31:59 xtraeme Exp $");
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/device.h>
48
49#include <dev/acpi/acpica.h>
50#include <dev/acpi/acpireg.h>
51#include <dev/acpi/acpivar.h>
52
53#include <dev/sysmon/sysmonvar.h>
54
55struct acpilid_softc {
56	struct acpi_devnode *sc_node;	/* our ACPI devnode */
57	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
58};
59
60static const char * const lid_hid[] = {
61	"PNP0C0D",
62	NULL
63};
64
65static int	acpilid_match(device_t, cfdata_t, void *);
66static void	acpilid_attach(device_t, device_t, void *);
67
68CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc),
69    acpilid_match, acpilid_attach, NULL, NULL);
70
71static void	acpilid_status_changed(void *);
72static void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *);
73
74static void	acpilid_wake_event(device_t, bool);
75static bool	acpilid_suspend(device_t PMF_FN_PROTO);
76
77/*
78 * acpilid_match:
79 *
80 *	Autoconfiguration `match' routine.
81 */
82static int
83acpilid_match(device_t parent, cfdata_t match, void *aux)
84{
85	struct acpi_attach_args *aa = aux;
86
87	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
88		return 0;
89
90	return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid);
91}
92
93/*
94 * acpilid_attach:
95 *
96 *	Autoconfiguration `attach' routine.
97 */
98static void
99acpilid_attach(device_t parent, device_t self, void *aux)
100{
101	struct acpilid_softc *sc = device_private(self);
102	struct acpi_attach_args *aa = aux;
103	ACPI_STATUS rv;
104
105	aprint_naive(": ACPI Lid Switch\n");
106	aprint_normal(": ACPI Lid Switch\n");
107
108	sc->sc_node = aa->aa_node;
109
110	sc->sc_smpsw.smpsw_name = device_xname(self);
111	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
112	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
113		aprint_error_dev(self, "unable to register with sysmon\n");
114		return;
115	}
116
117	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
118	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self);
119	if (ACPI_FAILURE(rv)) {
120		aprint_error_dev(self,
121		    "unable to register DEVICE NOTIFY handler: %s\n",
122		    AcpiFormatException(rv));
123		return;
124	}
125
126	acpi_set_wake_gpe(sc->sc_node->ad_handle);
127
128	if (!pmf_device_register(self, acpilid_suspend, NULL))
129		aprint_error_dev(self, "couldn't establish power handler\n");
130}
131
132static void
133acpilid_wake_event(device_t dv, bool enable)
134{
135	struct acpilid_softc *sc = device_private(dv);
136
137	ACPI_STATUS rv;
138        ACPI_OBJECT_LIST ArgList;
139        ACPI_OBJECT Arg;
140
141        ArgList.Count = 1;
142	ArgList.Pointer = &Arg;
143
144	Arg.Type = ACPI_TYPE_INTEGER;
145	Arg.Integer.Value = enable ? 1 : 0;
146
147	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_PSW",
148	    &ArgList, NULL);
149	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
150		aprint_error_dev(dv,
151		    "unable to evaluate _PSW handler: %s\n",
152		    AcpiFormatException(rv));
153}
154
155/*
156 * acpilid_status_changed:
157 *
158 *	Get, and possibly display, the lid status, and take action.
159 */
160static void
161acpilid_status_changed(void *arg)
162{
163	struct acpilid_softc *sc = arg;
164	ACPI_INTEGER status;
165	ACPI_STATUS rv;
166
167	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
168	if (ACPI_FAILURE(rv))
169		return;
170
171	sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ?
172	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
173}
174
175/*
176 * acpilid_notify_handler:
177 *
178 *	Callback from ACPI interrupt handler to notify us of an event.
179 */
180static void
181acpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
182{
183	device_t dv = context;
184	struct acpilid_softc *sc = device_private(dv);
185	int rv;
186
187	switch (notify) {
188	case ACPI_NOTIFY_LidStatusChanged:
189#ifdef ACPI_LID_DEBUG
190		printf("%s: received LidStatusChanged message\n",
191		    device_xname(dv));
192#endif
193		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
194		    acpilid_status_changed, sc);
195		if (ACPI_FAILURE(rv))
196			aprint_error_dev(dv,
197			    "WARNING: unable to queue lid change "
198			    "callback: %s\n", AcpiFormatException(rv));
199		break;
200
201	default:
202		aprint_debug_dev(dv,
203		    "received unknown notify message: 0x%x\n", notify);
204	}
205}
206
207static bool
208acpilid_suspend(device_t dv PMF_FN_ARGS)
209{
210	struct acpilid_softc *sc = device_private(dv);
211	ACPI_INTEGER status;
212	ACPI_STATUS rv;
213
214	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
215	if (ACPI_FAILURE(rv))
216		return true;
217
218	acpilid_wake_event(dv, status == 0);
219	return true;
220}
221