2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Mitsuru IWASAKI
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/condvar.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 #include <contrib/dev/acpica/include/acpi.h>
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpiio.h>
47 #include <machine/apm_bios.h>
50 * APM driver emulation
53 #define APM_UNKNOWN 0xff
55 static int apm_active;
57 static MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation");
59 static d_open_t apmopen;
60 static d_write_t apmwrite;
61 static d_ioctl_t apmioctl;
62 static d_poll_t apmpoll;
63 static d_kqfilter_t apmkqfilter;
64 static void apmreadfiltdetach(struct knote *kn);
65 static int apmreadfilt(struct knote *kn, long hint);
66 static struct filterops apm_readfiltops = {
68 .f_detach = apmreadfiltdetach,
69 .f_event = apmreadfilt,
72 static struct cdevsw apm_cdevsw = {
73 .d_version = D_VERSION,
79 .d_kqfilter = apmkqfilter
83 acpi_capm_convert_battstate(struct acpi_battinfo *battp)
89 if (battp->state & ACPI_BATT_STAT_DISCHARG) {
95 if (battp->state & ACPI_BATT_STAT_CRITICAL)
96 state = 2; /* critical */
97 if (battp->state & ACPI_BATT_STAT_CHARGING)
98 state = 3; /* charging */
100 /* If still unknown, determine it based on the battery capacity. */
101 if (state == APM_UNKNOWN) {
102 if (battp->cap >= 50)
103 state = 0; /* high */
112 acpi_capm_convert_battflags(struct acpi_battinfo *battp)
118 if (battp->cap >= 50)
119 flags |= APM_BATT_HIGH;
121 if (battp->state & ACPI_BATT_STAT_CRITICAL)
122 flags |= APM_BATT_CRITICAL;
124 flags |= APM_BATT_LOW;
126 if (battp->state & ACPI_BATT_STAT_CHARGING)
127 flags |= APM_BATT_CHARGING;
128 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
129 flags = APM_BATT_NOT_PRESENT;
135 acpi_capm_get_info(apm_info_t aip)
138 struct acpi_battinfo batt;
140 aip->ai_infoversion = 1;
143 aip->ai_status = apm_active;
144 aip->ai_capabilities= 0xff00; /* unknown */
146 if (acpi_acad_get_acline(&acline))
147 aip->ai_acline = APM_UNKNOWN; /* unknown */
149 aip->ai_acline = acline; /* on/off */
151 if (acpi_battery_get_battinfo(NULL, &batt) != 0) {
152 aip->ai_batt_stat = APM_UNKNOWN;
153 aip->ai_batt_life = APM_UNKNOWN;
154 aip->ai_batt_time = -1; /* unknown */
155 aip->ai_batteries = ~0U; /* unknown */
157 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
158 aip->ai_batt_life = batt.cap;
159 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
160 aip->ai_batteries = acpi_battery_get_units();
167 acpi_capm_get_pwstatus(apm_pwstatus_t app)
170 int acline, unit, error;
171 struct acpi_battinfo batt;
173 if (app->ap_device != PMDV_ALLDEV &&
174 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
177 if (app->ap_device == PMDV_ALLDEV)
178 error = acpi_battery_get_battinfo(NULL, &batt);
180 unit = app->ap_device - PMDV_BATT0;
181 dev = devclass_get_device(devclass_find("battery"), unit);
183 error = acpi_battery_get_battinfo(dev, &batt);
190 app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
191 app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
192 app->ap_batt_life = batt.cap;
193 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
195 if (acpi_acad_get_acline(&acline))
196 app->ap_acline = APM_UNKNOWN;
198 app->ap_acline = acline; /* on/off */
203 /* Create a struct for tracking per-device suspend notification. */
204 static struct apm_clone_data *
205 apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc)
207 struct apm_clone_data *clone;
209 clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK);
211 clone->acpi_sc = acpi_sc;
212 clone->notify_status = APM_EV_NONE;
213 bzero(&clone->sel_read, sizeof(clone->sel_read));
214 knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex);
217 * The acpi device is always managed by devd(8) and is considered
218 * writable (i.e., ack is required to allow suspend to proceed.)
220 if (strcmp("acpi", devtoname(dev)) == 0)
221 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE;
223 clone->flags = ACPI_EVF_NONE;
226 STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries);
234 struct apm_clone_data *clone;
235 struct acpi_softc *acpi_sc;
238 acpi_sc = clone->acpi_sc;
240 /* We are about to lose a reference so check if suspend should occur */
241 if (acpi_sc->acpi_next_sstate != 0 &&
242 clone->notify_status != APM_EV_ACKED)
243 acpi_AckSleepState(clone, 0);
245 /* Remove this clone's data from the list and free it. */
247 STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries);
248 seldrain(&clone->sel_read);
249 knlist_destroy(&clone->sel_read.si_note);
251 free(clone, M_APMDEV);
255 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
257 struct acpi_softc *acpi_sc;
258 struct apm_clone_data *clone;
260 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
261 clone = apm_create_clone(dev, acpi_sc);
262 devfs_set_cdevpriv(clone, apmdtor);
264 /* If the device is opened for write, record that. */
265 if ((flag & FWRITE) != 0)
266 clone->flags |= ACPI_EVF_WRITE;
272 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
275 struct apm_clone_data *clone;
276 struct acpi_softc *acpi_sc;
277 struct apm_info info;
278 struct apm_event_info *ev_info;
282 devfs_get_cdevpriv((void **)&clone);
283 acpi_sc = clone->acpi_sc;
287 if ((flag & FWRITE) == 0)
289 if (acpi_sc->acpi_next_sstate == 0) {
290 if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) {
291 error = acpi_ReqSleepState(acpi_sc,
292 acpi_sc->acpi_suspend_sx);
295 "power off via apm suspend not supported\n");
299 error = acpi_AckSleepState(clone, 0);
302 if ((flag & FWRITE) == 0)
304 if (acpi_sc->acpi_next_sstate == 0) {
305 if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) {
306 error = acpi_ReqSleepState(acpi_sc,
307 acpi_sc->acpi_standby_sx);
310 "power off via apm standby not supported\n");
314 error = acpi_AckSleepState(clone, 0);
316 case APMIO_NEXTEVENT:
317 printf("apm nextevent start\n");
319 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status ==
321 ev_info = (struct apm_event_info *)addr;
322 if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3)
323 ev_info->type = PMEV_STANDBYREQ;
325 ev_info->type = PMEV_SUSPENDREQ;
327 clone->notify_status = APM_EV_NOTIFIED;
328 printf("apm event returning %d\n", ev_info->type);
333 case APMIO_GETINFO_OLD:
334 if (acpi_capm_get_info(&info))
336 aiop = (apm_info_old_t)addr;
337 aiop->ai_major = info.ai_major;
338 aiop->ai_minor = info.ai_minor;
339 aiop->ai_acline = info.ai_acline;
340 aiop->ai_batt_stat = info.ai_batt_stat;
341 aiop->ai_batt_life = info.ai_batt_life;
342 aiop->ai_status = info.ai_status;
345 if (acpi_capm_get_info((apm_info_t)addr))
348 case APMIO_GETPWSTATUS:
349 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
353 if ((flag & FWRITE) == 0)
358 if ((flag & FWRITE) == 0)
364 case APMIO_NOTHALTCPU:
367 if ((flag & FWRITE) == 0)
371 if ((flag & FWRITE) == 0)
373 bzero(addr, sizeof(struct apm_bios_arg));
384 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
386 return (uio->uio_resid);
390 apmpoll(struct cdev *dev, int events, struct thread *td)
392 struct apm_clone_data *clone;
396 devfs_get_cdevpriv((void **)&clone);
398 if (clone->acpi_sc->acpi_next_sstate)
399 revents |= events & (POLLIN | POLLRDNORM);
401 selrecord(td, &clone->sel_read);
407 apmkqfilter(struct cdev *dev, struct knote *kn)
409 struct apm_clone_data *clone;
411 devfs_get_cdevpriv((void **)&clone);
414 kn->kn_fop = &apm_readfiltops;
415 knlist_add(&clone->sel_read.si_note, kn, 0);
421 apmreadfiltdetach(struct knote *kn)
423 struct apm_clone_data *clone;
427 knlist_remove(&clone->sel_read.si_note, kn, 0);
432 apmreadfilt(struct knote *kn, long hint)
434 struct apm_clone_data *clone;
439 sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0;
445 acpi_apm_init(struct acpi_softc *sc)
448 /* Create a clone for /dev/acpi also. */
449 STAILQ_INIT(&sc->apm_cdevs);
450 sc->acpi_clone = apm_create_clone(sc->acpi_dev_t, sc);
452 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
453 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664, "apm");