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;
39 static enum rbtn_type rbtn_check(struct acpi_device *device)
41 unsigned long long output;
44 status = acpi_evaluate_integer(device->handle, "CRBT", NULL, &output);
45 if (ACPI_FAILURE(status))
60 static int rbtn_get(struct acpi_device *device)
62 unsigned long long output;
65 status = acpi_evaluate_integer(device->handle, "GRBT", NULL, &output);
66 if (ACPI_FAILURE(status))
72 static int rbtn_acquire(struct acpi_device *device, bool enable)
74 struct acpi_object_list input;
75 union acpi_object param;
78 param.type = ACPI_TYPE_INTEGER;
79 param.integer.value = enable;
81 input.pointer = ¶m;
83 status = acpi_evaluate_object(device->handle, "ARBT", &input, NULL);
84 if (ACPI_FAILURE(status))
95 static void rbtn_rfkill_query(struct rfkill *rfkill, void *data)
97 struct acpi_device *device = data;
100 state = rbtn_get(device);
104 rfkill_set_states(rfkill, state, state);
107 static int rbtn_rfkill_set_block(void *data, bool blocked)
109 /* NOTE: setting soft rfkill state is not supported */
113 static struct rfkill_ops rbtn_ops = {
114 .query = rbtn_rfkill_query,
115 .set_block = rbtn_rfkill_set_block,
118 static int rbtn_rfkill_init(struct acpi_device *device)
120 struct rbtn_data *rbtn_data = device->driver_data;
123 if (rbtn_data->rfkill)
127 * NOTE: rbtn controls all radio devices, not only WLAN
128 * but rfkill interface does not support "ANY" type
129 * so "WLAN" type is used
131 rbtn_data->rfkill = rfkill_alloc("dell-rbtn", &device->dev,
132 RFKILL_TYPE_WLAN, &rbtn_ops, device);
133 if (!rbtn_data->rfkill)
136 ret = rfkill_register(rbtn_data->rfkill);
138 rfkill_destroy(rbtn_data->rfkill);
139 rbtn_data->rfkill = NULL;
146 static void rbtn_rfkill_exit(struct acpi_device *device)
148 struct rbtn_data *rbtn_data = device->driver_data;
150 if (!rbtn_data->rfkill)
153 rfkill_unregister(rbtn_data->rfkill);
154 rfkill_destroy(rbtn_data->rfkill);
155 rbtn_data->rfkill = NULL;
158 static void rbtn_rfkill_event(struct acpi_device *device)
160 struct rbtn_data *rbtn_data = device->driver_data;
162 if (rbtn_data->rfkill)
163 rbtn_rfkill_query(rbtn_data->rfkill, device);
171 static int rbtn_input_init(struct rbtn_data *rbtn_data)
175 rbtn_data->input_dev = input_allocate_device();
176 if (!rbtn_data->input_dev)
179 rbtn_data->input_dev->name = "DELL Wireless hotkeys";
180 rbtn_data->input_dev->phys = "dellabce/input0";
181 rbtn_data->input_dev->id.bustype = BUS_HOST;
182 rbtn_data->input_dev->evbit[0] = BIT(EV_KEY);
183 set_bit(KEY_RFKILL, rbtn_data->input_dev->keybit);
185 ret = input_register_device(rbtn_data->input_dev);
187 input_free_device(rbtn_data->input_dev);
188 rbtn_data->input_dev = NULL;
195 static void rbtn_input_exit(struct rbtn_data *rbtn_data)
197 input_unregister_device(rbtn_data->input_dev);
198 rbtn_data->input_dev = NULL;
201 static void rbtn_input_event(struct rbtn_data *rbtn_data)
203 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 1);
204 input_sync(rbtn_data->input_dev);
205 input_report_key(rbtn_data->input_dev, KEY_RFKILL, 0);
206 input_sync(rbtn_data->input_dev);
214 static int rbtn_add(struct acpi_device *device);
215 static int rbtn_remove(struct acpi_device *device);
216 static void rbtn_notify(struct acpi_device *device, u32 event);
218 static const struct acpi_device_id rbtn_ids[] = {
224 #ifdef CONFIG_PM_SLEEP
225 static void ACPI_SYSTEM_XFACE rbtn_clear_suspended_flag(void *context)
227 struct rbtn_data *rbtn_data = context;
229 rbtn_data->suspended = false;
232 static int rbtn_suspend(struct device *dev)
234 struct acpi_device *device = to_acpi_device(dev);
235 struct rbtn_data *rbtn_data = acpi_driver_data(device);
237 rbtn_data->suspended = true;
242 static int rbtn_resume(struct device *dev)
244 struct acpi_device *device = to_acpi_device(dev);
245 struct rbtn_data *rbtn_data = acpi_driver_data(device);
249 * Upon resume, some BIOSes send an ACPI notification thet triggers
250 * an unwanted input event. In order to ignore it, we use a flag
251 * that we set at suspend and clear once we have received the extra
252 * ACPI notification. Since ACPI notifications are delivered
253 * asynchronously to drivers, we clear the flag from the workqueue
254 * used to deliver the notifications. This should be enough
255 * to have the flag cleared only after we received the extra
256 * notification, if any.
258 status = acpi_os_execute(OSL_NOTIFY_HANDLER,
259 rbtn_clear_suspended_flag, rbtn_data);
260 if (ACPI_FAILURE(status))
261 rbtn_clear_suspended_flag(rbtn_data);
267 static SIMPLE_DEV_PM_OPS(rbtn_pm_ops, rbtn_suspend, rbtn_resume);
269 static struct acpi_driver rbtn_driver = {
272 .drv.pm = &rbtn_pm_ops,
275 .remove = rbtn_remove,
276 .notify = rbtn_notify,
278 .owner = THIS_MODULE,
283 * notifier export functions
286 static bool auto_remove_rfkill = true;
288 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head);
290 static int rbtn_inc_count(struct device *dev, void *data)
292 struct acpi_device *device = to_acpi_device(dev);
293 struct rbtn_data *rbtn_data = device->driver_data;
296 if (rbtn_data->type == RBTN_SLIDER)
302 static int rbtn_switch_dev(struct device *dev, void *data)
304 struct acpi_device *device = to_acpi_device(dev);
305 struct rbtn_data *rbtn_data = device->driver_data;
308 if (rbtn_data->type != RBTN_SLIDER)
312 rbtn_rfkill_init(device);
314 rbtn_rfkill_exit(device);
319 int dell_rbtn_notifier_register(struct notifier_block *nb)
326 ret = driver_for_each_device(&rbtn_driver.drv, NULL, &count,
328 if (ret || count == 0)
331 first = !rbtn_chain_head.head;
333 ret = atomic_notifier_chain_register(&rbtn_chain_head, nb);
337 if (auto_remove_rfkill && first)
338 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
339 (void *)false, rbtn_switch_dev);
343 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register);
345 int dell_rbtn_notifier_unregister(struct notifier_block *nb)
349 ret = atomic_notifier_chain_unregister(&rbtn_chain_head, nb);
353 if (auto_remove_rfkill && !rbtn_chain_head.head)
354 ret = driver_for_each_device(&rbtn_driver.drv, NULL,
355 (void *)true, rbtn_switch_dev);
359 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister);
363 * acpi driver functions
366 static int rbtn_add(struct acpi_device *device)
368 struct rbtn_data *rbtn_data;
372 type = rbtn_check(device);
373 if (type == RBTN_UNKNOWN) {
374 dev_info(&device->dev, "Unknown device type\n");
378 ret = rbtn_acquire(device, true);
380 dev_err(&device->dev, "Cannot enable device\n");
384 rbtn_data = devm_kzalloc(&device->dev, sizeof(*rbtn_data), GFP_KERNEL);
388 rbtn_data->type = type;
389 device->driver_data = rbtn_data;
391 switch (rbtn_data->type) {
393 ret = rbtn_input_init(rbtn_data);
396 if (auto_remove_rfkill && rbtn_chain_head.head)
399 ret = rbtn_rfkill_init(device);
409 static int rbtn_remove(struct acpi_device *device)
411 struct rbtn_data *rbtn_data = device->driver_data;
413 switch (rbtn_data->type) {
415 rbtn_input_exit(rbtn_data);
418 rbtn_rfkill_exit(device);
424 rbtn_acquire(device, false);
425 device->driver_data = NULL;
430 static void rbtn_notify(struct acpi_device *device, u32 event)
432 struct rbtn_data *rbtn_data = device->driver_data;
435 * Some BIOSes send a notification at resume.
436 * Ignore it to prevent unwanted input events.
438 if (rbtn_data->suspended) {
439 dev_dbg(&device->dev, "ACPI notification ignored\n");
444 dev_info(&device->dev, "Received unknown event (0x%x)\n",
449 switch (rbtn_data->type) {
451 rbtn_input_event(rbtn_data);
454 rbtn_rfkill_event(device);
455 atomic_notifier_call_chain(&rbtn_chain_head, event, device);
467 module_acpi_driver(rbtn_driver);
469 module_param(auto_remove_rfkill, bool, 0444);
471 MODULE_PARM_DESC(auto_remove_rfkill, "Automatically remove rfkill devices when "
472 "other modules start receiving events "
473 "from this module and re-add them when "
474 "the last module stops receiving events "
476 MODULE_DEVICE_TABLE(acpi, rbtn_ids);
477 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
478 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
479 MODULE_LICENSE("GPL");