acpi_button.c revision 1.14
11.14Skochi/*	$NetBSD: acpi_button.c,v 1.14 2004/04/11 06:48:25 kochi 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.14Skochi__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.14 2004/04/11 06:48:25 kochi 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.1Sthorpejstruct acpibut_softc {
561.1Sthorpej	struct device sc_dev;		/* base device glue */
571.1Sthorpej	struct acpi_devnode *sc_node;	/* our ACPI devnode */
581.7Sthorpej	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
591.1Sthorpej	int sc_flags;			/* see below */
601.1Sthorpej};
611.1Sthorpej
621.11Skochistatic const char * const button_hid[] = {
631.11Skochi	"PNP0C0C",
641.11Skochi	"PNP0C0E",
651.11Skochi	NULL
661.11Skochi};
671.11Skochi
681.1Sthorpej#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
691.1Sthorpej
701.1Sthorpejint	acpibut_match(struct device *, struct cfdata *, void *);
711.1Sthorpejvoid	acpibut_attach(struct device *, struct device *, void *);
721.1Sthorpej
731.5SthorpejCFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
741.6Sthorpej    acpibut_match, acpibut_attach, NULL, NULL);
751.1Sthorpej
761.7Sthorpejvoid	acpibut_pressed_event(void *);
771.1Sthorpejvoid	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
781.1Sthorpej
791.1Sthorpej/*
801.1Sthorpej * acpibut_match:
811.1Sthorpej *
821.1Sthorpej *	Autoconfiguration `match' routine.
831.1Sthorpej */
841.1Sthorpejint
851.1Sthorpejacpibut_match(struct device *parent, struct cfdata *match, void *aux)
861.1Sthorpej{
871.1Sthorpej	struct acpi_attach_args *aa = aux;
881.1Sthorpej
891.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
901.14Skochi		return 0;
911.1Sthorpej
921.14Skochi	return acpi_match_hid(aa->aa_node->ad_devinfo, button_hid);
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.7Sthorpej	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
1091.7Sthorpej
1101.11Skochi	if (strcmp(aa->aa_node->ad_devinfo->HardwareId.Value, "PNP0C0C") == 0) {
1111.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1121.1Sthorpej		desc = "Power";
1131.11Skochi	} else if (strcmp(aa->aa_node->ad_devinfo->HardwareId.Value, "PNP0C0E") == 0) {
1141.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1151.1Sthorpej		desc = "Sleep";
1161.1Sthorpej	} else {
1171.1Sthorpej		printf("\n");
1181.1Sthorpej		panic("acpibut_attach: impossible");
1191.1Sthorpej	}
1201.1Sthorpej
1211.1Sthorpej	printf(": ACPI %s Button\n", desc);
1221.1Sthorpej
1231.1Sthorpej	sc->sc_node = aa->aa_node;
1241.1Sthorpej
1251.7Sthorpej	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1261.7Sthorpej		printf("%s: unable to register with sysmon\n",
1271.7Sthorpej		    sc->sc_dev.dv_xname);
1281.7Sthorpej		return;
1291.7Sthorpej	}
1301.7Sthorpej
1311.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1321.1Sthorpej	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
1331.13Smycroft	if (ACPI_FAILURE(rv)) {
1341.12Smycroft		printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
1351.12Smycroft		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
1361.1Sthorpej		return;
1371.1Sthorpej	}
1381.1Sthorpej
1391.1Sthorpej#ifdef ACPI_BUT_DEBUG
1401.1Sthorpej	/* Display the current state when it changes. */
1411.1Sthorpej	sc->sc_flags = ACPIBUT_F_VERBOSE;
1421.1Sthorpej#endif
1431.1Sthorpej}
1441.1Sthorpej
1451.1Sthorpej/*
1461.7Sthorpej * acpibut_pressed_event:
1471.1Sthorpej *
1481.7Sthorpej *	Deal with a button being pressed.
1491.1Sthorpej */
1501.1Sthorpejvoid
1511.7Sthorpejacpibut_pressed_event(void *arg)
1521.1Sthorpej{
1531.1Sthorpej	struct acpibut_softc *sc = arg;
1541.1Sthorpej
1551.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1561.7Sthorpej		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
1571.1Sthorpej
1581.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1591.1Sthorpej}
1601.1Sthorpej
1611.1Sthorpej/*
1621.1Sthorpej * acpibut_notify_handler:
1631.1Sthorpej *
1641.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1651.1Sthorpej */
1661.1Sthorpejvoid
1671.1Sthorpejacpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
1681.1Sthorpej{
1691.1Sthorpej	struct acpibut_softc *sc = context;
1701.1Sthorpej	int rv;
1711.1Sthorpej
1721.1Sthorpej	switch (notify) {
1731.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
1741.1Sthorpej#if 0
1751.1Sthorpej	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
1761.1Sthorpej#endif
1771.1Sthorpej#ifdef ACPI_BUT_DEBUG
1781.1Sthorpej		printf("%s: received ButtonPressed message\n",
1791.1Sthorpej		    sc->sc_dev.dv_xname);
1801.1Sthorpej#endif
1811.1Sthorpej		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
1821.7Sthorpej		    acpibut_pressed_event, sc);
1831.13Smycroft		if (ACPI_FAILURE(rv))
1841.1Sthorpej			printf("%s: WARNING: unable to queue button pressed "
1851.12Smycroft			    "callback: %s\n", sc->sc_dev.dv_xname,
1861.12Smycroft			    AcpiFormatException(rv));
1871.1Sthorpej		break;
1881.1Sthorpej
1891.1Sthorpej	/* XXX ACPI_NOTIFY_DeviceWake?? */
1901.1Sthorpej
1911.1Sthorpej	default:
1921.1Sthorpej		printf("%s: received unknown notify message: 0x%x\n",
1931.1Sthorpej		    sc->sc_dev.dv_xname, notify);
1941.1Sthorpej	}
1951.1Sthorpej}
196