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