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