1
2
3
4
5
6
7
8
9
10
11
12
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>
20
21#include "hysdn_defs.h"
22
23
24extern struct proc_dir_entry *hysdn_proc_entry;
25
26static DEFINE_MUTEX(hysdn_log_mutex);
27static void put_log_buffer(hysdn_card * card, char *cp);
28
29
30
31
32struct log_data {
33 struct log_data *next;
34 unsigned long usage_cnt;
35 void *proc_ctrl;
36 char log_start[2];
37};
38
39
40
41
42struct procdata {
43 struct proc_dir_entry *log;
44 char log_name[15];
45 struct log_data *log_head, *log_tail;
46 int if_used;
47 int volatile del_lock;
48 unsigned char logtmp[LOG_MAX_LINELEN];
49 wait_queue_head_t rd_queue;
50};
51
52
53
54
55
56void
57hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
58{
59 char buf[ERRLOG_TEXT_SIZE + 40];
60
61 sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
62 put_log_buffer(card, buf);
63}
64
65
66
67
68void
69hysdn_addlog(hysdn_card * card, char *fmt,...)
70{
71 struct procdata *pd = card->proclog;
72 char *cp;
73 va_list args;
74
75 if (!pd)
76 return;
77
78 cp = pd->logtmp;
79 cp += sprintf(cp, "HYSDN: card %d ", card->myid);
80
81 va_start(args, fmt);
82 cp += vsprintf(cp, fmt, args);
83 va_end(args);
84 *cp++ = '\n';
85 *cp = 0;
86
87 if (card->debug_flags & DEB_OUT_SYSLOG)
88 printk(KERN_INFO "%s", pd->logtmp);
89 else
90 put_log_buffer(card, pd->logtmp);
91
92}
93
94
95
96
97
98
99
100static void
101put_log_buffer(hysdn_card * card, char *cp)
102{
103 struct log_data *ib;
104 struct procdata *pd = card->proclog;
105 int i;
106 unsigned long flags;
107
108 if (!pd)
109 return;
110 if (!cp)
111 return;
112 if (!*cp)
113 return;
114 if (pd->if_used <= 0)
115 return;
116
117 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
118 return;
119 strcpy(ib->log_start, cp);
120 ib->next = NULL;
121 ib->proc_ctrl = pd;
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;
126 else
127 pd->log_tail->next = ib;
128 pd->log_tail = ib;
129 i = pd->del_lock++;
130 spin_unlock_irqrestore(&card->hysdn_lock, flags);
131
132
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 }
143 pd->del_lock--;
144 wake_up_interruptible(&(pd->rd_queue));
145}
146
147
148
149
150
151
152
153
154
155static ssize_t
156hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
157{
158 int rc;
159 unsigned char valbuf[128];
160 hysdn_card *card = file->private_data;
161
162 if (count > (sizeof(valbuf) - 1))
163 count = sizeof(valbuf) - 1;
164 if (copy_from_user(valbuf, buf, count))
165 return (-EFAULT);
166
167 valbuf[count] = 0;
168
169 rc = kstrtoul(valbuf, 0, &card->debug_flags);
170 if (rc < 0)
171 return rc;
172 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
173 return (count);
174}
175
176
177
178
179static ssize_t
180hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
181{
182 struct log_data *inf;
183 int len;
184 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
185 struct procdata *pd = NULL;
186 hysdn_card *card;
187
188 if (!*((struct log_data **) file->private_data)) {
189 if (file->f_flags & O_NONBLOCK)
190 return (-EAGAIN);
191
192
193 card = card_root;
194 while (card) {
195 pd = card->proclog;
196 if (pd->log == pde)
197 break;
198 card = card->next;
199 }
200 if (card)
201 interruptible_sleep_on(&(pd->rd_queue));
202 else
203 return (-EAGAIN);
204
205 }
206 if (!(inf = *((struct log_data **) file->private_data)))
207 return (0);
208
209 inf->usage_cnt--;
210 file->private_data = &inf->next;
211 if ((len = strlen(inf->log_start)) <= count) {
212 if (copy_to_user(buf, inf->log_start, len))
213 return -EFAULT;
214 *off += len;
215 return (len);
216 }
217 return (0);
218}
219
220
221
222
223static int
224hysdn_log_open(struct inode *ino, struct file *filep)
225{
226 hysdn_card *card;
227 struct procdata *pd = NULL;
228 unsigned long flags;
229
230 mutex_lock(&hysdn_log_mutex);
231 card = card_root;
232 while (card) {
233 pd = card->proclog;
234 if (pd->log == PDE(ino))
235 break;
236 card = card->next;
237 }
238 if (!card) {
239 mutex_unlock(&hysdn_log_mutex);
240 return (-ENODEV);
241 }
242 filep->private_data = card;
243
244 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
245
246 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
247
248
249 spin_lock_irqsave(&card->hysdn_lock, flags);
250 pd->if_used++;
251 if (pd->log_head)
252 filep->private_data = &pd->log_tail->next;
253 else
254 filep->private_data = &pd->log_head;
255 spin_unlock_irqrestore(&card->hysdn_lock, flags);
256 } else {
257 mutex_unlock(&hysdn_log_mutex);
258 return (-EPERM);
259 }
260 mutex_unlock(&hysdn_log_mutex);
261 return nonseekable_open(ino, filep);
262}
263
264
265
266
267
268
269
270
271static int
272hysdn_log_close(struct inode *ino, struct file *filep)
273{
274 struct log_data *inf;
275 struct procdata *pd;
276 hysdn_card *card;
277 int retval = 0;
278
279 mutex_lock(&hysdn_log_mutex);
280 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
281
282 retval = 0;
283 } else {
284
285
286 pd = NULL;
287 inf = *((struct log_data **) filep->private_data);
288 if (inf)
289 pd = (struct procdata *) inf->proc_ctrl;
290 else {
291
292 card = card_root;
293 while (card) {
294 pd = card->proclog;
295 if (pd->log == PDE(ino))
296 break;
297 card = card->next;
298 }
299 if (card)
300 pd = card->proclog;
301 }
302 if (pd)
303 pd->if_used--;
304
305 while (inf) {
306 inf->usage_cnt--;
307 inf = inf->next;
308 }
309
310 if (pd)
311 if (pd->if_used <= 0)
312 while (pd->log_head) {
313 inf = pd->log_head;
314 pd->log_head = pd->log_head->next;
315 kfree(inf);
316 }
317 }
318 mutex_unlock(&hysdn_log_mutex);
319
320 return (retval);
321}
322
323
324
325
326static unsigned int
327hysdn_log_poll(struct file *file, poll_table * wait)
328{
329 unsigned int mask = 0;
330 struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
331 hysdn_card *card;
332 struct procdata *pd = NULL;
333
334 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
335 return (mask);
336
337
338 card = card_root;
339 while (card) {
340 pd = card->proclog;
341 if (pd->log == pde)
342 break;
343 card = card->next;
344 }
345 if (!card)
346 return (mask);
347
348 poll_wait(file, &(pd->rd_queue), wait);
349
350 if (*((struct log_data **) file->private_data))
351 mask |= POLLIN | POLLRDNORM;
352
353 return mask;
354}
355
356
357
358
359static const struct file_operations log_fops =
360{
361 .owner = THIS_MODULE,
362 .llseek = no_llseek,
363 .read = hysdn_log_read,
364 .write = hysdn_log_write,
365 .poll = hysdn_log_poll,
366 .open = hysdn_log_open,
367 .release = hysdn_log_close,
368};
369
370
371
372
373
374
375int
376hysdn_proclog_init(hysdn_card * card)
377{
378 struct procdata *pd;
379
380
381
382 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
383 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
384 pd->log = proc_create(pd->log_name,
385 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
386 &log_fops);
387
388 init_waitqueue_head(&(pd->rd_queue));
389
390 card->proclog = (void *) pd;
391 }
392 return (0);
393}
394
395
396
397
398
399
400void
401hysdn_proclog_release(hysdn_card * card)
402{
403 struct procdata *pd;
404
405 if ((pd = (struct procdata *) card->proclog) != NULL) {
406 if (pd->log)
407 remove_proc_entry(pd->log_name, hysdn_proc_entry);
408 kfree(pd);
409 card->proclog = NULL;
410 }
411}
412