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