xref: /netbsd-src/sys/dev/acpi/acpi_lid.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: acpi_lid.c,v 1.2 2001/09/29 05:36:49 thorpej Exp $	*/
2 
3 /*
4  * Copyright 2001 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/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 
46 #include <dev/acpi/acpica.h>
47 #include <dev/acpi/acpireg.h>
48 #include <dev/acpi/acpivar.h>
49 
50 struct acpilid_softc {
51 	struct device sc_dev;		/* base device glue */
52 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
53 	int sc_flags;			/* see below */
54 };
55 
56 #define	ACPILID_F_VERBOSE		0x01	/* verbose events */
57 
58 int	acpilid_match(struct device *, struct cfdata *, void *);
59 void	acpilid_attach(struct device *, struct device *, void *);
60 
61 struct cfattach acpilid_ca = {
62 	sizeof(struct acpilid_softc), acpilid_match, acpilid_attach,
63 };
64 
65 void	acpilid_status_changed(void *);
66 void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *context);
67 
68 /*
69  * acpilid_match:
70  *
71  *	Autoconfiguration `match' routine.
72  */
73 int
74 acpilid_match(struct device *parent, struct cfdata *match, void *aux)
75 {
76 	struct acpi_attach_args *aa = aux;
77 
78 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
79 		return (0);
80 
81 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0D") == 0)
82 		return (1);
83 
84 	return (0);
85 }
86 
87 /*
88  * acpilid_attach:
89  *
90  *	Autoconfiguration `attach' routine.
91  */
92 void
93 acpilid_attach(struct device *parent, struct device *self, void *aux)
94 {
95 	struct acpilid_softc *sc = (void *) self;
96 	struct acpi_attach_args *aa = aux;
97 	ACPI_STATUS rv;
98 
99 	printf(": ACPI Lid Switch\n");
100 
101 	sc->sc_node = aa->aa_node;
102 
103 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
104 	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, sc);
105 	if (rv != AE_OK) {
106 		printf("%s: unable to register device notify handler: %d\n",
107 		    sc->sc_dev.dv_xname, rv);
108 		return;
109 	}
110 
111 	/* Display the current state when it changes. */
112 	sc->sc_flags = ACPILID_F_VERBOSE;
113 }
114 
115 /*
116  * acpilid_status_changed:
117  *
118  *	Get, and possibly display, the lid status, and take action.
119  */
120 void
121 acpilid_status_changed(void *arg)
122 {
123 	struct acpilid_softc *sc = arg;
124 	int status;
125 
126 	if (acpi_eval_integer(sc->sc_node->ad_handle, "_LID",
127 	    &status) != AE_OK)
128 		return;
129 
130 	if (sc->sc_flags & ACPILID_F_VERBOSE)
131 		printf("%s: Lid %s\n",
132 		    sc->sc_dev.dv_xname,
133 		    status == 0 ? "closed " : "open");
134 
135 	/*
136 	 * XXX Perform the appropriate action for the new lid status.
137 	 */
138 }
139 
140 /*
141  * acpilid_notify_handler:
142  *
143  *	Callback from ACPI interrupt handler to notify us of an event.
144  */
145 void
146 acpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
147 {
148 	struct acpilid_softc *sc = context;
149 	int rv;
150 
151 	switch (notify) {
152 	case ACPI_NOTIFY_LidStatusChanged:
153 #ifdef ACPI_LID_DEBUG
154 		printf("%s: received LidStatusChanged message\n",
155 		    sc->sc_dev.dv_xname);
156 #endif
157 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
158 		    acpilid_status_changed, sc);
159 		if (rv != AE_OK)
160 			printf("%s: WARNING: unable to queue lid change "
161 			    "event: %d\n", sc->sc_dev.dv_xname, rv);
162 		break;
163 
164 	default:
165 		printf("%s: received unknown notify message: 0x%x\n",
166 		    sc->sc_dev.dv_xname, notify);
167 	}
168 }
169