acpi_wakedev.c revision 1.3 1 /* $NetBSD: acpi_wakedev.c,v 1.3 2010/03/05 14:00:17 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.3 2010/03/05 14:00:17 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/kmem.h>
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38
39 #include <dev/acpi/acpireg.h>
40 #include <dev/acpi/acpivar.h>
41 #include <dev/acpi/acpi_wakedev.h>
42
43 struct acpi_wakedev;
44
45 static TAILQ_HEAD(, acpi_wakedev) acpi_wakedevlist =
46 TAILQ_HEAD_INITIALIZER(acpi_wakedevlist);
47 static int acpi_wakedev_node = -1;
48
49 struct acpi_wakedev {
50 struct acpi_devnode *aw_node;
51 int aw_enabled;
52 struct sysctllog *aw_sysctllog;
53 TAILQ_ENTRY(acpi_wakedev) aw_list;
54 };
55
56 static const char * const acpi_wakedev_default[] = {
57 "PNP0C0C", /* power button */
58 "PNP0C0E", /* sleep button */
59 "PNP0C0D", /* lid switch */
60 "PNP03??", /* PC KBD port */
61 NULL,
62 };
63
64 SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.wake subtree setup")
65 {
66 const struct sysctlnode *rnode;
67 int err;
68
69 err = sysctl_createv(NULL, 0, NULL, NULL,
70 CTLFLAG_PERMANENT,
71 CTLTYPE_NODE, "hw", NULL,
72 NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
73 if (err)
74 return;
75 err = sysctl_createv(NULL, 0, NULL, &rnode,
76 CTLFLAG_PERMANENT,
77 CTLTYPE_NODE, "wake", NULL,
78 NULL, 0, NULL, 0,
79 CTL_HW, CTL_CREATE, CTL_EOL);
80 if (err)
81 return;
82 acpi_wakedev_node = rnode->sysctl_num;
83 }
84
85 static void
86 acpi_wakedev_sysctl_add(struct acpi_wakedev *aw)
87 {
88 int err;
89
90 if (acpi_wakedev_node == -1)
91 return;
92
93 err = sysctl_createv(&aw->aw_sysctllog, 0, NULL, NULL,
94 CTLFLAG_READWRITE, CTLTYPE_INT, aw->aw_node->ad_name,
95 NULL, NULL, 0, &aw->aw_enabled, 0,
96 CTL_HW, acpi_wakedev_node, CTL_CREATE, CTL_EOL);
97 if (err)
98 printf("acpi_wakedev_sysctl_add: "
99 "sysctl_createv(hw.wake.%s) failed. (%d)\n",
100 aw->aw_node->ad_name, err);
101 }
102
103 static bool
104 acpi_wakedev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
105 {
106 struct acpi_wakedev *aw;
107 ACPI_HANDLE hdl;
108
109 if (ACPI_FAILURE(AcpiGetHandle(ad->ad_handle, "_PRW", &hdl)))
110 return false;
111
112 aw = kmem_alloc(sizeof(*aw), KM_SLEEP);
113 if (aw == NULL) {
114 aprint_error("acpi_wakedev_add: kmem_alloc failed\n");
115 return false;
116 }
117 aw->aw_node = ad;
118 aw->aw_sysctllog = NULL;
119 if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
120 aw->aw_enabled = 1;
121 else
122 aw->aw_enabled = 0;
123
124 TAILQ_INSERT_TAIL(&acpi_wakedevlist, aw, aw_list);
125
126 acpi_wakedev_sysctl_add(aw);
127
128 return true;
129 }
130
131 static void
132 acpi_wakedev_print(struct acpi_wakedev *aw)
133 {
134 aprint_debug(" %s", aw->aw_node->ad_name);
135 }
136
137 int
138 acpi_wakedev_scan(struct acpi_softc *sc)
139 {
140 struct acpi_scope *as;
141 struct acpi_devnode *ad;
142 struct acpi_wakedev *aw;
143 ACPI_DEVICE_INFO *di;
144 int count = 0;
145
146 #define ACPI_STA_DEV_VALID \
147 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
148
149 TAILQ_FOREACH(as, &sc->sc_scopes, as_list)
150 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
151 di = ad->ad_devinfo;
152 if (di->Type != ACPI_TYPE_DEVICE)
153 continue;
154 if ((di->Valid & ACPI_VALID_STA) != 0 &&
155 (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
156 ACPI_STA_DEV_VALID)
157 continue;
158 if (acpi_wakedev_add(sc, ad) == true)
159 ++count;
160 }
161
162 #undef ACPI_STA_DEV_VALID
163
164 if (count == 0)
165 return 0;
166
167 aprint_debug_dev(sc->sc_dev, "wakeup devices:");
168 TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list)
169 acpi_wakedev_print(aw);
170 aprint_debug("\n");
171
172 return count;
173 }
174
175 void
176 acpi_wakedev_commit(struct acpi_softc *sc)
177 {
178 struct acpi_wakedev *aw;
179
180 TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list) {
181 if (aw->aw_enabled) {
182 aprint_debug_dev(sc->sc_dev, "set wake GPE (%s)\n",
183 aw->aw_node->ad_name);
184 acpi_set_wake_gpe(aw->aw_node->ad_handle);
185 } else
186 acpi_clear_wake_gpe(aw->aw_node->ad_handle);
187 }
188 }
189