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/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 #include <asm/uaccess.h>
31 static char *main_revision = "$Revision: 1.25.6.2 $";
35 MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
36 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
37 MODULE_SUPPORTED_DEVICE("DIVA card driver");
38 MODULE_LICENSE("GPL");
40 typedef struct _diva_um_idi_os_context {
41 wait_queue_head_t read_wait;
42 wait_queue_head_t close_wait;
43 struct timer_list diva_timer_id;
46 } diva_um_idi_os_context_t;
48 static char *DRIVERNAME = "Eicon DIVA - User IDI (http://www.melware.net)";
49 static char *DRIVERLNAME = "diva_idi";
50 static char *DEVNAME = "DivasIDI";
51 char *DRIVERRELEASE_IDI = "2.0";
53 extern int idifunc_init(void);
54 extern void idifunc_finit(void);
59 static char *getrev(const char *revision)
63 if ((p = strchr(revision, ':'))) {
75 static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
77 static ssize_t um_idi_write(struct file *file, const char __user *buf,
78 size_t count, loff_t * offset);
79 static unsigned int um_idi_poll(struct file *file, poll_table * wait);
80 static int um_idi_open(struct inode *inode, struct file *file);
81 static int um_idi_release(struct inode *inode, struct file *file);
82 static int remove_entity(void *entity);
83 static void diva_um_timer_function(unsigned long data);
88 extern struct proc_dir_entry *proc_net_eicon;
89 static struct proc_dir_entry *um_idi_proc_entry = NULL;
92 um_idi_proc_read(char *page, char **start, off_t off, int count, int *eof,
98 len += sprintf(page + len, "%s\n", DRIVERNAME);
99 len += sprintf(page + len, "name : %s\n", DRIVERLNAME);
100 len += sprintf(page + len, "release : %s\n", DRIVERRELEASE_IDI);
101 strcpy(tmprev, main_revision);
102 len += sprintf(page + len, "revision : %s\n", getrev(tmprev));
103 len += sprintf(page + len, "build : %s\n", DIVA_BUILD);
104 len += sprintf(page + len, "major : %d\n", major);
106 if (off + count >= len)
111 return ((count < len - off) ? count : len - off);
114 static int DIVA_INIT_FUNCTION create_um_idi_proc(void)
116 um_idi_proc_entry = create_proc_entry(DRIVERLNAME,
117 S_IFREG | S_IRUGO | S_IWUSR,
119 if (!um_idi_proc_entry)
122 um_idi_proc_entry->read_proc = um_idi_proc_read;
123 um_idi_proc_entry->owner = THIS_MODULE;
128 static void remove_um_idi_proc(void)
130 if (um_idi_proc_entry) {
131 remove_proc_entry(DRIVERLNAME, proc_net_eicon);
132 um_idi_proc_entry = NULL;
136 static struct file_operations divas_idi_fops = {
137 .owner = THIS_MODULE,
140 .write = um_idi_write,
143 .release = um_idi_release
146 static void divas_idi_unregister_chrdev(void)
148 devfs_remove(DEVNAME);
149 unregister_chrdev(major, DEVNAME);
152 static int DIVA_INIT_FUNCTION divas_idi_register_chrdev(void)
154 if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
156 printk(KERN_ERR "%s: failed to create /dev entry.\n",
167 static int DIVA_INIT_FUNCTION divasi_init(void)
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);
177 if (!divas_idi_register_chrdev()) {
182 if (!create_um_idi_proc()) {
183 divas_idi_unregister_chrdev();
184 printk(KERN_ERR "%s: failed to create proc entry.\n",
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",
198 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
208 static void DIVA_EXIT_FUNCTION divasi_exit(void)
211 remove_um_idi_proc();
212 divas_idi_unregister_chrdev();
214 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
217 module_init(divasi_init);
218 module_exit(divasi_exit);
226 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
229 memcpy(dst, src, length);
234 um_idi_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
236 diva_um_idi_os_context_t *p_os;
240 if (!file->private_data) {
246 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
255 if (!(data = diva_os_malloc(0, count))) {
259 ret = diva_um_idi_read(file->private_data,
261 divas_um_idi_copy_to_user);
263 case 0: /* no message available */
266 case (-1): /* adapter was removed */
269 case (-2): /* message_length > length of user buffer */
275 if (copy_to_user(buf, data, ret)) {
280 diva_os_free(0, data);
281 DBG_TRC(("read: ret %d", ret));
287 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
290 memcpy(dst, src, length);
294 static int um_idi_open_adapter(struct file *file, int adapter_nr)
296 diva_um_idi_os_context_t *p_os;
298 divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
300 if (!(file->private_data = e)) {
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;
310 p_os->adapter_nr = adapter_nr;
315 um_idi_write(struct file *file, const char __user *buf, size_t count,
318 diva_um_idi_os_context_t *p_os;
323 if (!file->private_data) {
324 /* the first write() selects the adapter_nr */
325 if (count == sizeof(int)) {
327 ((void *) &adapter_nr, buf,
328 count)) return (-EFAULT);
329 if (!(um_idi_open_adapter(file, adapter_nr)))
337 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
346 if (!(data = diva_os_malloc(0, count))) {
350 if (copy_from_user(data, buf, count)) {
353 ret = diva_um_idi_write(file->private_data,
355 divas_um_idi_copy_from_user);
357 case 0: /* no space available */
360 case (-1): /* adapter was removed */
363 case (-2): /* length of user buffer > max message_length */
368 diva_os_free(0, data);
369 DBG_TRC(("write: ret %d", ret));
373 static unsigned int um_idi_poll(struct file *file, poll_table * wait)
375 diva_um_idi_os_context_t *p_os;
377 if (!file->private_data) {
382 (diva_um_idi_os_context_t *)
383 diva_um_id_get_os_context(file->private_data)))
388 poll_wait(file, &p_os->read_wait, wait);
394 switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
402 return (POLLIN | POLLRDNORM);
405 static int um_idi_open(struct inode *inode, struct file *file)
411 static int um_idi_release(struct inode *inode, struct file *file)
413 diva_um_idi_os_context_t *p_os;
414 unsigned int adapter_nr;
417 if (!(file->private_data)) {
423 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
428 adapter_nr = p_os->adapter_nr;
430 if ((ret = remove_entity(file->private_data))) {
434 if (divas_um_idi_delete_entity
435 ((int) adapter_nr, file->private_data)) {
444 int diva_os_get_context_size(void)
446 return (sizeof(diva_um_idi_os_context_t));
449 void diva_os_wakeup_read(void *os_context)
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);
456 void diva_os_wakeup_close(void *os_context)
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);
464 void diva_um_timer_function(unsigned long data)
466 diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
469 wake_up_interruptible(&p_os->read_wait);
470 wake_up_interruptible(&p_os->close_wait);
471 DBG_ERR(("entity removal watchdog"))
475 ** If application exits without entity removal this function will remove
476 ** entity and block until removal is complete
478 static int remove_entity(void *entity)
480 struct task_struct *curtask = current;
481 diva_um_idi_os_context_t *p_os;
483 diva_um_idi_stop_wdog(entity);
486 DBG_FTL(("Zero entity on remove"))
491 (diva_um_idi_os_context_t *)
492 diva_um_id_get_os_context(entity))) {
493 DBG_FTL(("Zero entity os context on remove"))
497 if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
499 Entity is not assigned, also can be removed
504 DBG_TRC(("E(%08x) check remove", entity))
507 If adapter not answers on remove request inside of
508 10 Sec, then adapter is dead
510 diva_um_idi_start_wdog(entity);
513 DECLARE_WAITQUEUE(wait, curtask);
515 add_wait_queue(&p_os->close_wait, &wait);
517 set_current_state(TASK_INTERRUPTIBLE);
518 if (!divas_um_idi_entity_start_remove(entity)
524 set_current_state(TASK_RUNNING);
525 remove_wait_queue(&p_os->close_wait, &wait);
528 DBG_TRC(("E(%08x) start remove", entity))
530 DECLARE_WAITQUEUE(wait, curtask);
532 add_wait_queue(&p_os->close_wait, &wait);
534 set_current_state(TASK_INTERRUPTIBLE);
535 if (!divas_um_idi_entity_assigned(entity)
541 set_current_state(TASK_RUNNING);
542 remove_wait_queue(&p_os->close_wait, &wait);
545 DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
548 diva_um_idi_stop_wdog(entity);
558 void diva_um_idi_start_wdog(void *entity)
560 diva_um_idi_os_context_t *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);
570 void diva_um_idi_stop_wdog(void *entity)
572 diva_um_idi_os_context_t *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);