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