rfkill: Use switch to demux userspace operations
authorJoão Paulo Rechi Vita <jprvita@gmail.com>
Mon, 22 Feb 2016 16:36:39 +0000 (11:36 -0500)
committerJohannes Berg <johannes.berg@intel.com>
Tue, 5 Apr 2016 08:48:53 +0000 (10:48 +0200)
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 <jprvita@endlessm.com>
[roll in fix for RFKILL_OP_CHANGE from Jouni]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
net/rfkill/core.c

index 03f26e3a6f48afa0308d0ffbad0a3d740dab25b8..884027f62783a6a975b42c0bdd5f39010df9834a 100644 (file)
@@ -1141,6 +1141,7 @@ static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
 {
        struct rfkill *rfkill;
        struct rfkill_event ev;
+       int ret;
 
        /* we don't need the 'hard' variable but accept it */
        if (count < RFKILL_EVENT_SIZE_V1 - 1)
@@ -1155,29 +1156,36 @@ static ssize_t rfkill_fop_write(struct file *file, const char __user *buf,
        if (copy_from_user(&ev, buf, count))
                return -EFAULT;
 
-       if (ev.op != RFKILL_OP_CHANGE && ev.op != RFKILL_OP_CHANGE_ALL)
-               return -EINVAL;
-
        if (ev.type >= NUM_RFKILL_TYPES)
                return -EINVAL;
 
        mutex_lock(&rfkill_global_mutex);
 
-       if (ev.op == RFKILL_OP_CHANGE_ALL)
+       switch (ev.op) {
+       case RFKILL_OP_CHANGE_ALL:
                rfkill_update_global_state(ev.type, ev.soft);
-
-       list_for_each_entry(rfkill, &rfkill_list, node) {
-               if (rfkill->idx != ev.idx && ev.op != RFKILL_OP_CHANGE_ALL)
-                       continue;
-
-               if (rfkill->type != ev.type && ev.type != RFKILL_TYPE_ALL)
-                       continue;
-
-               rfkill_set_block(rfkill, ev.soft);
+               list_for_each_entry(rfkill, &rfkill_list, node)
+                       if (rfkill->type == ev.type ||
+                           ev.type == RFKILL_TYPE_ALL)
+                               rfkill_set_block(rfkill, ev.soft);
+               ret = 0;
+               break;
+       case RFKILL_OP_CHANGE:
+               list_for_each_entry(rfkill, &rfkill_list, node)
+                       if (rfkill->idx == ev.idx &&
+                           (rfkill->type == ev.type ||
+                            ev.type == RFKILL_TYPE_ALL))
+                               rfkill_set_block(rfkill, ev.soft);
+               ret = 0;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
        }
+
        mutex_unlock(&rfkill_global_mutex);
 
-       return count;
+       return ret ?: count;
 }
 
 static int rfkill_fop_release(struct inode *inode, struct file *file)