/*
 * MSI WMI hotkeys
 *
 * Copyright (C) 2009 Jérôme Pouiller <jezz@sysmic.org>
 * 
 * Portions base on hp-zmi.c:
 * Copyright (C) 2008 Red Hat <mjg@redhat.com>
 *
 * Portions based on wistron_btns.c:
 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/input.h>
#include <acpi/acpi_drivers.h>
#include <linux/platform_device.h>
#include <linux/acpi.h>
#include <linux/string.h>

MODULE_AUTHOR("Jérôme Pouiller <jezz@sysmic.org>");
MODULE_DESCRIPTION("MSI all-in-one WMI hotkeys driver");
MODULE_LICENSE("GPL");

MODULE_ALIAS("wmi:b6f3eef2-3d2f-49dc-9de3-85bce18c62f2");

#define MSIWMI_EVENT_GUID "b6f3eef2-3d2f-49dc-9de3-85bce18c62f2"

struct key_entry {
	u16 code;
	u16 keycode;
};

static struct key_entry msi_wmi_keymap[] = {
	{ 208, KEY_BRIGHTNESSUP },
	{ 209, KEY_BRIGHTNESSDOWN },
	{ 210, KEY_VOLUMEUP },
	{ 211, KEY_VOLUMEDOWN },
	{ 0 }
};

static struct input_dev *msi_wmi_input_dev;

static struct key_entry *msi_wmi_get_entry_by_scancode(int code)
{
	struct key_entry *key;

	for (key = msi_wmi_keymap; key->code; key++)
		if (code == key->code)
			return key;

	return NULL;
}

static struct key_entry *msi_wmi_get_entry_by_keycode(int keycode)
{
	struct key_entry *key;

	for (key = msi_wmi_keymap; key->code; key++)
		if (keycode == key->keycode)
			return key;

	return NULL;
}

static void msi_wmi_notify(u32 value, void *context)
{
	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
	static struct key_entry *key;
	union acpi_object *obj;
    acpi_status ret;

	ret = wmi_get_event_data(value, &response);

	obj = (union acpi_object *)response.pointer;

	if (obj && obj->type == ACPI_TYPE_INTEGER) {
		printk(KERN_DEBUG "MSI WMI: event correctly received: %llu\n",
               obj->integer.value);
        key = msi_wmi_get_entry_by_scancode(obj->integer.value);
		input_report_key(msi_wmi_input_dev,
				 key->keycode, 1);
		input_sync(msi_wmi_input_dev);
		input_report_key(msi_wmi_input_dev,
				 key->keycode, 0);
		input_sync(msi_wmi_input_dev);
    } else
		printk(KERN_INFO "MSI WMI: Unknown response received\n");
}

static int msi_wmi_getkeycode(struct input_dev *dev, 
                              int scancode, int *keycode)
{
	struct key_entry *key = msi_wmi_get_entry_by_scancode(scancode);

	if (key && key->code) {
		*keycode = key->keycode;
		return 0;
	}

	return -EINVAL;
}

static int msi_wmi_setkeycode(struct input_dev *dev, 
                              int scancode, int keycode)
{
	struct key_entry *key;
	int old_keycode;

	if (keycode < 0 || keycode > KEY_MAX)
		return -EINVAL;

	key = msi_wmi_get_entry_by_scancode(scancode);
	if (key && key->code) {
		old_keycode = key->keycode;
		key->keycode = keycode;
		set_bit(keycode, dev->keybit);
		if (!msi_wmi_get_entry_by_keycode(old_keycode))
			clear_bit(old_keycode, dev->keybit);
		return 0;
	}

	return -EINVAL;
}

static int __init msi_wmi_input_setup(void)
{
    struct key_entry *key;
	int err;

	msi_wmi_input_dev = input_allocate_device();

	msi_wmi_input_dev->name = "MSI WMI hotkeys";
	msi_wmi_input_dev->phys = "wmi/input0";
	msi_wmi_input_dev->id.bustype = BUS_HOST;
	msi_wmi_input_dev->getkeycode = msi_wmi_getkeycode;
	msi_wmi_input_dev->setkeycode = msi_wmi_setkeycode;

	for (key = msi_wmi_keymap; key->code; key++) {
		set_bit(EV_KEY, msi_wmi_input_dev->evbit);
		set_bit(key->keycode, msi_wmi_input_dev->keybit);
	}

	err = input_register_device(msi_wmi_input_dev);

	if (err) {
		input_free_device(msi_wmi_input_dev);
		return err;
	}
	return 0;
}

static int __init msi_wmi_init(void)
{
	int err;

	if (wmi_has_guid(MSIWMI_EVENT_GUID)) {
		err = wmi_install_notify_handler(MSIWMI_EVENT_GUID,
						 msi_wmi_notify, NULL);
		if (!err)
			msi_wmi_input_setup();
        else
           printk(KERN_INFO "MSI WMI: Error while intalling notify handler\n");
	}

	return 0;
}

static void __exit msi_wmi_exit(void)
{
	if (wmi_has_guid(MSIWMI_EVENT_GUID)) {
		wmi_remove_notify_handler(MSIWMI_EVENT_GUID);
		input_unregister_device(msi_wmi_input_dev);
	}
}

module_init(msi_wmi_init);
module_exit(msi_wmi_exit);

