]> Git Repo - linux.git/blob - drivers/platform/x86/intel-vbtn.c
drm/nouveau/kms: Don't change EDID when it hasn't actually changed
[linux.git] / drivers / platform / x86 / intel-vbtn.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Intel Virtual Button driver for Windows 8.1+
4  *
5  *  Copyright (C) 2016 AceLan Kao <[email protected]>
6  *  Copyright (C) 2016 Alex Hung <[email protected]>
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 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
30
31 /* In theory, these are HID usages. */
32 static 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
45 static 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
55 struct 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
63 static 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
81 static 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
120 static 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
158 out_unknown:
159         dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
160 }
161
162 static 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 static bool intel_vbtn_has_switches(acpi_handle handle)
171 {
172         const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
173         unsigned long long vgbs;
174         acpi_status status;
175
176         /*
177          * Some normal laptops have a VGBS method despite being non-convertible
178          * and their VGBS method always returns 0, causing detect_tablet_mode()
179          * to report SW_TABLET_MODE=1 to userspace, which causes issues.
180          * These laptops have a DMI chassis_type of 9 ("Laptop"), do not report
181          * switches on any devices with a DMI chassis_type of 9.
182          */
183         if (chassis_type && strcmp(chassis_type, "9") == 0)
184                 return false;
185
186         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
187         return ACPI_SUCCESS(status);
188 }
189
190 static int intel_vbtn_probe(struct platform_device *device)
191 {
192         acpi_handle handle = ACPI_HANDLE(&device->dev);
193         bool has_buttons, has_switches;
194         struct intel_vbtn_priv *priv;
195         acpi_status status;
196         int err;
197
198         has_buttons = intel_vbtn_has_buttons(handle);
199         has_switches = intel_vbtn_has_switches(handle);
200
201         if (!has_buttons && !has_switches) {
202                 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
203                 return -ENODEV;
204         }
205
206         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
207         if (!priv)
208                 return -ENOMEM;
209         dev_set_drvdata(&device->dev, priv);
210
211         priv->has_buttons = has_buttons;
212         priv->has_switches = has_switches;
213
214         err = intel_vbtn_input_setup(device);
215         if (err) {
216                 pr_err("Failed to setup Intel Virtual Button\n");
217                 return err;
218         }
219
220         status = acpi_install_notify_handler(handle,
221                                              ACPI_DEVICE_NOTIFY,
222                                              notify_handler,
223                                              device);
224         if (ACPI_FAILURE(status))
225                 return -EBUSY;
226
227         device_init_wakeup(&device->dev, true);
228         /*
229          * In order for system wakeup to work, the EC GPE has to be marked as
230          * a wakeup one, so do that here (this setting will persist, but it has
231          * no effect until the wakeup mask is set for the EC GPE).
232          */
233         acpi_ec_mark_gpe_for_wake();
234         return 0;
235 }
236
237 static int intel_vbtn_remove(struct platform_device *device)
238 {
239         acpi_handle handle = ACPI_HANDLE(&device->dev);
240
241         device_init_wakeup(&device->dev, false);
242         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
243
244         /*
245          * Even if we failed to shut off the event stream, we can still
246          * safely detach from the device.
247          */
248         return 0;
249 }
250
251 static int intel_vbtn_pm_prepare(struct device *dev)
252 {
253         if (device_may_wakeup(dev)) {
254                 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
255
256                 priv->wakeup_mode = true;
257         }
258         return 0;
259 }
260
261 static void intel_vbtn_pm_complete(struct device *dev)
262 {
263         struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
264
265         priv->wakeup_mode = false;
266 }
267
268 static int intel_vbtn_pm_resume(struct device *dev)
269 {
270         intel_vbtn_pm_complete(dev);
271         return 0;
272 }
273
274 static const struct dev_pm_ops intel_vbtn_pm_ops = {
275         .prepare = intel_vbtn_pm_prepare,
276         .complete = intel_vbtn_pm_complete,
277         .resume = intel_vbtn_pm_resume,
278         .restore = intel_vbtn_pm_resume,
279         .thaw = intel_vbtn_pm_resume,
280 };
281
282 static struct platform_driver intel_vbtn_pl_driver = {
283         .driver = {
284                 .name = "intel-vbtn",
285                 .acpi_match_table = intel_vbtn_ids,
286                 .pm = &intel_vbtn_pm_ops,
287         },
288         .probe = intel_vbtn_probe,
289         .remove = intel_vbtn_remove,
290 };
291
292 static acpi_status __init
293 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
294 {
295         const struct acpi_device_id *ids = context;
296         struct acpi_device *dev;
297
298         if (acpi_bus_get_device(handle, &dev) != 0)
299                 return AE_OK;
300
301         if (acpi_match_device_ids(dev, ids) == 0)
302                 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
303                         dev_info(&dev->dev,
304                                  "intel-vbtn: created platform device\n");
305
306         return AE_OK;
307 }
308
309 static int __init intel_vbtn_init(void)
310 {
311         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
312                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
313                             (void *)intel_vbtn_ids, NULL);
314
315         return platform_driver_register(&intel_vbtn_pl_driver);
316 }
317 module_init(intel_vbtn_init);
318
319 static void __exit intel_vbtn_exit(void)
320 {
321         platform_driver_unregister(&intel_vbtn_pl_driver);
322 }
323 module_exit(intel_vbtn_exit);
This page took 0.050456 seconds and 4 git commands to generate.