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.
22 * Basic doc of driver's /proc interface:
23 * /proc/switch/<interface>/
24 * registers: read-only
26 * reset: write causes hardware reset
27 * enable_vlan: "0", "1"
30 * media: "AUTO", "100FD", "100HD", "10FD", "10HD"
32 * ports: same syntax as for nvram's vlan*ports (eg. "1 2 3 4 5*")
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>
42 #include "switch-core.h"
44 static int drv_num
= 0;
45 static struct proc_dir_entry
*switch_root
;
46 switch_driver drivers
;
49 struct list_head list
;
50 struct proc_dir_entry
*parent
;
53 switch_config handler
;
54 } switch_proc_handler
;
57 struct proc_dir_entry
*driver_dir
, *port_dir
, *vlan_dir
;
58 struct proc_dir_entry
**ports
, **vlans
;
59 switch_proc_handler data
;
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
);
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
71 static ssize_t
switch_proc_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
74 struct inode
*inode
= file
->f_dentry
->d_inode
;
75 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
77 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
82 if ((page
= kmalloc(SWITCH_MAX_BUFSZ
, GFP_KERNEL
)) == NULL
)
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
);
93 len
= min_t(int, len
- *ppos
, count
);
94 if (copy_to_user(buf
, (page
+ *ppos
), len
)) {
108 static ssize_t
switch_proc_write(struct file
*file
, const char *buf
, size_t count
, void *data
)
111 struct inode
*inode
= file
->f_dentry
->d_inode
;
112 struct proc_dir_entry
*dent
= inode
->u
.generic_ip
;
114 struct proc_dir_entry
*dent
= PDE(file
->f_dentry
->d_inode
);
119 if ((page
= kmalloc(count
+ 1, GFP_KERNEL
)) == NULL
)
122 if (copy_from_user(page
, buf
, count
)) {
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)
140 static int handle_driver_name(void *driver
, char *buf
, int nr
)
142 char *name
= ((switch_driver
*) driver
)->name
;
143 return sprintf(buf
, "%s\n", name
);
146 static int handle_driver_version(void *driver
, char *buf
, int nr
)
148 char *version
= ((switch_driver
*) driver
)->version
;
149 strcpy(buf
, version
);
150 return sprintf(buf
, "%s\n", version
);
153 static void add_handler(switch_driver
*driver
, switch_config
*handler
, struct proc_dir_entry
*parent
, int nr
)
155 switch_priv
*priv
= (switch_priv
*) driver
->data
;
156 struct proc_dir_entry
*p
;
159 switch_proc_handler
*tmp
;
160 tmp
= (switch_proc_handler
*) kmalloc(sizeof(switch_proc_handler
), GFP_KERNEL
);
161 INIT_LIST_HEAD(&tmp
->list
);
162 tmp
->parent
= parent
;
164 tmp
->driver
= driver
;
165 memcpy(&tmp
->handler
, handler
, sizeof(switch_config
));
166 list_add(&tmp
->list
, &priv
->data
.list
);
169 if (handler
->read
!= NULL
) mode
|= S_IRUSR
;
170 if (handler
->write
!= NULL
) mode
|= S_IWUSR
;
172 if ((p
= create_proc_entry(handler
->name
, mode
, parent
)) != NULL
) {
173 p
->data
= (void *) tmp
;
174 p
->proc_fops
= &switch_proc_fops
;
178 static inline void add_handlers(switch_driver
*driver
, switch_config
*handlers
, struct proc_dir_entry
*parent
, int nr
)
182 for (i
= 0; handlers
[i
].name
!= NULL
; i
++) {
183 add_handler(driver
, &(handlers
[i
]), parent
, nr
);
187 static void remove_handlers(switch_priv
*priv
)
189 struct list_head
*pos
, *q
;
190 switch_proc_handler
*tmp
;
192 list_for_each_safe(pos
, q
, &priv
->data
.list
) {
193 tmp
= list_entry(pos
, switch_proc_handler
, list
);
195 remove_proc_entry(tmp
->handler
.name
, tmp
->parent
);
201 static void do_unregister(switch_driver
*driver
)
205 switch_priv
*priv
= (switch_priv
*) driver
->data
;
207 remove_handlers(priv
);
209 for(i
= 0; priv
->ports
[i
] != NULL
; i
++) {
210 sprintf(buf
, "%d", i
);
211 remove_proc_entry(buf
, priv
->port_dir
);
214 remove_proc_entry("port", priv
->driver_dir
);
216 for(i
= 0; priv
->vlans
[i
] != NULL
; i
++) {
217 sprintf(buf
, "%d", i
);
218 remove_proc_entry(buf
, priv
->vlan_dir
);
221 remove_proc_entry("vlan", priv
->driver_dir
);
223 remove_proc_entry(driver
->interface
, switch_root
);
225 if (priv
->nr
== (drv_num
- 1))
231 switch_config global_driver_handlers
[] = {
232 {"driver", handle_driver_name
, NULL
},
233 {"version", handle_driver_version
, NULL
},
237 static int do_register(switch_driver
*driver
)
243 if ((priv
= kmalloc(sizeof(switch_priv
), GFP_KERNEL
)) == NULL
)
245 driver
->data
= (void *) priv
;
247 INIT_LIST_HEAD(&priv
->data
.list
);
249 priv
->nr
= drv_num
++;
250 priv
->driver_dir
= proc_mkdir(driver
->interface
, switch_root
);
251 if (driver
->driver_handlers
!= NULL
) {
252 add_handlers(driver
, driver
->driver_handlers
, priv
->driver_dir
, 0);
253 add_handlers(driver
, global_driver_handlers
, priv
->driver_dir
, 0);
256 priv
->port_dir
= proc_mkdir("port", priv
->driver_dir
);
257 priv
->ports
= kmalloc((driver
->ports
+ 1) * sizeof(struct proc_dir_entry
*), GFP_KERNEL
);
258 for (i
= 0; i
< driver
->ports
; i
++) {
259 sprintf(buf
, "%d", i
);
260 priv
->ports
[i
] = proc_mkdir(buf
, priv
->port_dir
);
261 if (driver
->port_handlers
!= NULL
)
262 add_handlers(driver
, driver
->port_handlers
, priv
->ports
[i
], i
);
264 priv
->ports
[i
] = NULL
;
266 priv
->vlan_dir
= proc_mkdir("vlan", priv
->driver_dir
);
267 priv
->vlans
= kmalloc((driver
->vlans
+ 1) * sizeof(struct proc_dir_entry
*), GFP_KERNEL
);
268 for (i
= 0; i
< driver
->vlans
; i
++) {
269 sprintf(buf
, "%d", i
);
270 priv
->vlans
[i
] = proc_mkdir(buf
, priv
->vlan_dir
);
271 if (driver
->vlan_handlers
!= NULL
)
272 add_handlers(driver
, driver
->vlan_handlers
, priv
->vlans
[i
], i
);
274 priv
->vlans
[i
] = NULL
;
280 static inline int isspace(char c
) {
292 #define toupper(c) (islower(c) ? ((c) ^ 0x20) : (c))
293 #define islower(c) (((unsigned char)((c) - 'a')) < 26)
295 int switch_parse_media(char *buf
)
299 *buf
= toupper(*buf
);
303 if (strncmp(str
, "AUTO", 4) == 0)
304 return SWITCH_MEDIA_AUTO
;
305 else if (strncmp(str
, "100FD", 5) == 0)
306 return SWITCH_MEDIA_100
| SWITCH_MEDIA_FD
;
307 else if (strncmp(str
, "100HD", 5) == 0)
308 return SWITCH_MEDIA_100
;
309 else if (strncmp(str
, "10FD", 4) == 0)
310 return SWITCH_MEDIA_FD
;
311 else if (strncmp(str
, "10HD", 4) == 0)
316 int switch_print_media(char *buf
, int media
)
320 if (media
& SWITCH_MEDIA_AUTO
)
321 len
= sprintf(buf
, "Auto");
322 else if (media
== (SWITCH_MEDIA_100
| SWITCH_MEDIA_FD
))
323 len
= sprintf(buf
, "100FD");
324 else if (media
== SWITCH_MEDIA_100
)
325 len
= sprintf(buf
, "100HD");
326 else if (media
== SWITCH_MEDIA_FD
)
327 len
= sprintf(buf
, "10FD");
329 len
= sprintf(buf
, "10HD");
331 len
= sprintf(buf
, "Invalid");
336 switch_vlan_config
*switch_parse_vlan(switch_driver
*driver
, char *buf
)
338 switch_vlan_config
*c
;
341 c
= kmalloc(sizeof(switch_vlan_config
), GFP_KERNEL
);
342 memset(c
, 0, sizeof(switch_vlan_config
));
344 while (isspace(*buf
)) buf
++;
346 while (*buf
>= '0' && *buf
<= '9') {
350 u
= ((j
== driver
->cpuport
) ? 0 : 1);
352 s
= !(*buf
>= '0' && *buf
<= '9');
355 while (s
&& !isspace(*buf
) && (*buf
!= 0)) {
371 c
->untag
|= (1 << j
);
378 while (isspace(*buf
)) buf
++;
380 if (*buf
!= 0) return NULL
;
382 c
->port
&= (1 << driver
->ports
) - 1;
383 c
->untag
&= (1 << driver
->ports
) - 1;
384 c
->pvid
&= (1 << driver
->ports
) - 1;
390 int switch_register_driver(switch_driver
*driver
)
392 struct list_head
*pos
;
396 list_for_each(pos
, &drivers
.list
) {
397 if (strcmp(list_entry(pos
, switch_driver
, list
)->name
, driver
->name
) == 0) {
398 printk("Switch driver '%s' already exists in the kernel\n", driver
->name
);
401 if (strcmp(list_entry(pos
, switch_driver
, list
)->interface
, driver
->interface
) == 0) {
402 printk("There is already a switch registered on the device '%s'\n", driver
->interface
);
407 new = kmalloc(sizeof(switch_driver
), GFP_KERNEL
);
408 memcpy(new, driver
, sizeof(switch_driver
));
409 new->name
= strdup(driver
->name
);
410 new->interface
= strdup(driver
->interface
);
412 if ((ret
= do_register(new)) < 0) {
417 INIT_LIST_HEAD(&new->list
);
418 list_add(&new->list
, &drivers
.list
);
423 void switch_unregister_driver(char *name
) {
424 struct list_head
*pos
, *q
;
427 list_for_each_safe(pos
, q
, &drivers
.list
) {
428 tmp
= list_entry(pos
, switch_driver
, list
);
429 if (strcmp(tmp
->name
, name
) == 0) {
440 static int __init
switch_init(void)
442 if ((switch_root
= proc_mkdir("switch", NULL
)) == NULL
) {
443 printk("%s: proc_mkdir failed.\n", __FILE__
);
447 INIT_LIST_HEAD(&drivers
.list
);
452 static void __exit
switch_exit(void)
454 remove_proc_entry("switch", NULL
);
457 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
458 MODULE_LICENSE("GPL");
460 EXPORT_SYMBOL(switch_register_driver
);
461 EXPORT_SYMBOL(switch_unregister_driver
);
462 EXPORT_SYMBOL(switch_parse_vlan
);
463 EXPORT_SYMBOL(switch_parse_media
);
464 EXPORT_SYMBOL(switch_print_media
);
466 module_init(switch_init
);
467 module_exit(switch_exit
);