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