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,
242 ktime_to_us(op->kt_ival1));
244 if (op->kt_ival2.tv64)
245 len += snprintf(page + len, PAGE_SIZE - len,
247 ktime_to_us(op->kt_ival2));
250 len += snprintf(page + len, PAGE_SIZE - len,
251 "timeo=%ld ", op->j_ival1);
254 len += snprintf(page + len, PAGE_SIZE - len,
255 "thr=%ld ", op->j_ival2);
258 len += snprintf(page + len, PAGE_SIZE - len,
259 "# recv %ld (%ld) => reduction: ",
260 op->frames_filtered, op->frames_abs);
262 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
264 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
265 (reduction == 100)?"near ":"", reduction);
267 if (len > PAGE_SIZE - 200) {
268 /* mark output cut off */
269 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
274 list_for_each_entry(op, &bo->tx_ops, list) {
276 len += snprintf(page + len, PAGE_SIZE - len,
277 "tx_op: %03X %s [%d] ",
278 op->can_id, bcm_proc_getifname(op->ifindex),
281 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
282 if (op->kt_ival1.tv64)
283 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
284 ktime_to_us(op->kt_ival1));
286 if (op->kt_ival2.tv64)
287 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
288 ktime_to_us(op->kt_ival2));
291 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
295 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
299 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
302 if (len > PAGE_SIZE - 100) {
303 /* mark output cut off */
304 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
309 len += snprintf(page + len, PAGE_SIZE - len, "\n");
316 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
317 * of the given bcm tx op
319 static void bcm_can_tx(struct bcm_op *op)
322 struct net_device *dev;
323 struct can_frame *cf = &op->frames[op->currframe];
325 /* no target device? => exit */
329 dev = dev_get_by_index(&init_net, op->ifindex);
331 /* RFC: should this bcm_op remove itself here? */
335 skb = alloc_skb(CFSIZ, gfp_any());
339 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
341 /* send with loopback */
346 /* update statistics */
350 /* reached last frame? */
351 if (op->currframe >= op->nframes)
358 * bcm_send_to_user - send a BCM message to the userspace
359 * (consisting of bcm_msg_head + x CAN frames)
361 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
362 struct can_frame *frames, int has_timestamp)
365 struct can_frame *firstframe;
366 struct sockaddr_can *addr;
367 struct sock *sk = op->sk;
368 int datalen = head->nframes * CFSIZ;
371 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
375 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
378 /* can_frames starting here */
379 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
380 firstframe = (struct can_frame *) skb_tail_pointer(skb);
382 firstframe = (struct can_frame *) skb->tail;
385 memcpy(skb_put(skb, datalen), frames, datalen);
388 * the BCM uses the can_dlc-element of the can_frame
389 * structure for internal purposes. This is only
390 * relevant for updates that are generated by the
391 * BCM, where nframes is 1
393 if (head->nframes == 1)
394 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
398 /* restore rx timestamp */
399 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
400 skb->tstamp = op->rx_stamp;
402 skb_set_timestamp(skb, &op->rx_stamp);
407 * Put the datagram to the queue so that bcm_recvmsg() can
408 * get it from there. We need to pass the interface index to
409 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
410 * containing the interface index.
413 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
414 addr = (struct sockaddr_can *)skb->cb;
415 memset(addr, 0, sizeof(*addr));
416 addr->can_family = AF_CAN;
417 addr->can_ifindex = op->rx_ifindex;
419 err = sock_queue_rcv_skb(sk, skb);
421 struct bcm_sock *bo = bcm_sk(sk);
424 /* don't care about overflows in this statistic */
425 bo->dropped_usr_msgs++;
430 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
432 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
433 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
435 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
436 enum hrtimer_restart ret = HRTIMER_NORESTART;
438 if (op->kt_ival1.tv64 && (op->count > 0)) {
440 static void bcm_tx_timeout_handler(unsigned long data)
442 struct bcm_op *op = (struct bcm_op *)data;
444 if (op->j_ival1 && (op->count > 0)) {
448 if (!op->count && (op->flags & TX_COUNTEVT)) {
449 struct bcm_msg_head msg_head;
451 /* create notification to user */
452 msg_head.opcode = TX_EXPIRED;
453 msg_head.flags = op->flags;
454 msg_head.count = op->count;
455 msg_head.ival1 = op->ival1;
456 msg_head.ival2 = op->ival2;
457 msg_head.can_id = op->can_id;
458 msg_head.nframes = 0;
460 bcm_send_to_user(op, &msg_head, NULL, 0);
464 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
465 if (op->kt_ival1.tv64 && (op->count > 0)) {
467 /* send (next) frame */
469 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
470 ret = HRTIMER_RESTART;
473 if (op->kt_ival2.tv64) {
475 /* send (next) frame */
477 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
478 ret = HRTIMER_RESTART;
484 if (op->j_ival1 && (op->count > 0)) {
486 /* send (next) frame */
488 mod_timer(&op->timer, jiffies + op->j_ival1);
493 /* send (next) frame */
495 mod_timer(&op->timer, jiffies + op->j_ival2);
504 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
506 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
508 struct bcm_msg_head head;
510 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
511 op->j_lastmsg = jiffies;
514 /* update statistics */
515 op->frames_filtered++;
517 /* prevent statistics overflow */
518 if (op->frames_filtered > ULONG_MAX/100)
519 op->frames_filtered = op->frames_abs = 0;
521 head.opcode = RX_CHANGED;
522 head.flags = op->flags;
523 head.count = op->count;
524 head.ival1 = op->ival1;
525 head.ival2 = op->ival2;
526 head.can_id = op->can_id;
529 bcm_send_to_user(op, &head, data, 1);
532 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
533 /* TODO: move to linux/hrtimer.h */
534 static inline int hrtimer_callback_running(struct hrtimer *timer)
536 return timer->state & HRTIMER_STATE_CALLBACK;
541 * bcm_rx_update_and_send - process a detected relevant receive content change
542 * 1. update the last received data
543 * 2. send a notification to the user (if possible)
545 static void bcm_rx_update_and_send(struct bcm_op *op,
546 struct can_frame *lastdata,
547 struct can_frame *rxdata)
549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
550 memcpy(lastdata, rxdata, CFSIZ);
553 lastdata->can_dlc |= RX_RECV;
555 /* throtteling mode inactive OR data update already on the run ? */
556 if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
557 /* send RX_CHANGED to the user immediately */
558 bcm_rx_changed(op, rxdata);
562 if (hrtimer_active(&op->thrtimer)) {
563 /* mark as 'throttled' */
564 lastdata->can_dlc |= RX_THR;
568 if (!op->kt_lastmsg.tv64) {
569 /* send first RX_CHANGED to the user immediately */
570 bcm_rx_changed(op, rxdata);
571 op->kt_lastmsg = ktime_get();
575 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
576 ktime_to_us(op->kt_ival2)) {
577 /* mark as 'throttled' and start timer */
578 lastdata->can_dlc |= RX_THR;
579 hrtimer_start(&op->thrtimer,
580 ktime_add(op->kt_lastmsg, op->kt_ival2),
585 /* the gap was that big, that throttling was not needed here */
586 bcm_rx_changed(op, rxdata);
587 op->kt_lastmsg = ktime_get();
589 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
591 memcpy(lastdata, rxdata, CFSIZ);
594 lastdata->can_dlc |= RX_RECV;
596 /* throttle bcm_rx_changed ? */
597 if ((op->thrtimer.expires) ||
598 ((op->j_ival2) && (nexttx > jiffies))) {
599 /* we are already waiting OR we have to start waiting */
601 /* mark as 'throttled' */
602 lastdata->can_dlc |= RX_THR;
604 if (!(op->thrtimer.expires)) {
605 /* start the timer only the first time */
606 mod_timer(&op->thrtimer, nexttx);
610 /* send RX_CHANGED to the user immediately */
611 bcm_rx_changed(op, rxdata);
617 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
618 * received data stored in op->last_frames[]
620 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
621 struct can_frame *rxdata)
624 * no one uses the MSBs of can_dlc for comparation,
625 * so we use it here to detect the first time of reception
628 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
629 /* received data for the first time => send update to user */
630 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
634 /* do a real check in can_frame data section */
636 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
637 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
638 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
642 if (op->flags & RX_CHECK_DLC) {
643 /* do a real check in can_frame dlc */
644 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
646 bcm_rx_update_and_send(op, &op->last_frames[index],
654 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
656 static void bcm_rx_starttimer(struct bcm_op *op)
658 if (op->flags & RX_NO_AUTOTIMER)
661 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
662 if (op->kt_ival1.tv64)
663 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
666 mod_timer(&op->timer, jiffies + op->j_ival1);
671 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
673 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
674 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
676 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
678 static void bcm_rx_timeout_handler(unsigned long data)
680 struct bcm_op *op = (struct bcm_op *)data;
682 struct bcm_msg_head msg_head;
684 msg_head.opcode = RX_TIMEOUT;
685 msg_head.flags = op->flags;
686 msg_head.count = op->count;
687 msg_head.ival1 = op->ival1;
688 msg_head.ival2 = op->ival2;
689 msg_head.can_id = op->can_id;
690 msg_head.nframes = 0;
692 bcm_send_to_user(op, &msg_head, NULL, 0);
694 /* no restart of the timer is done here! */
696 /* if user wants to be informed, when cyclic CAN-Messages come back */
697 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
698 /* clear received can_frames to indicate 'nothing received' */
699 memset(op->last_frames, 0, op->nframes * CFSIZ);
701 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
703 return HRTIMER_NORESTART;
708 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
710 static int bcm_rx_thr_flush(struct bcm_op *op)
714 if (op->nframes > 1) {
717 /* for MUX filter we start at index 1 */
718 for (i = 1; i < op->nframes; i++) {
719 if ((op->last_frames) &&
720 (op->last_frames[i].can_dlc & RX_THR)) {
721 op->last_frames[i].can_dlc &= ~RX_THR;
722 bcm_rx_changed(op, &op->last_frames[i]);
728 /* for RX_FILTER_ID and simple filter */
729 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
730 op->last_frames[0].can_dlc &= ~RX_THR;
731 bcm_rx_changed(op, &op->last_frames[0]);
740 * bcm_rx_thr_handler - the time for blocked content updates is over now:
741 * Check for throttled data and send it to the userspace
743 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
744 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
746 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
748 if (bcm_rx_thr_flush(op)) {
749 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
750 return HRTIMER_RESTART;
752 /* rearm throttle handling */
753 op->kt_lastmsg = ktime_set(0, 0);
754 return HRTIMER_NORESTART;
758 static void bcm_rx_thr_handler(unsigned long data)
760 struct bcm_op *op = (struct bcm_op *)data;
762 if (bcm_rx_thr_flush(op))
763 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
765 /* mark disabled / consumed timer */
766 op->thrtimer.expires = 0;
772 * bcm_rx_handler - handle a CAN frame receiption
774 static void bcm_rx_handler(struct sk_buff *skb, void *data)
776 struct bcm_op *op = (struct bcm_op *)data;
777 struct can_frame rxframe;
780 /* disable timeout */
781 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
782 hrtimer_cancel(&op->timer);
784 del_timer(&op->timer);
787 if (skb->len == sizeof(rxframe)) {
788 memcpy(&rxframe, skb->data, sizeof(rxframe));
789 /* save rx timestamp */
790 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
791 op->rx_stamp = skb->tstamp;
793 skb_get_timestamp(skb, &op->rx_stamp);
795 /* save originator for recvfrom() */
796 op->rx_ifindex = skb->dev->ifindex;
797 /* update statistics */
806 if (op->can_id != rxframe.can_id)
809 if (op->flags & RX_RTR_FRAME) {
810 /* send reply for RTR-request (placed in op->frames[0]) */
815 if (op->flags & RX_FILTER_ID) {
816 /* the easiest case */
817 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
818 bcm_rx_starttimer(op);
822 if (op->nframes == 1) {
823 /* simple compare with index 0 */
824 bcm_rx_cmp_to_index(op, 0, &rxframe);
825 bcm_rx_starttimer(op);
829 if (op->nframes > 1) {
833 * find the first multiplex mask that fits.
834 * Remark: The MUX-mask is stored in index 0
837 for (i = 1; i < op->nframes; i++) {
838 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
839 (GET_U64(&op->frames[0]) &
840 GET_U64(&op->frames[i]))) {
841 bcm_rx_cmp_to_index(op, i, &rxframe);
845 bcm_rx_starttimer(op);
850 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
852 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
857 list_for_each_entry(op, ops, list) {
858 if ((op->can_id == can_id) && (op->ifindex == ifindex))
865 static void bcm_remove_op(struct bcm_op *op)
867 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
868 hrtimer_cancel(&op->timer);
869 hrtimer_cancel(&op->thrtimer);
871 del_timer(&op->timer);
872 del_timer(&op->thrtimer);
875 if ((op->frames) && (op->frames != &op->sframe))
878 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
879 kfree(op->last_frames);
886 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
888 if (op->rx_reg_dev == dev) {
889 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
892 /* mark as removed subscription */
893 op->rx_reg_dev = NULL;
895 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
896 "mismatch %p %p\n", op->rx_reg_dev, dev);
900 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
902 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
904 struct bcm_op *op, *n;
906 list_for_each_entry_safe(op, n, ops, list) {
907 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
910 * Don't care if we're bound or not (due to netdev
911 * problems) can_rx_unregister() is always a save
916 * Only remove subscriptions that had not
917 * been removed due to NETDEV_UNREGISTER
920 if (op->rx_reg_dev) {
921 struct net_device *dev;
923 dev = dev_get_by_index(&init_net,
926 bcm_rx_unreg(dev, op);
931 can_rx_unregister(NULL, op->can_id,
941 return 0; /* not found */
945 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
947 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
949 struct bcm_op *op, *n;
951 list_for_each_entry_safe(op, n, ops, list) {
952 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
959 return 0; /* not found */
963 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
965 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
968 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
973 /* put current values into msg_head */
974 msg_head->flags = op->flags;
975 msg_head->count = op->count;
976 msg_head->ival1 = op->ival1;
977 msg_head->ival2 = op->ival2;
978 msg_head->nframes = op->nframes;
980 bcm_send_to_user(op, msg_head, op->frames, 0);
986 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
988 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
989 int ifindex, struct sock *sk)
991 struct bcm_sock *bo = bcm_sk(sk);
995 /* we need a real device to send frames */
999 /* we need at least one can_frame */
1000 if (msg_head->nframes < 1)
1003 /* check the given can_id */
1004 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
1007 /* update existing BCM operation */
1010 * Do we need more space for the can_frames than currently
1011 * allocated? -> This is a _really_ unusual use-case and
1012 * therefore (complexity / locking) it is not supported.
1014 if (msg_head->nframes > op->nframes)
1017 /* update can_frames content */
1018 for (i = 0; i < msg_head->nframes; i++) {
1019 err = memcpy_fromiovec((u8 *)&op->frames[i],
1020 msg->msg_iov, CFSIZ);
1024 if (msg_head->flags & TX_CP_CAN_ID) {
1025 /* copy can_id into frame */
1026 op->frames[i].can_id = msg_head->can_id;
1031 /* insert new BCM operation for the given can_id */
1033 op = kzalloc(OPSIZ, GFP_KERNEL);
1037 op->can_id = msg_head->can_id;
1039 /* create array for can_frames and copy the data */
1040 if (msg_head->nframes > 1) {
1041 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1048 op->frames = &op->sframe;
1050 for (i = 0; i < msg_head->nframes; i++) {
1051 err = memcpy_fromiovec((u8 *)&op->frames[i],
1052 msg->msg_iov, CFSIZ);
1054 if (op->frames != &op->sframe)
1060 if (msg_head->flags & TX_CP_CAN_ID) {
1061 /* copy can_id into frame */
1062 op->frames[i].can_id = msg_head->can_id;
1066 /* tx_ops never compare with previous received messages */
1067 op->last_frames = NULL;
1069 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1071 op->ifindex = ifindex;
1073 /* initialize uninitialized (kzalloc) structure */
1074 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1075 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1076 op->timer.function = bcm_tx_timeout_handler;
1078 /* currently unused in tx_ops */
1079 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1081 setup_timer(&op->timer, bcm_tx_timeout_handler,
1084 /* currently unused in tx_ops */
1085 init_timer(&op->thrtimer);
1088 /* add this bcm_op to the list of the tx_ops */
1089 list_add(&op->list, &bo->tx_ops);
1091 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1093 if (op->nframes != msg_head->nframes) {
1094 op->nframes = msg_head->nframes;
1095 /* start multiple frame transmission with index 0 */
1101 op->flags = msg_head->flags;
1103 if (op->flags & TX_RESET_MULTI_IDX) {
1104 /* start multiple frame transmission with index 0 */
1108 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1109 if (op->flags & SETTIMER) {
1110 /* set timer values */
1111 op->count = msg_head->count;
1112 op->ival1 = msg_head->ival1;
1113 op->ival2 = msg_head->ival2;
1114 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1115 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1117 /* disable an active timer due to zero values? */
1118 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1119 hrtimer_cancel(&op->timer);
1122 if ((op->flags & STARTTIMER) &&
1123 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1125 /* spec: send can_frame when starting timer */
1126 op->flags |= TX_ANNOUNCE;
1128 if (op->kt_ival1.tv64 && (op->count > 0)) {
1129 /* op->count-- is done in bcm_tx_timeout_handler */
1130 hrtimer_start(&op->timer, op->kt_ival1,
1133 hrtimer_start(&op->timer, op->kt_ival2,
1137 if (op->flags & SETTIMER) {
1138 /* set timer values */
1139 op->count = msg_head->count;
1140 op->ival1 = msg_head->ival1;
1141 op->ival2 = msg_head->ival2;
1142 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1143 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1145 /* disable an active timer due to zero values? */
1146 if (!op->j_ival1 && !op->j_ival2)
1147 del_timer(&op->timer);
1150 if ((op->flags & STARTTIMER) &&
1151 ((op->j_ival1 && op->count) || op->j_ival2)) {
1153 /* spec: send can_frame when starting timer */
1154 op->flags |= TX_ANNOUNCE;
1156 if (op->j_ival1 && (op->count > 0)) {
1157 /* op->count-- is done in bcm_tx_timeout_handler */
1158 mod_timer(&op->timer, jiffies + op->j_ival1);
1160 mod_timer(&op->timer, jiffies + op->j_ival2);
1164 if (op->flags & TX_ANNOUNCE)
1167 return msg_head->nframes * CFSIZ + MHSIZ;
1171 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1173 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1174 int ifindex, struct sock *sk)
1176 struct bcm_sock *bo = bcm_sk(sk);
1181 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1182 /* be robust against wrong usage ... */
1183 msg_head->flags |= RX_FILTER_ID;
1184 /* ignore trailing garbage */
1185 msg_head->nframes = 0;
1188 if ((msg_head->flags & RX_RTR_FRAME) &&
1189 ((msg_head->nframes != 1) ||
1190 (!(msg_head->can_id & CAN_RTR_FLAG))))
1193 /* check the given can_id */
1194 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1196 /* update existing BCM operation */
1199 * Do we need more space for the can_frames than currently
1200 * allocated? -> This is a _really_ unusual use-case and
1201 * therefore (complexity / locking) it is not supported.
1203 if (msg_head->nframes > op->nframes)
1206 if (msg_head->nframes) {
1207 /* update can_frames content */
1208 err = memcpy_fromiovec((u8 *)op->frames,
1210 msg_head->nframes * CFSIZ);
1214 /* clear last_frames to indicate 'nothing received' */
1215 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1218 op->nframes = msg_head->nframes;
1220 /* Only an update -> do not call can_rx_register() */
1224 /* insert new BCM operation for the given can_id */
1225 op = kzalloc(OPSIZ, GFP_KERNEL);
1229 op->can_id = msg_head->can_id;
1230 op->nframes = msg_head->nframes;
1232 if (msg_head->nframes > 1) {
1233 /* create array for can_frames and copy the data */
1234 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1241 /* create and init array for received can_frames */
1242 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1244 if (!op->last_frames) {
1251 op->frames = &op->sframe;
1252 op->last_frames = &op->last_sframe;
1255 if (msg_head->nframes) {
1256 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1257 msg_head->nframes * CFSIZ);
1259 if (op->frames != &op->sframe)
1261 if (op->last_frames != &op->last_sframe)
1262 kfree(op->last_frames);
1268 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1270 op->ifindex = ifindex;
1272 /* initialize uninitialized (kzalloc) structure */
1273 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1274 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1275 op->timer.function = bcm_rx_timeout_handler;
1277 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1278 op->thrtimer.function = bcm_rx_thr_handler;
1280 setup_timer(&op->timer, bcm_rx_timeout_handler,
1283 /* init throttle timer for RX_CHANGED */
1284 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1287 /* mark disabled timer */
1288 op->thrtimer.expires = 0;
1291 /* add this bcm_op to the list of the rx_ops */
1292 list_add(&op->list, &bo->rx_ops);
1294 /* call can_rx_register() */
1297 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1300 op->flags = msg_head->flags;
1302 if (op->flags & RX_RTR_FRAME) {
1304 /* no timers in RTR-mode */
1305 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1306 hrtimer_cancel(&op->thrtimer);
1307 hrtimer_cancel(&op->timer);
1309 del_timer(&op->thrtimer);
1310 del_timer(&op->timer);
1314 * funny feature in RX(!)_SETUP only for RTR-mode:
1315 * copy can_id into frame BUT without RTR-flag to
1316 * prevent a full-load-loopback-test ... ;-]
1318 if ((op->flags & TX_CP_CAN_ID) ||
1319 (op->frames[0].can_id == op->can_id))
1320 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1323 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1324 if (op->flags & SETTIMER) {
1326 /* set timer value */
1327 op->ival1 = msg_head->ival1;
1328 op->ival2 = msg_head->ival2;
1329 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1330 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1332 /* disable an active timer due to zero value? */
1333 if (!op->kt_ival1.tv64)
1334 hrtimer_cancel(&op->timer);
1337 * In any case cancel the throttle timer, flush
1338 * potentially blocked msgs and reset throttle handling
1340 op->kt_lastmsg = ktime_set(0, 0);
1341 hrtimer_cancel(&op->thrtimer);
1342 bcm_rx_thr_flush(op);
1345 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1346 hrtimer_start(&op->timer, op->kt_ival1,
1349 if (op->flags & SETTIMER) {
1351 /* set timer value */
1352 op->ival1 = msg_head->ival1;
1353 op->ival2 = msg_head->ival2;
1354 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1355 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1357 /* disable an active timer due to zero value? */
1359 del_timer(&op->timer);
1362 * In any case cancel the throttle timer, flush
1363 * potentially blocked msgs and reset throttle handling
1365 del_timer(&op->thrtimer);
1366 bcm_rx_thr_flush(op);
1367 op->thrtimer.expires = 0;
1370 if ((op->flags & STARTTIMER) && op->j_ival1)
1371 mod_timer(&op->timer, jiffies + op->j_ival1);
1375 /* now we can register for can_ids, if we added a new bcm_op */
1376 if (do_rx_register) {
1378 struct net_device *dev;
1380 dev = dev_get_by_index(&init_net, ifindex);
1382 err = can_rx_register(dev, op->can_id,
1383 REGMASK(op->can_id),
1387 op->rx_reg_dev = dev;
1392 err = can_rx_register(NULL, op->can_id,
1393 REGMASK(op->can_id),
1394 bcm_rx_handler, op, "bcm");
1396 /* this bcm rx op is broken -> remove it */
1397 list_del(&op->list);
1403 return msg_head->nframes * CFSIZ + MHSIZ;
1407 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1409 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1411 struct sk_buff *skb;
1412 struct net_device *dev;
1415 /* we need a real device to send frames */
1419 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1424 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1430 dev = dev_get_by_index(&init_net, ifindex);
1438 can_send(skb, 1); /* send with loopback */
1441 return CFSIZ + MHSIZ;
1445 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1447 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1448 struct msghdr *msg, size_t size)
1450 struct sock *sk = sock->sk;
1451 struct bcm_sock *bo = bcm_sk(sk);
1452 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1453 struct bcm_msg_head msg_head;
1454 int ret; /* read bytes or error codes as return value */
1459 /* check for alternative ifindex for this bcm_op */
1461 if (!ifindex && msg->msg_name) {
1462 /* no bound device as default => check msg_name */
1463 struct sockaddr_can *addr =
1464 (struct sockaddr_can *)msg->msg_name;
1466 if (addr->can_family != AF_CAN)
1469 /* ifindex from sendto() */
1470 ifindex = addr->can_ifindex;
1473 struct net_device *dev;
1475 dev = dev_get_by_index(&init_net, ifindex);
1479 if (dev->type != ARPHRD_CAN) {
1488 /* read message head information */
1490 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1496 switch (msg_head.opcode) {
1499 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1503 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1507 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1514 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1521 /* reuse msg_head for the reply to TX_READ */
1522 msg_head.opcode = TX_STATUS;
1523 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1527 /* reuse msg_head for the reply to RX_READ */
1528 msg_head.opcode = RX_STATUS;
1529 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1533 /* we need at least one can_frame */
1534 if (msg_head.nframes < 1)
1537 ret = bcm_tx_send(msg, ifindex, sk);
1551 * notification handler for netdevice status changes
1553 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1556 struct net_device *dev = (struct net_device *)data;
1557 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1558 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1559 struct sock *sk = &bo->sk;
1561 struct sock *sk = bo->sk;
1564 int notify_enodev = 0;
1566 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1567 if (dev->nd_net != &init_net)
1571 if (dev->type != ARPHRD_CAN)
1576 case NETDEV_UNREGISTER:
1579 /* remove device specific receive entries */
1580 list_for_each_entry(op, &bo->rx_ops, list)
1581 if (op->rx_reg_dev == dev)
1582 bcm_rx_unreg(dev, op);
1584 /* remove device reference, if this is our bound device */
1585 if (bo->bound && bo->ifindex == dev->ifindex) {
1593 if (notify_enodev) {
1594 sk->sk_err = ENODEV;
1595 if (!sock_flag(sk, SOCK_DEAD))
1596 sk->sk_error_report(sk);
1601 if (bo->bound && bo->ifindex == dev->ifindex) {
1602 sk->sk_err = ENETDOWN;
1603 if (!sock_flag(sk, SOCK_DEAD))
1604 sk->sk_error_report(sk);
1612 * initial settings for all BCM sockets to be set at socket creation time
1614 static int bcm_init(struct sock *sk)
1616 struct bcm_sock *bo = bcm_sk(sk);
1618 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1623 bo->dropped_usr_msgs = 0;
1624 bo->bcm_proc_read = NULL;
1626 INIT_LIST_HEAD(&bo->tx_ops);
1627 INIT_LIST_HEAD(&bo->rx_ops);
1630 bo->notifier.notifier_call = bcm_notifier;
1632 register_netdevice_notifier(&bo->notifier);
1638 * standard socket functions
1640 static int bcm_release(struct socket *sock)
1642 struct sock *sk = sock->sk;
1643 struct bcm_sock *bo = bcm_sk(sk);
1644 struct bcm_op *op, *next;
1646 /* remove bcm_ops, timer, rx_unregister(), etc. */
1648 unregister_netdevice_notifier(&bo->notifier);
1652 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1655 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1657 * Don't care if we're bound or not (due to netdev problems)
1658 * can_rx_unregister() is always a save thing to do here.
1662 * Only remove subscriptions that had not
1663 * been removed due to NETDEV_UNREGISTER
1666 if (op->rx_reg_dev) {
1667 struct net_device *dev;
1669 dev = dev_get_by_index(&init_net, op->ifindex);
1671 bcm_rx_unreg(dev, op);
1676 can_rx_unregister(NULL, op->can_id,
1677 REGMASK(op->can_id),
1678 bcm_rx_handler, op);
1683 /* remove procfs entry */
1684 if (proc_dir && bo->bcm_proc_read)
1685 remove_proc_entry(bo->procname, proc_dir);
1687 /* remove device reference */
1699 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1702 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1703 struct sock *sk = sock->sk;
1704 struct bcm_sock *bo = bcm_sk(sk);
1709 /* bind a device to this socket */
1710 if (addr->can_ifindex) {
1711 struct net_device *dev;
1713 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1717 if (dev->type != ARPHRD_CAN) {
1722 bo->ifindex = dev->ifindex;
1726 /* no interface reference for ifindex = 0 ('any' CAN device) */
1733 /* unique socket address as filename */
1734 sprintf(bo->procname, "%p", sock);
1735 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1743 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1744 struct msghdr *msg, size_t size, int flags)
1746 struct sock *sk = sock->sk;
1747 struct sk_buff *skb;
1752 noblock = flags & MSG_DONTWAIT;
1753 flags &= ~MSG_DONTWAIT;
1754 skb = skb_recv_datagram(sk, flags, noblock, &error);
1758 if (skb->len < size)
1761 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1763 skb_free_datagram(sk, skb);
1767 sock_recv_timestamp(msg, sk, skb);
1769 if (msg->msg_name) {
1770 msg->msg_namelen = sizeof(struct sockaddr_can);
1771 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1774 skb_free_datagram(sk, skb);
1779 static struct proto_ops bcm_ops __read_mostly = {
1781 .release = bcm_release,
1782 .bind = sock_no_bind,
1783 .connect = bcm_connect,
1784 .socketpair = sock_no_socketpair,
1785 .accept = sock_no_accept,
1786 .getname = sock_no_getname,
1787 .poll = datagram_poll,
1788 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1789 .listen = sock_no_listen,
1790 .shutdown = sock_no_shutdown,
1791 .setsockopt = sock_no_setsockopt,
1792 .getsockopt = sock_no_getsockopt,
1793 .sendmsg = bcm_sendmsg,
1794 .recvmsg = bcm_recvmsg,
1795 .mmap = sock_no_mmap,
1796 .sendpage = sock_no_sendpage,
1799 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1800 static struct proto bcm_proto __read_mostly = {
1802 .owner = THIS_MODULE,
1803 .obj_size = sizeof(struct bcm_sock),
1807 static struct can_proto bcm_can_proto __read_mostly = {
1809 .protocol = CAN_BCM,
1815 static struct can_proto bcm_can_proto __read_mostly = {
1817 .protocol = CAN_BCM,
1820 .owner = THIS_MODULE,
1821 .obj_size = sizeof(struct bcm_sock),
1826 static int __init bcm_module_init(void)
1832 err = can_proto_register(&bcm_can_proto);
1834 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1838 /* create /proc/net/can-bcm directory */
1839 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1840 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1842 proc_dir = proc_mkdir("can-bcm", proc_net);
1846 proc_dir->owner = THIS_MODULE;
1851 static void __exit bcm_module_exit(void)
1853 can_proto_unregister(&bcm_can_proto);
1856 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1857 proc_net_remove(&init_net, "can-bcm");
1859 proc_net_remove("can-bcm");
1863 module_init(bcm_module_init);
1864 module_exit(bcm_module_exit);