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.
21 * Basic doc of driver's /proc interface:
22 * /proc/switch/<interface>/
23 * registers: read-only
25 * reset: write causes hardware reset
26 * enable_vlan: "0", "1"
29 * media: "AUTO", "100FD", "100HD", "10FD", "10HD"
31 * ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <asm/uaccess.h>
37 #include <linux/proc_fs.h>
38 #include <linux/list.h>
40 #include "switch-core.h"
42 static int drv_num = 0;
43 static struct proc_dir_entry *switch_root;
44 switch_driver drivers;
47 struct list_head list;
48 struct proc_dir_entry *parent;
51 switch_config handler;
52 } switch_proc_handler;
55 struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
56 struct proc_dir_entry **ports, **vlans;
57 switch_proc_handler data;
61 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
62 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data);
64 static struct file_operations switch_proc_fops = {
65 .read = (ssize_t (*) (struct file *, char __user *, size_t, loff_t *))switch_proc_read,
66 .write = (ssize_t (*) (struct file *, const char __user *, size_t, loff_t *))switch_proc_write
69 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
72 struct inode *inode = file->f_dentry->d_inode;
73 struct proc_dir_entry *dent = inode->u.generic_ip;
75 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
80 if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
83 if (dent->data != NULL) {
84 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
85 if (handler->handler.read != NULL)
86 len += handler->handler.read(handler->driver, page + len, handler->nr);
91 len = min_t(int, len - *ppos, count);
92 if (copy_to_user(buf, (page + *ppos), len)) {
106 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
109 struct inode *inode = file->f_dentry->d_inode;
110 struct proc_dir_entry *dent = inode->u.generic_ip;
112 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
117 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
120 if (copy_from_user(page, buf, count)) {
126 if (dent->data != NULL) {
127 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
128 if (handler->handler.write != NULL) {
129 if ((ret = handler->handler.write(handler->driver, page, handler->nr)) >= 0)
138 static int handle_driver_name(void *driver, char *buf, int nr)
140 const char *name = ((switch_driver *) driver)->name;
141 return sprintf(buf, "%s\n", name);
144 static int handle_driver_version(void *driver, char *buf, int nr)
146 const char *version = ((switch_driver *) driver)->version;
147 strcpy(buf, version);
148 return sprintf(buf, "%s\n", version);
151 static void add_handler(switch_driver *driver, const switch_config *handler, struct proc_dir_entry *parent, int nr)
153 switch_priv *priv = (switch_priv *) driver->data;
154 struct proc_dir_entry *p;
157 switch_proc_handler *tmp;
158 tmp = (switch_proc_handler *) kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
161 INIT_LIST_HEAD(&tmp->list);
162 tmp->parent = parent;
164 tmp->driver = driver;
165 memcpy(&tmp->handler, handler, sizeof(switch_config));
166 list_add(&tmp->list, &priv->data.list);
169 if (handler->read != NULL) mode |= S_IRUSR;
170 if (handler->write != NULL) mode |= S_IWUSR;
172 if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
173 p->data = (void *) tmp;
174 p->proc_fops = &switch_proc_fops;
178 static inline void add_handlers(switch_driver *driver, const switch_config *handlers, struct proc_dir_entry *parent, int nr)
182 for (i = 0; handlers[i].name != NULL; i++) {
183 add_handler(driver, &(handlers[i]), parent, nr);
187 static void remove_handlers(switch_priv *priv)
189 struct list_head *pos, *q;
190 switch_proc_handler *tmp;
192 list_for_each_safe(pos, q, &priv->data.list) {
193 tmp = list_entry(pos, switch_proc_handler, list);
195 remove_proc_entry(tmp->handler.name, tmp->parent);
201 static void do_unregister(switch_driver *driver)
205 switch_priv *priv = (switch_priv *) driver->data;
207 remove_handlers(priv);
209 for(i = 0; priv->ports[i] != NULL; i++) {
210 sprintf(buf, "%d", i);
211 remove_proc_entry(buf, priv->port_dir);
214 remove_proc_entry("port", priv->driver_dir);
216 for(i = 0; priv->vlans[i] != NULL; i++) {
217 sprintf(buf, "%d", i);
218 remove_proc_entry(buf, priv->vlan_dir);
221 remove_proc_entry("vlan", priv->driver_dir);
223 remove_proc_entry(driver->interface, switch_root);
225 if (priv->nr == (drv_num - 1))
231 switch_config global_driver_handlers[] = {
232 {"driver", handle_driver_name, NULL},
233 {"version", handle_driver_version, NULL},
237 static int do_register(switch_driver *driver)
243 priv = kmalloc(sizeof(switch_priv), GFP_KERNEL);
246 driver->data = (void *) priv;
248 priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *),
254 priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *),
262 INIT_LIST_HEAD(&priv->data.list);
264 priv->nr = drv_num++;
265 priv->driver_dir = proc_mkdir(driver->interface, switch_root);
266 if (driver->driver_handlers != NULL) {
267 add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
268 add_handlers(driver, global_driver_handlers, priv->driver_dir, 0);
271 priv->port_dir = proc_mkdir("port", priv->driver_dir);
272 for (i = 0; i < driver->ports; i++) {
273 sprintf(buf, "%d", i);
274 priv->ports[i] = proc_mkdir(buf, priv->port_dir);
275 if (driver->port_handlers != NULL)
276 add_handlers(driver, driver->port_handlers, priv->ports[i], i);
278 priv->ports[i] = NULL;
280 priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
281 for (i = 0; i < driver->vlans; i++) {
282 sprintf(buf, "%d", i);
283 priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
284 if (driver->vlan_handlers != NULL)
285 add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
287 priv->vlans[i] = NULL;
293 static inline int isspace(char c) {
305 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
306 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
308 int switch_parse_media(char *buf)
312 *buf = toupper(*buf);
316 if (strncmp(str, "AUTO", 4) == 0)
317 return SWITCH_MEDIA_AUTO;
318 else if (strncmp(str, "100FD", 5) == 0)
319 return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
320 else if (strncmp(str, "100HD", 5) == 0)
321 return SWITCH_MEDIA_100;
322 else if (strncmp(str, "10FD", 4) == 0)
323 return SWITCH_MEDIA_FD;
324 else if (strncmp(str, "10HD", 4) == 0)
329 int switch_print_media(char *buf, int media)
333 if (media & SWITCH_MEDIA_AUTO)
334 len = sprintf(buf, "Auto");
335 else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
336 len = sprintf(buf, "100FD");
337 else if (media == SWITCH_MEDIA_100)
338 len = sprintf(buf, "100HD");
339 else if (media == SWITCH_MEDIA_FD)
340 len = sprintf(buf, "10FD");
342 len = sprintf(buf, "10HD");
344 len = sprintf(buf, "Invalid");
349 switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
351 switch_vlan_config *c;
354 c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
357 memset(c, 0, sizeof(switch_vlan_config));
359 while (isspace(*buf)) buf++;
361 while (*buf >= '0' && *buf <= '9') {
365 u = ((j == driver->cpuport) ? 0 : 1);
367 s = !(*buf >= '0' && *buf <= '9');
370 while (s && !isspace(*buf) && (*buf != 0)) {
386 c->untag |= (1 << j);
393 while (isspace(*buf)) buf++;
395 if (*buf != 0) return NULL;
397 c->port &= (1 << driver->ports) - 1;
398 c->untag &= (1 << driver->ports) - 1;
399 c->pvid &= (1 << driver->ports) - 1;
405 int switch_device_registered (char* device) {
406 struct list_head *pos;
409 list_for_each(pos, &drivers.list) {
410 if (strcmp(list_entry(pos, switch_driver, list)->interface, device) == 0) {
411 printk("There is already a switch registered on the device '%s'\n", device);
420 int switch_register_driver(switch_driver *driver)
422 struct list_head *pos;
426 list_for_each(pos, &drivers.list) {
427 if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
428 printk("Switch driver '%s' already exists in the kernel\n", driver->name);
431 if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
432 printk("There is already a switch registered on the device '%s'\n", driver->interface);
437 new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
440 memcpy(new, driver, sizeof(switch_driver));
441 new->name = strdup(driver->name);
442 new->interface = strdup(driver->interface);
444 if ((ret = do_register(new)) < 0) {
449 INIT_LIST_HEAD(&new->list);
450 list_add(&new->list, &drivers.list);
455 void switch_unregister_driver(char *name) {
456 struct list_head *pos, *q;
459 list_for_each_safe(pos, q, &drivers.list) {
460 tmp = list_entry(pos, switch_driver, list);
461 if (strcmp(tmp->name, name) == 0) {
472 static int __init switch_init(void)
474 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
475 printk("%s: proc_mkdir failed.\n", __FILE__);
479 INIT_LIST_HEAD(&drivers.list);
484 static void __exit switch_exit(void)
486 remove_proc_entry("switch", NULL);
489 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
490 MODULE_LICENSE("GPL");
492 EXPORT_SYMBOL(switch_device_registered);
493 EXPORT_SYMBOL(switch_register_driver);
494 EXPORT_SYMBOL(switch_unregister_driver);
495 EXPORT_SYMBOL(switch_parse_vlan);
496 EXPORT_SYMBOL(switch_parse_media);
497 EXPORT_SYMBOL(switch_print_media);
499 module_init(switch_init);
500 module_exit(switch_exit);