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