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