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, the following disclaimer and
12 * the referenced file 'COPYING'.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Volkswagen nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * Alternatively, provided that this notice is retained in full, this
21 * software may be distributed under the terms of the GNU General
22 * Public License ("GPL") version 2 as distributed in the 'COPYING'
23 * file from the main directory of the linux kernel source.
25 * The provided data structures and external interfaces from this code
26 * are not restricted to be used by modules with a GPL compatible license.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * Send feedback to <socketcan-users@lists.berlios.de>
45 #include <linux/module.h>
46 #include <linux/version.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <linux/proc_fs.h>
50 #include <linux/uio.h>
51 #include <linux/poll.h>
52 #include <linux/net.h>
53 #include <linux/netdevice.h>
54 #include <linux/socket.h>
55 #include <linux/if_arp.h>
56 #include <linux/skbuff.h>
57 #include <linux/can.h>
58 #include <linux/can/core.h>
59 #include <linux/can/bcm.h>
61 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
62 #include <net/net_namespace.h>
65 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
68 /* use of last_frames[index].can_dlc */
69 #define RX_RECV 0x40 /* received data for this element */
70 #define RX_THR 0x80 /* element not been sent due to throttle feature */
71 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
73 /* get best masking value for can_rx_register() for a given single can_id */
74 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
75 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
78 #define CAN_BCM_VERSION CAN_VERSION
79 static __initdata const char banner[] = KERN_INFO
80 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
83 MODULE_LICENSE("Dual BSD/GPL");
84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
86 #ifdef CONFIG_CAN_DEBUG_CORE
88 module_param(debug, int, S_IRUGO);
89 MODULE_PARM_DESC(debug, "debug print mask: 1:debug, 2:frames, 4:skbs");
92 /* easy access to can_frame payload */
93 static inline u64 GET_U64(const struct can_frame *cp)
95 return *(u64 *)cp->data;
99 struct list_head list;
103 unsigned long j_ival1, j_ival2, j_lastmsg;
104 unsigned long frames_abs, frames_filtered;
105 struct timer_list timer, thrtimer;
106 struct timeval ival1, ival2;
107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
110 struct timeval rx_stamp;
116 struct can_frame *frames;
117 struct can_frame *last_frames;
118 struct can_frame sframe;
119 struct can_frame last_sframe;
121 struct net_device *rx_reg_dev;
124 static struct proc_dir_entry *proc_dir;
126 #ifdef CONFIG_CAN_BCM_USER
129 #define BCM_CAP CAP_NET_RAW
133 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
140 struct notifier_block notifier;
141 struct list_head rx_ops;
142 struct list_head tx_ops;
143 unsigned long dropped_usr_msgs;
144 struct proc_dir_entry *bcm_proc_read;
145 char procname [9]; /* pointer printed in ASCII with \0 */
148 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
150 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
151 return (struct bcm_sock *)sk;
153 return (struct bcm_sock *)sk->sk_protinfo;
157 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
158 static void *kzalloc(size_t size, unsigned int __nocast flags)
160 void *ret = kmalloc(size, flags);
163 memset(ret, 0, size);
168 static inline void skb_get_timestamp(const struct sk_buff *skb,
169 struct timeval *stamp)
171 stamp->tv_sec = skb->stamp.tv_sec;
172 stamp->tv_usec = skb->stamp.tv_usec;
175 static inline void skb_set_timestamp(struct sk_buff *skb,
176 const struct timeval *stamp)
178 skb->stamp.tv_sec = stamp->tv_sec;
179 skb->stamp.tv_usec = stamp->tv_usec;
183 #define CFSIZ sizeof(struct can_frame)
184 #define OPSIZ sizeof(struct bcm_op)
185 #define MHSIZ sizeof(struct bcm_msg_head)
188 * rounded_tv2jif - calculate jiffies from timeval including optional up
189 * @tv: pointer to timeval
192 * In opposite to timeval_to_jiffies() provided in include/linux/jiffies.h this
193 * function is intentionally more relaxed on precise timer ticks to get exact
194 * one jiffy for requested 1000us on a 1000HZ machine.
195 * This code is to be removed when upgrading to kernel hrtimer.
198 * calculated jiffies (max: ULONG_MAX)
200 static unsigned long rounded_tv2jif(const struct timeval *tv)
202 unsigned long sec = tv->tv_sec;
203 unsigned long usec = tv->tv_usec;
206 if (sec > ULONG_MAX / HZ)
209 /* round up to get at least the requested time */
210 usec += 1000000 / HZ - 1;
212 jif = usec / (1000000 / HZ);
214 if (sec * HZ > ULONG_MAX - jif)
217 return jif + sec * HZ;
223 static char *bcm_proc_getifname(int ifindex)
225 struct net_device *dev;
230 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
231 dev = __dev_get_by_index(&init_net, ifindex); /* no usage counting */
233 dev = __dev_get_by_index(ifindex); /* no usage counting */
241 static int bcm_read_proc(char *page, char **start, off_t off,
242 int count, int *eof, void *data)
245 struct sock *sk = (struct sock *)data;
246 struct bcm_sock *bo = bcm_sk(sk);
249 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
251 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
252 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
253 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
254 bo->dropped_usr_msgs);
255 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
256 bcm_proc_getifname(bo->ifindex));
257 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
259 list_for_each_entry(op, &bo->rx_ops, list) {
261 unsigned long reduction;
263 /* print only active entries & prevent division by zero */
267 len += snprintf(page + len, PAGE_SIZE - len,
269 op->can_id, bcm_proc_getifname(op->ifindex));
270 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
272 (op->flags & RX_CHECK_DLC)?'d':' ');
274 len += snprintf(page + len, PAGE_SIZE - len,
275 "timeo=%ld ", op->j_ival1);
278 len += snprintf(page + len, PAGE_SIZE - len,
279 "thr=%ld ", op->j_ival2);
281 len += snprintf(page + len, PAGE_SIZE - len,
282 "# recv %ld (%ld) => reduction: ",
283 op->frames_filtered, op->frames_abs);
285 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
287 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
288 (reduction == 100)?"near ":"", reduction);
290 if (len > PAGE_SIZE - 200) {
291 /* mark output cut off */
292 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
297 list_for_each_entry(op, &bo->tx_ops, list) {
299 len += snprintf(page + len, PAGE_SIZE - len,
300 "tx_op: %03X %s [%d] ",
301 op->can_id, bcm_proc_getifname(op->ifindex),
304 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
308 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
311 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
314 if (len > PAGE_SIZE - 100) {
315 /* mark output cut off */
316 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
321 len += snprintf(page + len, PAGE_SIZE - len, "\n");
328 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
329 * of the given bcm tx op
331 static void bcm_can_tx(struct bcm_op *op)
334 struct net_device *dev;
335 struct can_frame *cf = &op->frames[op->currframe];
337 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
339 /* no target device? => exit */
343 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
344 dev = dev_get_by_index(&init_net, op->ifindex);
346 dev = dev_get_by_index(op->ifindex);
349 /* RFC: should this bcm_op remove itself here? */
353 skb = alloc_skb(CFSIZ,
354 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
359 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
361 /* send with loopback */
366 /* update statistics */
370 /* reached last frame? */
371 if (op->currframe >= op->nframes)
378 * bcm_send_to_user - send a BCM message to the userspace
379 * (consisting of bcm_msg_head + x CAN frames)
381 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
382 struct can_frame *frames, int has_timestamp)
385 struct can_frame *firstframe;
386 struct sockaddr_can *addr;
387 struct sock *sk = op->sk;
388 int datalen = head->nframes * CFSIZ;
391 skb = alloc_skb(sizeof(*head) + datalen,
392 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
396 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
399 /* can_frames starting here */
400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
401 firstframe = (struct can_frame *) skb_tail_pointer(skb);
403 firstframe = (struct can_frame *) skb->tail;
406 memcpy(skb_put(skb, datalen), frames, datalen);
409 * the BCM uses the can_dlc-element of the can_frame
410 * structure for internal purposes. This is only
411 * relevant for updates that are generated by the
412 * BCM, where nframes is 1
414 if (head->nframes == 1)
415 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
419 /* restore rx timestamp */
420 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
421 skb->tstamp = op->rx_stamp;
423 skb_set_timestamp(skb, &op->rx_stamp);
427 /* restore originator for recvfrom() */
428 addr = (struct sockaddr_can *)skb->cb;
429 memset(addr, 0, sizeof(*addr));
430 addr->can_family = AF_CAN;
431 addr->can_ifindex = op->rx_ifindex;
433 err = sock_queue_rcv_skb(sk, skb);
435 struct bcm_sock *bo = bcm_sk(sk);
437 DBG("sock_queue_rcv_skb failed: %d\n", err);
439 /* don't care about overflows in this statistic */
440 bo->dropped_usr_msgs++;
445 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
447 static void bcm_tx_timeout_handler(unsigned long data)
449 struct bcm_op *op = (struct bcm_op *)data;
451 DBG("Called with bcm_op %p\n", op);
453 if (op->j_ival1 && (op->count > 0)) {
456 if (!op->count && (op->flags & TX_COUNTEVT)) {
457 struct bcm_msg_head msg_head;
459 /* create notification to user */
460 DBG("sending TX_EXPIRED for can_id %03X\n",
463 msg_head.opcode = TX_EXPIRED;
464 msg_head.flags = op->flags;
465 msg_head.count = op->count;
466 msg_head.ival1 = op->ival1;
467 msg_head.ival2 = op->ival2;
468 msg_head.can_id = op->can_id;
469 msg_head.nframes = 0;
471 bcm_send_to_user(op, &msg_head, NULL, 0);
475 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
476 op->count, op->j_ival1, op->j_ival2);
478 if (op->j_ival1 && (op->count > 0)) {
480 op->timer.expires = jiffies + op->j_ival1;
481 add_timer(&op->timer);
483 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
485 (char *) op->timer.data,
486 (unsigned int) op->timer.expires);
488 /* send (next) frame */
493 op->timer.expires = jiffies + op->j_ival2;
494 add_timer(&op->timer);
496 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
498 (char *) op->timer.data,
499 (unsigned int) op->timer.expires);
501 /* send (next) frame */
505 DBG("no timer restart\n");
512 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
514 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
516 struct bcm_msg_head head;
518 op->j_lastmsg = jiffies;
520 /* update statistics */
521 op->frames_filtered++;
523 /* prevent statistics overflow */
524 if (op->frames_filtered > ULONG_MAX/100)
525 op->frames_filtered = op->frames_abs = 0;
527 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
528 (unsigned int) op->j_lastmsg, op);
529 DBG("sending notification\n");
531 head.opcode = RX_CHANGED;
532 head.flags = op->flags;
533 head.count = op->count;
534 head.ival1 = op->ival1;
535 head.ival2 = op->ival2;
536 head.can_id = op->can_id;
539 bcm_send_to_user(op, &head, data, 1);
543 * bcm_rx_update_and_send - process a detected relevant receive content change
544 * 1. update the last received data
545 * 2. send a notification to the user (if possible)
547 static void bcm_rx_update_and_send(struct bcm_op *op,
548 struct can_frame *lastdata,
549 struct can_frame *rxdata)
551 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
553 memcpy(lastdata, rxdata, CFSIZ);
556 lastdata->can_dlc |= RX_RECV;
558 /* throttle bcm_rx_changed ? */
559 if ((op->thrtimer.expires) ||
560 ((op->j_ival2) && (nexttx > jiffies))) {
561 /* we are already waiting OR we have to start waiting */
563 /* mark as 'throttled' */
564 lastdata->can_dlc |= RX_THR;
566 if (!(op->thrtimer.expires)) {
567 /* start the timer only the first time */
568 op->thrtimer.expires = nexttx;
569 add_timer(&op->thrtimer);
571 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
572 op->thrtimer.function,
573 (char *) op->thrtimer.data,
574 (unsigned int) op->thrtimer.expires);
578 /* send RX_CHANGED to the user immediately */
579 bcm_rx_changed(op, rxdata);
584 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
585 * received data stored in op->last_frames[]
587 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
588 struct can_frame *rxdata)
591 * no one uses the MSBs of can_dlc for comparation,
592 * so we use it here to detect the first time of reception
595 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
596 /* received data for the first time => send update to user */
597 DBG("first time :)\n");
598 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
602 /* do a real check in can_frame data section */
604 DBG("op->frames[index].data = 0x%016llx\n",
605 GET_U64(&op->frames[index]));
606 DBG("op->last_frames[index].data = 0x%016llx\n",
607 GET_U64(&op->last_frames[index]));
608 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
610 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
611 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
612 DBG("relevant data change :)\n");
613 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
617 if (op->flags & RX_CHECK_DLC) {
618 /* do a real check in can_frame dlc */
619 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
621 DBG("dlc change :)\n");
622 bcm_rx_update_and_send(op, &op->last_frames[index],
627 DBG("no relevant change :(\n");
631 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
633 static void bcm_rx_starttimer(struct bcm_op *op)
635 if (op->flags & RX_NO_AUTOTIMER)
639 op->timer.expires = jiffies + op->j_ival1;
641 DBG("adding rx timeout timer ival1. func=%p data=%p "
644 (char *) op->timer.data,
645 (unsigned int) op->timer.expires);
647 add_timer(&op->timer);
652 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
654 static void bcm_rx_timeout_handler(unsigned long data)
656 struct bcm_op *op = (struct bcm_op *)data;
657 struct bcm_msg_head msg_head;
659 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
661 msg_head.opcode = RX_TIMEOUT;
662 msg_head.flags = op->flags;
663 msg_head.count = op->count;
664 msg_head.ival1 = op->ival1;
665 msg_head.ival2 = op->ival2;
666 msg_head.can_id = op->can_id;
667 msg_head.nframes = 0;
669 bcm_send_to_user(op, &msg_head, NULL, 0);
671 /* no restart of the timer is done here! */
673 /* if user wants to be informed, when cyclic CAN-Messages come back */
674 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
675 /* clear received can_frames to indicate 'nothing received' */
676 memset(op->last_frames, 0, op->nframes * CFSIZ);
677 DBG("RX_ANNOUNCE_RESTART\n");
682 * bcm_rx_thr_handler - the time for blocked content updates is over now:
683 * Check for throttled data and send it to the userspace
685 static void bcm_rx_thr_handler(unsigned long data)
687 struct bcm_op *op = (struct bcm_op *)data;
690 /* mark disabled / consumed timer */
691 op->thrtimer.expires = 0;
693 if (op->nframes > 1) {
694 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
696 /* for MUX filter we start at index 1 */
697 for (i = 1; i < op->nframes; i++) {
698 if ((op->last_frames) &&
699 (op->last_frames[i].can_dlc & RX_THR)) {
700 op->last_frames[i].can_dlc &= ~RX_THR;
701 bcm_rx_changed(op, &op->last_frames[i]);
706 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
708 /* for RX_FILTER_ID and simple filter */
709 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
710 op->last_frames[0].can_dlc &= ~RX_THR;
711 bcm_rx_changed(op, &op->last_frames[0]);
717 * bcm_rx_handler - handle a CAN frame receiption
719 static void bcm_rx_handler(struct sk_buff *skb, void *data)
721 struct bcm_op *op = (struct bcm_op *)data;
722 struct can_frame rxframe;
725 /* disable timeout */
726 del_timer(&op->timer);
728 DBG("Called with bcm_op %p\n", op);
730 if (skb->len == sizeof(rxframe)) {
731 memcpy(&rxframe, skb->data, sizeof(rxframe));
732 /* save rx timestamp */
733 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
734 op->rx_stamp = skb->tstamp;
736 skb_get_timestamp(skb, &op->rx_stamp);
738 /* save originator for recvfrom() */
739 op->rx_ifindex = skb->dev->ifindex;
740 /* update statistics */
743 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
746 DBG("Wrong skb->len = %d\n", skb->len);
751 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
753 if (op->can_id != rxframe.can_id) {
754 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
755 rxframe.can_id, op->can_id);
759 if (op->flags & RX_RTR_FRAME) {
760 /* send reply for RTR-request */
761 DBG("RTR-request\n");
763 /* send op->frames[0] to CAN device */
768 if (op->flags & RX_FILTER_ID) {
769 /* the easiest case */
770 DBG("Easy does it with RX_FILTER_ID\n");
772 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
773 bcm_rx_starttimer(op);
777 if (op->nframes == 1) {
778 /* simple compare with index 0 */
779 DBG("Simple compare\n");
781 bcm_rx_cmp_to_index(op, 0, &rxframe);
782 bcm_rx_starttimer(op);
786 if (op->nframes > 1) {
787 /* multiplex compare */
788 DBG("Multiplex compare\n");
791 * find the first multiplex mask that fits.
792 * Remark: The MUX-mask is stored in index 0
795 for (i = 1; i < op->nframes; i++) {
796 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
797 (GET_U64(&op->frames[0]) &
798 GET_U64(&op->frames[i]))) {
799 DBG("found MUX index %d\n", i);
800 bcm_rx_cmp_to_index(op, i, &rxframe);
804 bcm_rx_starttimer(op);
809 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
811 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
816 list_for_each_entry(op, ops, list) {
817 if ((op->can_id == can_id) && (op->ifindex == ifindex))
824 static void bcm_remove_op(struct bcm_op *op)
826 del_timer(&op->timer);
827 del_timer(&op->thrtimer);
829 if ((op->frames) && (op->frames != &op->sframe))
832 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
833 kfree(op->last_frames);
840 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
842 if (op->rx_reg_dev == dev) {
843 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
846 /* mark as removed subscription */
847 op->rx_reg_dev = NULL;
849 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
850 "mismatch %p %p\n", op->rx_reg_dev, dev);
854 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
856 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
858 struct bcm_op *op, *n;
860 list_for_each_entry_safe(op, n, ops, list) {
861 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
862 DBG("removing rx_op %p for can_id %03X\n",
866 * Don't care if we're bound or not (due to netdev
867 * problems) can_rx_unregister() is always a save
872 * Only remove subscriptions that had not
873 * been removed due to NETDEV_UNREGISTER
876 if (op->rx_reg_dev) {
877 struct net_device *dev;
879 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
880 dev = dev_get_by_index(&init_net,
883 dev = dev_get_by_index(op->ifindex);
886 bcm_rx_unreg(dev, op);
891 can_rx_unregister(NULL, op->can_id,
901 return 0; /* not found */
905 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
907 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
909 struct bcm_op *op, *n;
911 list_for_each_entry_safe(op, n, ops, list) {
912 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
913 DBG("removing rx_op %p for can_id %03X\n",
921 return 0; /* not found */
925 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
927 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
930 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
933 DBG("TRX_READ: did not find op for can_id %03X\n",
938 DBG("TRX_READ: sending status for can_id %03X\n",
940 /* put current values into msg_head */
941 msg_head->flags = op->flags;
942 msg_head->count = op->count;
943 msg_head->ival1 = op->ival1;
944 msg_head->ival2 = op->ival2;
945 msg_head->nframes = op->nframes;
947 bcm_send_to_user(op, msg_head, op->frames, 0);
953 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
955 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
956 int ifindex, struct sock *sk)
958 struct bcm_sock *bo = bcm_sk(sk);
962 /* we need a real device to send frames */
966 /* we need at least one can_frame */
967 if (msg_head->nframes < 1)
970 /* check the given can_id */
971 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
974 /* update existing BCM operation */
976 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
977 op, msg_head->can_id);
980 * Do we need more space for the can_frames than currently
981 * allocated? -> This is a _really_ unusual use-case and
982 * therefore (complexity / locking) it is not supported.
984 if (msg_head->nframes > op->nframes)
987 /* update can_frames content */
988 for (i = 0; i < msg_head->nframes; i++) {
989 err = memcpy_fromiovec((u8 *)&op->frames[i],
990 msg->msg_iov, CFSIZ);
994 if (msg_head->flags & TX_CP_CAN_ID) {
995 /* copy can_id into frame */
996 op->frames[i].can_id = msg_head->can_id;
1001 /* insert new BCM operation for the given can_id */
1003 op = kzalloc(OPSIZ, GFP_KERNEL);
1007 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
1008 op, msg_head->can_id);
1010 op->can_id = msg_head->can_id;
1012 /* create array for can_frames and copy the data */
1013 if (msg_head->nframes > 1) {
1014 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1021 op->frames = &op->sframe;
1023 for (i = 0; i < msg_head->nframes; i++) {
1024 err = memcpy_fromiovec((u8 *)&op->frames[i],
1025 msg->msg_iov, CFSIZ);
1027 if (op->frames != &op->sframe)
1033 if (msg_head->flags & TX_CP_CAN_ID) {
1034 /* copy can_id into frame */
1035 op->frames[i].can_id = msg_head->can_id;
1039 /* tx_ops never compare with previous received messages */
1040 op->last_frames = NULL;
1042 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1045 op->ifindex = ifindex;
1047 /* initialize uninitialized (kmalloc) structure */
1048 init_timer(&op->timer);
1050 /* currently unused in tx_ops */
1051 init_timer(&op->thrtimer);
1053 /* handler for tx_ops */
1054 op->timer.function = bcm_tx_timeout_handler;
1056 /* timer.data points to this op-structure */
1057 op->timer.data = (unsigned long)op;
1059 /* add this bcm_op to the list of the tx_ops */
1060 list_add(&op->list, &bo->tx_ops);
1062 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1064 if (op->nframes != msg_head->nframes) {
1065 op->nframes = msg_head->nframes;
1066 /* start multiple frame transmission with index 0 */
1072 op->flags = msg_head->flags;
1074 if (op->flags & TX_RESET_MULTI_IDX) {
1075 /* start multiple frame transmission with index 0 */
1079 if (op->flags & SETTIMER) {
1080 /* set timer values */
1082 op->count = msg_head->count;
1083 op->ival1 = msg_head->ival1;
1084 op->ival2 = msg_head->ival2;
1085 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1086 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1088 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1089 op->count, op->j_ival1, op->j_ival2);
1091 /* disable an active timer due to zero values? */
1092 if (!op->j_ival1 && !op->j_ival2) {
1093 del_timer(&op->timer);
1094 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1098 if ((op->flags & STARTTIMER) &&
1099 ((op->j_ival1 && op->count) || op->j_ival2)) {
1101 del_timer(&op->timer);
1103 /* spec: send can_frame when starting timer */
1104 op->flags |= TX_ANNOUNCE;
1106 if (op->j_ival1 && (op->count > 0)) {
1107 op->timer.expires = jiffies + op->j_ival1;
1108 /* op->count-- is done in bcm_tx_timeout_handler */
1109 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1112 (char *) op->timer.data,
1113 (unsigned int) op->timer.expires);
1116 op->timer.expires = jiffies + op->j_ival2;
1117 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1120 (char *) op->timer.data,
1121 (unsigned int) op->timer.expires);
1124 add_timer(&op->timer);
1127 if (op->flags & TX_ANNOUNCE)
1130 return msg_head->nframes * CFSIZ + MHSIZ;
1134 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1136 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1137 int ifindex, struct sock *sk)
1139 struct bcm_sock *bo = bcm_sk(sk);
1144 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1145 /* be robust against wrong usage ... */
1146 msg_head->flags |= RX_FILTER_ID;
1147 msg_head->nframes = 0; /* ignore trailing garbage */
1150 if ((msg_head->flags & RX_RTR_FRAME) &&
1151 ((msg_head->nframes != 1) ||
1152 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1154 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1158 /* check the given can_id */
1159 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1161 /* update existing BCM operation */
1163 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1164 op, msg_head->can_id);
1167 * Do we need more space for the can_frames than currently
1168 * allocated? -> This is a _really_ unusual use-case and
1169 * therefore (complexity / locking) it is not supported.
1171 if (msg_head->nframes > op->nframes)
1174 if (msg_head->nframes) {
1175 /* update can_frames content */
1176 err = memcpy_fromiovec((u8 *)op->frames,
1178 msg_head->nframes * CFSIZ);
1182 /* clear last_frames to indicate 'nothing received' */
1183 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1186 op->nframes = msg_head->nframes;
1188 /* Only an update -> do not call can_rx_register() */
1192 /* insert new BCM operation for the given can_id */
1194 op = kzalloc(OPSIZ, GFP_KERNEL);
1198 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1199 op, msg_head->can_id);
1201 op->can_id = msg_head->can_id;
1202 op->nframes = msg_head->nframes;
1204 if (msg_head->nframes > 1) {
1205 /* create array for can_frames and copy the data */
1206 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1213 /* create and init array for received can_frames */
1214 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1216 if (!op->last_frames) {
1223 op->frames = &op->sframe;
1224 op->last_frames = &op->last_sframe;
1227 if (msg_head->nframes) {
1228 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1229 msg_head->nframes * CFSIZ);
1231 if (op->frames != &op->sframe)
1233 if (op->last_frames != &op->last_sframe)
1234 kfree(op->last_frames);
1241 op->ifindex = ifindex;
1243 /* initialize uninitialized (kzalloc) structure */
1244 init_timer(&op->timer);
1246 /* init throttle timer for RX_CHANGED */
1247 init_timer(&op->thrtimer);
1249 /* handler for rx timeouts */
1250 op->timer.function = bcm_rx_timeout_handler;
1252 /* timer.data points to this op-structure */
1253 op->timer.data = (unsigned long)op;
1255 /* handler for RX_CHANGED throttle timeouts */
1256 op->thrtimer.function = bcm_rx_thr_handler;
1258 /* timer.data points to this op-structure */
1259 op->thrtimer.data = (unsigned long)op;
1261 /* mark disabled timer */
1262 op->thrtimer.expires = 0;
1264 /* add this bcm_op to the list of the tx_ops */
1265 list_add(&op->list, &bo->rx_ops);
1267 /* call can_rx_register() */
1270 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1273 op->flags = msg_head->flags;
1275 if (op->flags & RX_RTR_FRAME) {
1277 /* no timers in RTR-mode */
1278 del_timer(&op->thrtimer);
1279 del_timer(&op->timer);
1282 * funny feature in RX(!)_SETUP only for RTR-mode:
1283 * copy can_id into frame BUT without RTR-flag to
1284 * prevent a full-load-loopback-test ... ;-]
1286 if ((op->flags & TX_CP_CAN_ID) ||
1287 (op->frames[0].can_id == op->can_id))
1288 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1291 if (op->flags & SETTIMER) {
1293 /* set timer value */
1294 op->ival1 = msg_head->ival1;
1295 op->ival2 = msg_head->ival2;
1296 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1297 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1299 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1300 op->j_ival1, op->j_ival2);
1302 /* disable an active timer due to zero value? */
1304 del_timer(&op->timer);
1305 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1308 /* free currently blocked msgs ? */
1309 if (op->thrtimer.expires) {
1310 DBG("RX_SETUP: unblocking throttled msgs.\n");
1311 del_timer(&op->thrtimer);
1312 /* send blocked msgs hereafter */
1313 op->thrtimer.expires = jiffies + 2;
1314 add_timer(&op->thrtimer);
1317 * if (op->j_ival2) is zero, no (new) throttling
1318 * will happen. For details see functions
1319 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1323 if ((op->flags & STARTTIMER) && op->j_ival1) {
1325 del_timer(&op->timer);
1326 op->timer.expires = jiffies + op->j_ival1;
1328 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1330 (char *) op->timer.function,
1331 (char *) op->timer.data,
1332 (unsigned int) op->timer.expires);
1334 add_timer(&op->timer);
1338 /* now we can register for can_ids, if we added a new bcm_op */
1339 if (do_rx_register) {
1340 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1341 "rx_op is %p\n", op->can_id, op);
1344 struct net_device *dev;
1346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1347 dev = dev_get_by_index(&init_net, ifindex);
1349 dev = dev_get_by_index(ifindex);
1352 can_rx_register(dev, op->can_id,
1353 REGMASK(op->can_id),
1354 bcm_rx_handler, op, IDENT);
1355 op->rx_reg_dev = dev;
1360 can_rx_register(NULL, op->can_id, REGMASK(op->can_id),
1361 bcm_rx_handler, op, IDENT);
1364 return msg_head->nframes * CFSIZ + MHSIZ;
1368 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1370 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1372 struct sk_buff *skb;
1373 struct net_device *dev;
1376 /* just copy and send one can_frame */
1378 if (!ifindex) /* we need a real device to send frames */
1381 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1386 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1392 DBG_FRAME("BCM: TX_SEND: sending frame",
1393 (struct can_frame *)skb->data);
1395 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1396 dev = dev_get_by_index(&init_net, ifindex);
1398 dev = dev_get_by_index(ifindex);
1407 can_send(skb, 1); /* send with loopback */
1410 return CFSIZ + MHSIZ;
1414 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1416 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1417 struct msghdr *msg, size_t size)
1419 struct sock *sk = sock->sk;
1420 struct bcm_sock *bo = bcm_sk(sk);
1421 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1422 struct bcm_msg_head msg_head;
1423 int ret; /* read bytes or error codes as return value */
1426 DBG("sock %p not bound\n", sk);
1430 /* check for alternative ifindex for this bcm_op */
1432 if (!ifindex && msg->msg_name) {
1433 /* no bound device as default => check msg_name */
1434 struct sockaddr_can *addr =
1435 (struct sockaddr_can *)msg->msg_name;
1437 if (addr->can_family != AF_CAN)
1440 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1443 struct net_device *dev;
1445 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1446 dev = dev_get_by_index(&init_net, ifindex);
1448 dev = dev_get_by_index(ifindex);
1451 DBG("device %d not found\n", ifindex);
1455 if (dev->type != ARPHRD_CAN) {
1456 DBG("device %d no CAN device\n", ifindex);
1465 /* read message head information */
1467 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1471 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1475 switch (msg_head.opcode) {
1478 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1482 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1486 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1493 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1500 /* reuse msg_head for the reply to TX_READ */
1501 msg_head.opcode = TX_STATUS;
1502 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1506 /* reuse msg_head for the reply to RX_READ */
1507 msg_head.opcode = RX_STATUS;
1508 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1512 /* we need at least one can_frame */
1513 if (msg_head.nframes < 1)
1516 ret = bcm_tx_send(msg, ifindex, sk);
1520 DBG("Unknown opcode %d\n", msg_head.opcode);
1531 * notification handler for netdevice status changes
1533 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1536 struct net_device *dev = (struct net_device *)data;
1537 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1538 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1539 struct sock *sk = &bo->sk;
1541 struct sock *sk = bo->sk;
1544 int notify_enodev = 0;
1546 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1547 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1550 if (dev->nd_net != &init_net)
1554 if (dev->type != ARPHRD_CAN)
1559 case NETDEV_UNREGISTER:
1562 /* remove device specific receive entries */
1563 list_for_each_entry(op, &bo->rx_ops, list)
1564 if (op->rx_reg_dev == dev)
1565 bcm_rx_unreg(dev, op);
1567 /* remove device reference, if this is our bound device */
1568 if (bo->bound && bo->ifindex == dev->ifindex) {
1576 if (notify_enodev) {
1577 sk->sk_err = ENODEV;
1578 if (!sock_flag(sk, SOCK_DEAD))
1579 sk->sk_error_report(sk);
1584 if (bo->bound && bo->ifindex == dev->ifindex) {
1585 sk->sk_err = ENETDOWN;
1586 if (!sock_flag(sk, SOCK_DEAD))
1587 sk->sk_error_report(sk);
1595 * initial settings for all BCM sockets to be set at socket creation time
1597 static int bcm_init(struct sock *sk)
1599 struct bcm_sock *bo = bcm_sk(sk);
1601 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1606 bo->dropped_usr_msgs = 0;
1607 bo->bcm_proc_read = NULL;
1609 INIT_LIST_HEAD(&bo->tx_ops);
1610 INIT_LIST_HEAD(&bo->rx_ops);
1613 bo->notifier.notifier_call = bcm_notifier;
1615 register_netdevice_notifier(&bo->notifier);
1621 * standard socket functions
1623 static int bcm_release(struct socket *sock)
1625 struct sock *sk = sock->sk;
1626 struct bcm_sock *bo = bcm_sk(sk);
1627 struct bcm_op *op, *next;
1629 DBG("socket %p, sk %p\n", sock, sk);
1631 /* remove bcm_ops, timer, rx_unregister(), etc. */
1633 unregister_netdevice_notifier(&bo->notifier);
1637 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1638 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1642 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1643 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1646 * Don't care if we're bound or not (due to netdev problems)
1647 * can_rx_unregister() is always a save thing to do here.
1651 * Only remove subscriptions that had not
1652 * been removed due to NETDEV_UNREGISTER
1655 if (op->rx_reg_dev) {
1656 struct net_device *dev;
1658 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1659 dev = dev_get_by_index(&init_net, op->ifindex);
1661 dev = dev_get_by_index(op->ifindex);
1664 bcm_rx_unreg(dev, op);
1669 can_rx_unregister(NULL, op->can_id,
1670 REGMASK(op->can_id),
1671 bcm_rx_handler, op);
1676 /* remove procfs entry */
1677 if (proc_dir && bo->bcm_proc_read)
1678 remove_proc_entry(bo->procname, proc_dir);
1680 /* remove device reference */
1692 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1695 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1696 struct sock *sk = sock->sk;
1697 struct bcm_sock *bo = bcm_sk(sk);
1702 /* bind a device to this socket */
1703 if (addr->can_ifindex) {
1704 struct net_device *dev;
1706 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1707 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1709 dev = dev_get_by_index(addr->can_ifindex);
1712 DBG("could not find device index %d\n",
1717 if (dev->type != ARPHRD_CAN) {
1718 DBG("device %d no CAN device\n", addr->can_ifindex);
1723 bo->ifindex = dev->ifindex;
1726 DBG("socket %p bound to device %s (idx %d)\n",
1727 sock, dev->name, dev->ifindex);
1730 /* no interface reference for ifindex = 0 ('any' CAN device) */
1737 /* unique socket address as filename */
1738 sprintf(bo->procname, "%p", sock);
1739 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1747 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1748 struct msghdr *msg, size_t size, int flags)
1750 struct sock *sk = sock->sk;
1751 struct sk_buff *skb;
1756 DBG("socket %p, sk %p\n", sock, sk);
1758 noblock = flags & MSG_DONTWAIT;
1759 flags &= ~MSG_DONTWAIT;
1760 skb = skb_recv_datagram(sk, flags, noblock, &error);
1764 DBG("delivering skbuff %p\n", skb);
1767 if (skb->len < size)
1770 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1772 skb_free_datagram(sk, skb);
1776 sock_recv_timestamp(msg, sk, skb);
1778 if (msg->msg_name) {
1779 msg->msg_namelen = sizeof(struct sockaddr_can);
1780 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1783 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1784 skb_free_datagram(sk, skb);
1789 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1792 unsigned int mask = 0;
1794 DBG("socket %p\n", sock);
1796 mask = datagram_poll(file, sock, wait);
1800 static struct proto_ops bcm_ops = {
1802 .release = bcm_release,
1803 .bind = sock_no_bind,
1804 .connect = bcm_connect,
1805 .socketpair = sock_no_socketpair,
1806 .accept = sock_no_accept,
1807 .getname = sock_no_getname,
1809 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1810 .listen = sock_no_listen,
1811 .shutdown = sock_no_shutdown,
1812 .setsockopt = sock_no_setsockopt,
1813 .getsockopt = sock_no_getsockopt,
1814 .sendmsg = bcm_sendmsg,
1815 .recvmsg = bcm_recvmsg,
1816 .mmap = sock_no_mmap,
1817 .sendpage = sock_no_sendpage,
1820 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1821 static struct proto bcm_proto = {
1823 .owner = THIS_MODULE,
1824 .obj_size = sizeof(struct bcm_sock),
1828 static struct can_proto bcm_can_proto = {
1830 .protocol = CAN_BCM,
1831 .capability = BCM_CAP,
1836 static struct can_proto bcm_can_proto = {
1838 .protocol = CAN_BCM,
1839 .capability = BCM_CAP,
1841 .owner = THIS_MODULE,
1842 .obj_size = sizeof(struct bcm_sock),
1847 static int __init bcm_module_init(void)
1851 can_proto_register(&bcm_can_proto);
1853 /* create /proc/net/can-bcm directory */
1854 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1855 proc_dir = proc_mkdir("can-"IDENT, init_net.proc_net);
1857 proc_dir = proc_mkdir("can-"IDENT, proc_net);
1861 proc_dir->owner = THIS_MODULE;
1866 static void __exit bcm_module_exit(void)
1868 can_proto_unregister(&bcm_can_proto);
1871 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1872 proc_net_remove(&init_net, "can-"IDENT);
1874 proc_net_remove("can-"IDENT);
1878 module_init(bcm_module_init);
1879 module_exit(bcm_module_exit);