acpi_lid.c revision 1.40
1/* $NetBSD: acpi_lid.c,v 1.40 2010/04/27 05:57:43 jruoho Exp $ */ 2 3/* 4 * Copyright 2001, 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38/* 39 * ACPI Lid Switch driver. 40 */ 41 42#include <sys/cdefs.h> 43__KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.40 2010/04/27 05:57:43 jruoho Exp $"); 44 45#include <sys/param.h> 46#include <sys/device.h> 47#include <sys/module.h> 48#include <sys/systm.h> 49 50#include <dev/acpi/acpireg.h> 51#include <dev/acpi/acpivar.h> 52 53#define _COMPONENT ACPI_LID_COMPONENT 54ACPI_MODULE_NAME ("acpi_lid") 55 56#define ACPI_NOTIFY_LID 0x80 57 58struct acpilid_softc { 59 struct acpi_devnode *sc_node; 60 struct sysmon_pswitch sc_smpsw; 61 uint64_t sc_status; 62}; 63 64static const char * const lid_hid[] = { 65 "PNP0C0D", 66 NULL 67}; 68 69static int acpilid_match(device_t, cfdata_t, void *); 70static void acpilid_attach(device_t, device_t, void *); 71static int acpilid_detach(device_t, int); 72static void acpilid_status_changed(void *); 73static void acpilid_notify_handler(ACPI_HANDLE, uint32_t, void *); 74 75CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc), 76 acpilid_match, acpilid_attach, acpilid_detach, NULL); 77 78/* 79 * acpilid_match: 80 * 81 * Autoconfiguration `match' routine. 82 */ 83static int 84acpilid_match(device_t parent, cfdata_t match, void *aux) 85{ 86 struct acpi_attach_args *aa = aux; 87 88 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 89 return 0; 90 91 return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid); 92} 93 94/* 95 * acpilid_attach: 96 * 97 * Autoconfiguration `attach' routine. 98 */ 99static void 100acpilid_attach(device_t parent, device_t self, void *aux) 101{ 102 struct acpilid_softc *sc = device_private(self); 103 struct acpi_attach_args *aa = aux; 104 105 aprint_naive(": ACPI Lid Switch\n"); 106 aprint_normal(": ACPI Lid Switch\n"); 107 108 sc->sc_node = aa->aa_node; 109 110 sc->sc_smpsw.smpsw_name = device_xname(self); 111 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID; 112 113 (void)pmf_device_register(self, NULL, NULL); 114 (void)sysmon_pswitch_register(&sc->sc_smpsw); 115 (void)acpi_register_notify(sc->sc_node, acpilid_notify_handler); 116} 117 118static int 119acpilid_detach(device_t self, int flags) 120{ 121 struct acpilid_softc *sc = device_private(self); 122 123 pmf_device_deregister(self); 124 acpi_deregister_notify(sc->sc_node); 125 sysmon_pswitch_unregister(&sc->sc_smpsw); 126 127 return 0; 128} 129 130/* 131 * acpilid_status_changed: 132 * 133 * Get, and possibly display, the lid status, and take action. 134 */ 135static void 136acpilid_status_changed(void *arg) 137{ 138 device_t dv = arg; 139 struct acpilid_softc *sc = device_private(dv); 140 ACPI_STATUS rv; 141 142 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &sc->sc_status); 143 144 if (ACPI_FAILURE(rv)) 145 return; 146 147 sysmon_pswitch_event(&sc->sc_smpsw, (sc->sc_status == 0) ? 148 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 149} 150 151/* 152 * acpilid_notify_handler: 153 * 154 * Callback from ACPI interrupt handler to notify us of an event. 155 */ 156static void 157acpilid_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 158{ 159 static const int handler = OSL_NOTIFY_HANDLER; 160 device_t dv = context; 161 162 switch (notify) { 163 164 case ACPI_NOTIFY_LID: 165 (void)AcpiOsExecute(handler, acpilid_status_changed, dv); 166 break; 167 168 default: 169 aprint_error_dev(dv, "unknown notify 0x%02X\n", notify); 170 } 171} 172 173#ifdef _MODULE 174 175MODULE(MODULE_CLASS_DRIVER, acpilid, NULL); 176CFDRIVER_DECL(acpilid, DV_DULL, NULL); 177 178static int acpilidloc[] = { -1 }; 179extern struct cfattach acpilid_ca; 180 181static struct cfparent acpiparent = { 182 "acpinodebus", NULL, DVUNIT_ANY 183}; 184 185static struct cfdata acpilid_cfdata[] = { 186 { 187 .cf_name = "acpilid", 188 .cf_atname = "acpilid", 189 .cf_unit = 0, 190 .cf_fstate = FSTATE_STAR, 191 .cf_loc = acpilidloc, 192 .cf_flags = 0, 193 .cf_pspec = &acpiparent, 194 }, 195 196 { NULL } 197}; 198 199static int 200acpilid_modcmd(modcmd_t cmd, void *context) 201{ 202 int err; 203 204 switch (cmd) { 205 206 case MODULE_CMD_INIT: 207 208 err = config_cfdriver_attach(&acpilid_cd); 209 210 if (err != 0) 211 return err; 212 213 err = config_cfattach_attach("acpilid", &acpilid_ca); 214 215 if (err != 0) { 216 config_cfdriver_detach(&acpilid_cd); 217 return err; 218 } 219 220 err = config_cfdata_attach(acpilid_cfdata, 1); 221 222 if (err != 0) { 223 config_cfattach_detach("acpilid", &acpilid_ca); 224 config_cfdriver_detach(&acpilid_cd); 225 return err; 226 } 227 228 return 0; 229 230 case MODULE_CMD_FINI: 231 232 err = config_cfdata_detach(acpilid_cfdata); 233 234 if (err != 0) 235 return err; 236 237 config_cfattach_detach("acpilid", &acpilid_ca); 238 config_cfdriver_detach(&acpilid_cd); 239 240 return 0; 241 242 default: 243 return ENOTTY; 244 } 245} 246 247#endif /* _MODULE */ 248