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