ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 *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 *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 *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 *buf, size_t count, loff_t * off)
146 {
147         diva_dbg_entry_head_t *pmsg = 0;
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 (off != &file->f_pos)
155                 return -ESPIPE;
156
157         if (!file->private_data) {
158                 for (;;) {
159                         while (
160                                (pmsg =
161                                 diva_maint_get_message(&size,
162                                                        &old_irql))) {
163                                 if (!(pmsg->facility == MSG_TYPE_STRING)) {
164                                         diva_maint_ack_message(1,
165                                                                &old_irql);
166                                 } else {
167                                         break;
168                                 }
169                         }
170
171                         if (!pmsg) {
172                                 if (file->f_flags & O_NONBLOCK) {
173                                         return (-EAGAIN);
174                                 }
175                                 interruptible_sleep_on(&msgwaitq);
176                                 if (signal_pending(current)) {
177                                         return (-ERESTARTSYS);
178                                 }
179                         } else {
180                                 break;
181                         }
182                 }
183                 /*
184                    The length of message that shoule be read is:
185                    pmsg->data_length + label(25) + DrvID(2) + byte CR + trailing zero
186                  */
187                 if (!
188                     (str_msg =
189                      (int *) diva_os_malloc_tbuffer(0,
190                                                     pmsg->data_length +
191                                                     29 + 2 * sizeof(int)))) {
192                         diva_maint_ack_message(0, &old_irql);
193                         return (-ENOMEM);
194                 }
195                 pstr = (char *) &str_msg[2];
196
197                 switch (pmsg->dli) {
198                 case DLI_LOG:
199                         dli_label = "LOG";
200                         break;
201                 case DLI_FTL:
202                         dli_label = "FTL";
203                         break;
204                 case DLI_ERR:
205                         dli_label = "ERR";
206                         break;
207                 case DLI_TRC:
208                         dli_label = "TRC";
209                         break;
210                 case DLI_REG:
211                         dli_label = "REG";
212                         break;
213                 case DLI_MEM:
214                         dli_label = "MEM";
215                         break;
216                 case DLI_SPL:
217                         dli_label = "SPL";
218                         break;
219                 case DLI_IRP:
220                         dli_label = "IRP";
221                         break;
222                 case DLI_TIM:
223                         dli_label = "TIM";
224                         break;
225                 case DLI_TAPI:
226                         dli_label = "TAPI";
227                         break;
228                 case DLI_NDIS:
229                         dli_label = "NDIS";
230                         break;
231                 case DLI_CONN:
232                         dli_label = "CONN";
233                         break;
234                 case DLI_STAT:
235                         dli_label = "STAT";
236                         break;
237                 case DLI_PRV0:
238                         dli_label = "PRV0";
239                         break;
240                 case DLI_PRV1:
241                         dli_label = "PRV1";
242                         break;
243                 case DLI_PRV2:
244                         dli_label = "PRV2";
245                         break;
246                 case DLI_PRV3:
247                         dli_label = "PRV3";
248                         break;
249                 }
250                 str_length = sprintf(pstr, "%s %02x %s\n",
251                                      dli_label, (byte) pmsg->drv_id,
252                                      (char *) &pmsg[1]);
253                 str_msg[0] = str_length;
254                 str_msg[1] = 0;
255                 file->private_data = str_msg;
256                 diva_maint_ack_message(1, &old_irql);
257         } else {
258                 str_msg = (int *) file->private_data;
259                 pstr = (char *) &str_msg[2];
260                 pstr += str_msg[1];     /* head + offset */
261                 str_length = str_msg[0] - str_msg[1];   /* length - offset */
262         }
263         str_length = MIN(str_length, count);
264
265         if (diva_os_copy_to_user(NULL, buf, pstr, str_length)) {
266                 diva_os_free_tbuffer(0, str_msg);
267                 file->private_data = 0;
268                 return (-EFAULT);
269         }
270         str_msg[1] += str_length;
271         if ((str_msg[0] - str_msg[1]) <= 0) {
272                 diva_os_free_tbuffer(0, str_msg);
273                 file->private_data = 0;
274         }
275
276         return (str_length);
277 }
278
279 static ssize_t
280 maint_write(struct file *file, const char *buf, size_t count, loff_t * off)
281 {
282         return (-ENODEV);
283 }
284
285 static unsigned int maint_poll(struct file *file, poll_table * wait)
286 {
287         unsigned int mask = 0;
288
289         poll_wait(file, &msgwaitq, wait);
290         mask = POLLOUT | POLLWRNORM;
291         if (file->private_data || diva_dbg_q_length()) {
292                 mask |= POLLIN | POLLRDNORM;
293         }
294         return (mask);
295 }
296
297 static int maint_open(struct inode *ino, struct file *filep)
298 {
299         down(&opened_sem);
300         if (opened) {
301                 up(&opened_sem);
302                 return (-EBUSY);
303         }
304         opened++;
305         up(&opened_sem);
306
307         filep->private_data = 0;
308
309         return (0);
310 }
311
312 static int maint_close(struct inode *ino, struct file *filep)
313 {
314         if (filep->private_data) {
315                 diva_os_free_tbuffer(0, filep->private_data);
316                 filep->private_data = 0;
317         }
318
319         down(&opened_sem);
320         opened--;
321         up(&opened_sem);
322         return (0);
323 }
324
325 /*
326  * fops
327  */
328 static struct file_operations maint_fops = {
329         .owner   = THIS_MODULE,
330         .llseek  = no_llseek,
331         .read    = maint_read,
332         .write   = maint_write,
333         .poll    = maint_poll,
334         .open    = maint_open,
335         .release = maint_close
336 };
337
338 static int DIVA_INIT_FUNCTION create_maint_proc(void)
339 {
340         maint_proc_entry =
341             create_proc_entry("maint", S_IFREG | S_IRUGO | S_IWUSR,
342                               proc_net_eicon);
343         if (!maint_proc_entry)
344                 return (0);
345
346         maint_proc_entry->proc_fops = &maint_fops;
347         maint_proc_entry->owner = THIS_MODULE;
348
349         return (1);
350 }
351
352 static void remove_maint_proc(void)
353 {
354         if (maint_proc_entry) {
355                 remove_proc_entry("maint", proc_net_eicon);
356                 maint_proc_entry = NULL;
357         }
358 }
359
360 /*
361  * device node operations
362  */
363 static ssize_t divas_maint_write(struct file *file, const char *buf,
364                                  size_t count, loff_t * ppos)
365 {
366         return (maint_read_write((char *) buf, (int) count));
367 }
368
369 static ssize_t divas_maint_read(struct file *file, char *buf,
370                                 size_t count, loff_t * ppos)
371 {
372         return (maint_read_write(buf, (int) count));
373 }
374
375 static struct file_operations divas_maint_fops = {
376         .owner   = THIS_MODULE,
377         .llseek  = no_llseek,
378         .read    = divas_maint_read,
379         .write   = divas_maint_write,
380         .poll    = maint_poll,
381         .open    = maint_open,
382         .release = maint_close
383 };
384
385 static void divas_maint_unregister_chrdev(void)
386 {
387         devfs_remove(DEVNAME);
388         unregister_chrdev(major, DEVNAME);
389 }
390
391 static int DIVA_INIT_FUNCTION divas_maint_register_chrdev(void)
392 {
393         if ((major = register_chrdev(0, DEVNAME, &divas_maint_fops)) < 0)
394         {
395                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
396                        DRIVERLNAME);
397                 return (0);
398         }
399         devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
400
401         return (1);
402 }
403
404 /*
405  * wake up reader
406  */
407 void diva_maint_wakeup_read(void)
408 {
409         wake_up_interruptible(&msgwaitq);
410 }
411
412 /*
413  *  Driver Load
414  */
415 static int DIVA_INIT_FUNCTION maint_init(void)
416 {
417         char tmprev[50];
418         int ret = 0;
419         void *buffer = 0;
420
421         do_gettimeofday(&start_time);
422         init_waitqueue_head(&msgwaitq);
423
424         printk(KERN_INFO "%s\n", DRIVERNAME);
425         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_MNT);
426         strcpy(tmprev, main_revision);
427         printk("%s  Build: %s \n", getrev(tmprev), DIVA_BUILD);
428
429         if (!divas_maint_register_chrdev()) {
430                 ret = -EIO;
431                 goto out;
432         }
433         if (!create_maint_proc()) {
434                 printk(KERN_ERR "%s: failed to create proc entry.\n",
435                        DRIVERLNAME);
436                 divas_maint_unregister_chrdev();
437                 ret = -EIO;
438                 goto out;
439         }
440
441         if (!(mntfunc_init(&buffer_length, &buffer, diva_dbg_mem))) {
442                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
443                        DRIVERLNAME);
444                 remove_maint_proc();
445                 divas_maint_unregister_chrdev();
446                 ret = -EIO;
447                 goto out;
448         }
449
450         printk(KERN_INFO "%s: trace buffer = %p - %d kBytes, %s (Major: %d)\n",
451                DRIVERLNAME, buffer, (buffer_length / 1024),
452                (diva_dbg_mem == 0) ? "internal" : "external", major);
453
454       out:
455         return (ret);
456 }
457
458 /*
459 **  Driver Unload
460 */
461 static void DIVA_EXIT_FUNCTION maint_exit(void)
462 {
463         remove_maint_proc();
464         divas_maint_unregister_chrdev();
465         mntfunc_finit();
466
467         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
468 }
469
470 module_init(maint_init);
471 module_exit(maint_exit);