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
[] = {
222 * This driver can also handle the "DELLABC6" device that
223 * appears on the XPS 13 9350, but that device is disabled
224 * by the DSDT unless booted with acpi_osi="!Windows 2012"
225 * acpi_osi="!Windows 2013". Even if we boot that and bind
226 * the driver, we seem to have inconsistent behavior in
227 * which NetworkManager can get out of sync with the rfkill
230 * On the XPS 13 9350 and similar laptops, we're not supposed to
231 * use DELLABC6 at all. Instead, we handle the rfkill button
232 * via the intel-hid driver.
238 static struct acpi_driver rbtn_driver
= {
243 .remove
= rbtn_remove
,
244 .notify
= rbtn_notify
,
246 .owner
= THIS_MODULE
,
251 * notifier export functions
254 static bool auto_remove_rfkill
= true;
256 static ATOMIC_NOTIFIER_HEAD(rbtn_chain_head
);
258 static int rbtn_inc_count(struct device
*dev
, void *data
)
260 struct acpi_device
*device
= to_acpi_device(dev
);
261 struct rbtn_data
*rbtn_data
= device
->driver_data
;
264 if (rbtn_data
->type
== RBTN_SLIDER
)
270 static int rbtn_switch_dev(struct device
*dev
, void *data
)
272 struct acpi_device
*device
= to_acpi_device(dev
);
273 struct rbtn_data
*rbtn_data
= device
->driver_data
;
276 if (rbtn_data
->type
!= RBTN_SLIDER
)
280 rbtn_rfkill_init(device
);
282 rbtn_rfkill_exit(device
);
287 int dell_rbtn_notifier_register(struct notifier_block
*nb
)
294 ret
= driver_for_each_device(&rbtn_driver
.drv
, NULL
, &count
,
296 if (ret
|| count
== 0)
299 first
= !rbtn_chain_head
.head
;
301 ret
= atomic_notifier_chain_register(&rbtn_chain_head
, nb
);
305 if (auto_remove_rfkill
&& first
)
306 ret
= driver_for_each_device(&rbtn_driver
.drv
, NULL
,
307 (void *)false, rbtn_switch_dev
);
311 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_register
);
313 int dell_rbtn_notifier_unregister(struct notifier_block
*nb
)
317 ret
= atomic_notifier_chain_unregister(&rbtn_chain_head
, nb
);
321 if (auto_remove_rfkill
&& !rbtn_chain_head
.head
)
322 ret
= driver_for_each_device(&rbtn_driver
.drv
, NULL
,
323 (void *)true, rbtn_switch_dev
);
327 EXPORT_SYMBOL_GPL(dell_rbtn_notifier_unregister
);
331 * acpi driver functions
334 static int rbtn_add(struct acpi_device
*device
)
336 struct rbtn_data
*rbtn_data
;
340 type
= rbtn_check(device
);
341 if (type
== RBTN_UNKNOWN
) {
342 dev_info(&device
->dev
, "Unknown device type\n");
346 ret
= rbtn_acquire(device
, true);
348 dev_err(&device
->dev
, "Cannot enable device\n");
352 rbtn_data
= devm_kzalloc(&device
->dev
, sizeof(*rbtn_data
), GFP_KERNEL
);
356 rbtn_data
->type
= type
;
357 device
->driver_data
= rbtn_data
;
359 switch (rbtn_data
->type
) {
361 ret
= rbtn_input_init(rbtn_data
);
364 if (auto_remove_rfkill
&& rbtn_chain_head
.head
)
367 ret
= rbtn_rfkill_init(device
);
377 static int rbtn_remove(struct acpi_device
*device
)
379 struct rbtn_data
*rbtn_data
= device
->driver_data
;
381 switch (rbtn_data
->type
) {
383 rbtn_input_exit(rbtn_data
);
386 rbtn_rfkill_exit(device
);
392 rbtn_acquire(device
, false);
393 device
->driver_data
= NULL
;
398 static void rbtn_notify(struct acpi_device
*device
, u32 event
)
400 struct rbtn_data
*rbtn_data
= device
->driver_data
;
403 dev_info(&device
->dev
, "Received unknown event (0x%x)\n",
408 switch (rbtn_data
->type
) {
410 rbtn_input_event(rbtn_data
);
413 rbtn_rfkill_event(device
);
414 atomic_notifier_call_chain(&rbtn_chain_head
, event
, device
);
426 module_acpi_driver(rbtn_driver
);
428 module_param(auto_remove_rfkill
, bool, 0444);
430 MODULE_PARM_DESC(auto_remove_rfkill
, "Automatically remove rfkill devices when "
431 "other modules start receiving events "
432 "from this module and re-add them when "
433 "the last module stops receiving events "
435 MODULE_DEVICE_TABLE(acpi
, rbtn_ids
);
436 MODULE_DESCRIPTION("Dell Airplane Mode Switch driver");
437 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
438 MODULE_LICENSE("GPL");