hysdn_proclog.c 12 KB

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