blob: 7003698e667dc1a16e27c8023e93cbaca406ec46 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "hysdn_defs.h"
21
22/* the proc subdir for the interface is defined in the procconf module */
23extern struct proc_dir_entry *hysdn_proc_entry;
24
Arnd Bergmann76a64922010-07-11 11:18:53 +000025static DEFINE_MUTEX(hysdn_log_mutex);
Adrian Bunkaade0e82005-06-28 20:44:56 -070026static void put_log_buffer(hysdn_card * card, char *cp);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*************************************************/
29/* structure keeping ascii log for device output */
30/*************************************************/
31struct log_data {
32 struct log_data *next;
Andrew Mortonc721bcc2006-03-25 03:07:04 -080033 unsigned long usage_cnt;/* number of files still to work */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 void *proc_ctrl; /* pointer to own control procdata structure */
35 char log_start[2]; /* log string start (final len aligned by size) */
36};
37
38/**********************************************/
39/* structure holding proc entrys for one card */
40/**********************************************/
41struct procdata {
42 struct proc_dir_entry *log; /* log entry */
43 char log_name[15]; /* log filename */
44 struct log_data *log_head, *log_tail; /* head and tail for queue */
45 int if_used; /* open count for interface */
46 int volatile del_lock; /* lock for delete operations */
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
56hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
57{
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
68hysdn_addlog(hysdn_card * card, char *fmt,...)
69{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100put_log_buffer(hysdn_card * card, char *cp)
101{
102 struct log_data *ib;
103 struct procdata *pd = card->proclog;
104 int i;
105 unsigned long flags;
106
107 if (!pd)
108 return;
109 if (!cp)
110 return;
111 if (!*cp)
112 return;
113 if (pd->if_used <= 0)
114 return; /* no open file for read */
115
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800116 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return; /* no memory */
118 strcpy(ib->log_start, cp); /* set output string */
119 ib->next = NULL;
120 ib->proc_ctrl = pd; /* point to own control structure */
Amol Lad0d9ba862006-10-17 00:10:36 -0700121 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 ib->usage_cnt = pd->if_used;
123 if (!pd->log_head)
124 pd->log_head = ib; /* new head */
125 else
126 pd->log_tail->next = ib; /* follows existing messages */
127 pd->log_tail = ib; /* new tail */
128 i = pd->del_lock++; /* get lock state */
Amol Lad0d9ba862006-10-17 00:10:36 -0700129 spin_unlock_irqrestore(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* delete old entrys */
132 if (!i)
133 while (pd->log_head->next) {
134 if ((pd->log_head->usage_cnt <= 0) &&
135 (pd->log_head->next->usage_cnt <= 0)) {
136 ib = pd->log_head;
137 pd->log_head = pd->log_head->next;
138 kfree(ib);
139 } else
140 break;
141 } /* pd->log_head->next */
142 pd->del_lock--; /* release lock level */
143 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
144} /* put_log_buffer */
145
146
147/******************************/
148/* file operations and tables */
149/******************************/
150
151/****************************************/
152/* write log file -> set log level bits */
153/****************************************/
154static ssize_t
155hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
156{
Andrew Mortonc721bcc2006-03-25 03:07:04 -0800157 unsigned long u = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int found = 0;
Andrew Mortonc721bcc2006-03-25 03:07:04 -0800159 unsigned char *cp, valbuf[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 long base = 10;
Joe Perches54cbb1c2010-07-12 10:50:02 +0000161 hysdn_card *card = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (count > (sizeof(valbuf) - 1))
164 count = sizeof(valbuf) - 1; /* limit length */
165 if (copy_from_user(valbuf, buf, count))
166 return (-EFAULT); /* copy failed */
167
168 valbuf[count] = 0; /* terminating 0 */
169 cp = valbuf;
170 if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
171 cp += 2; /* pointer after hex modifier */
172 base = 16;
173 }
174 /* scan the input for debug flags */
175 while (*cp) {
176 if ((*cp >= '0') && (*cp <= '9')) {
177 found = 1;
178 u *= base; /* adjust to next digit */
179 u += *cp++ - '0';
180 continue;
181 }
182 if (base != 16)
183 break; /* end of number */
184
185 if ((*cp >= 'a') && (*cp <= 'f')) {
186 found = 1;
187 u *= base; /* adjust to next digit */
188 u += *cp++ - 'a' + 10;
189 continue;
190 }
191 break; /* terminated */
192 }
193
194 if (found) {
195 card->debug_flags = u; /* remember debug flags */
196 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
197 }
198 return (count);
199} /* hysdn_log_write */
200
201/******************/
202/* read log file */
203/******************/
204static ssize_t
205hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
206{
207 struct log_data *inf;
208 int len;
Josef Sipek4482dfa2006-12-08 02:37:13 -0800209 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct procdata *pd = NULL;
211 hysdn_card *card;
212
213 if (!*((struct log_data **) file->private_data)) {
214 if (file->f_flags & O_NONBLOCK)
215 return (-EAGAIN);
216
217 /* sorry, but we need to search the card */
218 card = card_root;
219 while (card) {
220 pd = card->proclog;
221 if (pd->log == pde)
222 break;
223 card = card->next; /* search next entry */
224 }
225 if (card)
226 interruptible_sleep_on(&(pd->rd_queue));
227 else
228 return (-EAGAIN);
229
230 }
231 if (!(inf = *((struct log_data **) file->private_data)))
232 return (0);
233
234 inf->usage_cnt--; /* new usage count */
235 file->private_data = &inf->next; /* next structure */
236 if ((len = strlen(inf->log_start)) <= count) {
237 if (copy_to_user(buf, inf->log_start, len))
238 return -EFAULT;
239 *off += len;
240 return (len);
241 }
242 return (0);
243} /* hysdn_log_read */
244
245/******************/
246/* open log file */
247/******************/
248static int
249hysdn_log_open(struct inode *ino, struct file *filep)
250{
251 hysdn_card *card;
252 struct procdata *pd = NULL;
Andrew Mortonc721bcc2006-03-25 03:07:04 -0800253 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Arnd Bergmann76a64922010-07-11 11:18:53 +0000255 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 card = card_root;
257 while (card) {
258 pd = card->proclog;
259 if (pd->log == PDE(ino))
260 break;
261 card = card->next; /* search next entry */
262 }
263 if (!card) {
Arnd Bergmann76a64922010-07-11 11:18:53 +0000264 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return (-ENODEV); /* device is unknown/invalid */
266 }
267 filep->private_data = card; /* remember our own card */
268
269 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
270 /* write only access -> write log level only */
271 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
272
273 /* read access -> log/debug read */
Amol Lad0d9ba862006-10-17 00:10:36 -0700274 spin_lock_irqsave(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 pd->if_used++;
276 if (pd->log_head)
277 filep->private_data = &pd->log_tail->next;
278 else
279 filep->private_data = &pd->log_head;
Amol Lad0d9ba862006-10-17 00:10:36 -0700280 spin_unlock_irqrestore(&card->hysdn_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else { /* simultaneous read/write access forbidden ! */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000282 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return (-EPERM); /* no permission this time */
284 }
Arnd Bergmann76a64922010-07-11 11:18:53 +0000285 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return nonseekable_open(ino, filep);
287} /* hysdn_log_open */
288
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/*******************************************************************************/
296static int
297hysdn_log_close(struct inode *ino, struct file *filep)
298{
299 struct log_data *inf;
300 struct procdata *pd;
301 hysdn_card *card;
302 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Arnd Bergmann76a64922010-07-11 11:18:53 +0000304 mutex_lock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
306 /* write only access -> write debug level written */
307 retval = 0; /* success */
308 } else {
309 /* read access -> log/debug read, mark one further file as closed */
310
311 pd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 inf = *((struct log_data **) filep->private_data); /* get first log entry */
313 if (inf)
314 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
315 else {
316 /* no info available -> search card */
317 card = card_root;
318 while (card) {
319 pd = card->proclog;
320 if (pd->log == PDE(ino))
321 break;
322 card = card->next; /* search next entry */
323 }
324 if (card)
325 pd = card->proclog; /* pointer to procfs log */
326 }
327 if (pd)
328 pd->if_used--; /* decrement interface usage count by one */
329
330 while (inf) {
331 inf->usage_cnt--; /* decrement usage count for buffers */
332 inf = inf->next;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (pd)
336 if (pd->if_used <= 0) /* delete buffers if last file closed */
337 while (pd->log_head) {
338 inf = pd->log_head;
339 pd->log_head = pd->log_head->next;
340 kfree(inf);
341 }
342 } /* read access */
Arnd Bergmann76a64922010-07-11 11:18:53 +0000343 mutex_unlock(&hysdn_log_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return (retval);
346} /* hysdn_log_close */
347
348/*************************************************/
349/* select/poll routine to be able using select() */
350/*************************************************/
351static unsigned int
352hysdn_log_poll(struct file *file, poll_table * wait)
353{
354 unsigned int mask = 0;
Josef Sipek4482dfa2006-12-08 02:37:13 -0800355 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 hysdn_card *card;
357 struct procdata *pd = NULL;
358
359 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
360 return (mask); /* no polling for write supported */
361
362 /* we need to search the card */
363 card = card_root;
364 while (card) {
365 pd = card->proclog;
366 if (pd->log == pde)
367 break;
368 card = card->next; /* search next entry */
369 }
370 if (!card)
371 return (mask); /* card not found */
372
373 poll_wait(file, &(pd->rd_queue), wait);
374
375 if (*((struct log_data **) file->private_data))
376 mask |= POLLIN | POLLRDNORM;
377
378 return mask;
379} /* hysdn_log_poll */
380
381/**************************************************/
382/* table for log filesystem functions defined above. */
383/**************************************************/
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800384static const struct file_operations log_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Denis V. Lunevac41cfd2008-04-29 01:02:30 -0700386 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .llseek = no_llseek,
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,
393};
394
395
396/***********************************************************************************/
397/* hysdn_proclog_init is called when the module is loaded after creating the cards */
398/* conf files. */
399/***********************************************************************************/
400int
401hysdn_proclog_init(hysdn_card * card)
402{
403 struct procdata *pd;
404
405 /* create a cardlog proc entry */
406
Burman Yan41f96932006-12-08 02:39:35 -0800407 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
Denis V. Lunevac41cfd2008-04-29 01:02:30 -0700409 pd->log = proc_create(pd->log_name,
410 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
411 &log_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 init_waitqueue_head(&(pd->rd_queue));
414
415 card->proclog = (void *) pd; /* remember procfs structure */
416 }
417 return (0);
418} /* hysdn_proclog_init */
419
420/************************************************************************************/
421/* hysdn_proclog_release is called when the module is unloaded and before the cards */
422/* conf file is released */
423/* The module counter is assumed to be 0 ! */
424/************************************************************************************/
425void
426hysdn_proclog_release(hysdn_card * card)
427{
428 struct procdata *pd;
429
430 if ((pd = (struct procdata *) card->proclog) != NULL) {
431 if (pd->log)
432 remove_proc_entry(pd->log_name, hysdn_proc_entry);
433 kfree(pd); /* release memory */
434 card->proclog = NULL;
435 }
436} /* hysdn_proclog_release */