1 /* $Id: divamnt.c,v 1.32 2004/01/15 09:48:13 armin Exp $
3 * Driver for Eicon DIVA Server ISDN cards.
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/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>
29 static char *main_revision = "$Revision: 1.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");
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");
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";
49 static wait_queue_head_t msgwaitq;
50 static DECLARE_MUTEX(opened_sem);
52 static struct timeval start_time;
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);
61 static char *getrev(const char *revision)
66 if ((p = strchr(revision, ':'))) {
79 void *diva_os_malloc_tbuffer(unsigned long flags, unsigned long size)
81 return (kmalloc(size, GFP_KERNEL));
83 void diva_os_free_tbuffer(unsigned long flags, void *ptr)
91 * kernel/user space copy functions
93 int diva_os_copy_to_user(void *os_handle, void *dst, const void *src,
96 return (copy_to_user(dst, src, length));
98 int diva_os_copy_from_user(void *os_handle, void *dst, const void *src,
101 return (copy_from_user(dst, src, length));
107 void diva_os_get_time(dword * sec, dword * usec)
111 do_gettimeofday(&tv);
113 if (tv.tv_sec > start_time.tv_sec) {
114 if (start_time.tv_usec > tv.tv_usec) {
116 tv.tv_usec += 1000000;
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) {
122 if (start_time.tv_usec < tv.tv_usec) {
123 *usec = (dword) (tv.tv_usec - start_time.tv_usec);
128 *sec = (dword) tv.tv_sec;
129 *usec = (dword) tv.tv_usec;
137 extern struct proc_dir_entry *proc_net_eicon;
138 static struct proc_dir_entry *maint_proc_entry = NULL;
141 Read function is provided for compatibility reason - this allows
142 to read unstructured traces, formated as ascii string only
145 maint_read(struct file *file, char *buf, size_t count, loff_t * off)
147 diva_dbg_entry_head_t *pmsg = 0;
148 diva_os_spin_lock_magic_t old_irql;
150 char *pstr, *dli_label = "UNK";
154 if (off != &file->f_pos)
157 if (!file->private_data) {
161 diva_maint_get_message(&size,
163 if (!(pmsg->facility == MSG_TYPE_STRING)) {
164 diva_maint_ack_message(1,
172 if (file->f_flags & O_NONBLOCK) {
175 interruptible_sleep_on(&msgwaitq);
176 if (signal_pending(current)) {
177 return (-ERESTARTSYS);
184 The length of message that shoule be read is:
185 pmsg->data_length + label(25) + DrvID(2) + byte CR + trailing zero
189 (int *) diva_os_malloc_tbuffer(0,
191 29 + 2 * sizeof(int)))) {
192 diva_maint_ack_message(0, &old_irql);
195 pstr = (char *) &str_msg[2];
250 str_length = sprintf(pstr, "%s %02x %s\n",
251 dli_label, (byte) pmsg->drv_id,
253 str_msg[0] = str_length;
255 file->private_data = str_msg;
256 diva_maint_ack_message(1, &old_irql);
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 */
263 str_length = MIN(str_length, count);
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;
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;
280 maint_write(struct file *file, const char *buf, size_t count, loff_t * off)
285 static unsigned int maint_poll(struct file *file, poll_table * wait)
287 unsigned int mask = 0;
289 poll_wait(file, &msgwaitq, wait);
290 mask = POLLOUT | POLLWRNORM;
291 if (file->private_data || diva_dbg_q_length()) {
292 mask |= POLLIN | POLLRDNORM;
297 static int maint_open(struct inode *ino, struct file *filep)
307 filep->private_data = 0;
312 static int maint_close(struct inode *ino, struct file *filep)
314 if (filep->private_data) {
315 diva_os_free_tbuffer(0, filep->private_data);
316 filep->private_data = 0;
328 static struct file_operations maint_fops = {
329 .owner = THIS_MODULE,
332 .write = maint_write,
335 .release = maint_close
338 static int DIVA_INIT_FUNCTION create_maint_proc(void)
341 create_proc_entry("maint", S_IFREG | S_IRUGO | S_IWUSR,
343 if (!maint_proc_entry)
346 maint_proc_entry->proc_fops = &maint_fops;
347 maint_proc_entry->owner = THIS_MODULE;
352 static void remove_maint_proc(void)
354 if (maint_proc_entry) {
355 remove_proc_entry("maint", proc_net_eicon);
356 maint_proc_entry = NULL;
361 * device node operations
363 static ssize_t divas_maint_write(struct file *file, const char *buf,
364 size_t count, loff_t * ppos)
366 return (maint_read_write((char *) buf, (int) count));
369 static ssize_t divas_maint_read(struct file *file, char *buf,
370 size_t count, loff_t * ppos)
372 return (maint_read_write(buf, (int) count));
375 static struct file_operations divas_maint_fops = {
376 .owner = THIS_MODULE,
378 .read = divas_maint_read,
379 .write = divas_maint_write,
382 .release = maint_close
385 static void divas_maint_unregister_chrdev(void)
387 devfs_remove(DEVNAME);
388 unregister_chrdev(major, DEVNAME);
391 static int DIVA_INIT_FUNCTION divas_maint_register_chrdev(void)
393 if ((major = register_chrdev(0, DEVNAME, &divas_maint_fops)) < 0)
395 printk(KERN_ERR "%s: failed to create /dev entry.\n",
399 devfs_mk_cdev(MKDEV(major, 0), S_IFCHR|S_IRUSR|S_IWUSR, DEVNAME);
407 void diva_maint_wakeup_read(void)
409 wake_up_interruptible(&msgwaitq);
415 static int DIVA_INIT_FUNCTION maint_init(void)
421 do_gettimeofday(&start_time);
422 init_waitqueue_head(&msgwaitq);
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);
429 if (!divas_maint_register_chrdev()) {
433 if (!create_maint_proc()) {
434 printk(KERN_ERR "%s: failed to create proc entry.\n",
436 divas_maint_unregister_chrdev();
441 if (!(mntfunc_init(&buffer_length, &buffer, diva_dbg_mem))) {
442 printk(KERN_ERR "%s: failed to connect to DIDD.\n",
445 divas_maint_unregister_chrdev();
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);
461 static void DIVA_EXIT_FUNCTION maint_exit(void)
464 divas_maint_unregister_chrdev();
467 printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
470 module_init(maint_init);
471 module_exit(maint_exit);