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/module.h>
13 #include <linux/poll.h>
15 #include <linux/proc_fs.h>
19 #include <linux/isdnif.h>
20 #include "isdn_divert.h"
23 /*********************************/
24 /* Variables for interface queue */
25 /*********************************/
26 ulong if_used
= 0; /* number of interface users */
27 static struct divert_info
*divert_info_head
= NULL
; /* head of queue */
28 static struct divert_info
*divert_info_tail
= NULL
; /* pointer to last entry */
29 static DEFINE_SPINLOCK(divert_info_lock
);/* lock for queue */
30 static wait_queue_head_t rd_queue
;
32 /*********************************/
33 /* put an info buffer into queue */
34 /*********************************/
36 put_info_buffer(char *cp
)
38 struct divert_info
*ib
;
47 if (!(ib
= kmalloc(sizeof(struct divert_info
) + strlen(cp
), GFP_ATOMIC
)))
48 return; /* no memory */
49 strcpy(ib
->info_start
, cp
); /* set output string */
51 spin_lock_irqsave( &divert_info_lock
, flags
);
52 ib
->usage_cnt
= if_used
;
53 if (!divert_info_head
)
54 divert_info_head
= ib
; /* new head */
56 divert_info_tail
->next
= ib
; /* follows existing messages */
57 divert_info_tail
= ib
; /* new tail */
59 /* delete old entrys */
60 while (divert_info_head
->next
) {
61 if ((divert_info_head
->usage_cnt
<= 0) &&
62 (divert_info_head
->next
->usage_cnt
<= 0)) {
63 ib
= divert_info_head
;
64 divert_info_head
= divert_info_head
->next
;
68 } /* divert_info_head->next */
69 spin_unlock_irqrestore( &divert_info_lock
, flags
);
70 wake_up_interruptible(&(rd_queue
));
71 } /* put_info_buffer */
73 /**********************************/
74 /* deflection device read routine */
75 /**********************************/
77 isdn_divert_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* off
)
79 struct divert_info
*inf
;
82 if (!*((struct divert_info
**) file
->private_data
)) {
83 if (file
->f_flags
& O_NONBLOCK
)
85 interruptible_sleep_on(&(rd_queue
));
87 if (!(inf
= *((struct divert_info
**) file
->private_data
)))
90 inf
->usage_cnt
--; /* new usage count */
91 file
->private_data
= &inf
->next
; /* next structure */
92 if ((len
= strlen(inf
->info_start
)) <= count
) {
93 if (copy_to_user(buf
, inf
->info_start
, len
))
99 } /* isdn_divert_read */
101 /**********************************/
102 /* deflection device write routine */
103 /**********************************/
105 isdn_divert_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
* off
)
108 } /* isdn_divert_write */
111 /***************************************/
112 /* select routines for various kernels */
113 /***************************************/
115 isdn_divert_poll(struct file
*file
, poll_table
* wait
)
117 unsigned int mask
= 0;
119 poll_wait(file
, &(rd_queue
), wait
);
120 /* mask = POLLOUT | POLLWRNORM; */
121 if (*((struct divert_info
**) file
->private_data
)) {
122 mask
|= POLLIN
| POLLRDNORM
;
125 } /* isdn_divert_poll */
131 isdn_divert_open(struct inode
*ino
, struct file
*filep
)
135 spin_lock_irqsave( &divert_info_lock
, flags
);
137 if (divert_info_head
)
138 filep
->private_data
= &(divert_info_tail
->next
);
140 filep
->private_data
= &divert_info_head
;
141 spin_unlock_irqrestore( &divert_info_lock
, flags
);
142 /* start_divert(); */
143 return nonseekable_open(ino
, filep
);
144 } /* isdn_divert_open */
146 /*******************/
148 /*******************/
150 isdn_divert_close(struct inode
*ino
, struct file
*filep
)
152 struct divert_info
*inf
;
155 spin_lock_irqsave( &divert_info_lock
, flags
);
157 inf
= *((struct divert_info
**) filep
->private_data
);
163 while (divert_info_head
) {
164 inf
= divert_info_head
;
165 divert_info_head
= divert_info_head
->next
;
168 spin_unlock_irqrestore( &divert_info_lock
, flags
);
170 } /* isdn_divert_close */
176 isdn_divert_ioctl(struct inode
*inode
, struct file
*file
,
185 if (copy_from_user(&dioctl
, (void __user
*) arg
, sizeof(dioctl
)))
190 dioctl
.drv_version
= DIVERT_IIOC_VERSION
; /* set version */
194 if ((dioctl
.getid
.drvid
= divert_if
.name_to_drv(dioctl
.getid
.drvnam
)) < 0)
199 cp
= divert_if
.drv_to_name(dioctl
.getid
.drvid
);
204 strcpy(dioctl
.getid
.drvnam
, cp
);
208 if (!(rulep
= getruleptr(dioctl
.getsetrule
.ruleidx
)))
210 dioctl
.getsetrule
.rule
= *rulep
; /* copy data */
214 if (!(rulep
= getruleptr(dioctl
.getsetrule
.ruleidx
)))
216 spin_lock_irqsave(&divert_lock
, flags
);
217 *rulep
= dioctl
.getsetrule
.rule
; /* copy data */
218 spin_unlock_irqrestore(&divert_lock
, flags
);
219 return (0); /* no copy required */
223 return (insertrule(dioctl
.getsetrule
.ruleidx
, &dioctl
.getsetrule
.rule
));
227 return (deleterule(dioctl
.getsetrule
.ruleidx
));
231 return (deflect_extern_action(dioctl
.fwd_ctrl
.subcmd
,
232 dioctl
.fwd_ctrl
.callid
,
233 dioctl
.fwd_ctrl
.to_nr
));
238 if (!divert_if
.drv_to_name(dioctl
.cf_ctrl
.drvid
))
239 return (-EINVAL
); /* invalid driver */
240 if ((i
= cf_command(dioctl
.cf_ctrl
.drvid
,
241 (cmd
== IIOCDOCFACT
) ? 1 : (cmd
== IIOCDOCFDIS
) ? 0 : 2,
242 dioctl
.cf_ctrl
.cfproc
,
244 dioctl
.cf_ctrl
.service
,
245 dioctl
.cf_ctrl
.fwd_nr
,
246 &dioctl
.cf_ctrl
.procid
)))
253 return copy_to_user((void __user
*)arg
, &dioctl
, sizeof(dioctl
)) ? -EFAULT
: 0;
254 } /* isdn_divert_ioctl */
257 #ifdef CONFIG_PROC_FS
258 static const struct file_operations isdn_fops
=
260 .owner
= THIS_MODULE
,
262 .read
= isdn_divert_read
,
263 .write
= isdn_divert_write
,
264 .poll
= isdn_divert_poll
,
265 .ioctl
= isdn_divert_ioctl
,
266 .open
= isdn_divert_open
,
267 .release
= isdn_divert_close
,
270 /****************************/
271 /* isdn subdir in /proc/net */
272 /****************************/
273 static struct proc_dir_entry
*isdn_proc_entry
= NULL
;
274 static struct proc_dir_entry
*isdn_divert_entry
= NULL
;
275 #endif /* CONFIG_PROC_FS */
277 /***************************************************************************/
278 /* divert_dev_init must be called before the proc filesystem may be used */
279 /***************************************************************************/
281 divert_dev_init(void)
284 init_waitqueue_head(&rd_queue
);
286 #ifdef CONFIG_PROC_FS
287 isdn_proc_entry
= proc_mkdir("net/isdn", NULL
);
288 if (!isdn_proc_entry
)
290 isdn_divert_entry
= create_proc_entry("divert", S_IFREG
| S_IRUGO
, isdn_proc_entry
);
291 if (!isdn_divert_entry
) {
292 remove_proc_entry("net/isdn", NULL
);
295 isdn_divert_entry
->proc_fops
= &isdn_fops
;
296 isdn_divert_entry
->owner
= THIS_MODULE
;
297 #endif /* CONFIG_PROC_FS */
300 } /* divert_dev_init */
302 /***************************************************************************/
303 /* divert_dev_deinit must be called before leaving isdn when included as */
305 /***************************************************************************/
307 divert_dev_deinit(void)
310 #ifdef CONFIG_PROC_FS
311 remove_proc_entry("divert", isdn_proc_entry
);
312 remove_proc_entry("net/isdn", NULL
);
313 #endif /* CONFIG_PROC_FS */
316 } /* divert_dev_deinit */