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 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/uio.h>
50 #include <linux/poll.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/socket.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <linux/can.h>
57 #include <linux/can/core.h>
58 #include <linux/can/bcm.h>
60 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
61 #include <net/net_namespace.h>
64 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
67 /* use of last_frames[index].can_dlc */
68 #define RX_RECV 0x40 /* received data for this element */
69 #define RX_THR 0x80 /* element not been sent due to throttle feature */
70 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
72 /* get best masking value for can_rx_register() for a given single can_id */
73 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
74 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
77 #define CAN_BCM_VERSION CAN_VERSION
78 static __initdata const char banner[] = KERN_INFO
79 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
81 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
82 MODULE_LICENSE("Dual BSD/GPL");
83 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
85 #ifdef CONFIG_CAN_DEBUG_CORE
87 module_param(debug, int, S_IRUGO);
88 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
91 /* easy access to can_frame payload */
92 static inline u64 GET_U64(const struct can_frame *cp)
94 return *(u64 *)cp->data;
98 struct list_head list;
102 unsigned long j_ival1, j_ival2, j_lastmsg;
103 unsigned long frames_abs, frames_filtered;
104 struct timer_list timer, thrtimer;
105 struct timeval ival1, ival2;
106 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
109 struct timeval rx_stamp;
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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
151 static void *kzalloc(size_t size, unsigned int __nocast flags)
153 void *ret = kmalloc(size, flags);
156 memset(ret, 0, size);
161 static inline void skb_get_timestamp(const struct sk_buff *skb,
162 struct timeval *stamp)
164 stamp->tv_sec = skb->stamp.tv_sec;
165 stamp->tv_usec = skb->stamp.tv_usec;
168 static inline void skb_set_timestamp(struct sk_buff *skb,
169 const struct timeval *stamp)
171 skb->stamp.tv_sec = stamp->tv_sec;
172 skb->stamp.tv_usec = stamp->tv_usec;
176 #define CFSIZ sizeof(struct can_frame)
177 #define OPSIZ sizeof(struct bcm_op)
178 #define MHSIZ sizeof(struct bcm_msg_head)
181 * rounded_tv2jif - calculate jiffies from timeval including optional up
182 * @tv: pointer to timeval
185 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
186 * function is intentionally more relaxed on precise timer ticks to get
187 * exact one jiffy for requested 1000us on a 1000HZ machine.
188 * This code is to be removed when upgrading to kernel hrtimer.
191 * calculated jiffies (max: ULONG_MAX)
193 static unsigned long rounded_tv2jif(const struct timeval *tv)
195 unsigned long sec = tv->tv_sec;
196 unsigned long usec = tv->tv_usec;
199 if (sec > ULONG_MAX / HZ)
202 /* round up to get at least the requested time */
203 usec += 1000000 / HZ - 1;
205 jif = usec / (1000000 / HZ);
207 if (sec * HZ > ULONG_MAX - jif)
210 return jif + sec * HZ;
216 static char *bcm_proc_getifname(int ifindex)
218 struct net_device *dev;
223 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
224 dev = __dev_get_by_index(&init_net, ifindex); /* no usage counting */
226 dev = __dev_get_by_index(ifindex); /* no usage counting */
234 static int bcm_read_proc(char *page, char **start, off_t off,
235 int count, int *eof, void *data)
238 struct sock *sk = (struct sock *)data;
239 struct bcm_sock *bo = bcm_sk(sk);
242 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
244 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
245 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
246 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
247 bo->dropped_usr_msgs);
248 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
249 bcm_proc_getifname(bo->ifindex));
250 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
252 list_for_each_entry(op, &bo->rx_ops, list) {
254 unsigned long reduction;
256 /* print only active entries & prevent division by zero */
260 len += snprintf(page + len, PAGE_SIZE - len,
262 op->can_id, bcm_proc_getifname(op->ifindex));
263 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
265 (op->flags & RX_CHECK_DLC)?'d':' ');
267 len += snprintf(page + len, PAGE_SIZE - len,
268 "timeo=%ld ", op->j_ival1);
271 len += snprintf(page + len, PAGE_SIZE - len,
272 "thr=%ld ", op->j_ival2);
274 len += snprintf(page + len, PAGE_SIZE - len,
275 "# recv %ld (%ld) => reduction: ",
276 op->frames_filtered, op->frames_abs);
278 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
280 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
281 (reduction == 100)?"near ":"", reduction);
283 if (len > PAGE_SIZE - 200) {
284 /* mark output cut off */
285 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
290 list_for_each_entry(op, &bo->tx_ops, list) {
292 len += snprintf(page + len, PAGE_SIZE - len,
293 "tx_op: %03X %s [%d] ",
294 op->can_id, bcm_proc_getifname(op->ifindex),
297 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
301 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
304 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
307 if (len > PAGE_SIZE - 100) {
308 /* mark output cut off */
309 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
314 len += snprintf(page + len, PAGE_SIZE - len, "\n");
321 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
322 * of the given bcm tx op
324 static void bcm_can_tx(struct bcm_op *op)
327 struct net_device *dev;
328 struct can_frame *cf = &op->frames[op->currframe];
330 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
332 /* no target device? => exit */
336 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
337 dev = dev_get_by_index(&init_net, op->ifindex);
339 dev = dev_get_by_index(op->ifindex);
342 /* RFC: should this bcm_op remove itself here? */
346 skb = alloc_skb(CFSIZ, gfp_any());
350 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
352 /* send with local echo */
357 /* update statistics */
361 /* reached last frame? */
362 if (op->currframe >= op->nframes)
369 * bcm_send_to_user - send a BCM message to the userspace
370 * (consisting of bcm_msg_head + x CAN frames)
372 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
373 struct can_frame *frames, int has_timestamp)
376 struct can_frame *firstframe;
377 struct sockaddr_can *addr;
378 struct sock *sk = op->sk;
379 int datalen = head->nframes * CFSIZ;
382 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
386 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
389 /* can_frames starting here */
390 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
391 firstframe = (struct can_frame *) skb_tail_pointer(skb);
393 firstframe = (struct can_frame *) skb->tail;
396 memcpy(skb_put(skb, datalen), frames, datalen);
399 * the BCM uses the can_dlc-element of the can_frame
400 * structure for internal purposes. This is only
401 * relevant for updates that are generated by the
402 * BCM, where nframes is 1
404 if (head->nframes == 1)
405 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
409 /* restore rx timestamp */
410 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
411 skb->tstamp = op->rx_stamp;
413 skb_set_timestamp(skb, &op->rx_stamp);
418 * Put the datagram to the queue so that bcm_recvmsg() can
419 * get it from there. We need to pass the interface index to
420 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
421 * containing the interface index.
424 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
425 addr = (struct sockaddr_can *)skb->cb;
426 memset(addr, 0, sizeof(*addr));
427 addr->can_family = AF_CAN;
428 addr->can_ifindex = op->rx_ifindex;
430 err = sock_queue_rcv_skb(sk, skb);
432 struct bcm_sock *bo = bcm_sk(sk);
434 DBG("sock_queue_rcv_skb failed: %d\n", err);
436 /* don't care about overflows in this statistic */
437 bo->dropped_usr_msgs++;
442 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
444 static void bcm_tx_timeout_handler(unsigned long data)
446 struct bcm_op *op = (struct bcm_op *)data;
448 DBG("Called with bcm_op %p\n", op);
450 if (op->j_ival1 && (op->count > 0)) {
453 if (!op->count && (op->flags & TX_COUNTEVT)) {
454 struct bcm_msg_head msg_head;
456 /* create notification to user */
457 DBG("sending TX_EXPIRED for can_id %03X\n",
460 msg_head.opcode = TX_EXPIRED;
461 msg_head.flags = op->flags;
462 msg_head.count = op->count;
463 msg_head.ival1 = op->ival1;
464 msg_head.ival2 = op->ival2;
465 msg_head.can_id = op->can_id;
466 msg_head.nframes = 0;
468 bcm_send_to_user(op, &msg_head, NULL, 0);
472 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
473 op->count, op->j_ival1, op->j_ival2);
475 if (op->j_ival1 && (op->count > 0)) {
477 op->timer.expires = jiffies + op->j_ival1;
478 add_timer(&op->timer);
480 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
482 (char *) op->timer.data,
483 (unsigned int) op->timer.expires);
485 /* send (next) frame */
490 op->timer.expires = jiffies + op->j_ival2;
491 add_timer(&op->timer);
493 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
495 (char *) op->timer.data,
496 (unsigned int) op->timer.expires);
498 /* send (next) frame */
502 DBG("no timer restart\n");
509 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
511 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
513 struct bcm_msg_head head;
515 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 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
525 (unsigned int) op->j_lastmsg, op);
526 DBG("sending notification\n");
528 head.opcode = RX_CHANGED;
529 head.flags = op->flags;
530 head.count = op->count;
531 head.ival1 = op->ival1;
532 head.ival2 = op->ival2;
533 head.can_id = op->can_id;
536 bcm_send_to_user(op, &head, data, 1);
540 * bcm_rx_update_and_send - process a detected relevant receive content change
541 * 1. update the last received data
542 * 2. send a notification to the user (if possible)
544 static void bcm_rx_update_and_send(struct bcm_op *op,
545 struct can_frame *lastdata,
546 struct can_frame *rxdata)
548 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
550 memcpy(lastdata, rxdata, CFSIZ);
553 lastdata->can_dlc |= RX_RECV;
555 /* throttle bcm_rx_changed ? */
556 if ((op->thrtimer.expires) ||
557 ((op->j_ival2) && (nexttx > jiffies))) {
558 /* we are already waiting OR we have to start waiting */
560 /* mark as 'throttled' */
561 lastdata->can_dlc |= RX_THR;
563 if (!(op->thrtimer.expires)) {
564 /* start the timer only the first time */
565 op->thrtimer.expires = nexttx;
566 add_timer(&op->thrtimer);
568 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
569 op->thrtimer.function,
570 (char *) op->thrtimer.data,
571 (unsigned int) op->thrtimer.expires);
575 /* send RX_CHANGED to the user immediately */
576 bcm_rx_changed(op, rxdata);
581 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
582 * received data stored in op->last_frames[]
584 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
585 struct can_frame *rxdata)
588 * no one uses the MSBs of can_dlc for comparation,
589 * so we use it here to detect the first time of reception
592 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
593 /* received data for the first time => send update to user */
594 DBG("first time :)\n");
595 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
599 /* do a real check in can_frame data section */
601 DBG("op->frames[index].data = 0x%016llx\n",
602 GET_U64(&op->frames[index]));
603 DBG("op->last_frames[index].data = 0x%016llx\n",
604 GET_U64(&op->last_frames[index]));
605 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
607 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
608 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
609 DBG("relevant data change :)\n");
610 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
614 if (op->flags & RX_CHECK_DLC) {
615 /* do a real check in can_frame dlc */
616 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
618 DBG("dlc change :)\n");
619 bcm_rx_update_and_send(op, &op->last_frames[index],
624 DBG("no relevant change :(\n");
628 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
630 static void bcm_rx_starttimer(struct bcm_op *op)
632 if (op->flags & RX_NO_AUTOTIMER)
636 op->timer.expires = jiffies + op->j_ival1;
638 DBG("adding rx timeout timer ival1. func=%p data=%p "
641 (char *) op->timer.data,
642 (unsigned int) op->timer.expires);
644 add_timer(&op->timer);
649 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
651 static void bcm_rx_timeout_handler(unsigned long data)
653 struct bcm_op *op = (struct bcm_op *)data;
654 struct bcm_msg_head msg_head;
656 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
658 msg_head.opcode = RX_TIMEOUT;
659 msg_head.flags = op->flags;
660 msg_head.count = op->count;
661 msg_head.ival1 = op->ival1;
662 msg_head.ival2 = op->ival2;
663 msg_head.can_id = op->can_id;
664 msg_head.nframes = 0;
666 bcm_send_to_user(op, &msg_head, NULL, 0);
668 /* no restart of the timer is done here! */
670 /* if user wants to be informed, when cyclic CAN-Messages come back */
671 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
672 /* clear received can_frames to indicate 'nothing received' */
673 memset(op->last_frames, 0, op->nframes * CFSIZ);
674 DBG("RX_ANNOUNCE_RESTART\n");
679 * bcm_rx_thr_handler - the time for blocked content updates is over now:
680 * Check for throttled data and send it to the userspace
682 static void bcm_rx_thr_handler(unsigned long data)
684 struct bcm_op *op = (struct bcm_op *)data;
687 /* mark disabled / consumed timer */
688 op->thrtimer.expires = 0;
690 if (op->nframes > 1) {
691 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
693 /* for MUX filter we start at index 1 */
694 for (i = 1; i < op->nframes; i++) {
695 if ((op->last_frames) &&
696 (op->last_frames[i].can_dlc & RX_THR)) {
697 op->last_frames[i].can_dlc &= ~RX_THR;
698 bcm_rx_changed(op, &op->last_frames[i]);
703 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
705 /* for RX_FILTER_ID and simple filter */
706 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
707 op->last_frames[0].can_dlc &= ~RX_THR;
708 bcm_rx_changed(op, &op->last_frames[0]);
714 * bcm_rx_handler - handle a CAN frame receiption
716 static void bcm_rx_handler(struct sk_buff *skb, void *data)
718 struct bcm_op *op = (struct bcm_op *)data;
719 struct can_frame rxframe;
722 /* disable timeout */
723 del_timer(&op->timer);
725 DBG("Called with bcm_op %p\n", op);
727 if (skb->len == sizeof(rxframe)) {
728 memcpy(&rxframe, skb->data, sizeof(rxframe));
729 /* save rx timestamp */
730 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
731 op->rx_stamp = skb->tstamp;
733 skb_get_timestamp(skb, &op->rx_stamp);
735 /* save originator for recvfrom() */
736 op->rx_ifindex = skb->dev->ifindex;
737 /* update statistics */
740 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
743 DBG("Wrong skb->len = %d\n", skb->len);
748 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
750 if (op->can_id != rxframe.can_id) {
751 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
752 rxframe.can_id, op->can_id);
756 if (op->flags & RX_RTR_FRAME) {
757 /* send reply for RTR-request */
758 DBG("RTR-request\n");
760 /* send op->frames[0] to CAN device */
765 if (op->flags & RX_FILTER_ID) {
766 /* the easiest case */
767 DBG("Easy does it with RX_FILTER_ID\n");
769 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
770 bcm_rx_starttimer(op);
774 if (op->nframes == 1) {
775 /* simple compare with index 0 */
776 DBG("Simple compare\n");
778 bcm_rx_cmp_to_index(op, 0, &rxframe);
779 bcm_rx_starttimer(op);
783 if (op->nframes > 1) {
784 /* multiplex compare */
785 DBG("Multiplex compare\n");
788 * find the first multiplex mask that fits.
789 * Remark: The MUX-mask is stored in index 0
792 for (i = 1; i < op->nframes; i++) {
793 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
794 (GET_U64(&op->frames[0]) &
795 GET_U64(&op->frames[i]))) {
796 DBG("found MUX index %d\n", i);
797 bcm_rx_cmp_to_index(op, i, &rxframe);
801 bcm_rx_starttimer(op);
806 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
808 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
813 list_for_each_entry(op, ops, list) {
814 if ((op->can_id == can_id) && (op->ifindex == ifindex))
821 static void bcm_remove_op(struct bcm_op *op)
823 del_timer(&op->timer);
824 del_timer(&op->thrtimer);
826 if ((op->frames) && (op->frames != &op->sframe))
829 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
830 kfree(op->last_frames);
837 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
839 if (op->rx_reg_dev == dev) {
840 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
843 /* mark as removed subscription */
844 op->rx_reg_dev = NULL;
846 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
847 "mismatch %p %p\n", op->rx_reg_dev, dev);
851 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
853 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
855 struct bcm_op *op, *n;
857 list_for_each_entry_safe(op, n, ops, list) {
858 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
859 DBG("removing rx_op %p for can_id %03X\n",
863 * Don't care if we're bound or not (due to netdev
864 * problems) can_rx_unregister() is always a save
869 * Only remove subscriptions that had not
870 * been removed due to NETDEV_UNREGISTER
873 if (op->rx_reg_dev) {
874 struct net_device *dev;
876 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
877 dev = dev_get_by_index(&init_net,
880 dev = dev_get_by_index(op->ifindex);
883 bcm_rx_unreg(dev, op);
888 can_rx_unregister(NULL, op->can_id,
898 return 0; /* not found */
902 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
904 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
906 struct bcm_op *op, *n;
908 list_for_each_entry_safe(op, n, ops, list) {
909 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
910 DBG("removing rx_op %p for can_id %03X\n",
918 return 0; /* not found */
922 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
924 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
927 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
930 DBG("TRX_READ: did not find op for can_id %03X\n",
935 DBG("TRX_READ: sending status for can_id %03X\n",
937 /* put current values into msg_head */
938 msg_head->flags = op->flags;
939 msg_head->count = op->count;
940 msg_head->ival1 = op->ival1;
941 msg_head->ival2 = op->ival2;
942 msg_head->nframes = op->nframes;
944 bcm_send_to_user(op, msg_head, op->frames, 0);
950 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
952 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
953 int ifindex, struct sock *sk)
955 struct bcm_sock *bo = bcm_sk(sk);
959 /* we need a real device to send frames */
963 /* we need at least one can_frame */
964 if (msg_head->nframes < 1)
967 /* check the given can_id */
968 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
971 /* update existing BCM operation */
973 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
974 op, msg_head->can_id);
977 * Do we need more space for the can_frames than currently
978 * allocated? -> This is a _really_ unusual use-case and
979 * therefore (complexity / locking) it is not supported.
981 if (msg_head->nframes > op->nframes)
984 /* update can_frames content */
985 for (i = 0; i < msg_head->nframes; i++) {
986 err = memcpy_fromiovec((u8 *)&op->frames[i],
987 msg->msg_iov, CFSIZ);
991 if (msg_head->flags & TX_CP_CAN_ID) {
992 /* copy can_id into frame */
993 op->frames[i].can_id = msg_head->can_id;
998 /* insert new BCM operation for the given can_id */
1000 op = kzalloc(OPSIZ, GFP_KERNEL);
1004 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
1005 op, msg_head->can_id);
1007 op->can_id = msg_head->can_id;
1009 /* create array for can_frames and copy the data */
1010 if (msg_head->nframes > 1) {
1011 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1018 op->frames = &op->sframe;
1020 for (i = 0; i < msg_head->nframes; i++) {
1021 err = memcpy_fromiovec((u8 *)&op->frames[i],
1022 msg->msg_iov, CFSIZ);
1024 if (op->frames != &op->sframe)
1030 if (msg_head->flags & TX_CP_CAN_ID) {
1031 /* copy can_id into frame */
1032 op->frames[i].can_id = msg_head->can_id;
1036 /* tx_ops never compare with previous received messages */
1037 op->last_frames = NULL;
1039 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1042 op->ifindex = ifindex;
1044 /* initialize uninitialized (kmalloc) structure */
1045 init_timer(&op->timer);
1047 /* currently unused in tx_ops */
1048 init_timer(&op->thrtimer);
1050 /* handler for tx_ops */
1051 op->timer.function = bcm_tx_timeout_handler;
1053 /* timer.data points to this op-structure */
1054 op->timer.data = (unsigned long)op;
1056 /* add this bcm_op to the list of the tx_ops */
1057 list_add(&op->list, &bo->tx_ops);
1059 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1061 if (op->nframes != msg_head->nframes) {
1062 op->nframes = msg_head->nframes;
1063 /* start multiple frame transmission with index 0 */
1069 op->flags = msg_head->flags;
1071 if (op->flags & TX_RESET_MULTI_IDX) {
1072 /* start multiple frame transmission with index 0 */
1076 if (op->flags & SETTIMER) {
1077 /* set timer values */
1079 op->count = msg_head->count;
1080 op->ival1 = msg_head->ival1;
1081 op->ival2 = msg_head->ival2;
1082 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1083 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1085 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1086 op->count, op->j_ival1, op->j_ival2);
1088 /* disable an active timer due to zero values? */
1089 if (!op->j_ival1 && !op->j_ival2) {
1090 del_timer(&op->timer);
1091 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1095 if ((op->flags & STARTTIMER) &&
1096 ((op->j_ival1 && op->count) || op->j_ival2)) {
1098 del_timer(&op->timer);
1100 /* spec: send can_frame when starting timer */
1101 op->flags |= TX_ANNOUNCE;
1103 if (op->j_ival1 && (op->count > 0)) {
1104 op->timer.expires = jiffies + op->j_ival1;
1105 /* op->count-- is done in bcm_tx_timeout_handler */
1106 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1109 (char *) op->timer.data,
1110 (unsigned int) op->timer.expires);
1113 op->timer.expires = jiffies + op->j_ival2;
1114 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1117 (char *) op->timer.data,
1118 (unsigned int) op->timer.expires);
1121 add_timer(&op->timer);
1124 if (op->flags & TX_ANNOUNCE)
1127 return msg_head->nframes * CFSIZ + MHSIZ;
1131 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1133 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1134 int ifindex, struct sock *sk)
1136 struct bcm_sock *bo = bcm_sk(sk);
1141 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1142 /* be robust against wrong usage ... */
1143 msg_head->flags |= RX_FILTER_ID;
1144 msg_head->nframes = 0; /* ignore trailing garbage */
1147 if ((msg_head->flags & RX_RTR_FRAME) &&
1148 ((msg_head->nframes != 1) ||
1149 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1151 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1155 /* check the given can_id */
1156 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1158 /* update existing BCM operation */
1160 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1161 op, msg_head->can_id);
1164 * Do we need more space for the can_frames than currently
1165 * allocated? -> This is a _really_ unusual use-case and
1166 * therefore (complexity / locking) it is not supported.
1168 if (msg_head->nframes > op->nframes)
1171 if (msg_head->nframes) {
1172 /* update can_frames content */
1173 err = memcpy_fromiovec((u8 *)op->frames,
1175 msg_head->nframes * CFSIZ);
1179 /* clear last_frames to indicate 'nothing received' */
1180 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1183 op->nframes = msg_head->nframes;
1185 /* Only an update -> do not call can_rx_register() */
1189 /* insert new BCM operation for the given can_id */
1191 op = kzalloc(OPSIZ, GFP_KERNEL);
1195 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1196 op, msg_head->can_id);
1198 op->can_id = msg_head->can_id;
1199 op->nframes = msg_head->nframes;
1201 if (msg_head->nframes > 1) {
1202 /* create array for can_frames and copy the data */
1203 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1210 /* create and init array for received can_frames */
1211 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1213 if (!op->last_frames) {
1220 op->frames = &op->sframe;
1221 op->last_frames = &op->last_sframe;
1224 if (msg_head->nframes) {
1225 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1226 msg_head->nframes * CFSIZ);
1228 if (op->frames != &op->sframe)
1230 if (op->last_frames != &op->last_sframe)
1231 kfree(op->last_frames);
1238 op->ifindex = ifindex;
1240 /* initialize uninitialized (kzalloc) structure */
1241 init_timer(&op->timer);
1243 /* init throttle timer for RX_CHANGED */
1244 init_timer(&op->thrtimer);
1246 /* handler for rx timeouts */
1247 op->timer.function = bcm_rx_timeout_handler;
1249 /* timer.data points to this op-structure */
1250 op->timer.data = (unsigned long)op;
1252 /* handler for RX_CHANGED throttle timeouts */
1253 op->thrtimer.function = bcm_rx_thr_handler;
1255 /* timer.data points to this op-structure */
1256 op->thrtimer.data = (unsigned long)op;
1258 /* mark disabled timer */
1259 op->thrtimer.expires = 0;
1261 /* add this bcm_op to the list of the rx_ops */
1262 list_add(&op->list, &bo->rx_ops);
1264 /* call can_rx_register() */
1267 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1270 op->flags = msg_head->flags;
1272 if (op->flags & RX_RTR_FRAME) {
1274 /* no timers in RTR-mode */
1275 del_timer(&op->thrtimer);
1276 del_timer(&op->timer);
1279 * funny feature in RX(!)_SETUP only for RTR-mode:
1280 * copy can_id into frame BUT without RTR-flag to
1281 * prevent a full-load-echo-test ... ;-]
1283 if ((op->flags & TX_CP_CAN_ID) ||
1284 (op->frames[0].can_id == op->can_id))
1285 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1288 if (op->flags & SETTIMER) {
1290 /* set timer value */
1291 op->ival1 = msg_head->ival1;
1292 op->ival2 = msg_head->ival2;
1293 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1294 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1296 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1297 op->j_ival1, op->j_ival2);
1299 /* disable an active timer due to zero value? */
1301 del_timer(&op->timer);
1302 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1305 /* free currently blocked msgs ? */
1306 if (op->thrtimer.expires) {
1307 DBG("RX_SETUP: unblocking throttled msgs.\n");
1308 del_timer(&op->thrtimer);
1309 /* send blocked msgs hereafter */
1310 op->thrtimer.expires = jiffies + 2;
1311 add_timer(&op->thrtimer);
1314 * if (op->j_ival2) is zero, no (new) throttling
1315 * will happen. For details see functions
1316 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1320 if ((op->flags & STARTTIMER) && op->j_ival1) {
1322 del_timer(&op->timer);
1323 op->timer.expires = jiffies + op->j_ival1;
1325 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1327 (char *) op->timer.function,
1328 (char *) op->timer.data,
1329 (unsigned int) op->timer.expires);
1331 add_timer(&op->timer);
1335 /* now we can register for can_ids, if we added a new bcm_op */
1336 if (do_rx_register) {
1337 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1338 "rx_op is %p\n", op->can_id, op);
1341 struct net_device *dev;
1343 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1344 dev = dev_get_by_index(&init_net, ifindex);
1346 dev = dev_get_by_index(ifindex);
1349 err = can_rx_register(dev, op->can_id,
1350 REGMASK(op->can_id),
1354 op->rx_reg_dev = dev;
1359 err = can_rx_register(NULL, op->can_id,
1360 REGMASK(op->can_id),
1361 bcm_rx_handler, op, IDENT);
1363 /* this bcm rx op is broken -> remove it */
1364 list_del(&op->list);
1370 return msg_head->nframes * CFSIZ + MHSIZ;
1374 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1376 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1378 struct sk_buff *skb;
1379 struct net_device *dev;
1382 /* just copy and send one can_frame */
1384 if (!ifindex) /* we need a real device to send frames */
1387 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1392 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1398 DBG_FRAME("BCM: TX_SEND: sending frame",
1399 (struct can_frame *)skb->data);
1401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1402 dev = dev_get_by_index(&init_net, ifindex);
1404 dev = dev_get_by_index(ifindex);
1413 can_send(skb, 1); /* send with local echo */
1416 return CFSIZ + MHSIZ;
1420 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1422 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1423 struct msghdr *msg, size_t size)
1425 struct sock *sk = sock->sk;
1426 struct bcm_sock *bo = bcm_sk(sk);
1427 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1428 struct bcm_msg_head msg_head;
1429 int ret; /* read bytes or error codes as return value */
1432 DBG("sock %p not bound\n", sk);
1436 /* check for alternative ifindex for this bcm_op */
1438 if (!ifindex && msg->msg_name) {
1439 /* no bound device as default => check msg_name */
1440 struct sockaddr_can *addr =
1441 (struct sockaddr_can *)msg->msg_name;
1443 if (addr->can_family != AF_CAN)
1446 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1449 struct net_device *dev;
1451 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1452 dev = dev_get_by_index(&init_net, ifindex);
1454 dev = dev_get_by_index(ifindex);
1457 DBG("device %d not found\n", ifindex);
1461 if (dev->type != ARPHRD_CAN) {
1462 DBG("device %d no CAN device\n", ifindex);
1471 /* read message head information */
1473 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1477 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1481 switch (msg_head.opcode) {
1484 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1488 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1492 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1499 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1506 /* reuse msg_head for the reply to TX_READ */
1507 msg_head.opcode = TX_STATUS;
1508 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1512 /* reuse msg_head for the reply to RX_READ */
1513 msg_head.opcode = RX_STATUS;
1514 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1518 /* we need at least one can_frame */
1519 if (msg_head.nframes < 1)
1522 ret = bcm_tx_send(msg, ifindex, sk);
1526 DBG("Unknown opcode %d\n", msg_head.opcode);
1537 * notification handler for netdevice status changes
1539 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1542 struct net_device *dev = (struct net_device *)data;
1543 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1544 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1545 struct sock *sk = &bo->sk;
1547 struct sock *sk = bo->sk;
1550 int notify_enodev = 0;
1552 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1553 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1555 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1556 if (dev->nd_net != &init_net)
1560 if (dev->type != ARPHRD_CAN)
1565 case NETDEV_UNREGISTER:
1568 /* remove device specific receive entries */
1569 list_for_each_entry(op, &bo->rx_ops, list)
1570 if (op->rx_reg_dev == dev)
1571 bcm_rx_unreg(dev, op);
1573 /* remove device reference, if this is our bound device */
1574 if (bo->bound && bo->ifindex == dev->ifindex) {
1582 if (notify_enodev) {
1583 sk->sk_err = ENODEV;
1584 if (!sock_flag(sk, SOCK_DEAD))
1585 sk->sk_error_report(sk);
1590 if (bo->bound && bo->ifindex == dev->ifindex) {
1591 sk->sk_err = ENETDOWN;
1592 if (!sock_flag(sk, SOCK_DEAD))
1593 sk->sk_error_report(sk);
1601 * initial settings for all BCM sockets to be set at socket creation time
1603 static int bcm_init(struct sock *sk)
1605 struct bcm_sock *bo = bcm_sk(sk);
1607 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1612 bo->dropped_usr_msgs = 0;
1613 bo->bcm_proc_read = NULL;
1615 INIT_LIST_HEAD(&bo->tx_ops);
1616 INIT_LIST_HEAD(&bo->rx_ops);
1619 bo->notifier.notifier_call = bcm_notifier;
1621 register_netdevice_notifier(&bo->notifier);
1627 * standard socket functions
1629 static int bcm_release(struct socket *sock)
1631 struct sock *sk = sock->sk;
1632 struct bcm_sock *bo = bcm_sk(sk);
1633 struct bcm_op *op, *next;
1635 DBG("socket %p, sk %p\n", sock, sk);
1637 /* remove bcm_ops, timer, rx_unregister(), etc. */
1639 unregister_netdevice_notifier(&bo->notifier);
1643 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1644 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1648 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1649 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1652 * Don't care if we're bound or not (due to netdev problems)
1653 * can_rx_unregister() is always a save thing to do here.
1657 * Only remove subscriptions that had not
1658 * been removed due to NETDEV_UNREGISTER
1661 if (op->rx_reg_dev) {
1662 struct net_device *dev;
1664 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1665 dev = dev_get_by_index(&init_net, op->ifindex);
1667 dev = dev_get_by_index(op->ifindex);
1670 bcm_rx_unreg(dev, op);
1675 can_rx_unregister(NULL, op->can_id,
1676 REGMASK(op->can_id),
1677 bcm_rx_handler, op);
1682 /* remove procfs entry */
1683 if (proc_dir && bo->bcm_proc_read)
1684 remove_proc_entry(bo->procname, proc_dir);
1686 /* remove device reference */
1698 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1701 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1702 struct sock *sk = sock->sk;
1703 struct bcm_sock *bo = bcm_sk(sk);
1708 /* bind a device to this socket */
1709 if (addr->can_ifindex) {
1710 struct net_device *dev;
1712 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1713 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1715 dev = dev_get_by_index(addr->can_ifindex);
1718 DBG("could not find device index %d\n",
1723 if (dev->type != ARPHRD_CAN) {
1724 DBG("device %d no CAN device\n", addr->can_ifindex);
1729 bo->ifindex = dev->ifindex;
1732 DBG("socket %p bound to device %s (idx %d)\n",
1733 sock, dev->name, dev->ifindex);
1736 /* no interface reference for ifindex = 0 ('any' CAN device) */
1743 /* unique socket address as filename */
1744 sprintf(bo->procname, "%p", sock);
1745 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1753 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1754 struct msghdr *msg, size_t size, int flags)
1756 struct sock *sk = sock->sk;
1757 struct sk_buff *skb;
1762 DBG("socket %p, sk %p\n", sock, sk);
1764 noblock = flags & MSG_DONTWAIT;
1765 flags &= ~MSG_DONTWAIT;
1766 skb = skb_recv_datagram(sk, flags, noblock, &error);
1770 DBG("delivering skbuff %p\n", skb);
1773 if (skb->len < size)
1776 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1778 skb_free_datagram(sk, skb);
1782 sock_recv_timestamp(msg, sk, skb);
1784 if (msg->msg_name) {
1785 msg->msg_namelen = sizeof(struct sockaddr_can);
1786 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1789 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1790 skb_free_datagram(sk, skb);
1795 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1798 unsigned int mask = 0;
1800 DBG("socket %p\n", sock);
1802 mask = datagram_poll(file, sock, wait);
1806 static struct proto_ops bcm_ops __read_mostly = {
1808 .release = bcm_release,
1809 .bind = sock_no_bind,
1810 .connect = bcm_connect,
1811 .socketpair = sock_no_socketpair,
1812 .accept = sock_no_accept,
1813 .getname = sock_no_getname,
1815 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1816 .listen = sock_no_listen,
1817 .shutdown = sock_no_shutdown,
1818 .setsockopt = sock_no_setsockopt,
1819 .getsockopt = sock_no_getsockopt,
1820 .sendmsg = bcm_sendmsg,
1821 .recvmsg = bcm_recvmsg,
1822 .mmap = sock_no_mmap,
1823 .sendpage = sock_no_sendpage,
1826 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1827 static struct proto bcm_proto __read_mostly = {
1829 .owner = THIS_MODULE,
1830 .obj_size = sizeof(struct bcm_sock),
1834 static struct can_proto bcm_can_proto __read_mostly = {
1836 .protocol = CAN_BCM,
1842 static struct can_proto bcm_can_proto __read_mostly = {
1844 .protocol = CAN_BCM,
1847 .owner = THIS_MODULE,
1848 .obj_size = sizeof(struct bcm_sock),
1853 static int __init bcm_module_init(void)
1859 err = can_proto_register(&bcm_can_proto);
1861 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1865 /* create /proc/net/can-bcm directory */
1866 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1867 proc_dir = proc_mkdir("can-"IDENT, init_net.proc_net);
1869 proc_dir = proc_mkdir("can-"IDENT, proc_net);
1873 proc_dir->owner = THIS_MODULE;
1878 static void __exit bcm_module_exit(void)
1880 can_proto_unregister(&bcm_can_proto);
1883 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1884 proc_net_remove(&init_net, "can-"IDENT);
1886 proc_net_remove("can-"IDENT);
1890 module_init(bcm_module_init);
1891 module_exit(bcm_module_exit);