Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.8.2.3
      1  1.8.2.3     skrll /*	$NetBSD: acpi_acad.c,v 1.8.2.3 2004/09/21 13:26:39 skrll 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.7  tshiozak #if 0
     39      1.7  tshiozak #define ACPI_ACAD_DEBUG
     40      1.7  tshiozak #endif
     41      1.7  tshiozak 
     42      1.1   thorpej /*
     43      1.1   thorpej  * ACPI AC Adapter driver.
     44      1.1   thorpej  */
     45      1.2     lukem 
     46      1.2     lukem #include <sys/cdefs.h>
     47  1.8.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.8.2.3 2004/09/21 13:26:39 skrll Exp $");
     48      1.1   thorpej 
     49      1.1   thorpej #include <sys/param.h>
     50      1.1   thorpej #include <sys/systm.h>
     51      1.1   thorpej #include <sys/device.h>
     52      1.1   thorpej 
     53      1.1   thorpej #include <dev/acpi/acpica.h>
     54      1.1   thorpej #include <dev/acpi/acpireg.h>
     55      1.1   thorpej #include <dev/acpi/acpivar.h>
     56      1.1   thorpej 
     57      1.6  explorer #include <dev/sysmon/sysmonvar.h>
     58      1.6  explorer 
     59      1.6  explorer #define ACPIACAD_NSENSORS	2
     60      1.6  explorer 
     61      1.6  explorer /* sensor indexes */
     62      1.6  explorer #define ACPIACAD_CONNECTED	0
     63      1.6  explorer #define ACPIACAD_DISCONNECTED	1
     64      1.6  explorer 
     65      1.1   thorpej struct acpiacad_softc {
     66      1.1   thorpej 	struct device sc_dev;		/* base device glue */
     67      1.1   thorpej 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
     68      1.1   thorpej 	int sc_flags;			/* see below */
     69      1.7  tshiozak 
     70      1.6  explorer 	struct sysmon_envsys sc_sysmon;
     71  1.8.2.1     skrll 	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
     72      1.6  explorer 	struct envsys_basic_info sc_info[ACPIACAD_NSENSORS];
     73      1.6  explorer 	struct envsys_tre_data sc_data[ACPIACAD_NSENSORS];
     74      1.7  tshiozak 
     75      1.7  tshiozak 	struct simplelock sc_lock;
     76      1.6  explorer };
     77      1.6  explorer 
     78  1.8.2.1     skrll static const struct envsys_range acpiacad_range[] = {
     79      1.6  explorer 	{ 0, 2,		ENVSYS_INDICATOR },
     80      1.6  explorer 	{ 1, 0, 	-1},
     81      1.1   thorpej };
     82      1.1   thorpej 
     83  1.8.2.1     skrll static const char * const acad_hid[] = {
     84  1.8.2.1     skrll 	"ACPI0003",
     85  1.8.2.1     skrll 	NULL
     86  1.8.2.1     skrll };
     87  1.8.2.1     skrll 
     88      1.1   thorpej #define	AACAD_F_VERBOSE		0x01	/* verbose events */
     89      1.7  tshiozak #define AACAD_F_AVAILABLE	0x02	/* information is available */
     90      1.7  tshiozak #define AACAD_F_LOCKED		0x04	/* is locked? */
     91      1.7  tshiozak 
     92      1.7  tshiozak #define AACAD_SET(sc, f)	(void)((sc)->sc_flags |= (f))
     93      1.7  tshiozak #define AACAD_CLEAR(sc, f)	(void)((sc)->sc_flags &= ~(f))
     94      1.7  tshiozak #define AACAD_ISSET(sc, f)	((sc)->sc_flags & (f))
     95      1.7  tshiozak 
     96      1.7  tshiozak #define AACAD_ASSERT_LOCKED(sc)					\
     97      1.7  tshiozak do {								\
     98      1.7  tshiozak 	if (!((sc)->sc_flags & AACAD_F_LOCKED))			\
     99      1.7  tshiozak 		panic("acpi_bat (expected to be locked)");	\
    100      1.7  tshiozak } while(/*CONSTCOND*/0)
    101      1.7  tshiozak #define AACAD_ASSERT_UNLOCKED(sc)				\
    102      1.7  tshiozak do {								\
    103      1.7  tshiozak 	if (((sc)->sc_flags & AACAD_F_LOCKED))			\
    104      1.7  tshiozak 		panic("acpi_bat (expected to be unlocked)");	\
    105      1.7  tshiozak } while(/*CONSTCOND*/0)
    106      1.7  tshiozak #define AACAD_LOCK(sc, s)			\
    107      1.7  tshiozak do {						\
    108      1.7  tshiozak 	AACAD_ASSERT_UNLOCKED(sc);		\
    109      1.7  tshiozak 	(s) = splhigh();			\
    110      1.7  tshiozak 	simple_lock(&(sc)->sc_lock);		\
    111      1.7  tshiozak 	AACAD_SET((sc), AACAD_F_LOCKED);	\
    112      1.7  tshiozak } while(/*CONSTCOND*/0)
    113      1.7  tshiozak #define AACAD_UNLOCK(sc, s)			\
    114      1.7  tshiozak do {						\
    115      1.7  tshiozak 	AACAD_ASSERT_LOCKED(sc);		\
    116      1.7  tshiozak 	AACAD_CLEAR((sc), AACAD_F_LOCKED);	\
    117      1.7  tshiozak 	simple_unlock(&(sc)->sc_lock);		\
    118      1.7  tshiozak 	splx((s));				\
    119      1.7  tshiozak } while(/*CONSTCOND*/0)
    120      1.1   thorpej 
    121      1.7  tshiozak static int acpiacad_match(struct device *, struct cfdata *, void *);
    122      1.7  tshiozak static void acpiacad_attach(struct device *, struct device *, void *);
    123      1.1   thorpej 
    124      1.4   thorpej CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
    125      1.5   thorpej     acpiacad_match, acpiacad_attach, NULL, NULL);
    126      1.1   thorpej 
    127      1.7  tshiozak static void acpiacad_get_status(void *);
    128      1.7  tshiozak static void acpiacad_clear_status(struct acpiacad_softc *);
    129      1.7  tshiozak static void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *);
    130      1.7  tshiozak static void acpiacad_init_envsys(struct acpiacad_softc *);
    131      1.6  explorer static int acpiacad_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
    132      1.6  explorer static int acpiacad_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    133      1.1   thorpej 
    134      1.1   thorpej /*
    135      1.1   thorpej  * acpiacad_match:
    136      1.1   thorpej  *
    137      1.1   thorpej  *	Autoconfiguration `match' routine.
    138      1.1   thorpej  */
    139  1.8.2.1     skrll static int
    140      1.1   thorpej acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
    141      1.1   thorpej {
    142      1.1   thorpej 	struct acpi_attach_args *aa = aux;
    143      1.1   thorpej 
    144      1.1   thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    145  1.8.2.1     skrll 		return 0;
    146      1.1   thorpej 
    147  1.8.2.1     skrll 	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
    148      1.1   thorpej }
    149      1.1   thorpej 
    150      1.1   thorpej /*
    151      1.1   thorpej  * acpiacad_attach:
    152      1.1   thorpej  *
    153      1.1   thorpej  *	Autoconfiguration `attach' routine.
    154      1.1   thorpej  */
    155  1.8.2.1     skrll static void
    156      1.1   thorpej acpiacad_attach(struct device *parent, struct device *self, void *aux)
    157      1.1   thorpej {
    158      1.1   thorpej 	struct acpiacad_softc *sc = (void *) self;
    159      1.1   thorpej 	struct acpi_attach_args *aa = aux;
    160      1.1   thorpej 	ACPI_STATUS rv;
    161      1.1   thorpej 
    162      1.1   thorpej 	printf(": ACPI AC Adapter\n");
    163      1.1   thorpej 
    164      1.1   thorpej 	sc->sc_node = aa->aa_node;
    165      1.8      yamt 	simple_lock_init(&sc->sc_lock);
    166      1.1   thorpej 
    167  1.8.2.1     skrll 	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
    168  1.8.2.1     skrll 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    169  1.8.2.1     skrll 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
    170  1.8.2.1     skrll 		printf("%s: unable to register with sysmon\n",
    171  1.8.2.1     skrll 		       sc->sc_dev.dv_xname);
    172  1.8.2.1     skrll 		return;
    173  1.8.2.1     skrll 	}
    174  1.8.2.1     skrll 
    175      1.1   thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    176      1.1   thorpej 	    ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
    177  1.8.2.1     skrll 	if (ACPI_FAILURE(rv)) {
    178  1.8.2.1     skrll 		printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
    179  1.8.2.1     skrll 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    180      1.1   thorpej 		return;
    181      1.1   thorpej 	}
    182      1.1   thorpej 
    183      1.1   thorpej 	/* XXX See acpiacad_notify_handler() */
    184      1.1   thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    185      1.1   thorpej 	    ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
    186  1.8.2.1     skrll 	if (ACPI_FAILURE(rv)) {
    187  1.8.2.1     skrll 		printf("%s: unable to register SYSTEM NOTIFY handler: %s\n",
    188  1.8.2.1     skrll 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    189      1.1   thorpej 		return;
    190      1.1   thorpej 	}
    191      1.1   thorpej 
    192      1.7  tshiozak #ifdef ACPI_ACAD_DEBUG
    193      1.1   thorpej 	/* Display the current state. */
    194      1.1   thorpej 	sc->sc_flags = AACAD_F_VERBOSE;
    195      1.7  tshiozak #endif
    196      1.7  tshiozak 
    197      1.6  explorer 	acpiacad_init_envsys(sc);
    198      1.1   thorpej }
    199      1.1   thorpej 
    200      1.1   thorpej /*
    201      1.1   thorpej  * acpiacad_get_status:
    202      1.1   thorpej  *
    203      1.1   thorpej  *	Get, and possibly display, the current AC line status.
    204      1.1   thorpej  */
    205  1.8.2.1     skrll static void
    206      1.1   thorpej acpiacad_get_status(void *arg)
    207      1.1   thorpej {
    208      1.1   thorpej 	struct acpiacad_softc *sc = arg;
    209  1.8.2.1     skrll 	ACPI_INTEGER status;
    210  1.8.2.1     skrll 	int s;
    211  1.8.2.1     skrll 	ACPI_STATUS rv;
    212      1.1   thorpej 
    213  1.8.2.1     skrll 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
    214  1.8.2.1     skrll 	if (ACPI_FAILURE(rv))
    215      1.1   thorpej 		return;
    216      1.1   thorpej 
    217      1.7  tshiozak 	AACAD_LOCK(sc, s);
    218      1.7  tshiozak 	sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = !!(status);
    219      1.7  tshiozak 	sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = !(status);
    220      1.7  tshiozak 	AACAD_SET(sc, AACAD_F_AVAILABLE);
    221      1.7  tshiozak 	AACAD_UNLOCK(sc, s);
    222      1.6  explorer 
    223  1.8.2.1     skrll 	/*
    224  1.8.2.1     skrll 	 * PSWITCH_EVENT_RELEASED : AC offline
    225  1.8.2.1     skrll 	 * PSWITCH_EVENT_PRESSED  : AC online
    226  1.8.2.1     skrll 	 */
    227  1.8.2.1     skrll 
    228  1.8.2.1     skrll 	sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ?
    229  1.8.2.1     skrll 	    PSWITCH_EVENT_RELEASED : PSWITCH_EVENT_PRESSED);
    230  1.8.2.1     skrll 
    231      1.7  tshiozak 	if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
    232      1.1   thorpej 		printf("%s: AC adapter %sconnected\n",
    233      1.7  tshiozak 		    sc->sc_dev.dv_xname, status == 0 ? "not " : "");
    234      1.7  tshiozak }
    235      1.7  tshiozak 
    236      1.7  tshiozak /*
    237      1.7  tshiozak  * Clear status
    238      1.7  tshiozak  */
    239  1.8.2.1     skrll static void
    240      1.7  tshiozak acpiacad_clear_status(struct acpiacad_softc *sc)
    241      1.7  tshiozak {
    242      1.7  tshiozak 
    243      1.7  tshiozak 	AACAD_ASSERT_LOCKED(sc);
    244      1.7  tshiozak 
    245      1.7  tshiozak 	sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = 0;
    246      1.7  tshiozak 	sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = 0;
    247      1.7  tshiozak 	AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
    248      1.1   thorpej }
    249      1.1   thorpej 
    250      1.1   thorpej /*
    251      1.1   thorpej  * acpiacad_notify_handler:
    252      1.1   thorpej  *
    253      1.1   thorpej  *	Callback from ACPI interrupt handler to notify us of an event.
    254      1.1   thorpej  */
    255  1.8.2.1     skrll static void
    256      1.1   thorpej acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
    257      1.1   thorpej {
    258      1.1   thorpej 	struct acpiacad_softc *sc = context;
    259      1.7  tshiozak 	int rv, s;
    260      1.1   thorpej 
    261      1.1   thorpej 	switch (notify) {
    262      1.1   thorpej 	/*
    263      1.1   thorpej 	 * XXX So, BusCheck is not exactly what I would expect,
    264      1.1   thorpej 	 * but at least my IBM T21 sends it on AC adapter status
    265      1.1   thorpej 	 * change.  --thorpej (at) wasabisystems.com
    266      1.1   thorpej 	 */
    267      1.1   thorpej 	case ACPI_NOTIFY_BusCheck:
    268      1.1   thorpej 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    269      1.1   thorpej #ifdef ACPI_ACAD_DEBUG
    270      1.1   thorpej 		printf("%s: received notify message: 0x%x\n",
    271      1.1   thorpej 		    sc->sc_dev.dv_xname, notify);
    272      1.1   thorpej #endif
    273      1.7  tshiozak 		AACAD_LOCK(sc, s);
    274      1.7  tshiozak 		acpiacad_clear_status(sc);
    275      1.7  tshiozak 		AACAD_UNLOCK(sc, s);
    276      1.1   thorpej 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    277      1.1   thorpej 		    acpiacad_get_status, sc);
    278  1.8.2.1     skrll 		if (ACPI_FAILURE(rv))
    279  1.8.2.1     skrll 			printf("%s: unable to queue status check: %s\n",
    280  1.8.2.1     skrll 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    281      1.1   thorpej 		break;
    282      1.1   thorpej 
    283      1.1   thorpej 	default:
    284      1.1   thorpej 		printf("%s: received unknown notify message: 0x%x\n",
    285      1.1   thorpej 		    sc->sc_dev.dv_xname, notify);
    286      1.1   thorpej 	}
    287      1.6  explorer }
    288      1.6  explorer 
    289  1.8.2.1     skrll static void
    290      1.6  explorer acpiacad_init_envsys(struct acpiacad_softc *sc)
    291      1.6  explorer {
    292      1.6  explorer 	int i;
    293      1.6  explorer 
    294      1.6  explorer 	sc->sc_sysmon.sme_ranges = acpiacad_range;
    295      1.6  explorer 
    296      1.6  explorer 	for (i=0; i<ACPIACAD_NSENSORS; i++) {
    297      1.6  explorer 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    298      1.6  explorer 		sc->sc_data[i].validflags |= (ENVSYS_FVALID | ENVSYS_FCURVALID);
    299      1.6  explorer 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    300      1.6  explorer 		sc->sc_data[i].warnflags = 0;
    301      1.6  explorer 	}
    302      1.6  explorer 
    303      1.6  explorer #define INITDATA(index, unit, string) \
    304      1.6  explorer 	sc->sc_data[index].units = unit;     				\
    305      1.6  explorer 	sc->sc_info[index].units = unit;     				\
    306      1.6  explorer 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info->desc),	\
    307      1.6  explorer 	    "%s %s", sc->sc_dev.dv_xname, string);			\
    308      1.6  explorer 
    309      1.6  explorer 	INITDATA(ACPIACAD_CONNECTED, ENVSYS_INDICATOR, "connected");
    310      1.6  explorer 	INITDATA(ACPIACAD_DISCONNECTED, ENVSYS_INDICATOR, "disconnected");
    311      1.6  explorer 
    312      1.6  explorer 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    313      1.6  explorer 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    314      1.6  explorer 	sc->sc_sysmon.sme_cookie = sc;
    315      1.6  explorer 	sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
    316      1.6  explorer 	sc->sc_sysmon.sme_streinfo = acpiacad_streinfo;
    317      1.6  explorer 	sc->sc_sysmon.sme_nsensors = ACPIACAD_NSENSORS;
    318      1.6  explorer 	sc->sc_sysmon.sme_envsys_version = 1000;
    319      1.6  explorer 
    320      1.6  explorer 	if (sysmon_envsys_register(&sc->sc_sysmon))
    321      1.6  explorer 		printf("%s: unable to register with sysmon\n",
    322      1.6  explorer 		    sc->sc_dev.dv_xname);
    323      1.6  explorer }
    324      1.6  explorer 
    325  1.8.2.1     skrll static int
    326      1.6  explorer acpiacad_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    327      1.6  explorer {
    328      1.6  explorer 	struct acpiacad_softc *sc = sme->sme_cookie;
    329      1.7  tshiozak 
    330      1.7  tshiozak 	if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
    331      1.7  tshiozak 		acpiacad_get_status(sc);
    332      1.6  explorer 
    333      1.6  explorer 	/* XXX locking */
    334      1.6  explorer 	*tred = sc->sc_data[tred->sensor];
    335      1.6  explorer 	/* XXX locking */
    336      1.6  explorer 
    337  1.8.2.1     skrll 	return 0;
    338      1.6  explorer }
    339      1.6  explorer 
    340      1.6  explorer 
    341  1.8.2.1     skrll static int
    342      1.6  explorer acpiacad_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    343      1.6  explorer {
    344      1.6  explorer 
    345      1.6  explorer 	/* XXX Not implemented */
    346      1.6  explorer 	binfo->validflags = 0;
    347      1.6  explorer 
    348  1.8.2.1     skrll 	return 0;
    349      1.1   thorpej }
    350