Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 - 2007 Ivo van Doorn |
| 3 | * Copyright (C) 2007 Dmitry Torokhov |
| 4 | * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 8232f1f | 2013-12-06 03:32:14 -0800 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/capability.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/rfkill.h> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 28 | #include <linux/sched.h> |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 30 | #include <linux/device.h> |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 31 | #include <linux/miscdevice.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/poll.h> |
| 34 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 36 | |
| 37 | #include "rfkill.h" |
| 38 | |
| 39 | #define POLL_INTERVAL (5 * HZ) |
| 40 | |
| 41 | #define RFKILL_BLOCK_HW BIT(0) |
| 42 | #define RFKILL_BLOCK_SW BIT(1) |
| 43 | #define RFKILL_BLOCK_SW_PREV BIT(2) |
| 44 | #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\ |
| 45 | RFKILL_BLOCK_SW |\ |
| 46 | RFKILL_BLOCK_SW_PREV) |
| 47 | #define RFKILL_BLOCK_SW_SETCALL BIT(31) |
| 48 | |
| 49 | struct rfkill { |
| 50 | spinlock_t lock; |
| 51 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 52 | enum rfkill_type type; |
| 53 | |
| 54 | unsigned long state; |
| 55 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 56 | u32 idx; |
| 57 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 58 | bool registered; |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 59 | bool persistent; |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 60 | bool polling_paused; |
| 61 | bool suspended; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 62 | |
| 63 | const struct rfkill_ops *ops; |
| 64 | void *data; |
| 65 | |
| 66 | #ifdef CONFIG_RFKILL_LEDS |
| 67 | struct led_trigger led_trigger; |
| 68 | const char *ledtrigname; |
| 69 | #endif |
| 70 | |
| 71 | struct device dev; |
| 72 | struct list_head node; |
| 73 | |
| 74 | struct delayed_work poll_work; |
| 75 | struct work_struct uevent_work; |
| 76 | struct work_struct sync_work; |
Johannes Berg | b7bb110 | 2015-12-10 10:37:51 +0100 | [diff] [blame] | 77 | char name[]; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 78 | }; |
| 79 | #define to_rfkill(d) container_of(d, struct rfkill, dev) |
| 80 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 81 | struct rfkill_int_event { |
| 82 | struct list_head list; |
| 83 | struct rfkill_event ev; |
| 84 | }; |
| 85 | |
| 86 | struct rfkill_data { |
| 87 | struct list_head list; |
| 88 | struct list_head events; |
| 89 | struct mutex mtx; |
| 90 | wait_queue_head_t read_wait; |
| 91 | bool input_handler; |
| 92 | }; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 93 | |
| 94 | |
| 95 | MODULE_AUTHOR("Ivo van Doorn <IvDoorn@gmail.com>"); |
| 96 | MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); |
| 97 | MODULE_DESCRIPTION("RF switch support"); |
| 98 | MODULE_LICENSE("GPL"); |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | * The locking here should be made much smarter, we currently have |
| 103 | * a bit of a stupid situation because drivers might want to register |
| 104 | * the rfkill struct under their own lock, and take this lock during |
| 105 | * rfkill method calls -- which will cause an AB-BA deadlock situation. |
| 106 | * |
| 107 | * To fix that, we need to rework this code here to be mostly lock-free |
| 108 | * and only use the mutex for list manipulations, not to protect the |
| 109 | * various other global variables. Then we can avoid holding the mutex |
| 110 | * around driver operations, and all is happy. |
| 111 | */ |
| 112 | static LIST_HEAD(rfkill_list); /* list of registered rf switches */ |
| 113 | static DEFINE_MUTEX(rfkill_global_mutex); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 114 | static LIST_HEAD(rfkill_fds); /* list of open fds of /dev/rfkill */ |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 115 | |
| 116 | static unsigned int rfkill_default_state = 1; |
| 117 | module_param_named(default_state, rfkill_default_state, uint, 0444); |
| 118 | MODULE_PARM_DESC(default_state, |
| 119 | "Default initial state for all radio types, 0 = radio off"); |
| 120 | |
| 121 | static struct { |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 122 | bool cur, sav; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 123 | } rfkill_global_states[NUM_RFKILL_TYPES]; |
| 124 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 125 | static bool rfkill_epo_lock_active; |
| 126 | |
| 127 | |
| 128 | #ifdef CONFIG_RFKILL_LEDS |
| 129 | static void rfkill_led_trigger_event(struct rfkill *rfkill) |
| 130 | { |
| 131 | struct led_trigger *trigger; |
| 132 | |
| 133 | if (!rfkill->registered) |
| 134 | return; |
| 135 | |
| 136 | trigger = &rfkill->led_trigger; |
| 137 | |
| 138 | if (rfkill->state & RFKILL_BLOCK_ANY) |
| 139 | led_trigger_event(trigger, LED_OFF); |
| 140 | else |
| 141 | led_trigger_event(trigger, LED_FULL); |
| 142 | } |
| 143 | |
| 144 | static void rfkill_led_trigger_activate(struct led_classdev *led) |
| 145 | { |
| 146 | struct rfkill *rfkill; |
| 147 | |
| 148 | rfkill = container_of(led->trigger, struct rfkill, led_trigger); |
| 149 | |
| 150 | rfkill_led_trigger_event(rfkill); |
| 151 | } |
| 152 | |
AceLan Kao | 06d7de8 | 2012-07-26 09:51:08 +0800 | [diff] [blame] | 153 | const char *rfkill_get_led_trigger_name(struct rfkill *rfkill) |
| 154 | { |
| 155 | return rfkill->led_trigger.name; |
| 156 | } |
| 157 | EXPORT_SYMBOL(rfkill_get_led_trigger_name); |
| 158 | |
| 159 | void rfkill_set_led_trigger_name(struct rfkill *rfkill, const char *name) |
| 160 | { |
| 161 | BUG_ON(!rfkill); |
| 162 | |
| 163 | rfkill->ledtrigname = name; |
| 164 | } |
| 165 | EXPORT_SYMBOL(rfkill_set_led_trigger_name); |
| 166 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 167 | static int rfkill_led_trigger_register(struct rfkill *rfkill) |
| 168 | { |
| 169 | rfkill->led_trigger.name = rfkill->ledtrigname |
| 170 | ? : dev_name(&rfkill->dev); |
| 171 | rfkill->led_trigger.activate = rfkill_led_trigger_activate; |
| 172 | return led_trigger_register(&rfkill->led_trigger); |
| 173 | } |
| 174 | |
| 175 | static void rfkill_led_trigger_unregister(struct rfkill *rfkill) |
| 176 | { |
| 177 | led_trigger_unregister(&rfkill->led_trigger); |
| 178 | } |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 179 | |
| 180 | static struct led_trigger rfkill_any_led_trigger; |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 181 | static struct led_trigger rfkill_none_led_trigger; |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 182 | static struct work_struct rfkill_global_led_trigger_work; |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 183 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 184 | static void rfkill_global_led_trigger_worker(struct work_struct *work) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 185 | { |
| 186 | enum led_brightness brightness = LED_OFF; |
| 187 | struct rfkill *rfkill; |
| 188 | |
| 189 | mutex_lock(&rfkill_global_mutex); |
| 190 | list_for_each_entry(rfkill, &rfkill_list, node) { |
| 191 | if (!(rfkill->state & RFKILL_BLOCK_ANY)) { |
| 192 | brightness = LED_FULL; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | mutex_unlock(&rfkill_global_mutex); |
| 197 | |
| 198 | led_trigger_event(&rfkill_any_led_trigger, brightness); |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 199 | led_trigger_event(&rfkill_none_led_trigger, |
| 200 | brightness == LED_OFF ? LED_FULL : LED_OFF); |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 201 | } |
| 202 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 203 | static void rfkill_global_led_trigger_event(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 204 | { |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 205 | schedule_work(&rfkill_global_led_trigger_work); |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 206 | } |
| 207 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 208 | static int rfkill_global_led_trigger_register(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 209 | { |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 210 | int ret; |
| 211 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 212 | INIT_WORK(&rfkill_global_led_trigger_work, |
| 213 | rfkill_global_led_trigger_worker); |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 214 | |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 215 | rfkill_any_led_trigger.name = "rfkill-any"; |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 216 | ret = led_trigger_register(&rfkill_any_led_trigger); |
| 217 | if (ret) |
| 218 | return ret; |
| 219 | |
| 220 | rfkill_none_led_trigger.name = "rfkill-none"; |
| 221 | ret = led_trigger_register(&rfkill_none_led_trigger); |
| 222 | if (ret) |
| 223 | led_trigger_unregister(&rfkill_any_led_trigger); |
| 224 | else |
| 225 | /* Delay activation until all global triggers are registered */ |
| 226 | rfkill_global_led_trigger_event(); |
| 227 | |
| 228 | return ret; |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 229 | } |
| 230 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 231 | static void rfkill_global_led_trigger_unregister(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 232 | { |
João Paulo Rechi Vita | 232aa23 | 2018-05-22 14:29:32 -0700 | [diff] [blame^] | 233 | led_trigger_unregister(&rfkill_none_led_trigger); |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 234 | led_trigger_unregister(&rfkill_any_led_trigger); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 235 | cancel_work_sync(&rfkill_global_led_trigger_work); |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 236 | } |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 237 | #else |
| 238 | static void rfkill_led_trigger_event(struct rfkill *rfkill) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | static inline int rfkill_led_trigger_register(struct rfkill *rfkill) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static inline void rfkill_led_trigger_unregister(struct rfkill *rfkill) |
| 248 | { |
| 249 | } |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 250 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 251 | static void rfkill_global_led_trigger_event(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 252 | { |
| 253 | } |
| 254 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 255 | static int rfkill_global_led_trigger_register(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 260 | static void rfkill_global_led_trigger_unregister(void) |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 261 | { |
| 262 | } |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 263 | #endif /* CONFIG_RFKILL_LEDS */ |
| 264 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 265 | static void rfkill_fill_event(struct rfkill_event *ev, struct rfkill *rfkill, |
| 266 | enum rfkill_operation op) |
| 267 | { |
| 268 | unsigned long flags; |
| 269 | |
| 270 | ev->idx = rfkill->idx; |
| 271 | ev->type = rfkill->type; |
| 272 | ev->op = op; |
| 273 | |
| 274 | spin_lock_irqsave(&rfkill->lock, flags); |
| 275 | ev->hard = !!(rfkill->state & RFKILL_BLOCK_HW); |
| 276 | ev->soft = !!(rfkill->state & (RFKILL_BLOCK_SW | |
| 277 | RFKILL_BLOCK_SW_PREV)); |
| 278 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 279 | } |
| 280 | |
| 281 | static void rfkill_send_events(struct rfkill *rfkill, enum rfkill_operation op) |
| 282 | { |
| 283 | struct rfkill_data *data; |
| 284 | struct rfkill_int_event *ev; |
| 285 | |
| 286 | list_for_each_entry(data, &rfkill_fds, list) { |
| 287 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 288 | if (!ev) |
| 289 | continue; |
| 290 | rfkill_fill_event(&ev->ev, rfkill, op); |
| 291 | mutex_lock(&data->mtx); |
| 292 | list_add_tail(&ev->list, &data->events); |
| 293 | mutex_unlock(&data->mtx); |
| 294 | wake_up_interruptible(&data->read_wait); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static void rfkill_event(struct rfkill *rfkill) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 299 | { |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 300 | if (!rfkill->registered) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 301 | return; |
| 302 | |
| 303 | kobject_uevent(&rfkill->dev.kobj, KOBJ_CHANGE); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 304 | |
| 305 | /* also send event to /dev/rfkill */ |
| 306 | rfkill_send_events(rfkill, RFKILL_OP_CHANGE); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 309 | /** |
| 310 | * rfkill_set_block - wrapper for set_block method |
| 311 | * |
| 312 | * @rfkill: the rfkill struct to use |
| 313 | * @blocked: the new software state |
| 314 | * |
| 315 | * Calls the set_block method (when applicable) and handles notifications |
| 316 | * etc. as well. |
| 317 | */ |
| 318 | static void rfkill_set_block(struct rfkill *rfkill, bool blocked) |
| 319 | { |
| 320 | unsigned long flags; |
Vitaly Wool | eab4834 | 2012-09-06 16:06:52 +0200 | [diff] [blame] | 321 | bool prev, curr; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 322 | int err; |
| 323 | |
Alan Jenkins | 7fa20a7 | 2009-06-16 14:53:24 +0100 | [diff] [blame] | 324 | if (unlikely(rfkill->dev.power.power_state.event & PM_EVENT_SLEEP)) |
| 325 | return; |
| 326 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Some platforms (...!) generate input events which affect the |
| 329 | * _hard_ kill state -- whenever something tries to change the |
| 330 | * current software state query the hardware state too. |
| 331 | */ |
| 332 | if (rfkill->ops->query) |
| 333 | rfkill->ops->query(rfkill, rfkill->data); |
| 334 | |
| 335 | spin_lock_irqsave(&rfkill->lock, flags); |
Vitaly Wool | eab4834 | 2012-09-06 16:06:52 +0200 | [diff] [blame] | 336 | prev = rfkill->state & RFKILL_BLOCK_SW; |
| 337 | |
João Paulo Rechi Vita | f3e7fae | 2016-01-19 10:42:37 -0500 | [diff] [blame] | 338 | if (prev) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 339 | rfkill->state |= RFKILL_BLOCK_SW_PREV; |
| 340 | else |
| 341 | rfkill->state &= ~RFKILL_BLOCK_SW_PREV; |
| 342 | |
| 343 | if (blocked) |
| 344 | rfkill->state |= RFKILL_BLOCK_SW; |
| 345 | else |
| 346 | rfkill->state &= ~RFKILL_BLOCK_SW; |
| 347 | |
| 348 | rfkill->state |= RFKILL_BLOCK_SW_SETCALL; |
| 349 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 350 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 351 | err = rfkill->ops->set_block(rfkill->data, blocked); |
| 352 | |
| 353 | spin_lock_irqsave(&rfkill->lock, flags); |
| 354 | if (err) { |
| 355 | /* |
João Paulo Rechi Vita | 3ff707d | 2016-02-22 11:36:32 -0500 | [diff] [blame] | 356 | * Failed -- reset status to _PREV, which may be different |
| 357 | * from what we have set _PREV to earlier in this function |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 358 | * if rfkill_set_sw_state was invoked. |
| 359 | */ |
| 360 | if (rfkill->state & RFKILL_BLOCK_SW_PREV) |
| 361 | rfkill->state |= RFKILL_BLOCK_SW; |
| 362 | else |
| 363 | rfkill->state &= ~RFKILL_BLOCK_SW; |
| 364 | } |
| 365 | rfkill->state &= ~RFKILL_BLOCK_SW_SETCALL; |
| 366 | rfkill->state &= ~RFKILL_BLOCK_SW_PREV; |
Vitaly Wool | eab4834 | 2012-09-06 16:06:52 +0200 | [diff] [blame] | 367 | curr = rfkill->state & RFKILL_BLOCK_SW; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 368 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 369 | |
| 370 | rfkill_led_trigger_event(rfkill); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 371 | rfkill_global_led_trigger_event(); |
Vitaly Wool | eab4834 | 2012-09-06 16:06:52 +0200 | [diff] [blame] | 372 | |
| 373 | if (prev != curr) |
| 374 | rfkill_event(rfkill); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 375 | } |
| 376 | |
João Paulo Rechi Vita | 9487bd6 | 2016-02-22 11:36:36 -0500 | [diff] [blame] | 377 | static void rfkill_update_global_state(enum rfkill_type type, bool blocked) |
| 378 | { |
| 379 | int i; |
| 380 | |
| 381 | if (type != RFKILL_TYPE_ALL) { |
| 382 | rfkill_global_states[type].cur = blocked; |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | for (i = 0; i < NUM_RFKILL_TYPES; i++) |
| 387 | rfkill_global_states[i].cur = blocked; |
| 388 | } |
| 389 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 390 | #ifdef CONFIG_RFKILL_INPUT |
| 391 | static atomic_t rfkill_input_disabled = ATOMIC_INIT(0); |
| 392 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 393 | /** |
| 394 | * __rfkill_switch_all - Toggle state of all switches of given type |
| 395 | * @type: type of interfaces to be affected |
Fabian Frederick | 2f29fed | 2014-10-07 22:20:23 +0200 | [diff] [blame] | 396 | * @blocked: the new state |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 397 | * |
| 398 | * This function sets the state of all switches of given type, |
João Paulo Rechi Vita | e2a35e8 | 2016-01-19 10:42:39 -0500 | [diff] [blame] | 399 | * unless a specific switch is suspended. |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 400 | * |
| 401 | * Caller must have acquired rfkill_global_mutex. |
| 402 | */ |
| 403 | static void __rfkill_switch_all(const enum rfkill_type type, bool blocked) |
| 404 | { |
| 405 | struct rfkill *rfkill; |
| 406 | |
João Paulo Rechi Vita | 9487bd6 | 2016-02-22 11:36:36 -0500 | [diff] [blame] | 407 | rfkill_update_global_state(type, blocked); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 408 | list_for_each_entry(rfkill, &rfkill_list, node) { |
Alex Hung | 27e49ca | 2012-05-23 14:11:52 +0800 | [diff] [blame] | 409 | if (rfkill->type != type && type != RFKILL_TYPE_ALL) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 410 | continue; |
| 411 | |
| 412 | rfkill_set_block(rfkill, blocked); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * rfkill_switch_all - Toggle state of all switches of given type |
| 418 | * @type: type of interfaces to be affected |
Fabian Frederick | 2f29fed | 2014-10-07 22:20:23 +0200 | [diff] [blame] | 419 | * @blocked: the new state |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 420 | * |
| 421 | * Acquires rfkill_global_mutex and calls __rfkill_switch_all(@type, @state). |
| 422 | * Please refer to __rfkill_switch_all() for details. |
| 423 | * |
| 424 | * Does nothing if the EPO lock is active. |
| 425 | */ |
| 426 | void rfkill_switch_all(enum rfkill_type type, bool blocked) |
| 427 | { |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 428 | if (atomic_read(&rfkill_input_disabled)) |
| 429 | return; |
| 430 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 431 | mutex_lock(&rfkill_global_mutex); |
| 432 | |
| 433 | if (!rfkill_epo_lock_active) |
| 434 | __rfkill_switch_all(type, blocked); |
| 435 | |
| 436 | mutex_unlock(&rfkill_global_mutex); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * rfkill_epo - emergency power off all transmitters |
| 441 | * |
| 442 | * This kicks all non-suspended rfkill devices to RFKILL_STATE_SOFT_BLOCKED, |
| 443 | * ignoring everything in its path but rfkill_global_mutex and rfkill->mutex. |
| 444 | * |
| 445 | * The global state before the EPO is saved and can be restored later |
| 446 | * using rfkill_restore_states(). |
| 447 | */ |
| 448 | void rfkill_epo(void) |
| 449 | { |
| 450 | struct rfkill *rfkill; |
| 451 | int i; |
| 452 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 453 | if (atomic_read(&rfkill_input_disabled)) |
| 454 | return; |
| 455 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 456 | mutex_lock(&rfkill_global_mutex); |
| 457 | |
| 458 | rfkill_epo_lock_active = true; |
| 459 | list_for_each_entry(rfkill, &rfkill_list, node) |
| 460 | rfkill_set_block(rfkill, true); |
| 461 | |
| 462 | for (i = 0; i < NUM_RFKILL_TYPES; i++) { |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 463 | rfkill_global_states[i].sav = rfkill_global_states[i].cur; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 464 | rfkill_global_states[i].cur = true; |
| 465 | } |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 466 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 467 | mutex_unlock(&rfkill_global_mutex); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * rfkill_restore_states - restore global states |
| 472 | * |
| 473 | * Restore (and sync switches to) the global state from the |
| 474 | * states in rfkill_default_states. This can undo the effects of |
| 475 | * a call to rfkill_epo(). |
| 476 | */ |
| 477 | void rfkill_restore_states(void) |
| 478 | { |
| 479 | int i; |
| 480 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 481 | if (atomic_read(&rfkill_input_disabled)) |
| 482 | return; |
| 483 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 484 | mutex_lock(&rfkill_global_mutex); |
| 485 | |
| 486 | rfkill_epo_lock_active = false; |
| 487 | for (i = 0; i < NUM_RFKILL_TYPES; i++) |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 488 | __rfkill_switch_all(i, rfkill_global_states[i].sav); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 489 | mutex_unlock(&rfkill_global_mutex); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * rfkill_remove_epo_lock - unlock state changes |
| 494 | * |
| 495 | * Used by rfkill-input manually unlock state changes, when |
| 496 | * the EPO switch is deactivated. |
| 497 | */ |
| 498 | void rfkill_remove_epo_lock(void) |
| 499 | { |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 500 | if (atomic_read(&rfkill_input_disabled)) |
| 501 | return; |
| 502 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 503 | mutex_lock(&rfkill_global_mutex); |
| 504 | rfkill_epo_lock_active = false; |
| 505 | mutex_unlock(&rfkill_global_mutex); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * rfkill_is_epo_lock_active - returns true EPO is active |
| 510 | * |
| 511 | * Returns 0 (false) if there is NOT an active EPO contidion, |
| 512 | * and 1 (true) if there is an active EPO contition, which |
| 513 | * locks all radios in one of the BLOCKED states. |
| 514 | * |
| 515 | * Can be called in atomic context. |
| 516 | */ |
| 517 | bool rfkill_is_epo_lock_active(void) |
| 518 | { |
| 519 | return rfkill_epo_lock_active; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * rfkill_get_global_sw_state - returns global state for a type |
| 524 | * @type: the type to get the global state of |
| 525 | * |
| 526 | * Returns the current global state for a given wireless |
| 527 | * device type. |
| 528 | */ |
| 529 | bool rfkill_get_global_sw_state(const enum rfkill_type type) |
| 530 | { |
| 531 | return rfkill_global_states[type].cur; |
| 532 | } |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 533 | #endif |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 534 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 535 | bool rfkill_set_hw_state(struct rfkill *rfkill, bool blocked) |
| 536 | { |
João Paulo Rechi Vita | 1926e26 | 2016-01-19 10:42:38 -0500 | [diff] [blame] | 537 | unsigned long flags; |
| 538 | bool ret, prev; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 539 | |
João Paulo Rechi Vita | 1926e26 | 2016-01-19 10:42:38 -0500 | [diff] [blame] | 540 | BUG_ON(!rfkill); |
| 541 | |
| 542 | spin_lock_irqsave(&rfkill->lock, flags); |
| 543 | prev = !!(rfkill->state & RFKILL_BLOCK_HW); |
| 544 | if (blocked) |
| 545 | rfkill->state |= RFKILL_BLOCK_HW; |
| 546 | else |
| 547 | rfkill->state &= ~RFKILL_BLOCK_HW; |
| 548 | ret = !!(rfkill->state & RFKILL_BLOCK_ANY); |
| 549 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 550 | |
| 551 | rfkill_led_trigger_event(rfkill); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 552 | rfkill_global_led_trigger_event(); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 553 | |
Johannes Berg | 74204f8 | 2016-12-13 09:38:25 +0100 | [diff] [blame] | 554 | if (rfkill->registered && prev != blocked) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 555 | schedule_work(&rfkill->uevent_work); |
| 556 | |
| 557 | return ret; |
| 558 | } |
| 559 | EXPORT_SYMBOL(rfkill_set_hw_state); |
| 560 | |
| 561 | static void __rfkill_set_sw_state(struct rfkill *rfkill, bool blocked) |
| 562 | { |
| 563 | u32 bit = RFKILL_BLOCK_SW; |
| 564 | |
| 565 | /* if in a ops->set_block right now, use other bit */ |
| 566 | if (rfkill->state & RFKILL_BLOCK_SW_SETCALL) |
| 567 | bit = RFKILL_BLOCK_SW_PREV; |
| 568 | |
| 569 | if (blocked) |
| 570 | rfkill->state |= bit; |
| 571 | else |
| 572 | rfkill->state &= ~bit; |
| 573 | } |
| 574 | |
| 575 | bool rfkill_set_sw_state(struct rfkill *rfkill, bool blocked) |
| 576 | { |
| 577 | unsigned long flags; |
| 578 | bool prev, hwblock; |
| 579 | |
| 580 | BUG_ON(!rfkill); |
| 581 | |
| 582 | spin_lock_irqsave(&rfkill->lock, flags); |
| 583 | prev = !!(rfkill->state & RFKILL_BLOCK_SW); |
| 584 | __rfkill_set_sw_state(rfkill, blocked); |
| 585 | hwblock = !!(rfkill->state & RFKILL_BLOCK_HW); |
| 586 | blocked = blocked || hwblock; |
| 587 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 588 | |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 589 | if (!rfkill->registered) |
| 590 | return blocked; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 591 | |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 592 | if (prev != blocked && !hwblock) |
| 593 | schedule_work(&rfkill->uevent_work); |
| 594 | |
| 595 | rfkill_led_trigger_event(rfkill); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 596 | rfkill_global_led_trigger_event(); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 597 | |
| 598 | return blocked; |
| 599 | } |
| 600 | EXPORT_SYMBOL(rfkill_set_sw_state); |
| 601 | |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 602 | void rfkill_init_sw_state(struct rfkill *rfkill, bool blocked) |
| 603 | { |
| 604 | unsigned long flags; |
| 605 | |
| 606 | BUG_ON(!rfkill); |
| 607 | BUG_ON(rfkill->registered); |
| 608 | |
| 609 | spin_lock_irqsave(&rfkill->lock, flags); |
| 610 | __rfkill_set_sw_state(rfkill, blocked); |
| 611 | rfkill->persistent = true; |
| 612 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 613 | } |
| 614 | EXPORT_SYMBOL(rfkill_init_sw_state); |
| 615 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 616 | void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw) |
| 617 | { |
| 618 | unsigned long flags; |
| 619 | bool swprev, hwprev; |
| 620 | |
| 621 | BUG_ON(!rfkill); |
| 622 | |
| 623 | spin_lock_irqsave(&rfkill->lock, flags); |
| 624 | |
| 625 | /* |
| 626 | * No need to care about prev/setblock ... this is for uevent only |
| 627 | * and that will get triggered by rfkill_set_block anyway. |
| 628 | */ |
| 629 | swprev = !!(rfkill->state & RFKILL_BLOCK_SW); |
| 630 | hwprev = !!(rfkill->state & RFKILL_BLOCK_HW); |
| 631 | __rfkill_set_sw_state(rfkill, sw); |
Alan Jenkins | 48ab357 | 2009-07-12 17:03:13 +0100 | [diff] [blame] | 632 | if (hw) |
| 633 | rfkill->state |= RFKILL_BLOCK_HW; |
| 634 | else |
| 635 | rfkill->state &= ~RFKILL_BLOCK_HW; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 636 | |
| 637 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 638 | |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 639 | if (!rfkill->registered) { |
| 640 | rfkill->persistent = true; |
| 641 | } else { |
| 642 | if (swprev != sw || hwprev != hw) |
| 643 | schedule_work(&rfkill->uevent_work); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 644 | |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 645 | rfkill_led_trigger_event(rfkill); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 646 | rfkill_global_led_trigger_event(); |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 647 | } |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 648 | } |
| 649 | EXPORT_SYMBOL(rfkill_set_states); |
| 650 | |
Heikki Krogerus | 648b50d | 2016-01-25 12:03:46 +0300 | [diff] [blame] | 651 | static const char * const rfkill_types[] = { |
| 652 | NULL, /* RFKILL_TYPE_ALL */ |
| 653 | "wlan", |
| 654 | "bluetooth", |
| 655 | "ultrawideband", |
| 656 | "wimax", |
| 657 | "wwan", |
| 658 | "gps", |
| 659 | "fm", |
| 660 | "nfc", |
| 661 | }; |
| 662 | |
| 663 | enum rfkill_type rfkill_find_type(const char *name) |
| 664 | { |
| 665 | int i; |
| 666 | |
| 667 | BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES); |
| 668 | |
| 669 | if (!name) |
| 670 | return RFKILL_TYPE_ALL; |
| 671 | |
| 672 | for (i = 1; i < NUM_RFKILL_TYPES; i++) |
| 673 | if (!strcmp(name, rfkill_types[i])) |
| 674 | return i; |
| 675 | return RFKILL_TYPE_ALL; |
| 676 | } |
| 677 | EXPORT_SYMBOL(rfkill_find_type); |
| 678 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 679 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 680 | char *buf) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 681 | { |
| 682 | struct rfkill *rfkill = to_rfkill(dev); |
| 683 | |
| 684 | return sprintf(buf, "%s\n", rfkill->name); |
| 685 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 686 | static DEVICE_ATTR_RO(name); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 687 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 688 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
| 689 | char *buf) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 690 | { |
| 691 | struct rfkill *rfkill = to_rfkill(dev); |
| 692 | |
Heikki Krogerus | 648b50d | 2016-01-25 12:03:46 +0300 | [diff] [blame] | 693 | return sprintf(buf, "%s\n", rfkill_types[rfkill->type]); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 694 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 695 | static DEVICE_ATTR_RO(type); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 696 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 697 | static ssize_t index_show(struct device *dev, struct device_attribute *attr, |
| 698 | char *buf) |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 699 | { |
| 700 | struct rfkill *rfkill = to_rfkill(dev); |
| 701 | |
| 702 | return sprintf(buf, "%d\n", rfkill->idx); |
| 703 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 704 | static DEVICE_ATTR_RO(index); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 705 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 706 | static ssize_t persistent_show(struct device *dev, |
| 707 | struct device_attribute *attr, char *buf) |
Alan Jenkins | 464902e | 2009-06-16 14:54:04 +0100 | [diff] [blame] | 708 | { |
| 709 | struct rfkill *rfkill = to_rfkill(dev); |
| 710 | |
| 711 | return sprintf(buf, "%d\n", rfkill->persistent); |
| 712 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 713 | static DEVICE_ATTR_RO(persistent); |
Alan Jenkins | 464902e | 2009-06-16 14:54:04 +0100 | [diff] [blame] | 714 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 715 | static ssize_t hard_show(struct device *dev, struct device_attribute *attr, |
| 716 | char *buf) |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 717 | { |
| 718 | struct rfkill *rfkill = to_rfkill(dev); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 719 | |
florian@mickler.org | 819bfec | 2010-03-13 13:31:05 +0100 | [diff] [blame] | 720 | return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_HW) ? 1 : 0 ); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 721 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 722 | static DEVICE_ATTR_RO(hard); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 723 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 724 | static ssize_t soft_show(struct device *dev, struct device_attribute *attr, |
| 725 | char *buf) |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 726 | { |
| 727 | struct rfkill *rfkill = to_rfkill(dev); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 728 | |
florian@mickler.org | 819bfec | 2010-03-13 13:31:05 +0100 | [diff] [blame] | 729 | return sprintf(buf, "%d\n", (rfkill->state & RFKILL_BLOCK_SW) ? 1 : 0 ); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 732 | static ssize_t soft_store(struct device *dev, struct device_attribute *attr, |
| 733 | const char *buf, size_t count) |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 734 | { |
| 735 | struct rfkill *rfkill = to_rfkill(dev); |
| 736 | unsigned long state; |
| 737 | int err; |
| 738 | |
| 739 | if (!capable(CAP_NET_ADMIN)) |
| 740 | return -EPERM; |
| 741 | |
Julia Lawall | 1bac92c | 2011-11-06 14:26:49 +0100 | [diff] [blame] | 742 | err = kstrtoul(buf, 0, &state); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 743 | if (err) |
| 744 | return err; |
| 745 | |
| 746 | if (state > 1 ) |
| 747 | return -EINVAL; |
| 748 | |
| 749 | mutex_lock(&rfkill_global_mutex); |
| 750 | rfkill_set_block(rfkill, state); |
| 751 | mutex_unlock(&rfkill_global_mutex); |
| 752 | |
Alan Cox | 6f7c962 | 2012-10-25 15:18:34 +0100 | [diff] [blame] | 753 | return count; |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 754 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 755 | static DEVICE_ATTR_RW(soft); |
florian@mickler.org | 6c26361 | 2010-02-26 12:01:34 +0100 | [diff] [blame] | 756 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 757 | static u8 user_state_from_blocked(unsigned long state) |
| 758 | { |
| 759 | if (state & RFKILL_BLOCK_HW) |
| 760 | return RFKILL_USER_STATE_HARD_BLOCKED; |
| 761 | if (state & RFKILL_BLOCK_SW) |
| 762 | return RFKILL_USER_STATE_SOFT_BLOCKED; |
| 763 | |
| 764 | return RFKILL_USER_STATE_UNBLOCKED; |
| 765 | } |
| 766 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 767 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 768 | char *buf) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 769 | { |
| 770 | struct rfkill *rfkill = to_rfkill(dev); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 771 | |
florian@mickler.org | 819bfec | 2010-03-13 13:31:05 +0100 | [diff] [blame] | 772 | return sprintf(buf, "%d\n", user_state_from_blocked(rfkill->state)); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 775 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 776 | const char *buf, size_t count) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 777 | { |
Johannes Berg | f54c142 | 2009-07-10 21:41:39 +0200 | [diff] [blame] | 778 | struct rfkill *rfkill = to_rfkill(dev); |
| 779 | unsigned long state; |
| 780 | int err; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 781 | |
Johannes Berg | f54c142 | 2009-07-10 21:41:39 +0200 | [diff] [blame] | 782 | if (!capable(CAP_NET_ADMIN)) |
| 783 | return -EPERM; |
| 784 | |
Julia Lawall | 1bac92c | 2011-11-06 14:26:49 +0100 | [diff] [blame] | 785 | err = kstrtoul(buf, 0, &state); |
Johannes Berg | f54c142 | 2009-07-10 21:41:39 +0200 | [diff] [blame] | 786 | if (err) |
| 787 | return err; |
| 788 | |
| 789 | if (state != RFKILL_USER_STATE_SOFT_BLOCKED && |
| 790 | state != RFKILL_USER_STATE_UNBLOCKED) |
| 791 | return -EINVAL; |
| 792 | |
| 793 | mutex_lock(&rfkill_global_mutex); |
| 794 | rfkill_set_block(rfkill, state == RFKILL_USER_STATE_SOFT_BLOCKED); |
| 795 | mutex_unlock(&rfkill_global_mutex); |
| 796 | |
Alan Cox | 6f7c962 | 2012-10-25 15:18:34 +0100 | [diff] [blame] | 797 | return count; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 798 | } |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 799 | static DEVICE_ATTR_RW(state); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 800 | |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 801 | static struct attribute *rfkill_dev_attrs[] = { |
| 802 | &dev_attr_name.attr, |
| 803 | &dev_attr_type.attr, |
| 804 | &dev_attr_index.attr, |
| 805 | &dev_attr_persistent.attr, |
| 806 | &dev_attr_state.attr, |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 807 | &dev_attr_soft.attr, |
| 808 | &dev_attr_hard.attr, |
| 809 | NULL, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 810 | }; |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 811 | ATTRIBUTE_GROUPS(rfkill_dev); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 812 | |
| 813 | static void rfkill_release(struct device *dev) |
| 814 | { |
| 815 | struct rfkill *rfkill = to_rfkill(dev); |
| 816 | |
| 817 | kfree(rfkill); |
| 818 | } |
| 819 | |
| 820 | static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 821 | { |
| 822 | struct rfkill *rfkill = to_rfkill(dev); |
| 823 | unsigned long flags; |
| 824 | u32 state; |
| 825 | int error; |
| 826 | |
| 827 | error = add_uevent_var(env, "RFKILL_NAME=%s", rfkill->name); |
| 828 | if (error) |
| 829 | return error; |
| 830 | error = add_uevent_var(env, "RFKILL_TYPE=%s", |
Heikki Krogerus | 648b50d | 2016-01-25 12:03:46 +0300 | [diff] [blame] | 831 | rfkill_types[rfkill->type]); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 832 | if (error) |
| 833 | return error; |
| 834 | spin_lock_irqsave(&rfkill->lock, flags); |
| 835 | state = rfkill->state; |
| 836 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 837 | error = add_uevent_var(env, "RFKILL_STATE=%d", |
| 838 | user_state_from_blocked(state)); |
| 839 | return error; |
| 840 | } |
| 841 | |
| 842 | void rfkill_pause_polling(struct rfkill *rfkill) |
| 843 | { |
| 844 | BUG_ON(!rfkill); |
| 845 | |
| 846 | if (!rfkill->ops->poll) |
| 847 | return; |
| 848 | |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 849 | rfkill->polling_paused = true; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 850 | cancel_delayed_work_sync(&rfkill->poll_work); |
| 851 | } |
| 852 | EXPORT_SYMBOL(rfkill_pause_polling); |
| 853 | |
| 854 | void rfkill_resume_polling(struct rfkill *rfkill) |
| 855 | { |
| 856 | BUG_ON(!rfkill); |
| 857 | |
| 858 | if (!rfkill->ops->poll) |
| 859 | return; |
| 860 | |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 861 | rfkill->polling_paused = false; |
| 862 | |
| 863 | if (rfkill->suspended) |
| 864 | return; |
| 865 | |
Shaibal Dutta | 67235cb | 2014-01-30 14:43:34 -0800 | [diff] [blame] | 866 | queue_delayed_work(system_power_efficient_wq, |
| 867 | &rfkill->poll_work, 0); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 868 | } |
| 869 | EXPORT_SYMBOL(rfkill_resume_polling); |
| 870 | |
Lars-Peter Clausen | 28f297a | 2015-05-16 14:23:55 +0200 | [diff] [blame] | 871 | #ifdef CONFIG_PM_SLEEP |
| 872 | static int rfkill_suspend(struct device *dev) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 873 | { |
| 874 | struct rfkill *rfkill = to_rfkill(dev); |
| 875 | |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 876 | rfkill->suspended = true; |
| 877 | cancel_delayed_work_sync(&rfkill->poll_work); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 878 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | static int rfkill_resume(struct device *dev) |
| 883 | { |
| 884 | struct rfkill *rfkill = to_rfkill(dev); |
| 885 | bool cur; |
| 886 | |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 887 | rfkill->suspended = false; |
| 888 | |
Alan Jenkins | 06d5caf | 2009-06-16 15:39:51 +0100 | [diff] [blame] | 889 | if (!rfkill->persistent) { |
| 890 | cur = !!(rfkill->state & RFKILL_BLOCK_SW); |
| 891 | rfkill_set_block(rfkill, cur); |
| 892 | } |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 893 | |
Johannes Berg | dd21dfc | 2016-01-20 10:39:23 +0100 | [diff] [blame] | 894 | if (rfkill->ops->poll && !rfkill->polling_paused) |
| 895 | queue_delayed_work(system_power_efficient_wq, |
| 896 | &rfkill->poll_work, 0); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
Lars-Peter Clausen | 28f297a | 2015-05-16 14:23:55 +0200 | [diff] [blame] | 901 | static SIMPLE_DEV_PM_OPS(rfkill_pm_ops, rfkill_suspend, rfkill_resume); |
| 902 | #define RFKILL_PM_OPS (&rfkill_pm_ops) |
| 903 | #else |
| 904 | #define RFKILL_PM_OPS NULL |
| 905 | #endif |
| 906 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 907 | static struct class rfkill_class = { |
| 908 | .name = "rfkill", |
| 909 | .dev_release = rfkill_release, |
Greg Kroah-Hartman | e49df67 | 2013-07-24 15:05:36 -0700 | [diff] [blame] | 910 | .dev_groups = rfkill_dev_groups, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 911 | .dev_uevent = rfkill_dev_uevent, |
Lars-Peter Clausen | 28f297a | 2015-05-16 14:23:55 +0200 | [diff] [blame] | 912 | .pm = RFKILL_PM_OPS, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 913 | }; |
| 914 | |
Johannes Berg | 6081162 | 2009-06-02 13:01:40 +0200 | [diff] [blame] | 915 | bool rfkill_blocked(struct rfkill *rfkill) |
| 916 | { |
| 917 | unsigned long flags; |
| 918 | u32 state; |
| 919 | |
| 920 | spin_lock_irqsave(&rfkill->lock, flags); |
| 921 | state = rfkill->state; |
| 922 | spin_unlock_irqrestore(&rfkill->lock, flags); |
| 923 | |
| 924 | return !!(state & RFKILL_BLOCK_ANY); |
| 925 | } |
| 926 | EXPORT_SYMBOL(rfkill_blocked); |
| 927 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 928 | |
| 929 | struct rfkill * __must_check rfkill_alloc(const char *name, |
| 930 | struct device *parent, |
| 931 | const enum rfkill_type type, |
| 932 | const struct rfkill_ops *ops, |
| 933 | void *ops_data) |
| 934 | { |
| 935 | struct rfkill *rfkill; |
| 936 | struct device *dev; |
| 937 | |
| 938 | if (WARN_ON(!ops)) |
| 939 | return NULL; |
| 940 | |
| 941 | if (WARN_ON(!ops->set_block)) |
| 942 | return NULL; |
| 943 | |
| 944 | if (WARN_ON(!name)) |
| 945 | return NULL; |
| 946 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 947 | if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES)) |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 948 | return NULL; |
| 949 | |
Johannes Berg | b7bb110 | 2015-12-10 10:37:51 +0100 | [diff] [blame] | 950 | rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 951 | if (!rfkill) |
| 952 | return NULL; |
| 953 | |
| 954 | spin_lock_init(&rfkill->lock); |
| 955 | INIT_LIST_HEAD(&rfkill->node); |
| 956 | rfkill->type = type; |
Johannes Berg | b7bb110 | 2015-12-10 10:37:51 +0100 | [diff] [blame] | 957 | strcpy(rfkill->name, name); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 958 | rfkill->ops = ops; |
| 959 | rfkill->data = ops_data; |
| 960 | |
| 961 | dev = &rfkill->dev; |
| 962 | dev->class = &rfkill_class; |
| 963 | dev->parent = parent; |
| 964 | device_initialize(dev); |
| 965 | |
| 966 | return rfkill; |
| 967 | } |
| 968 | EXPORT_SYMBOL(rfkill_alloc); |
| 969 | |
| 970 | static void rfkill_poll(struct work_struct *work) |
| 971 | { |
| 972 | struct rfkill *rfkill; |
| 973 | |
| 974 | rfkill = container_of(work, struct rfkill, poll_work.work); |
| 975 | |
| 976 | /* |
| 977 | * Poll hardware state -- driver will use one of the |
| 978 | * rfkill_set{,_hw,_sw}_state functions and use its |
| 979 | * return value to update the current status. |
| 980 | */ |
| 981 | rfkill->ops->poll(rfkill, rfkill->data); |
| 982 | |
Shaibal Dutta | 67235cb | 2014-01-30 14:43:34 -0800 | [diff] [blame] | 983 | queue_delayed_work(system_power_efficient_wq, |
| 984 | &rfkill->poll_work, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 985 | round_jiffies_relative(POLL_INTERVAL)); |
| 986 | } |
| 987 | |
| 988 | static void rfkill_uevent_work(struct work_struct *work) |
| 989 | { |
| 990 | struct rfkill *rfkill; |
| 991 | |
| 992 | rfkill = container_of(work, struct rfkill, uevent_work); |
| 993 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 994 | mutex_lock(&rfkill_global_mutex); |
| 995 | rfkill_event(rfkill); |
| 996 | mutex_unlock(&rfkill_global_mutex); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void rfkill_sync_work(struct work_struct *work) |
| 1000 | { |
| 1001 | struct rfkill *rfkill; |
| 1002 | bool cur; |
| 1003 | |
| 1004 | rfkill = container_of(work, struct rfkill, sync_work); |
| 1005 | |
| 1006 | mutex_lock(&rfkill_global_mutex); |
| 1007 | cur = rfkill_global_states[rfkill->type].cur; |
| 1008 | rfkill_set_block(rfkill, cur); |
| 1009 | mutex_unlock(&rfkill_global_mutex); |
| 1010 | } |
| 1011 | |
| 1012 | int __must_check rfkill_register(struct rfkill *rfkill) |
| 1013 | { |
| 1014 | static unsigned long rfkill_no; |
| 1015 | struct device *dev = &rfkill->dev; |
| 1016 | int error; |
| 1017 | |
| 1018 | BUG_ON(!rfkill); |
| 1019 | |
| 1020 | mutex_lock(&rfkill_global_mutex); |
| 1021 | |
| 1022 | if (rfkill->registered) { |
| 1023 | error = -EALREADY; |
| 1024 | goto unlock; |
| 1025 | } |
| 1026 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1027 | rfkill->idx = rfkill_no; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1028 | dev_set_name(dev, "rfkill%lu", rfkill_no); |
| 1029 | rfkill_no++; |
| 1030 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1031 | list_add_tail(&rfkill->node, &rfkill_list); |
| 1032 | |
| 1033 | error = device_add(dev); |
| 1034 | if (error) |
| 1035 | goto remove; |
| 1036 | |
| 1037 | error = rfkill_led_trigger_register(rfkill); |
| 1038 | if (error) |
| 1039 | goto devdel; |
| 1040 | |
| 1041 | rfkill->registered = true; |
| 1042 | |
Johannes Berg | 2ec2c68 | 2009-06-03 09:55:29 +0200 | [diff] [blame] | 1043 | INIT_DELAYED_WORK(&rfkill->poll_work, rfkill_poll); |
| 1044 | INIT_WORK(&rfkill->uevent_work, rfkill_uevent_work); |
| 1045 | INIT_WORK(&rfkill->sync_work, rfkill_sync_work); |
| 1046 | |
| 1047 | if (rfkill->ops->poll) |
Shaibal Dutta | 67235cb | 2014-01-30 14:43:34 -0800 | [diff] [blame] | 1048 | queue_delayed_work(system_power_efficient_wq, |
| 1049 | &rfkill->poll_work, |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1050 | round_jiffies_relative(POLL_INTERVAL)); |
Alan Jenkins | b3fa132 | 2009-06-08 13:27:27 +0100 | [diff] [blame] | 1051 | |
| 1052 | if (!rfkill->persistent || rfkill_epo_lock_active) { |
| 1053 | schedule_work(&rfkill->sync_work); |
| 1054 | } else { |
| 1055 | #ifdef CONFIG_RFKILL_INPUT |
| 1056 | bool soft_blocked = !!(rfkill->state & RFKILL_BLOCK_SW); |
| 1057 | |
| 1058 | if (!atomic_read(&rfkill_input_disabled)) |
| 1059 | __rfkill_switch_all(rfkill->type, soft_blocked); |
| 1060 | #endif |
| 1061 | } |
Johannes Berg | 2ec2c68 | 2009-06-03 09:55:29 +0200 | [diff] [blame] | 1062 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 1063 | rfkill_global_led_trigger_event(); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1064 | rfkill_send_events(rfkill, RFKILL_OP_ADD); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1065 | |
| 1066 | mutex_unlock(&rfkill_global_mutex); |
| 1067 | return 0; |
| 1068 | |
| 1069 | devdel: |
| 1070 | device_del(&rfkill->dev); |
| 1071 | remove: |
| 1072 | list_del_init(&rfkill->node); |
| 1073 | unlock: |
| 1074 | mutex_unlock(&rfkill_global_mutex); |
| 1075 | return error; |
| 1076 | } |
| 1077 | EXPORT_SYMBOL(rfkill_register); |
| 1078 | |
| 1079 | void rfkill_unregister(struct rfkill *rfkill) |
| 1080 | { |
| 1081 | BUG_ON(!rfkill); |
| 1082 | |
| 1083 | if (rfkill->ops->poll) |
| 1084 | cancel_delayed_work_sync(&rfkill->poll_work); |
| 1085 | |
| 1086 | cancel_work_sync(&rfkill->uevent_work); |
| 1087 | cancel_work_sync(&rfkill->sync_work); |
| 1088 | |
| 1089 | rfkill->registered = false; |
| 1090 | |
| 1091 | device_del(&rfkill->dev); |
| 1092 | |
| 1093 | mutex_lock(&rfkill_global_mutex); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1094 | rfkill_send_events(rfkill, RFKILL_OP_DEL); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1095 | list_del_init(&rfkill->node); |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 1096 | rfkill_global_led_trigger_event(); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1097 | mutex_unlock(&rfkill_global_mutex); |
| 1098 | |
| 1099 | rfkill_led_trigger_unregister(rfkill); |
| 1100 | } |
| 1101 | EXPORT_SYMBOL(rfkill_unregister); |
| 1102 | |
| 1103 | void rfkill_destroy(struct rfkill *rfkill) |
| 1104 | { |
| 1105 | if (rfkill) |
| 1106 | put_device(&rfkill->dev); |
| 1107 | } |
| 1108 | EXPORT_SYMBOL(rfkill_destroy); |
| 1109 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1110 | static int rfkill_fop_open(struct inode *inode, struct file *file) |
| 1111 | { |
| 1112 | struct rfkill_data *data; |
| 1113 | struct rfkill *rfkill; |
| 1114 | struct rfkill_int_event *ev, *tmp; |
| 1115 | |
| 1116 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1117 | if (!data) |
| 1118 | return -ENOMEM; |
| 1119 | |
| 1120 | INIT_LIST_HEAD(&data->events); |
| 1121 | mutex_init(&data->mtx); |
| 1122 | init_waitqueue_head(&data->read_wait); |
| 1123 | |
| 1124 | mutex_lock(&rfkill_global_mutex); |
| 1125 | mutex_lock(&data->mtx); |
| 1126 | /* |
| 1127 | * start getting events from elsewhere but hold mtx to get |
| 1128 | * startup events added first |
| 1129 | */ |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1130 | |
| 1131 | list_for_each_entry(rfkill, &rfkill_list, node) { |
| 1132 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 1133 | if (!ev) |
| 1134 | goto free; |
| 1135 | rfkill_fill_event(&ev->ev, rfkill, RFKILL_OP_ADD); |
| 1136 | list_add_tail(&ev->list, &data->events); |
| 1137 | } |
Julia Lawall | bd2281b | 2011-05-13 15:52:10 +0200 | [diff] [blame] | 1138 | list_add(&data->list, &rfkill_fds); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1139 | mutex_unlock(&data->mtx); |
| 1140 | mutex_unlock(&rfkill_global_mutex); |
| 1141 | |
| 1142 | file->private_data = data; |
| 1143 | |
| 1144 | return nonseekable_open(inode, file); |
| 1145 | |
| 1146 | free: |
| 1147 | mutex_unlock(&data->mtx); |
| 1148 | mutex_unlock(&rfkill_global_mutex); |
| 1149 | mutex_destroy(&data->mtx); |
| 1150 | list_for_each_entry_safe(ev, tmp, &data->events, list) |
| 1151 | kfree(ev); |
| 1152 | kfree(data); |
| 1153 | return -ENOMEM; |
| 1154 | } |
| 1155 | |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 1156 | static __poll_t rfkill_fop_poll(struct file *file, poll_table *wait) |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1157 | { |
| 1158 | struct rfkill_data *data = file->private_data; |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1159 | __poll_t res = EPOLLOUT | EPOLLWRNORM; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1160 | |
| 1161 | poll_wait(file, &data->read_wait, wait); |
| 1162 | |
| 1163 | mutex_lock(&data->mtx); |
| 1164 | if (!list_empty(&data->events)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1165 | res = EPOLLIN | EPOLLRDNORM; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1166 | mutex_unlock(&data->mtx); |
| 1167 | |
| 1168 | return res; |
| 1169 | } |
| 1170 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1171 | static ssize_t rfkill_fop_read(struct file *file, char __user *buf, |
| 1172 | size_t count, loff_t *pos) |
| 1173 | { |
| 1174 | struct rfkill_data *data = file->private_data; |
| 1175 | struct rfkill_int_event *ev; |
| 1176 | unsigned long sz; |
| 1177 | int ret; |
| 1178 | |
| 1179 | mutex_lock(&data->mtx); |
| 1180 | |
| 1181 | while (list_empty(&data->events)) { |
| 1182 | if (file->f_flags & O_NONBLOCK) { |
| 1183 | ret = -EAGAIN; |
| 1184 | goto out; |
| 1185 | } |
| 1186 | mutex_unlock(&data->mtx); |
Johannes Berg | 6736fde | 2016-01-26 11:29:03 +0100 | [diff] [blame] | 1187 | /* since we re-check and it just compares pointers, |
| 1188 | * using !list_empty() without locking isn't a problem |
| 1189 | */ |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1190 | ret = wait_event_interruptible(data->read_wait, |
Johannes Berg | 6736fde | 2016-01-26 11:29:03 +0100 | [diff] [blame] | 1191 | !list_empty(&data->events)); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1192 | mutex_lock(&data->mtx); |
| 1193 | |
| 1194 | if (ret) |
| 1195 | goto out; |
| 1196 | } |
| 1197 | |
| 1198 | ev = list_first_entry(&data->events, struct rfkill_int_event, |
| 1199 | list); |
| 1200 | |
| 1201 | sz = min_t(unsigned long, sizeof(ev->ev), count); |
| 1202 | ret = sz; |
| 1203 | if (copy_to_user(buf, &ev->ev, sz)) |
| 1204 | ret = -EFAULT; |
| 1205 | |
| 1206 | list_del(&ev->list); |
| 1207 | kfree(ev); |
| 1208 | out: |
| 1209 | mutex_unlock(&data->mtx); |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | static ssize_t rfkill_fop_write(struct file *file, const char __user *buf, |
| 1214 | size_t count, loff_t *pos) |
| 1215 | { |
| 1216 | struct rfkill *rfkill; |
| 1217 | struct rfkill_event ev; |
João Paulo Rechi Vita | 1948b2a | 2016-02-22 11:36:39 -0500 | [diff] [blame] | 1218 | int ret; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1219 | |
| 1220 | /* we don't need the 'hard' variable but accept it */ |
Johannes Berg | 1be491f | 2009-07-05 14:51:06 +0200 | [diff] [blame] | 1221 | if (count < RFKILL_EVENT_SIZE_V1 - 1) |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1222 | return -EINVAL; |
| 1223 | |
Johannes Berg | 1be491f | 2009-07-05 14:51:06 +0200 | [diff] [blame] | 1224 | /* |
| 1225 | * Copy as much data as we can accept into our 'ev' buffer, |
| 1226 | * but tell userspace how much we've copied so it can determine |
| 1227 | * our API version even in a write() call, if it cares. |
| 1228 | */ |
| 1229 | count = min(count, sizeof(ev)); |
| 1230 | if (copy_from_user(&ev, buf, count)) |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1231 | return -EFAULT; |
| 1232 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1233 | if (ev.type >= NUM_RFKILL_TYPES) |
| 1234 | return -EINVAL; |
| 1235 | |
| 1236 | mutex_lock(&rfkill_global_mutex); |
| 1237 | |
João Paulo Rechi Vita | 1948b2a | 2016-02-22 11:36:39 -0500 | [diff] [blame] | 1238 | switch (ev.op) { |
| 1239 | case RFKILL_OP_CHANGE_ALL: |
João Paulo Rechi Vita | 9487bd6 | 2016-02-22 11:36:36 -0500 | [diff] [blame] | 1240 | rfkill_update_global_state(ev.type, ev.soft); |
João Paulo Rechi Vita | 1948b2a | 2016-02-22 11:36:39 -0500 | [diff] [blame] | 1241 | list_for_each_entry(rfkill, &rfkill_list, node) |
| 1242 | if (rfkill->type == ev.type || |
| 1243 | ev.type == RFKILL_TYPE_ALL) |
| 1244 | rfkill_set_block(rfkill, ev.soft); |
| 1245 | ret = 0; |
| 1246 | break; |
| 1247 | case RFKILL_OP_CHANGE: |
| 1248 | list_for_each_entry(rfkill, &rfkill_list, node) |
| 1249 | if (rfkill->idx == ev.idx && |
| 1250 | (rfkill->type == ev.type || |
| 1251 | ev.type == RFKILL_TYPE_ALL)) |
| 1252 | rfkill_set_block(rfkill, ev.soft); |
| 1253 | ret = 0; |
| 1254 | break; |
| 1255 | default: |
| 1256 | ret = -EINVAL; |
| 1257 | break; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1258 | } |
João Paulo Rechi Vita | 1948b2a | 2016-02-22 11:36:39 -0500 | [diff] [blame] | 1259 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1260 | mutex_unlock(&rfkill_global_mutex); |
| 1261 | |
João Paulo Rechi Vita | 1948b2a | 2016-02-22 11:36:39 -0500 | [diff] [blame] | 1262 | return ret ?: count; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static int rfkill_fop_release(struct inode *inode, struct file *file) |
| 1266 | { |
| 1267 | struct rfkill_data *data = file->private_data; |
| 1268 | struct rfkill_int_event *ev, *tmp; |
| 1269 | |
| 1270 | mutex_lock(&rfkill_global_mutex); |
| 1271 | list_del(&data->list); |
| 1272 | mutex_unlock(&rfkill_global_mutex); |
| 1273 | |
| 1274 | mutex_destroy(&data->mtx); |
| 1275 | list_for_each_entry_safe(ev, tmp, &data->events, list) |
| 1276 | kfree(ev); |
| 1277 | |
| 1278 | #ifdef CONFIG_RFKILL_INPUT |
| 1279 | if (data->input_handler) |
Johannes Berg | 207ee16 | 2009-06-07 12:26:52 +0200 | [diff] [blame] | 1280 | if (atomic_dec_return(&rfkill_input_disabled) == 0) |
| 1281 | printk(KERN_DEBUG "rfkill: input handler enabled\n"); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1282 | #endif |
| 1283 | |
| 1284 | kfree(data); |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | #ifdef CONFIG_RFKILL_INPUT |
| 1290 | static long rfkill_fop_ioctl(struct file *file, unsigned int cmd, |
| 1291 | unsigned long arg) |
| 1292 | { |
| 1293 | struct rfkill_data *data = file->private_data; |
| 1294 | |
| 1295 | if (_IOC_TYPE(cmd) != RFKILL_IOC_MAGIC) |
| 1296 | return -ENOSYS; |
| 1297 | |
| 1298 | if (_IOC_NR(cmd) != RFKILL_IOC_NOINPUT) |
| 1299 | return -ENOSYS; |
| 1300 | |
| 1301 | mutex_lock(&data->mtx); |
| 1302 | |
| 1303 | if (!data->input_handler) { |
Johannes Berg | 207ee16 | 2009-06-07 12:26:52 +0200 | [diff] [blame] | 1304 | if (atomic_inc_return(&rfkill_input_disabled) == 1) |
| 1305 | printk(KERN_DEBUG "rfkill: input handler disabled\n"); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1306 | data->input_handler = true; |
| 1307 | } |
| 1308 | |
| 1309 | mutex_unlock(&data->mtx); |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | #endif |
| 1314 | |
| 1315 | static const struct file_operations rfkill_fops = { |
Johannes Berg | 45ba564 | 2009-11-23 11:27:30 +0100 | [diff] [blame] | 1316 | .owner = THIS_MODULE, |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1317 | .open = rfkill_fop_open, |
| 1318 | .read = rfkill_fop_read, |
| 1319 | .write = rfkill_fop_write, |
| 1320 | .poll = rfkill_fop_poll, |
| 1321 | .release = rfkill_fop_release, |
| 1322 | #ifdef CONFIG_RFKILL_INPUT |
| 1323 | .unlocked_ioctl = rfkill_fop_ioctl, |
| 1324 | .compat_ioctl = rfkill_fop_ioctl, |
| 1325 | #endif |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1326 | .llseek = no_llseek, |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1327 | }; |
| 1328 | |
| 1329 | static struct miscdevice rfkill_miscdev = { |
| 1330 | .name = "rfkill", |
| 1331 | .fops = &rfkill_fops, |
| 1332 | .minor = MISC_DYNAMIC_MINOR, |
| 1333 | }; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1334 | |
| 1335 | static int __init rfkill_init(void) |
| 1336 | { |
| 1337 | int error; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1338 | |
João Paulo Rechi Vita | 9487bd6 | 2016-02-22 11:36:36 -0500 | [diff] [blame] | 1339 | rfkill_update_global_state(RFKILL_TYPE_ALL, !rfkill_default_state); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1340 | |
| 1341 | error = class_register(&rfkill_class); |
| 1342 | if (error) |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1343 | goto error_class; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1344 | |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1345 | error = misc_register(&rfkill_miscdev); |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1346 | if (error) |
| 1347 | goto error_misc; |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1348 | |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 1349 | error = rfkill_global_led_trigger_register(); |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 1350 | if (error) |
| 1351 | goto error_led_trigger; |
| 1352 | |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1353 | #ifdef CONFIG_RFKILL_INPUT |
| 1354 | error = rfkill_handler_init(); |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1355 | if (error) |
| 1356 | goto error_input; |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1357 | #endif |
| 1358 | |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1359 | return 0; |
| 1360 | |
Arnd Bergmann | f6b4122 | 2016-12-16 09:44:08 +0100 | [diff] [blame] | 1361 | #ifdef CONFIG_RFKILL_INPUT |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1362 | error_input: |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 1363 | rfkill_global_led_trigger_unregister(); |
Johannes Berg | 7b85498 | 2016-12-19 09:51:11 +0100 | [diff] [blame] | 1364 | #endif |
Michał Kępień | 9b8e34e | 2017-01-06 07:07:47 +0100 | [diff] [blame] | 1365 | error_led_trigger: |
| 1366 | misc_deregister(&rfkill_miscdev); |
Michał Kępień | 6124c53e | 2016-12-08 08:30:51 +0100 | [diff] [blame] | 1367 | error_misc: |
| 1368 | class_unregister(&rfkill_class); |
| 1369 | error_class: |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1370 | return error; |
| 1371 | } |
| 1372 | subsys_initcall(rfkill_init); |
| 1373 | |
| 1374 | static void __exit rfkill_exit(void) |
| 1375 | { |
| 1376 | #ifdef CONFIG_RFKILL_INPUT |
| 1377 | rfkill_handler_exit(); |
| 1378 | #endif |
João Paulo Rechi Vita | d874cd7 | 2018-05-22 14:29:31 -0700 | [diff] [blame] | 1379 | rfkill_global_led_trigger_unregister(); |
Johannes Berg | c64fb01 | 2009-06-02 13:01:38 +0200 | [diff] [blame] | 1380 | misc_deregister(&rfkill_miscdev); |
Johannes Berg | 19d337d | 2009-06-02 13:01:37 +0200 | [diff] [blame] | 1381 | class_unregister(&rfkill_class); |
| 1382 | } |
| 1383 | module_exit(rfkill_exit); |