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