divamnt.c 5.5 KB

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