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