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