GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / platform / x86 / hp-wireless.c
1 /*
2  *  Airplane mode button for HP & Xiaomi laptops
3  *
4  *  Copyright (C) 2014-2017 Alex Hung <alex.hung@canonical.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/acpi.h>
27 #include <acpi/acpi_bus.h>
28
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Alex Hung");
31 MODULE_ALIAS("acpi*:HPQ6001:*");
32 MODULE_ALIAS("acpi*:WSTADEF:*");
33 MODULE_ALIAS("acpi*:AMDI0051:*");
34
35 static struct input_dev *hpwl_input_dev;
36
37 static const struct acpi_device_id hpwl_ids[] = {
38         {"HPQ6001", 0},
39         {"WSTADEF", 0},
40         {"AMDI0051", 0},
41         {"", 0},
42 };
43
44 static int hp_wireless_input_setup(void)
45 {
46         int err;
47
48         hpwl_input_dev = input_allocate_device();
49         if (!hpwl_input_dev)
50                 return -ENOMEM;
51
52         hpwl_input_dev->name = "HP Wireless hotkeys";
53         hpwl_input_dev->phys = "hpq6001/input0";
54         hpwl_input_dev->id.bustype = BUS_HOST;
55         hpwl_input_dev->evbit[0] = BIT(EV_KEY);
56         set_bit(KEY_RFKILL, hpwl_input_dev->keybit);
57
58         err = input_register_device(hpwl_input_dev);
59         if (err)
60                 goto err_free_dev;
61
62         return 0;
63
64 err_free_dev:
65         input_free_device(hpwl_input_dev);
66         return err;
67 }
68
69 static void hp_wireless_input_destroy(void)
70 {
71         input_unregister_device(hpwl_input_dev);
72 }
73
74 static void hpwl_notify(struct acpi_device *acpi_dev, u32 event)
75 {
76         if (event != 0x80) {
77                 pr_info("Received unknown event (0x%x)\n", event);
78                 return;
79         }
80
81         input_report_key(hpwl_input_dev, KEY_RFKILL, 1);
82         input_sync(hpwl_input_dev);
83         input_report_key(hpwl_input_dev, KEY_RFKILL, 0);
84         input_sync(hpwl_input_dev);
85 }
86
87 static int hpwl_add(struct acpi_device *device)
88 {
89         int err;
90
91         err = hp_wireless_input_setup();
92         if (err)
93                 pr_err("Failed to setup hp wireless hotkeys\n");
94
95         return err;
96 }
97
98 static int hpwl_remove(struct acpi_device *device)
99 {
100         hp_wireless_input_destroy();
101         return 0;
102 }
103
104 static struct acpi_driver hpwl_driver = {
105         .name   = "hp-wireless",
106         .owner  = THIS_MODULE,
107         .ids    = hpwl_ids,
108         .ops    = {
109                 .add    = hpwl_add,
110                 .remove = hpwl_remove,
111                 .notify = hpwl_notify,
112         },
113 };
114
115 module_acpi_driver(hpwl_driver);