1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Dell Airplane Mode Switch driver
4 Copyright (C) 2014-2015 Pali Rohár <pali@kernel.org>
8 #include <linux/module.h>
9 #include <linux/acpi.h>
10 #include <linux/rfkill.h>
11 #include <linux/input.h>
13 #include "dell-rbtn.h"
23 struct rfkill *rfkill;
24 struct input_dev *input_dev;
33 static enum rbtn_type rbtn_check(struct acpi_device *device)
35 unsigned long long output;
38 status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
39 if (ACPI_FAILURE(status))
54 static int rbtn_get(struct acpi_device *device)
56 unsigned long long output;
59 status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
60 if (ACPI_FAILURE(status))
66 static int rbtn_acquire(struct acpi_device *device, bool enable)
68 struct acpi_object_list input;
69 union acpi_object param;
72 param.type = ACPI_TYPE_INTEGER;
73 param.integer.value = enable;
75 input.pointer = ¶m;
77 status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
78 if (ACPI_FAILURE(status))
89 static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
91 struct acpi_device *device = data;
94 state = rbtn_get(device);
98 rfkill_set_states(rfkill, state, state);
101 static int rbtn_rfkill_set_block(void *data, bool blocked)
103 /* NOTE: setting soft rfkill state is not supported */
107 static const struct rfkill_ops rbtn_ops = {
108 .query = rbtn_rfkill_query,
109 .set_block = rbtn_rfkill_set_block,
112 static int rbtn_rfkill_init(struct acpi_device *device)
114 struct rbtn_data *rbtn_data = device->driver_data;
117 if (rbtn_data->rfkill)
121 * NOTE: rbtn controls all radio devices, not only WLAN
122 * but rfkill interface does not support "ANY" type
123 * so "WLAN" type is used
125 rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
126 RFKILL_TYPE_WLAN, &rbtn_ops, device);
127 if (!rbtn_data->rfkill)
130 ret = rfkill_register(rbtn_data->rfkill);
132 rfkill_destroy(rbtn_data->rfkill);
133 rbtn_data->rfkill = NULL;
140 static void rbtn_rfkill_exit(struct acpi_device *device)
142 struct rbtn_data *rbtn_data = device->driver_data;
144 if (!rbtn_data->rfkill)
147 rfkill_unregister(rbtn_data->rfkill);
148 rfkill_destroy(rbtn_data->rfkill);
149 rbtn_data->rfkill = NULL;
152 static void rbtn_rfkill_event(struct acpi_device *device)
154 struct rbtn_data *rbtn_data = device->driver_data;
156 if (rbtn_data->rfkill)
157 rbtn_rfkill_query(rbtn_data->rfkill, device);
165 static int rbtn_input_init(struct rbtn_data *rbtn_data)
169 rbtn_data->input_dev = input_allocate_device();
170 if (!rbtn_data->input_dev)
173 rbtn_data->input_dev->name = "DELL Wireless hotkeys";
174 rbtn_data->input_dev->phys = "dellabce/input0";
175 rbtn_data->input_dev->id.bustype = BUS_HOST;
176 rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
177 set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
179 ret = input_register_device(rbtn_data->input_dev);
181 input_free_device(rbtn_data->input_dev);
182 rbtn_data->input_dev = NULL;
189 static void rbtn_input_exit(struct rbtn_data *rbtn_data)
191 input_unregister_device(rbtn_data->input_dev);
192 rbtn_data->input_dev = NULL;
195 static void rbtn_input_event(struct rbtn_data *rbtn_data)
197 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
198 input_sync(rbtn_data->input_dev);
199 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
200 input_sync(rbtn_data->input_dev);
208 static int rbtn_add(struct acpi_device *device);
209 static int rbtn_remove(struct acpi_device *device);
210 static void rbtn_notify(struct acpi_device *device, u32 event);
212 static const struct acpi_device_id rbtn_ids[] = {
217 * This driver can also handle the "DELLABC6" device that
218 * appears on the XPS 13 9350, but that device is disabled by
219 * the DSDT unless booted with acpi_osi="!Windows 2012"
220 * acpi_osi="!Windows 2013".
222 * According to Mario at Dell:
224 * DELLABC6 is a custom interface that was created solely to
225 * have airplane mode support for Windows 7. For Windows 10
226 * the proper interface is to use that which is handled by
227 * intel-hid. A OEM airplane mode driver is not used.
229 * Since the kernel doesn't identify as Windows 7 it would be
230 * incorrect to do attempt to use that interface.
232 * Even if we override _OSI and bind to DELLABC6, we end up with
233 * inconsistent behavior in which userspace can get out of sync
234 * with the rfkill state as it conflicts with events from
237 * The upshot is that it is better to just ignore DELLABC6
244 #ifdef CONFIG_PM_SLEEP
245 static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
247 struct rbtn_data *rbtn_data = context;
249 rbtn_data->suspended = false;
252 static int rbtn_suspend(struct device *dev)
254 struct acpi_device *device = to_acpi_device(dev);
255 struct rbtn_data *rbtn_data = acpi_driver_data(device);
257 rbtn_data->suspended = true;
262 static int rbtn_resume(struct device *dev)
264 struct acpi_device *device = to_acpi_device(dev);
265 struct rbtn_data *rbtn_data = acpi_driver_data(device);
269 * Upon resume, some BIOSes send an ACPI notification thet triggers
270 * an unwanted input event. In order to ignore it, we use a flag
271 * that we set at suspend and clear once we have received the extra
272 * ACPI notification. Since ACPI notifications are delivered
273 * asynchronously to drivers, we clear the flag from the workqueue
274 * used to deliver the notifications. This should be enough
275 * to have the flag cleared only after we received the extra
276 * notification, if any.
278 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
279 rbtn_clear_suspended_flag, rbtn_data);
280 if (ACPI_FAILURE(status))
281 rbtn_clear_suspended_flag(rbtn_data);
287 static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
289 static struct acpi_driver rbtn_driver = {
292 .drv.pm = &rbtn_pm_ops,
295 .remove = rbtn_remove,
296 .notify = rbtn_notify,
298 .owner = THIS_MODULE,
303 * notifier export functions
306 static bool auto_remove_rfkill = true;
308 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
310 static int rbtn_inc_count(struct device *dev, void *data)
312 struct acpi_device *device = to_acpi_device(dev);
313 struct rbtn_data *rbtn_data = device->driver_data;
316 if (rbtn_data->type == RBTN_SLIDER)
322 static int rbtn_switch_dev(struct device *dev, void *data)
324 struct acpi_device *device = to_acpi_device(dev);
325 struct rbtn_data *rbtn_data = device->driver_data;
328 if (rbtn_data->type != RBTN_SLIDER)
332 rbtn_rfkill_init(device);
334 rbtn_rfkill_exit(device);
339 int dell_rbtn_notifier_register(struct notifier_block *nb)
346 ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
348 if (ret || count == 0)
351 first = !rbtn_chain_head.head;
353 ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
357 if (auto_remove_rfkill && first)
358 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
359 (void *)false, rbtn_switch_dev);
363 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
365 int dell_rbtn_notifier_unregister(struct notifier_block *nb)
369 ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
373 if (auto_remove_rfkill && !rbtn_chain_head.head)
374 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
375 (void *)true, rbtn_switch_dev);
379 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
383 * acpi driver functions
386 static int rbtn_add(struct acpi_device *device)
388 struct rbtn_data *rbtn_data;
392 type = rbtn_check(device);
393 if (type == RBTN_UNKNOWN) {
394 dev_info(&device->dev, "Unknown device type\n");
398 ret = rbtn_acquire(device, true);
400 dev_err(&device->dev, "Cannot enable device\n");
404 rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
408 rbtn_data->type = type;
409 device->driver_data = rbtn_data;
411 switch (rbtn_data->type) {
413 ret = rbtn_input_init(rbtn_data);
416 if (auto_remove_rfkill && rbtn_chain_head.head)
419 ret = rbtn_rfkill_init(device);
429 static int rbtn_remove(struct acpi_device *device)
431 struct rbtn_data *rbtn_data = device->driver_data;
433 switch (rbtn_data->type) {
435 rbtn_input_exit(rbtn_data);
438 rbtn_rfkill_exit(device);
444 rbtn_acquire(device, false);
445 device->driver_data = NULL;
450 static void rbtn_notify(struct acpi_device *device, u32 event)
452 struct rbtn_data *rbtn_data = device->driver_data;
455 * Some BIOSes send a notification at resume.
456 * Ignore it to prevent unwanted input events.
458 if (rbtn_data->suspended) {
459 dev_dbg(&device->dev, "ACPI notification ignored\n");
464 dev_info(&device->dev, "Received unknown event (0x%x)\n",
469 switch (rbtn_data->type) {
471 rbtn_input_event(rbtn_data);
474 rbtn_rfkill_event(device);
475 atomic_notifier_call_chain(&rbtn_chain_head, event, device);
487 module_acpi_driver(rbtn_driver);
489 module_param(auto_remove_rfkill, bool, 0444);
491 MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
492 "other modules start receiving events "
493 "from this module and re-add them when "
494 "the last module stops receiving events "
496 MODULE_DEVICE_TABLE(acpi, rbtn_ids);
497 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
498 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
499 MODULE_LICENSE("GPL");