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