xref: /netbsd-src/sys/dev/acpi/acpi_lid.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: acpi_lid.c,v 1.41 2010/08/24 15:03:34 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 Lid Switch driver.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.41 2010/08/24 15:03:34 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_LID_COMPONENT
54 ACPI_MODULE_NAME		 ("acpi_lid")
55 
56 #define ACPI_NOTIFY_LID		 0x80
57 
58 struct acpilid_softc {
59 	struct acpi_devnode	*sc_node;
60 	struct sysmon_pswitch	 sc_smpsw;
61 	uint64_t		 sc_status;
62 };
63 
64 static const char * const lid_hid[] = {
65 	"PNP0C0D",
66 	NULL
67 };
68 
69 static int	acpilid_match(device_t, cfdata_t, void *);
70 static void	acpilid_attach(device_t, device_t, void *);
71 static int	acpilid_detach(device_t, int);
72 static void	acpilid_status_changed(void *);
73 static void	acpilid_notify_handler(ACPI_HANDLE, uint32_t, void *);
74 
75 CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc),
76     acpilid_match, acpilid_attach, acpilid_detach, NULL);
77 
78 /*
79  * acpilid_match:
80  *
81  *	Autoconfiguration `match' routine.
82  */
83 static int
84 acpilid_match(device_t parent, cfdata_t match, void *aux)
85 {
86 	struct acpi_attach_args *aa = aux;
87 
88 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
89 		return 0;
90 
91 	return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid);
92 }
93 
94 /*
95  * acpilid_attach:
96  *
97  *	Autoconfiguration `attach' routine.
98  */
99 static void
100 acpilid_attach(device_t parent, device_t self, void *aux)
101 {
102 	struct acpilid_softc *sc = device_private(self);
103 	struct acpi_attach_args *aa = aux;
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 
113 	(void)pmf_device_register(self, NULL, NULL);
114 	(void)sysmon_pswitch_register(&sc->sc_smpsw);
115 	(void)acpi_register_notify(sc->sc_node, acpilid_notify_handler);
116 }
117 
118 static int
119 acpilid_detach(device_t self, int flags)
120 {
121 	struct acpilid_softc *sc = device_private(self);
122 
123 	pmf_device_deregister(self);
124 	acpi_deregister_notify(sc->sc_node);
125 	sysmon_pswitch_unregister(&sc->sc_smpsw);
126 
127 	return 0;
128 }
129 
130 /*
131  * acpilid_status_changed:
132  *
133  *	Get, and possibly display, the lid status, and take action.
134  */
135 static void
136 acpilid_status_changed(void *arg)
137 {
138 	device_t dv = arg;
139 	struct acpilid_softc *sc = device_private(dv);
140 	ACPI_STATUS rv;
141 
142 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &sc->sc_status);
143 
144 	if (ACPI_FAILURE(rv))
145 		return;
146 
147 	sysmon_pswitch_event(&sc->sc_smpsw, (sc->sc_status == 0) ?
148 	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
149 }
150 
151 /*
152  * acpilid_notify_handler:
153  *
154  *	Callback from ACPI interrupt handler to notify us of an event.
155  */
156 static void
157 acpilid_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
158 {
159 	static const int handler = OSL_NOTIFY_HANDLER;
160 	device_t dv = context;
161 
162 	switch (notify) {
163 
164 	case ACPI_NOTIFY_LID:
165 		(void)AcpiOsExecute(handler, acpilid_status_changed, dv);
166 		break;
167 
168 	case ACPI_NOTIFY_DEVICE_WAKE:
169 		break;
170 
171 	default:
172 		aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
173 	}
174 }
175 
176 #ifdef _MODULE
177 
178 MODULE(MODULE_CLASS_DRIVER, acpilid, NULL);
179 CFDRIVER_DECL(acpilid, DV_DULL, NULL);
180 
181 static int acpilidloc[] = { -1 };
182 extern struct cfattach acpilid_ca;
183 
184 static struct cfparent acpiparent = {
185 	"acpinodebus", NULL, DVUNIT_ANY
186 };
187 
188 static struct cfdata acpilid_cfdata[] = {
189 	{
190 		.cf_name = "acpilid",
191 		.cf_atname = "acpilid",
192 		.cf_unit = 0,
193 		.cf_fstate = FSTATE_STAR,
194 		.cf_loc = acpilidloc,
195 		.cf_flags = 0,
196 		.cf_pspec = &acpiparent,
197 	},
198 
199 	{ NULL }
200 };
201 
202 static int
203 acpilid_modcmd(modcmd_t cmd, void *context)
204 {
205 	int err;
206 
207 	switch (cmd) {
208 
209 	case MODULE_CMD_INIT:
210 
211 		err = config_cfdriver_attach(&acpilid_cd);
212 
213 		if (err != 0)
214 			return err;
215 
216 		err = config_cfattach_attach("acpilid", &acpilid_ca);
217 
218 		if (err != 0) {
219 			config_cfdriver_detach(&acpilid_cd);
220 			return err;
221 		}
222 
223 		err = config_cfdata_attach(acpilid_cfdata, 1);
224 
225 		if (err != 0) {
226 			config_cfattach_detach("acpilid", &acpilid_ca);
227 			config_cfdriver_detach(&acpilid_cd);
228 			return err;
229 		}
230 
231 		return 0;
232 
233 	case MODULE_CMD_FINI:
234 
235 		err = config_cfdata_detach(acpilid_cfdata);
236 
237 		if (err != 0)
238 			return err;
239 
240 		config_cfattach_detach("acpilid", &acpilid_ca);
241 		config_cfdriver_detach(&acpilid_cd);
242 
243 		return 0;
244 
245 	default:
246 		return ENOTTY;
247 	}
248 }
249 
250 #endif	/* _MODULE */
251