hysdn_proclog.c 11 KB

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