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