divamnt.c 5.5 KB

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