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