1 // SPDX-License-Identifier: GPL-2.0+
3 * Intel Virtual Button driver for Windows 8.1+
5 * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
6 * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
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>
18 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19 #define TABLET_MODE_FLAG 0x40
20 #define DOCK_MODE_FLAG 0x80
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("AceLan Kao");
25 static const struct acpi_device_id intel_vbtn_ids
[] = {
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 */
49 struct intel_vbtn_priv
{
50 struct input_dev
*input_dev
;
54 static int intel_vbtn_input_setup(struct platform_device
*device
)
56 struct intel_vbtn_priv
*priv
= dev_get_drvdata(&device
->dev
);
59 priv
->input_dev
= devm_input_allocate_device(&device
->dev
);
63 ret
= sparse_keymap_setup(priv
->input_dev
, intel_vbtn_keymap
, NULL
);
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
;
71 return input_register_device(priv
->input_dev
);
74 static void notify_handler(acpi_handle handle
, u32 event
, void *context
)
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
;
82 if (priv
->wakeup_mode
) {
83 if (sparse_keymap_entry_from_scancode(priv
->input_dev
, event
)) {
84 pm_wakeup_hard_event(&device
->dev
);
91 * Even press events are autorelease if there is no corresponding odd
92 * release event, or if the odd event is KE_IGNORE.
94 ke_rel
= sparse_keymap_entry_from_scancode(priv
->input_dev
, event
| 1);
95 autorelease
= val
&& (!ke_rel
|| ke_rel
->type
== KE_IGNORE
);
97 if (sparse_keymap_report_event(priv
->input_dev
, event
, val
, autorelease
))
101 dev_dbg(&device
->dev
, "unknown event index 0x%x\n", event
);
104 static void detect_tablet_mode(struct platform_device
*device
)
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
;
114 if (!(chassis_type
&& strcmp(chassis_type
, "31") == 0))
117 status
= acpi_evaluate_object(handle
, "VGBS", NULL
, &vgbs_output
);
118 if (ACPI_FAILURE(status
))
121 obj
= vgbs_output
.pointer
;
122 if (!(obj
&& obj
->type
== ACPI_TYPE_INTEGER
))
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
);
130 kfree(vgbs_output
.pointer
);
133 static int intel_vbtn_probe(struct platform_device
*device
)
135 acpi_handle handle
= ACPI_HANDLE(&device
->dev
);
136 struct intel_vbtn_priv
*priv
;
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");
146 priv
= devm_kzalloc(&device
->dev
, sizeof(*priv
), GFP_KERNEL
);
149 dev_set_drvdata(&device
->dev
, priv
);
151 err
= intel_vbtn_input_setup(device
);
153 pr_err("Failed to setup Intel Virtual Button\n");
157 detect_tablet_mode(device
);
159 status
= acpi_install_notify_handler(handle
,
163 if (ACPI_FAILURE(status
))
166 device_init_wakeup(&device
->dev
, true);
170 static int intel_vbtn_remove(struct platform_device
*device
)
172 acpi_handle handle
= ACPI_HANDLE(&device
->dev
);
174 device_init_wakeup(&device
->dev
, false);
175 acpi_remove_notify_handler(handle
, ACPI_DEVICE_NOTIFY
, notify_handler
);
178 * Even if we failed to shut off the event stream, we can still
179 * safely detach from the device.
184 static int intel_vbtn_pm_prepare(struct device
*dev
)
186 struct intel_vbtn_priv
*priv
= dev_get_drvdata(dev
);
188 priv
->wakeup_mode
= true;
192 static int intel_vbtn_pm_resume(struct device
*dev
)
194 struct intel_vbtn_priv
*priv
= dev_get_drvdata(dev
);
196 priv
->wakeup_mode
= false;
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
,
207 static struct platform_driver intel_vbtn_pl_driver
= {
209 .name
= "intel-vbtn",
210 .acpi_match_table
= intel_vbtn_ids
,
211 .pm
= &intel_vbtn_pm_ops
,
213 .probe
= intel_vbtn_probe
,
214 .remove
= intel_vbtn_remove
,
216 MODULE_DEVICE_TABLE(acpi
, intel_vbtn_ids
);
218 static acpi_status __init
219 check_acpi_dev(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
221 const struct acpi_device_id
*ids
= context
;
222 struct acpi_device
*dev
;
224 if (acpi_bus_get_device(handle
, &dev
) != 0)
227 if (acpi_match_device_ids(dev
, ids
) == 0)
228 if (acpi_create_platform_device(dev
, NULL
))
230 "intel-vbtn: created platform device\n");
235 static int __init
intel_vbtn_init(void)
237 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
238 ACPI_UINT32_MAX
, check_acpi_dev
, NULL
,
239 (void *)intel_vbtn_ids
, NULL
);
241 return platform_driver_register(&intel_vbtn_pl_driver
);
243 module_init(intel_vbtn_init
);
245 static void __exit
intel_vbtn_exit(void)
247 platform_driver_unregister(&intel_vbtn_pl_driver
);
249 module_exit(intel_vbtn_exit
);