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 *
| 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 * $FreeBSD: head/sys/i386/acpica/acpi_machdep.c 107144 2002-11-21 20:55:22Z jhb $
| 26 * $FreeBSD: head/sys/i386/acpica/acpi_machdep.c 111815 2003-03-03 12:15:54Z phk $
|
27 */ 28 29#include <sys/param.h> 30#include <sys/bus.h> 31#include <sys/conf.h> 32#include <sys/fcntl.h> 33#include <sys/uio.h> 34 35#include "acpi.h" 36 37#include <dev/acpica/acpivar.h> 38#include <dev/acpica/acpiio.h> 39 40static device_t acpi_dev; 41 42/* 43 * APM driver emulation 44 */ 45 46#if __FreeBSD_version < 500000 47#include <sys/select.h> 48#else 49#include <sys/selinfo.h> 50#endif 51 52#include <machine/apm_bios.h> 53#include <machine/pc/bios.h> 54 55#include <i386/apm/apm.h> 56 57static struct apm_softc apm_softc; 58 59static d_open_t apmopen; 60static d_close_t apmclose; 61static d_write_t apmwrite; 62static d_ioctl_t apmioctl; 63static d_poll_t apmpoll; 64 65#define CDEV_MAJOR 39 66static struct cdevsw apm_cdevsw = {
| 27 */ 28 29#include <sys/param.h> 30#include <sys/bus.h> 31#include <sys/conf.h> 32#include <sys/fcntl.h> 33#include <sys/uio.h> 34 35#include "acpi.h" 36 37#include <dev/acpica/acpivar.h> 38#include <dev/acpica/acpiio.h> 39 40static device_t acpi_dev; 41 42/* 43 * APM driver emulation 44 */ 45 46#if __FreeBSD_version < 500000 47#include <sys/select.h> 48#else 49#include <sys/selinfo.h> 50#endif 51 52#include <machine/apm_bios.h> 53#include <machine/pc/bios.h> 54 55#include <i386/apm/apm.h> 56 57static struct apm_softc apm_softc; 58 59static d_open_t apmopen; 60static d_close_t apmclose; 61static d_write_t apmwrite; 62static d_ioctl_t apmioctl; 63static d_poll_t apmpoll; 64 65#define CDEV_MAJOR 39 66static struct cdevsw apm_cdevsw = {
|
67 /* open */ apmopen, 68 /* close */ apmclose, 69 /* read */ noread, 70 /* write */ apmwrite, 71 /* ioctl */ apmioctl, 72 /* poll */ apmpoll, 73 /* mmap */ nommap, 74 /* strategy */ nostrategy, 75 /* name */ "apm", 76 /* maj */ CDEV_MAJOR, 77 /* dump */ nodump, 78 /* psize */ nopsize, 79 /* flags */ 0,
| 67 .d_open = apmopen, 68 .d_close = apmclose, 69 .d_write = apmwrite, 70 .d_ioctl = apmioctl, 71 .d_poll = apmpoll, 72 .d_name = "apm", 73 .d_maj = CDEV_MAJOR,
|
80}; 81 82static int 83acpi_capm_convert_battstate(struct acpi_battinfo *battp) 84{ 85 int state; 86 87 state = 0xff; /* XXX unknown */ 88 89 if (battp->state & ACPI_BATT_STAT_DISCHARG) { 90 if (battp->cap >= 50) { 91 state = 0; /* high */ 92 } else { 93 state = 1; /* low */ 94 } 95 } 96 if (battp->state & ACPI_BATT_STAT_CRITICAL) { 97 state = 2; /* critical */ 98 } 99 if (battp->state & ACPI_BATT_STAT_CHARGING) { 100 state = 3; /* charging */ 101 } 102 return (state); 103} 104 105static int 106acpi_capm_convert_battflags(struct acpi_battinfo *battp) 107{ 108 int flags; 109 110 flags = 0; 111 112 if (battp->cap >= 50) { 113 flags |= APM_BATT_HIGH; 114 } else { 115 if (battp->state & ACPI_BATT_STAT_CRITICAL) { 116 flags |= APM_BATT_CRITICAL; 117 } else { 118 flags |= APM_BATT_LOW; 119 } 120 } 121 if (battp->state & ACPI_BATT_STAT_CHARGING) { 122 flags |= APM_BATT_CHARGING; 123 } 124 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) { 125 flags = APM_BATT_NOT_PRESENT; 126 } 127 128 return (flags); 129} 130 131static int 132acpi_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_softc.active; 141 aip->ai_capabilities= 0xff00; /* XXX unknown */ 142 143 if (acpi_acad_get_acline(&acline)) { 144 aip->ai_acline = 0xff; /* unknown */ 145 } else { 146 aip->ai_acline = acline; /* on/off */ 147 } 148 149 if (acpi_battery_get_battinfo(-1, &batt)) { 150 aip->ai_batt_stat = 0xff; /* unknown */ 151 aip->ai_batt_life = 0xff; /* unknown */ 152 aip->ai_batt_time = -1; /* unknown */ 153 aip->ai_batteries = 0; 154 } else { 155 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 156 aip->ai_batt_life = batt.cap; 157 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 158 aip->ai_batteries = acpi_battery_get_units(); 159 } 160 161 return (0); 162} 163 164static int 165acpi_capm_get_pwstatus(apm_pwstatus_t app) 166{ 167 int batt_unit; 168 int acline; 169 struct acpi_battinfo batt; 170 171 if (app->ap_device != PMDV_ALLDEV && 172 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) { 173 return (1); 174 } 175 176 if (app->ap_device == PMDV_ALLDEV) { 177 batt_unit = -1; /* all units */ 178 } else { 179 batt_unit = app->ap_device - PMDV_BATT0; 180 } 181 182 if (acpi_battery_get_battinfo(batt_unit, &batt)) { 183 return (1); 184 } 185 186 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 187 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 188 app->ap_batt_life = batt.cap; 189 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 190 191 if (acpi_acad_get_acline(&acline)) { 192 app->ap_acline = 0xff; /* unknown */ 193 } else { 194 app->ap_acline = acline; /* on/off */ 195 } 196 197 return (0); 198} 199 200static int 201apmopen(dev_t dev, int flag, int fmt, d_thread_t *td) 202{ 203 return (0); 204} 205 206static int 207apmclose(dev_t dev, int flag, int fmt, d_thread_t *td) 208{ 209 return (0); 210} 211 212static int 213apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 214{ 215 int error = 0; 216 struct acpi_softc *acpi_sc; 217 struct apm_info info; 218 apm_info_old_t aiop; 219 220 if ((acpi_sc = device_get_softc(acpi_dev)) == NULL) { 221 return (ENXIO); 222 } 223 224 switch (cmd) { 225 case APMIO_SUSPEND: 226 if (!(flag & FWRITE)) 227 return (EPERM); 228 if (apm_softc.active) 229 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 230 else 231 error = EINVAL; 232 break; 233 234 case APMIO_STANDBY: 235 if (!(flag & FWRITE)) 236 return (EPERM); 237 if (apm_softc.active) 238 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 239 else 240 error = EINVAL; 241 break; 242 243 case APMIO_GETINFO_OLD: 244 if (acpi_capm_get_info(&info)) 245 error = ENXIO; 246 aiop = (apm_info_old_t)addr; 247 aiop->ai_major = info.ai_major; 248 aiop->ai_minor = info.ai_minor; 249 aiop->ai_acline = info.ai_acline; 250 aiop->ai_batt_stat = info.ai_batt_stat; 251 aiop->ai_batt_life = info.ai_batt_life; 252 aiop->ai_status = info.ai_status; 253 break; 254 255 case APMIO_GETINFO: 256 if (acpi_capm_get_info((apm_info_t)addr)) 257 error = ENXIO; 258 259 break; 260 261 case APMIO_GETPWSTATUS: 262 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 263 error = ENXIO; 264 break; 265 266 case APMIO_ENABLE: 267 if (!(flag & FWRITE)) 268 return (EPERM); 269 apm_softc.active = 1; 270 break; 271 272 case APMIO_DISABLE: 273 if (!(flag & FWRITE)) 274 return (EPERM); 275 apm_softc.active = 0; 276 break; 277 278 case APMIO_HALTCPU: 279 break; 280 281 case APMIO_NOTHALTCPU: 282 break; 283 284 case APMIO_DISPLAY: 285 if (!(flag & FWRITE)) 286 return (EPERM); 287 break; 288 289 case APMIO_BIOS: 290 if (!(flag & FWRITE)) 291 return (EPERM); 292 bzero(addr, sizeof(struct apm_bios_arg)); 293 break; 294 295 default: 296 error = EINVAL; 297 break; 298 } 299 300 return (error); 301} 302 303static int 304apmwrite(dev_t dev, struct uio *uio, int ioflag) 305{ 306 307 return (uio->uio_resid); 308} 309 310static int 311apmpoll(dev_t dev, int events, d_thread_t *td) 312{ 313 return (0); 314} 315 316static void 317acpi_capm_init(struct acpi_softc *sc) 318{ 319 320 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 321} 322 323int 324acpi_machdep_init(device_t dev) 325{ 326 struct acpi_softc *sc; 327 328 acpi_dev = dev; 329 if ((sc = device_get_softc(acpi_dev)) == NULL) { 330 return (ENXIO); 331 } 332 333 /* 334 * XXX: Prevent the PnP BIOS code from interfering with 335 * our own scan of ISA devices. 336 */ 337 PnPBIOStable = NULL; 338 339 acpi_capm_init(sc); 340 341 acpi_install_wakeup_handler(sc); 342 343#ifdef SMP 344 acpi_SetIntrModel(ACPI_INTR_APIC); 345#endif 346 return (0); 347} 348
| 74}; 75 76static int 77acpi_capm_convert_battstate(struct acpi_battinfo *battp) 78{ 79 int state; 80 81 state = 0xff; /* XXX unknown */ 82 83 if (battp->state & ACPI_BATT_STAT_DISCHARG) { 84 if (battp->cap >= 50) { 85 state = 0; /* high */ 86 } else { 87 state = 1; /* low */ 88 } 89 } 90 if (battp->state & ACPI_BATT_STAT_CRITICAL) { 91 state = 2; /* critical */ 92 } 93 if (battp->state & ACPI_BATT_STAT_CHARGING) { 94 state = 3; /* charging */ 95 } 96 return (state); 97} 98 99static int 100acpi_capm_convert_battflags(struct acpi_battinfo *battp) 101{ 102 int flags; 103 104 flags = 0; 105 106 if (battp->cap >= 50) { 107 flags |= APM_BATT_HIGH; 108 } else { 109 if (battp->state & ACPI_BATT_STAT_CRITICAL) { 110 flags |= APM_BATT_CRITICAL; 111 } else { 112 flags |= APM_BATT_LOW; 113 } 114 } 115 if (battp->state & ACPI_BATT_STAT_CHARGING) { 116 flags |= APM_BATT_CHARGING; 117 } 118 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) { 119 flags = APM_BATT_NOT_PRESENT; 120 } 121 122 return (flags); 123} 124 125static int 126acpi_capm_get_info(apm_info_t aip) 127{ 128 int acline; 129 struct acpi_battinfo batt; 130 131 aip->ai_infoversion = 1; 132 aip->ai_major = 1; 133 aip->ai_minor = 2; 134 aip->ai_status = apm_softc.active; 135 aip->ai_capabilities= 0xff00; /* XXX unknown */ 136 137 if (acpi_acad_get_acline(&acline)) { 138 aip->ai_acline = 0xff; /* unknown */ 139 } else { 140 aip->ai_acline = acline; /* on/off */ 141 } 142 143 if (acpi_battery_get_battinfo(-1, &batt)) { 144 aip->ai_batt_stat = 0xff; /* unknown */ 145 aip->ai_batt_life = 0xff; /* unknown */ 146 aip->ai_batt_time = -1; /* unknown */ 147 aip->ai_batteries = 0; 148 } else { 149 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 150 aip->ai_batt_life = batt.cap; 151 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 152 aip->ai_batteries = acpi_battery_get_units(); 153 } 154 155 return (0); 156} 157 158static int 159acpi_capm_get_pwstatus(apm_pwstatus_t app) 160{ 161 int batt_unit; 162 int acline; 163 struct acpi_battinfo batt; 164 165 if (app->ap_device != PMDV_ALLDEV && 166 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) { 167 return (1); 168 } 169 170 if (app->ap_device == PMDV_ALLDEV) { 171 batt_unit = -1; /* all units */ 172 } else { 173 batt_unit = app->ap_device - PMDV_BATT0; 174 } 175 176 if (acpi_battery_get_battinfo(batt_unit, &batt)) { 177 return (1); 178 } 179 180 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 181 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 182 app->ap_batt_life = batt.cap; 183 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 184 185 if (acpi_acad_get_acline(&acline)) { 186 app->ap_acline = 0xff; /* unknown */ 187 } else { 188 app->ap_acline = acline; /* on/off */ 189 } 190 191 return (0); 192} 193 194static int 195apmopen(dev_t dev, int flag, int fmt, d_thread_t *td) 196{ 197 return (0); 198} 199 200static int 201apmclose(dev_t dev, int flag, int fmt, d_thread_t *td) 202{ 203 return (0); 204} 205 206static int 207apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 208{ 209 int error = 0; 210 struct acpi_softc *acpi_sc; 211 struct apm_info info; 212 apm_info_old_t aiop; 213 214 if ((acpi_sc = device_get_softc(acpi_dev)) == NULL) { 215 return (ENXIO); 216 } 217 218 switch (cmd) { 219 case APMIO_SUSPEND: 220 if (!(flag & FWRITE)) 221 return (EPERM); 222 if (apm_softc.active) 223 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 224 else 225 error = EINVAL; 226 break; 227 228 case APMIO_STANDBY: 229 if (!(flag & FWRITE)) 230 return (EPERM); 231 if (apm_softc.active) 232 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 233 else 234 error = EINVAL; 235 break; 236 237 case APMIO_GETINFO_OLD: 238 if (acpi_capm_get_info(&info)) 239 error = ENXIO; 240 aiop = (apm_info_old_t)addr; 241 aiop->ai_major = info.ai_major; 242 aiop->ai_minor = info.ai_minor; 243 aiop->ai_acline = info.ai_acline; 244 aiop->ai_batt_stat = info.ai_batt_stat; 245 aiop->ai_batt_life = info.ai_batt_life; 246 aiop->ai_status = info.ai_status; 247 break; 248 249 case APMIO_GETINFO: 250 if (acpi_capm_get_info((apm_info_t)addr)) 251 error = ENXIO; 252 253 break; 254 255 case APMIO_GETPWSTATUS: 256 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 257 error = ENXIO; 258 break; 259 260 case APMIO_ENABLE: 261 if (!(flag & FWRITE)) 262 return (EPERM); 263 apm_softc.active = 1; 264 break; 265 266 case APMIO_DISABLE: 267 if (!(flag & FWRITE)) 268 return (EPERM); 269 apm_softc.active = 0; 270 break; 271 272 case APMIO_HALTCPU: 273 break; 274 275 case APMIO_NOTHALTCPU: 276 break; 277 278 case APMIO_DISPLAY: 279 if (!(flag & FWRITE)) 280 return (EPERM); 281 break; 282 283 case APMIO_BIOS: 284 if (!(flag & FWRITE)) 285 return (EPERM); 286 bzero(addr, sizeof(struct apm_bios_arg)); 287 break; 288 289 default: 290 error = EINVAL; 291 break; 292 } 293 294 return (error); 295} 296 297static int 298apmwrite(dev_t dev, struct uio *uio, int ioflag) 299{ 300 301 return (uio->uio_resid); 302} 303 304static int 305apmpoll(dev_t dev, int events, d_thread_t *td) 306{ 307 return (0); 308} 309 310static void 311acpi_capm_init(struct acpi_softc *sc) 312{ 313 314 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 315} 316 317int 318acpi_machdep_init(device_t dev) 319{ 320 struct acpi_softc *sc; 321 322 acpi_dev = dev; 323 if ((sc = device_get_softc(acpi_dev)) == NULL) { 324 return (ENXIO); 325 } 326 327 /* 328 * XXX: Prevent the PnP BIOS code from interfering with 329 * our own scan of ISA devices. 330 */ 331 PnPBIOStable = NULL; 332 333 acpi_capm_init(sc); 334 335 acpi_install_wakeup_handler(sc); 336 337#ifdef SMP 338 acpi_SetIntrModel(ACPI_INTR_APIC); 339#endif 340 return (0); 341} 342
|