3 * Copyright 1999 Digi International (www.digi.com)
4 * James Puzzo <jamesp at digi dot com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
26 * Handle the "config" proc entry for the linux realport device driver
27 * and provide slots for the "net" and "mon" devices
35 #include <linux/module.h>
36 #include <linux/tty.h>
37 #include <linux/sched.h>
38 #include <linux/cred.h>
39 #include <linux/proc_fs.h>
40 #include <linux/ctype.h>
41 #include <linux/seq_file.h>
42 #include <linux/uaccess.h>
43 #include <linux/vmalloc.h>
45 #include "dgrp_common.h"
47 static struct dgrp_proc_entry dgrp_table
[];
48 static struct proc_dir_entry
*dgrp_proc_dir_entry
;
50 static int dgrp_add_id(long id
);
51 static int dgrp_remove_nd(struct nd_struct
*nd
);
52 static void unregister_dgrp_device(struct proc_dir_entry
*de
);
53 static void register_dgrp_device(struct nd_struct
*node
,
54 struct proc_dir_entry
*root
,
55 void (*register_hook
)(struct proc_dir_entry
*de
));
57 /* File operation declarations */
58 static int dgrp_gen_proc_open(struct inode
*, struct file
*);
59 static int dgrp_gen_proc_close(struct inode
*, struct file
*);
60 static int parse_write_config(char *);
63 static const struct file_operations dgrp_proc_file_ops
= {
65 .open
= dgrp_gen_proc_open
,
66 .release
= dgrp_gen_proc_close
,
69 static struct inode_operations proc_inode_ops
= {
70 .permission
= dgrp_inode_permission
74 static void register_proc_table(struct dgrp_proc_entry
*,
75 struct proc_dir_entry
*);
76 static void unregister_proc_table(struct dgrp_proc_entry
*,
77 struct proc_dir_entry
*);
79 static struct dgrp_proc_entry dgrp_net_table
[];
80 static struct dgrp_proc_entry dgrp_mon_table
[];
81 static struct dgrp_proc_entry dgrp_ports_table
[];
82 static struct dgrp_proc_entry dgrp_dpa_table
[];
84 static ssize_t
config_proc_write(struct file
*file
, const char __user
*buffer
,
85 size_t count
, loff_t
*pos
);
87 static int nodeinfo_proc_open(struct inode
*inode
, struct file
*file
);
88 static int info_proc_open(struct inode
*inode
, struct file
*file
);
89 static int config_proc_open(struct inode
*inode
, struct file
*file
);
91 static struct file_operations config_proc_file_ops
= {
93 .open
= config_proc_open
,
96 .release
= seq_release
,
97 .write
= config_proc_write
100 static struct file_operations info_proc_file_ops
= {
101 .owner
= THIS_MODULE
,
102 .open
= info_proc_open
,
105 .release
= seq_release
,
108 static struct file_operations nodeinfo_proc_file_ops
= {
109 .owner
= THIS_MODULE
,
110 .open
= nodeinfo_proc_open
,
113 .release
= seq_release
,
116 static struct dgrp_proc_entry dgrp_table
[] = {
121 .proc_file_ops
= &config_proc_file_ops
,
127 .proc_file_ops
= &info_proc_file_ops
,
133 .proc_file_ops
= &nodeinfo_proc_file_ops
,
139 .child
= dgrp_net_table
145 .child
= dgrp_mon_table
151 .child
= dgrp_ports_table
157 .child
= dgrp_dpa_table
161 static struct proc_dir_entry
*net_entry_pointer
;
162 static struct proc_dir_entry
*mon_entry_pointer
;
163 static struct proc_dir_entry
*dpa_entry_pointer
;
164 static struct proc_dir_entry
*ports_entry_pointer
;
166 static struct dgrp_proc_entry dgrp_net_table
[] = {
170 static struct dgrp_proc_entry dgrp_mon_table
[] = {
174 static struct dgrp_proc_entry dgrp_ports_table
[] = {
178 static struct dgrp_proc_entry dgrp_dpa_table
[] = {
182 void dgrp_unregister_proc(void)
184 unregister_proc_table(dgrp_table
, dgrp_proc_dir_entry
);
185 net_entry_pointer
= NULL
;
186 mon_entry_pointer
= NULL
;
187 dpa_entry_pointer
= NULL
;
188 ports_entry_pointer
= NULL
;
190 if (dgrp_proc_dir_entry
) {
191 remove_proc_entry(dgrp_proc_dir_entry
->name
,
192 dgrp_proc_dir_entry
->parent
);
193 dgrp_proc_dir_entry
= NULL
;
198 void dgrp_register_proc(void)
201 * Register /proc/dgrp
203 dgrp_proc_dir_entry
= proc_create("dgrp", S_IFDIR
, NULL
,
204 &dgrp_proc_file_ops
);
205 register_proc_table(dgrp_table
, dgrp_proc_dir_entry
);
211 static int dgrp_proc_match(int len
, const char *name
, struct proc_dir_entry
*de
)
213 if (!de
|| !de
->low_ino
)
215 if (de
->namelen
!= len
)
217 return !memcmp(name
, de
->name
, len
);
222 * Scan the entries in table and add them all to /proc at the position
223 * referred to by "root"
225 static void register_proc_table(struct dgrp_proc_entry
*table
,
226 struct proc_dir_entry
*root
)
228 struct proc_dir_entry
*de
;
235 for (; table
->id
; table
++) {
236 /* Can't do anything without a proc name. */
240 /* Maybe we can't do anything with it... */
241 if (!table
->proc_file_ops
&&
243 pr_warn("dgrp: Can't register %s\n",
248 len
= strlen(table
->name
);
256 for (de
= root
->subdir
; de
; de
= de
->next
) {
257 if (dgrp_proc_match(len
, table
->name
, de
))
260 /* If the subdir exists already, de is non-NULL */
264 de
= create_proc_entry(table
->name
, mode
, root
);
267 de
->data
= (void *) table
;
269 de
->proc_iops
= &proc_inode_ops
;
270 if (table
->proc_file_ops
)
271 de
->proc_fops
= table
->proc_file_ops
;
273 de
->proc_fops
= &dgrp_proc_file_ops
;
277 if (de
->mode
& S_IFDIR
)
278 register_proc_table(table
->child
, de
);
280 if (table
->id
== DGRP_NETDIR
)
281 net_entry_pointer
= de
;
283 if (table
->id
== DGRP_MONDIR
)
284 mon_entry_pointer
= de
;
286 if (table
->id
== DGRP_DPADIR
)
287 dpa_entry_pointer
= de
;
289 if (table
->id
== DGRP_PORTSDIR
)
290 ports_entry_pointer
= de
;
295 * Unregister a /proc sysctl table and any subdirectories.
297 static void unregister_proc_table(struct dgrp_proc_entry
*table
,
298 struct proc_dir_entry
*root
)
300 struct proc_dir_entry
*de
;
301 struct nd_struct
*tmp
;
306 list_for_each_entry(tmp
, &nd_struct_list
, list
) {
307 if ((table
== dgrp_net_table
) && (tmp
->nd_net_de
)) {
308 unregister_dgrp_device(tmp
->nd_net_de
);
309 dgrp_remove_node_class_sysfs_files(tmp
);
312 if ((table
== dgrp_mon_table
) && (tmp
->nd_mon_de
))
313 unregister_dgrp_device(tmp
->nd_mon_de
);
315 if ((table
== dgrp_dpa_table
) && (tmp
->nd_dpa_de
))
316 unregister_dgrp_device(tmp
->nd_dpa_de
);
318 if ((table
== dgrp_ports_table
) && (tmp
->nd_ports_de
))
319 unregister_dgrp_device(tmp
->nd_ports_de
);
322 for (; table
->id
; table
++) {
327 if (de
->mode
& S_IFDIR
) {
329 pr_alert("dgrp: malformed sysctl tree on free\n");
332 unregister_proc_table(table
->child
, de
);
334 /* Don't unregister directories which still have entries */
339 /* Don't unregister proc entries that are still being used.. */
340 if ((atomic_read(&de
->count
)) != 1) {
341 pr_alert("proc entry %s in use, not removing\n",
346 remove_proc_entry(de
->name
, de
->parent
);
351 static int dgrp_gen_proc_open(struct inode
*inode
, struct file
*file
)
353 struct proc_dir_entry
*de
;
354 struct dgrp_proc_entry
*entry
;
357 de
= (struct proc_dir_entry
*) PDE(file_inode(file
));
358 if (!de
|| !de
->data
) {
363 entry
= (struct dgrp_proc_entry
*) de
->data
;
369 down(&entry
->excl_sem
);
376 up(&entry
->excl_sem
);
382 static int dgrp_gen_proc_close(struct inode
*inode
, struct file
*file
)
384 struct proc_dir_entry
*de
;
385 struct dgrp_proc_entry
*entry
;
387 de
= (struct proc_dir_entry
*) PDE(file_inode(file
));
388 if (!de
|| !de
->data
)
391 entry
= (struct dgrp_proc_entry
*) de
->data
;
395 down(&entry
->excl_sem
);
400 up(&entry
->excl_sem
);
406 static void *config_proc_start(struct seq_file
*m
, loff_t
*pos
)
408 return seq_list_start_head(&nd_struct_list
, *pos
);
411 static void *config_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
413 return seq_list_next(v
, &nd_struct_list
, pos
);
416 static void config_proc_stop(struct seq_file
*m
, void *v
)
420 static int config_proc_show(struct seq_file
*m
, void *v
)
422 struct nd_struct
*nd
;
425 if (v
== &nd_struct_list
) {
426 seq_puts(m
, "#-----------------------------------------------------------------------------\n");
427 seq_puts(m
, "# Avail\n");
428 seq_puts(m
, "# ID Major State Ports\n");
432 nd
= list_entry(v
, struct nd_struct
, list
);
434 ID_TO_CHAR(nd
->nd_ID
, tmp_id
);
436 seq_printf(m
, " %-2.2s %-5ld %-10.10s %-5d\n",
439 ND_STATE_STR(nd
->nd_state
),
445 static const struct seq_operations proc_config_ops
= {
446 .start
= config_proc_start
,
447 .next
= config_proc_next
,
448 .stop
= config_proc_stop
,
449 .show
= config_proc_show
452 static int config_proc_open(struct inode
*inode
, struct file
*file
)
454 return seq_open(file
, &proc_config_ops
);
459 * When writing configuration information, each "record" (i.e. each
460 * write) is treated as an independent request. See the "parse"
461 * description for more details.
463 static ssize_t
config_proc_write(struct file
*file
, const char __user
*buffer
,
464 size_t count
, loff_t
*pos
)
473 inbuf
= sp
= vzalloc(count
+ 1);
477 if (copy_from_user(inbuf
, buffer
, count
)) {
486 line
= strpbrk(sp
, ldelim
);
489 retval
= parse_write_config(sp
);
494 line
= strpbrk(sp
, ldelim
);
504 * ------------------------------------------------------------------------
506 * The following are the functions to parse input
508 * ------------------------------------------------------------------------
510 static inline char *skip_past_ws(const char *str
)
512 while ((*str
) && !isspace(*str
))
515 return skip_spaces(str
);
518 static int parse_id(char **c
, char *cID
)
522 if (isalnum(tmp
) || (tmp
== '_'))
529 if (isalnum(tmp
) || (tmp
== '_')) {
538 static int parse_add_config(char *buf
)
547 retval
= parse_id(&c
, cID
);
551 ID
= CHAR_TO_ID(cID
);
555 return dgrp_add_id(ID
);
558 static int parse_del_config(char *buf
)
562 struct nd_struct
*nd
;
569 retval
= parse_id(&c
, cID
);
573 ID
= CHAR_TO_ID(cID
);
577 retval
= kstrtol(c
, 10, &major
);
581 nd
= nd_struct_get(major
);
585 if ((nd
->nd_major
!= major
) || (nd
->nd_ID
!= ID
))
588 return dgrp_remove_nd(nd
);
591 static int parse_chg_config(char *buf
)
597 * The passed character buffer represents a single configuration request.
598 * If the first character is a "+", it is parsed as a request to add a
600 * If the first character is a "-", it is parsed as a request to delete a
602 * If the first character is a "*", it is parsed as a request to change a
604 * Any other character (including whitespace) causes the record to be
607 static int parse_write_config(char *buf
)
613 retval
= parse_add_config(buf
);
616 retval
= parse_del_config(buf
);
619 retval
= parse_chg_config(buf
);
628 static int info_proc_show(struct seq_file
*m
, void *v
)
630 seq_printf(m
, "version: %s\n", DIGI_VERSION
);
631 seq_puts(m
, "register_with_sysfs: 1\n");
632 seq_printf(m
, "pollrate: 0x%08x\t(%d)\n",
633 dgrp_poll_tick
, dgrp_poll_tick
);
638 static int info_proc_open(struct inode
*inode
, struct file
*file
)
640 return single_open(file
, info_proc_show
, NULL
);
644 static void *nodeinfo_start(struct seq_file
*m
, loff_t
*pos
)
646 return seq_list_start_head(&nd_struct_list
, *pos
);
649 static void *nodeinfo_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
651 return seq_list_next(v
, &nd_struct_list
, pos
);
654 static void nodeinfo_stop(struct seq_file
*m
, void *v
)
658 static int nodeinfo_show(struct seq_file
*m
, void *v
)
660 struct nd_struct
*nd
;
665 if (v
== &nd_struct_list
) {
666 seq_puts(m
, "#-----------------------------------------------------------------------------\n");
667 seq_puts(m
, "# HW HW SW\n");
668 seq_puts(m
, "# ID State Version ID Version Description\n");
672 nd
= list_entry(v
, struct nd_struct
, list
);
674 ID_TO_CHAR(nd
->nd_ID
, tmp_id
);
676 if (nd
->nd_state
== NS_READY
) {
677 sprintf(hwver
, "%d.%d", (nd
->nd_hw_ver
>> 8) & 0xff,
678 nd
->nd_hw_ver
& 0xff);
679 sprintf(swver
, "%d.%d", (nd
->nd_sw_ver
>> 8) & 0xff,
680 nd
->nd_sw_ver
& 0xff);
681 seq_printf(m
, " %-2.2s %-10.10s %-7.7s %-3d %-7.7s %-35.35s\n",
683 ND_STATE_STR(nd
->nd_state
),
690 seq_printf(m
, " %-2.2s %-10.10s\n",
692 ND_STATE_STR(nd
->nd_state
));
699 static const struct seq_operations nodeinfo_ops
= {
700 .start
= nodeinfo_start
,
701 .next
= nodeinfo_next
,
702 .stop
= nodeinfo_stop
,
703 .show
= nodeinfo_show
706 static int nodeinfo_proc_open(struct inode
*inode
, struct file
*file
)
708 return seq_open(file
, &nodeinfo_ops
);
712 * dgrp_add_id() -- creates new nd struct and adds it to list
713 * @id: id of device to add
715 static int dgrp_add_id(long id
)
717 struct nd_struct
*nd
;
721 nd
= kzalloc(sizeof(struct nd_struct
), GFP_KERNEL
);
728 spin_lock_init(&nd
->nd_lock
);
730 init_waitqueue_head(&nd
->nd_tx_waitq
);
731 init_waitqueue_head(&nd
->nd_mon_wqueue
);
732 init_waitqueue_head(&nd
->nd_dpa_wqueue
);
733 for (i
= 0; i
< SEQ_MAX
; i
++)
734 init_waitqueue_head(&nd
->nd_seq_wque
[i
]);
736 /* setup the structures to get the major number */
737 ret
= dgrp_tty_init(nd
);
741 nd
->nd_major
= nd
->nd_serial_ttdriver
->major
;
743 ret
= nd_struct_add(nd
);
747 register_dgrp_device(nd
, net_entry_pointer
, dgrp_register_net_hook
);
748 register_dgrp_device(nd
, mon_entry_pointer
, dgrp_register_mon_hook
);
749 register_dgrp_device(nd
, dpa_entry_pointer
, dgrp_register_dpa_hook
);
750 register_dgrp_device(nd
, ports_entry_pointer
,
751 dgrp_register_ports_hook
);
755 /* FIXME this guy should free the tty driver stored in nd and destroy
756 * all channel ports */
763 static int dgrp_remove_nd(struct nd_struct
*nd
)
767 /* Check to see if the selected structure is in use */
768 if (nd
->nd_tty_ref_cnt
)
772 unregister_dgrp_device(nd
->nd_net_de
);
773 dgrp_remove_node_class_sysfs_files(nd
);
777 unregister_dgrp_device(nd
->nd_mon_de
);
780 unregister_dgrp_device(nd
->nd_ports_de
);
783 unregister_dgrp_device(nd
->nd_dpa_de
);
787 ret
= nd_struct_del(nd
);
795 static void register_dgrp_device(struct nd_struct
*node
,
796 struct proc_dir_entry
*root
,
797 void (*register_hook
)(struct proc_dir_entry
*de
))
800 struct proc_dir_entry
*de
;
802 ID_TO_CHAR(node
->nd_ID
, buf
);
804 de
= create_proc_entry(buf
, 0600 | S_IFREG
, root
);
808 de
->data
= (void *) node
;
815 static void unregister_dgrp_device(struct proc_dir_entry
*de
)
820 /* Don't unregister proc entries that are still being used.. */
821 if ((atomic_read(&de
->count
)) != 1) {
822 pr_alert("%s - proc entry %s in use. Not removing.\n",
827 remove_proc_entry(de
->name
, de
->parent
);