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