]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm.c
fixed missing condition from r247.
[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                                 if (op->frames != &op->sframe)
931                                         kfree(op->frames);
932                                 kfree(op);
933                                 return err;
934                         }
935
936                         if (msg_head->flags & TX_CP_CAN_ID) {
937                                 /* copy can_id into frame */
938                                 op->frames[i].can_id = msg_head->can_id;
939                         }
940                 }
941
942                 /* tx_ops never compare with previous received messages */
943                 op->last_frames = NULL;
944
945                 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
946                 op->sk = sk;
947
948                 op->ifindex = ifindex;
949
950                 /* initialize uninitialized (kmalloc) structure */
951                 init_timer(&op->timer);
952
953                 /* currently unused in tx_ops */
954                 init_timer(&op->thrtimer);
955
956                 /* handler for tx_ops */
957                 op->timer.function = bcm_tx_timeout_handler;
958
959                 /* timer.data points to this op-structure */
960                 op->timer.data = (unsigned long)op;
961
962                 /* add this bcm_op to the list of the tx_ops */
963                 list_add(&op->list, &bo->tx_ops);
964
965         } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
966
967         if (op->nframes != msg_head->nframes) {
968                 op->nframes   = msg_head->nframes;
969                 /* start multiple frame transmission with index 0 */
970                 op->currframe = 0;
971         }
972
973         /* check flags */
974
975         op->flags = msg_head->flags;
976
977         if (op->flags & TX_RESET_MULTI_IDX) {
978                 /* start multiple frame transmission with index 0 */
979                 op->currframe = 0; 
980         }
981
982         if (op->flags & SETTIMER) {
983                 /* set timer values */
984
985                 op->count = msg_head->count;
986                 op->ival1 = msg_head->ival1;
987                 op->ival2 = msg_head->ival2;
988                 op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
989                 op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
990
991                 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
992                     op->count, op->j_ival1, op->j_ival2);
993
994                 /* disable an active timer due to zero values? */
995                 if (!op->j_ival1 && !op->j_ival2) {
996                         del_timer(&op->timer);
997                         DBG("TX_SETUP: SETTIMER disabled timer.\n");
998                 }
999         }
1000
1001         if ((op->flags & STARTTIMER) &&
1002             ((op->j_ival1 && op->count) || op->j_ival2)) {
1003
1004                 del_timer(&op->timer);
1005
1006                 /* spec: send can_frame when starting timer */
1007                 op->flags |= TX_ANNOUNCE;
1008
1009                 if (op->j_ival1 && (op->count > 0)) {
1010                         op->timer.expires = jiffies + op->j_ival1;
1011                         /* op->count-- is done in bcm_tx_timeout_handler */
1012                         DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1013                             "exp=0x%08X\n",
1014                             op->timer.function,
1015                             (char*) op->timer.data,
1016                             (unsigned int) op->timer.expires);
1017
1018                 } else {
1019                         op->timer.expires = jiffies + op->j_ival2;
1020                         DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1021                             "exp=0x%08X\n",
1022                             op->timer.function,
1023                             (char*) op->timer.data,
1024                             (unsigned int) op->timer.expires);
1025                 }
1026
1027                 add_timer(&op->timer);
1028         }
1029
1030         if (op->flags & TX_ANNOUNCE)
1031                 bcm_can_tx(op);
1032
1033         return msg_head->nframes * CFSIZ + MHSIZ;
1034 }
1035
1036 /*
1037  * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1038  */
1039 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1040                         int ifindex, struct sock *sk)
1041 {
1042         struct bcm_opt *bo = bcm_sk(sk);
1043         struct bcm_op *op;
1044         int do_rx_register;
1045         int err;
1046
1047         if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1048                 /* be robust against wrong usage ... */
1049                 msg_head->flags |= RX_FILTER_ID;
1050                 msg_head->nframes = 0; /* ignore trailing garbage */
1051         }
1052
1053         if ((msg_head->flags & RX_RTR_FRAME) &&
1054             ((msg_head->nframes != 1) ||
1055              (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1056
1057                 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1058                 return -EINVAL;
1059         }
1060
1061         /* check the given can_id */
1062         op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1063         if (op) {
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 > 1) {
1108                         /* create array for can_frames and copy the data */
1109                         op->frames = kmalloc(msg_head->nframes * CFSIZ,
1110                                              GFP_KERNEL);
1111                         if (!op->frames) {
1112                                 kfree(op);
1113                                 return -ENOMEM;
1114                         }
1115
1116                         /* create and init array for received can_frames */
1117                         op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1118                                                   GFP_KERNEL);
1119                         if (!op->last_frames) {
1120                                 kfree(op->frames);
1121                                 kfree(op);
1122                                 return -ENOMEM;
1123                         }
1124
1125                 } else {
1126                         op->frames = &op->sframe;
1127                         op->last_frames = &op->last_sframe;
1128                 }
1129
1130                 if (msg_head->nframes) {
1131                         err = memcpy_fromiovec((u8*)op->frames, msg->msg_iov,
1132                                                msg_head->nframes * CFSIZ);
1133                         if (err < 0) {
1134                                 if (op->frames != &op->sframe)
1135                                         kfree(op->frames);
1136                                 if (op->last_frames != &op->last_sframe)
1137                                         kfree(op->last_frames);
1138                                 kfree(op);
1139                                 return err;
1140                         }
1141                 }
1142
1143                 op->sk = sk;
1144                 op->ifindex = ifindex;
1145
1146                 /* initialize uninitialized (kzalloc) structure */
1147                 init_timer(&op->timer);
1148
1149                 /* init throttle timer for RX_CHANGED */
1150                 init_timer(&op->thrtimer);
1151
1152                 /* handler for rx timeouts */
1153                 op->timer.function = bcm_rx_timeout_handler;
1154
1155                 /* timer.data points to this op-structure */
1156                 op->timer.data = (unsigned long)op;
1157
1158                 /* handler for RX_CHANGED throttle timeouts */
1159                 op->thrtimer.function = bcm_rx_thr_handler;
1160
1161                 /* timer.data points to this op-structure */
1162                 op->thrtimer.data = (unsigned long)op;
1163
1164                 /* mark disabled timer */
1165                 op->thrtimer.expires = 0;
1166
1167                 /* add this bcm_op to the list of the tx_ops */
1168                 list_add(&op->list, &bo->rx_ops);
1169
1170                 /* call can_rx_register() */
1171                 do_rx_register = 1;
1172
1173         } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1174
1175         /* check flags */
1176         op->flags = msg_head->flags;
1177
1178         if (op->flags & RX_RTR_FRAME) {
1179
1180                 /* no timers in RTR-mode */
1181                 del_timer(&op->thrtimer);
1182                 del_timer(&op->timer);
1183
1184                 /*
1185                  * funny feature in RX(!)_SETUP only for RTR-mode:
1186                  * copy can_id into frame BUT without RTR-flag to
1187                  * prevent a full-load-loopback-test ... ;-]
1188                  */
1189                 if ((op->flags & TX_CP_CAN_ID) ||
1190                     (op->frames[0].can_id == op->can_id))
1191                         op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1192
1193         } else {
1194                 if (op->flags & SETTIMER) {
1195
1196                         /* set timer value */
1197                         op->ival1 = msg_head->ival1;
1198                         op->ival2 = msg_head->ival2;
1199                         op->j_ival1 = timeval2jiffies(&msg_head->ival1, 1);
1200                         op->j_ival2 = timeval2jiffies(&msg_head->ival2, 1);
1201
1202                         DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1203                             op->j_ival1, op->j_ival2);
1204
1205                         /* disable an active timer due to zero value? */
1206                         if (!op->j_ival1) {
1207                                 del_timer(&op->timer);
1208                                 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1209                         }
1210
1211                         /* free currently blocked msgs ? */
1212                         if (op->thrtimer.expires) {
1213                                 DBG("RX_SETUP: unblocking throttled msgs.\n");
1214                                 del_timer(&op->thrtimer);
1215                                 /* send blocked msgs hereafter */
1216                                 op->thrtimer.expires = jiffies + 2;
1217                                 add_timer(&op->thrtimer);
1218                         }
1219                         /*
1220                          * if (op->j_ival2) is zero, no (new) throttling
1221                          * will happen. For details see functions
1222                          * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1223                          */
1224                 }
1225
1226                 if ((op->flags & STARTTIMER) && op->j_ival1) {
1227
1228                         del_timer(&op->timer);
1229                         op->timer.expires = jiffies + op->j_ival1;
1230
1231                         DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1232                             " exp=0x%08X\n",
1233                             (char *) op->timer.function,
1234                             (char *) op->timer.data,
1235                             (unsigned int) op->timer.expires);
1236
1237                         add_timer(&op->timer);
1238                 }
1239         }
1240
1241         /* now we can register for can_ids, if we added a new bcm_op */
1242         if (do_rx_register) {
1243                 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1244                     "rx_op is %p\n", op->can_id, op);
1245
1246                 if (ifindex) {
1247                         struct net_device *dev = dev_get_by_index(ifindex);
1248
1249                         if (dev) {
1250                                 can_rx_register(dev, op->can_id,
1251                                                 REGMASK(op->can_id),
1252                                                 bcm_rx_handler, op, IDENT);
1253                                 dev_put(dev);
1254                         }
1255
1256                 } else 
1257                         can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1258                                         bcm_rx_handler, op, IDENT);
1259         }
1260
1261         return msg_head->nframes * CFSIZ + MHSIZ;
1262 }
1263
1264 /*
1265  * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1266  */
1267 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1268 {
1269         struct sk_buff *skb;
1270         struct net_device *dev;
1271         int err;
1272
1273         /* just copy and send one can_frame */
1274
1275         if (!ifindex) /* we need a real device to send frames */
1276                 return -ENODEV;
1277
1278         skb = alloc_skb(CFSIZ, GFP_KERNEL);
1279
1280         if (!skb)
1281                 return -ENOMEM;
1282
1283         err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1284         if (err < 0) {
1285                 kfree_skb(skb);
1286                 return err;
1287         }
1288
1289         DBG_FRAME("BCM: TX_SEND: sending frame",
1290                   (struct can_frame *)skb->data);
1291
1292         dev = dev_get_by_index(ifindex);
1293         if (!dev) {
1294                 kfree_skb(skb);
1295                 return -ENODEV;
1296         }
1297
1298         skb->dev = dev;
1299         skb->sk  = sk;
1300         can_send(skb, 1); /* send with loopback */
1301         dev_put(dev);
1302
1303         return CFSIZ + MHSIZ;
1304 }
1305
1306 /*
1307  * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1308  */
1309 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1310                        struct msghdr *msg, size_t size)
1311 {
1312         struct sock *sk = sock->sk;
1313         struct bcm_opt *bo = bcm_sk(sk);
1314         int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1315         struct bcm_msg_head msg_head;
1316         int ret; /* read bytes or error codes as return value */
1317
1318         if (!bo->bound) {
1319                 DBG("sock %p not bound\n", sk);
1320                 return -ENOTCONN;
1321         }
1322
1323         /* check for alternative ifindex for this bcm_op */
1324
1325         if (!ifindex && msg->msg_name) {
1326                 /* no bound device as default => check msg_name */
1327                 struct sockaddr_can *addr = 
1328                         (struct sockaddr_can *)msg->msg_name;
1329
1330                 if (addr->can_family != AF_CAN)
1331                         return -EINVAL;
1332
1333                 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1334
1335                 if (ifindex && !dev_get_by_index(ifindex)) {
1336                         DBG("device %d not found\n", ifindex);
1337                         return -ENODEV;
1338                 }
1339         }
1340
1341         /* read message head information */
1342
1343         ret = memcpy_fromiovec((u8*)&msg_head, msg->msg_iov, MHSIZ);
1344         if (ret < 0)
1345                 return ret;
1346
1347         DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1348
1349         switch (msg_head.opcode) {
1350
1351         case TX_SETUP:
1352                 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1353                 break;
1354
1355         case RX_SETUP:
1356                 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1357                 break;
1358
1359         case TX_DELETE:
1360                 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1361                         ret = MHSIZ;
1362                 else
1363                         ret = -EINVAL;
1364                 break;
1365                     
1366         case RX_DELETE:
1367                 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1368                         ret = MHSIZ;
1369                 else
1370                         ret = -EINVAL;
1371                 break;
1372
1373         case TX_READ:
1374                 /* reuse msg_head for the reply to TX_READ */
1375                 msg_head.opcode  = TX_STATUS;
1376                 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1377                 break;
1378
1379         case RX_READ:
1380                 /* reuse msg_head for the reply to RX_READ */
1381                 msg_head.opcode  = RX_STATUS;
1382                 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1383                 break;
1384
1385         case TX_SEND:
1386                 /* we need at least one can_frame */
1387                 if (msg_head.nframes < 1)
1388                         return -EINVAL;
1389
1390                 ret = bcm_tx_send(msg, ifindex, sk);
1391                 break;
1392
1393         default:
1394                 DBG("Unknown opcode %d\n", msg_head.opcode);
1395                 ret = -EINVAL;
1396                 break;
1397         }
1398
1399         return ret;
1400 }
1401
1402 /*
1403  * initial settings for all BCM sockets to be set at socket creation time
1404  */
1405 static int bcm_init(struct sock *sk)
1406 {
1407         struct bcm_opt *bo = bcm_sk(sk);
1408
1409         bo->bound            = 0;
1410         bo->ifindex          = 0;
1411         bo->dropped_usr_msgs = 0;
1412         bo->bcm_proc_read    = NULL;
1413
1414         INIT_LIST_HEAD(&bo->tx_ops);
1415         INIT_LIST_HEAD(&bo->rx_ops);
1416
1417         return 0;
1418 }
1419
1420 /*
1421  * notification handler for netdevice status changes
1422  */
1423 static void bcm_notifier(unsigned long msg, void *data)
1424 {
1425         struct sock *sk = (struct sock *)data;
1426         struct bcm_opt *bo = bcm_sk(sk);
1427
1428         DBG("called for sock %p\n", sk);
1429
1430         switch (msg) {
1431
1432         case NETDEV_UNREGISTER:
1433                 bo->bound   = 0;
1434                 bo->ifindex = 0;
1435                 /* fallthrough */
1436         case NETDEV_DOWN:
1437                 sk->sk_err = ENETDOWN;
1438                 if (!sock_flag(sk, SOCK_DEAD))
1439                         sk->sk_error_report(sk);
1440         }
1441 }
1442
1443 /*
1444  * standard socket functions
1445  */
1446 static int bcm_release(struct socket *sock)
1447 {
1448         struct sock *sk = sock->sk;
1449         struct bcm_opt *bo = bcm_sk(sk);
1450         struct bcm_op *op, *next;
1451
1452         DBG("socket %p, sk %p\n", sock, sk);
1453
1454         /* remove bcm_ops, timer, rx_unregister(), etc. */
1455
1456         list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1457                 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1458                 bcm_remove_op(op);
1459         }
1460
1461         list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1462                 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1463
1464                 /*
1465                  * Don't care if we're bound or not (due to netdev problems)
1466                  * can_rx_unregister() is always a save thing to do here.
1467                  */
1468                 if (op->ifindex) {
1469                         struct net_device *dev = dev_get_by_index(op->ifindex);
1470
1471                         if (dev) {
1472                                 can_rx_unregister(dev, op->can_id,
1473                                                   REGMASK(op->can_id),
1474                                                   bcm_rx_handler, op);
1475                                 dev_put(dev);
1476                         }
1477
1478                 } else
1479                         can_rx_unregister(NULL, op->can_id,
1480                                           REGMASK(op->can_id),
1481                                           bcm_rx_handler, op);
1482
1483                 bcm_remove_op(op);
1484         }
1485
1486         /* remove procfs entry */
1487         if (proc_dir && bo->bcm_proc_read)
1488                 remove_proc_entry(bo->procname, proc_dir);
1489
1490         /* remove device notifier */
1491         if (bo->ifindex) {
1492                 struct net_device *dev = dev_get_by_index(bo->ifindex);
1493
1494                 if (dev) {
1495                         can_dev_unregister(dev, bcm_notifier, sk);
1496                         dev_put(dev);
1497                 }
1498         }
1499
1500         sock_put(sk);
1501
1502         return 0;
1503 }
1504
1505 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1506                        int flags)
1507 {
1508         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1509         struct sock *sk = sock->sk;
1510         struct bcm_opt *bo = bcm_sk(sk);
1511
1512         if (bo->bound)
1513                 return -EISCONN;
1514
1515         /* bind a device to this socket */
1516         if (addr->can_ifindex) {
1517                 struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1518
1519                 if (!dev) {
1520                         DBG("could not find device index %d\n",
1521                             addr->can_ifindex);
1522                         return -ENODEV;
1523                 }
1524                 bo->ifindex = dev->ifindex;
1525                 can_dev_register(dev, bcm_notifier, sk); /* register notif. */
1526                 dev_put(dev);
1527
1528                 DBG("socket %p bound to device %s (idx %d)\n",
1529                     sock, dev->name, dev->ifindex);
1530
1531         } else {
1532                 /* no notifier for ifindex = 0 ('any' CAN device) */
1533                 bo->ifindex = 0;
1534         }
1535
1536         bo->bound = 1;
1537
1538         if (proc_dir) {
1539                 /* unique socket address as filename */
1540                 sprintf(bo->procname, "%p", sock);
1541                 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1542                                                            proc_dir,
1543                                                            bcm_read_proc, sk);
1544         }
1545
1546         return 0;
1547 }
1548
1549 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1550                        struct msghdr *msg, size_t size, int flags)
1551 {
1552         struct sock *sk = sock->sk;
1553         struct sk_buff *skb;
1554         int error = 0;
1555         int noblock;
1556         int err;
1557
1558         DBG("socket %p, sk %p\n", sock, sk);
1559
1560         noblock =  flags & MSG_DONTWAIT;
1561         flags   &= ~MSG_DONTWAIT;
1562         skb = skb_recv_datagram(sk, flags, noblock, &error);
1563         if (!skb)
1564                 return error;
1565
1566         DBG("delivering skbuff %p\n", skb);
1567         DBG_SKB(skb);
1568
1569         if (skb->len < size)
1570                 size = skb->len;
1571
1572         err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1573         if (err < 0) {
1574                 skb_free_datagram(sk, skb);
1575                 return err;
1576         }
1577
1578         sock_recv_timestamp(msg, sk, skb);
1579
1580         if (msg->msg_name) {
1581                 msg->msg_namelen = sizeof(struct sockaddr_can);
1582                 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1583         }
1584
1585         DBG("freeing sock %p, skbuff %p\n", sk, skb);
1586         skb_free_datagram(sk, skb);
1587
1588         return size;
1589 }
1590
1591 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1592                              poll_table *wait)
1593 {
1594         unsigned int mask = 0;
1595
1596         DBG("socket %p\n", sock);
1597
1598         mask = datagram_poll(file, sock, wait);
1599         return mask;
1600 }
1601
1602 static struct proto_ops bcm_ops = {
1603         .family        = PF_CAN,
1604         .release       = bcm_release,
1605         .bind          = sock_no_bind,
1606         .connect       = bcm_connect,
1607         .socketpair    = sock_no_socketpair,
1608         .accept        = sock_no_accept,
1609         .getname       = sock_no_getname,
1610         .poll          = bcm_poll,
1611         .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
1612         .listen        = sock_no_listen,
1613         .shutdown      = sock_no_shutdown,
1614         .setsockopt    = sock_no_setsockopt,
1615         .getsockopt    = sock_no_getsockopt,
1616         .sendmsg       = bcm_sendmsg,
1617         .recvmsg       = bcm_recvmsg,
1618         .mmap          = sock_no_mmap,
1619         .sendpage      = sock_no_sendpage,
1620 };
1621
1622 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1623 static struct proto bcm_proto = {
1624         .name       = "CAN_BCM",
1625         .owner      = THIS_MODULE,
1626         .obj_size   = sizeof(struct bcm_sock),
1627         .init       = bcm_init,
1628 };
1629
1630 static struct can_proto bcm_can_proto = {
1631         .type       = SOCK_DGRAM,
1632         .protocol   = CAN_BCM,
1633         .capability = BCM_CAP,
1634         .ops        = &bcm_ops,
1635         .prot       = &bcm_proto,
1636 };
1637 #else
1638 static struct can_proto bcm_can_proto = {
1639         .type       = SOCK_DGRAM,
1640         .protocol   = CAN_BCM,
1641         .capability = BCM_CAP,
1642         .ops        = &bcm_ops,
1643         .owner      = THIS_MODULE,
1644         .obj_size   = sizeof(struct bcm_opt),
1645         .init       = bcm_init,
1646 };
1647 #endif
1648
1649 static int __init bcm_module_init(void)
1650 {
1651         printk(banner);
1652
1653         can_proto_register(&bcm_can_proto);
1654
1655         /* create /proc/net/can/bcm directory */
1656         proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1657
1658         if (proc_dir)
1659                 proc_dir->owner = THIS_MODULE;
1660
1661         return 0;
1662 }
1663
1664 static void __exit bcm_module_exit(void)
1665 {
1666         can_proto_unregister(&bcm_can_proto);
1667
1668         if (proc_dir)
1669                 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1670 }
1671
1672 module_init(bcm_module_init);
1673 module_exit(bcm_module_exit);