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 
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("AceLan Kao");
24 
25 static const struct acpi_device_id intel_vbtn_ids[] = {
26 	{"INT33D6", 0},
27 	{"", 0},
28 };
29 
30 /* In theory, these are HID usages. */
31 static const struct key_entry intel_vbtn_keymap[] = {
32 	{ KE_KEY, 0xC0, { KEY_POWER } },	/* power key press */
33 	{ KE_IGNORE, 0xC1, { KEY_POWER } },	/* power key release */
34 	{ KE_KEY, 0xC2, { KEY_LEFTMETA } },		/* 'Windows' key press */
35 	{ KE_KEY, 0xC3, { KEY_LEFTMETA } },		/* 'Windows' key release */
36 	{ KE_KEY, 0xC4, { KEY_VOLUMEUP } },		/* volume-up key press */
37 	{ KE_IGNORE, 0xC5, { KEY_VOLUMEUP } },		/* volume-up key release */
38 	{ KE_KEY, 0xC6, { KEY_VOLUMEDOWN } },		/* volume-down key press */
39 	{ KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } },	/* volume-down key release */
40 	{ KE_KEY,    0xC8, { KEY_ROTATE_LOCK_TOGGLE } },	/* rotate-lock key press */
41 	{ KE_KEY,    0xC9, { KEY_ROTATE_LOCK_TOGGLE } },	/* rotate-lock key release */
42 	{ KE_SW,     0xCA, { .sw = { SW_DOCK, 1 } } },		/* Docked */
43 	{ KE_SW,     0xCB, { .sw = { SW_DOCK, 0 } } },		/* Undocked */
44 	{ KE_SW,     0xCC, { .sw = { SW_TABLET_MODE, 1 } } },	/* Tablet */
45 	{ KE_SW,     0xCD, { .sw = { SW_TABLET_MODE, 0 } } },	/* Laptop */
46 	{ KE_END },
47 };
48 
49 struct intel_vbtn_priv {
50 	struct input_dev *input_dev;
51 	bool wakeup_mode;
52 };
53 
intel_vbtn_input_setup(struct platform_device * device)54 static int intel_vbtn_input_setup(struct platform_device *device)
55 {
56 	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
57 	int ret;
58 
59 	priv->input_dev = devm_input_allocate_device(&device->dev);
60 	if (!priv->input_dev)
61 		return -ENOMEM;
62 
63 	ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
64 	if (ret)
65 		return ret;
66 
67 	priv->input_dev->dev.parent = &device->dev;
68 	priv->input_dev->name = "Intel Virtual Button driver";
69 	priv->input_dev->id.bustype = BUS_HOST;
70 
71 	return input_register_device(priv->input_dev);
72 }
73 
notify_handler(acpi_handle handle,u32 event,void * context)74 static void notify_handler(acpi_handle handle, u32 event, void *context)
75 {
76 	struct platform_device *device = context;
77 	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
78 	unsigned int val = !(event & 1); /* Even=press, Odd=release */
79 	const struct key_entry *ke_rel;
80 	bool autorelease;
81 
82 	if (priv->wakeup_mode) {
83 		if (sparse_keymap_entry_from_scancode(priv->input_dev, event)) {
84 			pm_wakeup_hard_event(&device->dev);
85 			return;
86 		}
87 		goto out_unknown;
88 	}
89 
90 	/*
91 	 * Even press events are autorelease if there is no corresponding odd
92 	 * release event, or if the odd event is KE_IGNORE.
93 	 */
94 	ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
95 	autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
96 
97 	if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
98 		return;
99 
100 out_unknown:
101 	dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
102 }
103 
detect_tablet_mode(struct platform_device * device)104 static void detect_tablet_mode(struct platform_device *device)
105 {
106 	const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
107 	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
108 	acpi_handle handle = ACPI_HANDLE(&device->dev);
109 	struct acpi_buffer vgbs_output = { ACPI_ALLOCATE_BUFFER, NULL };
110 	union acpi_object *obj;
111 	acpi_status status;
112 	int m;
113 
114 	if (!(chassis_type && strcmp(chassis_type, "31") == 0))
115 		goto out;
116 
117 	status = acpi_evaluate_object(handle, "VGBS", NULL, &vgbs_output);
118 	if (ACPI_FAILURE(status))
119 		goto out;
120 
121 	obj = vgbs_output.pointer;
122 	if (!(obj && obj->type == ACPI_TYPE_INTEGER))
123 		goto out;
124 
125 	m = !(obj->integer.value & TABLET_MODE_FLAG);
126 	input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
127 	m = (obj->integer.value & DOCK_MODE_FLAG) ? 1 : 0;
128 	input_report_switch(priv->input_dev, SW_DOCK, m);
129 out:
130 	kfree(vgbs_output.pointer);
131 }
132 
intel_vbtn_probe(struct platform_device * device)133 static int intel_vbtn_probe(struct platform_device *device)
134 {
135 	acpi_handle handle = ACPI_HANDLE(&device->dev);
136 	struct intel_vbtn_priv *priv;
137 	acpi_status status;
138 	int err;
139 
140 	status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
141 	if (ACPI_FAILURE(status)) {
142 		dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
143 		return -ENODEV;
144 	}
145 
146 	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
147 	if (!priv)
148 		return -ENOMEM;
149 	dev_set_drvdata(&device->dev, priv);
150 
151 	err = intel_vbtn_input_setup(device);
152 	if (err) {
153 		pr_err("Failed to setup Intel Virtual Button\n");
154 		return err;
155 	}
156 
157 	detect_tablet_mode(device);
158 
159 	status = acpi_install_notify_handler(handle,
160 					     ACPI_DEVICE_NOTIFY,
161 					     notify_handler,
162 					     device);
163 	if (ACPI_FAILURE(status))
164 		return -EBUSY;
165 
166 	device_init_wakeup(&device->dev, true);
167 	return 0;
168 }
169 
intel_vbtn_remove(struct platform_device * device)170 static int intel_vbtn_remove(struct platform_device *device)
171 {
172 	acpi_handle handle = ACPI_HANDLE(&device->dev);
173 
174 	device_init_wakeup(&device->dev, false);
175 	acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
176 
177 	/*
178 	 * Even if we failed to shut off the event stream, we can still
179 	 * safely detach from the device.
180 	 */
181 	return 0;
182 }
183 
intel_vbtn_pm_prepare(struct device * dev)184 static int intel_vbtn_pm_prepare(struct device *dev)
185 {
186 	struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
187 
188 	priv->wakeup_mode = true;
189 	return 0;
190 }
191 
intel_vbtn_pm_resume(struct device * dev)192 static int intel_vbtn_pm_resume(struct device *dev)
193 {
194 	struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
195 
196 	priv->wakeup_mode = false;
197 	return 0;
198 }
199 
200 static const struct dev_pm_ops intel_vbtn_pm_ops = {
201 	.prepare = intel_vbtn_pm_prepare,
202 	.resume = intel_vbtn_pm_resume,
203 	.restore = intel_vbtn_pm_resume,
204 	.thaw = intel_vbtn_pm_resume,
205 };
206 
207 static struct platform_driver intel_vbtn_pl_driver = {
208 	.driver = {
209 		.name = "intel-vbtn",
210 		.acpi_match_table = intel_vbtn_ids,
211 		.pm = &intel_vbtn_pm_ops,
212 	},
213 	.probe = intel_vbtn_probe,
214 	.remove = intel_vbtn_remove,
215 };
216 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
217 
218 static acpi_status __init
check_acpi_dev(acpi_handle handle,u32 lvl,void * context,void ** rv)219 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
220 {
221 	const struct acpi_device_id *ids = context;
222 	struct acpi_device *dev;
223 
224 	if (acpi_bus_get_device(handle, &dev) != 0)
225 		return AE_OK;
226 
227 	if (acpi_match_device_ids(dev, ids) == 0)
228 		if (acpi_create_platform_device(dev, NULL))
229 			dev_info(&dev->dev,
230 				 "intel-vbtn: created platform device\n");
231 
232 	return AE_OK;
233 }
234 
intel_vbtn_init(void)235 static int __init intel_vbtn_init(void)
236 {
237 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
238 			    ACPI_UINT32_MAX, check_acpi_dev, NULL,
239 			    (void *)intel_vbtn_ids, NULL);
240 
241 	return platform_driver_register(&intel_vbtn_pl_driver);
242 }
243 module_init(intel_vbtn_init);
244 
intel_vbtn_exit(void)245 static void __exit intel_vbtn_exit(void)
246 {
247 	platform_driver_unregister(&intel_vbtn_pl_driver);
248 }
249 module_exit(intel_vbtn_exit);
250