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