22 #include <linux/kernel.h>
23 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/list.h>
29 #include <linux/rfkill.h>
30 #include <linux/sched.h>
32 #include <linux/device.h>
34 #include <linux/wait.h>
35 #include <linux/poll.h>
37 #include <linux/slab.h>
41 #define POLL_INTERVAL (5 * HZ)
43 #define RFKILL_BLOCK_HW BIT(0)
44 #define RFKILL_BLOCK_SW BIT(1)
45 #define RFKILL_BLOCK_SW_PREV BIT(2)
46 #define RFKILL_BLOCK_ANY (RFKILL_BLOCK_HW |\
49 #define RFKILL_BLOCK_SW_SETCALL BIT(31)
67 #ifdef CONFIG_RFKILL_LEDS
68 struct led_trigger led_trigger;
69 const char *ledtrigname;
79 #define to_rfkill(d) container_of(d, struct rfkill, dev)
116 static unsigned int rfkill_default_state = 1;
119 "Default initial state for all radio types, 0 = radio off");
125 static bool rfkill_epo_lock_active;
128 #ifdef CONFIG_RFKILL_LEDS
129 static void rfkill_led_trigger_event(
struct rfkill *
rfkill)
136 trigger = &rfkill->led_trigger;
148 rfkill =
container_of(led->trigger,
struct rfkill, led_trigger);
150 rfkill_led_trigger_event(rfkill);
153 const char *rfkill_get_led_trigger_name(
struct rfkill *
rfkill)
155 return rfkill->led_trigger.
name;
163 rfkill->ledtrigname =
name;
167 static int rfkill_led_trigger_register(
struct rfkill *
rfkill)
169 rfkill->led_trigger.
name = rfkill->ledtrigname
170 ? : dev_name(&rfkill->
dev);
171 rfkill->led_trigger.activate = rfkill_led_trigger_activate;
175 static void rfkill_led_trigger_unregister(
struct rfkill *
rfkill)
180 static void rfkill_led_trigger_event(
struct rfkill *
rfkill)
184 static inline int rfkill_led_trigger_register(
struct rfkill *
rfkill)
189 static inline void rfkill_led_trigger_unregister(
struct rfkill *
rfkill)
207 spin_unlock_irqrestore(&rfkill->
lock, flags);
219 rfkill_fill_event(&ev->
ev, rfkill, op);
238 static bool __rfkill_set_hw_state(
struct rfkill *rfkill,
239 bool blocked,
bool *change)
252 *change = prev != blocked;
254 spin_unlock_irqrestore(&rfkill->
lock, flags);
256 rfkill_led_trigger_event(rfkill);
270 static void rfkill_set_block(
struct rfkill *rfkill,
bool blocked)
284 if (rfkill->
ops->query)
285 rfkill->
ops->query(rfkill, rfkill->
data);
301 spin_unlock_irqrestore(&rfkill->
lock, flags);
303 err = rfkill->
ops->set_block(rfkill->
data, blocked);
320 spin_unlock_irqrestore(&rfkill->
lock, flags);
322 rfkill_led_trigger_event(rfkill);
328 #ifdef CONFIG_RFKILL_INPUT
342 static void __rfkill_switch_all(
const enum rfkill_type type,
bool blocked)
344 struct rfkill *rfkill;
346 rfkill_global_states[
type].cur = blocked;
351 rfkill_set_block(rfkill, blocked);
372 if (!rfkill_epo_lock_active)
373 __rfkill_switch_all(type, blocked);
389 struct rfkill *rfkill;
397 rfkill_epo_lock_active =
true;
399 rfkill_set_block(rfkill,
true);
402 rfkill_global_states[
i].sav = rfkill_global_states[
i].cur;
403 rfkill_global_states[
i].cur =
true;
425 rfkill_epo_lock_active =
false;
427 __rfkill_switch_all(i, rfkill_global_states[i].
sav);
443 rfkill_epo_lock_active =
false;
458 return rfkill_epo_lock_active;
470 return rfkill_global_states[
type].cur;
479 ret = __rfkill_set_hw_state(rfkill, blocked, &change);
491 static void __rfkill_set_sw_state(
struct rfkill *rfkill,
bool blocked)
502 rfkill->
state &= ~bit;
514 __rfkill_set_sw_state(rfkill, blocked);
516 blocked = blocked || hwblock;
517 spin_unlock_irqrestore(&rfkill->
lock, flags);
522 if (prev != blocked && !hwblock)
525 rfkill_led_trigger_event(rfkill);
539 __rfkill_set_sw_state(rfkill, blocked);
541 spin_unlock_irqrestore(&rfkill->
lock, flags);
560 __rfkill_set_sw_state(rfkill, sw);
566 spin_unlock_irqrestore(&rfkill->
lock, flags);
571 if (swprev != sw || hwprev != hw)
574 rfkill_led_trigger_event(rfkill);
588 static const char *rfkill_get_type_str(
enum rfkill_type type)
598 return "ultrawideband";
618 return sprintf(buf,
"%s\n", rfkill_get_type_str(rfkill->
type));
659 const char *buf,
size_t count)
668 err = kstrtoul(buf, 0, &state);
676 rfkill_set_block(rfkill, state);
682 static u8 user_state_from_blocked(
unsigned long state)
698 return sprintf(buf,
"%d\n", user_state_from_blocked(rfkill->
state));
703 const char *buf,
size_t count)
712 err = kstrtoul(buf, 0, &state);
731 return sprintf(buf,
"%d\n", 0);
736 const char *buf,
size_t count)
753 static void rfkill_release(
struct device *dev)
771 rfkill_get_type_str(rfkill->
type));
775 state = rfkill->
state;
776 spin_unlock_irqrestore(&rfkill->
lock, flags);
778 user_state_from_blocked(state));
786 if (!rfkill->
ops->poll)
797 if (!rfkill->
ops->poll)
813 static int rfkill_resume(
struct device *dev)
820 rfkill_set_block(rfkill, cur);
828 static struct class rfkill_class = {
830 .dev_release = rfkill_release,
831 .dev_attrs = rfkill_dev_attrs,
832 .dev_uevent = rfkill_dev_uevent,
833 .suspend = rfkill_suspend,
834 .resume = rfkill_resume,
843 state = rfkill->
state;
844 spin_unlock_irqrestore(&rfkill->
lock, flags);
857 struct rfkill *rfkill;
872 rfkill = kzalloc(
sizeof(*rfkill),
GFP_KERNEL);
877 INIT_LIST_HEAD(&rfkill->
node);
881 rfkill->
data = ops_data;
884 dev->
class = &rfkill_class;
894 struct rfkill *rfkill;
903 rfkill->
ops->poll(rfkill, rfkill->
data);
909 static void rfkill_uevent_work(
struct work_struct *work)
911 struct rfkill *rfkill;
920 static void rfkill_sync_work(
struct work_struct *work)
922 struct rfkill *rfkill;
928 cur = rfkill_global_states[rfkill->
type].cur;
929 rfkill_set_block(rfkill, cur);
935 static unsigned long rfkill_no;
948 rfkill->
idx = rfkill_no;
958 error = rfkill_led_trigger_register(rfkill);
968 if (rfkill->
ops->poll)
972 if (!rfkill->
persistent || rfkill_epo_lock_active) {
975 #ifdef CONFIG_RFKILL_INPUT
979 __rfkill_switch_all(rfkill->
type, soft_blocked);
991 list_del_init(&rfkill->
node);
1002 if (rfkill->
ops->poll)
1014 list_del_init(&rfkill->
node);
1017 rfkill_led_trigger_unregister(rfkill);
1031 struct rfkill *rfkill;
1038 INIT_LIST_HEAD(&data->
events);
1056 list_add(&data->
list, &rfkill_fds);
1079 poll_wait(file, &data->
read_wait, wait);
1082 if (!list_empty(&data->
events))
1089 static bool rfkill_readable(
struct rfkill_data *data)
1094 r = !list_empty(&data->
events);
1100 static ssize_t rfkill_fop_read(
struct file *file,
char __user *buf,
1101 size_t count, loff_t *
pos)
1110 while (list_empty(&data->
events)) {
1117 rfkill_readable(data));
1127 sz =
min_t(
unsigned long,
sizeof(ev->
ev), count);
1139 static ssize_t rfkill_fop_write(
struct file *file,
const char __user *buf,
1140 size_t count, loff_t *pos)
1142 struct rfkill *rfkill;
1154 count =
min(count,
sizeof(ev));
1161 if (ev.type >= NUM_RFKILL_TYPES)
1170 rfkill_global_states[i].cur = ev.soft;
1172 rfkill_global_states[ev.type].cur = ev.soft;
1183 rfkill_set_block(rfkill, ev.soft);
1190 static int rfkill_fop_release(
struct inode *inode,
struct file *file)
1203 #ifdef CONFIG_RFKILL_INPUT
1214 #ifdef CONFIG_RFKILL_INPUT
1215 static long rfkill_fop_ioctl(
struct file *file,
unsigned int cmd,
1242 .open = rfkill_fop_open,
1243 .read = rfkill_fop_read,
1244 .write = rfkill_fop_write,
1245 .poll = rfkill_fop_poll,
1246 .release = rfkill_fop_release,
1247 #ifdef CONFIG_RFKILL_INPUT
1248 .unlocked_ioctl = rfkill_fop_ioctl,
1249 .compat_ioctl = rfkill_fop_ioctl,
1256 .fops = &rfkill_fops,
1260 static int __init rfkill_init(
void)
1266 rfkill_global_states[i].cur = !rfkill_default_state;
1278 #ifdef CONFIG_RFKILL_INPUT
1292 static void __exit rfkill_exit(
void)
1294 #ifdef CONFIG_RFKILL_INPUT