ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/config.h>
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/poll.h>
16 #include <linux/smp_lock.h>
17 #ifdef CONFIG_PROC_FS
18 #include <linux/proc_fs.h>
19 #else
20 #include <linux/fs.h>
21 #endif
22 #include <linux/isdnif.h>
23 #include "isdn_divert.h"
24
25 /*********************************/
26 /* Variables for interface queue */
27 /*********************************/
28 ulong if_used = 0;              /* number of interface users */
29 static struct divert_info *divert_info_head = NULL;     /* head of queue */
30 static struct divert_info *divert_info_tail = NULL;     /* pointer to last entry */
31 static spinlock_t divert_info_lock = SPIN_LOCK_UNLOCKED;/* lock for queue */
32 static wait_queue_head_t rd_queue;
33
34 /*********************************/
35 /* put an info buffer into queue */
36 /*********************************/
37 void
38 put_info_buffer(char *cp)
39 {
40         struct divert_info *ib;
41         unsigned long flags;
42
43         if (if_used <= 0)
44                 return;
45         if (!cp)
46                 return;
47         if (!*cp)
48                 return;
49         if (!(ib = (struct divert_info *) kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC)))
50                  return;        /* no memory */
51         strcpy(ib->info_start, cp);     /* set output string */
52         ib->next = NULL;
53         spin_lock_irqsave( &divert_info_lock, flags );
54         ib->usage_cnt = if_used;
55         if (!divert_info_head)
56                 divert_info_head = ib;  /* new head */
57         else
58                 divert_info_tail->next = ib;    /* follows existing messages */
59         divert_info_tail = ib;  /* new tail */
60
61         /* delete old entrys */
62         while (divert_info_head->next) {
63                 if ((divert_info_head->usage_cnt <= 0) &&
64                     (divert_info_head->next->usage_cnt <= 0)) {
65                         ib = divert_info_head;
66                         divert_info_head = divert_info_head->next;
67                         kfree(ib);
68                 } else
69                         break;
70         }                       /* divert_info_head->next */
71         spin_unlock_irqrestore( &divert_info_lock, flags );
72         wake_up_interruptible(&(rd_queue));
73 }                               /* put_info_buffer */
74
75 /**********************************/
76 /* deflection device read routine */
77 /**********************************/
78 static ssize_t
79 isdn_divert_read(struct file *file, char *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         (struct divert_info **) 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                 file->f_pos += len;
98                 return (len);
99         }
100         return (0);
101 }                               /* isdn_divert_read */
102
103 /**********************************/
104 /* deflection device write routine */
105 /**********************************/
106 static ssize_t
107 isdn_divert_write(struct file *file, const char *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 /***************************************/
116 static unsigned int
117 isdn_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 /****************/
132 static int
133 isdn_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                 (struct divert_info **) filep->private_data = &(divert_info_tail->next);
141         else
142                 (struct divert_info **) filep->private_data = &divert_info_head;
143         spin_unlock_irqrestore( &divert_info_lock, flags );
144         /*  start_divert(); */
145         return (0);
146 }                               /* isdn_divert_open */
147
148 /*******************/
149 /* close routine   */
150 /*******************/
151 static int
152 isdn_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 /*********/
177 static int
178 isdn_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, (char *) 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                         save_flags(flags);
219                         cli();
220                         *rulep = dioctl.getsetrule.rule;        /* copy data */
221                         restore_flags(flags);
222                         return (0);     /* no copy required */
223                         break;
224
225                 case IIOCINSRULE:
226                         return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule));
227                         break;
228
229                 case IIOCDELRULE:
230                         return (deleterule(dioctl.getsetrule.ruleidx));
231                         break;
232
233                 case IIOCDODFACT:
234                         return (deflect_extern_action(dioctl.fwd_ctrl.subcmd,
235                                                   dioctl.fwd_ctrl.callid,
236                                                  dioctl.fwd_ctrl.to_nr));
237
238                 case IIOCDOCFACT:
239                 case IIOCDOCFDIS:
240                 case IIOCDOCFINT:
241                         if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid))
242                                 return (-EINVAL);       /* invalid driver */
243                         if ((i = cf_command(dioctl.cf_ctrl.drvid,
244                                             (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2,
245                                             dioctl.cf_ctrl.cfproc,
246                                             dioctl.cf_ctrl.msn,
247                                             dioctl.cf_ctrl.service,
248                                             dioctl.cf_ctrl.fwd_nr,
249                                             &dioctl.cf_ctrl.procid)))
250                                 return (i);
251                         break;
252
253                 default:
254                         return (-EINVAL);
255         }                       /* switch cmd */
256         return copy_to_user((char *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0;
257 }                               /* isdn_divert_ioctl */
258
259
260 #ifdef CONFIG_PROC_FS
261 static struct file_operations isdn_fops =
262 {
263         .owner          = THIS_MODULE,
264         .llseek         = no_llseek,
265         .read           = isdn_divert_read,
266         .write          = isdn_divert_write,
267         .poll           = isdn_divert_poll,
268         .ioctl          = isdn_divert_ioctl,
269         .open           = isdn_divert_open,
270         .release        = isdn_divert_close,                                      
271 };
272
273 /****************************/
274 /* isdn subdir in /proc/net */
275 /****************************/
276 static struct proc_dir_entry *isdn_proc_entry = NULL;
277 static struct proc_dir_entry *isdn_divert_entry = NULL;
278 #endif  /* CONFIG_PROC_FS */
279
280 /***************************************************************************/
281 /* divert_dev_init must be called before the proc filesystem may be used   */
282 /***************************************************************************/
283 int
284 divert_dev_init(void)
285 {
286
287         init_waitqueue_head(&rd_queue);
288
289 #ifdef CONFIG_PROC_FS
290         isdn_proc_entry = create_proc_entry("isdn", S_IFDIR | S_IRUGO | S_IXUGO, proc_net);
291         if (!isdn_proc_entry)
292                 return (-1);
293         isdn_divert_entry = create_proc_entry("divert", S_IFREG | S_IRUGO, isdn_proc_entry);
294         if (!isdn_divert_entry) {
295                 remove_proc_entry("isdn", proc_net);
296                 return (-1);
297         }
298         isdn_divert_entry->proc_fops = &isdn_fops; 
299         isdn_divert_entry->owner = THIS_MODULE; 
300 #endif  /* CONFIG_PROC_FS */
301
302         return (0);
303 }                               /* divert_dev_init */
304
305 /***************************************************************************/
306 /* divert_dev_deinit must be called before leaving isdn when included as   */
307 /* a module.                                                               */
308 /***************************************************************************/
309 int
310 divert_dev_deinit(void)
311 {
312
313 #ifdef CONFIG_PROC_FS
314         remove_proc_entry("divert", isdn_proc_entry);
315         remove_proc_entry("isdn", proc_net);
316 #endif  /* CONFIG_PROC_FS */
317
318         return (0);
319 }                               /* divert_dev_deinit */