GNU Linux-libre 4.9.328-gnu1
[releases.git] / drivers / platform / x86 / asus-wireless.c
1 /*
2  * Asus Wireless Radio Control Driver
3  *
4  * Copyright (C) 2015-2016 Endless Mobile, Inc.
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 version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/acpi.h>
16 #include <linux/input.h>
17 #include <linux/pci_ids.h>
18 #include <linux/leds.h>
19
20 #define ASUS_WIRELESS_LED_STATUS 0x2
21 #define ASUS_WIRELESS_LED_OFF 0x4
22 #define ASUS_WIRELESS_LED_ON 0x5
23
24 struct asus_wireless_data {
25         struct input_dev *idev;
26         struct acpi_device *adev;
27         struct workqueue_struct *wq;
28         struct work_struct led_work;
29         struct led_classdev led;
30         int led_state;
31 };
32
33 static u64 asus_wireless_method(acpi_handle handle, const char *method,
34                                 int param)
35 {
36         struct acpi_object_list p;
37         union acpi_object obj;
38         acpi_status s;
39         u64 ret;
40
41         acpi_handle_debug(handle, "Evaluating method %s, parameter %#x\n",
42                           method, param);
43         obj.type = ACPI_TYPE_INTEGER;
44         obj.integer.value = param;
45         p.count = 1;
46         p.pointer = &obj;
47
48         s = acpi_evaluate_integer(handle, (acpi_string) method, &p, &ret);
49         if (ACPI_FAILURE(s))
50                 acpi_handle_err(handle,
51                                 "Failed to eval method %s, param %#x (%d)\n",
52                                 method, param, s);
53         acpi_handle_debug(handle, "%s returned %#x\n", method, (uint) ret);
54         return ret;
55 }
56
57 static enum led_brightness led_state_get(struct led_classdev *led)
58 {
59         struct asus_wireless_data *data;
60         int s;
61
62         data = container_of(led, struct asus_wireless_data, led);
63         s = asus_wireless_method(acpi_device_handle(data->adev), "HSWC",
64                                  ASUS_WIRELESS_LED_STATUS);
65         if (s == ASUS_WIRELESS_LED_ON)
66                 return LED_FULL;
67         return LED_OFF;
68 }
69
70 static void led_state_update(struct work_struct *work)
71 {
72         struct asus_wireless_data *data;
73
74         data = container_of(work, struct asus_wireless_data, led_work);
75         asus_wireless_method(acpi_device_handle(data->adev), "HSWC",
76                              data->led_state);
77 }
78
79 static void led_state_set(struct led_classdev *led,
80                                   enum led_brightness value)
81 {
82         struct asus_wireless_data *data;
83
84         data = container_of(led, struct asus_wireless_data, led);
85         data->led_state = value == LED_OFF ? ASUS_WIRELESS_LED_OFF :
86                                              ASUS_WIRELESS_LED_ON;
87         queue_work(data->wq, &data->led_work);
88 }
89
90 static void asus_wireless_notify(struct acpi_device *adev, u32 event)
91 {
92         struct asus_wireless_data *data = acpi_driver_data(adev);
93
94         dev_dbg(&adev->dev, "event=%#x\n", event);
95         if (event != 0x88) {
96                 dev_notice(&adev->dev, "Unknown ASHS event: %#x\n", event);
97                 return;
98         }
99         input_report_key(data->idev, KEY_RFKILL, 1);
100         input_sync(data->idev);
101         input_report_key(data->idev, KEY_RFKILL, 0);
102         input_sync(data->idev);
103 }
104
105 static int asus_wireless_add(struct acpi_device *adev)
106 {
107         struct asus_wireless_data *data;
108         int err;
109
110         data = devm_kzalloc(&adev->dev, sizeof(*data), GFP_KERNEL);
111         if (!data)
112                 return -ENOMEM;
113         adev->driver_data = data;
114
115         data->idev = devm_input_allocate_device(&adev->dev);
116         if (!data->idev)
117                 return -ENOMEM;
118         data->idev->name = "Asus Wireless Radio Control";
119         data->idev->phys = "asus-wireless/input0";
120         data->idev->id.bustype = BUS_HOST;
121         data->idev->id.vendor = PCI_VENDOR_ID_ASUSTEK;
122         set_bit(EV_KEY, data->idev->evbit);
123         set_bit(KEY_RFKILL, data->idev->keybit);
124         err = input_register_device(data->idev);
125         if (err)
126                 return err;
127
128         data->adev = adev;
129         data->wq = create_singlethread_workqueue("asus_wireless_workqueue");
130         if (!data->wq)
131                 return -ENOMEM;
132         INIT_WORK(&data->led_work, led_state_update);
133         data->led.name = "asus-wireless::airplane";
134         data->led.brightness_set = led_state_set;
135         data->led.brightness_get = led_state_get;
136         data->led.flags = LED_CORE_SUSPENDRESUME;
137         data->led.max_brightness = 1;
138         err = devm_led_classdev_register(&adev->dev, &data->led);
139         if (err)
140                 destroy_workqueue(data->wq);
141         return err;
142 }
143
144 static int asus_wireless_remove(struct acpi_device *adev)
145 {
146         struct asus_wireless_data *data = acpi_driver_data(adev);
147
148         if (data->wq) {
149                 devm_led_classdev_unregister(&adev->dev, &data->led);
150                 destroy_workqueue(data->wq);
151         }
152         return 0;
153 }
154
155 static const struct acpi_device_id device_ids[] = {
156         {"ATK4001", 0},
157         {"ATK4002", 0},
158         {"", 0},
159 };
160 MODULE_DEVICE_TABLE(acpi, device_ids);
161
162 static struct acpi_driver asus_wireless_driver = {
163         .name = "Asus Wireless Radio Control Driver",
164         .class = "hotkey",
165         .ids = device_ids,
166         .ops = {
167                 .add = asus_wireless_add,
168                 .remove = asus_wireless_remove,
169                 .notify = asus_wireless_notify,
170         },
171 };
172 module_acpi_driver(asus_wireless_driver);
173
174 MODULE_DESCRIPTION("Asus Wireless Radio Control Driver");
175 MODULE_AUTHOR("João Paulo Rechi Vita <jprvita@gmail.com>");
176 MODULE_LICENSE("GPL");