acpi_button.c revision 1.34
11.34Sjruoho/*	$NetBSD: acpi_button.c,v 1.34 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 Button driver.
401.1Sthorpej */
411.2Slukem
421.2Slukem#include <sys/cdefs.h>
431.34Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.34 2010/03/05 14:00:16 jruoho 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.1Sthorpej
531.32Sjruoho#define _COMPONENT		 ACPI_BUTTON_COMPONENT
541.32SjruohoACPI_MODULE_NAME		 ("acpi_button")
551.29Sjruoho
561.1Sthorpejstruct acpibut_softc {
571.32Sjruoho	struct acpi_devnode	*sc_node;
581.32Sjruoho	struct sysmon_pswitch	 sc_smpsw;
591.1Sthorpej};
601.1Sthorpej
611.15Skochistatic const char * const power_button_hid[] = {
621.11Skochi	"PNP0C0C",
631.15Skochi	NULL
641.15Skochi};
651.15Skochi
661.15Skochistatic const char * const sleep_button_hid[] = {
671.11Skochi	"PNP0C0E",
681.11Skochi	NULL
691.11Skochi};
701.11Skochi
711.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
721.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
731.30Sjruohostatic int	acpibut_detach(device_t, int);
741.30Sjruohostatic void	acpibut_pressed_event(void *);
751.32Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
761.1Sthorpej
771.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
781.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
791.1Sthorpej
801.1Sthorpej/*
811.1Sthorpej * acpibut_match:
821.1Sthorpej *
831.1Sthorpej *	Autoconfiguration `match' routine.
841.1Sthorpej */
851.16Skochistatic int
861.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
871.1Sthorpej{
881.1Sthorpej	struct acpi_attach_args *aa = aux;
891.1Sthorpej
901.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
911.14Skochi		return 0;
921.1Sthorpej
931.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
941.15Skochi		return 1;
951.15Skochi
961.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
971.15Skochi		return 1;
981.15Skochi
991.15Skochi	return 0;
1001.1Sthorpej}
1011.1Sthorpej
1021.1Sthorpej/*
1031.1Sthorpej * acpibut_attach:
1041.1Sthorpej *
1051.1Sthorpej *	Autoconfiguration `attach' routine.
1061.1Sthorpej */
1071.16Skochistatic void
1081.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1091.1Sthorpej{
1101.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1111.1Sthorpej	struct acpi_attach_args *aa = aux;
1121.1Sthorpej	const char *desc;
1131.1Sthorpej	ACPI_STATUS rv;
1141.1Sthorpej
1151.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1161.7Sthorpej
1171.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1181.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1191.1Sthorpej		desc = "Power";
1201.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1211.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1221.1Sthorpej		desc = "Sleep";
1231.32Sjruoho	} else
1241.32Sjruoho		panic("%s: impossible", __func__);
1251.1Sthorpej
1261.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1271.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1281.1Sthorpej
1291.1Sthorpej	sc->sc_node = aa->aa_node;
1301.1Sthorpej
1311.33Sjruoho	(void)pmf_device_register(self, NULL, NULL);
1321.32Sjruoho	(void)sysmon_pswitch_register(&sc->sc_smpsw);
1331.7Sthorpej
1341.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1351.23Sjoerg	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
1361.1Sthorpej
1371.32Sjruoho	if (ACPI_FAILURE(rv))
1381.32Sjruoho		aprint_error_dev(self, "failed to install notify handler\n");
1391.1Sthorpej}
1401.1Sthorpej
1411.1Sthorpej/*
1421.30Sjruoho * acpibut_detach:
1431.30Sjruoho *
1441.30Sjruoho *	Autoconfiguration `detach' routine.
1451.30Sjruoho */
1461.30Sjruohostatic int
1471.30Sjruohoacpibut_detach(device_t self, int flags)
1481.30Sjruoho{
1491.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1501.30Sjruoho	ACPI_STATUS rv;
1511.30Sjruoho
1521.30Sjruoho	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
1531.30Sjruoho	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler);
1541.30Sjruoho
1551.30Sjruoho	if (ACPI_FAILURE(rv))
1561.30Sjruoho		return EBUSY;
1571.30Sjruoho
1581.30Sjruoho	pmf_device_deregister(self);
1591.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1601.30Sjruoho
1611.30Sjruoho	return 0;
1621.30Sjruoho}
1631.30Sjruoho
1641.30Sjruoho/*
1651.7Sthorpej * acpibut_pressed_event:
1661.1Sthorpej *
1671.7Sthorpej *	Deal with a button being pressed.
1681.1Sthorpej */
1691.16Skochistatic void
1701.7Sthorpejacpibut_pressed_event(void *arg)
1711.1Sthorpej{
1721.23Sjoerg	device_t dv = arg;
1731.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1741.1Sthorpej
1751.32Sjruoho	aprint_debug_dev(dv, "button pressed\n");
1761.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1771.1Sthorpej}
1781.1Sthorpej
1791.1Sthorpej/*
1801.1Sthorpej * acpibut_notify_handler:
1811.1Sthorpej *
1821.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1831.1Sthorpej */
1841.16Skochistatic void
1851.32Sjruohoacpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
1861.1Sthorpej{
1871.32Sjruoho	static const int handler = OSL_NOTIFY_HANDLER;
1881.23Sjoerg	device_t dv = context;
1891.1Sthorpej
1901.1Sthorpej	switch (notify) {
1911.32Sjruoho
1921.32Sjruoho     /* case ACPI_NOTIFY_S0SleepButtonPressed: */
1931.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
1941.32Sjruoho		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
1951.1Sthorpej		break;
1961.1Sthorpej
1971.1Sthorpej	default:
1981.32Sjruoho		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
1991.1Sthorpej	}
2001.1Sthorpej}
2011.31Sjruoho
2021.31Sjruoho#ifdef _MODULE
2031.31Sjruoho
2041.31SjruohoMODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
2051.31SjruohoCFDRIVER_DECL(acpibut, DV_DULL, NULL);
2061.31Sjruoho
2071.31Sjruohostatic int acpibutloc[] = { -1 };
2081.31Sjruohoextern struct cfattach acpibut_ca;
2091.31Sjruoho
2101.31Sjruohostatic struct cfparent acpiparent = {
2111.31Sjruoho	"acpinodebus", NULL, DVUNIT_ANY
2121.31Sjruoho};
2131.31Sjruoho
2141.31Sjruohostatic struct cfdata acpibut_cfdata[] = {
2151.31Sjruoho	{
2161.31Sjruoho		.cf_name = "acpibut",
2171.31Sjruoho		.cf_atname = "acpibut",
2181.31Sjruoho		.cf_unit = 0,
2191.31Sjruoho		.cf_fstate = FSTATE_STAR,
2201.31Sjruoho		.cf_loc = acpibutloc,
2211.31Sjruoho		.cf_flags = 0,
2221.31Sjruoho		.cf_pspec = &acpiparent,
2231.31Sjruoho	},
2241.31Sjruoho
2251.31Sjruoho	{ NULL }
2261.31Sjruoho};
2271.31Sjruoho
2281.31Sjruohostatic int
2291.31Sjruohoacpibut_modcmd(modcmd_t cmd, void *context)
2301.31Sjruoho{
2311.31Sjruoho	int err;
2321.31Sjruoho
2331.31Sjruoho	switch (cmd) {
2341.31Sjruoho
2351.31Sjruoho	case MODULE_CMD_INIT:
2361.31Sjruoho
2371.31Sjruoho		err = config_cfdriver_attach(&acpibut_cd);
2381.31Sjruoho
2391.31Sjruoho		if (err != 0)
2401.31Sjruoho			return err;
2411.31Sjruoho
2421.31Sjruoho		err = config_cfattach_attach("acpibut", &acpibut_ca);
2431.31Sjruoho
2441.31Sjruoho		if (err != 0) {
2451.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2461.31Sjruoho			return err;
2471.31Sjruoho		}
2481.31Sjruoho
2491.31Sjruoho		err = config_cfdata_attach(acpibut_cfdata, 1);
2501.31Sjruoho
2511.31Sjruoho		if (err != 0) {
2521.31Sjruoho			config_cfattach_detach("acpibut", &acpibut_ca);
2531.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2541.31Sjruoho			return err;
2551.31Sjruoho		}
2561.31Sjruoho
2571.31Sjruoho		return 0;
2581.31Sjruoho
2591.31Sjruoho	case MODULE_CMD_FINI:
2601.31Sjruoho
2611.31Sjruoho		err = config_cfdata_detach(acpibut_cfdata);
2621.31Sjruoho
2631.31Sjruoho		if (err != 0)
2641.31Sjruoho			return err;
2651.31Sjruoho
2661.31Sjruoho		config_cfattach_detach("acpibut", &acpibut_ca);
2671.31Sjruoho		config_cfdriver_detach(&acpibut_cd);
2681.31Sjruoho
2691.31Sjruoho		return 0;
2701.31Sjruoho
2711.31Sjruoho	default:
2721.31Sjruoho		return ENOTTY;
2731.31Sjruoho	}
2741.31Sjruoho}
2751.31Sjruoho
2761.31Sjruoho#endif	/* _MODULE */
277