1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001 Mitsuru IWASAKI
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 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
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/condvar.h>
32 #include <sys/conf.h>
33 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/poll.h>
37 #include <sys/uio.h>
38
39 #include <contrib/dev/acpica/include/acpi.h>
40
41 #include <dev/acpica/acpivar.h>
42 #include <dev/acpica/acpiio.h>
43
44 #include <machine/apm_bios.h>
45
46 /*
47 * APM driver emulation
48 */
49
50 #define APM_UNKNOWN 0xff
51
52 static int apm_active;
53
54 static MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation");
55
56 static d_open_t apmopen;
57 static d_write_t apmwrite;
58 static d_ioctl_t apmioctl;
59 static d_poll_t apmpoll;
60 static d_kqfilter_t apmkqfilter;
61 static void apmreadfiltdetach(struct knote *kn);
62 static int apmreadfilt(struct knote *kn, long hint);
63 static const struct filterops apm_readfiltops = {
64 .f_isfd = 1,
65 .f_detach = apmreadfiltdetach,
66 .f_event = apmreadfilt,
67 };
68
69 static struct cdevsw apm_cdevsw = {
70 .d_version = D_VERSION,
71 .d_open = apmopen,
72 .d_write = apmwrite,
73 .d_ioctl = apmioctl,
74 .d_poll = apmpoll,
75 .d_name = "apm",
76 .d_kqfilter = apmkqfilter
77 };
78
79 static int
acpi_capm_convert_battstate(struct acpi_battinfo * battp)80 acpi_capm_convert_battstate(struct acpi_battinfo *battp)
81 {
82 int state;
83
84 state = APM_UNKNOWN;
85
86 if (battp->state & ACPI_BATT_STAT_DISCHARG) {
87 if (battp->cap >= 50)
88 state = 0; /* high */
89 else
90 state = 1; /* low */
91 }
92 if (battp->state & ACPI_BATT_STAT_CRITICAL)
93 state = 2; /* critical */
94 if (battp->state & ACPI_BATT_STAT_CHARGING)
95 state = 3; /* charging */
96
97 /* If still unknown, determine it based on the battery capacity. */
98 if (state == APM_UNKNOWN) {
99 if (battp->cap >= 50)
100 state = 0; /* high */
101 else
102 state = 1; /* low */
103 }
104
105 return (state);
106 }
107
108 static int
acpi_capm_convert_battflags(struct acpi_battinfo * battp)109 acpi_capm_convert_battflags(struct acpi_battinfo *battp)
110 {
111 int flags;
112
113 flags = 0;
114
115 if (battp->cap >= 50)
116 flags |= APM_BATT_HIGH;
117 else {
118 if (battp->state & ACPI_BATT_STAT_CRITICAL)
119 flags |= APM_BATT_CRITICAL;
120 else
121 flags |= APM_BATT_LOW;
122 }
123 if (battp->state & ACPI_BATT_STAT_CHARGING)
124 flags |= APM_BATT_CHARGING;
125 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
126 flags = APM_BATT_NOT_PRESENT;
127
128 return (flags);
129 }
130
131 static int
acpi_capm_get_info(apm_info_t aip)132 acpi_capm_get_info(apm_info_t aip)
133 {
134 int acline;
135 struct acpi_battinfo batt;
136
137 aip->ai_infoversion = 1;
138 aip->ai_major = 1;
139 aip->ai_minor = 2;
140 aip->ai_status = apm_active;
141 aip->ai_capabilities= 0xff00; /* unknown */
142
143 if (acpi_acad_get_acline(&acline))
144 aip->ai_acline = 1; /* no info -- on-line best guess */
145 else
146 aip->ai_acline = acline; /* on/off */
147
148 if (acpi_battery_get_battinfo(NULL, &batt) != 0) {
149 aip->ai_batt_stat = 0; /* "high" old I/F has no unknown state */
150 aip->ai_batt_life = 255; /* N/A, not -1 */
151 aip->ai_batt_time = -1; /* unknown */
152 aip->ai_batteries = ~0U; /* unknown */
153 } else {
154 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
155 aip->ai_batt_life = (batt.cap == -1) ? 255 : batt.cap;
156 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
157 aip->ai_batteries = acpi_battery_get_units();
158 }
159
160 return (0);
161 }
162
163 static int
acpi_capm_get_pwstatus(apm_pwstatus_t app)164 acpi_capm_get_pwstatus(apm_pwstatus_t app)
165 {
166 device_t dev;
167 int acline, unit, error;
168 struct acpi_battinfo batt;
169
170 if (app->ap_device != PMDV_ALLDEV &&
171 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
172 return (1);
173
174 if (app->ap_device == PMDV_ALLDEV)
175 error = acpi_battery_get_battinfo(NULL, &batt);
176 else {
177 unit = app->ap_device - PMDV_BATT0;
178 dev = devclass_get_device(devclass_find("battery"), unit);
179 if (dev != NULL)
180 error = acpi_battery_get_battinfo(dev, &batt);
181 else
182 error = ENXIO;
183 }
184 if (error)
185 return (1);
186
187 app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
188 app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
189 app->ap_batt_life = (batt.cap == -1) ? 255 : batt.cap;
190 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
191
192 if (acpi_acad_get_acline(&acline))
193 app->ap_acline = 1; /* no info -- on-line best guess */
194 else
195 app->ap_acline = acline; /* on/off */
196
197 return (0);
198 }
199
200 /* Create a struct for tracking per-device suspend notification. */
201 static struct apm_clone_data *
apm_create_clone(struct cdev * dev,struct acpi_softc * acpi_sc)202 apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc)
203 {
204 struct apm_clone_data *clone;
205
206 clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK);
207 clone->cdev = dev;
208 clone->acpi_sc = acpi_sc;
209 clone->notify_status = APM_EV_NONE;
210 bzero(&clone->sel_read, sizeof(clone->sel_read));
211 knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex);
212
213 /*
214 * The acpi device is always managed by devd(8) and is considered
215 * writable (i.e., ack is required to allow suspend to proceed.)
216 */
217 if (strcmp("acpi", devtoname(dev)) == 0)
218 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE;
219 else
220 clone->flags = ACPI_EVF_NONE;
221
222 ACPI_LOCK(acpi);
223 STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries);
224 ACPI_UNLOCK(acpi);
225 return (clone);
226 }
227
228 static void
apmdtor(void * data)229 apmdtor(void *data)
230 {
231 struct apm_clone_data *clone;
232 struct acpi_softc *acpi_sc;
233
234 clone = data;
235 acpi_sc = clone->acpi_sc;
236
237 /* We are about to lose a reference so check if suspend should occur */
238 if (acpi_sc->acpi_next_stype != POWER_STYPE_AWAKE &&
239 clone->notify_status != APM_EV_ACKED)
240 acpi_AckSleepState(clone, 0);
241
242 /* Remove this clone's data from the list and free it. */
243 ACPI_LOCK(acpi);
244 STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries);
245 seldrain(&clone->sel_read);
246 knlist_destroy(&clone->sel_read.si_note);
247 ACPI_UNLOCK(acpi);
248 free(clone, M_APMDEV);
249 }
250
251 static int
apmopen(struct cdev * dev,int flag,int fmt,struct thread * td)252 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
253 {
254 struct acpi_softc *acpi_sc;
255 struct apm_clone_data *clone;
256
257 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
258 clone = apm_create_clone(dev, acpi_sc);
259 devfs_set_cdevpriv(clone, apmdtor);
260
261 /* If the device is opened for write, record that. */
262 if ((flag & FWRITE) != 0)
263 clone->flags |= ACPI_EVF_WRITE;
264
265 return (0);
266 }
267
268 static int
apmioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)269 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
270 {
271 int error;
272 struct apm_clone_data *clone;
273 struct acpi_softc *acpi_sc;
274 struct apm_info info;
275 struct apm_event_info *ev_info;
276 apm_info_old_t aiop;
277
278 error = 0;
279 devfs_get_cdevpriv((void **)&clone);
280 acpi_sc = clone->acpi_sc;
281
282 switch (cmd) {
283 case APMIO_SUSPEND:
284 if ((flag & FWRITE) == 0)
285 return (EPERM);
286 if (acpi_sc->acpi_next_stype == POWER_STYPE_AWAKE) {
287 if (power_suspend_stype != POWER_STYPE_POWEROFF) {
288 error = acpi_ReqSleepState(acpi_sc,
289 power_suspend_stype);
290 } else {
291 printf(
292 "power off via apm suspend not supported\n");
293 error = ENXIO;
294 }
295 } else
296 error = acpi_AckSleepState(clone, 0);
297 break;
298 case APMIO_STANDBY:
299 if ((flag & FWRITE) == 0)
300 return (EPERM);
301 if (acpi_sc->acpi_next_stype == POWER_STYPE_AWAKE) {
302 if (power_standby_stype != POWER_STYPE_POWEROFF) {
303 error = acpi_ReqSleepState(acpi_sc,
304 power_standby_stype);
305 } else {
306 printf(
307 "power off via apm standby not supported\n");
308 error = ENXIO;
309 }
310 } else
311 error = acpi_AckSleepState(clone, 0);
312 break;
313 case APMIO_NEXTEVENT:
314 printf("apm nextevent start\n");
315 ACPI_LOCK(acpi);
316 if (acpi_sc->acpi_next_stype != POWER_STYPE_AWAKE &&
317 clone->notify_status == APM_EV_NONE) {
318 ev_info = (struct apm_event_info *)addr;
319 /* XXX Check this. */
320 if (acpi_sc->acpi_next_stype == POWER_STYPE_STANDBY)
321 ev_info->type = PMEV_STANDBYREQ;
322 else
323 ev_info->type = PMEV_SUSPENDREQ;
324 ev_info->index = 0;
325 clone->notify_status = APM_EV_NOTIFIED;
326 printf("apm event returning %d\n", ev_info->type);
327 } else
328 error = EAGAIN;
329 ACPI_UNLOCK(acpi);
330 break;
331 case APMIO_GETINFO_OLD:
332 if (acpi_capm_get_info(&info))
333 error = ENXIO;
334 aiop = (apm_info_old_t)addr;
335 aiop->ai_major = info.ai_major;
336 aiop->ai_minor = info.ai_minor;
337 aiop->ai_acline = info.ai_acline;
338 aiop->ai_batt_stat = info.ai_batt_stat;
339 aiop->ai_batt_life = info.ai_batt_life;
340 aiop->ai_status = info.ai_status;
341 break;
342 case APMIO_GETINFO:
343 if (acpi_capm_get_info((apm_info_t)addr))
344 error = ENXIO;
345 break;
346 case APMIO_GETPWSTATUS:
347 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
348 error = ENXIO;
349 break;
350 case APMIO_ENABLE:
351 if ((flag & FWRITE) == 0)
352 return (EPERM);
353 apm_active = 1;
354 break;
355 case APMIO_DISABLE:
356 if ((flag & FWRITE) == 0)
357 return (EPERM);
358 apm_active = 0;
359 break;
360 case APMIO_HALTCPU:
361 break;
362 case APMIO_NOTHALTCPU:
363 break;
364 case APMIO_DISPLAY:
365 if ((flag & FWRITE) == 0)
366 return (EPERM);
367 break;
368 case APMIO_BIOS:
369 if ((flag & FWRITE) == 0)
370 return (EPERM);
371 bzero(addr, sizeof(struct apm_bios_arg));
372 break;
373 default:
374 error = EINVAL;
375 break;
376 }
377
378 return (error);
379 }
380
381 static int
apmwrite(struct cdev * dev,struct uio * uio,int ioflag)382 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
383 {
384 return (uio->uio_resid);
385 }
386
387 static int
apmpoll(struct cdev * dev,int events,struct thread * td)388 apmpoll(struct cdev *dev, int events, struct thread *td)
389 {
390 struct apm_clone_data *clone;
391 int revents;
392
393 revents = 0;
394 devfs_get_cdevpriv((void **)&clone);
395 ACPI_LOCK(acpi);
396 if (clone->acpi_sc->acpi_next_stype != POWER_STYPE_AWAKE)
397 revents |= events & (POLLIN | POLLRDNORM);
398 else
399 selrecord(td, &clone->sel_read);
400 ACPI_UNLOCK(acpi);
401 return (revents);
402 }
403
404 static int
apmkqfilter(struct cdev * dev,struct knote * kn)405 apmkqfilter(struct cdev *dev, struct knote *kn)
406 {
407 struct apm_clone_data *clone;
408
409 devfs_get_cdevpriv((void **)&clone);
410 ACPI_LOCK(acpi);
411 kn->kn_hook = clone;
412 kn->kn_fop = &apm_readfiltops;
413 knlist_add(&clone->sel_read.si_note, kn, 0);
414 ACPI_UNLOCK(acpi);
415 return (0);
416 }
417
418 static void
apmreadfiltdetach(struct knote * kn)419 apmreadfiltdetach(struct knote *kn)
420 {
421 struct apm_clone_data *clone;
422
423 ACPI_LOCK(acpi);
424 clone = kn->kn_hook;
425 knlist_remove(&clone->sel_read.si_note, kn, 0);
426 ACPI_UNLOCK(acpi);
427 }
428
429 static int
apmreadfilt(struct knote * kn,long hint)430 apmreadfilt(struct knote *kn, long hint)
431 {
432 struct apm_clone_data *clone;
433 int sleeping;
434
435 ACPI_LOCK(acpi);
436 clone = kn->kn_hook;
437 sleeping = clone->acpi_sc->acpi_next_stype != POWER_STYPE_AWAKE;
438 ACPI_UNLOCK(acpi);
439 return (sleeping);
440 }
441
442 void
acpi_apm_init(struct acpi_softc * sc)443 acpi_apm_init(struct acpi_softc *sc)
444 {
445
446 /* Create a clone for /dev/acpi also. */
447 STAILQ_INIT(&sc->apm_cdevs);
448 sc->acpi_clone = apm_create_clone(sc->acpi_dev_t, sc);
449
450 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
451 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664, "apm");
452 }
453