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