INTR_EXCL moved to sys/bus.h, add #include.
[dragonfly.git] / sys / dev / acpica / acpi_acad.c
1 /*-
2  * Copyright (c) 2000 Takanori Watanabe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/acpica/acpi_acad.c,v 1.17.2.1 2003/08/22 20:49:20 jhb Exp $
27  *      $DragonFly: src/sys/dev/acpica/Attic/acpi_acad.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ 
28  */
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34
35 #include <machine/bus.h>
36 #include <machine/resource.h>
37 #include <sys/rman.h>
38 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
40 #include <sys/conf.h>
41 #include <sys/power.h>
42
43 #include  "acpi.h"
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpiio.h>
46  
47 /*
48  * Hooks for the ACPI CA debugging infrastructure
49  */
50 #define _COMPONENT      ACPI_AC_ADAPTER
51 ACPI_MODULE_NAME("AC_ADAPTER")
52
53 #define ACPI_DEVICE_CHECK_PNP           0x00
54 #define ACPI_DEVICE_CHECK_EXISTENCE     0x01
55 #define ACPI_POWERSOURCE_STAT_CHANGE    0x80
56
57 static void acpi_acad_get_status(void * );
58 static void acpi_acad_notify_handler(ACPI_HANDLE , UINT32 ,void *);
59 static int acpi_acad_probe(device_t);
60 static int acpi_acad_attach(device_t);
61 static int acpi_acad_ioctl(u_long, caddr_t, void *);
62 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
63 static void acpi_acad_init_acline(void *arg);
64
65 struct  acpi_acad_softc {
66         int status;
67
68         int initializing;
69 };
70
71 static void
72 acpi_acad_get_status(void *context)
73 {
74         int newstatus;
75         device_t dev = context;
76         struct acpi_acad_softc *sc = device_get_softc(dev);
77         ACPI_HANDLE h = acpi_get_handle(dev);
78
79         if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) {
80                 sc->status = -1;
81                 return;
82         }
83
84         if (sc->status != newstatus) {
85                 sc->status = newstatus;
86                 /* set system power profile based on AC adapter status */
87                 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY);
88                 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
89                     "%s Line\n",(sc->status) ? "On" : "Off");
90         }
91 }
92
93 static void
94 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
95 {
96         device_t dev = context;
97
98         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
99             "Notify %d\n", notify);
100
101         switch (notify) {
102         case ACPI_DEVICE_CHECK_PNP:
103         case ACPI_DEVICE_CHECK_EXISTENCE:
104         case ACPI_POWERSOURCE_STAT_CHANGE:
105                 /*Temporally. It is better to notify policy manager*/
106                 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
107                     acpi_acad_get_status,context);
108                 break;
109         default:
110                 break;
111         }
112 }
113
114 static int
115 acpi_acad_probe(device_t dev)
116 {
117
118     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
119         acpi_MatchHid(dev, "ACPI0003")) {
120       
121       /*
122        * Set device description 
123        */
124         device_set_desc(dev, "AC adapter");
125         return(0);
126     }
127     return(ENXIO);
128 }
129
130 static int
131 acpi_acad_attach(device_t dev)
132 {
133         int      error;
134         struct acpi_acad_softc  *sc;
135         struct acpi_softc       *acpi_sc;
136
137         ACPI_HANDLE handle = acpi_get_handle(dev);
138         AcpiInstallNotifyHandler(handle, 
139                                  ACPI_DEVICE_NOTIFY,
140                                  acpi_acad_notify_handler, dev);
141         /*Installing system notify is not so good*/
142         AcpiInstallNotifyHandler(handle, 
143                                  ACPI_SYSTEM_NOTIFY,
144                                  acpi_acad_notify_handler, dev);
145
146         if ((sc = device_get_softc(dev)) == NULL) {
147                 return (ENXIO);
148         }
149         if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS,
150                         acpi_acad_ioctl, dev)) != 0) {
151                 return (error);
152         }
153
154         if (device_get_unit(dev) == 0) {
155                 acpi_sc = acpi_device_get_parent_softc(dev);
156                 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
157                         SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
158                         OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
159                         &sc->status, 0, acpi_acad_sysctl, "I", "");
160         }
161
162         /* Get initial status after whole system is up. */
163         sc->status = -1;
164         sc->initializing = 0;
165
166         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
167
168         return(0);
169 }
170
171 static device_method_t acpi_acad_methods[] = {
172     /* Device interface */
173     DEVMETHOD(device_probe,     acpi_acad_probe),
174     DEVMETHOD(device_attach,    acpi_acad_attach),
175
176     {0, 0}
177 };
178
179 static driver_t acpi_acad_driver = {
180     "acpi_acad",
181     acpi_acad_methods,
182     sizeof(struct acpi_acad_softc),
183 };
184
185 static devclass_t acpi_acad_devclass;
186 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0);
187
188 static int
189 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
190 {
191         device_t                 dev;
192         struct acpi_acad_softc  *sc;
193
194         dev = (device_t)arg;
195         if ((sc = device_get_softc(dev)) == NULL) {
196                 return(ENXIO);
197         }
198
199         /*
200          * No security check required: information retrieval only.  If
201          * new functions are added here, a check might be required.
202          */
203
204         switch (cmd) {
205         case ACPIIO_ACAD_GET_STATUS:
206                 acpi_acad_get_status(dev);
207                 *(int *)addr = sc->status;
208                 break;
209         }
210
211         return(0);
212 }
213
214 static int
215 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
216 {
217         int     val;
218         int     error;
219
220         if (acpi_acad_get_acline(&val)) {
221                 return (ENXIO);
222         }
223
224         val = *(u_int *)oidp->oid_arg1;
225         error = sysctl_handle_int(oidp, &val, 0, req);
226         return (error);
227 }
228
229 static void
230 acpi_acad_init_acline(void *arg)
231 {
232         int             retry;
233         int             status;
234         device_t        dev = (device_t)arg;
235         struct acpi_acad_softc  *sc = device_get_softc(dev);
236 #define ACPI_ACAD_RETRY_MAX     6
237
238         if (sc->initializing) {
239                 return;
240         }
241
242         sc->initializing = 1;
243
244         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
245                     "acline initialization start\n");
246
247         status = 0;
248         for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
249                 acpi_acad_get_status(dev);
250                 if (status != sc->status)
251                         break;
252         }
253
254         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
255                     "acline initialization done, tried %d times\n", retry+1);
256
257         sc->initializing = 0;
258 }
259
260 /*
261  * Public interfaces.
262  */
263
264 int
265 acpi_acad_get_acline(int *status)
266 {
267         device_t                 dev;
268         struct acpi_acad_softc  *sc;
269
270         if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) {
271                 return (ENXIO);
272         }
273
274         if ((sc = device_get_softc(dev)) == NULL) {
275                 return (ENXIO);
276         }
277
278         acpi_acad_get_status(dev);
279         *status = sc->status;
280
281         return (0);
282 }
283