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 <linux/seq_file.h>
21 #include <linux/smp_lock.h>
22 #include <asm/uaccess.h>
30 static char *main_revision
= "$Revision: 1.25.6.2 $";
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");
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
;
45 } diva_um_idi_os_context_t
;
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";
52 extern int idifunc_init(void);
53 extern void idifunc_finit(void);
58 static char *getrev(const char *revision
)
62 if ((p
= strchr(revision
, ':'))) {
74 static ssize_t
um_idi_read(struct file
*file
, char __user
*buf
, size_t count
,
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
);
87 extern struct proc_dir_entry
*proc_net_eicon
;
88 static struct proc_dir_entry
*um_idi_proc_entry
= NULL
;
90 static int um_idi_proc_show(struct seq_file
*m
, void *v
)
94 seq_printf(m
, "%s\n", DRIVERNAME
);
95 seq_printf(m
, "name : %s\n", DRIVERLNAME
);
96 seq_printf(m
, "release : %s\n", DRIVERRELEASE_IDI
);
97 strcpy(tmprev
, main_revision
);
98 seq_printf(m
, "revision : %s\n", getrev(tmprev
));
99 seq_printf(m
, "build : %s\n", DIVA_BUILD
);
100 seq_printf(m
, "major : %d\n", major
);
105 static int um_idi_proc_open(struct inode
*inode
, struct file
*file
)
107 return single_open(file
, um_idi_proc_show
, NULL
);
110 static const struct file_operations um_idi_proc_fops
= {
111 .owner
= THIS_MODULE
,
112 .open
= um_idi_proc_open
,
115 .release
= single_release
,
118 static int DIVA_INIT_FUNCTION
create_um_idi_proc(void)
120 um_idi_proc_entry
= proc_create(DRIVERLNAME
, S_IRUGO
, proc_net_eicon
,
122 if (!um_idi_proc_entry
)
127 static void remove_um_idi_proc(void)
129 if (um_idi_proc_entry
) {
130 remove_proc_entry(DRIVERLNAME
, proc_net_eicon
);
131 um_idi_proc_entry
= NULL
;
135 static const struct file_operations divas_idi_fops
= {
136 .owner
= THIS_MODULE
,
139 .write
= um_idi_write
,
142 .release
= um_idi_release
145 static void divas_idi_unregister_chrdev(void)
147 unregister_chrdev(major
, DEVNAME
);
150 static int DIVA_INIT_FUNCTION
divas_idi_register_chrdev(void)
152 if ((major
= register_chrdev(0, DEVNAME
, &divas_idi_fops
)) < 0)
154 printk(KERN_ERR
"%s: failed to create /dev entry.\n",
165 static int DIVA_INIT_FUNCTION
divasi_init(void)
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
);
175 if (!divas_idi_register_chrdev()) {
180 if (!create_um_idi_proc()) {
181 divas_idi_unregister_chrdev();
182 printk(KERN_ERR
"%s: failed to create proc entry.\n",
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",
196 printk(KERN_INFO
"%s: started with major %d\n", DRIVERLNAME
, major
);
206 static void DIVA_EXIT_FUNCTION
divasi_exit(void)
209 remove_um_idi_proc();
210 divas_idi_unregister_chrdev();
212 printk(KERN_INFO
"%s: module unloaded.\n", DRIVERLNAME
);
215 module_init(divasi_init
);
216 module_exit(divasi_exit
);
224 divas_um_idi_copy_to_user(void *os_handle
, void *dst
, const void *src
,
227 memcpy(dst
, src
, length
);
232 um_idi_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* offset
)
234 diva_um_idi_os_context_t
*p_os
;
238 if (!file
->private_data
) {
244 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->
253 if (!(data
= diva_os_malloc(0, count
))) {
257 ret
= diva_um_idi_read(file
->private_data
,
259 divas_um_idi_copy_to_user
);
261 case 0: /* no message available */
264 case (-1): /* adapter was removed */
267 case (-2): /* message_length > length of user buffer */
273 if (copy_to_user(buf
, data
, ret
)) {
278 diva_os_free(0, data
);
279 DBG_TRC(("read: ret %d", ret
));
285 divas_um_idi_copy_from_user(void *os_handle
, void *dst
, const void *src
,
288 memcpy(dst
, src
, length
);
292 static int um_idi_open_adapter(struct file
*file
, int adapter_nr
)
294 diva_um_idi_os_context_t
*p_os
;
296 divas_um_idi_create_entity((dword
) adapter_nr
, (void *) file
);
298 if (!(file
->private_data
= e
)) {
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
;
308 p_os
->adapter_nr
= adapter_nr
;
313 um_idi_write(struct file
*file
, const char __user
*buf
, size_t count
,
316 diva_um_idi_os_context_t
*p_os
;
321 if (!file
->private_data
) {
322 /* the first write() selects the adapter_nr */
323 if (count
== sizeof(int)) {
325 ((void *) &adapter_nr
, buf
,
326 count
)) return (-EFAULT
);
327 if (!(um_idi_open_adapter(file
, adapter_nr
)))
335 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->
344 if (!(data
= diva_os_malloc(0, count
))) {
348 if (copy_from_user(data
, buf
, count
)) {
351 ret
= diva_um_idi_write(file
->private_data
,
353 divas_um_idi_copy_from_user
);
355 case 0: /* no space available */
358 case (-1): /* adapter was removed */
361 case (-2): /* length of user buffer > max message_length */
366 diva_os_free(0, data
);
367 DBG_TRC(("write: ret %d", ret
));
371 static unsigned int um_idi_poll(struct file
*file
, poll_table
* wait
)
373 diva_um_idi_os_context_t
*p_os
;
375 if (!file
->private_data
) {
380 (diva_um_idi_os_context_t
*)
381 diva_um_id_get_os_context(file
->private_data
)))
386 poll_wait(file
, &p_os
->read_wait
, wait
);
392 switch (diva_user_mode_idi_ind_ready(file
->private_data
, file
)) {
400 return (POLLIN
| POLLRDNORM
);
403 static int um_idi_open(struct inode
*inode
, struct file
*file
)
410 static int um_idi_release(struct inode
*inode
, struct file
*file
)
412 diva_um_idi_os_context_t
*p_os
;
413 unsigned int adapter_nr
;
416 if (!(file
->private_data
)) {
422 (diva_um_idi_os_context_t
*) diva_um_id_get_os_context(file
->private_data
))) {
427 adapter_nr
= p_os
->adapter_nr
;
429 if ((ret
= remove_entity(file
->private_data
))) {
433 if (divas_um_idi_delete_entity
434 ((int) adapter_nr
, file
->private_data
)) {
443 int diva_os_get_context_size(void)
445 return (sizeof(diva_um_idi_os_context_t
));
448 void diva_os_wakeup_read(void *os_context
)
450 diva_um_idi_os_context_t
*p_os
=
451 (diva_um_idi_os_context_t
*) os_context
;
452 wake_up_interruptible(&p_os
->read_wait
);
455 void diva_os_wakeup_close(void *os_context
)
457 diva_um_idi_os_context_t
*p_os
=
458 (diva_um_idi_os_context_t
*) os_context
;
459 wake_up_interruptible(&p_os
->close_wait
);
463 void diva_um_timer_function(unsigned long data
)
465 diva_um_idi_os_context_t
*p_os
= (diva_um_idi_os_context_t
*) data
;
468 wake_up_interruptible(&p_os
->read_wait
);
469 wake_up_interruptible(&p_os
->close_wait
);
470 DBG_ERR(("entity removal watchdog"))
474 ** If application exits without entity removal this function will remove
475 ** entity and block until removal is complete
477 static int remove_entity(void *entity
)
479 struct task_struct
*curtask
= current
;
480 diva_um_idi_os_context_t
*p_os
;
482 diva_um_idi_stop_wdog(entity
);
485 DBG_FTL(("Zero entity on remove"))
490 (diva_um_idi_os_context_t
*)
491 diva_um_id_get_os_context(entity
))) {
492 DBG_FTL(("Zero entity os context on remove"))
496 if (!divas_um_idi_entity_assigned(entity
) || p_os
->aborted
) {
498 Entity is not assigned, also can be removed
503 DBG_TRC(("E(%08x) check remove", entity
))
506 If adapter not answers on remove request inside of
507 10 Sec, then adapter is dead
509 diva_um_idi_start_wdog(entity
);
512 DECLARE_WAITQUEUE(wait
, curtask
);
514 add_wait_queue(&p_os
->close_wait
, &wait
);
516 set_current_state(TASK_INTERRUPTIBLE
);
517 if (!divas_um_idi_entity_start_remove(entity
)
523 set_current_state(TASK_RUNNING
);
524 remove_wait_queue(&p_os
->close_wait
, &wait
);
527 DBG_TRC(("E(%08x) start remove", entity
))
529 DECLARE_WAITQUEUE(wait
, curtask
);
531 add_wait_queue(&p_os
->close_wait
, &wait
);
533 set_current_state(TASK_INTERRUPTIBLE
);
534 if (!divas_um_idi_entity_assigned(entity
)
540 set_current_state(TASK_RUNNING
);
541 remove_wait_queue(&p_os
->close_wait
, &wait
);
544 DBG_TRC(("E(%08x) remove complete, aborted:%d", entity
,
547 diva_um_idi_stop_wdog(entity
);
557 void diva_um_idi_start_wdog(void *entity
)
559 diva_um_idi_os_context_t
*p_os
;
563 (diva_um_idi_os_context_t
*)
564 diva_um_id_get_os_context(entity
)))) {
565 mod_timer(&p_os
->diva_timer_id
, jiffies
+ 10 * HZ
);
569 void diva_um_idi_stop_wdog(void *entity
)
571 diva_um_idi_os_context_t
*p_os
;
575 (diva_um_idi_os_context_t
*)
576 diva_um_id_get_os_context(entity
)))) {
577 del_timer(&p_os
->diva_timer_id
);