5d4693c28f75f5e53f8753233d762b0c5ca1b1f2
[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 void *driver;
41 switch_config handler;
42 } switch_proc_handler;
43
44 typedef struct {
45 struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
46 struct proc_dir_entry **ports, **vlans;
47 switch_proc_handler data;
48 int nr;
49 } switch_priv;
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 inline 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(handler->driver, 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(handler->driver, page, handler->nr)) >= 0)
126 ret = count;
127 }
128 }
129
130 kfree(page);
131 return ret;
132 }
133
134 static int handle_driver_name(void *driver, char *buf, int nr)
135 {
136 char *name = ((switch_driver *) driver)->name;
137 return sprintf(buf, "%s\n", name);
138 }
139
140 static int handle_driver_version(void *driver, char *buf, int nr)
141 {
142 char *version = ((switch_driver *) driver)->version;
143 strcpy(buf, version);
144 return sprintf(buf, "%s\n", version);
145 }
146
147 static void add_handler(switch_driver *driver, switch_config *handler, struct proc_dir_entry *parent, int nr)
148 {
149 switch_priv *priv = (switch_priv *) driver->data;
150 struct proc_dir_entry *p;
151 int mode;
152
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;
157 tmp->nr = nr;
158 tmp->driver = driver;
159 memcpy(&tmp->handler, handler, sizeof(switch_config));
160 list_add(&tmp->list, &priv->data.list);
161
162 mode = 0;
163 if (handler->read != NULL) mode |= S_IRUSR;
164 if (handler->write != NULL) mode |= S_IWUSR;
165
166 if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
167 p->data = (void *) tmp;
168 p->proc_fops = &switch_proc_fops;
169 }
170 }
171
172 static inline void add_handlers(switch_driver *driver, switch_config *handlers, struct proc_dir_entry *parent, int nr)
173 {
174 int i;
175
176 for (i = 0; handlers[i].name != NULL; i++) {
177 add_handler(driver, &(handlers[i]), parent, nr);
178 }
179 }
180
181 static void remove_handlers(switch_priv *priv)
182 {
183 struct list_head *pos, *q;
184 switch_proc_handler *tmp;
185
186 list_for_each_safe(pos, q, &priv->data.list) {
187 tmp = list_entry(pos, switch_proc_handler, list);
188 list_del(pos);
189 remove_proc_entry(tmp->handler.name, tmp->parent);
190 kfree(tmp);
191 }
192 }
193
194
195 static void do_unregister(switch_driver *driver)
196 {
197 char buf[4];
198 int i;
199 switch_priv *priv = (switch_priv *) driver->data;
200
201 remove_handlers(priv);
202
203 for(i = 0; priv->ports[i] != NULL; i++) {
204 sprintf(buf, "%d", i);
205 remove_proc_entry(buf, priv->port_dir);
206 }
207 kfree(priv->ports);
208 remove_proc_entry("port", priv->driver_dir);
209
210 for(i = 0; priv->vlans[i] != NULL; i++) {
211 sprintf(buf, "%d", i);
212 remove_proc_entry(buf, priv->vlan_dir);
213 }
214 kfree(priv->vlans);
215 remove_proc_entry("vlan", priv->driver_dir);
216
217 remove_proc_entry(driver->interface, switch_root);
218
219 if (priv->nr == (drv_num - 1))
220 drv_num--;
221
222 kfree(priv);
223 }
224
225 switch_config global_driver_handlers[] = {
226 {"driver", handle_driver_name, NULL},
227 {"version", handle_driver_version, NULL},
228 {NULL, NULL, NULL}
229 };
230
231 static int do_register(switch_driver *driver)
232 {
233 switch_priv *priv;
234 int i;
235 char buf[4];
236
237 if ((priv = kmalloc(sizeof(switch_priv), GFP_KERNEL)) == NULL)
238 return -ENOBUFS;
239 driver->data = (void *) priv;
240
241 INIT_LIST_HEAD(&priv->data.list);
242
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);
248 }
249
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);
257 }
258 priv->ports[i] = NULL;
259
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);
267 }
268 priv->vlans[i] = NULL;
269
270
271 return 0;
272 }
273
274 static inline int isspace(char c) {
275 switch(c) {
276 case ' ':
277 case 0x09:
278 case 0x0a:
279 case 0x0d:
280 return 1;
281 default:
282 return 0;
283 }
284 }
285
286 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
287 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
288
289 int switch_parse_media(char *buf)
290 {
291 char *str = buf;
292 while (*buf != 0) {
293 *buf = toupper(*buf);
294 buf++;
295 }
296
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)
306 return 0;
307 else return -1;
308 }
309
310 int switch_print_media(char *buf, int media)
311 {
312 int len = 0;
313
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");
322 else if (media == 0)
323 len = sprintf(buf, "10HD");
324 else
325 len = sprintf(buf, "Invalid");
326
327 return len;
328 }
329
330 switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
331 {
332 switch_vlan_config *c;
333 int j, u, p, s;
334
335 c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
336 memset(c, 0, sizeof(switch_vlan_config));
337
338 while (isspace(*buf)) buf++;
339 j = 0;
340 while (*buf >= '0' && *buf <= '9') {
341 j *= 10;
342 j += *buf++ - '0';
343
344 u = ((j == driver->cpuport) ? 0 : 1);
345 p = 0;
346 s = !(*buf >= '0' && *buf <= '9');
347
348 if (s) {
349 while (s && !isspace(*buf) && (*buf != 0)) {
350 switch(*buf) {
351 case 'u':
352 u = 1;
353 break;
354 case 't':
355 u = 0;
356 break;
357 case '*':
358 p = 1;
359 break;
360 }
361 buf++;
362 }
363 c->port |= (1 << j);
364 if (u)
365 c->untag |= (1 << j);
366 if (p)
367 c->pvid |= (1 << j);
368
369 j = 0;
370 }
371
372 while (isspace(*buf)) buf++;
373 }
374 if (*buf != 0) return NULL;
375
376 c->port &= (1 << driver->ports) - 1;
377 c->untag &= (1 << driver->ports) - 1;
378 c->pvid &= (1 << driver->ports) - 1;
379
380 return c;
381 }
382
383
384 int switch_register_driver(switch_driver *driver)
385 {
386 struct list_head *pos;
387 switch_driver *new;
388 int ret;
389
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);
393 return -EINVAL;
394 }
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);
397 return -EINVAL;
398 }
399 }
400
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);
405
406 if ((ret = do_register(new)) < 0) {
407 kfree(new->name);
408 kfree(new);
409 return ret;
410 }
411 INIT_LIST_HEAD(&new->list);
412 list_add(&new->list, &drivers.list);
413
414 return 0;
415 }
416
417 void switch_unregister_driver(char *name) {
418 struct list_head *pos, *q;
419 switch_driver *tmp;
420
421 list_for_each_safe(pos, q, &drivers.list) {
422 tmp = list_entry(pos, switch_driver, list);
423 if (strcmp(tmp->name, name) == 0) {
424 do_unregister(tmp);
425 list_del(pos);
426 kfree(tmp->name);
427 kfree(tmp);
428
429 return;
430 }
431 }
432 }
433
434 static int __init switch_init()
435 {
436 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
437 printk("%s: proc_mkdir failed.\n", __FILE__);
438 return -ENODEV;
439 }
440
441 INIT_LIST_HEAD(&drivers.list);
442
443 return 0;
444 }
445
446 static void __exit switch_exit()
447 {
448 remove_proc_entry("switch", NULL);
449 }
450
451 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
452 MODULE_LICENSE("GPL");
453
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);
459
460 module_init(switch_init);
461 module_exit(switch_exit);