hysdn_proclog.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. unsigned char valbuf[128];
  138. hysdn_card *card = file->private_data;
  139. if (count > (sizeof(valbuf) - 1))
  140. count = sizeof(valbuf) - 1; /* limit length */
  141. if (copy_from_user(valbuf, buf, count))
  142. return (-EFAULT); /* copy failed */
  143. valbuf[count] = 0; /* terminating 0 */
  144. rc = kstrtoul(valbuf, 0, &card->debug_flags);
  145. if (rc < 0)
  146. return rc;
  147. hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
  148. return (count);
  149. } /* hysdn_log_write */
  150. /******************/
  151. /* read log file */
  152. /******************/
  153. static ssize_t
  154. hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
  155. {
  156. struct log_data *inf;
  157. int len;
  158. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  159. struct procdata *pd = NULL;
  160. hysdn_card *card;
  161. if (!*((struct log_data **) file->private_data)) {
  162. if (file->f_flags & O_NONBLOCK)
  163. return (-EAGAIN);
  164. /* sorry, but we need to search the card */
  165. card = card_root;
  166. while (card) {
  167. pd = card->proclog;
  168. if (pd->log == pde)
  169. break;
  170. card = card->next; /* search next entry */
  171. }
  172. if (card)
  173. interruptible_sleep_on(&(pd->rd_queue));
  174. else
  175. return (-EAGAIN);
  176. }
  177. if (!(inf = *((struct log_data **) file->private_data)))
  178. return (0);
  179. inf->usage_cnt--; /* new usage count */
  180. file->private_data = &inf->next; /* next structure */
  181. if ((len = strlen(inf->log_start)) <= count) {
  182. if (copy_to_user(buf, inf->log_start, len))
  183. return -EFAULT;
  184. *off += len;
  185. return (len);
  186. }
  187. return (0);
  188. } /* hysdn_log_read */
  189. /******************/
  190. /* open log file */
  191. /******************/
  192. static int
  193. hysdn_log_open(struct inode *ino, struct file *filep)
  194. {
  195. hysdn_card *card;
  196. struct procdata *pd = NULL;
  197. unsigned long flags;
  198. mutex_lock(&hysdn_log_mutex);
  199. card = card_root;
  200. while (card) {
  201. pd = card->proclog;
  202. if (pd->log == PDE(ino))
  203. break;
  204. card = card->next; /* search next entry */
  205. }
  206. if (!card) {
  207. mutex_unlock(&hysdn_log_mutex);
  208. return (-ENODEV); /* device is unknown/invalid */
  209. }
  210. filep->private_data = card; /* remember our own card */
  211. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  212. /* write only access -> write log level only */
  213. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  214. /* read access -> log/debug read */
  215. spin_lock_irqsave(&card->hysdn_lock, flags);
  216. pd->if_used++;
  217. if (pd->log_head)
  218. filep->private_data = &pd->log_tail->next;
  219. else
  220. filep->private_data = &pd->log_head;
  221. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  222. } else { /* simultaneous read/write access forbidden ! */
  223. mutex_unlock(&hysdn_log_mutex);
  224. return (-EPERM); /* no permission this time */
  225. }
  226. mutex_unlock(&hysdn_log_mutex);
  227. return nonseekable_open(ino, filep);
  228. } /* hysdn_log_open */
  229. /*******************************************************************************/
  230. /* close a cardlog file. If the file has been opened for exclusive write it is */
  231. /* assumed as pof data input and the pof loader is noticed about. */
  232. /* Otherwise file is handled as log output. In this case the interface usage */
  233. /* count is decremented and all buffers are noticed of closing. If this file */
  234. /* was the last one to be closed, all buffers are freed. */
  235. /*******************************************************************************/
  236. static int
  237. hysdn_log_close(struct inode *ino, struct file *filep)
  238. {
  239. struct log_data *inf;
  240. struct procdata *pd;
  241. hysdn_card *card;
  242. int retval = 0;
  243. mutex_lock(&hysdn_log_mutex);
  244. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  245. /* write only access -> write debug level written */
  246. retval = 0; /* success */
  247. } else {
  248. /* read access -> log/debug read, mark one further file as closed */
  249. pd = NULL;
  250. inf = *((struct log_data **) filep->private_data); /* get first log entry */
  251. if (inf)
  252. pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
  253. else {
  254. /* no info available -> search card */
  255. card = card_root;
  256. while (card) {
  257. pd = card->proclog;
  258. if (pd->log == PDE(ino))
  259. break;
  260. card = card->next; /* search next entry */
  261. }
  262. if (card)
  263. pd = card->proclog; /* pointer to procfs log */
  264. }
  265. if (pd)
  266. pd->if_used--; /* decrement interface usage count by one */
  267. while (inf) {
  268. inf->usage_cnt--; /* decrement usage count for buffers */
  269. inf = inf->next;
  270. }
  271. if (pd)
  272. if (pd->if_used <= 0) /* delete buffers if last file closed */
  273. while (pd->log_head) {
  274. inf = pd->log_head;
  275. pd->log_head = pd->log_head->next;
  276. kfree(inf);
  277. }
  278. } /* read access */
  279. mutex_unlock(&hysdn_log_mutex);
  280. return (retval);
  281. } /* hysdn_log_close */
  282. /*************************************************/
  283. /* select/poll routine to be able using select() */
  284. /*************************************************/
  285. static unsigned int
  286. hysdn_log_poll(struct file *file, poll_table * wait)
  287. {
  288. unsigned int mask = 0;
  289. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  290. hysdn_card *card;
  291. struct procdata *pd = NULL;
  292. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
  293. return (mask); /* no polling for write supported */
  294. /* we need to search the card */
  295. card = card_root;
  296. while (card) {
  297. pd = card->proclog;
  298. if (pd->log == pde)
  299. break;
  300. card = card->next; /* search next entry */
  301. }
  302. if (!card)
  303. return (mask); /* card not found */
  304. poll_wait(file, &(pd->rd_queue), wait);
  305. if (*((struct log_data **) file->private_data))
  306. mask |= POLLIN | POLLRDNORM;
  307. return mask;
  308. } /* hysdn_log_poll */
  309. /**************************************************/
  310. /* table for log filesystem functions defined above. */
  311. /**************************************************/
  312. static const struct file_operations log_fops =
  313. {
  314. .owner = THIS_MODULE,
  315. .llseek = no_llseek,
  316. .read = hysdn_log_read,
  317. .write = hysdn_log_write,
  318. .poll = hysdn_log_poll,
  319. .open = hysdn_log_open,
  320. .release = hysdn_log_close,
  321. };
  322. /***********************************************************************************/
  323. /* hysdn_proclog_init is called when the module is loaded after creating the cards */
  324. /* conf files. */
  325. /***********************************************************************************/
  326. int
  327. hysdn_proclog_init(hysdn_card * card)
  328. {
  329. struct procdata *pd;
  330. /* create a cardlog proc entry */
  331. if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
  332. sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
  333. pd->log = proc_create(pd->log_name,
  334. S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
  335. &log_fops);
  336. init_waitqueue_head(&(pd->rd_queue));
  337. card->proclog = (void *) pd; /* remember procfs structure */
  338. }
  339. return (0);
  340. } /* hysdn_proclog_init */
  341. /************************************************************************************/
  342. /* hysdn_proclog_release is called when the module is unloaded and before the cards */
  343. /* conf file is released */
  344. /* The module counter is assumed to be 0 ! */
  345. /************************************************************************************/
  346. void
  347. hysdn_proclog_release(hysdn_card * card)
  348. {
  349. struct procdata *pd;
  350. if ((pd = (struct procdata *) card->proclog) != NULL) {
  351. if (pd->log)
  352. remove_proc_entry(pd->log_name, hysdn_proc_entry);
  353. kfree(pd); /* release memory */
  354. card->proclog = NULL;
  355. }
  356. } /* hysdn_proclog_release */