| 1 | /* $NetBSD: acpi_lid.c,v 1.44 2015/04/23 23:23:00 pgoyette 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.44 2015/04/23 23:23:00 pgoyette 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 |
| 54 | ACPI_MODULE_NAME ("acpi_lid" ) |
| 55 | |
| 56 | #define ACPI_NOTIFY_LID 0x80 |
| 57 | |
| 58 | struct acpilid_softc { |
| 59 | struct acpi_devnode *sc_node; |
| 60 | struct sysmon_pswitch sc_smpsw; |
| 61 | uint64_t sc_status; |
| 62 | }; |
| 63 | |
| 64 | static const char * const lid_hid[] = { |
| 65 | "PNP0C0D" , |
| 66 | NULL |
| 67 | }; |
| 68 | |
| 69 | static int acpilid_match(device_t, cfdata_t, void *); |
| 70 | static void acpilid_attach(device_t, device_t, void *); |
| 71 | static int acpilid_detach(device_t, int); |
| 72 | static void acpilid_status_changed(void *); |
| 73 | static void acpilid_notify_handler(ACPI_HANDLE, uint32_t, void *); |
| 74 | |
| 75 | CFATTACH_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 | */ |
| 83 | static int |
| 84 | acpilid_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 | */ |
| 99 | static void |
| 100 | acpilid_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 | |
| 118 | static int |
| 119 | acpilid_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 | */ |
| 135 | static void |
| 136 | acpilid_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 | */ |
| 156 | static void |
| 157 | acpilid_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 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | aprint_debug_dev(dv, "unknown notify 0x%02X\n" , notify); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | MODULE(MODULE_CLASS_DRIVER, acpilid, "sysmon_power" ); |
| 177 | |
| 178 | #ifdef _MODULE |
| 179 | #include "ioconf.c" |
| 180 | #endif |
| 181 | |
| 182 | static int |
| 183 | acpilid_modcmd(modcmd_t cmd, void *aux) |
| 184 | { |
| 185 | int rv = 0; |
| 186 | |
| 187 | switch (cmd) { |
| 188 | |
| 189 | case MODULE_CMD_INIT: |
| 190 | |
| 191 | #ifdef _MODULE |
| 192 | rv = config_init_component(cfdriver_ioconf_acpilid, |
| 193 | cfattach_ioconf_acpilid, cfdata_ioconf_acpilid); |
| 194 | #endif |
| 195 | break; |
| 196 | |
| 197 | case MODULE_CMD_FINI: |
| 198 | |
| 199 | #ifdef _MODULE |
| 200 | rv = config_fini_component(cfdriver_ioconf_acpilid, |
| 201 | cfattach_ioconf_acpilid, cfdata_ioconf_acpilid); |
| 202 | #endif |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | rv = ENOTTY; |
| 207 | } |
| 208 | |
| 209 | return rv; |
| 210 | } |
| 211 | |