2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
48 #include <linux/hrtimer.h>
50 #include <linux/list.h>
51 #include <linux/proc_fs.h>
52 #include <linux/uio.h>
53 #include <linux/net.h>
54 #include <linux/netdevice.h>
55 #include <linux/socket.h>
56 #include <linux/if_arp.h>
57 #include <linux/skbuff.h>
58 #include <linux/can.h>
59 #include <linux/can/core.h>
60 #include <linux/can/bcm.h>
62 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
63 #include <net/net_namespace.h>
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
69 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
72 /* use of last_frames[index].can_dlc */
73 #define RX_RECV 0x40 /* received data for this element */
74 #define RX_THR 0x80 /* element not been sent due to throttle feature */
75 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
77 /* get best masking value for can_rx_register() for a given single can_id */
78 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
79 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
81 #define CAN_BCM_VERSION "20080415"
82 static __initdata const char banner[] = KERN_INFO
83 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
85 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
86 MODULE_LICENSE("Dual BSD/GPL");
87 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
89 /* easy access to can_frame payload */
90 static inline u64 GET_U64(const struct can_frame *cp)
92 return *(u64 *)cp->data;
96 struct list_head list;
100 unsigned long frames_abs, frames_filtered;
101 struct timeval ival1, ival2;
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
103 struct hrtimer timer, thrtimer;
104 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
106 struct timer_list timer, thrtimer;
107 struct timeval rx_stamp;
108 unsigned long j_ival1, j_ival2, j_lastmsg;
114 struct can_frame *frames;
115 struct can_frame *last_frames;
116 struct can_frame sframe;
117 struct can_frame last_sframe;
119 struct net_device *rx_reg_dev;
122 static struct proc_dir_entry *proc_dir;
125 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
132 struct notifier_block notifier;
133 struct list_head rx_ops;
134 struct list_head tx_ops;
135 unsigned long dropped_usr_msgs;
136 struct proc_dir_entry *bcm_proc_read;
137 char procname [9]; /* pointer printed in ASCII with \0 */
140 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
142 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
143 return (struct bcm_sock *)sk;
145 return (struct bcm_sock *)sk->sk_protinfo;
149 #define CFSIZ sizeof(struct can_frame)
150 #define OPSIZ sizeof(struct bcm_op)
151 #define MHSIZ sizeof(struct bcm_msg_head)
153 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
155 * rounded_tv2jif - calculate jiffies from timeval including optional up
156 * @tv: pointer to timeval
159 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
160 * function is intentionally more relaxed on precise timer ticks to get
161 * exact one jiffy for requested 1000us on a 1000HZ machine.
162 * This code is to be removed when upgrading to kernel hrtimer.
165 * calculated jiffies (max: ULONG_MAX)
167 static unsigned long rounded_tv2jif(const struct timeval *tv)
169 unsigned long sec = tv->tv_sec;
170 unsigned long usec = tv->tv_usec;
173 if (sec > ULONG_MAX / HZ)
176 /* round up to get at least the requested time */
177 usec += 1000000 / HZ - 1;
179 jif = usec / (1000000 / HZ);
181 if (sec * HZ > ULONG_MAX - jif)
184 return jif + sec * HZ;
191 static char *bcm_proc_getifname(int ifindex)
193 struct net_device *dev;
198 /* no usage counting */
199 dev = __dev_get_by_index(&init_net, ifindex);
206 static int bcm_read_proc(char *page, char **start, off_t off,
207 int count, int *eof, void *data)
210 struct sock *sk = (struct sock *)data;
211 struct bcm_sock *bo = bcm_sk(sk);
214 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
216 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
217 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
218 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
219 bo->dropped_usr_msgs);
220 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
221 bcm_proc_getifname(bo->ifindex));
222 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
224 list_for_each_entry(op, &bo->rx_ops, list) {
226 unsigned long reduction;
228 /* print only active entries & prevent division by zero */
232 len += snprintf(page + len, PAGE_SIZE - len,
234 op->can_id, bcm_proc_getifname(op->ifindex));
235 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
237 (op->flags & RX_CHECK_DLC)?'d':' ');
238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
239 if (op->kt_ival1.tv64)
240 len += snprintf(page + len, PAGE_SIZE - len,
243 ktime_to_us(op->kt_ival1));
245 if (op->kt_ival2.tv64)
246 len += snprintf(page + len, PAGE_SIZE - len,
249 ktime_to_us(op->kt_ival2));
252 len += snprintf(page + len, PAGE_SIZE - len,
253 "timeo=%ld ", op->j_ival1);
256 len += snprintf(page + len, PAGE_SIZE - len,
257 "thr=%ld ", op->j_ival2);
260 len += snprintf(page + len, PAGE_SIZE - len,
261 "# recv %ld (%ld) => reduction: ",
262 op->frames_filtered, op->frames_abs);
264 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
266 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
267 (reduction == 100)?"near ":"", reduction);
269 if (len > PAGE_SIZE - 200) {
270 /* mark output cut off */
271 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
276 list_for_each_entry(op, &bo->tx_ops, list) {
278 len += snprintf(page + len, PAGE_SIZE - len,
279 "tx_op: %03X %s [%d] ",
280 op->can_id, bcm_proc_getifname(op->ifindex),
283 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
284 if (op->kt_ival1.tv64)
285 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
286 (long long) ktime_to_us(op->kt_ival1));
288 if (op->kt_ival2.tv64)
289 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
290 (long long) ktime_to_us(op->kt_ival2));
293 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
297 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
301 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
304 if (len > PAGE_SIZE - 100) {
305 /* mark output cut off */
306 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
311 len += snprintf(page + len, PAGE_SIZE - len, "\n");
318 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
319 * of the given bcm tx op
321 static void bcm_can_tx(struct bcm_op *op)
324 struct net_device *dev;
325 struct can_frame *cf = &op->frames[op->currframe];
327 /* no target device? => exit */
331 dev = dev_get_by_index(&init_net, op->ifindex);
333 /* RFC: should this bcm_op remove itself here? */
337 skb = alloc_skb(CFSIZ, gfp_any());
341 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
343 /* send with loopback */
348 /* update statistics */
352 /* reached last frame? */
353 if (op->currframe >= op->nframes)
360 * bcm_send_to_user - send a BCM message to the userspace
361 * (consisting of bcm_msg_head + x CAN frames)
363 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
364 struct can_frame *frames, int has_timestamp)
367 struct can_frame *firstframe;
368 struct sockaddr_can *addr;
369 struct sock *sk = op->sk;
370 int datalen = head->nframes * CFSIZ;
373 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
377 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
380 /* can_frames starting here */
381 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
382 firstframe = (struct can_frame *)skb_tail_pointer(skb);
384 firstframe = (struct can_frame *)skb->tail;
387 memcpy(skb_put(skb, datalen), frames, datalen);
390 * the BCM uses the can_dlc-element of the can_frame
391 * structure for internal purposes. This is only
392 * relevant for updates that are generated by the
393 * BCM, where nframes is 1
395 if (head->nframes == 1)
396 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
400 /* restore rx timestamp */
401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
402 skb->tstamp = op->rx_stamp;
404 skb_set_timestamp(skb, &op->rx_stamp);
409 * Put the datagram to the queue so that bcm_recvmsg() can
410 * get it from there. We need to pass the interface index to
411 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
412 * containing the interface index.
415 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
416 addr = (struct sockaddr_can *)skb->cb;
417 memset(addr, 0, sizeof(*addr));
418 addr->can_family = AF_CAN;
419 addr->can_ifindex = op->rx_ifindex;
421 err = sock_queue_rcv_skb(sk, skb);
423 struct bcm_sock *bo = bcm_sk(sk);
426 /* don't care about overflows in this statistic */
427 bo->dropped_usr_msgs++;
432 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
434 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
435 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
437 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
438 enum hrtimer_restart ret = HRTIMER_NORESTART;
440 if (op->kt_ival1.tv64 && (op->count > 0)) {
442 static void bcm_tx_timeout_handler(unsigned long data)
444 struct bcm_op *op = (struct bcm_op *)data;
446 if (op->j_ival1 && (op->count > 0)) {
450 if (!op->count && (op->flags & TX_COUNTEVT)) {
451 struct bcm_msg_head msg_head;
453 /* create notification to user */
454 msg_head.opcode = TX_EXPIRED;
455 msg_head.flags = op->flags;
456 msg_head.count = op->count;
457 msg_head.ival1 = op->ival1;
458 msg_head.ival2 = op->ival2;
459 msg_head.can_id = op->can_id;
460 msg_head.nframes = 0;
462 bcm_send_to_user(op, &msg_head, NULL, 0);
466 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
467 if (op->kt_ival1.tv64 && (op->count > 0)) {
469 /* send (next) frame */
471 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
472 ret = HRTIMER_RESTART;
475 if (op->kt_ival2.tv64) {
477 /* send (next) frame */
479 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
480 ret = HRTIMER_RESTART;
486 if (op->j_ival1 && (op->count > 0)) {
488 /* send (next) frame */
490 mod_timer(&op->timer, jiffies + op->j_ival1);
495 /* send (next) frame */
497 mod_timer(&op->timer, jiffies + op->j_ival2);
506 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
508 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
510 struct bcm_msg_head head;
512 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
513 op->j_lastmsg = jiffies;
516 /* update statistics */
517 op->frames_filtered++;
519 /* prevent statistics overflow */
520 if (op->frames_filtered > ULONG_MAX/100)
521 op->frames_filtered = op->frames_abs = 0;
523 head.opcode = RX_CHANGED;
524 head.flags = op->flags;
525 head.count = op->count;
526 head.ival1 = op->ival1;
527 head.ival2 = op->ival2;
528 head.can_id = op->can_id;
531 bcm_send_to_user(op, &head, data, 1);
534 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
535 /* is part of linux/hrtimer.h since 2.6.26 */
536 static inline int hrtimer_callback_running(struct hrtimer *timer)
538 return timer->state & HRTIMER_STATE_CALLBACK;
541 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
542 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
544 return ktime_to_us(ktime_sub(later, earlier));
548 * bcm_rx_update_and_send - process a detected relevant receive content change
549 * 1. update the last received data
550 * 2. send a notification to the user (if possible)
552 static void bcm_rx_update_and_send(struct bcm_op *op,
553 struct can_frame *lastdata,
554 struct can_frame *rxdata)
556 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
557 memcpy(lastdata, rxdata, CFSIZ);
560 lastdata->can_dlc |= RX_RECV;
562 /* throtteling mode inactive OR data update already on the run ? */
563 if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
564 /* send RX_CHANGED to the user immediately */
565 bcm_rx_changed(op, rxdata);
569 if (hrtimer_active(&op->thrtimer)) {
570 /* mark as 'throttled' */
571 lastdata->can_dlc |= RX_THR;
575 if (!op->kt_lastmsg.tv64) {
576 /* send first RX_CHANGED to the user immediately */
577 bcm_rx_changed(op, rxdata);
578 op->kt_lastmsg = ktime_get();
582 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
583 ktime_to_us(op->kt_ival2)) {
584 /* mark as 'throttled' and start timer */
585 lastdata->can_dlc |= RX_THR;
586 hrtimer_start(&op->thrtimer,
587 ktime_add(op->kt_lastmsg, op->kt_ival2),
592 /* the gap was that big, that throttling was not needed here */
593 bcm_rx_changed(op, rxdata);
594 op->kt_lastmsg = ktime_get();
596 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
598 memcpy(lastdata, rxdata, CFSIZ);
601 lastdata->can_dlc |= RX_RECV;
603 /* throttle bcm_rx_changed ? */
604 if ((op->thrtimer.expires) ||
605 ((op->j_ival2) && (nexttx > jiffies))) {
606 /* we are already waiting OR we have to start waiting */
608 /* mark as 'throttled' */
609 lastdata->can_dlc |= RX_THR;
611 if (!(op->thrtimer.expires)) {
612 /* start the timer only the first time */
613 mod_timer(&op->thrtimer, nexttx);
617 /* send RX_CHANGED to the user immediately */
618 bcm_rx_changed(op, rxdata);
624 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
625 * received data stored in op->last_frames[]
627 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
628 struct can_frame *rxdata)
631 * no one uses the MSBs of can_dlc for comparation,
632 * so we use it here to detect the first time of reception
635 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
636 /* received data for the first time => send update to user */
637 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
641 /* do a real check in can_frame data section */
643 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
644 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
645 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
649 if (op->flags & RX_CHECK_DLC) {
650 /* do a real check in can_frame dlc */
651 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
653 bcm_rx_update_and_send(op, &op->last_frames[index],
661 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
663 static void bcm_rx_starttimer(struct bcm_op *op)
665 if (op->flags & RX_NO_AUTOTIMER)
668 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
669 if (op->kt_ival1.tv64)
670 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
673 mod_timer(&op->timer, jiffies + op->j_ival1);
678 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
680 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
681 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
683 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
685 static void bcm_rx_timeout_handler(unsigned long data)
687 struct bcm_op *op = (struct bcm_op *)data;
689 struct bcm_msg_head msg_head;
691 msg_head.opcode = RX_TIMEOUT;
692 msg_head.flags = op->flags;
693 msg_head.count = op->count;
694 msg_head.ival1 = op->ival1;
695 msg_head.ival2 = op->ival2;
696 msg_head.can_id = op->can_id;
697 msg_head.nframes = 0;
699 bcm_send_to_user(op, &msg_head, NULL, 0);
701 /* no restart of the timer is done here! */
703 /* if user wants to be informed, when cyclic CAN-Messages come back */
704 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
705 /* clear received can_frames to indicate 'nothing received' */
706 memset(op->last_frames, 0, op->nframes * CFSIZ);
708 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
710 return HRTIMER_NORESTART;
715 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
717 static int bcm_rx_thr_flush(struct bcm_op *op)
721 if (op->nframes > 1) {
724 /* for MUX filter we start at index 1 */
725 for (i = 1; i < op->nframes; i++) {
726 if ((op->last_frames) &&
727 (op->last_frames[i].can_dlc & RX_THR)) {
728 op->last_frames[i].can_dlc &= ~RX_THR;
729 bcm_rx_changed(op, &op->last_frames[i]);
735 /* for RX_FILTER_ID and simple filter */
736 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
737 op->last_frames[0].can_dlc &= ~RX_THR;
738 bcm_rx_changed(op, &op->last_frames[0]);
747 * bcm_rx_thr_handler - the time for blocked content updates is over now:
748 * Check for throttled data and send it to the userspace
750 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
751 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
753 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
755 if (bcm_rx_thr_flush(op)) {
756 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
757 return HRTIMER_RESTART;
759 /* rearm throttle handling */
760 op->kt_lastmsg = ktime_set(0, 0);
761 return HRTIMER_NORESTART;
765 static void bcm_rx_thr_handler(unsigned long data)
767 struct bcm_op *op = (struct bcm_op *)data;
769 if (bcm_rx_thr_flush(op))
770 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
772 /* mark disabled / consumed timer */
773 op->thrtimer.expires = 0;
779 * bcm_rx_handler - handle a CAN frame receiption
781 static void bcm_rx_handler(struct sk_buff *skb, void *data)
783 struct bcm_op *op = (struct bcm_op *)data;
784 struct can_frame rxframe;
787 /* disable timeout */
788 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
789 hrtimer_cancel(&op->timer);
791 del_timer(&op->timer);
794 if (skb->len == sizeof(rxframe)) {
795 memcpy(&rxframe, skb->data, sizeof(rxframe));
796 /* save rx timestamp */
797 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
798 op->rx_stamp = skb->tstamp;
800 skb_get_timestamp(skb, &op->rx_stamp);
802 /* save originator for recvfrom() */
803 op->rx_ifindex = skb->dev->ifindex;
804 /* update statistics */
813 if (op->can_id != rxframe.can_id)
816 if (op->flags & RX_RTR_FRAME) {
817 /* send reply for RTR-request (placed in op->frames[0]) */
822 if (op->flags & RX_FILTER_ID) {
823 /* the easiest case */
824 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
825 bcm_rx_starttimer(op);
829 if (op->nframes == 1) {
830 /* simple compare with index 0 */
831 bcm_rx_cmp_to_index(op, 0, &rxframe);
832 bcm_rx_starttimer(op);
836 if (op->nframes > 1) {
840 * find the first multiplex mask that fits.
841 * Remark: The MUX-mask is stored in index 0
844 for (i = 1; i < op->nframes; i++) {
845 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
846 (GET_U64(&op->frames[0]) &
847 GET_U64(&op->frames[i]))) {
848 bcm_rx_cmp_to_index(op, i, &rxframe);
852 bcm_rx_starttimer(op);
857 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
859 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
864 list_for_each_entry(op, ops, list) {
865 if ((op->can_id == can_id) && (op->ifindex == ifindex))
872 static void bcm_remove_op(struct bcm_op *op)
874 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
875 hrtimer_cancel(&op->timer);
876 hrtimer_cancel(&op->thrtimer);
878 del_timer(&op->timer);
879 del_timer(&op->thrtimer);
882 if ((op->frames) && (op->frames != &op->sframe))
885 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
886 kfree(op->last_frames);
893 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
895 if (op->rx_reg_dev == dev) {
896 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
899 /* mark as removed subscription */
900 op->rx_reg_dev = NULL;
902 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
903 "mismatch %p %p\n", op->rx_reg_dev, dev);
907 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
909 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
911 struct bcm_op *op, *n;
913 list_for_each_entry_safe(op, n, ops, list) {
914 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
917 * Don't care if we're bound or not (due to netdev
918 * problems) can_rx_unregister() is always a save
923 * Only remove subscriptions that had not
924 * been removed due to NETDEV_UNREGISTER
927 if (op->rx_reg_dev) {
928 struct net_device *dev;
930 dev = dev_get_by_index(&init_net,
933 bcm_rx_unreg(dev, op);
938 can_rx_unregister(NULL, op->can_id,
948 return 0; /* not found */
952 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
954 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
956 struct bcm_op *op, *n;
958 list_for_each_entry_safe(op, n, ops, list) {
959 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
966 return 0; /* not found */
970 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
972 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
975 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
980 /* put current values into msg_head */
981 msg_head->flags = op->flags;
982 msg_head->count = op->count;
983 msg_head->ival1 = op->ival1;
984 msg_head->ival2 = op->ival2;
985 msg_head->nframes = op->nframes;
987 bcm_send_to_user(op, msg_head, op->frames, 0);
993 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
995 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
996 int ifindex, struct sock *sk)
998 struct bcm_sock *bo = bcm_sk(sk);
1002 /* we need a real device to send frames */
1006 /* we need at least one can_frame */
1007 if (msg_head->nframes < 1)
1010 /* check the given can_id */
1011 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
1014 /* update existing BCM operation */
1017 * Do we need more space for the can_frames than currently
1018 * allocated? -> This is a _really_ unusual use-case and
1019 * therefore (complexity / locking) it is not supported.
1021 if (msg_head->nframes > op->nframes)
1024 /* update can_frames content */
1025 for (i = 0; i < msg_head->nframes; i++) {
1026 err = memcpy_fromiovec((u8 *)&op->frames[i],
1027 msg->msg_iov, CFSIZ);
1029 if (op->frames[i].can_dlc > 8)
1035 if (msg_head->flags & TX_CP_CAN_ID) {
1036 /* copy can_id into frame */
1037 op->frames[i].can_id = msg_head->can_id;
1042 /* insert new BCM operation for the given can_id */
1044 op = kzalloc(OPSIZ, GFP_KERNEL);
1048 op->can_id = msg_head->can_id;
1050 /* create array for can_frames and copy the data */
1051 if (msg_head->nframes > 1) {
1052 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1059 op->frames = &op->sframe;
1061 for (i = 0; i < msg_head->nframes; i++) {
1062 err = memcpy_fromiovec((u8 *)&op->frames[i],
1063 msg->msg_iov, CFSIZ);
1065 if (op->frames[i].can_dlc > 8)
1069 if (op->frames != &op->sframe)
1075 if (msg_head->flags & TX_CP_CAN_ID) {
1076 /* copy can_id into frame */
1077 op->frames[i].can_id = msg_head->can_id;
1081 /* tx_ops never compare with previous received messages */
1082 op->last_frames = NULL;
1084 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1086 op->ifindex = ifindex;
1088 /* initialize uninitialized (kzalloc) structure */
1089 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1090 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1091 op->timer.function = bcm_tx_timeout_handler;
1093 /* currently unused in tx_ops */
1094 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1096 setup_timer(&op->timer, bcm_tx_timeout_handler,
1099 /* currently unused in tx_ops */
1100 init_timer(&op->thrtimer);
1103 /* add this bcm_op to the list of the tx_ops */
1104 list_add(&op->list, &bo->tx_ops);
1106 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1108 if (op->nframes != msg_head->nframes) {
1109 op->nframes = msg_head->nframes;
1110 /* start multiple frame transmission with index 0 */
1116 op->flags = msg_head->flags;
1118 if (op->flags & TX_RESET_MULTI_IDX) {
1119 /* start multiple frame transmission with index 0 */
1123 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1124 if (op->flags & SETTIMER) {
1125 /* set timer values */
1126 op->count = msg_head->count;
1127 op->ival1 = msg_head->ival1;
1128 op->ival2 = msg_head->ival2;
1129 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1130 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1132 /* disable an active timer due to zero values? */
1133 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1134 hrtimer_cancel(&op->timer);
1137 if ((op->flags & STARTTIMER) &&
1138 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1140 /* spec: send can_frame when starting timer */
1141 op->flags |= TX_ANNOUNCE;
1143 if (op->kt_ival1.tv64 && (op->count > 0)) {
1144 /* op->count-- is done in bcm_tx_timeout_handler */
1145 hrtimer_start(&op->timer, op->kt_ival1,
1148 hrtimer_start(&op->timer, op->kt_ival2,
1152 if (op->flags & SETTIMER) {
1153 /* set timer values */
1154 op->count = msg_head->count;
1155 op->ival1 = msg_head->ival1;
1156 op->ival2 = msg_head->ival2;
1157 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1158 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1160 /* disable an active timer due to zero values? */
1161 if (!op->j_ival1 && !op->j_ival2)
1162 del_timer(&op->timer);
1165 if ((op->flags & STARTTIMER) &&
1166 ((op->j_ival1 && op->count) || op->j_ival2)) {
1168 /* spec: send can_frame when starting timer */
1169 op->flags |= TX_ANNOUNCE;
1171 if (op->j_ival1 && (op->count > 0)) {
1172 /* op->count-- is done in bcm_tx_timeout_handler */
1173 mod_timer(&op->timer, jiffies + op->j_ival1);
1175 mod_timer(&op->timer, jiffies + op->j_ival2);
1179 if (op->flags & TX_ANNOUNCE)
1182 return msg_head->nframes * CFSIZ + MHSIZ;
1186 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1188 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1189 int ifindex, struct sock *sk)
1191 struct bcm_sock *bo = bcm_sk(sk);
1196 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1197 /* be robust against wrong usage ... */
1198 msg_head->flags |= RX_FILTER_ID;
1199 /* ignore trailing garbage */
1200 msg_head->nframes = 0;
1203 if ((msg_head->flags & RX_RTR_FRAME) &&
1204 ((msg_head->nframes != 1) ||
1205 (!(msg_head->can_id & CAN_RTR_FLAG))))
1208 /* check the given can_id */
1209 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1211 /* update existing BCM operation */
1214 * Do we need more space for the can_frames than currently
1215 * allocated? -> This is a _really_ unusual use-case and
1216 * therefore (complexity / locking) it is not supported.
1218 if (msg_head->nframes > op->nframes)
1221 if (msg_head->nframes) {
1222 /* update can_frames content */
1223 err = memcpy_fromiovec((u8 *)op->frames,
1225 msg_head->nframes * CFSIZ);
1229 /* clear last_frames to indicate 'nothing received' */
1230 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1233 op->nframes = msg_head->nframes;
1235 /* Only an update -> do not call can_rx_register() */
1239 /* insert new BCM operation for the given can_id */
1240 op = kzalloc(OPSIZ, GFP_KERNEL);
1244 op->can_id = msg_head->can_id;
1245 op->nframes = msg_head->nframes;
1247 if (msg_head->nframes > 1) {
1248 /* create array for can_frames and copy the data */
1249 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1256 /* create and init array for received can_frames */
1257 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1259 if (!op->last_frames) {
1266 op->frames = &op->sframe;
1267 op->last_frames = &op->last_sframe;
1270 if (msg_head->nframes) {
1271 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1272 msg_head->nframes * CFSIZ);
1274 if (op->frames != &op->sframe)
1276 if (op->last_frames != &op->last_sframe)
1277 kfree(op->last_frames);
1283 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1285 op->ifindex = ifindex;
1287 /* initialize uninitialized (kzalloc) structure */
1288 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1289 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1290 op->timer.function = bcm_rx_timeout_handler;
1292 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1293 op->thrtimer.function = bcm_rx_thr_handler;
1295 setup_timer(&op->timer, bcm_rx_timeout_handler,
1298 /* init throttle timer for RX_CHANGED */
1299 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1302 /* mark disabled timer */
1303 op->thrtimer.expires = 0;
1306 /* add this bcm_op to the list of the rx_ops */
1307 list_add(&op->list, &bo->rx_ops);
1309 /* call can_rx_register() */
1312 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1315 op->flags = msg_head->flags;
1317 if (op->flags & RX_RTR_FRAME) {
1319 /* no timers in RTR-mode */
1320 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1321 hrtimer_cancel(&op->thrtimer);
1322 hrtimer_cancel(&op->timer);
1324 del_timer(&op->thrtimer);
1325 del_timer(&op->timer);
1329 * funny feature in RX(!)_SETUP only for RTR-mode:
1330 * copy can_id into frame BUT without RTR-flag to
1331 * prevent a full-load-loopback-test ... ;-]
1333 if ((op->flags & TX_CP_CAN_ID) ||
1334 (op->frames[0].can_id == op->can_id))
1335 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1338 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1339 if (op->flags & SETTIMER) {
1341 /* set timer value */
1342 op->ival1 = msg_head->ival1;
1343 op->ival2 = msg_head->ival2;
1344 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1345 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1347 /* disable an active timer due to zero value? */
1348 if (!op->kt_ival1.tv64)
1349 hrtimer_cancel(&op->timer);
1352 * In any case cancel the throttle timer, flush
1353 * potentially blocked msgs and reset throttle handling
1355 op->kt_lastmsg = ktime_set(0, 0);
1356 hrtimer_cancel(&op->thrtimer);
1357 bcm_rx_thr_flush(op);
1360 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1361 hrtimer_start(&op->timer, op->kt_ival1,
1364 if (op->flags & SETTIMER) {
1366 /* set timer value */
1367 op->ival1 = msg_head->ival1;
1368 op->ival2 = msg_head->ival2;
1369 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1370 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1372 /* disable an active timer due to zero value? */
1374 del_timer(&op->timer);
1377 * In any case cancel the throttle timer, flush
1378 * potentially blocked msgs and reset throttle handling
1380 del_timer(&op->thrtimer);
1381 bcm_rx_thr_flush(op);
1382 op->thrtimer.expires = 0;
1385 if ((op->flags & STARTTIMER) && op->j_ival1)
1386 mod_timer(&op->timer, jiffies + op->j_ival1);
1390 /* now we can register for can_ids, if we added a new bcm_op */
1391 if (do_rx_register) {
1393 struct net_device *dev;
1395 dev = dev_get_by_index(&init_net, ifindex);
1397 err = can_rx_register(dev, op->can_id,
1398 REGMASK(op->can_id),
1402 op->rx_reg_dev = dev;
1407 err = can_rx_register(NULL, op->can_id,
1408 REGMASK(op->can_id),
1409 bcm_rx_handler, op, "bcm");
1411 /* this bcm rx op is broken -> remove it */
1412 list_del(&op->list);
1418 return msg_head->nframes * CFSIZ + MHSIZ;
1422 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1424 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1426 struct sk_buff *skb;
1427 struct net_device *dev;
1430 /* we need a real device to send frames */
1434 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1439 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1445 dev = dev_get_by_index(&init_net, ifindex);
1453 err = can_send(skb, 1); /* send with loopback */
1459 return CFSIZ + MHSIZ;
1463 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1465 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1466 struct msghdr *msg, size_t size)
1468 struct sock *sk = sock->sk;
1469 struct bcm_sock *bo = bcm_sk(sk);
1470 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1471 struct bcm_msg_head msg_head;
1472 int ret; /* read bytes or error codes as return value */
1477 /* check for valid message length from userspace */
1478 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1481 /* check for alternative ifindex for this bcm_op */
1483 if (!ifindex && msg->msg_name) {
1484 /* no bound device as default => check msg_name */
1485 struct sockaddr_can *addr =
1486 (struct sockaddr_can *)msg->msg_name;
1488 if (addr->can_family != AF_CAN)
1491 /* ifindex from sendto() */
1492 ifindex = addr->can_ifindex;
1495 struct net_device *dev;
1497 dev = dev_get_by_index(&init_net, ifindex);
1501 if (dev->type != ARPHRD_CAN) {
1510 /* read message head information */
1512 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1518 switch (msg_head.opcode) {
1521 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1525 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1529 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1536 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1543 /* reuse msg_head for the reply to TX_READ */
1544 msg_head.opcode = TX_STATUS;
1545 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1549 /* reuse msg_head for the reply to RX_READ */
1550 msg_head.opcode = RX_STATUS;
1551 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1555 /* we need exactly one can_frame behind the msg head */
1556 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1559 ret = bcm_tx_send(msg, ifindex, sk);
1573 * notification handler for netdevice status changes
1575 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1578 struct net_device *dev = (struct net_device *)data;
1579 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1580 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1581 struct sock *sk = &bo->sk;
1583 struct sock *sk = bo->sk;
1586 int notify_enodev = 0;
1588 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1589 if (!net_eq(dev_net(dev), &init_net))
1591 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1592 if (dev->nd_net != &init_net)
1596 if (dev->type != ARPHRD_CAN)
1601 case NETDEV_UNREGISTER:
1604 /* remove device specific receive entries */
1605 list_for_each_entry(op, &bo->rx_ops, list)
1606 if (op->rx_reg_dev == dev)
1607 bcm_rx_unreg(dev, op);
1609 /* remove device reference, if this is our bound device */
1610 if (bo->bound && bo->ifindex == dev->ifindex) {
1618 if (notify_enodev) {
1619 sk->sk_err = ENODEV;
1620 if (!sock_flag(sk, SOCK_DEAD))
1621 sk->sk_error_report(sk);
1626 if (bo->bound && bo->ifindex == dev->ifindex) {
1627 sk->sk_err = ENETDOWN;
1628 if (!sock_flag(sk, SOCK_DEAD))
1629 sk->sk_error_report(sk);
1637 * initial settings for all BCM sockets to be set at socket creation time
1639 static int bcm_init(struct sock *sk)
1641 struct bcm_sock *bo = bcm_sk(sk);
1643 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1648 bo->dropped_usr_msgs = 0;
1649 bo->bcm_proc_read = NULL;
1651 INIT_LIST_HEAD(&bo->tx_ops);
1652 INIT_LIST_HEAD(&bo->rx_ops);
1655 bo->notifier.notifier_call = bcm_notifier;
1657 register_netdevice_notifier(&bo->notifier);
1663 * standard socket functions
1665 static int bcm_release(struct socket *sock)
1667 struct sock *sk = sock->sk;
1668 struct bcm_sock *bo = bcm_sk(sk);
1669 struct bcm_op *op, *next;
1671 /* remove bcm_ops, timer, rx_unregister(), etc. */
1673 unregister_netdevice_notifier(&bo->notifier);
1677 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1680 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1682 * Don't care if we're bound or not (due to netdev problems)
1683 * can_rx_unregister() is always a save thing to do here.
1687 * Only remove subscriptions that had not
1688 * been removed due to NETDEV_UNREGISTER
1691 if (op->rx_reg_dev) {
1692 struct net_device *dev;
1694 dev = dev_get_by_index(&init_net, op->ifindex);
1696 bcm_rx_unreg(dev, op);
1701 can_rx_unregister(NULL, op->can_id,
1702 REGMASK(op->can_id),
1703 bcm_rx_handler, op);
1708 /* remove procfs entry */
1709 if (proc_dir && bo->bcm_proc_read)
1710 remove_proc_entry(bo->procname, proc_dir);
1712 /* remove device reference */
1724 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1727 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1728 struct sock *sk = sock->sk;
1729 struct bcm_sock *bo = bcm_sk(sk);
1734 /* bind a device to this socket */
1735 if (addr->can_ifindex) {
1736 struct net_device *dev;
1738 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1742 if (dev->type != ARPHRD_CAN) {
1747 bo->ifindex = dev->ifindex;
1751 /* no interface reference for ifindex = 0 ('any' CAN device) */
1758 /* unique socket address as filename */
1759 sprintf(bo->procname, "%p", sock);
1760 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1768 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1769 struct msghdr *msg, size_t size, int flags)
1771 struct sock *sk = sock->sk;
1772 struct sk_buff *skb;
1777 noblock = flags & MSG_DONTWAIT;
1778 flags &= ~MSG_DONTWAIT;
1779 skb = skb_recv_datagram(sk, flags, noblock, &error);
1783 if (skb->len < size)
1786 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1788 skb_free_datagram(sk, skb);
1792 sock_recv_timestamp(msg, sk, skb);
1794 if (msg->msg_name) {
1795 msg->msg_namelen = sizeof(struct sockaddr_can);
1796 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1799 skb_free_datagram(sk, skb);
1804 static struct proto_ops bcm_ops __read_mostly = {
1806 .release = bcm_release,
1807 .bind = sock_no_bind,
1808 .connect = bcm_connect,
1809 .socketpair = sock_no_socketpair,
1810 .accept = sock_no_accept,
1811 .getname = sock_no_getname,
1812 .poll = datagram_poll,
1813 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1814 .listen = sock_no_listen,
1815 .shutdown = sock_no_shutdown,
1816 .setsockopt = sock_no_setsockopt,
1817 .getsockopt = sock_no_getsockopt,
1818 .sendmsg = bcm_sendmsg,
1819 .recvmsg = bcm_recvmsg,
1820 .mmap = sock_no_mmap,
1821 .sendpage = sock_no_sendpage,
1824 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1825 static struct proto bcm_proto __read_mostly = {
1827 .owner = THIS_MODULE,
1828 .obj_size = sizeof(struct bcm_sock),
1832 static struct can_proto bcm_can_proto __read_mostly = {
1834 .protocol = CAN_BCM,
1840 static struct can_proto bcm_can_proto __read_mostly = {
1842 .protocol = CAN_BCM,
1845 .owner = THIS_MODULE,
1846 .obj_size = sizeof(struct bcm_sock),
1851 static int __init bcm_module_init(void)
1857 err = can_proto_register(&bcm_can_proto);
1859 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1863 /* create /proc/net/can-bcm directory */
1864 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1865 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1867 proc_dir = proc_mkdir("can-bcm", proc_net);
1871 proc_dir->owner = THIS_MODULE;
1876 static void __exit bcm_module_exit(void)
1878 can_proto_unregister(&bcm_can_proto);
1881 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1882 proc_net_remove(&init_net, "can-bcm");
1884 proc_net_remove("can-bcm");
1888 module_init(bcm_module_init);
1889 module_exit(bcm_module_exit);