Skip to content

Commit 1948b2a

Browse files
jprvitajmberg-intel
authored andcommitted
rfkill: Use switch to demux userspace operations
Using a switch to handle different ev.op values in rfkill_fop_write() makes the code easier to extend, as out-of-range values can always be handled by the default case. Signed-off-by: João Paulo Rechi Vita <[email protected]> [roll in fix for RFKILL_OP_CHANGE from Jouni] Signed-off-by: Johannes Berg <[email protected]>
1 parent 98bd147 commit 1948b2a

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

net/rfkill/core.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
11411141
{
11421142
struct rfkill *rfkill;
11431143
struct rfkill_event ev;
1144+
int ret;
11441145

11451146
/* we don't need the 'hard' variable but accept it */
11461147
if (count < RFKILL_EVENT_SIZE_V1 - 1)
@@ -1155,29 +1156,36 @@ static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
11551156
if (copy_from_user(&ev, buf, count))
11561157
return -EFAULT;
11571158

1158-
if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
1159-
return -EINVAL;
1160-
11611159
if (ev.type >= NUM_RFKILL_TYPES)
11621160
return -EINVAL;
11631161

11641162
mutex_lock(&rfkill_global_mutex);
11651163

1166-
if (ev.op == RFKILL_OP_CHANGE_ALL)
1164+
switch (ev.op) {
1165+
case RFKILL_OP_CHANGE_ALL:
11671166
rfkill_update_global_state(ev.type, ev.soft);
1168-
1169-
list_for_each_entry(rfkill, &rfkill_list, node) {
1170-
if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
1171-
continue;
1172-
1173-
if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
1174-
continue;
1175-
1176-
rfkill_set_block(rfkill, ev.soft);
1167+
list_for_each_entry(rfkill, &rfkill_list, node)
1168+
if (rfkill->type == ev.type ||
1169+
ev.type == RFKILL_TYPE_ALL)
1170+
rfkill_set_block(rfkill, ev.soft);
1171+
ret = 0;
1172+
break;
1173+
case RFKILL_OP_CHANGE:
1174+
list_for_each_entry(rfkill, &rfkill_list, node)
1175+
if (rfkill->idx == ev.idx &&
1176+
(rfkill->type == ev.type ||
1177+
ev.type == RFKILL_TYPE_ALL))
1178+
rfkill_set_block(rfkill, ev.soft);
1179+
ret = 0;
1180+
break;
1181+
default:
1182+
ret = -EINVAL;
1183+
break;
11771184
}
1185+
11781186
mutex_unlock(&rfkill_global_mutex);
11791187

1180-
return count;
1188+
return ret ?: count;
11811189
}
11821190

11831191
static int rfkill_fop_release(struct inode *inode, struct file *file)

0 commit comments

Comments
 (0)