| 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> | 
|  | 14 | #include <linux/init.h> | 
|  | 15 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/poll.h> | 
| Jonathan Corbet | 3462032 | 2008-05-16 14:17:33 -0600 | [diff] [blame] | 17 | #include <linux/smp_lock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/uaccess.h> | 
|  | 19 |  | 
|  | 20 | #include "platform.h" | 
|  | 21 | #include "di_defs.h" | 
|  | 22 | #include "divasync.h" | 
|  | 23 | #include "debug_if.h" | 
|  | 24 |  | 
|  | 25 | static char *main_revision = "$Revision: 1.32.6.10 $"; | 
|  | 26 |  | 
|  | 27 | static int major; | 
|  | 28 |  | 
|  | 29 | MODULE_DESCRIPTION("Maint driver for Eicon DIVA Server cards"); | 
|  | 30 | MODULE_AUTHOR("Cytronics & Melware, Eicon Networks"); | 
|  | 31 | MODULE_SUPPORTED_DEVICE("DIVA card driver"); | 
|  | 32 | MODULE_LICENSE("GPL"); | 
|  | 33 |  | 
|  | 34 | static int buffer_length = 128; | 
|  | 35 | module_param(buffer_length, int, 0); | 
|  | 36 | static unsigned long diva_dbg_mem = 0; | 
|  | 37 | module_param(diva_dbg_mem, ulong, 0); | 
|  | 38 |  | 
|  | 39 | static char *DRIVERNAME = | 
|  | 40 | "Eicon DIVA - MAINT module (http://www.melware.net)"; | 
|  | 41 | static char *DRIVERLNAME = "diva_mnt"; | 
|  | 42 | static char *DEVNAME = "DivasMAINT"; | 
|  | 43 | char *DRIVERRELEASE_MNT = "2.0"; | 
|  | 44 |  | 
|  | 45 | static wait_queue_head_t msgwaitq; | 
|  | 46 | static unsigned long opened; | 
|  | 47 | static struct timeval start_time; | 
|  | 48 |  | 
|  | 49 | extern int mntfunc_init(int *, void **, unsigned long); | 
|  | 50 | extern void mntfunc_finit(void); | 
|  | 51 | extern int maint_read_write(void __user *buf, int count); | 
|  | 52 |  | 
|  | 53 | /* | 
|  | 54 | *  helper functions | 
|  | 55 | */ | 
|  | 56 | static 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 | */ | 
|  | 74 | int 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 | } | 
|  | 79 | int 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 | */ | 
|  | 88 | void 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 | */ | 
|  | 117 | static 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 |  | 
|  | 129 | static int maint_open(struct inode *ino, struct file *filep) | 
|  | 130 | { | 
| Jonathan Corbet | 3462032 | 2008-05-16 14:17:33 -0600 | [diff] [blame] | 131 | int ret; | 
|  | 132 |  | 
|  | 133 | lock_kernel(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* only one open is allowed, so we test | 
|  | 135 | it atomically */ | 
|  | 136 | if (test_and_set_bit(0, &opened)) | 
| Jonathan Corbet | 3462032 | 2008-05-16 14:17:33 -0600 | [diff] [blame] | 137 | ret = -EBUSY; | 
|  | 138 | else { | 
|  | 139 | filep->private_data = NULL; | 
|  | 140 | ret = nonseekable_open(ino, filep); | 
|  | 141 | } | 
|  | 142 | unlock_kernel(); | 
|  | 143 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | static int maint_close(struct inode *ino, struct file *filep) | 
|  | 147 | { | 
|  | 148 | if (filep->private_data) { | 
|  | 149 | diva_os_free(0, filep->private_data); | 
|  | 150 | filep->private_data = NULL; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | /* clear 'used' flag */ | 
|  | 154 | clear_bit(0, &opened); | 
|  | 155 |  | 
|  | 156 | return (0); | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | static ssize_t divas_maint_write(struct file *file, const char __user *buf, | 
|  | 160 | size_t count, loff_t * ppos) | 
|  | 161 | { | 
|  | 162 | return (maint_read_write((char __user *) buf, (int) count)); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | static ssize_t divas_maint_read(struct file *file, char __user *buf, | 
|  | 166 | size_t count, loff_t * ppos) | 
|  | 167 | { | 
|  | 168 | return (maint_read_write(buf, (int) count)); | 
|  | 169 | } | 
|  | 170 |  | 
| Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 171 | static const struct file_operations divas_maint_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | .owner   = THIS_MODULE, | 
|  | 173 | .llseek  = no_llseek, | 
|  | 174 | .read    = divas_maint_read, | 
|  | 175 | .write   = divas_maint_write, | 
|  | 176 | .poll    = maint_poll, | 
|  | 177 | .open    = maint_open, | 
|  | 178 | .release = maint_close | 
|  | 179 | }; | 
|  | 180 |  | 
|  | 181 | static void divas_maint_unregister_chrdev(void) | 
|  | 182 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | unregister_chrdev(major, DEVNAME); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | static int DIVA_INIT_FUNCTION divas_maint_register_chrdev(void) | 
|  | 187 | { | 
|  | 188 | if ((major = register_chrdev(0, DEVNAME, &divas_maint_fops)) < 0) | 
|  | 189 | { | 
|  | 190 | printk(KERN_ERR "%s: failed to create /dev entry.\n", | 
|  | 191 | DRIVERLNAME); | 
|  | 192 | return (0); | 
|  | 193 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 |  | 
|  | 195 | return (1); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | /* | 
|  | 199 | * wake up reader | 
|  | 200 | */ | 
|  | 201 | void diva_maint_wakeup_read(void) | 
|  | 202 | { | 
|  | 203 | wake_up_interruptible(&msgwaitq); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | /* | 
|  | 207 | *  Driver Load | 
|  | 208 | */ | 
|  | 209 | static int DIVA_INIT_FUNCTION maint_init(void) | 
|  | 210 | { | 
|  | 211 | char tmprev[50]; | 
|  | 212 | int ret = 0; | 
|  | 213 | void *buffer = NULL; | 
|  | 214 |  | 
|  | 215 | do_gettimeofday(&start_time); | 
|  | 216 | init_waitqueue_head(&msgwaitq); | 
|  | 217 |  | 
|  | 218 | printk(KERN_INFO "%s\n", DRIVERNAME); | 
|  | 219 | printk(KERN_INFO "%s: Rel:%s  Rev:", DRIVERLNAME, DRIVERRELEASE_MNT); | 
|  | 220 | strcpy(tmprev, main_revision); | 
|  | 221 | printk("%s  Build: %s \n", getrev(tmprev), DIVA_BUILD); | 
|  | 222 |  | 
|  | 223 | if (!divas_maint_register_chrdev()) { | 
|  | 224 | ret = -EIO; | 
|  | 225 | goto out; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | if (!(mntfunc_init(&buffer_length, &buffer, diva_dbg_mem))) { | 
|  | 229 | printk(KERN_ERR "%s: failed to connect to DIDD.\n", | 
|  | 230 | DRIVERLNAME); | 
|  | 231 | divas_maint_unregister_chrdev(); | 
|  | 232 | ret = -EIO; | 
|  | 233 | goto out; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | printk(KERN_INFO "%s: trace buffer = %p - %d kBytes, %s (Major: %d)\n", | 
|  | 237 | DRIVERLNAME, buffer, (buffer_length / 1024), | 
|  | 238 | (diva_dbg_mem == 0) ? "internal" : "external", major); | 
|  | 239 |  | 
|  | 240 | out: | 
|  | 241 | return (ret); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | /* | 
|  | 245 | **  Driver Unload | 
|  | 246 | */ | 
|  | 247 | static void DIVA_EXIT_FUNCTION maint_exit(void) | 
|  | 248 | { | 
|  | 249 | divas_maint_unregister_chrdev(); | 
|  | 250 | mntfunc_finit(); | 
|  | 251 |  | 
|  | 252 | printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME); | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | module_init(maint_init); | 
|  | 256 | module_exit(maint_exit); | 
|  | 257 |  |