acpi_button.c revision 1.31
11.31Sjruoho/*	$NetBSD: acpi_button.c,v 1.31 2010/02/28 17:22:41 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.31Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.31 2010/02/28 17:22:41 jruoho Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/systm.h>
471.1Sthorpej#include <sys/device.h>
481.31Sjruoho#include <sys/module.h>
491.1Sthorpej
501.1Sthorpej#include <dev/acpi/acpica.h>
511.1Sthorpej#include <dev/acpi/acpireg.h>
521.1Sthorpej#include <dev/acpi/acpivar.h>
531.1Sthorpej
541.7Sthorpej#include <dev/sysmon/sysmonvar.h>
551.7Sthorpej
561.29Sjruoho#define _COMPONENT		ACPI_BUTTON_COMPONENT
571.29SjruohoACPI_MODULE_NAME		("acpi_button")
581.29Sjruoho
591.1Sthorpejstruct acpibut_softc {
601.1Sthorpej	struct acpi_devnode *sc_node;	/* our ACPI devnode */
611.7Sthorpej	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
621.1Sthorpej	int sc_flags;			/* see below */
631.1Sthorpej};
641.1Sthorpej
651.15Skochistatic const char * const power_button_hid[] = {
661.11Skochi	"PNP0C0C",
671.15Skochi	NULL
681.15Skochi};
691.15Skochi
701.15Skochistatic const char * const sleep_button_hid[] = {
711.11Skochi	"PNP0C0E",
721.11Skochi	NULL
731.11Skochi};
741.11Skochi
751.1Sthorpej#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
761.1Sthorpej
771.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
781.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
791.30Sjruohostatic int	acpibut_detach(device_t, int);
801.30Sjruohostatic void	acpibut_pressed_event(void *);
811.30Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
821.1Sthorpej
831.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
841.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
851.1Sthorpej
861.1Sthorpej/*
871.1Sthorpej * acpibut_match:
881.1Sthorpej *
891.1Sthorpej *	Autoconfiguration `match' routine.
901.1Sthorpej */
911.16Skochistatic int
921.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
931.1Sthorpej{
941.1Sthorpej	struct acpi_attach_args *aa = aux;
951.1Sthorpej
961.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
971.14Skochi		return 0;
981.1Sthorpej
991.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
1001.15Skochi		return 1;
1011.15Skochi
1021.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
1031.15Skochi		return 1;
1041.15Skochi
1051.15Skochi	return 0;
1061.1Sthorpej}
1071.1Sthorpej
1081.1Sthorpej/*
1091.1Sthorpej * acpibut_attach:
1101.1Sthorpej *
1111.1Sthorpej *	Autoconfiguration `attach' routine.
1121.1Sthorpej */
1131.16Skochistatic void
1141.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1151.1Sthorpej{
1161.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1171.1Sthorpej	struct acpi_attach_args *aa = aux;
1181.1Sthorpej	const char *desc;
1191.1Sthorpej	ACPI_STATUS rv;
1201.1Sthorpej
1211.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1221.7Sthorpej
1231.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1241.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1251.1Sthorpej		desc = "Power";
1261.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1271.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1281.1Sthorpej		desc = "Sleep";
1291.1Sthorpej	} else {
1301.1Sthorpej		panic("acpibut_attach: impossible");
1311.1Sthorpej	}
1321.1Sthorpej
1331.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1341.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1351.1Sthorpej
1361.1Sthorpej	sc->sc_node = aa->aa_node;
1371.1Sthorpej
1381.7Sthorpej	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
1391.23Sjoerg		aprint_error_dev(self, "unable to register with sysmon\n");
1401.7Sthorpej		return;
1411.7Sthorpej	}
1421.7Sthorpej
1431.1Sthorpej	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
1441.23Sjoerg	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
1451.13Smycroft	if (ACPI_FAILURE(rv)) {
1461.23Sjoerg		aprint_error_dev(self,
1471.23Sjoerg		    "unable to register DEVICE NOTIFY handler: %s\n",
1481.23Sjoerg		    AcpiFormatException(rv));
1491.1Sthorpej		return;
1501.1Sthorpej	}
1511.1Sthorpej
1521.1Sthorpej#ifdef ACPI_BUT_DEBUG
1531.1Sthorpej	/* Display the current state when it changes. */
1541.1Sthorpej	sc->sc_flags = ACPIBUT_F_VERBOSE;
1551.1Sthorpej#endif
1561.25Sjmcneill
1571.25Sjmcneill	if (!pmf_device_register(self, NULL, NULL))
1581.25Sjmcneill		aprint_error_dev(self, "couldn't establish power handler\n");
1591.1Sthorpej}
1601.1Sthorpej
1611.1Sthorpej/*
1621.30Sjruoho * acpibut_detach:
1631.30Sjruoho *
1641.30Sjruoho *	Autoconfiguration `detach' routine.
1651.30Sjruoho */
1661.30Sjruohostatic int
1671.30Sjruohoacpibut_detach(device_t self, int flags)
1681.30Sjruoho{
1691.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1701.30Sjruoho	ACPI_STATUS rv;
1711.30Sjruoho
1721.30Sjruoho	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
1731.30Sjruoho	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler);
1741.30Sjruoho
1751.30Sjruoho	if (ACPI_FAILURE(rv))
1761.30Sjruoho		return EBUSY;
1771.30Sjruoho
1781.30Sjruoho	pmf_device_deregister(self);
1791.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1801.30Sjruoho
1811.30Sjruoho	return 0;
1821.30Sjruoho}
1831.30Sjruoho
1841.30Sjruoho/*
1851.7Sthorpej * acpibut_pressed_event:
1861.1Sthorpej *
1871.7Sthorpej *	Deal with a button being pressed.
1881.1Sthorpej */
1891.16Skochistatic void
1901.7Sthorpejacpibut_pressed_event(void *arg)
1911.1Sthorpej{
1921.23Sjoerg	device_t dv = arg;
1931.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1941.1Sthorpej
1951.1Sthorpej	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
1961.23Sjoerg		aprint_verbose_dev(dv, "button pressed\n");
1971.1Sthorpej
1981.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1991.1Sthorpej}
2001.1Sthorpej
2011.1Sthorpej/*
2021.1Sthorpej * acpibut_notify_handler:
2031.1Sthorpej *
2041.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
2051.1Sthorpej */
2061.16Skochistatic void
2071.23Sjoergacpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
2081.1Sthorpej{
2091.23Sjoerg	device_t dv = context;
2101.28Sjmcneill	ACPI_STATUS rv;
2111.1Sthorpej
2121.1Sthorpej	switch (notify) {
2131.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
2141.1Sthorpej#if 0
2151.1Sthorpej	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
2161.1Sthorpej#endif
2171.1Sthorpej#ifdef ACPI_BUT_DEBUG
2181.23Sjoerg		aprint_debug_dev(dv, "received ButtonPressed message\n");
2191.1Sthorpej#endif
2201.25Sjmcneill		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
2211.24Sjoerg		    acpibut_pressed_event, dv);
2221.13Smycroft		if (ACPI_FAILURE(rv))
2231.23Sjoerg			aprint_error_dev(dv,
2241.23Sjoerg			    "WARNING: unable to queue button pressed callback: %s\n",
2251.12Smycroft			    AcpiFormatException(rv));
2261.1Sthorpej		break;
2271.1Sthorpej
2281.1Sthorpej	/* XXX ACPI_NOTIFY_DeviceWake?? */
2291.1Sthorpej
2301.1Sthorpej	default:
2311.23Sjoerg		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
2321.23Sjoerg		    notify);
2331.1Sthorpej	}
2341.1Sthorpej}
2351.31Sjruoho
2361.31Sjruoho#ifdef _MODULE
2371.31Sjruoho
2381.31SjruohoMODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
2391.31SjruohoCFDRIVER_DECL(acpibut, DV_DULL, NULL);
2401.31Sjruoho
2411.31Sjruohostatic int acpibutloc[] = { -1 };
2421.31Sjruohoextern struct cfattach acpibut_ca;
2431.31Sjruoho
2441.31Sjruohostatic struct cfparent acpiparent = {
2451.31Sjruoho	"acpinodebus", NULL, DVUNIT_ANY
2461.31Sjruoho};
2471.31Sjruoho
2481.31Sjruohostatic struct cfdata acpibut_cfdata[] = {
2491.31Sjruoho	{
2501.31Sjruoho		.cf_name = "acpibut",
2511.31Sjruoho		.cf_atname = "acpibut",
2521.31Sjruoho		.cf_unit = 0,
2531.31Sjruoho		.cf_fstate = FSTATE_STAR,
2541.31Sjruoho		.cf_loc = acpibutloc,
2551.31Sjruoho		.cf_flags = 0,
2561.31Sjruoho		.cf_pspec = &acpiparent,
2571.31Sjruoho	},
2581.31Sjruoho
2591.31Sjruoho	{ NULL }
2601.31Sjruoho};
2611.31Sjruoho
2621.31Sjruohostatic int
2631.31Sjruohoacpibut_modcmd(modcmd_t cmd, void *context)
2641.31Sjruoho{
2651.31Sjruoho	int err;
2661.31Sjruoho
2671.31Sjruoho	switch (cmd) {
2681.31Sjruoho
2691.31Sjruoho	case MODULE_CMD_INIT:
2701.31Sjruoho
2711.31Sjruoho		err = config_cfdriver_attach(&acpibut_cd);
2721.31Sjruoho
2731.31Sjruoho		if (err != 0)
2741.31Sjruoho			return err;
2751.31Sjruoho
2761.31Sjruoho		err = config_cfattach_attach("acpibut", &acpibut_ca);
2771.31Sjruoho
2781.31Sjruoho		if (err != 0) {
2791.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2801.31Sjruoho			return err;
2811.31Sjruoho		}
2821.31Sjruoho
2831.31Sjruoho		err = config_cfdata_attach(acpibut_cfdata, 1);
2841.31Sjruoho
2851.31Sjruoho		if (err != 0) {
2861.31Sjruoho			config_cfattach_detach("acpibut", &acpibut_ca);
2871.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2881.31Sjruoho			return err;
2891.31Sjruoho		}
2901.31Sjruoho
2911.31Sjruoho		return 0;
2921.31Sjruoho
2931.31Sjruoho	case MODULE_CMD_FINI:
2941.31Sjruoho
2951.31Sjruoho		err = config_cfdata_detach(acpibut_cfdata);
2961.31Sjruoho
2971.31Sjruoho		if (err != 0)
2981.31Sjruoho			return err;
2991.31Sjruoho
3001.31Sjruoho		config_cfattach_detach("acpibut", &acpibut_ca);
3011.31Sjruoho		config_cfdriver_detach(&acpibut_cd);
3021.31Sjruoho
3031.31Sjruoho		return 0;
3041.31Sjruoho
3051.31Sjruoho	default:
3061.31Sjruoho		return ENOTTY;
3071.31Sjruoho	}
3081.31Sjruoho}
3091.31Sjruoho
3101.31Sjruoho#endif	/* _MODULE */
311