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