xref: /netbsd-src/sys/dev/acpi/acpi_lid.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: acpi_lid.c,v 1.15 2004/05/01 12:03:48 kochi 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.15 2004/05/01 12:03:48 kochi 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 
55 struct acpilid_softc {
56 	struct device sc_dev;		/* base device glue */
57 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
58 	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
59 };
60 
61 static const char * const lid_hid[] = {
62 	"PNP0C0D",
63 	NULL
64 };
65 
66 static int	acpilid_match(struct device *, struct cfdata *, void *);
67 static void	acpilid_attach(struct device *, struct device *, void *);
68 
69 CFATTACH_DECL(acpilid, sizeof(struct acpilid_softc),
70     acpilid_match, acpilid_attach, NULL, NULL);
71 
72 static void	acpilid_status_changed(void *);
73 static void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *context);
74 
75 /*
76  * acpilid_match:
77  *
78  *	Autoconfiguration `match' routine.
79  */
80 static int
81 acpilid_match(struct device *parent, struct cfdata *match, void *aux)
82 {
83 	struct acpi_attach_args *aa = aux;
84 
85 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
86 		return 0;
87 
88 	return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid);
89 }
90 
91 /*
92  * acpilid_attach:
93  *
94  *	Autoconfiguration `attach' routine.
95  */
96 static void
97 acpilid_attach(struct device *parent, struct device *self, void *aux)
98 {
99 	struct acpilid_softc *sc = (void *) self;
100 	struct acpi_attach_args *aa = aux;
101 	ACPI_STATUS rv;
102 
103 	printf(": ACPI Lid Switch\n");
104 
105 	sc->sc_node = aa->aa_node;
106 
107 	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
108 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
109 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
110 		printf("%s: unable to register with sysmon\n",
111 		    sc->sc_dev.dv_xname);
112 		return;
113 	}
114 
115 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
116 	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, sc);
117 	if (ACPI_FAILURE(rv)) {
118 		printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
119 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
120 		return;
121 	}
122 }
123 
124 /*
125  * acpilid_status_changed:
126  *
127  *	Get, and possibly display, the lid status, and take action.
128  */
129 static void
130 acpilid_status_changed(void *arg)
131 {
132 	struct acpilid_softc *sc = arg;
133 	ACPI_INTEGER status;
134 	ACPI_STATUS rv;
135 
136 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
137 	if (ACPI_FAILURE(rv))
138 		return;
139 
140 	sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ?
141 	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
142 }
143 
144 /*
145  * acpilid_notify_handler:
146  *
147  *	Callback from ACPI interrupt handler to notify us of an event.
148  */
149 static void
150 acpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
151 {
152 	struct acpilid_softc *sc = context;
153 	int rv;
154 
155 	switch (notify) {
156 	case ACPI_NOTIFY_LidStatusChanged:
157 #ifdef ACPI_LID_DEBUG
158 		printf("%s: received LidStatusChanged message\n",
159 		    sc->sc_dev.dv_xname);
160 #endif
161 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
162 		    acpilid_status_changed, sc);
163 		if (ACPI_FAILURE(rv))
164 			printf("%s: WARNING: unable to queue lid change "
165 			    "callback: %s\n", sc->sc_dev.dv_xname,
166 			    AcpiFormatException(rv));
167 		break;
168 
169 	default:
170 		printf("%s: received unknown notify message: 0x%x\n",
171 		    sc->sc_dev.dv_xname, notify);
172 	}
173 }
174