acpi_button.c revision 1.36
11.36Sjruoho/*	$NetBSD: acpi_button.c,v 1.36 2010/04/27 05:57:43 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.36Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.36 2010/04/27 05:57:43 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.36Sjruoho#define ACPI_NOTIFY_BUTTON	 0x80
571.36Sjruoho
581.1Sthorpejstruct acpibut_softc {
591.32Sjruoho	struct acpi_devnode	*sc_node;
601.32Sjruoho	struct sysmon_pswitch	 sc_smpsw;
611.1Sthorpej};
621.1Sthorpej
631.15Skochistatic const char * const power_button_hid[] = {
641.11Skochi	"PNP0C0C",
651.15Skochi	NULL
661.15Skochi};
671.15Skochi
681.15Skochistatic const char * const sleep_button_hid[] = {
691.11Skochi	"PNP0C0E",
701.11Skochi	NULL
711.11Skochi};
721.11Skochi
731.26Sceggerstatic int	acpibut_match(device_t, cfdata_t, void *);
741.23Sjoergstatic void	acpibut_attach(device_t, device_t, void *);
751.30Sjruohostatic int	acpibut_detach(device_t, int);
761.30Sjruohostatic void	acpibut_pressed_event(void *);
771.32Sjruohostatic void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
781.1Sthorpej
791.23SjoergCFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
801.30Sjruoho    acpibut_match, acpibut_attach, acpibut_detach, NULL);
811.1Sthorpej
821.1Sthorpej/*
831.1Sthorpej * acpibut_match:
841.1Sthorpej *
851.1Sthorpej *	Autoconfiguration `match' routine.
861.1Sthorpej */
871.16Skochistatic int
881.26Sceggeracpibut_match(device_t parent, cfdata_t match, void *aux)
891.1Sthorpej{
901.1Sthorpej	struct acpi_attach_args *aa = aux;
911.1Sthorpej
921.1Sthorpej	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
931.14Skochi		return 0;
941.1Sthorpej
951.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
961.15Skochi		return 1;
971.15Skochi
981.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
991.15Skochi		return 1;
1001.15Skochi
1011.15Skochi	return 0;
1021.1Sthorpej}
1031.1Sthorpej
1041.1Sthorpej/*
1051.1Sthorpej * acpibut_attach:
1061.1Sthorpej *
1071.1Sthorpej *	Autoconfiguration `attach' routine.
1081.1Sthorpej */
1091.16Skochistatic void
1101.23Sjoergacpibut_attach(device_t parent, device_t self, void *aux)
1111.1Sthorpej{
1121.23Sjoerg	struct acpibut_softc *sc = device_private(self);
1131.1Sthorpej	struct acpi_attach_args *aa = aux;
1141.1Sthorpej	const char *desc;
1151.1Sthorpej
1161.23Sjoerg	sc->sc_smpsw.smpsw_name = device_xname(self);
1171.7Sthorpej
1181.15Skochi	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
1191.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1201.1Sthorpej		desc = "Power";
1211.15Skochi	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
1221.9Sthorpej		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
1231.1Sthorpej		desc = "Sleep";
1241.32Sjruoho	} else
1251.32Sjruoho		panic("%s: impossible", __func__);
1261.1Sthorpej
1271.20Skochi	aprint_naive(": ACPI %s Button\n", desc);
1281.20Skochi	aprint_normal(": ACPI %s Button\n", desc);
1291.1Sthorpej
1301.1Sthorpej	sc->sc_node = aa->aa_node;
1311.1Sthorpej
1321.33Sjruoho	(void)pmf_device_register(self, NULL, NULL);
1331.32Sjruoho	(void)sysmon_pswitch_register(&sc->sc_smpsw);
1341.35Sjruoho	(void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
1351.1Sthorpej}
1361.1Sthorpej
1371.1Sthorpej/*
1381.30Sjruoho * acpibut_detach:
1391.30Sjruoho *
1401.30Sjruoho *	Autoconfiguration `detach' routine.
1411.30Sjruoho */
1421.30Sjruohostatic int
1431.30Sjruohoacpibut_detach(device_t self, int flags)
1441.30Sjruoho{
1451.30Sjruoho	struct acpibut_softc *sc = device_private(self);
1461.30Sjruoho
1471.30Sjruoho	pmf_device_deregister(self);
1481.35Sjruoho	acpi_deregister_notify(sc->sc_node);
1491.30Sjruoho	sysmon_pswitch_unregister(&sc->sc_smpsw);
1501.30Sjruoho
1511.30Sjruoho	return 0;
1521.30Sjruoho}
1531.30Sjruoho
1541.30Sjruoho/*
1551.7Sthorpej * acpibut_pressed_event:
1561.1Sthorpej *
1571.7Sthorpej *	Deal with a button being pressed.
1581.1Sthorpej */
1591.16Skochistatic void
1601.7Sthorpejacpibut_pressed_event(void *arg)
1611.1Sthorpej{
1621.23Sjoerg	device_t dv = arg;
1631.23Sjoerg	struct acpibut_softc *sc = device_private(dv);
1641.1Sthorpej
1651.32Sjruoho	aprint_debug_dev(dv, "button pressed\n");
1661.9Sthorpej	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1671.1Sthorpej}
1681.1Sthorpej
1691.1Sthorpej/*
1701.1Sthorpej * acpibut_notify_handler:
1711.1Sthorpej *
1721.1Sthorpej *	Callback from ACPI interrupt handler to notify us of an event.
1731.1Sthorpej */
1741.16Skochistatic void
1751.32Sjruohoacpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
1761.1Sthorpej{
1771.32Sjruoho	static const int handler = OSL_NOTIFY_HANDLER;
1781.23Sjoerg	device_t dv = context;
1791.1Sthorpej
1801.1Sthorpej	switch (notify) {
1811.32Sjruoho
1821.36Sjruoho	case ACPI_NOTIFY_BUTTON:
1831.32Sjruoho		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
1841.1Sthorpej		break;
1851.1Sthorpej
1861.1Sthorpej	default:
1871.32Sjruoho		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
1881.1Sthorpej	}
1891.1Sthorpej}
1901.31Sjruoho
1911.31Sjruoho#ifdef _MODULE
1921.31Sjruoho
1931.31SjruohoMODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
1941.31SjruohoCFDRIVER_DECL(acpibut, DV_DULL, NULL);
1951.31Sjruoho
1961.31Sjruohostatic int acpibutloc[] = { -1 };
1971.31Sjruohoextern struct cfattach acpibut_ca;
1981.31Sjruoho
1991.31Sjruohostatic struct cfparent acpiparent = {
2001.31Sjruoho	"acpinodebus", NULL, DVUNIT_ANY
2011.31Sjruoho};
2021.31Sjruoho
2031.31Sjruohostatic struct cfdata acpibut_cfdata[] = {
2041.31Sjruoho	{
2051.31Sjruoho		.cf_name = "acpibut",
2061.31Sjruoho		.cf_atname = "acpibut",
2071.31Sjruoho		.cf_unit = 0,
2081.31Sjruoho		.cf_fstate = FSTATE_STAR,
2091.31Sjruoho		.cf_loc = acpibutloc,
2101.31Sjruoho		.cf_flags = 0,
2111.31Sjruoho		.cf_pspec = &acpiparent,
2121.31Sjruoho	},
2131.31Sjruoho
2141.31Sjruoho	{ NULL }
2151.31Sjruoho};
2161.31Sjruoho
2171.31Sjruohostatic int
2181.31Sjruohoacpibut_modcmd(modcmd_t cmd, void *context)
2191.31Sjruoho{
2201.31Sjruoho	int err;
2211.31Sjruoho
2221.31Sjruoho	switch (cmd) {
2231.31Sjruoho
2241.31Sjruoho	case MODULE_CMD_INIT:
2251.31Sjruoho
2261.31Sjruoho		err = config_cfdriver_attach(&acpibut_cd);
2271.31Sjruoho
2281.31Sjruoho		if (err != 0)
2291.31Sjruoho			return err;
2301.31Sjruoho
2311.31Sjruoho		err = config_cfattach_attach("acpibut", &acpibut_ca);
2321.31Sjruoho
2331.31Sjruoho		if (err != 0) {
2341.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2351.31Sjruoho			return err;
2361.31Sjruoho		}
2371.31Sjruoho
2381.31Sjruoho		err = config_cfdata_attach(acpibut_cfdata, 1);
2391.31Sjruoho
2401.31Sjruoho		if (err != 0) {
2411.31Sjruoho			config_cfattach_detach("acpibut", &acpibut_ca);
2421.31Sjruoho			config_cfdriver_detach(&acpibut_cd);
2431.31Sjruoho			return err;
2441.31Sjruoho		}
2451.31Sjruoho
2461.31Sjruoho		return 0;
2471.31Sjruoho
2481.31Sjruoho	case MODULE_CMD_FINI:
2491.31Sjruoho
2501.31Sjruoho		err = config_cfdata_detach(acpibut_cfdata);
2511.31Sjruoho
2521.31Sjruoho		if (err != 0)
2531.31Sjruoho			return err;
2541.31Sjruoho
2551.31Sjruoho		config_cfattach_detach("acpibut", &acpibut_ca);
2561.31Sjruoho		config_cfdriver_detach(&acpibut_cd);
2571.31Sjruoho
2581.31Sjruoho		return 0;
2591.31Sjruoho
2601.31Sjruoho	default:
2611.31Sjruoho		return ENOTTY;
2621.31Sjruoho	}
2631.31Sjruoho}
2641.31Sjruoho
2651.31Sjruoho#endif	/* _MODULE */
266