ip17xx: Add VLAN tag field
[openwrt.git] / target / linux / generic-2.6 / files / drivers / net / phy / ip175c.c
index cf22908..706c973 100644 (file)
@@ -40,10 +40,12 @@ typedef char bitnum;
 
 #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
 
+struct ip175c_state;
+
 /*********** CONSTANTS ***********/
 struct register_mappings {
        char *NAME;
-       u16 MODEL_NO;                   // compare to bits 4-9 of MII register 0,3.
+       u16 MODEL_NO;                   // Compare to bits 4-9 of MII register 0,3.
        bitnum NUM_PORTS;
        bitnum CPU_PORT;
 
@@ -96,8 +98,21 @@ struct register_mappings {
 
        // set to 1 for 178C, 0 for 175C.
        bitnum SIMPLE_VLAN_REGISTERS;   // 175C has two vlans per register but 178C has only one.
+
+       // Pointers to functions which manipulate hardware state
+       int (*get_flags)(struct ip175c_state *state);
+       int (*get_state)(struct ip175c_state *state);
+       int (*update_state)(struct ip175c_state *state);
+       int (*set_vlan_mode)(struct ip175c_state *state);
+       int (*reset)(struct ip175c_state *state);
 };
 
+static int ip175c_get_flags(struct ip175c_state *state);
+static int ip175c_get_state(struct ip175c_state *state);
+static int ip175c_update_state(struct ip175c_state *state);
+static int ip175c_set_vlan_mode(struct ip175c_state *state);
+static int ip175c_do_reset(struct ip175c_state *state);
+
 static const struct register_mappings IP178C = {
        .NAME = "IP178C",
        .MODEL_NO = 0x18,
@@ -140,6 +155,11 @@ static const struct register_mappings IP178C = {
 
        .MII_REGISTER_EN = NOTSUPPORTED,
 
+       .get_flags = ip175c_get_flags,
+       .get_state = ip175c_get_state,
+       .update_state = ip175c_update_state,
+       .set_vlan_mode = ip175c_set_vlan_mode,
+       .reset = ip175c_do_reset,
 };
 
 static const struct register_mappings IP175C = {
@@ -184,6 +204,11 @@ static const struct register_mappings IP175C = {
 
        .MII_REGISTER_EN = NOTSUPPORTED,
 
+       .get_flags = ip175c_get_flags,
+       .get_state = ip175c_get_state,
+       .update_state = ip175c_update_state,
+       .set_vlan_mode = ip175c_set_vlan_mode,
+       .reset = ip175c_do_reset,
 };
 
 static const struct register_mappings IP175A = {
@@ -201,11 +226,8 @@ static const struct register_mappings IP175A = {
 
        .SIMPLE_VLAN_REGISTERS = 0,
 
-       // Register 19-21 documentation is missing/contradictory.
-       // For registers 19-21 ports need to be: even numbers to MSB, odd to LSB.
-       // This contradicts text for ROM registers, but follows logic of CoS bits.
-
-       .VLAN_LOOKUP_REG = {0,19},// +N/2
+       // Only programmable via EEPROM
+       .VLAN_LOOKUP_REG = NOTSUPPORTED,// +N/2
        .VLAN_LOOKUP_REG_5 = NOTSUPPORTED,
        .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,-1,-1,-1,-1},
        .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,-1,-1,-1,-1},
@@ -231,6 +253,12 @@ static const struct register_mappings IP175A = {
 
        .MII_REGISTER_EN = {0, 18},
        .MII_REGISTER_EN_BIT = 7,
+
+       .get_flags = ip175c_get_flags,
+       .get_state = ip175c_get_state,
+       .update_state = ip175c_update_state,
+       .set_vlan_mode = ip175c_set_vlan_mode,
+       .reset = ip175c_do_reset,
 };
 
 
@@ -248,13 +276,17 @@ struct ip175c_state {
        unsigned int add_tag;
        unsigned int remove_tag;
        int num_vlans;
-       unsigned int vlan_ports[MAX_VLANS];
+       struct vlan_state {
+               unsigned int ports;
+               unsigned int tag;       // VLAN tag (IP175D only)
+       } vlans[MAX_VLANS];
        const struct register_mappings *regs;
-       reg proc_mii; /*!< phy/reg for the low level register access via /proc */
+       reg proc_mii;   // phy/reg for the low level register access via swconfig
 
        char buf[80];
 };
 
+
 static int ip_phy_read(struct ip175c_state *state, int port, int reg)
 {
        int val = mdiobus_read(state->mii_bus, port, reg);
@@ -267,7 +299,6 @@ static int ip_phy_read(struct ip175c_state *state, int port, int reg)
        return val;
 }
 
-
 static int ip_phy_write(struct ip175c_state *state, int port, int reg, u16 val)
 {
        int err;
@@ -281,7 +312,6 @@ static int ip_phy_write(struct ip175c_state *state, int port, int reg, u16 val)
        return err;
 }
 
-
 static int ip_phy_write_masked(struct ip175c_state *state, int port, int reg, unsigned int mask, unsigned int data)
 {
        int val = ip_phy_read(state, port, reg);
@@ -310,6 +340,7 @@ static int setPhy(struct ip175c_state *state, reg mii, u16 value)
        return 0;
 }
 
+
 /**
  * These two macros are to simplify the mapping of logical bits to the bits in hardware.
  * NOTE: these macros will return if there is an error!
@@ -344,6 +375,7 @@ static int setPhy(struct ip175c_state *state, reg mii, u16 value)
                        return val;                             \
        } while (0)
 
+
 static int get_model(struct ip175c_state *state)
 {
        reg oui_id_reg = {0, 2};
@@ -387,7 +419,7 @@ static int get_model(struct ip175c_state *state)
 }
 
 /** Get only the vlan and router flags on the router **/
-static int get_flags(struct ip175c_state *state)
+static int ip175c_get_flags(struct ip175c_state *state)
 {
        int val;
 
@@ -412,7 +444,6 @@ static int get_flags(struct ip175c_state *state)
                state->num_vlans+=1; // does not include WAN.
        }
 
-
        val = getPhy(state, state->regs->VLAN_CONTROL_REG);
        if (val < 0) {
                return 0;
@@ -422,15 +453,18 @@ static int get_flags(struct ip175c_state *state)
 
        return  0;
 }
-/** Get all state variables for VLAN mappings and port-based tagging. **/
-static int get_state(struct ip175c_state *state)
+
+/** Get all state variables for VLAN mappings and port-based tagging **/
+static int ip175c_get_state(struct ip175c_state *state)
 {
        int i, j;
        int ret;
-       ret = get_flags(state);
+
+       ret = ip175c_get_flags(state);
        if (ret < 0) {
                return ret;
        }
+
        GET_PORT_BITS(state, state->remove_tag,
                                  state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
        GET_PORT_BITS(state, state->add_tag,
@@ -442,39 +476,39 @@ static int get_state(struct ip175c_state *state)
        }
 
        if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
-       for (j=0; j<MAX_PORTS; j++) {
-               state->ports[j].shareports = 0; // initialize them in case.
-       }
-       for (j=0; j<state->regs->NUM_PORTS; j++) {
-               reg addr;
-               const bitnum *bit_lookup = (j%2==0)?
-                       state->regs->VLAN_LOOKUP_EVEN_BIT:
-                       state->regs->VLAN_LOOKUP_ODD_BIT;
-               addr = state->regs->VLAN_LOOKUP_REG;
-               if (state->regs->SIMPLE_VLAN_REGISTERS) {
-                       addr.m += j;
-               } else {
-                       switch (j) {
-                       case 0:
-                       case 1:
-                               break;
-                       case 2:
-                       case 3:
-                               addr.m+=1;
-                               break;
-                       case 4:
-                               addr.m+=2;
-                               break;
-                       case 5:
-                               addr = state->regs->VLAN_LOOKUP_REG_5;
-                               break;
-                       }
+               for (j=0; j<MAX_PORTS; j++) {
+                       state->ports[j].shareports = 0; // initialize them in case.
                }
+               for (j=0; j<state->regs->NUM_PORTS; j++) {
+                       reg addr;
+                       const bitnum *bit_lookup = (j%2==0)?
+                               state->regs->VLAN_LOOKUP_EVEN_BIT:
+                               state->regs->VLAN_LOOKUP_ODD_BIT;
+                       addr = state->regs->VLAN_LOOKUP_REG;
+                       if (state->regs->SIMPLE_VLAN_REGISTERS) {
+                               addr.m += j;
+                       } else {
+                               switch (j) {
+                               case 0:
+                               case 1:
+                                       break;
+                               case 2:
+                               case 3:
+                                       addr.m+=1;
+                                       break;
+                               case 4:
+                                       addr.m+=2;
+                                       break;
+                               case 5:
+                                       addr = state->regs->VLAN_LOOKUP_REG_5;
+                                       break;
+                               }
+                       }
 
-               if (REG_SUPP(addr)) {
-                       GET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
+                       if (REG_SUPP(addr)) {
+                               GET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
+                       }
                }
-       }
        } else {
                for (j=0; j<MAX_PORTS; j++) {
                        state->ports[j].shareports = 0xff;
@@ -504,15 +538,15 @@ static int get_state(struct ip175c_state *state)
                        } else {
                                addr.m += j/2;
                        }
-                       GET_PORT_BITS(state, state->vlan_ports[j], addr, bit_lookup);
+                       GET_PORT_BITS(state, state->vlans[j].ports, addr, bit_lookup);
                }
        } else {
                for (j=0; j<MAX_VLANS; j++) {
-                       state->vlan_ports[j] = 0;
+                       state->vlans[j].ports = 0;
                        for (i=0; i<state->regs->NUM_PORTS; i++) {
                                if ((state->ports[i].pvid == j) ||
                                                (state->ports[i].pvid == 0)) {
-                                       state->vlan_ports[j] |= (1<<i);
+                                       state->vlans[j].ports |= (1<<i);
                                }
                        }
                }
@@ -521,9 +555,8 @@ static int get_state(struct ip175c_state *state)
        return 0;
 }
 
-
-/** Only update vlan and router flags in the switch **/
-static int update_flags(struct ip175c_state *state)
+/** Only set vlan and router flags in the switch **/
+static int ip175c_set_flags(struct ip175c_state *state)
 {
        int val;
 
@@ -560,8 +593,8 @@ static int update_flags(struct ip175c_state *state)
        return setPhy(state, state->regs->ROUTER_CONTROL_REG, val);
 }
 
-/** Update all VLAN and port state.  Usually you should call "correct_vlan_state" first. **/
-static int update_state(struct ip175c_state *state)
+/** Set all VLAN and port state.  Usually you should call "correct_vlan_state" first. **/
+static int ip175c_set_state(struct ip175c_state *state)
 {
        int j;
        int i;
@@ -571,57 +604,57 @@ static int update_state(struct ip175c_state *state)
                                  state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
 
        if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
-       for (j=0; j<state->regs->NUM_PORTS; j++) {
-               reg addr;
-               const bitnum *bit_lookup = (j%2==0)?
-                       state->regs->VLAN_LOOKUP_EVEN_BIT:
-                       state->regs->VLAN_LOOKUP_ODD_BIT;
-
-               // duplicate code -- sorry
-               addr = state->regs->VLAN_LOOKUP_REG;
-               if (state->regs->SIMPLE_VLAN_REGISTERS) {
-                       addr.m += j;
-               } else {
-                       switch (j) {
-                       case 0:
-                       case 1:
-                               break;
-                       case 2:
-                       case 3:
-                               addr.m+=1;
-                               break;
-                       case 4:
-                               addr.m+=2;
-                               break;
-                       case 5:
-                               addr = state->regs->VLAN_LOOKUP_REG_5;
-                               break;
-                       default:
-                               addr.m = -1; // shouldn't get here, but...
-                               break;
+               for (j=0; j<state->regs->NUM_PORTS; j++) {
+                       reg addr;
+                       const bitnum *bit_lookup = (j%2==0)?
+                               state->regs->VLAN_LOOKUP_EVEN_BIT:
+                               state->regs->VLAN_LOOKUP_ODD_BIT;
+
+                       // duplicate code -- sorry
+                       addr = state->regs->VLAN_LOOKUP_REG;
+                       if (state->regs->SIMPLE_VLAN_REGISTERS) {
+                               addr.m += j;
+                       } else {
+                               switch (j) {
+                               case 0:
+                               case 1:
+                                       break;
+                               case 2:
+                               case 3:
+                                       addr.m+=1;
+                                       break;
+                               case 4:
+                                       addr.m+=2;
+                                       break;
+                               case 5:
+                                       addr = state->regs->VLAN_LOOKUP_REG_5;
+                                       break;
+                               default:
+                                       addr.m = -1; // shouldn't get here, but...
+                                       break;
+                               }
+                       }
+                       //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
+                       if (REG_SUPP(addr)) {
+                               SET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
                        }
                }
-               //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
-               if (REG_SUPP(addr)) {
-                       SET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
-               }
-       }
        }
        if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
-       for (j=0; j<MAX_VLANS; j++) {
-               reg addr = state->regs->TAG_VLAN_MASK_REG;
-               const bitnum *bit_lookup = (j%2==0)?
-                       state->regs->TAG_VLAN_MASK_EVEN_BIT:
-                       state->regs->TAG_VLAN_MASK_ODD_BIT;
-               unsigned int vlan_mask;
-               if (state->regs->SIMPLE_VLAN_REGISTERS) {
-                       addr.m += j;
-               } else {
-                       addr.m += j/2;
+               for (j=0; j<MAX_VLANS; j++) {
+                       reg addr = state->regs->TAG_VLAN_MASK_REG;
+                       const bitnum *bit_lookup = (j%2==0)?
+                               state->regs->TAG_VLAN_MASK_EVEN_BIT:
+                               state->regs->TAG_VLAN_MASK_ODD_BIT;
+                       unsigned int vlan_mask;
+                       if (state->regs->SIMPLE_VLAN_REGISTERS) {
+                               addr.m += j;
+                       } else {
+                               addr.m += j/2;
+                       }
+                       vlan_mask = state->vlans[j].ports;
+                       SET_PORT_BITS(state, vlan_mask, addr, bit_lookup);
                }
-               vlan_mask = state->vlan_ports[j];
-               SET_PORT_BITS(state, vlan_mask, addr, bit_lookup);
-       }
        }
 
        for (i=0; i<MAX_PORTS; i++) {
@@ -634,43 +667,63 @@ static int update_state(struct ip175c_state *state)
                }
        }
 
-       return update_flags(state);
-
-       // software reset: 30.0 = 0x175C
-       // wait 2ms
-       // reset ports 0,1,2,3,4
+       return ip175c_set_flags(state);
 }
 
-/*
-  Uses only the VLAN port mask and the add tag mask to generate the other fields:
-  which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
+/**
 Uses only the VLAN port mask and the add tag mask to generate the other fields:
 which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
  */
-static void correct_vlan_state(struct ip175c_state *state)
+static void ip175c_correct_vlan_state(struct ip175c_state *state)
 {
        int i, j;
        state->num_vlans = 0;
        for (i=0; i<MAX_VLANS; i++) {
-               if (state->vlan_ports[i] != 0) {
-                       state->num_vlans = i+1; //hack -- we need to store the "set" vlans somewhere...
+               if (state->vlans[i].ports != 0) {
+                       state->num_vlans = i+1; // Hack -- we need to store the "set" vlans somewhere...
                }
        }
 
-
-
        for (i=0; i<state->regs->NUM_PORTS; i++) {
                unsigned int portmask = (1<<i);
                if (!state->vlan_enabled) {
-                       // share with everybody!
+                       // Share with everybody!
                        state->ports[i].shareports = (1<<state->regs->NUM_PORTS)-1;
                        continue;
                }
                state->ports[i].shareports = portmask;
                for (j=0; j<MAX_VLANS; j++) {
-                       if (state->vlan_ports[j] & portmask)
-                               state->ports[i].shareports |= state->vlan_ports[j];
+                       if (state->vlans[j].ports & portmask)
+                               state->ports[i].shareports |= state->vlans[j].ports;
                }
        }
-       state->remove_tag = ((~state->add_tag) & ((1<<state->regs->NUM_PORTS)-1));
+}
+
+static int ip175c_update_state(struct ip175c_state *state)
+{
+       ip175c_correct_vlan_state(state);
+       return ip175c_set_state(state);
+}
+
+static int ip175c_set_vlan_mode(struct ip175c_state *state)
+{
+       return ip175c_update_state(state);
+}
+
+static int ip175c_do_reset(struct ip175c_state *state)
+{
+       int err;
+
+       if (REG_SUPP(state->regs->MODE_REG)) {
+               err = setPhy(state, state->regs->MODE_REG, state->regs->MODE_VAL);
+               if (err < 0)
+                       return err;
+               err = getPhy(state, state->regs->MODE_REG);
+               if (err < 0)
+                       return err;
+       }
+
+       return 0;
 }
 
 static int ip175c_get_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
@@ -678,47 +731,48 @@ static int ip175c_get_enable_vlan(struct switch_dev *dev, const struct switch_at
        struct ip175c_state *state = dev->priv;
        int err;
 
-       err = get_state(state); // may be set in get_state.
+       err = state->regs->get_state(state); // May be set in get_state.
        if (err < 0)
                return err;
        val->value.i = state->vlan_enabled;
        return 0;
 }
 
+static void ip175c_reset_vlan_config(struct ip175c_state *state)
+{
+       int i;
+
+       state->remove_tag = (state->vlan_enabled ? ((1<<state->regs->NUM_PORTS)-1) : 0x0000);
+       state->add_tag = 0x0000;
+       for (i = 0; i < MAX_VLANS; i++) {
+               state->vlans[i].ports = 0x0000;
+               state->vlans[i].tag = (i ? i : 16);
+       }
+       for (i = 0; i < MAX_PORTS; i++)
+               state->ports[i].pvid = 0;
+}
+
 static int ip175c_set_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
        int err;
        int enable;
-       int i;
 
-       err = get_state(state);
+       err = state->regs->get_state(state);
        if (err < 0)
                return err;
        enable = val->value.i;
 
        if (state->vlan_enabled == enable) {
-               // do not change any state.
+               // Do not change any state.
                return 0;
        }
        state->vlan_enabled = enable;
 
        // Otherwise, if we are switching state, set fields to a known default.
-       state->remove_tag = 0x0000;
-       state->add_tag = 0x0000;
-       for (i = 0; i < MAX_PORTS; i++)
-               state->ports[i].shareports = 0xffff;
+       ip175c_reset_vlan_config(state);
 
-       for (i = 0; i < MAX_VLANS; i++)
-               state->vlan_ports[i] = 0x0;
-
-       if (state->vlan_enabled) {
-               // updates other fields only based off vlan_ports and add_tag fields.
-               // Note that by default, no ports are in any vlans.
-               correct_vlan_state(state);
-       }
-       // ensure sane defaults?
-       return update_state(state);
+       return state->regs->set_vlan_mode(state);
 }
 
 static int ip175c_get_ports(struct switch_dev *dev, struct switch_val *val)
@@ -732,11 +786,11 @@ static int ip175c_get_ports(struct switch_dev *dev, struct switch_val *val)
        if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
                return -EINVAL;
 
-       err = get_state(state);
+       err = state->regs->get_state(state);
        if (err<0)
                return err;
 
-       ports = state->vlan_ports[val->port_vlan];
+       ports = state->vlans[val->port_vlan].ports;
        b = 0;
        ind = 0;
        while (b < MAX_PORTS) {
@@ -763,25 +817,24 @@ static int ip175c_set_ports(struct switch_dev *dev, struct switch_val *val)
        if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
                return -EINVAL;
 
-       err = get_state(state);
+       err = state->regs->get_state(state);
        if (err < 0)
                return err;
 
-       state->vlan_ports[val->port_vlan] = 0;
+       state->vlans[val->port_vlan].ports = 0;
        for (i = 0; i < val->len; i++) {
-               int bitmask = (1<<val->value.ports[i].id);
-               state->vlan_ports[val->port_vlan] |= bitmask;
+               unsigned int bitmask = (1<<val->value.ports[i].id);
+               state->vlans[val->port_vlan].ports |= bitmask;
                if (val->value.ports[i].flags & (1<<SWITCH_PORT_FLAG_TAGGED)) {
                        state->add_tag |= bitmask;
+                       state->remove_tag &= (~bitmask);
                } else {
                        state->add_tag &= (~bitmask);
+                       state->remove_tag |= bitmask;
                }
        }
 
-       correct_vlan_state(state);
-       err = update_state(state);
-
-       return err;
+       return state->regs->update_state(state);
 }
 
 static int ip175c_apply(struct switch_dev *dev)
@@ -789,11 +842,11 @@ static int ip175c_apply(struct switch_dev *dev)
        struct ip175c_state *state = dev->priv;
        int err;
 
-       err = get_flags(state);
+       err = state->regs->get_flags(state);
        if (err < 0)
                return err;
 
-       if (REG_SUPP(state->regs->MII_REGISTER_EN)){
+       if (REG_SUPP(state->regs->MII_REGISTER_EN)) {
                int val = getPhy(state, state->regs->MII_REGISTER_EN);
                if (val < 0) {
                        return val;
@@ -809,7 +862,7 @@ static int ip175c_reset(struct switch_dev *dev)
        struct ip175c_state *state = dev->priv;
        int i, err;
 
-       err = get_flags(state);
+       err = state->regs->get_flags(state);
        if (err < 0)
                return err;
 
@@ -819,19 +872,14 @@ static int ip175c_reset(struct switch_dev *dev)
                        return err;
                err = getPhy(state, state->regs->RESET_REG);
 
-               /* data sheet specifies reset period is 2 msec
-                  (don't see any mention of the 2ms delay in the IP178C spec, only
-                   in IP175C, but it can't hurt.) */
+               /*
+                *  Data sheet specifies reset period to be 2 msec.
+                *  (I don't see any mention of the 2ms delay in the IP178C spec, only
+                *  in IP175C, but it can't hurt.)
+                */
                mdelay(2);
        }
 
-       if (REG_SUPP(state->regs->MODE_REG)) {
-               err = setPhy(state, state->regs->MODE_REG, state->regs->RESET_VAL);
-               if (err < 0)
-                       return err;
-               err = getPhy(state, state->regs->MODE_REG);
-       }
-
        /* reset switch ports */
        for (i = 0; i < state->regs->NUM_PORTS-1; i++) {
                err = ip_phy_write(state, i, MII_BMCR, BMCR_RESET);
@@ -839,16 +887,17 @@ static int ip175c_reset(struct switch_dev *dev)
                        return err;
        }
 
-       return 0;
+       ip175c_reset_vlan_config(state);
+
+       return state->regs->reset(state);
 }
 
-/*! get the current register number */
 static int ip175c_get_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
        int err;
 
-       err = get_state(state);
+       err = state->regs->get_state(state);
        if (err < 0)
                return err;
 
@@ -866,13 +915,12 @@ static int ip175c_get_tagged(struct switch_dev *dev, const struct switch_attr *a
        return 0;
 }
 
-/*! set a new register address for low level access to registers */
 static int ip175c_set_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
        int err;
 
-       err = get_state(state);
+       err = state->regs->get_state(state);
        if (err < 0)
                return err;
 
@@ -884,18 +932,10 @@ static int ip175c_set_tagged(struct switch_dev *dev, const struct switch_attr *a
        if (val->value.i == 1)
                state->add_tag |= (1<<val->port_vlan);
 
-       SET_PORT_BITS(state, state->add_tag,
-                                 state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
-       SET_PORT_BITS(state, state->remove_tag,
-                                 state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
-
-       return err;
+       return state->regs->update_state(state);
 }
 
-
-/* low level /proc procedures */
-
-/*! get the current phy address */
+/** Get the current phy address */
 static int ip175c_get_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -904,7 +944,7 @@ static int ip175c_get_phy(struct switch_dev *dev, const struct switch_attr *attr
        return 0;
 }
 
-/*! set a new phy address for low level access to registers */
+/** Set a new phy address for low level access to registers */
 static int ip175c_set_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -917,7 +957,7 @@ static int ip175c_set_phy(struct switch_dev *dev, const struct switch_attr *attr
        return 0;
 }
 
-/*! get the current register number */
+/** Get the current register number */
 static int ip175c_get_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -926,7 +966,7 @@ static int ip175c_get_reg(struct switch_dev *dev, const struct switch_attr *attr
        return 0;
 }
 
-/*! set a new register address for low level access to registers */
+/** Set a new register address for low level access to registers */
 static int ip175c_set_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -939,7 +979,7 @@ static int ip175c_set_reg(struct switch_dev *dev, const struct switch_attr *attr
        return 0;
 }
 
-/*! get the register content of state->proc_mii */
+/** Get the register content of state->proc_mii */
 static int ip175c_get_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -955,11 +995,11 @@ static int ip175c_get_val(struct switch_dev *dev, const struct switch_attr *attr
        }
 }
 
-/*! write a value to the register defined by phy/reg above */
+/** Write a value to the register defined by phy/reg above */
 static int ip175c_set_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
-       int myval, err = 0;
+       int myval, err = -EINVAL;
 
        myval = val->value.i;
        if (myval <= 0xffff && myval >= 0 && REG_SUPP(state->proc_mii)) {
@@ -971,10 +1011,37 @@ static int ip175c_set_val(struct switch_dev *dev, const struct switch_attr *attr
 static int ip175c_read_name(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
-       val->value.s = state->regs->NAME; // just a const pointer, won't be freed by swconfig.
+       val->value.s = state->regs->NAME; // Just a const pointer, won't be freed by swconfig.
        return 0;
 }
 
+static int ip175c_get_tag(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+{
+       struct ip175c_state *state = dev->priv;
+       int vlan = val->port_vlan;
+
+       if (vlan < 0 || vlan >= MAX_VLANS)
+               return -EINVAL;
+
+       val->value.i = state->vlans[vlan].tag;
+       return 0;
+}
+
+static int ip175c_set_tag(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
+{
+       struct ip175c_state *state = dev->priv;
+       int vlan = val->port_vlan;
+       int tag = val->value.i;
+
+       if (vlan < 0 || vlan >= MAX_VLANS)
+               return -EINVAL;
+
+       if (tag < 0 || tag > 4095)
+               return -EINVAL;
+
+       state->vlans[vlan].tag = tag;
+       return state->regs->update_state(state);
+}
 
 static int ip175c_set_port_speed(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
@@ -994,7 +1061,7 @@ static int ip175c_set_port_speed(struct switch_dev *dev, const struct switch_att
                speed = 1;
        }
 
-       /* can't set speed for cpu port */
+       /* Can't set speed for cpu port */
        if (nr == state->regs->CPU_PORT)
                return -EINVAL;
 
@@ -1040,7 +1107,6 @@ static int ip175c_get_port_speed(struct switch_dev *dev, const struct switch_att
        return 0;
 }
 
-
 static int ip175c_get_port_status(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
 {
        struct ip175c_state *state = dev->priv;
@@ -1096,13 +1162,18 @@ static int ip175c_get_pvid(struct switch_dev *dev, int port, int *val)
 static int ip175c_set_pvid(struct switch_dev *dev, int port, int val)
 {
        struct ip175c_state *state = dev->priv;
+       int err;
 
-       state->ports[port].pvid = val;
+       if (val < 0 || val >= MAX_VLANS)
+               return -EINVAL;
 
-       if (!REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[port]))
-               return 0;
+       err = state->regs->get_state(state);
+       if (err < 0)
+               return err;
+
+       state->ports[port].pvid = val;
 
-       return setPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[port], val);
+       return state->regs->update_state(state);
 }
 
 
@@ -1122,6 +1193,10 @@ enum Globals {
        IP175C_REGISTER_ERRNO,
 };
 
+enum Vlans {
+       IP175C_VLAN_TAG,
+};
+
 static const struct switch_attr ip175c_global[] = {
        [IP175C_ENABLE_VLAN] = {
                .id = IP175C_ENABLE_VLAN,
@@ -1143,7 +1218,7 @@ static const struct switch_attr ip175c_global[] = {
        [IP175C_REGISTER_PHY] = {
                .id = IP175C_REGISTER_PHY,
                .type = SWITCH_TYPE_INT,
-               .description = "Direct register access: set phy (0-4, or 29,30,31)",
+               .description = "Direct register access: set PHY (0-4, or 29,30,31)",
                .name  = "phy",
                .get  = ip175c_get_phy,
                .set = ip175c_set_phy,
@@ -1151,7 +1226,7 @@ static const struct switch_attr ip175c_global[] = {
        [IP175C_REGISTER_MII] = {
                .id = IP175C_REGISTER_MII,
                .type = SWITCH_TYPE_INT,
-               .description = "Direct register access: set mii number (0-31)",
+               .description = "Direct register access: set MII register number (0-31)",
                .name  = "reg",
                .get  = ip175c_get_reg,
                .set = ip175c_set_reg,
@@ -1167,6 +1242,14 @@ static const struct switch_attr ip175c_global[] = {
 };
 
 static const struct switch_attr ip175c_vlan[] = {
+       [IP175C_VLAN_TAG] = {
+               .id = IP175C_VLAN_TAG,
+               .type = SWITCH_TYPE_INT,
+               .description = "VLAN tag (0-4095) [IP175D only]",
+               .name = "tag",
+               .get = ip175c_get_tag,
+               .set = ip175c_set_tag,
+       }
 };
 
 static const struct switch_attr ip175c_port[] = {
@@ -1202,7 +1285,7 @@ static int ip175c_probe(struct phy_device *pdev)
        struct switch_dev *dev;
        int err;
 
-       /* we only attach to PHY 0, but use all available PHYs */
+       /* We only attach to PHY 0, but use all available PHYs */
        if (pdev->addr != 0)
                return -ENODEV;
 
@@ -1336,4 +1419,3 @@ MODULE_LICENSE("GPL");
 
 module_init(ip175c_init);
 module_exit(ip175c_exit);
-
This page took 0.048856 seconds and 4 git commands to generate.