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