Path: blob/master/drivers/isdn/hysdn/hysdn_proclog.c
17659 views
/* $Id: hysdn_proclog.c,v 1.9.6.3 2001/09/23 22:24:54 kai Exp $1*2* Linux driver for HYSDN cards, /proc/net filesystem log functions.3*4* Author Werner Cornelius ([email protected]) for Hypercope GmbH5* Copyright 1999 by Werner Cornelius ([email protected])6*7* This software may be used and distributed according to the terms8* of the GNU General Public License, incorporated herein by reference.9*10*/1112#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>1920#include "hysdn_defs.h"2122/* the proc subdir for the interface is defined in the procconf module */23extern struct proc_dir_entry *hysdn_proc_entry;2425static DEFINE_MUTEX(hysdn_log_mutex);26static void put_log_buffer(hysdn_card * card, char *cp);2728/*************************************************/29/* structure keeping ascii log for device output */30/*************************************************/31struct log_data {32struct log_data *next;33unsigned long usage_cnt;/* number of files still to work */34void *proc_ctrl; /* pointer to own control procdata structure */35char log_start[2]; /* log string start (final len aligned by size) */36};3738/**********************************************/39/* structure holding proc entrys for one card */40/**********************************************/41struct procdata {42struct proc_dir_entry *log; /* log entry */43char log_name[15]; /* log filename */44struct log_data *log_head, *log_tail; /* head and tail for queue */45int if_used; /* open count for interface */46int volatile del_lock; /* lock for delete operations */47unsigned char logtmp[LOG_MAX_LINELEN];48wait_queue_head_t rd_queue;49};505152/**********************************************/53/* log function for cards error log interface */54/**********************************************/55void56hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)57{58char buf[ERRLOG_TEXT_SIZE + 40];5960sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);61put_log_buffer(card, buf); /* output the string */62} /* hysdn_card_errlog */6364/***************************************************/65/* Log function using format specifiers for output */66/***************************************************/67void68hysdn_addlog(hysdn_card * card, char *fmt,...)69{70struct procdata *pd = card->proclog;71char *cp;72va_list args;7374if (!pd)75return; /* log structure non existent */7677cp = pd->logtmp;78cp += sprintf(cp, "HYSDN: card %d ", card->myid);7980va_start(args, fmt);81cp += vsprintf(cp, fmt, args);82va_end(args);83*cp++ = '\n';84*cp = 0;8586if (card->debug_flags & DEB_OUT_SYSLOG)87printk(KERN_INFO "%s", pd->logtmp);88else89put_log_buffer(card, pd->logtmp);9091} /* hysdn_addlog */9293/********************************************/94/* put an log buffer into the log queue. */95/* This buffer will be kept until all files */96/* opened for read got the contents. */97/* Flushes buffers not longer in use. */98/********************************************/99static void100put_log_buffer(hysdn_card * card, char *cp)101{102struct log_data *ib;103struct procdata *pd = card->proclog;104int i;105unsigned long flags;106107if (!pd)108return;109if (!cp)110return;111if (!*cp)112return;113if (pd->if_used <= 0)114return; /* no open file for read */115116if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))117return; /* no memory */118strcpy(ib->log_start, cp); /* set output string */119ib->next = NULL;120ib->proc_ctrl = pd; /* point to own control structure */121spin_lock_irqsave(&card->hysdn_lock, flags);122ib->usage_cnt = pd->if_used;123if (!pd->log_head)124pd->log_head = ib; /* new head */125else126pd->log_tail->next = ib; /* follows existing messages */127pd->log_tail = ib; /* new tail */128i = pd->del_lock++; /* get lock state */129spin_unlock_irqrestore(&card->hysdn_lock, flags);130131/* delete old entrys */132if (!i)133while (pd->log_head->next) {134if ((pd->log_head->usage_cnt <= 0) &&135(pd->log_head->next->usage_cnt <= 0)) {136ib = pd->log_head;137pd->log_head = pd->log_head->next;138kfree(ib);139} else140break;141} /* pd->log_head->next */142pd->del_lock--; /* release lock level */143wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */144} /* put_log_buffer */145146147/******************************/148/* file operations and tables */149/******************************/150151/****************************************/152/* write log file -> set log level bits */153/****************************************/154static ssize_t155hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)156{157int rc;158unsigned char valbuf[128];159hysdn_card *card = file->private_data;160161if (count > (sizeof(valbuf) - 1))162count = sizeof(valbuf) - 1; /* limit length */163if (copy_from_user(valbuf, buf, count))164return (-EFAULT); /* copy failed */165166valbuf[count] = 0; /* terminating 0 */167168rc = kstrtoul(valbuf, 0, &card->debug_flags);169if (rc < 0)170return rc;171hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);172return (count);173} /* hysdn_log_write */174175/******************/176/* read log file */177/******************/178static ssize_t179hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)180{181struct log_data *inf;182int len;183struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);184struct procdata *pd = NULL;185hysdn_card *card;186187if (!*((struct log_data **) file->private_data)) {188if (file->f_flags & O_NONBLOCK)189return (-EAGAIN);190191/* sorry, but we need to search the card */192card = card_root;193while (card) {194pd = card->proclog;195if (pd->log == pde)196break;197card = card->next; /* search next entry */198}199if (card)200interruptible_sleep_on(&(pd->rd_queue));201else202return (-EAGAIN);203204}205if (!(inf = *((struct log_data **) file->private_data)))206return (0);207208inf->usage_cnt--; /* new usage count */209file->private_data = &inf->next; /* next structure */210if ((len = strlen(inf->log_start)) <= count) {211if (copy_to_user(buf, inf->log_start, len))212return -EFAULT;213*off += len;214return (len);215}216return (0);217} /* hysdn_log_read */218219/******************/220/* open log file */221/******************/222static int223hysdn_log_open(struct inode *ino, struct file *filep)224{225hysdn_card *card;226struct procdata *pd = NULL;227unsigned long flags;228229mutex_lock(&hysdn_log_mutex);230card = card_root;231while (card) {232pd = card->proclog;233if (pd->log == PDE(ino))234break;235card = card->next; /* search next entry */236}237if (!card) {238mutex_unlock(&hysdn_log_mutex);239return (-ENODEV); /* device is unknown/invalid */240}241filep->private_data = card; /* remember our own card */242243if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {244/* write only access -> write log level only */245} else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {246247/* read access -> log/debug read */248spin_lock_irqsave(&card->hysdn_lock, flags);249pd->if_used++;250if (pd->log_head)251filep->private_data = &pd->log_tail->next;252else253filep->private_data = &pd->log_head;254spin_unlock_irqrestore(&card->hysdn_lock, flags);255} else { /* simultaneous read/write access forbidden ! */256mutex_unlock(&hysdn_log_mutex);257return (-EPERM); /* no permission this time */258}259mutex_unlock(&hysdn_log_mutex);260return nonseekable_open(ino, filep);261} /* hysdn_log_open */262263/*******************************************************************************/264/* close a cardlog file. If the file has been opened for exclusive write it is */265/* assumed as pof data input and the pof loader is noticed about. */266/* Otherwise file is handled as log output. In this case the interface usage */267/* count is decremented and all buffers are noticed of closing. If this file */268/* was the last one to be closed, all buffers are freed. */269/*******************************************************************************/270static int271hysdn_log_close(struct inode *ino, struct file *filep)272{273struct log_data *inf;274struct procdata *pd;275hysdn_card *card;276int retval = 0;277278mutex_lock(&hysdn_log_mutex);279if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {280/* write only access -> write debug level written */281retval = 0; /* success */282} else {283/* read access -> log/debug read, mark one further file as closed */284285pd = NULL;286inf = *((struct log_data **) filep->private_data); /* get first log entry */287if (inf)288pd = (struct procdata *) inf->proc_ctrl; /* still entries there */289else {290/* no info available -> search card */291card = card_root;292while (card) {293pd = card->proclog;294if (pd->log == PDE(ino))295break;296card = card->next; /* search next entry */297}298if (card)299pd = card->proclog; /* pointer to procfs log */300}301if (pd)302pd->if_used--; /* decrement interface usage count by one */303304while (inf) {305inf->usage_cnt--; /* decrement usage count for buffers */306inf = inf->next;307}308309if (pd)310if (pd->if_used <= 0) /* delete buffers if last file closed */311while (pd->log_head) {312inf = pd->log_head;313pd->log_head = pd->log_head->next;314kfree(inf);315}316} /* read access */317mutex_unlock(&hysdn_log_mutex);318319return (retval);320} /* hysdn_log_close */321322/*************************************************/323/* select/poll routine to be able using select() */324/*************************************************/325static unsigned int326hysdn_log_poll(struct file *file, poll_table * wait)327{328unsigned int mask = 0;329struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);330hysdn_card *card;331struct procdata *pd = NULL;332333if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)334return (mask); /* no polling for write supported */335336/* we need to search the card */337card = card_root;338while (card) {339pd = card->proclog;340if (pd->log == pde)341break;342card = card->next; /* search next entry */343}344if (!card)345return (mask); /* card not found */346347poll_wait(file, &(pd->rd_queue), wait);348349if (*((struct log_data **) file->private_data))350mask |= POLLIN | POLLRDNORM;351352return mask;353} /* hysdn_log_poll */354355/**************************************************/356/* table for log filesystem functions defined above. */357/**************************************************/358static const struct file_operations log_fops =359{360.owner = THIS_MODULE,361.llseek = no_llseek,362.read = hysdn_log_read,363.write = hysdn_log_write,364.poll = hysdn_log_poll,365.open = hysdn_log_open,366.release = hysdn_log_close,367};368369370/***********************************************************************************/371/* hysdn_proclog_init is called when the module is loaded after creating the cards */372/* conf files. */373/***********************************************************************************/374int375hysdn_proclog_init(hysdn_card * card)376{377struct procdata *pd;378379/* create a cardlog proc entry */380381if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {382sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);383pd->log = proc_create(pd->log_name,384S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,385&log_fops);386387init_waitqueue_head(&(pd->rd_queue));388389card->proclog = (void *) pd; /* remember procfs structure */390}391return (0);392} /* hysdn_proclog_init */393394/************************************************************************************/395/* hysdn_proclog_release is called when the module is unloaded and before the cards */396/* conf file is released */397/* The module counter is assumed to be 0 ! */398/************************************************************************************/399void400hysdn_proclog_release(hysdn_card * card)401{402struct procdata *pd;403404if ((pd = (struct procdata *) card->proclog) != NULL) {405if (pd->log)406remove_proc_entry(pd->log_name, hysdn_proc_entry);407kfree(pd); /* release memory */408card->proclog = NULL;409}410} /* hysdn_proclog_release */411412413