AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Intel Virtual Button driver for Windows 8.1+ |
| 3 | * |
| 4 | * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com> |
| 5 | * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/input.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/input/sparse-keymap.h> |
| 25 | #include <linux/acpi.h> |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 26 | #include <linux/suspend.h> |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 27 | #include <acpi/acpi_bus.h> |
| 28 | |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | MODULE_AUTHOR("AceLan Kao"); |
| 31 | |
| 32 | static const struct acpi_device_id intel_vbtn_ids[] = { |
| 33 | {"INT33D6", 0}, |
| 34 | {"", 0}, |
| 35 | }; |
| 36 | |
| 37 | /* In theory, these are HID usages. */ |
| 38 | static const struct key_entry intel_vbtn_keymap[] = { |
Mario Limonciello | c801603 | 2017-08-04 12:00:06 -0500 | [diff] [blame] | 39 | { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */ |
| 40 | { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */ |
Maarten Maathuis | 8d9e299 | 2017-04-24 23:35:21 +0200 | [diff] [blame] | 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 */ |
Stefan BrĂ¼ns | 1c82849 | 2017-11-09 23:44:32 +0100 | [diff] [blame^] | 45 | { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */ |
| 46 | { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */ |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 47 | { KE_END }, |
| 48 | }; |
| 49 | |
| 50 | struct intel_vbtn_priv { |
| 51 | struct input_dev *input_dev; |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 52 | bool wakeup_mode; |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static 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 | |
Axel Lin | bb9ad48 | 2016-09-20 17:01:24 +0800 | [diff] [blame] | 60 | priv->input_dev = devm_input_allocate_device(&device->dev); |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 61 | if (!priv->input_dev) |
| 62 | return -ENOMEM; |
| 63 | |
| 64 | ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL); |
| 65 | if (ret) |
Axel Lin | bb9ad48 | 2016-09-20 17:01:24 +0800 | [diff] [blame] | 66 | return ret; |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 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 | |
Axel Lin | bb9ad48 | 2016-09-20 17:01:24 +0800 | [diff] [blame] | 72 | return input_register_device(priv->input_dev); |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static 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 | |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 80 | if (priv->wakeup_mode) { |
| 81 | if (sparse_keymap_entry_from_scancode(priv->input_dev, event)) { |
| 82 | pm_wakeup_hard_event(&device->dev); |
| 83 | return; |
| 84 | } |
| 85 | } else if (sparse_keymap_report_event(priv->input_dev, event, 1, true)) { |
| 86 | return; |
| 87 | } |
Alex Hung | a9c37b7 | 2017-07-20 20:56:48 -0700 | [diff] [blame] | 88 | dev_dbg(&device->dev, "unknown event index 0x%x\n", event); |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int intel_vbtn_probe(struct platform_device *device) |
| 92 | { |
| 93 | acpi_handle handle = ACPI_HANDLE(&device->dev); |
| 94 | struct intel_vbtn_priv *priv; |
| 95 | acpi_status status; |
| 96 | int err; |
| 97 | |
| 98 | status = acpi_evaluate_object(handle, "VBDL", NULL, NULL); |
Axel Lin | 3526eca | 2016-09-19 09:33:51 +0800 | [diff] [blame] | 99 | if (ACPI_FAILURE(status)) { |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 100 | dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n"); |
| 101 | return -ENODEV; |
| 102 | } |
| 103 | |
| 104 | priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL); |
| 105 | if (!priv) |
| 106 | return -ENOMEM; |
| 107 | dev_set_drvdata(&device->dev, priv); |
| 108 | |
| 109 | err = intel_vbtn_input_setup(device); |
| 110 | if (err) { |
| 111 | pr_err("Failed to setup Intel Virtual Button\n"); |
| 112 | return err; |
| 113 | } |
| 114 | |
| 115 | status = acpi_install_notify_handler(handle, |
| 116 | ACPI_DEVICE_NOTIFY, |
| 117 | notify_handler, |
| 118 | device); |
Axel Lin | bb9ad48 | 2016-09-20 17:01:24 +0800 | [diff] [blame] | 119 | if (ACPI_FAILURE(status)) |
| 120 | return -EBUSY; |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 121 | |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 122 | device_init_wakeup(&device->dev, true); |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 123 | return 0; |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int intel_vbtn_remove(struct platform_device *device) |
| 127 | { |
| 128 | acpi_handle handle = ACPI_HANDLE(&device->dev); |
| 129 | |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 130 | acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler); |
| 131 | |
| 132 | /* |
| 133 | * Even if we failed to shut off the event stream, we can still |
| 134 | * safely detach from the device. |
| 135 | */ |
| 136 | return 0; |
| 137 | } |
| 138 | |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 139 | static int intel_vbtn_pm_prepare(struct device *dev) |
| 140 | { |
| 141 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); |
| 142 | |
| 143 | priv->wakeup_mode = true; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int intel_vbtn_pm_resume(struct device *dev) |
| 148 | { |
| 149 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); |
| 150 | |
| 151 | priv->wakeup_mode = false; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static const struct dev_pm_ops intel_vbtn_pm_ops = { |
| 156 | .prepare = intel_vbtn_pm_prepare, |
| 157 | .resume = intel_vbtn_pm_resume, |
| 158 | .restore = intel_vbtn_pm_resume, |
| 159 | .thaw = intel_vbtn_pm_resume, |
| 160 | }; |
| 161 | |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 162 | static struct platform_driver intel_vbtn_pl_driver = { |
| 163 | .driver = { |
| 164 | .name = "intel-vbtn", |
| 165 | .acpi_match_table = intel_vbtn_ids, |
Rafael J. Wysocki | 91f9e85 | 2017-06-08 02:15:52 +0200 | [diff] [blame] | 166 | .pm = &intel_vbtn_pm_ops, |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 167 | }, |
| 168 | .probe = intel_vbtn_probe, |
| 169 | .remove = intel_vbtn_remove, |
| 170 | }; |
| 171 | MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids); |
| 172 | |
| 173 | static acpi_status __init |
| 174 | check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 175 | { |
| 176 | const struct acpi_device_id *ids = context; |
| 177 | struct acpi_device *dev; |
| 178 | |
| 179 | if (acpi_bus_get_device(handle, &dev) != 0) |
| 180 | return AE_OK; |
| 181 | |
| 182 | if (acpi_match_device_ids(dev, ids) == 0) |
Heikki Krogerus | 1571875 | 2016-11-03 16:21:26 +0200 | [diff] [blame] | 183 | if (acpi_create_platform_device(dev, NULL)) |
AceLan Kao | 332e081 | 2016-07-01 09:51:49 +0800 | [diff] [blame] | 184 | dev_info(&dev->dev, |
| 185 | "intel-vbtn: created platform device\n"); |
| 186 | |
| 187 | return AE_OK; |
| 188 | } |
| 189 | |
| 190 | static int __init intel_vbtn_init(void) |
| 191 | { |
| 192 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 193 | ACPI_UINT32_MAX, check_acpi_dev, NULL, |
| 194 | (void *)intel_vbtn_ids, NULL); |
| 195 | |
| 196 | return platform_driver_register(&intel_vbtn_pl_driver); |
| 197 | } |
| 198 | module_init(intel_vbtn_init); |
| 199 | |
| 200 | static void __exit intel_vbtn_exit(void) |
| 201 | { |
| 202 | platform_driver_unregister(&intel_vbtn_pl_driver); |
| 203 | } |
| 204 | module_exit(intel_vbtn_exit); |