blob: 88e4f0ee073cf8efc7c95cbae29e6db405d319a8 [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 */
47 int volatile del_lock; /* lock for delete operations */
Andrew Mortonc721bcc2006-03-25 03:07:04 -080048 unsigned char logtmp[LOG_MAX_LINELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 wait_queue_head_t rd_queue;
50};
51
52
53/**********************************************/
54/* log function for cards error log interface */
55/**********************************************/
56void
Joe Perches475be4d2012-02-19 19:52:38 -080057hysdn_card_errlog(hysdn_card *card, tErrLogEntry *logp, int maxsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 char buf[ERRLOG_TEXT_SIZE + 40];
60
61 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
62 put_log_buffer(card, buf); /* output the string */
63} /* hysdn_card_errlog */
64
65/***************************************************/
66/* Log function using format specifiers for output */
67/***************************************************/
68void
Joe Perches475be4d2012-02-19 19:52:38 -080069hysdn_addlog(hysdn_card *card, char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 struct procdata *pd = card->proclog;
72 char *cp;
73 va_list args;
74
75 if (!pd)
76 return; /* log structure non existent */
77
78 cp = pd->logtmp;
79 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
80
81 va_start(args, fmt);
82 cp += vsprintf(cp, fmt, args);
83 va_end(args);
84 *cp++ = '\n';
85 *cp = 0;
86
87 if (card->debug_flags & DEB_OUT_SYSLOG)
88 printk(KERN_INFO "%s", pd->logtmp);
89 else
90 put_log_buffer(card, pd->logtmp);
91
92} /* hysdn_addlog */
93
94/********************************************/
95/* put an log buffer into the log queue. */
96/* This buffer will be kept until all files */
97/* opened for read got the contents. */
98/* Flushes buffers not longer in use. */
99/********************************************/
Adrian Bunkaade0e82005-06-28 20:44:56 -0700100static void
Joe Perches475be4d2012-02-19 19:52:38 -0800101put_log_buffer(hysdn_card *card, char *cp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct log_data *ib;
104 struct procdata *pd = card->proclog;
105 int i;
106 unsigned long flags;
107
108 if (!pd)
109 return;
110 if (!cp)
111 return;
112 if (!*cp)
113 return;
114 if (pd->if_used <= 0)
115 return; /* no open file for read */
116
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800117 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
Joe Perches475be4d2012-02-19 19:52:38 -0800118 return; /* no memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 strcpy(ib->log_start, cp); /* set output string */
120 ib->next = NULL;
121 ib->proc_ctrl = pd; /* point to own control structure */
Amol Lad0d9ba862006-10-17 00:10:36 -0700122 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ib->usage_cnt = pd->if_used;
124 if (!pd->log_head)
125 pd->log_head = ib; /* new head */
126 else
127 pd->log_tail->next = ib; /* follows existing messages */
128 pd->log_tail = ib; /* new tail */
129 i = pd->del_lock++; /* get lock state */
Amol Lad0d9ba862006-10-17 00:10:36 -0700130 spin_unlock_irqrestore(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* delete old entrys */
133 if (!i)
134 while (pd->log_head->next) {
135 if ((pd->log_head->usage_cnt <= 0) &&
136 (pd->log_head->next->usage_cnt <= 0)) {
137 ib = pd->log_head;
138 pd->log_head = pd->log_head->next;
139 kfree(ib);
140 } else
141 break;
142 } /* pd->log_head->next */
143 pd->del_lock--; /* release lock level */
144 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
145} /* put_log_buffer */
146
147
148/******************************/
149/* file operations and tables */
150/******************************/
151
152/****************************************/
153/* write log file -> set log level bits */
154/****************************************/
155static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -0800156hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Andy Shevchenko3944ad62010-07-15 02:37:19 +0000158 int rc;
Joe Perches54cbb1c2010-07-12 10:50:02 +0000159 hysdn_card *card = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Peter Hüwe8831a3f2012-04-14 13:42:59 +0000161 rc = kstrtoul_from_user(buf, count, 0, &card->debug_flags);
Alexey Dobriyan19eccc22011-03-27 02:58:35 +0000162 if (rc < 0)
163 return rc;
164 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return (count);
166} /* hysdn_log_write */
167
168/******************/
169/* read log file */
170/******************/
171static ssize_t
Joe Perches475be4d2012-02-19 19:52:38 -0800172hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct log_data *inf;
175 int len;
Josef Sipek4482dfa2006-12-08 02:37:13 -0800176 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct procdata *pd = NULL;
178 hysdn_card *card;
179
180 if (!*((struct log_data **) file->private_data)) {
181 if (file->f_flags & O_NONBLOCK)
182 return (-EAGAIN);
183
184 /* sorry, but we need to search the card */
185 card = card_root;
186 while (card) {
187 pd = card->proclog;
188 if (pd->log == pde)
189 break;
190 card = card->next; /* search next entry */
191 }
192 if (card)
193 interruptible_sleep_on(&(pd->rd_queue));
194 else
195 return (-EAGAIN);
196
197 }
198 if (!(inf = *((struct log_data **) file->private_data)))
199 return (0);
200
201 inf->usage_cnt--; /* new usage count */
202 file->private_data = &inf->next; /* next structure */
203 if ((len = strlen(inf->log_start)) <= count) {
204 if (copy_to_user(buf, inf->log_start, len))
205 return -EFAULT;
206 *off += len;
207 return (len);
208 }
209 return (0);
210} /* hysdn_log_read */
211
212/******************/
213/* open log file */
214/******************/
215static int
216hysdn_log_open(struct inode *ino, struct file *filep)
217{
218 hysdn_card *card;
219 struct procdata *pd = NULL;
Andrew Mortonc721bcc2006-03-25 03:07:04 -0800220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Arnd Bergmann76a64922010-07-11 11:18:53 +0000222 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 card = card_root;
224 while (card) {
225 pd = card->proclog;
226 if (pd->log == PDE(ino))
227 break;
228 card = card->next; /* search next entry */
229 }
230 if (!card) {
Arnd Bergmann76a64922010-07-11 11:18:53 +0000231 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return (-ENODEV); /* device is unknown/invalid */
233 }
234 filep->private_data = card; /* remember our own card */
235
236 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
237 /* write only access -> write log level only */
238 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
239
240 /* read access -> log/debug read */
Amol Lad0d9ba862006-10-17 00:10:36 -0700241 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pd->if_used++;
243 if (pd->log_head)
244 filep->private_data = &pd->log_tail->next;
245 else
246 filep->private_data = &pd->log_head;
Amol Lad0d9ba862006-10-17 00:10:36 -0700247 spin_unlock_irqrestore(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 } else { /* simultaneous read/write access forbidden ! */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000249 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return (-EPERM); /* no permission this time */
251 }
Arnd Bergmann76a64922010-07-11 11:18:53 +0000252 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return nonseekable_open(ino, filep);
254} /* hysdn_log_open */
255
256/*******************************************************************************/
257/* close a cardlog file. If the file has been opened for exclusive write it is */
258/* assumed as pof data input and the pof loader is noticed about. */
259/* Otherwise file is handled as log output. In this case the interface usage */
260/* count is decremented and all buffers are noticed of closing. If this file */
261/* was the last one to be closed, all buffers are freed. */
262/*******************************************************************************/
263static int
264hysdn_log_close(struct inode *ino, struct file *filep)
265{
266 struct log_data *inf;
267 struct procdata *pd;
268 hysdn_card *card;
269 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Arnd Bergmann76a64922010-07-11 11:18:53 +0000271 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
273 /* write only access -> write debug level written */
274 retval = 0; /* success */
275 } else {
276 /* read access -> log/debug read, mark one further file as closed */
277
278 pd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 inf = *((struct log_data **) filep->private_data); /* get first log entry */
280 if (inf)
281 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
282 else {
283 /* no info available -> search card */
284 card = card_root;
285 while (card) {
286 pd = card->proclog;
287 if (pd->log == PDE(ino))
288 break;
289 card = card->next; /* search next entry */
290 }
291 if (card)
292 pd = card->proclog; /* pointer to procfs log */
293 }
294 if (pd)
295 pd->if_used--; /* decrement interface usage count by one */
296
297 while (inf) {
298 inf->usage_cnt--; /* decrement usage count for buffers */
299 inf = inf->next;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (pd)
303 if (pd->if_used <= 0) /* delete buffers if last file closed */
304 while (pd->log_head) {
305 inf = pd->log_head;
306 pd->log_head = pd->log_head->next;
307 kfree(inf);
308 }
309 } /* read access */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000310 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return (retval);
313} /* hysdn_log_close */
314
315/*************************************************/
316/* select/poll routine to be able using select() */
317/*************************************************/
318static unsigned int
Joe Perches475be4d2012-02-19 19:52:38 -0800319hysdn_log_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned int mask = 0;
Josef Sipek4482dfa2006-12-08 02:37:13 -0800322 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 hysdn_card *card;
324 struct procdata *pd = NULL;
325
326 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
327 return (mask); /* no polling for write supported */
328
329 /* we need to search the card */
330 card = card_root;
331 while (card) {
332 pd = card->proclog;
333 if (pd->log == pde)
334 break;
335 card = card->next; /* search next entry */
336 }
337 if (!card)
338 return (mask); /* card not found */
339
340 poll_wait(file, &(pd->rd_queue), wait);
341
342 if (*((struct log_data **) file->private_data))
343 mask |= POLLIN | POLLRDNORM;
344
345 return mask;
346} /* hysdn_log_poll */
347
348/**************************************************/
349/* table for log filesystem functions defined above. */
350/**************************************************/
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800351static const struct file_operations log_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Denis V. Lunevac41cfd2008-04-29 01:02:30 -0700353 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .llseek = no_llseek,
355 .read = hysdn_log_read,
356 .write = hysdn_log_write,
357 .poll = hysdn_log_poll,
358 .open = hysdn_log_open,
Joe Perches475be4d2012-02-19 19:52:38 -0800359 .release = hysdn_log_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
362
363/***********************************************************************************/
364/* hysdn_proclog_init is called when the module is loaded after creating the cards */
365/* conf files. */
366/***********************************************************************************/
367int
Joe Perches475be4d2012-02-19 19:52:38 -0800368hysdn_proclog_init(hysdn_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct procdata *pd;
371
372 /* create a cardlog proc entry */
373
Burman Yan41f96932006-12-08 02:39:35 -0800374 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
Denis V. Lunevac41cfd2008-04-29 01:02:30 -0700376 pd->log = proc_create(pd->log_name,
Joe Perches475be4d2012-02-19 19:52:38 -0800377 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
378 &log_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 init_waitqueue_head(&(pd->rd_queue));
381
382 card->proclog = (void *) pd; /* remember procfs structure */
383 }
384 return (0);
385} /* hysdn_proclog_init */
386
387/************************************************************************************/
388/* hysdn_proclog_release is called when the module is unloaded and before the cards */
389/* conf file is released */
390/* The module counter is assumed to be 0 ! */
391/************************************************************************************/
392void
Joe Perches475be4d2012-02-19 19:52:38 -0800393hysdn_proclog_release(hysdn_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct procdata *pd;
396
397 if ((pd = (struct procdata *) card->proclog) != NULL) {
398 if (pd->log)
399 remove_proc_entry(pd->log_name, hysdn_proc_entry);
400 kfree(pd); /* release memory */
401 card->proclog = NULL;
402 }
403} /* hysdn_proclog_release */