Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.7 275 lines 7.6 kB view raw
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/* When NOT in tablet mode, VGBS returns with the flag 0x40 */ 19#define TABLET_MODE_FLAG 0x40 20#define DOCK_MODE_FLAG 0x80 21 22MODULE_LICENSE("GPL"); 23MODULE_AUTHOR("AceLan Kao"); 24 25static const struct acpi_device_id intel_vbtn_ids[] = { 26 {"INT33D6", 0}, 27 {"", 0}, 28}; 29MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids); 30 31/* In theory, these are HID usages. */ 32static const struct key_entry intel_vbtn_keymap[] = { 33 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */ 34 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */ 35 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */ 36 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */ 37 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */ 38 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */ 39 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */ 40 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */ 41 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */ 42 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */ 43 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */ 44 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */ 45 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */ 46 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */ 47 { KE_END }, 48}; 49 50struct intel_vbtn_priv { 51 struct input_dev *input_dev; 52 bool wakeup_mode; 53}; 54 55static int intel_vbtn_input_setup(struct platform_device *device) 56{ 57 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev); 58 int ret; 59 60 priv->input_dev = devm_input_allocate_device(&device->dev); 61 if (!priv->input_dev) 62 return -ENOMEM; 63 64 ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL); 65 if (ret) 66 return ret; 67 68 priv->input_dev->dev.parent = &device->dev; 69 priv->input_dev->name = "Intel Virtual Button driver"; 70 priv->input_dev->id.bustype = BUS_HOST; 71 72 return input_register_device(priv->input_dev); 73} 74 75static void notify_handler(acpi_handle handle, u32 event, void *context) 76{ 77 struct platform_device *device = context; 78 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev); 79 unsigned int val = !(event & 1); /* Even=press, Odd=release */ 80 const struct key_entry *ke, *ke_rel; 81 bool autorelease; 82 83 if (priv->wakeup_mode) { 84 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event); 85 if (ke) { 86 pm_wakeup_hard_event(&device->dev); 87 88 /* 89 * Switch events like tablet mode will wake the device 90 * and report the new switch position to the input 91 * subsystem. 92 */ 93 if (ke->type == KE_SW) 94 sparse_keymap_report_event(priv->input_dev, 95 event, 96 val, 97 0); 98 return; 99 } 100 goto out_unknown; 101 } 102 103 /* 104 * Even press events are autorelease if there is no corresponding odd 105 * release event, or if the odd event is KE_IGNORE. 106 */ 107 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1); 108 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE); 109 110 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease)) 111 return; 112 113out_unknown: 114 dev_dbg(&device->dev, "unknown event index 0x%x\n", event); 115} 116 117static void detect_tablet_mode(struct platform_device *device) 118{ 119 const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE); 120 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev); 121 acpi_handle handle = ACPI_HANDLE(&device->dev); 122 struct acpi_buffer vgbs_output = { ACPI_ALLOCATE_BUFFER, NULL }; 123 union acpi_object *obj; 124 acpi_status status; 125 int m; 126 127 if (!(chassis_type && strcmp(chassis_type, "31") == 0)) 128 goto out; 129 130 status = acpi_evaluate_object(handle, "VGBS", NULL, &vgbs_output); 131 if (ACPI_FAILURE(status)) 132 goto out; 133 134 obj = vgbs_output.pointer; 135 if (!(obj && obj->type == ACPI_TYPE_INTEGER)) 136 goto out; 137 138 m = !(obj->integer.value & TABLET_MODE_FLAG); 139 input_report_switch(priv->input_dev, SW_TABLET_MODE, m); 140 m = (obj->integer.value & DOCK_MODE_FLAG) ? 1 : 0; 141 input_report_switch(priv->input_dev, SW_DOCK, m); 142out: 143 kfree(vgbs_output.pointer); 144} 145 146static int intel_vbtn_probe(struct platform_device *device) 147{ 148 acpi_handle handle = ACPI_HANDLE(&device->dev); 149 struct intel_vbtn_priv *priv; 150 acpi_status status; 151 int err; 152 153 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL); 154 if (ACPI_FAILURE(status)) { 155 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n"); 156 return -ENODEV; 157 } 158 159 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL); 160 if (!priv) 161 return -ENOMEM; 162 dev_set_drvdata(&device->dev, priv); 163 164 err = intel_vbtn_input_setup(device); 165 if (err) { 166 pr_err("Failed to setup Intel Virtual Button\n"); 167 return err; 168 } 169 170 detect_tablet_mode(device); 171 172 status = acpi_install_notify_handler(handle, 173 ACPI_DEVICE_NOTIFY, 174 notify_handler, 175 device); 176 if (ACPI_FAILURE(status)) 177 return -EBUSY; 178 179 device_init_wakeup(&device->dev, true); 180 /* 181 * In order for system wakeup to work, the EC GPE has to be marked as 182 * a wakeup one, so do that here (this setting will persist, but it has 183 * no effect until the wakeup mask is set for the EC GPE). 184 */ 185 acpi_ec_mark_gpe_for_wake(); 186 return 0; 187} 188 189static int intel_vbtn_remove(struct platform_device *device) 190{ 191 acpi_handle handle = ACPI_HANDLE(&device->dev); 192 193 device_init_wakeup(&device->dev, false); 194 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler); 195 196 /* 197 * Even if we failed to shut off the event stream, we can still 198 * safely detach from the device. 199 */ 200 return 0; 201} 202 203static int intel_vbtn_pm_prepare(struct device *dev) 204{ 205 if (device_may_wakeup(dev)) { 206 struct intel_vbtn_priv *priv = dev_get_drvdata(dev); 207 208 priv->wakeup_mode = true; 209 } 210 return 0; 211} 212 213static void intel_vbtn_pm_complete(struct device *dev) 214{ 215 struct intel_vbtn_priv *priv = dev_get_drvdata(dev); 216 217 priv->wakeup_mode = false; 218} 219 220static int intel_vbtn_pm_resume(struct device *dev) 221{ 222 intel_vbtn_pm_complete(dev); 223 return 0; 224} 225 226static const struct dev_pm_ops intel_vbtn_pm_ops = { 227 .prepare = intel_vbtn_pm_prepare, 228 .complete = intel_vbtn_pm_complete, 229 .resume = intel_vbtn_pm_resume, 230 .restore = intel_vbtn_pm_resume, 231 .thaw = intel_vbtn_pm_resume, 232}; 233 234static struct platform_driver intel_vbtn_pl_driver = { 235 .driver = { 236 .name = "intel-vbtn", 237 .acpi_match_table = intel_vbtn_ids, 238 .pm = &intel_vbtn_pm_ops, 239 }, 240 .probe = intel_vbtn_probe, 241 .remove = intel_vbtn_remove, 242}; 243 244static acpi_status __init 245check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv) 246{ 247 const struct acpi_device_id *ids = context; 248 struct acpi_device *dev; 249 250 if (acpi_bus_get_device(handle, &dev) != 0) 251 return AE_OK; 252 253 if (acpi_match_device_ids(dev, ids) == 0) 254 if (acpi_create_platform_device(dev, NULL)) 255 dev_info(&dev->dev, 256 "intel-vbtn: created platform device\n"); 257 258 return AE_OK; 259} 260 261static int __init intel_vbtn_init(void) 262{ 263 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 264 ACPI_UINT32_MAX, check_acpi_dev, NULL, 265 (void *)intel_vbtn_ids, NULL); 266 267 return platform_driver_register(&intel_vbtn_pl_driver); 268} 269module_init(intel_vbtn_init); 270 271static void __exit intel_vbtn_exit(void) 272{ 273 platform_driver_unregister(&intel_vbtn_pl_driver); 274} 275module_exit(intel_vbtn_exit);