1 /* $Id: divert_procfs.c,v 1.11.6.2 2001/09/23 22:24:36 kai Exp $
3 * Filesystem handling for the diversion supplementary services.
5 * Copyright 1998 by Werner Cornelius (werner@isdn4linux.de)
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/smp_lock.h>
17 #include <linux/proc_fs.h>
21 #include <linux/isdnif.h>
22 #include "isdn_divert.h"
25 /*********************************/
26 /* Variables for interface queue */
27 /*********************************/
28 ulong if_used = 0; /* number of interface users */
29 static struct divert_info *divert_info_head = NULL; /* head of queue */
30 static struct divert_info *divert_info_tail = NULL; /* pointer to last entry */
31 static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */
32 static wait_queue_head_t rd_queue;
34 /*********************************/
35 /* put an info buffer into queue */
36 /*********************************/
38 put_info_buffer(char *cp)
40 struct divert_info *ib;
49 if (!(ib = (struct divert_info *) kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
50 return; /* no memory */
51 strcpy(ib->info_start, cp); /* set output string */
53 spin_lock_irqsave( &divert_info_lock, flags );
54 ib->usage_cnt = if_used;
55 if (!divert_info_head)
56 divert_info_head = ib; /* new head */
58 divert_info_tail->next = ib; /* follows existing messages */
59 divert_info_tail = ib; /* new tail */
61 /* delete old entrys */
62 while (divert_info_head->next) {
63 if ((divert_info_head->usage_cnt <= 0) &&
64 (divert_info_head->next->usage_cnt <= 0)) {
65 ib = divert_info_head;
66 divert_info_head = divert_info_head->next;
70 } /* divert_info_head->next */
71 spin_unlock_irqrestore( &divert_info_lock, flags );
72 wake_up_interruptible(&(rd_queue));
73 } /* put_info_buffer */
75 /**********************************/
76 /* deflection device read routine */
77 /**********************************/
79 isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t * off)
81 struct divert_info *inf;
84 if (!*((struct divert_info **) file->private_data)) {
85 if (file->f_flags & O_NONBLOCK)
87 interruptible_sleep_on(&(rd_queue));
89 if (!(inf = *((struct divert_info **) file->private_data)))
92 inf->usage_cnt--; /* new usage count */
93 file->private_data = &inf->next; /* next structure */
94 if ((len = strlen(inf->info_start)) <= count) {
95 if (copy_to_user(buf, inf->info_start, len))
101 } /* isdn_divert_read */
103 /**********************************/
104 /* deflection device write routine */
105 /**********************************/
107 isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
110 } /* isdn_divert_write */
113 /***************************************/
114 /* select routines for various kernels */
115 /***************************************/
117 isdn_divert_poll(struct file *file, poll_table * wait)
119 unsigned int mask = 0;
121 poll_wait(file, &(rd_queue), wait);
122 /* mask = POLLOUT | POLLWRNORM; */
123 if (*((struct divert_info **) file->private_data)) {
124 mask |= POLLIN | POLLRDNORM;
127 } /* isdn_divert_poll */
133 isdn_divert_open(struct inode *ino, struct file *filep)
137 spin_lock_irqsave( &divert_info_lock, flags );
139 if (divert_info_head)
140 filep->private_data = &(divert_info_tail->next);
142 filep->private_data = &divert_info_head;
143 spin_unlock_irqrestore( &divert_info_lock, flags );
144 /* start_divert(); */
145 return nonseekable_open(ino, filep);
146 } /* isdn_divert_open */
148 /*******************/
150 /*******************/
152 isdn_divert_close(struct inode *ino, struct file *filep)
154 struct divert_info *inf;
157 spin_lock_irqsave( &divert_info_lock, flags );
159 inf = *((struct divert_info **) filep->private_data);
165 while (divert_info_head) {
166 inf = divert_info_head;
167 divert_info_head = divert_info_head->next;
170 spin_unlock_irqrestore( &divert_info_lock, flags );
172 } /* isdn_divert_close */
178 isdn_divert_ioctl(struct inode *inode, struct file *file,
187 if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl)))
192 dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
196 if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
201 cp = divert_if.drv_to_name(dioctl.getid.drvid);
206 strcpy(dioctl.getid.drvnam, cp);
210 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
212 dioctl.getsetrule.rule = *rulep; /* copy data */
216 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
218 spin_lock_irqsave(&divert_lock, flags);
219 *rulep = dioctl.getsetrule.rule; /* copy data */
220 spin_unlock_irqrestore(&divert_lock, flags);
221 return (0); /* no copy required */
225 return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
229 return (deleterule(dioctl.getsetrule.ruleidx));
233 return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
234 dioctl.fwd_ctrl.callid,
235 dioctl.fwd_ctrl.to_nr));
240 if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
241 return (-EINVAL); /* invalid driver */
242 if ((i = cf_command(dioctl.cf_ctrl.drvid,
243 (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
244 dioctl.cf_ctrl.cfproc,
246 dioctl.cf_ctrl.service,
247 dioctl.cf_ctrl.fwd_nr,
248 &dioctl.cf_ctrl.procid)))
255 return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
256 } /* isdn_divert_ioctl */
259 #ifdef CONFIG_PROC_FS
260 static struct file_operations isdn_fops =
262 .owner = THIS_MODULE,
264 .read = isdn_divert_read,
265 .write = isdn_divert_write,
266 .poll = isdn_divert_poll,
267 .ioctl = isdn_divert_ioctl,
268 .open = isdn_divert_open,
269 .release = isdn_divert_close,
272 /****************************/
273 /* isdn subdir in /proc/net */
274 /****************************/
275 static struct proc_dir_entry *isdn_proc_entry = NULL;
276 static struct proc_dir_entry *isdn_divert_entry = NULL;
277 #endif /* CONFIG_PROC_FS */
279 /***************************************************************************/
280 /* divert_dev_init must be called before the proc filesystem may be used */
281 /***************************************************************************/
283 divert_dev_init(void)
286 init_waitqueue_head(&rd_queue);
288 #ifdef CONFIG_PROC_FS
289 isdn_proc_entry = proc_mkdir("net/isdn", NULL);
290 if (!isdn_proc_entry)
292 isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
293 if (!isdn_divert_entry) {
294 remove_proc_entry("net/isdn", NULL);
297 isdn_divert_entry->proc_fops = &isdn_fops;
298 isdn_divert_entry->owner = THIS_MODULE;
299 #endif /* CONFIG_PROC_FS */
302 } /* divert_dev_init */
304 /***************************************************************************/
305 /* divert_dev_deinit must be called before leaving isdn when included as */
307 /***************************************************************************/
309 divert_dev_deinit(void)
312 #ifdef CONFIG_PROC_FS
313 remove_proc_entry("divert", isdn_proc_entry);
314 remove_proc_entry("net/isdn", NULL);
315 #endif /* CONFIG_PROC_FS */
318 } /* divert_dev_deinit */