lantiq: Tune the XWAY subtarget cflags
[openwrt/staging/yousong.git] / package / switch / src / switch-core.c
index 87aa9729ccbd05c7e82063e8034c3edcac3507de..fc8a6bf2c76df72863d5c9f3f32f90a495013413 100644 (file)
@@ -17,7 +17,6 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
- * $Id: $
  *
  * Basic doc of driver's /proc interface:
  * /proc/switch/<interface>/
  *   enable_vlan:            "0", "1"
  *   port/<port-number>/
  *     enabled:              "0", "1"
- *     media:                "AUTO", "100FD", "100HD", "10FD", "10HD"
+ *     media:                "AUTO", "1000FD", "1000HD", "100FD", "100HD", "10FD", "10HD"
  *   vlan/<port-number>/
  *     ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
  */
 
-#include <linux/autoconf.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <asm/uaccess.h>
@@ -70,18 +68,13 @@ static struct file_operations switch_proc_fops = {
 
 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
 {
-#ifdef LINUX_2_4
-       struct inode *inode = file->f_dentry->d_inode;
-       struct proc_dir_entry *dent = inode->u.generic_ip;
-#else
        struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
-#endif
        char *page;
        int len = 0;
-       
+
        if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
                return -ENOBUFS;
-       
+
        if (dent->data != NULL) {
                switch_proc_handler *handler = (switch_proc_handler *) dent->data;
                if (handler->handler.read != NULL)
@@ -107,12 +100,7 @@ static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff
 
 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
 {
-#ifdef LINUX_2_4
-       struct inode *inode = file->f_dentry->d_inode;
-       struct proc_dir_entry *dent = inode->u.generic_ip;
-#else
        struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
-#endif
        char *page;
        int ret = -EINVAL;
 
@@ -124,7 +112,7 @@ static ssize_t switch_proc_write(struct file *file, const char *buf, size_t coun
                return -EINVAL;
        }
        page[count] = 0;
-       
+
        if (dent->data != NULL) {
                switch_proc_handler *handler = (switch_proc_handler *) dent->data;
                if (handler->handler.write != NULL) {
@@ -150,6 +138,30 @@ static int handle_driver_version(void *driver, char *buf, int nr)
        return sprintf(buf, "%s\n", version);
 }
 
+static int handle_driver_cpuport(void *driver, char *buf, int nr)
+{
+       int cpuport = ((switch_driver *) driver)->cpuport;
+       return sprintf(buf, "%i\n", cpuport);
+}
+
+static int handle_driver_ports(void *driver, char *buf, int nr)
+{
+       int ports = ((switch_driver *) driver)->ports;
+       return sprintf(buf, "%i\n", ports);
+}
+
+static int handle_driver_vlans(void *driver, char *buf, int nr)
+{
+       int vlans = ((switch_driver *) driver)->vlans;
+       return sprintf(buf, "%i\n", vlans);
+}
+
+static int handle_driver_dev_name(void *driver, char *buf, int nr)
+{
+       char *dev_name = ((switch_driver *) driver)->dev_name;
+       return sprintf(buf, "%s\n", dev_name);
+}
+
 static void add_handler(switch_driver *driver, const switch_config *handler, struct proc_dir_entry *parent, int nr)
 {
        switch_priv *priv = (switch_priv *) driver->data;
@@ -166,11 +178,11 @@ static void add_handler(switch_driver *driver, const switch_config *handler, str
        tmp->driver = driver;
        memcpy(&tmp->handler, handler, sizeof(switch_config));
        list_add(&tmp->list, &priv->data.list);
-       
+
        mode = 0;
        if (handler->read != NULL) mode |= S_IRUSR;
        if (handler->write != NULL) mode |= S_IWUSR;
-       
+
        if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
                p->data = (void *) tmp;
                p->proc_fops = &switch_proc_fops;
@@ -180,11 +192,11 @@ static void add_handler(switch_driver *driver, const switch_config *handler, str
 static inline void add_handlers(switch_driver *driver, const switch_config *handlers, struct proc_dir_entry *parent, int nr)
 {
        int i;
-       
+
        for (i = 0; handlers[i].name != NULL; i++) {
                add_handler(driver, &(handlers[i]), parent, nr);
        }
-}              
+}
 
 static void remove_handlers(switch_priv *priv)
 {
@@ -207,7 +219,7 @@ static void do_unregister(switch_driver *driver)
        switch_priv *priv = (switch_priv *) driver->data;
 
        remove_handlers(priv);
-       
+
        for(i = 0; priv->ports[i] != NULL; i++) {
                sprintf(buf, "%d", i);
                remove_proc_entry(buf, priv->port_dir);
@@ -223,7 +235,7 @@ static void do_unregister(switch_driver *driver)
        remove_proc_entry("vlan", priv->driver_dir);
 
        remove_proc_entry(driver->interface, switch_root);
-                       
+
        if (priv->nr == (drv_num - 1))
                drv_num--;
 
@@ -233,6 +245,10 @@ static void do_unregister(switch_driver *driver)
 switch_config global_driver_handlers[] = {
        {"driver", handle_driver_name, NULL},
        {"version", handle_driver_version, NULL},
+       {"cpuport", handle_driver_cpuport, NULL},
+       {"ports", handle_driver_ports, NULL},
+       {"vlans", handle_driver_vlans, NULL},
+       {"dev_name", handle_driver_dev_name, NULL},
        {NULL, NULL, NULL}
 };
 
@@ -262,14 +278,14 @@ static int do_register(switch_driver *driver)
        }
 
        INIT_LIST_HEAD(&priv->data.list);
-       
+
        priv->nr = drv_num++;
        priv->driver_dir = proc_mkdir(driver->interface, switch_root);
        if (driver->driver_handlers != NULL) {
                add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
                add_handlers(driver, global_driver_handlers, priv->driver_dir, 0);
        }
-       
+
        priv->port_dir = proc_mkdir("port", priv->driver_dir);
        for (i = 0; i < driver->ports; i++) {
                sprintf(buf, "%d", i);
@@ -278,7 +294,7 @@ static int do_register(switch_driver *driver)
                        add_handlers(driver, driver->port_handlers, priv->ports[i], i);
        }
        priv->ports[i] = NULL;
-       
+
        priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
        for (i = 0; i < driver->vlans; i++) {
                sprintf(buf, "%d", i);
@@ -287,7 +303,7 @@ static int do_register(switch_driver *driver)
                        add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
        }
        priv->vlans[i] = NULL;
-       
+
 
        return 0;
 }
@@ -306,7 +322,7 @@ static inline int isspace(char c) {
 
 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
-                                                                                                                
+
 int switch_parse_media(char *buf)
 {
        char *str = buf;
@@ -317,6 +333,10 @@ int switch_parse_media(char *buf)
 
        if (strncmp(str, "AUTO", 4) == 0)
                return SWITCH_MEDIA_AUTO;
+       else if (strncmp(str, "1000FD", 6) == 0)
+               return SWITCH_MEDIA_1000 | SWITCH_MEDIA_FD;
+       else if (strncmp(str, "1000HD", 6) == 0)
+               return SWITCH_MEDIA_1000;
        else if (strncmp(str, "100FD", 5) == 0)
                return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
        else if (strncmp(str, "100HD", 5) == 0)
@@ -334,14 +354,18 @@ int switch_print_media(char *buf, int media)
 
        if (media & SWITCH_MEDIA_AUTO)
                len = sprintf(buf, "Auto");
+       else if (media == (SWITCH_MEDIA_1000 | SWITCH_MEDIA_FD))
+               len = sprintf(buf, "1000FD");
+       else if (media == SWITCH_MEDIA_1000)
+               len = sprintf(buf, "1000HD");
        else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
                len = sprintf(buf, "100FD");
        else if (media == SWITCH_MEDIA_100)
-               len = sprintf(buf,  "100HD");
+               len = sprintf(buf, "100HD");
        else if (media == SWITCH_MEDIA_FD)
-               len = sprintf(buf,  "10FD");
+               len = sprintf(buf, "10FD");
        else if (media == 0)
-               len = sprintf(buf,  "10HD");
+               len = sprintf(buf, "10HD");
        else
                len = sprintf(buf, "Invalid");
 
@@ -352,11 +376,10 @@ switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
 {
        switch_vlan_config *c;
        int j, u, p, s;
-       
-       c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
+
+       c = kzalloc(sizeof(switch_vlan_config), GFP_KERNEL);
        if (!c)
                return NULL;
-       memset(c, 0, sizeof(switch_vlan_config));
 
        while (isspace(*buf)) buf++;
        j = 0;
@@ -367,7 +390,7 @@ switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
                u = ((j == driver->cpuport) ? 0 : 1);
                p = 0;
                s = !(*buf >= '0' && *buf <= '9');
-       
+
                if (s) {
                        while (s && !isspace(*buf) && (*buf != 0)) {
                                switch(*buf) {
@@ -391,19 +414,36 @@ switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
 
                        j = 0;
                }
-               
+
                while (isspace(*buf)) buf++;
        }
-       if (*buf != 0) return NULL;
+       if (*buf != 0) {
+               kfree(c);
+               return NULL;
+       }
 
        c->port &= (1 << driver->ports) - 1;
        c->untag &= (1 << driver->ports) - 1;
        c->pvid &= (1 << driver->ports) - 1;
-       
+
        return c;
 }
 
 
+int switch_device_registered (char* device) {
+       struct list_head *pos;
+
+       list_for_each(pos, &drivers.list) {
+               if (strcmp(list_entry(pos, switch_driver, list)->interface, device) == 0) {
+                       printk("There is already a switch registered on the device '%s'\n", device);
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
+
 int switch_register_driver(switch_driver *driver)
 {
        struct list_head *pos;
@@ -464,7 +504,7 @@ static int __init switch_init(void)
        }
 
        INIT_LIST_HEAD(&drivers.list);
-       
+
        return 0;
 }
 
@@ -476,6 +516,7 @@ static void __exit switch_exit(void)
 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
 MODULE_LICENSE("GPL");
 
+EXPORT_SYMBOL(switch_device_registered);
 EXPORT_SYMBOL(switch_register_driver);
 EXPORT_SYMBOL(switch_unregister_driver);
 EXPORT_SYMBOL(switch_parse_vlan);