Linux Audio

Check our new training course

Loading...
Note: File does not exist in v4.6.
  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
 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 */
 40	{ KE_END },
 41};
 42
 43struct intel_vbtn_priv {
 44	struct input_dev *input_dev;
 45};
 46
 47static int intel_vbtn_input_setup(struct platform_device *device)
 48{
 49	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
 50	int ret;
 51
 52	priv->input_dev = devm_input_allocate_device(&device->dev);
 53	if (!priv->input_dev)
 54		return -ENOMEM;
 55
 56	ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
 57	if (ret)
 58		return ret;
 59
 60	priv->input_dev->dev.parent = &device->dev;
 61	priv->input_dev->name = "Intel Virtual Button driver";
 62	priv->input_dev->id.bustype = BUS_HOST;
 63
 64	return input_register_device(priv->input_dev);
 65}
 66
 67static void notify_handler(acpi_handle handle, u32 event, void *context)
 68{
 69	struct platform_device *device = context;
 70	struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
 71
 72	if (!sparse_keymap_report_event(priv->input_dev, event, 1, true))
 73		dev_info(&device->dev, "unknown event index 0x%x\n",
 74			 event);
 75}
 76
 77static int intel_vbtn_probe(struct platform_device *device)
 78{
 79	acpi_handle handle = ACPI_HANDLE(&device->dev);
 80	struct intel_vbtn_priv *priv;
 81	acpi_status status;
 82	int err;
 83
 84	status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
 85	if (ACPI_FAILURE(status)) {
 86		dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
 87		return -ENODEV;
 88	}
 89
 90	priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
 91	if (!priv)
 92		return -ENOMEM;
 93	dev_set_drvdata(&device->dev, priv);
 94
 95	err = intel_vbtn_input_setup(device);
 96	if (err) {
 97		pr_err("Failed to setup Intel Virtual Button\n");
 98		return err;
 99	}
100
101	status = acpi_install_notify_handler(handle,
102					     ACPI_DEVICE_NOTIFY,
103					     notify_handler,
104					     device);
105	if (ACPI_FAILURE(status))
106		return -EBUSY;
107
108	return 0;
109}
110
111static int intel_vbtn_remove(struct platform_device *device)
112{
113	acpi_handle handle = ACPI_HANDLE(&device->dev);
114
115	acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
116
117	/*
118	 * Even if we failed to shut off the event stream, we can still
119	 * safely detach from the device.
120	 */
121	return 0;
122}
123
124static struct platform_driver intel_vbtn_pl_driver = {
125	.driver = {
126		.name = "intel-vbtn",
127		.acpi_match_table = intel_vbtn_ids,
128	},
129	.probe = intel_vbtn_probe,
130	.remove = intel_vbtn_remove,
131};
132MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
133
134static acpi_status __init
135check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
136{
137	const struct acpi_device_id *ids = context;
138	struct acpi_device *dev;
139
140	if (acpi_bus_get_device(handle, &dev) != 0)
141		return AE_OK;
142
143	if (acpi_match_device_ids(dev, ids) == 0)
144		if (acpi_create_platform_device(dev, NULL))
145			dev_info(&dev->dev,
146				 "intel-vbtn: created platform device\n");
147
148	return AE_OK;
149}
150
151static int __init intel_vbtn_init(void)
152{
153	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
154			    ACPI_UINT32_MAX, check_acpi_dev, NULL,
155			    (void *)intel_vbtn_ids, NULL);
156
157	return platform_driver_register(&intel_vbtn_pl_driver);
158}
159module_init(intel_vbtn_init);
160
161static void __exit intel_vbtn_exit(void)
162{
163	platform_driver_unregister(&intel_vbtn_pl_driver);
164}
165module_exit(intel_vbtn_exit);