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