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