1 /* $NetBSD: apm.c,v 1.10 2004/04/15 11:03:15 aymeric Exp $ */ 2 /* $OpenBSD: apm.c,v 1.5 2002/06/07 07:13:59 miod Exp $ */ 3 4 /*- 5 * Copyright (c) 2001 Alexander Guy. All rights reserved. 6 * Copyright (c) 1998-2001 Michael Shalayeff. All rights reserved. 7 * Copyright (c) 1995 John T. Kohl. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: apm.c,v 1.10 2004/04/15 11:03:15 aymeric Exp $"); 41 42 #include "apm.h" 43 44 #if NAPM > 1 45 #error only one APM emulation device may be configured 46 #endif 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/proc.h> 52 #include <sys/device.h> 53 #include <sys/fcntl.h> 54 #include <sys/ioctl.h> 55 #ifdef __OpenBSD__ 56 #include <sys/event.h> 57 #endif 58 #ifdef __NetBSD__ 59 #include <sys/select.h> 60 #include <sys/poll.h> 61 #include <sys/conf.h> 62 #endif 63 64 #ifdef __OpenBSD__ 65 #include <machine/conf.h> 66 #endif 67 #include <machine/cpu.h> 68 #include <machine/apmvar.h> 69 70 #include <macppc/dev/adbvar.h> 71 #include <macppc/dev/adb_direct.h> 72 #include <macppc/dev/pm_direct.h> 73 74 #if defined(APMDEBUG) 75 #define DPRINTF(x) printf x 76 #else 77 #define DPRINTF(x) /**/ 78 #endif 79 80 #define APM_NEVENTS 16 81 82 struct apm_softc { 83 struct device sc_dev; 84 struct selinfo sc_rsel; 85 #ifdef __OpenBSD__ 86 struct klist sc_note; 87 #endif 88 int sc_flags; 89 int event_count; 90 int event_ptr; 91 struct lock sc_lock; 92 struct apm_event_info event_list[APM_NEVENTS]; 93 }; 94 95 /* 96 * A brief note on the locking protocol: it's very simple; we 97 * assert an exclusive lock any time thread context enters the 98 * APM module. This is both the APM thread itself, as well as 99 * user context. 100 */ 101 #ifdef __NetBSD__ 102 #define APM_LOCK(apmsc) \ 103 (void) lockmgr(&(apmsc)->sc_lock, LK_EXCLUSIVE, NULL) 104 #define APM_UNLOCK(apmsc) \ 105 (void) lockmgr(&(apmsc)->sc_lock, LK_RELEASE, NULL) 106 #else 107 #define APM_LOCK(apmsc) 108 #define APM_UNLOCK(apmsc) 109 #endif 110 111 int apmmatch(struct device *, struct cfdata *, void *); 112 void apmattach(struct device *, struct device *, void *); 113 114 #ifdef __NetBSD__ 115 #if 0 116 static int apm_record_event __P((struct apm_softc *, u_int)); 117 #endif 118 #endif 119 120 CFATTACH_DECL(apm, sizeof(struct apm_softc), 121 apmmatch, apmattach, NULL, NULL); 122 123 #ifdef __OpenBSD__ 124 struct cfdriver apm_cd = { 125 NULL, "apm", DV_DULL 126 }; 127 #else 128 extern struct cfdriver apm_cd; 129 130 dev_type_open(apmopen); 131 dev_type_close(apmclose); 132 dev_type_ioctl(apmioctl); 133 dev_type_poll(apmpoll); 134 dev_type_kqfilter(apmkqfilter); 135 136 const struct cdevsw apm_cdevsw = { 137 apmopen, apmclose, noread, nowrite, apmioctl, 138 nostop, notty, apmpoll, nommap, apmkqfilter, 139 }; 140 #endif 141 142 int apm_evindex; 143 144 #define APMUNIT(dev) (minor(dev)&0xf0) 145 #define APMDEV(dev) (minor(dev)&0x0f) 146 #define APMDEV_NORMAL 0 147 #define APMDEV_CTL 8 148 149 /* 150 * Flags to control kernel display 151 * SCFLAG_NOPRINT: do not output APM power messages due to 152 * a power change event. 153 * 154 * SCFLAG_PCTPRINT: do not output APM power messages due to 155 * to a power change event unless the battery 156 * percentage changes. 157 */ 158 159 #define SCFLAG_NOPRINT 0x0008000 160 #define SCFLAG_PCTPRINT 0x0004000 161 #define SCFLAG_PRINT (SCFLAG_NOPRINT|SCFLAG_PCTPRINT) 162 163 #define SCFLAG_OREAD (1 << 0) 164 #define SCFLAG_OWRITE (1 << 1) 165 #define SCFLAG_OPEN (SCFLAG_OREAD|SCFLAG_OWRITE) 166 167 168 int 169 apmmatch(parent, match, aux) 170 struct device *parent; 171 struct cfdata *match; 172 void *aux; 173 { 174 struct adb_attach_args *aa = (void *)aux; 175 if (aa->origaddr != ADBADDR_APM || 176 aa->handler_id != ADBADDR_APM || 177 aa->adbaddr != ADBADDR_APM) 178 return 0; 179 180 if (adbHardware != ADB_HW_PB) 181 return 0; 182 183 return 1; 184 } 185 186 void 187 apmattach(parent, self, aux) 188 struct device *parent, *self; 189 void *aux; 190 { 191 struct apm_softc *sc = (struct apm_softc *) self; 192 struct pmu_battery_info info; 193 194 pm_battery_info(0, &info); 195 196 printf(": battery flags 0x%X, ", info.flags); 197 printf("%d%% charged\n", ((info.cur_charge * 100) / info.max_charge)); 198 199 sc->sc_flags = 0; 200 sc->event_ptr = 0; 201 sc->event_count = 0; 202 lockinit(&sc->sc_lock, PWAIT, "apmlk", 0, 0); 203 } 204 205 int 206 apmopen(dev, flag, mode, p) 207 dev_t dev; 208 int flag, mode; 209 struct proc *p; 210 { 211 struct apm_softc *sc; 212 int error = 0; 213 214 /* apm0 only */ 215 if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 || 216 !(sc = apm_cd.cd_devs[APMUNIT(dev)])) 217 return ENXIO; 218 219 DPRINTF(("apmopen: dev %d pid %d flag %x mode %x\n", 220 APMDEV(dev), p->p_pid, flag, mode)); 221 222 APM_LOCK(sc); 223 switch (APMDEV(dev)) { 224 case APMDEV_CTL: 225 if (!(flag & FWRITE)) { 226 error = EINVAL; 227 break; 228 } 229 if (sc->sc_flags & SCFLAG_OWRITE) { 230 error = EBUSY; 231 break; 232 } 233 sc->sc_flags |= SCFLAG_OWRITE; 234 break; 235 case APMDEV_NORMAL: 236 if (!(flag & FREAD) || (flag & FWRITE)) { 237 error = EINVAL; 238 break; 239 } 240 sc->sc_flags |= SCFLAG_OREAD; 241 break; 242 default: 243 error = ENXIO; 244 break; 245 } 246 APM_UNLOCK(sc); 247 return error; 248 } 249 250 int 251 apmclose(dev, flag, mode, p) 252 dev_t dev; 253 int flag, mode; 254 struct proc *p; 255 { 256 struct apm_softc *sc; 257 258 /* apm0 only */ 259 if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 || 260 !(sc = apm_cd.cd_devs[APMUNIT(dev)])) 261 return ENXIO; 262 263 DPRINTF(("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode)); 264 265 APM_LOCK(sc); 266 switch (APMDEV(dev)) { 267 case APMDEV_CTL: 268 sc->sc_flags &= ~SCFLAG_OWRITE; 269 break; 270 case APMDEV_NORMAL: 271 sc->sc_flags &= ~SCFLAG_OREAD; 272 break; 273 } 274 APM_UNLOCK(sc); 275 return 0; 276 } 277 278 int 279 apmioctl(dev, cmd, data, flag, p) 280 dev_t dev; 281 u_long cmd; 282 caddr_t data; 283 int flag; 284 struct proc *p; 285 { 286 struct apm_softc *sc; 287 struct pmu_battery_info batt; 288 struct apm_power_info *power; 289 int error = 0; 290 291 /* apm0 only */ 292 if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 || 293 !(sc = apm_cd.cd_devs[APMUNIT(dev)])) 294 return ENXIO; 295 296 APM_LOCK(sc); 297 switch (cmd) { 298 /* some ioctl names from linux */ 299 case APM_IOC_STANDBY: 300 if ((flag & FWRITE) == 0) 301 error = EBADF; 302 case APM_IOC_SUSPEND: 303 if ((flag & FWRITE) == 0) 304 error = EBADF; 305 break; 306 case APM_IOC_PRN_CTL: 307 if ((flag & FWRITE) == 0) 308 error = EBADF; 309 else { 310 int flag = *(int *)data; 311 DPRINTF(( "APM_IOC_PRN_CTL: %d\n", flag )); 312 switch (flag) { 313 case APM_PRINT_ON: /* enable printing */ 314 sc->sc_flags &= ~SCFLAG_PRINT; 315 break; 316 case APM_PRINT_OFF: /* disable printing */ 317 sc->sc_flags &= ~SCFLAG_PRINT; 318 sc->sc_flags |= SCFLAG_NOPRINT; 319 break; 320 case APM_PRINT_PCT: /* disable some printing */ 321 sc->sc_flags &= ~SCFLAG_PRINT; 322 sc->sc_flags |= SCFLAG_PCTPRINT; 323 break; 324 default: 325 error = EINVAL; 326 break; 327 } 328 } 329 break; 330 case APM_IOC_DEV_CTL: 331 if ((flag & FWRITE) == 0) 332 error = EBADF; 333 break; 334 case APM_IOC_GETPOWER: 335 power = (struct apm_power_info *)data; 336 337 pm_battery_info(0, &batt); 338 339 power->ac_state = ((batt.flags & PMU_PWR_AC_PRESENT) ? 340 APM_AC_ON : APM_AC_OFF); 341 power->battery_life = 342 ((batt.cur_charge * 100) / batt.max_charge); 343 344 /* 345 * If the battery is charging, return the minutes left until 346 * charging is complete. apmd knows this. 347 */ 348 349 if (!(batt.flags & PMU_PWR_BATT_PRESENT)) { 350 power->battery_state = APM_BATT_UNKNOWN; 351 power->minutes_left = 0; 352 power->battery_life = 0; 353 } else if ((power->ac_state == APM_AC_ON) && 354 (batt.draw > 0)) { 355 power->minutes_left = 356 (((batt.max_charge - batt.cur_charge) * 3600) / 357 batt.draw) / 60; 358 power->battery_state = APM_BATT_CHARGING; 359 } else { 360 power->minutes_left = 361 ((batt.cur_charge * 3600) / (-batt.draw)) / 60; 362 363 /* XXX - Arbitrary */ 364 if (power->battery_life > 60) { 365 power->battery_state = APM_BATT_HIGH; 366 } else if (power->battery_life < 10) { 367 power->battery_state = APM_BATT_CRITICAL; 368 } else { 369 power->battery_state = APM_BATT_LOW; 370 } 371 } 372 373 break; 374 375 default: 376 error = ENOTTY; 377 } 378 APM_UNLOCK(sc); 379 380 return error; 381 } 382 383 #ifdef __NetBSD__ 384 #if 0 385 /* 386 * return 0 if the user will notice and handle the event, 387 * return 1 if the kernel driver should do so. 388 */ 389 static int 390 apm_record_event(sc, event_type) 391 struct apm_softc *sc; 392 u_int event_type; 393 { 394 struct apm_event_info *evp; 395 396 if ((sc->sc_flags & SCFLAG_OPEN) == 0) 397 return 1; /* no user waiting */ 398 if (sc->event_count == APM_NEVENTS) { 399 DPRINTF(("apm_record_event: queue full!\n")); 400 return 1; /* overflow */ 401 } 402 evp = &sc->event_list[sc->event_ptr]; 403 sc->event_count++; 404 sc->event_ptr++; 405 sc->event_ptr %= APM_NEVENTS; 406 evp->type = event_type; 407 evp->index = ++apm_evindex; 408 selwakeup(&sc->sc_rsel); 409 return (sc->sc_flags & SCFLAG_OWRITE) ? 0 : 1; /* user may handle */ 410 } 411 #endif 412 413 int 414 apmpoll(dev, events, p) 415 dev_t dev; 416 int events; 417 struct proc *p; 418 { 419 struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)]; 420 int revents = 0; 421 422 APM_LOCK(sc); 423 if (events & (POLLIN | POLLRDNORM)) { 424 if (sc->event_count) 425 revents |= events & (POLLIN | POLLRDNORM); 426 else 427 selrecord(p, &sc->sc_rsel); 428 } 429 APM_UNLOCK(sc); 430 431 return (revents); 432 } 433 #endif 434 435 static void 436 filt_apmrdetach(struct knote *kn) 437 { 438 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook; 439 440 APM_LOCK(sc); 441 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 442 APM_UNLOCK(sc); 443 } 444 445 static int 446 filt_apmread(struct knote *kn, long hint) 447 { 448 struct apm_softc *sc = kn->kn_hook; 449 450 kn->kn_data = sc->event_count; 451 return (kn->kn_data > 0); 452 } 453 454 static struct filterops apmread_filtops = 455 { 1, NULL, filt_apmrdetach, filt_apmread}; 456 457 int 458 apmkqfilter(dev, kn) 459 dev_t dev; 460 struct knote *kn; 461 { 462 struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)]; 463 struct klist *klist; 464 465 switch (kn->kn_filter) { 466 case EVFILT_READ: 467 klist = &sc->sc_rsel.sel_klist; 468 kn->kn_fop = &apmread_filtops; 469 break; 470 default: 471 return (1); 472 } 473 474 kn->kn_hook = sc; 475 476 APM_LOCK(sc); 477 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 478 APM_UNLOCK(sc); 479 480 return (0); 481 } 482