divamnt.c 5.5 KB

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