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)
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
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 /*************************************************/
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 /**********************************************/
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 /**********************************************/
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 /***************************************************/
88 hysdn_addlog(hysdn_card
* card
, char *fmt
,...)
90 struct procdata
*pd
= card
->proclog
;
95 return; /* log structure non existent */
98 cp
+= sprintf(cp
, "HYSDN: card %d ", card
->myid
);
101 cp
+= vsprintf(cp
, fmt
, args
);
106 if (card
->debug_flags
& DEB_OUT_SYSLOG
)
107 printk(KERN_INFO
"%s", pd
->logtmp
);
109 put_log_buffer(card
, pd
->logtmp
);
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 /********************************************/
120 put_log_buffer(hysdn_card
* card
, char *cp
)
123 struct procdata
*pd
= card
->proclog
;
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 */
139 ib
->proc_ctrl
= pd
; /* point to own control structure */
142 ib
->usage_cnt
= pd
->if_used
;
144 pd
->log_head
= ib
; /* new head */
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 */
153 while (pd
->log_head
->next
) {
154 if ((pd
->log_head
->usage_cnt
<= 0) &&
155 (pd
->log_head
->next
->usage_cnt
<= 0)) {
157 pd
->log_head
= pd
->log_head
->next
;
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 /*************************/
171 hysdn_dummy_lseek(struct file
*file
, loff_t offset
, int orig
)
174 } /* hysdn_dummy_lseek */
176 /******************************/
177 /* file operations and tables */
178 /******************************/
180 /****************************************/
181 /* write log file -> set log level bits */
182 /****************************************/
184 hysdn_log_write(struct file
*file
, const char *buf
, size_t count
, loff_t
* off
)
188 uchar
*cp
, valbuf
[128];
190 hysdn_card
*card
= (hysdn_card
*) file
->private_data
;
192 if (&file
->f_pos
!= off
) /* fs error check */
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 */
202 if ((count
> 2) && (valbuf
[0] == '0') && (valbuf
[1] == 'x')) {
203 cp
+= 2; /* pointer after hex modifier */
206 /* scan the input for debug flags */
208 if ((*cp
>= '0') && (*cp
<= '9')) {
210 u
*= base
; /* adjust to next digit */
215 break; /* end of number */
217 if ((*cp
>= 'a') && (*cp
<= 'f')) {
219 u
*= base
; /* adjust to next digit */
220 u
+= *cp
++ - 'a' + 10;
223 break; /* terminated */
227 card
->debug_flags
= u
; /* remember debug flags */
228 hysdn_addlog(card
, "debug set to 0x%lx", card
->debug_flags
);
231 } /* hysdn_log_write */
237 hysdn_log_read(struct file
*file
, char *buf
, size_t count
, loff_t
* off
)
239 struct log_data
*inf
;
245 if (!*((struct log_data
**) file
->private_data
)) {
246 if (file
->f_flags
& O_NONBLOCK
)
249 /* sorry, but we need to search the card */
250 ino
= file
->f_dentry
->d_inode
->i_ino
& 0xFFFF; /* low-ino */
254 if (pd
->log
->low_ino
== ino
)
256 card
= card
->next
; /* search next entry */
259 interruptible_sleep_on(&(pd
->rd_queue
));
264 if (!(inf
= *((struct log_data
**) file
->private_data
)))
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
))
276 } /* hysdn_log_read */
282 hysdn_log_open(struct inode
*ino
, struct file
*filep
)
288 MOD_INC_USE_COUNT
; /* lock module */
292 if (pd
->log
->low_ino
== (ino
->i_ino
& 0xFFFF))
294 card
= card
->next
; /* search next entry */
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 */
311 (struct log_data
**) filep
->private_data
= &(pd
->log_tail
->next
);
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 */
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 /*******************************************************************************/
330 hysdn_log_close(struct inode
*ino
, struct file
*filep
)
332 struct log_data
*inf
;
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 */
342 /* read access -> log/debug read, mark one further file as closed */
347 inf
= *((struct log_data
**) filep
->private_data
); /* get first log entry */
349 pd
= (struct procdata
*) inf
->proc_ctrl
; /* still entries there */
351 /* no info available -> search card */
355 if (pd
->log
->low_ino
== (ino
->i_ino
& 0xFFFF))
357 card
= card
->next
; /* search next entry */
360 pd
= card
->proclog
; /* pointer to procfs log */
363 pd
->if_used
--; /* decrement interface usage count by one */
366 inf
->usage_cnt
--; /* decrement usage count for buffers */
369 restore_flags(flags
);
372 if (pd
->if_used
<= 0) /* delete buffers if last file closed */
373 while (pd
->log_head
) {
375 pd
->log_head
= pd
->log_head
->next
;
382 } /* hysdn_log_close */
384 /*************************************************/
385 /* select/poll routine to be able using select() */
386 /*************************************************/
388 hysdn_log_poll(struct file
*file
, poll_table
* wait
)
390 unsigned int mask
= 0;
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 */
403 if (pd
->log
->low_ino
== ino
)
405 card
= card
->next
; /* search next entry */
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
;
416 } /* hysdn_log_poll */
418 /**************************************************/
419 /* table for log filesystem functions defined above. */
420 /**************************************************/
421 static struct file_operations log_fops
=
427 hysdn_log_poll
, /* poll */
436 struct inode_operations log_inode_operations
;
438 /***********************************************************************************/
439 /* hysdn_proclog_init is called when the module is loaded after creating the cards */
441 /***********************************************************************************/
443 hysdn_proclog_init(hysdn_card
* card
)
447 /* create a cardlog proc entry */
449 if ((pd
= (struct procdata
*) kmalloc(sizeof(struct procdata
), GFP_KERNEL
)) != NULL
) {
450 memset(pd
, 0, sizeof(struct procdata
));
451 memset(&log_inode_operations
, 0, sizeof(struct inode_operations
));
452 log_inode_operations
.default_file_ops
= &log_fops
;
454 sprintf(pd
->log_name
, "%s%d", PROC_LOG_BASENAME
, card
->myid
);
455 if ((pd
->log
= create_proc_entry(pd
->log_name
, S_IFREG
| S_IRUGO
| S_IWUSR
, hysdn_proc_entry
)) != NULL
)
456 pd
->log
->ops
= &log_inode_operations
; /* set new operations table */
458 init_waitqueue_head(&(pd
->rd_queue
));
460 card
->proclog
= (void *) pd
; /* remember procfs structure */
463 } /* hysdn_proclog_init */
465 /************************************************************************************/
466 /* hysdn_proclog_release is called when the module is unloaded and before the cards */
467 /* conf file is released */
468 /* The module counter is assumed to be 0 ! */
469 /************************************************************************************/
471 hysdn_proclog_release(hysdn_card
* card
)
475 if ((pd
= (struct procdata
*) card
->proclog
) != NULL
) {
477 remove_proc_entry(pd
->log_name
, hysdn_proc_entry
);
478 kfree(pd
); /* release memory */
479 card
->proclog
= NULL
;
481 } /* hysdn_proclog_release */