From 6cf070d231d82d31248dfd204260ba8817f72d43 Mon Sep 17 00:00:00 2001 From: Sergio Paracuellos Date: Thu, 19 Apr 2018 07:07:58 +0200 Subject: [PATCH] staging: ks7010: refactor ks_wlan_set_encode function This commit refactors ks_wlan_set_encode function to improve readability. It just removes level indentation in some paths as well as removes not needed conditions paths which was checked before. Changes are as follows: - (dwrq->length > MAX_KEY_SIZE) check has been moved to the top. - extra check about (dwrq->length > 0) inside an if block where that was the condition to enter inside it has been removed. - (dwrq->flags & IW_ENCODE_NOKEY) check has been turned to avoid one level indentation. - extra check (index >= 0) && (index < 4) has been removed. In the top of the file invalid index values are being checked so it has no sense to check that again. - remove commented line. Signed-off-by: Sergio Paracuellos Signed-off-by: Greg Kroah-Hartman --- drivers/staging/ks7010/ks_wlan_net.c | 84 +++++++++++++----------------------- 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/drivers/staging/ks7010/ks_wlan_net.c b/drivers/staging/ks7010/ks_wlan_net.c index d0350a2..1f2e9f3 100644 --- a/drivers/staging/ks7010/ks_wlan_net.c +++ b/drivers/staging/ks7010/ks_wlan_net.c @@ -819,64 +819,48 @@ static int ks_wlan_set_encode(struct net_device *dev, struct iw_point *dwrq, char *extra) { struct ks_wlan_private *priv = netdev_priv(dev); - struct wep_key key; int index = (dwrq->flags & IW_ENCODE_INDEX); - int current_index = priv->reg.wep_index; - int i; if (priv->sleep_mode == SLP_SLEEP) return -EPERM; + if (dwrq->length > MAX_KEY_SIZE) + return -EINVAL; + /* for SLEEP MODE */ - /* index check */ if ((index < 0) || (index > 4)) return -EINVAL; - else if (index == 0) - index = current_index; - else - index--; + + index = (index == 0) ? priv->reg.wep_index : (index - 1); /* Is WEP supported ? */ /* Basic checking: do we have a key to set ? */ if (dwrq->length > 0) { - if (dwrq->length > MAX_KEY_SIZE) { /* Check the size of the key */ - return -EINVAL; - } - if (dwrq->length > MIN_KEY_SIZE) { /* Set the length */ - key.len = MAX_KEY_SIZE; - priv->reg.privacy_invoked = 0x01; - priv->need_commit |= SME_WEP_FLAG; - wep_on_off = WEP_ON_128BIT; - } else { - if (dwrq->length > 0) { - key.len = MIN_KEY_SIZE; - priv->reg.privacy_invoked = 0x01; - priv->need_commit |= SME_WEP_FLAG; - wep_on_off = WEP_ON_64BIT; - } else { /* Disable the key */ - key.len = 0; - } - } + key.len = (dwrq->length > MIN_KEY_SIZE) ? + MAX_KEY_SIZE : MIN_KEY_SIZE; + priv->reg.privacy_invoked = 0x01; + priv->need_commit |= SME_WEP_FLAG; + wep_on_off = (dwrq->length > MIN_KEY_SIZE) ? + WEP_ON_128BIT : WEP_ON_64BIT; /* Check if the key is not marked as invalid */ - if (!(dwrq->flags & IW_ENCODE_NOKEY)) { - /* Cleanup */ - memset(key.key, 0, MAX_KEY_SIZE); - /* Copy the key in the driver */ - if (copy_from_user - (key.key, dwrq->pointer, dwrq->length)) { - key.len = 0; - return -EFAULT; - } - /* Send the key to the card */ - priv->reg.wep_key[index].size = key.len; - for (i = 0; i < (priv->reg.wep_key[index].size); i++) - priv->reg.wep_key[index].val[i] = key.key[i]; + if (dwrq->flags & IW_ENCODE_NOKEY) + return 0; - priv->need_commit |= (SME_WEP_VAL1 << index); - priv->reg.wep_index = index; - priv->need_commit |= SME_WEP_INDEX; + /* Cleanup */ + memset(key.key, 0, MAX_KEY_SIZE); + /* Copy the key in the driver */ + if (copy_from_user(key.key, dwrq->pointer, dwrq->length)) { + key.len = 0; + return -EFAULT; } + /* Send the key to the card */ + priv->reg.wep_key[index].size = key.len; + memcpy(&priv->reg.wep_key[index].val[0], &key.key[0], + priv->reg.wep_key[index].size); + priv->need_commit |= (SME_WEP_VAL1 << index); + priv->reg.wep_index = index; + priv->need_commit |= SME_WEP_INDEX; } else { if (dwrq->flags & IW_ENCODE_DISABLED) { priv->reg.wep_key[0].size = 0; @@ -891,16 +875,11 @@ static int ks_wlan_set_encode(struct net_device *dev, wep_on_off = WEP_OFF; priv->need_commit |= SME_WEP_FLAG; } else { - /* Do we want to just set the transmit key index ? */ - if ((index >= 0) && (index < 4)) { - /* set_wep_key(priv, index, 0, 0, 1); xxx */ - if (priv->reg.wep_key[index].size != 0) { - priv->reg.wep_index = index; - priv->need_commit |= SME_WEP_INDEX; - } else { - return -EINVAL; - } - } + /* set_wep_key(priv, index, 0, 0, 1); xxx */ + if (priv->reg.wep_key[index].size == 0) + return -EINVAL; + priv->reg.wep_index = index; + priv->need_commit |= SME_WEP_INDEX; } } @@ -919,7 +898,6 @@ static int ks_wlan_set_encode(struct net_device *dev, priv->reg.authenticate_type = AUTH_TYPE_SHARED_KEY; } -// return -EINPROGRESS; /* Call commit handler */ if (priv->need_commit) { ks_wlan_setup_parameter(priv, priv->need_commit); priv->need_commit = 0; -- 2.7.4