1 /* $Id: divasi.c,v 1.25.6.2 2005/01/31 12:22:20 armin Exp $
3 * Driver for Eicon DIVA Server ISDN cards.
4 * User Mode IDI Interface
6 * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
7 * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
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 <asm/uaccess.h>
28 static char *main_revision
= "$Revision: 1.25.6.2 $";
32 MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
33 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
34 MODULE_SUPPORTED_DEVICE("DIVA card driver");
35 MODULE_LICENSE("GPL");
37 typedef struct _diva_um_idi_os_context
{
38 wait_queue_head_t read_wait
;
39 wait_queue_head_t close_wait
;
40 struct timer_list diva_timer_id
;
43 } diva_um_idi_os_context_t
;
45 static char *DRIVERNAME
= "Eicon DIVA - User IDI (http://www.melware.net)";
46 static char *DRIVERLNAME
= "diva_idi";
47 static char *DEVNAME
= "DivasIDI";
48 char *DRIVERRELEASE_IDI
= "2.0";
50 extern int idifunc_init(void);
51 extern void idifunc_finit(void);
56 static char *getrev(const char *revision
)
60 if ((p
= strchr(revision
, ':'))) {
72 static ssize_t
um_idi_read(struct file
*file
, char __user
*buf
, size_t count
,
74 static ssize_t
um_idi_write(struct file
*file
, const char __user
*buf
,
75 size_t count
, loff_t
* offset
);
76 static unsigned int um_idi_poll(struct file
*file
, poll_table
* wait
);
77 static int um_idi_open(struct inode
*inode
, struct file
*file
);
78 static int um_idi_release(struct inode
*inode
, struct file
*file
);
79 static int remove_entity(void *entity
);
80 static void diva_um_timer_function(unsigned long data
);
85 extern struct proc_dir_entry
*proc_net_eicon
;
86 static struct proc_dir_entry
*um_idi_proc_entry
= NULL
;
89 um_idi_proc_read(char *page
, char **start
, off_t off
, int count
, int *eof
,
95 len
+= sprintf(page
+ len
, "%s\n", DRIVERNAME
);
96 len
+= sprintf(page
+ len
, "name : %s\n", DRIVERLNAME
);
97 len
+= sprintf(page
+ len
, "release : %s\n", DRIVERRELEASE_IDI
);
98 strcpy(tmprev
, main_revision
);
99 len
+= sprintf(page
+ len
, "revision : %s\n", getrev(tmprev
));
100 len
+= sprintf(page
+ len
, "build : %s\n", DIVA_BUILD
);
101 len
+= sprintf(page
+ len
, "major : %d\n", major
);
103 if (off
+ count
>= len
)
108 return ((count
< len
- off
) ? count
: len
- off
);
111 static int DIVA_INIT_FUNCTION
create_um_idi_proc(void)
113 um_idi_proc_entry
= create_proc_entry(DRIVERLNAME
,
114 S_IFREG
| S_IRUGO
| S_IWUSR
,
116 if (!um_idi_proc_entry
)
119 um_idi_proc_entry
->read_proc
= um_idi_proc_read
;
120 um_idi_proc_entry
->owner
= THIS_MODULE
;
125 static void remove_um_idi_proc(void)
127 if (um_idi_proc_entry
) {
128 remove_proc_entry(DRIVERLNAME
, proc_net_eicon
);
129 um_idi_proc_entry
= NULL
;
133 static const struct file_operations divas_idi_fops
= {
134 .owner
= THIS_MODULE
,
137 .write
= um_idi_write
,
140 .release
= um_idi_release
143 static void divas_idi_unregister_chrdev(void)
145 unregister_chrdev(major
, DEVNAME
);
148 static int DIVA_INIT_FUNCTION
divas_idi_register_chrdev(void)
150 if ((major
= register_chrdev(0, DEVNAME
, &divas_idi_fops
)) < 0)
152 printk(KERN_ERR
"%s: failed to create /dev entry.\n",
163 static int DIVA_INIT_FUNCTION
divasi_init(void)
168 printk(KERN_INFO
"%s\n", DRIVERNAME
);
169 printk(KERN_INFO
"%s: Rel:%s Rev:", DRIVERLNAME
, DRIVERRELEASE_IDI
);
170 strcpy(tmprev
, main_revision
);
171 printk("%s Build: %s\n", getrev(tmprev
), DIVA_BUILD
);
173 if (!divas_idi_register_chrdev()) {
178 if (!create_um_idi_proc()) {
179 divas_idi_unregister_chrdev();
180 printk(KERN_ERR
"%s: failed to create proc entry.\n",
186 if (!(idifunc_init())) {
187 remove_um_idi_proc();
188 divas_idi_unregister_chrdev();
189 printk(KERN_ERR
"%s: failed to connect to DIDD.\n",
194 printk(KERN_INFO
"%s: started with major %d\n", DRIVERLNAME
, major
);
204 static void DIVA_EXIT_FUNCTION
divasi_exit(void)
207 remove_um_idi_proc();
208 divas_idi_unregister_chrdev();
210 printk(KERN_INFO
"%s: module unloaded.\n", DRIVERLNAME
);
213 module_init(divasi_init
);
214 module_exit(divasi_exit
);
222 divas_um_idi_copy_to_user(void *os_handle
, void *dst
, const void *src
,
225 memcpy(dst
, src
, length
);
230 um_idi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* offset
)
232 diva_um_idi_os_context_t
*p_os
;
236 if (!file
->private_data
) {
242 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->
251 if (!(data
= diva_os_malloc(0, count
))) {
255 ret
= diva_um_idi_read(file
->private_data
,
257 divas_um_idi_copy_to_user
);
259 case 0: /* no message available */
262 case (-1): /* adapter was removed */
265 case (-2): /* message_length > length of user buffer */
271 if (copy_to_user(buf
, data
, ret
)) {
276 diva_os_free(0, data
);
277 DBG_TRC(("read: ret %d", ret
));
283 divas_um_idi_copy_from_user(void *os_handle
, void *dst
, const void *src
,
286 memcpy(dst
, src
, length
);
290 static int um_idi_open_adapter(struct file
*file
, int adapter_nr
)
292 diva_um_idi_os_context_t
*p_os
;
294 divas_um_idi_create_entity((dword
) adapter_nr
, (void *) file
);
296 if (!(file
->private_data
= e
)) {
299 p_os
= (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(e
);
300 init_waitqueue_head(&p_os
->read_wait
);
301 init_waitqueue_head(&p_os
->close_wait
);
302 init_timer(&p_os
->diva_timer_id
);
303 p_os
->diva_timer_id
.function
= (void *) diva_um_timer_function
;
304 p_os
->diva_timer_id
.data
= (unsigned long) p_os
;
306 p_os
->adapter_nr
= adapter_nr
;
311 um_idi_write(struct file
*file
, const char __user
*buf
, size_t count
,
314 diva_um_idi_os_context_t
*p_os
;
319 if (!file
->private_data
) {
320 /* the first write() selects the adapter_nr */
321 if (count
== sizeof(int)) {
323 ((void *) &adapter_nr
, buf
,
324 count
)) return (-EFAULT
);
325 if (!(um_idi_open_adapter(file
, adapter_nr
)))
333 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->
342 if (!(data
= diva_os_malloc(0, count
))) {
346 if (copy_from_user(data
, buf
, count
)) {
349 ret
= diva_um_idi_write(file
->private_data
,
351 divas_um_idi_copy_from_user
);
353 case 0: /* no space available */
356 case (-1): /* adapter was removed */
359 case (-2): /* length of user buffer > max message_length */
364 diva_os_free(0, data
);
365 DBG_TRC(("write: ret %d", ret
));
369 static unsigned int um_idi_poll(struct file
*file
, poll_table
* wait
)
371 diva_um_idi_os_context_t
*p_os
;
373 if (!file
->private_data
) {
378 (diva_um_idi_os_context_t
*)
379 diva_um_id_get_os_context(file
->private_data
)))
384 poll_wait(file
, &p_os
->read_wait
, wait
);
390 switch (diva_user_mode_idi_ind_ready(file
->private_data
, file
)) {
398 return (POLLIN
| POLLRDNORM
);
401 static int um_idi_open(struct inode
*inode
, struct file
*file
)
407 static int um_idi_release(struct inode
*inode
, struct file
*file
)
409 diva_um_idi_os_context_t
*p_os
;
410 unsigned int adapter_nr
;
413 if (!(file
->private_data
)) {
419 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->private_data
))) {
424 adapter_nr
= p_os
->adapter_nr
;
426 if ((ret
= remove_entity(file
->private_data
))) {
430 if (divas_um_idi_delete_entity
431 ((int) adapter_nr
, file
->private_data
)) {
440 int diva_os_get_context_size(void)
442 return (sizeof(diva_um_idi_os_context_t
));
445 void diva_os_wakeup_read(void *os_context
)
447 diva_um_idi_os_context_t
*p_os
=
448 (diva_um_idi_os_context_t
*) os_context
;
449 wake_up_interruptible(&p_os
->read_wait
);
452 void diva_os_wakeup_close(void *os_context
)
454 diva_um_idi_os_context_t
*p_os
=
455 (diva_um_idi_os_context_t
*) os_context
;
456 wake_up_interruptible(&p_os
->close_wait
);
460 void diva_um_timer_function(unsigned long data
)
462 diva_um_idi_os_context_t
*p_os
= (diva_um_idi_os_context_t
*) data
;
465 wake_up_interruptible(&p_os
->read_wait
);
466 wake_up_interruptible(&p_os
->close_wait
);
467 DBG_ERR(("entity removal watchdog"))
471 ** If application exits without entity removal this function will remove
472 ** entity and block until removal is complete
474 static int remove_entity(void *entity
)
476 struct task_struct
*curtask
= current
;
477 diva_um_idi_os_context_t
*p_os
;
479 diva_um_idi_stop_wdog(entity
);
482 DBG_FTL(("Zero entity on remove"))
487 (diva_um_idi_os_context_t
*)
488 diva_um_id_get_os_context(entity
))) {
489 DBG_FTL(("Zero entity os context on remove"))
493 if (!divas_um_idi_entity_assigned(entity
) || p_os
->aborted
) {
495 Entity is not assigned, also can be removed
500 DBG_TRC(("E(%08x) check remove", entity
))
503 If adapter not answers on remove request inside of
504 10 Sec, then adapter is dead
506 diva_um_idi_start_wdog(entity
);
509 DECLARE_WAITQUEUE(wait
, curtask
);
511 add_wait_queue(&p_os
->close_wait
, &wait
);
513 set_current_state(TASK_INTERRUPTIBLE
);
514 if (!divas_um_idi_entity_start_remove(entity
)
520 set_current_state(TASK_RUNNING
);
521 remove_wait_queue(&p_os
->close_wait
, &wait
);
524 DBG_TRC(("E(%08x) start remove", entity
))
526 DECLARE_WAITQUEUE(wait
, curtask
);
528 add_wait_queue(&p_os
->close_wait
, &wait
);
530 set_current_state(TASK_INTERRUPTIBLE
);
531 if (!divas_um_idi_entity_assigned(entity
)
537 set_current_state(TASK_RUNNING
);
538 remove_wait_queue(&p_os
->close_wait
, &wait
);
541 DBG_TRC(("E(%08x) remove complete, aborted:%d", entity
,
544 diva_um_idi_stop_wdog(entity
);
554 void diva_um_idi_start_wdog(void *entity
)
556 diva_um_idi_os_context_t
*p_os
;
560 (diva_um_idi_os_context_t
*)
561 diva_um_id_get_os_context(entity
)))) {
562 mod_timer(&p_os
->diva_timer_id
, jiffies
+ 10 * HZ
);
566 void diva_um_idi_stop_wdog(void *entity
)
568 diva_um_idi_os_context_t
*p_os
;
572 (diva_um_idi_os_context_t
*)
573 diva_um_id_get_os_context(entity
)))) {
574 del_timer(&p_os
->diva_timer_id
);