added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / isdn / hardware / eicon / divasi.c
blobf4969fe0a0554ad5f567268075be2a6e90b34bb1
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/smp_lock.h>
21 #include <asm/uaccess.h>
23 #include "platform.h"
24 #include "di_defs.h"
25 #include "divasync.h"
26 #include "um_xdi.h"
27 #include "um_idi.h"
29 static char *main_revision = "$Revision: 1.25.6.2 $";
31 static int major;
33 MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
34 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
35 MODULE_SUPPORTED_DEVICE("DIVA card driver");
36 MODULE_LICENSE("GPL");
38 typedef struct _diva_um_idi_os_context {
39 wait_queue_head_t read_wait;
40 wait_queue_head_t close_wait;
41 struct timer_list diva_timer_id;
42 int aborted;
43 int adapter_nr;
44 } diva_um_idi_os_context_t;
46 static char *DRIVERNAME = "Eicon DIVA - User IDI (http://www.melware.net)";
47 static char *DRIVERLNAME = "diva_idi";
48 static char *DEVNAME = "DivasIDI";
49 char *DRIVERRELEASE_IDI = "2.0";
51 extern int idifunc_init(void);
52 extern void idifunc_finit(void);
55 * helper functions
57 static char *getrev(const char *revision)
59 char *rev;
60 char *p;
61 if ((p = strchr(revision, ':'))) {
62 rev = p + 2;
63 p = strchr(rev, '$');
64 *--p = 0;
65 } else
66 rev = "1.0";
67 return rev;
71 * LOCALS
73 static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
74 loff_t * offset);
75 static ssize_t um_idi_write(struct file *file, const char __user *buf,
76 size_t count, loff_t * offset);
77 static unsigned int um_idi_poll(struct file *file, poll_table * wait);
78 static int um_idi_open(struct inode *inode, struct file *file);
79 static int um_idi_release(struct inode *inode, struct file *file);
80 static int remove_entity(void *entity);
81 static void diva_um_timer_function(unsigned long data);
84 * proc entry
86 extern struct proc_dir_entry *proc_net_eicon;
87 static struct proc_dir_entry *um_idi_proc_entry = NULL;
89 static int
90 um_idi_proc_read(char *page, char **start, off_t off, int count, int *eof,
91 void *data)
93 int len = 0;
94 char tmprev[32];
96 len += sprintf(page + len, "%s\n", DRIVERNAME);
97 len += sprintf(page + len, "name : %s\n", DRIVERLNAME);
98 len += sprintf(page + len, "release : %s\n", DRIVERRELEASE_IDI);
99 strcpy(tmprev, main_revision);
100 len += sprintf(page + len, "revision : %s\n", getrev(tmprev));
101 len += sprintf(page + len, "build : %s\n", DIVA_BUILD);
102 len += sprintf(page + len, "major : %d\n", major);
104 if (off + count >= len)
105 *eof = 1;
106 if (len < off)
107 return 0;
108 *start = page + off;
109 return ((count < len - off) ? count : len - off);
112 static int DIVA_INIT_FUNCTION create_um_idi_proc(void)
114 um_idi_proc_entry = create_proc_entry(DRIVERLNAME,
115 S_IFREG | S_IRUGO | S_IWUSR,
116 proc_net_eicon);
117 if (!um_idi_proc_entry)
118 return (0);
120 um_idi_proc_entry->read_proc = um_idi_proc_read;
121 um_idi_proc_entry->owner = THIS_MODULE;
123 return (1);
126 static void remove_um_idi_proc(void)
128 if (um_idi_proc_entry) {
129 remove_proc_entry(DRIVERLNAME, proc_net_eicon);
130 um_idi_proc_entry = NULL;
134 static const struct file_operations divas_idi_fops = {
135 .owner = THIS_MODULE,
136 .llseek = no_llseek,
137 .read = um_idi_read,
138 .write = um_idi_write,
139 .poll = um_idi_poll,
140 .open = um_idi_open,
141 .release = um_idi_release
144 static void divas_idi_unregister_chrdev(void)
146 unregister_chrdev(major, DEVNAME);
149 static int DIVA_INIT_FUNCTION divas_idi_register_chrdev(void)
151 if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
153 printk(KERN_ERR "%s: failed to create /dev entry.\n",
154 DRIVERLNAME);
155 return (0);
158 return (1);
162 ** Driver Load
164 static int DIVA_INIT_FUNCTION divasi_init(void)
166 char tmprev[50];
167 int ret = 0;
169 printk(KERN_INFO "%s\n", DRIVERNAME);
170 printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
171 strcpy(tmprev, main_revision);
172 printk("%s Build: %s\n", getrev(tmprev), DIVA_BUILD);
174 if (!divas_idi_register_chrdev()) {
175 ret = -EIO;
176 goto out;
179 if (!create_um_idi_proc()) {
180 divas_idi_unregister_chrdev();
181 printk(KERN_ERR "%s: failed to create proc entry.\n",
182 DRIVERLNAME);
183 ret = -EIO;
184 goto out;
187 if (!(idifunc_init())) {
188 remove_um_idi_proc();
189 divas_idi_unregister_chrdev();
190 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
191 DRIVERLNAME);
192 ret = -EIO;
193 goto out;
195 printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
197 out:
198 return (ret);
203 ** Driver Unload
205 static void DIVA_EXIT_FUNCTION divasi_exit(void)
207 idifunc_finit();
208 remove_um_idi_proc();
209 divas_idi_unregister_chrdev();
211 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
214 module_init(divasi_init);
215 module_exit(divasi_exit);
219 * FILE OPERATIONS
222 static int
223 divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
224 int length)
226 memcpy(dst, src, length);
227 return (length);
230 static ssize_t
231 um_idi_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
233 diva_um_idi_os_context_t *p_os;
234 int ret = -EINVAL;
235 void *data;
237 if (!file->private_data) {
238 return (-ENODEV);
241 if (!
242 (p_os =
243 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
244 private_data)))
246 return (-ENODEV);
248 if (p_os->aborted) {
249 return (-ENODEV);
252 if (!(data = diva_os_malloc(0, count))) {
253 return (-ENOMEM);
256 ret = diva_um_idi_read(file->private_data,
257 file, data, count,
258 divas_um_idi_copy_to_user);
259 switch (ret) {
260 case 0: /* no message available */
261 ret = (-EAGAIN);
262 break;
263 case (-1): /* adapter was removed */
264 ret = (-ENODEV);
265 break;
266 case (-2): /* message_length > length of user buffer */
267 ret = (-EFAULT);
268 break;
271 if (ret > 0) {
272 if (copy_to_user(buf, data, ret)) {
273 ret = (-EFAULT);
277 diva_os_free(0, data);
278 DBG_TRC(("read: ret %d", ret));
279 return (ret);
283 static int
284 divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
285 int length)
287 memcpy(dst, src, length);
288 return (length);
291 static int um_idi_open_adapter(struct file *file, int adapter_nr)
293 diva_um_idi_os_context_t *p_os;
294 void *e =
295 divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
297 if (!(file->private_data = e)) {
298 return (0);
300 p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
301 init_waitqueue_head(&p_os->read_wait);
302 init_waitqueue_head(&p_os->close_wait);
303 init_timer(&p_os->diva_timer_id);
304 p_os->diva_timer_id.function = (void *) diva_um_timer_function;
305 p_os->diva_timer_id.data = (unsigned long) p_os;
306 p_os->aborted = 0;
307 p_os->adapter_nr = adapter_nr;
308 return (1);
311 static ssize_t
312 um_idi_write(struct file *file, const char __user *buf, size_t count,
313 loff_t * offset)
315 diva_um_idi_os_context_t *p_os;
316 int ret = -EINVAL;
317 void *data;
318 int adapter_nr = 0;
320 if (!file->private_data) {
321 /* the first write() selects the adapter_nr */
322 if (count == sizeof(int)) {
323 if (copy_from_user
324 ((void *) &adapter_nr, buf,
325 count)) return (-EFAULT);
326 if (!(um_idi_open_adapter(file, adapter_nr)))
327 return (-ENODEV);
328 return (count);
329 } else
330 return (-ENODEV);
333 if (!(p_os =
334 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
335 private_data)))
337 return (-ENODEV);
339 if (p_os->aborted) {
340 return (-ENODEV);
343 if (!(data = diva_os_malloc(0, count))) {
344 return (-ENOMEM);
347 if (copy_from_user(data, buf, count)) {
348 ret = -EFAULT;
349 } else {
350 ret = diva_um_idi_write(file->private_data,
351 file, data, count,
352 divas_um_idi_copy_from_user);
353 switch (ret) {
354 case 0: /* no space available */
355 ret = (-EAGAIN);
356 break;
357 case (-1): /* adapter was removed */
358 ret = (-ENODEV);
359 break;
360 case (-2): /* length of user buffer > max message_length */
361 ret = (-EFAULT);
362 break;
365 diva_os_free(0, data);
366 DBG_TRC(("write: ret %d", ret));
367 return (ret);
370 static unsigned int um_idi_poll(struct file *file, poll_table * wait)
372 diva_um_idi_os_context_t *p_os;
374 if (!file->private_data) {
375 return (POLLERR);
378 if ((!(p_os =
379 (diva_um_idi_os_context_t *)
380 diva_um_id_get_os_context(file->private_data)))
381 || p_os->aborted) {
382 return (POLLERR);
385 poll_wait(file, &p_os->read_wait, wait);
387 if (p_os->aborted) {
388 return (POLLERR);
391 switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
392 case (-1):
393 return (POLLERR);
395 case 0:
396 return (0);
399 return (POLLIN | POLLRDNORM);
402 static int um_idi_open(struct inode *inode, struct file *file)
404 cycle_kernel_lock();
405 return (0);
409 static int um_idi_release(struct inode *inode, struct file *file)
411 diva_um_idi_os_context_t *p_os;
412 unsigned int adapter_nr;
413 int ret = 0;
415 if (!(file->private_data)) {
416 ret = -ENODEV;
417 goto out;
420 if (!(p_os =
421 (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
422 ret = -ENODEV;
423 goto out;
426 adapter_nr = p_os->adapter_nr;
428 if ((ret = remove_entity(file->private_data))) {
429 goto out;
432 if (divas_um_idi_delete_entity
433 ((int) adapter_nr, file->private_data)) {
434 ret = -ENODEV;
435 goto out;
438 out:
439 return (ret);
442 int diva_os_get_context_size(void)
444 return (sizeof(diva_um_idi_os_context_t));
447 void diva_os_wakeup_read(void *os_context)
449 diva_um_idi_os_context_t *p_os =
450 (diva_um_idi_os_context_t *) os_context;
451 wake_up_interruptible(&p_os->read_wait);
454 void diva_os_wakeup_close(void *os_context)
456 diva_um_idi_os_context_t *p_os =
457 (diva_um_idi_os_context_t *) os_context;
458 wake_up_interruptible(&p_os->close_wait);
461 static
462 void diva_um_timer_function(unsigned long data)
464 diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
466 p_os->aborted = 1;
467 wake_up_interruptible(&p_os->read_wait);
468 wake_up_interruptible(&p_os->close_wait);
469 DBG_ERR(("entity removal watchdog"))
473 ** If application exits without entity removal this function will remove
474 ** entity and block until removal is complete
476 static int remove_entity(void *entity)
478 struct task_struct *curtask = current;
479 diva_um_idi_os_context_t *p_os;
481 diva_um_idi_stop_wdog(entity);
483 if (!entity) {
484 DBG_FTL(("Zero entity on remove"))
485 return (0);
488 if (!(p_os =
489 (diva_um_idi_os_context_t *)
490 diva_um_id_get_os_context(entity))) {
491 DBG_FTL(("Zero entity os context on remove"))
492 return (0);
495 if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
497 Entity is not assigned, also can be removed
499 return (0);
502 DBG_TRC(("E(%08x) check remove", entity))
505 If adapter not answers on remove request inside of
506 10 Sec, then adapter is dead
508 diva_um_idi_start_wdog(entity);
511 DECLARE_WAITQUEUE(wait, curtask);
513 add_wait_queue(&p_os->close_wait, &wait);
514 for (;;) {
515 set_current_state(TASK_INTERRUPTIBLE);
516 if (!divas_um_idi_entity_start_remove(entity)
517 || p_os->aborted) {
518 break;
520 schedule();
522 set_current_state(TASK_RUNNING);
523 remove_wait_queue(&p_os->close_wait, &wait);
526 DBG_TRC(("E(%08x) start remove", entity))
528 DECLARE_WAITQUEUE(wait, curtask);
530 add_wait_queue(&p_os->close_wait, &wait);
531 for (;;) {
532 set_current_state(TASK_INTERRUPTIBLE);
533 if (!divas_um_idi_entity_assigned(entity)
534 || p_os->aborted) {
535 break;
537 schedule();
539 set_current_state(TASK_RUNNING);
540 remove_wait_queue(&p_os->close_wait, &wait);
543 DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
544 p_os->aborted))
546 diva_um_idi_stop_wdog(entity);
548 p_os->aborted = 0;
550 return (0);
554 * timer watchdog
556 void diva_um_idi_start_wdog(void *entity)
558 diva_um_idi_os_context_t *p_os;
560 if (entity &&
561 ((p_os =
562 (diva_um_idi_os_context_t *)
563 diva_um_id_get_os_context(entity)))) {
564 mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
568 void diva_um_idi_stop_wdog(void *entity)
570 diva_um_idi_os_context_t *p_os;
572 if (entity &&
573 ((p_os =
574 (diva_um_idi_os_context_t *)
575 diva_um_id_get_os_context(entity)))) {
576 del_timer(&p_os->diva_timer_id);