blob: c2035e121ac2944c880cb4716c7026870277878a [file] [log] [blame]
AceLan Kao332e0812016-07-01 09:51:49 +08001/*
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>
26#include <acpi/acpi_bus.h>
27
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("AceLan Kao");
30
31static const struct acpi_device_id intel_vbtn_ids[] = {
32 {"INT33D6", 0},
33 {"", 0},
34};
35
36/* In theory, these are HID usages. */
37static const struct key_entry intel_vbtn_keymap[] = {
38 { KE_IGNORE, 0xC0, { KEY_POWER } }, /* power key press */
39 { KE_KEY, 0xC1, { KEY_POWER } }, /* power key release */
Maarten Maathuis8d9e2992017-04-24 23:35:21 +020040 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
41 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
42 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
43 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
AceLan Kao332e0812016-07-01 09:51:49 +080044 { KE_END },
45};
46
47struct intel_vbtn_priv {
48 struct input_dev *input_dev;
49};
50
51static int intel_vbtn_input_setup(struct platform_device *device)
52{
53 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
54 int ret;
55
Axel Linbb9ad482016-09-20 17:01:24 +080056 priv->input_dev = devm_input_allocate_device(&device->dev);
AceLan Kao332e0812016-07-01 09:51:49 +080057 if (!priv->input_dev)
58 return -ENOMEM;
59
60 ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
61 if (ret)
Axel Linbb9ad482016-09-20 17:01:24 +080062 return ret;
AceLan Kao332e0812016-07-01 09:51:49 +080063
64 priv->input_dev->dev.parent = &device->dev;
65 priv->input_dev->name = "Intel Virtual Button driver";
66 priv->input_dev->id.bustype = BUS_HOST;
67
Axel Linbb9ad482016-09-20 17:01:24 +080068 return input_register_device(priv->input_dev);
AceLan Kao332e0812016-07-01 09:51:49 +080069}
70
71static void notify_handler(acpi_handle handle, u32 event, void *context)
72{
73 struct platform_device *device = context;
74 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
75
76 if (!sparse_keymap_report_event(priv->input_dev, event, 1, true))
77 dev_info(&device->dev, "unknown event index 0x%x\n",
78 event);
79}
80
81static int intel_vbtn_probe(struct platform_device *device)
82{
83 acpi_handle handle = ACPI_HANDLE(&device->dev);
84 struct intel_vbtn_priv *priv;
85 acpi_status status;
86 int err;
87
88 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
Axel Lin3526eca2016-09-19 09:33:51 +080089 if (ACPI_FAILURE(status)) {
AceLan Kao332e0812016-07-01 09:51:49 +080090 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
91 return -ENODEV;
92 }
93
94 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
95 if (!priv)
96 return -ENOMEM;
97 dev_set_drvdata(&device->dev, priv);
98
99 err = intel_vbtn_input_setup(device);
100 if (err) {
101 pr_err("Failed to setup Intel Virtual Button\n");
102 return err;
103 }
104
105 status = acpi_install_notify_handler(handle,
106 ACPI_DEVICE_NOTIFY,
107 notify_handler,
108 device);
Axel Linbb9ad482016-09-20 17:01:24 +0800109 if (ACPI_FAILURE(status))
110 return -EBUSY;
AceLan Kao332e0812016-07-01 09:51:49 +0800111
112 return 0;
AceLan Kao332e0812016-07-01 09:51:49 +0800113}
114
115static int intel_vbtn_remove(struct platform_device *device)
116{
117 acpi_handle handle = ACPI_HANDLE(&device->dev);
118
AceLan Kao332e0812016-07-01 09:51:49 +0800119 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
120
121 /*
122 * Even if we failed to shut off the event stream, we can still
123 * safely detach from the device.
124 */
125 return 0;
126}
127
128static struct platform_driver intel_vbtn_pl_driver = {
129 .driver = {
130 .name = "intel-vbtn",
131 .acpi_match_table = intel_vbtn_ids,
132 },
133 .probe = intel_vbtn_probe,
134 .remove = intel_vbtn_remove,
135};
136MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
137
138static acpi_status __init
139check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
140{
141 const struct acpi_device_id *ids = context;
142 struct acpi_device *dev;
143
144 if (acpi_bus_get_device(handle, &dev) != 0)
145 return AE_OK;
146
147 if (acpi_match_device_ids(dev, ids) == 0)
Heikki Krogerus15718752016-11-03 16:21:26 +0200148 if (acpi_create_platform_device(dev, NULL))
AceLan Kao332e0812016-07-01 09:51:49 +0800149 dev_info(&dev->dev,
150 "intel-vbtn: created platform device\n");
151
152 return AE_OK;
153}
154
155static int __init intel_vbtn_init(void)
156{
157 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
158 ACPI_UINT32_MAX, check_acpi_dev, NULL,
159 (void *)intel_vbtn_ids, NULL);
160
161 return platform_driver_register(&intel_vbtn_pl_driver);
162}
163module_init(intel_vbtn_init);
164
165static void __exit intel_vbtn_exit(void)
166{
167 platform_driver_unregister(&intel_vbtn_pl_driver);
168}
169module_exit(intel_vbtn_exit);