[PATCH] devfs: Remove the devfs_fs_kernel.h file from the tree
[safe/jmp/linux-2.6] / drivers / isdn / hardware / eicon / divasi.c
1 /* $Id: divasi.c,v 1.25.6.2 2005/01/31 12:22:20 armin Exp $
2  *
3  * Driver for Eicon DIVA Server ISDN cards.
4  * User Mode IDI Interface 
5  *
6  * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
7  * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  */
12
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/smp_lock.h>
19 #include <linux/poll.h>
20 #include <linux/proc_fs.h>
21 #include <linux/skbuff.h>
22 #include <asm/uaccess.h>
23
24 #include "platform.h"
25 #include "di_defs.h"
26 #include "divasync.h"
27 #include "um_xdi.h"
28 #include "um_idi.h"
29
30 static char *main_revision = "$Revision: 1.25.6.2 $";
31
32 static int major;
33
34 MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
35 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
36 MODULE_SUPPORTED_DEVICE("DIVA card driver");
37 MODULE_LICENSE("GPL");
38
39 typedef struct _diva_um_idi_os_context {
40         wait_queue_head_t read_wait;
41         wait_queue_head_t close_wait;
42         struct timer_list diva_timer_id;
43         int aborted;
44         int adapter_nr;
45 } diva_um_idi_os_context_t;
46
47 static char *DRIVERNAME = "Eicon DIVA - User IDI (http://www.melware.net)";
48 static char *DRIVERLNAME = "diva_idi";
49 static char *DEVNAME = "DivasIDI";
50 char *DRIVERRELEASE_IDI = "2.0";
51
52 extern int idifunc_init(void);
53 extern void idifunc_finit(void);
54
55 /*
56  *  helper functions
57  */
58 static char *getrev(const char *revision)
59 {
60         char *rev;
61         char *p;
62         if ((p = strchr(revision, ':'))) {
63                 rev = p + 2;
64                 p = strchr(rev, '$');
65                 *--p = 0;
66         } else
67                 rev = "1.0";
68         return rev;
69 }
70
71 /*
72  *  LOCALS
73  */
74 static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
75                            loff_t * offset);
76 static ssize_t um_idi_write(struct file *file, const char __user *buf,
77                             size_t count, loff_t * offset);
78 static unsigned int um_idi_poll(struct file *file, poll_table * wait);
79 static int um_idi_open(struct inode *inode, struct file *file);
80 static int um_idi_release(struct inode *inode, struct file *file);
81 static int remove_entity(void *entity);
82 static void diva_um_timer_function(unsigned long data);
83
84 /*
85  * proc entry
86  */
87 extern struct proc_dir_entry *proc_net_eicon;
88 static struct proc_dir_entry *um_idi_proc_entry = NULL;
89
90 static int
91 um_idi_proc_read(char *page, char **start, off_t off, int count, int *eof,
92                  void *data)
93 {
94         int len = 0;
95         char tmprev[32];
96
97         len += sprintf(page + len, "%s\n", DRIVERNAME);
98         len += sprintf(page + len, "name     : %s\n", DRIVERLNAME);
99         len += sprintf(page + len, "release  : %s\n", DRIVERRELEASE_IDI);
100         strcpy(tmprev, main_revision);
101         len += sprintf(page + len, "revision : %s\n", getrev(tmprev));
102         len += sprintf(page + len, "build    : %s\n", DIVA_BUILD);
103         len += sprintf(page + len, "major    : %d\n", major);
104
105         if (off + count >= len)
106                 *eof = 1;
107         if (len < off)
108                 return 0;
109         *start = page + off;
110         return ((count < len - off) ? count : len - off);
111 }
112
113 static int DIVA_INIT_FUNCTION create_um_idi_proc(void)
114 {
115         um_idi_proc_entry = create_proc_entry(DRIVERLNAME,
116                                               S_IFREG | S_IRUGO | S_IWUSR,
117                                               proc_net_eicon);
118         if (!um_idi_proc_entry)
119                 return (0);
120
121         um_idi_proc_entry->read_proc = um_idi_proc_read;
122         um_idi_proc_entry->owner = THIS_MODULE;
123
124         return (1);
125 }
126
127 static void remove_um_idi_proc(void)
128 {
129         if (um_idi_proc_entry) {
130                 remove_proc_entry(DRIVERLNAME, proc_net_eicon);
131                 um_idi_proc_entry = NULL;
132         }
133 }
134
135 static struct file_operations divas_idi_fops = {
136         .owner   = THIS_MODULE,
137         .llseek  = no_llseek,
138         .read    = um_idi_read,
139         .write   = um_idi_write,
140         .poll    = um_idi_poll,
141         .open    = um_idi_open,
142         .release = um_idi_release
143 };
144
145 static void divas_idi_unregister_chrdev(void)
146 {
147         unregister_chrdev(major, DEVNAME);
148 }
149
150 static int DIVA_INIT_FUNCTION divas_idi_register_chrdev(void)
151 {
152         if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
153         {
154                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
155                        DRIVERLNAME);
156                 return (0);
157         }
158
159         return (1);
160 }
161
162 /*
163 ** Driver Load
164 */
165 static int DIVA_INIT_FUNCTION divasi_init(void)
166 {
167         char tmprev[50];
168         int ret = 0;
169
170         printk(KERN_INFO "%s\n", DRIVERNAME);
171         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
172         strcpy(tmprev, main_revision);
173         printk("%s  Build: %s\n", getrev(tmprev), DIVA_BUILD);
174
175         if (!divas_idi_register_chrdev()) {
176                 ret = -EIO;
177                 goto out;
178         }
179
180         if (!create_um_idi_proc()) {
181                 divas_idi_unregister_chrdev();
182                 printk(KERN_ERR "%s: failed to create proc entry.\n",
183                        DRIVERLNAME);
184                 ret = -EIO;
185                 goto out;
186         }
187
188         if (!(idifunc_init())) {
189                 remove_um_idi_proc();
190                 divas_idi_unregister_chrdev();
191                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
192                        DRIVERLNAME);
193                 ret = -EIO;
194                 goto out;
195         }
196         printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
197
198       out:
199         return (ret);
200 }
201
202
203 /*
204 ** Driver Unload
205 */
206 static void DIVA_EXIT_FUNCTION divasi_exit(void)
207 {
208         idifunc_finit();
209         remove_um_idi_proc();
210         divas_idi_unregister_chrdev();
211
212         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
213 }
214
215 module_init(divasi_init);
216 module_exit(divasi_exit);
217
218
219 /*
220  *  FILE OPERATIONS
221  */
222
223 static int
224 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
225                           int length)
226 {
227         memcpy(dst, src, length);
228         return (length);
229 }
230
231 static ssize_t
232 um_idi_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
233 {
234         diva_um_idi_os_context_t *p_os;
235         int ret = -EINVAL;
236         void *data;
237
238         if (!file->private_data) {
239                 return (-ENODEV);
240         }
241
242         if (!
243             (p_os =
244              (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
245                                                                     private_data)))
246         {
247                 return (-ENODEV);
248         }
249         if (p_os->aborted) {
250                 return (-ENODEV);
251         }
252
253         if (!(data = diva_os_malloc(0, count))) {
254                 return (-ENOMEM);
255         }
256
257         ret = diva_um_idi_read(file->private_data,
258                                file, data, count,
259                                divas_um_idi_copy_to_user);
260         switch (ret) {
261         case 0:         /* no message available */
262                 ret = (-EAGAIN);
263                 break;
264         case (-1):              /* adapter was removed */
265                 ret = (-ENODEV);
266                 break;
267         case (-2):              /* message_length > length of user buffer */
268                 ret = (-EFAULT);
269                 break;
270         }
271
272         if (ret > 0) {
273                 if (copy_to_user(buf, data, ret)) {
274                         ret = (-EFAULT);
275                 }
276         }
277
278         diva_os_free(0, data);
279         DBG_TRC(("read: ret %d", ret));
280         return (ret);
281 }
282
283
284 static int
285 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
286                             int length)
287 {
288         memcpy(dst, src, length);
289         return (length);
290 }
291
292 static int um_idi_open_adapter(struct file *file, int adapter_nr)
293 {
294         diva_um_idi_os_context_t *p_os;
295         void *e =
296             divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
297
298         if (!(file->private_data = e)) {
299                 return (0);
300         }
301         p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
302         init_waitqueue_head(&p_os->read_wait);
303         init_waitqueue_head(&p_os->close_wait);
304         init_timer(&p_os->diva_timer_id);
305         p_os->diva_timer_id.function = (void *) diva_um_timer_function;
306         p_os->diva_timer_id.data = (unsigned long) p_os;
307         p_os->aborted = 0;
308         p_os->adapter_nr = adapter_nr;
309         return (1);
310 }
311
312 static ssize_t
313 um_idi_write(struct file *file, const char __user *buf, size_t count,
314              loff_t * offset)
315 {
316         diva_um_idi_os_context_t *p_os;
317         int ret = -EINVAL;
318         void *data;
319         int adapter_nr = 0;
320
321         if (!file->private_data) {
322                 /* the first write() selects the adapter_nr */
323                 if (count == sizeof(int)) {
324                         if (copy_from_user
325                             ((void *) &adapter_nr, buf,
326                              count)) return (-EFAULT);
327                         if (!(um_idi_open_adapter(file, adapter_nr)))
328                                 return (-ENODEV);
329                         return (count);
330                 } else
331                         return (-ENODEV);
332         }
333
334         if (!(p_os =
335              (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
336                                                                     private_data)))
337         {
338                 return (-ENODEV);
339         }
340         if (p_os->aborted) {
341                 return (-ENODEV);
342         }
343
344         if (!(data = diva_os_malloc(0, count))) {
345                 return (-ENOMEM);
346         }
347
348         if (copy_from_user(data, buf, count)) {
349                 ret = -EFAULT;
350         } else {
351                 ret = diva_um_idi_write(file->private_data,
352                                         file, data, count,
353                                         divas_um_idi_copy_from_user);
354                 switch (ret) {
355                 case 0: /* no space available */
356                         ret = (-EAGAIN);
357                         break;
358                 case (-1):      /* adapter was removed */
359                         ret = (-ENODEV);
360                         break;
361                 case (-2):      /* length of user buffer > max message_length */
362                         ret = (-EFAULT);
363                         break;
364                 }
365         }
366         diva_os_free(0, data);
367         DBG_TRC(("write: ret %d", ret));
368         return (ret);
369 }
370
371 static unsigned int um_idi_poll(struct file *file, poll_table * wait)
372 {
373         diva_um_idi_os_context_t *p_os;
374
375         if (!file->private_data) {
376                 return (POLLERR);
377         }
378
379         if ((!(p_os =
380                (diva_um_idi_os_context_t *)
381                diva_um_id_get_os_context(file->private_data)))
382             || p_os->aborted) {
383                 return (POLLERR);
384         }
385
386         poll_wait(file, &p_os->read_wait, wait);
387
388         if (p_os->aborted) {
389                 return (POLLERR);
390         }
391
392         switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
393         case (-1):
394                 return (POLLERR);
395
396         case 0:
397                 return (0);
398         }
399
400         return (POLLIN | POLLRDNORM);
401 }
402
403 static int um_idi_open(struct inode *inode, struct file *file)
404 {
405         return (0);
406 }
407
408
409 static int um_idi_release(struct inode *inode, struct file *file)
410 {
411         diva_um_idi_os_context_t *p_os;
412         unsigned int adapter_nr;
413         int ret = 0;
414
415         if (!(file->private_data)) {
416                 ret = -ENODEV;
417                 goto out;
418         }
419
420         if (!(p_os =
421                 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
422                 ret = -ENODEV;
423                 goto out;
424         }
425
426         adapter_nr = p_os->adapter_nr;
427
428         if ((ret = remove_entity(file->private_data))) {
429                 goto out;
430         }
431
432         if (divas_um_idi_delete_entity
433             ((int) adapter_nr, file->private_data)) {
434                 ret = -ENODEV;
435                 goto out;
436         }
437
438       out:
439         return (ret);
440 }
441
442 int diva_os_get_context_size(void)
443 {
444         return (sizeof(diva_um_idi_os_context_t));
445 }
446
447 void diva_os_wakeup_read(void *os_context)
448 {
449         diva_um_idi_os_context_t *p_os =
450             (diva_um_idi_os_context_t *) os_context;
451         wake_up_interruptible(&p_os->read_wait);
452 }
453
454 void diva_os_wakeup_close(void *os_context)
455 {
456         diva_um_idi_os_context_t *p_os =
457             (diva_um_idi_os_context_t *) os_context;
458         wake_up_interruptible(&p_os->close_wait);
459 }
460
461 static
462 void diva_um_timer_function(unsigned long data)
463 {
464         diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
465
466         p_os->aborted = 1;
467         wake_up_interruptible(&p_os->read_wait);
468         wake_up_interruptible(&p_os->close_wait);
469         DBG_ERR(("entity removal watchdog"))
470 }
471
472 /*
473 **  If application exits without entity removal this function will remove
474 **  entity and block until removal is complete
475 */
476 static int remove_entity(void *entity)
477 {
478         struct task_struct *curtask = current;
479         diva_um_idi_os_context_t *p_os;
480
481         diva_um_idi_stop_wdog(entity);
482
483         if (!entity) {
484                 DBG_FTL(("Zero entity on remove"))
485                 return (0);
486         }
487
488         if (!(p_os =
489              (diva_um_idi_os_context_t *)
490              diva_um_id_get_os_context(entity))) {
491                 DBG_FTL(("Zero entity os context on remove"))
492                 return (0);
493         }
494
495         if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
496                 /*
497                    Entity is not assigned, also can be removed
498                  */
499                 return (0);
500         }
501
502         DBG_TRC(("E(%08x) check remove", entity))
503
504         /*
505            If adapter not answers on remove request inside of
506            10 Sec, then adapter is dead
507          */
508         diva_um_idi_start_wdog(entity);
509
510         {
511                 DECLARE_WAITQUEUE(wait, curtask);
512
513                 add_wait_queue(&p_os->close_wait, &wait);
514                 for (;;) {
515                         set_current_state(TASK_INTERRUPTIBLE);
516                         if (!divas_um_idi_entity_start_remove(entity)
517                             || p_os->aborted) {
518                                 break;
519                         }
520                         schedule();
521                 }
522                 set_current_state(TASK_RUNNING);
523                 remove_wait_queue(&p_os->close_wait, &wait);
524         }
525
526         DBG_TRC(("E(%08x) start remove", entity))
527         {
528                 DECLARE_WAITQUEUE(wait, curtask);
529
530                 add_wait_queue(&p_os->close_wait, &wait);
531                 for (;;) {
532                         set_current_state(TASK_INTERRUPTIBLE);
533                         if (!divas_um_idi_entity_assigned(entity)
534                             || p_os->aborted) {
535                                 break;
536                         }
537                         schedule();
538                 }
539                 set_current_state(TASK_RUNNING);
540                 remove_wait_queue(&p_os->close_wait, &wait);
541         }
542
543         DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
544                  p_os->aborted))
545
546         diva_um_idi_stop_wdog(entity);
547
548         p_os->aborted = 0;
549
550         return (0);
551 }
552
553 /*
554  * timer watchdog
555  */
556 void diva_um_idi_start_wdog(void *entity)
557 {
558         diva_um_idi_os_context_t *p_os;
559
560         if (entity &&
561             ((p_os =
562               (diva_um_idi_os_context_t *)
563               diva_um_id_get_os_context(entity)))) {
564                 mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
565         }
566 }
567
568 void diva_um_idi_stop_wdog(void *entity)
569 {
570         diva_um_idi_os_context_t *p_os;
571
572         if (entity &&
573             ((p_os =
574               (diva_um_idi_os_context_t *)
575               diva_um_id_get_os_context(entity)))) {
576                 del_timer(&p_os->diva_timer_id);
577         }
578 }