]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm.c
Added can_id depended filter mask generation for can_rx_register() to
[socketcan-devel.git] / kernel / 2.6 / net / can / bcm.c
1 /*
2  * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
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, the following disclaimer and
12  *    the referenced file 'COPYING'.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Volkswagen nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * Alternatively, provided that this notice is retained in full, this
21  * software may be distributed under the terms of the GNU General
22  * Public License ("GPL") version 2 as distributed in the 'COPYING'
23  * file from the main directory of the linux kernel source.
24  *
25  * The provided data structures and external interfaces from this code
26  * are not restricted to be used by modules with a GPL compatible license.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
45 #include <linux/autoconf.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/net.h>
49 #include <linux/netdevice.h>
50 #include <linux/proc_fs.h>
51 #include <linux/poll.h>
52 #include <net/sock.h>
53
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
57 #include <linux/can/version.h>
58
59 RCSID("$Id$");
60
61 #ifdef CONFIG_CAN_DEBUG_CORE
62 static int debug = 0;
63 module_param(debug, int, S_IRUGO);
64 #define DBG(args...)       (debug & 1 ? \
65                                (printk(KERN_DEBUG "BCM %s: ", __func__), \
66                                 printk(args)) : 0)
67 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
68 #define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
69 #else
70 #define DBG(args...)
71 #define DBG_FRAME(args...)
72 #define DBG_SKB(skb)
73 #endif
74
75 /* use of last_frames[index].can_dlc */
76 #define RX_RECV    0x40 /* received data for this element */
77 #define RX_THR     0x80 /* element not been sent due to throttle feature */
78 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
79
80 /* get best masking value for can_rx_register() for a given single can_id */
81 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
82                         (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
83
84 #define IDENT "bcm"
85 static __initdata const char banner[] = KERN_INFO
86         "CAN: broadcast manager (bcm) socket protocol " VERSION "\n"; 
87
88 MODULE_DESCRIPTION("PF_CAN bcm sockets");
89 MODULE_LICENSE("Dual BSD/GPL");
90 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
91
92 #define GET_U64(p) (*(u64*)(p)->data) /* easy access */
93
94 struct bcm_op {
95         struct list_head list;
96         int ifindex;
97         canid_t can_id;
98         int flags;
99         unsigned long j_ival1, j_ival2, j_lastmsg;
100         unsigned long frames_abs, frames_filtered;
101         struct timer_list timer, thrtimer;
102         struct timeval ival1, ival2;
103         struct timeval rx_stamp;
104         int rx_ifindex;
105         int count;
106         int nframes;
107         int currframe;
108         struct can_frame *frames;
109         struct can_frame *last_frames;
110         struct sock *sk;
111 };
112
113 struct bcm_opt {
114         int bound;
115         int ifindex;
116         struct list_head rx_ops;
117         struct list_head tx_ops;
118         unsigned long dropped_usr_msgs;
119         struct proc_dir_entry *bcm_proc_read;
120         char procname [9]; /* pointer printed in ASCII with \0 */
121 };
122
123 static struct proc_dir_entry *proc_dir = NULL;
124
125 static int  bcm_init(struct sock *sk);
126 static void bcm_notifier(unsigned long msg, void *data);
127 static int  bcm_release(struct socket *sock);
128 static int  bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
129                         int flags);
130 static int  bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
131                         struct msghdr *msg, size_t size);
132 static int  bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
133                         struct msghdr *msg, size_t size, int flags);
134 static unsigned int bcm_poll(struct file *file, struct socket *sock,
135                              poll_table *wait);
136
137 static int  bcm_read_proc(char *page, char **start, off_t off,
138                           int count, int *eof, void *data);
139
140 static void bcm_tx_timeout_handler(unsigned long data);
141 static int  bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk);
142 static int  bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
143                          int ifindex, struct sock *sk);
144 static void bcm_can_tx(struct bcm_op *op);
145
146 static int  bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
147                          int ifindex, struct sock *sk);
148 static void bcm_rx_handler(struct sk_buff *skb, void *op);
149 static void bcm_rx_timeout_handler(unsigned long data);
150 static void bcm_rx_thr_handler(unsigned long data);
151 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
152                                 struct can_frame *rxdata);
153 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
154 static void bcm_rx_starttimer(struct bcm_op *op);
155 static void bcm_rx_update_and_send(struct bcm_op *op,
156                                    struct can_frame *lastdata,
157                                    struct can_frame *rxdata);
158 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
159                              struct can_frame *frames, struct timeval *tv);
160
161 static int  bcm_delete_tx_op(struct list_head *ops, canid_t can_id,
162                              int ifindex);
163 static int  bcm_delete_rx_op(struct list_head *ops, canid_t can_id,
164                              int ifindex);
165 static void bcm_remove_op(struct bcm_op *op);
166 static int  bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
167                         int ifindex);
168 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
169                                   int ifindex);
170
171 static struct proto_ops bcm_ops = {
172         .family        = PF_CAN,
173         .release       = bcm_release,
174         .bind          = sock_no_bind,
175         .connect       = bcm_connect,
176         .socketpair    = sock_no_socketpair,
177         .accept        = sock_no_accept,
178         .getname       = sock_no_getname,
179         .poll          = bcm_poll,
180         .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
181         .listen        = sock_no_listen,
182         .shutdown      = sock_no_shutdown,
183         .setsockopt    = sock_no_setsockopt,
184         .getsockopt    = sock_no_getsockopt,
185         .sendmsg       = bcm_sendmsg,
186         .recvmsg       = bcm_recvmsg,
187         .mmap          = sock_no_mmap,
188         .sendpage      = sock_no_sendpage,
189 };
190
191 #ifdef CONFIG_CAN_BCM_USER
192 #define BCM_CAP (-1)
193 #else
194 #define BCM_CAP CAP_NET_RAW
195 #endif
196
197 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
198 struct bcm_sock {
199         struct sock    sk;
200         struct bcm_opt opt;
201 };
202
203 #define bcm_sk(sk) (&((struct bcm_sock *)(sk))->opt)
204
205 static struct proto bcm_proto = {
206         .name       = "CAN_BCM",
207         .owner      = THIS_MODULE,
208         .obj_size   = sizeof(struct bcm_sock),
209         .init       = bcm_init,
210 };
211
212 static struct can_proto bcm_can_proto = {
213         .type       = SOCK_DGRAM,
214         .protocol   = CAN_BCM,
215         .capability = BCM_CAP,
216         .ops        = &bcm_ops,
217         .prot       = &bcm_proto,
218 };
219 #else
220 #define bcm_sk(sk) ((struct bcm_opt *)(sk)->sk_protinfo)
221
222 static struct can_proto bcm_can_proto = {
223         .type       = SOCK_DGRAM,
224         .protocol   = CAN_BCM,
225         .capability = BCM_CAP,
226         .ops        = &bcm_ops,
227         .owner      = THIS_MODULE,
228         .obj_size   = sizeof(struct bcm_opt),
229         .init       = bcm_init,
230 };
231 #endif
232
233 #define CFSIZ sizeof(struct can_frame)
234 #define OPSIZ sizeof(struct bcm_op)
235 #define MHSIZ sizeof(struct bcm_msg_head)
236
237 static int __init bcm_module_init(void)
238 {
239         printk(banner);
240
241         can_proto_register(&bcm_can_proto);
242
243         /* create /proc/net/can/bcm directory */
244         proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
245
246         if (proc_dir)
247                 proc_dir->owner = THIS_MODULE;
248
249         return 0;
250 }
251
252 static void __exit bcm_module_exit(void)
253 {
254         can_proto_unregister(&bcm_can_proto);
255
256         if (proc_dir)
257                 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
258
259 }
260
261 /**************************************************/
262 /* initial settings at socket creation time       */
263 /**************************************************/
264
265 static int bcm_init(struct sock *sk)
266 {
267         struct bcm_opt *bo = bcm_sk(sk);
268
269         bo->bound            = 0;
270         bo->ifindex          = 0;
271         bo->dropped_usr_msgs = 0;
272         bo->bcm_proc_read    = NULL;
273
274         INIT_LIST_HEAD(&bo->tx_ops);
275         INIT_LIST_HEAD(&bo->rx_ops);
276
277         return 0;
278 }
279
280 /**************************************************/
281 /* handling of netdevice problems                 */
282 /**************************************************/
283
284 static void bcm_notifier(unsigned long msg, void *data)
285 {
286         struct sock *sk = (struct sock *)data;
287         struct bcm_opt *bo = bcm_sk(sk);
288
289         DBG("called for sock %p\n", sk);
290
291         switch (msg) {
292         case NETDEV_UNREGISTER:
293                 bo->bound   = 0;
294                 bo->ifindex = 0;
295                 /* fallthrough */
296         case NETDEV_DOWN:
297                 sk->sk_err = ENETDOWN;
298                 if (!sock_flag(sk, SOCK_DEAD))
299                         sk->sk_error_report(sk);
300         }
301 }
302
303 /**************************************************/
304 /* standard socket functions                      */
305 /**************************************************/
306
307 static int bcm_release(struct socket *sock)
308 {
309         struct sock *sk = sock->sk;
310         struct bcm_opt *bo = bcm_sk(sk);
311         struct bcm_op *op, *next;
312
313         DBG("socket %p, sk %p\n", sock, sk);
314
315         /* remove bcm_ops, timer, rx_unregister(), etc. */
316
317         list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
318                 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
319                 bcm_remove_op(op);
320         }
321
322         list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
323                 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
324
325                 /* Don't care if we're bound or not (due to netdev problems) */
326                 /* can_rx_unregister() is always a save thing to do here     */
327                 if (op->ifindex) {
328                         struct net_device *dev = dev_get_by_index(op->ifindex);
329                         if (dev) {
330                                 can_rx_unregister(dev, op->can_id,
331                                                   REGMASK(op->can_id),
332                                                   bcm_rx_handler, op);
333                                 dev_put(dev);
334                         }
335                 } else
336                         can_rx_unregister(NULL, op->can_id,
337                                           REGMASK(op->can_id),
338                                           bcm_rx_handler, op);
339
340                 bcm_remove_op(op);
341         }
342
343         /* remove procfs entry */
344         if ((proc_dir) && (bo->bcm_proc_read)) {
345                 remove_proc_entry(bo->procname, proc_dir);
346         }
347
348         /* remove device notifier */
349         if (bo->ifindex) {
350                 struct net_device *dev = dev_get_by_index(bo->ifindex);
351                 if (dev) {
352                         can_dev_unregister(dev, bcm_notifier, sk);
353                         dev_put(dev);
354                 }
355         }
356
357         sock_put(sk);
358
359         return 0;
360 }
361
362 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
363                        int flags)
364 {
365         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
366         struct sock *sk = sock->sk;
367         struct bcm_opt *bo = bcm_sk(sk);
368
369         if (bo->bound)
370                 return -EISCONN;
371
372         /* bind a device to this socket */
373         if (addr->can_ifindex) {
374                 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
375                 if (!dev) {
376                         DBG("could not find device index %d\n",
377                             addr->can_ifindex);
378                         return -ENODEV;
379                 }
380                 bo->ifindex = dev->ifindex;
381                 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
382                 dev_put(dev);
383
384                 DBG("socket %p bound to device %s (idx %d)\n",
385                     sock, dev->name, dev->ifindex);
386         } else {
387                 /* no notifier for ifindex = 0 ('any' CAN device) */
388                 bo->ifindex = 0;
389         }
390
391         bo->bound = 1;
392
393         if (proc_dir) {
394                 /* unique socket address as filename */
395                 sprintf(bo->procname, "%p", sock);
396                 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
397                                                            proc_dir,
398                                                            bcm_read_proc, sk);
399         }
400
401         return 0;
402 }
403
404 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
405                        struct msghdr *msg, size_t size)
406 {
407         struct sock *sk = sock->sk;
408         struct bcm_opt *bo = bcm_sk(sk);
409         int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
410         struct bcm_msg_head msg_head;
411         int ret; /* read bytes or error codes as return value */
412
413         if (!bo->bound) {
414                 DBG("sock %p not bound\n", sk);
415                 return -ENOTCONN;
416         }
417
418         /* check for alternative ifindex for this bcm_op */
419
420         if (!ifindex && msg->msg_name) { /* no bound device as default */
421                 struct sockaddr_can *addr = 
422                         (struct sockaddr_can *)msg->msg_name;
423                 if (addr->can_family != AF_CAN)
424                         return -EINVAL;
425                 ifindex = addr->can_ifindex; /* ifindex from sendto() */
426
427                 if (ifindex && !dev_get_by_index(ifindex)) {
428                         DBG("device %d not found\n", ifindex);
429                         return -ENODEV;
430                 }
431         }
432
433         /* read message head information */
434
435         if ((ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov,
436                                     MHSIZ)) < 0)
437                 return ret;
438
439         DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
440
441         switch (msg_head.opcode) {
442
443         case TX_SETUP:
444
445                 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
446                 break;
447
448         case RX_SETUP:
449
450                 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
451                 break;
452
453         case TX_DELETE:
454
455                 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
456                         ret = MHSIZ;
457                 else
458                         ret = -EINVAL;
459                 break;
460                     
461         case RX_DELETE:
462
463                 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
464                         ret = MHSIZ;
465                 else
466                         ret = -EINVAL;
467                 break;
468
469         case TX_READ:
470
471                 /* reuse msg_head for the reply */
472                 msg_head.opcode  = TX_STATUS; /* reply to TX_READ */
473                 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
474                 break;
475
476         case RX_READ:
477
478                 /* reuse msg_head for the reply */
479                 msg_head.opcode  = RX_STATUS; /* reply to RX_READ */
480                 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
481                 break;
482
483         case TX_SEND:
484
485                 if (msg_head.nframes < 1) /* we need at least one can_frame */
486                         return -EINVAL;
487
488                 ret = bcm_tx_send(msg, ifindex, sk);
489                 break;
490
491         default:
492
493                 DBG("Unknown opcode %d\n", msg_head.opcode);
494                 ret = -EINVAL;
495                 break;
496         }
497
498         return ret;
499 }
500
501 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
502                        struct msghdr *msg, size_t size, int flags)
503 {
504         struct sock *sk = sock->sk;
505         struct sk_buff *skb;
506         int error = 0;
507         int noblock;
508         int err;
509
510         DBG("socket %p, sk %p\n", sock, sk);
511
512         noblock =  flags & MSG_DONTWAIT;
513         flags   &= ~MSG_DONTWAIT;
514         if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
515                 return error;
516         }
517
518         DBG("delivering skbuff %p\n", skb);
519         DBG_SKB(skb);
520
521         if (skb->len < size)
522                 size = skb->len;
523         if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
524                 skb_free_datagram(sk, skb);
525                 return err;
526         }
527
528         sock_recv_timestamp(msg, sk, skb);
529
530         if (msg->msg_name) {
531                 msg->msg_namelen = sizeof(struct sockaddr_can);
532                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
533         }
534
535         DBG("freeing sock %p, skbuff %p\n", sk, skb);
536         skb_free_datagram(sk, skb);
537
538         return size;
539 }
540
541 static unsigned int bcm_poll(struct file *file, struct socket *sock,
542                              poll_table *wait)
543 {
544         unsigned int mask = 0;
545
546         DBG("socket %p\n", sock);
547
548         mask = datagram_poll(file, sock, wait);
549         return mask;
550 }
551
552 /**************************************************/
553 /* helper functions for bcm_sendmsg()             */
554 /**************************************************/
555
556 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
557                         int ifindex, struct sock *sk)
558 {
559         struct bcm_opt *bo = bcm_sk(sk);
560         struct bcm_op *op;
561         int i, err;
562
563         if (!ifindex) /* we need a real device to send frames */
564                 return -ENODEV;
565
566         if (msg_head->nframes < 1) /* we need at least one can_frame */
567                 return -EINVAL;
568
569         /* check the given can_id */
570
571         if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) {
572
573                 /* update existing BCM operation */
574
575                 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
576                     op, msg_head->can_id);
577
578                 /* Do we need more space for the can_frames than currently */
579                 /* allocated? -> This is a _really_ unusual use-case and   */
580                 /* therefore (complexity / locking) it is not supported.   */
581                 if (msg_head->nframes > op->nframes)
582                         return -E2BIG;
583
584                 /* update can_frames content */
585                 for (i = 0; i < msg_head->nframes; i++) {
586                         if ((err = memcpy_fromiovec((u8*)&op->frames[i],
587                                                     msg->msg_iov, CFSIZ)) < 0)
588                                 return err;
589
590                         if (msg_head->flags & TX_CP_CAN_ID) {
591                                 /* copy can_id into frame */
592                                 op->frames[i].can_id = msg_head->can_id;
593                         }
594                 }
595
596         } else {
597                 /* insert new BCM operation for the given can_id */
598
599                 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
600                         return -ENOMEM;
601
602                 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
603
604                 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
605                     op, msg_head->can_id);
606
607                 op->can_id    = msg_head->can_id;
608
609                 /* create array for can_frames and copy the data */
610                 if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
611                                            GFP_KERNEL))) {
612                         kfree(op);
613                         return -ENOMEM;
614                 }
615
616                 for (i = 0; i < msg_head->nframes; i++) {
617                         if ((err = memcpy_fromiovec((u8*)&op->frames[i],
618                                                     msg->msg_iov,
619                                                     CFSIZ)) < 0) {
620                                 kfree(op->frames);
621                                 kfree(op);
622                                 return err;
623                         }
624
625                         if (msg_head->flags & TX_CP_CAN_ID) {
626                                 /* copy can_id into frame */
627                                 op->frames[i].can_id = msg_head->can_id;
628                         }
629                 }
630
631                 /* tx_ops never compare with previous received messages */
632                 op->last_frames = NULL;
633
634                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
635                 op->sk = sk;
636
637                 op->ifindex = ifindex;
638
639                 /* initialize uninitialized (kmalloc) structure */
640                 init_timer(&op->timer);
641
642                 /* currently unused in tx_ops */
643                 init_timer(&op->thrtimer);
644
645                 /* handler for tx_ops */
646                 op->timer.function = bcm_tx_timeout_handler;
647
648                 /* timer.data points to this op-structure */
649                 op->timer.data = (unsigned long)op;
650
651                 /* add this bcm_op to the list of the tx_ops */
652                 list_add(&op->list, &bo->tx_ops);
653
654         } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
655
656         if (op->nframes != msg_head->nframes) {
657                 op->nframes   = msg_head->nframes;
658                 /* start multiple frame transmission with index 0 */
659                 op->currframe = 0;
660         }
661
662         /* check flags */
663
664         op->flags = msg_head->flags;
665
666         if (op->flags & TX_RESET_MULTI_IDX) {
667                 /* start multiple frame transmission with index 0 */
668                 op->currframe = 0; 
669         }
670
671         if (op->flags & SETTIMER) {
672
673                 /* set timer values */
674
675                 op->count   = msg_head->count;
676                 op->ival1   = msg_head->ival1;
677                 op->ival2   = msg_head->ival2;
678                 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
679                 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
680
681                 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
682                     op->count, op->j_ival1, op->j_ival2);
683
684                 /* disable an active timer due to zero values? */
685                 if (!op->j_ival1 && !op->j_ival2) {
686                         del_timer(&op->timer);
687                         DBG("TX_SETUP: SETTIMER disabled timer.\n");
688                 }
689         }
690
691         if ((op->flags & STARTTIMER) &&
692             ((op->j_ival1 && op->count) || op->j_ival2)) {
693
694                 del_timer(&op->timer);
695
696                 /* spec: send can_frame when starting timer */
697                 op->flags |= TX_ANNOUNCE;
698
699                 if (op->j_ival1 && (op->count > 0)){
700                         op->timer.expires = jiffies + op->j_ival1;
701                         /* op->count-- is done in bcm_tx_timeout_handler */
702                         DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
703                             "exp=0x%08X\n",
704                             op->timer.function,
705                             (char*) op->timer.data,
706                             (unsigned int) op->timer.expires);
707                 } else{
708                         op->timer.expires = jiffies + op->j_ival2;
709                         DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
710                             "exp=0x%08X\n",
711                             op->timer.function,
712                             (char*) op->timer.data,
713                             (unsigned int) op->timer.expires);
714                 }
715
716                 add_timer(&op->timer);
717         }
718
719         if (op->flags & TX_ANNOUNCE)
720                 bcm_can_tx(op);
721
722         return msg_head->nframes * CFSIZ + MHSIZ;
723 }
724
725 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
726                         int ifindex, struct sock *sk)
727 {
728         struct bcm_opt *bo = bcm_sk(sk);
729         struct bcm_op *op;
730         int do_rx_register;
731         int err;
732
733         if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
734                 /* be robust against wrong usage ... */
735                 msg_head->flags |= RX_FILTER_ID;
736                 msg_head->nframes = 0; /* ignore trailing garbage */
737         }
738
739         if ((msg_head->flags & RX_RTR_FRAME) &&
740             ((msg_head->nframes != 1) ||
741              (!(msg_head->can_id & CAN_RTR_FLAG)))) {
742
743                 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
744                 return -EINVAL;
745         }
746
747         /* check the given can_id */
748
749         if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) {
750
751                 /* update existing BCM operation */
752
753                 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
754                     op, msg_head->can_id);
755
756                 /* Do we need more space for the can_frames than currently */
757                 /* allocated? -> This is a _really_ unusual use-case and   */
758                 /* therefore (complexity / locking) it is not supported.   */
759                 if (msg_head->nframes > op->nframes)
760                         return -E2BIG;
761
762                 if (msg_head->nframes) {
763                         /* update can_frames content */
764                         if ((err = memcpy_fromiovec((u8*)op->frames,
765                                                     msg->msg_iov,
766                                                     msg_head->nframes
767                                                     * CFSIZ) < 0))
768                                 return err;
769
770                         /* clear last_frames to indicate 'nothing received' */
771                         memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
772                 }
773
774                 op->nframes = msg_head->nframes;
775                 /* Only an update -> do not call can_rx_register() */
776                 do_rx_register = 0;
777
778         } else {
779                 /* insert new BCM operation for the given can_id */
780
781                 if (!(op = kmalloc(OPSIZ, GFP_KERNEL)))
782                         return -ENOMEM;
783
784                 memset(op, 0, OPSIZ); /* init to zero, e.g. for timers */
785
786                 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
787                     op, msg_head->can_id);
788
789                 op->can_id    = msg_head->can_id;
790                 op->nframes   = msg_head->nframes;
791
792                 if (msg_head->nframes) {
793
794                         /* create array for can_frames and copy the data */
795                         if (!(op->frames = kmalloc(msg_head->nframes * CFSIZ,
796                                                    GFP_KERNEL))) {
797                                 kfree(op);
798                                 return -ENOMEM;
799                         }
800
801                         if ((err = memcpy_fromiovec((u8*)op->frames,
802                                                     msg->msg_iov,
803                                                     msg_head->nframes
804                                                     * CFSIZ)) < 0) {
805                                 kfree(op->frames);
806                                 kfree(op);
807                                 return err;
808                         }
809
810                         /* create array for received can_frames */
811                         if (!(op->last_frames = kmalloc(msg_head->nframes
812                                                         * CFSIZ,
813                                                         GFP_KERNEL))) {
814                                 kfree(op->frames);
815                                 kfree(op);
816                                 return -ENOMEM;
817                         }
818
819                         /* clear last_frames to indicate 'nothing received' */
820                         memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
821                 } else {
822                         /* op->frames = NULL due to memset */
823
824                         /* even when we have the RX_FILTER_ID case, we need */
825                         /* to store the last frame for the throttle feature */
826
827                         /* create array for received can_frames */
828                         if (!(op->last_frames = kmalloc(CFSIZ, GFP_KERNEL))) {
829                                 kfree(op);
830                                 return -ENOMEM;
831                         }
832
833                         /* clear last_frames to indicate 'nothing received' */
834                         memset(op->last_frames, 0, CFSIZ);
835                 }
836
837                 op->sk = sk; /* bcm_delete_rx_op() needs this */
838                 op->ifindex = ifindex;
839
840                 /* initialize uninitialized (kmalloc) structure */
841                 init_timer(&op->timer);
842
843                 /* init throttle timer for RX_CHANGED */
844                 init_timer(&op->thrtimer);
845
846                 /* handler for rx timeouts */
847                 op->timer.function = bcm_rx_timeout_handler;
848
849                 /* timer.data points to this op-structure */
850                 op->timer.data = (unsigned long)op;
851
852                 /* handler for RX_CHANGED throttle timeouts */
853                 op->thrtimer.function = bcm_rx_thr_handler;
854
855                 /* timer.data points to this op-structure */
856                 op->thrtimer.data = (unsigned long)op;
857
858                 op->thrtimer.expires = 0; /* mark disabled timer */
859
860                 /* add this bcm_op to the list of the tx_ops */
861                 list_add(&op->list, &bo->rx_ops);
862
863                 do_rx_register = 1; /* call can_rx_register() */
864
865         } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
866
867
868         /* check flags */
869
870         op->flags = msg_head->flags;
871
872         if (op->flags & RX_RTR_FRAME) {
873
874                 /* no timers in RTR-mode */
875                 del_timer(&op->thrtimer);
876                 del_timer(&op->timer);
877
878                 /* funny feature in RX(!)_SETUP only for RTR-mode: */
879                 /* copy can_id into frame BUT without RTR-flag to  */
880                 /* prevent a full-load-loopback-test ... ;-]       */
881                 if ((op->flags & TX_CP_CAN_ID) ||
882                     (op->frames[0].can_id == op->can_id))
883                         op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
884
885         } else {
886                 if (op->flags & SETTIMER) {
887
888                         /* set timer value */
889
890                         op->ival1   = msg_head->ival1;
891                         op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
892                         op->ival2   = msg_head->ival2;
893                         op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
894
895                         DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
896                             op->j_ival1, op->j_ival2);
897
898                         /* disable an active timer due to zero value? */
899                         if (!op->j_ival1) {
900                                 del_timer(&op->timer);
901                                 DBG("RX_SETUP: disabled timer rx timeouts.\n");
902                         }
903
904                         /* free currently blocked msgs ? */
905                         if (op->thrtimer.expires) { /* blocked by timer? */
906                                 DBG("RX_SETUP: unblocking throttled msgs.\n");
907                                 del_timer(&op->thrtimer);
908                                 /* send blocked msgs hereafter */
909                                 op->thrtimer.expires = jiffies + 2;
910                                 add_timer(&op->thrtimer);
911                         }
912                         /* if (op->j_ival2) is zero, no (new) throttling     */
913                         /* will happen. For details see functions            */
914                         /* bcm_rx_update_and_send() and bcm_rx_thr_handler() */
915                 }
916
917                 if ((op->flags & STARTTIMER) && op->j_ival1) {
918
919                         del_timer(&op->timer);
920
921                         op->timer.expires = jiffies + op->j_ival1;
922
923                         DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
924                             " exp=0x%08X\n",
925                             (char *) op->timer.function,
926                             (char *) op->timer.data,
927                             (unsigned int) op->timer.expires);
928
929                         add_timer(&op->timer);
930                 }
931         }
932
933         /* now we can register for can_ids, if we added a new bcm_op */
934         if (do_rx_register) {
935                 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
936                     "rx_op is %p\n", op->can_id, op);
937
938                 if (ifindex) {
939                         struct net_device *dev = dev_get_by_index(ifindex);
940
941                         if (dev) {
942                                 can_rx_register(dev, op->can_id,
943                                                 REGMASK(op->can_id),
944                                                 bcm_rx_handler, op, IDENT);
945                                 dev_put(dev);
946                         }
947                 } else 
948                         can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
949                                         bcm_rx_handler, op, IDENT);
950         }
951
952         return msg_head->nframes * CFSIZ + MHSIZ;
953 }
954
955 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
956 {
957         struct sk_buff *skb;
958         struct net_device *dev;
959         int err;
960
961         /* just copy and send one can_frame */
962
963         if (!ifindex) /* we need a real device to send frames */
964                 return -ENODEV;
965
966         skb = alloc_skb(CFSIZ, GFP_KERNEL);
967
968         if (!skb)
969                 return -ENOMEM;
970
971         if ((err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov,
972                                     CFSIZ)) < 0) {
973                 kfree_skb(skb);
974                 return err;
975         }
976
977         DBG_FRAME("BCM: TX_SEND: sending frame",
978                   (struct can_frame *)skb->data);
979         dev = dev_get_by_index(ifindex);
980
981         if (!dev) {
982                 kfree_skb(skb);
983                 return -ENODEV;
984         }
985
986         skb->dev = dev;
987         skb->sk  = sk;
988         can_send(skb, 1); /* send with loopback */
989         dev_put(dev);
990
991         return CFSIZ + MHSIZ;
992 }
993
994 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
995                        int ifindex)
996 {
997         struct bcm_op *op;
998         int ret;
999
1000         if ((op = bcm_find_op(ops, msg_head->can_id, ifindex))) {
1001
1002                 DBG("TRX_READ: sending status for can_id %03X\n",
1003                     msg_head->can_id);
1004                 /* put current values into msg_head */
1005                 msg_head->flags   = op->flags;
1006                 msg_head->count   = op->count;
1007                 msg_head->ival1   = op->ival1;
1008                 msg_head->ival2   = op->ival2;
1009                 msg_head->nframes = op->nframes;
1010
1011                 bcm_send_to_user(op, msg_head, op->frames, NULL);
1012
1013                 ret = MHSIZ;
1014
1015         } else {
1016
1017                 DBG("TRX_READ: did not find op for can_id %03X\n",
1018                     msg_head->can_id);
1019                 ret = -EINVAL;
1020         }
1021
1022         return ret;
1023 }
1024
1025 /**************************************************/
1026 /* procfs functions                               */
1027 /**************************************************/
1028
1029 static char *bcm_proc_getifname(int ifindex)
1030 {
1031         struct net_device *dev;
1032
1033         if (!ifindex)
1034                 return "any";
1035
1036         dev = __dev_get_by_index(ifindex); /* no usage counting */
1037         if (dev)
1038                 return dev->name;
1039
1040         return "???";
1041 }
1042
1043 static int bcm_read_proc(char *page, char **start, off_t off,
1044                          int count, int *eof, void *data)
1045 {
1046         int len = 0;
1047         struct sock *sk = (struct sock *)data;
1048         struct bcm_opt *bo = bcm_sk(sk);
1049         struct bcm_op *op;
1050
1051         len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
1052                         sk->sk_socket);
1053         len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
1054         len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
1055         len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
1056                         bo->dropped_usr_msgs);
1057         len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
1058                         bcm_proc_getifname(bo->ifindex));
1059         len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
1060
1061         list_for_each_entry(op, &bo->rx_ops, list) {
1062
1063                 unsigned long reduction;
1064
1065                 /* print only active entries & prevent division by zero */
1066                 if (!op->frames_abs)
1067                         continue;
1068
1069                 len += snprintf(page + len, PAGE_SIZE - len,
1070                                 "rx_op: %03X %-5s ",
1071                                 op->can_id, bcm_proc_getifname(op->ifindex));
1072                 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
1073                                 op->nframes,
1074                                 (op->flags & RX_CHECK_DLC)?'d':' ');
1075                 if (op->j_ival1)
1076                         len += snprintf(page + len, PAGE_SIZE - len,
1077                                         "timeo=%ld ", op->j_ival1);
1078
1079                 if (op->j_ival2)
1080                         len += snprintf(page + len, PAGE_SIZE - len,
1081                                         "thr=%ld ", op->j_ival2);
1082
1083                 len += snprintf(page + len, PAGE_SIZE - len,
1084                                 "# recv %ld (%ld) => reduction: ",
1085                                 op->frames_filtered, op->frames_abs);
1086
1087                 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
1088
1089                 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
1090                                 (reduction == 100)?"near ":"", reduction);
1091
1092                 if (len > PAGE_SIZE - 200) {
1093                         /* mark output cut off */
1094                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1095                         break;
1096                 }
1097         }
1098
1099         list_for_each_entry(op, &bo->tx_ops, list) {
1100
1101                 len += snprintf(page + len, PAGE_SIZE - len,
1102                                 "tx_op: %03X %s [%d] ",
1103                                 op->can_id, bcm_proc_getifname(op->ifindex),
1104                                 op->nframes);
1105                 if (op->j_ival1)
1106                         len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
1107                                         op->j_ival1);
1108
1109                 if (op->j_ival2)
1110                         len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
1111                                         op->j_ival2);
1112
1113                 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
1114                                 op->frames_abs);
1115
1116                 if (len > PAGE_SIZE - 100) {
1117                         /* mark output cut off */
1118                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
1119                         break;
1120                 }
1121         }
1122
1123         len += snprintf(page + len, PAGE_SIZE - len, "\n");
1124
1125         *eof = 1;
1126         return len;
1127 }
1128
1129 /**************************************************/
1130 /* bcm_op handling tx path                        */
1131 /**************************************************/
1132
1133 static void bcm_can_tx(struct bcm_op *op)
1134 {
1135         struct sk_buff *skb;
1136         struct net_device *dev;
1137         struct can_frame *cf = &op->frames[op->currframe];
1138
1139         DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1140
1141         if (!op->ifindex)
1142                 return; /* no target device -> exit */
1143
1144         dev = dev_get_by_index(op->ifindex);
1145
1146         if (!dev)
1147                 return; /* should this bcm_op remove itself here? */
1148
1149         skb = alloc_skb(CFSIZ,
1150                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1151
1152         if (!skb)
1153                 goto out; /* no memory */
1154
1155         memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
1156
1157         skb->dev = dev;
1158         skb->sk = op->sk;
1159         can_send(skb, 1); /* send with loopback */
1160
1161         op->currframe++;
1162         op->frames_abs++; /* statistics */
1163
1164         /* reached last frame? */
1165         if (op->currframe >= op->nframes)
1166                 op->currframe = 0;
1167  out:
1168         dev_put(dev);
1169 }
1170
1171 static void bcm_tx_timeout_handler(unsigned long data)
1172 {
1173         struct bcm_op *op = (struct bcm_op*)data;
1174
1175         DBG("Called with bcm_op %p\n", op);
1176
1177         if (op->j_ival1 && (op->count > 0)) {
1178
1179                 op->count--;
1180
1181                 if (!op->count && (op->flags & TX_COUNTEVT)) {
1182                         /* create notification to user */
1183
1184                         struct bcm_msg_head msg_head;
1185
1186                         DBG("sending TX_EXPIRED for can_id %03X\n",
1187                             op->can_id);
1188
1189                         msg_head.opcode  = TX_EXPIRED;
1190                         msg_head.flags   = op->flags;
1191                         msg_head.count   = op->count;
1192                         msg_head.ival1   = op->ival1;
1193                         msg_head.ival2   = op->ival2;
1194                         msg_head.can_id  = op->can_id;
1195                         msg_head.nframes = 0;
1196
1197                         bcm_send_to_user(op, &msg_head, NULL, NULL);
1198                 }
1199         }
1200
1201         DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
1202             op->count, op->j_ival1, op->j_ival2);
1203
1204         if (op->j_ival1 && (op->count > 0)) {
1205
1206                 op->timer.expires = jiffies + op->j_ival1;
1207                 add_timer(&op->timer);
1208
1209                 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
1210                     op->timer.function,
1211                     (char*) op->timer.data,
1212                     (unsigned int) op->timer.expires);
1213
1214                 bcm_can_tx(op); /* send (next) frame */
1215         } else {
1216                 if (op->j_ival2) {
1217                         op->timer.expires = jiffies + op->j_ival2;
1218                         add_timer(&op->timer);
1219
1220                         DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
1221                             op->timer.function,
1222                             (char*) op->timer.data,
1223                             (unsigned int) op->timer.expires);
1224
1225                         bcm_can_tx(op); /* send (next) frame */
1226                 } else
1227                         DBG("no timer restart\n");
1228         }
1229
1230         return;
1231
1232 }
1233
1234 /**************************************************/
1235 /* bcm_op handling rx path                        */
1236 /**************************************************/
1237
1238 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1239 {
1240         struct bcm_op *op = (struct bcm_op*)data;
1241         struct can_frame rxframe;
1242         int i;
1243
1244         del_timer(&op->timer); /* disable timeout */
1245
1246         DBG("Called with bcm_op %p\n", op);
1247
1248         if (skb->len == sizeof(rxframe)) {
1249                 memcpy(&rxframe, skb->data, sizeof(rxframe));
1250                 skb_get_timestamp(skb, &op->rx_stamp); /* save rx timestamp */
1251                 /* save originator for recvfrom() */
1252                 op->rx_ifindex = skb->dev->ifindex;
1253                 op->frames_abs++; /* statistics */
1254                 kfree_skb(skb);
1255                 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
1256         } else {
1257                 DBG("Wrong skb->len = %d\n", skb->len);
1258                 kfree_skb(skb);
1259                 return;
1260         }
1261
1262         DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1263
1264         if (op->can_id != rxframe.can_id) {
1265                 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
1266                     rxframe.can_id, op->can_id);
1267                 return;
1268         }
1269
1270         if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
1271                 DBG("RTR-request\n");
1272                 bcm_can_tx(op); /* send op->frames[0] to CAN device */
1273                 return;
1274         }
1275
1276         if (op->flags & RX_FILTER_ID) { /* the easiest case */
1277                 DBG("Easy does it with RX_FILTER_ID\n");
1278                 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1279                 bcm_rx_starttimer(op);
1280                 return;
1281         }
1282
1283         if (op->nframes == 1) { /* simple compare with index 0 */
1284                 DBG("Simple compare\n");
1285                 bcm_rx_cmp_to_index(op, 0, &rxframe);
1286                 bcm_rx_starttimer(op);
1287                 return;
1288         }
1289
1290         if (op->nframes > 1) { /* multiplex compare */
1291
1292                 DBG("Multiplex compare\n");
1293                 /* find the first multiplex mask that fits */
1294                 /* MUX-mask is in index 0 */
1295
1296                 for (i=1; i < op->nframes; i++) {
1297
1298                         if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1299                             (GET_U64(&op->frames[0]) &
1300                              GET_U64(&op->frames[i]))) {
1301                                 DBG("found MUX index %d\n", i);
1302                                 bcm_rx_cmp_to_index(op, i, &rxframe);
1303                                 break;
1304                         }
1305                 }
1306                 bcm_rx_starttimer(op);
1307         }
1308 }
1309
1310 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1311                                 struct can_frame *rxdata)
1312 {
1313         /* no one uses the MSBs of can_dlc for comparation, */
1314         /* so we use it here to detect the first time of reception */
1315
1316         if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
1317                 DBG("first time :)\n");
1318                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1319                 return;
1320         }
1321
1322         /* do a real check in can_data */
1323
1324         DBG("op->frames[index].data = 0x%016llx\n",
1325             GET_U64(&op->frames[index]));
1326         DBG("op->last_frames[index].data = 0x%016llx\n",
1327             GET_U64(&op->last_frames[index]));
1328         DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1329
1330         if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1331             (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1332                 DBG("relevant data change :)\n");
1333                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1334                 return;
1335         }
1336
1337
1338         if (op->flags & RX_CHECK_DLC) {
1339
1340                 /* do a real check in dlc */
1341
1342                 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
1343                                         BCM_CAN_DLC_MASK)) {
1344                         DBG("dlc change :)\n");
1345                         bcm_rx_update_and_send(op, &op->last_frames[index],
1346                                                rxdata);
1347                         return;
1348                 }
1349         }
1350         DBG("no relevant change :(\n");
1351 }
1352
1353 static void bcm_rx_update_and_send(struct bcm_op *op,
1354                                    struct can_frame *lastdata,
1355                                    struct can_frame *rxdata)
1356 {
1357         unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1358
1359         memcpy(lastdata, rxdata, CFSIZ);
1360         lastdata->can_dlc |= RX_RECV; /* mark as used */
1361
1362         /* throttle bcm_rx_changed ? */
1363         if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
1364             ((op->j_ival2) && (nexttx > jiffies))) {      /* we have to wait */
1365
1366                 lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
1367
1368                 if (!(op->thrtimer.expires)) { /* start only the first time */
1369                         op->thrtimer.expires = nexttx;
1370                         add_timer(&op->thrtimer);
1371
1372                         DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
1373                             op->thrtimer.function,
1374                             (char*) op->thrtimer.data,
1375                             (unsigned int) op->thrtimer.expires);
1376                 }
1377         } else
1378                 bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
1379 }
1380
1381 static void bcm_rx_starttimer(struct bcm_op *op)
1382 {
1383         if (op->flags & RX_NO_AUTOTIMER)
1384                 return;
1385
1386         if (op->j_ival1) {
1387
1388                 op->timer.expires = jiffies + op->j_ival1;
1389
1390                 DBG("adding rx timeout timer ival1. func=%p data=%p "
1391                     "exp=0x%08X\n",
1392                     op->timer.function,
1393                     (char*) op->timer.data,
1394                     (unsigned int) op->timer.expires);
1395
1396                 add_timer(&op->timer);
1397         }
1398 }
1399
1400
1401 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1402 {
1403         struct bcm_msg_head head;
1404
1405         op->j_lastmsg = jiffies;
1406         op->frames_filtered++; /* statistics */
1407
1408         if (op->frames_filtered > ULONG_MAX/100)
1409                 op->frames_filtered = op->frames_abs = 0; /* restart */
1410
1411         DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
1412             (unsigned int) op->j_lastmsg, op);
1413         DBG("sending notification\n");
1414
1415         head.opcode  = RX_CHANGED;
1416         head.flags   = op->flags;
1417         head.count   = op->count;
1418         head.ival1   = op->ival1;
1419         head.ival2   = op->ival2;
1420         head.can_id  = op->can_id;
1421         head.nframes = 1;
1422
1423         bcm_send_to_user(op, &head, data, &op->rx_stamp);
1424 }
1425
1426
1427 static void bcm_rx_timeout_handler(unsigned long data)
1428 {
1429         struct bcm_op *op = (struct bcm_op*)data;
1430         struct bcm_msg_head msg_head;
1431
1432         DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
1433
1434         msg_head.opcode  = RX_TIMEOUT;
1435         msg_head.flags   = op->flags;
1436         msg_head.count   = op->count;
1437         msg_head.ival1   = op->ival1;
1438         msg_head.ival2   = op->ival2;
1439         msg_head.can_id  = op->can_id;
1440         msg_head.nframes = 0;
1441
1442         bcm_send_to_user(op, &msg_head, NULL, NULL);
1443
1444         /* no restart of the timer is done here! */
1445
1446         /* if user wants to be informed, when cyclic CAN-Messages come back */
1447         if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1448                 /* clear received can_frames to indicate 'nothing received' */
1449                 memset(op->last_frames, 0, op->nframes * CFSIZ);
1450                 DBG("RX_ANNOUNCE_RESTART\n");
1451         }
1452
1453 }
1454
1455 static void bcm_rx_thr_handler(unsigned long data)
1456 {
1457         struct bcm_op *op = (struct bcm_op*)data;
1458         int i = 0;
1459
1460         op->thrtimer.expires = 0; /* mark disabled / consumed timer */
1461
1462         if (op->nframes > 1){
1463
1464                 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
1465                     op->can_id, op);
1466                 /* for MUX filter we start at index 1 */
1467                 for (i=1; i<op->nframes; i++){
1468                         if ((op->last_frames) &&
1469                             (op->last_frames[i].can_dlc & RX_THR)){
1470                                 op->last_frames[i].can_dlc &= ~RX_THR;
1471                                 bcm_rx_changed(op, &op->last_frames[i]);
1472                         }
1473                 }
1474         } else {
1475
1476                 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
1477                     op->can_id, op);
1478                 /* for RX_FILTER_ID and simple filter */
1479                 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1480                         op->last_frames[0].can_dlc &= ~RX_THR;
1481                         bcm_rx_changed(op, &op->last_frames[0]);
1482                 }
1483         }
1484 }
1485
1486 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
1487                              struct can_frame *frames, struct timeval *tv)
1488 {
1489         struct sk_buff *skb;
1490         struct can_frame *firstframe;
1491         struct sock *sk = op->sk;
1492         int datalen = head->nframes * CFSIZ;
1493         struct sockaddr_can *addr;
1494         int err;
1495
1496         skb = alloc_skb(sizeof(*head) + datalen,
1497                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1498         if (!skb)
1499                 return;
1500
1501         memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1502         /* can_frames starting here */
1503         firstframe = (struct can_frame *) skb->tail;
1504
1505         if (tv)
1506                 skb_set_timestamp(skb, tv); /* restore timestamp */
1507
1508         addr = (struct sockaddr_can *)skb->cb;
1509         memset(addr, 0, sizeof(*addr));
1510         addr->can_family  = AF_CAN;
1511         /* restore originator for recvfrom() */
1512         addr->can_ifindex = op->rx_ifindex;
1513
1514         if (head->nframes){
1515                 memcpy(skb_put(skb, datalen), frames, datalen);
1516
1517                 /* the BCM uses the can_dlc-element of the can_frame */
1518                 /* structure for internal purposes. This is only     */
1519                 /* relevant for updates that are generated by the    */
1520                 /* BCM, where nframes is 1                           */
1521                 if (head->nframes == 1)
1522                         firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1523         }
1524         if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
1525                 struct bcm_opt *bo = bcm_sk(sk);
1526                 DBG("sock_queue_rcv_skb failed: %d\n", err);
1527                 kfree_skb(skb);
1528                 bo->dropped_usr_msgs++; /* don't care about overflows */
1529         }
1530 }
1531
1532 /**************************************************/
1533 /* bcm_op handling: find & delete bcm_op elements */
1534 /**************************************************/
1535
1536 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
1537                                   int ifindex)
1538 {
1539         struct bcm_op *op;
1540
1541         list_for_each_entry(op, ops, list)
1542                 if ((op->can_id == can_id) && (op->ifindex == ifindex))
1543                         return op;
1544
1545         return NULL;
1546 }
1547
1548 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
1549 {
1550         struct bcm_op *op, *n;
1551
1552         list_for_each_entry_safe(op, n, ops, list) {
1553                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1554                         DBG("removing rx_op %p for can_id %03X\n",
1555                             op, op->can_id);
1556
1557                         /* Don't care if we're bound or not (due to netdev */
1558                         /* problems) can_rx_unregister() is always a save  */
1559                         /* thing to do here.                               */
1560                         if (op->ifindex) {
1561                                 struct net_device *dev =
1562                                         dev_get_by_index(op->ifindex);
1563                                 if (dev) {
1564                                         can_rx_unregister(dev, op->can_id,
1565                                                           REGMASK(op->can_id),
1566                                                           bcm_rx_handler, op);
1567                                         dev_put(dev);
1568                                 }
1569                         } else
1570                                 can_rx_unregister(NULL, op->can_id,
1571                                                   REGMASK(op->can_id),
1572                                                   bcm_rx_handler, op);
1573
1574                         list_del(&op->list);
1575                         bcm_remove_op(op);
1576                         return 1; /* done */
1577                 }
1578         }
1579
1580         return 0; /* not found */
1581 }
1582
1583 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
1584 {
1585         struct bcm_op *op, *n;
1586
1587         list_for_each_entry_safe(op, n, ops, list) {
1588                 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
1589                         DBG("removing rx_op %p for can_id %03X\n",
1590                             op, op->can_id);
1591                         list_del(&op->list);
1592                         bcm_remove_op(op);
1593                         return 1; /* done */
1594                 }
1595         }
1596
1597         return 0; /* not found */
1598 }
1599
1600 static void bcm_remove_op(struct bcm_op *op)
1601 {
1602         del_timer(&op->timer);
1603         del_timer(&op->thrtimer);
1604         if (op->frames)
1605                 kfree(op->frames);
1606         if (op->last_frames)
1607                 kfree(op->last_frames);
1608         kfree(op);
1609
1610         return;
1611 }
1612
1613 module_init(bcm_module_init);
1614 module_exit(bcm_module_exit);