acpi_button.c revision 1.30
11.30Sjruoho/*	$NetBSD: acpi_button.c,v 1.30 2010/01/31 06:10:53 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.30Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.30 2010/01/31 06:10:53 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.29Sjruoho#define _COMPONENT		ACPI_BUTTON_COMPONENT
561.29SjruohoACPI_MODULE_NAME		("acpi_button")
571.29Sjruoho
581.1Sthorpejstruct acpibut_softc {
591.1Sthorpej	struct acpi_devnode *sc_node;	/* our ACPI devnode */
601.7Sthorpej	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
611.1Sthorpej	int sc_flags;			/* see below */
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.1Sthorpej#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
751.1Sthorpej
761.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
771.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
781.30Sjruohostatic int	acpibut_detach(device_t, int);
791.30Sjruohostatic void	acpibut_pressed_event(void *);
801.30Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
811.1Sthorpej
821.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
831.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
841.1Sthorpej
851.1Sthorpej/*
861.1Sthorpej * acpibut_match:
871.1Sthorpej *
881.1Sthorpej *	Autoconfiguration `match' routine.
891.1Sthorpej */
901.16Skochistatic int
911.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
921.1Sthorpej{
931.1Sthorpej	struct acpi_attach_args *aa = aux;
941.1Sthorpej
951.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
961.14Skochi		return 0;
971.1Sthorpej
981.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
991.15Skochi		return 1;
1001.15Skochi
1011.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
1021.15Skochi		return 1;
1031.15Skochi
1041.15Skochi	return 0;
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.1Sthorpej	const char *desc;
1181.1Sthorpej	ACPI_STATUS rv;
1191.1Sthorpej
1201.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1211.7Sthorpej
1221.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1231.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1241.1Sthorpej		desc = "Power";
1251.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1261.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1271.1Sthorpej		desc = "Sleep";
1281.1Sthorpej	} else {
1291.1Sthorpej		panic("acpibut_attach: impossible");
1301.1Sthorpej	}
1311.1Sthorpej
1321.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1331.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1341.1Sthorpej
1351.1Sthorpej	sc->sc_node = aa->aa_node;
1361.1Sthorpej
1371.7Sthorpej	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1381.23Sjoerg		aprint_error_dev(self, "unable to register with sysmon\n");
1391.7Sthorpej		return;
1401.7Sthorpej	}
1411.7Sthorpej
1421.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1431.23Sjoerg	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
1441.13Smycroft	if (ACPI_FAILURE(rv)) {
1451.23Sjoerg		aprint_error_dev(self,
1461.23Sjoerg		    "unable to register DEVICE NOTIFY handler: %s\n",
1471.23Sjoerg		    AcpiFormatException(rv));
1481.1Sthorpej		return;
1491.1Sthorpej	}
1501.1Sthorpej
1511.1Sthorpej#ifdef ACPI_BUT_DEBUG
1521.1Sthorpej	/* Display the current state when it changes. */
1531.1Sthorpej	sc->sc_flags = ACPIBUT_F_VERBOSE;
1541.1Sthorpej#endif
1551.25Sjmcneill
1561.25Sjmcneill	if (!pmf_device_register(self, NULL, NULL))
1571.25Sjmcneill		aprint_error_dev(self, "couldn't establish power handler\n");
1581.1Sthorpej}
1591.1Sthorpej
1601.1Sthorpej/*
1611.30Sjruoho * acpibut_detach:
1621.30Sjruoho *
1631.30Sjruoho *	Autoconfiguration `detach' routine.
1641.30Sjruoho */
1651.30Sjruohostatic int
1661.30Sjruohoacpibut_detach(device_t self, int flags)
1671.30Sjruoho{
1681.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1691.30Sjruoho	ACPI_STATUS rv;
1701.30Sjruoho
1711.30Sjruoho	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
1721.30Sjruoho	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler);
1731.30Sjruoho
1741.30Sjruoho	if (ACPI_FAILURE(rv))
1751.30Sjruoho		return EBUSY;
1761.30Sjruoho
1771.30Sjruoho	pmf_device_deregister(self);
1781.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1791.30Sjruoho
1801.30Sjruoho	return 0;
1811.30Sjruoho}
1821.30Sjruoho
1831.30Sjruoho/*
1841.7Sthorpej * acpibut_pressed_event:
1851.1Sthorpej *
1861.7Sthorpej *	Deal with a button being pressed.
1871.1Sthorpej */
1881.16Skochistatic void
1891.7Sthorpejacpibut_pressed_event(void *arg)
1901.1Sthorpej{
1911.23Sjoerg	device_t dv = arg;
1921.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1931.1Sthorpej
1941.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1951.23Sjoerg		aprint_verbose_dev(dv, "button pressed\n");
1961.1Sthorpej
1971.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1981.1Sthorpej}
1991.1Sthorpej
2001.1Sthorpej/*
2011.1Sthorpej * acpibut_notify_handler:
2021.1Sthorpej *
2031.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
2041.1Sthorpej */
2051.16Skochistatic void
2061.23Sjoergacpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
2071.1Sthorpej{
2081.23Sjoerg	device_t dv = context;
2091.28Sjmcneill	ACPI_STATUS rv;
2101.1Sthorpej
2111.1Sthorpej	switch (notify) {
2121.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
2131.1Sthorpej#if 0
2141.1Sthorpej	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
2151.1Sthorpej#endif
2161.1Sthorpej#ifdef ACPI_BUT_DEBUG
2171.23Sjoerg		aprint_debug_dev(dv, "received ButtonPressed message\n");
2181.1Sthorpej#endif
2191.25Sjmcneill		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
2201.24Sjoerg		    acpibut_pressed_event, dv);
2211.13Smycroft		if (ACPI_FAILURE(rv))
2221.23Sjoerg			aprint_error_dev(dv,
2231.23Sjoerg			    "WARNING: unable to queue button pressed callback: %s\n",
2241.12Smycroft			    AcpiFormatException(rv));
2251.1Sthorpej		break;
2261.1Sthorpej
2271.1Sthorpej	/* XXX ACPI_NOTIFY_DeviceWake?? */
2281.1Sthorpej
2291.1Sthorpej	default:
2301.23Sjoerg		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
2311.23Sjoerg		    notify);
2321.1Sthorpej	}
2331.1Sthorpej}
234