Home | History | Annotate | Line # | Download | only in acpi
sony_acpi.c revision 1.9
      1 /*	$NetBSD: sony_acpi.c,v 1.9 2009/08/18 16:41:02 jmcneill Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      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  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: sony_acpi.c,v 1.9 2009/08/18 16:41:02 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/proc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/callout.h>
     40 #include <sys/sysctl.h>
     41 
     42 #include <machine/bus.h>
     43 
     44 #include <dev/acpi/acpica.h>
     45 #include <dev/acpi/acpivar.h>
     46 
     47 #define	SONY_NOTIFY_FnKeyEvent			0x92
     48 #define	SONY_NOTIFY_BrightnessDownPressed	0x85
     49 #define	SONY_NOTIFY_BrightnessDownReleased	0x05
     50 #define	SONY_NOTIFY_BrightnessUpPressed		0x86
     51 #define	SONY_NOTIFY_BrightnessUpReleased	0x06
     52 #define	SONY_NOTIFY_DisplaySwitchPressed	0x87
     53 #define	SONY_NOTIFY_DisplaySwitchReleased	0x07
     54 #define	SONY_NOTIFY_ZoomPressed			0x8a
     55 #define	SONY_NOTIFY_ZoomReleased		0x0a
     56 #define	SONY_NOTIFY_SuspendPressed		0x8c
     57 #define	SONY_NOTIFY_SuspendReleased		0x0c
     58 
     59 struct sony_acpi_softc {
     60 	device_t sc_dev;
     61 	struct sysctllog *sc_log;
     62 	struct acpi_devnode *sc_node;
     63 
     64 #define	SONY_PSW_SLEEP		0
     65 #define	SONY_PSW_DISPLAY_CYCLE	1
     66 #define	SONY_PSW_ZOOM		2
     67 #define	SONY_PSW_LAST		3
     68 	struct sysmon_pswitch sc_smpsw[SONY_PSW_LAST];
     69 	int sc_smpsw_valid;
     70 
     71 #define	SONY_ACPI_QUIRK_FNINIT	0x01
     72 	int sc_quirks;
     73 	bool sc_has_pic;
     74 
     75 	struct sony_acpi_pmstate {
     76 		ACPI_INTEGER	brt;
     77 	} sc_pmstate;
     78 };
     79 
     80 static const char * const sony_acpi_ids[] = {
     81 	"SNY5001",
     82 	NULL
     83 };
     84 
     85 static int	sony_acpi_match(device_t, cfdata_t, void *);
     86 static void	sony_acpi_attach(device_t, device_t, void *);
     87 static ACPI_STATUS sony_acpi_eval_set_integer(ACPI_HANDLE, const char *,
     88     ACPI_INTEGER, ACPI_INTEGER *);
     89 static void	sony_acpi_quirk_setup(struct sony_acpi_softc *);
     90 static void	sony_acpi_notify_handler(ACPI_HANDLE, UINT32, void *);
     91 static bool	sony_acpi_suspend(device_t PMF_FN_PROTO);
     92 static bool	sony_acpi_resume(device_t PMF_FN_PROTO);
     93 static void	sony_acpi_brightness_down(device_t);
     94 static void	sony_acpi_brightness_up(device_t);
     95 static ACPI_STATUS sony_acpi_find_pic(ACPI_HANDLE, UINT32, void *, void **);
     96 
     97 CFATTACH_DECL_NEW(sony_acpi, sizeof(struct sony_acpi_softc),
     98     sony_acpi_match, sony_acpi_attach, NULL, NULL);
     99 
    100 static int
    101 sony_acpi_match(device_t parent, cfdata_t match, void *aux)
    102 {
    103 	struct acpi_attach_args *aa = aux;
    104 
    105 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    106 		return 0;
    107 
    108 	return acpi_match_hid(aa->aa_node->ad_devinfo, sony_acpi_ids);
    109 }
    110 
    111 static int
    112 sony_sysctl_helper(SYSCTLFN_ARGS)
    113 {
    114 	struct sysctlnode node;
    115 	ACPI_INTEGER acpi_val;
    116 	ACPI_STATUS rv;
    117 	int val, old_val, error;
    118 	char buf[SYSCTL_NAMELEN + 1], *ptr;
    119 	struct sony_acpi_softc *sc = rnode->sysctl_data;
    120 
    121 	(void)snprintf(buf, sizeof(buf), "G%s", rnode->sysctl_name);
    122 	for (ptr = buf; *ptr; ptr++)
    123 		*ptr = toupper((unsigned char)*ptr);
    124 
    125 	rv = acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val);
    126 	if (ACPI_FAILURE(rv)) {
    127 #ifdef DIAGNOSTIC
    128 		printf("%s: couldn't get `%s'\n", device_xname(sc->sc_dev), buf);
    129 #endif
    130 		return EIO;
    131 	}
    132 	val = old_val = acpi_val;
    133 
    134 	node = *rnode;
    135 	node.sysctl_data = &val;
    136 
    137 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    138 	if (error || newp == NULL)
    139 		return error;
    140 
    141 	buf[0] = 'S';
    142 	acpi_val = val;
    143 	rv = sony_acpi_eval_set_integer(sc->sc_node->ad_handle, buf,
    144 	    acpi_val, NULL);
    145 	if (ACPI_FAILURE(rv)) {
    146 #ifdef DIAGNOSTIC
    147 		printf("%s: couldn't set `%s' to %d\n",
    148 		    device_xname(sc->sc_dev), buf, val);
    149 #endif
    150 		return EIO;
    151 	}
    152 	return 0;
    153 }
    154 
    155 static ACPI_STATUS
    156 sony_walk_cb(ACPI_HANDLE hnd, UINT32 v, void *context, void **status)
    157 {
    158 	struct sony_acpi_softc *sc = (void *)context;
    159 	const struct sysctlnode *node, *snode;
    160 	const char *name = acpi_name(hnd);
    161 	ACPI_INTEGER acpi_val;
    162 	char buf[SYSCTL_NAMELEN + 1], *ptr;
    163 	int rv;
    164 
    165 	if ((name = strrchr(name, '.')) == NULL)
    166 		return AE_OK;
    167 
    168 	name++;
    169 	if ((*name != 'G') && (*name != 'S'))
    170 		return AE_OK;
    171 
    172 	(void)strlcpy(buf, name, sizeof(buf));
    173 	*buf = 'G';
    174 
    175 	/*
    176 	 * We assume that if the 'get' of the name as an integer is
    177 	 * successful it is ok.
    178 	 */
    179 	if (acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val))
    180 		return AE_OK;
    181 
    182 	for (ptr = buf; *ptr; ptr++)
    183 		*ptr = tolower(*ptr);
    184 
    185 	if ((rv = sysctl_createv(&sc->sc_log, 0, NULL, &node, CTLFLAG_PERMANENT,
    186 	    CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
    187 		goto out;
    188 
    189 	if ((rv = sysctl_createv(&sc->sc_log, 0, &node, &snode, 0,
    190 	    CTLTYPE_NODE, device_xname(sc->sc_dev),
    191 	    SYSCTL_DESCR("sony controls"),
    192 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    193 		goto out;
    194 
    195 	if ((rv = sysctl_createv(&sc->sc_log, 0, &snode, &node,
    196 	    CTLFLAG_READWRITE, CTLTYPE_INT, buf + 1, NULL,
    197 	    sony_sysctl_helper, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
    198 		goto out;
    199 
    200 out:
    201 #ifdef DIAGNOSTIC
    202 	if (rv)
    203 		printf("%s: sysctl_createv failed (rv = %d)\n",
    204 		    device_xname(sc->sc_dev), rv);
    205 #endif
    206 	return AE_OK;
    207 }
    208 
    209 ACPI_STATUS
    210 sony_acpi_eval_set_integer(ACPI_HANDLE handle, const char *path,
    211     ACPI_INTEGER val, ACPI_INTEGER *valp)
    212 {
    213 	ACPI_STATUS rv;
    214 	ACPI_BUFFER buf;
    215 	ACPI_OBJECT param, ret_val;
    216 	ACPI_OBJECT_LIST params;
    217 
    218 	if (handle == NULL)
    219 		handle = ACPI_ROOT_OBJECT;
    220 
    221 	params.Count = 1;
    222 	params.Pointer = &param;
    223 
    224 	param.Type = ACPI_TYPE_INTEGER;
    225 	param.Integer.Value = val;
    226 
    227 	buf.Pointer = &ret_val;
    228 	buf.Length = sizeof(ret_val);
    229 
    230 	rv = AcpiEvaluateObjectTyped(handle, path, &params, &buf,
    231 	    ACPI_TYPE_INTEGER);
    232 
    233 	if (ACPI_SUCCESS(rv) && valp)
    234 		*valp = ret_val.Integer.Value;
    235 
    236 	return rv;
    237 }
    238 
    239 static void
    240 sony_acpi_attach(device_t parent, device_t self, void *aux)
    241 {
    242 	struct sony_acpi_softc *sc = device_private(self);
    243 	struct acpi_attach_args *aa = aux;
    244 	ACPI_STATUS rv;
    245 	int i;
    246 
    247 	aprint_naive(": Sony Miscellaneous Controller\n");
    248 	aprint_normal(": Sony Miscellaneous Controller\n");
    249 
    250 	sc->sc_node = aa->aa_node;
    251 	sc->sc_dev = self;
    252 
    253 	rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100,
    254 	    sony_acpi_find_pic, sc, NULL);
    255 	if (ACPI_FAILURE(rv))
    256 		aprint_error_dev(self, "couldn't walk namespace: %s\n",
    257 		    AcpiFormatException(rv));
    258 
    259 	/*
    260 	 * If we don't find an SNY6001 device, assume that we need the
    261 	 * Fn key initialization sequence.
    262 	 */
    263 	if (sc->sc_has_pic == false)
    264 		sc->sc_quirks |= SONY_ACPI_QUIRK_FNINIT;
    265 
    266 	sony_acpi_quirk_setup(sc);
    267 
    268 	/* Configure suspend button and hotkeys */
    269 	sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_name = device_xname(self);
    270 	sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
    271 	sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_name =
    272 	    PSWITCH_HK_DISPLAY_CYCLE;
    273 	sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_type = PSWITCH_TYPE_HOTKEY;
    274 	sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
    275 	sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_type = PSWITCH_TYPE_HOTKEY;
    276 	sc->sc_smpsw_valid = 1;
    277 
    278 	for (i = 0; i < SONY_PSW_LAST; i++)
    279 		if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
    280 			aprint_error_dev(self,
    281 			    "couldn't register %s with sysmon\n",
    282 			    sc->sc_smpsw[i].smpsw_name);
    283 			sc->sc_smpsw_valid = 0;
    284 		}
    285 
    286 	/* Install notify handler */
    287 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
    288 	    ACPI_DEVICE_NOTIFY, sony_acpi_notify_handler, self);
    289 	if (ACPI_FAILURE(rv))
    290 		aprint_error_dev(self,
    291 		    "couldn't install notify handler (%d)\n", rv);
    292 
    293 	/* Install sysctl handler */
    294 	rv = AcpiWalkNamespace(ACPI_TYPE_METHOD,
    295 	    sc->sc_node->ad_handle, 1, sony_walk_cb, sc, NULL);
    296 #ifdef DIAGNOSTIC
    297 	if (ACPI_FAILURE(rv))
    298 		aprint_error_dev(self, "Cannot walk ACPI namespace (%d)\n",
    299 		    rv);
    300 #endif
    301 
    302 	if (!pmf_device_register(self, sony_acpi_suspend, sony_acpi_resume))
    303 		aprint_error_dev(self, "couldn't establish power handler\n");
    304 
    305 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
    306 				 sony_acpi_brightness_up, true))
    307 		aprint_error_dev(self, "couldn't register BRIGHTNESS UP handler\n");
    308 
    309 	if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
    310 				 sony_acpi_brightness_down, true))
    311 		aprint_error_dev(self, "couldn't register BRIGHTNESS DOWN handler\n");
    312 }
    313 
    314 static void
    315 sony_acpi_quirk_setup(struct sony_acpi_softc *sc)
    316 {
    317 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
    318 
    319 	if (sc->sc_quirks & SONY_ACPI_QUIRK_FNINIT) {
    320 		/* Initialize extra Fn keys */
    321 		sony_acpi_eval_set_integer(hdl, "SN02", 0x04, NULL);
    322 		sony_acpi_eval_set_integer(hdl, "SN07", 0x02, NULL);
    323 		sony_acpi_eval_set_integer(hdl, "SN02", 0x10, NULL);
    324 		sony_acpi_eval_set_integer(hdl, "SN07", 0x00, NULL);
    325 		sony_acpi_eval_set_integer(hdl, "SN03", 0x02, NULL);
    326 		sony_acpi_eval_set_integer(hdl, "SN07", 0x101, NULL);
    327 	}
    328 }
    329 
    330 static void
    331 sony_acpi_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    332 {
    333 	device_t dv = opaque;
    334 	struct sony_acpi_softc *sc = device_private(dv);
    335 	ACPI_STATUS rv;
    336 	ACPI_INTEGER arg;
    337 	int s;
    338 
    339 	if (notify == SONY_NOTIFY_FnKeyEvent) {
    340 		rv = sony_acpi_eval_set_integer(hdl, "SN07", 0x202, &arg);
    341 		if (ACPI_FAILURE(rv))
    342 			return;
    343 
    344 		notify = arg & 0xff;
    345 	}
    346 
    347 	s = spltty();
    348 	switch (notify) {
    349 	case SONY_NOTIFY_BrightnessDownPressed:
    350 		sony_acpi_brightness_down(dv);
    351 		break;
    352 	case SONY_NOTIFY_BrightnessUpPressed:
    353 		sony_acpi_brightness_up(dv);
    354 		break;
    355 	case SONY_NOTIFY_BrightnessDownReleased:
    356 	case SONY_NOTIFY_BrightnessUpReleased:
    357 		break;
    358 	case SONY_NOTIFY_SuspendPressed:
    359 		if (!sc->sc_smpsw_valid)
    360 			break;
    361 		sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_SLEEP],
    362 		    PSWITCH_EVENT_PRESSED);
    363 		break;
    364 	case SONY_NOTIFY_SuspendReleased:
    365 		break;
    366 	case SONY_NOTIFY_DisplaySwitchPressed:
    367 		if (!sc->sc_smpsw_valid)
    368 			break;
    369 		sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE],
    370 		    PSWITCH_EVENT_PRESSED);
    371 		break;
    372 	case SONY_NOTIFY_DisplaySwitchReleased:
    373 		break;
    374 	case SONY_NOTIFY_ZoomPressed:
    375 		if (!sc->sc_smpsw_valid)
    376 			break;
    377 		sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_ZOOM],
    378 		    PSWITCH_EVENT_PRESSED);
    379 		break;
    380 	case SONY_NOTIFY_ZoomReleased:
    381 		break;
    382 	default:
    383 		aprint_debug_dev(dv, "unknown notify event 0x%x\n",
    384 		    notify);
    385 		break;
    386 	}
    387 	splx(s);
    388 }
    389 
    390 static bool
    391 sony_acpi_suspend(device_t dv PMF_FN_ARGS)
    392 {
    393 	struct sony_acpi_softc *sc = device_private(dv);
    394 
    395 	acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &sc->sc_pmstate.brt);
    396 
    397 	return true;
    398 }
    399 
    400 static bool
    401 sony_acpi_resume(device_t dv PMF_FN_ARGS)
    402 {
    403 	struct sony_acpi_softc *sc = device_private(dv);
    404 
    405 	sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT",
    406 	    sc->sc_pmstate.brt, NULL);
    407 	sony_acpi_quirk_setup(sc);
    408 
    409 	return true;
    410 }
    411 
    412 static void
    413 sony_acpi_brightness_up(device_t dv)
    414 {
    415 	struct sony_acpi_softc *sc = device_private(dv);
    416 	ACPI_INTEGER arg;
    417 	ACPI_STATUS rv;
    418 
    419 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
    420 	if (ACPI_FAILURE(rv))
    421 		return;
    422 	if (arg >= 8)
    423 		arg = 8;
    424 	else
    425 		arg++;
    426 	sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
    427 }
    428 
    429 static void
    430 sony_acpi_brightness_down(device_t dv)
    431 {
    432 	struct sony_acpi_softc *sc = device_private(dv);
    433 	ACPI_INTEGER arg;
    434 	ACPI_STATUS rv;
    435 
    436 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
    437 	if (ACPI_FAILURE(rv))
    438 		return;
    439 	if (arg <= 0)
    440 		arg = 0;
    441 	else
    442 		arg--;
    443 	sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
    444 }
    445 
    446 static ACPI_STATUS
    447 sony_acpi_find_pic(ACPI_HANDLE hdl, UINT32 level, void *opaque, void **status)
    448 {
    449 	struct sony_acpi_softc *sc = opaque;
    450 	ACPI_STATUS rv;
    451 	ACPI_DEVICE_INFO *devinfo;
    452 
    453 	rv = AcpiGetObjectInfo(hdl, &devinfo);
    454 	if (ACPI_FAILURE(rv) || devinfo == NULL)
    455 		return AE_OK;	/* we don't want to stop searching */
    456 
    457 	if (strncmp(devinfo->HardwareId.String, "SNY6001", 7) == 0)
    458 		sc->sc_has_pic = true;
    459 
    460 	AcpiOsFree(devinfo);
    461 
    462 	return AE_OK;
    463 }
    464