Merge branch 'nfs-for-2.6.35' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / isdn / divert / divert_procfs.c
1 /* $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
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/slab.h>
15 #ifdef CONFIG_PROC_FS
16 #include <linux/proc_fs.h>
17 #else
18 #include <linux/fs.h>
19 #endif
20 #include <linux/sched.h>
21 #include <linux/isdnif.h>
22 #include <net/net_namespace.h>
23 #include <linux/smp_lock.h>
24 #include "isdn_divert.h"
25
26
27 /*********************************/
28 /* Variables for interface queue */
29 /*********************************/
30 ulong if_used = 0;              /* number of interface users */
31 static struct divert_info *divert_info_head = NULL;     /* head of queue */
32 static struct divert_info *divert_info_tail = NULL;     /* pointer to last entry */
33 static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */
34 static wait_queue_head_t rd_queue;
35
36 /*********************************/
37 /* put an info buffer into queue */
38 /*********************************/
39 void
40 put_info_buffer(char *cp)
41 {
42         struct divert_info *ib;
43         unsigned long flags;
44
45         if (if_used <= 0)
46                 return;
47         if (!cp)
48                 return;
49         if (!*cp)
50                 return;
51         if (!(ib = kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
52                  return;        /* no memory */
53         strcpy(ib->info_start, cp);     /* set output string */
54         ib->next = NULL;
55         spin_lock_irqsave( &divert_info_lock, flags );
56         ib->usage_cnt = if_used;
57         if (!divert_info_head)
58                 divert_info_head = ib;  /* new head */
59         else
60                 divert_info_tail->next = ib;    /* follows existing messages */
61         divert_info_tail = ib;  /* new tail */
62
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;
69                         kfree(ib);
70                 } else
71                         break;
72         }                       /* divert_info_head->next */
73         spin_unlock_irqrestore( &divert_info_lock, flags );
74         wake_up_interruptible(&(rd_queue));
75 }                               /* put_info_buffer */
76
77 #ifdef CONFIG_PROC_FS
78
79 /**********************************/
80 /* deflection device read routine */
81 /**********************************/
82 static ssize_t
83 isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t * off)
84 {
85         struct divert_info *inf;
86         int len;
87
88         if (!*((struct divert_info **) file->private_data)) {
89                 if (file->f_flags & O_NONBLOCK)
90                         return -EAGAIN;
91                 interruptible_sleep_on(&(rd_queue));
92         }
93         if (!(inf = *((struct divert_info **) file->private_data)))
94                 return (0);
95
96         inf->usage_cnt--;       /* new usage count */
97         file->private_data = &inf->next;        /* next structure */
98         if ((len = strlen(inf->info_start)) <= count) {
99                 if (copy_to_user(buf, inf->info_start, len))
100                         return -EFAULT;
101                 *off += len;
102                 return (len);
103         }
104         return (0);
105 }                               /* isdn_divert_read */
106
107 /**********************************/
108 /* deflection device write routine */
109 /**********************************/
110 static ssize_t
111 isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
112 {
113         return (-ENODEV);
114 }                               /* isdn_divert_write */
115
116
117 /***************************************/
118 /* select routines for various kernels */
119 /***************************************/
120 static unsigned int
121 isdn_divert_poll(struct file *file, poll_table * wait)
122 {
123         unsigned int mask = 0;
124
125         poll_wait(file, &(rd_queue), wait);
126         /* mask = POLLOUT | POLLWRNORM; */
127         if (*((struct divert_info **) file->private_data)) {
128                 mask |= POLLIN | POLLRDNORM;
129         }
130         return mask;
131 }                               /* isdn_divert_poll */
132
133 /****************/
134 /* Open routine */
135 /****************/
136 static int
137 isdn_divert_open(struct inode *ino, struct file *filep)
138 {
139         unsigned long flags;
140
141         spin_lock_irqsave( &divert_info_lock, flags );
142         if_used++;
143         if (divert_info_head)
144                 filep->private_data = &(divert_info_tail->next);
145         else
146                 filep->private_data = &divert_info_head;
147         spin_unlock_irqrestore( &divert_info_lock, flags );
148         /*  start_divert(); */
149         return nonseekable_open(ino, filep);
150 }                               /* isdn_divert_open */
151
152 /*******************/
153 /* close routine   */
154 /*******************/
155 static int
156 isdn_divert_close(struct inode *ino, struct file *filep)
157 {
158         struct divert_info *inf;
159         unsigned long flags;
160
161         spin_lock_irqsave( &divert_info_lock, flags );
162         if_used--;
163         inf = *((struct divert_info **) filep->private_data);
164         while (inf) {
165                 inf->usage_cnt--;
166                 inf = inf->next;
167         }
168         if (if_used <= 0)
169                 while (divert_info_head) {
170                         inf = divert_info_head;
171                         divert_info_head = divert_info_head->next;
172                         kfree(inf);
173                 }
174         spin_unlock_irqrestore( &divert_info_lock, flags );
175         return (0);
176 }                               /* isdn_divert_close */
177
178 /*********/
179 /* IOCTL */
180 /*********/
181 static int isdn_divert_ioctl_unlocked(struct file *file, uint cmd, ulong arg)
182 {
183         divert_ioctl dioctl;
184         int i;
185         unsigned long flags;
186         divert_rule *rulep;
187         char *cp;
188
189         if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl)))
190                 return -EFAULT;
191
192         switch (cmd) {
193                 case IIOCGETVER:
194                         dioctl.drv_version = DIVERT_IIOC_VERSION;       /* set version */
195                         break;
196
197                 case IIOCGETDRV:
198                         if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
199                                 return (-EINVAL);
200                         break;
201
202                 case IIOCGETNAM:
203                         cp = divert_if.drv_to_name(dioctl.getid.drvid);
204                         if (!cp)
205                                 return (-EINVAL);
206                         if (!*cp)
207                                 return (-EINVAL);
208                         strcpy(dioctl.getid.drvnam, cp);
209                         break;
210
211                 case IIOCGETRULE:
212                         if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
213                                 return (-EINVAL);
214                         dioctl.getsetrule.rule = *rulep;        /* copy data */
215                         break;
216
217                 case IIOCMODRULE:
218                         if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
219                                 return (-EINVAL);
220             spin_lock_irqsave(&divert_lock, flags);
221                         *rulep = dioctl.getsetrule.rule;        /* copy data */
222                         spin_unlock_irqrestore(&divert_lock, flags);
223                         return (0);     /* no copy required */
224                         break;
225
226                 case IIOCINSRULE:
227                         return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
228                         break;
229
230                 case IIOCDELRULE:
231                         return (deleterule(dioctl.getsetrule.ruleidx));
232                         break;
233
234                 case IIOCDODFACT:
235                         return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
236                                                   dioctl.fwd_ctrl.callid,
237                                                  dioctl.fwd_ctrl.to_nr));
238
239                 case IIOCDOCFACT:
240                 case IIOCDOCFDIS:
241                 case IIOCDOCFINT:
242                         if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
243                                 return (-EINVAL);       /* invalid driver */
244                         if ((i = cf_command(dioctl.cf_ctrl.drvid,
245                                             (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
246                                             dioctl.cf_ctrl.cfproc,
247                                             dioctl.cf_ctrl.msn,
248                                             dioctl.cf_ctrl.service,
249                                             dioctl.cf_ctrl.fwd_nr,
250                                             &dioctl.cf_ctrl.procid)))
251                                 return (i);
252                         break;
253
254                 default:
255                         return (-EINVAL);
256         }                       /* switch cmd */
257         return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
258 }                               /* isdn_divert_ioctl */
259
260 static long isdn_divert_ioctl(struct file *file, uint cmd, ulong arg)
261 {
262         long ret;
263
264         lock_kernel();
265         ret = isdn_divert_ioctl_unlocked(file, cmd, arg);
266         unlock_kernel();
267
268         return ret;
269 }
270
271 static const struct file_operations isdn_fops =
272 {
273         .owner          = THIS_MODULE,
274         .llseek         = no_llseek,
275         .read           = isdn_divert_read,
276         .write          = isdn_divert_write,
277         .poll           = isdn_divert_poll,
278         .unlocked_ioctl = isdn_divert_ioctl,
279         .open           = isdn_divert_open,
280         .release        = isdn_divert_close,                                      
281 };
282
283 /****************************/
284 /* isdn subdir in /proc/net */
285 /****************************/
286 static struct proc_dir_entry *isdn_proc_entry = NULL;
287 static struct proc_dir_entry *isdn_divert_entry = NULL;
288 #endif  /* CONFIG_PROC_FS */
289
290 /***************************************************************************/
291 /* divert_dev_init must be called before the proc filesystem may be used   */
292 /***************************************************************************/
293 int
294 divert_dev_init(void)
295 {
296
297         init_waitqueue_head(&rd_queue);
298
299 #ifdef CONFIG_PROC_FS
300         isdn_proc_entry = proc_mkdir("isdn", init_net.proc_net);
301         if (!isdn_proc_entry)
302                 return (-1);
303         isdn_divert_entry = proc_create("divert", S_IFREG | S_IRUGO,
304                                         isdn_proc_entry, &isdn_fops);
305         if (!isdn_divert_entry) {
306                 remove_proc_entry("isdn", init_net.proc_net);
307                 return (-1);
308         }
309 #endif  /* CONFIG_PROC_FS */
310
311         return (0);
312 }                               /* divert_dev_init */
313
314 /***************************************************************************/
315 /* divert_dev_deinit must be called before leaving isdn when included as   */
316 /* a module.                                                               */
317 /***************************************************************************/
318 int
319 divert_dev_deinit(void)
320 {
321
322 #ifdef CONFIG_PROC_FS
323         remove_proc_entry("divert", isdn_proc_entry);
324         remove_proc_entry("isdn", init_net.proc_net);
325 #endif  /* CONFIG_PROC_FS */
326
327         return (0);
328 }                               /* divert_dev_deinit */