Home | History | Annotate | Line # | Download | only in acpi
acpi_acad.c revision 1.40
      1 /*	$NetBSD: acpi_acad.c,v 1.40 2010/02/24 22:37:56 dyoung 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.40 2010/02/24 22:37:56 dyoung Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/device.h>
     48 #include <sys/mutex.h>
     49 
     50 #include <dev/acpi/acpica.h>
     51 #include <dev/acpi/acpireg.h>
     52 #include <dev/acpi/acpivar.h>
     53 
     54 #include <dev/sysmon/sysmonvar.h>
     55 
     56 #define _COMPONENT		 ACPI_ACAD_COMPONENT
     57 ACPI_MODULE_NAME		 ("acpi_acad")
     58 
     59 struct acpiacad_softc {
     60 	struct acpi_devnode	*sc_node;
     61 	struct sysmon_envsys	*sc_sme;
     62 	struct sysmon_pswitch	 sc_smpsw;
     63 	envsys_data_t		 sc_sensor;
     64 	kmutex_t		 sc_mutex;
     65 	int			 sc_status;
     66 };
     67 
     68 static const char * const acad_hid[] = {
     69 	"ACPI0003",
     70 	NULL
     71 };
     72 
     73 static int	acpiacad_match(device_t, cfdata_t, void *);
     74 static void	acpiacad_attach(device_t, device_t, void *);
     75 static int	acpiacad_detach(device_t, int);
     76 static bool	acpiacad_resume(device_t, const pmf_qual_t *);
     77 static void	acpiacad_get_status(void *);
     78 static void	acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
     79 static void	acpiacad_init_envsys(device_t);
     80 
     81 CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
     82     acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);
     83 
     84 /*
     85  * acpiacad_match:
     86  *
     87  *	Autoconfiguration `match' routine.
     88  */
     89 static int
     90 acpiacad_match(device_t parent, cfdata_t match, void *aux)
     91 {
     92 	struct acpi_attach_args *aa = aux;
     93 
     94 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     95 		return 0;
     96 
     97 	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
     98 }
     99 
    100 /*
    101  * acpiacad_attach:
    102  *
    103  *	Autoconfiguration `attach' routine.
    104  */
    105 static void
    106 acpiacad_attach(device_t parent, device_t self, void *aux)
    107 {
    108 	struct acpiacad_softc *sc = device_private(self);
    109 	struct acpi_attach_args *aa = aux;
    110 	ACPI_STATUS rv;
    111 
    112 	aprint_naive(": ACPI AC Adapter\n");
    113 	aprint_normal(": ACPI AC Adapter\n");
    114 
    115 	sc->sc_sme = NULL;
    116 	sc->sc_status = -1;
    117 	sc->sc_node = aa->aa_node;
    118 
    119 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
    120 
    121 	sc->sc_smpsw.smpsw_name = device_xname(self);
    122 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    123 
    124 	(void)sysmon_pswitch_register(&sc->sc_smpsw);
    125 	(void)pmf_device_register(self, NULL, acpiacad_resume);
    126 
    127 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    128 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);
    129 
    130 	if (ACPI_SUCCESS(rv))
    131 		acpiacad_init_envsys(self);
    132 }
    133 
    134 /*
    135  * acpiacad_detach:
    136  *
    137  *	Autoconfiguration `detach' routine.
    138  */
    139 static int
    140 acpiacad_detach(device_t self, int flags)
    141 {
    142 	struct acpiacad_softc *sc = device_private(self);
    143 	ACPI_STATUS rv;
    144 
    145 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
    146 	    ACPI_ALL_NOTIFY, acpiacad_notify_handler);
    147 
    148 	if (ACPI_FAILURE(rv))
    149 		return EBUSY;
    150 
    151 	mutex_destroy(&sc->sc_mutex);
    152 
    153 	if (sc->sc_sme != NULL)
    154 		sysmon_envsys_unregister(sc->sc_sme);
    155 
    156 	pmf_device_deregister(self);
    157 	sysmon_pswitch_unregister(&sc->sc_smpsw);
    158 
    159 	return 0;
    160 }
    161 
    162 /*
    163  * acpiacad_resume:
    164  *
    165  * 	Queue a new status check.
    166  */
    167 static bool
    168 acpiacad_resume(device_t dv, const pmf_qual_t *qual)
    169 {
    170 
    171 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
    172 
    173 	return true;
    174 }
    175 
    176 /*
    177  * acpiacad_get_status:
    178  *
    179  *	Get, and possibly display, the current AC line status.
    180  */
    181 static void
    182 acpiacad_get_status(void *arg)
    183 {
    184 	device_t dv = arg;
    185 	struct acpiacad_softc *sc = device_private(dv);
    186 	ACPI_INTEGER status;
    187 	ACPI_STATUS rv;
    188 
    189 	mutex_enter(&sc->sc_mutex);
    190 
    191 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);
    192 
    193 	if (ACPI_FAILURE(rv))
    194 		goto fail;
    195 
    196 	if (status != 0 && status != 1) {
    197 		rv = AE_BAD_VALUE;
    198 		goto fail;
    199 	}
    200 
    201 	if (sc->sc_status != status) {
    202 
    203 		/*
    204 		 * If status has changed, send the event:
    205 		 *
    206 		 * PSWITCH_EVENT_PRESSED  : _PSR = 1 : AC online.
    207 		 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
    208 		 */
    209 		sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
    210 	    	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    211 
    212 		aprint_debug_dev(dv, "AC adapter %sconnected\n",
    213 		    status == 0 ? "not " : "");
    214 	}
    215 
    216 	sc->sc_status = status;
    217 	sc->sc_sensor.state = ENVSYS_SVALID;
    218 	sc->sc_sensor.value_cur = sc->sc_status;
    219 
    220 	mutex_exit(&sc->sc_mutex);
    221 
    222 	return;
    223 
    224 fail:
    225 	sc->sc_status = -1;
    226 	sc->sc_sensor.state = ENVSYS_SINVALID;
    227 
    228 	aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
    229 	    AcpiFormatException(rv));
    230 
    231 	mutex_exit(&sc->sc_mutex);
    232 }
    233 
    234 /*
    235  * acpiacad_notify_handler:
    236  *
    237  *	Callback from ACPI interrupt handler to notify us of an event.
    238  */
    239 static void
    240 acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
    241 {
    242 	static const int handler = OSL_NOTIFY_HANDLER;
    243 	device_t dv = context;
    244 
    245 	switch (notify) {
    246 	/*
    247 	 * XXX So, BusCheck is not exactly what I would expect,
    248 	 * but at least my IBM T21 sends it on AC adapter status
    249 	 * change.  --thorpej (at) wasabisystems.com
    250 	 */
    251 	/*
    252 	 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
    253 	 * adapter status change.
    254 	 *  --rpaulo (at) NetBSD.org
    255 	 */
    256 	/*
    257 	 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
    258 	 * adapter status change.
    259 	 *  --jmcneill (at) NetBSD.org
    260 	 */
    261 	case ACPI_NOTIFY_BusCheck:
    262 	case ACPI_NOTIFY_DeviceCheck:
    263 	case ACPI_NOTIFY_PowerSourceStatusChanged:
    264 	case ACPI_NOTIFY_BatteryInformationChanged:
    265 		(void)AcpiOsExecute(handler, acpiacad_get_status, dv);
    266 		break;
    267 
    268 	default:
    269 		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
    270 	}
    271 }
    272 
    273 static void
    274 acpiacad_init_envsys(device_t dv)
    275 {
    276 	struct acpiacad_softc *sc = device_private(dv);
    277 
    278 	sc->sc_sme = sysmon_envsys_create();
    279 
    280 	sc->sc_sensor.state = ENVSYS_SINVALID;
    281 	sc->sc_sensor.units = ENVSYS_INDICATOR;
    282 
    283  	(void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);
    284 
    285 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
    286 		goto fail;
    287 
    288 	sc->sc_sme->sme_name = device_xname(dv);
    289 	sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
    290 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    291 
    292 	if (sysmon_envsys_register(sc->sc_sme) != 0)
    293 		goto fail;
    294 
    295 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);
    296 
    297 	return;
    298 
    299 fail:
    300 	aprint_error_dev(dv, "failed to initialize sysmon\n");
    301 	sysmon_envsys_destroy(sc->sc_sme);
    302 	sc->sc_sme = NULL;
    303 }
    304