fix a bug that prevents the wgt634u from initializing the filesystem properly
[openwrt.git] / openwrt / target / linux / package / switch / src / switch-robo.c
index d596dc6..b23fca6 100644 (file)
@@ -34,6 +34,7 @@
 #include "etc53xx.h"
 
 #define DRIVER_NAME            "bcm53xx"
+#define DRIVER_VERSION "0.01"
 
 #define ROBO_PHY_ADDR  0x1E    /* robo switch phy address */
 
 #define SIOCGETCPHYRD           (SIOCDEVPRIVATE + 9)
 #define SIOCSETCPHYWR           (SIOCDEVPRIVATE + 10)
 
+static char *device;
 static int use_et = 0;
 static int is_5350 = 0;
-static int max_vlans, max_ports;
 static struct ifreq ifr;
 static struct net_device *dev;
-
-static int isspace(char c) {
-       switch(c) {
-               case ' ':
-               case 0x09:
-               case 0x0a:
-               case 0x0d:
-                       return 1;
-               default:
-                       return 0;
-       }
-}
+static unsigned char port[6] = { 0, 1, 2, 3, 4, 8 };
 
 static int do_ioctl(int cmd, void *buf)
 {
@@ -268,36 +258,13 @@ static int robo_probe(char *devname)
        }
        
        is_5350 = robo_vlan5350();
-       max_ports = 6;
        
-       for (i = 0; i <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); i++) {
-               /* issue read */
-               __u16 val16 = (i) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
-               
-               if (is_5350) {
-                       u32 val32;
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
-                       /* actual read */
-                       val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
-                       if ((val32 & (1 << 20)) /* valid */) {
-                               max_vlans = i + 1;
-                       }
-               } else {
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
-                       /* actual read */
-                       val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
-                       if ((val16 & (1 << 14)) /* valid */) {
-                               max_vlans = i + 1;
-                       }
-               }
-       }
-
        printk("found!\n");
        return 0;
 }
 
 
-static int handle_vlan_port_read(char *buf, int nr)
+static int handle_vlan_port_read(void *driver, char *buf, int nr)
 {
        __u16 val16;
        int len = 0;
@@ -313,8 +280,15 @@ static int handle_vlan_port_read(char *buf, int nr)
                if ((val32 & (1 << 20)) /* valid */) {
                        for (j = 0; j < 6; j++) {
                                if (val32 & (1 << j)) {
-                                       len += sprintf(buf + len, "%d%s\t", j, 
-                                               (val32 & (1 << (j + 6))) ? (j == 5 ? "u" : "") : "t");
+                                       len += sprintf(buf + len, "%d", j);
+                                       if (val32 & (1 << (j + 6))) {
+                                               if (j == 5) buf[len++] = 'u';
+                                       } else {
+                                               buf[len++] = 't';
+                                               if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
+                                                       buf[len++] = '*';
+                                       }
+                                       buf[len++] = '\t';
                                }
                        }
                        len += sprintf(buf + len, "\n");
@@ -326,8 +300,15 @@ static int handle_vlan_port_read(char *buf, int nr)
                if ((val16 & (1 << 14)) /* valid */) {
                        for (j = 0; j < 6; j++) {
                                if (val16 & (1 << j)) {
-                                       len += sprintf(buf + len, "%d%s\t", j, (val16 & (1 << (j + 7))) ? 
-                                               (j == 5 ? "u" : "") : "t");
+                                       len += sprintf(buf + len, "%d", j);
+                                       if (val16 & (1 << (j + 7))) {
+                                               if (j == 5) buf[len++] = 'u';
+                                       } else {
+                                               buf[len++] = 't';
+                                               if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
+                                                       buf[len++] = '*';
+                                       }
+                                       buf[len++] = '\t';
                                }
                        }
                        len += sprintf(buf + len, "\n");
@@ -337,69 +318,144 @@ static int handle_vlan_port_read(char *buf, int nr)
        return len;
 }
 
-static int handle_vlan_port_write(char *buf, int nr)
+static int handle_vlan_port_write(void *driver, char *buf, int nr)
 {
-       int untag = 0;
-       int member = 0;
+       switch_driver *d = (switch_driver *) driver;
+       switch_vlan_config *c = switch_parse_vlan(d, buf);
        int j;
        __u16 val16;
        
-       while (*buf >= '0' && *buf <= '9') {
-               j = *buf++ - '0';
-               member |= 1 << j;
-               
-               /* untag if needed, CPU port requires special handling */
-               if (*buf == 'u' || (j != 5 && (isspace(*buf) || *buf == 0))) {
-                       untag |= 1 << j;
-                       if (*buf) buf++;
+       if (c == NULL)
+               return -EINVAL;
+
+       for (j = 0; j < d->ports; j++) {
+               if ((c->untag | c->pvid) & (1 << j))
                        /* change default vlan tag */
                        robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
-               } else if (*buf == '*' || *buf == 't' || isspace(*buf)) {
-                       buf++;
-               } else break;
-               
-               while (isspace(*buf)) buf++;
        }
-       
-       if (*buf) {
-               return -1;
+
+       /* write config now */
+       val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
+       if (is_5350) {
+               robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
+                       (1 << 20) /* valid */ | (c->untag << 6) | c->port);
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
        } else {
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
+                       (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
+       }
+
+       return 0;
+}
+
+#define set_switch(state) \
+       robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
+
+static int handle_enable_read(void *driver, char *buf, int nr)
+{
+       return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
+}
+
+static int handle_enable_write(void *driver, char *buf, int nr)
+{
+       set_switch(buf[0] == '1');
+
+       return 0;
+}
+
+static int handle_enable_vlan_read(void *driver, char *buf, int nr)
+{
+       return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
+}
+
+static int handle_enable_vlan_write(void *driver, char *buf, int nr)
+{
+       int disable = ((buf[0] != '1') ? 1 : 0);
+       
+       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
+               (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
+       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
+               (1 << 1) | (1 << 2) | (1 << 3) /* RSV multicast */);
+       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
+               (1 << 6) /* drop invalid VID frames */);
+       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
+               (1 << 3) /* drop miss V table frames */);
+
+       return 0;
+}
+
+static int handle_reset(void *driver, char *buf, int nr)
+{
+       switch_driver *d = (switch_driver *) driver;
+       switch_vlan_config *c = switch_parse_vlan(d, buf);
+       int j;
+       __u16 val16;
+       
+       if (c == NULL)
+               return -EINVAL;
+
+       /* disable switching */
+       set_switch(0);
+
+       /* reset vlans */
+       for (j = 0; j <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
                /* write config now */
-               val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
-               if (is_5350) {
-                       robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
-                               (1 << 20) /* valid */ | (untag << 6) | member);
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
-               } else {
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
-                               (1 << 14)  /* valid */ | (untag << 7) | member);
-                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
-               }
+               val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
+               if (is_5350)
+                       robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
+               else
+                       robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
+               robo_write16(ROBO_VLAN_PAGE, (is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 : ROBO_VLAN_TABLE_ACCESS), val16);
+       }
+
+       /* reset ports to a known good state */
+       for (j = 0; j < d->ports; j++) {
+               robo_write16(ROBO_CTRL_PAGE, port[j], 0x0000);
+               robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
        }
+
+       /* enable switching */
+       set_switch(1);
+
+       /* enable vlans */
+       handle_enable_vlan_write(driver, "1", 0);
+
        return 0;
 }
 
 static int __init robo_init()
 {
-       char *device = "ethX";
        int notfound = 1;
 
+       device = strdup("ethX");
        for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
                notfound = robo_probe(device);
        }
+       device[3]--;
        
-       if (notfound)
+       if (notfound) {
+               kfree(device);
                return -ENODEV;
-       else {
+       } else {
+               switch_config cfg[] = {
+                       {"enable", handle_enable_read, handle_enable_write},
+                       {"enable_vlan", handle_enable_vlan_read, handle_enable_vlan_write},
+                       {"reset", NULL, handle_reset},
+                       {NULL, NULL, NULL}
+               };
                switch_config vlan[] = {
                        {"ports", handle_vlan_port_read, handle_vlan_port_write},
                        {NULL, NULL, NULL}
                };
                switch_driver driver = {
                        name: DRIVER_NAME,
-                       ports: max_ports,
-                       vlans: max_vlans,
-                       driver_handlers: NULL,
+                       version: DRIVER_VERSION,
+                       interface: device,
+                       cpuport: 5,
+                       ports: 6,
+                       vlans: 16,
+                       driver_handlers: cfg,
                        port_handlers: NULL,
                        vlan_handlers: vlan,
                };
@@ -411,6 +467,7 @@ static int __init robo_init()
 static void __exit robo_exit()
 {
        switch_unregister_driver(DRIVER_NAME);
+       kfree(device);
 }
 
 
This page took 0.04853 seconds and 4 git commands to generate.