acpi_button.c revision 1.2
11.2Slukem/*	$NetBSD: acpi_button.c,v 1.2 2001/11/13 13:01:57 lukem Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.1Sthorpej * Copyright 2001 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.2Slukem__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.2 2001/11/13 13:01:57 lukem 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.1Sthorpej#define	ACPI_BUT_DEBUG
541.1Sthorpej
551.1Sthorpejstruct acpibut_softc {
561.1Sthorpej	struct device sc_dev;		/* base device glue */
571.1Sthorpej	struct acpi_devnode *sc_node;	/* our ACPI devnode */
581.1Sthorpej	int sc_buttype;			/* button type */
591.1Sthorpej	int sc_flags;			/* see below */
601.1Sthorpej};
611.1Sthorpej
621.1Sthorpej#define	ACPIBUT_TYPE_POWER		0
631.1Sthorpej#define	ACPIBUT_TYPE_SLEEP		1
641.1Sthorpej
651.1Sthorpej#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
661.1Sthorpej
671.1Sthorpejint	acpibut_match(struct device *, struct cfdata *, void *);
681.1Sthorpejvoid	acpibut_attach(struct device *, struct device *, void *);
691.1Sthorpej
701.1Sthorpejstruct cfattach acpibut_ca = {
711.1Sthorpej	sizeof(struct acpibut_softc), acpibut_match, acpibut_attach,
721.1Sthorpej};
731.1Sthorpej
741.1Sthorpejvoid	acpibut_pressed_for_sleep(void *);
751.1Sthorpejvoid	acpibut_pressed_for_wakeup(void *);
761.1Sthorpejvoid	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
771.1Sthorpej
781.1Sthorpej/*
791.1Sthorpej * acpibut_match:
801.1Sthorpej *
811.1Sthorpej *	Autoconfiguration `match' routine.
821.1Sthorpej */
831.1Sthorpejint
841.1Sthorpejacpibut_match(struct device *parent, struct cfdata *match, void *aux)
851.1Sthorpej{
861.1Sthorpej	struct acpi_attach_args *aa = aux;
871.1Sthorpej
881.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
891.1Sthorpej		return (0);
901.1Sthorpej
911.1Sthorpej	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0 ||
921.1Sthorpej	    strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0)
931.1Sthorpej		return (1);
941.1Sthorpej
951.1Sthorpej	return (0);
961.1Sthorpej}
971.1Sthorpej
981.1Sthorpej/*
991.1Sthorpej * acpibut_attach:
1001.1Sthorpej *
1011.1Sthorpej *	Autoconfiguration `attach' routine.
1021.1Sthorpej */
1031.1Sthorpejvoid
1041.1Sthorpejacpibut_attach(struct device *parent, struct device *self, void *aux)
1051.1Sthorpej{
1061.1Sthorpej	struct acpibut_softc *sc = (void *) self;
1071.1Sthorpej	struct acpi_attach_args *aa = aux;
1081.1Sthorpej	const char *desc;
1091.1Sthorpej	ACPI_STATUS rv;
1101.1Sthorpej
1111.1Sthorpej	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0) {
1121.1Sthorpej		sc->sc_buttype = ACPIBUT_TYPE_POWER;
1131.1Sthorpej		desc = "Power";
1141.1Sthorpej	} else if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0) {
1151.1Sthorpej		sc->sc_buttype = ACPIBUT_TYPE_SLEEP;
1161.1Sthorpej		desc = "Sleep";
1171.1Sthorpej	} else {
1181.1Sthorpej		printf("\n");
1191.1Sthorpej		panic("acpibut_attach: impossible");
1201.1Sthorpej	}
1211.1Sthorpej
1221.1Sthorpej	printf(": ACPI %s Button\n", desc);
1231.1Sthorpej
1241.1Sthorpej	sc->sc_node = aa->aa_node;
1251.1Sthorpej
1261.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1271.1Sthorpej	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
1281.1Sthorpej	if (rv != AE_OK) {
1291.1Sthorpej		printf("%s: unable to register device notify handler: %d\n",
1301.1Sthorpej		    sc->sc_dev.dv_xname, rv);
1311.1Sthorpej		return;
1321.1Sthorpej	}
1331.1Sthorpej
1341.1Sthorpej#ifdef ACPI_BUT_DEBUG
1351.1Sthorpej	/* Display the current state when it changes. */
1361.1Sthorpej	sc->sc_flags = ACPIBUT_F_VERBOSE;
1371.1Sthorpej#endif
1381.1Sthorpej}
1391.1Sthorpej
1401.1Sthorpej/*
1411.1Sthorpej * acpibut_pressed_for_sleep:
1421.1Sthorpej *
1431.1Sthorpej *	Deal with a button being pressed for sleep.
1441.1Sthorpej */
1451.1Sthorpejvoid
1461.1Sthorpejacpibut_pressed_for_sleep(void *arg)
1471.1Sthorpej{
1481.1Sthorpej	struct acpibut_softc *sc = arg;
1491.1Sthorpej
1501.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1511.1Sthorpej		printf("%s: button pressed for sleep\n",
1521.1Sthorpej		    sc->sc_dev.dv_xname);
1531.1Sthorpej
1541.1Sthorpej	/*
1551.1Sthorpej	 * XXX Perform the appropriate action.
1561.1Sthorpej	 */
1571.1Sthorpej}
1581.1Sthorpej
1591.1Sthorpej/*
1601.1Sthorpej * acpibut_pressed_for_wakeup:
1611.1Sthorpej *
1621.1Sthorpej *	Deal with a button being pressed for wakeup.
1631.1Sthorpej */
1641.1Sthorpejvoid
1651.1Sthorpejacpibut_pressed_for_wakeup(void *arg)
1661.1Sthorpej{
1671.1Sthorpej	struct acpibut_softc *sc = arg;
1681.1Sthorpej
1691.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1701.1Sthorpej		printf("%s: button pressed for wakeup\n",
1711.1Sthorpej		    sc->sc_dev.dv_xname);
1721.1Sthorpej
1731.1Sthorpej	/*
1741.1Sthorpej	 * XXX Perform the appropriate action.
1751.1Sthorpej	 */
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.1Sthorpejvoid
1841.1Sthorpejacpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
1851.1Sthorpej{
1861.1Sthorpej	struct acpibut_softc *sc = context;
1871.1Sthorpej	int rv;
1881.1Sthorpej
1891.1Sthorpej	switch (notify) {
1901.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
1911.1Sthorpej#if 0
1921.1Sthorpej	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
1931.1Sthorpej#endif
1941.1Sthorpej#ifdef ACPI_BUT_DEBUG
1951.1Sthorpej		printf("%s: received ButtonPressed message\n",
1961.1Sthorpej		    sc->sc_dev.dv_xname);
1971.1Sthorpej#endif
1981.1Sthorpej		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
1991.1Sthorpej		    acpibut_pressed_for_sleep, sc);
2001.1Sthorpej		if (rv != AE_OK)
2011.1Sthorpej			printf("%s: WARNING: unable to queue button pressed "
2021.1Sthorpej			    "event: %d\n", sc->sc_dev.dv_xname, rv);
2031.1Sthorpej		break;
2041.1Sthorpej
2051.1Sthorpej	/* XXX ACPI_NOTIFY_DeviceWake?? */
2061.1Sthorpej
2071.1Sthorpej	default:
2081.1Sthorpej		printf("%s: received unknown notify message: 0x%x\n",
2091.1Sthorpej		    sc->sc_dev.dv_xname, notify);
2101.1Sthorpej	}
2111.1Sthorpej}
212