Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.git] / drivers / isdn / hardware / eicon / divamnt.c
1 /* $Id: divamnt.c,v 1.32.6.10 2005/02/11 19:40:25 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/module.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/smp_lock.h>
18 #include <linux/poll.h>
19 #include <asm/uaccess.h>
20
21 #include "platform.h"
22 #include "di_defs.h"
23 #include "divasync.h"
24 #include "debug_if.h"
25
26 static char *main_revision = "$Revision: 1.32.6.10 $";
27
28 static int major;
29
30 MODULE_DESCRIPTION("Maint driver for Eicon DIVA Server cards");
31 MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
32 MODULE_SUPPORTED_DEVICE("DIVA card driver");
33 MODULE_LICENSE("GPL");
34
35 static int buffer_length = 128;
36 module_param(buffer_length, int, 0);
37 static unsigned long diva_dbg_mem = 0;
38 module_param(diva_dbg_mem, ulong, 0);
39
40 static char *DRIVERNAME =
41     "Eicon DIVA - MAINT module (http://www.melware.net)";
42 static char *DRIVERLNAME = "diva_mnt";
43 static char *DEVNAME = "DivasMAINT";
44 char *DRIVERRELEASE_MNT = "2.0";
45
46 static wait_queue_head_t msgwaitq;
47 static unsigned long opened;
48 static struct timeval start_time;
49
50 extern int mntfunc_init(int *, void **, unsigned long);
51 extern void mntfunc_finit(void);
52 extern int maint_read_write(void __user *buf, int count);
53
54 /*
55  *  helper functions
56  */
57 static char *getrev(const char *revision)
58 {
59         char *rev;
60         char *p;
61
62         if ((p = strchr(revision, ':'))) {
63                 rev = p + 2;
64                 p = strchr(rev, '$');
65                 *--p = 0;
66         } else
67                 rev = "1.0";
68
69         return rev;
70 }
71
72 /*
73  * kernel/user space copy functions
74  */
75 int diva_os_copy_to_user(void *os_handle, void __user *dst, const void *src,
76                          int length)
77 {
78         return (copy_to_user(dst, src, length));
79 }
80 int diva_os_copy_from_user(void *os_handle, void *dst, const void __user *src,
81                            int length)
82 {
83         return (copy_from_user(dst, src, length));
84 }
85
86 /*
87  * get time
88  */
89 void diva_os_get_time(dword * sec, dword * usec)
90 {
91         struct timeval tv;
92
93         do_gettimeofday(&tv);
94
95         if (tv.tv_sec > start_time.tv_sec) {
96                 if (start_time.tv_usec > tv.tv_usec) {
97                         tv.tv_sec--;
98                         tv.tv_usec += 1000000;
99                 }
100                 *sec = (dword) (tv.tv_sec - start_time.tv_sec);
101                 *usec = (dword) (tv.tv_usec - start_time.tv_usec);
102         } else if (tv.tv_sec == start_time.tv_sec) {
103                 *sec = 0;
104                 if (start_time.tv_usec < tv.tv_usec) {
105                         *usec = (dword) (tv.tv_usec - start_time.tv_usec);
106                 } else {
107                         *usec = 0;
108                 }
109         } else {
110                 *sec = (dword) tv.tv_sec;
111                 *usec = (dword) tv.tv_usec;
112         }
113 }
114
115 /*
116  * device node operations
117  */
118 static unsigned int maint_poll(struct file *file, poll_table * wait)
119 {
120         unsigned int mask = 0;
121
122         poll_wait(file, &msgwaitq, wait);
123         mask = POLLOUT | POLLWRNORM;
124         if (file->private_data || diva_dbg_q_length()) {
125                 mask |= POLLIN | POLLRDNORM;
126         }
127         return (mask);
128 }
129
130 static int maint_open(struct inode *ino, struct file *filep)
131 {
132         /* only one open is allowed, so we test
133            it atomically */
134         if (test_and_set_bit(0, &opened))
135                 return (-EBUSY);
136
137         filep->private_data = NULL;
138
139         return nonseekable_open(ino, filep);
140 }
141
142 static int maint_close(struct inode *ino, struct file *filep)
143 {
144         if (filep->private_data) {
145                 diva_os_free(0, filep->private_data);
146                 filep->private_data = NULL;
147         }
148
149         /* clear 'used' flag */
150         clear_bit(0, &opened);
151         
152         return (0);
153 }
154
155 static ssize_t divas_maint_write(struct file *file, const char __user *buf,
156                                  size_t count, loff_t * ppos)
157 {
158         return (maint_read_write((char __user *) buf, (int) count));
159 }
160
161 static ssize_t divas_maint_read(struct file *file, char __user *buf,
162                                 size_t count, loff_t * ppos)
163 {
164         return (maint_read_write(buf, (int) count));
165 }
166
167 static const struct file_operations divas_maint_fops = {
168         .owner   = THIS_MODULE,
169         .llseek  = no_llseek,
170         .read    = divas_maint_read,
171         .write   = divas_maint_write,
172         .poll    = maint_poll,
173         .open    = maint_open,
174         .release = maint_close
175 };
176
177 static void divas_maint_unregister_chrdev(void)
178 {
179         unregister_chrdev(major, DEVNAME);
180 }
181
182 static int DIVA_INIT_FUNCTION divas_maint_register_chrdev(void)
183 {
184         if ((major = register_chrdev(0, DEVNAME, &divas_maint_fops)) < 0)
185         {
186                 printk(KERN_ERR "%s: failed to create /dev entry.\n",
187                        DRIVERLNAME);
188                 return (0);
189         }
190
191         return (1);
192 }
193
194 /*
195  * wake up reader
196  */
197 void diva_maint_wakeup_read(void)
198 {
199         wake_up_interruptible(&msgwaitq);
200 }
201
202 /*
203  *  Driver Load
204  */
205 static int DIVA_INIT_FUNCTION maint_init(void)
206 {
207         char tmprev[50];
208         int ret = 0;
209         void *buffer = NULL;
210
211         do_gettimeofday(&start_time);
212         init_waitqueue_head(&msgwaitq);
213
214         printk(KERN_INFO "%s\n", DRIVERNAME);
215         printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_MNT);
216         strcpy(tmprev, main_revision);
217         printk("%s  Build: %s \n", getrev(tmprev), DIVA_BUILD);
218
219         if (!divas_maint_register_chrdev()) {
220                 ret = -EIO;
221                 goto out;
222         }
223
224         if (!(mntfunc_init(&buffer_length, &buffer, diva_dbg_mem))) {
225                 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
226                        DRIVERLNAME);
227                 divas_maint_unregister_chrdev();
228                 ret = -EIO;
229                 goto out;
230         }
231
232         printk(KERN_INFO "%s: trace buffer = %p - %d kBytes, %s (Major: %d)\n",
233                DRIVERLNAME, buffer, (buffer_length / 1024),
234                (diva_dbg_mem == 0) ? "internal" : "external", major);
235
236       out:
237         return (ret);
238 }
239
240 /*
241 **  Driver Unload
242 */
243 static void DIVA_EXIT_FUNCTION maint_exit(void)
244 {
245         divas_maint_unregister_chrdev();
246         mntfunc_finit();
247
248         printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
249 }
250
251 module_init(maint_init);
252 module_exit(maint_exit);
253