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