Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.41.6.8
      1  1.41.6.8     joerg /*	$NetBSD: acpi_ec.c,v 1.41.6.8 2007/10/04 10:43:47 joerg Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4  1.41.6.5     joerg  * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8       1.1   thorpej  * modification, are permitted provided that the following conditions
      9       1.1   thorpej  * are met:
     10  1.41.6.5     joerg  *
     11       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.41.6.5     joerg  *    notice, this list of conditions and the following disclaimer in
     15  1.41.6.5     joerg  *    the documentation and/or other materials provided with the
     16  1.41.6.5     joerg  *    distribution.
     17  1.41.6.5     joerg  *
     18  1.41.6.5     joerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  1.41.6.5     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  1.41.6.5     joerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  1.41.6.5     joerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     22  1.41.6.5     joerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.41.6.5     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  1.41.6.5     joerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  1.41.6.5     joerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  1.41.6.5     joerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  1.41.6.5     joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28  1.41.6.5     joerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1   thorpej  * SUCH DAMAGE.
     30       1.1   thorpej  */
     31       1.1   thorpej 
     32       1.3     lukem #include <sys/cdefs.h>
     33  1.41.6.8     joerg __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.41.6.8 2007/10/04 10:43:47 joerg Exp $");
     34       1.1   thorpej 
     35       1.1   thorpej #include <sys/param.h>
     36       1.1   thorpej #include <sys/systm.h>
     37  1.41.6.5     joerg #include <sys/condvar.h>
     38       1.1   thorpej #include <sys/device.h>
     39       1.1   thorpej #include <sys/kernel.h>
     40  1.41.6.5     joerg #include <sys/kthread.h>
     41  1.41.6.5     joerg #include <sys/mutex.h>
     42       1.1   thorpej 
     43       1.1   thorpej #include <machine/bus.h>
     44       1.1   thorpej 
     45       1.1   thorpej #include <dev/acpi/acpivar.h>
     46       1.1   thorpej 
     47  1.41.6.7     joerg /* Maximum time to wait for global ACPI lock in ms */
     48  1.41.6.5     joerg #define	EC_LOCK_TIMEOUT		5
     49      1.23     kochi 
     50  1.41.6.7     joerg /* Maximum time to poll for completion of a command  in ms */
     51  1.41.6.7     joerg #define	EC_POLL_TIMEOUT		5
     52  1.41.6.7     joerg 
     53  1.41.6.5     joerg /* From ACPI 3.0b, chapter 12.3 */
     54  1.41.6.5     joerg #define EC_COMMAND_READ		0x80
     55  1.41.6.5     joerg #define	EC_COMMAND_WRITE	0x81
     56  1.41.6.5     joerg #define	EC_COMMAND_BURST_EN	0x82
     57  1.41.6.5     joerg #define	EC_COMMAND_BURST_DIS	0x83
     58  1.41.6.5     joerg #define	EC_COMMAND_QUERY	0x84
     59  1.41.6.5     joerg 
     60  1.41.6.5     joerg /* From ACPI 3.0b, chapter 12.2.1 */
     61  1.41.6.5     joerg #define	EC_STATUS_OBF		0x01
     62  1.41.6.5     joerg #define	EC_STATUS_IBF		0x02
     63  1.41.6.5     joerg #define	EC_STATUS_CMD		0x08
     64  1.41.6.5     joerg #define	EC_STATUS_BURST		0x10
     65  1.41.6.5     joerg #define	EC_STATUS_SCI		0x20
     66  1.41.6.5     joerg #define	EC_STATUS_SMI		0x40
     67       1.1   thorpej 
     68  1.41.6.5     joerg static const char *ec_hid[] = {
     69  1.41.6.5     joerg 	"PNP0C09",
     70  1.41.6.5     joerg 	NULL,
     71  1.41.6.5     joerg };
     72       1.1   thorpej 
     73  1.41.6.5     joerg enum ec_state_t {
     74  1.41.6.5     joerg 	EC_STATE_QUERY,
     75  1.41.6.7     joerg 	EC_STATE_QUERY_VAL,
     76  1.41.6.5     joerg 	EC_STATE_READ,
     77  1.41.6.7     joerg 	EC_STATE_READ_ADDR,
     78  1.41.6.5     joerg 	EC_STATE_READ_VAL,
     79  1.41.6.5     joerg 	EC_STATE_WRITE,
     80  1.41.6.7     joerg 	EC_STATE_WRITE_ADDR,
     81  1.41.6.5     joerg 	EC_STATE_WRITE_VAL,
     82  1.41.6.5     joerg 	EC_STATE_FREE
     83  1.41.6.5     joerg };
     84       1.1   thorpej 
     85  1.41.6.5     joerg struct acpiec_softc {
     86  1.41.6.5     joerg 	ACPI_HANDLE sc_ech;
     87       1.1   thorpej 
     88  1.41.6.5     joerg 	ACPI_HANDLE sc_gpeh;
     89  1.41.6.5     joerg 	UINT8 sc_gpebit;
     90       1.1   thorpej 
     91  1.41.6.5     joerg 	bus_space_tag_t sc_data_st;
     92  1.41.6.5     joerg 	bus_space_handle_t sc_data_sh;
     93       1.1   thorpej 
     94  1.41.6.5     joerg 	bus_space_tag_t sc_csr_st;
     95  1.41.6.5     joerg 	bus_space_handle_t sc_csr_sh;
     96      1.17   mycroft 
     97  1.41.6.5     joerg 	bool sc_need_global_lock;
     98  1.41.6.5     joerg 	UINT32 sc_global_lock;
     99       1.1   thorpej 
    100  1.41.6.5     joerg 	kmutex_t sc_mtx, sc_access_mtx;
    101  1.41.6.5     joerg 	kcondvar_t sc_cv, sc_cv_sci;
    102  1.41.6.5     joerg 	enum ec_state_t sc_state;
    103  1.41.6.5     joerg 	bool sc_got_sci;
    104  1.41.6.5     joerg 
    105  1.41.6.5     joerg 	uint8_t sc_cur_addr, sc_cur_val;
    106      1.16     kochi };
    107      1.16     kochi 
    108  1.41.6.5     joerg static int acpiecdt_match(device_t, struct cfdata *, void *);
    109  1.41.6.5     joerg static void acpiecdt_attach(device_t, device_t, void *);
    110       1.1   thorpej 
    111  1.41.6.5     joerg static int acpiec_match(device_t, struct cfdata *, void *);
    112  1.41.6.5     joerg static void acpiec_attach(device_t, device_t, void *);
    113  1.41.6.5     joerg 
    114  1.41.6.5     joerg static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
    115  1.41.6.5     joerg     bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
    116  1.41.6.5     joerg 
    117  1.41.6.5     joerg static pnp_status_t acpiec_power(device_t, pnp_request_t, void *);
    118      1.17   mycroft 
    119  1.41.6.5     joerg static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
    120  1.41.6.5     joerg     ACPI_HANDLE *, uint8_t *);
    121      1.20      yamt 
    122  1.41.6.5     joerg static void acpiec_gpe_query(void *);
    123  1.41.6.5     joerg static UINT32 acpiec_gpe_handler(void *);
    124  1.41.6.5     joerg static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
    125  1.41.6.5     joerg static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
    126  1.41.6.5     joerg     UINT32, ACPI_INTEGER *, void *, void *);
    127      1.20      yamt 
    128  1.41.6.5     joerg static void acpiec_gpe_state_maschine(device_t);
    129      1.20      yamt 
    130  1.41.6.5     joerg CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
    131      1.20      yamt     acpiec_match, acpiec_attach, NULL, NULL);
    132      1.20      yamt 
    133  1.41.6.5     joerg CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
    134  1.41.6.5     joerg     acpiecdt_match, acpiecdt_attach, NULL, NULL);
    135      1.23     kochi 
    136  1.41.6.5     joerg static device_t ec_singleton = NULL;
    137  1.41.6.5     joerg static bool acpiec_cold = false;
    138  1.41.6.5     joerg 
    139  1.41.6.5     joerg static bool
    140  1.41.6.5     joerg acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
    141  1.41.6.5     joerg     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
    142       1.9  tshiozak {
    143  1.41.6.6  jmcneill 	ACPI_TABLE_ECDT *ecdt;
    144  1.41.6.5     joerg 	ACPI_STATUS rv;
    145  1.41.6.5     joerg 
    146  1.41.6.6  jmcneill 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
    147  1.41.6.6  jmcneill 	if (ACPI_FAILURE(rv))
    148  1.41.6.5     joerg 		return false;
    149  1.41.6.5     joerg 
    150  1.41.6.6  jmcneill 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
    151  1.41.6.5     joerg 		aprint_error_dev(parent,
    152  1.41.6.5     joerg 		    "ECDT register width invalid (%d/%d)\n",
    153  1.41.6.6  jmcneill 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
    154  1.41.6.5     joerg 		return false;
    155  1.41.6.5     joerg 	}
    156  1.41.6.5     joerg 
    157  1.41.6.6  jmcneill 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
    158  1.41.6.6  jmcneill 	if (ACPI_FAILURE(rv)) {
    159  1.41.6.5     joerg 		aprint_error_dev(parent,
    160  1.41.6.5     joerg 		    "failed to look up EC object %s: %s\n",
    161  1.41.6.6  jmcneill 		    ecdt->Id, AcpiFormatException(rv));
    162  1.41.6.5     joerg 		return false;
    163  1.41.6.5     joerg 	}
    164  1.41.6.5     joerg 
    165  1.41.6.6  jmcneill 	*cmd_reg = ecdt->Control.Address;
    166  1.41.6.6  jmcneill 	*data_reg = ecdt->Data.Address;
    167  1.41.6.6  jmcneill 	*gpebit = ecdt->Gpe;
    168      1.24     kochi 
    169  1.41.6.5     joerg 	return true;
    170       1.9  tshiozak }
    171       1.4   thorpej 
    172  1.41.6.5     joerg static int
    173  1.41.6.5     joerg acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
    174       1.1   thorpej {
    175  1.41.6.5     joerg 	ACPI_HANDLE ec_handle;
    176  1.41.6.5     joerg 	bus_addr_t cmd_reg, data_reg;
    177  1.41.6.5     joerg 	uint8_t gpebit;
    178      1.20      yamt 
    179  1.41.6.5     joerg 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    180  1.41.6.5     joerg 		return 1;
    181  1.41.6.5     joerg 	else
    182  1.41.6.5     joerg 		return 0;
    183       1.1   thorpej }
    184       1.1   thorpej 
    185  1.41.6.5     joerg static void
    186  1.41.6.5     joerg acpiecdt_attach(device_t parent, device_t self, void *aux)
    187       1.1   thorpej {
    188  1.41.6.5     joerg 	ACPI_HANDLE ec_handle;
    189  1.41.6.5     joerg 	bus_addr_t cmd_reg, data_reg;
    190  1.41.6.5     joerg 	uint8_t gpebit;
    191      1.20      yamt 
    192  1.41.6.5     joerg 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    193  1.41.6.5     joerg 		panic("ECDT disappeared");
    194  1.41.6.5     joerg 
    195  1.41.6.5     joerg 	aprint_naive(": ACPI Embedded Controller via ECDT\n");
    196  1.41.6.5     joerg 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
    197  1.41.6.5     joerg 
    198  1.41.6.5     joerg 	acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
    199  1.41.6.5     joerg 	    NULL, gpebit);
    200       1.1   thorpej }
    201       1.1   thorpej 
    202      1.31     kochi static int
    203  1.41.6.5     joerg acpiec_match(device_t parent, struct cfdata *match, void *aux)
    204       1.1   thorpej {
    205       1.1   thorpej 	struct acpi_attach_args *aa = aux;
    206       1.1   thorpej 
    207       1.1   thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    208      1.25     kochi 		return 0;
    209       1.1   thorpej 
    210      1.25     kochi 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
    211       1.1   thorpej }
    212       1.1   thorpej 
    213  1.41.6.5     joerg static void
    214  1.41.6.5     joerg acpiec_attach(device_t parent, device_t self, void *aux)
    215      1.17   mycroft {
    216  1.41.6.5     joerg 	struct acpi_attach_args *aa = aux;
    217  1.41.6.5     joerg 	struct acpi_resources ec_res;
    218  1.41.6.5     joerg 	struct acpi_io *io0, *io1;
    219  1.41.6.5     joerg 	ACPI_HANDLE gpe_handle;
    220  1.41.6.5     joerg 	uint8_t gpebit;
    221      1.23     kochi 	ACPI_STATUS rv;
    222      1.17   mycroft 
    223  1.41.6.5     joerg 	if (ec_singleton != NULL) {
    224  1.41.6.5     joerg 		aprint_naive(": ACPI Embedded Controller (disabled)\n");
    225  1.41.6.5     joerg 		aprint_normal(": ACPI Embedded Controller (disabled)\n");
    226  1.41.6.5     joerg 		pnp_register(self, pnp_generic_power);
    227      1.23     kochi 		return;
    228      1.23     kochi 	}
    229      1.23     kochi 
    230  1.41.6.5     joerg 	aprint_naive(": ACPI Embedded Controller\n");
    231  1.41.6.5     joerg 	aprint_normal(": ACPI Embedded Controller\n");
    232      1.23     kochi 
    233  1.41.6.5     joerg 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
    234  1.41.6.5     joerg 				      &gpe_handle, &gpebit))
    235  1.41.6.5     joerg 		return;
    236      1.23     kochi 
    237  1.41.6.5     joerg 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    238  1.41.6.5     joerg 	    &ec_res, &acpi_resource_parse_ops_default);
    239  1.41.6.5     joerg 	if (rv != AE_OK) {
    240  1.41.6.5     joerg 		aprint_error_dev(self, "resource parsing failed: %s\n",
    241      1.23     kochi 		    AcpiFormatException(rv));
    242  1.41.6.5     joerg 		return;
    243      1.17   mycroft 	}
    244      1.23     kochi 
    245  1.41.6.5     joerg 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
    246  1.41.6.5     joerg 		aprint_error_dev(self, "no data register resource\n");
    247  1.41.6.5     joerg 		goto free_res;
    248  1.41.6.5     joerg 	}
    249  1.41.6.5     joerg 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
    250  1.41.6.5     joerg 		aprint_error_dev(self, "no CSR register resource\n");
    251  1.41.6.5     joerg 		goto free_res;
    252  1.41.6.5     joerg 	}
    253      1.23     kochi 
    254  1.41.6.5     joerg 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
    255  1.41.6.5     joerg 	    io1->ar_base, io0->ar_base, gpe_handle, gpebit);
    256      1.23     kochi 
    257  1.41.6.5     joerg free_res:
    258  1.41.6.5     joerg 	acpi_resource_cleanup(&ec_res);
    259      1.17   mycroft }
    260      1.17   mycroft 
    261      1.31     kochi static void
    262  1.41.6.5     joerg acpiec_common_attach(device_t parent, device_t self,
    263  1.41.6.5     joerg     ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
    264  1.41.6.5     joerg     ACPI_HANDLE gpe_handle, uint8_t gpebit)
    265       1.1   thorpej {
    266  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(self);
    267       1.1   thorpej 	ACPI_STATUS rv;
    268  1.41.6.5     joerg 	ACPI_INTEGER val;
    269       1.1   thorpej 
    270  1.41.6.5     joerg 	sc->sc_ech = ec_handle;
    271  1.41.6.5     joerg 	sc->sc_gpeh = gpe_handle;
    272  1.41.6.5     joerg 	sc->sc_gpebit = gpebit;
    273  1.41.6.5     joerg 
    274  1.41.6.5     joerg 	sc->sc_state = EC_STATE_FREE;
    275  1.41.6.5     joerg 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
    276  1.41.6.5     joerg 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
    277  1.41.6.5     joerg 	cv_init(&sc->sc_cv, "eccv");
    278  1.41.6.5     joerg 	cv_init(&sc->sc_cv_sci, "ecsci");
    279  1.41.6.5     joerg 
    280  1.41.6.5     joerg 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
    281  1.41.6.5     joerg 	    &sc->sc_data_sh) != 0) {
    282  1.41.6.5     joerg 		aprint_error_dev(self, "unable to map data register\n");
    283  1.41.6.5     joerg 		return;
    284  1.41.6.5     joerg 	}
    285       1.1   thorpej 
    286  1.41.6.5     joerg 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
    287  1.41.6.5     joerg 		aprint_error_dev(self, "unable to map CSR register\n");
    288  1.41.6.5     joerg 		goto post_data_map;
    289  1.41.6.5     joerg 	}
    290      1.17   mycroft 
    291  1.41.6.5     joerg 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
    292  1.41.6.5     joerg 	if (rv == AE_OK) {
    293  1.41.6.5     joerg 		sc->sc_need_global_lock = val != 0;
    294  1.41.6.5     joerg 	} else if (rv != AE_NOT_FOUND) {
    295  1.41.6.5     joerg 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
    296  1.41.6.5     joerg 		    AcpiFormatException(rv));
    297  1.41.6.5     joerg 		goto post_csr_map;
    298  1.41.6.5     joerg 	} else {
    299  1.41.6.5     joerg 		sc->sc_need_global_lock = false;
    300  1.41.6.5     joerg 	}
    301  1.41.6.5     joerg 	if (sc->sc_need_global_lock)
    302  1.41.6.5     joerg 		aprint_normal_dev(self, "using global ACPI lock\n");
    303       1.1   thorpej 
    304  1.41.6.5     joerg 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
    305  1.41.6.5     joerg 	    acpiec_space_handler, acpiec_space_setup, self);
    306  1.41.6.5     joerg 	if (rv != AE_OK) {
    307  1.41.6.5     joerg 		aprint_error_dev(self,
    308  1.41.6.5     joerg 		    "unable to install address space handler: %s\n",
    309  1.41.6.5     joerg 		    AcpiFormatException(rv));
    310  1.41.6.5     joerg 		goto post_csr_map;
    311  1.41.6.5     joerg 	}
    312       1.1   thorpej 
    313  1.41.6.5     joerg 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    314  1.41.6.5     joerg 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
    315  1.41.6.5     joerg 	if (rv != AE_OK) {
    316  1.41.6.5     joerg 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
    317  1.41.6.5     joerg 		    AcpiFormatException(rv));
    318  1.41.6.5     joerg 		goto post_csr_map;
    319  1.41.6.5     joerg 	}
    320      1.23     kochi 
    321  1.41.6.5     joerg 	rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
    322  1.41.6.5     joerg 	if (rv != AE_OK) {
    323  1.41.6.5     joerg 		aprint_error_dev(self, "unable to set GPE type: %s\n",
    324  1.41.6.5     joerg 		    AcpiFormatException(rv));
    325  1.41.6.5     joerg 		goto post_csr_map;
    326       1.1   thorpej 	}
    327       1.1   thorpej 
    328  1.41.6.5     joerg 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    329  1.41.6.5     joerg 	if (rv != AE_OK) {
    330  1.41.6.5     joerg 		aprint_error_dev(self, "unable to enable GPE: %s\n",
    331  1.41.6.5     joerg 		    AcpiFormatException(rv));
    332  1.41.6.5     joerg 		goto post_csr_map;
    333  1.41.6.5     joerg 	}
    334      1.23     kochi 
    335  1.41.6.5     joerg 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
    336  1.41.6.5     joerg 		           self, NULL, "acpiec sci thread")) {
    337  1.41.6.5     joerg 		aprint_error_dev(self, "unable to create query kthread\n");
    338  1.41.6.5     joerg 		goto post_csr_map;
    339      1.17   mycroft 	}
    340      1.17   mycroft 
    341  1.41.6.5     joerg 	ec_singleton = self;
    342       1.1   thorpej 
    343  1.41.6.5     joerg 	pnp_register(self, acpiec_power);
    344       1.1   thorpej 
    345  1.41.6.5     joerg 	return;
    346       1.1   thorpej 
    347  1.41.6.5     joerg post_csr_map:
    348  1.41.6.5     joerg 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    349  1.41.6.5     joerg 	    acpiec_gpe_handler);
    350  1.41.6.5     joerg 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
    351  1.41.6.5     joerg 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
    352  1.41.6.5     joerg 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
    353  1.41.6.5     joerg post_data_map:
    354  1.41.6.5     joerg 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
    355  1.41.6.5     joerg }
    356  1.41.6.5     joerg 
    357  1.41.6.5     joerg static pnp_status_t
    358  1.41.6.5     joerg acpiec_power(device_t dv, pnp_request_t req, void *opaque)
    359  1.41.6.5     joerg {
    360  1.41.6.5     joerg 	pnp_capabilities_t *pcaps;
    361  1.41.6.5     joerg 	pnp_state_t *pstate;
    362  1.41.6.5     joerg 
    363  1.41.6.5     joerg 	switch (req) {
    364  1.41.6.5     joerg 	case PNP_REQUEST_GET_CAPABILITIES:
    365  1.41.6.5     joerg 		pcaps = opaque;
    366  1.41.6.5     joerg 		pcaps->state = PNP_STATE_D0 | PNP_STATE_D3;
    367  1.41.6.5     joerg 		break;
    368  1.41.6.5     joerg 	case PNP_REQUEST_GET_STATE:
    369  1.41.6.5     joerg 		pstate = opaque;
    370  1.41.6.5     joerg 		if (acpiec_cold)
    371  1.41.6.5     joerg 			*pstate = PNP_STATE_D0;
    372  1.41.6.5     joerg 		else
    373  1.41.6.5     joerg 			*pstate = PNP_STATE_D3;
    374  1.41.6.5     joerg 		break;
    375  1.41.6.5     joerg 	case PNP_REQUEST_SET_STATE:
    376  1.41.6.5     joerg 		pstate = opaque;
    377  1.41.6.5     joerg 		switch (*pstate) {
    378  1.41.6.5     joerg 		case PNP_STATE_D0:
    379  1.41.6.5     joerg 			acpiec_cold = false;
    380       1.1   thorpej 			break;
    381  1.41.6.5     joerg 		case PNP_STATE_D3:
    382  1.41.6.5     joerg 			acpiec_cold = true;
    383       1.1   thorpej 			break;
    384  1.41.6.5     joerg 		default:
    385  1.41.6.5     joerg 			return PNP_STATUS_UNSUPPORTED;
    386       1.1   thorpej 		}
    387  1.41.6.5     joerg 		break;
    388  1.41.6.5     joerg 	default:
    389  1.41.6.5     joerg 		return PNP_STATUS_UNSUPPORTED;
    390       1.1   thorpej 	}
    391       1.1   thorpej 
    392  1.41.6.5     joerg 	return PNP_STATUS_SUCCESS;
    393       1.1   thorpej }
    394       1.1   thorpej 
    395  1.41.6.5     joerg static bool
    396  1.41.6.5     joerg acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
    397  1.41.6.5     joerg     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
    398       1.1   thorpej {
    399  1.41.6.5     joerg 	ACPI_BUFFER buf;
    400  1.41.6.5     joerg 	ACPI_OBJECT *p, *c;
    401      1.25     kochi 	ACPI_STATUS rv;
    402       1.1   thorpej 
    403  1.41.6.5     joerg 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
    404  1.41.6.5     joerg 	if (rv != AE_OK) {
    405  1.41.6.5     joerg 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
    406  1.41.6.5     joerg 		    AcpiFormatException(rv));
    407  1.41.6.5     joerg 		return false;
    408  1.41.6.5     joerg 	}
    409  1.41.6.5     joerg 
    410  1.41.6.5     joerg 	p = buf.Pointer;
    411  1.41.6.5     joerg 
    412  1.41.6.5     joerg 	if (p->Type == ACPI_TYPE_INTEGER) {
    413  1.41.6.5     joerg 		*gpe_handle = NULL;
    414  1.41.6.5     joerg 		*gpebit = p->Integer.Value;
    415  1.41.6.5     joerg 		AcpiOsFree(p);
    416  1.41.6.5     joerg 		return true;
    417  1.41.6.5     joerg 	}
    418  1.41.6.5     joerg 
    419  1.41.6.5     joerg 	if (p->Type != ACPI_TYPE_PACKAGE) {
    420  1.41.6.5     joerg 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
    421  1.41.6.5     joerg 		AcpiOsFree(p);
    422  1.41.6.5     joerg 		return false;
    423  1.41.6.5     joerg 	}
    424  1.41.6.5     joerg 
    425  1.41.6.5     joerg 	if (p->Package.Count != 2) {
    426  1.41.6.5     joerg 		aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
    427  1.41.6.5     joerg 		AcpiOsFree(p);
    428  1.41.6.5     joerg 		return false;
    429       1.1   thorpej 	}
    430      1.33     kochi 
    431  1.41.6.5     joerg 	c = &p->Package.Elements[0];
    432  1.41.6.5     joerg 	switch (c->Type) {
    433  1.41.6.5     joerg 	case ACPI_TYPE_LOCAL_REFERENCE:
    434  1.41.6.5     joerg 	case ACPI_TYPE_ANY:
    435  1.41.6.5     joerg 		*gpe_handle = c->Reference.Handle;
    436  1.41.6.5     joerg 		break;
    437  1.41.6.5     joerg 	case ACPI_TYPE_STRING:
    438  1.41.6.5     joerg 		/* XXX should be using real scope here */
    439  1.41.6.5     joerg 		rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
    440  1.41.6.5     joerg 		if (rv != AE_OK) {
    441  1.41.6.5     joerg 			aprint_error_dev(self,
    442  1.41.6.5     joerg 			    "_GPE device reference unresolvable\n");
    443  1.41.6.5     joerg 			AcpiOsFree(p);
    444  1.41.6.5     joerg 			return false;
    445  1.41.6.5     joerg 		}
    446  1.41.6.5     joerg 		break;
    447  1.41.6.5     joerg 	default:
    448  1.41.6.5     joerg 		aprint_error_dev(self, "_GPE device reference incorrect\n");
    449  1.41.6.5     joerg 		AcpiOsFree(p);
    450  1.41.6.5     joerg 		return false;
    451  1.41.6.5     joerg 	}
    452  1.41.6.5     joerg 	c = &p->Package.Elements[1];
    453  1.41.6.5     joerg 	if (c->Type != ACPI_TYPE_INTEGER) {
    454  1.41.6.5     joerg 		aprint_error_dev(self,
    455  1.41.6.5     joerg 		    "_GPE package needs integer as 2nd field\n");
    456  1.41.6.5     joerg 		AcpiOsFree(p);
    457  1.41.6.5     joerg 		return false;
    458  1.41.6.5     joerg 	}
    459  1.41.6.5     joerg 	*gpebit = c->Integer.Value;
    460  1.41.6.5     joerg 	AcpiOsFree(p);
    461  1.41.6.5     joerg 	return true;
    462       1.1   thorpej }
    463       1.1   thorpej 
    464  1.41.6.5     joerg static uint8_t
    465  1.41.6.5     joerg acpiec_read_data(struct acpiec_softc *sc)
    466       1.1   thorpej {
    467  1.41.6.5     joerg 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
    468  1.41.6.5     joerg }
    469       1.1   thorpej 
    470  1.41.6.5     joerg static void
    471  1.41.6.5     joerg acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
    472  1.41.6.5     joerg {
    473  1.41.6.5     joerg 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
    474  1.41.6.5     joerg }
    475       1.1   thorpej 
    476  1.41.6.5     joerg static uint8_t
    477  1.41.6.5     joerg acpiec_read_status(struct acpiec_softc *sc)
    478  1.41.6.5     joerg {
    479  1.41.6.5     joerg 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
    480  1.41.6.5     joerg }
    481       1.1   thorpej 
    482  1.41.6.5     joerg static void
    483  1.41.6.5     joerg acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
    484  1.41.6.5     joerg {
    485  1.41.6.5     joerg 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
    486       1.1   thorpej }
    487       1.1   thorpej 
    488       1.1   thorpej static ACPI_STATUS
    489  1.41.6.5     joerg acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
    490  1.41.6.5     joerg     void **region_arg)
    491       1.1   thorpej {
    492  1.41.6.5     joerg 	if (func == ACPI_REGION_DEACTIVATE)
    493  1.41.6.5     joerg 		*region_arg = NULL;
    494  1.41.6.5     joerg 	else
    495  1.41.6.5     joerg 		*region_arg = arg;
    496       1.1   thorpej 
    497  1.41.6.5     joerg 	return AE_OK;
    498  1.41.6.5     joerg }
    499       1.1   thorpej 
    500  1.41.6.5     joerg static void
    501  1.41.6.5     joerg acpiec_lock(device_t dv)
    502  1.41.6.5     joerg {
    503  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    504  1.41.6.5     joerg 	ACPI_STATUS rv;
    505       1.1   thorpej 
    506  1.41.6.5     joerg 	mutex_enter(&sc->sc_access_mtx);
    507       1.1   thorpej 
    508  1.41.6.5     joerg 	if (sc->sc_need_global_lock) {
    509  1.41.6.5     joerg 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
    510  1.41.6.5     joerg 		if (rv != AE_OK) {
    511  1.41.6.5     joerg 			aprint_error("%s: failed to acquire global lock (continuing)\n",
    512  1.41.6.5     joerg 			    device_xname(dv));
    513  1.41.6.5     joerg 			return;
    514  1.41.6.5     joerg 		}
    515  1.41.6.5     joerg 	}
    516  1.41.6.5     joerg }
    517       1.1   thorpej 
    518  1.41.6.5     joerg static void
    519  1.41.6.5     joerg acpiec_unlock(device_t dv)
    520  1.41.6.5     joerg {
    521  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    522  1.41.6.5     joerg 	ACPI_STATUS rv;
    523  1.41.6.5     joerg 
    524  1.41.6.5     joerg 	if (sc->sc_need_global_lock) {
    525  1.41.6.5     joerg 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
    526  1.41.6.5     joerg 		if (rv != AE_OK) {
    527  1.41.6.5     joerg 			aprint_error("%s: failed to release global lock (continuing)\n",
    528  1.41.6.5     joerg 			    device_xname(dv));
    529  1.41.6.5     joerg 		}
    530       1.1   thorpej 	}
    531  1.41.6.5     joerg 	mutex_exit(&sc->sc_access_mtx);
    532  1.41.6.5     joerg }
    533       1.1   thorpej 
    534  1.41.6.5     joerg static ACPI_STATUS
    535  1.41.6.5     joerg acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
    536  1.41.6.5     joerg {
    537  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    538  1.41.6.7     joerg 	int i, timeouts = 0;
    539       1.1   thorpej 
    540  1.41.6.5     joerg 	acpiec_lock(dv);
    541  1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    542       1.1   thorpej 
    543  1.41.6.5     joerg retry:
    544  1.41.6.5     joerg 	sc->sc_cur_addr = addr;
    545  1.41.6.5     joerg 	sc->sc_state = EC_STATE_READ;
    546  1.41.6.5     joerg 
    547  1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    548  1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    549  1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    550  1.41.6.7     joerg 			goto done;
    551  1.41.6.7     joerg 		delay(1);
    552  1.41.6.7     joerg 	}
    553  1.41.6.7     joerg 
    554  1.41.6.5     joerg 	if (cold || acpiec_cold) {
    555  1.41.6.7     joerg 		while (sc->sc_state != EC_STATE_FREE) {
    556  1.41.6.7     joerg 			delay(1);
    557  1.41.6.5     joerg 			acpiec_gpe_state_maschine(dv);
    558  1.41.6.7     joerg 		}
    559  1.41.6.5     joerg 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
    560  1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    561  1.41.6.5     joerg 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    562  1.41.6.5     joerg 		mutex_enter(&sc->sc_mtx);
    563  1.41.6.5     joerg 		if (++timeouts < 5)
    564  1.41.6.5     joerg 			goto retry;
    565  1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    566  1.41.6.5     joerg 		acpiec_unlock(dv);
    567  1.41.6.5     joerg 		aprint_error_dev(dv, "command takes over 5sec...\n");
    568  1.41.6.5     joerg 		return AE_ERROR;
    569  1.41.6.5     joerg 	}
    570  1.41.6.5     joerg 
    571  1.41.6.7     joerg done:
    572  1.41.6.7     joerg 	*val = sc->sc_cur_val;
    573  1.41.6.5     joerg 
    574  1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    575  1.41.6.5     joerg 	acpiec_unlock(dv);
    576  1.41.6.5     joerg 	return AE_OK;
    577       1.1   thorpej }
    578       1.1   thorpej 
    579       1.1   thorpej static ACPI_STATUS
    580  1.41.6.5     joerg acpiec_write(device_t dv, uint8_t addr, uint8_t val)
    581       1.1   thorpej {
    582  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    583  1.41.6.7     joerg 	int i, timeouts = 0;
    584       1.1   thorpej 
    585  1.41.6.5     joerg 	acpiec_lock(dv);
    586  1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    587       1.1   thorpej 
    588  1.41.6.5     joerg retry:
    589  1.41.6.5     joerg 	sc->sc_cur_addr = addr;
    590  1.41.6.5     joerg 	sc->sc_cur_val = val;
    591  1.41.6.5     joerg 	sc->sc_state = EC_STATE_WRITE;
    592  1.41.6.5     joerg 
    593  1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    594  1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    595  1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    596  1.41.6.7     joerg 			goto done;
    597  1.41.6.7     joerg 		delay(1);
    598  1.41.6.7     joerg 	}
    599  1.41.6.7     joerg 
    600  1.41.6.5     joerg 	if (cold || acpiec_cold) {
    601  1.41.6.7     joerg 		while (sc->sc_state != EC_STATE_FREE) {
    602  1.41.6.7     joerg 			delay(1);
    603  1.41.6.5     joerg 			acpiec_gpe_state_maschine(dv);
    604  1.41.6.7     joerg 		}
    605  1.41.6.5     joerg 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
    606  1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    607  1.41.6.5     joerg 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    608  1.41.6.5     joerg 		mutex_enter(&sc->sc_mtx);
    609  1.41.6.5     joerg 		if (++timeouts < 5)
    610  1.41.6.5     joerg 			goto retry;
    611  1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    612  1.41.6.5     joerg 		acpiec_unlock(dv);
    613  1.41.6.5     joerg 		aprint_error_dev(dv, "command takes over 5sec...\n");
    614  1.41.6.5     joerg 		return AE_ERROR;
    615  1.41.6.4     joerg 	}
    616  1.41.6.4     joerg 
    617  1.41.6.7     joerg done:
    618  1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    619  1.41.6.5     joerg 	acpiec_unlock(dv);
    620  1.41.6.5     joerg 	return AE_OK;
    621       1.1   thorpej }
    622       1.1   thorpej 
    623       1.1   thorpej static ACPI_STATUS
    624  1.41.6.5     joerg acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
    625  1.41.6.5     joerg     UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
    626       1.1   thorpej {
    627  1.41.6.5     joerg 	device_t dv;
    628  1.41.6.5     joerg 	struct acpiec_softc *sc;
    629      1.25     kochi 	ACPI_STATUS rv;
    630  1.41.6.5     joerg 	uint8_t addr, reg;
    631  1.41.6.5     joerg 	unsigned int i;
    632  1.41.6.5     joerg 
    633  1.41.6.5     joerg 	if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
    634  1.41.6.5     joerg 	    paddr + width / 8 > 0xff)
    635  1.41.6.5     joerg 		return AE_BAD_PARAMETER;
    636       1.1   thorpej 
    637  1.41.6.5     joerg 	addr = paddr;
    638  1.41.6.5     joerg 	dv = arg;
    639  1.41.6.5     joerg 	sc = device_private(dv);
    640  1.41.6.5     joerg 
    641  1.41.6.5     joerg 	rv = AE_OK;
    642  1.41.6.5     joerg 
    643  1.41.6.5     joerg 	switch (func) {
    644  1.41.6.5     joerg 	case ACPI_READ:
    645  1.41.6.8     joerg 		*value = 0;
    646  1.41.6.5     joerg 		for (i = 0; i < width; i += 8, ++addr) {
    647  1.41.6.5     joerg 			rv = acpiec_read(dv, addr, &reg);
    648  1.41.6.5     joerg 			if (rv != AE_OK)
    649  1.41.6.5     joerg 				break;
    650  1.41.6.5     joerg 			*value |= (ACPI_INTEGER)reg << i;
    651  1.41.6.5     joerg 		}
    652  1.41.6.5     joerg 		break;
    653  1.41.6.5     joerg 	case ACPI_WRITE:
    654  1.41.6.5     joerg 		for (i = 0; i < width; i += 8, ++addr) {
    655  1.41.6.5     joerg 			reg = (*value >>i) & 0xff;
    656  1.41.6.5     joerg 			rv = acpiec_write(dv, addr, reg);
    657  1.41.6.5     joerg 			if (rv != AE_OK)
    658  1.41.6.5     joerg 				break;
    659  1.41.6.5     joerg 		}
    660  1.41.6.5     joerg 		break;
    661  1.41.6.5     joerg 	default:
    662  1.41.6.5     joerg 		aprint_error("%s: invalid Address Space function called: %x\n",
    663  1.41.6.5     joerg 		    device_xname(dv), (unsigned int)func);
    664  1.41.6.5     joerg 		return AE_BAD_PARAMETER;
    665  1.41.6.5     joerg 	}
    666       1.1   thorpej 
    667      1.25     kochi 	return rv;
    668      1.24     kochi }
    669       1.1   thorpej 
    670  1.41.6.5     joerg static void
    671  1.41.6.5     joerg acpiec_gpe_query(void *arg)
    672       1.1   thorpej {
    673  1.41.6.5     joerg 	device_t dv = arg;
    674  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    675  1.41.6.5     joerg 	uint8_t reg;
    676  1.41.6.5     joerg 	char qxx[5];
    677      1.25     kochi 	ACPI_STATUS rv;
    678  1.41.6.7     joerg 	int i;
    679       1.1   thorpej 
    680  1.41.6.5     joerg loop:
    681  1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    682       1.1   thorpej 
    683  1.41.6.5     joerg 	if (sc->sc_got_sci == false)
    684  1.41.6.5     joerg 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
    685  1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    686       1.1   thorpej 
    687  1.41.6.5     joerg 	acpiec_lock(dv);
    688  1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    689       1.1   thorpej 
    690  1.41.6.7     joerg 	/* The Query command can always be issued, so be defensive here. */
    691  1.41.6.7     joerg 	sc->sc_got_sci = false;
    692  1.41.6.5     joerg 	sc->sc_state = EC_STATE_QUERY;
    693       1.1   thorpej 
    694  1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    695  1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    696  1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    697  1.41.6.7     joerg 			goto done;
    698  1.41.6.7     joerg 		delay(1);
    699  1.41.6.7     joerg 	}
    700  1.41.6.7     joerg 
    701  1.41.6.5     joerg 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
    702       1.1   thorpej 
    703  1.41.6.7     joerg done:
    704  1.41.6.7     joerg 	reg = sc->sc_cur_val;
    705       1.1   thorpej 
    706  1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    707  1.41.6.5     joerg 	acpiec_unlock(dv);
    708  1.41.6.5     joerg 
    709  1.41.6.5     joerg 	if (reg == 0)
    710  1.41.6.5     joerg 		goto loop; /* Spurious query result */
    711  1.41.6.7     joerg 
    712  1.41.6.5     joerg 	/*
    713  1.41.6.5     joerg 	 * Evaluate _Qxx to respond to the controller.
    714  1.41.6.5     joerg 	 */
    715  1.41.6.5     joerg 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
    716  1.41.6.5     joerg 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
    717  1.41.6.5     joerg 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
    718  1.41.6.5     joerg 		aprint_error("%s: GPE query method %s failed: %s",
    719  1.41.6.5     joerg 		    device_xname(dv), qxx, AcpiFormatException(rv));
    720  1.41.6.5     joerg 	}
    721  1.41.6.5     joerg 
    722  1.41.6.5     joerg 	goto loop;
    723      1.24     kochi }
    724       1.1   thorpej 
    725  1.41.6.5     joerg static void
    726  1.41.6.5     joerg acpiec_gpe_state_maschine(device_t dv)
    727       1.1   thorpej {
    728  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    729  1.41.6.5     joerg 	uint8_t reg;
    730       1.1   thorpej 
    731  1.41.6.5     joerg 	reg = acpiec_read_status(sc);
    732       1.1   thorpej 
    733  1.41.6.5     joerg 	if (reg & EC_STATUS_SCI)
    734  1.41.6.5     joerg 		sc->sc_got_sci = true;
    735       1.1   thorpej 
    736  1.41.6.5     joerg 	switch (sc->sc_state) {
    737  1.41.6.5     joerg 	case EC_STATE_QUERY:
    738  1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    739  1.41.6.7     joerg 			break; /* Nothing of interest here. */
    740  1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_QUERY);
    741  1.41.6.7     joerg 		sc->sc_state = EC_STATE_QUERY_VAL;
    742  1.41.6.7     joerg 		break;
    743  1.41.6.7     joerg 
    744  1.41.6.7     joerg 	case EC_STATE_QUERY_VAL:
    745  1.41.6.5     joerg 		if ((reg & EC_STATUS_OBF) == 0)
    746  1.41.6.5     joerg 			break; /* Nothing of interest here. */
    747       1.1   thorpej 
    748  1.41.6.7     joerg 		sc->sc_cur_val = acpiec_read_data(sc);
    749  1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    750  1.41.6.7     joerg 
    751  1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    752  1.41.6.5     joerg 		break;
    753       1.1   thorpej 
    754  1.41.6.5     joerg 	case EC_STATE_READ:
    755  1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    756  1.41.6.5     joerg 			break; /* Nothing of interest here. */
    757       1.1   thorpej 
    758  1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_READ);
    759  1.41.6.7     joerg 		sc->sc_state = EC_STATE_READ_ADDR;
    760  1.41.6.7     joerg 		break;
    761  1.41.6.7     joerg 
    762  1.41.6.7     joerg 	case EC_STATE_READ_ADDR:
    763  1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    764  1.41.6.7     joerg 			break; /* Nothing of interest here. */
    765  1.41.6.7     joerg 
    766  1.41.6.5     joerg 		acpiec_write_data(sc, sc->sc_cur_addr);
    767  1.41.6.5     joerg 		sc->sc_state = EC_STATE_READ_VAL;
    768  1.41.6.5     joerg 		break;
    769       1.1   thorpej 
    770  1.41.6.5     joerg 	case EC_STATE_READ_VAL:
    771  1.41.6.5     joerg 		if ((reg & EC_STATUS_OBF) == 0)
    772  1.41.6.5     joerg 			break; /* Nothing of interest here. */
    773  1.41.6.7     joerg 		sc->sc_cur_val = acpiec_read_data(sc);
    774  1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    775  1.41.6.7     joerg 
    776  1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    777  1.41.6.5     joerg 		break;
    778  1.41.6.3     joerg 
    779  1.41.6.5     joerg 	case EC_STATE_WRITE:
    780  1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    781  1.41.6.5     joerg 			break; /* Nothing of interest here. */
    782  1.41.6.7     joerg 
    783  1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_WRITE);
    784  1.41.6.7     joerg 		sc->sc_state = EC_STATE_WRITE_ADDR;
    785  1.41.6.7     joerg 		break;
    786  1.41.6.7     joerg 
    787  1.41.6.7     joerg 	case EC_STATE_WRITE_ADDR:
    788  1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    789  1.41.6.7     joerg 			break; /* Nothing of interest here. */
    790  1.41.6.5     joerg 		acpiec_write_data(sc, sc->sc_cur_addr);
    791  1.41.6.5     joerg 		sc->sc_state = EC_STATE_WRITE_VAL;
    792  1.41.6.5     joerg 		break;
    793  1.41.6.3     joerg 
    794  1.41.6.5     joerg 	case EC_STATE_WRITE_VAL:
    795  1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    796  1.41.6.5     joerg 			break; /* Nothing of interest here. */
    797  1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    798  1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    799  1.41.6.3     joerg 
    800  1.41.6.7     joerg 		acpiec_write_data(sc, sc->sc_cur_val);
    801  1.41.6.3     joerg 		break;
    802  1.41.6.3     joerg 
    803  1.41.6.5     joerg 	case EC_STATE_FREE:
    804  1.41.6.5     joerg 		if (sc->sc_got_sci)
    805  1.41.6.5     joerg 			cv_signal(&sc->sc_cv_sci);
    806  1.41.6.3     joerg 		break;
    807  1.41.6.5     joerg 	default:
    808  1.41.6.5     joerg 		panic("invalid state");
    809  1.41.6.3     joerg 	}
    810  1.41.6.5     joerg }
    811  1.41.6.3     joerg 
    812  1.41.6.5     joerg static UINT32
    813  1.41.6.5     joerg acpiec_gpe_handler(void *arg)
    814  1.41.6.5     joerg {
    815  1.41.6.5     joerg 	device_t dv = arg;
    816  1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    817  1.41.6.3     joerg 
    818  1.41.6.5     joerg 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    819  1.41.6.5     joerg 
    820  1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    821  1.41.6.5     joerg 	acpiec_gpe_state_maschine(dv);
    822  1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    823  1.41.6.5     joerg 
    824  1.41.6.5     joerg 	return 0;
    825  1.41.6.3     joerg }
    826