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