blob: aaca0b3d662eb18bda0848652c309985d5f6ad0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
13#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/poll.h>
15#include <linux/proc_fs.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040016#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Arnd Bergmann76a64922010-07-11 11:18:53 +000018#include <linux/mutex.h>
Andy Shevchenko3944ad62010-07-15 02:37:19 +000019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "hysdn_defs.h"
22
23/* the proc subdir for the interface is defined in the procconf module */
24extern struct proc_dir_entry *hysdn_proc_entry;
25
Arnd Bergmann76a64922010-07-11 11:18:53 +000026static DEFINE_MUTEX(hysdn_log_mutex);
Joe Perches475be4d2012-02-19 19:52:38 -080027static void put_log_buffer(hysdn_card *card, char *cp);
Adrian Bunkaade0e82005-06-28 20:44:56 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*************************************************/
30/* structure keeping ascii log for device output */
31/*************************************************/
32struct log_data {
33 struct log_data *next;
Andrew Mortonc721bcc2006-03-25 03:07:04 -080034 unsigned long usage_cnt;/* number of files still to work */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 void *proc_ctrl; /* pointer to own control procdata structure */
36 char log_start[2]; /* log string start (final len aligned by size) */
37};
38
39/**********************************************/
40/* structure holding proc entrys for one card */
41/**********************************************/
42struct procdata {
43 struct proc_dir_entry *log; /* log entry */
44 char log_name[15]; /* log filename */
45 struct log_data *log_head, *log_tail; /* head and tail for queue */
46 int if_used; /* open count for interface */
Andrew Mortonc721bcc2006-03-25 03:07:04 -080047 unsigned char logtmp[LOG_MAX_LINELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 wait_queue_head_t rd_queue;
49};
50
51
52/**********************************************/
53/* log function for cards error log interface */
54/**********************************************/
55void
Joe Perches475be4d2012-02-19 19:52:38 -080056hysdn_card_errlog(hysdn_card *card, tErrLogEntry *logp, int maxsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 char buf[ERRLOG_TEXT_SIZE + 40];
59
60 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
61 put_log_buffer(card, buf); /* output the string */
62} /* hysdn_card_errlog */
63
64/***************************************************/
65/* Log function using format specifiers for output */
66/***************************************************/
67void
Joe Perches475be4d2012-02-19 19:52:38 -080068hysdn_addlog(hysdn_card *card, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct procdata *pd = card->proclog;
71 char *cp;
72 va_list args;
73
74 if (!pd)
75 return; /* log structure non existent */
76
77 cp = pd->logtmp;
78 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
79
80 va_start(args, fmt);
81 cp += vsprintf(cp, fmt, args);
82 va_end(args);
83 *cp++ = '\n';
84 *cp = 0;
85
86 if (card->debug_flags & DEB_OUT_SYSLOG)
87 printk(KERN_INFO "%s", pd->logtmp);
88 else
89 put_log_buffer(card, pd->logtmp);
90
91} /* hysdn_addlog */
92
93/********************************************/
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/********************************************/
Adrian Bunkaade0e82005-06-28 20:44:56 -070099static void
Joe Perches475be4d2012-02-19 19:52:38 -0800100put_log_buffer(hysdn_card *card, char *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct log_data *ib;
103 struct procdata *pd = card->proclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long flags;
105
106 if (!pd)
107 return;
108 if (!cp)
109 return;
110 if (!*cp)
111 return;
112 if (pd->if_used <= 0)
113 return; /* no open file for read */
114
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800115 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
Joe Perches475be4d2012-02-19 19:52:38 -0800116 return; /* no memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 strcpy(ib->log_start, cp); /* set output string */
118 ib->next = NULL;
119 ib->proc_ctrl = pd; /* point to own control structure */
Amol Lad0d9ba862006-10-17 00:10:36 -0700120 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 ib->usage_cnt = pd->if_used;
122 if (!pd->log_head)
123 pd->log_head = ib; /* new head */
124 else
125 pd->log_tail->next = ib; /* follows existing messages */
126 pd->log_tail = ib; /* new tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* delete old entrys */
Anton Volkovb925ef32017-08-07 15:54:14 +0300129 while (pd->log_head->next) {
130 if ((pd->log_head->usage_cnt <= 0) &&
131 (pd->log_head->next->usage_cnt <= 0)) {
132 ib = pd->log_head;
133 pd->log_head = pd->log_head->next;
134 kfree(ib);
135 } else {
136 break;
137 }
138 } /* pd->log_head->next */
139
140 spin_unlock_irqrestore(&card->hysdn_lock, flags);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
143} /* put_log_buffer */
144
145
146/******************************/
147/* file operations and tables */
148/******************************/
149
150/****************************************/
151/* write log file -> set log level bits */
152/****************************************/
153static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -0800154hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Andy Shevchenko3944ad62010-07-15 02:37:19 +0000156 int rc;
Joe Perches54cbb1c2010-07-12 10:50:02 +0000157 hysdn_card *card = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Peter Hüwe8831a3f2012-04-14 13:42:59 +0000159 rc = kstrtoul_from_user(buf, count, 0, &card->debug_flags);
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000160 if (rc < 0)
161 return rc;
162 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return (count);
164} /* hysdn_log_write */
165
166/******************/
167/* read log file */
168/******************/
169static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -0800170hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct log_data *inf;
173 int len;
Al Virod9dda782013-03-31 18:16:14 -0400174 hysdn_card *card = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Arnd Bergmannc11da832014-02-26 12:01:54 +0100176 if (!(inf = *((struct log_data **) file->private_data))) {
Al Viro4d8e8d22013-03-28 18:56:21 -0400177 struct procdata *pd = card->proclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (file->f_flags & O_NONBLOCK)
179 return (-EAGAIN);
180
Arnd Bergmannc11da832014-02-26 12:01:54 +0100181 wait_event_interruptible(pd->rd_queue, (inf =
182 *((struct log_data **) file->private_data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Arnd Bergmannc11da832014-02-26 12:01:54 +0100184 if (!inf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return (0);
186
187 inf->usage_cnt--; /* new usage count */
188 file->private_data = &inf->next; /* next structure */
189 if ((len = strlen(inf->log_start)) <= count) {
190 if (copy_to_user(buf, inf->log_start, len))
191 return -EFAULT;
192 *off += len;
193 return (len);
194 }
195 return (0);
196} /* hysdn_log_read */
197
198/******************/
199/* open log file */
200/******************/
201static int
202hysdn_log_open(struct inode *ino, struct file *filep)
203{
Al Virod9dda782013-03-31 18:16:14 -0400204 hysdn_card *card = PDE_DATA(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Arnd Bergmann76a64922010-07-11 11:18:53 +0000206 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
208 /* write only access -> write log level only */
Al Viro4d8e8d22013-03-28 18:56:21 -0400209 filep->private_data = card; /* remember our own card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
Al Viro4d8e8d22013-03-28 18:56:21 -0400211 struct procdata *pd = card->proclog;
212 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* read access -> log/debug read */
Amol Lad0d9ba862006-10-17 00:10:36 -0700215 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 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;
Amol Lad0d9ba862006-10-17 00:10:36 -0700221 spin_unlock_irqrestore(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else { /* simultaneous read/write access forbidden ! */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000223 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return (-EPERM); /* no permission this time */
225 }
Arnd Bergmann76a64922010-07-11 11:18:53 +0000226 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return nonseekable_open(ino, filep);
228} /* hysdn_log_open */
229
230/*******************************************************************************/
231/* close a cardlog file. If the file has been opened for exclusive write it is */
232/* assumed as pof data input and the pof loader is noticed about. */
233/* Otherwise file is handled as log output. In this case the interface usage */
234/* count is decremented and all buffers are noticed of closing. If this file */
235/* was the last one to be closed, all buffers are freed. */
236/*******************************************************************************/
237static int
238hysdn_log_close(struct inode *ino, struct file *filep)
239{
240 struct log_data *inf;
241 struct procdata *pd;
242 hysdn_card *card;
243 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Arnd Bergmann76a64922010-07-11 11:18:53 +0000245 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 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 */
Al Virod9dda782013-03-31 18:16:14 -0400257 card = PDE_DATA(file_inode(filep));
Al Viro4d8e8d22013-03-28 18:56:21 -0400258 pd = card->proclog; /* pointer to procfs log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 if (pd)
261 pd->if_used--; /* decrement interface usage count by one */
262
263 while (inf) {
264 inf->usage_cnt--; /* decrement usage count for buffers */
265 inf = inf->next;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (pd)
269 if (pd->if_used <= 0) /* delete buffers if last file closed */
270 while (pd->log_head) {
271 inf = pd->log_head;
272 pd->log_head = pd->log_head->next;
273 kfree(inf);
274 }
275 } /* read access */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000276 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return (retval);
279} /* hysdn_log_close */
280
281/*************************************************/
282/* select/poll routine to be able using select() */
283/*************************************************/
284static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -0800285hysdn_log_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned int mask = 0;
Al Virod9dda782013-03-31 18:16:14 -0400288 hysdn_card *card = PDE_DATA(file_inode(file));
Al Viro4d8e8d22013-03-28 18:56:21 -0400289 struct procdata *pd = card->proclog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
292 return (mask); /* no polling for write supported */
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 poll_wait(file, &(pd->rd_queue), wait);
295
296 if (*((struct log_data **) file->private_data))
297 mask |= POLLIN | POLLRDNORM;
298
299 return mask;
300} /* hysdn_log_poll */
301
302/**************************************************/
303/* table for log filesystem functions defined above. */
304/**************************************************/
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800305static const struct file_operations log_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Denis V. Lunevac41cfd2008-04-29 01:02:30 -0700307 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .llseek = no_llseek,
309 .read = hysdn_log_read,
310 .write = hysdn_log_write,
311 .poll = hysdn_log_poll,
312 .open = hysdn_log_open,
Joe Perches475be4d2012-02-19 19:52:38 -0800313 .release = hysdn_log_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
316
317/***********************************************************************************/
318/* hysdn_proclog_init is called when the module is loaded after creating the cards */
319/* conf files. */
320/***********************************************************************************/
321int
Joe Perches475be4d2012-02-19 19:52:38 -0800322hysdn_proclog_init(hysdn_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct procdata *pd;
325
326 /* create a cardlog proc entry */
327
Burman Yan41f96932006-12-08 02:39:35 -0800328 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
Al Viro4d8e8d22013-03-28 18:56:21 -0400330 pd->log = proc_create_data(pd->log_name,
Joe Perches475be4d2012-02-19 19:52:38 -0800331 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
Al Viro4d8e8d22013-03-28 18:56:21 -0400332 &log_fops, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 init_waitqueue_head(&(pd->rd_queue));
335
336 card->proclog = (void *) pd; /* remember procfs structure */
337 }
338 return (0);
339} /* hysdn_proclog_init */
340
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/************************************************************************************/
346void
Joe Perches475be4d2012-02-19 19:52:38 -0800347hysdn_proclog_release(hysdn_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct procdata *pd;
350
351 if ((pd = (struct procdata *) card->proclog) != NULL) {
352 if (pd->log)
353 remove_proc_entry(pd->log_name, hysdn_proc_entry);
354 kfree(pd); /* release memory */
355 card->proclog = NULL;
356 }
357} /* hysdn_proclog_release */