]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/net/can/bcm.c
merge changes from hlist branch into trunk.
[socketcan-devel.git] / kernel / 2.6 / net / can / bcm.c
1 /*
2  * bcm.c
3  *
4  * Copyright (c) 2002-2005 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/config.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/af_can.h>
55 #include <linux/can/bcm.h>
56 #include <linux/can/version.h>
57
58
59 RCSID("$Id$");
60
61 #ifdef CONFIG_CAN_DEBUG_CORE
62 static int debug = 0;
63 module_param(debug, int, S_IRUGO);
64 #define DBG(args...)       (debug & 1 ? \
65                                (printk(KERN_DEBUG "BCM %s: ", __func__), \
66                                 printk(args)) : 0)
67 #define DBG_FRAME(args...) (debug & 2 ? can_debug_cframe(args) : 0)
68 #define DBG_SKB(skb)       (debug & 4 ? can_debug_skb(skb) : 0)
69 #else
70 #define DBG(args...)
71 #define DBG_FRAME(args...)
72 #define DBG_SKB(skb)
73 #endif
74
75 /* use of last_frames[index].can_dlc */
76 #define RX_RECV    0x40 /* received data for this element */
77 #define RX_THR     0x80 /* this element has not been sent due to throttle functionality */
78 #define BCM_CAN_DLC_MASK 0x0F /* clean flags by masking with BCM_CAN_DLC_MASK */
79 #define BCM_RX_REGMASK (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)
80
81 #define NAME "Broadcast Manager (BCM) for LLCF"
82 #define IDENT "bcm"
83 static __initdata const char banner[] = BANNER(NAME);
84
85 MODULE_DESCRIPTION(NAME);
86 MODULE_LICENSE("Dual BSD/GPL");
87 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
88
89
90 #define GET_U64(p) (*(unsigned long long*)(p)->data)
91
92 struct bcm_op {
93         struct list_head list;
94         canid_t can_id;
95         int flags;
96         unsigned long j_ival1, j_ival2, j_lastmsg;
97         unsigned long frames_abs, frames_filtered;
98         struct timeval ival1, ival2, stamp;
99         struct timer_list timer, thrtimer;
100         int count;
101         int nframes;
102         int currframe;
103         struct can_frame *frames;
104         struct can_frame *last_frames;
105         struct sock *sk;
106 };
107
108 struct bcm_user_data {
109         struct list_head rx_ops;
110         struct list_head tx_ops;
111         struct proc_dir_entry *bcm_proc_read;
112         char procname [9];
113 };
114
115 static struct proc_dir_entry *proc_dir = NULL;
116 int bcm_read_proc(char *page, char **start, off_t off,
117                   int count, int *eof, void *data);
118
119 static void bcm_notifier(unsigned long msg, void *data);
120 static int bcm_release(struct socket *sock);
121 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
122                        int flags);
123 static unsigned int bcm_poll(struct file *file, struct socket *sock,
124                              poll_table *wait);
125 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
126                        struct msghdr *msg, size_t size);
127 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
128                        struct msghdr *msg, size_t size, int flags);
129
130 static void bcm_tx_timeout_handler(unsigned long data);
131 static void bcm_rx_handler(struct sk_buff *skb, void *op);
132 static void bcm_rx_timeout_handler(unsigned long data);
133 static void bcm_rx_thr_handler(unsigned long data);
134 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id);
135 static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id);
136 static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id);
137 static void bcm_remove_op(struct bcm_op *op);
138 static void bcm_can_tx(struct bcm_op *op);
139 static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
140                              struct can_frame *frames, struct timeval *tv);
141 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data);
142 static void bcm_rx_starttimer(struct bcm_op *op);
143 static void bcm_rx_update_and_send(struct bcm_op *op,
144                                    struct can_frame *lastdata,
145                                    struct can_frame *rxdata);
146 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
147                                 struct can_frame *rxdata);
148
149 static struct proto_ops bcm_ops = {
150         .family        = PF_CAN,
151         .release       = bcm_release,
152         .bind          = sock_no_bind,
153         .connect       = bcm_connect,
154         .socketpair    = sock_no_socketpair,
155         .accept        = sock_no_accept,
156         .getname       = sock_no_getname,
157         .poll          = bcm_poll,
158         .ioctl         = NULL,          /* use can_ioctl() from af_can.c */
159         .listen        = sock_no_listen,
160         .shutdown      = sock_no_shutdown,
161         .setsockopt    = sock_no_setsockopt,
162         .getsockopt    = sock_no_getsockopt,
163         .sendmsg       = bcm_sendmsg,
164         .recvmsg       = bcm_recvmsg,
165         .mmap          = sock_no_mmap,
166         .sendpage      = sock_no_sendpage,
167 };
168
169 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
170
171 struct bcm_sock {
172         struct sock          sk;
173         struct bcm_user_data opt;
174 };
175
176 #define bcm_sk(sk) ((struct bcm_user_data *)(sk)->sk_protinfo)
177
178 static struct proto bcm_proto = {
179         .name     = "CAN_BCM",
180         .owner    = THIS_MODULE,
181         .obj_size = sizeof(struct bcm_sock),
182 };
183
184 static struct can_proto bcm_can_proto = {
185         .ops  = &bcm_ops,
186         .prot = &bcm_proto,
187 };
188
189 #else
190
191 #define bcm_sk(sk) ((struct bcm_user_data *)(sk)->sk_protinfo)
192
193 static struct can_proto bcm_can_proto = {
194         .ops      = &bcm_ops,
195         .owner    = THIS_MODULE,
196         .obj_size = 0,
197 };
198
199 #endif
200
201
202 static int __init bcm_init(void)
203 {
204         printk(banner);
205
206         can_proto_register(CAN_BCM, &bcm_can_proto);
207
208         /* create /proc/net/can/bcm directory */
209         proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
210
211         if (proc_dir)
212                 proc_dir->owner = THIS_MODULE;
213
214         return 0;
215 }
216
217 static void __exit bcm_exit(void)
218 {
219         can_proto_unregister(CAN_BCM);
220
221         if (proc_dir)
222                 remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
223
224 }
225
226 static void bcm_notifier(unsigned long msg, void *data)
227 {
228         struct sock *sk = (struct sock *)data;
229
230         DBG("called for sock %p\n", sk);
231
232         switch (msg) {
233         case NETDEV_UNREGISTER:
234                 sk->sk_bound_dev_if = 0;
235         case NETDEV_DOWN:
236                 sk->sk_err = ENETDOWN;
237                 sk->sk_error_report(sk);
238         }
239 }
240
241 static int bcm_release(struct socket *sock)
242 {
243         struct sock *sk = sock->sk;
244         struct bcm_user_data *ud = bcm_sk(sk);
245         struct bcm_op *op, *n;
246
247         /* many things to do here:
248            free all rx_ops and tx_ops, bcm_user_data structure,
249            can_rx_unregister(dev, canid, raw_rcv) and can-data in ?x_ops */
250
251         DBG("socket %p, sk %p\n", sock, sk);
252
253         /* remove userdata, bcm_ops, timer, etc. */
254
255         if (ud) {
256                 list_for_each_entry_safe(op, n, &ud->tx_ops, list) {
257                         DBG("removing tx_op (%p) for can_id <%03X>\n", op, op->can_id);
258                         bcm_remove_op(op);
259                 }
260
261                 list_for_each_entry_safe(op, n, &ud->rx_ops, list) {
262                         DBG("removing rx_op (%p) for can_id <%03X>\n", op, op->can_id);
263
264                         if (sk->sk_bound_dev_if) {
265                                 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
266                                 if (dev) {
267                                         can_rx_unregister(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op);
268                                         dev_put(dev);
269                                 }
270                         } else
271                                 DBG("sock %p not bound for can_rx_unregister()\n", sk);
272
273                         bcm_remove_op(op);
274                 }
275
276                 if ((proc_dir) && (ud->bcm_proc_read)) {
277                         remove_proc_entry(ud->procname, proc_dir);
278                 }
279
280                 kfree (ud);
281                 sk->sk_protinfo = NULL;
282         }
283
284         if (sk->sk_bound_dev_if) {
285                 struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
286                 if (dev) {
287                         can_dev_unregister(dev, bcm_notifier, sk);
288                         dev_put(dev);
289                 }
290         } else
291                 DBG("sock %p not bound for can_dev_unregister()\n", sk);
292
293         sock_put(sk);
294
295         return 0;
296 }
297
298 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
299                        int flags)
300 {
301         struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
302         struct sock *sk = sock->sk;
303         struct net_device *dev;
304         struct bcm_user_data *ud;
305
306         /* bind a device to this socket */
307
308         dev = dev_get_by_index(addr->can_ifindex);
309         if (!dev) {
310                 DBG("could not find device %d\n", addr->can_ifindex);
311                 return -ENODEV;
312         }
313         sk->sk_bound_dev_if = dev->ifindex;
314         can_dev_register(dev, bcm_notifier, sk);
315         dev_put(dev);
316
317         DBG("socket %p to device %s (idx %d)\n", sock, dev->name, dev->ifindex);
318
319         /* create struct for BCM-specific data for this socket */
320
321         if (!(ud = kmalloc(sizeof(struct bcm_user_data), GFP_KERNEL)))
322                 return -ENOMEM;
323
324         /* intitial BCM operations */
325         INIT_LIST_HEAD(&ud->tx_ops);
326         INIT_LIST_HEAD(&ud->rx_ops);
327         ud->bcm_proc_read = NULL;
328
329         sk->sk_protinfo = ud;
330
331         if (proc_dir) {
332                 sprintf(ud->procname, "%p", ud);
333                 ud->bcm_proc_read = create_proc_read_entry(ud->procname, 0644,
334                                                            proc_dir, bcm_read_proc, ud);
335         }
336
337         return 0;
338 }
339
340 int bcm_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
341 {
342         int len = 0;
343         struct bcm_user_data *ud = (struct bcm_user_data *) data;
344         struct bcm_op *op;
345         struct net_device *dev = NULL;
346
347         len += snprintf(page + len, PAGE_SIZE - len,">>> ud %p", ud);
348
349         if (!list_empty(&ud->rx_ops)) {
350                 struct sock *sk;
351                 op = list_entry(ud->rx_ops.next, struct bcm_op, list);
352                 sk = op->sk;
353                 if (sk->sk_bound_dev_if)
354                         dev = dev_get_by_index(sk->sk_bound_dev_if);
355                 len += snprintf(page + len, PAGE_SIZE - len,
356                                 " / sk %p / socket %p", sk, sk->sk_socket);
357         } else if (!list_empty(&ud->tx_ops)) {
358                 struct sock *sk;
359                 op = list_entry(ud->tx_ops.next, struct bcm_op, list);
360                 sk = op->sk;
361                 if (sk->sk_bound_dev_if)
362                         dev = dev_get_by_index(sk->sk_bound_dev_if);
363                 len += snprintf(page + len, PAGE_SIZE - len,
364                                 " / sk %p / socket %p", sk, sk->sk_socket);
365         }
366
367         if (dev) {
368                 len += snprintf(page + len, PAGE_SIZE - len, " / %s", dev->name);
369                 dev_put(dev);
370         }
371
372         len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
373
374         list_for_each_entry(op, &ud->rx_ops, list) {
375
376                 unsigned long reduction;
377
378                 /* print only active entries & prevent division by zero */
379                 if (!op->frames_abs)
380                         continue;
381
382                 len += snprintf(page + len, PAGE_SIZE - len, "rx_op: %03X [%d]%c ",
383                                 op->can_id, op->nframes,(op->flags & RX_CHECK_DLC)?'d':' ');
384                 if (op->j_ival1)
385                         len += snprintf(page + len, PAGE_SIZE - len, "timeo=%ld ", op->j_ival1);
386
387                 if (op->j_ival2)
388                         len += snprintf(page + len, PAGE_SIZE - len, "thr=%ld ", op->j_ival2);
389
390                 len += snprintf(page + len, PAGE_SIZE - len, "# recv %ld (%ld) => reduction: ",
391                                 op->frames_filtered, op->frames_abs);
392
393                 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
394
395                 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
396                                 (reduction == 100)?"near ":"", reduction);
397
398                 if (len >= PAGE_SIZE - 100) {
399                         /* mark output cut off */
400                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
401                         break;
402                 } 
403         }
404
405         list_for_each_entry(op, &ud->tx_ops, list) {
406
407                 len += snprintf(page + len, PAGE_SIZE - len, "tx_op: %03X [%d] ",
408                                 op->can_id, op->nframes);
409                 if (op->j_ival1)
410                         len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ", op->j_ival1);
411
412                 if (op->j_ival2)
413                         len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ", op->j_ival2);
414
415                 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n", op->frames_abs);
416
417                 if (len >= PAGE_SIZE - 100) {
418                         /* mark output cut off */
419                         len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
420                         break;
421                 }
422         }
423
424         len += snprintf(page + len, PAGE_SIZE - len, "\n");
425
426         *eof = 1;
427         return len;
428 }
429
430 static unsigned int bcm_poll(struct file *file, struct socket *sock,
431                              poll_table *wait)
432 {
433         unsigned int mask = 0;
434
435         DBG("socket %p\n", sock);
436
437         mask = datagram_poll(file, sock, wait);
438         return mask;
439 }
440
441 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
442                        struct msghdr *msg, size_t size)
443 {
444         struct bcm_msg_head msg_head;
445         int i;
446         struct bcm_op *op;
447         int err;
448         struct sock *sk = sock->sk;
449         struct bcm_user_data *ud = bcm_sk(sk);
450         char c;
451         int rbytes = 0; /* read bytes as return value */
452
453         /* read message head information */
454
455         if ((err = memcpy_fromiovec((unsigned char*)&msg_head, msg->msg_iov,
456                                     sizeof(msg_head))) < 0)
457                 return err;
458
459         DBG("opcode %d for can_id <%03X>\n", msg_head.opcode, msg_head.can_id);
460
461         if (!sk->sk_bound_dev_if) {
462                 DBG("sock %p not bound\n", sk); /* and therefore ud not initialized */
463                 return -ENOTCONN;
464         }
465
466         switch (msg_head.opcode) {
467
468         case TX_SETUP:
469
470                 if (msg_head.nframes < 1) /* we need at least one can_frame */
471                         return -EINVAL;
472
473                 /* check the given can_id */
474
475                 if (!(op = bcm_find_op(&ud->tx_ops, msg_head.can_id))) {
476
477                         /* insert new BCM operation for the given can_id */
478
479                         if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
480                                 return -ENOMEM;
481
482                         memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
483
484                         DBG("TX_SETUP: creating new tx_op (%p) for can_id <%03X>\n",
485                             op, msg_head.can_id);
486
487                         op->can_id    = msg_head.can_id;
488
489                         /* create array for can_frames and copy the data */
490                         if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
491                                 kfree(op);
492                                 return -ENOMEM;
493                         }
494
495                         for (i = 0; i < msg_head.nframes; i++) {
496                                 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
497                                 if (msg_head.flags & TX_CP_CAN_ID)
498                                         op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
499                         }
500
501                         op->last_frames = NULL; /* tx_ops never compare with previous received messages */
502
503                         op->sk = sk; /* bcm_can_tx / bcm_tx_timeout_handler needs this */
504
505                         init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
506                         init_timer(&op->thrtimer); /* currently unused in tx_ops */
507
508                         op->timer.function = bcm_tx_timeout_handler; /* handler for tx_ops */
509                         op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
510
511                         /* add this bcm_op to the list of the tx_ops */
512                         list_add(&op->list, &ud->tx_ops);
513
514                 } else {
515                         /* update existing BCM operation */
516
517                         DBG("TX_SETUP: modifying existing tx_op (%p) for can_id <%03X>\n",
518                             op, msg_head.can_id);
519
520                         /* do we need more space for the can_frames? */
521                         if (msg_head.nframes > op->nframes) {
522
523                                 /* yes => create new array */
524
525                                 struct can_frame *p;
526                                 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
527                                         return -ENOMEM;
528
529                                 kfree (op->frames);
530                                 op->frames = p;
531                         }
532
533                         /* update can_frames content */
534                         for (i = 0; i < msg_head.nframes; i++) {
535                                 memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
536                                 if (msg_head.flags & TX_CP_CAN_ID)
537                                         op->frames[i].can_id = msg_head.can_id; /* copy can_id into frame */
538                         }
539
540                 }
541
542                 if (op->nframes != msg_head.nframes) {
543                         op->nframes   = msg_head.nframes;
544                         op->currframe = 0; /* start multiple frame transmission with index 0 */
545                 }
546
547                 /* check flags */
548
549                 op->flags = msg_head.flags;
550
551                 if (op->flags & TX_RESET_MULTI_IDX)
552                         op->currframe = 0; /* start multiple frame transmission with index 0 */
553
554                 if (op->flags & SETTIMER) {
555
556                         /* set timer values */
557
558                         op->count   = msg_head.count;
559                         op->ival1   = msg_head.ival1;
560                         op->ival2   = msg_head.ival2;
561                         op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
562                         op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
563
564                         DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
565                             op->count, op->j_ival1, op->j_ival2);
566
567                         /* disable an active timer due to zero values? */
568                         if (!op->j_ival1 && !op->j_ival2) {
569                                 del_timer(&op->timer);
570                                 DBG("TX_SETUP: SETTIMER disabled timer.\n");
571                         }
572
573                 }
574
575                 if ((op->flags & STARTTIMER) && ((op->j_ival1 && op->count) || op->j_ival2)) {
576
577                         del_timer(&op->timer);
578
579                         op->flags |= TX_ANNOUNCE; /* spec: send can_frame when starting timer */
580                         if (op->j_ival1 && (op->count > 0)){
581                                 op->timer.expires = jiffies + op->j_ival1;
582                                 /* op->count-- is done in bcm_tx_timeout_handler */
583                                 DBG("TX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
584                                     op->timer.function,
585                                     (char*) op->timer.data,
586                                     (unsigned int) op->timer.expires);
587                         } else{
588                                 op->timer.expires = jiffies + op->j_ival2;
589                                 DBG("TX_SETUP: adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
590                                     op->timer.function,
591                                     (char*) op->timer.data,
592                                     (unsigned int) op->timer.expires);
593                         }
594
595                         add_timer(&op->timer);
596                 }
597
598                 if (op->flags & TX_ANNOUNCE)
599                         bcm_can_tx(op);
600
601                 rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
602
603                 break; /* TX_SETUP */
604
605         case TX_DELETE:
606
607                 bcm_delete_tx_op(&ud->tx_ops, msg_head.can_id);
608
609                 rbytes = sizeof(struct bcm_msg_head);
610
611                 break; /* TX_DELETE */
612
613         case TX_READ:
614
615                 /* reuse msg_head for the reply */
616                 msg_head.opcode  = TX_STATUS; /* reply to TX_READ */
617                 op = bcm_find_op(&ud->tx_ops, msg_head.can_id);
618                 c  = 'T'; /* for nice debug output ... */
619
620                 goto TRX_READ;
621
622         case RX_READ:
623
624                 /* reuse msg_head for the reply */
625                 msg_head.opcode  = RX_STATUS; /* reply to RX_READ */
626                 op = bcm_find_op(&ud->rx_ops, msg_head.can_id);
627                 c  = 'R'; /* for nice debug output ... */
628
629         TRX_READ:
630
631                 /* check the given can_id */
632
633                 if (!op) {
634                         DBG("%cX_READ: did not find op for can_id <%03X>\n",
635                             c, msg_head.can_id);
636
637                         msg_head.flags   |= CMD_ERROR;
638                         msg_head.nframes  = 0;
639                         bcm_send_to_user(sk, &msg_head, NULL, NULL);
640                 }
641                 else {
642                         DBG("%cX_READ: sending status for can_id <%03X>\n",
643                             c, msg_head.can_id);
644
645                         /* put current values into msg_head */
646                         msg_head.flags   = op->flags;
647                         msg_head.count   = op->count;
648                         msg_head.ival1   = op->ival1;
649                         msg_head.ival2   = op->ival2;
650                         msg_head.nframes = op->nframes;
651
652                         bcm_send_to_user(sk, &msg_head, op->frames, NULL);
653                 }
654
655                 rbytes = sizeof(struct bcm_msg_head);
656
657                 break; /* [T|R]X_READ */
658
659         case TX_SEND:
660         {
661                 struct sk_buff *skb;
662                 struct net_device *dev;
663
664                 /* just copy and send one can_frame */
665
666                 if (msg_head.nframes < 1) /* we need at least one can_frame */
667                         return -EINVAL;
668
669                 skb = alloc_skb(sizeof(struct can_frame), GFP_KERNEL);
670
671                 if (!skb)
672                         break;
673
674                 memcpy_fromiovec(skb_put(skb, sizeof(struct can_frame)), msg->msg_iov, sizeof(struct can_frame));
675
676                 DBG_FRAME("BCM: TX_SEND: sending frame",
677                           (struct can_frame *)skb->data);
678                 dev = dev_get_by_index(sk->sk_bound_dev_if);
679
680                 if (dev) {
681                         skb->dev = dev;
682                         skb->sk  = sk;
683                         can_send(skb, 1); /* send with loopback */
684                         dev_put(dev);
685                 }
686
687                 rbytes = sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
688         }
689         break;
690
691         case RX_SETUP:
692
693                 if ((msg_head.flags & RX_FILTER_ID) || (!(msg_head.nframes))) {
694                         /* be robust against wrong usage ... */
695                         msg_head.flags |= RX_FILTER_ID;
696                         msg_head.nframes = 0; /* ignore trailing garbage */
697                 }
698
699                 if ((msg_head.flags & RX_RTR_FRAME) &&
700                     ((msg_head.nframes != 1) || (!(msg_head.can_id & CAN_RTR_FLAG)))) {
701
702                         DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
703
704                         msg_head.flags   |= CMD_ERROR; /* return msg_head back to sender */
705                         msg_head.nframes  = 0;
706                         bcm_send_to_user(sk, &msg_head, NULL, NULL);
707
708                         rbytes = sizeof(struct bcm_msg_head);
709
710                         break;
711                 }
712
713                 /* check the given can_id */
714
715                 if (!(op = bcm_find_op(&ud->rx_ops, msg_head.can_id))) {
716
717                         /* insert new BCM operation for the given can_id */
718
719                         if (!(op = kmalloc(sizeof(struct bcm_op), GFP_KERNEL)))
720                                 return -ENOMEM;
721
722                         memset(op, 0, sizeof(struct bcm_op)); /* init to zero, e.g. for timers */
723
724                         DBG("RX_SETUP: creating new rx_op (%p) for can_id <%03X>\n",
725                             op, msg_head.can_id);
726
727                         op->can_id    = msg_head.can_id;
728                         op->nframes   = msg_head.nframes;
729
730                         if (op->nframes) {
731
732                                 /* create array for can_frames and copy the data */
733                                 if (!(op->frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
734                                         kfree(op);
735                                         return -ENOMEM;
736                                 }
737
738                                 for (i = 0; i < msg_head.nframes; i++)
739                                         memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
740
741                                 /* create array for received can_frames */
742                                 if (!(op->last_frames = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL))) {
743                                         kfree(op->frames);
744                                         kfree(op);
745                                         return -ENOMEM;
746                                 }
747
748                                 /* clear received can_frames to indicate 'nothing received' */
749                                 memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
750                         } else {
751                                 op->frames = NULL;
752
753                                 /* even when we have the RX_FILTER_ID case, we need to store the last frame */
754                                 /* for the throttle functionality */
755
756                                 /* create array for received can_frames */
757                                 if (!(op->last_frames = kmalloc(sizeof(struct can_frame), GFP_KERNEL))) {
758                                         kfree(op);
759                                         return -ENOMEM;
760                                 }
761
762                                 /* clear received can_frames to indicate 'nothing received' */
763                                 memset(op->last_frames, 0, sizeof(struct can_frame));
764                         }
765
766                         op->sk = sk; /* bcm_delete_rx_op() needs this */
767
768                         init_timer(&op->timer); /* initialize uninitialized (kmalloc) structure */
769                         init_timer(&op->thrtimer); /* init throttle timer for RX_CHANGED */
770
771                         op->timer.function = bcm_rx_timeout_handler; /* handler for rx timeouts */
772                         op->timer.data = (unsigned long)op; /* timer.data points to this op-structure */
773
774                         op->thrtimer.function = bcm_rx_thr_handler; /* handler for RX_CHANGED throttle timeouts */
775                         op->thrtimer.data = (unsigned long)op; /* timer.data points to this op-structure */
776                         op->thrtimer.expires = 0; /* mark disabled timer */
777
778                         /* add this bcm_op to the list of the tx_ops */
779                         list_add(&op->list, &ud->rx_ops);
780
781                         c=1; /* call can_rx_register() at end of RX_SETUP */
782
783                 } else {
784                         /* update existing BCM operation */
785
786                         DBG("RX_SETUP: modifying existing rx_op (%p) for can_id <%03X>\n",
787                             op, msg_head.can_id);
788
789                         /* do we need more space for the can_frames? */
790                         if (msg_head.nframes > op->nframes) {
791
792                                 /* yes => create new arrays */
793
794                                 struct can_frame *p;
795
796                                 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
797                                         return -ENOMEM;
798
799                                 if (op->frames)
800                                         kfree (op->frames);
801                                 op->frames = p;
802
803                                 if (!(p = kmalloc(msg_head.nframes * sizeof(struct can_frame), GFP_KERNEL)))
804                                         return -ENOMEM;
805                                 if (op->last_frames)
806                                         kfree (op->last_frames);
807                                 op->last_frames = p;
808                         }
809
810                         if (msg_head.nframes) {
811                                 /* update can_frames content */
812                                 for (i = 0; i < msg_head.nframes; i++)
813                                         memcpy_fromiovec((unsigned char*)&op->frames[i], msg->msg_iov, sizeof(struct can_frame));
814
815                                 /* clear received can_frames to indicate 'nothing received' */
816                                 memset(op->last_frames, 0, msg_head.nframes * sizeof(struct can_frame));
817                         }
818
819                         op->nframes = msg_head.nframes;
820                         c=0; /* do not call can_rx_register() at end of RX_SETUP */
821
822                 } /* if (!bcm_find_op(&ud->tx_ops, msg_head.can_id)) */
823
824
825                 /* check flags */
826
827                 op->flags = msg_head.flags;
828
829                 if (op->flags & RX_RTR_FRAME) {
830
831                         /* no timers in RTR-mode */
832                         del_timer(&op->thrtimer);
833                         del_timer(&op->timer);
834
835                         /* funny feature in RX(!)_SETUP only for RTR-mode: */
836                         /* copy can_id into frame BUT without RTR-flag to  */
837                         /* prevent a full-load-loopback-test ... ;-]       */
838                         if ((op->flags & TX_CP_CAN_ID) ||
839                             (op->frames[0].can_id == op->can_id))
840                                 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
841
842                 } else {
843                         if (op->flags & SETTIMER) {
844
845                                 /* set timer value */
846
847                                 op->ival1   = msg_head.ival1;
848                                 op->j_ival1 = timeval2jiffies(&msg_head.ival1, 1);
849                                 op->ival2   = msg_head.ival2;
850                                 op->j_ival2 = timeval2jiffies(&msg_head.ival2, 1);
851
852                                 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
853                                     op->j_ival1, op->j_ival2);
854
855                                 /* disable an active timer due to zero value? */
856                                 if (!op->j_ival1) {
857                                         del_timer(&op->timer);
858                                         DBG("RX_SETUP: disabled timer for rx timeouts.\n");
859                                 }
860
861                                 /* free currently blocked msgs ? */
862                                 if (op->thrtimer.expires) { /* running throttle timer? */
863                                         DBG("RX_SETUP: unblocking throttled msgs.\n");
864                                         del_timer(&op->thrtimer);
865                                         op->thrtimer.expires = jiffies + 2; /* send blocked msgs hereafter */
866                                         add_timer(&op->thrtimer);
867                                 }
868                                 /* if (op->j_ival2) is zero, no (new) throttling will happen */
869                                 /* see bcm_rx_update_and_send() and bcm_rx_thr_handler()     */
870                         }
871
872                         if ((op->flags & STARTTIMER) && op->j_ival1) {
873
874                                 del_timer(&op->timer);
875
876                                 op->timer.expires = jiffies + op->j_ival1;
877
878                                 DBG("RX_SETUP: adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
879                                     (char *) op->timer.function,
880                                     (char *) op->timer.data,
881                                     (unsigned int) op->timer.expires);
882
883                                 add_timer(&op->timer);
884                         }
885                 }
886
887                 /* now we can register for can_ids, if we added a new bcm_op */
888                 if (c) {
889                         struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
890
891                         DBG("RX_SETUP: can_rx_register() for can_id <%03X>. rx_op is (%p)\n", op->can_id, op);
892
893                         if (dev) {
894                                 can_rx_register(dev, op->can_id, BCM_RX_REGMASK, bcm_rx_handler, op, IDENT);
895                                 dev_put(dev);
896                         }
897                 }
898
899                 rbytes = msg_head.nframes * sizeof(struct can_frame) + sizeof(struct bcm_msg_head);
900
901                 break; /* RX_SETUP */
902
903         case RX_DELETE:
904
905                 bcm_delete_rx_op(&ud->rx_ops, msg_head.can_id);
906
907                 rbytes = sizeof(struct bcm_msg_head);
908
909                 break; /* RX_DELETE */
910
911         default:
912
913                 DBG("Unknown opcode %d\n", msg_head.opcode);
914
915                 msg_head.flags   |= CMD_ERROR; /* return msg_head back to sender */
916                 msg_head.nframes  = 0;
917                 bcm_send_to_user(sk, &msg_head, NULL, NULL);
918
919                 rbytes = sizeof(struct bcm_msg_head);
920
921                 break;
922         }
923
924         return rbytes;
925 }
926
927 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
928                        struct msghdr *msg, size_t size, int flags)
929 {
930         struct sock *sk = sock->sk;
931         struct sk_buff *skb;
932         int error = 0;
933         int noblock;
934         int err;
935
936         DBG("socket %p, sk %p\n", sock, sk);
937
938         noblock =  flags & MSG_DONTWAIT;
939         flags   &= ~MSG_DONTWAIT;
940         if (!(skb = skb_recv_datagram(sk, flags, noblock, &error))) {
941                 return error;
942         }
943
944         DBG("delivering skbuff %p\n", skb);
945         DBG_SKB(skb);
946
947         if (skb->len < size)
948                 size = skb->len;
949         if ((err = memcpy_toiovec(msg->msg_iov, skb->data, size)) < 0) {
950                 skb_free_datagram(sk, skb);
951                 return err;
952         }
953
954         sock_recv_timestamp(msg, sk, skb);
955
956         DBG("freeing sock %p, skbuff %p\n", sk, skb);
957         skb_free_datagram(sk, skb);
958
959         return size;
960 }
961
962 static void bcm_tx_timeout_handler(unsigned long data)
963 {
964         struct bcm_op *op = (struct bcm_op*)data;
965
966         DBG("Called with bcm_op (%p)\n", op);
967
968         if (op->j_ival1 && (op->count > 0)) {
969
970                 op->count--;
971
972                 if (!op->count && (op->flags & TX_COUNTEVT)) { /* create notification to user? */
973
974                         struct bcm_msg_head msg_head;
975
976                         DBG("sending TX_EXPIRED for can_id <%03X>\n", op->can_id);
977
978                         msg_head.opcode  = TX_EXPIRED;
979                         msg_head.flags   = op->flags;
980                         msg_head.count   = op->count;
981                         msg_head.ival1   = op->ival1;
982                         msg_head.ival2   = op->ival2;
983                         msg_head.can_id  = op->can_id;
984                         msg_head.nframes = 0;
985
986                         bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
987                 }
988         }
989
990         DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
991             op->count, op->j_ival1, op->j_ival2);
992
993         if (op->j_ival1 && (op->count > 0)) {
994
995                 op->timer.expires = jiffies + op->j_ival1;
996                 add_timer(&op->timer);
997
998                 DBG("adding timer ival1. func=%p data=(%p) exp=0x%08X\n",
999                     op->timer.function,
1000                     (char*) op->timer.data,
1001                     (unsigned int) op->timer.expires);
1002
1003                 bcm_can_tx(op); /* send (next) frame */
1004         } else {
1005                 if (op->j_ival2) {
1006                         op->timer.expires = jiffies + op->j_ival2;
1007                         add_timer(&op->timer);
1008
1009                         DBG("adding timer ival2. func=%p data=(%p) exp=0x%08X\n",
1010                             op->timer.function,
1011                             (char*) op->timer.data,
1012                             (unsigned int) op->timer.expires);
1013
1014                         bcm_can_tx(op); /* send (next) frame */
1015                 } else
1016                         DBG("no timer restart\n");
1017         }
1018
1019         return;
1020
1021 }
1022
1023 static void bcm_rx_handler(struct sk_buff *skb, void *data)
1024 {
1025         struct bcm_op *op = (struct bcm_op*)data;
1026         struct can_frame rxframe;
1027         int i;
1028
1029         del_timer(&op->timer); /* disable timeout */
1030
1031         DBG("Called with bcm_op (%p)\n", op);
1032
1033         if (skb->len == sizeof(rxframe)) {
1034                 memcpy(&rxframe, skb->data, sizeof(rxframe));
1035                 skb_get_timestamp(skb, &op->stamp); /* save rx timestamp */
1036                 op->frames_abs++; /* statistics */
1037                 kfree_skb(skb);
1038                 DBG("got can_frame with can_id <%03X>\n", rxframe.can_id);
1039         } else {
1040                 DBG("Wrong skb->len = %d\n", skb->len);
1041                 kfree_skb(skb);
1042                 return;
1043         }
1044
1045         DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
1046
1047         if (op->can_id != rxframe.can_id) {
1048                 DBG("ERROR! Got wrong can_id <%03X>! Expected <%03X>.\n",
1049                     rxframe.can_id, op->can_id);
1050                 return;
1051         }
1052
1053         if (op->flags & RX_RTR_FRAME) { /* send reply for RTR-request */
1054                 DBG("RTR-request\n");
1055                 bcm_can_tx(op); /* send op->frames[0] to CAN device */
1056                 return;
1057         }
1058
1059         if (op->flags & RX_FILTER_ID) { /* the easiest case */
1060                 DBG("Easy does it with RX_FILTER_ID\n");
1061                 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
1062                 bcm_rx_starttimer(op);
1063                 return;
1064         }
1065
1066         if (op->nframes == 1) { /* simple compare with index 0 */
1067                 DBG("Simple compare\n");
1068                 bcm_rx_cmp_to_index(op, 0, &rxframe);
1069                 bcm_rx_starttimer(op);
1070                 return;
1071         }
1072
1073         if (op->nframes > 1) { /* multiplex compare */
1074
1075                 DBG("Multiplex compare\n");
1076                 /* find the first multiplex mask that fits */
1077                 /* MUX-mask is in index 0 */
1078
1079                 for (i=1; i < op->nframes; i++) {
1080
1081                         if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
1082                             (GET_U64(&op->frames[0]) & GET_U64(&op->frames[i]))) {
1083                                 DBG("found MUX index %d\n", i);
1084                                 bcm_rx_cmp_to_index(op, i, &rxframe);
1085                                 break;
1086                         }
1087                 }
1088                 bcm_rx_starttimer(op);
1089         }
1090 }
1091
1092 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
1093                                 struct can_frame *rxdata)
1094 {
1095         /* no one uses the MSBs of can_dlc for comparation, */
1096         /* so we use it here to detect the first time of reception */
1097
1098         if (!(op->last_frames[index].can_dlc & RX_RECV)) { /* first time? */
1099                 DBG("first time :)\n");
1100                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1101                 return;
1102         }
1103
1104         /* do a real check in can_data */
1105
1106         DBG("op->frames[index].data = 0x%016llx\n", GET_U64(&op->frames[index]));
1107         DBG("op->last_frames[index].data = 0x%016llx\n",
1108             GET_U64(&op->last_frames[index]));
1109         DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
1110
1111         if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
1112             (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
1113                 DBG("relevant data change :)\n");
1114                 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1115                 return;
1116         }
1117
1118
1119         if (op->flags & RX_CHECK_DLC) {
1120
1121                 /* do a real check in dlc */
1122
1123                 if (rxdata->can_dlc != (op->last_frames[index].can_dlc & BCM_CAN_DLC_MASK)) {
1124                         DBG("dlc change :)\n");
1125                         bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
1126                         return;
1127                 }
1128         }
1129         DBG("no relevant change :(\n");
1130 }
1131
1132 static void bcm_rx_update_and_send(struct bcm_op *op,
1133                                    struct can_frame *lastdata,
1134                                    struct can_frame *rxdata)
1135 {
1136         unsigned long nexttx = op->j_lastmsg + op->j_ival2;
1137
1138         memcpy(lastdata, rxdata, sizeof(struct can_frame));
1139         lastdata->can_dlc |= RX_RECV; /* mark as used */
1140
1141         /* throttle bcm_rx_changed ? */
1142         if ((op->thrtimer.expires) || /* somebody else is already waiting OR */
1143             ((op->j_ival2) && (nexttx > jiffies))) {      /* we have to wait */
1144
1145                 lastdata->can_dlc |= RX_THR; /* mark as 'throttled' */
1146
1147                 if (!(op->thrtimer.expires)) { /* start only the first time */
1148                         op->thrtimer.expires = nexttx;
1149                         add_timer(&op->thrtimer);
1150
1151                         DBG("adding thrtimer. func=%p data=(%p) exp=0x%08X\n",
1152                             op->thrtimer.function,
1153                             (char*) op->thrtimer.data,
1154                             (unsigned int) op->thrtimer.expires);
1155                 }
1156         } else
1157                 bcm_rx_changed(op, rxdata); /* send RX_CHANGED to the user */
1158 }
1159
1160 static void bcm_rx_starttimer(struct bcm_op *op)
1161 {
1162         if (op->flags & RX_NO_AUTOTIMER)
1163                 return;
1164
1165         if (op->j_ival1) {
1166
1167                 op->timer.expires = jiffies + op->j_ival1;
1168
1169                 DBG("adding rx timeout timer ival1. func=%p data=(%p) exp=0x%08X\n",
1170                     op->timer.function,
1171                     (char*) op->timer.data,
1172                     (unsigned int) op->timer.expires);
1173
1174                 add_timer(&op->timer);
1175         }
1176 }
1177
1178
1179 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
1180 {
1181         struct bcm_msg_head head;
1182
1183         op->j_lastmsg = jiffies;
1184         op->frames_filtered++; /* statistics */
1185
1186         if (op->frames_filtered > ULONG_MAX/100)
1187                 op->frames_filtered = op->frames_abs = 0; /* restart - spinlock ? */
1188
1189         DBG("setting j_lastmsg to 0x%08X for rx_op(%p)\n",
1190             (unsigned int) op->j_lastmsg, op);
1191         DBG("sending notification\n");
1192
1193         head.opcode  = RX_CHANGED;
1194         head.flags   = op->flags;
1195         head.count   = op->count;
1196         head.ival1   = op->ival1;
1197         head.ival2   = op->ival2;
1198         head.can_id  = op->can_id;
1199         head.nframes = 1;
1200
1201         bcm_send_to_user(op->sk, &head, data, &op->stamp);
1202 }
1203
1204
1205 static void bcm_rx_timeout_handler(unsigned long data)
1206 {
1207         struct bcm_op *op = (struct bcm_op*)data;
1208         struct bcm_msg_head msg_head;
1209
1210         DBG("sending RX_TIMEOUT for can_id <%03X>. op is (%p)\n", op->can_id, op);
1211
1212         msg_head.opcode  = RX_TIMEOUT;
1213         msg_head.flags   = op->flags;
1214         msg_head.count   = op->count;
1215         msg_head.ival1   = op->ival1;
1216         msg_head.ival2   = op->ival2;
1217         msg_head.can_id  = op->can_id;
1218         msg_head.nframes = 0;
1219
1220         bcm_send_to_user(op->sk, &msg_head, NULL, NULL);
1221
1222         /* no restart of the timer is done here! */
1223
1224         /* if the user wants to be informed, when cyclic CAN-Messages come back ... */
1225         if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
1226                 /* clear received can_frames to indicate 'nothing received' */
1227                 memset(op->last_frames, 0, op->nframes * sizeof(struct can_frame));
1228                 DBG("RX_ANNOUNCE_RESTART\n");
1229         }
1230
1231 }
1232
1233 static void bcm_rx_thr_handler(unsigned long data)
1234 {
1235         struct bcm_op *op = (struct bcm_op*)data;
1236         int i = 0;
1237
1238         op->thrtimer.expires = 0; /* mark disabled / consumed timer */
1239
1240         if (op->nframes > 1){
1241
1242                 DBG("sending MUX RX_CHANGED for can_id <%03X>. op is (%p)\n",
1243                     op->can_id, op);
1244                 /* for MUX filter we start at index 1 */
1245                 for (i=1; i<op->nframes; i++){
1246                         if ((op->last_frames) && (op->last_frames[i].can_dlc & RX_THR)){
1247                                 op->last_frames[i].can_dlc &= ~RX_THR;
1248                                 bcm_rx_changed(op, &op->last_frames[i]);
1249                         }
1250                 }
1251         } else {
1252
1253                 DBG("sending simple RX_CHANGED for can_id <%03X>. op is (%p)\n",
1254                     op->can_id, op);
1255                 /* for RX_FILTER_ID and simple filter */
1256                 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)){
1257                         op->last_frames[0].can_dlc &= ~RX_THR;
1258                         bcm_rx_changed(op, &op->last_frames[0]);
1259                 }
1260         }
1261 }
1262
1263 static void bcm_can_tx(struct bcm_op *op)
1264 {
1265         struct sk_buff *skb;
1266         struct net_device *dev;
1267         struct can_frame *cf = &op->frames[op->currframe];
1268
1269         DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
1270
1271         skb = alloc_skb(sizeof(struct can_frame),
1272                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1273
1274         if (!skb)
1275                 return;
1276
1277         memcpy(skb_put(skb, sizeof(struct can_frame)), cf, sizeof(struct can_frame));
1278
1279         if (op->sk->sk_bound_dev_if) {
1280                 dev = dev_get_by_index(op->sk->sk_bound_dev_if);
1281
1282                 if (dev) {
1283                         skb->dev = dev;
1284                         skb->sk = op->sk;
1285                         can_send(skb, 1); /* send with loopback */
1286                         dev_put(dev);
1287                 }
1288         }
1289
1290         op->currframe++;
1291         op->frames_abs++; /* statistics */
1292
1293         /* reached last frame? */
1294         if (op->currframe >= op->nframes)
1295                 op->currframe = 0;
1296
1297 }
1298
1299 static void bcm_send_to_user(struct sock *sk, struct bcm_msg_head *head,
1300                              struct can_frame *frames, struct timeval *tv)
1301 {
1302         struct sk_buff *skb;
1303         struct can_frame *firstframe;
1304         int datalen = head->nframes * sizeof(struct can_frame);
1305         int err;
1306
1307         if (!sk) {
1308                 DBG("no sk available\n");
1309                 return;
1310         }
1311
1312         skb = alloc_skb(sizeof(*head) + datalen,
1313                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
1314         memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
1315         firstframe = (struct can_frame *) skb->tail; /* can_frames starting here */
1316
1317         if (tv)
1318                 skb_set_timestamp(skb, tv);
1319
1320         if (head->nframes){
1321                 memcpy(skb_put(skb, datalen), frames, datalen);
1322
1323                 /* the BCM uses the can_dlc-element of the can_frame structure
1324                    for internal purposes. This is only relevant for updates
1325                    that are generated by the BCM, where nframes is 1
1326                 */
1327                 if (head->nframes == 1)
1328                         firstframe->can_dlc &= BCM_CAN_DLC_MASK;
1329         }
1330         if ((err = sock_queue_rcv_skb(sk, skb)) < 0) {
1331                 DBG("sock_queue_rcv_skb failed: %d\n", err);
1332                 kfree_skb(skb);
1333         }
1334 }
1335
1336 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id)
1337 {
1338         struct bcm_op *p;
1339
1340         list_for_each_entry(p, ops, list)
1341                 if (p->can_id == can_id)
1342                         return p;
1343
1344         return NULL;
1345 }
1346
1347 static void bcm_delete_rx_op(struct list_head *ops, canid_t can_id)
1348 {
1349         struct bcm_op *p, *n;
1350
1351         list_for_each_entry_safe(p, n, ops, list) {
1352                 if (p->can_id == can_id) {
1353                         DBG("removing rx_op (%p) for can_id <%03X>\n", p, p->can_id);
1354
1355                         if (p->sk->sk_bound_dev_if) {
1356                                 struct net_device *dev = dev_get_by_index(p->sk->sk_bound_dev_if);
1357                                 if (dev) {
1358                                         can_rx_unregister(dev, p->can_id, BCM_RX_REGMASK, bcm_rx_handler, p);
1359                                         dev_put(dev);
1360                                 }
1361                         } else
1362                                 DBG("sock %p not bound for can_rx_unregister()\n", p->sk);
1363
1364                         list_del(&p->list);
1365                         bcm_remove_op(p);
1366                         return;
1367                 }
1368         }
1369 }
1370
1371 static void bcm_delete_tx_op(struct list_head *ops, canid_t can_id)
1372 {
1373         struct bcm_op *p, *n;
1374
1375         list_for_each_entry_safe(p, n, ops, list) {
1376                 if (p->can_id == can_id) {
1377                         DBG("removing rx_op (%p) for can_id <%03X>\n",
1378                             p, p->can_id);
1379                         list_del(&p->list);
1380                         bcm_remove_op(p);
1381                         return;
1382                 }
1383         }
1384 }
1385
1386 static void bcm_remove_op(struct bcm_op *op)
1387 {
1388         del_timer(&op->timer);
1389         del_timer(&op->thrtimer);
1390         if (op->frames)
1391                 kfree(op->frames);
1392         if (op->last_frames)
1393                 kfree(op->last_frames);
1394         kfree(op);
1395
1396         return;
1397 }
1398
1399 module_init(bcm_init);
1400 module_exit(bcm_exit);