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