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