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>
21 #include "dell-rbtn.h"
31 struct rfkill *rfkill;
32 struct input_dev *input_dev;
41 static enum rbtn_type rbtn_check(struct acpi_device *device)
43 unsigned long long output;
46 status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
47 if (ACPI_FAILURE(status))
62 static int rbtn_get(struct acpi_device *device)
64 unsigned long long output;
67 status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
68 if (ACPI_FAILURE(status))
74 static int rbtn_acquire(struct acpi_device *device, bool enable)
76 struct acpi_object_list input;
77 union acpi_object param;
80 param.type = ACPI_TYPE_INTEGER;
81 param.integer.value = enable;
83 input.pointer = ¶m;
85 status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
86 if (ACPI_FAILURE(status))
97 static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
99 struct acpi_device *device = data;
102 state = rbtn_get(device);
106 rfkill_set_states(rfkill, state, state);
109 static int rbtn_rfkill_set_block(void *data, bool blocked)
111 /* NOTE: setting soft rfkill state is not supported */
115 static const struct rfkill_ops rbtn_ops = {
116 .query = rbtn_rfkill_query,
117 .set_block = rbtn_rfkill_set_block,
120 static int rbtn_rfkill_init(struct acpi_device *device)
122 struct rbtn_data *rbtn_data = device->driver_data;
125 if (rbtn_data->rfkill)
129 * NOTE: rbtn controls all radio devices, not only WLAN
130 * but rfkill interface does not support "ANY" type
131 * so "WLAN" type is used
133 rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
134 RFKILL_TYPE_WLAN, &rbtn_ops, device);
135 if (!rbtn_data->rfkill)
138 ret = rfkill_register(rbtn_data->rfkill);
140 rfkill_destroy(rbtn_data->rfkill);
141 rbtn_data->rfkill = NULL;
148 static void rbtn_rfkill_exit(struct acpi_device *device)
150 struct rbtn_data *rbtn_data = device->driver_data;
152 if (!rbtn_data->rfkill)
155 rfkill_unregister(rbtn_data->rfkill);
156 rfkill_destroy(rbtn_data->rfkill);
157 rbtn_data->rfkill = NULL;
160 static void rbtn_rfkill_event(struct acpi_device *device)
162 struct rbtn_data *rbtn_data = device->driver_data;
164 if (rbtn_data->rfkill)
165 rbtn_rfkill_query(rbtn_data->rfkill, device);
173 static int rbtn_input_init(struct rbtn_data *rbtn_data)
177 rbtn_data->input_dev = input_allocate_device();
178 if (!rbtn_data->input_dev)
181 rbtn_data->input_dev->name = "DELL Wireless hotkeys";
182 rbtn_data->input_dev->phys = "dellabce/input0";
183 rbtn_data->input_dev->id.bustype = BUS_HOST;
184 rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
185 set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
187 ret = input_register_device(rbtn_data->input_dev);
189 input_free_device(rbtn_data->input_dev);
190 rbtn_data->input_dev = NULL;
197 static void rbtn_input_exit(struct rbtn_data *rbtn_data)
199 input_unregister_device(rbtn_data->input_dev);
200 rbtn_data->input_dev = NULL;
203 static void rbtn_input_event(struct rbtn_data *rbtn_data)
205 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
206 input_sync(rbtn_data->input_dev);
207 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
208 input_sync(rbtn_data->input_dev);
216 static int rbtn_add(struct acpi_device *device);
217 static int rbtn_remove(struct acpi_device *device);
218 static void rbtn_notify(struct acpi_device *device, u32 event);
220 static const struct acpi_device_id rbtn_ids[] = {
225 * This driver can also handle the "DELLABC6" device that
226 * appears on the XPS 13 9350, but that device is disabled by
227 * the DSDT unless booted with acpi_osi="!Windows 2012"
228 * acpi_osi="!Windows 2013".
230 * According to Mario at Dell:
232 * DELLABC6 is a custom interface that was created solely to
233 * have airplane mode support for Windows 7. For Windows 10
234 * the proper interface is to use that which is handled by
235 * intel-hid. A OEM airplane mode driver is not used.
237 * Since the kernel doesn't identify as Windows 7 it would be
238 * incorrect to do attempt to use that interface.
240 * Even if we override _OSI and bind to DELLABC6, we end up with
241 * inconsistent behavior in which userspace can get out of sync
242 * with the rfkill state as it conflicts with events from
245 * The upshot is that it is better to just ignore DELLABC6
252 #ifdef CONFIG_PM_SLEEP
253 static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
255 struct rbtn_data *rbtn_data = context;
257 rbtn_data->suspended = false;
260 static int rbtn_suspend(struct device *dev)
262 struct acpi_device *device = to_acpi_device(dev);
263 struct rbtn_data *rbtn_data = acpi_driver_data(device);
265 rbtn_data->suspended = true;
270 static int rbtn_resume(struct device *dev)
272 struct acpi_device *device = to_acpi_device(dev);
273 struct rbtn_data *rbtn_data = acpi_driver_data(device);
277 * Upon resume, some BIOSes send an ACPI notification thet triggers
278 * an unwanted input event. In order to ignore it, we use a flag
279 * that we set at suspend and clear once we have received the extra
280 * ACPI notification. Since ACPI notifications are delivered
281 * asynchronously to drivers, we clear the flag from the workqueue
282 * used to deliver the notifications. This should be enough
283 * to have the flag cleared only after we received the extra
284 * notification, if any.
286 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
287 rbtn_clear_suspended_flag, rbtn_data);
288 if (ACPI_FAILURE(status))
289 rbtn_clear_suspended_flag(rbtn_data);
295 static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
297 static struct acpi_driver rbtn_driver = {
300 .drv.pm = &rbtn_pm_ops,
303 .remove = rbtn_remove,
304 .notify = rbtn_notify,
306 .owner = THIS_MODULE,
311 * notifier export functions
314 static bool auto_remove_rfkill = true;
316 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
318 static int rbtn_inc_count(struct device *dev, void *data)
320 struct acpi_device *device = to_acpi_device(dev);
321 struct rbtn_data *rbtn_data = device->driver_data;
324 if (rbtn_data->type == RBTN_SLIDER)
330 static int rbtn_switch_dev(struct device *dev, void *data)
332 struct acpi_device *device = to_acpi_device(dev);
333 struct rbtn_data *rbtn_data = device->driver_data;
336 if (rbtn_data->type != RBTN_SLIDER)
340 rbtn_rfkill_init(device);
342 rbtn_rfkill_exit(device);
347 int dell_rbtn_notifier_register(struct notifier_block *nb)
354 ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
356 if (ret || count == 0)
359 first = !rbtn_chain_head.head;
361 ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
365 if (auto_remove_rfkill && first)
366 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
367 (void *)false, rbtn_switch_dev);
371 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
373 int dell_rbtn_notifier_unregister(struct notifier_block *nb)
377 ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
381 if (auto_remove_rfkill && !rbtn_chain_head.head)
382 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
383 (void *)true, rbtn_switch_dev);
387 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
391 * acpi driver functions
394 static int rbtn_add(struct acpi_device *device)
396 struct rbtn_data *rbtn_data;
400 type = rbtn_check(device);
401 if (type == RBTN_UNKNOWN) {
402 dev_info(&device->dev, "Unknown device type\n");
406 ret = rbtn_acquire(device, true);
408 dev_err(&device->dev, "Cannot enable device\n");
412 rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
416 rbtn_data->type = type;
417 device->driver_data = rbtn_data;
419 switch (rbtn_data->type) {
421 ret = rbtn_input_init(rbtn_data);
424 if (auto_remove_rfkill && rbtn_chain_head.head)
427 ret = rbtn_rfkill_init(device);
437 static int rbtn_remove(struct acpi_device *device)
439 struct rbtn_data *rbtn_data = device->driver_data;
441 switch (rbtn_data->type) {
443 rbtn_input_exit(rbtn_data);
446 rbtn_rfkill_exit(device);
452 rbtn_acquire(device, false);
453 device->driver_data = NULL;
458 static void rbtn_notify(struct acpi_device *device, u32 event)
460 struct rbtn_data *rbtn_data = device->driver_data;
463 * Some BIOSes send a notification at resume.
464 * Ignore it to prevent unwanted input events.
466 if (rbtn_data->suspended) {
467 dev_dbg(&device->dev, "ACPI notification ignored\n");
472 dev_info(&device->dev, "Received unknown event (0x%x)\n",
477 switch (rbtn_data->type) {
479 rbtn_input_event(rbtn_data);
482 rbtn_rfkill_event(device);
483 atomic_notifier_call_chain(&rbtn_chain_head, event, device);
495 module_acpi_driver(rbtn_driver);
497 module_param(auto_remove_rfkill, bool, 0444);
499 MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
500 "other modules start receiving events "
501 "from this module and re-add them when "
502 "the last module stops receiving events "
504 MODULE_DEVICE_TABLE(acpi, rbtn_ids);
505 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
506 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
507 MODULE_LICENSE("GPL");