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/vmalloc.h>
44 #include "dgrp_common.h"
46 static struct dgrp_proc_entry dgrp_table[];
47 static struct proc_dir_entry *dgrp_proc_dir_entry;
49 static int dgrp_add_id(long id);
50 static int dgrp_remove_nd(struct nd_struct *nd);
51 static void unregister_dgrp_device(struct proc_dir_entry *de);
52 static void register_dgrp_device(struct nd_struct *node,
53 struct proc_dir_entry *root,
54 void (*register_hook)(struct proc_dir_entry *de));
56 /* File operation declarations */
57 static int dgrp_gen_proc_open(struct inode *, struct file *);
58 static int dgrp_gen_proc_close(struct inode *, struct file *);
59 static int parse_write_config(char *);
62 static const struct file_operations dgrp_proc_file_ops = {
64 .open = dgrp_gen_proc_open,
65 .release = dgrp_gen_proc_close,
68 static struct inode_operations proc_inode_ops = {
69 .permission = dgrp_inode_permission
73 static void register_proc_table(struct dgrp_proc_entry *,
74 struct proc_dir_entry *);
75 static void unregister_proc_table(struct dgrp_proc_entry *,
76 struct proc_dir_entry *);
78 static struct dgrp_proc_entry dgrp_net_table[];
79 static struct dgrp_proc_entry dgrp_mon_table[];
80 static struct dgrp_proc_entry dgrp_ports_table[];
81 static struct dgrp_proc_entry dgrp_dpa_table[];
83 static ssize_t config_proc_write(struct file *file, const char __user *buffer,
84 size_t count, loff_t *pos);
86 static int nodeinfo_proc_open(struct inode *inode, struct file *file);
87 static int info_proc_open(struct inode *inode, struct file *file);
88 static int config_proc_open(struct inode *inode, struct file *file);
90 static struct file_operations config_proc_file_ops = {
92 .open = config_proc_open,
95 .release = seq_release,
96 .write = config_proc_write
99 static struct file_operations info_proc_file_ops = {
100 .owner = THIS_MODULE,
101 .open = info_proc_open,
104 .release = seq_release,
107 static struct file_operations nodeinfo_proc_file_ops = {
108 .owner = THIS_MODULE,
109 .open = nodeinfo_proc_open,
112 .release = seq_release,
115 static struct dgrp_proc_entry dgrp_table[] = {
120 .proc_file_ops = &config_proc_file_ops,
126 .proc_file_ops = &info_proc_file_ops,
132 .proc_file_ops = &nodeinfo_proc_file_ops,
138 .child = dgrp_net_table
144 .child = dgrp_mon_table
150 .child = dgrp_ports_table
156 .child = dgrp_dpa_table
160 static struct proc_dir_entry *net_entry_pointer;
161 static struct proc_dir_entry *mon_entry_pointer;
162 static struct proc_dir_entry *dpa_entry_pointer;
163 static struct proc_dir_entry *ports_entry_pointer;
165 static struct dgrp_proc_entry dgrp_net_table[] = {
169 static struct dgrp_proc_entry dgrp_mon_table[] = {
173 static struct dgrp_proc_entry dgrp_ports_table[] = {
177 static struct dgrp_proc_entry dgrp_dpa_table[] = {
181 void dgrp_unregister_proc(void)
183 unregister_proc_table(dgrp_table, dgrp_proc_dir_entry);
184 net_entry_pointer = NULL;
185 mon_entry_pointer = NULL;
186 dpa_entry_pointer = NULL;
187 ports_entry_pointer = NULL;
189 if (dgrp_proc_dir_entry) {
190 remove_proc_entry(dgrp_proc_dir_entry->name,
191 dgrp_proc_dir_entry->parent);
192 dgrp_proc_dir_entry = NULL;
197 void dgrp_register_proc(void)
200 * Register /proc/dgrp
202 dgrp_proc_dir_entry = proc_create("dgrp", S_IFDIR, NULL,
203 &dgrp_proc_file_ops);
204 register_proc_table(dgrp_table, dgrp_proc_dir_entry);
210 static int dgrp_proc_match(int len, const char *name, struct proc_dir_entry *de)
212 if (!de || !de->low_ino)
214 if (de->namelen != len)
216 return !memcmp(name, de->name, len);
221 * Scan the entries in table and add them all to /proc at the position
222 * referred to by "root"
224 static void register_proc_table(struct dgrp_proc_entry *table,
225 struct proc_dir_entry *root)
227 struct proc_dir_entry *de;
231 for (; table->id; table++) {
232 /* Can't do anything without a proc name. */
236 /* Maybe we can't do anything with it... */
237 if (!table->proc_file_ops &&
239 pr_warn("dgrp: Can't register %s\n",
244 len = strlen(table->name);
252 for (de = root->subdir; de; de = de->next) {
253 if (dgrp_proc_match(len, table->name, de))
256 /* If the subdir exists already, de is non-NULL */
260 de = create_proc_entry(table->name, mode, root);
263 de->data = (void *) table;
265 de->proc_iops = &proc_inode_ops;
266 if (table->proc_file_ops)
267 de->proc_fops = table->proc_file_ops;
269 de->proc_fops = &dgrp_proc_file_ops;
273 if (de->mode & S_IFDIR)
274 register_proc_table(table->child, de);
276 if (table->id == DGRP_NETDIR)
277 net_entry_pointer = de;
279 if (table->id == DGRP_MONDIR)
280 mon_entry_pointer = de;
282 if (table->id == DGRP_DPADIR)
283 dpa_entry_pointer = de;
285 if (table->id == DGRP_PORTSDIR)
286 ports_entry_pointer = de;
291 * Unregister a /proc sysctl table and any subdirectories.
293 static void unregister_proc_table(struct dgrp_proc_entry *table,
294 struct proc_dir_entry *root)
296 struct proc_dir_entry *de;
297 struct nd_struct *tmp;
299 list_for_each_entry(tmp, &nd_struct_list, list) {
300 if ((table == dgrp_net_table) && (tmp->nd_net_de)) {
301 unregister_dgrp_device(tmp->nd_net_de);
302 dgrp_remove_node_class_sysfs_files(tmp);
305 if ((table == dgrp_mon_table) && (tmp->nd_mon_de))
306 unregister_dgrp_device(tmp->nd_mon_de);
308 if ((table == dgrp_dpa_table) && (tmp->nd_dpa_de))
309 unregister_dgrp_device(tmp->nd_dpa_de);
311 if ((table == dgrp_ports_table) && (tmp->nd_ports_de))
312 unregister_dgrp_device(tmp->nd_ports_de);
315 for (; table->id; table++) {
320 if (de->mode & S_IFDIR) {
322 pr_alert("dgrp: malformed sysctl tree on free\n");
325 unregister_proc_table(table->child, de);
327 /* Don't unregister directories which still have entries */
332 /* Don't unregister proc entries that are still being used.. */
333 if ((atomic_read(&de->count)) != 1) {
334 pr_alert("proc entry %s in use, not removing\n",
339 remove_proc_entry(de->name, de->parent);
344 static int dgrp_gen_proc_open(struct inode *inode, struct file *file)
346 struct proc_dir_entry *de;
347 struct dgrp_proc_entry *entry;
350 de = (struct proc_dir_entry *) PDE(file->f_dentry->d_inode);
351 if (!de || !de->data) {
356 entry = (struct dgrp_proc_entry *) de->data;
362 down(&entry->excl_sem);
369 up(&entry->excl_sem);
375 static int dgrp_gen_proc_close(struct inode *inode, struct file *file)
377 struct proc_dir_entry *de;
378 struct dgrp_proc_entry *entry;
380 de = (struct proc_dir_entry *) PDE(file->f_dentry->d_inode);
381 if (!de || !de->data)
384 entry = (struct dgrp_proc_entry *) de->data;
388 down(&entry->excl_sem);
393 up(&entry->excl_sem);
399 static void *config_proc_start(struct seq_file *m, loff_t *pos)
401 return seq_list_start_head(&nd_struct_list, *pos);
404 static void *config_proc_next(struct seq_file *p, void *v, loff_t *pos)
406 return seq_list_next(v, &nd_struct_list, pos);
409 static void config_proc_stop(struct seq_file *m, void *v)
413 static int config_proc_show(struct seq_file *m, void *v)
415 struct nd_struct *nd;
418 if (v == &nd_struct_list) {
419 seq_puts(m, "#-----------------------------------------------------------------------------\n");
420 seq_puts(m, "# Avail\n");
421 seq_puts(m, "# ID Major State Ports\n");
425 nd = list_entry(v, struct nd_struct, list);
427 ID_TO_CHAR(nd->nd_ID, tmp_id);
429 seq_printf(m, " %-2.2s %-5ld %-10.10s %-5d\n",
432 ND_STATE_STR(nd->nd_state),
438 static const struct seq_operations proc_config_ops = {
439 .start = config_proc_start,
440 .next = config_proc_next,
441 .stop = config_proc_stop,
442 .show = config_proc_show
445 static int config_proc_open(struct inode *inode, struct file *file)
447 return seq_open(file, &proc_config_ops);
452 * When writing configuration information, each "record" (i.e. each
453 * write) is treated as an independent request. See the "parse"
454 * description for more details.
456 static ssize_t config_proc_write(struct file *file, const char __user *buffer,
457 size_t count, loff_t *pos)
466 inbuf = sp = vzalloc(count + 1);
470 if (copy_from_user(inbuf, buffer, count)) {
479 line = strpbrk(sp, ldelim);
482 retval = parse_write_config(sp);
487 line = strpbrk(sp, ldelim);
497 * ------------------------------------------------------------------------
499 * The following are the functions to parse input
501 * ------------------------------------------------------------------------
503 static inline char *skip_past_ws(const char *str)
505 while ((*str) && !isspace(*str))
508 return skip_spaces(str);
511 static int parse_id(char **c, char *cID)
515 if (isalnum(tmp) || (tmp == '_'))
522 if (isalnum(tmp) || (tmp == '_')) {
531 static int parse_add_config(char *buf)
540 retval = parse_id(&c, cID);
544 ID = CHAR_TO_ID(cID);
548 return dgrp_add_id(ID);
551 static int parse_del_config(char *buf)
555 struct nd_struct *nd;
562 retval = parse_id(&c, cID);
566 ID = CHAR_TO_ID(cID);
570 retval = kstrtol(c, 10, &major);
574 nd = nd_struct_get(major);
578 if ((nd->nd_major != major) || (nd->nd_ID != ID))
581 return dgrp_remove_nd(nd);
584 static int parse_chg_config(char *buf)
590 * The passed character buffer represents a single configuration request.
591 * If the first character is a "+", it is parsed as a request to add a
593 * If the first character is a "-", it is parsed as a request to delete a
595 * If the first character is a "*", it is parsed as a request to change a
597 * Any other character (including whitespace) causes the record to be
600 static int parse_write_config(char *buf)
606 retval = parse_add_config(buf);
609 retval = parse_del_config(buf);
612 retval = parse_chg_config(buf);
621 static int info_proc_show(struct seq_file *m, void *v)
623 seq_printf(m, "version: %s\n", DIGI_VERSION);
624 seq_puts(m, "register_with_sysfs: 1\n");
625 seq_printf(m, "rawreadok: 0x%08x\t(%d)\n",
626 dgrp_rawreadok, dgrp_rawreadok);
627 seq_printf(m, "pollrate: 0x%08x\t(%d)\n",
628 dgrp_poll_tick, dgrp_poll_tick);
633 static int info_proc_open(struct inode *inode, struct file *file)
635 return single_open(file, info_proc_show, NULL);
639 static void *nodeinfo_start(struct seq_file *m, loff_t *pos)
641 return seq_list_start_head(&nd_struct_list, *pos);
644 static void *nodeinfo_next(struct seq_file *p, void *v, loff_t *pos)
646 return seq_list_next(v, &nd_struct_list, pos);
649 static void nodeinfo_stop(struct seq_file *m, void *v)
653 static int nodeinfo_show(struct seq_file *m, void *v)
655 struct nd_struct *nd;
660 if (v == &nd_struct_list) {
661 seq_puts(m, "#-----------------------------------------------------------------------------\n");
662 seq_puts(m, "# HW HW SW\n");
663 seq_puts(m, "# ID State Version ID Version Description\n");
667 nd = list_entry(v, struct nd_struct, list);
669 ID_TO_CHAR(nd->nd_ID, tmp_id);
671 if (nd->nd_state == NS_READY) {
672 sprintf(hwver, "%d.%d", (nd->nd_hw_ver >> 8) & 0xff,
673 nd->nd_hw_ver & 0xff);
674 sprintf(swver, "%d.%d", (nd->nd_sw_ver >> 8) & 0xff,
675 nd->nd_sw_ver & 0xff);
676 seq_printf(m, " %-2.2s %-10.10s %-7.7s %-3d %-7.7s %-35.35s\n",
678 ND_STATE_STR(nd->nd_state),
685 seq_printf(m, " %-2.2s %-10.10s\n",
687 ND_STATE_STR(nd->nd_state));
694 static const struct seq_operations nodeinfo_ops = {
695 .start = nodeinfo_start,
696 .next = nodeinfo_next,
697 .stop = nodeinfo_stop,
698 .show = nodeinfo_show
701 static int nodeinfo_proc_open(struct inode *inode, struct file *file)
703 return seq_open(file, &nodeinfo_ops);
707 * dgrp_add_id() -- creates new nd struct and adds it to list
708 * @id: id of device to add
710 static int dgrp_add_id(long id)
712 struct nd_struct *nd;
716 nd = kzalloc(sizeof(struct nd_struct), GFP_KERNEL);
723 spin_lock_init(&nd->nd_lock);
725 init_waitqueue_head(&nd->nd_tx_waitq);
726 init_waitqueue_head(&nd->nd_mon_wqueue);
727 init_waitqueue_head(&nd->nd_dpa_wqueue);
728 for (i = 0; i < SEQ_MAX; i++)
729 init_waitqueue_head(&nd->nd_seq_wque[i]);
731 /* setup the structures to get the major number */
732 ret = dgrp_tty_init(nd);
736 nd->nd_major = nd->nd_serial_ttdriver->major;
738 ret = nd_struct_add(nd);
742 register_dgrp_device(nd, net_entry_pointer, dgrp_register_net_hook);
743 register_dgrp_device(nd, mon_entry_pointer, dgrp_register_mon_hook);
744 register_dgrp_device(nd, dpa_entry_pointer, dgrp_register_dpa_hook);
745 register_dgrp_device(nd, ports_entry_pointer,
746 dgrp_register_ports_hook);
756 static int dgrp_remove_nd(struct nd_struct *nd)
760 /* Check to see if the selected structure is in use */
761 if (nd->nd_tty_ref_cnt)
765 unregister_dgrp_device(nd->nd_net_de);
766 dgrp_remove_node_class_sysfs_files(nd);
770 unregister_dgrp_device(nd->nd_mon_de);
773 unregister_dgrp_device(nd->nd_ports_de);
776 unregister_dgrp_device(nd->nd_dpa_de);
780 ret = nd_struct_del(nd);
788 static void register_dgrp_device(struct nd_struct *node,
789 struct proc_dir_entry *root,
790 void (*register_hook)(struct proc_dir_entry *de))
793 struct proc_dir_entry *de;
795 ID_TO_CHAR(node->nd_ID, buf);
797 de = create_proc_entry(buf, 0600 | S_IFREG, root);
801 de->data = (void *) node;
808 static void unregister_dgrp_device(struct proc_dir_entry *de)
813 /* Don't unregister proc entries that are still being used.. */
814 if ((atomic_read(&de->count)) != 1) {
815 pr_alert("%s - proc entry %s in use. Not removing.\n",
820 remove_proc_entry(de->name, de->parent);