]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* $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> | |
1da177e4 LT |
14 | #include <linux/poll.h> |
15 | #include <linux/proc_fs.h> | |
d43c36dc | 16 | #include <linux/sched.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
76a64921 | 18 | #include <linux/mutex.h> |
1da177e4 LT |
19 | |
20 | #include "hysdn_defs.h" | |
21 | ||
22 | /* the proc subdir for the interface is defined in the procconf module */ | |
23 | extern struct proc_dir_entry *hysdn_proc_entry; | |
24 | ||
76a64921 | 25 | static DEFINE_MUTEX(hysdn_log_mutex); |
aade0e82 AB |
26 | static void put_log_buffer(hysdn_card * card, char *cp); |
27 | ||
1da177e4 LT |
28 | /*************************************************/ |
29 | /* structure keeping ascii log for device output */ | |
30 | /*************************************************/ | |
31 | struct log_data { | |
32 | struct log_data *next; | |
c721bcce | 33 | unsigned long usage_cnt;/* number of files still to work */ |
1da177e4 LT |
34 | 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 | /**********************************************/ | |
41 | struct 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 */ | |
c721bcce | 47 | unsigned char logtmp[LOG_MAX_LINELEN]; |
1da177e4 LT |
48 | wait_queue_head_t rd_queue; |
49 | }; | |
50 | ||
51 | ||
52 | /**********************************************/ | |
53 | /* log function for cards error log interface */ | |
54 | /**********************************************/ | |
55 | void | |
56 | hysdn_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 | /***************************************************/ | |
67 | void | |
68 | hysdn_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 | /********************************************/ | |
aade0e82 | 99 | static void |
1da177e4 LT |
100 | put_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 | ||
5cbded58 | 116 | if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC))) |
1da177e4 LT |
117 | 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 */ | |
0d9ba869 | 121 | spin_lock_irqsave(&card->hysdn_lock, flags); |
1da177e4 LT |
122 | 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 */ | |
0d9ba869 | 129 | spin_unlock_irqrestore(&card->hysdn_lock, flags); |
1da177e4 LT |
130 | |
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 | /****************************************/ | |
154 | static ssize_t | |
155 | hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off) | |
156 | { | |
c721bcce | 157 | unsigned long u = 0; |
1da177e4 | 158 | int found = 0; |
c721bcce | 159 | unsigned char *cp, valbuf[128]; |
1da177e4 LT |
160 | long base = 10; |
161 | hysdn_card *card = (hysdn_card *) file->private_data; | |
162 | ||
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 | /******************/ | |
204 | static ssize_t | |
205 | hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off) | |
206 | { | |
207 | struct log_data *inf; | |
208 | int len; | |
4482dfad | 209 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); |
1da177e4 LT |
210 | 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 | /******************/ | |
248 | static int | |
249 | hysdn_log_open(struct inode *ino, struct file *filep) | |
250 | { | |
251 | hysdn_card *card; | |
252 | struct procdata *pd = NULL; | |
c721bcce | 253 | unsigned long flags; |
1da177e4 | 254 | |
76a64921 | 255 | mutex_lock(&hysdn_log_mutex); |
1da177e4 LT |
256 | 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) { | |
76a64921 | 264 | mutex_unlock(&hysdn_log_mutex); |
1da177e4 LT |
265 | 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 */ | |
0d9ba869 | 274 | spin_lock_irqsave(&card->hysdn_lock, flags); |
1da177e4 LT |
275 | 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; | |
0d9ba869 | 280 | spin_unlock_irqrestore(&card->hysdn_lock, flags); |
1da177e4 | 281 | } else { /* simultaneous read/write access forbidden ! */ |
76a64921 | 282 | mutex_unlock(&hysdn_log_mutex); |
1da177e4 LT |
283 | return (-EPERM); /* no permission this time */ |
284 | } | |
76a64921 | 285 | mutex_unlock(&hysdn_log_mutex); |
1da177e4 LT |
286 | 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 | /*******************************************************************************/ | |
296 | static int | |
297 | hysdn_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; | |
1da177e4 | 303 | |
76a64921 | 304 | mutex_lock(&hysdn_log_mutex); |
1da177e4 LT |
305 | 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; | |
1da177e4 LT |
312 | 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 | } | |
1da177e4 LT |
334 | |
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 */ | |
76a64921 | 343 | mutex_unlock(&hysdn_log_mutex); |
1da177e4 LT |
344 | |
345 | return (retval); | |
346 | } /* hysdn_log_close */ | |
347 | ||
348 | /*************************************************/ | |
349 | /* select/poll routine to be able using select() */ | |
350 | /*************************************************/ | |
351 | static unsigned int | |
352 | hysdn_log_poll(struct file *file, poll_table * wait) | |
353 | { | |
354 | unsigned int mask = 0; | |
4482dfad | 355 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); |
1da177e4 LT |
356 | 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 | /**************************************************/ | |
2b8693c0 | 384 | static const struct file_operations log_fops = |
1da177e4 | 385 | { |
ac41cfd1 | 386 | .owner = THIS_MODULE, |
1da177e4 LT |
387 | .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 | /***********************************************************************************/ | |
400 | int | |
401 | hysdn_proclog_init(hysdn_card * card) | |
402 | { | |
403 | struct procdata *pd; | |
404 | ||
405 | /* create a cardlog proc entry */ | |
406 | ||
41f96935 | 407 | if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) { |
1da177e4 | 408 | sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid); |
ac41cfd1 DL |
409 | pd->log = proc_create(pd->log_name, |
410 | S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry, | |
411 | &log_fops); | |
1da177e4 LT |
412 | |
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 | /************************************************************************************/ | |
425 | void | |
426 | hysdn_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 */ |