acpi_button.c revision 1.33
11.33Sjruoho/* $NetBSD: acpi_button.c,v 1.33 2010/03/04 23:06:36 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.33Sjruoho__KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.33 2010/03/04 23:06:36 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/acpireg.h> 511.1Sthorpej#include <dev/acpi/acpivar.h> 521.1Sthorpej 531.7Sthorpej#include <dev/sysmon/sysmonvar.h> 541.7Sthorpej 551.32Sjruoho#define _COMPONENT ACPI_BUTTON_COMPONENT 561.32SjruohoACPI_MODULE_NAME ("acpi_button") 571.29Sjruoho 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 ACPI_STATUS rv; 1161.1Sthorpej 1171.23Sjoerg sc->sc_smpsw.smpsw_name = device_xname(self); 1181.7Sthorpej 1191.15Skochi if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) { 1201.9Sthorpej sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 1211.1Sthorpej desc = "Power"; 1221.15Skochi } else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) { 1231.9Sthorpej sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP; 1241.1Sthorpej desc = "Sleep"; 1251.32Sjruoho } else 1261.32Sjruoho panic("%s: impossible", __func__); 1271.1Sthorpej 1281.20Skochi aprint_naive(": ACPI %s Button\n", desc); 1291.20Skochi aprint_normal(": ACPI %s Button\n", desc); 1301.1Sthorpej 1311.1Sthorpej sc->sc_node = aa->aa_node; 1321.1Sthorpej 1331.33Sjruoho (void)pmf_device_register(self, NULL, NULL); 1341.32Sjruoho (void)sysmon_pswitch_register(&sc->sc_smpsw); 1351.7Sthorpej 1361.1Sthorpej rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 1371.23Sjoerg ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self); 1381.1Sthorpej 1391.32Sjruoho if (ACPI_FAILURE(rv)) 1401.32Sjruoho aprint_error_dev(self, "failed to install notify handler\n"); 1411.1Sthorpej} 1421.1Sthorpej 1431.1Sthorpej/* 1441.30Sjruoho * acpibut_detach: 1451.30Sjruoho * 1461.30Sjruoho * Autoconfiguration `detach' routine. 1471.30Sjruoho */ 1481.30Sjruohostatic int 1491.30Sjruohoacpibut_detach(device_t self, int flags) 1501.30Sjruoho{ 1511.30Sjruoho struct acpibut_softc *sc = device_private(self); 1521.30Sjruoho ACPI_STATUS rv; 1531.30Sjruoho 1541.30Sjruoho rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle, 1551.30Sjruoho ACPI_DEVICE_NOTIFY, acpibut_notify_handler); 1561.30Sjruoho 1571.30Sjruoho if (ACPI_FAILURE(rv)) 1581.30Sjruoho return EBUSY; 1591.30Sjruoho 1601.30Sjruoho pmf_device_deregister(self); 1611.30Sjruoho sysmon_pswitch_unregister(&sc->sc_smpsw); 1621.30Sjruoho 1631.30Sjruoho return 0; 1641.30Sjruoho} 1651.30Sjruoho 1661.30Sjruoho/* 1671.7Sthorpej * acpibut_pressed_event: 1681.1Sthorpej * 1691.7Sthorpej * Deal with a button being pressed. 1701.1Sthorpej */ 1711.16Skochistatic void 1721.7Sthorpejacpibut_pressed_event(void *arg) 1731.1Sthorpej{ 1741.23Sjoerg device_t dv = arg; 1751.23Sjoerg struct acpibut_softc *sc = device_private(dv); 1761.1Sthorpej 1771.32Sjruoho aprint_debug_dev(dv, "button pressed\n"); 1781.9Sthorpej sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 1791.1Sthorpej} 1801.1Sthorpej 1811.1Sthorpej/* 1821.1Sthorpej * acpibut_notify_handler: 1831.1Sthorpej * 1841.1Sthorpej * Callback from ACPI interrupt handler to notify us of an event. 1851.1Sthorpej */ 1861.16Skochistatic void 1871.32Sjruohoacpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 1881.1Sthorpej{ 1891.32Sjruoho static const int handler = OSL_NOTIFY_HANDLER; 1901.23Sjoerg device_t dv = context; 1911.1Sthorpej 1921.1Sthorpej switch (notify) { 1931.32Sjruoho 1941.32Sjruoho /* case ACPI_NOTIFY_S0SleepButtonPressed: */ 1951.1Sthorpej case ACPI_NOTIFY_S0PowerButtonPressed: 1961.32Sjruoho (void)AcpiOsExecute(handler, acpibut_pressed_event, dv); 1971.1Sthorpej break; 1981.1Sthorpej 1991.1Sthorpej default: 2001.32Sjruoho aprint_error_dev(dv, "unknown notify 0x%02X\n", notify); 2011.1Sthorpej } 2021.1Sthorpej} 2031.31Sjruoho 2041.31Sjruoho#ifdef _MODULE 2051.31Sjruoho 2061.31SjruohoMODULE(MODULE_CLASS_DRIVER, acpibut, NULL); 2071.31SjruohoCFDRIVER_DECL(acpibut, DV_DULL, NULL); 2081.31Sjruoho 2091.31Sjruohostatic int acpibutloc[] = { -1 }; 2101.31Sjruohoextern struct cfattach acpibut_ca; 2111.31Sjruoho 2121.31Sjruohostatic struct cfparent acpiparent = { 2131.31Sjruoho "acpinodebus", NULL, DVUNIT_ANY 2141.31Sjruoho}; 2151.31Sjruoho 2161.31Sjruohostatic struct cfdata acpibut_cfdata[] = { 2171.31Sjruoho { 2181.31Sjruoho .cf_name = "acpibut", 2191.31Sjruoho .cf_atname = "acpibut", 2201.31Sjruoho .cf_unit = 0, 2211.31Sjruoho .cf_fstate = FSTATE_STAR, 2221.31Sjruoho .cf_loc = acpibutloc, 2231.31Sjruoho .cf_flags = 0, 2241.31Sjruoho .cf_pspec = &acpiparent, 2251.31Sjruoho }, 2261.31Sjruoho 2271.31Sjruoho { NULL } 2281.31Sjruoho}; 2291.31Sjruoho 2301.31Sjruohostatic int 2311.31Sjruohoacpibut_modcmd(modcmd_t cmd, void *context) 2321.31Sjruoho{ 2331.31Sjruoho int err; 2341.31Sjruoho 2351.31Sjruoho switch (cmd) { 2361.31Sjruoho 2371.31Sjruoho case MODULE_CMD_INIT: 2381.31Sjruoho 2391.31Sjruoho err = config_cfdriver_attach(&acpibut_cd); 2401.31Sjruoho 2411.31Sjruoho if (err != 0) 2421.31Sjruoho return err; 2431.31Sjruoho 2441.31Sjruoho err = config_cfattach_attach("acpibut", &acpibut_ca); 2451.31Sjruoho 2461.31Sjruoho if (err != 0) { 2471.31Sjruoho config_cfdriver_detach(&acpibut_cd); 2481.31Sjruoho return err; 2491.31Sjruoho } 2501.31Sjruoho 2511.31Sjruoho err = config_cfdata_attach(acpibut_cfdata, 1); 2521.31Sjruoho 2531.31Sjruoho if (err != 0) { 2541.31Sjruoho config_cfattach_detach("acpibut", &acpibut_ca); 2551.31Sjruoho config_cfdriver_detach(&acpibut_cd); 2561.31Sjruoho return err; 2571.31Sjruoho } 2581.31Sjruoho 2591.31Sjruoho return 0; 2601.31Sjruoho 2611.31Sjruoho case MODULE_CMD_FINI: 2621.31Sjruoho 2631.31Sjruoho err = config_cfdata_detach(acpibut_cfdata); 2641.31Sjruoho 2651.31Sjruoho if (err != 0) 2661.31Sjruoho return err; 2671.31Sjruoho 2681.31Sjruoho config_cfattach_detach("acpibut", &acpibut_ca); 2691.31Sjruoho config_cfdriver_detach(&acpibut_cd); 2701.31Sjruoho 2711.31Sjruoho return 0; 2721.31Sjruoho 2731.31Sjruoho default: 2741.31Sjruoho return ENOTTY; 2751.31Sjruoho } 2761.31Sjruoho} 2771.31Sjruoho 2781.31Sjruoho#endif /* _MODULE */ 279