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 hysdn_card *card = file->private_data;
160
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}
167
168
169
170
171static ssize_t
172hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
173{
174 struct log_data *inf;
175 int len;
176 hysdn_card *card = PDE_DATA(file_inode(file));
177
178 if (!(inf = *((struct log_data **) file->private_data))) {
179 struct procdata *pd = card->proclog;
180 if (file->f_flags & O_NONBLOCK)
181 return (-EAGAIN);
182
183 wait_event_interruptible(pd->rd_queue, (inf =
184 *((struct log_data **) file->private_data)));
185 }
186 if (!inf)
187 return (0);
188
189 inf->usage_cnt--;
190 file->private_data = &inf->next;
191 if ((len = strlen(inf->log_start)) <= count) {
192 if (copy_to_user(buf, inf->log_start, len))
193 return -EFAULT;
194 *off += len;
195 return (len);
196 }
197 return (0);
198}
199
200
201
202
203static int
204hysdn_log_open(struct inode *ino, struct file *filep)
205{
206 hysdn_card *card = PDE_DATA(ino);
207
208 mutex_lock(&hysdn_log_mutex);
209 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
210
211 filep->private_data = card;
212 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
213 struct procdata *pd = card->proclog;
214 unsigned long flags;
215
216
217 spin_lock_irqsave(&card->hysdn_lock, flags);
218 pd->if_used++;
219 if (pd->log_head)
220 filep->private_data = &pd->log_tail->next;
221 else
222 filep->private_data = &pd->log_head;
223 spin_unlock_irqrestore(&card->hysdn_lock, flags);
224 } else {
225 mutex_unlock(&hysdn_log_mutex);
226 return (-EPERM);
227 }
228 mutex_unlock(&hysdn_log_mutex);
229 return nonseekable_open(ino, filep);
230}
231
232
233
234
235
236
237
238
239static int
240hysdn_log_close(struct inode *ino, struct file *filep)
241{
242 struct log_data *inf;
243 struct procdata *pd;
244 hysdn_card *card;
245 int retval = 0;
246
247 mutex_lock(&hysdn_log_mutex);
248 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
249
250 retval = 0;
251 } else {
252
253
254 inf = *((struct log_data **) filep->private_data);
255 if (inf)
256 pd = (struct procdata *) inf->proc_ctrl;
257 else {
258
259 card = PDE_DATA(file_inode(filep));
260 pd = card->proclog;
261 }
262 if (pd)
263 pd->if_used--;
264
265 while (inf) {
266 inf->usage_cnt--;
267 inf = inf->next;
268 }
269
270 if (pd)
271 if (pd->if_used <= 0)
272 while (pd->log_head) {
273 inf = pd->log_head;
274 pd->log_head = pd->log_head->next;
275 kfree(inf);
276 }
277 }
278 mutex_unlock(&hysdn_log_mutex);
279
280 return (retval);
281}
282
283
284
285
286static unsigned int
287hysdn_log_poll(struct file *file, poll_table *wait)
288{
289 unsigned int mask = 0;
290 hysdn_card *card = PDE_DATA(file_inode(file));
291 struct procdata *pd = card->proclog;
292
293 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
294 return (mask);
295
296 poll_wait(file, &(pd->rd_queue), wait);
297
298 if (*((struct log_data **) file->private_data))
299 mask |= POLLIN | POLLRDNORM;
300
301 return mask;
302}
303
304
305
306
307static const struct file_operations log_fops =
308{
309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
311 .read = hysdn_log_read,
312 .write = hysdn_log_write,
313 .poll = hysdn_log_poll,
314 .open = hysdn_log_open,
315 .release = hysdn_log_close,
316};
317
318
319
320
321
322
323int
324hysdn_proclog_init(hysdn_card *card)
325{
326 struct procdata *pd;
327
328
329
330 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
331 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
332 pd->log = proc_create_data(pd->log_name,
333 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
334 &log_fops, card);
335
336 init_waitqueue_head(&(pd->rd_queue));
337
338 card->proclog = (void *) pd;
339 }
340 return (0);
341}
342
343
344
345
346
347
348void
349hysdn_proclog_release(hysdn_card *card)
350{
351 struct procdata *pd;
352
353 if ((pd = (struct procdata *) card->proclog) != NULL) {
354 if (pd->log)
355 remove_proc_entry(pd->log_name, hysdn_proc_entry);
356 kfree(pd);
357 card->proclog = NULL;
358 }
359}
360