Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: acpi_acad.c,v 1.4 2002/09/30 20:41:17 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.1  thorpej  *
      9  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1  thorpej  * modification, are permitted provided that the following conditions
     11  1.1  thorpej  * are met:
     12  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1  thorpej  *    must display the following acknowledgement:
     19  1.1  thorpej  *	This product includes software developed for the NetBSD Project by
     20  1.1  thorpej  *	Wasabi Systems, Inc.
     21  1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  thorpej  *    or promote products derived from this software without specific prior
     23  1.1  thorpej  *    written permission.
     24  1.1  thorpej  *
     25  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  thorpej  */
     37  1.1  thorpej 
     38  1.1  thorpej /*
     39  1.1  thorpej  * ACPI AC Adapter driver.
     40  1.1  thorpej  */
     41  1.2    lukem 
     42  1.2    lukem #include <sys/cdefs.h>
     43  1.4  thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.4 2002/09/30 20:41:17 thorpej Exp $");
     44  1.1  thorpej 
     45  1.1  thorpej #include <sys/param.h>
     46  1.1  thorpej #include <sys/systm.h>
     47  1.1  thorpej #include <sys/device.h>
     48  1.1  thorpej 
     49  1.1  thorpej #include <dev/acpi/acpica.h>
     50  1.1  thorpej #include <dev/acpi/acpireg.h>
     51  1.1  thorpej #include <dev/acpi/acpivar.h>
     52  1.1  thorpej 
     53  1.1  thorpej struct acpiacad_softc {
     54  1.1  thorpej 	struct device sc_dev;		/* base device glue */
     55  1.1  thorpej 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
     56  1.1  thorpej 	int sc_flags;			/* see below */
     57  1.1  thorpej 	int sc_status;			/* power status */
     58  1.1  thorpej };
     59  1.1  thorpej 
     60  1.1  thorpej #define	AACAD_F_VERBOSE		0x01	/* verbose events */
     61  1.1  thorpej 
     62  1.1  thorpej int	acpiacad_match(struct device *, struct cfdata *, void *);
     63  1.1  thorpej void	acpiacad_attach(struct device *, struct device *, void *);
     64  1.1  thorpej 
     65  1.4  thorpej CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
     66  1.4  thorpej     acpiacad_match, acpiacad_attach, NULL, NULL)
     67  1.1  thorpej 
     68  1.1  thorpej void	acpiacad_get_status(void *);
     69  1.1  thorpej void	acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *context);
     70  1.1  thorpej 
     71  1.1  thorpej /*
     72  1.1  thorpej  * acpiacad_match:
     73  1.1  thorpej  *
     74  1.1  thorpej  *	Autoconfiguration `match' routine.
     75  1.1  thorpej  */
     76  1.1  thorpej int
     77  1.1  thorpej acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
     78  1.1  thorpej {
     79  1.1  thorpej 	struct acpi_attach_args *aa = aux;
     80  1.1  thorpej 
     81  1.1  thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     82  1.1  thorpej 		return (0);
     83  1.1  thorpej 
     84  1.1  thorpej 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
     85  1.1  thorpej 		return (1);
     86  1.1  thorpej 
     87  1.1  thorpej 	return (0);
     88  1.1  thorpej }
     89  1.1  thorpej 
     90  1.1  thorpej /*
     91  1.1  thorpej  * acpiacad_attach:
     92  1.1  thorpej  *
     93  1.1  thorpej  *	Autoconfiguration `attach' routine.
     94  1.1  thorpej  */
     95  1.1  thorpej void
     96  1.1  thorpej acpiacad_attach(struct device *parent, struct device *self, void *aux)
     97  1.1  thorpej {
     98  1.1  thorpej 	struct acpiacad_softc *sc = (void *) self;
     99  1.1  thorpej 	struct acpi_attach_args *aa = aux;
    100  1.1  thorpej 	ACPI_STATUS rv;
    101  1.1  thorpej 
    102  1.1  thorpej 	printf(": ACPI AC Adapter\n");
    103  1.1  thorpej 
    104  1.1  thorpej 	sc->sc_node = aa->aa_node;
    105  1.1  thorpej 
    106  1.1  thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    107  1.1  thorpej 	    ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
    108  1.1  thorpej 	if (rv != AE_OK) {
    109  1.1  thorpej 		printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
    110  1.1  thorpej 		    sc->sc_dev.dv_xname, rv);
    111  1.1  thorpej 		return;
    112  1.1  thorpej 	}
    113  1.1  thorpej 
    114  1.1  thorpej 	/* XXX See acpiacad_notify_handler() */
    115  1.1  thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    116  1.1  thorpej 	    ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
    117  1.1  thorpej 	if (rv != AE_OK) {
    118  1.1  thorpej 		printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
    119  1.1  thorpej 		    sc->sc_dev.dv_xname, rv);
    120  1.1  thorpej 		return;
    121  1.1  thorpej 	}
    122  1.1  thorpej 
    123  1.1  thorpej 	/* Display the current state. */
    124  1.1  thorpej 	sc->sc_flags = AACAD_F_VERBOSE;
    125  1.1  thorpej 	acpiacad_get_status(sc);
    126  1.1  thorpej 
    127  1.1  thorpej 	/*
    128  1.1  thorpej 	 * XXX Hook into sysmon here.
    129  1.1  thorpej 	 */
    130  1.1  thorpej }
    131  1.1  thorpej 
    132  1.1  thorpej /*
    133  1.1  thorpej  * acpiacad_get_status:
    134  1.1  thorpej  *
    135  1.1  thorpej  *	Get, and possibly display, the current AC line status.
    136  1.1  thorpej  */
    137  1.1  thorpej void
    138  1.1  thorpej acpiacad_get_status(void *arg)
    139  1.1  thorpej {
    140  1.1  thorpej 	struct acpiacad_softc *sc = arg;
    141  1.1  thorpej 
    142  1.1  thorpej 	if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
    143  1.1  thorpej 	    &sc->sc_status) != AE_OK)
    144  1.1  thorpej 		return;
    145  1.1  thorpej 
    146  1.1  thorpej 	if (sc->sc_flags & AACAD_F_VERBOSE)
    147  1.1  thorpej 		printf("%s: AC adapter %sconnected\n",
    148  1.1  thorpej 		    sc->sc_dev.dv_xname, sc->sc_status == 0 ? "not " : "");
    149  1.1  thorpej }
    150  1.1  thorpej 
    151  1.1  thorpej /*
    152  1.1  thorpej  * acpiacad_notify_handler:
    153  1.1  thorpej  *
    154  1.1  thorpej  *	Callback from ACPI interrupt handler to notify us of an event.
    155  1.1  thorpej  */
    156  1.1  thorpej void
    157  1.1  thorpej acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
    158  1.1  thorpej {
    159  1.1  thorpej 	struct acpiacad_softc *sc = context;
    160  1.1  thorpej 	int rv;
    161  1.1  thorpej 
    162  1.1  thorpej 	switch (notify) {
    163  1.1  thorpej 	/*
    164  1.1  thorpej 	 * XXX So, BusCheck is not exactly what I would expect,
    165  1.1  thorpej 	 * but at least my IBM T21 sends it on AC adapter status
    166  1.1  thorpej 	 * change.  --thorpej (at) wasabisystems.com
    167  1.1  thorpej 	 */
    168  1.1  thorpej 	case ACPI_NOTIFY_BusCheck:
    169  1.1  thorpej 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    170  1.1  thorpej #ifdef ACPI_ACAD_DEBUG
    171  1.1  thorpej 		printf("%s: received notify message: 0x%x\n",
    172  1.1  thorpej 		    sc->sc_dev.dv_xname, notify);
    173  1.1  thorpej #endif
    174  1.1  thorpej 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    175  1.1  thorpej 		    acpiacad_get_status, sc);
    176  1.1  thorpej 		if (rv != AE_OK)
    177  1.1  thorpej 			printf("%s: unable to queue status check: %d\n",
    178  1.1  thorpej 			    sc->sc_dev.dv_xname, rv);
    179  1.1  thorpej 		break;
    180  1.1  thorpej 
    181  1.1  thorpej 	default:
    182  1.1  thorpej 		printf("%s: received unknown notify message: 0x%x\n",
    183  1.1  thorpej 		    sc->sc_dev.dv_xname, notify);
    184  1.1  thorpej 	}
    185  1.1  thorpej }
    186