add patch from #196
[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 * Basic doc of driver's /proc interface:
23 * /proc/switch/<interface>/
24 * registers: read-only
25 * counters: read-only
26 * reset: write causes hardware reset
27 * enable_vlan: "0", "1"
28 * port/<port-number>/
29 * enabled: "0", "1"
30 * media: "AUTO", "100FD", "100HD", "10FD", "10HD"
31 * vlan/<port-number>/
32 * ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
33 */
34
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <asm/uaccess.h>
39 #include <linux/proc_fs.h>
40 #include <linux/list.h>
41
42 #include "switch-core.h"
43
44 static int drv_num = 0;
45 static struct proc_dir_entry *switch_root;
46 switch_driver drivers;
47
48 typedef struct {
49 struct list_head list;
50 struct proc_dir_entry *parent;
51 int nr;
52 void *driver;
53 switch_config handler;
54 } switch_proc_handler;
55
56 typedef struct {
57 struct proc_dir_entry *driver_dir, *port_dir, *vlan_dir;
58 struct proc_dir_entry **ports, **vlans;
59 switch_proc_handler data;
60 int nr;
61 } switch_priv;
62
63 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos);
64 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data);
65
66 static struct file_operations switch_proc_fops = {
67 read: switch_proc_read,
68 write: switch_proc_write
69 };
70
71 static inline char *strdup(char *str)
72 {
73 char *new = kmalloc(strlen(str) + 1, GFP_KERNEL);
74 strcpy(new, str);
75 return new;
76 }
77
78 static ssize_t switch_proc_read(struct file *file, char *buf, size_t count, loff_t *ppos)
79 {
80 #ifdef LINUX_2_4
81 struct inode *inode = file->f_dentry->d_inode;
82 struct proc_dir_entry *dent = inode->u.generic_ip;
83 #else
84 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
85 #endif
86 char *page;
87 int len = 0;
88
89 if ((page = kmalloc(SWITCH_MAX_BUFSZ, GFP_KERNEL)) == NULL)
90 return -ENOBUFS;
91
92 if (dent->data != NULL) {
93 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
94 if (handler->handler.read != NULL)
95 len += handler->handler.read(handler->driver, page + len, handler->nr);
96 }
97 len += 1;
98
99 if (*ppos < len) {
100 len = min_t(int, len - *ppos, count);
101 if (copy_to_user(buf, (page + *ppos), len)) {
102 kfree(page);
103 return -EFAULT;
104 }
105 *ppos += len;
106 } else {
107 len = 0;
108 }
109
110 return len;
111 }
112
113
114 static ssize_t switch_proc_write(struct file *file, const char *buf, size_t count, void *data)
115 {
116 #ifdef LINUX_2_4
117 struct inode *inode = file->f_dentry->d_inode;
118 struct proc_dir_entry *dent = inode->u.generic_ip;
119 #else
120 struct proc_dir_entry *dent = PDE(file->f_dentry->d_inode);
121 #endif
122 char *page;
123 int ret = -EINVAL;
124
125 if ((page = kmalloc(count + 1, GFP_KERNEL)) == NULL)
126 return -ENOBUFS;
127
128 if (copy_from_user(page, buf, count)) {
129 kfree(page);
130 return -EINVAL;
131 }
132 page[count] = 0;
133
134 if (dent->data != NULL) {
135 switch_proc_handler *handler = (switch_proc_handler *) dent->data;
136 if (handler->handler.write != NULL) {
137 if ((ret = handler->handler.write(handler->driver, page, handler->nr)) >= 0)
138 ret = count;
139 }
140 }
141
142 kfree(page);
143 return ret;
144 }
145
146 static int handle_driver_name(void *driver, char *buf, int nr)
147 {
148 char *name = ((switch_driver *) driver)->name;
149 return sprintf(buf, "%s\n", name);
150 }
151
152 static int handle_driver_version(void *driver, char *buf, int nr)
153 {
154 char *version = ((switch_driver *) driver)->version;
155 strcpy(buf, version);
156 return sprintf(buf, "%s\n", version);
157 }
158
159 static void add_handler(switch_driver *driver, switch_config *handler, struct proc_dir_entry *parent, int nr)
160 {
161 switch_priv *priv = (switch_priv *) driver->data;
162 struct proc_dir_entry *p;
163 int mode;
164
165 switch_proc_handler *tmp;
166 tmp = (switch_proc_handler *) kmalloc(sizeof(switch_proc_handler), GFP_KERNEL);
167 INIT_LIST_HEAD(&tmp->list);
168 tmp->parent = parent;
169 tmp->nr = nr;
170 tmp->driver = driver;
171 memcpy(&tmp->handler, handler, sizeof(switch_config));
172 list_add(&tmp->list, &priv->data.list);
173
174 mode = 0;
175 if (handler->read != NULL) mode |= S_IRUSR;
176 if (handler->write != NULL) mode |= S_IWUSR;
177
178 if ((p = create_proc_entry(handler->name, mode, parent)) != NULL) {
179 p->data = (void *) tmp;
180 p->proc_fops = &switch_proc_fops;
181 }
182 }
183
184 static inline void add_handlers(switch_driver *driver, switch_config *handlers, struct proc_dir_entry *parent, int nr)
185 {
186 int i;
187
188 for (i = 0; handlers[i].name != NULL; i++) {
189 add_handler(driver, &(handlers[i]), parent, nr);
190 }
191 }
192
193 static void remove_handlers(switch_priv *priv)
194 {
195 struct list_head *pos, *q;
196 switch_proc_handler *tmp;
197
198 list_for_each_safe(pos, q, &priv->data.list) {
199 tmp = list_entry(pos, switch_proc_handler, list);
200 list_del(pos);
201 remove_proc_entry(tmp->handler.name, tmp->parent);
202 kfree(tmp);
203 }
204 }
205
206
207 static void do_unregister(switch_driver *driver)
208 {
209 char buf[4];
210 int i;
211 switch_priv *priv = (switch_priv *) driver->data;
212
213 remove_handlers(priv);
214
215 for(i = 0; priv->ports[i] != NULL; i++) {
216 sprintf(buf, "%d", i);
217 remove_proc_entry(buf, priv->port_dir);
218 }
219 kfree(priv->ports);
220 remove_proc_entry("port", priv->driver_dir);
221
222 for(i = 0; priv->vlans[i] != NULL; i++) {
223 sprintf(buf, "%d", i);
224 remove_proc_entry(buf, priv->vlan_dir);
225 }
226 kfree(priv->vlans);
227 remove_proc_entry("vlan", priv->driver_dir);
228
229 remove_proc_entry(driver->interface, switch_root);
230
231 if (priv->nr == (drv_num - 1))
232 drv_num--;
233
234 kfree(priv);
235 }
236
237 switch_config global_driver_handlers[] = {
238 {"driver", handle_driver_name, NULL},
239 {"version", handle_driver_version, NULL},
240 {NULL, NULL, NULL}
241 };
242
243 static int do_register(switch_driver *driver)
244 {
245 switch_priv *priv;
246 int i;
247 char buf[4];
248
249 if ((priv = kmalloc(sizeof(switch_priv), GFP_KERNEL)) == NULL)
250 return -ENOBUFS;
251 driver->data = (void *) priv;
252
253 INIT_LIST_HEAD(&priv->data.list);
254
255 priv->nr = drv_num++;
256 priv->driver_dir = proc_mkdir(driver->interface, switch_root);
257 if (driver->driver_handlers != NULL) {
258 add_handlers(driver, driver->driver_handlers, priv->driver_dir, 0);
259 add_handlers(driver, global_driver_handlers, priv->driver_dir, 0);
260 }
261
262 priv->port_dir = proc_mkdir("port", priv->driver_dir);
263 priv->ports = kmalloc((driver->ports + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
264 for (i = 0; i < driver->ports; i++) {
265 sprintf(buf, "%d", i);
266 priv->ports[i] = proc_mkdir(buf, priv->port_dir);
267 if (driver->port_handlers != NULL)
268 add_handlers(driver, driver->port_handlers, priv->ports[i], i);
269 }
270 priv->ports[i] = NULL;
271
272 priv->vlan_dir = proc_mkdir("vlan", priv->driver_dir);
273 priv->vlans = kmalloc((driver->vlans + 1) * sizeof(struct proc_dir_entry *), GFP_KERNEL);
274 for (i = 0; i < driver->vlans; i++) {
275 sprintf(buf, "%d", i);
276 priv->vlans[i] = proc_mkdir(buf, priv->vlan_dir);
277 if (driver->vlan_handlers != NULL)
278 add_handlers(driver, driver->vlan_handlers, priv->vlans[i], i);
279 }
280 priv->vlans[i] = NULL;
281
282
283 return 0;
284 }
285
286 static inline int isspace(char c) {
287 switch(c) {
288 case ' ':
289 case 0x09:
290 case 0x0a:
291 case 0x0d:
292 return 1;
293 default:
294 return 0;
295 }
296 }
297
298 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
299 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
300
301 int switch_parse_media(char *buf)
302 {
303 char *str = buf;
304 while (*buf != 0) {
305 *buf = toupper(*buf);
306 buf++;
307 }
308
309 if (strncmp(str, "AUTO", 4) == 0)
310 return SWITCH_MEDIA_AUTO;
311 else if (strncmp(str, "100FD", 5) == 0)
312 return SWITCH_MEDIA_100 | SWITCH_MEDIA_FD;
313 else if (strncmp(str, "100HD", 5) == 0)
314 return SWITCH_MEDIA_100;
315 else if (strncmp(str, "10FD", 4) == 0)
316 return SWITCH_MEDIA_FD;
317 else if (strncmp(str, "10HD", 4) == 0)
318 return 0;
319 else return -1;
320 }
321
322 int switch_print_media(char *buf, int media)
323 {
324 int len = 0;
325
326 if (media & SWITCH_MEDIA_AUTO)
327 len = sprintf(buf, "Auto");
328 else if (media == (SWITCH_MEDIA_100 | SWITCH_MEDIA_FD))
329 len = sprintf(buf, "100FD");
330 else if (media == SWITCH_MEDIA_100)
331 len = sprintf(buf, "100HD");
332 else if (media == SWITCH_MEDIA_FD)
333 len = sprintf(buf, "10FD");
334 else if (media == 0)
335 len = sprintf(buf, "10HD");
336 else
337 len = sprintf(buf, "Invalid");
338
339 return len;
340 }
341
342 switch_vlan_config *switch_parse_vlan(switch_driver *driver, char *buf)
343 {
344 switch_vlan_config *c;
345 int j, u, p, s;
346
347 c = kmalloc(sizeof(switch_vlan_config), GFP_KERNEL);
348 memset(c, 0, sizeof(switch_vlan_config));
349
350 while (isspace(*buf)) buf++;
351 j = 0;
352 while (*buf >= '0' && *buf <= '9') {
353 j *= 10;
354 j += *buf++ - '0';
355
356 u = ((j == driver->cpuport) ? 0 : 1);
357 p = 0;
358 s = !(*buf >= '0' && *buf <= '9');
359
360 if (s) {
361 while (s && !isspace(*buf) && (*buf != 0)) {
362 switch(*buf) {
363 case 'u':
364 u = 1;
365 break;
366 case 't':
367 u = 0;
368 break;
369 case '*':
370 p = 1;
371 break;
372 }
373 buf++;
374 }
375 c->port |= (1 << j);
376 if (u)
377 c->untag |= (1 << j);
378 if (p)
379 c->pvid |= (1 << j);
380
381 j = 0;
382 }
383
384 while (isspace(*buf)) buf++;
385 }
386 if (*buf != 0) return NULL;
387
388 c->port &= (1 << driver->ports) - 1;
389 c->untag &= (1 << driver->ports) - 1;
390 c->pvid &= (1 << driver->ports) - 1;
391
392 return c;
393 }
394
395
396 int switch_register_driver(switch_driver *driver)
397 {
398 struct list_head *pos;
399 switch_driver *new;
400 int ret;
401
402 list_for_each(pos, &drivers.list) {
403 if (strcmp(list_entry(pos, switch_driver, list)->name, driver->name) == 0) {
404 printk("Switch driver '%s' already exists in the kernel\n", driver->name);
405 return -EINVAL;
406 }
407 if (strcmp(list_entry(pos, switch_driver, list)->interface, driver->interface) == 0) {
408 printk("There is already a switch registered on the device '%s'\n", driver->interface);
409 return -EINVAL;
410 }
411 }
412
413 new = kmalloc(sizeof(switch_driver), GFP_KERNEL);
414 memcpy(new, driver, sizeof(switch_driver));
415 new->name = strdup(driver->name);
416 new->interface = strdup(driver->interface);
417
418 if ((ret = do_register(new)) < 0) {
419 kfree(new->name);
420 kfree(new);
421 return ret;
422 }
423 INIT_LIST_HEAD(&new->list);
424 list_add(&new->list, &drivers.list);
425
426 return 0;
427 }
428
429 void switch_unregister_driver(char *name) {
430 struct list_head *pos, *q;
431 switch_driver *tmp;
432
433 list_for_each_safe(pos, q, &drivers.list) {
434 tmp = list_entry(pos, switch_driver, list);
435 if (strcmp(tmp->name, name) == 0) {
436 do_unregister(tmp);
437 list_del(pos);
438 kfree(tmp->name);
439 kfree(tmp);
440
441 return;
442 }
443 }
444 }
445
446 static int __init switch_init()
447 {
448 if ((switch_root = proc_mkdir("switch", NULL)) == NULL) {
449 printk("%s: proc_mkdir failed.\n", __FILE__);
450 return -ENODEV;
451 }
452
453 INIT_LIST_HEAD(&drivers.list);
454
455 return 0;
456 }
457
458 static void __exit switch_exit()
459 {
460 remove_proc_entry("switch", NULL);
461 }
462
463 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
464 MODULE_LICENSE("GPL");
465
466 EXPORT_SYMBOL(switch_register_driver);
467 EXPORT_SYMBOL(switch_unregister_driver);
468 EXPORT_SYMBOL(switch_parse_vlan);
469 EXPORT_SYMBOL(switch_parse_media);
470 EXPORT_SYMBOL(switch_print_media);
471
472 module_init(switch_init);
473 module_exit(switch_exit);