Import 2.3.48
[davej-history.git] / drivers / isdn / hysdn / hysdn_proclog.c
blob67a91cf0fcca1aa86879789893c5bb58f7a26edf
1 /* $Id: hysdn_proclog.c,v 1.2 2000/02/14 19:23:03 werner Exp $
3 * Linux driver for HYSDN cards, /proc/net filesystem log functions.
4 * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 * Copyright 1999 by Werner Cornelius (werner@titro.de)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * $Log: hysdn_proclog.c,v $
23 * Revision 1.2 2000/02/14 19:23:03 werner
25 * Changed handling of proc filesystem tables to a more portable version
27 * Revision 1.1 2000/02/10 19:45:18 werner
29 * Initial release
34 #define __NO_VERSION__
35 #include <linux/module.h>
36 #include <linux/version.h>
37 #include <linux/poll.h>
38 #include <linux/proc_fs.h>
39 #include <linux/pci.h>
41 #include "hysdn_defs.h"
43 static char *hysdn_proclog_revision = "$Revision: 1.2 $";
45 /* the proc subdir for the interface is defined in the procconf module */
46 extern struct proc_dir_entry *hysdn_proc_entry;
48 /*************************************************/
49 /* structure keeping ascii log for device output */
50 /*************************************************/
51 struct log_data {
52 struct log_data *next;
53 ulong usage_cnt; /* number of files still to work */
54 void *proc_ctrl; /* pointer to own control procdata structure */
55 char log_start[2]; /* log string start (final len aligned by size) */
58 /**********************************************/
59 /* structure holding proc entrys for one card */
60 /**********************************************/
61 struct procdata {
62 struct proc_dir_entry *log; /* log entry */
63 char log_name[15]; /* log filename */
64 struct log_data *log_head, *log_tail; /* head and tail for queue */
65 int if_used; /* open count for interface */
66 int volatile del_lock; /* lock for delete operations */
67 uchar logtmp[LOG_MAX_LINELEN];
68 wait_queue_head_t rd_queue;
72 /**********************************************/
73 /* log function for cards error log interface */
74 /**********************************************/
75 void
76 hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
78 char buf[ERRLOG_TEXT_SIZE + 40];
80 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
81 put_log_buffer(card, buf); /* output the string */
82 } /* hysdn_card_errlog */
84 /***************************************************/
85 /* Log function using format specifiers for output */
86 /***************************************************/
87 void
88 hysdn_addlog(hysdn_card * card, char *fmt,...)
90 struct procdata *pd = card->proclog;
91 char *cp;
92 va_list args;
94 if (!pd)
95 return; /* log structure non existent */
97 cp = pd->logtmp;
98 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
100 va_start(args, fmt);
101 cp += vsprintf(cp, fmt, args);
102 va_end(args);
103 *cp++ = '\n';
104 *cp = 0;
106 if (card->debug_flags & DEB_OUT_SYSLOG)
107 printk(KERN_INFO "%s", pd->logtmp);
108 else
109 put_log_buffer(card, pd->logtmp);
111 } /* hysdn_addlog */
113 /********************************************/
114 /* put an log buffer into the log queue. */
115 /* This buffer will be kept until all files */
116 /* opened for read got the contents. */
117 /* Flushes buffers not longer in use. */
118 /********************************************/
119 void
120 put_log_buffer(hysdn_card * card, char *cp)
122 struct log_data *ib;
123 struct procdata *pd = card->proclog;
124 int i, flags;
126 if (!pd)
127 return;
128 if (!cp)
129 return;
130 if (!*cp)
131 return;
132 if (pd->if_used <= 0)
133 return; /* no open file for read */
135 if (!(ib = (struct log_data *) kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
136 return; /* no memory */
137 strcpy(ib->log_start, cp); /* set output string */
138 ib->next = NULL;
139 ib->proc_ctrl = pd; /* point to own control structure */
140 save_flags(flags);
141 cli();
142 ib->usage_cnt = pd->if_used;
143 if (!pd->log_head)
144 pd->log_head = ib; /* new head */
145 else
146 pd->log_tail->next = ib; /* follows existing messages */
147 pd->log_tail = ib; /* new tail */
148 i = pd->del_lock++; /* get lock state */
149 restore_flags(flags);
151 /* delete old entrys */
152 if (!i)
153 while (pd->log_head->next) {
154 if ((pd->log_head->usage_cnt <= 0) &&
155 (pd->log_head->next->usage_cnt <= 0)) {
156 ib = pd->log_head;
157 pd->log_head = pd->log_head->next;
158 kfree(ib);
159 } else
160 break;
161 } /* pd->log_head->next */
162 pd->del_lock--; /* release lock level */
163 wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
164 } /* put_log_buffer */
167 /*************************/
168 /* dummy file operations */
169 /*************************/
170 static loff_t
171 hysdn_dummy_lseek(struct file *file, loff_t offset, int orig)
173 return -ESPIPE;
174 } /* hysdn_dummy_lseek */
176 /******************************/
177 /* file operations and tables */
178 /******************************/
180 /****************************************/
181 /* write log file -> set log level bits */
182 /****************************************/
183 static ssize_t
184 hysdn_log_write(struct file *file, const char *buf, size_t count, loff_t * off)
186 ulong u = 0;
187 int found = 0;
188 uchar *cp, valbuf[128];
189 long base = 10;
190 hysdn_card *card = (hysdn_card *) file->private_data;
192 if (&file->f_pos != off) /* fs error check */
193 return (-ESPIPE);
195 if (count > (sizeof(valbuf) - 1))
196 count = sizeof(valbuf) - 1; /* limit length */
197 if (copy_from_user(valbuf, buf, count))
198 return (-EFAULT); /* copy failed */
200 valbuf[count] = 0; /* terminating 0 */
201 cp = valbuf;
202 if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
203 cp += 2; /* pointer after hex modifier */
204 base = 16;
206 /* scan the input for debug flags */
207 while (*cp) {
208 if ((*cp >= '0') && (*cp <= '9')) {
209 found = 1;
210 u *= base; /* adjust to next digit */
211 u += *cp++ - '0';
212 continue;
214 if (base != 16)
215 break; /* end of number */
217 if ((*cp >= 'a') && (*cp <= 'f')) {
218 found = 1;
219 u *= base; /* adjust to next digit */
220 u += *cp++ - 'a' + 10;
221 continue;
223 break; /* terminated */
226 if (found) {
227 card->debug_flags = u; /* remember debug flags */
228 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
230 return (count);
231 } /* hysdn_log_write */
233 /******************/
234 /* read log file */
235 /******************/
236 static ssize_t
237 hysdn_log_read(struct file *file, char *buf, size_t count, loff_t * off)
239 struct log_data *inf;
240 int len;
241 word ino;
242 struct procdata *pd;
243 hysdn_card *card;
245 if (!*((struct log_data **) file->private_data)) {
246 if (file->f_flags & O_NONBLOCK)
247 return (-EAGAIN);
249 /* sorry, but we need to search the card */
250 ino = file->f_dentry->d_inode->i_ino & 0xFFFF; /* low-ino */
251 card = card_root;
252 while (card) {
253 pd = card->proclog;
254 if (pd->log->low_ino == ino)
255 break;
256 card = card->next; /* search next entry */
258 if (card)
259 interruptible_sleep_on(&(pd->rd_queue));
260 else
261 return (-EAGAIN);
264 if (!(inf = *((struct log_data **) file->private_data)))
265 return (0);
267 inf->usage_cnt--; /* new usage count */
268 (struct log_data **) file->private_data = &inf->next; /* next structure */
269 if ((len = strlen(inf->log_start)) <= count) {
270 if (copy_to_user(buf, inf->log_start, len))
271 return -EFAULT;
272 file->f_pos += len;
273 return (len);
275 return (0);
276 } /* hysdn_log_read */
278 /******************/
279 /* open log file */
280 /******************/
281 static int
282 hysdn_log_open(struct inode *ino, struct file *filep)
284 hysdn_card *card;
285 struct procdata *pd;
286 ulong flags;
288 MOD_INC_USE_COUNT; /* lock module */
289 card = card_root;
290 while (card) {
291 pd = card->proclog;
292 if (pd->log->low_ino == (ino->i_ino & 0xFFFF))
293 break;
294 card = card->next; /* search next entry */
296 if (!card) {
297 MOD_DEC_USE_COUNT; /* unlock module */
298 return (-ENODEV); /* device is unknown/invalid */
300 filep->private_data = card; /* remember our own card */
302 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
303 /* write only access -> write log level only */
304 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
306 /* read access -> log/debug read */
307 save_flags(flags);
308 cli();
309 pd->if_used++;
310 if (pd->log_head)
311 (struct log_data **) filep->private_data = &(pd->log_tail->next);
312 else
313 (struct log_data **) filep->private_data = &(pd->log_head);
314 restore_flags(flags);
315 } else { /* simultaneous read/write access forbidden ! */
316 MOD_DEC_USE_COUNT; /* unlock module */
317 return (-EPERM); /* no permission this time */
319 return (0);
320 } /* hysdn_log_open */
322 /*******************************************************************************/
323 /* close a cardlog file. If the file has been opened for exclusive write it is */
324 /* assumed as pof data input and the pof loader is noticed about. */
325 /* Otherwise file is handled as log output. In this case the interface usage */
326 /* count is decremented and all buffers are noticed of closing. If this file */
327 /* was the last one to be closed, all buffers are freed. */
328 /*******************************************************************************/
329 static int
330 hysdn_log_close(struct inode *ino, struct file *filep)
332 struct log_data *inf;
333 struct procdata *pd;
334 hysdn_card *card;
335 int flags, retval = 0;
338 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
339 /* write only access -> write debug level written */
340 retval = 0; /* success */
341 } else {
342 /* read access -> log/debug read, mark one further file as closed */
344 pd = NULL;
345 save_flags(flags);
346 cli();
347 inf = *((struct log_data **) filep->private_data); /* get first log entry */
348 if (inf)
349 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
350 else {
351 /* no info available -> search card */
352 card = card_root;
353 while (card) {
354 pd = card->proclog;
355 if (pd->log->low_ino == (ino->i_ino & 0xFFFF))
356 break;
357 card = card->next; /* search next entry */
359 if (card)
360 pd = card->proclog; /* pointer to procfs log */
362 if (pd)
363 pd->if_used--; /* decrement interface usage count by one */
365 while (inf) {
366 inf->usage_cnt--; /* decrement usage count for buffers */
367 inf = inf->next;
369 restore_flags(flags);
371 if (pd)
372 if (pd->if_used <= 0) /* delete buffers if last file closed */
373 while (pd->log_head) {
374 inf = pd->log_head;
375 pd->log_head = pd->log_head->next;
376 kfree(inf);
378 } /* read access */
380 MOD_DEC_USE_COUNT;
381 return (retval);
382 } /* hysdn_log_close */
384 /*************************************************/
385 /* select/poll routine to be able using select() */
386 /*************************************************/
387 static unsigned int
388 hysdn_log_poll(struct file *file, poll_table * wait)
390 unsigned int mask = 0;
391 word ino;
392 hysdn_card *card;
393 struct procdata *pd;
395 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
396 return (mask); /* no polling for write supported */
398 /* we need to search the card */
399 ino = file->f_dentry->d_inode->i_ino & 0xFFFF; /* low-ino */
400 card = card_root;
401 while (card) {
402 pd = card->proclog;
403 if (pd->log->low_ino == ino)
404 break;
405 card = card->next; /* search next entry */
407 if (!card)
408 return (mask); /* card not found */
410 poll_wait(file, &(pd->rd_queue), wait);
412 if (*((struct log_data **) file->private_data))
413 mask |= POLLIN | POLLRDNORM;
415 return mask;
416 } /* hysdn_log_poll */
418 /**************************************************/
419 /* table for log filesystem functions defined above. */
420 /**************************************************/
421 static struct file_operations log_fops =
423 hysdn_dummy_lseek,
424 hysdn_log_read,
425 hysdn_log_write,
426 NULL, /* readdir */
427 hysdn_log_poll, /* poll */
428 NULL,
429 NULL, /* mmap */
430 hysdn_log_open,
431 NULL, /* flush */
432 hysdn_log_close,
433 NULL /* fsync */
436 /***********************************************************************************/
437 /* hysdn_proclog_init is called when the module is loaded after creating the cards */
438 /* conf files. */
439 /***********************************************************************************/
441 hysdn_proclog_init(hysdn_card * card)
443 struct procdata *pd;
445 /* create a cardlog proc entry */
447 if ((pd = (struct procdata *) kmalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
448 memset(pd, 0, sizeof(struct procdata));
450 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
451 if ((pd->log = create_proc_entry(pd->log_name, S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry)) != NULL)
452 pd->log->proc_fops = &log_fops; /* set new operations table */
454 init_waitqueue_head(&(pd->rd_queue));
456 card->proclog = (void *) pd; /* remember procfs structure */
458 return (0);
459 } /* hysdn_proclog_init */
461 /************************************************************************************/
462 /* hysdn_proclog_release is called when the module is unloaded and before the cards */
463 /* conf file is released */
464 /* The module counter is assumed to be 0 ! */
465 /************************************************************************************/
466 void
467 hysdn_proclog_release(hysdn_card * card)
469 struct procdata *pd;
471 if ((pd = (struct procdata *) card->proclog) != NULL) {
472 if (pd->log)
473 remove_proc_entry(pd->log_name, hysdn_proc_entry);
474 kfree(pd); /* release memory */
475 card->proclog = NULL;
477 } /* hysdn_proclog_release */