acpi_button.c revision 1.29
11.29Sjruoho/*	$NetBSD: acpi_button.c,v 1.29 2010/01/30 18:35:48 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.29Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.29 2010/01/30 18:35:48 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.1Sthorpej
791.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
801.6Sthorpej    acpibut_match, acpibut_attach, NULL, NULL);
811.1Sthorpej
821.16Skochistatic void	acpibut_pressed_event(void *);
831.19Skochistatic void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
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.7Sthorpej * acpibut_pressed_event:
1621.1Sthorpej *
1631.7Sthorpej *	Deal with a button being pressed.
1641.1Sthorpej */
1651.16Skochistatic void
1661.7Sthorpejacpibut_pressed_event(void *arg)
1671.1Sthorpej{
1681.23Sjoerg	device_t dv = arg;
1691.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1701.1Sthorpej
1711.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1721.23Sjoerg		aprint_verbose_dev(dv, "button pressed\n");
1731.1Sthorpej
1741.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1751.1Sthorpej}
1761.1Sthorpej
1771.1Sthorpej/*
1781.1Sthorpej * acpibut_notify_handler:
1791.1Sthorpej *
1801.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1811.1Sthorpej */
1821.16Skochistatic void
1831.23Sjoergacpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
1841.1Sthorpej{
1851.23Sjoerg	device_t dv = context;
1861.28Sjmcneill	ACPI_STATUS rv;
1871.1Sthorpej
1881.1Sthorpej	switch (notify) {
1891.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
1901.1Sthorpej#if 0
1911.1Sthorpej	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
1921.1Sthorpej#endif
1931.1Sthorpej#ifdef ACPI_BUT_DEBUG
1941.23Sjoerg		aprint_debug_dev(dv, "received ButtonPressed message\n");
1951.1Sthorpej#endif
1961.25Sjmcneill		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
1971.24Sjoerg		    acpibut_pressed_event, dv);
1981.13Smycroft		if (ACPI_FAILURE(rv))
1991.23Sjoerg			aprint_error_dev(dv,
2001.23Sjoerg			    "WARNING: unable to queue button pressed callback: %s\n",
2011.12Smycroft			    AcpiFormatException(rv));
2021.1Sthorpej		break;
2031.1Sthorpej
2041.1Sthorpej	/* XXX ACPI_NOTIFY_DeviceWake?? */
2051.1Sthorpej
2061.1Sthorpej	default:
2071.23Sjoerg		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
2081.23Sjoerg		    notify);
2091.1Sthorpej	}
2101.1Sthorpej}
211