swconfig: simplify init code
[openwrt/openwrt.git] / target / linux / generic / files / drivers / net / phy / swconfig.c
index 6bb3be1..4bfe64f 100644 (file)
@@ -127,30 +127,21 @@ swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
        return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
 }
 
-static const char *
-swconfig_speed_str(enum switch_port_speed speed)
+static int
+swconfig_set_link(struct switch_dev *dev, const struct switch_attr *attr,
+                       struct switch_val *val)
 {
-       switch (speed) {
-       case SWITCH_PORT_SPEED_10:
-               return "10baseT";
-       case SWITCH_PORT_SPEED_100:
-               return "100baseT";
-       case SWITCH_PORT_SPEED_1000:
-               return "1000baseT";
-       default:
-               break;
-       }
+       if (!dev->ops->set_port_link)
+               return -EOPNOTSUPP;
 
-       return "unknown";
+       return dev->ops->set_port_link(dev, val->port_vlan, val->value.link);
 }
 
 static int
 swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
                        struct switch_val *val)
 {
-       struct switch_port_link link;
-       int len;
-       int ret;
+       struct switch_port_link *link = val->value.link;
 
        if (val->port_vlan >= dev->ports)
                return -EINVAL;
@@ -158,32 +149,8 @@ swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
        if (!dev->ops->get_port_link)
                return -EOPNOTSUPP;
 
-       memset(&link, 0, sizeof(link));
-       ret = dev->ops->get_port_link(dev, val->port_vlan, &link);
-       if (ret)
-               return ret;
-
-       memset(dev->buf, 0, sizeof(dev->buf));
-
-       if (link.link)
-               len = snprintf(dev->buf, sizeof(dev->buf),
-                              "port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
-                              val->port_vlan,
-                              swconfig_speed_str(link.speed),
-                              link.duplex ? "full" : "half",
-                              link.tx_flow ? "txflow " : "",
-                              link.rx_flow ?   "rxflow " : "",
-                              link.eee & ADVERTISED_100baseT_Full ? "eee100 " : "",
-                              link.eee & ADVERTISED_1000baseT_Full ? "eee1000 " : "",
-                              link.aneg ? "auto" : "");
-       else
-               len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
-                              val->port_vlan);
-
-       val->value.s = dev->buf;
-       val->len = len;
-
-       return 0;
+       memset(link, 0, sizeof(*link));
+       return dev->ops->get_port_link(dev, val->port_vlan, link);
 }
 
 static int
@@ -246,10 +213,10 @@ static struct switch_attr default_port[] = {
                .get = swconfig_get_pvid,
        },
        [PORT_LINK] = {
-               .type = SWITCH_TYPE_STRING,
+               .type = SWITCH_TYPE_LINK,
                .name = "link",
                .description = "Get port link information",
-               .set = NULL,
+               .set = swconfig_set_link,
                .get = swconfig_get_link,
        }
 };
@@ -325,6 +292,12 @@ static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
        [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
 };
 
+static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
+       [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
+       [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
+       [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
+};
+
 static inline void
 swconfig_lock(void)
 {
@@ -637,6 +610,22 @@ swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
        return 0;
 }
 
+static int
+swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla,
+                   struct switch_port_link *link)
+{
+       struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
+
+       if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy))
+               return -EINVAL;
+
+       link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
+       link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
+       link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
+
+       return 0;
+}
+
 static int
 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
 {
@@ -687,6 +676,21 @@ swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
                        err = 0;
                }
                break;
+       case SWITCH_TYPE_LINK:
+               val.value.link = &dev->linkbuf;
+               memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
+
+               if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) {
+                       err = swconfig_parse_link(skb,
+                                                 info->attrs[SWITCH_ATTR_OP_VALUE_LINK],
+                                                 val.value.link);
+                       if (err < 0)
+                               goto error;
+               } else {
+                       val.len = 0;
+                       err = 0;
+               }
+               break;
        default:
                goto error;
        }
@@ -770,6 +774,53 @@ done:
        return err;
 }
 
+static int
+swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr,
+                  const struct switch_port_link *link)
+{
+       struct nlattr *p = NULL;
+       int err = 0;
+
+       p = nla_nest_start(msg, attr);
+       if (link->link) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK))
+                       goto nla_put_failure;
+       }
+       if (link->duplex) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX))
+                       goto nla_put_failure;
+       }
+       if (link->aneg) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG))
+                       goto nla_put_failure;
+       }
+       if (link->tx_flow) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW))
+                       goto nla_put_failure;
+       }
+       if (link->rx_flow) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW))
+                       goto nla_put_failure;
+       }
+       if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed))
+               goto nla_put_failure;
+       if (link->eee & ADVERTISED_100baseT_Full) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET))
+                       goto nla_put_failure;
+       }
+       if (link->eee & ADVERTISED_1000baseT_Full) {
+               if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET))
+                       goto nla_put_failure;
+       }
+       nla_nest_end(msg, p);
+
+       return err;
+
+nla_put_failure:
+       nla_nest_cancel(msg, p);
+       return -1;
+}
+
 static int
 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
 {
@@ -794,6 +845,9 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
                val.value.ports = dev->portbuf;
                memset(dev->portbuf, 0,
                        sizeof(struct switch_port) * dev->ports);
+       } else if (attr->type == SWITCH_TYPE_LINK) {
+               val.value.link = &dev->linkbuf;
+               memset(&dev->linkbuf, 0, sizeof(struct switch_port_link));
        }
 
        err = attr->get(dev, attr, &val);
@@ -824,6 +878,12 @@ swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
                if (err < 0)
                        goto nla_put_failure;
                break;
+       case SWITCH_TYPE_LINK:
+               err = swconfig_send_link(msg, info,
+                                        SWITCH_ATTR_OP_VALUE_LINK, val.value.link);
+               if (err < 0)
+                       goto nla_put_failure;
+               break;
        default:
                pr_debug("invalid type in attribute\n");
                err = -EINVAL;
@@ -1112,34 +1172,9 @@ EXPORT_SYMBOL_GPL(unregister_switch);
 static int __init
 swconfig_init(void)
 {
-       int err;
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
-       int i;
-#endif
-
        INIT_LIST_HEAD(&swdevs);
        
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
-       err = genl_register_family(&switch_fam);
-       if (err)
-               return err;
-
-       for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
-               err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
-               if (err)
-                       goto unregister;
-       }
-       return 0;
-
-unregister:
-       genl_unregister_family(&switch_fam);
-       return err;
-#else
-       err = genl_register_family_with_ops(&switch_fam, swconfig_ops);
-       if (err)
-               return err;
-       return 0;
-#endif
+       return genl_register_family_with_ops(&switch_fam, swconfig_ops);
 }
 
 static void __exit