4 * Copyright (C) 2005 Felix Fietkau <openwrt@nbd.name>
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.
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.
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.
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>
30 #include "switch-core.h"
32 static int drv_num
= 0;
33 static struct proc_dir_entry
*switch_root
;
34 switch_driver drivers
;
37 struct list_head list
;
38 struct proc_dir_entry
*parent
;
41 switch_config handler
;
42 } switch_proc_handler
;
45 struct proc_dir_entry
*driver_dir
, *port_dir
, *vlan_dir
;
46 struct proc_dir_entry
**ports
, **vlans
;
47 switch_proc_handler data
;
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
);
54 static struct file_operations switch_proc_fops
= {
55 read
: switch_proc_read
,
56 write
: switch_proc_write
59 static inline char *strdup(char *str
)
61 char *new = kmalloc(strlen(str
) + 1, GFP_KERNEL
);
66 static ssize_t
switch_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
69 struct inode
*inode
= file
->f_dentry
->d_inode
;
70 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
72 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
77 if ((page
= kmalloc(SWITCH_MAX_BUFSZ
, GFP_KERNEL
)) == NULL
)
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
);
88 len
= min_t(int, len
- *ppos
, count
);
89 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
102 static ssize_t
switch_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
)
105 struct inode
*inode
= file
->f_dentry
->d_inode
;
106 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
108 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
113 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
116 if (copy_from_user(page
, buf
, count
)) {
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)
134 static void add_handlers(switch_driver
*driver
, switch_config
*handlers
, struct proc_dir_entry
*parent
, int nr
)
136 switch_priv
*priv
= (switch_priv
*) driver
->data
;
137 switch_proc_handler
*tmp
;
139 struct proc_dir_entry
*p
;
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
;
146 tmp
->driver
= driver
;
147 memcpy(&tmp
->handler
, &(handlers
[i
]), sizeof(switch_config
));
148 list_add(&tmp
->list
, &priv
->data
.list
);
151 if (handlers
[i
].read
!= NULL
) mode
|= S_IRUSR
;
152 if (handlers
[i
].write
!= NULL
) mode
|= S_IWUSR
;
154 if ((p
= create_proc_entry(handlers
[i
].name
, mode
, parent
)) != NULL
) {
155 p
->data
= (void *) tmp
;
156 p
->proc_fops
= &switch_proc_fops
;
161 static void remove_handlers(switch_priv
*priv
)
163 struct list_head
*pos
, *q
;
164 switch_proc_handler
*tmp
;
166 list_for_each_safe(pos
, q
, &priv
->data
.list
) {
167 tmp
= list_entry(pos
, switch_proc_handler
, list
);
169 remove_proc_entry(tmp
->handler
.name
, tmp
->parent
);
175 static void do_unregister(switch_driver
*driver
)
179 switch_priv
*priv
= (switch_priv
*) driver
->data
;
181 remove_handlers(priv
);
183 for(i
= 0; priv
->ports
[i
] != NULL
; i
++) {
184 sprintf(buf
, "%d", i
);
185 remove_proc_entry(buf
, priv
->port_dir
);
188 remove_proc_entry("port", priv
->driver_dir
);
190 for(i
= 0; priv
->vlans
[i
] != NULL
; i
++) {
191 sprintf(buf
, "%d", i
);
192 remove_proc_entry(buf
, priv
->vlan_dir
);
195 remove_proc_entry("vlan", priv
->driver_dir
);
197 remove_proc_entry(driver
->interface
, switch_root
);
199 if (priv
->nr
== (drv_num
- 1))
205 static int do_register(switch_driver
*driver
)
211 if ((priv
= kmalloc(sizeof(switch_priv
), GFP_KERNEL
)) == NULL
)
213 driver
->data
= (void *) priv
;
215 INIT_LIST_HEAD(&priv
->data
.list
);
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);
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
);
230 priv
->ports
[i
] = NULL
;
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
);
240 priv
->vlans
[i
] = NULL
;
246 static inline int isspace(char c
) {
258 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
259 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
261 int switch_parse_media(char *buf
)
265 *buf
= toupper(*buf
);
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)
282 int switch_print_media(char *buf
, int media
)
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");
295 len
= sprintf(buf
, "10HD");
297 len
= sprintf(buf
, "Invalid");
302 switch_vlan_config
*switch_parse_vlan(switch_driver
*driver
, char *buf
)
304 switch_vlan_config
*c
;
307 c
= kmalloc(sizeof(switch_vlan_config
), GFP_KERNEL
);
308 memset(c
, 0, sizeof(switch_vlan_config
));
310 while (isspace(*buf
)) buf
++;
312 while (*buf
>= '0' && *buf
<= '9') {
316 u
= ((j
== driver
->cpuport
) ? 0 : 1);
318 s
= !(*buf
>= '0' && *buf
<= '9');
321 while (s
&& !isspace(*buf
) && (*buf
!= 0)) {
337 c
->untag
|= (1 << j
);
344 while (isspace(*buf
)) buf
++;
346 if (*buf
!= 0) return NULL
;
348 c
->port
&= (1 << driver
->ports
) - 1;
349 c
->untag
&= (1 << driver
->ports
) - 1;
350 c
->pvid
&= (1 << driver
->ports
) - 1;
356 int switch_register_driver(switch_driver
*driver
)
358 struct list_head
*pos
;
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
);
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
);
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
);
378 if ((ret
= do_register(new)) < 0) {
383 INIT_LIST_HEAD(&new->list
);
384 list_add(&new->list
, &drivers
.list
);
389 void switch_unregister_driver(char *name
) {
390 struct list_head
*pos
, *q
;
393 list_for_each_safe(pos
, q
, &drivers
.list
) {
394 tmp
= list_entry(pos
, switch_driver
, list
);
395 if (strcmp(tmp
->name
, name
) == 0) {
406 static int __init
switch_init()
408 if ((switch_root
= proc_mkdir("switch", NULL
)) == NULL
) {
409 printk("%s: proc_mkdir failed.\n", __FILE__
);
413 INIT_LIST_HEAD(&drivers
.list
);
418 static void __exit
switch_exit()
420 remove_proc_entry("vlan", NULL
);
423 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
424 MODULE_LICENSE("GPL");
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
);
432 module_init(switch_init
);
433 module_exit(switch_exit
);