PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / isdn / hysdn / hysdn_proclog.c
blobb61e8d5e84ad022e566f5bfa191ddcb392f3df7b
1 /* $Id: hysdn_proclog.c,v 1.9.6.3 2001/09/23 22:24:54 kai Exp $
3 * Linux driver for HYSDN cards, /proc/net filesystem log functions.
5 * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
6 * Copyright 1999 by Werner Cornelius (werner@titro.de)
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/proc_fs.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/mutex.h>
19 #include <linux/kernel.h>
21 #include "hysdn_defs.h"
23 /* the proc subdir for the interface is defined in the procconf module */
24 extern struct proc_dir_entry *hysdn_proc_entry;
26 static DEFINE_MUTEX(hysdn_log_mutex);
27 static void put_log_buffer(hysdn_card *card, char *cp);
29 /*************************************************/
30 /* structure keeping ascii log for device output */
31 /*************************************************/
32 struct log_data {
33 struct log_data *next;
34 unsigned long usage_cnt;/* number of files still to work */
35 void *proc_ctrl; /* pointer to own control procdata structure */
36 char log_start[2]; /* log string start (final len aligned by size) */
39 /**********************************************/
40 /* structure holding proc entrys for one card */
41 /**********************************************/
42 struct 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 */
48 unsigned char logtmp[LOG_MAX_LINELEN];
49 wait_queue_head_t rd_queue;
53 /**********************************************/
54 /* log function for cards error log interface */
55 /**********************************************/
56 void
57 hysdn_card_errlog(hysdn_card *card, tErrLogEntry *logp, int maxsize)
59 char buf[ERRLOG_TEXT_SIZE + 40];
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 */
65 /***************************************************/
66 /* Log function using format specifiers for output */
67 /***************************************************/
68 void
69 hysdn_addlog(hysdn_card *card, char *fmt, ...)
71 struct procdata *pd = card->proclog;
72 char *cp;
73 va_list args;
75 if (!pd)
76 return; /* log structure non existent */
78 cp = pd->logtmp;
79 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
81 va_start(args, fmt);
82 cp += vsprintf(cp, fmt, args);
83 va_end(args);
84 *cp++ = '\n';
85 *cp = 0;
87 if (card->debug_flags & DEB_OUT_SYSLOG)
88 printk(KERN_INFO "%s", pd->logtmp);
89 else
90 put_log_buffer(card, pd->logtmp);
92 } /* hysdn_addlog */
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 /********************************************/
100 static void
101 put_log_buffer(hysdn_card *card, char *cp)
103 struct log_data *ib;
104 struct procdata *pd = card->proclog;
105 int i;
106 unsigned long flags;
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 */
117 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
118 return; /* no memory */
119 strcpy(ib->log_start, cp); /* set output string */
120 ib->next = NULL;
121 ib->proc_ctrl = pd; /* point to own control structure */
122 spin_lock_irqsave(&card->hysdn_lock, flags);
123 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 */
130 spin_unlock_irqrestore(&card->hysdn_lock, flags);
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 */
148 /******************************/
149 /* file operations and tables */
150 /******************************/
152 /****************************************/
153 /* write log file -> set log level bits */
154 /****************************************/
155 static ssize_t
156 hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
158 int rc;
159 hysdn_card *card = file->private_data;
161 rc = kstrtoul_from_user(buf, count, 0, &card->debug_flags);
162 if (rc < 0)
163 return rc;
164 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
165 return (count);
166 } /* hysdn_log_write */
168 /******************/
169 /* read log file */
170 /******************/
171 static ssize_t
172 hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
174 struct log_data *inf;
175 int len;
176 hysdn_card *card = PDE_DATA(file_inode(file));
178 if (!*((struct log_data **) file->private_data)) {
179 struct procdata *pd = card->proclog;
180 if (file->f_flags & O_NONBLOCK)
181 return (-EAGAIN);
183 interruptible_sleep_on(&(pd->rd_queue));
185 if (!(inf = *((struct log_data **) file->private_data)))
186 return (0);
188 inf->usage_cnt--; /* new usage count */
189 file->private_data = &inf->next; /* next structure */
190 if ((len = strlen(inf->log_start)) <= count) {
191 if (copy_to_user(buf, inf->log_start, len))
192 return -EFAULT;
193 *off += len;
194 return (len);
196 return (0);
197 } /* hysdn_log_read */
199 /******************/
200 /* open log file */
201 /******************/
202 static int
203 hysdn_log_open(struct inode *ino, struct file *filep)
205 hysdn_card *card = PDE_DATA(ino);
207 mutex_lock(&hysdn_log_mutex);
208 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
209 /* write only access -> write log level only */
210 filep->private_data = card; /* remember our own card */
211 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
212 struct procdata *pd = card->proclog;
213 unsigned long flags;
215 /* read access -> log/debug read */
216 spin_lock_irqsave(&card->hysdn_lock, flags);
217 pd->if_used++;
218 if (pd->log_head)
219 filep->private_data = &pd->log_tail->next;
220 else
221 filep->private_data = &pd->log_head;
222 spin_unlock_irqrestore(&card->hysdn_lock, flags);
223 } else { /* simultaneous read/write access forbidden ! */
224 mutex_unlock(&hysdn_log_mutex);
225 return (-EPERM); /* no permission this time */
227 mutex_unlock(&hysdn_log_mutex);
228 return nonseekable_open(ino, filep);
229 } /* hysdn_log_open */
231 /*******************************************************************************/
232 /* close a cardlog file. If the file has been opened for exclusive write it is */
233 /* assumed as pof data input and the pof loader is noticed about. */
234 /* Otherwise file is handled as log output. In this case the interface usage */
235 /* count is decremented and all buffers are noticed of closing. If this file */
236 /* was the last one to be closed, all buffers are freed. */
237 /*******************************************************************************/
238 static int
239 hysdn_log_close(struct inode *ino, struct file *filep)
241 struct log_data *inf;
242 struct procdata *pd;
243 hysdn_card *card;
244 int retval = 0;
246 mutex_lock(&hysdn_log_mutex);
247 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
248 /* write only access -> write debug level written */
249 retval = 0; /* success */
250 } else {
251 /* read access -> log/debug read, mark one further file as closed */
253 inf = *((struct log_data **) filep->private_data); /* get first log entry */
254 if (inf)
255 pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
256 else {
257 /* no info available -> search card */
258 card = PDE_DATA(file_inode(filep));
259 pd = card->proclog; /* pointer to procfs log */
261 if (pd)
262 pd->if_used--; /* decrement interface usage count by one */
264 while (inf) {
265 inf->usage_cnt--; /* decrement usage count for buffers */
266 inf = inf->next;
269 if (pd)
270 if (pd->if_used <= 0) /* delete buffers if last file closed */
271 while (pd->log_head) {
272 inf = pd->log_head;
273 pd->log_head = pd->log_head->next;
274 kfree(inf);
276 } /* read access */
277 mutex_unlock(&hysdn_log_mutex);
279 return (retval);
280 } /* hysdn_log_close */
282 /*************************************************/
283 /* select/poll routine to be able using select() */
284 /*************************************************/
285 static unsigned int
286 hysdn_log_poll(struct file *file, poll_table *wait)
288 unsigned int mask = 0;
289 hysdn_card *card = PDE_DATA(file_inode(file));
290 struct procdata *pd = card->proclog;
292 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
293 return (mask); /* no polling for write supported */
295 poll_wait(file, &(pd->rd_queue), wait);
297 if (*((struct log_data **) file->private_data))
298 mask |= POLLIN | POLLRDNORM;
300 return mask;
301 } /* hysdn_log_poll */
303 /**************************************************/
304 /* table for log filesystem functions defined above. */
305 /**************************************************/
306 static const struct file_operations log_fops =
308 .owner = THIS_MODULE,
309 .llseek = no_llseek,
310 .read = hysdn_log_read,
311 .write = hysdn_log_write,
312 .poll = hysdn_log_poll,
313 .open = hysdn_log_open,
314 .release = hysdn_log_close,
318 /***********************************************************************************/
319 /* hysdn_proclog_init is called when the module is loaded after creating the cards */
320 /* conf files. */
321 /***********************************************************************************/
323 hysdn_proclog_init(hysdn_card *card)
325 struct procdata *pd;
327 /* create a cardlog proc entry */
329 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
330 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
331 pd->log = proc_create_data(pd->log_name,
332 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
333 &log_fops, card);
335 init_waitqueue_head(&(pd->rd_queue));
337 card->proclog = (void *) pd; /* remember procfs structure */
339 return (0);
340 } /* hysdn_proclog_init */
342 /************************************************************************************/
343 /* hysdn_proclog_release is called when the module is unloaded and before the cards */
344 /* conf file is released */
345 /* The module counter is assumed to be 0 ! */
346 /************************************************************************************/
347 void
348 hysdn_proclog_release(hysdn_card *card)
350 struct procdata *pd;
352 if ((pd = (struct procdata *) card->proclog) != NULL) {
353 if (pd->log)
354 remove_proc_entry(pd->log_name, hysdn_proc_entry);
355 kfree(pd); /* release memory */
356 card->proclog = NULL;
358 } /* hysdn_proclog_release */