ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / hardware / eicon / divasi.c
1 /* $Id: divasi.c,v 1.25 2003/09/09 06:46:29 schindler 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 <linux/devfs_fs_kernel.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 $";
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 *buf, size_t count,
75                            loff_t * offset);
76 static ssize_t um_idi_write(struct file *file, const char *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         devfs_remove(DEVNAME);
148         unregister_chrdev(major, DEVNAME);
149 }
150
151 static int DIVA_INIT_FUNCTION divas_idi_register_chrdev(void)
152 {
153         if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
154         {
155                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
156                        DRIVERLNAME);
157                 return (0);
158         }
159         devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
160
161         return (1);
162 }
163
164 /*
165 ** Driver Load
166 */
167 static int DIVA_INIT_FUNCTION divasi_init(void)
168 {
169         char tmprev[50];
170         int ret = 0;
171
172         printk(KERN_INFO "%s\n", DRIVERNAME);
173         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
174         strcpy(tmprev, main_revision);
175         printk("%s  Build: %s\n", getrev(tmprev), DIVA_BUILD);
176
177         if (!divas_idi_register_chrdev()) {
178                 ret = -EIO;
179                 goto out;
180         }
181
182         if (!create_um_idi_proc()) {
183                 divas_idi_unregister_chrdev();
184                 printk(KERN_ERR "%s: failed to create proc entry.\n",
185                        DRIVERLNAME);
186                 ret = -EIO;
187                 goto out;
188         }
189
190         if (!(idifunc_init())) {
191                 remove_um_idi_proc();
192                 divas_idi_unregister_chrdev();
193                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
194                        DRIVERLNAME);
195                 ret = -EIO;
196                 goto out;
197         }
198         printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
199
200       out:
201         return (ret);
202 }
203
204
205 /*
206 ** Driver Unload
207 */
208 static void DIVA_EXIT_FUNCTION divasi_exit(void)
209 {
210         idifunc_finit();
211         remove_um_idi_proc();
212         divas_idi_unregister_chrdev();
213
214         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
215 }
216
217 module_init(divasi_init);
218 module_exit(divasi_exit);
219
220
221 /*
222  *  FILE OPERATIONS
223  */
224
225 static int
226 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
227                           int length)
228 {
229         memcpy(dst, src, length);
230         return (length);
231 }
232
233 static ssize_t
234 um_idi_read(struct file *file, char *buf, size_t count, loff_t * offset)
235 {
236         diva_um_idi_os_context_t *p_os;
237         int ret = -EINVAL;
238         void *data;
239
240         if (!file->private_data) {
241                 return (-ENODEV);
242         }
243
244         if (!
245             (p_os =
246              (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
247                                                                     private_data)))
248         {
249                 return (-ENODEV);
250         }
251         if (p_os->aborted) {
252                 return (-ENODEV);
253         }
254
255         if (!(data = diva_os_malloc(0, count))) {
256                 return (-ENOMEM);
257         }
258
259         ret = diva_um_idi_read(file->private_data,
260                                file, data, count,
261                                divas_um_idi_copy_to_user);
262         switch (ret) {
263         case 0:         /* no message available */
264                 ret = (-EAGAIN);
265                 break;
266         case (-1):              /* adapter was removed */
267                 ret = (-ENODEV);
268                 break;
269         case (-2):              /* message_length > length of user buffer */
270                 ret = (-EFAULT);
271                 break;
272         }
273
274         if (ret > 0) {
275                 if (copy_to_user(buf, data, ret)) {
276                         ret = (-EFAULT);
277                 }
278         }
279
280         diva_os_free(0, data);
281         DBG_TRC(("read: ret %d", ret));
282         return (ret);
283 }
284
285
286 static int
287 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
288                             int length)
289 {
290         memcpy(dst, src, length);
291         return (length);
292 }
293
294 static int um_idi_open_adapter(struct file *file, int adapter_nr)
295 {
296         diva_um_idi_os_context_t *p_os;
297         void *e =
298             divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
299
300         if (!(file->private_data = e)) {
301                 return (0);
302         }
303         p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
304         init_waitqueue_head(&p_os->read_wait);
305         init_waitqueue_head(&p_os->close_wait);
306         init_timer(&p_os->diva_timer_id);
307         p_os->diva_timer_id.function = (void *) diva_um_timer_function;
308         p_os->diva_timer_id.data = (unsigned long) p_os;
309         p_os->aborted = 0;
310         p_os->adapter_nr = adapter_nr;
311         return (1);
312 }
313
314 static ssize_t
315 um_idi_write(struct file *file, const char *buf, size_t count,
316              loff_t * offset)
317 {
318         diva_um_idi_os_context_t *p_os;
319         int ret = -EINVAL;
320         void *data;
321         int adapter_nr = 0;
322
323         if (!file->private_data) {
324                 /* the first write() selects the adapter_nr */
325                 if (count == sizeof(int)) {
326                         if (copy_from_user
327                             ((void *) &adapter_nr, buf,
328                              count)) return (-EFAULT);
329                         if (!(um_idi_open_adapter(file, adapter_nr)))
330                                 return (-ENODEV);
331                         return (count);
332                 } else
333                         return (-ENODEV);
334         }
335
336         if (!(p_os =
337              (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
338                                                                     private_data)))
339         {
340                 return (-ENODEV);
341         }
342         if (p_os->aborted) {
343                 return (-ENODEV);
344         }
345
346         if (!(data = diva_os_malloc(0, count))) {
347                 return (-ENOMEM);
348         }
349
350         if (copy_from_user(data, buf, count)) {
351                 ret = -EFAULT;
352         } else {
353                 ret = diva_um_idi_write(file->private_data,
354                                         file, data, count,
355                                         divas_um_idi_copy_from_user);
356                 switch (ret) {
357                 case 0: /* no space available */
358                         ret = (-EAGAIN);
359                         break;
360                 case (-1):      /* adapter was removed */
361                         ret = (-ENODEV);
362                         break;
363                 case (-2):      /* length of user buffer > max message_length */
364                         ret = (-EFAULT);
365                         break;
366                 }
367         }
368         diva_os_free(0, data);
369         DBG_TRC(("write: ret %d", ret));
370         return (ret);
371 }
372
373 static unsigned int um_idi_poll(struct file *file, poll_table * wait)
374 {
375         diva_um_idi_os_context_t *p_os;
376
377         if (!file->private_data) {
378                 return (POLLERR);
379         }
380
381         if ((!(p_os =
382                (diva_um_idi_os_context_t *)
383                diva_um_id_get_os_context(file->private_data)))
384             || p_os->aborted) {
385                 return (POLLERR);
386         }
387
388         poll_wait(file, &p_os->read_wait, wait);
389
390         if (p_os->aborted) {
391                 return (POLLERR);
392         }
393
394         switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
395         case (-1):
396                 return (POLLERR);
397
398         case 0:
399                 return (0);
400         }
401
402         return (POLLIN | POLLRDNORM);
403 }
404
405 static int um_idi_open(struct inode *inode, struct file *file)
406 {
407         return (0);
408 }
409
410
411 static int um_idi_release(struct inode *inode, struct file *file)
412 {
413         diva_um_idi_os_context_t *p_os;
414         unsigned int adapter_nr;
415         int ret = 0;
416
417         if (!(file->private_data)) {
418                 ret = -ENODEV;
419                 goto out;
420         }
421
422         if (!(p_os =
423                 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
424                 ret = -ENODEV;
425                 goto out;
426         }
427
428         adapter_nr = p_os->adapter_nr;
429
430         if ((ret = remove_entity(file->private_data))) {
431                 goto out;
432         }
433
434         if (divas_um_idi_delete_entity
435             ((int) adapter_nr, file->private_data)) {
436                 ret = -ENODEV;
437                 goto out;
438         }
439
440       out:
441         return (ret);
442 }
443
444 int diva_os_get_context_size(void)
445 {
446         return (sizeof(diva_um_idi_os_context_t));
447 }
448
449 void diva_os_wakeup_read(void *os_context)
450 {
451         diva_um_idi_os_context_t *p_os =
452             (diva_um_idi_os_context_t *) os_context;
453         wake_up_interruptible(&p_os->read_wait);
454 }
455
456 void diva_os_wakeup_close(void *os_context)
457 {
458         diva_um_idi_os_context_t *p_os =
459             (diva_um_idi_os_context_t *) os_context;
460         wake_up_interruptible(&p_os->close_wait);
461 }
462
463 static
464 void diva_um_timer_function(unsigned long data)
465 {
466         diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
467
468         p_os->aborted = 1;
469         wake_up_interruptible(&p_os->read_wait);
470         wake_up_interruptible(&p_os->close_wait);
471         DBG_ERR(("entity removal watchdog"))
472 }
473
474 /*
475 **  If application exits without entity removal this function will remove
476 **  entity and block until removal is complete
477 */
478 static int remove_entity(void *entity)
479 {
480         struct task_struct *curtask = current;
481         diva_um_idi_os_context_t *p_os;
482
483         diva_um_idi_stop_wdog(entity);
484
485         if (!entity) {
486                 DBG_FTL(("Zero entity on remove"))
487                 return (0);
488         }
489
490         if (!(p_os =
491              (diva_um_idi_os_context_t *)
492              diva_um_id_get_os_context(entity))) {
493                 DBG_FTL(("Zero entity os context on remove"))
494                 return (0);
495         }
496
497         if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
498                 /*
499                    Entity is not assigned, also can be removed
500                  */
501                 return (0);
502         }
503
504         DBG_TRC(("E(%08x) check remove", entity))
505
506         /*
507            If adapter not answers on remove request inside of
508            10 Sec, then adapter is dead
509          */
510         diva_um_idi_start_wdog(entity);
511
512         {
513                 DECLARE_WAITQUEUE(wait, curtask);
514
515                 add_wait_queue(&p_os->close_wait, &wait);
516                 for (;;) {
517                         set_current_state(TASK_INTERRUPTIBLE);
518                         if (!divas_um_idi_entity_start_remove(entity)
519                             || p_os->aborted) {
520                                 break;
521                         }
522                         schedule();
523                 }
524                 set_current_state(TASK_RUNNING);
525                 remove_wait_queue(&p_os->close_wait, &wait);
526         }
527
528         DBG_TRC(("E(%08x) start remove", entity))
529         {
530                 DECLARE_WAITQUEUE(wait, curtask);
531
532                 add_wait_queue(&p_os->close_wait, &wait);
533                 for (;;) {
534                         set_current_state(TASK_INTERRUPTIBLE);
535                         if (!divas_um_idi_entity_assigned(entity)
536                             || p_os->aborted) {
537                                 break;
538                         }
539                         schedule();
540                 }
541                 set_current_state(TASK_RUNNING);
542                 remove_wait_queue(&p_os->close_wait, &wait);
543         }
544
545         DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
546                  p_os->aborted))
547
548         diva_um_idi_stop_wdog(entity);
549
550         p_os->aborted = 0;
551
552         return (0);
553 }
554
555 /*
556  * timer watchdog
557  */
558 void diva_um_idi_start_wdog(void *entity)
559 {
560         diva_um_idi_os_context_t *p_os;
561
562         if (entity &&
563             ((p_os =
564               (diva_um_idi_os_context_t *)
565               diva_um_id_get_os_context(entity)))) {
566                 mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
567         }
568 }
569
570 void diva_um_idi_stop_wdog(void *entity)
571 {
572         diva_um_idi_os_context_t *p_os;
573
574         if (entity &&
575             ((p_os =
576               (diva_um_idi_os_context_t *)
577               diva_um_id_get_os_context(entity)))) {
578                 del_timer(&p_os->diva_timer_id);
579         }
580 }