acpi_button.c revision 1.43
11.43Sthorpej/*	$NetBSD: acpi_button.c,v 1.43 2021/01/29 15:24:00 thorpej 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.43Sthorpej__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.43 2021/01/29 15:24:00 thorpej 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.43Sthorpejstruct button_type {
651.43Sthorpej	const char *desc;
661.43Sthorpej	int type;
671.43Sthorpej};
681.43Sthorpej
691.43Sthorpejstatic const struct button_type power_button_type = {
701.43Sthorpej	.desc = "Power",
711.43Sthorpej	.type = PSWITCH_TYPE_POWER
721.15Skochi};
731.15Skochi
741.43Sthorpejstatic const struct button_type sleep_button_type = {
751.43Sthorpej	.desc = "Sleep",
761.43Sthorpej	.type = PSWITCH_TYPE_SLEEP
771.43Sthorpej};
781.43Sthorpej
791.43Sthorpejstatic const struct device_compatible_entry compat_data[] = {
801.43Sthorpej	{ .compat = "PNP0C0C",	.data = &power_button_type },
811.43Sthorpej	{ .compat = "PNP0C0E",	.data = &sleep_button_type },
821.43Sthorpej	DEVICE_COMPAT_EOL
831.11Skochi};
841.11Skochi
851.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
861.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
871.30Sjruohostatic int	acpibut_detach(device_t, int);
881.30Sjruohostatic void	acpibut_pressed_event(void *);
891.32Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
901.1Sthorpej
911.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
921.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
931.1Sthorpej
941.1Sthorpej/*
951.1Sthorpej * acpibut_match:
961.1Sthorpej *
971.1Sthorpej *	Autoconfiguration `match' routine.
981.1Sthorpej */
991.16Skochistatic int
1001.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
1011.1Sthorpej{
1021.1Sthorpej	struct acpi_attach_args *aa = aux;
1031.1Sthorpej
1041.43Sthorpej	return acpi_compatible_match(aa, compat_data);
1051.1Sthorpej}
1061.1Sthorpej
1071.1Sthorpej/*
1081.1Sthorpej * acpibut_attach:
1091.1Sthorpej *
1101.1Sthorpej *	Autoconfiguration `attach' routine.
1111.1Sthorpej */
1121.16Skochistatic void
1131.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1141.1Sthorpej{
1151.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1161.1Sthorpej	struct acpi_attach_args *aa = aux;
1171.43Sthorpej	const struct device_compatible_entry *dce;
1181.43Sthorpej	const struct button_type *type;
1191.41Sjruoho	struct acpi_wakedev *aw;
1201.1Sthorpej
1211.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1221.7Sthorpej
1231.43Sthorpej	dce = acpi_compatible_lookup(aa, compat_data);
1241.43Sthorpej	KASSERT(dce != NULL);
1251.43Sthorpej	type = dce->data;
1261.43Sthorpej
1271.43Sthorpej	sc->sc_smpsw.smpsw_type = type->type;
1281.1Sthorpej
1291.43Sthorpej	aprint_naive(": ACPI %s Button\n", type->desc);
1301.43Sthorpej	aprint_normal(": ACPI %s Button\n", type->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