acpi_lid.c revision 1.36
11.36Sjruoho/*	$NetBSD: acpi_lid.c,v 1.36 2010/03/05 14:00:16 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.36Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.36 2010/03/05 14:00:16 jruoho Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/device.h>
471.35Sjruoho#include <sys/module.h>
481.36Sjruoho#include <sys/systm.h>
491.1Sthorpej
501.1Sthorpej#include <dev/acpi/acpireg.h>
511.1Sthorpej#include <dev/acpi/acpivar.h>
521.1Sthorpej
531.34Sjruoho#define _COMPONENT		 ACPI_LID_COMPONENT
541.34SjruohoACPI_MODULE_NAME		 ("acpi_lid")
551.32Sjruoho
561.1Sthorpejstruct acpilid_softc {
571.34Sjruoho	struct acpi_devnode	*sc_node;
581.34Sjruoho	struct sysmon_pswitch	 sc_smpsw;
591.34Sjruoho	uint64_t		 sc_status;
601.1Sthorpej};
611.1Sthorpej
621.10Skochistatic const char * const lid_hid[] = {
631.10Skochi	"PNP0C0D",
641.10Skochi	NULL
651.10Skochi};
661.10Skochi
671.25Sxtraemestatic int	acpilid_match(device_t, cfdata_t, void *);
681.25Sxtraemestatic void	acpilid_attach(device_t, device_t, void *);
691.26Sdyoungstatic int	acpilid_detach(device_t, int);
701.15Skochistatic void	acpilid_status_changed(void *);
711.18Skochistatic void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *);
721.25Sxtraemestatic void	acpilid_wake_event(device_t, bool);
731.33Sdyoungstatic bool	acpilid_suspend(device_t, const pmf_qual_t *);
741.22Sjmcneill
751.34SjruohoCFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc),
761.34Sjruoho    acpilid_match, acpilid_attach, acpilid_detach, NULL);
771.34Sjruoho
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.34Sjruoho
1141.34Sjruoho	(void)sysmon_pswitch_register(&sc->sc_smpsw);
1151.34Sjruoho	(void)pmf_device_register(self, acpilid_suspend, NULL);
1161.7Sthorpej
1171.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1181.25Sxtraeme	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self);
1191.16Skochi
1201.34Sjruoho	if (ACPI_FAILURE(rv))
1211.34Sjruoho		aprint_error_dev(self, "failed to register notify handler\n");
1221.22Sjmcneill}
1231.22Sjmcneill
1241.26Sdyoungstatic int
1251.26Sdyoungacpilid_detach(device_t self, int flags)
1261.26Sdyoung{
1271.26Sdyoung	struct acpilid_softc *sc = device_private(self);
1281.26Sdyoung	ACPI_STATUS rv;
1291.26Sdyoung
1301.26Sdyoung	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
1311.26Sdyoung	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler);
1321.34Sjruoho
1331.34Sjruoho	if (ACPI_FAILURE(rv))
1341.26Sdyoung		return EBUSY;
1351.26Sdyoung
1361.26Sdyoung	pmf_device_deregister(self);
1371.26Sdyoung	sysmon_pswitch_unregister(&sc->sc_smpsw);
1381.26Sdyoung
1391.26Sdyoung	return 0;
1401.26Sdyoung}
1411.26Sdyoung
1421.22Sjmcneillstatic void
1431.25Sxtraemeacpilid_wake_event(device_t dv, bool enable)
1441.22Sjmcneill{
1451.25Sxtraeme	struct acpilid_softc *sc = device_private(dv);
1461.30Sjruoho	ACPI_OBJECT_LIST arg;
1471.30Sjruoho	ACPI_OBJECT obj[3];
1481.30Sjruoho	ACPI_STATUS rv;
1491.30Sjruoho
1501.30Sjruoho	/*
1511.30Sjruoho	 * First try to call the Device Sleep Wake control method, _DSW.
1521.30Sjruoho	 * Only if this is not available, resort to to the Power State
1531.30Sjruoho	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
1541.30Sjruoho	 */
1551.30Sjruoho	obj[0].Integer.Value = enable ? 1 : 0;
1561.30Sjruoho	obj[1].Integer.Value = obj[2].Integer.Value = 0;
1571.30Sjruoho	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
1581.30Sjruoho
1591.30Sjruoho	arg.Count = 3;
1601.30Sjruoho	arg.Pointer = obj;
1611.30Sjruoho
1621.30Sjruoho	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_DSW", &arg, NULL);
1631.30Sjruoho
1641.30Sjruoho	if (ACPI_SUCCESS(rv))
1651.30Sjruoho		return;
1661.25Sxtraeme
1671.30Sjruoho	if (rv != AE_NOT_FOUND)
1681.30Sjruoho		goto fail;
1691.22Sjmcneill
1701.29Scegger	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "_PSW",
1711.29Scegger	    enable ? 1 : 0);
1721.30Sjruoho
1731.22Sjmcneill	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
1741.30Sjruoho		goto fail;
1751.30Sjruoho
1761.30Sjruoho	return;
1771.30Sjruoho
1781.30Sjruohofail:
1791.30Sjruoho	aprint_error_dev(dv, "unable to evaluate wake control method: %s\n",
1801.30Sjruoho	    AcpiFormatException(rv));
1811.1Sthorpej}
1821.1Sthorpej
1831.1Sthorpej/*
1841.1Sthorpej * acpilid_status_changed:
1851.1Sthorpej *
1861.1Sthorpej *	Get, and possibly display, the lid status, and take action.
1871.1Sthorpej */
1881.15Skochistatic void
1891.1Sthorpejacpilid_status_changed(void *arg)
1901.1Sthorpej{
1911.34Sjruoho	device_t dv = arg;
1921.34Sjruoho	struct acpilid_softc *sc = device_private(dv);
1931.12Smycroft	ACPI_STATUS rv;
1941.1Sthorpej
1951.34Sjruoho	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &sc->sc_status);
1961.34Sjruoho
1971.12Smycroft	if (ACPI_FAILURE(rv))
1981.1Sthorpej		return;
1991.1Sthorpej
2001.34Sjruoho	sysmon_pswitch_event(&sc->sc_smpsw, (sc->sc_status == 0) ?
2011.8Sthorpej	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
2021.1Sthorpej}
2031.1Sthorpej
2041.1Sthorpej/*
2051.1Sthorpej * acpilid_notify_handler:
2061.1Sthorpej *
2071.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
2081.1Sthorpej */
2091.15Skochistatic void
2101.34Sjruohoacpilid_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
2111.1Sthorpej{
2121.34Sjruoho	static const int handler = OSL_NOTIFY_HANDLER;
2131.25Sxtraeme	device_t dv = context;
2141.1Sthorpej
2151.1Sthorpej	switch (notify) {
2161.34Sjruoho
2171.1Sthorpej	case ACPI_NOTIFY_LidStatusChanged:
2181.34Sjruoho		(void)AcpiOsExecute(handler, acpilid_status_changed, dv);
2191.1Sthorpej		break;
2201.1Sthorpej
2211.1Sthorpej	default:
2221.34Sjruoho		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
2231.1Sthorpej	}
2241.1Sthorpej}
2251.22Sjmcneill
2261.22Sjmcneillstatic bool
2271.33Sdyoungacpilid_suspend(device_t dv, const pmf_qual_t *qual)
2281.22Sjmcneill{
2291.22Sjmcneill	struct acpilid_softc *sc = device_private(dv);
2301.22Sjmcneill
2311.34Sjruoho	acpilid_wake_event(dv, sc->sc_status == 0);
2321.22Sjmcneill
2331.22Sjmcneill	return true;
2341.22Sjmcneill}
2351.35Sjruoho
2361.35Sjruoho#ifdef _MODULE
2371.35Sjruoho
2381.35SjruohoMODULE(MODULE_CLASS_DRIVER, acpilid, NULL);
2391.35SjruohoCFDRIVER_DECL(acpilid, DV_DULL, NULL);
2401.35Sjruoho
2411.35Sjruohostatic int acpilidloc[] = { -1 };
2421.35Sjruohoextern struct cfattach acpilid_ca;
2431.35Sjruoho
2441.35Sjruohostatic struct cfparent acpiparent = {
2451.35Sjruoho	"acpinodebus", NULL, DVUNIT_ANY
2461.35Sjruoho};
2471.35Sjruoho
2481.35Sjruohostatic struct cfdata acpilid_cfdata[] = {
2491.35Sjruoho	{
2501.35Sjruoho		.cf_name = "acpilid",
2511.35Sjruoho		.cf_atname = "acpilid",
2521.35Sjruoho		.cf_unit = 0,
2531.35Sjruoho		.cf_fstate = FSTATE_STAR,
2541.35Sjruoho		.cf_loc = acpilidloc,
2551.35Sjruoho		.cf_flags = 0,
2561.35Sjruoho		.cf_pspec = &acpiparent,
2571.35Sjruoho	},
2581.35Sjruoho
2591.35Sjruoho	{ NULL }
2601.35Sjruoho};
2611.35Sjruoho
2621.35Sjruohostatic int
2631.35Sjruohoacpilid_modcmd(modcmd_t cmd, void *context)
2641.35Sjruoho{
2651.35Sjruoho	int err;
2661.35Sjruoho
2671.35Sjruoho	switch (cmd) {
2681.35Sjruoho
2691.35Sjruoho	case MODULE_CMD_INIT:
2701.35Sjruoho
2711.35Sjruoho		err = config_cfdriver_attach(&acpilid_cd);
2721.35Sjruoho
2731.35Sjruoho		if (err != 0)
2741.35Sjruoho			return err;
2751.35Sjruoho
2761.35Sjruoho		err = config_cfattach_attach("acpilid", &acpilid_ca);
2771.35Sjruoho
2781.35Sjruoho		if (err != 0) {
2791.35Sjruoho			config_cfdriver_detach(&acpilid_cd);
2801.35Sjruoho			return err;
2811.35Sjruoho		}
2821.35Sjruoho
2831.35Sjruoho		err = config_cfdata_attach(acpilid_cfdata, 1);
2841.35Sjruoho
2851.35Sjruoho		if (err != 0) {
2861.35Sjruoho			config_cfattach_detach("acpilid", &acpilid_ca);
2871.35Sjruoho			config_cfdriver_detach(&acpilid_cd);
2881.35Sjruoho			return err;
2891.35Sjruoho		}
2901.35Sjruoho
2911.35Sjruoho		return 0;
2921.35Sjruoho
2931.35Sjruoho	case MODULE_CMD_FINI:
2941.35Sjruoho
2951.35Sjruoho		err = config_cfdata_detach(acpilid_cfdata);
2961.35Sjruoho
2971.35Sjruoho		if (err != 0)
2981.35Sjruoho			return err;
2991.35Sjruoho
3001.35Sjruoho		config_cfattach_detach("acpilid", &acpilid_ca);
3011.35Sjruoho		config_cfdriver_detach(&acpilid_cd);
3021.35Sjruoho
3031.35Sjruoho		return 0;
3041.35Sjruoho
3051.35Sjruoho	default:
3061.35Sjruoho		return ENOTTY;
3071.35Sjruoho	}
3081.35Sjruoho}
3091.35Sjruoho
3101.35Sjruoho#endif	/* _MODULE */
311