linux/drivers/platform/x86/intel-hid.c
<<
>>
Prefs
   1/*
   2 *  Intel HID event & 5 button array driver
   3 *
   4 *  Copyright (C) 2015 Alex Hung <alex.hung@canonical.com>
   5 *  Copyright (C) 2015 Andrew Lutomirski <luto@kernel.org>
   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("Alex Hung");
  30
  31static const struct acpi_device_id intel_hid_ids[] = {
  32        {"INT33D5", 0},
  33        {"", 0},
  34};
  35
  36/* In theory, these are HID usages. */
  37static const struct key_entry intel_hid_keymap[] = {
  38        /* 1: LSuper (Page 0x07, usage 0xE3) -- unclear what to do */
  39        /* 2: Toggle SW_ROTATE_LOCK -- easy to implement if seen in wild */
  40        { KE_KEY, 3, { KEY_NUMLOCK } },
  41        { KE_KEY, 4, { KEY_HOME } },
  42        { KE_KEY, 5, { KEY_END } },
  43        { KE_KEY, 6, { KEY_PAGEUP } },
  44        { KE_KEY, 7, { KEY_PAGEDOWN } },
  45        { KE_KEY, 8, { KEY_RFKILL } },
  46        { KE_KEY, 9, { KEY_POWER } },
  47        { KE_KEY, 11, { KEY_SLEEP } },
  48        /* 13 has two different meanings in the spec -- ignore it. */
  49        { KE_KEY, 14, { KEY_STOPCD } },
  50        { KE_KEY, 15, { KEY_PLAYPAUSE } },
  51        { KE_KEY, 16, { KEY_MUTE } },
  52        { KE_KEY, 17, { KEY_VOLUMEUP } },
  53        { KE_KEY, 18, { KEY_VOLUMEDOWN } },
  54        { KE_KEY, 19, { KEY_BRIGHTNESSUP } },
  55        { KE_KEY, 20, { KEY_BRIGHTNESSDOWN } },
  56        /* 27: wake -- needs special handling */
  57        { KE_END },
  58};
  59
  60/* 5 button array notification value. */
  61static const struct key_entry intel_array_keymap[] = {
  62        { KE_KEY,    0xC2, { KEY_LEFTMETA } },                /* Press */
  63        { KE_IGNORE, 0xC3, { KEY_LEFTMETA } },                /* Release */
  64        { KE_KEY,    0xC4, { KEY_VOLUMEUP } },                /* Press */
  65        { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } },                /* Release */
  66        { KE_KEY,    0xC6, { KEY_VOLUMEDOWN } },              /* Press */
  67        { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } },              /* Release */
  68        { KE_SW,     0xC8, { .sw = { SW_ROTATE_LOCK, 1 } } }, /* Press */
  69        { KE_SW,     0xC9, { .sw = { SW_ROTATE_LOCK, 0 } } }, /* Release */
  70        { KE_KEY,    0xCE, { KEY_POWER } },                   /* Press */
  71        { KE_IGNORE, 0xCF, { KEY_POWER } },                   /* Release */
  72        { KE_END },
  73};
  74
  75struct intel_hid_priv {
  76        struct input_dev *input_dev;
  77        struct input_dev *array;
  78};
  79
  80static int intel_hid_set_enable(struct device *device, int enable)
  81{
  82        union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  83        struct acpi_object_list args = { 1, &arg0 };
  84        acpi_status status;
  85
  86        arg0.integer.value = enable;
  87        status = acpi_evaluate_object(ACPI_HANDLE(device), "HDSM", &args, NULL);
  88        if (ACPI_FAILURE(status)) {
  89                dev_warn(device, "failed to %sable hotkeys\n",
  90                         enable ? "en" : "dis");
  91                return -EIO;
  92        }
  93
  94        return 0;
  95}
  96
  97static void intel_button_array_enable(struct device *device, bool enable)
  98{
  99        struct intel_hid_priv *priv = dev_get_drvdata(device);
 100        acpi_handle handle = ACPI_HANDLE(device);
 101        unsigned long long button_cap;
 102        acpi_status status;
 103
 104        if (!priv->array)
 105                return;
 106
 107        /* Query supported platform features */
 108        status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
 109        if (ACPI_FAILURE(status)) {
 110                dev_warn(device, "failed to get button capability\n");
 111                return;
 112        }
 113
 114        /* Enable|disable features - power button is always enabled */
 115        status = acpi_execute_simple_method(handle, "BTNE",
 116                                            enable ? button_cap : 1);
 117        if (ACPI_FAILURE(status))
 118                dev_warn(device, "failed to set button capability\n");
 119}
 120
 121static int intel_hid_pl_suspend_handler(struct device *device)
 122{
 123        intel_hid_set_enable(device, 0);
 124        intel_button_array_enable(device, false);
 125
 126        return 0;
 127}
 128
 129static int intel_hid_pl_resume_handler(struct device *device)
 130{
 131        intel_hid_set_enable(device, 1);
 132        intel_button_array_enable(device, true);
 133
 134        return 0;
 135}
 136
 137static const struct dev_pm_ops intel_hid_pl_pm_ops = {
 138        .freeze  = intel_hid_pl_suspend_handler,
 139        .restore  = intel_hid_pl_resume_handler,
 140        .suspend  = intel_hid_pl_suspend_handler,
 141        .resume  = intel_hid_pl_resume_handler,
 142};
 143
 144static int intel_hid_input_setup(struct platform_device *device)
 145{
 146        struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
 147        int ret;
 148
 149        priv->input_dev = input_allocate_device();
 150        if (!priv->input_dev)
 151                return -ENOMEM;
 152
 153        ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
 154        if (ret)
 155                goto err_free_device;
 156
 157        priv->input_dev->dev.parent = &device->dev;
 158        priv->input_dev->name = "Intel HID events";
 159        priv->input_dev->id.bustype = BUS_HOST;
 160        set_bit(KEY_RFKILL, priv->input_dev->keybit);
 161
 162        ret = input_register_device(priv->input_dev);
 163        if (ret)
 164                goto err_free_device;
 165
 166        return 0;
 167
 168err_free_device:
 169        input_free_device(priv->input_dev);
 170        return ret;
 171}
 172
 173static int intel_button_array_input_setup(struct platform_device *device)
 174{
 175        struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
 176        int ret;
 177
 178        /* Setup input device for 5 button array */
 179        priv->array = devm_input_allocate_device(&device->dev);
 180        if (!priv->array)
 181                return -ENOMEM;
 182
 183        ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
 184        if (ret)
 185                return ret;
 186
 187        priv->array->dev.parent = &device->dev;
 188        priv->array->name = "Intel HID 5 button array";
 189        priv->array->id.bustype = BUS_HOST;
 190
 191        return input_register_device(priv->array);
 192}
 193
 194static void intel_hid_input_destroy(struct platform_device *device)
 195{
 196        struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
 197
 198        input_unregister_device(priv->input_dev);
 199}
 200
 201static void notify_handler(acpi_handle handle, u32 event, void *context)
 202{
 203        struct platform_device *device = context;
 204        struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
 205        unsigned long long ev_index;
 206        acpi_status status;
 207
 208        /* 0xC0 is for HID events, other values are for 5 button array */
 209        if (event != 0xc0) {
 210                if (!priv->array ||
 211                    !sparse_keymap_report_event(priv->array, event, 1, true))
 212                        dev_info(&device->dev, "unknown event 0x%x\n", event);
 213                return;
 214        }
 215
 216        status = acpi_evaluate_integer(handle, "HDEM", NULL, &ev_index);
 217        if (ACPI_FAILURE(status)) {
 218                dev_warn(&device->dev, "failed to get event index\n");
 219                return;
 220        }
 221
 222        if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
 223                dev_info(&device->dev, "unknown event index 0x%llx\n",
 224                         ev_index);
 225}
 226
 227static int intel_hid_probe(struct platform_device *device)
 228{
 229        acpi_handle handle = ACPI_HANDLE(&device->dev);
 230        unsigned long long event_cap, mode;
 231        struct intel_hid_priv *priv;
 232        acpi_status status;
 233        int err;
 234
 235        status = acpi_evaluate_integer(handle, "HDMM", NULL, &mode);
 236        if (ACPI_FAILURE(status)) {
 237                dev_warn(&device->dev, "failed to read mode\n");
 238                return -ENODEV;
 239        }
 240
 241        if (mode != 0) {
 242                /*
 243                 * This driver only implements "simple" mode.  There appear
 244                 * to be no other modes, but we should be paranoid and check
 245                 * for compatibility.
 246                 */
 247                dev_info(&device->dev, "platform is not in simple mode\n");
 248                return -ENODEV;
 249        }
 250
 251        priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
 252        if (!priv)
 253                return -ENOMEM;
 254        dev_set_drvdata(&device->dev, priv);
 255
 256        err = intel_hid_input_setup(device);
 257        if (err) {
 258                pr_err("Failed to setup Intel HID hotkeys\n");
 259                return err;
 260        }
 261
 262        /* Setup 5 button array */
 263        status = acpi_evaluate_integer(handle, "HEBC", NULL, &event_cap);
 264        if (ACPI_SUCCESS(status) && (event_cap & 0x20000)) {
 265                dev_info(&device->dev, "platform supports 5 button array\n");
 266                err = intel_button_array_input_setup(device);
 267                if (err)
 268                        pr_err("Failed to setup Intel 5 button array hotkeys\n");
 269        }
 270
 271        status = acpi_install_notify_handler(handle,
 272                                             ACPI_DEVICE_NOTIFY,
 273                                             notify_handler,
 274                                             device);
 275        if (ACPI_FAILURE(status)) {
 276                err = -EBUSY;
 277                goto err_remove_input;
 278        }
 279
 280        err = intel_hid_set_enable(&device->dev, 1);
 281        if (err)
 282                goto err_remove_notify;
 283
 284        if (priv->array) {
 285                intel_button_array_enable(&device->dev, true);
 286
 287                /* Call button load method to enable HID power button */
 288                status = acpi_evaluate_object(handle, "BTNL", NULL, NULL);
 289                if (ACPI_FAILURE(status))
 290                        dev_warn(&device->dev,
 291                                 "failed to enable HID power button\n");
 292        }
 293
 294        return 0;
 295
 296err_remove_notify:
 297        acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
 298
 299err_remove_input:
 300        intel_hid_input_destroy(device);
 301
 302        return err;
 303}
 304
 305static int intel_hid_remove(struct platform_device *device)
 306{
 307        acpi_handle handle = ACPI_HANDLE(&device->dev);
 308
 309        acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
 310        intel_hid_input_destroy(device);
 311        intel_hid_set_enable(&device->dev, 0);
 312        intel_button_array_enable(&device->dev, false);
 313
 314        /*
 315         * Even if we failed to shut off the event stream, we can still
 316         * safely detach from the device.
 317         */
 318        return 0;
 319}
 320
 321static struct platform_driver intel_hid_pl_driver = {
 322        .driver = {
 323                .name = "intel-hid",
 324                .acpi_match_table = intel_hid_ids,
 325                .pm = &intel_hid_pl_pm_ops,
 326        },
 327        .probe = intel_hid_probe,
 328        .remove = intel_hid_remove,
 329};
 330MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
 331
 332/*
 333 * Unfortunately, some laptops provide a _HID="INT33D5" device with
 334 * _CID="PNP0C02".  This causes the pnpacpi scan driver to claim the
 335 * ACPI node, so no platform device will be created.  The pnpacpi
 336 * driver rejects this device in subsequent processing, so no physical
 337 * node is created at all.
 338 *
 339 * As a workaround until the ACPI core figures out how to handle
 340 * this corner case, manually ask the ACPI platform device code to
 341 * claim the ACPI node.
 342 */
 343static acpi_status __init
 344check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
 345{
 346        const struct acpi_device_id *ids = context;
 347        struct acpi_device *dev;
 348
 349        if (acpi_bus_get_device(handle, &dev) != 0)
 350                return AE_OK;
 351
 352        if (acpi_match_device_ids(dev, ids) == 0)
 353                if (acpi_create_platform_device(dev, NULL))
 354                        dev_info(&dev->dev,
 355                                 "intel-hid: created platform device\n");
 356
 357        return AE_OK;
 358}
 359
 360static int __init intel_hid_init(void)
 361{
 362        acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
 363                            ACPI_UINT32_MAX, check_acpi_dev, NULL,
 364                            (void *)intel_hid_ids, NULL);
 365
 366        return platform_driver_register(&intel_hid_pl_driver);
 367}
 368module_init(intel_hid_init);
 369
 370static void __exit intel_hid_exit(void)
 371{
 372        platform_driver_unregister(&intel_hid_pl_driver);
 373}
 374module_exit(intel_hid_exit);
 375