4 * Copyright (C) 2005 Felix Fietkau <openwrt@nbd.name>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <asm/uaccess.h>
27 #include <linux/proc_fs.h>
28 #include <linux/list.h>
30 #include "switch-core.h"
32 static int drv_num = 0;
33 static struct proc_dir_entry *switch_root;
34 switch_driver drivers;
37 struct list_head list;
38 struct proc_dir_entry *parent;
41 switch_config handler;
42 } switch_proc_handler;
45 struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
46 struct proc_dir_entry **ports, **vlans;
47 switch_proc_handler data;
51 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
52 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data);
54 static struct file_operations switch_proc_fops = {
55 read: switch_proc_read,
56 write: switch_proc_write
59 static inline char *strdup(char *str)
61 char *new = kmalloc(strlen(str) + 1, GFP_KERNEL);
66 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
69 struct inode *inode = file->f_dentry->d_inode;
70 struct proc_dir_entry *dent = inode->u.generic_ip;
72 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
77 if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
80 if (dent->data != NULL) {
81 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
82 if (handler->handler.read != NULL)
83 len += handler->handler.read(handler->driver, page + len, handler->nr);
88 len = min_t(int, len - *ppos, count);
89 if (copy_to_user(buf, (page + *ppos), len)) {
102 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
105 struct inode *inode = file->f_dentry->d_inode;
106 struct proc_dir_entry *dent = inode->u.generic_ip;
108 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
113 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
116 if (copy_from_user(page, buf, count)) {
122 if (dent->data != NULL) {
123 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
124 if (handler->handler.write != NULL) {
125 if ((ret = handler->handler.write(handler->driver, page, handler->nr)) >= 0)
134 static int handle_driver_name(void *driver, char *buf, int nr)
136 char *name = ((switch_driver *) driver)->name;
137 return sprintf(buf, "%s\n", name);
140 static int handle_driver_version(void *driver, char *buf, int nr)
142 char *version = ((switch_driver *) driver)->version;
143 strcpy(buf, version);
144 return sprintf(buf, "%s\n", version);
147 static void add_handler(switch_driver *driver, switch_config *handler, struct proc_dir_entry *parent, int nr)
149 switch_priv *priv = (switch_priv *) driver->data;
150 struct proc_dir_entry *p;
153 switch_proc_handler *tmp;
154 tmp = (switch_proc_handler *) kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
155 INIT_LIST_HEAD(&tmp->list);
156 tmp->parent = parent;
158 tmp->driver = driver;
159 memcpy(&tmp->handler, handler, sizeof(switch_config));
160 list_add(&tmp->list, &priv->data.list);
163 if (handler->read != NULL) mode |= S_IRUSR;
164 if (handler->write != NULL) mode |= S_IWUSR;
166 if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
167 p->data = (void *) tmp;
168 p->proc_fops = &switch_proc_fops;
172 static inline void add_handlers(switch_driver *driver, switch_config *handlers, struct proc_dir_entry *parent, int nr)
176 for (i = 0; handlers[i].name != NULL; i++) {
177 add_handler(driver, &(handlers[i]), parent, nr);
181 static void remove_handlers(switch_priv *priv)
183 struct list_head *pos, *q;
184 switch_proc_handler *tmp;
186 list_for_each_safe(pos, q, &priv->data.list) {
187 tmp = list_entry(pos, switch_proc_handler, list);
189 remove_proc_entry(tmp->handler.name, tmp->parent);
195 static void do_unregister(switch_driver *driver)
199 switch_priv *priv = (switch_priv *) driver->data;
201 remove_handlers(priv);
203 for(i = 0; priv->ports[i] != NULL; i++) {
204 sprintf(buf, "%d", i);
205 remove_proc_entry(buf, priv->port_dir);
208 remove_proc_entry("port", priv->driver_dir);
210 for(i = 0; priv->vlans[i] != NULL; i++) {
211 sprintf(buf, "%d", i);
212 remove_proc_entry(buf, priv->vlan_dir);
215 remove_proc_entry("vlan", priv->driver_dir);
217 remove_proc_entry(driver->interface, switch_root);
219 if (priv->nr == (drv_num - 1))
225 switch_config global_driver_handlers[] = {
226 {"driver", handle_driver_name, NULL},
227 {"version", handle_driver_version, NULL},
231 static int do_register(switch_driver *driver)
237 if ((priv = kmalloc(sizeof(switch_priv), GFP_KERNEL)) == NULL)
239 driver->data = (void *) priv;
241 INIT_LIST_HEAD(&priv->data.list);
243 priv->nr = drv_num++;
244 priv->driver_dir = proc_mkdir(driver->interface, switch_root);
245 if (driver->driver_handlers != NULL) {
246 add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
247 add_handlers(driver, global_driver_handlers, priv->driver_dir, 0);
250 priv->port_dir = proc_mkdir("port", priv->driver_dir);
251 priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
252 for (i = 0; i < driver->ports; i++) {
253 sprintf(buf, "%d", i);
254 priv->ports[i] = proc_mkdir(buf, priv->port_dir);
255 if (driver->port_handlers != NULL)
256 add_handlers(driver, driver->port_handlers, priv->ports[i], i);
258 priv->ports[i] = NULL;
260 priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
261 priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
262 for (i = 0; i < driver->vlans; i++) {
263 sprintf(buf, "%d", i);
264 priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
265 if (driver->vlan_handlers != NULL)
266 add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
268 priv->vlans[i] = NULL;
274 static inline int isspace(char c) {
286 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
287 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
289 int switch_parse_media(char *buf)
293 *buf = toupper(*buf);
297 if (strncmp(str, "AUTO", 4) == 0)
298 return SWITCH_MEDIA_AUTO;
299 else if (strncmp(str, "100FD", 5) == 0)
300 return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
301 else if (strncmp(str, "100HD", 5) == 0)
302 return SWITCH_MEDIA_100;
303 else if (strncmp(str, "10FD", 4) == 0)
304 return SWITCH_MEDIA_FD;
305 else if (strncmp(str, "10HD", 4) == 0)
310 int switch_print_media(char *buf, int media)
314 if (media & SWITCH_MEDIA_AUTO)
315 len = sprintf(buf, "Auto");
316 else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
317 len = sprintf(buf, "100FD");
318 else if (media == SWITCH_MEDIA_100)
319 len = sprintf(buf, "100HD");
320 else if (media == SWITCH_MEDIA_FD)
321 len = sprintf(buf, "10FD");
323 len = sprintf(buf, "10HD");
325 len = sprintf(buf, "Invalid");
330 switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
332 switch_vlan_config *c;
335 c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
336 memset(c, 0, sizeof(switch_vlan_config));
338 while (isspace(*buf)) buf++;
340 while (*buf >= '0' && *buf <= '9') {
344 u = ((j == driver->cpuport) ? 0 : 1);
346 s = !(*buf >= '0' && *buf <= '9');
349 while (s && !isspace(*buf) && (*buf != 0)) {
365 c->untag |= (1 << j);
372 while (isspace(*buf)) buf++;
374 if (*buf != 0) return NULL;
376 c->port &= (1 << driver->ports) - 1;
377 c->untag &= (1 << driver->ports) - 1;
378 c->pvid &= (1 << driver->ports) - 1;
384 int switch_register_driver(switch_driver *driver)
386 struct list_head *pos;
390 list_for_each(pos, &drivers.list) {
391 if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
392 printk("Switch driver '%s' already exists in the kernel\n", driver->name);
395 if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
396 printk("There is already a switch registered on the device '%s'\n", driver->interface);
401 new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
402 memcpy(new, driver, sizeof(switch_driver));
403 new->name = strdup(driver->name);
404 new->interface = strdup(driver->interface);
406 if ((ret = do_register(new)) < 0) {
411 INIT_LIST_HEAD(&new->list);
412 list_add(&new->list, &drivers.list);
417 void switch_unregister_driver(char *name) {
418 struct list_head *pos, *q;
421 list_for_each_safe(pos, q, &drivers.list) {
422 tmp = list_entry(pos, switch_driver, list);
423 if (strcmp(tmp->name, name) == 0) {
434 static int __init switch_init()
436 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
437 printk("%s: proc_mkdir failed.\n", __FILE__);
441 INIT_LIST_HEAD(&drivers.list);
446 static void __exit switch_exit()
448 remove_proc_entry("vlan", NULL);
451 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
452 MODULE_LICENSE("GPL");
454 EXPORT_SYMBOL(switch_register_driver);
455 EXPORT_SYMBOL(switch_unregister_driver);
456 EXPORT_SYMBOL(switch_parse_vlan);
457 EXPORT_SYMBOL(switch_parse_media);
458 EXPORT_SYMBOL(switch_print_media);
460 module_init(switch_init);
461 module_exit(switch_exit);