Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.33.4.3
      1  1.33.4.3      yamt /*	$NetBSD: acpi_acad.c,v 1.33.4.3 2010/03/11 15:03:22 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.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.33.4.3      yamt __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.33.4.3 2010/03/11 15:03:22 yamt Exp $");
     44       1.1   thorpej 
     45       1.1   thorpej #include <sys/param.h>
     46       1.1   thorpej #include <sys/device.h>
     47  1.33.4.3      yamt #include <sys/module.h>
     48      1.23   xtraeme #include <sys/mutex.h>
     49  1.33.4.3      yamt #include <sys/systm.h>
     50       1.1   thorpej 
     51       1.1   thorpej #include <dev/acpi/acpireg.h>
     52       1.1   thorpej #include <dev/acpi/acpivar.h>
     53       1.1   thorpej 
     54  1.33.4.3      yamt #define _COMPONENT		 ACPI_ACAD_COMPONENT
     55  1.33.4.3      yamt ACPI_MODULE_NAME		 ("acpi_acad")
     56       1.6  explorer 
     57       1.1   thorpej struct acpiacad_softc {
     58  1.33.4.3      yamt 	struct acpi_devnode	*sc_node;
     59  1.33.4.3      yamt 	struct sysmon_envsys	*sc_sme;
     60  1.33.4.3      yamt 	struct sysmon_pswitch	 sc_smpsw;
     61  1.33.4.3      yamt 	envsys_data_t		 sc_sensor;
     62  1.33.4.3      yamt 	kmutex_t		 sc_mutex;
     63  1.33.4.3      yamt 	int			 sc_status;
     64       1.1   thorpej };
     65       1.1   thorpej 
     66      1.10     kochi static const char * const acad_hid[] = {
     67      1.10     kochi 	"ACPI0003",
     68      1.10     kochi 	NULL
     69      1.10     kochi };
     70      1.10     kochi 
     71  1.33.4.3      yamt static int	acpiacad_match(device_t, cfdata_t, void *);
     72  1.33.4.3      yamt static void	acpiacad_attach(device_t, device_t, void *);
     73  1.33.4.3      yamt static int	acpiacad_detach(device_t, int);
     74  1.33.4.3      yamt static bool	acpiacad_resume(device_t, const pmf_qual_t *);
     75  1.33.4.3      yamt static void	acpiacad_get_status(void *);
     76  1.33.4.3      yamt static void	acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
     77  1.33.4.3      yamt static void	acpiacad_init_envsys(device_t);
     78       1.1   thorpej 
     79      1.27     joerg CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
     80  1.33.4.3      yamt     acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
     81       1.1   thorpej 
     82       1.1   thorpej /*
     83       1.1   thorpej  * acpiacad_match:
     84       1.1   thorpej  *
     85       1.1   thorpej  *	Autoconfiguration `match' routine.
     86       1.1   thorpej  */
     87      1.15     kochi static int
     88  1.33.4.1      yamt acpiacad_match(device_t parent, cfdata_t match, void *aux)
     89       1.1   thorpej {
     90       1.1   thorpej 	struct acpi_attach_args *aa = aux;
     91       1.1   thorpej 
     92       1.1   thorpej 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     93      1.14     kochi 		return 0;
     94       1.1   thorpej 
     95      1.14     kochi 	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
     96       1.1   thorpej }
     97       1.1   thorpej 
     98       1.1   thorpej /*
     99       1.1   thorpej  * acpiacad_attach:
    100       1.1   thorpej  *
    101       1.1   thorpej  *	Autoconfiguration `attach' routine.
    102       1.1   thorpej  */
    103      1.15     kochi static void
    104      1.27     joerg acpiacad_attach(device_t parent, device_t self, void *aux)
    105       1.1   thorpej {
    106      1.27     joerg 	struct acpiacad_softc *sc = device_private(self);
    107       1.1   thorpej 	struct acpi_attach_args *aa = aux;
    108       1.1   thorpej 	ACPI_STATUS rv;
    109       1.1   thorpej 
    110      1.18     kochi 	aprint_naive(": ACPI AC Adapter\n");
    111      1.18     kochi 	aprint_normal(": ACPI AC Adapter\n");
    112       1.1   thorpej 
    113  1.33.4.3      yamt 	sc->sc_sme = NULL;
    114  1.33.4.3      yamt 	sc->sc_status = -1;
    115       1.1   thorpej 	sc->sc_node = aa->aa_node;
    116  1.33.4.3      yamt 
    117  1.33.4.3      yamt 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
    118       1.1   thorpej 
    119      1.27     joerg 	sc->sc_smpsw.smpsw_name = device_xname(self);
    120      1.16     kochi 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    121      1.16     kochi 
    122  1.33.4.3      yamt 	(void)sysmon_pswitch_register(&sc->sc_smpsw);
    123  1.33.4.3      yamt 	(void)pmf_device_register(self, NULL, acpiacad_resume);
    124       1.1   thorpej 
    125       1.1   thorpej 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    126      1.27     joerg 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
    127       1.1   thorpej 
    128  1.33.4.3      yamt 	if (ACPI_SUCCESS(rv))
    129  1.33.4.3      yamt 		acpiacad_init_envsys(self);
    130  1.33.4.3      yamt }
    131  1.33.4.3      yamt 
    132  1.33.4.3      yamt /*
    133  1.33.4.3      yamt  * acpiacad_detach:
    134  1.33.4.3      yamt  *
    135  1.33.4.3      yamt  *	Autoconfiguration `detach' routine.
    136  1.33.4.3      yamt  */
    137  1.33.4.3      yamt static int
    138  1.33.4.3      yamt acpiacad_detach(device_t self, int flags)
    139  1.33.4.3      yamt {
    140  1.33.4.3      yamt 	struct acpiacad_softc *sc = device_private(self);
    141  1.33.4.3      yamt 	ACPI_STATUS rv;
    142  1.33.4.3      yamt 
    143  1.33.4.3      yamt 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
    144  1.33.4.3      yamt 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler);
    145  1.33.4.3      yamt 
    146  1.33.4.3      yamt 	if (ACPI_FAILURE(rv))
    147  1.33.4.3      yamt 		return EBUSY;
    148  1.33.4.3      yamt 
    149  1.33.4.3      yamt 	mutex_destroy(&sc->sc_mutex);
    150  1.33.4.3      yamt 
    151  1.33.4.3      yamt 	if (sc->sc_sme != NULL)
    152  1.33.4.3      yamt 		sysmon_envsys_unregister(sc->sc_sme);
    153       1.7  tshiozak 
    154  1.33.4.3      yamt 	pmf_device_deregister(self);
    155  1.33.4.3      yamt 	sysmon_pswitch_unregister(&sc->sc_smpsw);
    156      1.31  jmcneill 
    157  1.33.4.3      yamt 	return 0;
    158       1.1   thorpej }
    159       1.1   thorpej 
    160       1.1   thorpej /*
    161      1.32   xtraeme  * acpiacad_resume:
    162      1.32   xtraeme  *
    163  1.33.4.3      yamt  * 	Queue a new status check.
    164      1.32   xtraeme  */
    165      1.32   xtraeme static bool
    166  1.33.4.3      yamt acpiacad_resume(device_t dv, const pmf_qual_t *qual)
    167      1.32   xtraeme {
    168      1.32   xtraeme 
    169  1.33.4.3      yamt 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
    170  1.33.4.3      yamt 
    171      1.32   xtraeme 	return true;
    172      1.32   xtraeme }
    173      1.32   xtraeme 
    174      1.32   xtraeme /*
    175       1.1   thorpej  * acpiacad_get_status:
    176       1.1   thorpej  *
    177       1.1   thorpej  *	Get, and possibly display, the current AC line status.
    178       1.1   thorpej  */
    179      1.15     kochi static void
    180       1.1   thorpej acpiacad_get_status(void *arg)
    181       1.1   thorpej {
    182      1.27     joerg 	device_t dv = arg;
    183      1.27     joerg 	struct acpiacad_softc *sc = device_private(dv);
    184      1.13   kanaoka 	ACPI_INTEGER status;
    185      1.12   mycroft 	ACPI_STATUS rv;
    186       1.1   thorpej 
    187  1.33.4.3      yamt 	mutex_enter(&sc->sc_mutex);
    188  1.33.4.3      yamt 
    189      1.12   mycroft 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
    190  1.33.4.3      yamt 
    191      1.12   mycroft 	if (ACPI_FAILURE(rv))
    192  1.33.4.3      yamt 		goto fail;
    193       1.1   thorpej 
    194  1.33.4.3      yamt 	if (status != 0 && status != 1) {
    195  1.33.4.3      yamt 		rv = AE_BAD_VALUE;
    196  1.33.4.3      yamt 		goto fail;
    197      1.23   xtraeme 	}
    198      1.23   xtraeme 
    199  1.33.4.3      yamt 	if (sc->sc_status != status) {
    200  1.33.4.3      yamt 
    201  1.33.4.3      yamt 		/*
    202  1.33.4.3      yamt 		 * If status has changed, send the event:
    203  1.33.4.3      yamt 		 *
    204  1.33.4.3      yamt 		 * PSWITCH_EVENT_PRESSED  : _PSR = 1 : AC online.
    205  1.33.4.3      yamt 		 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
    206  1.33.4.3      yamt 		 */
    207  1.33.4.3      yamt 		sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
    208      1.23   xtraeme 	    	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    209  1.33.4.3      yamt 
    210  1.33.4.3      yamt 		aprint_debug_dev(dv, "AC adapter %sconnected\n",
    211  1.33.4.3      yamt 		    status == 0 ? "not " : "");
    212      1.23   xtraeme 	}
    213       1.7  tshiozak 
    214  1.33.4.3      yamt 	sc->sc_status = status;
    215  1.33.4.3      yamt 	sc->sc_sensor.state = ENVSYS_SVALID;
    216  1.33.4.3      yamt 	sc->sc_sensor.value_cur = sc->sc_status;
    217  1.33.4.3      yamt 
    218  1.33.4.3      yamt 	mutex_exit(&sc->sc_mutex);
    219  1.33.4.3      yamt 
    220  1.33.4.3      yamt 	return;
    221  1.33.4.3      yamt 
    222  1.33.4.3      yamt fail:
    223  1.33.4.3      yamt 	sc->sc_status = -1;
    224      1.29   xtraeme 	sc->sc_sensor.state = ENVSYS_SINVALID;
    225  1.33.4.3      yamt 
    226  1.33.4.3      yamt 	aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
    227  1.33.4.3      yamt 	    AcpiFormatException(rv));
    228  1.33.4.3      yamt 
    229  1.33.4.3      yamt 	mutex_exit(&sc->sc_mutex);
    230       1.1   thorpej }
    231       1.1   thorpej 
    232       1.1   thorpej /*
    233       1.1   thorpej  * acpiacad_notify_handler:
    234       1.1   thorpej  *
    235       1.1   thorpej  *	Callback from ACPI interrupt handler to notify us of an event.
    236       1.1   thorpej  */
    237      1.15     kochi static void
    238  1.33.4.3      yamt acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
    239       1.1   thorpej {
    240  1.33.4.3      yamt 	static const int handler = OSL_NOTIFY_HANDLER;
    241      1.27     joerg 	device_t dv = context;
    242       1.1   thorpej 
    243       1.1   thorpej 	switch (notify) {
    244       1.1   thorpej 	/*
    245       1.1   thorpej 	 * XXX So, BusCheck is not exactly what I would expect,
    246       1.1   thorpej 	 * but at least my IBM T21 sends it on AC adapter status
    247       1.1   thorpej 	 * change.  --thorpej (at) wasabisystems.com
    248       1.1   thorpej 	 */
    249      1.19    rpaulo 	/*
    250      1.19    rpaulo 	 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
    251      1.19    rpaulo 	 * adapter status change.
    252      1.19    rpaulo 	 *  --rpaulo (at) NetBSD.org
    253      1.19    rpaulo 	 */
    254      1.22  jmcneill 	/*
    255      1.22  jmcneill 	 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
    256      1.22  jmcneill 	 * adapter status change.
    257      1.22  jmcneill 	 *  --jmcneill (at) NetBSD.org
    258      1.22  jmcneill 	 */
    259       1.1   thorpej 	case ACPI_NOTIFY_BusCheck:
    260      1.19    rpaulo 	case ACPI_NOTIFY_DeviceCheck:
    261       1.1   thorpej 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    262      1.22  jmcneill 	case ACPI_NOTIFY_BatteryInformationChanged:
    263  1.33.4.3      yamt 		(void)AcpiOsExecute(handler, acpiacad_get_status, dv);
    264       1.1   thorpej 		break;
    265       1.1   thorpej 
    266       1.1   thorpej 	default:
    267  1.33.4.3      yamt 		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
    268       1.1   thorpej 	}
    269       1.6  explorer }
    270       1.6  explorer 
    271      1.15     kochi static void
    272      1.27     joerg acpiacad_init_envsys(device_t dv)
    273       1.6  explorer {
    274      1.27     joerg 	struct acpiacad_softc *sc = device_private(dv);
    275      1.27     joerg 
    276      1.29   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    277  1.33.4.3      yamt 
    278  1.33.4.3      yamt 	sc->sc_sensor.state = ENVSYS_SINVALID;
    279      1.29   xtraeme 	sc->sc_sensor.units = ENVSYS_INDICATOR;
    280      1.29   xtraeme 
    281  1.33.4.3      yamt  	(void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);
    282  1.33.4.3      yamt 
    283  1.33.4.3      yamt 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
    284  1.33.4.3      yamt 		goto fail;
    285       1.6  explorer 
    286      1.29   xtraeme 	sc->sc_sme->sme_name = device_xname(dv);
    287      1.29   xtraeme 	sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
    288  1.33.4.3      yamt 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    289      1.29   xtraeme 
    290  1.33.4.3      yamt 	if (sysmon_envsys_register(sc->sc_sme) != 0)
    291  1.33.4.3      yamt 		goto fail;
    292  1.33.4.3      yamt 
    293  1.33.4.3      yamt 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
    294  1.33.4.3      yamt 
    295  1.33.4.3      yamt 	return;
    296  1.33.4.3      yamt 
    297  1.33.4.3      yamt fail:
    298  1.33.4.3      yamt 	aprint_error_dev(dv, "failed to initialize sysmon\n");
    299  1.33.4.3      yamt 	sysmon_envsys_destroy(sc->sc_sme);
    300  1.33.4.3      yamt 	sc->sc_sme = NULL;
    301       1.6  explorer }
    302       1.6  explorer 
    303  1.33.4.3      yamt #ifdef _MODULE
    304  1.33.4.3      yamt 
    305  1.33.4.3      yamt MODULE(MODULE_CLASS_DRIVER, acpiacad, NULL);
    306  1.33.4.3      yamt CFDRIVER_DECL(acpiacad, DV_DULL, NULL);
    307  1.33.4.3      yamt 
    308  1.33.4.3      yamt static int acpiacadloc[] = { -1 };
    309  1.33.4.3      yamt extern struct cfattach acpiacad_ca;
    310  1.33.4.3      yamt 
    311  1.33.4.3      yamt static struct cfparent acpiparent = {
    312  1.33.4.3      yamt 	"acpinodebus", NULL, DVUNIT_ANY
    313  1.33.4.3      yamt };
    314  1.33.4.3      yamt 
    315  1.33.4.3      yamt static struct cfdata acpiacad_cfdata[] = {
    316  1.33.4.3      yamt 	{
    317  1.33.4.3      yamt 		.cf_name = "acpiacad",
    318  1.33.4.3      yamt 		.cf_atname = "acpiacad",
    319  1.33.4.3      yamt 		.cf_unit = 0,
    320  1.33.4.3      yamt 		.cf_fstate = FSTATE_STAR,
    321  1.33.4.3      yamt 		.cf_loc = acpiacadloc,
    322  1.33.4.3      yamt 		.cf_flags = 0,
    323  1.33.4.3      yamt 		.cf_pspec = &acpiparent,
    324  1.33.4.3      yamt 	},
    325  1.33.4.3      yamt 
    326  1.33.4.3      yamt 	{ NULL }
    327  1.33.4.3      yamt };
    328  1.33.4.3      yamt 
    329  1.33.4.3      yamt static int
    330  1.33.4.3      yamt acpiacad_modcmd(modcmd_t cmd, void *context)
    331       1.6  explorer {
    332  1.33.4.3      yamt 	int err;
    333  1.33.4.3      yamt 
    334  1.33.4.3      yamt 	switch (cmd) {
    335  1.33.4.3      yamt 
    336  1.33.4.3      yamt 	case MODULE_CMD_INIT:
    337  1.33.4.3      yamt 
    338  1.33.4.3      yamt 		err = config_cfdriver_attach(&acpiacad_cd);
    339  1.33.4.3      yamt 
    340  1.33.4.3      yamt 		if (err != 0)
    341  1.33.4.3      yamt 			return err;
    342  1.33.4.3      yamt 
    343  1.33.4.3      yamt 		err = config_cfattach_attach("acpiacad", &acpiacad_ca);
    344  1.33.4.3      yamt 
    345  1.33.4.3      yamt 		if (err != 0) {
    346  1.33.4.3      yamt 			config_cfdriver_detach(&acpiacad_cd);
    347  1.33.4.3      yamt 			return err;
    348  1.33.4.3      yamt 		}
    349  1.33.4.3      yamt 
    350  1.33.4.3      yamt 		err = config_cfdata_attach(acpiacad_cfdata, 1);
    351  1.33.4.3      yamt 
    352  1.33.4.3      yamt 		if (err != 0) {
    353  1.33.4.3      yamt 			config_cfattach_detach("acpiacad", &acpiacad_ca);
    354  1.33.4.3      yamt 			config_cfdriver_detach(&acpiacad_cd);
    355  1.33.4.3      yamt 			return err;
    356  1.33.4.3      yamt 		}
    357       1.7  tshiozak 
    358  1.33.4.3      yamt 		return 0;
    359  1.33.4.3      yamt 
    360  1.33.4.3      yamt 	case MODULE_CMD_FINI:
    361  1.33.4.3      yamt 
    362  1.33.4.3      yamt 		err = config_cfdata_detach(acpiacad_cfdata);
    363  1.33.4.3      yamt 
    364  1.33.4.3      yamt 		if (err != 0)
    365  1.33.4.3      yamt 			return err;
    366  1.33.4.3      yamt 
    367  1.33.4.3      yamt 		config_cfattach_detach("acpiacad", &acpiacad_ca);
    368  1.33.4.3      yamt 		config_cfdriver_detach(&acpiacad_cd);
    369  1.33.4.3      yamt 
    370  1.33.4.3      yamt 		return 0;
    371  1.33.4.3      yamt 
    372  1.33.4.3      yamt 	default:
    373  1.33.4.3      yamt 		return ENOTTY;
    374  1.33.4.3      yamt 	}
    375       1.1   thorpej }
    376  1.33.4.3      yamt 
    377  1.33.4.3      yamt #endif	/* _MODULE */
    378