acpi_button.c revision 1.42
11.42Spgoyette/*	$NetBSD: acpi_button.c,v 1.42 2015/04/23 23:23:00 pgoyette 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 Button driver.
401.1Sthorpej */
411.2Slukem
421.2Slukem#include <sys/cdefs.h>
431.42Spgoyette__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.42 2015/04/23 23:23:00 pgoyette Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/device.h>
471.31Sjruoho#include <sys/module.h>
481.34Sjruoho#include <sys/systm.h>
491.1Sthorpej
501.1Sthorpej#include <dev/acpi/acpireg.h>
511.1Sthorpej#include <dev/acpi/acpivar.h>
521.41Sjruoho#include <dev/acpi/acpi_wakedev.h>
531.1Sthorpej
541.32Sjruoho#define _COMPONENT		 ACPI_BUTTON_COMPONENT
551.32SjruohoACPI_MODULE_NAME		 ("acpi_button")
561.29Sjruoho
571.36Sjruoho#define ACPI_NOTIFY_BUTTON	 0x80
581.36Sjruoho
591.1Sthorpejstruct acpibut_softc {
601.32Sjruoho	struct acpi_devnode	*sc_node;
611.32Sjruoho	struct sysmon_pswitch	 sc_smpsw;
621.1Sthorpej};
631.1Sthorpej
641.15Skochistatic const char * const power_button_hid[] = {
651.11Skochi	"PNP0C0C",
661.15Skochi	NULL
671.15Skochi};
681.15Skochi
691.15Skochistatic const char * const sleep_button_hid[] = {
701.11Skochi	"PNP0C0E",
711.11Skochi	NULL
721.11Skochi};
731.11Skochi
741.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
751.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
761.30Sjruohostatic int	acpibut_detach(device_t, int);
771.30Sjruohostatic void	acpibut_pressed_event(void *);
781.32Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
791.1Sthorpej
801.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
811.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
821.1Sthorpej
831.1Sthorpej/*
841.1Sthorpej * acpibut_match:
851.1Sthorpej *
861.1Sthorpej *	Autoconfiguration `match' routine.
871.1Sthorpej */
881.16Skochistatic int
891.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
901.1Sthorpej{
911.1Sthorpej	struct acpi_attach_args *aa = aux;
921.1Sthorpej
931.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
941.14Skochi		return 0;
951.1Sthorpej
961.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
971.15Skochi		return 1;
981.15Skochi
991.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
1001.15Skochi		return 1;
1011.15Skochi
1021.15Skochi	return 0;
1031.1Sthorpej}
1041.1Sthorpej
1051.1Sthorpej/*
1061.1Sthorpej * acpibut_attach:
1071.1Sthorpej *
1081.1Sthorpej *	Autoconfiguration `attach' routine.
1091.1Sthorpej */
1101.16Skochistatic void
1111.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1121.1Sthorpej{
1131.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1141.1Sthorpej	struct acpi_attach_args *aa = aux;
1151.41Sjruoho	struct acpi_wakedev *aw;
1161.1Sthorpej	const char *desc;
1171.1Sthorpej
1181.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1191.7Sthorpej
1201.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1211.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1221.1Sthorpej		desc = "Power";
1231.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1241.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1251.1Sthorpej		desc = "Sleep";
1261.32Sjruoho	} else
1271.32Sjruoho		panic("%s: impossible", __func__);
1281.1Sthorpej
1291.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1301.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1311.1Sthorpej
1321.1Sthorpej	sc->sc_node = aa->aa_node;
1331.41Sjruoho	aw = sc->sc_node->ad_wakedev;
1341.41Sjruoho
1351.41Sjruoho	/*
1361.41Sjruoho	 * This GPE should always be enabled.
1371.41Sjruoho	 */
1381.41Sjruoho	if (aw != NULL)
1391.41Sjruoho		(void)AcpiEnableGpe(aw->aw_handle, aw->aw_number);
1401.1Sthorpej
1411.33Sjruoho	(void)pmf_device_register(self, NULL, NULL);
1421.32Sjruoho	(void)sysmon_pswitch_register(&sc->sc_smpsw);
1431.35Sjruoho	(void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
1441.1Sthorpej}
1451.1Sthorpej
1461.1Sthorpej/*
1471.30Sjruoho * acpibut_detach:
1481.30Sjruoho *
1491.30Sjruoho *	Autoconfiguration `detach' routine.
1501.30Sjruoho */
1511.30Sjruohostatic int
1521.30Sjruohoacpibut_detach(device_t self, int flags)
1531.30Sjruoho{
1541.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1551.30Sjruoho
1561.30Sjruoho	pmf_device_deregister(self);
1571.35Sjruoho	acpi_deregister_notify(sc->sc_node);
1581.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1591.30Sjruoho
1601.30Sjruoho	return 0;
1611.30Sjruoho}
1621.30Sjruoho
1631.30Sjruoho/*
1641.7Sthorpej * acpibut_pressed_event:
1651.1Sthorpej *
1661.7Sthorpej *	Deal with a button being pressed.
1671.1Sthorpej */
1681.16Skochistatic void
1691.7Sthorpejacpibut_pressed_event(void *arg)
1701.1Sthorpej{
1711.23Sjoerg	device_t dv = arg;
1721.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1731.1Sthorpej
1741.32Sjruoho	aprint_debug_dev(dv, "button pressed\n");
1751.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1761.1Sthorpej}
1771.1Sthorpej
1781.1Sthorpej/*
1791.1Sthorpej * acpibut_notify_handler:
1801.1Sthorpej *
1811.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1821.1Sthorpej */
1831.16Skochistatic void
1841.32Sjruohoacpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
1851.1Sthorpej{
1861.32Sjruoho	static const int handler = OSL_NOTIFY_HANDLER;
1871.23Sjoerg	device_t dv = context;
1881.1Sthorpej
1891.1Sthorpej	switch (notify) {
1901.32Sjruoho
1911.36Sjruoho	case ACPI_NOTIFY_BUTTON:
1921.32Sjruoho		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
1931.1Sthorpej		break;
1941.1Sthorpej
1951.37Sjmcneill	case ACPI_NOTIFY_DEVICE_WAKE:
1961.37Sjmcneill		break;
1971.37Sjmcneill
1981.1Sthorpej	default:
1991.38Sjruoho		aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
2001.1Sthorpej	}
2011.1Sthorpej}
2021.31Sjruoho
2031.42SpgoyetteMODULE(MODULE_CLASS_DRIVER, acpibut, "sysmon_power");
2041.31Sjruoho
2051.40Sjruoho#ifdef _MODULE
2061.40Sjruoho#include "ioconf.c"
2071.40Sjruoho#endif
2081.31Sjruoho
2091.31Sjruohostatic int
2101.40Sjruohoacpibut_modcmd(modcmd_t cmd, void *aux)
2111.31Sjruoho{
2121.40Sjruoho	int rv = 0;
2131.31Sjruoho
2141.31Sjruoho	switch (cmd) {
2151.31Sjruoho
2161.31Sjruoho	case MODULE_CMD_INIT:
2171.31Sjruoho
2181.40Sjruoho#ifdef _MODULE
2191.40Sjruoho		rv = config_init_component(cfdriver_ioconf_acpibut,
2201.40Sjruoho		    cfattach_ioconf_acpibut, cfdata_ioconf_acpibut);
2211.40Sjruoho#endif
2221.40Sjruoho		break;
2231.31Sjruoho
2241.31Sjruoho	case MODULE_CMD_FINI:
2251.31Sjruoho
2261.40Sjruoho#ifdef _MODULE
2271.40Sjruoho		rv = config_fini_component(cfdriver_ioconf_acpibut,
2281.40Sjruoho		    cfattach_ioconf_acpibut, cfdata_ioconf_acpibut);
2291.40Sjruoho#endif
2301.40Sjruoho		break;
2311.31Sjruoho
2321.31Sjruoho	default:
2331.40Sjruoho		rv = ENOTTY;
2341.31Sjruoho	}
2351.40Sjruoho
2361.40Sjruoho	return rv;
2371.31Sjruoho}
238