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