hysdn_proclog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* $Id: hysdn_proclog.c,v 1.9.6.3 2001/09/23 22:24:54 kai Exp $
  2. *
  3. * Linux driver for HYSDN cards, /proc/net filesystem log functions.
  4. *
  5. * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
  6. * Copyright 1999 by Werner Cornelius (werner@titro.de)
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/poll.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/mutex.h>
  18. #include <linux/kernel.h>
  19. #include "hysdn_defs.h"
  20. /* the proc subdir for the interface is defined in the procconf module */
  21. extern struct proc_dir_entry *hysdn_proc_entry;
  22. static DEFINE_MUTEX(hysdn_log_mutex);
  23. static void put_log_buffer(hysdn_card *card, char *cp);
  24. /*************************************************/
  25. /* structure keeping ascii log for device output */
  26. /*************************************************/
  27. struct log_data {
  28. struct log_data *next;
  29. unsigned long usage_cnt;/* number of files still to work */
  30. void *proc_ctrl; /* pointer to own control procdata structure */
  31. char log_start[2]; /* log string start (final len aligned by size) */
  32. };
  33. /**********************************************/
  34. /* structure holding proc entrys for one card */
  35. /**********************************************/
  36. struct procdata {
  37. struct proc_dir_entry *log; /* log entry */
  38. char log_name[15]; /* log filename */
  39. struct log_data *log_head, *log_tail; /* head and tail for queue */
  40. int if_used; /* open count for interface */
  41. int volatile del_lock; /* lock for delete operations */
  42. unsigned char logtmp[LOG_MAX_LINELEN];
  43. wait_queue_head_t rd_queue;
  44. };
  45. /**********************************************/
  46. /* log function for cards error log interface */
  47. /**********************************************/
  48. void
  49. hysdn_card_errlog(hysdn_card *card, tErrLogEntry *logp, int maxsize)
  50. {
  51. char buf[ERRLOG_TEXT_SIZE + 40];
  52. sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
  53. put_log_buffer(card, buf); /* output the string */
  54. } /* hysdn_card_errlog */
  55. /***************************************************/
  56. /* Log function using format specifiers for output */
  57. /***************************************************/
  58. void
  59. hysdn_addlog(hysdn_card *card, char *fmt, ...)
  60. {
  61. struct procdata *pd = card->proclog;
  62. char *cp;
  63. va_list args;
  64. if (!pd)
  65. return; /* log structure non existent */
  66. cp = pd->logtmp;
  67. cp += sprintf(cp, "HYSDN: card %d ", card->myid);
  68. va_start(args, fmt);
  69. cp += vsprintf(cp, fmt, args);
  70. va_end(args);
  71. *cp++ = '\n';
  72. *cp = 0;
  73. if (card->debug_flags & DEB_OUT_SYSLOG)
  74. printk(KERN_INFO "%s", pd->logtmp);
  75. else
  76. put_log_buffer(card, pd->logtmp);
  77. } /* hysdn_addlog */
  78. /********************************************/
  79. /* put an log buffer into the log queue. */
  80. /* This buffer will be kept until all files */
  81. /* opened for read got the contents. */
  82. /* Flushes buffers not longer in use. */
  83. /********************************************/
  84. static void
  85. put_log_buffer(hysdn_card *card, char *cp)
  86. {
  87. struct log_data *ib;
  88. struct procdata *pd = card->proclog;
  89. int i;
  90. unsigned long flags;
  91. if (!pd)
  92. return;
  93. if (!cp)
  94. return;
  95. if (!*cp)
  96. return;
  97. if (pd->if_used <= 0)
  98. return; /* no open file for read */
  99. if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
  100. return; /* no memory */
  101. strcpy(ib->log_start, cp); /* set output string */
  102. ib->next = NULL;
  103. ib->proc_ctrl = pd; /* point to own control structure */
  104. spin_lock_irqsave(&card->hysdn_lock, flags);
  105. ib->usage_cnt = pd->if_used;
  106. if (!pd->log_head)
  107. pd->log_head = ib; /* new head */
  108. else
  109. pd->log_tail->next = ib; /* follows existing messages */
  110. pd->log_tail = ib; /* new tail */
  111. i = pd->del_lock++; /* get lock state */
  112. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  113. /* delete old entrys */
  114. if (!i)
  115. while (pd->log_head->next) {
  116. if ((pd->log_head->usage_cnt <= 0) &&
  117. (pd->log_head->next->usage_cnt <= 0)) {
  118. ib = pd->log_head;
  119. pd->log_head = pd->log_head->next;
  120. kfree(ib);
  121. } else
  122. break;
  123. } /* pd->log_head->next */
  124. pd->del_lock--; /* release lock level */
  125. wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
  126. } /* put_log_buffer */
  127. /******************************/
  128. /* file operations and tables */
  129. /******************************/
  130. /****************************************/
  131. /* write log file -> set log level bits */
  132. /****************************************/
  133. static ssize_t
  134. hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  135. {
  136. int rc;
  137. hysdn_card *card = file->private_data;
  138. rc = kstrtoul_from_user(buf, count, 0, &card->debug_flags);
  139. if (rc < 0)
  140. return rc;
  141. hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
  142. return (count);
  143. } /* hysdn_log_write */
  144. /******************/
  145. /* read log file */
  146. /******************/
  147. static ssize_t
  148. hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
  149. {
  150. struct log_data *inf;
  151. int len;
  152. hysdn_card *card = PDE_DATA(file_inode(file));
  153. if (!(inf = *((struct log_data **) file->private_data))) {
  154. struct procdata *pd = card->proclog;
  155. if (file->f_flags & O_NONBLOCK)
  156. return (-EAGAIN);
  157. wait_event_interruptible(pd->rd_queue, (inf =
  158. *((struct log_data **) file->private_data)));
  159. }
  160. if (!inf)
  161. return (0);
  162. inf->usage_cnt--; /* new usage count */
  163. file->private_data = &inf->next; /* next structure */
  164. if ((len = strlen(inf->log_start)) <= count) {
  165. if (copy_to_user(buf, inf->log_start, len))
  166. return -EFAULT;
  167. *off += len;
  168. return (len);
  169. }
  170. return (0);
  171. } /* hysdn_log_read */
  172. /******************/
  173. /* open log file */
  174. /******************/
  175. static int
  176. hysdn_log_open(struct inode *ino, struct file *filep)
  177. {
  178. hysdn_card *card = PDE_DATA(ino);
  179. mutex_lock(&hysdn_log_mutex);
  180. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  181. /* write only access -> write log level only */
  182. filep->private_data = card; /* remember our own card */
  183. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  184. struct procdata *pd = card->proclog;
  185. unsigned long flags;
  186. /* read access -> log/debug read */
  187. spin_lock_irqsave(&card->hysdn_lock, flags);
  188. pd->if_used++;
  189. if (pd->log_head)
  190. filep->private_data = &pd->log_tail->next;
  191. else
  192. filep->private_data = &pd->log_head;
  193. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  194. } else { /* simultaneous read/write access forbidden ! */
  195. mutex_unlock(&hysdn_log_mutex);
  196. return (-EPERM); /* no permission this time */
  197. }
  198. mutex_unlock(&hysdn_log_mutex);
  199. return nonseekable_open(ino, filep);
  200. } /* hysdn_log_open */
  201. /*******************************************************************************/
  202. /* close a cardlog file. If the file has been opened for exclusive write it is */
  203. /* assumed as pof data input and the pof loader is noticed about. */
  204. /* Otherwise file is handled as log output. In this case the interface usage */
  205. /* count is decremented and all buffers are noticed of closing. If this file */
  206. /* was the last one to be closed, all buffers are freed. */
  207. /*******************************************************************************/
  208. static int
  209. hysdn_log_close(struct inode *ino, struct file *filep)
  210. {
  211. struct log_data *inf;
  212. struct procdata *pd;
  213. hysdn_card *card;
  214. int retval = 0;
  215. mutex_lock(&hysdn_log_mutex);
  216. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  217. /* write only access -> write debug level written */
  218. retval = 0; /* success */
  219. } else {
  220. /* read access -> log/debug read, mark one further file as closed */
  221. inf = *((struct log_data **) filep->private_data); /* get first log entry */
  222. if (inf)
  223. pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
  224. else {
  225. /* no info available -> search card */
  226. card = PDE_DATA(file_inode(filep));
  227. pd = card->proclog; /* pointer to procfs log */
  228. }
  229. if (pd)
  230. pd->if_used--; /* decrement interface usage count by one */
  231. while (inf) {
  232. inf->usage_cnt--; /* decrement usage count for buffers */
  233. inf = inf->next;
  234. }
  235. if (pd)
  236. if (pd->if_used <= 0) /* delete buffers if last file closed */
  237. while (pd->log_head) {
  238. inf = pd->log_head;
  239. pd->log_head = pd->log_head->next;
  240. kfree(inf);
  241. }
  242. } /* read access */
  243. mutex_unlock(&hysdn_log_mutex);
  244. return (retval);
  245. } /* hysdn_log_close */
  246. /*************************************************/
  247. /* select/poll routine to be able using select() */
  248. /*************************************************/
  249. static unsigned int
  250. hysdn_log_poll(struct file *file, poll_table *wait)
  251. {
  252. unsigned int mask = 0;
  253. hysdn_card *card = PDE_DATA(file_inode(file));
  254. struct procdata *pd = card->proclog;
  255. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
  256. return (mask); /* no polling for write supported */
  257. poll_wait(file, &(pd->rd_queue), wait);
  258. if (*((struct log_data **) file->private_data))
  259. mask |= POLLIN | POLLRDNORM;
  260. return mask;
  261. } /* hysdn_log_poll */
  262. /**************************************************/
  263. /* table for log filesystem functions defined above. */
  264. /**************************************************/
  265. static const struct file_operations log_fops =
  266. {
  267. .owner = THIS_MODULE,
  268. .llseek = no_llseek,
  269. .read = hysdn_log_read,
  270. .write = hysdn_log_write,
  271. .poll = hysdn_log_poll,
  272. .open = hysdn_log_open,
  273. .release = hysdn_log_close,
  274. };
  275. /***********************************************************************************/
  276. /* hysdn_proclog_init is called when the module is loaded after creating the cards */
  277. /* conf files. */
  278. /***********************************************************************************/
  279. int
  280. hysdn_proclog_init(hysdn_card *card)
  281. {
  282. struct procdata *pd;
  283. /* create a cardlog proc entry */
  284. if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
  285. sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
  286. pd->log = proc_create_data(pd->log_name,
  287. S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
  288. &log_fops, card);
  289. init_waitqueue_head(&(pd->rd_queue));
  290. card->proclog = (void *) pd; /* remember procfs structure */
  291. }
  292. return (0);
  293. } /* hysdn_proclog_init */
  294. /************************************************************************************/
  295. /* hysdn_proclog_release is called when the module is unloaded and before the cards */
  296. /* conf file is released */
  297. /* The module counter is assumed to be 0 ! */
  298. /************************************************************************************/
  299. void
  300. hysdn_proclog_release(hysdn_card *card)
  301. {
  302. struct procdata *pd;
  303. if ((pd = (struct procdata *) card->proclog) != NULL) {
  304. if (pd->log)
  305. remove_proc_entry(pd->log_name, hysdn_proc_entry);
  306. kfree(pd); /* release memory */
  307. card->proclog = NULL;
  308. }
  309. } /* hysdn_proclog_release */