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