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