/*	$NetBSD$	*/

/*-
 * Copyright (c) 2007 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Pierre Pronchery.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *        This product includes software developed by the NetBSD
 *        Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/callout.h>
#include <sys/sysctl.h>

#include <machine/bus.h>

#include <dev/acpi/acpica.h>
#include <dev/acpi/acpivar.h>

#define DIAGNOSTIC
#define IBM_DEBUG

struct ibm_acpi_softc {
        struct device sc_dev;
	struct sysctllog *sc_log;
	struct acpi_devnode *sc_node;

	int hotkey_mask_supported;
	int hotkey_status_orig;
	int hotkey_mask_orig;

	ACPI_HANDLE video_handle;

	ACPI_HANDLE bay_handle;
};

static const char * const ibm_acpi_ids[] = {
	"IBM0068",
	NULL
};

static int	ibm_acpi_match(struct device *, struct cfdata *, void *);
static void	ibm_acpi_attach(struct device *, struct device *, void *);

/* callbacks */
static int ibm_acpi_hotkey_get(struct ibm_acpi_softc *, UINT32 *, UINT32 *);
static int ibm_acpi_hotkey_set(struct ibm_acpi_softc *, UINT32, UINT32);
static int ibm_acpi_bluetooth_switch(struct ibm_acpi_softc *);
static int ibm_acpi_video_switch(struct ibm_acpi_softc *);
static int ibm_acpi_mouse_switch(struct ibm_acpi_softc *);
static int ibm_acpi_bay_eject(struct ibm_acpi_softc *);

/* helpers */
static int	ibm_acpi_eval(struct ibm_acpi_softc * sc, char const * path,
    ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret);
static ACPI_STATUS ibm_acpi_eval_set_integer(ACPI_HANDLE, const char *,
    ACPI_INTEGER, ACPI_INTEGER *);

/* wrappers */
static int	ibm_acpi_dhkn_get(struct ibm_acpi_softc *, UINT32 *);
static int	ibm_acpi_video_eval(struct ibm_acpi_softc * sc,
    char const * path, ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret);
static int	ibm_acpi_bay_eval(struct ibm_acpi_softc * sc,
    char const * path, ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret);


CFATTACH_DECL(ibm_acpi, sizeof(struct ibm_acpi_softc),
    ibm_acpi_match, ibm_acpi_attach, NULL, NULL);


/*
 * ibm_acpi_match:
 *
 * 	Autoconfiguration `match' routine.
 */
static int
ibm_acpi_match(struct device *parent, struct cfdata *match,
    void *aux)
{
	struct acpi_attach_args *aa = aux;

	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
		return 0;

	return acpi_match_hid(aa->aa_node->ad_devinfo, ibm_acpi_ids);
}


/*
 * ibm_acpi_attach:
 *
 * 	Autoconfiguration `attach' routine.
 */
static void	ibm_acpi_hotkey_notify_handler(ACPI_HANDLE, UINT32, void *);
static ACPI_STATUS ibm_walk_cb(ACPI_HANDLE hnd, UINT32 v, void *context,
    void **status);

static void
ibm_acpi_attach(struct device *parent, struct device *self, void *aux)
{
	struct ibm_acpi_softc *sc = (void *)self;
	struct acpi_attach_args *aa = aux;
	ACPI_STATUS rv;

	aprint_naive(": IBM ACPI\n");
	aprint_normal(": IBM ACPI\n");

	sc->sc_node = aa->aa_node;

	/* Install sysctl handler */
	rv = AcpiWalkNamespace(ACPI_TYPE_METHOD,
	    sc->sc_node->ad_handle, 1, ibm_walk_cb, sc, NULL);
#ifdef DIAGNOSTIC
	if (ACPI_FAILURE(rv))
		aprint_error("%s: Cannot walk ACPI namespace (%d)\n",
		    sc->sc_dev.dv_xname, rv);
#endif

	/* Install hotkey notify handler */
	sc->hotkey_mask_supported = ibm_acpi_dhkn_get(sc, NULL);
	if(ibm_acpi_hotkey_get(sc, &sc->hotkey_status_orig,
				&sc->hotkey_mask_orig))
	{
		printf("%s: hotkey supported (mask is 0x%04x)\n",
				sc->sc_dev.dv_xname, sc->hotkey_mask_orig);
		ibm_acpi_hotkey_set(sc, 1, 0xffff);
		rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
				ACPI_DEVICE_NOTIFY,
				ibm_acpi_hotkey_notify_handler, sc);
#ifdef IBM_DEBUG
		if (ACPI_FAILURE(rv))
			aprint_error("%s: can't install hotkey notify handler:"
					" %s\n", sc->sc_dev.dv_xname,
					AcpiFormatException(rv));
#endif
	}

	/* Grab video switch handle */
	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB.PCI0.AGP.VID",
			&sc->video_handle);
	if (ACPI_FAILURE(rv))
	{
		sc->video_handle = NULL;
		aprint_error("%s: can't install video switch handler:"
				" %s\n", sc->sc_dev.dv_xname,
				AcpiFormatException(rv));
	}
	else if(sc->video_handle != NULL)
		printf("%s: video switch supported\n", sc->sc_dev.dv_xname);

	/* Grab bay handle */
	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB.PCI0.SATA.SCND.MSTR",
			&sc->bay_handle);
	if (ACPI_FAILURE(rv))
		rv = AcpiGetHandle(ACPI_ROOT_OBJECT,
				"\\_SB.PCI0.IDE0.SCND.MSTR", &sc->bay_handle);
	if (ACPI_FAILURE(rv))
	{
		sc->bay_handle = NULL;
		aprint_error("%s: can't install bay handler:"
				" %s\n", sc->sc_dev.dv_xname,
				AcpiFormatException(rv));
	}
	else if(sc->video_handle != NULL)
		printf("%s: bay supported\n", sc->sc_dev.dv_xname);
}

static int	ibm_sysctl_helper(SYSCTLFN_ARGS);
static ACPI_STATUS
ibm_walk_cb(ACPI_HANDLE hnd, UINT32 v, void *context,
    void **status)
{
	struct ibm_acpi_softc *sc = (void*)context;
	const struct sysctlnode *node, *snode;
	const char *name = acpi_name(hnd);
	ACPI_INTEGER acpi_val;
	char buf[SYSCTL_NAMELEN + 1], *ptr;
	int rv;

	name++;
	if ((*name != 'G') && (*name != 'S'))
		return AE_OK;

	(void)strlcpy(buf, name, sizeof(buf));
	*buf = 'G';

	/*
	 * We assume that if the 'get' of the name as an integer is
	 * successful it is ok.
	 */
	if (acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val))
		return AE_OK;

	for (ptr = buf; *ptr; ptr++)
		*ptr = tolower(*ptr);

	if ((rv = sysctl_createv(&sc->sc_log, 0, NULL, &node, CTLFLAG_PERMANENT,
	    CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
		goto out;

	if ((rv = sysctl_createv(&sc->sc_log, 0, &node, &snode, 0,
	    CTLTYPE_NODE, sc->sc_dev.dv_xname, SYSCTL_DESCR("ibm controls"),
	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
		goto out;

	if ((rv = sysctl_createv(&sc->sc_log, 0, &snode, &node,
	    CTLFLAG_READWRITE, CTLTYPE_INT, buf + 1, NULL,
	    ibm_sysctl_helper, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
		goto out;

out:
#ifdef DIAGNOSTIC
	if (rv)
		printf("%s: sysctl_createv failed (rv = %d)\n",
		    sc->sc_dev.dv_xname, rv);
#endif
	return AE_OK;
}

static int
ibm_sysctl_helper(SYSCTLFN_ARGS)
{
	struct sysctlnode node;
	ACPI_INTEGER acpi_val;
	ACPI_STATUS rv;
	int val, old_val, error;
	char buf[SYSCTL_NAMELEN + 1], *ptr;
	struct ibm_acpi_softc *sc = rnode->sysctl_data;

	(void)snprintf(buf, sizeof(buf), "G%s", rnode->sysctl_name);
	for (ptr = buf; *ptr; ptr++)
		*ptr = toupper(*ptr);

	rv = acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val);
	if (ACPI_FAILURE(rv)) {
#ifdef DIAGNOSTIC
		printf("%s: couldn't get `%s'\n", sc->sc_dev.dv_xname, buf);
#endif
		return EIO;
	}
	val = old_val = acpi_val;

	node = *rnode;
	node.sysctl_data = &val;

	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	(void)snprintf(buf, sizeof(buf), "S%s", rnode->sysctl_name);
	acpi_val = val;
	rv = ibm_acpi_eval_set_integer(sc->sc_node->ad_handle, buf,
	    acpi_val, NULL);
	if (ACPI_FAILURE(rv)) {
#ifdef DIAGNOSTIC
		printf("%s: couldn't set `%s' to %d\n",
		    sc->sc_dev.dv_xname, buf, val);
#endif
		return EIO;
	}
	return 0;
}


/*
 * ibm_acpi_hotkey_notify_handler
 *
 * 	Notify handler.
 */
static void
ibm_acpi_hotkey_notify_handler(ACPI_HANDLE handle, UINT32 notify,
		void *context)
{
	struct ibm_acpi_softc *sc = context;
	ACPI_BUFFER buf;
	ACPI_OBJECT obj;
	int hotkey;

	if (notify != 0x80) {
		printf("%s: received unknown notify message: 0x%x\n",
				sc->sc_dev.dv_xname, notify);
		return;
	}
	buf.Pointer = &obj;
	buf.Length = sizeof(obj);
	if(!ibm_acpi_eval(sc, "MHKP", NULL, &buf))
		return;
	hotkey = obj.Integer.Value;
	/* TODO queue the function for execution instead */
	switch (hotkey)
	{
		case 0x1005: /* Fn + F5 */
			ibm_acpi_bluetooth_switch(sc);
			break;
		case 0x1007: /* Fn + F7 */
			ibm_acpi_video_switch(sc);
			break;
		case 0x1008: /* Fn + F8 */
			ibm_acpi_mouse_switch(sc);
			break;
		case 0x100c: /* Fn + F12 */
			ibm_acpi_bay_eject(sc);
			break;
#ifdef IBM_DEBUG
		default:
			printf("%s: hotkey 0x%04x is not supported\n",
					sc->sc_dev.dv_xname, hotkey);
			break;
#endif
	}
}


static int
ibm_acpi_hotkey_get(struct ibm_acpi_softc * sc, UINT32 * status, UINT32 * mask)
{
	ACPI_BUFFER buf;
	ACPI_OBJECT obj;

	buf.Pointer = &obj;
	buf.Length = sizeof(obj);
	if(!ibm_acpi_eval(sc, "DHKC", NULL, &buf))
		return 0;
	if(status)
		*status = obj.Integer.Value;
	if(sc->hotkey_mask_supported)
		if(!ibm_acpi_dhkn_get(sc, mask))
			return 0;
	return 1;
}


static int ibm_acpi_hotkey_set(struct ibm_acpi_softc * sc, UINT32 status,
		UINT32 mask)
{
	int i;
	ACPI_OBJECT arg[2];
	ACPI_OBJECT_LIST args;

	arg[0].Type = ACPI_TYPE_INTEGER;
	arg[0].Integer.Value = status;
	args.Count = 1;
	args.Pointer = arg;
	if(!ibm_acpi_eval(sc, "MHKC", &args, NULL))
		return 0;
	if(!sc->hotkey_mask_supported)
		return 1;
	arg[1].Type = ACPI_TYPE_INTEGER;
	args.Count = 2;
	for (i = 0; i < 32; i++) {
		arg[0].Integer.Value = i + 1;
		arg[1].Integer.Value = ((1 << i) & mask) != 0;
		if (!ibm_acpi_eval(sc, "MHKM", &args, NULL))
			return 0;
	}
	return 1;
}


static int ibm_acpi_bluetooth_get(struct ibm_acpi_softc * sc, UINT32 * ret)
{
	ACPI_BUFFER buf;
	ACPI_OBJECT obj;

	buf.Pointer = &obj;
	buf.Length = sizeof(obj);
	if(!ibm_acpi_eval(sc, "GBDC", NULL, &buf))
		return 0;
	if(ret)
		*ret = obj.Integer.Value & 2 ? 1 : 0;
	return 1;
}


static int ibm_acpi_bluetooth_set(struct ibm_acpi_softc * sc, UINT32 status)
{
	int old;
	ACPI_OBJECT arg;
	ACPI_OBJECT_LIST args;

	if(!ibm_acpi_bluetooth_get(sc, &old))
		return 0;
	arg.Type = ACPI_TYPE_INTEGER;
	arg.Integer.Value = status ? old | 2 : old & ~2;
	args.Count = 1;
	args.Pointer = &arg;
	return ibm_acpi_eval(sc, "SBDC", &args, NULL);
}


static int ibm_acpi_bluetooth_switch(struct ibm_acpi_softc * sc)
{
	int status;

	if(!ibm_acpi_bluetooth_get(sc, &status))
		return 0;
	printf("%s: %s bluetooth\n", sc->sc_dev.dv_xname, !status ? "enabling"
			: "disabling");
	return ibm_acpi_bluetooth_set(sc, !status);
}


static int ibm_acpi_video_switch(struct ibm_acpi_softc * sc)
{
	int ret;
	ACPI_BUFFER buf;
	ACPI_OBJECT obj;
	int autosw;
	ACPI_OBJECT_LIST args;

	if(sc->video_handle == NULL)
		return 0;
	buf.Pointer = &obj;
	buf.Length = sizeof(obj);
	if(!ibm_acpi_video_eval(sc, "^VDEE", NULL, &buf))
		return 0;
	autosw = obj.Integer.Value & 1;
	obj.Type = ACPI_TYPE_INTEGER;
	obj.Integer.Value = 1;
	args.Count = 1;
	args.Pointer = &obj;
	if(!ibm_acpi_video_eval(sc, "_DOS", &args, NULL))
		return 0;
	printf("%s: switching video\n", sc->sc_dev.dv_xname);
	ret = ibm_acpi_video_eval(sc, "VSWT", NULL, NULL);
	obj.Integer.Value = autosw;
	if(!ibm_acpi_video_eval(sc, "_DOS", &args, NULL))
		return 0;
#ifdef IBM_DEBUG
	printf("%s: autosw is 0x%x, ret = %d\n", sc->sc_dev.dv_xname, autosw,
			ret);
#endif
	return ret;
}


static int ibm_acpi_mouse_switch(struct ibm_acpi_softc * sc)
{
	/* FIXME implement */
	return 0;
}


static int ibm_acpi_bay_eject(struct ibm_acpi_softc * sc)
{
	ACPI_OBJECT arg;
	ACPI_OBJECT_LIST args;

	if(sc->bay_handle == NULL)
		return 0;
	arg.Type = ACPI_TYPE_INTEGER;
	arg.Integer.Value = 1;
	args.Count = 1;
	args.Pointer = &arg;
	if(!ibm_acpi_bay_eval(sc, NULL, &args, NULL))
		return 0;
	return 1;
}


/* helpers */
static int
ibm_acpi_eval(struct ibm_acpi_softc * sc, char const * path,
		ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret)
{
	ACPI_STATUS rv;

	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, path, args, ret);
	if (ACPI_FAILURE(rv)) {
		printf("%s: failed to evaluate %s: %s\n",
				sc->sc_dev.dv_xname, path,
				AcpiFormatException(rv));
		return 0;
	}
	return 1;
}


static int
ibm_acpi_video_eval(struct ibm_acpi_softc * sc, char const * path,
		ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret)
{
	ACPI_STATUS rv;

	if(sc->video_handle == NULL)
		return 0;
	rv = AcpiEvaluateObject(sc->video_handle, path, args, ret);
	if (ACPI_FAILURE(rv)) {
		printf("%s: failed to evaluate %s: %s\n",
				sc->sc_dev.dv_xname, path,
				AcpiFormatException(rv));
		return 0;
	}
	return 1;
}


static int
ibm_acpi_bay_eval(struct ibm_acpi_softc * sc, char const * path,
		ACPI_OBJECT_LIST * args, ACPI_BUFFER * ret)
{
	ACPI_STATUS rv;

	if(sc->bay_handle == NULL)
		return 0;
	rv = AcpiEvaluateObject(sc->bay_handle, path, args, ret);
	if (ACPI_FAILURE(rv)) {
		printf("%s: failed to evaluate %s: %s\n",
				sc->sc_dev.dv_xname, path,
				AcpiFormatException(rv));
		return 0;
	}
	return 1;
}



ACPI_STATUS
ibm_acpi_eval_set_integer(ACPI_HANDLE handle, const char *path,
    ACPI_INTEGER val, ACPI_INTEGER *valp)
{
	ACPI_STATUS rv;
	ACPI_BUFFER buf;
	ACPI_OBJECT param, ret_val;
	ACPI_OBJECT_LIST params;

	if (handle == NULL)
		handle = ACPI_ROOT_OBJECT;

	params.Count = 1;
	params.Pointer = &param;

	param.Type = ACPI_TYPE_INTEGER;
	param.Integer.Value = val;

	buf.Pointer = &ret_val;
	buf.Length = sizeof(ret_val);

	rv = AcpiEvaluateObjectTyped(handle, path, &params, &buf,
	    ACPI_TYPE_INTEGER);

	if (ACPI_SUCCESS(rv) && valp)
		*valp = ret_val.Integer.Value;

	return rv;
}


/* wrappers */
static int
ibm_acpi_dhkn_get(struct ibm_acpi_softc * sc, UINT32 * ret)
{
	ACPI_BUFFER buf;
	ACPI_OBJECT obj;

	buf.Pointer = &obj;
	buf.Length = sizeof(obj);
	if(!ibm_acpi_eval(sc, "DHKN", NULL, &buf))
		return 0;
	if(ret)
		*ret = obj.Integer.Value;
	return 1;
}

