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",
160 devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
168 static int DIVA_INIT_FUNCTION divasi_init(void)
173 printk(KERN_INFO "%s\n", DRIVERNAME);
174 printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
175 strcpy(tmprev, main_revision);
176 printk("%s Build: %s\n", getrev(tmprev), DIVA_BUILD);
178 if (!divas_idi_register_chrdev()) {
183 if (!create_um_idi_proc()) {
184 divas_idi_unregister_chrdev();
185 printk(KERN_ERR "%s: failed to create proc entry.\n",
191 if (!(idifunc_init())) {
192 remove_um_idi_proc();
193 divas_idi_unregister_chrdev();
194 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
199 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
209 static void DIVA_EXIT_FUNCTION divasi_exit(void)
212 remove_um_idi_proc();
213 divas_idi_unregister_chrdev();
215 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
218 module_init(divasi_init);
219 module_exit(divasi_exit);
227 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
230 memcpy(dst, src, length);
235 um_idi_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
237 diva_um_idi_os_context_t *p_os;
241 if (!file->private_data) {
247 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
256 if (!(data = diva_os_malloc(0, count))) {
260 ret = diva_um_idi_read(file->private_data,
262 divas_um_idi_copy_to_user);
264 case 0: /* no message available */
267 case (-1): /* adapter was removed */
270 case (-2): /* message_length > length of user buffer */
276 if (copy_to_user(buf, data, ret)) {
281 diva_os_free(0, data);
282 DBG_TRC(("read: ret %d", ret));
288 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
291 memcpy(dst, src, length);
295 static int um_idi_open_adapter(struct file *file, int adapter_nr)
297 diva_um_idi_os_context_t *p_os;
299 divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
301 if (!(file->private_data = e)) {
304 p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
305 init_waitqueue_head(&p_os->read_wait);
306 init_waitqueue_head(&p_os->close_wait);
307 init_timer(&p_os->diva_timer_id);
308 p_os->diva_timer_id.function = (void *) diva_um_timer_function;
309 p_os->diva_timer_id.data = (unsigned long) p_os;
311 p_os->adapter_nr = adapter_nr;
316 um_idi_write(struct file *file, const char __user *buf, size_t count,
319 diva_um_idi_os_context_t *p_os;
324 if (!file->private_data) {
325 /* the first write() selects the adapter_nr */
326 if (count == sizeof(int)) {
328 ((void *) &adapter_nr, buf,
329 count)) return (-EFAULT);
330 if (!(um_idi_open_adapter(file, adapter_nr)))
338 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
347 if (!(data = diva_os_malloc(0, count))) {
351 if (copy_from_user(data, buf, count)) {
354 ret = diva_um_idi_write(file->private_data,
356 divas_um_idi_copy_from_user);
358 case 0: /* no space available */
361 case (-1): /* adapter was removed */
364 case (-2): /* length of user buffer > max message_length */
369 diva_os_free(0, data);
370 DBG_TRC(("write: ret %d", ret));
374 static unsigned int um_idi_poll(struct file *file, poll_table * wait)
376 diva_um_idi_os_context_t *p_os;
378 if (!file->private_data) {
383 (diva_um_idi_os_context_t *)
384 diva_um_id_get_os_context(file->private_data)))
389 poll_wait(file, &p_os->read_wait, wait);
395 switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
403 return (POLLIN | POLLRDNORM);
406 static int um_idi_open(struct inode *inode, struct file *file)
412 static int um_idi_release(struct inode *inode, struct file *file)
414 diva_um_idi_os_context_t *p_os;
415 unsigned int adapter_nr;
418 if (!(file->private_data)) {
424 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
429 adapter_nr = p_os->adapter_nr;
431 if ((ret = remove_entity(file->private_data))) {
435 if (divas_um_idi_delete_entity
436 ((int) adapter_nr, file->private_data)) {
445 int diva_os_get_context_size(void)
447 return (sizeof(diva_um_idi_os_context_t));
450 void diva_os_wakeup_read(void *os_context)
452 diva_um_idi_os_context_t *p_os =
453 (diva_um_idi_os_context_t *) os_context;
454 wake_up_interruptible(&p_os->read_wait);
457 void diva_os_wakeup_close(void *os_context)
459 diva_um_idi_os_context_t *p_os =
460 (diva_um_idi_os_context_t *) os_context;
461 wake_up_interruptible(&p_os->close_wait);
465 void diva_um_timer_function(unsigned long data)
467 diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
470 wake_up_interruptible(&p_os->read_wait);
471 wake_up_interruptible(&p_os->close_wait);
472 DBG_ERR(("entity removal watchdog"))
476 ** If application exits without entity removal this function will remove
477 ** entity and block until removal is complete
479 static int remove_entity(void *entity)
481 struct task_struct *curtask = current;
482 diva_um_idi_os_context_t *p_os;
484 diva_um_idi_stop_wdog(entity);
487 DBG_FTL(("Zero entity on remove"))
492 (diva_um_idi_os_context_t *)
493 diva_um_id_get_os_context(entity))) {
494 DBG_FTL(("Zero entity os context on remove"))
498 if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
500 Entity is not assigned, also can be removed
505 DBG_TRC(("E(%08x) check remove", entity))
508 If adapter not answers on remove request inside of
509 10 Sec, then adapter is dead
511 diva_um_idi_start_wdog(entity);
514 DECLARE_WAITQUEUE(wait, curtask);
516 add_wait_queue(&p_os->close_wait, &wait);
518 set_current_state(TASK_INTERRUPTIBLE);
519 if (!divas_um_idi_entity_start_remove(entity)
525 set_current_state(TASK_RUNNING);
526 remove_wait_queue(&p_os->close_wait, &wait);
529 DBG_TRC(("E(%08x) start remove", entity))
531 DECLARE_WAITQUEUE(wait, curtask);
533 add_wait_queue(&p_os->close_wait, &wait);
535 set_current_state(TASK_INTERRUPTIBLE);
536 if (!divas_um_idi_entity_assigned(entity)
542 set_current_state(TASK_RUNNING);
543 remove_wait_queue(&p_os->close_wait, &wait);
546 DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
549 diva_um_idi_stop_wdog(entity);
559 void diva_um_idi_start_wdog(void *entity)
561 diva_um_idi_os_context_t *p_os;
565 (diva_um_idi_os_context_t *)
566 diva_um_id_get_os_context(entity)))) {
567 mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
571 void diva_um_idi_stop_wdog(void *entity)
573 diva_um_idi_os_context_t *p_os;
577 (diva_um_idi_os_context_t *)
578 diva_um_id_get_os_context(entity)))) {
579 del_timer(&p_os->diva_timer_id);