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