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