aboutsummaryrefslogtreecommitdiffstats
path: root/package/switch/src/switch-core.c
blob: c34386b76f69b489c1ff8bb053151b8ee5e3358c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#ifndef _I386_PAGE_H
#define _I386_PAGE_H


#ifndef __ASSEMBLY__
#define BUG() do {					\
	printk("BUG at %s:%d\n", __FILE__, __LINE__);	\
	__asm__ __volatile__("ud2");			\
} while (0)
#endif /* __ASSEMBLY__ */


#define L1_PAGETABLE_SHIFT       12
#define L2_PAGETABLE_SHIFT       22

#define ENTRIES_PER_L1_PAGETABLE 1024
#define ENTRIES_PER_L2_PAGETABLE 1024

#define PAGE_SHIFT               L1_PAGETABLE_SHIFT
#define PAGE_SIZE	         (1UL << PAGE_SHIFT)
#define PAGE_MASK	         (~(PAGE_SIZE-1))

#define clear_page(_p)           memset((void *)(_p), 0, PAGE_SIZE)
#define copy_page(_t,_f)         memcpy((void *)(_t), (void *)(_f), PAGE_SIZE)

#ifndef __ASSEMBLY__
#include <xeno/config.h>
typedef struct { unsigned long l1_lo; } l1_pgentry_t;
typedef struct { unsigned long l2_lo; } l2_pgentry_t;
typedef l1_pgentry_t *l1_pagetable_t;
typedef l2_pgentry_t *l2_pagetable_t;
typedef struct { unsigned long pt_lo; } pagetable_t;
#endif /* !__ASSEMBLY__ */

/* Strip type from a table entry. */
#define l1_pgentry_val(_x) ((_x).l1_lo)
#define l2_pgentry_val(_x) ((_x).l2_lo)
#define pagetable_val(_x)  ((_x).pt_lo)

#define alloc_l1_pagetable()  ((l1_pgentry_t *)get_free_page(GFP_KERNEL))
#define alloc_l2_pagetable()  ((l2_pgentry_t *)get_free_page(GFP_KERNEL))

/* Add type to a table entry. */
#define mk_l1_pgentry(_x)  ( (l1_pgentry_t) { (_x) } )
#define mk_l2_pgentry(_x)  ( (l2_pgentry_t) { (_x) } )
#define mk_pagetable(_x)   ( (pagetable_t) { (_x) } )

/* Turn a typed table entry into a page index. */
#define l1_pgentry_to_pagenr(_x) (l1_pgentry_val(_x) >> PAGE_SHIFT) 
#define l2_pgentry_to_pagenr(_x) (l2_pgentry_val(_x) >> PAGE_SHIFT)

/* Turn a typed table entry into a physical address. */
#define l1_pgentry_to_phys(_x) (l1_pgentry_val(_x) & PAGE_MASK)
#define l2_pgentry_to_phys(_x) (l2_pgentry_val(_x) & PAGE_MASK)

/* Dereference a typed level-2 entry to yield a typed level-1 table. */
#define l2_pgentry_to_l1(_x)     \
  ((l1_pgentry_t *)__va(l2_pgentry_val(_x) & PAGE_MASK))

/* Given a virtual address, get an entry offset into a page table. */
#define l1_table_offset(_a) \
  (((_a) >> L1_PAGETABLE_SHIFT) & (ENTRIES_PER_L1_PAGETABLE - 1))
#define l2_table_offset(_a) \
  ((_a) >> L2_PAGETABLE_SHIFT)

/* Hypervisor table entries use zero to sugnify 'empty'. */
#define l1_pgentry_empty(_x) (!l1_pgentry_val(_x))
#define l2_pgentry_empty(_x) (!l2_pgentry_val(_x))

#define __PAGE_OFFSET		(0xFC400000)
#define PAGE_OFFSET		((unsigned long)__PAGE_OFFSET)
#define __pa(x)			((unsigned long)(x)-PAGE_OFFSET)
#define __va(x)			((void *)((unsigned long)(x)+PAGE_OFFSET))
#define page_address(_p)        (__va(((_p) - frame_table) << PAGE_SHIFT))
#define virt_to_page(kaddr)	(frame_table + (__pa(kaddr) >> PAGE_SHIFT))
#define VALID_PAGE(page)	((page - frame_table) < max_mapnr)

/* High table entries are reserved by the hypervisor. */
#define DOMAIN_ENTRIES_PER_L2_PAGETABLE	    \
  (HYPERVISOR_VIRT_START >> L2_PAGETABLE_SHIFT)
#define HYPERVISOR_ENTRIES_PER_L2_PAGETABLE \
  (ENTRIES_PER_L2_PAGETABLE - DOMAIN_ENTRIES_PER_L2_PAGETABLE)

#ifndef __ASSEMBLY__
#include <asm/processor.h>
#include <asm/fixmap.h>
#include <asm/bitops.h&
/*
 * switch-core.c
 *
 * Copyright (C) 2005 Felix Fietkau <openwrt@nbd.name>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * 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>/
 *   registers:              read-only
 *   counters:               read-only
 *   reset:                  write causes hardware reset
 *   enable_vlan:            "0", "1"
 *   port/<port-number>/
 *     enabled:              "0", "1"
 *     media:                "AUTO", "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>
#include <linux/proc_fs.h>
#include <linux/list.h>

#include "switch-core.h"

static int drv_num = 0;
static struct proc_dir_entry *switch_root;
switch_driver drivers;

typedef struct {
	struct list_head list;
	struct proc_dir_entry *parent;
	int nr;
	void *driver;
	switch_config handler;
} switch_proc_handler;

typedef struct {
	struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
	struct proc_dir_entry **ports, **vlans;
	switch_proc_handler data;
	int nr;
} switch_priv;

static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data);

static struct file_operations switch_proc_fops = {
	.read = (ssize_t (*) (struct file *, char __user *, size_t, loff_t *))switch_proc_read,
	.write = (ssize_t (*) (struct file *, const char __user *, size_t, loff_t *))switch_proc_write
};

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)
			len += handler->handler.read(handler->driver, page + len, handler->nr);
	}
	len += 1;

	if (*ppos < len) {
		len = min_t(int, len - *ppos, count);
		if (copy_to_user(buf, (page + *ppos), len)) {
			kfree(page);
			return -EFAULT;
		}
		*ppos += len;
	} else {
		len = 0;
	}

	kfree(page);
	return len;
}


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;

	if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
		return -ENOBUFS;

	if (copy_from_user(page, buf, count)) {
		kfree(page);
		return -EINVAL;
	}
	page[count] = 0;
	
	if (dent->data != NULL) {
		switch_proc_handler *handler = (switch_proc_handler *) dent->data;
		if (handler->handler.write != NULL) {
			if ((ret = handler->handler.write(handler->driver, page, handler->nr)) >= 0)
				ret = count;
		}
	}

	kfree(page);
	return ret;
}

static int handle_driver_name(void *driver, char *buf, int nr)
{
	const char *name = ((switch_driver *) driver)->name;
	return sprintf(buf, "%s\n", name);
}

static int handle_driver_version(void *driver, char *buf, int nr)
{
	const char *version = ((switch_driver *) driver)->version;
	strcpy(buf, version);
	return sprintf(buf, "%s\n", version);
}

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;
	struct proc_dir_entry *p;
	int mode;

	switch_proc_handler *tmp;
	tmp = (switch_proc_handler *) kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
	if (!tmp)
		return;
	INIT_LIST_HEAD(&tmp->list);
	tmp->parent = parent;
	tmp->nr = nr;
	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;
	}
}

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)
{
	struct list_head *pos, *q;
	switch_proc_handler *tmp;

	list_for_each_safe(pos, q, &priv->data.list) {
		tmp = list_entry(pos, switch_proc_handler, list);
		list_del(pos);
		remove_proc_entry(tmp->handler.name, tmp->parent);
		kfree(tmp);
	}
}


static void do_unregister(switch_driver *driver)
{
	char buf[4];
	int i;
	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);
	}
	kfree(priv->ports);
	remove_proc_entry("port", priv->driver_dir);

	for(i = 0; priv->vlans[i] != NULL; i++) {
		sprintf(buf, "%d", i);
		remove_proc_entry(buf, priv->vlan_dir);
	}
	kfree(priv->vlans);
	remove_proc_entry("vlan", priv->driver_dir);

	remove_proc_entry(driver->interface, switch_root);
			
	if (priv->nr == (drv_num - 1))
		drv_num--;

	kfree(priv);
}

switch_config global_driver_handlers[] = {
	{"driver", handle_driver_name, NULL},
	{"version", handle_driver_version, NULL},
	{NULL, NULL, NULL}
};

static int do_register(switch_driver *driver)
{
	switch_priv *priv;
	int i;
	char buf[4];

	priv = kmalloc(sizeof(switch_priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	driver->data = (void *) priv;

	priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *),
			      GFP_KERNEL);
	if (!priv->ports) {
		kfree(priv);
		return -ENOMEM;
	}
	priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *),
			      GFP_KERNEL);
	if (!priv->vlans) {
		kfree(priv->ports);
		kfree(priv);
		return -ENOMEM;
	}

	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);
		priv->ports[i] = proc_mkdir(buf, priv->port_dir);
		if (driver->port_handlers != NULL)
			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);
		priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
		if (driver->vlan_handlers != NULL)
			add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
	}
	priv->vlans[i] = NULL;
	

	return 0;
}

static inline int isspace(char c) {
	switch(c) {
		case ' ':
		case 0x09:
		case 0x0a:
		case 0x0d:
			return 1;
		default:
			return 0;
	}
}

#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;
	while (*buf != 0) {
		*buf = toupper(*buf);
		buf++;
	}

	if (strncmp(str, "AUTO", 4) == 0)
		return SWITCH_MEDIA_AUTO;
	else if (strncmp(str, "100FD", 5) == 0)
		return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
	else if (strncmp(str, "100HD", 5) == 0)
		return SWITCH_MEDIA_100;
	else if (strncmp(str, "10FD", 4) == 0)
		return SWITCH_MEDIA_FD;
	else if (strncmp(str, "10HD", 4) == 0)
		return 0;
	else return -1;
}

int switch_print_media(char *buf, int media)
{
	int len = 0;

	if (media & SWITCH_MEDIA_AUTO)
		len = sprintf(buf, "Auto");
	else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
		len = sprintf(buf, "100FD");
	else if (media == SWITCH_MEDIA_100)
		len = sprintf(buf,  "100HD");
	else if (media == SWITCH_MEDIA_FD)
		len = sprintf(buf,  "10FD");
	else if (media == 0)
		len = sprintf(buf,  "10HD");
	else
		len = sprintf(buf, "Invalid");

	return len;
}

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);
	if (!c)
		return NULL;
	memset(c, 0, sizeof(switch_vlan_config));

	while (isspace(*buf)) buf++;
	j = 0;
	while (*buf >= '0' && *buf <= '9') {
		j *= 10;
		j += *buf++ - '0';

		u = ((j == driver->cpuport) ? 0 : 1);
		p = 0;
		s = !(*buf >= '0' && *buf <= '9');
	
		if (s) {
			while (s && !isspace(*buf) && (*buf != 0)) {
				switch(*buf) {
					case 'u':
						u = 1;
						break;
					case 't':
						u = 0;
						break;
					case '*':
						p = 1;
						break;
				}
				buf++;
			}
			c->port |= (1 << j);
			if (u)
				c->untag |= (1 << j);
			if (p)
				c->pvid |= (1 << j);

			j = 0;
		}
		
		while (isspace(*buf)) buf++;
	}
	if (*buf != 0) 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;
	switch_driver *new;

	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;
	switch_driver *new;
	int ret;

	list_for_each(pos, &drivers.list) {
		if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
			printk("Switch driver '%s' already exists in the kernel\n", driver->name);
			return -EINVAL;
		}
		if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
			printk("There is already a switch registered on the device '%s'\n", driver->interface);
			return -EINVAL;
		}
	}

	new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
	if (!new)
		return -ENOMEM;
	memcpy(new, driver, sizeof(switch_driver));
	new->name = strdup(driver->name);
	new->interface = strdup(driver->interface);

	if ((ret = do_register(new)) < 0) {
		kfree(new->name);
		kfree(new);
		return ret;
	}
	INIT_LIST_HEAD(&new->list);
	list_add(&new->list, &drivers.list);

	return 0;
}

void switch_unregister_driver(char *name) {
	struct list_head *pos, *q;
	switch_driver *tmp;

	list_for_each_safe(pos, q, &drivers.list) {
		tmp = list_entry(pos, switch_driver, list);
		if (strcmp(tmp->name, name) == 0) {
			do_unregister(tmp);
			list_del(pos);
			kfree(tmp->name);
			kfree(tmp);

			return;
		}
	}
}

static int __init switch_init(void)
{
	if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
		printk("%s: proc_mkdir failed.\n", __FILE__);
		return -ENODEV;
	}

	INIT_LIST_HEAD(&drivers.list);
	
	return 0;
}

static void __exit switch_exit(void)
{
	remove_proc_entry("switch", NULL);
}

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);
EXPORT_SYMBOL(switch_parse_media);
EXPORT_SYMBOL(switch_print_media);

module_init(switch_init);
module_exit(switch_exit);