This source file includes following definitions.
- hysdn_card_errlog
- hysdn_addlog
- put_log_buffer
- hysdn_log_write
- hysdn_log_read
- hysdn_log_open
- hysdn_log_close
- hysdn_log_poll
- hysdn_proclog_init
- hysdn_proclog_release
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
24 extern struct proc_dir_entry *hysdn_proc_entry;
25
26 static DEFINE_MUTEX(hysdn_log_mutex);
27 static void put_log_buffer(hysdn_card *card, char *cp);
28
29
30
31
32 struct 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
42 struct 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 unsigned char logtmp[LOG_MAX_LINELEN];
48 wait_queue_head_t rd_queue;
49 };
50
51
52
53
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);
62 }
63
64
65
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;
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 }
92
93
94
95
96
97
98
99 static void
100 put_log_buffer(hysdn_card *card, char *cp)
101 {
102 struct log_data *ib;
103 struct procdata *pd = card->proclog;
104 unsigned long flags;
105
106 if (!pd)
107 return;
108 if (!cp)
109 return;
110 if (!*cp)
111 return;
112 if (pd->if_used <= 0)
113 return;
114
115 if (!(ib = kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
116 return;
117 strcpy(ib->log_start, cp);
118 ib->next = NULL;
119 ib->proc_ctrl = pd;
120 spin_lock_irqsave(&card->hysdn_lock, flags);
121 ib->usage_cnt = pd->if_used;
122 if (!pd->log_head)
123 pd->log_head = ib;
124 else
125 pd->log_tail->next = ib;
126 pd->log_tail = ib;
127
128
129 while (pd->log_head->next) {
130 if ((pd->log_head->usage_cnt <= 0) &&
131 (pd->log_head->next->usage_cnt <= 0)) {
132 ib = pd->log_head;
133 pd->log_head = pd->log_head->next;
134 kfree(ib);
135 } else {
136 break;
137 }
138 }
139
140 spin_unlock_irqrestore(&card->hysdn_lock, flags);
141
142 wake_up_interruptible(&(pd->rd_queue));
143 }
144
145
146
147
148
149
150
151
152
153 static ssize_t
154 hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
155 {
156 int rc;
157 hysdn_card *card = file->private_data;
158
159 rc = kstrtoul_from_user(buf, count, 0, &card->debug_flags);
160 if (rc < 0)
161 return rc;
162 hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
163 return (count);
164 }
165
166
167
168
169 static ssize_t
170 hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t *off)
171 {
172 struct log_data *inf;
173 int len;
174 hysdn_card *card = PDE_DATA(file_inode(file));
175
176 if (!(inf = *((struct log_data **) file->private_data))) {
177 struct procdata *pd = card->proclog;
178 if (file->f_flags & O_NONBLOCK)
179 return (-EAGAIN);
180
181 wait_event_interruptible(pd->rd_queue, (inf =
182 *((struct log_data **) file->private_data)));
183 }
184 if (!inf)
185 return (0);
186
187 inf->usage_cnt--;
188 file->private_data = &inf->next;
189 if ((len = strlen(inf->log_start)) <= count) {
190 if (copy_to_user(buf, inf->log_start, len))
191 return -EFAULT;
192 *off += len;
193 return (len);
194 }
195 return (0);
196 }
197
198
199
200
201 static int
202 hysdn_log_open(struct inode *ino, struct file *filep)
203 {
204 hysdn_card *card = PDE_DATA(ino);
205
206 mutex_lock(&hysdn_log_mutex);
207 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
208
209 filep->private_data = card;
210 } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
211 struct procdata *pd = card->proclog;
212 unsigned long flags;
213
214
215 spin_lock_irqsave(&card->hysdn_lock, flags);
216 pd->if_used++;
217 if (pd->log_head)
218 filep->private_data = &pd->log_tail->next;
219 else
220 filep->private_data = &pd->log_head;
221 spin_unlock_irqrestore(&card->hysdn_lock, flags);
222 } else {
223 mutex_unlock(&hysdn_log_mutex);
224 return (-EPERM);
225 }
226 mutex_unlock(&hysdn_log_mutex);
227 return nonseekable_open(ino, filep);
228 }
229
230
231
232
233
234
235
236
237 static int
238 hysdn_log_close(struct inode *ino, struct file *filep)
239 {
240 struct log_data *inf;
241 struct procdata *pd;
242 hysdn_card *card;
243 int retval = 0;
244
245 mutex_lock(&hysdn_log_mutex);
246 if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
247
248 retval = 0;
249 } else {
250
251
252 inf = *((struct log_data **) filep->private_data);
253 if (inf)
254 pd = (struct procdata *) inf->proc_ctrl;
255 else {
256
257 card = PDE_DATA(file_inode(filep));
258 pd = card->proclog;
259 }
260 if (pd)
261 pd->if_used--;
262
263 while (inf) {
264 inf->usage_cnt--;
265 inf = inf->next;
266 }
267
268 if (pd)
269 if (pd->if_used <= 0)
270 while (pd->log_head) {
271 inf = pd->log_head;
272 pd->log_head = pd->log_head->next;
273 kfree(inf);
274 }
275 }
276 mutex_unlock(&hysdn_log_mutex);
277
278 return (retval);
279 }
280
281
282
283
284 static __poll_t
285 hysdn_log_poll(struct file *file, poll_table *wait)
286 {
287 __poll_t mask = 0;
288 hysdn_card *card = PDE_DATA(file_inode(file));
289 struct procdata *pd = card->proclog;
290
291 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
292 return (mask);
293
294 poll_wait(file, &(pd->rd_queue), wait);
295
296 if (*((struct log_data **) file->private_data))
297 mask |= EPOLLIN | EPOLLRDNORM;
298
299 return mask;
300 }
301
302
303
304
305 static const struct file_operations log_fops =
306 {
307 .owner = THIS_MODULE,
308 .llseek = no_llseek,
309 .read = hysdn_log_read,
310 .write = hysdn_log_write,
311 .poll = hysdn_log_poll,
312 .open = hysdn_log_open,
313 .release = hysdn_log_close,
314 };
315
316
317
318
319
320
321 int
322 hysdn_proclog_init(hysdn_card *card)
323 {
324 struct procdata *pd;
325
326
327
328 if ((pd = kzalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
329 sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
330 pd->log = proc_create_data(pd->log_name,
331 S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry,
332 &log_fops, card);
333
334 init_waitqueue_head(&(pd->rd_queue));
335
336 card->proclog = (void *) pd;
337 }
338 return (0);
339 }
340
341
342
343
344
345
346 void
347 hysdn_proclog_release(hysdn_card *card)
348 {
349 struct procdata *pd;
350
351 if ((pd = (struct procdata *) card->proclog) != NULL) {
352 if (pd->log)
353 remove_proc_entry(pd->log_name, hysdn_proc_entry);
354 kfree(pd);
355 card->proclog = NULL;
356 }
357 }