1/*
2 *  WMI hotkeys support for Dell All-In-One series
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/types.h>
25#include <linux/input.h>
26#include <linux/input/sparse-keymap.h>
27#include <linux/acpi.h>
28#include <linux/string.h>
29
30MODULE_DESCRIPTION("WMI hotkeys driver for Dell All-In-One series");
31MODULE_LICENSE("GPL");
32
33#define EVENT_GUID1 "284A0E6B-380E-472A-921F-E52786257FB4"
34#define EVENT_GUID2 "02314822-307C-4F66-BF0E-48AEAEB26CC8"
35
36struct dell_wmi_event {
37	u16	length;
38	/* 0x000: A hot key pressed or an event occurred
39	 * 0x00F: A sequence of hot keys are pressed */
40	u16	type;
41	u16	event[];
42};
43
44static const char *dell_wmi_aio_guids[] = {
45	EVENT_GUID1,
46	EVENT_GUID2,
47	NULL
48};
49
50MODULE_ALIAS("wmi:"EVENT_GUID1);
51MODULE_ALIAS("wmi:"EVENT_GUID2);
52
53static const struct key_entry dell_wmi_aio_keymap[] = {
54	{ KE_KEY, 0xc0, { KEY_VOLUMEUP } },
55	{ KE_KEY, 0xc1, { KEY_VOLUMEDOWN } },
56	{ KE_KEY, 0xe030, { KEY_VOLUMEUP } },
57	{ KE_KEY, 0xe02e, { KEY_VOLUMEDOWN } },
58	{ KE_KEY, 0xe020, { KEY_MUTE } },
59	{ KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
60	{ KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
61	{ KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
62	{ KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
63	{ KE_END, 0 }
64};
65
66static struct input_dev *dell_wmi_aio_input_dev;
67
68/*
69 * The new WMI event data format will follow the dell_wmi_event structure
70 * So, we will check if the buffer matches the format
71 */
72static bool dell_wmi_aio_event_check(u8 *buffer, int length)
73{
74	struct dell_wmi_event *event = (struct dell_wmi_event *)buffer;
75
76	if (event == NULL || length < 6)
77		return false;
78
79	if ((event->type == 0 || event->type == 0xf) &&
80			event->length >= 2)
81		return true;
82
83	return false;
84}
85
86static void dell_wmi_aio_notify(u32 value, void *context)
87{
88	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
89	union acpi_object *obj;
90	struct dell_wmi_event *event;
91	acpi_status status;
92
93	status = wmi_get_event_data(value, &response);
94	if (status != AE_OK) {
95		pr_info("bad event status 0x%x\n", status);
96		return;
97	}
98
99	obj = (union acpi_object *)response.pointer;
100	if (obj) {
101		unsigned int scancode = 0;
102
103		switch (obj->type) {
104		case ACPI_TYPE_INTEGER:
105			/* Most All-In-One correctly return integer scancode */
106			scancode = obj->integer.value;
107			sparse_keymap_report_event(dell_wmi_aio_input_dev,
108				scancode, 1, true);
109			break;
110		case ACPI_TYPE_BUFFER:
111			if (dell_wmi_aio_event_check(obj->buffer.pointer,
112						obj->buffer.length)) {
113				event = (struct dell_wmi_event *)
114					obj->buffer.pointer;
115				scancode = event->event[0];
116			} else {
117				/* Broken machines return the scancode in a
118				   buffer */
119				if (obj->buffer.pointer &&
120						obj->buffer.length > 0)
121					scancode = obj->buffer.pointer[0];
122			}
123			if (scancode)
124				sparse_keymap_report_event(
125					dell_wmi_aio_input_dev,
126					scancode, 1, true);
127			break;
128		}
129	}
130	kfree(obj);
131}
132
133static int __init dell_wmi_aio_input_setup(void)
134{
135	int err;
136
137	dell_wmi_aio_input_dev = input_allocate_device();
138
139	if (!dell_wmi_aio_input_dev)
140		return -ENOMEM;
141
142	dell_wmi_aio_input_dev->name = "Dell AIO WMI hotkeys";
143	dell_wmi_aio_input_dev->phys = "wmi/input0";
144	dell_wmi_aio_input_dev->id.bustype = BUS_HOST;
145
146	err = sparse_keymap_setup(dell_wmi_aio_input_dev,
147			dell_wmi_aio_keymap, NULL);
148	if (err) {
149		pr_err("Unable to setup input device keymap\n");
150		goto err_free_dev;
151	}
152	err = input_register_device(dell_wmi_aio_input_dev);
153	if (err) {
154		pr_info("Unable to register input device\n");
155		goto err_free_keymap;
156	}
157	return 0;
158
159err_free_keymap:
160	sparse_keymap_free(dell_wmi_aio_input_dev);
161err_free_dev:
162	input_free_device(dell_wmi_aio_input_dev);
163	return err;
164}
165
166static const char *dell_wmi_aio_find(void)
167{
168	int i;
169
170	for (i = 0; dell_wmi_aio_guids[i] != NULL; i++)
171		if (wmi_has_guid(dell_wmi_aio_guids[i]))
172			return dell_wmi_aio_guids[i];
173
174	return NULL;
175}
176
177static int __init dell_wmi_aio_init(void)
178{
179	int err;
180	const char *guid;
181
182	guid = dell_wmi_aio_find();
183	if (!guid) {
184		pr_warn("No known WMI GUID found\n");
185		return -ENXIO;
186	}
187
188	err = dell_wmi_aio_input_setup();
189	if (err)
190		return err;
191
192	err = wmi_install_notify_handler(guid, dell_wmi_aio_notify, NULL);
193	if (err) {
194		pr_err("Unable to register notify handler - %d\n", err);
195		sparse_keymap_free(dell_wmi_aio_input_dev);
196		input_unregister_device(dell_wmi_aio_input_dev);
197		return err;
198	}
199
200	return 0;
201}
202
203static void __exit dell_wmi_aio_exit(void)
204{
205	const char *guid;
206
207	guid = dell_wmi_aio_find();
208	wmi_remove_notify_handler(guid);
209	sparse_keymap_free(dell_wmi_aio_input_dev);
210	input_unregister_device(dell_wmi_aio_input_dev);
211}
212
213module_init(dell_wmi_aio_init);
214module_exit(dell_wmi_aio_exit);
215