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