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_EFF_FLAG) ? \
79 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
80 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
82 #define CAN_BCM_VERSION CAN_VERSION
83 static __initdata const char banner[] = KERN_INFO
84 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
86 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
87 MODULE_LICENSE("Dual BSD/GPL");
88 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
90 /* easy access to can_frame payload */
91 static inline u64 GET_U64(const struct can_frame *cp)
93 return *(u64 *)cp->data;
97 struct list_head list;
101 unsigned long frames_abs, frames_filtered;
102 struct timeval ival1, ival2;
103 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
104 struct hrtimer timer, thrtimer;
105 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
107 struct timer_list timer, thrtimer;
108 struct timeval rx_stamp;
109 unsigned long j_ival1, j_ival2, j_lastmsg;
115 struct can_frame *frames;
116 struct can_frame *last_frames;
117 struct can_frame sframe;
118 struct can_frame last_sframe;
120 struct net_device *rx_reg_dev;
123 static struct proc_dir_entry *proc_dir;
126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
133 struct notifier_block notifier;
134 struct list_head rx_ops;
135 struct list_head tx_ops;
136 unsigned long dropped_usr_msgs;
137 struct proc_dir_entry *bcm_proc_read;
138 char procname [9]; /* pointer printed in ASCII with \0 */
141 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
143 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
144 return (struct bcm_sock *)sk;
146 return (struct bcm_sock *)sk->sk_protinfo;
150 #define CFSIZ sizeof(struct can_frame)
151 #define OPSIZ sizeof(struct bcm_op)
152 #define MHSIZ sizeof(struct bcm_msg_head)
154 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
156 * rounded_tv2jif - calculate jiffies from timeval including optional up
157 * @tv: pointer to timeval
160 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
161 * function is intentionally more relaxed on precise timer ticks to get
162 * exact one jiffy for requested 1000us on a 1000HZ machine.
163 * This code is to be removed when upgrading to kernel hrtimer.
166 * calculated jiffies (max: ULONG_MAX)
168 static unsigned long rounded_tv2jif(const struct timeval *tv)
170 unsigned long sec = tv->tv_sec;
171 unsigned long usec = tv->tv_usec;
174 if (sec > ULONG_MAX / HZ)
177 /* round up to get at least the requested time */
178 usec += 1000000 / HZ - 1;
180 jif = usec / (1000000 / HZ);
182 if (sec * HZ > ULONG_MAX - jif)
185 return jif + sec * HZ;
192 static char *bcm_proc_getifname(int ifindex)
194 struct net_device *dev;
199 /* no usage counting */
200 dev = __dev_get_by_index(&init_net, ifindex);
207 static int bcm_read_proc(char *page, char **start, off_t off,
208 int count, int *eof, void *data)
211 struct sock *sk = (struct sock *)data;
212 struct bcm_sock *bo = bcm_sk(sk);
215 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
217 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
218 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
219 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
220 bo->dropped_usr_msgs);
221 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
222 bcm_proc_getifname(bo->ifindex));
223 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
225 list_for_each_entry(op, &bo->rx_ops, list) {
227 unsigned long reduction;
229 /* print only active entries & prevent division by zero */
233 len += snprintf(page + len, PAGE_SIZE - len,
235 op->can_id, bcm_proc_getifname(op->ifindex));
236 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
238 (op->flags & RX_CHECK_DLC)?'d':' ');
239 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
240 if (op->kt_ival1.tv64)
241 len += snprintf(page + len, PAGE_SIZE - len,
244 ktime_to_us(op->kt_ival1));
246 if (op->kt_ival2.tv64)
247 len += snprintf(page + len, PAGE_SIZE - len,
250 ktime_to_us(op->kt_ival2));
253 len += snprintf(page + len, PAGE_SIZE - len,
254 "timeo=%ld ", op->j_ival1);
257 len += snprintf(page + len, PAGE_SIZE - len,
258 "thr=%ld ", op->j_ival2);
261 len += snprintf(page + len, PAGE_SIZE - len,
262 "# recv %ld (%ld) => reduction: ",
263 op->frames_filtered, op->frames_abs);
265 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
267 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
268 (reduction == 100)?"near ":"", reduction);
270 if (len > PAGE_SIZE - 200) {
271 /* mark output cut off */
272 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
277 list_for_each_entry(op, &bo->tx_ops, list) {
279 len += snprintf(page + len, PAGE_SIZE - len,
280 "tx_op: %03X %s [%d] ",
281 op->can_id, bcm_proc_getifname(op->ifindex),
284 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
285 if (op->kt_ival1.tv64)
286 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
287 (long long) ktime_to_us(op->kt_ival1));
289 if (op->kt_ival2.tv64)
290 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
291 (long long) ktime_to_us(op->kt_ival2));
294 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
298 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
302 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
305 if (len > PAGE_SIZE - 100) {
306 /* mark output cut off */
307 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
312 len += snprintf(page + len, PAGE_SIZE - len, "\n");
319 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
320 * of the given bcm tx op
322 static void bcm_can_tx(struct bcm_op *op)
325 struct net_device *dev;
326 struct can_frame *cf = &op->frames[op->currframe];
328 /* no target device? => exit */
332 dev = dev_get_by_index(&init_net, op->ifindex);
334 /* RFC: should this bcm_op remove itself here? */
338 skb = alloc_skb(CFSIZ, gfp_any());
342 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
344 /* send with loopback */
349 /* update statistics */
353 /* reached last frame? */
354 if (op->currframe >= op->nframes)
361 * bcm_send_to_user - send a BCM message to the userspace
362 * (consisting of bcm_msg_head + x CAN frames)
364 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
365 struct can_frame *frames, int has_timestamp)
368 struct can_frame *firstframe;
369 struct sockaddr_can *addr;
370 struct sock *sk = op->sk;
371 int datalen = head->nframes * CFSIZ;
374 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
378 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
381 /* can_frames starting here */
382 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
383 firstframe = (struct can_frame *)skb_tail_pointer(skb);
385 firstframe = (struct can_frame *)skb->tail;
388 memcpy(skb_put(skb, datalen), frames, datalen);
391 * the BCM uses the can_dlc-element of the can_frame
392 * structure for internal purposes. This is only
393 * relevant for updates that are generated by the
394 * BCM, where nframes is 1
396 if (head->nframes == 1)
397 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
401 /* restore rx timestamp */
402 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
403 skb->tstamp = op->rx_stamp;
405 skb_set_timestamp(skb, &op->rx_stamp);
410 * Put the datagram to the queue so that bcm_recvmsg() can
411 * get it from there. We need to pass the interface index to
412 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
413 * containing the interface index.
416 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
417 addr = (struct sockaddr_can *)skb->cb;
418 memset(addr, 0, sizeof(*addr));
419 addr->can_family = AF_CAN;
420 addr->can_ifindex = op->rx_ifindex;
422 err = sock_queue_rcv_skb(sk, skb);
424 struct bcm_sock *bo = bcm_sk(sk);
427 /* don't care about overflows in this statistic */
428 bo->dropped_usr_msgs++;
433 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
435 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
436 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
438 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
439 enum hrtimer_restart ret = HRTIMER_NORESTART;
441 if (op->kt_ival1.tv64 && (op->count > 0)) {
443 static void bcm_tx_timeout_handler(unsigned long data)
445 struct bcm_op *op = (struct bcm_op *)data;
447 if (op->j_ival1 && (op->count > 0)) {
451 if (!op->count && (op->flags & TX_COUNTEVT)) {
452 struct bcm_msg_head msg_head;
454 /* create notification to user */
455 msg_head.opcode = TX_EXPIRED;
456 msg_head.flags = op->flags;
457 msg_head.count = op->count;
458 msg_head.ival1 = op->ival1;
459 msg_head.ival2 = op->ival2;
460 msg_head.can_id = op->can_id;
461 msg_head.nframes = 0;
463 bcm_send_to_user(op, &msg_head, NULL, 0);
467 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
468 if (op->kt_ival1.tv64 && (op->count > 0)) {
470 /* send (next) frame */
472 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
473 ret = HRTIMER_RESTART;
476 if (op->kt_ival2.tv64) {
478 /* send (next) frame */
480 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
481 ret = HRTIMER_RESTART;
487 if (op->j_ival1 && (op->count > 0)) {
489 /* send (next) frame */
491 mod_timer(&op->timer, jiffies + op->j_ival1);
496 /* send (next) frame */
498 mod_timer(&op->timer, jiffies + op->j_ival2);
507 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
509 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
511 struct bcm_msg_head head;
513 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
514 op->j_lastmsg = jiffies;
517 /* update statistics */
518 op->frames_filtered++;
520 /* prevent statistics overflow */
521 if (op->frames_filtered > ULONG_MAX/100)
522 op->frames_filtered = op->frames_abs = 0;
524 head.opcode = RX_CHANGED;
525 head.flags = op->flags;
526 head.count = op->count;
527 head.ival1 = op->ival1;
528 head.ival2 = op->ival2;
529 head.can_id = op->can_id;
532 bcm_send_to_user(op, &head, data, 1);
535 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
536 /* is part of linux/hrtimer.h since 2.6.26 */
537 static inline int hrtimer_callback_running(struct hrtimer *timer)
539 return timer->state & HRTIMER_STATE_CALLBACK;
542 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
543 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
545 return ktime_to_us(ktime_sub(later, earlier));
549 * bcm_rx_update_and_send - process a detected relevant receive content change
550 * 1. update the last received data
551 * 2. send a notification to the user (if possible)
553 static void bcm_rx_update_and_send(struct bcm_op *op,
554 struct can_frame *lastdata,
555 struct can_frame *rxdata)
557 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
558 memcpy(lastdata, rxdata, CFSIZ);
561 lastdata->can_dlc |= RX_RECV;
563 /* throtteling mode inactive OR data update already on the run ? */
564 if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
565 /* send RX_CHANGED to the user immediately */
566 bcm_rx_changed(op, rxdata);
570 if (hrtimer_active(&op->thrtimer)) {
571 /* mark as 'throttled' */
572 lastdata->can_dlc |= RX_THR;
576 if (!op->kt_lastmsg.tv64) {
577 /* send first RX_CHANGED to the user immediately */
578 bcm_rx_changed(op, rxdata);
579 op->kt_lastmsg = ktime_get();
583 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
584 ktime_to_us(op->kt_ival2)) {
585 /* mark as 'throttled' and start timer */
586 lastdata->can_dlc |= RX_THR;
587 hrtimer_start(&op->thrtimer,
588 ktime_add(op->kt_lastmsg, op->kt_ival2),
593 /* the gap was that big, that throttling was not needed here */
594 bcm_rx_changed(op, rxdata);
595 op->kt_lastmsg = ktime_get();
597 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
599 memcpy(lastdata, rxdata, CFSIZ);
602 lastdata->can_dlc |= RX_RECV;
604 /* throttle bcm_rx_changed ? */
605 if ((op->thrtimer.expires) ||
606 ((op->j_ival2) && (nexttx > jiffies))) {
607 /* we are already waiting OR we have to start waiting */
609 /* mark as 'throttled' */
610 lastdata->can_dlc |= RX_THR;
612 if (!(op->thrtimer.expires)) {
613 /* start the timer only the first time */
614 mod_timer(&op->thrtimer, nexttx);
618 /* send RX_CHANGED to the user immediately */
619 bcm_rx_changed(op, rxdata);
625 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
626 * received data stored in op->last_frames[]
628 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
629 struct can_frame *rxdata)
632 * no one uses the MSBs of can_dlc for comparation,
633 * so we use it here to detect the first time of reception
636 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
637 /* received data for the first time => send update to user */
638 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
642 /* do a real check in can_frame data section */
644 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
645 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
646 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
650 if (op->flags & RX_CHECK_DLC) {
651 /* do a real check in can_frame dlc */
652 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
654 bcm_rx_update_and_send(op, &op->last_frames[index],
662 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
664 static void bcm_rx_starttimer(struct bcm_op *op)
666 if (op->flags & RX_NO_AUTOTIMER)
669 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
670 if (op->kt_ival1.tv64)
671 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
674 mod_timer(&op->timer, jiffies + op->j_ival1);
679 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
681 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
682 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
684 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
686 static void bcm_rx_timeout_handler(unsigned long data)
688 struct bcm_op *op = (struct bcm_op *)data;
690 struct bcm_msg_head msg_head;
692 msg_head.opcode = RX_TIMEOUT;
693 msg_head.flags = op->flags;
694 msg_head.count = op->count;
695 msg_head.ival1 = op->ival1;
696 msg_head.ival2 = op->ival2;
697 msg_head.can_id = op->can_id;
698 msg_head.nframes = 0;
700 bcm_send_to_user(op, &msg_head, NULL, 0);
702 /* no restart of the timer is done here! */
704 /* if user wants to be informed, when cyclic CAN-Messages come back */
705 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
706 /* clear received can_frames to indicate 'nothing received' */
707 memset(op->last_frames, 0, op->nframes * CFSIZ);
709 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
711 return HRTIMER_NORESTART;
716 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
718 static int bcm_rx_thr_flush(struct bcm_op *op)
722 if (op->nframes > 1) {
725 /* for MUX filter we start at index 1 */
726 for (i = 1; i < op->nframes; i++) {
727 if ((op->last_frames) &&
728 (op->last_frames[i].can_dlc & RX_THR)) {
729 op->last_frames[i].can_dlc &= ~RX_THR;
730 bcm_rx_changed(op, &op->last_frames[i]);
736 /* for RX_FILTER_ID and simple filter */
737 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
738 op->last_frames[0].can_dlc &= ~RX_THR;
739 bcm_rx_changed(op, &op->last_frames[0]);
748 * bcm_rx_thr_handler - the time for blocked content updates is over now:
749 * Check for throttled data and send it to the userspace
751 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
752 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
754 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
756 if (bcm_rx_thr_flush(op)) {
757 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
758 return HRTIMER_RESTART;
760 /* rearm throttle handling */
761 op->kt_lastmsg = ktime_set(0, 0);
762 return HRTIMER_NORESTART;
766 static void bcm_rx_thr_handler(unsigned long data)
768 struct bcm_op *op = (struct bcm_op *)data;
770 if (bcm_rx_thr_flush(op))
771 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
773 /* mark disabled / consumed timer */
774 op->thrtimer.expires = 0;
780 * bcm_rx_handler - handle a CAN frame receiption
782 static void bcm_rx_handler(struct sk_buff *skb, void *data)
784 struct bcm_op *op = (struct bcm_op *)data;
785 struct can_frame rxframe;
788 /* disable timeout */
789 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
790 hrtimer_cancel(&op->timer);
792 del_timer(&op->timer);
795 if (skb->len == sizeof(rxframe)) {
796 memcpy(&rxframe, skb->data, sizeof(rxframe));
797 /* save rx timestamp */
798 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
799 op->rx_stamp = skb->tstamp;
801 skb_get_timestamp(skb, &op->rx_stamp);
803 /* save originator for recvfrom() */
804 op->rx_ifindex = skb->dev->ifindex;
805 /* update statistics */
814 if (op->can_id != rxframe.can_id)
817 if (op->flags & RX_RTR_FRAME) {
818 /* send reply for RTR-request (placed in op->frames[0]) */
823 if (op->flags & RX_FILTER_ID) {
824 /* the easiest case */
825 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
826 bcm_rx_starttimer(op);
830 if (op->nframes == 1) {
831 /* simple compare with index 0 */
832 bcm_rx_cmp_to_index(op, 0, &rxframe);
833 bcm_rx_starttimer(op);
837 if (op->nframes > 1) {
841 * find the first multiplex mask that fits.
842 * Remark: The MUX-mask is stored in index 0
845 for (i = 1; i < op->nframes; i++) {
846 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
847 (GET_U64(&op->frames[0]) &
848 GET_U64(&op->frames[i]))) {
849 bcm_rx_cmp_to_index(op, i, &rxframe);
853 bcm_rx_starttimer(op);
858 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
860 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
865 list_for_each_entry(op, ops, list) {
866 if ((op->can_id == can_id) && (op->ifindex == ifindex))
873 static void bcm_remove_op(struct bcm_op *op)
875 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
876 hrtimer_cancel(&op->timer);
877 hrtimer_cancel(&op->thrtimer);
879 del_timer(&op->timer);
880 del_timer(&op->thrtimer);
883 if ((op->frames) && (op->frames != &op->sframe))
886 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
887 kfree(op->last_frames);
894 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
896 if (op->rx_reg_dev == dev) {
897 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
900 /* mark as removed subscription */
901 op->rx_reg_dev = NULL;
903 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
904 "mismatch %p %p\n", op->rx_reg_dev, dev);
908 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
910 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
912 struct bcm_op *op, *n;
914 list_for_each_entry_safe(op, n, ops, list) {
915 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
918 * Don't care if we're bound or not (due to netdev
919 * problems) can_rx_unregister() is always a save
924 * Only remove subscriptions that had not
925 * been removed due to NETDEV_UNREGISTER
928 if (op->rx_reg_dev) {
929 struct net_device *dev;
931 dev = dev_get_by_index(&init_net,
934 bcm_rx_unreg(dev, op);
939 can_rx_unregister(NULL, op->can_id,
949 return 0; /* not found */
953 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
955 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
957 struct bcm_op *op, *n;
959 list_for_each_entry_safe(op, n, ops, list) {
960 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
967 return 0; /* not found */
971 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
973 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
976 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
981 /* put current values into msg_head */
982 msg_head->flags = op->flags;
983 msg_head->count = op->count;
984 msg_head->ival1 = op->ival1;
985 msg_head->ival2 = op->ival2;
986 msg_head->nframes = op->nframes;
988 bcm_send_to_user(op, msg_head, op->frames, 0);
994 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
996 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
997 int ifindex, struct sock *sk)
999 struct bcm_sock *bo = bcm_sk(sk);
1003 /* we need a real device to send frames */
1007 /* we need at least one can_frame */
1008 if (msg_head->nframes < 1)
1011 /* check the given can_id */
1012 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
1015 /* update existing BCM operation */
1018 * Do we need more space for the can_frames than currently
1019 * allocated? -> This is a _really_ unusual use-case and
1020 * therefore (complexity / locking) it is not supported.
1022 if (msg_head->nframes > op->nframes)
1025 /* update can_frames content */
1026 for (i = 0; i < msg_head->nframes; i++) {
1027 err = memcpy_fromiovec((u8 *)&op->frames[i],
1028 msg->msg_iov, CFSIZ);
1030 if (op->frames[i].can_dlc > 8)
1036 if (msg_head->flags & TX_CP_CAN_ID) {
1037 /* copy can_id into frame */
1038 op->frames[i].can_id = msg_head->can_id;
1043 /* insert new BCM operation for the given can_id */
1045 op = kzalloc(OPSIZ, GFP_KERNEL);
1049 op->can_id = msg_head->can_id;
1051 /* create array for can_frames and copy the data */
1052 if (msg_head->nframes > 1) {
1053 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1060 op->frames = &op->sframe;
1062 for (i = 0; i < msg_head->nframes; i++) {
1063 err = memcpy_fromiovec((u8 *)&op->frames[i],
1064 msg->msg_iov, CFSIZ);
1066 if (op->frames[i].can_dlc > 8)
1070 if (op->frames != &op->sframe)
1076 if (msg_head->flags & TX_CP_CAN_ID) {
1077 /* copy can_id into frame */
1078 op->frames[i].can_id = msg_head->can_id;
1082 /* tx_ops never compare with previous received messages */
1083 op->last_frames = NULL;
1085 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1087 op->ifindex = ifindex;
1089 /* initialize uninitialized (kzalloc) structure */
1090 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1091 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1092 op->timer.function = bcm_tx_timeout_handler;
1094 /* currently unused in tx_ops */
1095 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1097 setup_timer(&op->timer, bcm_tx_timeout_handler,
1100 /* currently unused in tx_ops */
1101 init_timer(&op->thrtimer);
1104 /* add this bcm_op to the list of the tx_ops */
1105 list_add(&op->list, &bo->tx_ops);
1107 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1109 if (op->nframes != msg_head->nframes) {
1110 op->nframes = msg_head->nframes;
1111 /* start multiple frame transmission with index 0 */
1117 op->flags = msg_head->flags;
1119 if (op->flags & TX_RESET_MULTI_IDX) {
1120 /* start multiple frame transmission with index 0 */
1124 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1125 if (op->flags & SETTIMER) {
1126 /* set timer values */
1127 op->count = msg_head->count;
1128 op->ival1 = msg_head->ival1;
1129 op->ival2 = msg_head->ival2;
1130 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1131 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1133 /* disable an active timer due to zero values? */
1134 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1135 hrtimer_cancel(&op->timer);
1138 if ((op->flags & STARTTIMER) &&
1139 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
1141 /* spec: send can_frame when starting timer */
1142 op->flags |= TX_ANNOUNCE;
1144 if (op->kt_ival1.tv64 && (op->count > 0)) {
1145 /* op->count-- is done in bcm_tx_timeout_handler */
1146 hrtimer_start(&op->timer, op->kt_ival1,
1149 hrtimer_start(&op->timer, op->kt_ival2,
1153 if (op->flags & SETTIMER) {
1154 /* set timer values */
1155 op->count = msg_head->count;
1156 op->ival1 = msg_head->ival1;
1157 op->ival2 = msg_head->ival2;
1158 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1159 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1161 /* disable an active timer due to zero values? */
1162 if (!op->j_ival1 && !op->j_ival2)
1163 del_timer(&op->timer);
1166 if ((op->flags & STARTTIMER) &&
1167 ((op->j_ival1 && op->count) || op->j_ival2)) {
1169 /* spec: send can_frame when starting timer */
1170 op->flags |= TX_ANNOUNCE;
1172 if (op->j_ival1 && (op->count > 0)) {
1173 /* op->count-- is done in bcm_tx_timeout_handler */
1174 mod_timer(&op->timer, jiffies + op->j_ival1);
1176 mod_timer(&op->timer, jiffies + op->j_ival2);
1180 if (op->flags & TX_ANNOUNCE)
1183 return msg_head->nframes * CFSIZ + MHSIZ;
1187 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1189 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1190 int ifindex, struct sock *sk)
1192 struct bcm_sock *bo = bcm_sk(sk);
1197 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1198 /* be robust against wrong usage ... */
1199 msg_head->flags |= RX_FILTER_ID;
1200 /* ignore trailing garbage */
1201 msg_head->nframes = 0;
1204 if ((msg_head->flags & RX_RTR_FRAME) &&
1205 ((msg_head->nframes != 1) ||
1206 (!(msg_head->can_id & CAN_RTR_FLAG))))
1209 /* check the given can_id */
1210 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1212 /* update existing BCM operation */
1215 * Do we need more space for the can_frames than currently
1216 * allocated? -> This is a _really_ unusual use-case and
1217 * therefore (complexity / locking) it is not supported.
1219 if (msg_head->nframes > op->nframes)
1222 if (msg_head->nframes) {
1223 /* update can_frames content */
1224 err = memcpy_fromiovec((u8 *)op->frames,
1226 msg_head->nframes * CFSIZ);
1230 /* clear last_frames to indicate 'nothing received' */
1231 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1234 op->nframes = msg_head->nframes;
1236 /* Only an update -> do not call can_rx_register() */
1240 /* insert new BCM operation for the given can_id */
1241 op = kzalloc(OPSIZ, GFP_KERNEL);
1245 op->can_id = msg_head->can_id;
1246 op->nframes = msg_head->nframes;
1248 if (msg_head->nframes > 1) {
1249 /* create array for can_frames and copy the data */
1250 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1257 /* create and init array for received can_frames */
1258 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1260 if (!op->last_frames) {
1267 op->frames = &op->sframe;
1268 op->last_frames = &op->last_sframe;
1271 if (msg_head->nframes) {
1272 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1273 msg_head->nframes * CFSIZ);
1275 if (op->frames != &op->sframe)
1277 if (op->last_frames != &op->last_sframe)
1278 kfree(op->last_frames);
1284 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1286 op->ifindex = ifindex;
1288 /* initialize uninitialized (kzalloc) structure */
1289 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1290 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1291 op->timer.function = bcm_rx_timeout_handler;
1293 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1294 op->thrtimer.function = bcm_rx_thr_handler;
1296 setup_timer(&op->timer, bcm_rx_timeout_handler,
1299 /* init throttle timer for RX_CHANGED */
1300 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1303 /* mark disabled timer */
1304 op->thrtimer.expires = 0;
1307 /* add this bcm_op to the list of the rx_ops */
1308 list_add(&op->list, &bo->rx_ops);
1310 /* call can_rx_register() */
1313 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1316 op->flags = msg_head->flags;
1318 if (op->flags & RX_RTR_FRAME) {
1320 /* no timers in RTR-mode */
1321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1322 hrtimer_cancel(&op->thrtimer);
1323 hrtimer_cancel(&op->timer);
1325 del_timer(&op->thrtimer);
1326 del_timer(&op->timer);
1330 * funny feature in RX(!)_SETUP only for RTR-mode:
1331 * copy can_id into frame BUT without RTR-flag to
1332 * prevent a full-load-loopback-test ... ;-]
1334 if ((op->flags & TX_CP_CAN_ID) ||
1335 (op->frames[0].can_id == op->can_id))
1336 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1339 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
1340 if (op->flags & SETTIMER) {
1342 /* set timer value */
1343 op->ival1 = msg_head->ival1;
1344 op->ival2 = msg_head->ival2;
1345 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1346 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1348 /* disable an active timer due to zero value? */
1349 if (!op->kt_ival1.tv64)
1350 hrtimer_cancel(&op->timer);
1353 * In any case cancel the throttle timer, flush
1354 * potentially blocked msgs and reset throttle handling
1356 op->kt_lastmsg = ktime_set(0, 0);
1357 hrtimer_cancel(&op->thrtimer);
1358 bcm_rx_thr_flush(op);
1361 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1362 hrtimer_start(&op->timer, op->kt_ival1,
1365 if (op->flags & SETTIMER) {
1367 /* set timer value */
1368 op->ival1 = msg_head->ival1;
1369 op->ival2 = msg_head->ival2;
1370 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1371 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1373 /* disable an active timer due to zero value? */
1375 del_timer(&op->timer);
1378 * In any case cancel the throttle timer, flush
1379 * potentially blocked msgs and reset throttle handling
1381 del_timer(&op->thrtimer);
1382 bcm_rx_thr_flush(op);
1383 op->thrtimer.expires = 0;
1386 if ((op->flags & STARTTIMER) && op->j_ival1)
1387 mod_timer(&op->timer, jiffies + op->j_ival1);
1391 /* now we can register for can_ids, if we added a new bcm_op */
1392 if (do_rx_register) {
1394 struct net_device *dev;
1396 dev = dev_get_by_index(&init_net, ifindex);
1398 err = can_rx_register(dev, op->can_id,
1399 REGMASK(op->can_id),
1403 op->rx_reg_dev = dev;
1408 err = can_rx_register(NULL, op->can_id,
1409 REGMASK(op->can_id),
1410 bcm_rx_handler, op, "bcm");
1412 /* this bcm rx op is broken -> remove it */
1413 list_del(&op->list);
1419 return msg_head->nframes * CFSIZ + MHSIZ;
1423 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1425 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1427 struct sk_buff *skb;
1428 struct net_device *dev;
1431 /* we need a real device to send frames */
1435 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1440 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1446 dev = dev_get_by_index(&init_net, ifindex);
1454 err = can_send(skb, 1); /* send with loopback */
1460 return CFSIZ + MHSIZ;
1464 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1466 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1467 struct msghdr *msg, size_t size)
1469 struct sock *sk = sock->sk;
1470 struct bcm_sock *bo = bcm_sk(sk);
1471 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1472 struct bcm_msg_head msg_head;
1473 int ret; /* read bytes or error codes as return value */
1478 /* check for valid message length from userspace */
1479 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1482 /* check for alternative ifindex for this bcm_op */
1484 if (!ifindex && msg->msg_name) {
1485 /* no bound device as default => check msg_name */
1486 struct sockaddr_can *addr =
1487 (struct sockaddr_can *)msg->msg_name;
1489 if (addr->can_family != AF_CAN)
1492 /* ifindex from sendto() */
1493 ifindex = addr->can_ifindex;
1496 struct net_device *dev;
1498 dev = dev_get_by_index(&init_net, ifindex);
1502 if (dev->type != ARPHRD_CAN) {
1511 /* read message head information */
1513 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1519 switch (msg_head.opcode) {
1522 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1526 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1530 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1537 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1544 /* reuse msg_head for the reply to TX_READ */
1545 msg_head.opcode = TX_STATUS;
1546 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1550 /* reuse msg_head for the reply to RX_READ */
1551 msg_head.opcode = RX_STATUS;
1552 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1556 /* we need exactly one can_frame behind the msg head */
1557 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1560 ret = bcm_tx_send(msg, ifindex, sk);
1574 * notification handler for netdevice status changes
1576 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1579 struct net_device *dev = (struct net_device *)data;
1580 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1581 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1582 struct sock *sk = &bo->sk;
1584 struct sock *sk = bo->sk;
1587 int notify_enodev = 0;
1589 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1590 if (!net_eq(dev_net(dev), &init_net))
1592 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1593 if (dev->nd_net != &init_net)
1597 if (dev->type != ARPHRD_CAN)
1602 case NETDEV_UNREGISTER:
1605 /* remove device specific receive entries */
1606 list_for_each_entry(op, &bo->rx_ops, list)
1607 if (op->rx_reg_dev == dev)
1608 bcm_rx_unreg(dev, op);
1610 /* remove device reference, if this is our bound device */
1611 if (bo->bound && bo->ifindex == dev->ifindex) {
1619 if (notify_enodev) {
1620 sk->sk_err = ENODEV;
1621 if (!sock_flag(sk, SOCK_DEAD))
1622 sk->sk_error_report(sk);
1627 if (bo->bound && bo->ifindex == dev->ifindex) {
1628 sk->sk_err = ENETDOWN;
1629 if (!sock_flag(sk, SOCK_DEAD))
1630 sk->sk_error_report(sk);
1638 * initial settings for all BCM sockets to be set at socket creation time
1640 static int bcm_init(struct sock *sk)
1642 struct bcm_sock *bo = bcm_sk(sk);
1644 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1649 bo->dropped_usr_msgs = 0;
1650 bo->bcm_proc_read = NULL;
1652 INIT_LIST_HEAD(&bo->tx_ops);
1653 INIT_LIST_HEAD(&bo->rx_ops);
1656 bo->notifier.notifier_call = bcm_notifier;
1658 register_netdevice_notifier(&bo->notifier);
1664 * standard socket functions
1666 static int bcm_release(struct socket *sock)
1668 struct sock *sk = sock->sk;
1669 struct bcm_sock *bo = bcm_sk(sk);
1670 struct bcm_op *op, *next;
1672 /* remove bcm_ops, timer, rx_unregister(), etc. */
1674 unregister_netdevice_notifier(&bo->notifier);
1678 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1681 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1683 * Don't care if we're bound or not (due to netdev problems)
1684 * can_rx_unregister() is always a save thing to do here.
1688 * Only remove subscriptions that had not
1689 * been removed due to NETDEV_UNREGISTER
1692 if (op->rx_reg_dev) {
1693 struct net_device *dev;
1695 dev = dev_get_by_index(&init_net, op->ifindex);
1697 bcm_rx_unreg(dev, op);
1702 can_rx_unregister(NULL, op->can_id,
1703 REGMASK(op->can_id),
1704 bcm_rx_handler, op);
1709 /* remove procfs entry */
1710 if (proc_dir && bo->bcm_proc_read)
1711 remove_proc_entry(bo->procname, proc_dir);
1713 /* remove device reference */
1725 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1728 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1729 struct sock *sk = sock->sk;
1730 struct bcm_sock *bo = bcm_sk(sk);
1735 /* bind a device to this socket */
1736 if (addr->can_ifindex) {
1737 struct net_device *dev;
1739 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1743 if (dev->type != ARPHRD_CAN) {
1748 bo->ifindex = dev->ifindex;
1752 /* no interface reference for ifindex = 0 ('any' CAN device) */
1759 /* unique socket address as filename */
1760 sprintf(bo->procname, "%p", sock);
1761 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1769 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1770 struct msghdr *msg, size_t size, int flags)
1772 struct sock *sk = sock->sk;
1773 struct sk_buff *skb;
1778 noblock = flags & MSG_DONTWAIT;
1779 flags &= ~MSG_DONTWAIT;
1780 skb = skb_recv_datagram(sk, flags, noblock, &error);
1784 if (skb->len < size)
1787 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1789 skb_free_datagram(sk, skb);
1793 sock_recv_timestamp(msg, sk, skb);
1795 if (msg->msg_name) {
1796 msg->msg_namelen = sizeof(struct sockaddr_can);
1797 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1800 skb_free_datagram(sk, skb);
1805 static struct proto_ops bcm_ops __read_mostly = {
1807 .release = bcm_release,
1808 .bind = sock_no_bind,
1809 .connect = bcm_connect,
1810 .socketpair = sock_no_socketpair,
1811 .accept = sock_no_accept,
1812 .getname = sock_no_getname,
1813 .poll = datagram_poll,
1814 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1815 .listen = sock_no_listen,
1816 .shutdown = sock_no_shutdown,
1817 .setsockopt = sock_no_setsockopt,
1818 .getsockopt = sock_no_getsockopt,
1819 .sendmsg = bcm_sendmsg,
1820 .recvmsg = bcm_recvmsg,
1821 .mmap = sock_no_mmap,
1822 .sendpage = sock_no_sendpage,
1825 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1826 static struct proto bcm_proto __read_mostly = {
1828 .owner = THIS_MODULE,
1829 .obj_size = sizeof(struct bcm_sock),
1833 static struct can_proto bcm_can_proto __read_mostly = {
1835 .protocol = CAN_BCM,
1841 static struct can_proto bcm_can_proto __read_mostly = {
1843 .protocol = CAN_BCM,
1846 .owner = THIS_MODULE,
1847 .obj_size = sizeof(struct bcm_sock),
1852 static int __init bcm_module_init(void)
1858 err = can_proto_register(&bcm_can_proto);
1860 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1864 /* create /proc/net/can-bcm directory */
1865 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1866 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1868 proc_dir = proc_mkdir("can-bcm", proc_net);
1872 proc_dir->owner = THIS_MODULE;
1877 static void __exit bcm_module_exit(void)
1879 can_proto_unregister(&bcm_can_proto);
1882 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1883 proc_net_remove(&init_net, "can-bcm");
1885 proc_net_remove("can-bcm");
1889 module_init(bcm_module_init);
1890 module_exit(bcm_module_exit);