2 CAN: Add broadcast manager (bcm) protocol
4 This patch adds the CAN broadcast manager (bcm) protocol.
6 Signed-off-by: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
7 Signed-off-by: Urs Thuermann <urs.thuermann@volkswagen.de>
10 include/linux/can/bcm.h | 65 +
13 net/can/bcm.c | 1755 ++++++++++++++++++++++++++++++++++++++++++++++++
14 4 files changed, 1851 insertions(+)
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
24 + * Definitions for CAN Broadcast Manager (BCM)
26 + * Author: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
27 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
28 + * All rights reserved.
30 + * Send feedback to <socketcan-users@lists.berlios.de>
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.
48 +struct bcm_msg_head {
52 + struct timeval ival1, ival2;
55 + struct can_frame frames[0];
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) */
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
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
91 Say Y here if you want non-root users to be able to access CAN_RAW
95 + tristate "Broadcast Manager CAN Protocol (with content filtering)"
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.
108 + bool "Allow non-root users to access CAN broadcast manager sockets"
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
122 config CAN_DEBUG_CORE
123 bool "CAN Core debugging messages"
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
131 obj-$(CONFIG_CAN_RAW) += can-raw.o
132 can-raw-objs := raw.o
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
142 + * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
144 + * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
145 + * All rights reserved.
147 + * Redistribution and use in source and binary forms, with or without
148 + * modification, are permitted provided that the following conditions
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.
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.
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.
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
181 + * Send feedback to <socketcan-users@lists.berlios.de>
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>
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 */
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))
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";
215 +MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
216 +MODULE_LICENSE("Dual BSD/GPL");
217 +MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
219 +#ifdef CONFIG_CAN_DEBUG_CORE
221 +module_param(debug, int, S_IRUGO);
222 +MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
225 +/* easy access to can_frame payload */
226 +static inline u64 GET_U64(const struct can_frame *cp)
228 + return *(u64 *)cp->data;
232 + struct list_head list;
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;
245 + struct can_frame *frames;
246 + struct can_frame *last_frames;
247 + struct can_frame sframe;
248 + struct can_frame last_sframe;
250 + struct net_device *rx_reg_dev;
253 +static struct proc_dir_entry *proc_dir;
255 +#ifdef CONFIG_CAN_BCM_USER
256 +#define BCM_CAP (-1)
258 +#define BCM_CAP CAP_NET_RAW
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 */
273 +static inline struct bcm_sock *bcm_sk(const struct sock *sk)
275 + return (struct bcm_sock *)sk;
278 +#define CFSIZ sizeof(struct can_frame)
279 +#define OPSIZ sizeof(struct bcm_op)
280 +#define MHSIZ sizeof(struct bcm_msg_head)
283 + * rounded_tv2jif - calculate jiffies from timeval including optional up
284 + * @tv: pointer to timeval
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.
293 + * calculated jiffies (max: ULONG_MAX)
295 +static unsigned long rounded_tv2jif(const struct timeval *tv)
297 + unsigned long sec = tv->tv_sec;
298 + unsigned long usec = tv->tv_usec;
301 + if (sec > ULONG_MAX / HZ)
304 + /* round up to get at least the requested time */
305 + usec += 1000000 / HZ - 1;
307 + jif = usec / (1000000 / HZ);
309 + if (sec * HZ > ULONG_MAX - jif)
312 + return jif + sec * HZ;
318 +static char *bcm_proc_getifname(int ifindex)
320 + struct net_device *dev;
325 + dev = __dev_get_by_index(ifindex); /* no usage counting */
332 +static int bcm_read_proc(char *page, char **start, off_t off,
333 + int count, int *eof, void *data)
336 + struct sock *sk = (struct sock *)data;
337 + struct bcm_sock *bo = bcm_sk(sk);
340 + len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
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");
350 + list_for_each_entry(op, &bo->rx_ops, list) {
352 + unsigned long reduction;
354 + /* print only active entries & prevent division by zero */
355 + if (!op->frames_abs)
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 ",
363 + (op->flags & RX_CHECK_DLC)?'d':' ');
365 + len += snprintf(page + len, PAGE_SIZE - len,
366 + "timeo=%ld ", op->j_ival1);
369 + len += snprintf(page + len, PAGE_SIZE - len,
370 + "thr=%ld ", op->j_ival2);
372 + len += snprintf(page + len, PAGE_SIZE - len,
373 + "# recv %ld (%ld) => reduction: ",
374 + op->frames_filtered, op->frames_abs);
376 + reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
378 + len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
379 + (reduction == 100)?"near ":"", reduction);
381 + if (len > PAGE_SIZE - 200) {
382 + /* mark output cut off */
383 + len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
388 + list_for_each_entry(op, &bo->tx_ops, list) {
390 + len += snprintf(page + len, PAGE_SIZE - len,
391 + "tx_op: %03X %s [%d] ",
392 + op->can_id, bcm_proc_getifname(op->ifindex),
395 + len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
399 + len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
402 + len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
405 + if (len > PAGE_SIZE - 100) {
406 + /* mark output cut off */
407 + len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
412 + len += snprintf(page + len, PAGE_SIZE - len, "\n");
419 + * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
420 + * of the given bcm tx op
422 +static void bcm_can_tx(struct bcm_op *op)
424 + struct sk_buff *skb;
425 + struct net_device *dev;
426 + struct can_frame *cf = &op->frames[op->currframe];
428 + DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
430 + /* no target device? => exit */
434 + dev = dev_get_by_index(op->ifindex);
437 + /* RFC: should this bcm_op remove itself here? */
441 + skb = alloc_skb(CFSIZ,
442 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
447 + memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
449 + /* send with loopback */
454 + /* update statistics */
458 + /* reached last frame? */
459 + if (op->currframe >= op->nframes)
466 + * bcm_send_to_user - send a BCM message to the userspace
467 + * (consisting of bcm_msg_head + x CAN frames)
469 +static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
470 + struct can_frame *frames, int has_timestamp)
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;
479 + skb = alloc_skb(sizeof(*head) + datalen,
480 + in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
484 + memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
486 + if (head->nframes) {
487 + /* can_frames starting here */
488 + firstframe = (struct can_frame *) skb_tail_pointer(skb);
490 + memcpy(skb_put(skb, datalen), frames, datalen);
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
498 + if (head->nframes == 1)
499 + firstframe->can_dlc &= BCM_CAN_DLC_MASK;
502 + if (has_timestamp) {
503 + /* restore rx timestamp */
504 + skb->tstamp = op->rx_stamp;
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;
513 + err = sock_queue_rcv_skb(sk, skb);
515 + struct bcm_sock *bo = bcm_sk(sk);
517 + DBG("sock_queue_rcv_skb failed: %d\n", err);
519 + /* don't care about overflows in this statistic */
520 + bo->dropped_usr_msgs++;
525 + * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
527 +static void bcm_tx_timeout_handler(unsigned long data)
529 + struct bcm_op *op = (struct bcm_op *)data;
531 + DBG("Called with bcm_op %p\n", op);
533 + if (op->j_ival1 && (op->count > 0)) {
536 + if (!op->count && (op->flags & TX_COUNTEVT)) {
537 + struct bcm_msg_head msg_head;
539 + /* create notification to user */
540 + DBG("sending TX_EXPIRED for can_id %03X\n",
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;
551 + bcm_send_to_user(op, &msg_head, NULL, 0);
555 + DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
556 + op->count, op->j_ival1, op->j_ival2);
558 + if (op->j_ival1 && (op->count > 0)) {
560 + op->timer.expires = jiffies + op->j_ival1;
561 + add_timer(&op->timer);
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);
568 + /* send (next) frame */
573 + op->timer.expires = jiffies + op->j_ival2;
574 + add_timer(&op->timer);
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);
581 + /* send (next) frame */
585 + DBG("no timer restart\n");
592 + * bcm_rx_changed - create a RX_CHANGED notification due to changed content
594 +static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
596 + struct bcm_msg_head head;
598 + op->j_lastmsg = jiffies;
600 + /* update statistics */
601 + op->frames_filtered++;
603 + /* prevent statistics overflow */
604 + if (op->frames_filtered > ULONG_MAX/100)
605 + op->frames_filtered = op->frames_abs = 0;
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");
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;
619 + bcm_send_to_user(op, &head, data, 1);
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)
627 +static void bcm_rx_update_and_send(struct bcm_op *op,
628 + struct can_frame *lastdata,
629 + struct can_frame *rxdata)
631 + unsigned long nexttx = op->j_lastmsg + op->j_ival2;
633 + memcpy(lastdata, rxdata, CFSIZ);
636 + lastdata->can_dlc |= RX_RECV;
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 */
643 + /* mark as 'throttled' */
644 + lastdata->can_dlc |= RX_THR;
646 + if (!(op->thrtimer.expires)) {
647 + /* start the timer only the first time */
648 + op->thrtimer.expires = nexttx;
649 + add_timer(&op->thrtimer);
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);
658 + /* send RX_CHANGED to the user immediately */
659 + bcm_rx_changed(op, rxdata);
664 + * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
665 + * received data stored in op->last_frames[]
667 +static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
668 + struct can_frame *rxdata)
671 + * no one uses the MSBs of can_dlc for comparation,
672 + * so we use it here to detect the first time of reception
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);
682 + /* do a real check in can_frame data section */
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));
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);
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],
707 + DBG("no relevant change :(\n");
711 + * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
713 +static void bcm_rx_starttimer(struct bcm_op *op)
715 + if (op->flags & RX_NO_AUTOTIMER)
719 + op->timer.expires = jiffies + op->j_ival1;
721 + DBG("adding rx timeout timer ival1. func=%p data=%p "
723 + op->timer.function,
724 + (char *) op->timer.data,
725 + (unsigned int) op->timer.expires);
727 + add_timer(&op->timer);
732 + * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
734 +static void bcm_rx_timeout_handler(unsigned long data)
736 + struct bcm_op *op = (struct bcm_op *)data;
737 + struct bcm_msg_head msg_head;
739 + DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
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;
749 + bcm_send_to_user(op, &msg_head, NULL, 0);
751 + /* no restart of the timer is done here! */
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");
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
765 +static void bcm_rx_thr_handler(unsigned long data)
767 + struct bcm_op *op = (struct bcm_op *)data;
770 + /* mark disabled / consumed timer */
771 + op->thrtimer.expires = 0;
773 + if (op->nframes > 1) {
774 + DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
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]);
786 + DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
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]);
797 + * bcm_rx_handler - handle a CAN frame receiption
799 +static void bcm_rx_handler(struct sk_buff *skb, void *data)
801 + struct bcm_op *op = (struct bcm_op *)data;
802 + struct can_frame rxframe;
805 + /* disable timeout */
806 + del_timer(&op->timer);
808 + DBG("Called with bcm_op %p\n", op);
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 */
819 + DBG("got can_frame with can_id %03X\n", rxframe.can_id);
822 + DBG("Wrong skb->len = %d\n", skb->len);
827 + DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
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);
835 + if (op->flags & RX_RTR_FRAME) {
836 + /* send reply for RTR-request */
837 + DBG("RTR-request\n");
839 + /* send op->frames[0] to CAN device */
844 + if (op->flags & RX_FILTER_ID) {
845 + /* the easiest case */
846 + DBG("Easy does it with RX_FILTER_ID\n");
848 + bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
849 + bcm_rx_starttimer(op);
853 + if (op->nframes == 1) {
854 + /* simple compare with index 0 */
855 + DBG("Simple compare\n");
857 + bcm_rx_cmp_to_index(op, 0, &rxframe);
858 + bcm_rx_starttimer(op);
862 + if (op->nframes > 1) {
863 + /* multiplex compare */
864 + DBG("Multiplex compare\n");
867 + * find the first multiplex mask that fits.
868 + * Remark: The MUX-mask is stored in index 0
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);
880 + bcm_rx_starttimer(op);
885 + * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
887 +static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
892 + list_for_each_entry(op, ops, list) {
893 + if ((op->can_id == can_id) && (op->ifindex == ifindex))
900 +static void bcm_remove_op(struct bcm_op *op)
902 + del_timer(&op->timer);
903 + del_timer(&op->thrtimer);
905 + if ((op->frames) && (op->frames != &op->sframe))
908 + if ((op->last_frames) && (op->last_frames != &op->last_sframe))
909 + kfree(op->last_frames);
916 +static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
918 + if (op->rx_reg_dev == dev) {
919 + can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
920 + bcm_rx_handler, op);
922 + /* mark as removed subscription */
923 + op->rx_reg_dev = NULL;
925 + printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
926 + "mismatch %p %p\n", op->rx_reg_dev, dev);
930 + * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
932 +static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
934 + struct bcm_op *op, *n;
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",
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.
948 + * Only remove subscriptions that had not
949 + * been removed due to NETDEV_UNREGISTER
950 + * in bcm_notifier()
952 + if (op->rx_reg_dev) {
953 + struct net_device *dev;
955 + dev = dev_get_by_index(op->ifindex);
957 + bcm_rx_unreg(dev, op);
962 + can_rx_unregister(NULL, op->can_id,
963 + REGMASK(op->can_id),
964 + bcm_rx_handler, op);
966 + list_del(&op->list);
968 + return 1; /* done */
972 + return 0; /* not found */
976 + * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
978 +static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
980 + struct bcm_op *op, *n;
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",
986 + list_del(&op->list);
988 + return 1; /* done */
992 + return 0; /* not found */
996 + * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
998 +static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
1001 + struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
1004 + DBG("TRX_READ: did not find op for can_id %03X\n",
1005 + msg_head->can_id);
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;
1018 + bcm_send_to_user(op, msg_head, op->frames, 0);
1024 + * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
1026 +static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1027 + int ifindex, struct sock *sk)
1029 + struct bcm_sock *bo = bcm_sk(sk);
1030 + struct bcm_op *op;
1033 + /* we need a real device to send frames */
1037 + /* we need at least one can_frame */
1038 + if (msg_head->nframes < 1)
1041 + /* check the given can_id */
1042 + op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
1045 + /* update existing BCM operation */
1047 + DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
1048 + op, msg_head->can_id);
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.
1055 + if (msg_head->nframes > op->nframes)
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);
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;
1072 + /* insert new BCM operation for the given can_id */
1074 + op = kzalloc(OPSIZ, GFP_KERNEL);
1078 + DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
1079 + op, msg_head->can_id);
1081 + op->can_id = msg_head->can_id;
1083 + /* create array for can_frames and copy the data */
1084 + if (msg_head->nframes > 1) {
1085 + op->frames = kmalloc(msg_head->nframes * CFSIZ,
1087 + if (!op->frames) {
1092 + op->frames = &op->sframe;
1094 + for (i = 0; i < msg_head->nframes; i++) {
1095 + err = memcpy_fromiovec((u8 *)&op->frames[i],
1096 + msg->msg_iov, CFSIZ);
1098 + if (op->frames != &op->sframe)
1099 + kfree(op->frames);
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;
1110 + /* tx_ops never compare with previous received messages */
1111 + op->last_frames = NULL;
1113 + /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1116 + op->ifindex = ifindex;
1118 + /* initialize uninitialized (kmalloc) structure */
1119 + init_timer(&op->timer);
1121 + /* currently unused in tx_ops */
1122 + init_timer(&op->thrtimer);
1124 + /* handler for tx_ops */
1125 + op->timer.function = bcm_tx_timeout_handler;
1127 + /* timer.data points to this op-structure */
1128 + op->timer.data = (unsigned long)op;
1130 + /* add this bcm_op to the list of the tx_ops */
1131 + list_add(&op->list, &bo->tx_ops);
1133 + } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
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;
1143 + op->flags = msg_head->flags;
1145 + if (op->flags & TX_RESET_MULTI_IDX) {
1146 + /* start multiple frame transmission with index 0 */
1147 + op->currframe = 0;
1150 + if (op->flags & SETTIMER) {
1151 + /* set timer values */
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);
1159 + DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1160 + op->count, op->j_ival1, op->j_ival2);
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");
1169 + if ((op->flags & STARTTIMER) &&
1170 + ((op->j_ival1 && op->count) || op->j_ival2)) {
1172 + del_timer(&op->timer);
1174 + /* spec: send can_frame when starting timer */
1175 + op->flags |= TX_ANNOUNCE;
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 "
1182 + op->timer.function,
1183 + (char *) op->timer.data,
1184 + (unsigned int) op->timer.expires);
1187 + op->timer.expires = jiffies + op->j_ival2;
1188 + DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1190 + op->timer.function,
1191 + (char *) op->timer.data,
1192 + (unsigned int) op->timer.expires);
1195 + add_timer(&op->timer);
1198 + if (op->flags & TX_ANNOUNCE)
1201 + return msg_head->nframes * CFSIZ + MHSIZ;
1205 + * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1207 +static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1208 + int ifindex, struct sock *sk)
1210 + struct bcm_sock *bo = bcm_sk(sk);
1211 + struct bcm_op *op;
1212 + int do_rx_register;
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 */
1221 + if ((msg_head->flags & RX_RTR_FRAME) &&
1222 + ((msg_head->nframes != 1) ||
1223 + (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1225 + DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1229 + /* check the given can_id */
1230 + op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1232 + /* update existing BCM operation */
1234 + DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1235 + op, msg_head->can_id);
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.
1242 + if (msg_head->nframes > op->nframes)
1245 + if (msg_head->nframes) {
1246 + /* update can_frames content */
1247 + err = memcpy_fromiovec((u8 *)op->frames,
1249 + msg_head->nframes * CFSIZ);
1253 + /* clear last_frames to indicate 'nothing received' */
1254 + memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1257 + op->nframes = msg_head->nframes;
1259 + /* Only an update -> do not call can_rx_register() */
1260 + do_rx_register = 0;
1263 + /* insert new BCM operation for the given can_id */
1265 + op = kzalloc(OPSIZ, GFP_KERNEL);
1269 + DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1270 + op, msg_head->can_id);
1272 + op->can_id = msg_head->can_id;
1273 + op->nframes = msg_head->nframes;
1275 + if (msg_head->nframes > 1) {
1276 + /* create array for can_frames and copy the data */
1277 + op->frames = kmalloc(msg_head->nframes * CFSIZ,
1279 + if (!op->frames) {
1284 + /* create and init array for received can_frames */
1285 + op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1287 + if (!op->last_frames) {
1288 + kfree(op->frames);
1294 + op->frames = &op->sframe;
1295 + op->last_frames = &op->last_sframe;
1298 + if (msg_head->nframes) {
1299 + err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1300 + msg_head->nframes * CFSIZ);
1302 + if (op->frames != &op->sframe)
1303 + kfree(op->frames);
1304 + if (op->last_frames != &op->last_sframe)
1305 + kfree(op->last_frames);
1312 + op->ifindex = ifindex;
1314 + /* initialize uninitialized (kzalloc) structure */
1315 + init_timer(&op->timer);
1317 + /* init throttle timer for RX_CHANGED */
1318 + init_timer(&op->thrtimer);
1320 + /* handler for rx timeouts */
1321 + op->timer.function = bcm_rx_timeout_handler;
1323 + /* timer.data points to this op-structure */
1324 + op->timer.data = (unsigned long)op;
1326 + /* handler for RX_CHANGED throttle timeouts */
1327 + op->thrtimer.function = bcm_rx_thr_handler;
1329 + /* timer.data points to this op-structure */
1330 + op->thrtimer.data = (unsigned long)op;
1332 + /* mark disabled timer */
1333 + op->thrtimer.expires = 0;
1335 + /* add this bcm_op to the list of the tx_ops */
1336 + list_add(&op->list, &bo->rx_ops);
1338 + /* call can_rx_register() */
1339 + do_rx_register = 1;
1341 + } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1344 + op->flags = msg_head->flags;
1346 + if (op->flags & RX_RTR_FRAME) {
1348 + /* no timers in RTR-mode */
1349 + del_timer(&op->thrtimer);
1350 + del_timer(&op->timer);
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 ... ;-]
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;
1362 + if (op->flags & SETTIMER) {
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);
1370 + DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1371 + op->j_ival1, op->j_ival2);
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");
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);
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()
1394 + if ((op->flags & STARTTIMER) && op->j_ival1) {
1396 + del_timer(&op->timer);
1397 + op->timer.expires = jiffies + op->j_ival1;
1399 + DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1401 + (char *) op->timer.function,
1402 + (char *) op->timer.data,
1403 + (unsigned int) op->timer.expires);
1405 + add_timer(&op->timer);
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);
1415 + struct net_device *dev = dev_get_by_index(ifindex);
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;
1426 + can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1427 + bcm_rx_handler, op, IDENT);
1430 + return msg_head->nframes * CFSIZ + MHSIZ;
1434 + * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1436 +static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1438 + struct sk_buff *skb;
1439 + struct net_device *dev;
1442 + /* just copy and send one can_frame */
1444 + if (!ifindex) /* we need a real device to send frames */
1447 + skb = alloc_skb(CFSIZ, GFP_KERNEL);
1452 + err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1458 + DBG_FRAME("BCM: TX_SEND: sending frame",
1459 + (struct can_frame *)skb->data);
1461 + dev = dev_get_by_index(ifindex);
1469 + can_send(skb, 1); /* send with loopback */
1472 + return CFSIZ + MHSIZ;
1476 + * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1478 +static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1479 + struct msghdr *msg, size_t size)
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 */
1488 + DBG("sock %p not bound\n", sk);
1492 + /* check for alternative ifindex for this bcm_op */
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;
1499 + if (addr->can_family != AF_CAN)
1502 + ifindex = addr->can_ifindex; /* ifindex from sendto() */
1505 + struct net_device *dev = dev_get_by_index(ifindex);
1508 + DBG("device %d not found\n", ifindex);
1512 + if (dev->type != ARPHRD_CAN) {
1513 + DBG("device %d no CAN device\n", ifindex);
1522 + /* read message head information */
1524 + ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1528 + DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1532 + switch (msg_head.opcode) {
1535 + ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1539 + ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1543 + if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1550 + if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
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);
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);
1569 + /* we need at least one can_frame */
1570 + if (msg_head.nframes < 1)
1573 + ret = bcm_tx_send(msg, ifindex, sk);
1577 + DBG("Unknown opcode %d\n", msg_head.opcode);
1588 + * notification handler for netdevice status changes
1590 +static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
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;
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);
1602 + if (dev->type != ARPHRD_CAN)
1603 + return NOTIFY_DONE;
1607 + case NETDEV_UNREGISTER:
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);
1615 + /* remove device reference, if this is our bound device */
1616 + if (bo->bound && bo->ifindex == dev->ifindex) {
1619 + notify_enodev = 1;
1624 + if (notify_enodev) {
1625 + sk->sk_err = ENODEV;
1626 + if (!sock_flag(sk, SOCK_DEAD))
1627 + sk->sk_error_report(sk);
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);
1639 + return NOTIFY_DONE;
1643 + * initial settings for all BCM sockets to be set at socket creation time
1645 +static int bcm_init(struct sock *sk)
1647 + struct bcm_sock *bo = bcm_sk(sk);
1651 + bo->dropped_usr_msgs = 0;
1652 + bo->bcm_proc_read = NULL;
1654 + INIT_LIST_HEAD(&bo->tx_ops);
1655 + INIT_LIST_HEAD(&bo->rx_ops);
1657 + /* set notifier */
1658 + bo->notifier.notifier_call = bcm_notifier;
1660 + register_netdevice_notifier(&bo->notifier);
1666 + * standard socket functions
1668 +static int bcm_release(struct socket *sock)
1670 + struct sock *sk = sock->sk;
1671 + struct bcm_sock *bo = bcm_sk(sk);
1672 + struct bcm_op *op, *next;
1674 + DBG("socket %p, sk %p\n", sock, sk);
1676 + /* remove bcm_ops, timer, rx_unregister(), etc. */
1678 + unregister_netdevice_notifier(&bo->notifier);
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);
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);
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.
1694 + if (op->ifindex) {
1696 + * Only remove subscriptions that had not
1697 + * been removed due to NETDEV_UNREGISTER
1698 + * in bcm_notifier()
1700 + if (op->rx_reg_dev) {
1701 + struct net_device *dev;
1703 + dev = dev_get_by_index(op->ifindex);
1705 + bcm_rx_unreg(dev, op);
1710 + can_rx_unregister(NULL, op->can_id,
1711 + REGMASK(op->can_id),
1712 + bcm_rx_handler, op);
1714 + bcm_remove_op(op);
1717 + /* remove procfs entry */
1718 + if (proc_dir && bo->bcm_proc_read)
1719 + remove_proc_entry(bo->procname, proc_dir);
1721 + /* remove device reference */
1733 +static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1736 + struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1737 + struct sock *sk = sock->sk;
1738 + struct bcm_sock *bo = bcm_sk(sk);
1743 + /* bind a device to this socket */
1744 + if (addr->can_ifindex) {
1745 + struct net_device *dev = dev_get_by_index(addr->can_ifindex);
1748 + DBG("could not find device index %d\n",
1749 + addr->can_ifindex);
1753 + if (dev->type != ARPHRD_CAN) {
1754 + DBG("device %d no CAN device\n", addr->can_ifindex);
1759 + bo->ifindex = dev->ifindex;
1762 + DBG("socket %p bound to device %s (idx %d)\n",
1763 + sock, dev->name, dev->ifindex);
1766 + /* no interface reference for ifindex = 0 ('any' CAN device) */
1773 + /* unique socket address as filename */
1774 + sprintf(bo->procname, "%p", sock);
1775 + bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1777 + bcm_read_proc, sk);
1783 +static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1784 + struct msghdr *msg, size_t size, int flags)
1786 + struct sock *sk = sock->sk;
1787 + struct sk_buff *skb;
1792 + DBG("socket %p, sk %p\n", sock, sk);
1794 + noblock = flags & MSG_DONTWAIT;
1795 + flags &= ~MSG_DONTWAIT;
1796 + skb = skb_recv_datagram(sk, flags, noblock, &error);
1800 + DBG("delivering skbuff %p\n", skb);
1803 + if (skb->len < size)
1806 + err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1808 + skb_free_datagram(sk, skb);
1812 + sock_recv_timestamp(msg, sk, skb);
1814 + if (msg->msg_name) {
1815 + msg->msg_namelen = sizeof(struct sockaddr_can);
1816 + memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1819 + DBG("freeing sock %p, skbuff %p\n", sk, skb);
1820 + skb_free_datagram(sk, skb);
1825 +static unsigned int bcm_poll(struct file *file, struct socket *sock,
1828 + unsigned int mask = 0;
1830 + DBG("socket %p\n", sock);
1832 + mask = datagram_poll(file, sock, wait);
1836 +static struct proto_ops bcm_ops = {
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,
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,
1856 +static struct proto bcm_proto = {
1857 + .name = "CAN_BCM",
1858 + .owner = THIS_MODULE,
1859 + .obj_size = sizeof(struct bcm_sock),
1863 +static struct can_proto bcm_can_proto = {
1864 + .type = SOCK_DGRAM,
1865 + .protocol = CAN_BCM,
1866 + .capability = BCM_CAP,
1868 + .prot = &bcm_proto,
1871 +static int __init bcm_module_init(void)
1875 + can_proto_register(&bcm_can_proto);
1877 + /* create /proc/net/can/bcm directory */
1878 + proc_dir = proc_mkdir(CAN_PROC_DIR"/"IDENT, NULL);
1881 + proc_dir->owner = THIS_MODULE;
1886 +static void __exit bcm_module_exit(void)
1888 + can_proto_unregister(&bcm_can_proto);
1891 + remove_proc_entry(CAN_PROC_DIR"/"IDENT, NULL);
1894 +module_init(bcm_module_init);
1895 +module_exit(bcm_module_exit);