acpi_button.c revision 1.35
11.35Sjruoho/*	$NetBSD: acpi_button.c,v 1.35 2010/04/15 07:02:24 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.35Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.35 2010/04/15 07:02:24 jruoho Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/device.h>
471.31Sjruoho#include <sys/module.h>
481.34Sjruoho#include <sys/systm.h>
491.1Sthorpej
501.1Sthorpej#include <dev/acpi/acpireg.h>
511.1Sthorpej#include <dev/acpi/acpivar.h>
521.1Sthorpej
531.32Sjruoho#define _COMPONENT		 ACPI_BUTTON_COMPONENT
541.32SjruohoACPI_MODULE_NAME		 ("acpi_button")
551.29Sjruoho
561.1Sthorpejstruct acpibut_softc {
571.32Sjruoho	struct acpi_devnode	*sc_node;
581.32Sjruoho	struct sysmon_pswitch	 sc_smpsw;
591.1Sthorpej};
601.1Sthorpej
611.15Skochistatic const char * const power_button_hid[] = {
621.11Skochi	"PNP0C0C",
631.15Skochi	NULL
641.15Skochi};
651.15Skochi
661.15Skochistatic const char * const sleep_button_hid[] = {
671.11Skochi	"PNP0C0E",
681.11Skochi	NULL
691.11Skochi};
701.11Skochi
711.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
721.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
731.30Sjruohostatic int	acpibut_detach(device_t, int);
741.30Sjruohostatic void	acpibut_pressed_event(void *);
751.32Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
761.1Sthorpej
771.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
781.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
791.1Sthorpej
801.1Sthorpej/*
811.1Sthorpej * acpibut_match:
821.1Sthorpej *
831.1Sthorpej *	Autoconfiguration `match' routine.
841.1Sthorpej */
851.16Skochistatic int
861.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
871.1Sthorpej{
881.1Sthorpej	struct acpi_attach_args *aa = aux;
891.1Sthorpej
901.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
911.14Skochi		return 0;
921.1Sthorpej
931.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
941.15Skochi		return 1;
951.15Skochi
961.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
971.15Skochi		return 1;
981.15Skochi
991.15Skochi	return 0;
1001.1Sthorpej}
1011.1Sthorpej
1021.1Sthorpej/*
1031.1Sthorpej * acpibut_attach:
1041.1Sthorpej *
1051.1Sthorpej *	Autoconfiguration `attach' routine.
1061.1Sthorpej */
1071.16Skochistatic void
1081.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1091.1Sthorpej{
1101.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1111.1Sthorpej	struct acpi_attach_args *aa = aux;
1121.1Sthorpej	const char *desc;
1131.1Sthorpej
1141.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1151.7Sthorpej
1161.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1171.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1181.1Sthorpej		desc = "Power";
1191.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1201.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1211.1Sthorpej		desc = "Sleep";
1221.32Sjruoho	} else
1231.32Sjruoho		panic("%s: impossible", __func__);
1241.1Sthorpej
1251.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1261.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1271.1Sthorpej
1281.1Sthorpej	sc->sc_node = aa->aa_node;
1291.1Sthorpej
1301.33Sjruoho	(void)pmf_device_register(self, NULL, NULL);
1311.32Sjruoho	(void)sysmon_pswitch_register(&sc->sc_smpsw);
1321.35Sjruoho	(void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
1331.1Sthorpej}
1341.1Sthorpej
1351.1Sthorpej/*
1361.30Sjruoho * acpibut_detach:
1371.30Sjruoho *
1381.30Sjruoho *	Autoconfiguration `detach' routine.
1391.30Sjruoho */
1401.30Sjruohostatic int
1411.30Sjruohoacpibut_detach(device_t self, int flags)
1421.30Sjruoho{
1431.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1441.30Sjruoho
1451.30Sjruoho	pmf_device_deregister(self);
1461.35Sjruoho	acpi_deregister_notify(sc->sc_node);
1471.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1481.30Sjruoho
1491.30Sjruoho	return 0;
1501.30Sjruoho}
1511.30Sjruoho
1521.30Sjruoho/*
1531.7Sthorpej * acpibut_pressed_event:
1541.1Sthorpej *
1551.7Sthorpej *	Deal with a button being pressed.
1561.1Sthorpej */
1571.16Skochistatic void
1581.7Sthorpejacpibut_pressed_event(void *arg)
1591.1Sthorpej{
1601.23Sjoerg	device_t dv = arg;
1611.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1621.1Sthorpej
1631.32Sjruoho	aprint_debug_dev(dv, "button pressed\n");
1641.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1651.1Sthorpej}
1661.1Sthorpej
1671.1Sthorpej/*
1681.1Sthorpej * acpibut_notify_handler:
1691.1Sthorpej *
1701.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1711.1Sthorpej */
1721.16Skochistatic void
1731.32Sjruohoacpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
1741.1Sthorpej{
1751.32Sjruoho	static const int handler = OSL_NOTIFY_HANDLER;
1761.23Sjoerg	device_t dv = context;
1771.1Sthorpej
1781.1Sthorpej	switch (notify) {
1791.32Sjruoho
1801.32Sjruoho     /* case ACPI_NOTIFY_S0SleepButtonPressed: */
1811.1Sthorpej	case ACPI_NOTIFY_S0PowerButtonPressed:
1821.32Sjruoho		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
1831.1Sthorpej		break;
1841.1Sthorpej
1851.1Sthorpej	default:
1861.32Sjruoho		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
1871.1Sthorpej	}
1881.1Sthorpej}
1891.31Sjruoho
1901.31Sjruoho#ifdef _MODULE
1911.31Sjruoho
1921.31SjruohoMODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
1931.31SjruohoCFDRIVER_DECL(acpibut, DV_DULL, NULL);
1941.31Sjruoho
1951.31Sjruohostatic int acpibutloc[] = { -1 };
1961.31Sjruohoextern struct cfattach acpibut_ca;
1971.31Sjruoho
1981.31Sjruohostatic struct cfparent acpiparent = {
1991.31Sjruoho	"acpinodebus", NULL, DVUNIT_ANY
2001.31Sjruoho};
2011.31Sjruoho
2021.31Sjruohostatic struct cfdata acpibut_cfdata[] = {
2031.31Sjruoho	{
2041.31Sjruoho		.cf_name = "acpibut",
2051.31Sjruoho		.cf_atname = "acpibut",
2061.31Sjruoho		.cf_unit = 0,
2071.31Sjruoho		.cf_fstate = FSTATE_STAR,
2081.31Sjruoho		.cf_loc = acpibutloc,
2091.31Sjruoho		.cf_flags = 0,
2101.31Sjruoho		.cf_pspec = &acpiparent,
2111.31Sjruoho	},
2121.31Sjruoho
2131.31Sjruoho	{ NULL }
2141.31Sjruoho};
2151.31Sjruoho
2161.31Sjruohostatic int
2171.31Sjruohoacpibut_modcmd(modcmd_t cmd, void *context)
2181.31Sjruoho{
2191.31Sjruoho	int err;
2201.31Sjruoho
2211.31Sjruoho	switch (cmd) {
2221.31Sjruoho
2231.31Sjruoho	case MODULE_CMD_INIT:
2241.31Sjruoho
2251.31Sjruoho		err = config_cfdriver_attach(&acpibut_cd);
2261.31Sjruoho
2271.31Sjruoho		if (err != 0)
2281.31Sjruoho			return err;
2291.31Sjruoho
2301.31Sjruoho		err = config_cfattach_attach("acpibut", &acpibut_ca);
2311.31Sjruoho
2321.31Sjruoho		if (err != 0) {
2331.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2341.31Sjruoho			return err;
2351.31Sjruoho		}
2361.31Sjruoho
2371.31Sjruoho		err = config_cfdata_attach(acpibut_cfdata, 1);
2381.31Sjruoho
2391.31Sjruoho		if (err != 0) {
2401.31Sjruoho			config_cfattach_detach("acpibut", &acpibut_ca);
2411.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2421.31Sjruoho			return err;
2431.31Sjruoho		}
2441.31Sjruoho
2451.31Sjruoho		return 0;
2461.31Sjruoho
2471.31Sjruoho	case MODULE_CMD_FINI:
2481.31Sjruoho
2491.31Sjruoho		err = config_cfdata_detach(acpibut_cfdata);
2501.31Sjruoho
2511.31Sjruoho		if (err != 0)
2521.31Sjruoho			return err;
2531.31Sjruoho
2541.31Sjruoho		config_cfattach_detach("acpibut", &acpibut_ca);
2551.31Sjruoho		config_cfdriver_detach(&acpibut_cd);
2561.31Sjruoho
2571.31Sjruoho		return 0;
2581.31Sjruoho
2591.31Sjruoho	default:
2601.31Sjruoho		return ENOTTY;
2611.31Sjruoho	}
2621.31Sjruoho}
2631.31Sjruoho
2641.31Sjruoho#endif	/* _MODULE */
265