xref: /freebsd/sys/dev/acpica/acpi_acad.c (revision 6adf353a56a161443406b44a45d00c688ca7b857)
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$
27  */
28 
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 
34 #include <machine/bus.h>
35 #include <machine/resource.h>
36 #include <sys/rman.h>
37 #include <sys/ioccom.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 
41 #include  "acpi.h"
42 #include <dev/acpica/acpivar.h>
43 #include <dev/acpica/acpiio.h>
44 
45 /*
46  * Hooks for the ACPI CA debugging infrastructure
47  */
48 #define _COMPONENT	ACPI_AC_ADAPTER
49 MODULE_NAME("AC_ADAPTER")
50 
51 #define ACPI_DEVICE_CHECK_PNP		0x00
52 #define ACPI_DEVICE_CHECK_EXISTENCE	0x01
53 #define ACPI_POWERSOURCE_STAT_CHANGE	0x80
54 
55 static void acpi_acad_get_status(void * );
56 static void acpi_acad_notify_handler(ACPI_HANDLE , UINT32 ,void *);
57 static int acpi_acad_probe(device_t);
58 static int acpi_acad_attach(device_t);
59 static int acpi_acad_ioctl(u_long, caddr_t, void *);
60 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
61 
62 struct  acpi_acad_softc {
63 	int status;
64 };
65 
66 static void
67 acpi_acad_get_status(void *context)
68 {
69 	int newstatus;
70 	device_t dev = context;
71 	struct acpi_acad_softc *sc = device_get_softc(dev);
72 	ACPI_HANDLE h = acpi_get_handle(dev);
73 
74 	if (acpi_EvaluateInteger(h, "_PSR", &newstatus) != AE_OK) {
75 		sc->status = -1;
76 		return;
77 	}
78 
79 	if (sc->status != newstatus) {
80 		sc->status = newstatus;
81 		/* set system power profile based on AC adapter status */
82 		powerprofile_set_state(sc->status ? POWERPROFILE_PERFORMANCE : POWERPROFILE_ECONOMY);
83 	}
84 
85 	if (bootverbose) {
86 		device_printf(dev,"%s\n",(sc->status) ? "On Line" : "Off Line");
87 	}
88 }
89 
90 static void
91 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
92 {
93 	device_t dev = context;
94 
95 	if (bootverbose) {
96 		device_printf(dev, "Notify %d\n", notify);
97 	}
98 
99 	switch (notify) {
100 	case ACPI_DEVICE_CHECK_PNP:
101 	case ACPI_DEVICE_CHECK_EXISTENCE:
102 	case ACPI_POWERSOURCE_STAT_CHANGE:
103 		/*Temporally. It is better to notify policy manager*/
104 		AcpiOsQueueForExecution(OSD_PRIORITY_LO,
105 		    acpi_acad_get_status,context);
106 		break;
107 	default:
108 		break;
109 	}
110 }
111 
112 static int
113 acpi_acad_probe(device_t dev)
114 {
115 
116     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
117 	acpi_MatchHid(dev, "ACPI0003")) {
118 
119       /*
120        * Set device description
121        */
122 	device_set_desc(dev, "AC adapter");
123 	return(0);
124     }
125     return(ENXIO);
126 }
127 
128 static int
129 acpi_acad_attach(device_t dev)
130 {
131 	int	 error;
132 	struct acpi_acad_softc	*sc;
133 	struct acpi_softc	*acpi_sc;
134 
135 	ACPI_HANDLE handle = acpi_get_handle(dev);
136 	AcpiInstallNotifyHandler(handle,
137 				 ACPI_DEVICE_NOTIFY,
138 				 acpi_acad_notify_handler, dev);
139 	/*Installing system notify is not so good*/
140 	AcpiInstallNotifyHandler(handle,
141 				 ACPI_SYSTEM_NOTIFY,
142 				 acpi_acad_notify_handler, dev);
143 
144 	if ((sc = device_get_softc(dev)) == NULL) {
145 		return (ENXIO);
146 	}
147 	if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS,
148 			acpi_acad_ioctl, dev)) != 0) {
149 		return (error);
150 	}
151 
152 	if (device_get_unit(dev) == 0) {
153 		acpi_sc = acpi_device_get_parent_softc(dev);
154 		SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
155 			SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
156 			OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
157 			&sc->status, 0, acpi_acad_sysctl, "I", "");
158 	}
159 
160 	/* Get initial status after whole system is up. */
161 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, dev);
162 
163 	return(0);
164 }
165 
166 static device_method_t acpi_acad_methods[] = {
167     /* Device interface */
168     DEVMETHOD(device_probe,	acpi_acad_probe),
169     DEVMETHOD(device_attach,	acpi_acad_attach),
170 
171     {0, 0}
172 };
173 
174 static driver_t acpi_acad_driver = {
175     "acpi_acad",
176     acpi_acad_methods,
177     sizeof(struct acpi_acad_softc),
178 };
179 
180 static devclass_t acpi_acad_devclass;
181 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0);
182 
183 static int
184 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
185 {
186 	device_t		 dev;
187 	struct acpi_acad_softc	*sc;
188 
189 	dev = (device_t)arg;
190 	if ((sc = device_get_softc(dev)) == NULL) {
191 		return(ENXIO);
192 	}
193 
194 	switch (cmd) {
195 	case ACPIIO_ACAD_GET_STATUS:
196 		acpi_acad_get_status(dev);
197 		*(int *)addr = sc->status;
198 		break;
199 	}
200 
201 	return(0);
202 }
203 
204 static int
205 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
206 {
207 	int     val;
208 	int     error;
209 	device_t	dev;
210 	struct acpi_acad_softc	*sc;
211 
212 	if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) {
213 		return (ENXIO);
214 	}
215 	if ((sc = device_get_softc(dev)) == NULL) {
216 		return (ENXIO);
217 	}
218 	acpi_acad_get_status(dev);
219 	val = *(u_int *)oidp->oid_arg1;
220 	error = sysctl_handle_int(oidp, &val, 0, req);
221 	return (error);
222 }
223 
224