Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Input layer to RF Kill interface connector
0004  *
0005  * Copyright (c) 2007 Dmitry Torokhov
0006  * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
0007  *
0008  * If you ever run into a situation in which you have a SW_ type rfkill
0009  * input device, then you can revive code that was removed in the patch
0010  * "rfkill-input: remove unused code".
0011  */
0012 
0013 #include <linux/input.h>
0014 #include <linux/slab.h>
0015 #include <linux/moduleparam.h>
0016 #include <linux/workqueue.h>
0017 #include <linux/init.h>
0018 #include <linux/rfkill.h>
0019 #include <linux/sched.h>
0020 
0021 #include "rfkill.h"
0022 
0023 enum rfkill_input_master_mode {
0024     RFKILL_INPUT_MASTER_UNLOCK = 0,
0025     RFKILL_INPUT_MASTER_RESTORE = 1,
0026     RFKILL_INPUT_MASTER_UNBLOCKALL = 2,
0027     NUM_RFKILL_INPUT_MASTER_MODES
0028 };
0029 
0030 /* Delay (in ms) between consecutive switch ops */
0031 #define RFKILL_OPS_DELAY 200
0032 
0033 static enum rfkill_input_master_mode rfkill_master_switch_mode =
0034                     RFKILL_INPUT_MASTER_UNBLOCKALL;
0035 module_param_named(master_switch_mode, rfkill_master_switch_mode, uint, 0);
0036 MODULE_PARM_DESC(master_switch_mode,
0037     "SW_RFKILL_ALL ON should: 0=do nothing (only unlock); 1=restore; 2=unblock all");
0038 
0039 static DEFINE_SPINLOCK(rfkill_op_lock);
0040 static bool rfkill_op_pending;
0041 static unsigned long rfkill_sw_pending[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
0042 static unsigned long rfkill_sw_state[BITS_TO_LONGS(NUM_RFKILL_TYPES)];
0043 
0044 enum rfkill_sched_op {
0045     RFKILL_GLOBAL_OP_EPO = 0,
0046     RFKILL_GLOBAL_OP_RESTORE,
0047     RFKILL_GLOBAL_OP_UNLOCK,
0048     RFKILL_GLOBAL_OP_UNBLOCK,
0049 };
0050 
0051 static enum rfkill_sched_op rfkill_master_switch_op;
0052 static enum rfkill_sched_op rfkill_op;
0053 
0054 static void __rfkill_handle_global_op(enum rfkill_sched_op op)
0055 {
0056     unsigned int i;
0057 
0058     switch (op) {
0059     case RFKILL_GLOBAL_OP_EPO:
0060         rfkill_epo();
0061         break;
0062     case RFKILL_GLOBAL_OP_RESTORE:
0063         rfkill_restore_states();
0064         break;
0065     case RFKILL_GLOBAL_OP_UNLOCK:
0066         rfkill_remove_epo_lock();
0067         break;
0068     case RFKILL_GLOBAL_OP_UNBLOCK:
0069         rfkill_remove_epo_lock();
0070         for (i = 0; i < NUM_RFKILL_TYPES; i++)
0071             rfkill_switch_all(i, false);
0072         break;
0073     default:
0074         /* memory corruption or bug, fail safely */
0075         rfkill_epo();
0076         WARN(1, "Unknown requested operation %d! "
0077             "rfkill Emergency Power Off activated\n",
0078             op);
0079     }
0080 }
0081 
0082 static void __rfkill_handle_normal_op(const enum rfkill_type type,
0083                       const bool complement)
0084 {
0085     bool blocked;
0086 
0087     blocked = rfkill_get_global_sw_state(type);
0088     if (complement)
0089         blocked = !blocked;
0090 
0091     rfkill_switch_all(type, blocked);
0092 }
0093 
0094 static void rfkill_op_handler(struct work_struct *work)
0095 {
0096     unsigned int i;
0097     bool c;
0098 
0099     spin_lock_irq(&rfkill_op_lock);
0100     do {
0101         if (rfkill_op_pending) {
0102             enum rfkill_sched_op op = rfkill_op;
0103             rfkill_op_pending = false;
0104             memset(rfkill_sw_pending, 0,
0105                 sizeof(rfkill_sw_pending));
0106             spin_unlock_irq(&rfkill_op_lock);
0107 
0108             __rfkill_handle_global_op(op);
0109 
0110             spin_lock_irq(&rfkill_op_lock);
0111 
0112             /*
0113              * handle global ops first -- during unlocked period
0114              * we might have gotten a new global op.
0115              */
0116             if (rfkill_op_pending)
0117                 continue;
0118         }
0119 
0120         if (rfkill_is_epo_lock_active())
0121             continue;
0122 
0123         for (i = 0; i < NUM_RFKILL_TYPES; i++) {
0124             if (__test_and_clear_bit(i, rfkill_sw_pending)) {
0125                 c = __test_and_clear_bit(i, rfkill_sw_state);
0126                 spin_unlock_irq(&rfkill_op_lock);
0127 
0128                 __rfkill_handle_normal_op(i, c);
0129 
0130                 spin_lock_irq(&rfkill_op_lock);
0131             }
0132         }
0133     } while (rfkill_op_pending);
0134     spin_unlock_irq(&rfkill_op_lock);
0135 }
0136 
0137 static DECLARE_DELAYED_WORK(rfkill_op_work, rfkill_op_handler);
0138 static unsigned long rfkill_last_scheduled;
0139 
0140 static unsigned long rfkill_ratelimit(const unsigned long last)
0141 {
0142     const unsigned long delay = msecs_to_jiffies(RFKILL_OPS_DELAY);
0143     return time_after(jiffies, last + delay) ? 0 : delay;
0144 }
0145 
0146 static void rfkill_schedule_ratelimited(void)
0147 {
0148     if (schedule_delayed_work(&rfkill_op_work,
0149                   rfkill_ratelimit(rfkill_last_scheduled)))
0150         rfkill_last_scheduled = jiffies;
0151 }
0152 
0153 static void rfkill_schedule_global_op(enum rfkill_sched_op op)
0154 {
0155     unsigned long flags;
0156 
0157     spin_lock_irqsave(&rfkill_op_lock, flags);
0158     rfkill_op = op;
0159     rfkill_op_pending = true;
0160     if (op == RFKILL_GLOBAL_OP_EPO && !rfkill_is_epo_lock_active()) {
0161         /* bypass the limiter for EPO */
0162         mod_delayed_work(system_wq, &rfkill_op_work, 0);
0163         rfkill_last_scheduled = jiffies;
0164     } else
0165         rfkill_schedule_ratelimited();
0166     spin_unlock_irqrestore(&rfkill_op_lock, flags);
0167 }
0168 
0169 static void rfkill_schedule_toggle(enum rfkill_type type)
0170 {
0171     unsigned long flags;
0172 
0173     if (rfkill_is_epo_lock_active())
0174         return;
0175 
0176     spin_lock_irqsave(&rfkill_op_lock, flags);
0177     if (!rfkill_op_pending) {
0178         __set_bit(type, rfkill_sw_pending);
0179         __change_bit(type, rfkill_sw_state);
0180         rfkill_schedule_ratelimited();
0181     }
0182     spin_unlock_irqrestore(&rfkill_op_lock, flags);
0183 }
0184 
0185 static void rfkill_schedule_evsw_rfkillall(int state)
0186 {
0187     if (state)
0188         rfkill_schedule_global_op(rfkill_master_switch_op);
0189     else
0190         rfkill_schedule_global_op(RFKILL_GLOBAL_OP_EPO);
0191 }
0192 
0193 static void rfkill_event(struct input_handle *handle, unsigned int type,
0194             unsigned int code, int data)
0195 {
0196     if (type == EV_KEY && data == 1) {
0197         switch (code) {
0198         case KEY_WLAN:
0199             rfkill_schedule_toggle(RFKILL_TYPE_WLAN);
0200             break;
0201         case KEY_BLUETOOTH:
0202             rfkill_schedule_toggle(RFKILL_TYPE_BLUETOOTH);
0203             break;
0204         case KEY_UWB:
0205             rfkill_schedule_toggle(RFKILL_TYPE_UWB);
0206             break;
0207         case KEY_WIMAX:
0208             rfkill_schedule_toggle(RFKILL_TYPE_WIMAX);
0209             break;
0210         case KEY_RFKILL:
0211             rfkill_schedule_toggle(RFKILL_TYPE_ALL);
0212             break;
0213         }
0214     } else if (type == EV_SW && code == SW_RFKILL_ALL)
0215         rfkill_schedule_evsw_rfkillall(data);
0216 }
0217 
0218 static int rfkill_connect(struct input_handler *handler, struct input_dev *dev,
0219               const struct input_device_id *id)
0220 {
0221     struct input_handle *handle;
0222     int error;
0223 
0224     handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
0225     if (!handle)
0226         return -ENOMEM;
0227 
0228     handle->dev = dev;
0229     handle->handler = handler;
0230     handle->name = "rfkill";
0231 
0232     /* causes rfkill_start() to be called */
0233     error = input_register_handle(handle);
0234     if (error)
0235         goto err_free_handle;
0236 
0237     error = input_open_device(handle);
0238     if (error)
0239         goto err_unregister_handle;
0240 
0241     return 0;
0242 
0243  err_unregister_handle:
0244     input_unregister_handle(handle);
0245  err_free_handle:
0246     kfree(handle);
0247     return error;
0248 }
0249 
0250 static void rfkill_start(struct input_handle *handle)
0251 {
0252     /*
0253      * Take event_lock to guard against configuration changes, we
0254      * should be able to deal with concurrency with rfkill_event()
0255      * just fine (which event_lock will also avoid).
0256      */
0257     spin_lock_irq(&handle->dev->event_lock);
0258 
0259     if (test_bit(EV_SW, handle->dev->evbit) &&
0260         test_bit(SW_RFKILL_ALL, handle->dev->swbit))
0261         rfkill_schedule_evsw_rfkillall(test_bit(SW_RFKILL_ALL,
0262                             handle->dev->sw));
0263 
0264     spin_unlock_irq(&handle->dev->event_lock);
0265 }
0266 
0267 static void rfkill_disconnect(struct input_handle *handle)
0268 {
0269     input_close_device(handle);
0270     input_unregister_handle(handle);
0271     kfree(handle);
0272 }
0273 
0274 static const struct input_device_id rfkill_ids[] = {
0275     {
0276         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
0277         .evbit = { BIT_MASK(EV_KEY) },
0278         .keybit = { [BIT_WORD(KEY_WLAN)] = BIT_MASK(KEY_WLAN) },
0279     },
0280     {
0281         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
0282         .evbit = { BIT_MASK(EV_KEY) },
0283         .keybit = { [BIT_WORD(KEY_BLUETOOTH)] = BIT_MASK(KEY_BLUETOOTH) },
0284     },
0285     {
0286         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
0287         .evbit = { BIT_MASK(EV_KEY) },
0288         .keybit = { [BIT_WORD(KEY_UWB)] = BIT_MASK(KEY_UWB) },
0289     },
0290     {
0291         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
0292         .evbit = { BIT_MASK(EV_KEY) },
0293         .keybit = { [BIT_WORD(KEY_WIMAX)] = BIT_MASK(KEY_WIMAX) },
0294     },
0295     {
0296         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
0297         .evbit = { BIT_MASK(EV_KEY) },
0298         .keybit = { [BIT_WORD(KEY_RFKILL)] = BIT_MASK(KEY_RFKILL) },
0299     },
0300     {
0301         .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_SWBIT,
0302         .evbit = { BIT(EV_SW) },
0303         .swbit = { [BIT_WORD(SW_RFKILL_ALL)] = BIT_MASK(SW_RFKILL_ALL) },
0304     },
0305     { }
0306 };
0307 
0308 static struct input_handler rfkill_handler = {
0309     .name = "rfkill",
0310     .event = rfkill_event,
0311     .connect = rfkill_connect,
0312     .start = rfkill_start,
0313     .disconnect = rfkill_disconnect,
0314     .id_table = rfkill_ids,
0315 };
0316 
0317 int __init rfkill_handler_init(void)
0318 {
0319     switch (rfkill_master_switch_mode) {
0320     case RFKILL_INPUT_MASTER_UNBLOCKALL:
0321         rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNBLOCK;
0322         break;
0323     case RFKILL_INPUT_MASTER_RESTORE:
0324         rfkill_master_switch_op = RFKILL_GLOBAL_OP_RESTORE;
0325         break;
0326     case RFKILL_INPUT_MASTER_UNLOCK:
0327         rfkill_master_switch_op = RFKILL_GLOBAL_OP_UNLOCK;
0328         break;
0329     default:
0330         return -EINVAL;
0331     }
0332 
0333     /* Avoid delay at first schedule */
0334     rfkill_last_scheduled =
0335             jiffies - msecs_to_jiffies(RFKILL_OPS_DELAY) - 1;
0336     return input_register_handler(&rfkill_handler);
0337 }
0338 
0339 void __exit rfkill_handler_exit(void)
0340 {
0341     input_unregister_handler(&rfkill_handler);
0342     cancel_delayed_work_sync(&rfkill_op_work);
0343 }