2 Dell Airplane Mode Switch driver
3 Copyright (C) 2014-2015 Pali Rohár <pali.rohar@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/rfkill.h>
19 #include <linux/input.h>
29 struct rfkill *rfkill;
30 struct input_dev *input_dev;
38 static enum rbtn_type rbtn_check(struct acpi_device *device)
40 unsigned long long output;
43 status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
44 if (ACPI_FAILURE(status))
59 static int rbtn_get(struct acpi_device *device)
61 unsigned long long output;
64 status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
65 if (ACPI_FAILURE(status))
71 static int rbtn_acquire(struct acpi_device *device, bool enable)
73 struct acpi_object_list input;
74 union acpi_object param;
77 param.type = ACPI_TYPE_INTEGER;
78 param.integer.value = enable;
80 input.pointer = ¶m;
82 status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
83 if (ACPI_FAILURE(status))
94 static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
96 struct acpi_device *device = data;
99 state = rbtn_get(device);
103 rfkill_set_states(rfkill, state, state);
106 static int rbtn_rfkill_set_block(void *data, bool blocked)
108 /* NOTE: setting soft rfkill state is not supported */
112 static struct rfkill_ops rbtn_ops = {
113 .query = rbtn_rfkill_query,
114 .set_block = rbtn_rfkill_set_block,
117 static int rbtn_rfkill_init(struct acpi_device *device)
119 struct rbtn_data *rbtn_data = device->driver_data;
122 if (rbtn_data->rfkill)
126 * NOTE: rbtn controls all radio devices, not only WLAN
127 * but rfkill interface does not support "ANY" type
128 * so "WLAN" type is used
130 rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
131 RFKILL_TYPE_WLAN, &rbtn_ops, device);
132 if (!rbtn_data->rfkill)
135 ret = rfkill_register(rbtn_data->rfkill);
137 rfkill_destroy(rbtn_data->rfkill);
138 rbtn_data->rfkill = NULL;
145 static void rbtn_rfkill_exit(struct acpi_device *device)
147 struct rbtn_data *rbtn_data = device->driver_data;
149 if (!rbtn_data->rfkill)
152 rfkill_unregister(rbtn_data->rfkill);
153 rfkill_destroy(rbtn_data->rfkill);
154 rbtn_data->rfkill = NULL;
157 static void rbtn_rfkill_event(struct acpi_device *device)
159 struct rbtn_data *rbtn_data = device->driver_data;
161 if (rbtn_data->rfkill)
162 rbtn_rfkill_query(rbtn_data->rfkill, device);
170 static int rbtn_input_init(struct rbtn_data *rbtn_data)
174 rbtn_data->input_dev = input_allocate_device();
175 if (!rbtn_data->input_dev)
178 rbtn_data->input_dev->name = "DELL Wireless hotkeys";
179 rbtn_data->input_dev->phys = "dellabce/input0";
180 rbtn_data->input_dev->id.bustype = BUS_HOST;
181 rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
182 set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
184 ret = input_register_device(rbtn_data->input_dev);
186 input_free_device(rbtn_data->input_dev);
187 rbtn_data->input_dev = NULL;
194 static void rbtn_input_exit(struct rbtn_data *rbtn_data)
196 input_unregister_device(rbtn_data->input_dev);
197 rbtn_data->input_dev = NULL;
200 static void rbtn_input_event(struct rbtn_data *rbtn_data)
202 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
203 input_sync(rbtn_data->input_dev);
204 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
205 input_sync(rbtn_data->input_dev);
213 static int rbtn_add(struct acpi_device *device);
214 static int rbtn_remove(struct acpi_device *device);
215 static void rbtn_notify(struct acpi_device *device, u32 event);
217 static const struct acpi_device_id rbtn_ids[] = {
223 static struct acpi_driver rbtn_driver = {
228 .remove = rbtn_remove,
229 .notify = rbtn_notify,
231 .owner = THIS_MODULE,
236 * notifier export functions
239 static bool auto_remove_rfkill = true;
241 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
243 static int rbtn_inc_count(struct device *dev, void *data)
245 struct acpi_device *device = to_acpi_device(dev);
246 struct rbtn_data *rbtn_data = device->driver_data;
249 if (rbtn_data->type == RBTN_SLIDER)
255 static int rbtn_switch_dev(struct device *dev, void *data)
257 struct acpi_device *device = to_acpi_device(dev);
258 struct rbtn_data *rbtn_data = device->driver_data;
261 if (rbtn_data->type != RBTN_SLIDER)
265 rbtn_rfkill_init(device);
267 rbtn_rfkill_exit(device);
272 int dell_rbtn_notifier_register(struct notifier_block *nb)
279 ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
281 if (ret || count == 0)
284 first = !rbtn_chain_head.head;
286 ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
290 if (auto_remove_rfkill && first)
291 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
292 (void *)false, rbtn_switch_dev);
296 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
298 int dell_rbtn_notifier_unregister(struct notifier_block *nb)
302 ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
306 if (auto_remove_rfkill && !rbtn_chain_head.head)
307 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
308 (void *)true, rbtn_switch_dev);
312 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
316 * acpi driver functions
319 static int rbtn_add(struct acpi_device *device)
321 struct rbtn_data *rbtn_data;
325 type = rbtn_check(device);
326 if (type == RBTN_UNKNOWN) {
327 dev_info(&device->dev, "Unknown device type\n");
331 ret = rbtn_acquire(device, true);
333 dev_err(&device->dev, "Cannot enable device\n");
337 rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
341 rbtn_data->type = type;
342 device->driver_data = rbtn_data;
344 switch (rbtn_data->type) {
346 ret = rbtn_input_init(rbtn_data);
349 if (auto_remove_rfkill && rbtn_chain_head.head)
352 ret = rbtn_rfkill_init(device);
362 static int rbtn_remove(struct acpi_device *device)
364 struct rbtn_data *rbtn_data = device->driver_data;
366 switch (rbtn_data->type) {
368 rbtn_input_exit(rbtn_data);
371 rbtn_rfkill_exit(device);
377 rbtn_acquire(device, false);
378 device->driver_data = NULL;
383 static void rbtn_notify(struct acpi_device *device, u32 event)
385 struct rbtn_data *rbtn_data = device->driver_data;
388 dev_info(&device->dev, "Received unknown event (0x%x)\n",
393 switch (rbtn_data->type) {
395 rbtn_input_event(rbtn_data);
398 rbtn_rfkill_event(device);
399 atomic_notifier_call_chain(&rbtn_chain_head, event, device);
411 module_acpi_driver(rbtn_driver);
413 module_param(auto_remove_rfkill, bool, 0444);
415 MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
416 "other modules start receiving events "
417 "from this module and re-add them when "
418 "the last module stops receiving events "
420 MODULE_DEVICE_TABLE(acpi, rbtn_ids);
421 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
422 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
423 MODULE_LICENSE("GPL");