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/smp_lock.h>
18 #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;
23 static void put_log_buffer(hysdn_card * card, char *cp);
25 /*************************************************/
26 /* structure keeping ascii log for device output */
27 /*************************************************/
29 struct log_data *next;
30 unsigned long usage_cnt;/* number of files still to work */
31 void *proc_ctrl; /* pointer to own control procdata structure */
32 char log_start[2]; /* log string start (final len aligned by size) */
35 /**********************************************/
36 /* structure holding proc entrys for one card */
37 /**********************************************/
39 struct proc_dir_entry *log; /* log entry */
40 char log_name[15]; /* log filename */
41 struct log_data *log_head, *log_tail; /* head and tail for queue */
42 int if_used; /* open count for interface */
43 int volatile del_lock; /* lock for delete operations */
44 unsigned char logtmp[LOG_MAX_LINELEN];
45 wait_queue_head_t rd_queue;
49 /**********************************************/
50 /* log function for cards error log interface */
51 /**********************************************/
53 hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
55 char buf[ERRLOG_TEXT_SIZE + 40];
57 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
58 put_log_buffer(card, buf); /* output the string */
59 } /* hysdn_card_errlog */
61 /***************************************************/
62 /* Log function using format specifiers for output */
63 /***************************************************/
65 hysdn_addlog(hysdn_card * card, char *fmt,...)
67 struct procdata *pd = card->proclog;
72 return; /* log structure non existent */
75 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
78 cp += vsprintf(cp, fmt, args);
83 if (card->debug_flags & DEB_OUT_SYSLOG)
84 printk(KERN_INFO "%s", pd->logtmp);
86 put_log_buffer(card, pd->logtmp);
90 /********************************************/
91 /* put an log buffer into the log queue. */
92 /* This buffer will be kept until all files */
93 /* opened for read got the contents. */
94 /* Flushes buffers not longer in use. */
95 /********************************************/
97 put_log_buffer(hysdn_card * card, char *cp)
100 struct procdata *pd = card->proclog;
110 if (pd->if_used <= 0)
111 return; /* no open file for read */
113 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
114 return; /* no memory */
115 strcpy(ib->log_start, cp); /* set output string */
117 ib->proc_ctrl = pd; /* point to own control structure */
118 spin_lock_irqsave(&card->hysdn_lock, flags);
119 ib->usage_cnt = pd->if_used;
121 pd->log_head = ib; /* new head */
123 pd->log_tail->next = ib; /* follows existing messages */
124 pd->log_tail = ib; /* new tail */
125 i = pd->del_lock++; /* get lock state */
126 spin_unlock_irqrestore(&card->hysdn_lock, flags);
128 /* delete old entrys */
130 while (pd->log_head->next) {
131 if ((pd->log_head->usage_cnt <= 0) &&
132 (pd->log_head->next->usage_cnt <= 0)) {
134 pd->log_head = pd->log_head->next;
138 } /* pd->log_head->next */
139 pd->del_lock--; /* release lock level */
140 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
141 } /* put_log_buffer */
144 /******************************/
145 /* file operations and tables */
146 /******************************/
148 /****************************************/
149 /* write log file -> set log level bits */
150 /****************************************/
152 hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
156 unsigned char *cp, valbuf[128];
158 hysdn_card *card = (hysdn_card *) file->private_data;
160 if (count > (sizeof(valbuf) - 1))
161 count = sizeof(valbuf) - 1; /* limit length */
162 if (copy_from_user(valbuf, buf, count))
163 return (-EFAULT); /* copy failed */
165 valbuf[count] = 0; /* terminating 0 */
167 if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
168 cp += 2; /* pointer after hex modifier */
171 /* scan the input for debug flags */
173 if ((*cp >= '0') && (*cp <= '9')) {
175 u *= base; /* adjust to next digit */
180 break; /* end of number */
182 if ((*cp >= 'a') && (*cp <= 'f')) {
184 u *= base; /* adjust to next digit */
185 u += *cp++ - 'a' + 10;
188 break; /* terminated */
192 card->debug_flags = u; /* remember debug flags */
193 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
196 } /* hysdn_log_write */
202 hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
204 struct log_data *inf;
206 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
207 struct procdata *pd = NULL;
210 if (!*((struct log_data **) file->private_data)) {
211 if (file->f_flags & O_NONBLOCK)
214 /* sorry, but we need to search the card */
220 card = card->next; /* search next entry */
223 interruptible_sleep_on(&(pd->rd_queue));
228 if (!(inf = *((struct log_data **) file->private_data)))
231 inf->usage_cnt--; /* new usage count */
232 file->private_data = &inf->next; /* next structure */
233 if ((len = strlen(inf->log_start)) <= count) {
234 if (copy_to_user(buf, inf->log_start, len))
240 } /* hysdn_log_read */
246 hysdn_log_open(struct inode *ino, struct file *filep)
249 struct procdata *pd = NULL;
256 if (pd->log == PDE(ino))
258 card = card->next; /* search next entry */
262 return (-ENODEV); /* device is unknown/invalid */
264 filep->private_data = card; /* remember our own card */
266 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
267 /* write only access -> write log level only */
268 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
270 /* read access -> log/debug read */
271 spin_lock_irqsave(&card->hysdn_lock, flags);
274 filep->private_data = &pd->log_tail->next;
276 filep->private_data = &pd->log_head;
277 spin_unlock_irqrestore(&card->hysdn_lock, flags);
278 } else { /* simultaneous read/write access forbidden ! */
280 return (-EPERM); /* no permission this time */
283 return nonseekable_open(ino, filep);
284 } /* hysdn_log_open */
286 /*******************************************************************************/
287 /* close a cardlog file. If the file has been opened for exclusive write it is */
288 /* assumed as pof data input and the pof loader is noticed about. */
289 /* Otherwise file is handled as log output. In this case the interface usage */
290 /* count is decremented and all buffers are noticed of closing. If this file */
291 /* was the last one to be closed, all buffers are freed. */
292 /*******************************************************************************/
294 hysdn_log_close(struct inode *ino, struct file *filep)
296 struct log_data *inf;
301 spinlock_t hysdn_lock = SPIN_LOCK_UNLOCKED;
304 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
305 /* write only access -> write debug level written */
306 retval = 0; /* success */
308 /* read access -> log/debug read, mark one further file as closed */
311 spin_lock_irqsave(&hysdn_lock, flags);
312 inf = *((struct log_data **) filep->private_data); /* get first log entry */
314 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
316 /* no info available -> search card */
320 if (pd->log == PDE(ino))
322 card = card->next; /* search next entry */
325 pd = card->proclog; /* pointer to procfs log */
328 pd->if_used--; /* decrement interface usage count by one */
331 inf->usage_cnt--; /* decrement usage count for buffers */
334 spin_unlock_irqrestore(&hysdn_lock, flags);
337 if (pd->if_used <= 0) /* delete buffers if last file closed */
338 while (pd->log_head) {
340 pd->log_head = pd->log_head->next;
347 } /* hysdn_log_close */
349 /*************************************************/
350 /* select/poll routine to be able using select() */
351 /*************************************************/
353 hysdn_log_poll(struct file *file, poll_table * wait)
355 unsigned int mask = 0;
356 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
358 struct procdata *pd = NULL;
360 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
361 return (mask); /* no polling for write supported */
363 /* we need to search the card */
369 card = card->next; /* search next entry */
372 return (mask); /* card not found */
374 poll_wait(file, &(pd->rd_queue), wait);
376 if (*((struct log_data **) file->private_data))
377 mask |= POLLIN | POLLRDNORM;
380 } /* hysdn_log_poll */
382 /**************************************************/
383 /* table for log filesystem functions defined above. */
384 /**************************************************/
385 static const struct file_operations log_fops =
388 .read = hysdn_log_read,
389 .write = hysdn_log_write,
390 .poll = hysdn_log_poll,
391 .open = hysdn_log_open,
392 .release = hysdn_log_close,
396 /***********************************************************************************/
397 /* hysdn_proclog_init is called when the module is loaded after creating the cards */
399 /***********************************************************************************/
401 hysdn_proclog_init(hysdn_card * card)
405 /* create a cardlog proc entry */
407 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
408 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
409 if ((pd->log = create_proc_entry(pd->log_name, S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry)) != NULL) {
410 pd->log->proc_fops = &log_fops;
411 pd->log->owner = THIS_MODULE;
414 init_waitqueue_head(&(pd->rd_queue));
416 card->proclog = (void *) pd; /* remember procfs structure */
419 } /* hysdn_proclog_init */
421 /************************************************************************************/
422 /* hysdn_proclog_release is called when the module is unloaded and before the cards */
423 /* conf file is released */
424 /* The module counter is assumed to be 0 ! */
425 /************************************************************************************/
427 hysdn_proclog_release(hysdn_card * card)
431 if ((pd = (struct procdata *) card->proclog) != NULL) {
433 remove_proc_entry(pd->log_name, hysdn_proc_entry);
434 kfree(pd); /* release memory */
435 card->proclog = NULL;
437 } /* hysdn_proclog_release */