Home | History | Annotate | Line # | Download | only in wmi
wmi_msi.c revision 1.1
      1 /*	$NetBSD: wmi_msi.c,v 1.1 2010/10/24 08:54:15 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: wmi_msi.c,v 1.1 2010/10/24 08:54:15 jruoho Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 
     39 #include <dev/acpi/acpireg.h>
     40 #include <dev/acpi/acpivar.h>
     41 #include <dev/acpi/wmi/wmi_acpivar.h>
     42 
     43 #define _COMPONENT			ACPI_RESOURCE_COMPONENT
     44 ACPI_MODULE_NAME			("wmi_msi")
     45 
     46 #define WMI_MSI_HOTKEY_BRIGHTNESS_UP	0xD0
     47 #define WMI_MSI_HOTKEY_BRIGHTNESS_DOWN	0xD1
     48 #define WMI_MSI_HOTKEY_VOLUME_UP	0xD2
     49 #define WMI_MSI_HOTKEY_VOLUME_DOWN	0xD3
     50 /*      WMI_MSI_HOTKEY_UNKNOWN		0xXXXX */
     51 
     52 #define WMI_MSI_GUID_EVENT		"B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
     53 
     54 struct wmi_msi_softc {
     55 	device_t		sc_dev;
     56 	device_t		sc_parent;
     57 };
     58 
     59 static int	wmi_msi_match(device_t, cfdata_t, void *);
     60 static void	wmi_msi_attach(device_t, device_t, void *);
     61 static int	wmi_msi_detach(device_t, int);
     62 static void	wmi_msi_notify_handler(ACPI_HANDLE, uint32_t, void *);
     63 static bool	wmi_msi_suspend(device_t, const pmf_qual_t *);
     64 static bool	wmi_msi_resume(device_t, const pmf_qual_t *);
     65 
     66 CFATTACH_DECL_NEW(wmimsi, sizeof(struct wmi_msi_softc),
     67     wmi_msi_match, wmi_msi_attach, wmi_msi_detach, NULL);
     68 
     69 static int
     70 wmi_msi_match(device_t parent, cfdata_t match, void *aux)
     71 {
     72 	return acpi_wmi_guid_match(parent, WMI_MSI_GUID_EVENT);
     73 }
     74 
     75 static void
     76 wmi_msi_attach(device_t parent, device_t self, void *aux)
     77 {
     78 	struct wmi_msi_softc *sc = device_private(self);
     79 	ACPI_STATUS rv;
     80 
     81 	sc->sc_dev = self;
     82 	sc->sc_parent = parent;
     83 
     84 	rv = acpi_wmi_event_register(parent, wmi_msi_notify_handler);
     85 
     86 	if (ACPI_FAILURE(rv)) {
     87 		aprint_error(": failed to install WMI notify handler\n");
     88 		return;
     89 	}
     90 
     91 	aprint_naive("\n");
     92 	aprint_normal(": MSI WMI mappings\n");
     93 
     94 	(void)pmf_device_register(self, wmi_msi_suspend, wmi_msi_resume);
     95 }
     96 
     97 static int
     98 wmi_msi_detach(device_t self, int flags)
     99 {
    100 	struct wmi_msi_softc *sc = device_private(self);
    101 	device_t parent = sc->sc_parent;
    102 
    103 	(void)pmf_device_deregister(self);
    104 	(void)acpi_wmi_event_deregister(parent);
    105 
    106 	return 0;
    107 }
    108 
    109 static bool
    110 wmi_msi_suspend(device_t self, const pmf_qual_t *qual)
    111 {
    112 	struct wmi_msi_softc *sc = device_private(self);
    113 	device_t parent = sc->sc_parent;
    114 
    115 	(void)acpi_wmi_event_deregister(parent);
    116 
    117 	return true;
    118 }
    119 
    120 static bool
    121 wmi_msi_resume(device_t self, const pmf_qual_t *qual)
    122 {
    123 	struct wmi_msi_softc *sc = device_private(self);
    124 	device_t parent = sc->sc_parent;
    125 
    126 	(void)acpi_wmi_event_register(parent, wmi_msi_notify_handler);
    127 
    128 	return true;
    129 }
    130 
    131 static void
    132 wmi_msi_notify_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux)
    133 {
    134 	struct wmi_msi_softc *sc;
    135 	device_t self = aux;
    136 	ACPI_OBJECT *obj;
    137 	ACPI_BUFFER buf;
    138 	ACPI_STATUS rv;
    139 	uint32_t val;
    140 
    141 	buf.Pointer = NULL;
    142 
    143 	sc = device_private(self);
    144 	rv = acpi_wmi_event_get(sc->sc_parent, evt, &buf);
    145 
    146 	if (ACPI_FAILURE(rv))
    147 		goto out;
    148 
    149 	obj = buf.Pointer;
    150 
    151 	if (obj->Type != ACPI_TYPE_INTEGER) {
    152 		rv = AE_TYPE;
    153 		goto out;
    154 	}
    155 
    156 	if (obj->Integer.Value > UINT32_MAX) {
    157 		rv = AE_AML_NUMERIC_OVERFLOW;
    158 		goto out;
    159 	}
    160 
    161 	val = obj->Integer.Value;
    162 
    163 	switch (val) {
    164 
    165 	case WMI_MSI_HOTKEY_BRIGHTNESS_DOWN:
    166 		pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_DOWN);
    167 		break;
    168 
    169 	case WMI_MSI_HOTKEY_BRIGHTNESS_UP:
    170 		pmf_event_inject(NULL, PMFE_DISPLAY_BRIGHTNESS_UP);
    171 		break;
    172 
    173 	case WMI_MSI_HOTKEY_VOLUME_DOWN:
    174 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    175 		break;
    176 
    177 	case WMI_MSI_HOTKEY_VOLUME_UP:
    178 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    179 		break;
    180 
    181 	default:
    182 		aprint_normal_dev(sc->sc_dev,
    183 		    "unknown key 0x%02X for event 0x%02X\n", val, evt);
    184 		break;
    185 	}
    186 
    187 out:
    188 	if (buf.Pointer != NULL)
    189 		ACPI_FREE(buf.Pointer);
    190 
    191 	if (ACPI_FAILURE(rv))
    192 		aprint_error_dev(sc->sc_dev, "failed to get data for "
    193 		    "event 0x%02X: %s\n", evt, AcpiFormatException(rv));
    194 }
    195