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