Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.41.6.12
      1  1.41.6.12     joerg /*	$NetBSD: acpi_ec.c,v 1.41.6.12 2007/11/06 14:27:12 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.12     joerg __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.41.6.12 2007/11/06 14:27:12 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.41.6.11     joerg #include <sys/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.12     joerg static bool acpiec_resume(device_t);
    118  1.41.6.12     joerg static bool acpiec_suspend(device_t);
    119       1.17   mycroft 
    120   1.41.6.5     joerg static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
    121   1.41.6.5     joerg     ACPI_HANDLE *, uint8_t *);
    122       1.20      yamt 
    123   1.41.6.5     joerg static void acpiec_gpe_query(void *);
    124   1.41.6.5     joerg static UINT32 acpiec_gpe_handler(void *);
    125   1.41.6.5     joerg static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
    126   1.41.6.5     joerg static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
    127   1.41.6.5     joerg     UINT32, ACPI_INTEGER *, void *, void *);
    128       1.20      yamt 
    129   1.41.6.5     joerg static void acpiec_gpe_state_maschine(device_t);
    130       1.20      yamt 
    131   1.41.6.5     joerg CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
    132       1.20      yamt     acpiec_match, acpiec_attach, NULL, NULL);
    133       1.20      yamt 
    134   1.41.6.5     joerg CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
    135   1.41.6.5     joerg     acpiecdt_match, acpiecdt_attach, NULL, NULL);
    136       1.23     kochi 
    137   1.41.6.5     joerg static device_t ec_singleton = NULL;
    138   1.41.6.5     joerg static bool acpiec_cold = false;
    139   1.41.6.5     joerg 
    140   1.41.6.5     joerg static bool
    141   1.41.6.5     joerg acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
    142   1.41.6.5     joerg     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
    143        1.9  tshiozak {
    144   1.41.6.6  jmcneill 	ACPI_TABLE_ECDT *ecdt;
    145   1.41.6.5     joerg 	ACPI_STATUS rv;
    146   1.41.6.5     joerg 
    147   1.41.6.6  jmcneill 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
    148   1.41.6.6  jmcneill 	if (ACPI_FAILURE(rv))
    149   1.41.6.5     joerg 		return false;
    150   1.41.6.5     joerg 
    151   1.41.6.6  jmcneill 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
    152   1.41.6.5     joerg 		aprint_error_dev(parent,
    153   1.41.6.5     joerg 		    "ECDT register width invalid (%d/%d)\n",
    154   1.41.6.6  jmcneill 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
    155   1.41.6.5     joerg 		return false;
    156   1.41.6.5     joerg 	}
    157   1.41.6.5     joerg 
    158   1.41.6.6  jmcneill 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
    159   1.41.6.6  jmcneill 	if (ACPI_FAILURE(rv)) {
    160   1.41.6.5     joerg 		aprint_error_dev(parent,
    161   1.41.6.5     joerg 		    "failed to look up EC object %s: %s\n",
    162   1.41.6.6  jmcneill 		    ecdt->Id, AcpiFormatException(rv));
    163   1.41.6.5     joerg 		return false;
    164   1.41.6.5     joerg 	}
    165   1.41.6.5     joerg 
    166   1.41.6.6  jmcneill 	*cmd_reg = ecdt->Control.Address;
    167   1.41.6.6  jmcneill 	*data_reg = ecdt->Data.Address;
    168   1.41.6.6  jmcneill 	*gpebit = ecdt->Gpe;
    169       1.24     kochi 
    170   1.41.6.5     joerg 	return true;
    171        1.9  tshiozak }
    172        1.4   thorpej 
    173   1.41.6.5     joerg static int
    174   1.41.6.5     joerg acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
    175        1.1   thorpej {
    176   1.41.6.5     joerg 	ACPI_HANDLE ec_handle;
    177   1.41.6.5     joerg 	bus_addr_t cmd_reg, data_reg;
    178   1.41.6.5     joerg 	uint8_t gpebit;
    179       1.20      yamt 
    180   1.41.6.5     joerg 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    181   1.41.6.5     joerg 		return 1;
    182   1.41.6.5     joerg 	else
    183   1.41.6.5     joerg 		return 0;
    184        1.1   thorpej }
    185        1.1   thorpej 
    186   1.41.6.5     joerg static void
    187   1.41.6.5     joerg acpiecdt_attach(device_t parent, device_t self, void *aux)
    188        1.1   thorpej {
    189   1.41.6.5     joerg 	ACPI_HANDLE ec_handle;
    190   1.41.6.5     joerg 	bus_addr_t cmd_reg, data_reg;
    191   1.41.6.5     joerg 	uint8_t gpebit;
    192       1.20      yamt 
    193   1.41.6.5     joerg 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    194   1.41.6.5     joerg 		panic("ECDT disappeared");
    195   1.41.6.5     joerg 
    196   1.41.6.5     joerg 	aprint_naive(": ACPI Embedded Controller via ECDT\n");
    197   1.41.6.5     joerg 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
    198   1.41.6.5     joerg 
    199   1.41.6.5     joerg 	acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
    200   1.41.6.5     joerg 	    NULL, gpebit);
    201        1.1   thorpej }
    202        1.1   thorpej 
    203       1.31     kochi static int
    204   1.41.6.5     joerg acpiec_match(device_t parent, struct cfdata *match, void *aux)
    205        1.1   thorpej {
    206        1.1   thorpej 	struct acpi_attach_args *aa = aux;
    207        1.1   thorpej 
    208        1.1   thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    209       1.25     kochi 		return 0;
    210        1.1   thorpej 
    211       1.25     kochi 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
    212        1.1   thorpej }
    213        1.1   thorpej 
    214   1.41.6.5     joerg static void
    215   1.41.6.5     joerg acpiec_attach(device_t parent, device_t self, void *aux)
    216       1.17   mycroft {
    217   1.41.6.5     joerg 	struct acpi_attach_args *aa = aux;
    218   1.41.6.5     joerg 	struct acpi_resources ec_res;
    219   1.41.6.5     joerg 	struct acpi_io *io0, *io1;
    220   1.41.6.5     joerg 	ACPI_HANDLE gpe_handle;
    221   1.41.6.5     joerg 	uint8_t gpebit;
    222       1.23     kochi 	ACPI_STATUS rv;
    223       1.17   mycroft 
    224   1.41.6.5     joerg 	if (ec_singleton != NULL) {
    225   1.41.6.5     joerg 		aprint_naive(": ACPI Embedded Controller (disabled)\n");
    226   1.41.6.5     joerg 		aprint_normal(": ACPI Embedded Controller (disabled)\n");
    227  1.41.6.12     joerg 		if (!pnp_device_register(self, NULL, NULL))
    228  1.41.6.12     joerg 			aprint_error_dev(self, "couldn't establish power handler\n");
    229       1.23     kochi 		return;
    230       1.23     kochi 	}
    231       1.23     kochi 
    232   1.41.6.5     joerg 	aprint_naive(": ACPI Embedded Controller\n");
    233   1.41.6.5     joerg 	aprint_normal(": ACPI Embedded Controller\n");
    234       1.23     kochi 
    235   1.41.6.5     joerg 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
    236   1.41.6.5     joerg 				      &gpe_handle, &gpebit))
    237   1.41.6.5     joerg 		return;
    238       1.23     kochi 
    239   1.41.6.5     joerg 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    240   1.41.6.5     joerg 	    &ec_res, &acpi_resource_parse_ops_default);
    241   1.41.6.5     joerg 	if (rv != AE_OK) {
    242   1.41.6.5     joerg 		aprint_error_dev(self, "resource parsing failed: %s\n",
    243       1.23     kochi 		    AcpiFormatException(rv));
    244   1.41.6.5     joerg 		return;
    245       1.17   mycroft 	}
    246       1.23     kochi 
    247   1.41.6.5     joerg 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
    248   1.41.6.5     joerg 		aprint_error_dev(self, "no data register resource\n");
    249   1.41.6.5     joerg 		goto free_res;
    250   1.41.6.5     joerg 	}
    251   1.41.6.5     joerg 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
    252   1.41.6.5     joerg 		aprint_error_dev(self, "no CSR register resource\n");
    253   1.41.6.5     joerg 		goto free_res;
    254   1.41.6.5     joerg 	}
    255       1.23     kochi 
    256   1.41.6.5     joerg 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
    257   1.41.6.5     joerg 	    io1->ar_base, io0->ar_base, gpe_handle, gpebit);
    258       1.23     kochi 
    259   1.41.6.5     joerg free_res:
    260   1.41.6.5     joerg 	acpi_resource_cleanup(&ec_res);
    261       1.17   mycroft }
    262       1.17   mycroft 
    263       1.31     kochi static void
    264   1.41.6.5     joerg acpiec_common_attach(device_t parent, device_t self,
    265   1.41.6.5     joerg     ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
    266   1.41.6.5     joerg     ACPI_HANDLE gpe_handle, uint8_t gpebit)
    267        1.1   thorpej {
    268   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(self);
    269        1.1   thorpej 	ACPI_STATUS rv;
    270   1.41.6.5     joerg 	ACPI_INTEGER val;
    271        1.1   thorpej 
    272   1.41.6.5     joerg 	sc->sc_ech = ec_handle;
    273   1.41.6.5     joerg 	sc->sc_gpeh = gpe_handle;
    274   1.41.6.5     joerg 	sc->sc_gpebit = gpebit;
    275   1.41.6.5     joerg 
    276   1.41.6.5     joerg 	sc->sc_state = EC_STATE_FREE;
    277   1.41.6.5     joerg 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
    278   1.41.6.5     joerg 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
    279   1.41.6.5     joerg 	cv_init(&sc->sc_cv, "eccv");
    280   1.41.6.5     joerg 	cv_init(&sc->sc_cv_sci, "ecsci");
    281   1.41.6.5     joerg 
    282   1.41.6.5     joerg 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
    283   1.41.6.5     joerg 	    &sc->sc_data_sh) != 0) {
    284   1.41.6.5     joerg 		aprint_error_dev(self, "unable to map data register\n");
    285   1.41.6.5     joerg 		return;
    286   1.41.6.5     joerg 	}
    287        1.1   thorpej 
    288   1.41.6.5     joerg 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
    289   1.41.6.5     joerg 		aprint_error_dev(self, "unable to map CSR register\n");
    290   1.41.6.5     joerg 		goto post_data_map;
    291   1.41.6.5     joerg 	}
    292       1.17   mycroft 
    293   1.41.6.5     joerg 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
    294   1.41.6.5     joerg 	if (rv == AE_OK) {
    295   1.41.6.5     joerg 		sc->sc_need_global_lock = val != 0;
    296   1.41.6.5     joerg 	} else if (rv != AE_NOT_FOUND) {
    297   1.41.6.5     joerg 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
    298   1.41.6.5     joerg 		    AcpiFormatException(rv));
    299   1.41.6.5     joerg 		goto post_csr_map;
    300   1.41.6.5     joerg 	} else {
    301   1.41.6.5     joerg 		sc->sc_need_global_lock = false;
    302   1.41.6.5     joerg 	}
    303   1.41.6.5     joerg 	if (sc->sc_need_global_lock)
    304   1.41.6.5     joerg 		aprint_normal_dev(self, "using global ACPI lock\n");
    305        1.1   thorpej 
    306   1.41.6.5     joerg 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
    307   1.41.6.5     joerg 	    acpiec_space_handler, acpiec_space_setup, self);
    308   1.41.6.5     joerg 	if (rv != AE_OK) {
    309   1.41.6.5     joerg 		aprint_error_dev(self,
    310   1.41.6.5     joerg 		    "unable to install address space handler: %s\n",
    311   1.41.6.5     joerg 		    AcpiFormatException(rv));
    312   1.41.6.5     joerg 		goto post_csr_map;
    313   1.41.6.5     joerg 	}
    314        1.1   thorpej 
    315   1.41.6.5     joerg 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    316   1.41.6.5     joerg 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
    317   1.41.6.5     joerg 	if (rv != AE_OK) {
    318   1.41.6.5     joerg 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
    319   1.41.6.5     joerg 		    AcpiFormatException(rv));
    320   1.41.6.5     joerg 		goto post_csr_map;
    321   1.41.6.5     joerg 	}
    322       1.23     kochi 
    323   1.41.6.5     joerg 	rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
    324   1.41.6.5     joerg 	if (rv != AE_OK) {
    325   1.41.6.5     joerg 		aprint_error_dev(self, "unable to set GPE type: %s\n",
    326   1.41.6.5     joerg 		    AcpiFormatException(rv));
    327   1.41.6.5     joerg 		goto post_csr_map;
    328        1.1   thorpej 	}
    329        1.1   thorpej 
    330   1.41.6.5     joerg 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    331   1.41.6.5     joerg 	if (rv != AE_OK) {
    332   1.41.6.5     joerg 		aprint_error_dev(self, "unable to enable GPE: %s\n",
    333   1.41.6.5     joerg 		    AcpiFormatException(rv));
    334   1.41.6.5     joerg 		goto post_csr_map;
    335   1.41.6.5     joerg 	}
    336       1.23     kochi 
    337   1.41.6.5     joerg 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
    338   1.41.6.5     joerg 		           self, NULL, "acpiec sci thread")) {
    339   1.41.6.5     joerg 		aprint_error_dev(self, "unable to create query kthread\n");
    340   1.41.6.5     joerg 		goto post_csr_map;
    341       1.17   mycroft 	}
    342       1.17   mycroft 
    343   1.41.6.5     joerg 	ec_singleton = self;
    344        1.1   thorpej 
    345  1.41.6.12     joerg 	if (!pnp_device_register(self, acpiec_suspend, acpiec_resume))
    346  1.41.6.12     joerg 		aprint_error_dev(self, "couldn't establish power handler\n");
    347        1.1   thorpej 
    348   1.41.6.5     joerg 	return;
    349        1.1   thorpej 
    350   1.41.6.5     joerg post_csr_map:
    351   1.41.6.5     joerg 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    352   1.41.6.5     joerg 	    acpiec_gpe_handler);
    353   1.41.6.5     joerg 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
    354   1.41.6.5     joerg 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
    355   1.41.6.5     joerg 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
    356   1.41.6.5     joerg post_data_map:
    357   1.41.6.5     joerg 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
    358   1.41.6.5     joerg }
    359   1.41.6.5     joerg 
    360  1.41.6.12     joerg static bool
    361  1.41.6.12     joerg acpiec_suspend(device_t dv)
    362   1.41.6.5     joerg {
    363  1.41.6.12     joerg 	acpiec_cold = true;
    364   1.41.6.5     joerg 
    365  1.41.6.12     joerg 	return true;
    366  1.41.6.12     joerg }
    367        1.1   thorpej 
    368  1.41.6.12     joerg static bool
    369  1.41.6.12     joerg acpiec_resume(device_t dv)
    370  1.41.6.12     joerg {
    371  1.41.6.12     joerg 	acpiec_cold = false;
    372  1.41.6.12     joerg 
    373  1.41.6.12     joerg 	return true;
    374        1.1   thorpej }
    375        1.1   thorpej 
    376   1.41.6.5     joerg static bool
    377   1.41.6.5     joerg acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
    378   1.41.6.5     joerg     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
    379        1.1   thorpej {
    380   1.41.6.5     joerg 	ACPI_BUFFER buf;
    381   1.41.6.5     joerg 	ACPI_OBJECT *p, *c;
    382       1.25     kochi 	ACPI_STATUS rv;
    383        1.1   thorpej 
    384   1.41.6.5     joerg 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
    385   1.41.6.5     joerg 	if (rv != AE_OK) {
    386   1.41.6.5     joerg 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
    387   1.41.6.5     joerg 		    AcpiFormatException(rv));
    388   1.41.6.5     joerg 		return false;
    389   1.41.6.5     joerg 	}
    390   1.41.6.5     joerg 
    391   1.41.6.5     joerg 	p = buf.Pointer;
    392   1.41.6.5     joerg 
    393   1.41.6.5     joerg 	if (p->Type == ACPI_TYPE_INTEGER) {
    394   1.41.6.5     joerg 		*gpe_handle = NULL;
    395   1.41.6.5     joerg 		*gpebit = p->Integer.Value;
    396   1.41.6.5     joerg 		AcpiOsFree(p);
    397   1.41.6.5     joerg 		return true;
    398   1.41.6.5     joerg 	}
    399   1.41.6.5     joerg 
    400   1.41.6.5     joerg 	if (p->Type != ACPI_TYPE_PACKAGE) {
    401   1.41.6.5     joerg 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
    402   1.41.6.5     joerg 		AcpiOsFree(p);
    403   1.41.6.5     joerg 		return false;
    404   1.41.6.5     joerg 	}
    405   1.41.6.5     joerg 
    406   1.41.6.5     joerg 	if (p->Package.Count != 2) {
    407   1.41.6.5     joerg 		aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
    408   1.41.6.5     joerg 		AcpiOsFree(p);
    409   1.41.6.5     joerg 		return false;
    410        1.1   thorpej 	}
    411       1.33     kochi 
    412   1.41.6.5     joerg 	c = &p->Package.Elements[0];
    413   1.41.6.5     joerg 	switch (c->Type) {
    414   1.41.6.5     joerg 	case ACPI_TYPE_LOCAL_REFERENCE:
    415   1.41.6.5     joerg 	case ACPI_TYPE_ANY:
    416   1.41.6.5     joerg 		*gpe_handle = c->Reference.Handle;
    417   1.41.6.5     joerg 		break;
    418   1.41.6.5     joerg 	case ACPI_TYPE_STRING:
    419   1.41.6.5     joerg 		/* XXX should be using real scope here */
    420   1.41.6.5     joerg 		rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
    421   1.41.6.5     joerg 		if (rv != AE_OK) {
    422   1.41.6.5     joerg 			aprint_error_dev(self,
    423   1.41.6.5     joerg 			    "_GPE device reference unresolvable\n");
    424   1.41.6.5     joerg 			AcpiOsFree(p);
    425   1.41.6.5     joerg 			return false;
    426   1.41.6.5     joerg 		}
    427   1.41.6.5     joerg 		break;
    428   1.41.6.5     joerg 	default:
    429   1.41.6.5     joerg 		aprint_error_dev(self, "_GPE device reference incorrect\n");
    430   1.41.6.5     joerg 		AcpiOsFree(p);
    431   1.41.6.5     joerg 		return false;
    432   1.41.6.5     joerg 	}
    433   1.41.6.5     joerg 	c = &p->Package.Elements[1];
    434   1.41.6.5     joerg 	if (c->Type != ACPI_TYPE_INTEGER) {
    435   1.41.6.5     joerg 		aprint_error_dev(self,
    436   1.41.6.5     joerg 		    "_GPE package needs integer as 2nd field\n");
    437   1.41.6.5     joerg 		AcpiOsFree(p);
    438   1.41.6.5     joerg 		return false;
    439   1.41.6.5     joerg 	}
    440   1.41.6.5     joerg 	*gpebit = c->Integer.Value;
    441   1.41.6.5     joerg 	AcpiOsFree(p);
    442   1.41.6.5     joerg 	return true;
    443        1.1   thorpej }
    444        1.1   thorpej 
    445   1.41.6.5     joerg static uint8_t
    446   1.41.6.5     joerg acpiec_read_data(struct acpiec_softc *sc)
    447        1.1   thorpej {
    448   1.41.6.5     joerg 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
    449   1.41.6.5     joerg }
    450        1.1   thorpej 
    451   1.41.6.5     joerg static void
    452   1.41.6.5     joerg acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
    453   1.41.6.5     joerg {
    454   1.41.6.5     joerg 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
    455   1.41.6.5     joerg }
    456        1.1   thorpej 
    457   1.41.6.5     joerg static uint8_t
    458   1.41.6.5     joerg acpiec_read_status(struct acpiec_softc *sc)
    459   1.41.6.5     joerg {
    460   1.41.6.5     joerg 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
    461   1.41.6.5     joerg }
    462        1.1   thorpej 
    463   1.41.6.5     joerg static void
    464   1.41.6.5     joerg acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
    465   1.41.6.5     joerg {
    466   1.41.6.5     joerg 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
    467        1.1   thorpej }
    468        1.1   thorpej 
    469        1.1   thorpej static ACPI_STATUS
    470   1.41.6.5     joerg acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
    471   1.41.6.5     joerg     void **region_arg)
    472        1.1   thorpej {
    473   1.41.6.5     joerg 	if (func == ACPI_REGION_DEACTIVATE)
    474   1.41.6.5     joerg 		*region_arg = NULL;
    475   1.41.6.5     joerg 	else
    476   1.41.6.5     joerg 		*region_arg = arg;
    477        1.1   thorpej 
    478   1.41.6.5     joerg 	return AE_OK;
    479   1.41.6.5     joerg }
    480        1.1   thorpej 
    481   1.41.6.5     joerg static void
    482   1.41.6.5     joerg acpiec_lock(device_t dv)
    483   1.41.6.5     joerg {
    484   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    485   1.41.6.5     joerg 	ACPI_STATUS rv;
    486        1.1   thorpej 
    487   1.41.6.5     joerg 	mutex_enter(&sc->sc_access_mtx);
    488        1.1   thorpej 
    489   1.41.6.5     joerg 	if (sc->sc_need_global_lock) {
    490   1.41.6.5     joerg 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
    491   1.41.6.5     joerg 		if (rv != AE_OK) {
    492  1.41.6.10     joerg 			aprint_error_dev(dv, "failed to acquire global lock: %s\n",
    493  1.41.6.10     joerg 			    AcpiFormatException(rv));
    494   1.41.6.5     joerg 			return;
    495   1.41.6.5     joerg 		}
    496   1.41.6.5     joerg 	}
    497   1.41.6.5     joerg }
    498        1.1   thorpej 
    499   1.41.6.5     joerg static void
    500   1.41.6.5     joerg acpiec_unlock(device_t dv)
    501   1.41.6.5     joerg {
    502   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    503   1.41.6.5     joerg 	ACPI_STATUS rv;
    504   1.41.6.5     joerg 
    505   1.41.6.5     joerg 	if (sc->sc_need_global_lock) {
    506   1.41.6.5     joerg 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
    507   1.41.6.5     joerg 		if (rv != AE_OK) {
    508  1.41.6.10     joerg 			aprint_error_dev(dv, "failed to release global lock: %s\n",
    509  1.41.6.10     joerg 			    AcpiFormatException(rv));
    510   1.41.6.5     joerg 		}
    511        1.1   thorpej 	}
    512   1.41.6.5     joerg 	mutex_exit(&sc->sc_access_mtx);
    513   1.41.6.5     joerg }
    514        1.1   thorpej 
    515   1.41.6.5     joerg static ACPI_STATUS
    516   1.41.6.5     joerg acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
    517   1.41.6.5     joerg {
    518   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    519   1.41.6.7     joerg 	int i, timeouts = 0;
    520        1.1   thorpej 
    521   1.41.6.5     joerg 	acpiec_lock(dv);
    522   1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    523        1.1   thorpej 
    524   1.41.6.5     joerg retry:
    525   1.41.6.5     joerg 	sc->sc_cur_addr = addr;
    526   1.41.6.5     joerg 	sc->sc_state = EC_STATE_READ;
    527   1.41.6.5     joerg 
    528   1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    529   1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    530   1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    531   1.41.6.7     joerg 			goto done;
    532   1.41.6.7     joerg 		delay(1);
    533   1.41.6.7     joerg 	}
    534   1.41.6.7     joerg 
    535   1.41.6.5     joerg 	if (cold || acpiec_cold) {
    536   1.41.6.7     joerg 		while (sc->sc_state != EC_STATE_FREE) {
    537   1.41.6.7     joerg 			delay(1);
    538   1.41.6.5     joerg 			acpiec_gpe_state_maschine(dv);
    539   1.41.6.7     joerg 		}
    540   1.41.6.5     joerg 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
    541   1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    542   1.41.6.5     joerg 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    543   1.41.6.5     joerg 		mutex_enter(&sc->sc_mtx);
    544   1.41.6.5     joerg 		if (++timeouts < 5)
    545   1.41.6.5     joerg 			goto retry;
    546   1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    547   1.41.6.5     joerg 		acpiec_unlock(dv);
    548   1.41.6.5     joerg 		aprint_error_dev(dv, "command takes over 5sec...\n");
    549   1.41.6.5     joerg 		return AE_ERROR;
    550   1.41.6.5     joerg 	}
    551   1.41.6.5     joerg 
    552   1.41.6.7     joerg done:
    553   1.41.6.7     joerg 	*val = sc->sc_cur_val;
    554   1.41.6.5     joerg 
    555   1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    556   1.41.6.5     joerg 	acpiec_unlock(dv);
    557   1.41.6.5     joerg 	return AE_OK;
    558        1.1   thorpej }
    559        1.1   thorpej 
    560        1.1   thorpej static ACPI_STATUS
    561   1.41.6.5     joerg acpiec_write(device_t dv, uint8_t addr, uint8_t val)
    562        1.1   thorpej {
    563   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    564   1.41.6.7     joerg 	int i, timeouts = 0;
    565        1.1   thorpej 
    566   1.41.6.5     joerg 	acpiec_lock(dv);
    567   1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    568        1.1   thorpej 
    569   1.41.6.5     joerg retry:
    570   1.41.6.5     joerg 	sc->sc_cur_addr = addr;
    571   1.41.6.5     joerg 	sc->sc_cur_val = val;
    572   1.41.6.5     joerg 	sc->sc_state = EC_STATE_WRITE;
    573   1.41.6.5     joerg 
    574   1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    575   1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    576   1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    577   1.41.6.7     joerg 			goto done;
    578   1.41.6.7     joerg 		delay(1);
    579   1.41.6.7     joerg 	}
    580   1.41.6.7     joerg 
    581   1.41.6.5     joerg 	if (cold || acpiec_cold) {
    582   1.41.6.7     joerg 		while (sc->sc_state != EC_STATE_FREE) {
    583   1.41.6.7     joerg 			delay(1);
    584   1.41.6.5     joerg 			acpiec_gpe_state_maschine(dv);
    585   1.41.6.7     joerg 		}
    586   1.41.6.5     joerg 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
    587   1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    588   1.41.6.5     joerg 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    589   1.41.6.5     joerg 		mutex_enter(&sc->sc_mtx);
    590   1.41.6.5     joerg 		if (++timeouts < 5)
    591   1.41.6.5     joerg 			goto retry;
    592   1.41.6.5     joerg 		mutex_exit(&sc->sc_mtx);
    593   1.41.6.5     joerg 		acpiec_unlock(dv);
    594   1.41.6.5     joerg 		aprint_error_dev(dv, "command takes over 5sec...\n");
    595   1.41.6.5     joerg 		return AE_ERROR;
    596   1.41.6.4     joerg 	}
    597   1.41.6.4     joerg 
    598   1.41.6.7     joerg done:
    599   1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    600   1.41.6.5     joerg 	acpiec_unlock(dv);
    601   1.41.6.5     joerg 	return AE_OK;
    602        1.1   thorpej }
    603        1.1   thorpej 
    604        1.1   thorpej static ACPI_STATUS
    605   1.41.6.5     joerg acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
    606   1.41.6.5     joerg     UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
    607        1.1   thorpej {
    608   1.41.6.5     joerg 	device_t dv;
    609   1.41.6.5     joerg 	struct acpiec_softc *sc;
    610       1.25     kochi 	ACPI_STATUS rv;
    611   1.41.6.5     joerg 	uint8_t addr, reg;
    612   1.41.6.5     joerg 	unsigned int i;
    613   1.41.6.5     joerg 
    614   1.41.6.5     joerg 	if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
    615   1.41.6.5     joerg 	    paddr + width / 8 > 0xff)
    616   1.41.6.5     joerg 		return AE_BAD_PARAMETER;
    617        1.1   thorpej 
    618   1.41.6.5     joerg 	addr = paddr;
    619   1.41.6.5     joerg 	dv = arg;
    620   1.41.6.5     joerg 	sc = device_private(dv);
    621   1.41.6.5     joerg 
    622   1.41.6.5     joerg 	rv = AE_OK;
    623   1.41.6.5     joerg 
    624   1.41.6.5     joerg 	switch (func) {
    625   1.41.6.5     joerg 	case ACPI_READ:
    626   1.41.6.8     joerg 		*value = 0;
    627   1.41.6.5     joerg 		for (i = 0; i < width; i += 8, ++addr) {
    628   1.41.6.5     joerg 			rv = acpiec_read(dv, addr, &reg);
    629   1.41.6.5     joerg 			if (rv != AE_OK)
    630   1.41.6.5     joerg 				break;
    631   1.41.6.5     joerg 			*value |= (ACPI_INTEGER)reg << i;
    632   1.41.6.5     joerg 		}
    633   1.41.6.5     joerg 		break;
    634   1.41.6.5     joerg 	case ACPI_WRITE:
    635   1.41.6.5     joerg 		for (i = 0; i < width; i += 8, ++addr) {
    636   1.41.6.5     joerg 			reg = (*value >>i) & 0xff;
    637   1.41.6.5     joerg 			rv = acpiec_write(dv, addr, reg);
    638   1.41.6.5     joerg 			if (rv != AE_OK)
    639   1.41.6.5     joerg 				break;
    640   1.41.6.5     joerg 		}
    641   1.41.6.5     joerg 		break;
    642   1.41.6.5     joerg 	default:
    643   1.41.6.5     joerg 		aprint_error("%s: invalid Address Space function called: %x\n",
    644   1.41.6.5     joerg 		    device_xname(dv), (unsigned int)func);
    645   1.41.6.5     joerg 		return AE_BAD_PARAMETER;
    646   1.41.6.5     joerg 	}
    647        1.1   thorpej 
    648       1.25     kochi 	return rv;
    649       1.24     kochi }
    650        1.1   thorpej 
    651   1.41.6.5     joerg static void
    652   1.41.6.5     joerg acpiec_gpe_query(void *arg)
    653        1.1   thorpej {
    654   1.41.6.5     joerg 	device_t dv = arg;
    655   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    656   1.41.6.5     joerg 	uint8_t reg;
    657   1.41.6.5     joerg 	char qxx[5];
    658       1.25     kochi 	ACPI_STATUS rv;
    659   1.41.6.7     joerg 	int i;
    660        1.1   thorpej 
    661   1.41.6.5     joerg loop:
    662   1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    663        1.1   thorpej 
    664   1.41.6.5     joerg 	if (sc->sc_got_sci == false)
    665   1.41.6.5     joerg 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
    666   1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    667        1.1   thorpej 
    668   1.41.6.5     joerg 	acpiec_lock(dv);
    669   1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    670        1.1   thorpej 
    671   1.41.6.7     joerg 	/* The Query command can always be issued, so be defensive here. */
    672   1.41.6.7     joerg 	sc->sc_got_sci = false;
    673   1.41.6.5     joerg 	sc->sc_state = EC_STATE_QUERY;
    674        1.1   thorpej 
    675   1.41.6.7     joerg 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    676   1.41.6.7     joerg 		acpiec_gpe_state_maschine(dv);
    677   1.41.6.7     joerg 		if (sc->sc_state == EC_STATE_FREE)
    678   1.41.6.7     joerg 			goto done;
    679   1.41.6.7     joerg 		delay(1);
    680   1.41.6.7     joerg 	}
    681   1.41.6.7     joerg 
    682   1.41.6.5     joerg 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
    683        1.1   thorpej 
    684   1.41.6.7     joerg done:
    685   1.41.6.7     joerg 	reg = sc->sc_cur_val;
    686        1.1   thorpej 
    687   1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    688   1.41.6.5     joerg 	acpiec_unlock(dv);
    689   1.41.6.5     joerg 
    690   1.41.6.5     joerg 	if (reg == 0)
    691   1.41.6.5     joerg 		goto loop; /* Spurious query result */
    692   1.41.6.7     joerg 
    693   1.41.6.5     joerg 	/*
    694   1.41.6.5     joerg 	 * Evaluate _Qxx to respond to the controller.
    695   1.41.6.5     joerg 	 */
    696   1.41.6.5     joerg 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
    697   1.41.6.5     joerg 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
    698   1.41.6.5     joerg 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
    699   1.41.6.5     joerg 		aprint_error("%s: GPE query method %s failed: %s",
    700   1.41.6.5     joerg 		    device_xname(dv), qxx, AcpiFormatException(rv));
    701   1.41.6.5     joerg 	}
    702   1.41.6.5     joerg 
    703   1.41.6.5     joerg 	goto loop;
    704       1.24     kochi }
    705        1.1   thorpej 
    706   1.41.6.5     joerg static void
    707   1.41.6.5     joerg acpiec_gpe_state_maschine(device_t dv)
    708        1.1   thorpej {
    709   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    710   1.41.6.5     joerg 	uint8_t reg;
    711        1.1   thorpej 
    712   1.41.6.5     joerg 	reg = acpiec_read_status(sc);
    713        1.1   thorpej 
    714   1.41.6.5     joerg 	if (reg & EC_STATUS_SCI)
    715   1.41.6.5     joerg 		sc->sc_got_sci = true;
    716        1.1   thorpej 
    717   1.41.6.5     joerg 	switch (sc->sc_state) {
    718   1.41.6.5     joerg 	case EC_STATE_QUERY:
    719   1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    720   1.41.6.7     joerg 			break; /* Nothing of interest here. */
    721   1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_QUERY);
    722   1.41.6.7     joerg 		sc->sc_state = EC_STATE_QUERY_VAL;
    723   1.41.6.7     joerg 		break;
    724   1.41.6.7     joerg 
    725   1.41.6.7     joerg 	case EC_STATE_QUERY_VAL:
    726   1.41.6.5     joerg 		if ((reg & EC_STATUS_OBF) == 0)
    727   1.41.6.5     joerg 			break; /* Nothing of interest here. */
    728        1.1   thorpej 
    729   1.41.6.7     joerg 		sc->sc_cur_val = acpiec_read_data(sc);
    730   1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    731   1.41.6.7     joerg 
    732   1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    733   1.41.6.5     joerg 		break;
    734        1.1   thorpej 
    735   1.41.6.5     joerg 	case EC_STATE_READ:
    736   1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    737   1.41.6.5     joerg 			break; /* Nothing of interest here. */
    738        1.1   thorpej 
    739   1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_READ);
    740   1.41.6.7     joerg 		sc->sc_state = EC_STATE_READ_ADDR;
    741   1.41.6.7     joerg 		break;
    742   1.41.6.7     joerg 
    743   1.41.6.7     joerg 	case EC_STATE_READ_ADDR:
    744   1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    745   1.41.6.7     joerg 			break; /* Nothing of interest here. */
    746   1.41.6.7     joerg 
    747   1.41.6.5     joerg 		acpiec_write_data(sc, sc->sc_cur_addr);
    748   1.41.6.5     joerg 		sc->sc_state = EC_STATE_READ_VAL;
    749   1.41.6.5     joerg 		break;
    750        1.1   thorpej 
    751   1.41.6.5     joerg 	case EC_STATE_READ_VAL:
    752   1.41.6.5     joerg 		if ((reg & EC_STATUS_OBF) == 0)
    753   1.41.6.5     joerg 			break; /* Nothing of interest here. */
    754   1.41.6.7     joerg 		sc->sc_cur_val = acpiec_read_data(sc);
    755   1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    756   1.41.6.7     joerg 
    757   1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    758   1.41.6.5     joerg 		break;
    759   1.41.6.3     joerg 
    760   1.41.6.5     joerg 	case EC_STATE_WRITE:
    761   1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    762   1.41.6.5     joerg 			break; /* Nothing of interest here. */
    763   1.41.6.7     joerg 
    764   1.41.6.7     joerg 		acpiec_write_command(sc, EC_COMMAND_WRITE);
    765   1.41.6.7     joerg 		sc->sc_state = EC_STATE_WRITE_ADDR;
    766   1.41.6.7     joerg 		break;
    767   1.41.6.7     joerg 
    768   1.41.6.7     joerg 	case EC_STATE_WRITE_ADDR:
    769   1.41.6.7     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    770   1.41.6.7     joerg 			break; /* Nothing of interest here. */
    771   1.41.6.5     joerg 		acpiec_write_data(sc, sc->sc_cur_addr);
    772   1.41.6.5     joerg 		sc->sc_state = EC_STATE_WRITE_VAL;
    773   1.41.6.5     joerg 		break;
    774   1.41.6.3     joerg 
    775   1.41.6.5     joerg 	case EC_STATE_WRITE_VAL:
    776   1.41.6.5     joerg 		if ((reg & EC_STATUS_IBF) != 0)
    777   1.41.6.5     joerg 			break; /* Nothing of interest here. */
    778   1.41.6.7     joerg 		sc->sc_state = EC_STATE_FREE;
    779   1.41.6.5     joerg 		cv_signal(&sc->sc_cv);
    780   1.41.6.3     joerg 
    781   1.41.6.7     joerg 		acpiec_write_data(sc, sc->sc_cur_val);
    782   1.41.6.3     joerg 		break;
    783   1.41.6.3     joerg 
    784   1.41.6.5     joerg 	case EC_STATE_FREE:
    785   1.41.6.5     joerg 		if (sc->sc_got_sci)
    786   1.41.6.5     joerg 			cv_signal(&sc->sc_cv_sci);
    787   1.41.6.3     joerg 		break;
    788   1.41.6.5     joerg 	default:
    789   1.41.6.5     joerg 		panic("invalid state");
    790   1.41.6.3     joerg 	}
    791   1.41.6.5     joerg }
    792   1.41.6.3     joerg 
    793   1.41.6.5     joerg static UINT32
    794   1.41.6.5     joerg acpiec_gpe_handler(void *arg)
    795   1.41.6.5     joerg {
    796   1.41.6.5     joerg 	device_t dv = arg;
    797   1.41.6.5     joerg 	struct acpiec_softc *sc = device_private(dv);
    798   1.41.6.3     joerg 
    799   1.41.6.5     joerg 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    800   1.41.6.5     joerg 
    801   1.41.6.5     joerg 	mutex_enter(&sc->sc_mtx);
    802   1.41.6.5     joerg 	acpiec_gpe_state_maschine(dv);
    803   1.41.6.5     joerg 	mutex_exit(&sc->sc_mtx);
    804   1.41.6.5     joerg 
    805   1.41.6.5     joerg 	return 0;
    806   1.41.6.3     joerg }
    807