c0e5cc9eaa2777bfa0ad60397c2a79ca0922a17b
[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 void add_handlers(switch_driver *driver, switch_config *handlers, struct proc_dir_entry *parent, int nr)
135 {
136 switch_priv *priv = (switch_priv *) driver->data;
137 switch_proc_handler *tmp;
138 int i, mode;
139 struct proc_dir_entry *p;
140
141 for (i = 0; handlers[i].name != NULL; i++) {
142 tmp = kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
143 INIT_LIST_HEAD(&tmp->list);
144 tmp->parent = parent;
145 tmp->nr = nr;
146 tmp->driver = driver;
147 memcpy(&tmp->handler, &(handlers[i]), sizeof(switch_config));
148 list_add(&tmp->list, &priv->data.list);
149
150 mode = 0;
151 if (handlers[i].read != NULL) mode |= S_IRUSR;
152 if (handlers[i].write != NULL) mode |= S_IWUSR;
153
154 if ((p = create_proc_entry(handlers[i].name, mode, parent)) != NULL) {
155 p->data = (void *) tmp;
156 p->proc_fops = &switch_proc_fops;
157 }
158 }
159 }
160
161 static void remove_handlers(switch_priv *priv)
162 {
163 struct list_head *pos, *q;
164 switch_proc_handler *tmp;
165
166 list_for_each_safe(pos, q, &priv->data.list) {
167 tmp = list_entry(pos, switch_proc_handler, list);
168 list_del(pos);
169 remove_proc_entry(tmp->handler.name, tmp->parent);
170 kfree(tmp);
171 }
172 }
173
174
175 static void do_unregister(switch_driver *driver)
176 {
177 char buf[4];
178 int i;
179 switch_priv *priv = (switch_priv *) driver->data;
180
181 remove_handlers(priv);
182
183 for(i = 0; priv->ports[i] != NULL; i++) {
184 sprintf(buf, "%d", i);
185 remove_proc_entry(buf, priv->port_dir);
186 }
187 kfree(priv->ports);
188 remove_proc_entry("port", priv->driver_dir);
189
190 for(i = 0; priv->vlans[i] != NULL; i++) {
191 sprintf(buf, "%d", i);
192 remove_proc_entry(buf, priv->vlan_dir);
193 }
194 kfree(priv->vlans);
195 remove_proc_entry("vlan", priv->driver_dir);
196
197 remove_proc_entry(driver->interface, switch_root);
198
199 if (priv->nr == (drv_num - 1))
200 drv_num--;
201
202 kfree(priv);
203 }
204
205 static int do_register(switch_driver *driver)
206 {
207 switch_priv *priv;
208 int i;
209 char buf[4];
210
211 if ((priv = kmalloc(sizeof(switch_priv), GFP_KERNEL)) == NULL)
212 return -ENOBUFS;
213 driver->data = (void *) priv;
214
215 INIT_LIST_HEAD(&priv->data.list);
216
217 priv->nr = drv_num++;
218 priv->driver_dir = proc_mkdir(driver->interface, switch_root);
219 if (driver->driver_handlers != NULL)
220 add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
221
222 priv->port_dir = proc_mkdir("port", priv->driver_dir);
223 priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
224 for (i = 0; i < driver->ports; i++) {
225 sprintf(buf, "%d", i);
226 priv->ports[i] = proc_mkdir(buf, priv->port_dir);
227 if (driver->port_handlers != NULL)
228 add_handlers(driver, driver->port_handlers, priv->ports[i], i);
229 }
230 priv->ports[i] = NULL;
231
232 priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
233 priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
234 for (i = 0; i < driver->vlans; i++) {
235 sprintf(buf, "%d", i);
236 priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
237 if (driver->vlan_handlers != NULL)
238 add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
239 }
240 priv->vlans[i] = NULL;
241
242
243 return 0;
244 }
245
246 static inline int isspace(char c) {
247 switch(c) {
248 case ' ':
249 case 0x09:
250 case 0x0a:
251 case 0x0d:
252 return 1;
253 default:
254 return 0;
255 }
256 }
257
258 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
259 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
260
261 int switch_parse_media(char *buf)
262 {
263 char *str = buf;
264 while (*buf != 0) {
265 *buf = toupper(*buf);
266 buf++;
267 }
268
269 if (strncmp(str, "AUTO", 4) == 0)
270 return SWITCH_MEDIA_AUTO;
271 else if (strncmp(str, "100FD", 5) == 0)
272 return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
273 else if (strncmp(str, "100HD", 5) == 0)
274 return SWITCH_MEDIA_100;
275 else if (strncmp(str, "10FD", 4) == 0)
276 return SWITCH_MEDIA_FD;
277 else if (strncmp(str, "10HD", 4) == 0)
278 return 0;
279 else return -1;
280 }
281
282 int switch_print_media(char *buf, int media)
283 {
284 int len = 0;
285
286 if (media & SWITCH_MEDIA_AUTO)
287 len = sprintf(buf, "Auto");
288 else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
289 len = sprintf(buf, "100FD");
290 else if (media == SWITCH_MEDIA_100)
291 len = sprintf(buf, "100HD");
292 else if (media == SWITCH_MEDIA_FD)
293 len = sprintf(buf, "10FD");
294 else if (media == 0)
295 len = sprintf(buf, "10HD");
296 else
297 len = sprintf(buf, "Invalid");
298
299 return len;
300 }
301
302 switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
303 {
304 switch_vlan_config *c;
305 int j, u, p, s;
306
307 c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
308 memset(c, 0, sizeof(switch_vlan_config));
309
310 while (isspace(*buf)) buf++;
311 j = 0;
312 while (*buf >= '0' && *buf <= '9') {
313 j *= 10;
314 j += *buf++ - '0';
315
316 u = ((j == driver->cpuport) ? 0 : 1);
317 p = 0;
318 s = !(*buf >= '0' && *buf <= '9');
319
320 if (s) {
321 while (s && !isspace(*buf) && (*buf != 0)) {
322 switch(*buf) {
323 case 'u':
324 u = 1;
325 break;
326 case 't':
327 u = 0;
328 break;
329 case '*':
330 p = 1;
331 break;
332 }
333 buf++;
334 }
335 c->port |= (1 << j);
336 if (u)
337 c->untag |= (1 << j);
338 if (p)
339 c->pvid |= (1 << j);
340
341 j = 0;
342 }
343
344 while (isspace(*buf)) buf++;
345 }
346 if (*buf != 0) return NULL;
347
348 c->port &= (1 << driver->ports) - 1;
349 c->untag &= (1 << driver->ports) - 1;
350 c->pvid &= (1 << driver->ports) - 1;
351
352 return c;
353 }
354
355
356 int switch_register_driver(switch_driver *driver)
357 {
358 struct list_head *pos;
359 switch_driver *new;
360 int ret;
361
362 list_for_each(pos, &drivers.list) {
363 if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
364 printk("Switch driver '%s' already exists in the kernel\n", driver->name);
365 return -EINVAL;
366 }
367 if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
368 printk("There is already a switch registered on the device '%s'\n", driver->interface);
369 return -EINVAL;
370 }
371 }
372
373 new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
374 memcpy(new, driver, sizeof(switch_driver));
375 new->name = strdup(driver->name);
376 new->interface = strdup(driver->interface);
377
378 if ((ret = do_register(new)) < 0) {
379 kfree(new->name);
380 kfree(new);
381 return ret;
382 }
383 INIT_LIST_HEAD(&new->list);
384 list_add(&new->list, &drivers.list);
385
386 return 0;
387 }
388
389 void switch_unregister_driver(char *name) {
390 struct list_head *pos, *q;
391 switch_driver *tmp;
392
393 list_for_each_safe(pos, q, &drivers.list) {
394 tmp = list_entry(pos, switch_driver, list);
395 if (strcmp(tmp->name, name) == 0) {
396 do_unregister(tmp);
397 list_del(pos);
398 kfree(tmp->name);
399 kfree(tmp);
400
401 return;
402 }
403 }
404 }
405
406 static int __init switch_init()
407 {
408 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
409 printk("%s: proc_mkdir failed.\n", __FILE__);
410 return -ENODEV;
411 }
412
413 INIT_LIST_HEAD(&drivers.list);
414
415 return 0;
416 }
417
418 static void __exit switch_exit()
419 {
420 remove_proc_entry("vlan", NULL);
421 }
422
423 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
424 MODULE_LICENSE("GPL");
425
426 EXPORT_SYMBOL(switch_register_driver);
427 EXPORT_SYMBOL(switch_unregister_driver);
428 EXPORT_SYMBOL(switch_parse_vlan);
429 EXPORT_SYMBOL(switch_parse_media);
430 EXPORT_SYMBOL(switch_print_media);
431
432 module_init(switch_init);
433 module_exit(switch_exit);