blob: 559a0d0244cf38b59d6aa809bddee183089f8c44 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: divert_procfs.c,v 1.11.6.2 2001/09/23 22:24:36 kai Exp $
2 *
3 * Filesystem handling for the diversion supplementary services.
4 *
5 * Copyright 1998 by Werner Cornelius (werner@isdn4linux.de)
6 *
7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference.
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#ifdef CONFIG_PROC_FS
15#include <linux/proc_fs.h>
16#else
17#include <linux/fs.h>
18#endif
19#include <linux/isdnif.h>
20#include "isdn_divert.h"
21
22
23/*********************************/
24/* Variables for interface queue */
25/*********************************/
26ulong if_used = 0; /* number of interface users */
27static struct divert_info *divert_info_head = NULL; /* head of queue */
28static struct divert_info *divert_info_tail = NULL; /* pointer to last entry */
29static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */
30static wait_queue_head_t rd_queue;
31
32/*********************************/
33/* put an info buffer into queue */
34/*********************************/
35void
36put_info_buffer(char *cp)
37{
38 struct divert_info *ib;
39 unsigned long flags;
40
41 if (if_used <= 0)
42 return;
43 if (!cp)
44 return;
45 if (!*cp)
46 return;
Robert P. J. Day5cbded52006-12-13 00:35:56 -080047 if (!(ib = kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return; /* no memory */
49 strcpy(ib->info_start, cp); /* set output string */
50 ib->next = NULL;
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 */
55 else
56 divert_info_tail->next = ib; /* follows existing messages */
57 divert_info_tail = ib; /* new tail */
58
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;
65 kfree(ib);
66 } else
67 break;
68 } /* divert_info_head->next */
69 spin_unlock_irqrestore( &divert_info_lock, flags );
70 wake_up_interruptible(&(rd_queue));
71} /* put_info_buffer */
72
Charlie Shepherd732781d2007-07-31 00:39:45 -070073#ifdef CONFIG_PROC_FS
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/**********************************/
76/* deflection device read routine */
77/**********************************/
78static ssize_t
79isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t * off)
80{
81 struct divert_info *inf;
82 int len;
83
84 if (!*((struct divert_info **) file->private_data)) {
85 if (file->f_flags & O_NONBLOCK)
86 return -EAGAIN;
87 interruptible_sleep_on(&(rd_queue));
88 }
89 if (!(inf = *((struct divert_info **) file->private_data)))
90 return (0);
91
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))
96 return -EFAULT;
97 *off += len;
98 return (len);
99 }
100 return (0);
101} /* isdn_divert_read */
102
103/**********************************/
104/* deflection device write routine */
105/**********************************/
106static ssize_t
107isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
108{
109 return (-ENODEV);
110} /* isdn_divert_write */
111
112
113/***************************************/
114/* select routines for various kernels */
115/***************************************/
116static unsigned int
117isdn_divert_poll(struct file *file, poll_table * wait)
118{
119 unsigned int mask = 0;
120
121 poll_wait(file, &(rd_queue), wait);
122 /* mask = POLLOUT | POLLWRNORM; */
123 if (*((struct divert_info **) file->private_data)) {
124 mask |= POLLIN | POLLRDNORM;
125 }
126 return mask;
127} /* isdn_divert_poll */
128
129/****************/
130/* Open routine */
131/****************/
132static int
133isdn_divert_open(struct inode *ino, struct file *filep)
134{
135 unsigned long flags;
136
137 spin_lock_irqsave( &divert_info_lock, flags );
138 if_used++;
139 if (divert_info_head)
140 filep->private_data = &(divert_info_tail->next);
141 else
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 */
147
148/*******************/
149/* close routine */
150/*******************/
151static int
152isdn_divert_close(struct inode *ino, struct file *filep)
153{
154 struct divert_info *inf;
155 unsigned long flags;
156
157 spin_lock_irqsave( &divert_info_lock, flags );
158 if_used--;
159 inf = *((struct divert_info **) filep->private_data);
160 while (inf) {
161 inf->usage_cnt--;
162 inf = inf->next;
163 }
164 if (if_used <= 0)
165 while (divert_info_head) {
166 inf = divert_info_head;
167 divert_info_head = divert_info_head->next;
168 kfree(inf);
169 }
170 spin_unlock_irqrestore( &divert_info_lock, flags );
171 return (0);
172} /* isdn_divert_close */
173
174/*********/
175/* IOCTL */
176/*********/
177static int
178isdn_divert_ioctl(struct inode *inode, struct file *file,
179 uint cmd, ulong arg)
180{
181 divert_ioctl dioctl;
182 int i;
183 unsigned long flags;
184 divert_rule *rulep;
185 char *cp;
186
187 if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl)))
188 return -EFAULT;
189
190 switch (cmd) {
191 case IIOCGETVER:
192 dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */
193 break;
194
195 case IIOCGETDRV:
196 if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
197 return (-EINVAL);
198 break;
199
200 case IIOCGETNAM:
201 cp = divert_if.drv_to_name(dioctl.getid.drvid);
202 if (!cp)
203 return (-EINVAL);
204 if (!*cp)
205 return (-EINVAL);
206 strcpy(dioctl.getid.drvnam, cp);
207 break;
208
209 case IIOCGETRULE:
210 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
211 return (-EINVAL);
212 dioctl.getsetrule.rule = *rulep; /* copy data */
213 break;
214
215 case IIOCMODRULE:
216 if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
217 return (-EINVAL);
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 */
222 break;
223
224 case IIOCINSRULE:
225 return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
226 break;
227
228 case IIOCDELRULE:
229 return (deleterule(dioctl.getsetrule.ruleidx));
230 break;
231
232 case IIOCDODFACT:
233 return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
234 dioctl.fwd_ctrl.callid,
235 dioctl.fwd_ctrl.to_nr));
236
237 case IIOCDOCFACT:
238 case IIOCDOCFDIS:
239 case IIOCDOCFINT:
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,
245 dioctl.cf_ctrl.msn,
246 dioctl.cf_ctrl.service,
247 dioctl.cf_ctrl.fwd_nr,
248 &dioctl.cf_ctrl.procid)))
249 return (i);
250 break;
251
252 default:
253 return (-EINVAL);
254 } /* switch cmd */
255 return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
256} /* isdn_divert_ioctl */
257
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800258static const struct file_operations isdn_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 .owner = THIS_MODULE,
261 .llseek = no_llseek,
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,
268};
269
270/****************************/
271/* isdn subdir in /proc/net */
272/****************************/
273static struct proc_dir_entry *isdn_proc_entry = NULL;
274static struct proc_dir_entry *isdn_divert_entry = NULL;
275#endif /* CONFIG_PROC_FS */
276
277/***************************************************************************/
278/* divert_dev_init must be called before the proc filesystem may be used */
279/***************************************************************************/
280int
281divert_dev_init(void)
282{
283
284 init_waitqueue_head(&rd_queue);
285
286#ifdef CONFIG_PROC_FS
Al Viro66600222005-09-28 22:32:57 +0100287 isdn_proc_entry = proc_mkdir("net/isdn", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!isdn_proc_entry)
289 return (-1);
290 isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
291 if (!isdn_divert_entry) {
Al Viro66600222005-09-28 22:32:57 +0100292 remove_proc_entry("net/isdn", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return (-1);
294 }
295 isdn_divert_entry->proc_fops = &isdn_fops;
296 isdn_divert_entry->owner = THIS_MODULE;
297#endif /* CONFIG_PROC_FS */
298
299 return (0);
300} /* divert_dev_init */
301
302/***************************************************************************/
303/* divert_dev_deinit must be called before leaving isdn when included as */
304/* a module. */
305/***************************************************************************/
306int
307divert_dev_deinit(void)
308{
309
310#ifdef CONFIG_PROC_FS
311 remove_proc_entry("divert", isdn_proc_entry);
Al Viro66600222005-09-28 22:32:57 +0100312 remove_proc_entry("net/isdn", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif /* CONFIG_PROC_FS */
314
315 return (0);
316} /* divert_dev_deinit */