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