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