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