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