Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *  Intel Virtual Button driver for Windows 8.1+
  4 *
  5 *  Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
  6 *  Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
  7 */
  8
  9#include <linux/acpi.h>
 10#include <linux/dmi.h>
 11#include <linux/input.h>
 12#include <linux/input/sparse-keymap.h>
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/platform_device.h>
 16#include <linux/suspend.h>
 17
 18/* When NOT in tablet mode, VGBS returns with the flag 0x40 */
 19#define TABLET_MODE_FLAG 0x40
 20#define DOCK_MODE_FLAG   0x80
 21
 22MODULE_LICENSE("GPL");
 23MODULE_AUTHOR("AceLan Kao");
 24
 25static const struct acpi_device_id intel_vbtn_ids[] = {
 26	{"INT33D6", 0},
 27	{"", 0},
 28};
 29MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
 30
 31/* In theory, these are HID usages. */
 32static const struct key_entry intel_vbtn_keymap[] = {
 33	{ KE_KEY, 0xC0, { KEY_POWER } },	/* power key press */
 34	{ KE_IGNORE, 0xC1, { KEY_POWER } },	/* power key release */
 35	{ KE_KEY, 0xC2, { KEY_LEFTMETA } },		/* 'Windows' key press */
 36	{ KE_KEY, 0xC3, { KEY_LEFTMETA } },		/* 'Windows' key release */
 37	{ KE_KEY, 0xC4, { KEY_VOLUMEUP } },		/* volume-up key press */
 38	{ KE_IGNORE, 0xC5, { KEY_VOLUMEUP } },		/* volume-up key release */
 39	{ KE_KEY, 0xC6, { KEY_VOLUMEDOWN } },		/* volume-down key press */
 40	{ KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } },	/* volume-down key release */
 41	{ KE_KEY,    0xC8, { KEY_ROTATE_LOCK_TOGGLE } },	/* rotate-lock key press */
 42	{ KE_KEY,    0xC9, { KEY_ROTATE_LOCK_TOGGLE } },	/* rotate-lock key release */
 43};
 44
 45static const struct key_entry intel_vbtn_switchmap[] = {
 46	{ KE_SW,     0xCA, { .sw = { SW_DOCK, 1 } } },		/* Docked */
 47	{ KE_SW,     0xCB, { .sw = { SW_DOCK, 0 } } },		/* Undocked */
 48	{ KE_SW,     0xCC, { .sw = { SW_TABLET_MODE, 1 } } },	/* Tablet */
 49	{ KE_SW,     0xCD, { .sw = { SW_TABLET_MODE, 0 } } },	/* Laptop */
 50};
 51
 52#define KEYMAP_LEN \
 53	(ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
 54
 55struct intel_vbtn_priv {
 56	struct key_entry keymap[KEYMAP_LEN];
 57	struct input_dev *input_dev;
 58	bool has_buttons;
 59	bool has_switches;
 60	bool wakeup_mode;
 61};
 62
 63static void detect_tablet_mode(struct platform_device *device)
 64{
 65	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
 66	acpi_handle handle = ACPI_HANDLE(&device->dev);
 67	unsigned long long vgbs;
 68	acpi_status status;
 69	int m;
 70
 71	status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
 72	if (ACPI_FAILURE(status))
 73		return;
 74
 75	m = !(vgbs & TABLET_MODE_FLAG);
 76	input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
 77	m = (vgbs & DOCK_MODE_FLAG) ? 1 : 0;
 78	input_report_switch(priv->input_dev, SW_DOCK, m);
 79}
 80
 81static int intel_vbtn_input_setup(struct platform_device *device)
 82{
 83	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
 84	int ret, keymap_len = 0;
 85
 86	if (priv->has_buttons) {
 87		memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
 88		       ARRAY_SIZE(intel_vbtn_keymap) *
 89		       sizeof(struct key_entry));
 90		keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
 91	}
 92
 93	if (priv->has_switches) {
 94		memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
 95		       ARRAY_SIZE(intel_vbtn_switchmap) *
 96		       sizeof(struct key_entry));
 97		keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
 98	}
 99
100	priv->keymap[keymap_len].type = KE_END;
101
102	priv->input_dev = devm_input_allocate_device(&device->dev);
103	if (!priv->input_dev)
104		return -ENOMEM;
105
106	ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
107	if (ret)
108		return ret;
109
110	priv->input_dev->dev.parent = &device->dev;
111	priv->input_dev->name = "Intel Virtual Button driver";
112	priv->input_dev->id.bustype = BUS_HOST;
113
114	if (priv->has_switches)
115		detect_tablet_mode(device);
116
117	return input_register_device(priv->input_dev);
118}
119
120static void notify_handler(acpi_handle handle, u32 event, void *context)
121{
122	struct platform_device *device = context;
123	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
124	unsigned int val = !(event & 1); /* Even=press, Odd=release */
125	const struct key_entry *ke, *ke_rel;
126	bool autorelease;
127
128	if (priv->wakeup_mode) {
129		ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
130		if (ke) {
131			pm_wakeup_hard_event(&device->dev);
132
133			/*
134			 * Switch events like tablet mode will wake the device
135			 * and report the new switch position to the input
136			 * subsystem.
137			 */
138			if (ke->type == KE_SW)
139				sparse_keymap_report_event(priv->input_dev,
140							   event,
141							   val,
142							   0);
143			return;
144		}
145		goto out_unknown;
146	}
147
148	/*
149	 * Even press events are autorelease if there is no corresponding odd
150	 * release event, or if the odd event is KE_IGNORE.
151	 */
152	ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
153	autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
154
155	if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
156		return;
157
158out_unknown:
159	dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
160}
161
162static bool intel_vbtn_has_buttons(acpi_handle handle)
163{
164	acpi_status status;
165
166	status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
167	return ACPI_SUCCESS(status);
168}
169
170/*
171 * There are several laptops (non 2-in-1) models out there which support VGBS,
172 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
173 * turn causes userspace (libinput) to suppress events from the builtin
174 * keyboard and touchpad, making the laptop essentially unusable.
175 *
176 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
177 * with libinput, leads to a non-usable system. Where as OTOH many people will
178 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
179 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
180 *
181 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
182 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
183 * these are matched on a per model basis, since many normal laptops with a
184 * possible broken VGBS ACPI-method also use these chassis-types.
185 */
186static const struct dmi_system_id dmi_switches_allow_list[] = {
187	{
188		.matches = {
189			DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
190		},
191	},
192	{
193		.matches = {
194			DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
195		},
196	},
197	{
198		.matches = {
199			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200			DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
201		},
202	},
203	{
204		.matches = {
205			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
206			DMI_MATCH(DMI_PRODUCT_NAME, "HP Stream x360 Convertible PC 11"),
207		},
208	},
209	{} /* Array terminator */
210};
211
212static bool intel_vbtn_has_switches(acpi_handle handle)
213{
214	unsigned long long vgbs;
215	acpi_status status;
216
217	if (!dmi_check_system(dmi_switches_allow_list))
218		return false;
219
220	status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
221	return ACPI_SUCCESS(status);
222}
223
224static int intel_vbtn_probe(struct platform_device *device)
225{
226	acpi_handle handle = ACPI_HANDLE(&device->dev);
227	bool has_buttons, has_switches;
228	struct intel_vbtn_priv *priv;
229	acpi_status status;
230	int err;
231
232	has_buttons = intel_vbtn_has_buttons(handle);
233	has_switches = intel_vbtn_has_switches(handle);
234
235	if (!has_buttons && !has_switches) {
236		dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
237		return -ENODEV;
238	}
239
240	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
241	if (!priv)
242		return -ENOMEM;
243	dev_set_drvdata(&device->dev, priv);
244
245	priv->has_buttons = has_buttons;
246	priv->has_switches = has_switches;
247
248	err = intel_vbtn_input_setup(device);
249	if (err) {
250		pr_err("Failed to setup Intel Virtual Button\n");
251		return err;
252	}
253
254	status = acpi_install_notify_handler(handle,
255					     ACPI_DEVICE_NOTIFY,
256					     notify_handler,
257					     device);
258	if (ACPI_FAILURE(status))
259		return -EBUSY;
260
261	device_init_wakeup(&device->dev, true);
262	/*
263	 * In order for system wakeup to work, the EC GPE has to be marked as
264	 * a wakeup one, so do that here (this setting will persist, but it has
265	 * no effect until the wakeup mask is set for the EC GPE).
266	 */
267	acpi_ec_mark_gpe_for_wake();
268	return 0;
269}
270
271static int intel_vbtn_remove(struct platform_device *device)
272{
273	acpi_handle handle = ACPI_HANDLE(&device->dev);
274
275	device_init_wakeup(&device->dev, false);
276	acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
277
278	/*
279	 * Even if we failed to shut off the event stream, we can still
280	 * safely detach from the device.
281	 */
282	return 0;
283}
284
285static int intel_vbtn_pm_prepare(struct device *dev)
286{
287	if (device_may_wakeup(dev)) {
288		struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
289
290		priv->wakeup_mode = true;
291	}
292	return 0;
293}
294
295static void intel_vbtn_pm_complete(struct device *dev)
296{
297	struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
298
299	priv->wakeup_mode = false;
300}
301
302static int intel_vbtn_pm_resume(struct device *dev)
303{
304	intel_vbtn_pm_complete(dev);
305	return 0;
306}
307
308static const struct dev_pm_ops intel_vbtn_pm_ops = {
309	.prepare = intel_vbtn_pm_prepare,
310	.complete = intel_vbtn_pm_complete,
311	.resume = intel_vbtn_pm_resume,
312	.restore = intel_vbtn_pm_resume,
313	.thaw = intel_vbtn_pm_resume,
314};
315
316static struct platform_driver intel_vbtn_pl_driver = {
317	.driver = {
318		.name = "intel-vbtn",
319		.acpi_match_table = intel_vbtn_ids,
320		.pm = &intel_vbtn_pm_ops,
321	},
322	.probe = intel_vbtn_probe,
323	.remove = intel_vbtn_remove,
324};
325
326static acpi_status __init
327check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
328{
329	const struct acpi_device_id *ids = context;
330	struct acpi_device *dev;
331
332	if (acpi_bus_get_device(handle, &dev) != 0)
333		return AE_OK;
334
335	if (acpi_match_device_ids(dev, ids) == 0)
336		if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
337			dev_info(&dev->dev,
338				 "intel-vbtn: created platform device\n");
339
340	return AE_OK;
341}
342
343static int __init intel_vbtn_init(void)
344{
345	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
346			    ACPI_UINT32_MAX, check_acpi_dev, NULL,
347			    (void *)intel_vbtn_ids, NULL);
348
349	return platform_driver_register(&intel_vbtn_pl_driver);
350}
351module_init(intel_vbtn_init);
352
353static void __exit intel_vbtn_exit(void)
354{
355	platform_driver_unregister(&intel_vbtn_pl_driver);
356}
357module_exit(intel_vbtn_exit);