]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/proc.c
net: convert %p usage to %pK
[socketcan-devel.git] / kernel / 2.6 / net / can / proc.c
1 /*
2  * proc.c - procfs support for Protocol family CAN core module
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <socketcan-users@lists.berlios.de>
41  *
42  */
43
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/proc_fs.h>
47 #include <linux/list.h>
48 #include <linux/rcupdate.h>
49 #include <socketcan/can/core.h>
50
51 #include "af_can.h"
52 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
53 #include "compat.h"
54 #endif
55
56 #include <socketcan/can/version.h> /* for RCSID. Removed by mkpatch script */
57 RCSID("$Id$");
58
59 /*
60  * proc filenames for the PF_CAN core
61  */
62
63 #define CAN_PROC_VERSION     "version"
64 #define CAN_PROC_STATS       "stats"
65 #define CAN_PROC_RESET_STATS "reset_stats"
66 #define CAN_PROC_RCVLIST_ALL "rcvlist_all"
67 #define CAN_PROC_RCVLIST_FIL "rcvlist_fil"
68 #define CAN_PROC_RCVLIST_INV "rcvlist_inv"
69 #define CAN_PROC_RCVLIST_SFF "rcvlist_sff"
70 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff"
71 #define CAN_PROC_RCVLIST_ERR "rcvlist_err"
72
73 static struct proc_dir_entry *can_dir;
74 static struct proc_dir_entry *pde_version;
75 static struct proc_dir_entry *pde_stats;
76 static struct proc_dir_entry *pde_reset_stats;
77 static struct proc_dir_entry *pde_rcvlist_all;
78 static struct proc_dir_entry *pde_rcvlist_fil;
79 static struct proc_dir_entry *pde_rcvlist_inv;
80 static struct proc_dir_entry *pde_rcvlist_sff;
81 static struct proc_dir_entry *pde_rcvlist_eff;
82 static struct proc_dir_entry *pde_rcvlist_err;
83
84 static int user_reset;
85
86 static const char rx_list_name[][8] = {
87         [RX_ERR] = "rx_err",
88         [RX_ALL] = "rx_all",
89         [RX_FIL] = "rx_fil",
90         [RX_INV] = "rx_inv",
91         [RX_EFF] = "rx_eff",
92 };
93
94 /*
95  * af_can statistics stuff
96  */
97
98 static void can_init_stats(void)
99 {
100         /*
101          * This memset function is called from a timer context (when
102          * can_stattimer is active which is the default) OR in a process
103          * context (reading the proc_fs when can_stattimer is disabled).
104          */
105         memset(&can_stats, 0, sizeof(can_stats));
106         can_stats.jiffies_init = jiffies;
107
108         can_pstats.stats_reset++;
109
110         if (user_reset) {
111                 user_reset = 0;
112                 can_pstats.user_reset++;
113         }
114 }
115
116 static unsigned long calc_rate(unsigned long oldjif, unsigned long newjif,
117                                unsigned long count)
118 {
119         unsigned long rate;
120
121         if (oldjif == newjif)
122                 return 0;
123
124         /* see can_stat_update() - this should NEVER happen! */
125         if (count > (ULONG_MAX / HZ)) {
126                 printk(KERN_ERR "can: calc_rate: count exceeded! %ld\n",
127                        count);
128                 return 99999999;
129         }
130
131         rate = (count * HZ) / (newjif - oldjif);
132
133         return rate;
134 }
135
136 void can_stat_update(unsigned long data)
137 {
138         unsigned long j = jiffies; /* snapshot */
139
140         /* restart counting in timer context on user request */
141         if (user_reset)
142                 can_init_stats();
143
144         /* restart counting on jiffies overflow */
145         if (j < can_stats.jiffies_init)
146                 can_init_stats();
147
148         /* prevent overflow in calc_rate() */
149         if (can_stats.rx_frames > (ULONG_MAX / HZ))
150                 can_init_stats();
151
152         /* prevent overflow in calc_rate() */
153         if (can_stats.tx_frames > (ULONG_MAX / HZ))
154                 can_init_stats();
155
156         /* matches overflow - very improbable */
157         if (can_stats.matches > (ULONG_MAX / 100))
158                 can_init_stats();
159
160         /* calc total values */
161         if (can_stats.rx_frames)
162                 can_stats.total_rx_match_ratio = (can_stats.matches * 100) /
163                         can_stats.rx_frames;
164
165         can_stats.total_tx_rate = calc_rate(can_stats.jiffies_init, j,
166                                             can_stats.tx_frames);
167         can_stats.total_rx_rate = calc_rate(can_stats.jiffies_init, j,
168                                             can_stats.rx_frames);
169
170         /* calc current values */
171         if (can_stats.rx_frames_delta)
172                 can_stats.current_rx_match_ratio =
173                         (can_stats.matches_delta * 100) /
174                         can_stats.rx_frames_delta;
175
176         can_stats.current_tx_rate = calc_rate(0, HZ, can_stats.tx_frames_delta);
177         can_stats.current_rx_rate = calc_rate(0, HZ, can_stats.rx_frames_delta);
178
179         /* check / update maximum values */
180         if (can_stats.max_tx_rate < can_stats.current_tx_rate)
181                 can_stats.max_tx_rate = can_stats.current_tx_rate;
182
183         if (can_stats.max_rx_rate < can_stats.current_rx_rate)
184                 can_stats.max_rx_rate = can_stats.current_rx_rate;
185
186         if (can_stats.max_rx_match_ratio < can_stats.current_rx_match_ratio)
187                 can_stats.max_rx_match_ratio = can_stats.current_rx_match_ratio;
188
189         /* clear values for 'current rate' calculation */
190         can_stats.tx_frames_delta = 0;
191         can_stats.rx_frames_delta = 0;
192         can_stats.matches_delta   = 0;
193
194         /* restart timer (one second) */
195         mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
196 }
197
198 /*
199  * proc read functions
200  *
201  * From known use-cases we expect about 10 entries in a receive list to be
202  * printed in the proc_fs. So PAGE_SIZE is definitely enough space here.
203  *
204  */
205
206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
207 static void can_print_rcvlist(struct seq_file *m, struct hlist_head *rx_list,
208                               struct net_device *dev)
209 {
210         struct receiver *r;
211         struct hlist_node *n;
212
213         hlist_for_each_entry_rcu(r, n, rx_list, list) {
214                 char *fmt = (r->can_id & CAN_EFF_FLAG)?
215 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
216                         "   %-5s  %08x  %08x  %p  %p  %8ld  %s\n" :
217                         "   %-5s     %03x    %08x  %p  %p  %8ld  %s\n";
218 #else
219                         "   %-5s  %08x  %08x  %pK  %pK  %8ld  %s\n" :
220                         "   %-5s     %03x    %08x  %pK  %pK  %8ld  %s\n";
221 #endif
222
223                 seq_printf(m, fmt, DNAME(dev), r->can_id, r->mask,
224                                 r->func, r->data, r->matches, r->ident);
225         }
226 }
227
228 static void can_print_recv_banner(struct seq_file *m)
229 {
230         /*
231          *                  can1.  00000000  00000000  00000000
232          *                 .......          0  tp20
233          */
234         seq_puts(m, "  device   can_id   can_mask  function"
235                         "  userdata   matches  ident\n");
236 }
237
238 static int can_stats_proc_show(struct seq_file *m, void *v)
239 {
240         seq_putc(m, '\n');
241         seq_printf(m, " %8ld transmitted frames (TXF)\n", can_stats.tx_frames);
242         seq_printf(m, " %8ld received frames (RXF)\n", can_stats.rx_frames);
243         seq_printf(m, " %8ld matched frames (RXMF)\n", can_stats.matches);
244
245         seq_putc(m, '\n');
246
247         if (can_stattimer.function == can_stat_update) {
248                 seq_printf(m, " %8ld %% total match ratio (RXMR)\n",
249                                 can_stats.total_rx_match_ratio);
250
251                 seq_printf(m, " %8ld frames/s total tx rate (TXR)\n",
252                                 can_stats.total_tx_rate);
253                 seq_printf(m, " %8ld frames/s total rx rate (RXR)\n",
254                                 can_stats.total_rx_rate);
255
256                 seq_putc(m, '\n');
257
258                 seq_printf(m, " %8ld %% current match ratio (CRXMR)\n",
259                                 can_stats.current_rx_match_ratio);
260
261                 seq_printf(m, " %8ld frames/s current tx rate (CTXR)\n",
262                                 can_stats.current_tx_rate);
263                 seq_printf(m, " %8ld frames/s current rx rate (CRXR)\n",
264                                 can_stats.current_rx_rate);
265
266                 seq_putc(m, '\n');
267
268                 seq_printf(m, " %8ld %% max match ratio (MRXMR)\n",
269                                 can_stats.max_rx_match_ratio);
270
271                 seq_printf(m, " %8ld frames/s max tx rate (MTXR)\n",
272                                 can_stats.max_tx_rate);
273                 seq_printf(m, " %8ld frames/s max rx rate (MRXR)\n",
274                                 can_stats.max_rx_rate);
275
276                 seq_putc(m, '\n');
277         }
278
279         seq_printf(m, " %8ld current receive list entries (CRCV)\n",
280                         can_pstats.rcv_entries);
281         seq_printf(m, " %8ld maximum receive list entries (MRCV)\n",
282                         can_pstats.rcv_entries_max);
283
284         if (can_pstats.stats_reset)
285                 seq_printf(m, "\n %8ld statistic resets (STR)\n",
286                                 can_pstats.stats_reset);
287
288         if (can_pstats.user_reset)
289                 seq_printf(m, " %8ld user statistic resets (USTR)\n",
290                                 can_pstats.user_reset);
291
292         seq_putc(m, '\n');
293         return 0;
294 }
295
296 static int can_stats_proc_open(struct inode *inode, struct file *file)
297 {
298         return single_open(file, can_stats_proc_show, NULL);
299 }
300
301 static const struct file_operations can_stats_proc_fops = {
302         .owner          = THIS_MODULE,
303         .open           = can_stats_proc_open,
304         .read           = seq_read,
305         .llseek         = seq_lseek,
306         .release        = single_release,
307 };
308
309 static int can_reset_stats_proc_show(struct seq_file *m, void *v)
310 {
311         user_reset = 1;
312
313         if (can_stattimer.function == can_stat_update) {
314                 seq_printf(m, "Scheduled statistic reset #%ld.\n",
315                                 can_pstats.stats_reset + 1);
316
317         } else {
318                 if (can_stats.jiffies_init != jiffies)
319                         can_init_stats();
320
321                 seq_printf(m, "Performed statistic reset #%ld.\n",
322                                 can_pstats.stats_reset);
323         }
324         return 0;
325 }
326
327 static int can_reset_stats_proc_open(struct inode *inode, struct file *file)
328 {
329         return single_open(file, can_reset_stats_proc_show, NULL);
330 }
331
332 static const struct file_operations can_reset_stats_proc_fops = {
333         .owner          = THIS_MODULE,
334         .open           = can_reset_stats_proc_open,
335         .read           = seq_read,
336         .llseek         = seq_lseek,
337         .release        = single_release,
338 };
339
340 static int can_version_proc_show(struct seq_file *m, void *v)
341 {
342         seq_printf(m, "%s\n", CAN_VERSION_STRING);
343         return 0;
344 }
345
346 static int can_version_proc_open(struct inode *inode, struct file *file)
347 {
348         return single_open(file, can_version_proc_show, NULL);
349 }
350
351 static const struct file_operations can_version_proc_fops = {
352         .owner          = THIS_MODULE,
353         .open           = can_version_proc_open,
354         .read           = seq_read,
355         .llseek         = seq_lseek,
356         .release        = single_release,
357 };
358
359 static int can_rcvlist_proc_show(struct seq_file *m, void *v)
360 {
361         /* double cast to prevent GCC warning */
362         int idx = (int)(long)m->private;
363         struct dev_rcv_lists *d;
364         struct hlist_node *n;
365
366         seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]);
367
368         rcu_read_lock();
369         hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
370
371                 if (!hlist_empty(&d->rx[idx])) {
372                         can_print_recv_banner(m);
373                         can_print_rcvlist(m, &d->rx[idx], d->dev);
374                 } else
375                         seq_printf(m, "  (%s: no entry)\n", DNAME(d->dev));
376         }
377         rcu_read_unlock();
378
379         seq_putc(m, '\n');
380         return 0;
381 }
382
383 static int can_rcvlist_proc_open(struct inode *inode, struct file *file)
384 {
385         return single_open(file, can_rcvlist_proc_show, PDE(inode)->data);
386 }
387
388 static const struct file_operations can_rcvlist_proc_fops = {
389         .owner          = THIS_MODULE,
390         .open           = can_rcvlist_proc_open,
391         .read           = seq_read,
392         .llseek         = seq_lseek,
393         .release        = single_release,
394 };
395
396 static int can_rcvlist_sff_proc_show(struct seq_file *m, void *v)
397 {
398         struct dev_rcv_lists *d;
399         struct hlist_node *n;
400
401         /* RX_SFF */
402         seq_puts(m, "\nreceive list 'rx_sff':\n");
403
404         rcu_read_lock();
405         hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
406                 int i, all_empty = 1;
407                 /* check wether at least one list is non-empty */
408                 for (i = 0; i < 0x800; i++)
409                         if (!hlist_empty(&d->rx_sff[i])) {
410                                 all_empty = 0;
411                                 break;
412                         }
413
414                 if (!all_empty) {
415                         can_print_recv_banner(m);
416                         for (i = 0; i < 0x800; i++) {
417                                 if (!hlist_empty(&d->rx_sff[i]))
418                                         can_print_rcvlist(m, &d->rx_sff[i],
419                                                           d->dev);
420                         }
421                 } else
422                         seq_printf(m, "  (%s: no entry)\n", DNAME(d->dev));
423         }
424         rcu_read_unlock();
425
426         seq_putc(m, '\n');
427         return 0;
428 }
429
430 static int can_rcvlist_sff_proc_open(struct inode *inode, struct file *file)
431 {
432         return single_open(file, can_rcvlist_sff_proc_show, NULL);
433 }
434
435 static const struct file_operations can_rcvlist_sff_proc_fops = {
436         .owner          = THIS_MODULE,
437         .open           = can_rcvlist_sff_proc_open,
438         .read           = seq_read,
439         .llseek         = seq_lseek,
440         .release        = single_release,
441 };
442 #else
443 static int can_print_rcvlist(char *page, int len, struct hlist_head *rx_list,
444                              struct net_device *dev)
445 {
446         struct receiver *r;
447         struct hlist_node *n;
448
449         hlist_for_each_entry_rcu(r, n, rx_list, list) {
450                 char *fmt = (r->can_id & CAN_EFF_FLAG)?
451                         "   %-5s  %08X  %08x  %08x  %08x  %8ld  %s\n" :
452                         "   %-5s     %03X    %08x  %08lx  %08lx  %8ld  %s\n";
453
454                 len += snprintf(page + len, PAGE_SIZE - len, fmt,
455                                 DNAME(dev), r->can_id, r->mask,
456                                 (unsigned long)r->func, (unsigned long)r->data,
457                                 r->matches, r->ident);
458
459                 /* does a typical line fit into the current buffer? */
460
461                 /* 100 Bytes before end of buffer */
462                 if (len > PAGE_SIZE - 100) {
463                         /* mark output cut off */
464                         len += snprintf(page + len, PAGE_SIZE - len,
465                                         "   (..)\n");
466                         break;
467                 }
468         }
469
470         return len;
471 }
472
473 static int can_print_recv_banner(char *page, int len)
474 {
475         /*
476          *                  can1.  00000000  00000000  00000000
477          *                 .......          0  tp20
478          */
479         len += snprintf(page + len, PAGE_SIZE - len,
480                         "  device   can_id   can_mask  function"
481                         "  userdata   matches  ident\n");
482
483         return len;
484 }
485
486 static int can_proc_read_stats(char *page, char **start, off_t off,
487                                int count, int *eof, void *data)
488 {
489         int len = 0;
490
491         len += snprintf(page + len, PAGE_SIZE - len, "\n");
492         len += snprintf(page + len, PAGE_SIZE - len,
493                         " %8ld transmitted frames (TXF)\n",
494                         can_stats.tx_frames);
495         len += snprintf(page + len, PAGE_SIZE - len,
496                         " %8ld received frames (RXF)\n", can_stats.rx_frames);
497         len += snprintf(page + len, PAGE_SIZE - len,
498                         " %8ld matched frames (RXMF)\n", can_stats.matches);
499
500         len += snprintf(page + len, PAGE_SIZE - len, "\n");
501
502         if (can_stattimer.function == can_stat_update) {
503                 len += snprintf(page + len, PAGE_SIZE - len,
504                                 " %8ld %% total match ratio (RXMR)\n",
505                                 can_stats.total_rx_match_ratio);
506
507                 len += snprintf(page + len, PAGE_SIZE - len,
508                                 " %8ld frames/s total tx rate (TXR)\n",
509                                 can_stats.total_tx_rate);
510                 len += snprintf(page + len, PAGE_SIZE - len,
511                                 " %8ld frames/s total rx rate (RXR)\n",
512                                 can_stats.total_rx_rate);
513
514                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
515
516                 len += snprintf(page + len, PAGE_SIZE - len,
517                                 " %8ld %% current match ratio (CRXMR)\n",
518                                 can_stats.current_rx_match_ratio);
519
520                 len += snprintf(page + len, PAGE_SIZE - len,
521                                 " %8ld frames/s current tx rate (CTXR)\n",
522                                 can_stats.current_tx_rate);
523                 len += snprintf(page + len, PAGE_SIZE - len,
524                                 " %8ld frames/s current rx rate (CRXR)\n",
525                                 can_stats.current_rx_rate);
526
527                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
528
529                 len += snprintf(page + len, PAGE_SIZE - len,
530                                 " %8ld %% max match ratio (MRXMR)\n",
531                                 can_stats.max_rx_match_ratio);
532
533                 len += snprintf(page + len, PAGE_SIZE - len,
534                                 " %8ld frames/s max tx rate (MTXR)\n",
535                                 can_stats.max_tx_rate);
536                 len += snprintf(page + len, PAGE_SIZE - len,
537                                 " %8ld frames/s max rx rate (MRXR)\n",
538                                 can_stats.max_rx_rate);
539
540                 len += snprintf(page + len, PAGE_SIZE - len, "\n");
541         }
542
543         len += snprintf(page + len, PAGE_SIZE - len,
544                         " %8ld current receive list entries (CRCV)\n",
545                         can_pstats.rcv_entries);
546         len += snprintf(page + len, PAGE_SIZE - len,
547                         " %8ld maximum receive list entries (MRCV)\n",
548                         can_pstats.rcv_entries_max);
549
550         if (can_pstats.stats_reset)
551                 len += snprintf(page + len, PAGE_SIZE - len,
552                                 "\n %8ld statistic resets (STR)\n",
553                                 can_pstats.stats_reset);
554
555         if (can_pstats.user_reset)
556                 len += snprintf(page + len, PAGE_SIZE - len,
557                                 " %8ld user statistic resets (USTR)\n",
558                                 can_pstats.user_reset);
559
560         len += snprintf(page + len, PAGE_SIZE - len, "\n");
561
562         *eof = 1;
563         return len;
564 }
565
566 static int can_proc_read_reset_stats(char *page, char **start, off_t off,
567                                      int count, int *eof, void *data)
568 {
569         int len = 0;
570
571         user_reset = 1;
572
573         if (can_stattimer.function == can_stat_update) {
574                 len += snprintf(page + len, PAGE_SIZE - len,
575                                 "Scheduled statistic reset #%ld.\n",
576                                 can_pstats.stats_reset + 1);
577
578         } else {
579                 if (can_stats.jiffies_init != jiffies)
580                         can_init_stats();
581
582                 len += snprintf(page + len, PAGE_SIZE - len,
583                                 "Performed statistic reset #%ld.\n",
584                                 can_pstats.stats_reset);
585         }
586
587         *eof = 1;
588         return len;
589 }
590
591 static int can_proc_read_version(char *page, char **start, off_t off,
592                                  int count, int *eof, void *data)
593 {
594         int len = 0;
595
596         len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
597                         CAN_VERSION_STRING);
598         *eof = 1;
599         return len;
600 }
601
602 static int can_proc_read_rcvlist(char *page, char **start, off_t off,
603                                  int count, int *eof, void *data)
604 {
605         /* double cast to prevent GCC warning */
606         int idx = (int)(long)data;
607         int len = 0;
608         struct dev_rcv_lists *d;
609         struct hlist_node *n;
610
611         len += snprintf(page + len, PAGE_SIZE - len,
612                         "\nreceive list '%s':\n", rx_list_name[idx]);
613
614         rcu_read_lock();
615         hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
616
617                 if (!hlist_empty(&d->rx[idx])) {
618                         len = can_print_recv_banner(page, len);
619                         len = can_print_rcvlist(page, len, &d->rx[idx], d->dev);
620                 } else
621                         len += snprintf(page + len, PAGE_SIZE - len,
622                                         "  (%s: no entry)\n", DNAME(d->dev));
623
624                 /* exit on end of buffer? */
625                 if (len > PAGE_SIZE - 100)
626                         break;
627         }
628         rcu_read_unlock();
629
630         len += snprintf(page + len, PAGE_SIZE - len, "\n");
631
632         *eof = 1;
633         return len;
634 }
635
636 static int can_proc_read_rcvlist_sff(char *page, char **start, off_t off,
637                                      int count, int *eof, void *data)
638 {
639         int len = 0;
640         struct dev_rcv_lists *d;
641         struct hlist_node *n;
642
643         /* RX_SFF */
644         len += snprintf(page + len, PAGE_SIZE - len,
645                         "\nreceive list 'rx_sff':\n");
646
647         rcu_read_lock();
648         hlist_for_each_entry_rcu(d, n, &can_rx_dev_list, list) {
649                 int i, all_empty = 1;
650                 /* check wether at least one list is non-empty */
651                 for (i = 0; i < 0x800; i++)
652                         if (!hlist_empty(&d->rx_sff[i])) {
653                                 all_empty = 0;
654                                 break;
655                         }
656
657                 if (!all_empty) {
658                         len = can_print_recv_banner(page, len);
659                         for (i = 0; i < 0x800; i++) {
660                                 if (!hlist_empty(&d->rx_sff[i]) &&
661                                     len < PAGE_SIZE - 100)
662                                         len = can_print_rcvlist(page, len,
663                                                                 &d->rx_sff[i],
664                                                                 d->dev);
665                         }
666                 } else
667                         len += snprintf(page + len, PAGE_SIZE - len,
668                                         "  (%s: no entry)\n", DNAME(d->dev));
669
670                 /* exit on end of buffer? */
671                 if (len > PAGE_SIZE - 100)
672                         break;
673         }
674         rcu_read_unlock();
675
676         len += snprintf(page + len, PAGE_SIZE - len, "\n");
677
678         *eof = 1;
679         return len;
680 }
681 #endif
682
683 /*
684  * proc utility functions
685  */
686
687 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
688 static struct proc_dir_entry *can_create_proc_readentry(const char *name,
689                                                         mode_t mode,
690                                                         read_proc_t *read_proc,
691                                                         void *data)
692 {
693         if (can_dir)
694                 return create_proc_read_entry(name, mode, can_dir, read_proc,
695                                               data);
696         else
697                 return NULL;
698 }
699 #endif
700
701 static void can_remove_proc_readentry(const char *name)
702 {
703         if (can_dir)
704                 remove_proc_entry(name, can_dir);
705 }
706
707 /*
708  * can_init_proc - create main CAN proc directory and procfs entries
709  */
710 void can_init_proc(void)
711 {
712         /* create /proc/net/can directory */
713 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
714         can_dir = proc_mkdir("can", init_net.proc_net);
715 #else
716         can_dir = proc_mkdir("can", proc_net);
717 #endif
718
719         if (!can_dir) {
720                 printk(KERN_INFO "can: failed to create /proc/net/can . "
721                        "CONFIG_PROC_FS missing?\n");
722                 return;
723         }
724
725 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
726         can_dir->owner = THIS_MODULE;
727 #endif
728
729         /* own procfs entries from the AF_CAN core */
730 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
731         pde_version     = proc_create(CAN_PROC_VERSION, 0644, can_dir,
732                                       &can_version_proc_fops);
733         pde_stats       = proc_create(CAN_PROC_STATS, 0644, can_dir,
734                                       &can_stats_proc_fops);
735         pde_reset_stats = proc_create(CAN_PROC_RESET_STATS, 0644, can_dir,
736                                       &can_reset_stats_proc_fops);
737         pde_rcvlist_err = proc_create_data(CAN_PROC_RCVLIST_ERR, 0644, can_dir,
738                                            &can_rcvlist_proc_fops, (void *)RX_ERR);
739         pde_rcvlist_all = proc_create_data(CAN_PROC_RCVLIST_ALL, 0644, can_dir,
740                                            &can_rcvlist_proc_fops, (void *)RX_ALL);
741         pde_rcvlist_fil = proc_create_data(CAN_PROC_RCVLIST_FIL, 0644, can_dir,
742                                            &can_rcvlist_proc_fops, (void *)RX_FIL);
743         pde_rcvlist_inv = proc_create_data(CAN_PROC_RCVLIST_INV, 0644, can_dir,
744                                            &can_rcvlist_proc_fops, (void *)RX_INV);
745         pde_rcvlist_eff = proc_create_data(CAN_PROC_RCVLIST_EFF, 0644, can_dir,
746                                            &can_rcvlist_proc_fops, (void *)RX_EFF);
747         pde_rcvlist_sff = proc_create(CAN_PROC_RCVLIST_SFF, 0644, can_dir,
748                                       &can_rcvlist_sff_proc_fops);
749 #else
750         pde_version     = can_create_proc_readentry(CAN_PROC_VERSION, 0644,
751                                         can_proc_read_version, NULL);
752         pde_stats       = can_create_proc_readentry(CAN_PROC_STATS, 0644,
753                                         can_proc_read_stats, NULL);
754         pde_reset_stats = can_create_proc_readentry(CAN_PROC_RESET_STATS, 0644,
755                                         can_proc_read_reset_stats, NULL);
756         pde_rcvlist_err = can_create_proc_readentry(CAN_PROC_RCVLIST_ERR, 0644,
757                                         can_proc_read_rcvlist, (void *)RX_ERR);
758         pde_rcvlist_all = can_create_proc_readentry(CAN_PROC_RCVLIST_ALL, 0644,
759                                         can_proc_read_rcvlist, (void *)RX_ALL);
760         pde_rcvlist_fil = can_create_proc_readentry(CAN_PROC_RCVLIST_FIL, 0644,
761                                         can_proc_read_rcvlist, (void *)RX_FIL);
762         pde_rcvlist_inv = can_create_proc_readentry(CAN_PROC_RCVLIST_INV, 0644,
763                                         can_proc_read_rcvlist, (void *)RX_INV);
764         pde_rcvlist_eff = can_create_proc_readentry(CAN_PROC_RCVLIST_EFF, 0644,
765                                         can_proc_read_rcvlist, (void *)RX_EFF);
766         pde_rcvlist_sff = can_create_proc_readentry(CAN_PROC_RCVLIST_SFF, 0644,
767                                         can_proc_read_rcvlist_sff, NULL);
768 #endif
769 }
770
771 /*
772  * can_remove_proc - remove procfs entries and main CAN proc directory
773  */
774 void can_remove_proc(void)
775 {
776         if (pde_version)
777                 can_remove_proc_readentry(CAN_PROC_VERSION);
778
779         if (pde_stats)
780                 can_remove_proc_readentry(CAN_PROC_STATS);
781
782         if (pde_reset_stats)
783                 can_remove_proc_readentry(CAN_PROC_RESET_STATS);
784
785         if (pde_rcvlist_err)
786                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR);
787
788         if (pde_rcvlist_all)
789                 can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL);
790
791         if (pde_rcvlist_fil)
792                 can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL);
793
794         if (pde_rcvlist_inv)
795                 can_remove_proc_readentry(CAN_PROC_RCVLIST_INV);
796
797         if (pde_rcvlist_eff)
798                 can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF);
799
800         if (pde_rcvlist_sff)
801                 can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF);
802
803         if (can_dir)
804 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
805                 proc_net_remove(&init_net, "can");
806 #else
807                 proc_net_remove("can");
808 #endif
809 }