acpi_lid.c revision 1.30
11.30Sjruoho/*	$NetBSD: acpi_lid.c,v 1.30 2009/12/31 10:12:51 jruoho Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.7Sthorpej * Copyright 2001, 2003 Wasabi Systems, Inc.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
81.1Sthorpej *
91.1Sthorpej * Redistribution and use in source and binary forms, with or without
101.1Sthorpej * modification, are permitted provided that the following conditions
111.1Sthorpej * are met:
121.1Sthorpej * 1. Redistributions of source code must retain the above copyright
131.1Sthorpej *    notice, this list of conditions and the following disclaimer.
141.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
151.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
161.1Sthorpej *    documentation and/or other materials provided with the distribution.
171.1Sthorpej * 3. All advertising materials mentioning features or use of this software
181.1Sthorpej *    must display the following acknowledgement:
191.1Sthorpej *	This product includes software developed for the NetBSD Project by
201.1Sthorpej *	Wasabi Systems, Inc.
211.1Sthorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Sthorpej *    or promote products derived from this software without specific prior
231.1Sthorpej *    written permission.
241.1Sthorpej *
251.1Sthorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Sthorpej * POSSIBILITY OF SUCH DAMAGE.
361.1Sthorpej */
371.1Sthorpej
381.1Sthorpej/*
391.1Sthorpej * ACPI Lid Switch driver.
401.1Sthorpej */
411.3Slukem
421.3Slukem#include <sys/cdefs.h>
431.30Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.30 2009/12/31 10:12:51 jruoho Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/systm.h>
471.1Sthorpej#include <sys/device.h>
481.1Sthorpej
491.1Sthorpej#include <dev/acpi/acpica.h>
501.1Sthorpej#include <dev/acpi/acpireg.h>
511.1Sthorpej#include <dev/acpi/acpivar.h>
521.1Sthorpej
531.7Sthorpej#include <dev/sysmon/sysmonvar.h>
541.7Sthorpej
551.1Sthorpejstruct acpilid_softc {
561.1Sthorpej	struct acpi_devnode *sc_node;	/* our ACPI devnode */
571.7Sthorpej	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
581.1Sthorpej};
591.1Sthorpej
601.10Skochistatic const char * const lid_hid[] = {
611.10Skochi	"PNP0C0D",
621.10Skochi	NULL
631.10Skochi};
641.10Skochi
651.25Sxtraemestatic int	acpilid_match(device_t, cfdata_t, void *);
661.25Sxtraemestatic void	acpilid_attach(device_t, device_t, void *);
671.26Sdyoungstatic int	acpilid_detach(device_t, int);
681.1Sthorpej
691.25SxtraemeCFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc),
701.26Sdyoung    acpilid_match, acpilid_attach, acpilid_detach, NULL);
711.1Sthorpej
721.15Skochistatic void	acpilid_status_changed(void *);
731.18Skochistatic void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *);
741.1Sthorpej
751.25Sxtraemestatic void	acpilid_wake_event(device_t, bool);
761.23Sdyoungstatic bool	acpilid_suspend(device_t PMF_FN_PROTO);
771.22Sjmcneill
781.1Sthorpej/*
791.1Sthorpej * acpilid_match:
801.1Sthorpej *
811.1Sthorpej *	Autoconfiguration `match' routine.
821.1Sthorpej */
831.15Skochistatic int
841.25Sxtraemeacpilid_match(device_t parent, cfdata_t match, void *aux)
851.1Sthorpej{
861.1Sthorpej	struct acpi_attach_args *aa = aux;
871.1Sthorpej
881.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
891.14Skochi		return 0;
901.1Sthorpej
911.14Skochi	return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid);
921.1Sthorpej}
931.1Sthorpej
941.1Sthorpej/*
951.1Sthorpej * acpilid_attach:
961.1Sthorpej *
971.1Sthorpej *	Autoconfiguration `attach' routine.
981.1Sthorpej */
991.15Skochistatic void
1001.25Sxtraemeacpilid_attach(device_t parent, device_t self, void *aux)
1011.1Sthorpej{
1021.25Sxtraeme	struct acpilid_softc *sc = device_private(self);
1031.1Sthorpej	struct acpi_attach_args *aa = aux;
1041.1Sthorpej	ACPI_STATUS rv;
1051.1Sthorpej
1061.19Skochi	aprint_naive(": ACPI Lid Switch\n");
1071.19Skochi	aprint_normal(": ACPI Lid Switch\n");
1081.1Sthorpej
1091.1Sthorpej	sc->sc_node = aa->aa_node;
1101.1Sthorpej
1111.25Sxtraeme	sc->sc_smpsw.smpsw_name = device_xname(self);
1121.8Sthorpej	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
1131.7Sthorpej	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1141.25Sxtraeme		aprint_error_dev(self, "unable to register with sysmon\n");
1151.7Sthorpej		return;
1161.7Sthorpej	}
1171.7Sthorpej
1181.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1191.25Sxtraeme	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self);
1201.12Smycroft	if (ACPI_FAILURE(rv)) {
1211.25Sxtraeme		aprint_error_dev(self,
1221.25Sxtraeme		    "unable to register DEVICE NOTIFY handler: %s\n",
1231.25Sxtraeme		    AcpiFormatException(rv));
1241.1Sthorpej		return;
1251.1Sthorpej	}
1261.16Skochi
1271.22Sjmcneill	if (!pmf_device_register(self, acpilid_suspend, NULL))
1281.22Sjmcneill		aprint_error_dev(self, "couldn't establish power handler\n");
1291.22Sjmcneill}
1301.22Sjmcneill
1311.26Sdyoungstatic int
1321.26Sdyoungacpilid_detach(device_t self, int flags)
1331.26Sdyoung{
1341.26Sdyoung	struct acpilid_softc *sc = device_private(self);
1351.26Sdyoung	ACPI_STATUS rv;
1361.26Sdyoung
1371.26Sdyoung	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
1381.26Sdyoung	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler);
1391.26Sdyoung	if (ACPI_FAILURE(rv)) {
1401.26Sdyoung		aprint_error_dev(self,
1411.26Sdyoung		    "unable to deregister DEVICE NOTIFY handler: %s\n",
1421.26Sdyoung		    AcpiFormatException(rv));
1431.26Sdyoung		return EBUSY;
1441.26Sdyoung	}
1451.26Sdyoung
1461.26Sdyoung	pmf_device_deregister(self);
1471.26Sdyoung	sysmon_pswitch_unregister(&sc->sc_smpsw);
1481.26Sdyoung
1491.26Sdyoung	return 0;
1501.26Sdyoung}
1511.26Sdyoung
1521.22Sjmcneillstatic void
1531.25Sxtraemeacpilid_wake_event(device_t dv, bool enable)
1541.22Sjmcneill{
1551.25Sxtraeme	struct acpilid_softc *sc = device_private(dv);
1561.30Sjruoho	ACPI_OBJECT_LIST arg;
1571.30Sjruoho	ACPI_OBJECT obj[3];
1581.30Sjruoho	ACPI_STATUS rv;
1591.30Sjruoho
1601.30Sjruoho	/*
1611.30Sjruoho	 * First try to call the Device Sleep Wake control method, _DSW.
1621.30Sjruoho	 * Only if this is not available, resort to to the Power State
1631.30Sjruoho	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
1641.30Sjruoho	 */
1651.30Sjruoho	obj[0].Integer.Value = enable ? 1 : 0;
1661.30Sjruoho	obj[1].Integer.Value = obj[2].Integer.Value = 0;
1671.30Sjruoho	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
1681.30Sjruoho
1691.30Sjruoho	arg.Count = 3;
1701.30Sjruoho	arg.Pointer = obj;
1711.30Sjruoho
1721.30Sjruoho	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_DSW", &arg, NULL);
1731.30Sjruoho
1741.30Sjruoho	if (ACPI_SUCCESS(rv))
1751.30Sjruoho		return;
1761.25Sxtraeme
1771.30Sjruoho	if (rv != AE_NOT_FOUND)
1781.30Sjruoho		goto fail;
1791.22Sjmcneill
1801.29Scegger	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "_PSW",
1811.29Scegger	    enable ? 1 : 0);
1821.30Sjruoho
1831.22Sjmcneill	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
1841.30Sjruoho		goto fail;
1851.30Sjruoho
1861.30Sjruoho	return;
1871.30Sjruoho
1881.30Sjruohofail:
1891.30Sjruoho	aprint_error_dev(dv, "unable to evaluate wake control method: %s\n",
1901.30Sjruoho	    AcpiFormatException(rv));
1911.1Sthorpej}
1921.1Sthorpej
1931.1Sthorpej/*
1941.1Sthorpej * acpilid_status_changed:
1951.1Sthorpej *
1961.1Sthorpej *	Get, and possibly display, the lid status, and take action.
1971.1Sthorpej */
1981.15Skochistatic void
1991.1Sthorpejacpilid_status_changed(void *arg)
2001.1Sthorpej{
2011.1Sthorpej	struct acpilid_softc *sc = arg;
2021.13Skanaoka	ACPI_INTEGER status;
2031.12Smycroft	ACPI_STATUS rv;
2041.1Sthorpej
2051.12Smycroft	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
2061.12Smycroft	if (ACPI_FAILURE(rv))
2071.1Sthorpej		return;
2081.1Sthorpej
2091.7Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ?
2101.8Sthorpej	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
2111.1Sthorpej}
2121.1Sthorpej
2131.1Sthorpej/*
2141.1Sthorpej * acpilid_notify_handler:
2151.1Sthorpej *
2161.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
2171.1Sthorpej */
2181.15Skochistatic void
2191.25Sxtraemeacpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
2201.1Sthorpej{
2211.25Sxtraeme	device_t dv = context;
2221.25Sxtraeme	struct acpilid_softc *sc = device_private(dv);
2231.28Sjmcneill	ACPI_STATUS rv;
2241.1Sthorpej
2251.1Sthorpej	switch (notify) {
2261.1Sthorpej	case ACPI_NOTIFY_LidStatusChanged:
2271.1Sthorpej#ifdef ACPI_LID_DEBUG
2281.1Sthorpej		printf("%s: received LidStatusChanged message\n",
2291.25Sxtraeme		    device_xname(dv));
2301.1Sthorpej#endif
2311.22Sjmcneill		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
2321.1Sthorpej		    acpilid_status_changed, sc);
2331.12Smycroft		if (ACPI_FAILURE(rv))
2341.25Sxtraeme			aprint_error_dev(dv,
2351.25Sxtraeme			    "WARNING: unable to queue lid change "
2361.25Sxtraeme			    "callback: %s\n", AcpiFormatException(rv));
2371.1Sthorpej		break;
2381.1Sthorpej
2391.1Sthorpej	default:
2401.25Sxtraeme		aprint_debug_dev(dv,
2411.25Sxtraeme		    "received unknown notify message: 0x%x\n", notify);
2421.1Sthorpej	}
2431.1Sthorpej}
2441.22Sjmcneill
2451.22Sjmcneillstatic bool
2461.23Sdyoungacpilid_suspend(device_t dv PMF_FN_ARGS)
2471.22Sjmcneill{
2481.22Sjmcneill	struct acpilid_softc *sc = device_private(dv);
2491.22Sjmcneill	ACPI_INTEGER status;
2501.22Sjmcneill	ACPI_STATUS rv;
2511.22Sjmcneill
2521.22Sjmcneill	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
2531.22Sjmcneill	if (ACPI_FAILURE(rv))
2541.22Sjmcneill		return true;
2551.22Sjmcneill
2561.25Sxtraeme	acpilid_wake_event(dv, status == 0);
2571.22Sjmcneill	return true;
2581.22Sjmcneill}
259