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