Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / isdn / hardware / eicon / divasi.c
blobdf715b47e2b43f64239821353ab4f9b22c3de4fb
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>
25 #include "platform.h"
26 #include "di_defs.h"
27 #include "divasync.h"
28 #include "um_xdi.h"
29 #include "um_idi.h"
31 static char *main_revision = "$Revision: 1.25.6.2 $";
33 static int major;
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;
44 int aborted;
45 int adapter_nr;
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);
57 * helper functions
59 static char *getrev(const char *revision)
61 char *rev;
62 char *p;
63 if ((p = strchr(revision, ':'))) {
64 rev = p + 2;
65 p = strchr(rev, '$');
66 *--p = 0;
67 } else
68 rev = "1.0";
69 return rev;
73 * LOCALS
75 static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
76 loff_t * offset);
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);
86 * proc entry
88 extern struct proc_dir_entry *proc_net_eicon;
89 static struct proc_dir_entry *um_idi_proc_entry = NULL;
91 static int
92 um_idi_proc_read(char *page, char **start, off_t off, int count, int *eof,
93 void *data)
95 int len = 0;
96 char tmprev[32];
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)
107 *eof = 1;
108 if (len < off)
109 return 0;
110 *start = page + off;
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,
118 proc_net_eicon);
119 if (!um_idi_proc_entry)
120 return (0);
122 um_idi_proc_entry->read_proc = um_idi_proc_read;
123 um_idi_proc_entry->owner = THIS_MODULE;
125 return (1);
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,
138 .llseek = no_llseek,
139 .read = um_idi_read,
140 .write = um_idi_write,
141 .poll = um_idi_poll,
142 .open = um_idi_open,
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",
157 DRIVERLNAME);
158 return (0);
160 devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
162 return (1);
166 ** Driver Load
168 static int DIVA_INIT_FUNCTION divasi_init(void)
170 char tmprev[50];
171 int ret = 0;
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()) {
179 ret = -EIO;
180 goto out;
183 if (!create_um_idi_proc()) {
184 divas_idi_unregister_chrdev();
185 printk(KERN_ERR "%s: failed to create proc entry.\n",
186 DRIVERLNAME);
187 ret = -EIO;
188 goto out;
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",
195 DRIVERLNAME);
196 ret = -EIO;
197 goto out;
199 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
201 out:
202 return (ret);
207 ** Driver Unload
209 static void DIVA_EXIT_FUNCTION divasi_exit(void)
211 idifunc_finit();
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);
223 * FILE OPERATIONS
226 static int
227 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
228 int length)
230 memcpy(dst, src, length);
231 return (length);
234 static ssize_t
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;
238 int ret = -EINVAL;
239 void *data;
241 if (!file->private_data) {
242 return (-ENODEV);
245 if (!
246 (p_os =
247 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
248 private_data)))
250 return (-ENODEV);
252 if (p_os->aborted) {
253 return (-ENODEV);
256 if (!(data = diva_os_malloc(0, count))) {
257 return (-ENOMEM);
260 ret = diva_um_idi_read(file->private_data,
261 file, data, count,
262 divas_um_idi_copy_to_user);
263 switch (ret) {
264 case 0: /* no message available */
265 ret = (-EAGAIN);
266 break;
267 case (-1): /* adapter was removed */
268 ret = (-ENODEV);
269 break;
270 case (-2): /* message_length > length of user buffer */
271 ret = (-EFAULT);
272 break;
275 if (ret > 0) {
276 if (copy_to_user(buf, data, ret)) {
277 ret = (-EFAULT);
281 diva_os_free(0, data);
282 DBG_TRC(("read: ret %d", ret));
283 return (ret);
287 static int
288 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
289 int length)
291 memcpy(dst, src, length);
292 return (length);
295 static int um_idi_open_adapter(struct file *file, int adapter_nr)
297 diva_um_idi_os_context_t *p_os;
298 void *e =
299 divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
301 if (!(file->private_data = e)) {
302 return (0);
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;
310 p_os->aborted = 0;
311 p_os->adapter_nr = adapter_nr;
312 return (1);
315 static ssize_t
316 um_idi_write(struct file *file, const char __user *buf, size_t count,
317 loff_t * offset)
319 diva_um_idi_os_context_t *p_os;
320 int ret = -EINVAL;
321 void *data;
322 int adapter_nr = 0;
324 if (!file->private_data) {
325 /* the first write() selects the adapter_nr */
326 if (count == sizeof(int)) {
327 if (copy_from_user
328 ((void *) &adapter_nr, buf,
329 count)) return (-EFAULT);
330 if (!(um_idi_open_adapter(file, adapter_nr)))
331 return (-ENODEV);
332 return (count);
333 } else
334 return (-ENODEV);
337 if (!(p_os =
338 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
339 private_data)))
341 return (-ENODEV);
343 if (p_os->aborted) {
344 return (-ENODEV);
347 if (!(data = diva_os_malloc(0, count))) {
348 return (-ENOMEM);
351 if (copy_from_user(data, buf, count)) {
352 ret = -EFAULT;
353 } else {
354 ret = diva_um_idi_write(file->private_data,
355 file, data, count,
356 divas_um_idi_copy_from_user);
357 switch (ret) {
358 case 0: /* no space available */
359 ret = (-EAGAIN);
360 break;
361 case (-1): /* adapter was removed */
362 ret = (-ENODEV);
363 break;
364 case (-2): /* length of user buffer > max message_length */
365 ret = (-EFAULT);
366 break;
369 diva_os_free(0, data);
370 DBG_TRC(("write: ret %d", ret));
371 return (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) {
379 return (POLLERR);
382 if ((!(p_os =
383 (diva_um_idi_os_context_t *)
384 diva_um_id_get_os_context(file->private_data)))
385 || p_os->aborted) {
386 return (POLLERR);
389 poll_wait(file, &p_os->read_wait, wait);
391 if (p_os->aborted) {
392 return (POLLERR);
395 switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
396 case (-1):
397 return (POLLERR);
399 case 0:
400 return (0);
403 return (POLLIN | POLLRDNORM);
406 static int um_idi_open(struct inode *inode, struct file *file)
408 return (0);
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;
416 int ret = 0;
418 if (!(file->private_data)) {
419 ret = -ENODEV;
420 goto out;
423 if (!(p_os =
424 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
425 ret = -ENODEV;
426 goto out;
429 adapter_nr = p_os->adapter_nr;
431 if ((ret = remove_entity(file->private_data))) {
432 goto out;
435 if (divas_um_idi_delete_entity
436 ((int) adapter_nr, file->private_data)) {
437 ret = -ENODEV;
438 goto out;
441 out:
442 return (ret);
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);
464 static
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;
469 p_os->aborted = 1;
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);
486 if (!entity) {
487 DBG_FTL(("Zero entity on remove"))
488 return (0);
491 if (!(p_os =
492 (diva_um_idi_os_context_t *)
493 diva_um_id_get_os_context(entity))) {
494 DBG_FTL(("Zero entity os context on remove"))
495 return (0);
498 if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
500 Entity is not assigned, also can be removed
502 return (0);
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);
517 for (;;) {
518 set_current_state(TASK_INTERRUPTIBLE);
519 if (!divas_um_idi_entity_start_remove(entity)
520 || p_os->aborted) {
521 break;
523 schedule();
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);
534 for (;;) {
535 set_current_state(TASK_INTERRUPTIBLE);
536 if (!divas_um_idi_entity_assigned(entity)
537 || p_os->aborted) {
538 break;
540 schedule();
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,
547 p_os->aborted))
549 diva_um_idi_stop_wdog(entity);
551 p_os->aborted = 0;
553 return (0);
557 * timer watchdog
559 void diva_um_idi_start_wdog(void *entity)
561 diva_um_idi_os_context_t *p_os;
563 if (entity &&
564 ((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;
575 if (entity &&
576 ((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);