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