1 /* $NetBSD: asus_acpi.c,v 1.6 2008/09/21 21:15:28 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2008 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: asus_acpi.c,v 1.6 2008/09/21 21:15:28 jmcneill Exp $"); 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/malloc.h> 35 #include <sys/buf.h> 36 #include <sys/callout.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 #include <sys/pmf.h> 40 41 #include <dev/acpi/acpivar.h> 42 43 typedef struct asus_softc { 44 device_t sc_dev; 45 struct acpi_devnode *sc_node; 46 47 #define ASUS_PSW_DISPLAY_CYCLE 0 48 #define ASUS_PSW_LAST 1 49 struct sysmon_pswitch sc_smpsw[ASUS_PSW_LAST]; 50 bool sc_smpsw_valid; 51 52 ACPI_INTEGER sc_brightness; 53 } asus_softc_t; 54 55 #define ASUS_NOTIFY_WirelessSwitch 0x10 56 #define ASUS_NOTIFY_BrightnessLow 0x20 57 #define ASUS_NOTIFY_BrightnessHigh 0x2f 58 #define ASUS_NOTIFY_DisplayCycle 0x30 59 #define ASUS_NOTIFY_WindowSwitch 0x12 /* XXXJDM ?? */ 60 #define ASUS_NOTIFY_VolumeMute 0x13 61 #define ASUS_NOTIFY_VolumeDown 0x14 62 #define ASUS_NOTIFY_VolumeUp 0x15 63 64 #define ASUS_METHOD_SDSP "SDSP" 65 #define ASUS_SDSP_LCD 0x01 66 #define ASUS_SDSP_CRT 0x02 67 #define ASUS_SDSP_TV 0x04 68 #define ASUS_SDSP_DVI 0x08 69 #define ASUS_SDSP_ALL \ 70 (ASUS_SDSP_LCD | ASUS_SDSP_CRT | ASUS_SDSP_TV | ASUS_SDSP_DVI) 71 #define ASUS_METHOD_PBLG "PBLG" 72 #define ASUS_METHOD_PBLS "PBLS" 73 74 static int asus_match(device_t, cfdata_t, void *); 75 static void asus_attach(device_t, device_t, void *); 76 77 static void asus_notify_handler(ACPI_HANDLE, UINT32, void *); 78 79 static void asus_init(device_t); 80 static bool asus_suspend(device_t PMF_FN_PROTO); 81 static bool asus_resume(device_t PMF_FN_PROTO); 82 83 CFATTACH_DECL_NEW(asus, sizeof(asus_softc_t), 84 asus_match, asus_attach, NULL, NULL); 85 86 static const char * const asus_ids[] = { 87 "ASUS010", 88 NULL 89 }; 90 91 static int 92 asus_match(device_t parent, cfdata_t match, void *opaque) 93 { 94 struct acpi_attach_args *aa = opaque; 95 96 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 97 return 0; 98 99 return acpi_match_hid(aa->aa_node->ad_devinfo, asus_ids); 100 } 101 102 static void 103 asus_attach(device_t parent, device_t self, void *opaque) 104 { 105 asus_softc_t *sc = device_private(self); 106 struct acpi_attach_args *aa = opaque; 107 ACPI_STATUS rv; 108 109 sc->sc_node = aa->aa_node; 110 sc->sc_dev = self; 111 112 aprint_naive("\n"); 113 aprint_normal("\n"); 114 115 asus_init(self); 116 117 sc->sc_smpsw_valid = true; 118 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_name = 119 PSWITCH_HK_DISPLAY_CYCLE; 120 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_type = 121 PSWITCH_TYPE_HOTKEY; 122 if (sysmon_pswitch_register(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE])) { 123 aprint_error_dev(self, "couldn't register with sysmon\n"); 124 sc->sc_smpsw_valid = false; 125 } 126 127 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, ACPI_ALL_NOTIFY, 128 asus_notify_handler, sc); 129 if (ACPI_FAILURE(rv)) 130 aprint_error_dev(self, "couldn't install notify handler: %s\n", 131 AcpiFormatException(rv)); 132 133 if (!pmf_device_register(self, asus_suspend, asus_resume)) 134 aprint_error_dev(self, "couldn't establish power handler\n"); 135 } 136 137 static void 138 asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque) 139 { 140 asus_softc_t *sc = opaque; 141 142 if (notify >= ASUS_NOTIFY_BrightnessLow && 143 notify <= ASUS_NOTIFY_BrightnessHigh) { 144 aprint_debug_dev(sc->sc_dev, "brightness %d percent\n", 145 (notify & 0xf) * 100 / 0xf); 146 return; 147 } 148 149 switch (notify) { 150 case ASUS_NOTIFY_WirelessSwitch: /* handled by AML */ 151 case ASUS_NOTIFY_WindowSwitch: /* XXXJDM what is this? */ 152 break; 153 case ASUS_NOTIFY_DisplayCycle: 154 if (sc->sc_smpsw_valid == false) 155 break; 156 sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE], 157 PSWITCH_EVENT_PRESSED); 158 break; 159 case ASUS_NOTIFY_VolumeMute: 160 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE); 161 break; 162 case ASUS_NOTIFY_VolumeDown: 163 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN); 164 break; 165 case ASUS_NOTIFY_VolumeUp: 166 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP); 167 break; 168 default: 169 aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify); 170 break; 171 } 172 } 173 174 static void 175 asus_init(device_t self) 176 { 177 asus_softc_t *sc = device_private(self); 178 ACPI_STATUS rv; 179 ACPI_OBJECT param; 180 ACPI_OBJECT_LIST params; 181 ACPI_BUFFER ret; 182 183 ret.Pointer = NULL; 184 ret.Length = ACPI_ALLOCATE_BUFFER; 185 param.Type = ACPI_TYPE_INTEGER; 186 param.Integer.Value = 0x40; /* disable ASL display switching */ 187 params.Pointer = ¶m; 188 params.Count = 1; 189 190 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "INIT", 191 ¶ms, &ret); 192 if (ACPI_FAILURE(rv)) 193 aprint_error_dev(self, "couldn't evaluate INIT: %s\n", 194 AcpiFormatException(rv)); 195 196 if (ret.Pointer) 197 AcpiOsFree(ret.Pointer); 198 } 199 200 static bool 201 asus_suspend(device_t self PMF_FN_ARGS) 202 { 203 asus_softc_t *sc = device_private(self); 204 ACPI_STATUS rv; 205 206 /* capture display brightness when we're sleeping */ 207 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG, 208 &sc->sc_brightness); 209 if (ACPI_FAILURE(rv)) 210 aprint_error_dev(sc->sc_dev, "couldn't evaluate PBLG: %s\n", 211 AcpiFormatException(rv)); 212 213 return true; 214 } 215 216 static bool 217 asus_resume(device_t self PMF_FN_ARGS) 218 { 219 asus_softc_t *sc = device_private(self); 220 ACPI_STATUS rv; 221 ACPI_OBJECT param; 222 ACPI_OBJECT_LIST params; 223 ACPI_BUFFER ret; 224 225 asus_init(self); 226 227 /* restore previous display brightness */ 228 ret.Pointer = NULL; 229 ret.Length = ACPI_ALLOCATE_BUFFER; 230 param.Type = ACPI_TYPE_INTEGER; 231 param.Integer.Value = sc->sc_brightness; 232 params.Pointer = ¶m; 233 params.Count = 1; 234 235 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, ASUS_METHOD_PBLS, 236 ¶ms, &ret); 237 if (ACPI_FAILURE(rv)) 238 aprint_error_dev(self, "couldn't evaluate PBLS: %s\n", 239 AcpiFormatException(rv)); 240 241 return true; 242 } 243