VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / isdn / hardware / eicon / divamnt.c
1 /* $Id: divamnt.c,v 1.32 2004/01/15 09:48:13 armin Exp $
2  *
3  * Driver for Eicon DIVA Server ISDN cards.
4  * Maint module
5  *
6  * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
7  * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  */
12
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
24 #include "platform.h"
25 #include "di_defs.h"
26 #include "divasync.h"
27 #include "debug_if.h"
28
29 static char *main_revision = "$Revision: 1.32 $";
30
31 static int major;
32
33 MODULE_DESCRIPTION("Maint driver for Eicon DIVA Server cards");
34 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
35 MODULE_SUPPORTED_DEVICE("DIVA card driver");
36 MODULE_LICENSE("GPL");
37
38 int buffer_length = 128;
39 MODULE_PARM(buffer_length, "i");
40 unsigned long diva_dbg_mem = 0;
41 MODULE_PARM(diva_dbg_mem, "l");
42
43 static char *DRIVERNAME =
44     "Eicon DIVA - MAINT module (http://www.melware.net)";
45 static char *DRIVERLNAME = "diva_mnt";
46 static char *DEVNAME = "DivasMAINT";
47 char *DRIVERRELEASE_MNT = "2.0";
48
49 static wait_queue_head_t msgwaitq;
50 static DECLARE_MUTEX(opened_sem);
51 static int opened;
52 static struct timeval start_time;
53
54 extern int mntfunc_init(int *, void **, unsigned long);
55 extern void mntfunc_finit(void);
56 extern int maint_read_write(void __user *buf, int count);
57
58 /*
59  *  helper functions
60  */
61 static char *getrev(const char *revision)
62 {
63         char *rev;
64         char *p;
65
66         if ((p = strchr(revision, ':'))) {
67                 rev = p + 2;
68                 p = strchr(rev, '$');
69                 *--p = 0;
70         } else
71                 rev = "1.0";
72
73         return rev;
74 }
75
76 /*
77  * buffer alloc
78  */
79 void *diva_os_malloc_tbuffer(unsigned long flags, unsigned long size)
80 {
81         return (kmalloc(size, GFP_KERNEL));
82 }
83 void diva_os_free_tbuffer(unsigned long flags, void *ptr)
84 {
85         if (ptr) {
86                 kfree(ptr);
87         }
88 }
89
90 /*
91  * kernel/user space copy functions
92  */
93 int diva_os_copy_to_user(void *os_handle, void __user *dst, const void *src,
94                          int length)
95 {
96         return (copy_to_user(dst, src, length));
97 }
98 int diva_os_copy_from_user(void *os_handle, void *dst, const void __user *src,
99                            int length)
100 {
101         return (copy_from_user(dst, src, length));
102 }
103
104 /*
105  * get time
106  */
107 void diva_os_get_time(dword * sec, dword * usec)
108 {
109         struct timeval tv;
110
111         do_gettimeofday(&tv);
112
113         if (tv.tv_sec > start_time.tv_sec) {
114                 if (start_time.tv_usec > tv.tv_usec) {
115                         tv.tv_sec--;
116                         tv.tv_usec += 1000000;
117                 }
118                 *sec = (dword) (tv.tv_sec - start_time.tv_sec);
119                 *usec = (dword) (tv.tv_usec - start_time.tv_usec);
120         } else if (tv.tv_sec == start_time.tv_sec) {
121                 *sec = 0;
122                 if (start_time.tv_usec < tv.tv_usec) {
123                         *usec = (dword) (tv.tv_usec - start_time.tv_usec);
124                 } else {
125                         *usec = 0;
126                 }
127         } else {
128                 *sec = (dword) tv.tv_sec;
129                 *usec = (dword) tv.tv_usec;
130         }
131 }
132
133 /*
134  * /proc entries
135  */
136
137 extern struct proc_dir_entry *proc_net_eicon;
138 static struct proc_dir_entry *maint_proc_entry = NULL;
139
140 /*
141         Read function is provided for compatibility reason - this allows
142   to read unstructured traces, formated as ascii string only
143   */
144 static ssize_t
145 maint_read(struct file *file, char __user *buf, size_t count, loff_t * off)
146 {
147         diva_dbg_entry_head_t *pmsg = NULL;
148         diva_os_spin_lock_magic_t old_irql;
149         word size;
150         char *pstr, *dli_label = "UNK";
151         int str_length;
152         int *str_msg;
153
154         if (!file->private_data) {
155                 for (;;) {
156                         while (
157                                (pmsg =
158                                 diva_maint_get_message(&size,
159                                                        &old_irql))) {
160                                 if (!(pmsg->facility == MSG_TYPE_STRING)) {
161                                         diva_maint_ack_message(1,
162                                                                &old_irql);
163                                 } else {
164                                         break;
165                                 }
166                         }
167
168                         if (!pmsg) {
169                                 if (file->f_flags & O_NONBLOCK) {
170                                         return (-EAGAIN);
171                                 }
172                                 interruptible_sleep_on(&msgwaitq);
173                                 if (signal_pending(current)) {
174                                         return (-ERESTARTSYS);
175                                 }
176                         } else {
177                                 break;
178                         }
179                 }
180                 /*
181                    The length of message that shoule be read is:
182                    pmsg->data_length + label(25) + DrvID(2) + byte CR + trailing zero
183                  */
184                 if (!
185                     (str_msg =
186                      (int *) diva_os_malloc_tbuffer(0,
187                                                     pmsg->data_length +
188                                                     29 + 2 * sizeof(int)))) {
189                         diva_maint_ack_message(0, &old_irql);
190                         return (-ENOMEM);
191                 }
192                 pstr = (char *) &str_msg[2];
193
194                 switch (pmsg->dli) {
195                 case DLI_LOG:
196                         dli_label = "LOG";
197                         break;
198                 case DLI_FTL:
199                         dli_label = "FTL";
200                         break;
201                 case DLI_ERR:
202                         dli_label = "ERR";
203                         break;
204                 case DLI_TRC:
205                         dli_label = "TRC";
206                         break;
207                 case DLI_REG:
208                         dli_label = "REG";
209                         break;
210                 case DLI_MEM:
211                         dli_label = "MEM";
212                         break;
213                 case DLI_SPL:
214                         dli_label = "SPL";
215                         break;
216                 case DLI_IRP:
217                         dli_label = "IRP";
218                         break;
219                 case DLI_TIM:
220                         dli_label = "TIM";
221                         break;
222                 case DLI_TAPI:
223                         dli_label = "TAPI";
224                         break;
225                 case DLI_NDIS:
226                         dli_label = "NDIS";
227                         break;
228                 case DLI_CONN:
229                         dli_label = "CONN";
230                         break;
231                 case DLI_STAT:
232                         dli_label = "STAT";
233                         break;
234                 case DLI_PRV0:
235                         dli_label = "PRV0";
236                         break;
237                 case DLI_PRV1:
238                         dli_label = "PRV1";
239                         break;
240                 case DLI_PRV2:
241                         dli_label = "PRV2";
242                         break;
243                 case DLI_PRV3:
244                         dli_label = "PRV3";
245                         break;
246                 }
247                 str_length = sprintf(pstr, "%s %02x %s\n",
248                                      dli_label, (byte) pmsg->drv_id,
249                                      (char *) &pmsg[1]);
250                 str_msg[0] = str_length;
251                 str_msg[1] = 0;
252                 file->private_data = str_msg;
253                 diva_maint_ack_message(1, &old_irql);
254         } else {
255                 str_msg = (int *) file->private_data;
256                 pstr = (char *) &str_msg[2];
257                 pstr += str_msg[1];     /* head + offset */
258                 str_length = str_msg[0] - str_msg[1];   /* length - offset */
259         }
260         str_length = MIN(str_length, count);
261
262         if (diva_os_copy_to_user(NULL, buf, pstr, str_length)) {
263                 diva_os_free_tbuffer(0, str_msg);
264                 file->private_data = NULL;
265                 return (-EFAULT);
266         }
267         str_msg[1] += str_length;
268         if ((str_msg[0] - str_msg[1]) <= 0) {
269                 diva_os_free_tbuffer(0, str_msg);
270                 file->private_data = NULL;
271         }
272
273         return (str_length);
274 }
275
276 static ssize_t
277 maint_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
278 {
279         return (-ENODEV);
280 }
281
282 static unsigned int maint_poll(struct file *file, poll_table * wait)
283 {
284         unsigned int mask = 0;
285
286         poll_wait(file, &msgwaitq, wait);
287         mask = POLLOUT | POLLWRNORM;
288         if (file->private_data || diva_dbg_q_length()) {
289                 mask |= POLLIN | POLLRDNORM;
290         }
291         return (mask);
292 }
293
294 static int maint_open(struct inode *ino, struct file *filep)
295 {
296         down(&opened_sem);
297         if (opened) {
298                 up(&opened_sem);
299                 return (-EBUSY);
300         }
301         opened++;
302         up(&opened_sem);
303
304         filep->private_data = NULL;
305
306         return nonseekable_open(ino, filep);
307 }
308
309 static int maint_close(struct inode *ino, struct file *filep)
310 {
311         if (filep->private_data) {
312                 diva_os_free_tbuffer(0, filep->private_data);
313                 filep->private_data = NULL;
314         }
315
316         down(&opened_sem);
317         opened--;
318         up(&opened_sem);
319         return (0);
320 }
321
322 /*
323  * fops
324  */
325 static struct file_operations maint_fops = {
326         .owner   = THIS_MODULE,
327         .llseek  = no_llseek,
328         .read    = maint_read,
329         .write   = maint_write,
330         .poll    = maint_poll,
331         .open    = maint_open,
332         .release = maint_close
333 };
334
335 static int DIVA_INIT_FUNCTION create_maint_proc(void)
336 {
337         maint_proc_entry =
338             create_proc_entry("maint", S_IFREG | S_IRUGO | S_IWUSR,
339                               proc_net_eicon);
340         if (!maint_proc_entry)
341                 return (0);
342
343         maint_proc_entry->proc_fops = &maint_fops;
344         maint_proc_entry->owner = THIS_MODULE;
345
346         return (1);
347 }
348
349 static void remove_maint_proc(void)
350 {
351         if (maint_proc_entry) {
352                 remove_proc_entry("maint", proc_net_eicon);
353                 maint_proc_entry = NULL;
354         }
355 }
356
357 /*
358  * device node operations
359  */
360 static ssize_t divas_maint_write(struct file *file, const char __user *buf,
361                                  size_t count, loff_t * ppos)
362 {
363         return (maint_read_write((char __user *) buf, (int) count));
364 }
365
366 static ssize_t divas_maint_read(struct file *file, char __user *buf,
367                                 size_t count, loff_t * ppos)
368 {
369         return (maint_read_write(buf, (int) count));
370 }
371
372 static struct file_operations divas_maint_fops = {
373         .owner   = THIS_MODULE,
374         .llseek  = no_llseek,
375         .read    = divas_maint_read,
376         .write   = divas_maint_write,
377         .poll    = maint_poll,
378         .open    = maint_open,
379         .release = maint_close
380 };
381
382 static void divas_maint_unregister_chrdev(void)
383 {
384         devfs_remove(DEVNAME);
385         unregister_chrdev(major, DEVNAME);
386 }
387
388 static int DIVA_INIT_FUNCTION divas_maint_register_chrdev(void)
389 {
390         if ((major = register_chrdev(0, DEVNAME, &divas_maint_fops)) < 0)
391         {
392                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
393                        DRIVERLNAME);
394                 return (0);
395         }
396         devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
397
398         return (1);
399 }
400
401 /*
402  * wake up reader
403  */
404 void diva_maint_wakeup_read(void)
405 {
406         wake_up_interruptible(&msgwaitq);
407 }
408
409 /*
410  *  Driver Load
411  */
412 static int DIVA_INIT_FUNCTION maint_init(void)
413 {
414         char tmprev[50];
415         int ret = 0;
416         void *buffer = NULL;
417
418         do_gettimeofday(&start_time);
419         init_waitqueue_head(&msgwaitq);
420
421         printk(KERN_INFO "%s\n", DRIVERNAME);
422         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_MNT);
423         strcpy(tmprev, main_revision);
424         printk("%s  Build: %s \n", getrev(tmprev), DIVA_BUILD);
425
426         if (!divas_maint_register_chrdev()) {
427                 ret = -EIO;
428                 goto out;
429         }
430         if (!create_maint_proc()) {
431                 printk(KERN_ERR "%s: failed to create proc entry.\n",
432                        DRIVERLNAME);
433                 divas_maint_unregister_chrdev();
434                 ret = -EIO;
435                 goto out;
436         }
437
438         if (!(mntfunc_init(&buffer_length, &buffer, diva_dbg_mem))) {
439                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
440                        DRIVERLNAME);
441                 remove_maint_proc();
442                 divas_maint_unregister_chrdev();
443                 ret = -EIO;
444                 goto out;
445         }
446
447         printk(KERN_INFO "%s: trace buffer = %p - %d kBytes, %s (Major: %d)\n",
448                DRIVERLNAME, buffer, (buffer_length / 1024),
449                (diva_dbg_mem == 0) ? "internal" : "external", major);
450
451       out:
452         return (ret);
453 }
454
455 /*
456 **  Driver Unload
457 */
458 static void DIVA_EXIT_FUNCTION maint_exit(void)
459 {
460         remove_maint_proc();
461         divas_maint_unregister_chrdev();
462         mntfunc_finit();
463
464         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
465 }
466
467 module_init(maint_init);
468 module_exit(maint_exit);