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