1 /* $Id: hysdn_proclog.c,v 1.9.6.3 2001/09/23 22:24:54 kai Exp $
3 * Linux driver for HYSDN cards, /proc/net filesystem log functions.
5 * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 * Copyright 1999 by Werner Cornelius (werner@titro.de)
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/proc_fs.h>
16 #include <linux/pci.h>
17 #include <linux/smp_lock.h>
19 #include "hysdn_defs.h"
21 /* the proc subdir for the interface is defined in the procconf module */
22 extern struct proc_dir_entry *hysdn_proc_entry;
24 static void put_log_buffer(hysdn_card * card, char *cp);
26 /*************************************************/
27 /* structure keeping ascii log for device output */
28 /*************************************************/
30 struct log_data *next;
31 ulong usage_cnt; /* number of files still to work */
32 void *proc_ctrl; /* pointer to own control procdata structure */
33 char log_start[2]; /* log string start (final len aligned by size) */
36 /**********************************************/
37 /* structure holding proc entrys for one card */
38 /**********************************************/
40 struct proc_dir_entry *log; /* log entry */
41 char log_name[15]; /* log filename */
42 struct log_data *log_head, *log_tail; /* head and tail for queue */
43 int if_used; /* open count for interface */
44 int volatile del_lock; /* lock for delete operations */
45 uchar logtmp[LOG_MAX_LINELEN];
46 wait_queue_head_t rd_queue;
50 /**********************************************/
51 /* log function for cards error log interface */
52 /**********************************************/
54 hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
56 char buf[ERRLOG_TEXT_SIZE + 40];
58 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
59 put_log_buffer(card, buf); /* output the string */
60 } /* hysdn_card_errlog */
62 /***************************************************/
63 /* Log function using format specifiers for output */
64 /***************************************************/
66 hysdn_addlog(hysdn_card * card, char *fmt,...)
68 struct procdata *pd = card->proclog;
73 return; /* log structure non existent */
76 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
79 cp += vsprintf(cp, fmt, args);
84 if (card->debug_flags & DEB_OUT_SYSLOG)
85 printk(KERN_INFO "%s", pd->logtmp);
87 put_log_buffer(card, pd->logtmp);
91 /********************************************/
92 /* put an log buffer into the log queue. */
93 /* This buffer will be kept until all files */
94 /* opened for read got the contents. */
95 /* Flushes buffers not longer in use. */
96 /********************************************/
98 put_log_buffer(hysdn_card * card, char *cp)
101 struct procdata *pd = card->proclog;
111 if (pd->if_used <= 0)
112 return; /* no open file for read */
114 if (!(ib = (struct log_data *) kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
115 return; /* no memory */
116 strcpy(ib->log_start, cp); /* set output string */
118 ib->proc_ctrl = pd; /* point to own control structure */
121 ib->usage_cnt = pd->if_used;
123 pd->log_head = ib; /* new head */
125 pd->log_tail->next = ib; /* follows existing messages */
126 pd->log_tail = ib; /* new tail */
127 i = pd->del_lock++; /* get lock state */
128 restore_flags(flags);
130 /* delete old entrys */
132 while (pd->log_head->next) {
133 if ((pd->log_head->usage_cnt <= 0) &&
134 (pd->log_head->next->usage_cnt <= 0)) {
136 pd->log_head = pd->log_head->next;
140 } /* pd->log_head->next */
141 pd->del_lock--; /* release lock level */
142 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
143 } /* put_log_buffer */
146 /******************************/
147 /* file operations and tables */
148 /******************************/
150 /****************************************/
151 /* write log file -> set log level bits */
152 /****************************************/
154 hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
158 uchar *cp, valbuf[128];
160 hysdn_card *card = (hysdn_card *) file->private_data;
162 if (count > (sizeof(valbuf) - 1))
163 count = sizeof(valbuf) - 1; /* limit length */
164 if (copy_from_user(valbuf, buf, count))
165 return (-EFAULT); /* copy failed */
167 valbuf[count] = 0; /* terminating 0 */
169 if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
170 cp += 2; /* pointer after hex modifier */
173 /* scan the input for debug flags */
175 if ((*cp >= '0') && (*cp <= '9')) {
177 u *= base; /* adjust to next digit */
182 break; /* end of number */
184 if ((*cp >= 'a') && (*cp <= 'f')) {
186 u *= base; /* adjust to next digit */
187 u += *cp++ - 'a' + 10;
190 break; /* terminated */
194 card->debug_flags = u; /* remember debug flags */
195 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
198 } /* hysdn_log_write */
204 hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
206 struct log_data *inf;
208 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
209 struct procdata *pd = NULL;
212 if (!*((struct log_data **) file->private_data)) {
213 if (file->f_flags & O_NONBLOCK)
216 /* sorry, but we need to search the card */
222 card = card->next; /* search next entry */
225 interruptible_sleep_on(&(pd->rd_queue));
230 if (!(inf = *((struct log_data **) file->private_data)))
233 inf->usage_cnt--; /* new usage count */
234 file->private_data = &inf->next; /* next structure */
235 if ((len = strlen(inf->log_start)) <= count) {
236 if (copy_to_user(buf, inf->log_start, len))
242 } /* hysdn_log_read */
248 hysdn_log_open(struct inode *ino, struct file *filep)
251 struct procdata *pd = NULL;
258 if (pd->log == PDE(ino))
260 card = card->next; /* search next entry */
264 return (-ENODEV); /* device is unknown/invalid */
266 filep->private_data = card; /* remember our own card */
268 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
269 /* write only access -> write log level only */
270 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
272 /* read access -> log/debug read */
277 filep->private_data = &pd->log_tail->next;
279 filep->private_data = &pd->log_head;
280 restore_flags(flags);
281 } else { /* simultaneous read/write access forbidden ! */
283 return (-EPERM); /* no permission this time */
286 return nonseekable_open(ino, filep);
287 } /* hysdn_log_open */
289 /*******************************************************************************/
290 /* close a cardlog file. If the file has been opened for exclusive write it is */
291 /* assumed as pof data input and the pof loader is noticed about. */
292 /* Otherwise file is handled as log output. In this case the interface usage */
293 /* count is decremented and all buffers are noticed of closing. If this file */
294 /* was the last one to be closed, all buffers are freed. */
295 /*******************************************************************************/
297 hysdn_log_close(struct inode *ino, struct file *filep)
299 struct log_data *inf;
307 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
308 /* write only access -> write debug level written */
309 retval = 0; /* success */
311 /* read access -> log/debug read, mark one further file as closed */
316 inf = *((struct log_data **) filep->private_data); /* get first log entry */
318 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
320 /* no info available -> search card */
324 if (pd->log == PDE(ino))
326 card = card->next; /* search next entry */
329 pd = card->proclog; /* pointer to procfs log */
332 pd->if_used--; /* decrement interface usage count by one */
335 inf->usage_cnt--; /* decrement usage count for buffers */
338 restore_flags(flags);
341 if (pd->if_used <= 0) /* delete buffers if last file closed */
342 while (pd->log_head) {
344 pd->log_head = pd->log_head->next;
351 } /* hysdn_log_close */
353 /*************************************************/
354 /* select/poll routine to be able using select() */
355 /*************************************************/
357 hysdn_log_poll(struct file *file, poll_table * wait)
359 unsigned int mask = 0;
360 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
362 struct procdata *pd = NULL;
364 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
365 return (mask); /* no polling for write supported */
367 /* we need to search the card */
373 card = card->next; /* search next entry */
376 return (mask); /* card not found */
378 poll_wait(file, &(pd->rd_queue), wait);
380 if (*((struct log_data **) file->private_data))
381 mask |= POLLIN | POLLRDNORM;
384 } /* hysdn_log_poll */
386 /**************************************************/
387 /* table for log filesystem functions defined above. */
388 /**************************************************/
389 static struct file_operations log_fops =
392 .read = hysdn_log_read,
393 .write = hysdn_log_write,
394 .poll = hysdn_log_poll,
395 .open = hysdn_log_open,
396 .release = hysdn_log_close,
400 /***********************************************************************************/
401 /* hysdn_proclog_init is called when the module is loaded after creating the cards */
403 /***********************************************************************************/
405 hysdn_proclog_init(hysdn_card * card)
409 /* create a cardlog proc entry */
411 if ((pd = (struct procdata *) kmalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
412 memset(pd, 0, sizeof(struct procdata));
413 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
414 if ((pd->log = create_proc_entry(pd->log_name, S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry)) != NULL) {
415 pd->log->proc_fops = &log_fops;
416 pd->log->owner = THIS_MODULE;
419 init_waitqueue_head(&(pd->rd_queue));
421 card->proclog = (void *) pd; /* remember procfs structure */
424 } /* hysdn_proclog_init */
426 /************************************************************************************/
427 /* hysdn_proclog_release is called when the module is unloaded and before the cards */
428 /* conf file is released */
429 /* The module counter is assumed to be 0 ! */
430 /************************************************************************************/
432 hysdn_proclog_release(hysdn_card * card)
436 if ((pd = (struct procdata *) card->proclog) != NULL) {
438 remove_proc_entry(pd->log_name, hysdn_proc_entry);
439 kfree(pd); /* release memory */
440 card->proclog = NULL;
442 } /* hysdn_proclog_release */