Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.6
      1 /*	$NetBSD: acpi_acad.c,v 1.6 2002/12/31 05:26:56 explorer 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 /*
     39  * ACPI AC Adapter driver.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.6 2002/12/31 05:26:56 explorer Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/device.h>
     48 
     49 #include <dev/acpi/acpica.h>
     50 #include <dev/acpi/acpireg.h>
     51 #include <dev/acpi/acpivar.h>
     52 
     53 #include <dev/sysmon/sysmonvar.h>
     54 
     55 #define ACPIACAD_NSENSORS	2
     56 
     57 /* sensor indexes */
     58 #define ACPIACAD_CONNECTED	0
     59 #define ACPIACAD_DISCONNECTED	1
     60 
     61 struct acpiacad_softc {
     62 	struct device sc_dev;		/* base device glue */
     63 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
     64 	int sc_flags;			/* see below */
     65 	int sc_status;			/* power status */
     66 	struct sysmon_envsys sc_sysmon;
     67 	struct envsys_basic_info sc_info[ACPIACAD_NSENSORS];
     68 	struct envsys_tre_data sc_data[ACPIACAD_NSENSORS];
     69 };
     70 
     71 const struct envsys_range acpiacad_range[] = {
     72 	{ 0, 2,		ENVSYS_INDICATOR },
     73 	{ 1, 0, 	-1},
     74 };
     75 
     76 #define	AACAD_F_VERBOSE		0x01	/* verbose events */
     77 
     78 int	acpiacad_match(struct device *, struct cfdata *, void *);
     79 void	acpiacad_attach(struct device *, struct device *, void *);
     80 
     81 CFATTACH_DECL(acpiacad, sizeof(struct acpiacad_softc),
     82     acpiacad_match, acpiacad_attach, NULL, NULL);
     83 
     84 void	acpiacad_get_status(void *);
     85 void	acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *context);
     86 static void acpiacad_init_envsys(struct acpiacad_softc *sc);
     87 static int acpiacad_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
     88 static int acpiacad_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
     89 
     90 /*
     91  * acpiacad_match:
     92  *
     93  *	Autoconfiguration `match' routine.
     94  */
     95 int
     96 acpiacad_match(struct device *parent, struct cfdata *match, void *aux)
     97 {
     98 	struct acpi_attach_args *aa = aux;
     99 
    100 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    101 		return (0);
    102 
    103 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "ACPI0003") == 0)
    104 		return (1);
    105 
    106 	return (0);
    107 }
    108 
    109 /*
    110  * acpiacad_attach:
    111  *
    112  *	Autoconfiguration `attach' routine.
    113  */
    114 void
    115 acpiacad_attach(struct device *parent, struct device *self, void *aux)
    116 {
    117 	struct acpiacad_softc *sc = (void *) self;
    118 	struct acpi_attach_args *aa = aux;
    119 	ACPI_STATUS rv;
    120 
    121 	printf(": ACPI AC Adapter\n");
    122 
    123 	sc->sc_node = aa->aa_node;
    124 
    125 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    126 	    ACPI_DEVICE_NOTIFY, acpiacad_notify_handler, sc);
    127 	if (rv != AE_OK) {
    128 		printf("%s: unable to register DEVICE NOTIFY handler: %d\n",
    129 		    sc->sc_dev.dv_xname, rv);
    130 		return;
    131 	}
    132 
    133 	/* XXX See acpiacad_notify_handler() */
    134 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    135 	    ACPI_SYSTEM_NOTIFY, acpiacad_notify_handler, sc);
    136 	if (rv != AE_OK) {
    137 		printf("%s: unable to register SYSTEM NOTIFY handler: %d\n",
    138 		    sc->sc_dev.dv_xname, rv);
    139 		return;
    140 	}
    141 
    142 	/* Display the current state. */
    143 	sc->sc_flags = AACAD_F_VERBOSE;
    144 	acpiacad_get_status(sc);
    145 	acpiacad_init_envsys(sc);
    146 }
    147 
    148 /*
    149  * acpiacad_get_status:
    150  *
    151  *	Get, and possibly display, the current AC line status.
    152  */
    153 void
    154 acpiacad_get_status(void *arg)
    155 {
    156 	struct acpiacad_softc *sc = arg;
    157 
    158 	if (acpi_eval_integer(sc->sc_node->ad_handle, "_PSR",
    159 	    &sc->sc_status) != AE_OK)
    160 		return;
    161 
    162 	sc->sc_data[ACPIACAD_CONNECTED].cur.data_s = !!(sc->sc_status);
    163 	sc->sc_data[ACPIACAD_DISCONNECTED].cur.data_s = !(sc->sc_status);
    164 
    165 	if (sc->sc_flags & AACAD_F_VERBOSE)
    166 		printf("%s: AC adapter %sconnected\n",
    167 		    sc->sc_dev.dv_xname, sc->sc_status == 0 ? "not " : "");
    168 }
    169 
    170 /*
    171  * acpiacad_notify_handler:
    172  *
    173  *	Callback from ACPI interrupt handler to notify us of an event.
    174  */
    175 void
    176 acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
    177 {
    178 	struct acpiacad_softc *sc = context;
    179 	int rv;
    180 
    181 	switch (notify) {
    182 	/*
    183 	 * XXX So, BusCheck is not exactly what I would expect,
    184 	 * but at least my IBM T21 sends it on AC adapter status
    185 	 * change.  --thorpej (at) wasabisystems.com
    186 	 */
    187 	case ACPI_NOTIFY_BusCheck:
    188 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    189 #ifdef ACPI_ACAD_DEBUG
    190 		printf("%s: received notify message: 0x%x\n",
    191 		    sc->sc_dev.dv_xname, notify);
    192 #endif
    193 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    194 		    acpiacad_get_status, sc);
    195 		if (rv != AE_OK)
    196 			printf("%s: unable to queue status check: %d\n",
    197 			    sc->sc_dev.dv_xname, rv);
    198 		break;
    199 
    200 	default:
    201 		printf("%s: received unknown notify message: 0x%x\n",
    202 		    sc->sc_dev.dv_xname, notify);
    203 	}
    204 }
    205 
    206 static void
    207 acpiacad_init_envsys(struct acpiacad_softc *sc)
    208 {
    209 	int i;
    210 
    211 	sc->sc_sysmon.sme_ranges = acpiacad_range;
    212 
    213 	for (i=0; i<ACPIACAD_NSENSORS; i++) {
    214 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    215 		sc->sc_data[i].validflags |= (ENVSYS_FVALID | ENVSYS_FCURVALID);
    216 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    217 		sc->sc_data[i].warnflags = 0;
    218 	}
    219 
    220 #define INITDATA(index, unit, string) \
    221 	sc->sc_data[index].units = unit;     				\
    222 	sc->sc_info[index].units = unit;     				\
    223 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info->desc),	\
    224 	    "%s %s", sc->sc_dev.dv_xname, string);			\
    225 
    226 	INITDATA(ACPIACAD_CONNECTED, ENVSYS_INDICATOR, "connected");
    227 	INITDATA(ACPIACAD_DISCONNECTED, ENVSYS_INDICATOR, "disconnected");
    228 
    229 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    230 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    231 	sc->sc_sysmon.sme_cookie = sc;
    232 	sc->sc_sysmon.sme_gtredata = acpiacad_gtredata;
    233 	sc->sc_sysmon.sme_streinfo = acpiacad_streinfo;
    234 	sc->sc_sysmon.sme_nsensors = ACPIACAD_NSENSORS;
    235 	sc->sc_sysmon.sme_envsys_version = 1000;
    236 
    237 	if (sysmon_envsys_register(&sc->sc_sysmon))
    238 		printf("%s: unable to register with sysmon\n",
    239 		    sc->sc_dev.dv_xname);
    240 }
    241 
    242 
    243 int
    244 acpiacad_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    245 {
    246 	struct acpiacad_softc *sc = sme->sme_cookie;
    247 
    248 	/* XXX locking */
    249 	*tred = sc->sc_data[tred->sensor];
    250 	/* XXX locking */
    251 
    252 	return (0);
    253 }
    254 
    255 
    256 int
    257 acpiacad_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    258 {
    259 
    260 	/* XXX Not implemented */
    261 	binfo->validflags = 0;
    262 
    263 	return (0);
    264 }
    265