1 /* $Id: divert_procfs.c,v 1.1.4.1 2001/11/20 14:19:35 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 #define __NO_VERSION__
14 #include <linux/module.h>
15 #include <linux/version.h>
16 #include <linux/poll.h>
17 #include <linux/smp_lock.h>
19 #include <linux/proc_fs.h>
23 #include <linux/isdnif.h>
24 #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 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 */
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 */
63 /* delete old entrys */
64 while (divert_info_head->next) {
65 if ((divert_info_head->usage_cnt <= 0) &&
66 (divert_info_head->next->usage_cnt <= 0)) {
67 ib = divert_info_head;
68 divert_info_head = divert_info_head->next;
72 } /* divert_info_head->next */
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 *buf, size_t count, loff_t * off)
82 struct divert_info *inf;
86 if (!*((struct divert_info **) file->private_data)) {
87 if (file->f_flags & O_NONBLOCK)
89 interruptible_sleep_on(&(rd_queue));
91 if (!(inf = *((struct divert_info **) file->private_data)))
94 inf->usage_cnt--; /* new usage count */
95 file->private_data = &inf->next; /* next structure */
96 if ((len = strlen(inf->info_start)) <= count) {
97 if (copy_to_user(buf, inf->info_start, len))
103 } /* isdn_divert_read */
105 /**********************************/
106 /* deflection device write routine */
107 /**********************************/
109 isdn_divert_write(struct file *file, const char *buf, size_t count, loff_t * off)
112 } /* isdn_divert_write */
115 /***************************************/
116 /* select routines for various kernels */
117 /***************************************/
119 isdn_divert_poll(struct file *file, poll_table * wait)
121 unsigned int mask = 0;
123 poll_wait(file, &(rd_queue), wait);
124 /* mask = POLLOUT | POLLWRNORM; */
125 if (*((struct divert_info **) file->private_data)) {
126 mask |= POLLIN | POLLRDNORM;
129 } /* isdn_divert_poll */
135 isdn_divert_open(struct inode *ino, struct file *filep)
143 if (divert_info_head)
144 filep->private_data = &(divert_info_tail->next);
146 filep->private_data = &divert_info_head;
147 restore_flags(flags);
148 /* start_divert(); */
151 } /* isdn_divert_open */
153 /*******************/
155 /*******************/
157 isdn_divert_close(struct inode *ino, struct file *filep)
159 struct divert_info *inf;
166 inf = *((struct divert_info **) filep->private_data);
171 restore_flags(flags);
173 while (divert_info_head) {
174 inf = divert_info_head;
175 divert_info_head = divert_info_head->next;
180 } /* isdn_divert_close */
186 isdn_divert_ioctl(struct inode *inode, struct file *file,
195 if ((i = copy_from_user(&dioctl, (char *) arg, sizeof(dioctl))))
200 dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
204 if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
209 cp = divert_if.drv_to_name(dioctl.getid.drvid);
214 strcpy(dioctl.getid.drvnam, cp);
218 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
220 dioctl.getsetrule.rule = *rulep; /* copy data */
224 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
228 *rulep = dioctl.getsetrule.rule; /* copy data */
229 restore_flags(flags);
230 return (0); /* no copy required */
234 return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
238 return (deleterule(dioctl.getsetrule.ruleidx));
242 return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
243 dioctl.fwd_ctrl.callid,
244 dioctl.fwd_ctrl.to_nr));
249 if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
250 return (-EINVAL); /* invalid driver */
251 if ((i = cf_command(dioctl.cf_ctrl.drvid,
252 (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
253 dioctl.cf_ctrl.cfproc,
255 dioctl.cf_ctrl.service,
256 dioctl.cf_ctrl.fwd_nr,
257 &dioctl.cf_ctrl.procid)))
264 return (copy_to_user((char *) arg, &dioctl, sizeof(dioctl))); /* success */
265 } /* isdn_divert_ioctl */
268 #ifdef CONFIG_PROC_FS
269 static struct file_operations isdn_fops =
272 read: isdn_divert_read,
273 write: isdn_divert_write,
274 poll: isdn_divert_poll,
275 ioctl: isdn_divert_ioctl,
276 open: isdn_divert_open,
277 release: isdn_divert_close,
280 /****************************/
281 /* isdn subdir in /proc/net */
282 /****************************/
283 static struct proc_dir_entry *isdn_proc_entry = NULL;
284 static struct proc_dir_entry *isdn_divert_entry = NULL;
285 #endif /* CONFIG_PROC_FS */
287 /***************************************************************************/
288 /* divert_dev_init must be called before the proc filesystem may be used */
289 /***************************************************************************/
291 divert_dev_init(void)
294 init_waitqueue_head(&rd_queue);
296 #ifdef CONFIG_PROC_FS
297 isdn_proc_entry = create_proc_entry("isdn", S_IFDIR | S_IRUGO | S_IXUGO, proc_net);
298 if (!isdn_proc_entry)
300 isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
301 if (!isdn_divert_entry) {
302 remove_proc_entry("isdn", proc_net);
305 isdn_divert_entry->proc_fops = &isdn_fops;
306 isdn_divert_entry->owner = THIS_MODULE;
307 #endif /* CONFIG_PROC_FS */
310 } /* divert_dev_init */
312 /***************************************************************************/
313 /* divert_dev_deinit must be called before leaving isdn when included as */
315 /***************************************************************************/
317 divert_dev_deinit(void)
320 #ifdef CONFIG_PROC_FS
321 remove_proc_entry("divert", isdn_proc_entry);
322 remove_proc_entry("isdn", proc_net);
323 #endif /* CONFIG_PROC_FS */
326 } /* divert_dev_deinit */