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