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
dgrp_config_proc_write(struct file
*file
,
85 const char __user
*buffer
,
86 size_t count
, loff_t
*pos
);
88 static int dgrp_nodeinfo_proc_open(struct inode
*inode
, struct file
*file
);
89 static int dgrp_info_proc_open(struct inode
*inode
, struct file
*file
);
90 static int dgrp_config_proc_open(struct inode
*inode
, struct file
*file
);
92 static struct file_operations config_proc_file_ops
= {
94 .open
= dgrp_config_proc_open
,
97 .release
= seq_release
,
98 .write
= dgrp_config_proc_write
,
101 static struct file_operations info_proc_file_ops
= {
102 .owner
= THIS_MODULE
,
103 .open
= dgrp_info_proc_open
,
106 .release
= single_release
,
109 static struct file_operations nodeinfo_proc_file_ops
= {
110 .owner
= THIS_MODULE
,
111 .open
= dgrp_nodeinfo_proc_open
,
114 .release
= seq_release
,
117 static struct dgrp_proc_entry dgrp_table
[] = {
122 .proc_file_ops
= &config_proc_file_ops
,
128 .proc_file_ops
= &info_proc_file_ops
,
134 .proc_file_ops
= &nodeinfo_proc_file_ops
,
140 .child
= dgrp_net_table
146 .child
= dgrp_mon_table
152 .child
= dgrp_ports_table
158 .child
= dgrp_dpa_table
162 static struct proc_dir_entry
*net_entry_pointer
;
163 static struct proc_dir_entry
*mon_entry_pointer
;
164 static struct proc_dir_entry
*dpa_entry_pointer
;
165 static struct proc_dir_entry
*ports_entry_pointer
;
167 static struct dgrp_proc_entry dgrp_net_table
[] = {
171 static struct dgrp_proc_entry dgrp_mon_table
[] = {
175 static struct dgrp_proc_entry dgrp_ports_table
[] = {
179 static struct dgrp_proc_entry dgrp_dpa_table
[] = {
183 void dgrp_unregister_proc(void)
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 unregister_proc_table(dgrp_table
, dgrp_proc_dir_entry
);
192 remove_proc_entry(dgrp_proc_dir_entry
->name
,
193 dgrp_proc_dir_entry
->parent
);
194 dgrp_proc_dir_entry
= NULL
;
199 void dgrp_register_proc(void)
202 * Register /proc/dgrp
204 dgrp_proc_dir_entry
= proc_create("dgrp", S_IFDIR
, NULL
,
205 &dgrp_proc_file_ops
);
206 register_proc_table(dgrp_table
, dgrp_proc_dir_entry
);
212 static int dgrp_proc_match(int len
, const char *name
, struct proc_dir_entry
*de
)
214 if (!de
|| !de
->low_ino
)
216 if (de
->namelen
!= len
)
218 return !memcmp(name
, de
->name
, len
);
223 * Scan the entries in table and add them all to /proc at the position
224 * referred to by "root"
226 static void register_proc_table(struct dgrp_proc_entry
*table
,
227 struct proc_dir_entry
*root
)
229 struct proc_dir_entry
*de
;
238 for (; table
->id
; table
++) {
239 /* Can't do anything without a proc name. */
243 /* Maybe we can't do anything with it... */
244 if (!table
->proc_file_ops
&&
246 pr_warn("dgrp: Can't register %s\n",
251 len
= strlen(table
->name
);
259 for (de
= root
->subdir
; de
; de
= de
->next
) {
260 if (dgrp_proc_match(len
, table
->name
, de
))
263 /* If the subdir exists already, de is non-NULL */
267 de
= create_proc_entry(table
->name
, mode
, root
);
270 de
->data
= (void *) table
;
272 de
->proc_iops
= &proc_inode_ops
;
273 if (table
->proc_file_ops
)
274 de
->proc_fops
= table
->proc_file_ops
;
276 de
->proc_fops
= &dgrp_proc_file_ops
;
280 if (de
->mode
& S_IFDIR
)
281 register_proc_table(table
->child
, de
);
283 if (table
->id
== DGRP_NETDIR
)
284 net_entry_pointer
= de
;
286 if (table
->id
== DGRP_MONDIR
)
287 mon_entry_pointer
= de
;
289 if (table
->id
== DGRP_DPADIR
)
290 dpa_entry_pointer
= de
;
292 if (table
->id
== DGRP_PORTSDIR
)
293 ports_entry_pointer
= de
;
298 * Unregister a /proc sysctl table and any subdirectories.
300 static void unregister_proc_table(struct dgrp_proc_entry
*table
,
301 struct proc_dir_entry
*root
)
303 struct proc_dir_entry
*de
;
304 struct nd_struct
*tmp
;
309 list_for_each_entry(tmp
, &nd_struct_list
, list
) {
310 if ((table
== dgrp_net_table
) && (tmp
->nd_net_de
)) {
311 unregister_dgrp_device(tmp
->nd_net_de
);
312 dgrp_remove_node_class_sysfs_files(tmp
);
315 if ((table
== dgrp_mon_table
) && (tmp
->nd_mon_de
))
316 unregister_dgrp_device(tmp
->nd_mon_de
);
318 if ((table
== dgrp_dpa_table
) && (tmp
->nd_dpa_de
))
319 unregister_dgrp_device(tmp
->nd_dpa_de
);
321 if ((table
== dgrp_ports_table
) && (tmp
->nd_ports_de
))
322 unregister_dgrp_device(tmp
->nd_ports_de
);
325 for (; table
->id
; table
++) {
330 if (de
->mode
& S_IFDIR
) {
332 pr_alert("dgrp: malformed sysctl tree on free\n");
335 unregister_proc_table(table
->child
, de
);
337 /* Don't unregister directories which still have entries */
342 /* Don't unregister proc entries that are still being used.. */
343 if ((atomic_read(&de
->count
)) != 1) {
344 pr_alert("proc entry %s in use, not removing\n",
349 remove_proc_entry(de
->name
, de
->parent
);
354 static int dgrp_gen_proc_open(struct inode
*inode
, struct file
*file
)
356 struct proc_dir_entry
*de
;
357 struct dgrp_proc_entry
*entry
;
360 de
= (struct proc_dir_entry
*) PDE(file
->f_dentry
->d_inode
);
361 if (!de
|| !de
->data
) {
366 entry
= (struct dgrp_proc_entry
*) de
->data
;
372 down(&entry
->excl_sem
);
379 up(&entry
->excl_sem
);
385 static int dgrp_gen_proc_close(struct inode
*inode
, struct file
*file
)
387 struct proc_dir_entry
*de
;
388 struct dgrp_proc_entry
*entry
;
390 de
= (struct proc_dir_entry
*) PDE(file
->f_dentry
->d_inode
);
391 if (!de
|| !de
->data
)
394 entry
= (struct dgrp_proc_entry
*) de
->data
;
398 down(&entry
->excl_sem
);
403 up(&entry
->excl_sem
);
409 static void *dgrp_config_proc_start(struct seq_file
*m
, loff_t
*pos
)
411 return seq_list_start_head(&nd_struct_list
, *pos
);
414 static void *dgrp_config_proc_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
416 return seq_list_next(v
, &nd_struct_list
, pos
);
419 static void dgrp_config_proc_stop(struct seq_file
*m
, void *v
)
423 static int dgrp_config_proc_show(struct seq_file
*m
, void *v
)
425 struct nd_struct
*nd
;
428 if (v
== &nd_struct_list
) {
429 seq_puts(m
, "#-----------------------------------------------------------------------------\n");
430 seq_puts(m
, "# Avail\n");
431 seq_puts(m
, "# ID Major State Ports\n");
435 nd
= list_entry(v
, struct nd_struct
, list
);
437 ID_TO_CHAR(nd
->nd_ID
, tmp_id
);
439 seq_printf(m
, " %-2.2s %-5ld %-10.10s %-5d\n",
442 ND_STATE_STR(nd
->nd_state
),
448 static const struct seq_operations proc_config_ops
= {
449 .start
= dgrp_config_proc_start
,
450 .next
= dgrp_config_proc_next
,
451 .stop
= dgrp_config_proc_stop
,
452 .show
= dgrp_config_proc_show
,
455 static int dgrp_config_proc_open(struct inode
*inode
, struct file
*file
)
457 return seq_open(file
, &proc_config_ops
);
462 * When writing configuration information, each "record" (i.e. each
463 * write) is treated as an independent request. See the "parse"
464 * description for more details.
466 static ssize_t
dgrp_config_proc_write(struct file
*file
,
467 const char __user
*buffer
,
468 size_t count
, loff_t
*pos
)
477 inbuf
= sp
= vzalloc(count
+ 1);
481 if (copy_from_user(inbuf
, buffer
, count
)) {
490 line
= strpbrk(sp
, ldelim
);
493 retval
= parse_write_config(sp
);
498 line
= strpbrk(sp
, ldelim
);
508 * ------------------------------------------------------------------------
510 * The following are the functions to parse input
512 * ------------------------------------------------------------------------
514 static inline char *skip_past_ws(const char *str
)
516 while ((*str
) && !isspace(*str
))
519 return skip_spaces(str
);
522 static int parse_id(char **c
, char *cID
)
526 if (isalnum(tmp
) || (tmp
== '_'))
533 if (isalnum(tmp
) || (tmp
== '_')) {
542 static int parse_add_config(char *buf
)
551 retval
= parse_id(&c
, cID
);
555 ID
= CHAR_TO_ID(cID
);
559 return dgrp_add_id(ID
);
562 static int parse_del_config(char *buf
)
566 struct nd_struct
*nd
;
573 retval
= parse_id(&c
, cID
);
577 ID
= CHAR_TO_ID(cID
);
581 retval
= kstrtol(c
, 10, &major
);
585 nd
= nd_struct_get(major
);
589 if ((nd
->nd_major
!= major
) || (nd
->nd_ID
!= ID
))
592 return dgrp_remove_nd(nd
);
595 static int parse_chg_config(char *buf
)
601 * The passed character buffer represents a single configuration request.
602 * If the first character is a "+", it is parsed as a request to add a
604 * If the first character is a "-", it is parsed as a request to delete a
606 * If the first character is a "*", it is parsed as a request to change a
608 * Any other character (including whitespace) causes the record to be
611 static int parse_write_config(char *buf
)
617 retval
= parse_add_config(buf
);
620 retval
= parse_del_config(buf
);
623 retval
= parse_chg_config(buf
);
632 static int dgrp_info_proc_show(struct seq_file
*m
, void *v
)
634 seq_printf(m
, "version: %s\n", DIGI_VERSION
);
635 seq_puts(m
, "register_with_sysfs: 1\n");
636 seq_printf(m
, "pollrate: 0x%08x\t(%d)\n",
637 dgrp_poll_tick
, dgrp_poll_tick
);
642 static int dgrp_info_proc_open(struct inode
*inode
, struct file
*file
)
644 return single_open(file
, dgrp_info_proc_show
, NULL
);
648 static void *dgrp_nodeinfo_start(struct seq_file
*m
, loff_t
*pos
)
650 return seq_list_start_head(&nd_struct_list
, *pos
);
653 static void *dgrp_nodeinfo_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
655 return seq_list_next(v
, &nd_struct_list
, pos
);
658 static void dgrp_nodeinfo_stop(struct seq_file
*m
, void *v
)
662 static int dgrp_nodeinfo_show(struct seq_file
*m
, void *v
)
664 struct nd_struct
*nd
;
669 if (v
== &nd_struct_list
) {
670 seq_puts(m
, "#-----------------------------------------------------------------------------\n");
671 seq_puts(m
, "# HW HW SW\n");
672 seq_puts(m
, "# ID State Version ID Version Description\n");
676 nd
= list_entry(v
, struct nd_struct
, list
);
678 ID_TO_CHAR(nd
->nd_ID
, tmp_id
);
680 if (nd
->nd_state
== NS_READY
) {
681 sprintf(hwver
, "%d.%d", (nd
->nd_hw_ver
>> 8) & 0xff,
682 nd
->nd_hw_ver
& 0xff);
683 sprintf(swver
, "%d.%d", (nd
->nd_sw_ver
>> 8) & 0xff,
684 nd
->nd_sw_ver
& 0xff);
685 seq_printf(m
, " %-2.2s %-10.10s %-7.7s %-3d %-7.7s %-35.35s\n",
687 ND_STATE_STR(nd
->nd_state
),
694 seq_printf(m
, " %-2.2s %-10.10s\n",
696 ND_STATE_STR(nd
->nd_state
));
703 static const struct seq_operations nodeinfo_ops
= {
704 .start
= dgrp_nodeinfo_start
,
705 .next
= dgrp_nodeinfo_next
,
706 .stop
= dgrp_nodeinfo_stop
,
707 .show
= dgrp_nodeinfo_show
,
710 static int dgrp_nodeinfo_proc_open(struct inode
*inode
, struct file
*file
)
712 return seq_open(file
, &nodeinfo_ops
);
716 * dgrp_add_id() -- creates new nd struct and adds it to list
717 * @id: id of device to add
719 static int dgrp_add_id(long id
)
721 struct nd_struct
*nd
;
725 nd
= kzalloc(sizeof(struct nd_struct
), GFP_KERNEL
);
732 spin_lock_init(&nd
->nd_lock
);
734 init_waitqueue_head(&nd
->nd_tx_waitq
);
735 init_waitqueue_head(&nd
->nd_mon_wqueue
);
736 init_waitqueue_head(&nd
->nd_dpa_wqueue
);
737 for (i
= 0; i
< SEQ_MAX
; i
++)
738 init_waitqueue_head(&nd
->nd_seq_wque
[i
]);
740 /* setup the structures to get the major number */
741 ret
= dgrp_tty_init(nd
);
745 nd
->nd_major
= nd
->nd_serial_ttdriver
->major
;
747 ret
= nd_struct_add(nd
);
751 register_dgrp_device(nd
, net_entry_pointer
, dgrp_register_net_hook
);
752 register_dgrp_device(nd
, mon_entry_pointer
, dgrp_register_mon_hook
);
753 register_dgrp_device(nd
, dpa_entry_pointer
, dgrp_register_dpa_hook
);
754 register_dgrp_device(nd
, ports_entry_pointer
,
755 dgrp_register_ports_hook
);
759 /* FIXME this guy should free the tty driver stored in nd and destroy
760 * all channel ports */
767 static int dgrp_remove_nd(struct nd_struct
*nd
)
771 /* Check to see if the selected structure is in use */
772 if (nd
->nd_tty_ref_cnt
)
776 unregister_dgrp_device(nd
->nd_net_de
);
777 dgrp_remove_node_class_sysfs_files(nd
);
780 unregister_dgrp_device(nd
->nd_mon_de
);
782 unregister_dgrp_device(nd
->nd_ports_de
);
784 unregister_dgrp_device(nd
->nd_dpa_de
);
788 ret
= nd_struct_del(nd
);
796 static void register_dgrp_device(struct nd_struct
*node
,
797 struct proc_dir_entry
*root
,
798 void (*register_hook
)(struct proc_dir_entry
*de
))
801 struct proc_dir_entry
*de
;
803 ID_TO_CHAR(node
->nd_ID
, buf
);
805 de
= create_proc_entry(buf
, 0600 | S_IFREG
, root
);
809 de
->data
= (void *) node
;
816 static void unregister_dgrp_device(struct proc_dir_entry
*de
)
821 /* Don't unregister proc entries that are still being used.. */
822 if ((atomic_read(&de
->count
)) != 1) {
823 pr_alert("%s - proc entry %s in use. Not removing.\n",
828 remove_proc_entry(de
->name
, de
->parent
);