Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.16.12.6
      1  1.16.12.6      yamt /*	$NetBSD: acpi_acad.c,v 1.16.12.6 2007/12/07 17:29:36 yamt 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.16.12.6      yamt __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.16.12.6 2007/12/07 17:29:36 yamt 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.16.12.3      yamt #include <sys/mutex.h>
     53        1.1   thorpej 
     54        1.1   thorpej #include <dev/acpi/acpica.h>
     55        1.1   thorpej #include <dev/acpi/acpireg.h>
     56        1.1   thorpej #include <dev/acpi/acpivar.h>
     57        1.1   thorpej 
     58        1.6  explorer #include <dev/sysmon/sysmonvar.h>
     59        1.6  explorer 
     60        1.1   thorpej struct acpiacad_softc {
     61        1.1   thorpej 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
     62        1.1   thorpej 	int sc_flags;			/* see below */
     63  1.16.12.3      yamt 	int sc_status;			/* status changed/not changed */
     64  1.16.12.3      yamt 	int sc_notifysent;		/* notify message sent */
     65        1.7  tshiozak 
     66  1.16.12.6      yamt 	struct sysmon_envsys *sc_sme;
     67       1.16     kochi 	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
     68  1.16.12.6      yamt 	envsys_data_t sc_sensor;
     69        1.7  tshiozak 
     70  1.16.12.3      yamt 	kmutex_t sc_mtx;
     71        1.1   thorpej };
     72        1.1   thorpej 
     73       1.10     kochi static const char * const acad_hid[] = {
     74       1.10     kochi 	"ACPI0003",
     75       1.10     kochi 	NULL
     76       1.10     kochi };
     77       1.10     kochi 
     78        1.1   thorpej #define	AACAD_F_VERBOSE		0x01	/* verbose events */
     79        1.7  tshiozak #define AACAD_F_AVAILABLE	0x02	/* information is available */
     80  1.16.12.3      yamt #define AACAD_F_STCHANGED	0x04	/* status changed */
     81        1.7  tshiozak 
     82        1.7  tshiozak #define AACAD_SET(sc, f)	(void)((sc)->sc_flags |= (f))
     83        1.7  tshiozak #define AACAD_CLEAR(sc, f)	(void)((sc)->sc_flags &= ~(f))
     84        1.7  tshiozak #define AACAD_ISSET(sc, f)	((sc)->sc_flags & (f))
     85        1.7  tshiozak 
     86  1.16.12.4      yamt static int acpiacad_match(device_t, struct cfdata *, void *);
     87  1.16.12.4      yamt static void acpiacad_attach(device_t, device_t, void *);
     88        1.1   thorpej 
     89  1.16.12.4      yamt CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
     90        1.5   thorpej     acpiacad_match, acpiacad_attach, NULL, NULL);
     91        1.1   thorpej 
     92        1.7  tshiozak static void acpiacad_get_status(void *);
     93        1.7  tshiozak static void acpiacad_clear_status(struct acpiacad_softc *);
     94        1.7  tshiozak static void acpiacad_notify_handler(ACPI_HANDLE, UINT32, void *);
     95  1.16.12.4      yamt static void acpiacad_init_envsys(device_t);
     96  1.16.12.6      yamt static void acpiacad_refresh(struct sysmon_envsys *, envsys_data_t *);
     97        1.1   thorpej 
     98        1.1   thorpej /*
     99        1.1   thorpej  * acpiacad_match:
    100        1.1   thorpej  *
    101        1.1   thorpej  *	Autoconfiguration `match' routine.
    102        1.1   thorpej  */
    103       1.15     kochi static int
    104  1.16.12.4      yamt acpiacad_match(device_t parent, struct cfdata *match, void *aux)
    105        1.1   thorpej {
    106        1.1   thorpej 	struct acpi_attach_args *aa = aux;
    107        1.1   thorpej 
    108        1.1   thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    109       1.14     kochi 		return 0;
    110        1.1   thorpej 
    111       1.14     kochi 	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
    112        1.1   thorpej }
    113        1.1   thorpej 
    114        1.1   thorpej /*
    115        1.1   thorpej  * acpiacad_attach:
    116        1.1   thorpej  *
    117        1.1   thorpej  *	Autoconfiguration `attach' routine.
    118        1.1   thorpej  */
    119       1.15     kochi static void
    120  1.16.12.4      yamt acpiacad_attach(device_t parent, device_t self, void *aux)
    121        1.1   thorpej {
    122  1.16.12.4      yamt 	struct acpiacad_softc *sc = device_private(self);
    123        1.1   thorpej 	struct acpi_attach_args *aa = aux;
    124        1.1   thorpej 	ACPI_STATUS rv;
    125        1.1   thorpej 
    126  1.16.12.1      yamt 	aprint_naive(": ACPI AC Adapter\n");
    127  1.16.12.1      yamt 	aprint_normal(": ACPI AC Adapter\n");
    128        1.1   thorpej 
    129        1.1   thorpej 	sc->sc_node = aa->aa_node;
    130  1.16.12.6      yamt 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
    131        1.1   thorpej 
    132  1.16.12.4      yamt 	sc->sc_smpsw.smpsw_name = device_xname(self);
    133       1.16     kochi 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    134       1.16     kochi 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
    135  1.16.12.4      yamt 		aprint_error_dev(self, "unable to register with sysmon\n");
    136       1.16     kochi 		return;
    137       1.16     kochi 	}
    138       1.16     kochi 
    139  1.16.12.3      yamt 	sc->sc_status = -1;
    140        1.1   thorpej 
    141        1.1   thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    142  1.16.12.4      yamt 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
    143       1.12   mycroft 	if (ACPI_FAILURE(rv)) {
    144  1.16.12.4      yamt 		aprint_error_dev(self, "unable to register DEVICE and SYSTEM "
    145  1.16.12.4      yamt 		    "NOTIFY handler: %s\n", AcpiFormatException(rv));
    146        1.1   thorpej 		return;
    147        1.1   thorpej 	}
    148        1.1   thorpej 
    149        1.7  tshiozak #ifdef ACPI_ACAD_DEBUG
    150        1.1   thorpej 	/* Display the current state. */
    151        1.1   thorpej 	sc->sc_flags = AACAD_F_VERBOSE;
    152        1.7  tshiozak #endif
    153        1.7  tshiozak 
    154  1.16.12.4      yamt 	acpiacad_init_envsys(self);
    155        1.1   thorpej }
    156        1.1   thorpej 
    157        1.1   thorpej /*
    158        1.1   thorpej  * acpiacad_get_status:
    159        1.1   thorpej  *
    160        1.1   thorpej  *	Get, and possibly display, the current AC line status.
    161        1.1   thorpej  */
    162       1.15     kochi static void
    163        1.1   thorpej acpiacad_get_status(void *arg)
    164        1.1   thorpej {
    165  1.16.12.4      yamt 	device_t dv = arg;
    166  1.16.12.4      yamt 	struct acpiacad_softc *sc = device_private(dv);
    167       1.13   kanaoka 	ACPI_INTEGER status;
    168       1.12   mycroft 	ACPI_STATUS rv;
    169        1.1   thorpej 
    170       1.12   mycroft 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
    171       1.12   mycroft 	if (ACPI_FAILURE(rv))
    172        1.1   thorpej 		return;
    173        1.1   thorpej 
    174  1.16.12.3      yamt 	mutex_enter(&sc->sc_mtx);
    175  1.16.12.3      yamt 	if (sc->sc_status != status) {
    176  1.16.12.3      yamt 		sc->sc_status = status;
    177  1.16.12.3      yamt 		if (status)
    178  1.16.12.6      yamt 			sc->sc_sensor.value_cur = 1;
    179  1.16.12.3      yamt 		else
    180  1.16.12.6      yamt 			sc->sc_sensor.value_cur = 0;
    181  1.16.12.3      yamt 		AACAD_SET(sc, AACAD_F_STCHANGED);
    182  1.16.12.3      yamt 		sc->sc_notifysent = 0;
    183  1.16.12.3      yamt 	}
    184        1.6  explorer 
    185  1.16.12.6      yamt 	sc->sc_sensor.state = ENVSYS_SVALID;
    186  1.16.12.3      yamt 	AACAD_SET(sc, AACAD_F_AVAILABLE);
    187       1.16     kochi 	/*
    188  1.16.12.3      yamt 	 * If status has changed, send the event.
    189  1.16.12.3      yamt 	 *
    190       1.16     kochi 	 * PSWITCH_EVENT_RELEASED : AC offline
    191       1.16     kochi 	 * PSWITCH_EVENT_PRESSED  : AC online
    192       1.16     kochi 	 */
    193  1.16.12.3      yamt 	if (AACAD_ISSET(sc, AACAD_F_STCHANGED)) {
    194  1.16.12.3      yamt 		sysmon_pswitch_event(&sc->sc_smpsw, status ?
    195  1.16.12.3      yamt 	    	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    196  1.16.12.3      yamt 		if (AACAD_ISSET(sc, AACAD_F_VERBOSE))
    197  1.16.12.4      yamt 			aprint_verbose_dev(dv, "AC adapter %sconnected\n",
    198  1.16.12.4      yamt 		    	    status == 0 ? "not " : "");
    199  1.16.12.3      yamt 	}
    200  1.16.12.3      yamt 	mutex_exit(&sc->sc_mtx);
    201        1.7  tshiozak }
    202        1.7  tshiozak 
    203        1.7  tshiozak /*
    204        1.7  tshiozak  * Clear status
    205        1.7  tshiozak  */
    206       1.15     kochi static void
    207        1.7  tshiozak acpiacad_clear_status(struct acpiacad_softc *sc)
    208        1.7  tshiozak {
    209  1.16.12.6      yamt 	sc->sc_sensor.state = ENVSYS_SINVALID;
    210        1.7  tshiozak 	AACAD_CLEAR(sc, AACAD_F_AVAILABLE);
    211  1.16.12.3      yamt 	AACAD_CLEAR(sc, AACAD_F_STCHANGED);
    212        1.1   thorpej }
    213        1.1   thorpej 
    214        1.1   thorpej /*
    215        1.1   thorpej  * acpiacad_notify_handler:
    216        1.1   thorpej  *
    217        1.1   thorpej  *	Callback from ACPI interrupt handler to notify us of an event.
    218        1.1   thorpej  */
    219       1.15     kochi static void
    220  1.16.12.3      yamt acpiacad_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
    221        1.1   thorpej {
    222  1.16.12.4      yamt 	device_t dv = context;
    223  1.16.12.4      yamt 	struct acpiacad_softc *sc = device_private(dv);
    224  1.16.12.3      yamt 	int rv;
    225        1.1   thorpej 
    226        1.1   thorpej 	switch (notify) {
    227        1.1   thorpej 	/*
    228        1.1   thorpej 	 * XXX So, BusCheck is not exactly what I would expect,
    229        1.1   thorpej 	 * but at least my IBM T21 sends it on AC adapter status
    230        1.1   thorpej 	 * change.  --thorpej (at) wasabisystems.com
    231        1.1   thorpej 	 */
    232  1.16.12.1      yamt 	/*
    233  1.16.12.1      yamt 	 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
    234  1.16.12.1      yamt 	 * adapter status change.
    235  1.16.12.1      yamt 	 *  --rpaulo (at) NetBSD.org
    236  1.16.12.1      yamt 	 */
    237  1.16.12.3      yamt 	/*
    238  1.16.12.3      yamt 	 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
    239  1.16.12.3      yamt 	 * adapter status change.
    240  1.16.12.3      yamt 	 *  --jmcneill (at) NetBSD.org
    241  1.16.12.3      yamt 	 */
    242        1.1   thorpej 	case ACPI_NOTIFY_BusCheck:
    243  1.16.12.1      yamt 	case ACPI_NOTIFY_DeviceCheck:
    244        1.1   thorpej 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    245  1.16.12.3      yamt 	case ACPI_NOTIFY_BatteryInformationChanged:
    246  1.16.12.3      yamt 		mutex_enter(&sc->sc_mtx);
    247  1.16.12.3      yamt 		acpiacad_clear_status(sc);
    248  1.16.12.3      yamt 		mutex_exit(&sc->sc_mtx);
    249  1.16.12.3      yamt 		if (sc->sc_status == -1 || !sc->sc_notifysent) {
    250  1.16.12.3      yamt 			rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    251  1.16.12.4      yamt 			    acpiacad_get_status, dv);
    252  1.16.12.3      yamt 			if (ACPI_FAILURE(rv))
    253  1.16.12.4      yamt 				aprint_error_dev(dv,
    254  1.16.12.4      yamt 				    "unable to queue status check: %s\n",
    255  1.16.12.3      yamt 				    AcpiFormatException(rv));
    256  1.16.12.3      yamt 			sc->sc_notifysent = 1;
    257        1.1   thorpej #ifdef ACPI_ACAD_DEBUG
    258  1.16.12.4      yamt 			aprint_debug_dev(dv, "received notify message: 0x%x\n",
    259  1.16.12.4      yamt 		    	    notify);
    260        1.1   thorpej #endif
    261  1.16.12.3      yamt 		}
    262        1.1   thorpej 		break;
    263        1.1   thorpej 
    264        1.1   thorpej 	default:
    265  1.16.12.4      yamt 		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
    266  1.16.12.4      yamt 		    notify);
    267        1.1   thorpej 	}
    268        1.6  explorer }
    269        1.6  explorer 
    270       1.15     kochi static void
    271  1.16.12.4      yamt acpiacad_init_envsys(device_t dv)
    272        1.6  explorer {
    273  1.16.12.4      yamt 	struct acpiacad_softc *sc = device_private(dv);
    274  1.16.12.4      yamt 
    275  1.16.12.6      yamt 	sc->sc_sme = sysmon_envsys_create();
    276  1.16.12.6      yamt 	sc->sc_sensor.state = ENVSYS_SVALID;
    277  1.16.12.6      yamt 	sc->sc_sensor.units = ENVSYS_INDICATOR;
    278  1.16.12.6      yamt  	strlcpy(sc->sc_sensor.desc, "connected", sizeof(sc->sc_sensor.desc));
    279  1.16.12.6      yamt 
    280  1.16.12.6      yamt 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    281  1.16.12.6      yamt 		aprint_error_dev(dv, "unable to add sensor\n");
    282  1.16.12.6      yamt 		sysmon_envsys_destroy(sc->sc_sme);
    283  1.16.12.6      yamt 		return;
    284  1.16.12.6      yamt 	}
    285        1.6  explorer 
    286  1.16.12.6      yamt 	sc->sc_sme->sme_name = device_xname(dv);
    287  1.16.12.6      yamt 	sc->sc_sme->sme_cookie = dv;
    288  1.16.12.6      yamt 	sc->sc_sme->sme_refresh = acpiacad_refresh;
    289  1.16.12.6      yamt 	sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
    290  1.16.12.6      yamt 
    291  1.16.12.6      yamt 	if (sysmon_envsys_register(sc->sc_sme)) {
    292  1.16.12.4      yamt 		aprint_error_dev(dv, "unable to register with sysmon\n");
    293  1.16.12.6      yamt 		sysmon_envsys_destroy(sc->sc_sme);
    294  1.16.12.6      yamt 	}
    295        1.6  explorer }
    296        1.6  explorer 
    297  1.16.12.6      yamt static void
    298  1.16.12.6      yamt acpiacad_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    299        1.6  explorer {
    300  1.16.12.4      yamt 	device_t dv = sme->sme_cookie;
    301  1.16.12.4      yamt 	struct acpiacad_softc *sc = device_private(dv);
    302        1.7  tshiozak 
    303        1.7  tshiozak 	if (!AACAD_ISSET(sc, AACAD_F_AVAILABLE))
    304  1.16.12.4      yamt 		acpiacad_get_status(dv);
    305        1.1   thorpej }
    306