Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.7
      1 /*	$NetBSD: acpi_acad.c,v 1.7 2003/02/16 16:50:09 tshiozak Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #if 0
     39 #define ACPI_ACAD_DEBUG
     40 #endif
     41 
     42 /*
     43  * ACPI AC Adapter driver.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.7 2003/02/16 16:50:09 tshiozak Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 
     53 #include <dev/acpi/acpica.h>
     54 #include <dev/acpi/acpireg.h>
     55 #include <dev/acpi/acpivar.h>
     56 
     57 #include <dev/sysmon/sysmonvar.h>
     58 
     59 #define ACPIACAD_NSENSORS	2
     60 
     61 /* sensor indexes */
     62 #define ACPIACAD_CONNECTED	0
     63 #define ACPIACAD_DISCONNECTED	1
     64 
     65 struct acpiacad_softc {
     66 	struct device sc_dev;		/* base device glue */
     67 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
     68 	int sc_flags;			/* see below */
     69 
     70 	struct sysmon_envsys sc_sysmon;
     71 	struct envsys_basic_info sc_info[ACPIACAD_NSENSORS];
     72 	struct envsys_tre_data sc_data[ACPIACAD_NSENSORS];
     73 
     74 	struct simplelock sc_lock;
     75 };
     76 
     77 const struct envsys_range acpiacad_range[] = {
     78 	{ 0, 2,		ENVSYS_INDICATOR },
     79 	{ 1, 0, 	-1},
     80 };
     81 
     82 #define	AACAD_F_VERBOSE		0x01	/* verbose events */
     83 #define AACAD_F_AVAILABLE	0x02	/* information is available */
     84 #define AACAD_F_LOCKED		0x04	/* is locked? */
     85 
     86 #define AACAD_SET(sc, f)	(void)((sc)->sc_flags |= (f))
     87 #define AACAD_CLEAR(sc, f)	(void)((sc)->sc_flags &= ~(f))
     88 #define AACAD_ISSET(sc, f)	((sc)->sc_flags & (f))
     89 
     90 #define AACAD_ASSERT_LOCKED(sc)					\
     91 do {								\
     92 	if (!((sc)->sc_flags & AACAD_F_LOCKED))			\
     93 		panic("acpi_bat (expected to be locked)");	\
     94 } while(/*CONSTCOND*/0)
     95 #define AACAD_ASSERT_UNLOCKED(sc)				\
     96 do {								\
     97 	if (((sc)->sc_flags & AACAD_F_LOCKED))			\
     98 		panic("acpi_bat (expected to be unlocked)");	\
     99 } while(/*CONSTCOND*/0)
    100 #define AACAD_LOCK(sc, s)			\
    101 do {						\
    102 	AACAD_ASSERT_UNLOCKED(sc);		\
    103 	(s) = splhigh();			\
    104 	simple_lock(&(sc)->sc_lock);		\
    105 	AACAD_SET((sc), AACAD_F_LOCKED);	\
    106 } while(/*CONSTCOND*/0)
    107 #define AACAD_UNLOCK(sc, s)			\
    108 do {						\
    109 	AACAD_ASSERT_LOCKED(sc);		\
    110 	AACAD_CLEAR((sc), AACAD_F_LOCKED);	\
    111 	simple_unlock(&(sc)->sc_lock);		\
    112 	splx((s));				\
    113 } while(/*CONSTCOND*/0)
    114 
    115 static int acpiacad_match(struct device *, struct cfdata *, void *);
    116 static void acpiacad_attach(struct device *, struct device *, void *);
    117 
    118 CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
    119     acpiacad_match, acpiacad_attach, NULL, NULL);
    120 
    121 static void acpiacad_get_status(void *);
    122 static void acpiacad_clear_status(struct acpiacad_softc *);
    123 static void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *);
    124 static void acpiacad_init_envsys(struct acpiacad_softc *);
    125 static int acpiacad_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
    126 static int acpiacad_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
    127 
    128 /*
    129  * acpiacad_match:
    130  *
    131  *	Autoconfiguration `match' routine.
    132  */
    133 int
    134 acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
    135 {
    136 	struct acpi_attach_args *aa = aux;
    137 
    138 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    139 		return (0);
    140 
    141 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
    142 		return (1);
    143 
    144 	return (0);
    145 }
    146 
    147 /*
    148  * acpiacad_attach:
    149  *
    150  *	Autoconfiguration `attach' routine.
    151  */
    152 void
    153 acpiacad_attach(struct device *parent, struct device *self, void *aux)
    154 {
    155 	struct acpiacad_softc *sc = (void *) self;
    156 	struct acpi_attach_args *aa = aux;
    157 	ACPI_STATUS rv;
    158 
    159 	printf(": ACPI AC Adapter\n");
    160 
    161 	sc->sc_node = aa->aa_node;
    162 
    163 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    164 	    ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
    165 	if (rv != AE_OK) {
    166 		printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
    167 		    sc->sc_dev.dv_xname, rv);
    168 		return;
    169 	}
    170 
    171 	/* XXX See acpiacad_notify_handler() */
    172 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    173 	    ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
    174 	if (rv != AE_OK) {
    175 		printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
    176 		    sc->sc_dev.dv_xname, rv);
    177 		return;
    178 	}
    179 
    180 #ifdef ACPI_ACAD_DEBUG
    181 	/* Display the current state. */
    182 	sc->sc_flags = AACAD_F_VERBOSE;
    183 #endif
    184 
    185 	acpiacad_init_envsys(sc);
    186 }
    187 
    188 /*
    189  * acpiacad_get_status:
    190  *
    191  *	Get, and possibly display, the current AC line status.
    192  */
    193 void
    194 acpiacad_get_status(void *arg)
    195 {
    196 	struct acpiacad_softc *sc = arg;
    197 	int status, s;
    198 
    199 	if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
    200 	    &status) != AE_OK)
    201 		return;
    202 
    203 	AACAD_LOCK(sc, s);
    204 	sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = !!(status);
    205 	sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = !(status);
    206 	AACAD_SET(sc, AACAD_F_AVAILABLE);
    207 	AACAD_UNLOCK(sc, s);
    208 
    209 	if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
    210 		printf("%s: AC adapter %sconnected\n",
    211 		    sc->sc_dev.dv_xname, status == 0 ? "not " : "");
    212 }
    213 
    214 /*
    215  * Clear status
    216  */
    217 void
    218 acpiacad_clear_status(struct acpiacad_softc *sc)
    219 {
    220 
    221 	AACAD_ASSERT_LOCKED(sc);
    222 
    223 	sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = 0;
    224 	sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = 0;
    225 	AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
    226 }
    227 
    228 /*
    229  * acpiacad_notify_handler:
    230  *
    231  *	Callback from ACPI interrupt handler to notify us of an event.
    232  */
    233 void
    234 acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
    235 {
    236 	struct acpiacad_softc *sc = context;
    237 	int rv, s;
    238 
    239 	switch (notify) {
    240 	/*
    241 	 * XXX So, BusCheck is not exactly what I would expect,
    242 	 * but at least my IBM T21 sends it on AC adapter status
    243 	 * change.  --thorpej (at) wasabisystems.com
    244 	 */
    245 	case ACPI_NOTIFY_BusCheck:
    246 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    247 #ifdef ACPI_ACAD_DEBUG
    248 		printf("%s: received notify message: 0x%x\n",
    249 		    sc->sc_dev.dv_xname, notify);
    250 #endif
    251 		AACAD_LOCK(sc, s);
    252 		acpiacad_clear_status(sc);
    253 		AACAD_UNLOCK(sc, s);
    254 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    255 		    acpiacad_get_status, sc);
    256 		if (rv != AE_OK)
    257 			printf("%s: unable to queue status check: %d\n",
    258 			    sc->sc_dev.dv_xname, rv);
    259 		break;
    260 
    261 	default:
    262 		printf("%s: received unknown notify message: 0x%x\n",
    263 		    sc->sc_dev.dv_xname, notify);
    264 	}
    265 }
    266 
    267 void
    268 acpiacad_init_envsys(struct acpiacad_softc *sc)
    269 {
    270 	int i;
    271 
    272 	sc->sc_sysmon.sme_ranges = acpiacad_range;
    273 
    274 	for (i=0; i<ACPIACAD_NSENSORS; i++) {
    275 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    276 		sc->sc_data[i].validflags |= (ENVSYS_FVALID | ENVSYS_FCURVALID);
    277 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    278 		sc->sc_data[i].warnflags = 0;
    279 	}
    280 
    281 #define INITDATA(index, unit, string) \
    282 	sc->sc_data[index].units = unit;     				\
    283 	sc->sc_info[index].units = unit;     				\
    284 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info->desc),	\
    285 	    "%s %s", sc->sc_dev.dv_xname, string);			\
    286 
    287 	INITDATA(ACPIACAD_CONNECTED, ENVSYS_INDICATOR, "connected");
    288 	INITDATA(ACPIACAD_DISCONNECTED, ENVSYS_INDICATOR, "disconnected");
    289 
    290 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    291 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    292 	sc->sc_sysmon.sme_cookie = sc;
    293 	sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
    294 	sc->sc_sysmon.sme_streinfo = acpiacad_streinfo;
    295 	sc->sc_sysmon.sme_nsensors = ACPIACAD_NSENSORS;
    296 	sc->sc_sysmon.sme_envsys_version = 1000;
    297 
    298 	if (sysmon_envsys_register(&sc->sc_sysmon))
    299 		printf("%s: unable to register with sysmon\n",
    300 		    sc->sc_dev.dv_xname);
    301 }
    302 
    303 int
    304 acpiacad_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    305 {
    306 	struct acpiacad_softc *sc = sme->sme_cookie;
    307 
    308 	if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
    309 		acpiacad_get_status(sc);
    310 
    311 	/* XXX locking */
    312 	*tred = sc->sc_data[tred->sensor];
    313 	/* XXX locking */
    314 
    315 	return (0);
    316 }
    317 
    318 
    319 int
    320 acpiacad_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    321 {
    322 
    323 	/* XXX Not implemented */
    324 	binfo->validflags = 0;
    325 
    326 	return (0);
    327 }
    328