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