first try to integrate nbds great new switch drivers for 2.4/2.6 with full vlan suppo...
[openwrt/svn-archive/archive.git] / openwrt / target / linux / package / switch / src / switch-core.c
1 /*
2 * switch-core.c
3 *
4 * Copyright (C) 2005 Felix Fietkau <openwrt@nbd.name>
5 *
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.
10 *
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.
15 *
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.
19 *
20 * $Id: $
21 */
22
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>
29
30 #include "switch-core.h"
31
32 static int drv_num = 0;
33 static struct proc_dir_entry *switch_root;
34 switch_driver drivers;
35
36 typedef struct {
37 struct list_head list;
38 struct proc_dir_entry *parent;
39 int nr;
40 switch_config handler;
41 } switch_proc_handler;
42
43 typedef struct {
44 struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
45 struct proc_dir_entry **ports, **vlans;
46 switch_proc_handler data;
47 int nr;
48 } switch_priv;
49
50
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);
53
54 static struct file_operations switch_proc_fops = {
55 read: switch_proc_read,
56 write: switch_proc_write
57 };
58
59 static char *strdup(char *str)
60 {
61 char *new = kmalloc(strlen(str) + 1, GFP_KERNEL);
62 strcpy(new, str);
63 return new;
64 }
65
66 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
67 {
68 #ifdef LINUX_2_4
69 struct inode *inode = file->f_dentry->d_inode;
70 struct proc_dir_entry *dent = inode->u.generic_ip;
71 #else
72 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
73 #endif
74 char *page;
75 int len = 0;
76
77 if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
78 return -ENOBUFS;
79
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(page + len, handler->nr);
84 }
85 len += 1;
86
87 if (*ppos < len) {
88 len = min_t(int, len - *ppos, count);
89 if (copy_to_user(buf, (page + *ppos), len)) {
90 kfree(page);
91 return -EFAULT;
92 }
93 *ppos += len;
94 } else {
95 len = 0;
96 }
97
98 return len;
99 }
100
101
102 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
103 {
104 #ifdef LINUX_2_4
105 struct inode *inode = file->f_dentry->d_inode;
106 struct proc_dir_entry *dent = inode->u.generic_ip;
107 #else
108 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
109 #endif
110 char *page;
111 int ret = -EINVAL;
112
113 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
114 return -ENOBUFS;
115
116 if (copy_from_user(page, buf, count)) {
117 kfree(page);
118 return -EINVAL;
119 }
120 page[count] = 0;
121
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(page, handler->nr)) >= 0)
126 ret = count;
127 }
128 }
129
130 kfree(page);
131 return ret;
132 }
133
134 static void add_handlers(switch_priv *priv, switch_config *handlers, struct proc_dir_entry *parent, int nr)
135 {
136 switch_proc_handler *tmp;
137 int i, mode;
138 struct proc_dir_entry *p;
139
140 for (i = 0; handlers[i].name != NULL; i++) {
141 tmp = kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
142 INIT_LIST_HEAD(&tmp->list);
143 tmp->parent = parent;
144 tmp->nr = nr;
145 memcpy(&tmp->handler, &(handlers[i]), sizeof(switch_config));
146 list_add(&tmp->list, &priv->data.list);
147
148 mode = 0;
149 if (handlers[i].read != NULL) mode |= S_IRUSR;
150 if (handlers[i].write != NULL) mode |= S_IWUSR;
151
152 if ((p = create_proc_entry(handlers[i].name, mode, parent)) != NULL) {
153 p->data = (void *) tmp;
154 p->proc_fops = &switch_proc_fops;
155 }
156 }
157 }
158
159 static void remove_handlers(switch_priv *priv)
160 {
161 struct list_head *pos, *q;
162 switch_proc_handler *tmp;
163
164 list_for_each_safe(pos, q, &priv->data.list) {
165 tmp = list_entry(pos, switch_proc_handler, list);
166 list_del(pos);
167 remove_proc_entry(tmp->handler.name, tmp->parent);
168 kfree(tmp);
169 }
170 }
171
172
173 static void do_unregister(switch_driver *driver)
174 {
175 char buf[4];
176 int i;
177 switch_priv *priv = (switch_priv *) driver->data;
178
179 remove_handlers(priv);
180
181 for(i = 0; priv->ports[i] != NULL; i++) {
182 sprintf(buf, "%d", i);
183 remove_proc_entry(buf, priv->port_dir);
184 }
185 kfree(priv->ports);
186 remove_proc_entry("port", priv->driver_dir);
187
188 for(i = 0; priv->vlans[i] != NULL; i++) {
189 sprintf(buf, "%d", i);
190 remove_proc_entry(buf, priv->vlan_dir);
191 }
192 kfree(priv->vlans);
193 remove_proc_entry("vlan", priv->driver_dir);
194
195 remove_proc_entry(driver->name, switch_root);
196
197 if (priv->nr == (drv_num - 1))
198 drv_num--;
199
200 kfree(priv);
201 }
202
203 static int do_register(switch_driver *driver)
204 {
205 switch_priv *priv;
206 int i;
207 char buf[4];
208
209 if ((priv = kmalloc(sizeof(switch_priv), GFP_KERNEL)) == NULL)
210 return -ENOBUFS;
211 driver->data = (void *) priv;
212
213 INIT_LIST_HEAD(&priv->data.list);
214
215 priv->nr = drv_num++;
216 sprintf(buf, "%d", priv->nr);
217 priv->driver_dir = proc_mkdir(buf, switch_root);
218 if (driver->driver_handlers != NULL)
219 add_handlers(priv, driver->driver_handlers, priv->driver_dir, 0);
220
221 priv->port_dir = proc_mkdir("port", priv->driver_dir);
222 priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
223 for (i = 0; i < driver->ports; i++) {
224 sprintf(buf, "%d", i);
225 priv->ports[i] = proc_mkdir(buf, priv->port_dir);
226 if (driver->port_handlers != NULL)
227 add_handlers(priv, driver->port_handlers, priv->ports[i], i);
228 }
229 priv->ports[i] = NULL;
230
231 priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
232 priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
233 for (i = 0; i < driver->vlans; i++) {
234 sprintf(buf, "%d", i);
235 priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
236 if (driver->vlan_handlers != NULL)
237 add_handlers(priv, driver->vlan_handlers, priv->vlans[i], i);
238 }
239 priv->vlans[i] = NULL;
240
241
242 return 0;
243 }
244
245 static int isspace(char c) {
246 switch(c) {
247 case ' ':
248 case 0x09:
249 case 0x0a:
250 case 0x0d:
251 return 1;
252 default:
253 return 0;
254 }
255 }
256
257 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
258 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
259
260 int switch_parse_media(char *buf)
261 {
262 char *str = buf;
263 while (*buf != 0) {
264 *buf = toupper(*buf);
265 buf++;
266 }
267
268 if (strncmp(str, "AUTO", 4) == 0)
269 return SWITCH_MEDIA_AUTO;
270 else if (strncmp(str, "100FD", 5) == 0)
271 return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
272 else if (strncmp(str, "100HD", 5) == 0)
273 return SWITCH_MEDIA_100;
274 else if (strncmp(str, "10FD", 4) == 0)
275 return SWITCH_MEDIA_FD;
276 else if (strncmp(str, "10HD", 4) == 0)
277 return 0;
278 else return -1;
279 }
280
281 int switch_print_media(char *buf, int media)
282 {
283 int len = 0;
284
285 if (media & SWITCH_MEDIA_AUTO)
286 len = sprintf(buf, "Auto");
287 else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
288 len = sprintf(buf, "100FD");
289 else if (media == SWITCH_MEDIA_100)
290 len = sprintf(buf, "100HD");
291 else if (media == SWITCH_MEDIA_FD)
292 len = sprintf(buf, "10FD");
293 else if (media == 0)
294 len = sprintf(buf, "10HD");
295 else
296 len = sprintf(buf, "Invalid");
297
298 return len;
299 }
300
301 int switch_parse_vlan(char *buf)
302 {
303 char vlan = 0, tag = 0, pvid_port = 0;
304 int untag, j;
305
306 while (isspace(*buf)) buf++;
307
308 while (*buf >= '0' && *buf <= '9') {
309 j = *buf++ - '0';
310 vlan |= 1 << j;
311
312 untag = 0;
313 /* untag if needed, CPU port requires special handling */
314 if (*buf == 'u' || (j != 5 && (isspace(*buf) || *buf == 0))) {
315 untag = 1;
316 if (*buf) buf++;
317 } else if (*buf == '*') {
318 pvid_port |= (1 << j);
319 buf++;
320 } else if (*buf == 't' || isspace(*buf)) {
321 buf++;
322 } else break;
323
324 if (!untag)
325 tag |= 1 << j;
326
327 while (isspace(*buf)) buf++;
328 }
329
330 if (*buf)
331 return -1;
332
333 return (pvid_port << 16) | (tag << 8) | vlan;
334 }
335
336
337 int switch_register_driver(switch_driver *driver)
338 {
339 struct list_head *pos;
340 switch_driver *new;
341 int ret;
342
343 list_for_each(pos, &drivers.list) {
344 if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
345 printk("Switch driver '%s' already exists in the kernel\n", driver->name);
346 return -EINVAL;
347 }
348 }
349
350 new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
351 memcpy(new, driver, sizeof(switch_driver));
352 new->name = strdup(driver->name);
353
354 if ((ret = do_register(new)) < 0) {
355 kfree(new->name);
356 kfree(new);
357 return ret;
358 }
359 INIT_LIST_HEAD(&new->list);
360 list_add(&new->list, &drivers.list);
361
362 return 0;
363 }
364
365 void switch_unregister_driver(char *name) {
366 struct list_head *pos, *q;
367 switch_driver *tmp;
368
369 list_for_each_safe(pos, q, &drivers.list) {
370 tmp = list_entry(pos, switch_driver, list);
371 if (strcmp(tmp->name, name) == 0) {
372 do_unregister(tmp);
373 list_del(pos);
374 kfree(tmp->name);
375 kfree(tmp);
376
377 return;
378 }
379 }
380 }
381
382 static int __init switch_init()
383 {
384 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
385 printk("%s: proc_mkdir failed.\n", __FILE__);
386 return -ENODEV;
387 }
388
389 INIT_LIST_HEAD(&drivers.list);
390
391 return 0;
392 }
393
394 static void __exit switch_exit()
395 {
396 remove_proc_entry("vlan", NULL);
397 }
398
399 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
400 MODULE_LICENSE("GPL");
401
402 EXPORT_SYMBOL(switch_register_driver);
403 EXPORT_SYMBOL(switch_unregister_driver);
404 EXPORT_SYMBOL(switch_parse_vlan);
405 EXPORT_SYMBOL(switch_parse_media);
406 EXPORT_SYMBOL(switch_print_media);
407
408 module_init(switch_init);
409 module_exit(switch_exit);