Module Name: src Committed By: jruoho Date: Mon Apr 12 18:55:27 UTC 2010
Modified Files: src/sys/dev/acpi: acpi.c acpi_quirks.c acpivar.h Log Message: Remove some unused and global variables. Make the code more readable. Deny invalid writes to machdep.sleep_state. Cosmetics. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.165 -r1.166 src/sys/dev/acpi/acpi.c cvs rdiff -u -r1.11 -r1.12 src/sys/dev/acpi/acpi_quirks.c cvs rdiff -u -r1.45 -r1.46 src/sys/dev/acpi/acpivar.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/acpi/acpi.c diff -u src/sys/dev/acpi/acpi.c:1.165 src/sys/dev/acpi/acpi.c:1.166 --- src/sys/dev/acpi/acpi.c:1.165 Mon Apr 12 12:14:26 2010 +++ src/sys/dev/acpi/acpi.c Mon Apr 12 18:55:27 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: acpi.c,v 1.165 2010/04/12 12:14:26 jruoho Exp $ */ +/* $NetBSD: acpi.c,v 1.166 2010/04/12 18:55:27 jruoho Exp $ */ /*- * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc. @@ -70,7 +70,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.165 2010/04/12 12:14:26 jruoho Exp $"); +__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.166 2010/04/12 18:55:27 jruoho Exp $"); #include "opt_acpi.h" #include "opt_pcifixup.h" @@ -94,7 +94,7 @@ #include <dev/acpi/acpidevs_data.h> #endif -#define _COMPONENT ACPI_TOOLS +#define _COMPONENT ACPI_BUS_COMPONENT ACPI_MODULE_NAME ("acpi") #if defined(ACPI_PCI_FIXUP) @@ -135,6 +135,7 @@ static int sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS); static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS); +static int sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS); extern struct cfdriver acpi_cd; @@ -150,15 +151,8 @@ int acpi_force_load; int acpi_suspended = 0; -/* - * Pointer to the ACPI subsystem's state. There can be only - * one ACPI instance. - */ struct acpi_softc *acpi_softc; - -/* - * Locking stuff. - */ +static uint64_t acpi_root_pointer; extern kmutex_t acpi_interrupt_list_mtx; /* @@ -184,14 +178,6 @@ }; /* - * sysctl-related information - */ - -static uint64_t acpi_root_pointer; /* found as hw.acpi.root */ -static int acpi_sleepstate = ACPI_STATE_S0; -static char acpi_supported_states[3 * 6 + 1] = ""; - -/* * Prototypes. */ static void acpi_build_tree(struct acpi_softc *); @@ -199,10 +185,10 @@ void *, void **); static void acpi_enable_fixed_events(struct acpi_softc *); +static void acpi_sleep_init(struct acpi_softc *); static ACPI_TABLE_HEADER *acpi_map_rsdt(void); static void acpi_unmap_rsdt(ACPI_TABLE_HEADER *); -static int is_available_state(struct acpi_softc *, int); static bool acpi_suspend(device_t, const pmf_qual_t *); static bool acpi_resume(device_t, const pmf_qual_t *); @@ -435,8 +421,8 @@ { struct acpi_softc *sc = device_private(self); struct acpibus_attach_args *aa = aux; - ACPI_STATUS rv; ACPI_TABLE_HEADER *rsdt; + ACPI_STATUS rv; aprint_naive("\n"); aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION); @@ -461,6 +447,7 @@ sc->sc_dev = self; sc->sc_quirks = acpi_find_quirks(); + sc->sc_sleepstate = ACPI_STATE_S0; sc->sc_iot = aa->aa_iot; sc->sc_memt = aa->aa_memt; @@ -540,16 +527,9 @@ if (acpi_dbgr & ACPI_DBGR_PROBE) acpi_osd_debugger(); #endif - acpi_build_tree(sc); - snprintf(acpi_supported_states, sizeof(acpi_supported_states), - "%s%s%s%s%s%s", - is_available_state(sc, ACPI_STATE_S0) ? "S0 " : "", - is_available_state(sc, ACPI_STATE_S1) ? "S1 " : "", - is_available_state(sc, ACPI_STATE_S2) ? "S2 " : "", - is_available_state(sc, ACPI_STATE_S3) ? "S3 " : "", - is_available_state(sc, ACPI_STATE_S4) ? "S4 " : "", - is_available_state(sc, ACPI_STATE_S5) ? "S5 " : ""); + acpi_build_tree(sc); + acpi_sleep_init(sc); #ifdef ACPI_DEBUGGER if (acpi_dbgr & ACPI_DBGR_RUNNING) @@ -1480,13 +1460,28 @@ * ACPI sleep support. *****************************************************************************/ -static int -is_available_state(struct acpi_softc *sc, int state) +/* + * acpi_sleep_init: + * + * Evaluate supported sleep states. + */ +static void +acpi_sleep_init(struct acpi_softc *sc) { - UINT8 type_a, type_b; + uint8_t a, b, i; + ACPI_STATUS rv; - return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state, - &type_a, &type_b)); + CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1); + CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3); + CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5); + + for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) { + + rv = AcpiGetSleepTypeData(i, &a, &b); + + if (ACPI_SUCCESS(rv)) + sc->sc_sleepstates |= __BIT(i); + } } /* @@ -1494,84 +1489,100 @@ * * Enter to the specified sleep state. */ - ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *sc, int state) { + ACPI_STATUS rv = AE_OK; int err; - ACPI_STATUS ret = AE_OK; - if (state == acpi_sleepstate) + if (state == sc->sc_sleepstate) return AE_OK; - aprint_normal_dev(sc->sc_dev, "entering state %d\n", state); + aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state); switch (state) { + case ACPI_STATE_S0: break; + case ACPI_STATE_S1: case ACPI_STATE_S2: case ACPI_STATE_S3: case ACPI_STATE_S4: - if (!is_available_state(sc, state)) { - aprint_error_dev(sc->sc_dev, - "ACPI S%d not available on this platform\n", state); + + if ((sc->sc_sleepstates & __BIT(state)) == 0) { + aprint_error_dev(sc->sc_dev, "sleep state " + "S%d is not available\n", state); break; } acpi_wakedev_commit(sc, state); - if (state != ACPI_STATE_S1 && !pmf_system_suspend(PMF_Q_NONE)) { + if (state != ACPI_STATE_S1 && + pmf_system_suspend(PMF_Q_NONE) != true) { aprint_error_dev(sc->sc_dev, "aborting suspend\n"); break; } - ret = AcpiEnterSleepStatePrep(state); - if (ACPI_FAILURE(ret)) { - aprint_error_dev(sc->sc_dev, - "failed preparing to sleep (%s)\n", - AcpiFormatException(ret)); + rv = AcpiEnterSleepStatePrep(state); + + if (ACPI_FAILURE(rv)) { + aprint_error_dev(sc->sc_dev, "failed to prepare " + "S%d: %s\n", state, AcpiFormatException(rv)); break; } - acpi_sleepstate = state; + sc->sc_sleepstate = state; + if (state == ACPI_STATE_S1) { - /* just enter the state */ + + /* Just enter the state. */ acpi_md_OsDisableInterrupt(); - ret = AcpiEnterSleepState((UINT8)state); - if (ACPI_FAILURE(ret)) - aprint_error_dev(sc->sc_dev, - "failed to enter sleep state S1: %s\n", - AcpiFormatException(ret)); - AcpiLeaveSleepState((UINT8)state); + rv = AcpiEnterSleepState(state); + + if (ACPI_FAILURE(rv)) + aprint_error_dev(sc->sc_dev, "failed to " + "enter S1: %s\n", AcpiFormatException(rv)); + + (void)AcpiLeaveSleepState(state); + } else { + err = acpi_md_sleep(state); + if (state == ACPI_STATE_S4) AcpiEnable(); + pmf_system_bus_resume(PMF_Q_NONE); - AcpiLeaveSleepState((UINT8)state); + (void)AcpiLeaveSleepState(state); pmf_system_resume(PMF_Q_NONE); } break; case ACPI_STATE_S5: - ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5); - if (ACPI_FAILURE(ret)) { - aprint_error_dev(sc->sc_dev, - "failed preparing to sleep (%s)\n", - AcpiFormatException(ret)); + + rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5); + + if (ACPI_FAILURE(rv)) { + aprint_error_dev(sc->sc_dev, "failed to prepare " + "S%d: %s\n", state, AcpiFormatException(rv)); break; } + DELAY(1000000); - acpi_sleepstate = state; + + sc->sc_sleepstate = state; acpi_md_OsDisableInterrupt(); - AcpiEnterSleepState(ACPI_STATE_S5); - aprint_error_dev(sc->sc_dev, "WARNING powerdown failed!\n"); + + (void)AcpiEnterSleepState(ACPI_STATE_S5); + + aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n"); break; } - acpi_sleepstate = ACPI_STATE_S0; - return ret; + sc->sc_sleepstate = ACPI_STATE_S0; + + return rv; } #ifdef ACPI_ACTIVATE_DEV @@ -1766,7 +1777,7 @@ (void)sysctl_createv(NULL, 0, &rnode, NULL, CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING, "supported_states", SYSCTL_DESCR("Supported system states"), - NULL, 0, acpi_supported_states, 0, + sysctl_hw_acpi_sleepstates, 0, NULL, 0, CTL_CREATE, CTL_EOL); err = sysctl_createv(NULL, 0, NULL, &mnode, @@ -1844,11 +1855,15 @@ static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS) { + struct acpi_softc *sc = acpi_softc; struct sysctlnode node; int err, t; + if (acpi_softc == NULL) + return ENOSYS; + node = *rnode; - t = acpi_sleepstate; + t = sc->sc_sleepstate; node.sysctl_data = &t; err = sysctl_lookup(SYSCTLFN_CALL(&node)); @@ -1856,13 +1871,42 @@ if (err || newp == NULL) return err; + if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5) + return EINVAL; + + acpi_enter_sleep_state(sc, t); + + return 0; +} + +static int +sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS) +{ + struct acpi_softc *sc = acpi_softc; + struct sysctlnode node; + char t[3 * 6 + 1]; + int err; + if (acpi_softc == NULL) return ENOSYS; - if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5) - return EINVAL; + (void)memset(t, '\0', sizeof(t)); + + (void)snprintf(t, sizeof(t), "%s%s%s%s%s%s", + ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "", + ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "", + ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "", + ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "", + ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "", + ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : ""); + + node = *rnode; + node.sysctl_data = &t; - acpi_enter_sleep_state(acpi_softc, t); + err = sysctl_lookup(SYSCTLFN_CALL(&node)); + + if (err || newp == NULL) + return err; return 0; } Index: src/sys/dev/acpi/acpi_quirks.c diff -u src/sys/dev/acpi/acpi_quirks.c:1.11 src/sys/dev/acpi/acpi_quirks.c:1.12 --- src/sys/dev/acpi/acpi_quirks.c:1.11 Fri Mar 5 14:00:17 2010 +++ src/sys/dev/acpi/acpi_quirks.c Mon Apr 12 18:55:27 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: acpi_quirks.c,v 1.11 2010/03/05 14:00:17 jruoho Exp $ */ +/* $NetBSD: acpi_quirks.c,v 1.12 2010/04/12 18:55:27 jruoho Exp $ */ /* * Copyright 2002 Wasabi Systems, Inc. @@ -37,7 +37,7 @@ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: acpi_quirks.c,v 1.11 2010/03/05 14:00:17 jruoho Exp $"); +__KERNEL_RCSID(0, "$NetBSD: acpi_quirks.c,v 1.12 2010/04/12 18:55:27 jruoho Exp $"); #include "opt_acpi.h" @@ -47,6 +47,12 @@ #include <dev/acpi/acpivar.h> +#define AQ_GT 0 /* > */ +#define AQ_LT 1 /* < */ +#define AQ_GTE 2 /* >= */ +#define AQ_LTE 3 /* <= */ +#define AQ_EQ 4 /* == */ + static int acpi_rev_cmp(uint32_t, uint32_t, int); /* Index: src/sys/dev/acpi/acpivar.h diff -u src/sys/dev/acpi/acpivar.h:1.45 src/sys/dev/acpi/acpivar.h:1.46 --- src/sys/dev/acpi/acpivar.h:1.45 Mon Apr 12 12:14:26 2010 +++ src/sys/dev/acpi/acpivar.h Mon Apr 12 18:55:27 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: acpivar.h,v 1.45 2010/04/12 12:14:26 jruoho Exp $ */ +/* $NetBSD: acpivar.h,v 1.46 2010/04/12 18:55:27 jruoho Exp $ */ /* * Copyright 2001 Wasabi Systems, Inc. @@ -66,14 +66,6 @@ }; /* - * Types of switches that ACPI understands. - */ -#define ACPI_SWITCH_POWERBUTTON 0 -#define ACPI_SWITCH_SLEEPBUTTON 1 -#define ACPI_SWITCH_LID 2 -#define ACPI_NSWITCHES 3 - -/* * ACPI driver capabilities. */ #define ACPI_DEVICE_POWER __BIT(0) @@ -119,13 +111,8 @@ struct sysmon_pswitch sc_smpsw_power; struct sysmon_pswitch sc_smpsw_sleep; - /* - * Sleep state to transition to when a given - * switch is activated. - */ - int sc_switch_sleep[ACPI_NSWITCHES]; - int sc_sleepstate; /* current sleep state */ + int sc_sleepstates; /* supported sleep states */ int sc_quirks; @@ -313,12 +300,6 @@ int aq_quirks; /* the actual quirks */ }; -#define AQ_GT 0 /* > */ -#define AQ_LT 1 /* < */ -#define AQ_GTE 2 /* >= */ -#define AQ_LTE 3 /* <= */ -#define AQ_EQ 4 /* == */ - #define ACPI_QUIRK_BROKEN 0x00000001 /* totally broken */ #define ACPI_QUIRK_BADPCI 0x00000002 /* bad PCI hierarchy */ #define ACPI_QUIRK_BADBBN 0x00000004 /* _BBN broken */