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