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/version.h>
15 #include <linux/poll.h>
16 #include <linux/smp_lock.h>
18 #include <linux/proc_fs.h>
22 #include <linux/isdnif.h>
23 #include "isdn_divert.h"
26 /*********************************/
27 /* Variables for interface queue */
28 /*********************************/
29 ulong if_used = 0; /* number of interface users */
30 static struct divert_info *divert_info_head = NULL; /* head of queue */
31 static struct divert_info *divert_info_tail = NULL; /* pointer to last entry */
32 static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */
33 static wait_queue_head_t rd_queue;
35 /*********************************/
36 /* put an info buffer into queue */
37 /*********************************/
39 put_info_buffer(char *cp)
41 struct divert_info *ib;
50 if (!(ib = (struct divert_info *) kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
51 return; /* no memory */
52 strcpy(ib->info_start, cp); /* set output string */
54 spin_lock_irqsave( &divert_info_lock, flags );
55 ib->usage_cnt = if_used;
56 if (!divert_info_head)
57 divert_info_head = ib; /* new head */
59 divert_info_tail->next = ib; /* follows existing messages */
60 divert_info_tail = ib; /* new tail */
62 /* delete old entrys */
63 while (divert_info_head->next) {
64 if ((divert_info_head->usage_cnt <= 0) &&
65 (divert_info_head->next->usage_cnt <= 0)) {
66 ib = divert_info_head;
67 divert_info_head = divert_info_head->next;
71 } /* divert_info_head->next */
72 spin_unlock_irqrestore( &divert_info_lock, flags );
73 wake_up_interruptible(&(rd_queue));
74 } /* put_info_buffer */
76 /**********************************/
77 /* deflection device read routine */
78 /**********************************/
80 isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t * off)
82 struct divert_info *inf;
85 if (!*((struct divert_info **) file->private_data)) {
86 if (file->f_flags & O_NONBLOCK)
88 interruptible_sleep_on(&(rd_queue));
90 if (!(inf = *((struct divert_info **) file->private_data)))
93 inf->usage_cnt--; /* new usage count */
94 file->private_data = &inf->next; /* next structure */
95 if ((len = strlen(inf->info_start)) <= count) {
96 if (copy_to_user(buf, inf->info_start, len))
102 } /* isdn_divert_read */
104 /**********************************/
105 /* deflection device write routine */
106 /**********************************/
108 isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
111 } /* isdn_divert_write */
114 /***************************************/
115 /* select routines for various kernels */
116 /***************************************/
118 isdn_divert_poll(struct file *file, poll_table * wait)
120 unsigned int mask = 0;
122 poll_wait(file, &(rd_queue), wait);
123 /* mask = POLLOUT | POLLWRNORM; */
124 if (*((struct divert_info **) file->private_data)) {
125 mask |= POLLIN | POLLRDNORM;
128 } /* isdn_divert_poll */
134 isdn_divert_open(struct inode *ino, struct file *filep)
138 spin_lock_irqsave( &divert_info_lock, flags );
140 if (divert_info_head)
141 filep->private_data = &(divert_info_tail->next);
143 filep->private_data = &divert_info_head;
144 spin_unlock_irqrestore( &divert_info_lock, flags );
145 /* start_divert(); */
146 return nonseekable_open(ino, filep);
147 } /* isdn_divert_open */
149 /*******************/
151 /*******************/
153 isdn_divert_close(struct inode *ino, struct file *filep)
155 struct divert_info *inf;
158 spin_lock_irqsave( &divert_info_lock, flags );
160 inf = *((struct divert_info **) filep->private_data);
166 while (divert_info_head) {
167 inf = divert_info_head;
168 divert_info_head = divert_info_head->next;
171 spin_unlock_irqrestore( &divert_info_lock, flags );
173 } /* isdn_divert_close */
179 isdn_divert_ioctl(struct inode *inode, struct file *file,
188 if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl)))
193 dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
197 if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
202 cp = divert_if.drv_to_name(dioctl.getid.drvid);
207 strcpy(dioctl.getid.drvnam, cp);
211 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
213 dioctl.getsetrule.rule = *rulep; /* copy data */
217 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
219 spin_lock_irqsave(&divert_lock, flags);
220 *rulep = dioctl.getsetrule.rule; /* copy data */
221 spin_unlock_irqrestore(&divert_lock, flags);
222 return (0); /* no copy required */
226 return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
230 return (deleterule(dioctl.getsetrule.ruleidx));
234 return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
235 dioctl.fwd_ctrl.callid,
236 dioctl.fwd_ctrl.to_nr));
241 if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
242 return (-EINVAL); /* invalid driver */
243 if ((i = cf_command(dioctl.cf_ctrl.drvid,
244 (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
245 dioctl.cf_ctrl.cfproc,
247 dioctl.cf_ctrl.service,
248 dioctl.cf_ctrl.fwd_nr,
249 &dioctl.cf_ctrl.procid)))
256 return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
257 } /* isdn_divert_ioctl */
260 #ifdef CONFIG_PROC_FS
261 static struct file_operations isdn_fops =
263 .owner = THIS_MODULE,
265 .read = isdn_divert_read,
266 .write = isdn_divert_write,
267 .poll = isdn_divert_poll,
268 .ioctl = isdn_divert_ioctl,
269 .open = isdn_divert_open,
270 .release = isdn_divert_close,
273 /****************************/
274 /* isdn subdir in /proc/net */
275 /****************************/
276 static struct proc_dir_entry *isdn_proc_entry = NULL;
277 static struct proc_dir_entry *isdn_divert_entry = NULL;
278 #endif /* CONFIG_PROC_FS */
280 /***************************************************************************/
281 /* divert_dev_init must be called before the proc filesystem may be used */
282 /***************************************************************************/
284 divert_dev_init(void)
287 init_waitqueue_head(&rd_queue);
289 #ifdef CONFIG_PROC_FS
290 isdn_proc_entry = proc_mkdir("net/isdn", NULL);
291 if (!isdn_proc_entry)
293 isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
294 if (!isdn_divert_entry) {
295 remove_proc_entry("net/isdn", NULL);
298 isdn_divert_entry->proc_fops = &isdn_fops;
299 isdn_divert_entry->owner = THIS_MODULE;
300 #endif /* CONFIG_PROC_FS */
303 } /* divert_dev_init */
305 /***************************************************************************/
306 /* divert_dev_deinit must be called before leaving isdn when included as */
308 /***************************************************************************/
310 divert_dev_deinit(void)
313 #ifdef CONFIG_PROC_FS
314 remove_proc_entry("divert", isdn_proc_entry);
315 remove_proc_entry("net/isdn", NULL);
316 #endif /* CONFIG_PROC_FS */
319 } /* divert_dev_deinit */