• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
26 #include <acpi/acpi_bus.h>
27 
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("AceLan Kao");
30 
31 static const struct acpi_device_id intel_vbtn_ids[] = {
32 	{"INT33D6", 0},
33 	{"", 0},
34 };
35 
36 /* In theory, these are HID usages. */
37 static 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 */
40 	{ 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 */
44 	{ KE_END },
45 };
46 
47 struct intel_vbtn_priv {
48 	struct input_dev *input_dev;
49 };
50 
intel_vbtn_input_setup(struct platform_device * device)51 static 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 
56 	priv->input_dev = input_allocate_device();
57 	if (!priv->input_dev)
58 		return -ENOMEM;
59 
60 	ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
61 	if (ret)
62 		goto err_free_device;
63 
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 
68 	ret = input_register_device(priv->input_dev);
69 	if (ret)
70 		goto err_free_device;
71 
72 	return 0;
73 
74 err_free_device:
75 	input_free_device(priv->input_dev);
76 	return ret;
77 }
78 
intel_vbtn_input_destroy(struct platform_device * device)79 static void intel_vbtn_input_destroy(struct platform_device *device)
80 {
81 	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
82 
83 	input_unregister_device(priv->input_dev);
84 }
85 
notify_handler(acpi_handle handle,u32 event,void * context)86 static void notify_handler(acpi_handle handle, u32 event, void *context)
87 {
88 	struct platform_device *device = context;
89 	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
90 
91 	if (!sparse_keymap_report_event(priv->input_dev, event, 1, true))
92 		dev_info(&device->dev, "unknown event index 0x%x\n",
93 			 event);
94 }
95 
intel_vbtn_probe(struct platform_device * device)96 static int intel_vbtn_probe(struct platform_device *device)
97 {
98 	acpi_handle handle = ACPI_HANDLE(&device->dev);
99 	struct intel_vbtn_priv *priv;
100 	acpi_status status;
101 	int err;
102 
103 	status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
104 	if (!ACPI_SUCCESS(status)) {
105 		dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
106 		return -ENODEV;
107 	}
108 
109 	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
110 	if (!priv)
111 		return -ENOMEM;
112 	dev_set_drvdata(&device->dev, priv);
113 
114 	err = intel_vbtn_input_setup(device);
115 	if (err) {
116 		pr_err("Failed to setup Intel Virtual Button\n");
117 		return err;
118 	}
119 
120 	status = acpi_install_notify_handler(handle,
121 					     ACPI_DEVICE_NOTIFY,
122 					     notify_handler,
123 					     device);
124 	if (ACPI_FAILURE(status)) {
125 		err = -EBUSY;
126 		goto err_remove_input;
127 	}
128 
129 	return 0;
130 
131 err_remove_input:
132 	intel_vbtn_input_destroy(device);
133 
134 	return err;
135 }
136 
intel_vbtn_remove(struct platform_device * device)137 static int intel_vbtn_remove(struct platform_device *device)
138 {
139 	acpi_handle handle = ACPI_HANDLE(&device->dev);
140 
141 	intel_vbtn_input_destroy(device);
142 	acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
143 
144 	/*
145 	 * Even if we failed to shut off the event stream, we can still
146 	 * safely detach from the device.
147 	 */
148 	return 0;
149 }
150 
151 static struct platform_driver intel_vbtn_pl_driver = {
152 	.driver = {
153 		.name = "intel-vbtn",
154 		.acpi_match_table = intel_vbtn_ids,
155 	},
156 	.probe = intel_vbtn_probe,
157 	.remove = intel_vbtn_remove,
158 };
159 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
160 
161 static acpi_status __init
check_acpi_dev(acpi_handle handle,u32 lvl,void * context,void ** rv)162 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
163 {
164 	const struct acpi_device_id *ids = context;
165 	struct acpi_device *dev;
166 
167 	if (acpi_bus_get_device(handle, &dev) != 0)
168 		return AE_OK;
169 
170 	if (acpi_match_device_ids(dev, ids) == 0)
171 		if (acpi_create_platform_device(dev, NULL))
172 			dev_info(&dev->dev,
173 				 "intel-vbtn: created platform device\n");
174 
175 	return AE_OK;
176 }
177 
intel_vbtn_init(void)178 static int __init intel_vbtn_init(void)
179 {
180 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
181 			    ACPI_UINT32_MAX, check_acpi_dev, NULL,
182 			    (void *)intel_vbtn_ids, NULL);
183 
184 	return platform_driver_register(&intel_vbtn_pl_driver);
185 }
186 module_init(intel_vbtn_init);
187 
intel_vbtn_exit(void)188 static void __exit intel_vbtn_exit(void)
189 {
190 	platform_driver_unregister(&intel_vbtn_pl_driver);
191 }
192 module_exit(intel_vbtn_exit);
193