v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / isdn / divert / divert_procfs.c
1 /*
2  * $Id: divert_procfs.c,v 1.11 2000/11/25 17:01:00 kai Exp $
3  *
4  * Filesystem handling for the diversion supplementary services.
5  *
6  * Copyright 1998       by Werner Cornelius (werner@isdn4linux.de)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <linux/config.h>
25 #define __NO_VERSION__
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/poll.h>
29 #include <linux/smp_lock.h>
30 #ifdef CONFIG_PROC_FS
31 #include <linux/proc_fs.h>
32 #else
33 #include <linux/fs.h>
34 #endif
35 #include <linux/isdnif.h>
36 #include "isdn_divert.h"
37
38 /*********************************/
39 /* Variables for interface queue */
40 /*********************************/
41 ulong if_used = 0;              /* number of interface users */
42 static struct divert_info *divert_info_head = NULL;     /* head of queue */
43 static struct divert_info *divert_info_tail = NULL;     /* pointer to last entry */
44 static wait_queue_head_t rd_queue;
45
46 /*********************************/
47 /* put an info buffer into queue */
48 /*********************************/
49 void
50 put_info_buffer(char *cp)
51 {
52         struct divert_info *ib;
53         int flags;
54
55         if (if_used <= 0)
56                 return;
57         if (!cp)
58                 return;
59         if (!*cp)
60                 return;
61         if (!(ib = (struct divert_info *) kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
62                  return;        /* no memory */
63         strcpy(ib->info_start, cp);     /* set output string */
64         ib->next = NULL;
65         save_flags(flags);
66         cli();
67         ib->usage_cnt = if_used;
68         if (!divert_info_head)
69                 divert_info_head = ib;  /* new head */
70         else
71                 divert_info_tail->next = ib;    /* follows existing messages */
72         divert_info_tail = ib;  /* new tail */
73         restore_flags(flags);
74
75         /* delete old entrys */
76         while (divert_info_head->next) {
77                 if ((divert_info_head->usage_cnt <= 0) &&
78                     (divert_info_head->next->usage_cnt <= 0)) {
79                         ib = divert_info_head;
80                         divert_info_head = divert_info_head->next;
81                         kfree(ib);
82                 } else
83                         break;
84         }                       /* divert_info_head->next */
85         wake_up_interruptible(&(rd_queue));
86 }                               /* put_info_buffer */
87
88 /**********************************/
89 /* deflection device read routine */
90 /**********************************/
91 static ssize_t
92 isdn_divert_read(struct file *file, char *buf, size_t count, loff_t * off)
93 {
94         struct divert_info *inf;
95         int len;
96
97         if (!*((struct divert_info **) file->private_data)) {
98                 if (file->f_flags & O_NONBLOCK)
99                         return -EAGAIN;
100                 interruptible_sleep_on(&(rd_queue));
101         }
102         if (!(inf = *((struct divert_info **) file->private_data)))
103                 return (0);
104
105         inf->usage_cnt--;       /* new usage count */
106         (struct divert_info **) file->private_data = &inf->next;        /* next structure */
107         if ((len = strlen(inf->info_start)) <= count) {
108                 if (copy_to_user(buf, inf->info_start, len))
109                         return -EFAULT;
110                 file->f_pos += len;
111                 return (len);
112         }
113         return (0);
114 }                               /* isdn_divert_read */
115
116 /**********************************/
117 /* deflection device write routine */
118 /**********************************/
119 static ssize_t
120 isdn_divert_write(struct file *file, const char *buf, size_t count, loff_t * off)
121 {
122         return (-ENODEV);
123 }                               /* isdn_divert_write */
124
125
126 /***************************************/
127 /* select routines for various kernels */
128 /***************************************/
129 static unsigned int
130 isdn_divert_poll(struct file *file, poll_table * wait)
131 {
132         unsigned int mask = 0;
133
134         poll_wait(file, &(rd_queue), wait);
135         /* mask = POLLOUT | POLLWRNORM; */
136         if (*((struct divert_info **) file->private_data)) {
137                 mask |= POLLIN | POLLRDNORM;
138         }
139         return mask;
140 }                               /* isdn_divert_poll */
141
142 /****************/
143 /* Open routine */
144 /****************/
145 static int
146 isdn_divert_open(struct inode *ino, struct file *filep)
147 {
148         int flags;
149
150         lock_kernel();
151         save_flags(flags);
152         cli();
153         if_used++;
154         if (divert_info_head)
155                 (struct divert_info **) filep->private_data = &(divert_info_tail->next);
156         else
157                 (struct divert_info **) filep->private_data = &divert_info_head;
158         restore_flags(flags);
159         /*  start_divert(); */
160         unlock_kernel();
161         return (0);
162 }                               /* isdn_divert_open */
163
164 /*******************/
165 /* close routine   */
166 /*******************/
167 static int
168 isdn_divert_close(struct inode *ino, struct file *filep)
169 {
170         struct divert_info *inf;
171         int flags;
172
173         lock_kernel();
174         save_flags(flags);
175         cli();
176         if_used--;
177         inf = *((struct divert_info **) filep->private_data);
178         while (inf) {
179                 inf->usage_cnt--;
180                 inf = inf->next;
181         }
182         restore_flags(flags);
183         if (if_used <= 0)
184                 while (divert_info_head) {
185                         inf = divert_info_head;
186                         divert_info_head = divert_info_head->next;
187                         kfree(inf);
188                 }
189         unlock_kernel();
190         return (0);
191 }                               /* isdn_divert_close */
192
193 /*********/
194 /* IOCTL */
195 /*********/
196 static int
197 isdn_divert_ioctl(struct inode *inode, struct file *file,
198                   uint cmd, ulong arg)
199 {
200         divert_ioctl dioctl;
201         int i, flags;
202         divert_rule *rulep;
203         char *cp;
204
205         if ((i = copy_from_user(&dioctl, (char *) arg, sizeof(dioctl))))
206                 return (i);
207
208         switch (cmd) {
209                 case IIOCGETVER:
210                         dioctl.drv_version = DIVERT_IIOC_VERSION;       /* set version */
211                         break;
212
213                 case IIOCGETDRV:
214                         if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0)
215                                 return (-EINVAL);
216                         break;
217
218                 case IIOCGETNAM:
219                         cp = divert_if.drv_to_name(dioctl.getid.drvid);
220                         if (!cp)
221                                 return (-EINVAL);
222                         if (!*cp)
223                                 return (-EINVAL);
224                         strcpy(dioctl.getid.drvnam, cp);
225                         break;
226
227                 case IIOCGETRULE:
228                         if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
229                                 return (-EINVAL);
230                         dioctl.getsetrule.rule = *rulep;        /* copy data */
231                         break;
232
233                 case IIOCMODRULE:
234                         if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx)))
235                                 return (-EINVAL);
236                         save_flags(flags);
237                         cli();
238                         *rulep = dioctl.getsetrule.rule;        /* copy data */
239                         restore_flags(flags);
240                         return (0);     /* no copy required */
241                         break;
242
243                 case IIOCINSRULE:
244                         return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
245                         break;
246
247                 case IIOCDELRULE:
248                         return (deleterule(dioctl.getsetrule.ruleidx));
249                         break;
250
251                 case IIOCDODFACT:
252                         return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
253                                                   dioctl.fwd_ctrl.callid,
254                                                  dioctl.fwd_ctrl.to_nr));
255
256                 case IIOCDOCFACT:
257                 case IIOCDOCFDIS:
258                 case IIOCDOCFINT:
259                         if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
260                                 return (-EINVAL);       /* invalid driver */
261                         if ((i = cf_command(dioctl.cf_ctrl.drvid,
262                                             (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
263                                             dioctl.cf_ctrl.cfproc,
264                                             dioctl.cf_ctrl.msn,
265                                             dioctl.cf_ctrl.service,
266                                             dioctl.cf_ctrl.fwd_nr,
267                                             &dioctl.cf_ctrl.procid)))
268                                 return (i);
269                         break;
270
271                 default:
272                         return (-EINVAL);
273         }                       /* switch cmd */
274         return (copy_to_user((char *) arg, &dioctl, sizeof(dioctl)));   /* success */
275 }                               /* isdn_divert_ioctl */
276
277
278 #ifdef CONFIG_PROC_FS
279 static struct file_operations isdn_fops =
280 {
281         llseek:         no_llseek,
282         read:           isdn_divert_read,
283         write:          isdn_divert_write,
284         poll:           isdn_divert_poll,
285         ioctl:          isdn_divert_ioctl,
286         open:           isdn_divert_open,
287         release:        isdn_divert_close,                                      
288 };
289
290 /****************************/
291 /* isdn subdir in /proc/net */
292 /****************************/
293 static struct proc_dir_entry *isdn_proc_entry = NULL;
294 static struct proc_dir_entry *isdn_divert_entry = NULL;
295 #endif  /* CONFIG_PROC_FS */
296
297 /***************************************************************************/
298 /* divert_dev_init must be called before the proc filesystem may be used   */
299 /***************************************************************************/
300 int
301 divert_dev_init(void)
302 {
303
304         init_waitqueue_head(&rd_queue);
305
306 #ifdef CONFIG_PROC_FS
307         isdn_proc_entry = create_proc_entry("isdn", S_IFDIR | S_IRUGO | S_IXUGO, proc_net);
308         if (!isdn_proc_entry)
309                 return (-1);
310         isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
311         if (!isdn_divert_entry) {
312                 remove_proc_entry("isdn", proc_net);
313                 return (-1);
314         }
315         isdn_divert_entry->proc_fops = &isdn_fops; 
316         isdn_divert_entry->owner = THIS_MODULE; 
317 #endif  /* CONFIG_PROC_FS */
318
319         return (0);
320 }                               /* divert_dev_init */
321
322 /***************************************************************************/
323 /* divert_dev_deinit must be called before leaving isdn when included as   */
324 /* a module.                                                               */
325 /***************************************************************************/
326 int
327 divert_dev_deinit(void)
328 {
329
330 #ifdef CONFIG_PROC_FS
331         remove_proc_entry("divert", isdn_proc_entry);
332         remove_proc_entry("isdn", proc_net);
333 #endif  /* CONFIG_PROC_FS */
334
335         return (0);
336 }                               /* divert_dev_deinit */