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