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 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
193 * function is intentionally more relaxed on precise timer ticks to get
194 * exact 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);
428 * Put the datagram to the queue so that bcm_recvmsg() can
429 * get it from there. We need to pass the interface index to
430 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
431 * containing the interface index.
434 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
435 addr = (struct sockaddr_can *)skb->cb;
436 memset(addr, 0, sizeof(*addr));
437 addr->can_family = AF_CAN;
438 addr->can_ifindex = op->rx_ifindex;
440 err = sock_queue_rcv_skb(sk, skb);
442 struct bcm_sock *bo = bcm_sk(sk);
444 DBG("sock_queue_rcv_skb failed: %d\n", err);
446 /* don't care about overflows in this statistic */
447 bo->dropped_usr_msgs++;
452 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
454 static void bcm_tx_timeout_handler(unsigned long data)
456 struct bcm_op *op = (struct bcm_op *)data;
458 DBG("Called with bcm_op %p\n", op);
460 if (op->j_ival1 && (op->count > 0)) {
463 if (!op->count && (op->flags & TX_COUNTEVT)) {
464 struct bcm_msg_head msg_head;
466 /* create notification to user */
467 DBG("sending TX_EXPIRED for can_id %03X\n",
470 msg_head.opcode = TX_EXPIRED;
471 msg_head.flags = op->flags;
472 msg_head.count = op->count;
473 msg_head.ival1 = op->ival1;
474 msg_head.ival2 = op->ival2;
475 msg_head.can_id = op->can_id;
476 msg_head.nframes = 0;
478 bcm_send_to_user(op, &msg_head, NULL, 0);
482 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
483 op->count, op->j_ival1, op->j_ival2);
485 if (op->j_ival1 && (op->count > 0)) {
487 op->timer.expires = jiffies + op->j_ival1;
488 add_timer(&op->timer);
490 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
492 (char *) op->timer.data,
493 (unsigned int) op->timer.expires);
495 /* send (next) frame */
500 op->timer.expires = jiffies + op->j_ival2;
501 add_timer(&op->timer);
503 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
505 (char *) op->timer.data,
506 (unsigned int) op->timer.expires);
508 /* send (next) frame */
512 DBG("no timer restart\n");
519 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
521 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
523 struct bcm_msg_head head;
525 op->j_lastmsg = jiffies;
527 /* update statistics */
528 op->frames_filtered++;
530 /* prevent statistics overflow */
531 if (op->frames_filtered > ULONG_MAX/100)
532 op->frames_filtered = op->frames_abs = 0;
534 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
535 (unsigned int) op->j_lastmsg, op);
536 DBG("sending notification\n");
538 head.opcode = RX_CHANGED;
539 head.flags = op->flags;
540 head.count = op->count;
541 head.ival1 = op->ival1;
542 head.ival2 = op->ival2;
543 head.can_id = op->can_id;
546 bcm_send_to_user(op, &head, data, 1);
550 * bcm_rx_update_and_send - process a detected relevant receive content change
551 * 1. update the last received data
552 * 2. send a notification to the user (if possible)
554 static void bcm_rx_update_and_send(struct bcm_op *op,
555 struct can_frame *lastdata,
556 struct can_frame *rxdata)
558 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
560 memcpy(lastdata, rxdata, CFSIZ);
563 lastdata->can_dlc |= RX_RECV;
565 /* throttle bcm_rx_changed ? */
566 if ((op->thrtimer.expires) ||
567 ((op->j_ival2) && (nexttx > jiffies))) {
568 /* we are already waiting OR we have to start waiting */
570 /* mark as 'throttled' */
571 lastdata->can_dlc |= RX_THR;
573 if (!(op->thrtimer.expires)) {
574 /* start the timer only the first time */
575 op->thrtimer.expires = nexttx;
576 add_timer(&op->thrtimer);
578 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
579 op->thrtimer.function,
580 (char *) op->thrtimer.data,
581 (unsigned int) op->thrtimer.expires);
585 /* send RX_CHANGED to the user immediately */
586 bcm_rx_changed(op, rxdata);
591 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
592 * received data stored in op->last_frames[]
594 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
595 struct can_frame *rxdata)
598 * no one uses the MSBs of can_dlc for comparation,
599 * so we use it here to detect the first time of reception
602 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
603 /* received data for the first time => send update to user */
604 DBG("first time :)\n");
605 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
609 /* do a real check in can_frame data section */
611 DBG("op->frames[index].data = 0x%016llx\n",
612 GET_U64(&op->frames[index]));
613 DBG("op->last_frames[index].data = 0x%016llx\n",
614 GET_U64(&op->last_frames[index]));
615 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
617 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
618 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
619 DBG("relevant data change :)\n");
620 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
624 if (op->flags & RX_CHECK_DLC) {
625 /* do a real check in can_frame dlc */
626 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
628 DBG("dlc change :)\n");
629 bcm_rx_update_and_send(op, &op->last_frames[index],
634 DBG("no relevant change :(\n");
638 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
640 static void bcm_rx_starttimer(struct bcm_op *op)
642 if (op->flags & RX_NO_AUTOTIMER)
646 op->timer.expires = jiffies + op->j_ival1;
648 DBG("adding rx timeout timer ival1. func=%p data=%p "
651 (char *) op->timer.data,
652 (unsigned int) op->timer.expires);
654 add_timer(&op->timer);
659 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
661 static void bcm_rx_timeout_handler(unsigned long data)
663 struct bcm_op *op = (struct bcm_op *)data;
664 struct bcm_msg_head msg_head;
666 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
668 msg_head.opcode = RX_TIMEOUT;
669 msg_head.flags = op->flags;
670 msg_head.count = op->count;
671 msg_head.ival1 = op->ival1;
672 msg_head.ival2 = op->ival2;
673 msg_head.can_id = op->can_id;
674 msg_head.nframes = 0;
676 bcm_send_to_user(op, &msg_head, NULL, 0);
678 /* no restart of the timer is done here! */
680 /* if user wants to be informed, when cyclic CAN-Messages come back */
681 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
682 /* clear received can_frames to indicate 'nothing received' */
683 memset(op->last_frames, 0, op->nframes * CFSIZ);
684 DBG("RX_ANNOUNCE_RESTART\n");
689 * bcm_rx_thr_handler - the time for blocked content updates is over now:
690 * Check for throttled data and send it to the userspace
692 static void bcm_rx_thr_handler(unsigned long data)
694 struct bcm_op *op = (struct bcm_op *)data;
697 /* mark disabled / consumed timer */
698 op->thrtimer.expires = 0;
700 if (op->nframes > 1) {
701 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
703 /* for MUX filter we start at index 1 */
704 for (i = 1; i < op->nframes; i++) {
705 if ((op->last_frames) &&
706 (op->last_frames[i].can_dlc & RX_THR)) {
707 op->last_frames[i].can_dlc &= ~RX_THR;
708 bcm_rx_changed(op, &op->last_frames[i]);
713 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
715 /* for RX_FILTER_ID and simple filter */
716 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
717 op->last_frames[0].can_dlc &= ~RX_THR;
718 bcm_rx_changed(op, &op->last_frames[0]);
724 * bcm_rx_handler - handle a CAN frame receiption
726 static void bcm_rx_handler(struct sk_buff *skb, void *data)
728 struct bcm_op *op = (struct bcm_op *)data;
729 struct can_frame rxframe;
732 /* disable timeout */
733 del_timer(&op->timer);
735 DBG("Called with bcm_op %p\n", op);
737 if (skb->len == sizeof(rxframe)) {
738 memcpy(&rxframe, skb->data, sizeof(rxframe));
739 /* save rx timestamp */
740 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
741 op->rx_stamp = skb->tstamp;
743 skb_get_timestamp(skb, &op->rx_stamp);
745 /* save originator for recvfrom() */
746 op->rx_ifindex = skb->dev->ifindex;
747 /* update statistics */
750 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
753 DBG("Wrong skb->len = %d\n", skb->len);
758 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
760 if (op->can_id != rxframe.can_id) {
761 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
762 rxframe.can_id, op->can_id);
766 if (op->flags & RX_RTR_FRAME) {
767 /* send reply for RTR-request */
768 DBG("RTR-request\n");
770 /* send op->frames[0] to CAN device */
775 if (op->flags & RX_FILTER_ID) {
776 /* the easiest case */
777 DBG("Easy does it with RX_FILTER_ID\n");
779 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
780 bcm_rx_starttimer(op);
784 if (op->nframes == 1) {
785 /* simple compare with index 0 */
786 DBG("Simple compare\n");
788 bcm_rx_cmp_to_index(op, 0, &rxframe);
789 bcm_rx_starttimer(op);
793 if (op->nframes > 1) {
794 /* multiplex compare */
795 DBG("Multiplex compare\n");
798 * find the first multiplex mask that fits.
799 * Remark: The MUX-mask is stored in index 0
802 for (i = 1; i < op->nframes; i++) {
803 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
804 (GET_U64(&op->frames[0]) &
805 GET_U64(&op->frames[i]))) {
806 DBG("found MUX index %d\n", i);
807 bcm_rx_cmp_to_index(op, i, &rxframe);
811 bcm_rx_starttimer(op);
816 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
818 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
823 list_for_each_entry(op, ops, list) {
824 if ((op->can_id == can_id) && (op->ifindex == ifindex))
831 static void bcm_remove_op(struct bcm_op *op)
833 del_timer(&op->timer);
834 del_timer(&op->thrtimer);
836 if ((op->frames) && (op->frames != &op->sframe))
839 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
840 kfree(op->last_frames);
847 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
849 if (op->rx_reg_dev == dev) {
850 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
853 /* mark as removed subscription */
854 op->rx_reg_dev = NULL;
856 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
857 "mismatch %p %p\n", op->rx_reg_dev, dev);
861 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
863 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
865 struct bcm_op *op, *n;
867 list_for_each_entry_safe(op, n, ops, list) {
868 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
869 DBG("removing rx_op %p for can_id %03X\n",
873 * Don't care if we're bound or not (due to netdev
874 * problems) can_rx_unregister() is always a save
879 * Only remove subscriptions that had not
880 * been removed due to NETDEV_UNREGISTER
883 if (op->rx_reg_dev) {
884 struct net_device *dev;
886 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
887 dev = dev_get_by_index(&init_net,
890 dev = dev_get_by_index(op->ifindex);
893 bcm_rx_unreg(dev, op);
898 can_rx_unregister(NULL, op->can_id,
908 return 0; /* not found */
912 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
914 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
916 struct bcm_op *op, *n;
918 list_for_each_entry_safe(op, n, ops, list) {
919 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
920 DBG("removing rx_op %p for can_id %03X\n",
928 return 0; /* not found */
932 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
934 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
937 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
940 DBG("TRX_READ: did not find op for can_id %03X\n",
945 DBG("TRX_READ: sending status for can_id %03X\n",
947 /* put current values into msg_head */
948 msg_head->flags = op->flags;
949 msg_head->count = op->count;
950 msg_head->ival1 = op->ival1;
951 msg_head->ival2 = op->ival2;
952 msg_head->nframes = op->nframes;
954 bcm_send_to_user(op, msg_head, op->frames, 0);
960 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
962 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
963 int ifindex, struct sock *sk)
965 struct bcm_sock *bo = bcm_sk(sk);
969 /* we need a real device to send frames */
973 /* we need at least one can_frame */
974 if (msg_head->nframes < 1)
977 /* check the given can_id */
978 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
981 /* update existing BCM operation */
983 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
984 op, msg_head->can_id);
987 * Do we need more space for the can_frames than currently
988 * allocated? -> This is a _really_ unusual use-case and
989 * therefore (complexity / locking) it is not supported.
991 if (msg_head->nframes > op->nframes)
994 /* update can_frames content */
995 for (i = 0; i < msg_head->nframes; i++) {
996 err = memcpy_fromiovec((u8 *)&op->frames[i],
997 msg->msg_iov, CFSIZ);
1001 if (msg_head->flags & TX_CP_CAN_ID) {
1002 /* copy can_id into frame */
1003 op->frames[i].can_id = msg_head->can_id;
1008 /* insert new BCM operation for the given can_id */
1010 op = kzalloc(OPSIZ, GFP_KERNEL);
1014 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
1015 op, msg_head->can_id);
1017 op->can_id = msg_head->can_id;
1019 /* create array for can_frames and copy the data */
1020 if (msg_head->nframes > 1) {
1021 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1028 op->frames = &op->sframe;
1030 for (i = 0; i < msg_head->nframes; i++) {
1031 err = memcpy_fromiovec((u8 *)&op->frames[i],
1032 msg->msg_iov, CFSIZ);
1034 if (op->frames != &op->sframe)
1040 if (msg_head->flags & TX_CP_CAN_ID) {
1041 /* copy can_id into frame */
1042 op->frames[i].can_id = msg_head->can_id;
1046 /* tx_ops never compare with previous received messages */
1047 op->last_frames = NULL;
1049 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1052 op->ifindex = ifindex;
1054 /* initialize uninitialized (kmalloc) structure */
1055 init_timer(&op->timer);
1057 /* currently unused in tx_ops */
1058 init_timer(&op->thrtimer);
1060 /* handler for tx_ops */
1061 op->timer.function = bcm_tx_timeout_handler;
1063 /* timer.data points to this op-structure */
1064 op->timer.data = (unsigned long)op;
1066 /* add this bcm_op to the list of the tx_ops */
1067 list_add(&op->list, &bo->tx_ops);
1069 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1071 if (op->nframes != msg_head->nframes) {
1072 op->nframes = msg_head->nframes;
1073 /* start multiple frame transmission with index 0 */
1079 op->flags = msg_head->flags;
1081 if (op->flags & TX_RESET_MULTI_IDX) {
1082 /* start multiple frame transmission with index 0 */
1086 if (op->flags & SETTIMER) {
1087 /* set timer values */
1089 op->count = msg_head->count;
1090 op->ival1 = msg_head->ival1;
1091 op->ival2 = msg_head->ival2;
1092 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1093 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1095 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1096 op->count, op->j_ival1, op->j_ival2);
1098 /* disable an active timer due to zero values? */
1099 if (!op->j_ival1 && !op->j_ival2) {
1100 del_timer(&op->timer);
1101 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1105 if ((op->flags & STARTTIMER) &&
1106 ((op->j_ival1 && op->count) || op->j_ival2)) {
1108 del_timer(&op->timer);
1110 /* spec: send can_frame when starting timer */
1111 op->flags |= TX_ANNOUNCE;
1113 if (op->j_ival1 && (op->count > 0)) {
1114 op->timer.expires = jiffies + op->j_ival1;
1115 /* op->count-- is done in bcm_tx_timeout_handler */
1116 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1119 (char *) op->timer.data,
1120 (unsigned int) op->timer.expires);
1123 op->timer.expires = jiffies + op->j_ival2;
1124 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1127 (char *) op->timer.data,
1128 (unsigned int) op->timer.expires);
1131 add_timer(&op->timer);
1134 if (op->flags & TX_ANNOUNCE)
1137 return msg_head->nframes * CFSIZ + MHSIZ;
1141 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1143 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1144 int ifindex, struct sock *sk)
1146 struct bcm_sock *bo = bcm_sk(sk);
1151 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1152 /* be robust against wrong usage ... */
1153 msg_head->flags |= RX_FILTER_ID;
1154 msg_head->nframes = 0; /* ignore trailing garbage */
1157 if ((msg_head->flags & RX_RTR_FRAME) &&
1158 ((msg_head->nframes != 1) ||
1159 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1161 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1165 /* check the given can_id */
1166 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1168 /* update existing BCM operation */
1170 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1171 op, msg_head->can_id);
1174 * Do we need more space for the can_frames than currently
1175 * allocated? -> This is a _really_ unusual use-case and
1176 * therefore (complexity / locking) it is not supported.
1178 if (msg_head->nframes > op->nframes)
1181 if (msg_head->nframes) {
1182 /* update can_frames content */
1183 err = memcpy_fromiovec((u8 *)op->frames,
1185 msg_head->nframes * CFSIZ);
1189 /* clear last_frames to indicate 'nothing received' */
1190 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1193 op->nframes = msg_head->nframes;
1195 /* Only an update -> do not call can_rx_register() */
1199 /* insert new BCM operation for the given can_id */
1201 op = kzalloc(OPSIZ, GFP_KERNEL);
1205 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1206 op, msg_head->can_id);
1208 op->can_id = msg_head->can_id;
1209 op->nframes = msg_head->nframes;
1211 if (msg_head->nframes > 1) {
1212 /* create array for can_frames and copy the data */
1213 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1220 /* create and init array for received can_frames */
1221 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1223 if (!op->last_frames) {
1230 op->frames = &op->sframe;
1231 op->last_frames = &op->last_sframe;
1234 if (msg_head->nframes) {
1235 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1236 msg_head->nframes * CFSIZ);
1238 if (op->frames != &op->sframe)
1240 if (op->last_frames != &op->last_sframe)
1241 kfree(op->last_frames);
1248 op->ifindex = ifindex;
1250 /* initialize uninitialized (kzalloc) structure */
1251 init_timer(&op->timer);
1253 /* init throttle timer for RX_CHANGED */
1254 init_timer(&op->thrtimer);
1256 /* handler for rx timeouts */
1257 op->timer.function = bcm_rx_timeout_handler;
1259 /* timer.data points to this op-structure */
1260 op->timer.data = (unsigned long)op;
1262 /* handler for RX_CHANGED throttle timeouts */
1263 op->thrtimer.function = bcm_rx_thr_handler;
1265 /* timer.data points to this op-structure */
1266 op->thrtimer.data = (unsigned long)op;
1268 /* mark disabled timer */
1269 op->thrtimer.expires = 0;
1271 /* add this bcm_op to the list of the tx_ops */
1272 list_add(&op->list, &bo->rx_ops);
1274 /* call can_rx_register() */
1277 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1280 op->flags = msg_head->flags;
1282 if (op->flags & RX_RTR_FRAME) {
1284 /* no timers in RTR-mode */
1285 del_timer(&op->thrtimer);
1286 del_timer(&op->timer);
1289 * funny feature in RX(!)_SETUP only for RTR-mode:
1290 * copy can_id into frame BUT without RTR-flag to
1291 * prevent a full-load-loopback-test ... ;-]
1293 if ((op->flags & TX_CP_CAN_ID) ||
1294 (op->frames[0].can_id == op->can_id))
1295 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1298 if (op->flags & SETTIMER) {
1300 /* set timer value */
1301 op->ival1 = msg_head->ival1;
1302 op->ival2 = msg_head->ival2;
1303 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1304 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1306 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1307 op->j_ival1, op->j_ival2);
1309 /* disable an active timer due to zero value? */
1311 del_timer(&op->timer);
1312 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1315 /* free currently blocked msgs ? */
1316 if (op->thrtimer.expires) {
1317 DBG("RX_SETUP: unblocking throttled msgs.\n");
1318 del_timer(&op->thrtimer);
1319 /* send blocked msgs hereafter */
1320 op->thrtimer.expires = jiffies + 2;
1321 add_timer(&op->thrtimer);
1324 * if (op->j_ival2) is zero, no (new) throttling
1325 * will happen. For details see functions
1326 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1330 if ((op->flags & STARTTIMER) && op->j_ival1) {
1332 del_timer(&op->timer);
1333 op->timer.expires = jiffies + op->j_ival1;
1335 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1337 (char *) op->timer.function,
1338 (char *) op->timer.data,
1339 (unsigned int) op->timer.expires);
1341 add_timer(&op->timer);
1345 /* now we can register for can_ids, if we added a new bcm_op */
1346 if (do_rx_register) {
1347 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1348 "rx_op is %p\n", op->can_id, op);
1351 struct net_device *dev;
1353 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1354 dev = dev_get_by_index(&init_net, ifindex);
1356 dev = dev_get_by_index(ifindex);
1359 err = can_rx_register(dev, op->can_id,
1360 REGMASK(op->can_id),
1364 op->rx_reg_dev = dev;
1370 err = can_rx_register(NULL, op->can_id,
1371 REGMASK(op->can_id),
1372 bcm_rx_handler, op, IDENT);
1377 return msg_head->nframes * CFSIZ + MHSIZ;
1381 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1383 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1385 struct sk_buff *skb;
1386 struct net_device *dev;
1389 /* just copy and send one can_frame */
1391 if (!ifindex) /* we need a real device to send frames */
1394 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1399 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1405 DBG_FRAME("BCM: TX_SEND: sending frame",
1406 (struct can_frame *)skb->data);
1408 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1409 dev = dev_get_by_index(&init_net, ifindex);
1411 dev = dev_get_by_index(ifindex);
1420 can_send(skb, 1); /* send with loopback */
1423 return CFSIZ + MHSIZ;
1427 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1429 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1430 struct msghdr *msg, size_t size)
1432 struct sock *sk = sock->sk;
1433 struct bcm_sock *bo = bcm_sk(sk);
1434 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1435 struct bcm_msg_head msg_head;
1436 int ret; /* read bytes or error codes as return value */
1439 DBG("sock %p not bound\n", sk);
1443 /* check for alternative ifindex for this bcm_op */
1445 if (!ifindex && msg->msg_name) {
1446 /* no bound device as default => check msg_name */
1447 struct sockaddr_can *addr =
1448 (struct sockaddr_can *)msg->msg_name;
1450 if (addr->can_family != AF_CAN)
1453 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1456 struct net_device *dev;
1458 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1459 dev = dev_get_by_index(&init_net, ifindex);
1461 dev = dev_get_by_index(ifindex);
1464 DBG("device %d not found\n", ifindex);
1468 if (dev->type != ARPHRD_CAN) {
1469 DBG("device %d no CAN device\n", ifindex);
1478 /* read message head information */
1480 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1484 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1488 switch (msg_head.opcode) {
1491 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1495 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1499 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1506 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1513 /* reuse msg_head for the reply to TX_READ */
1514 msg_head.opcode = TX_STATUS;
1515 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1519 /* reuse msg_head for the reply to RX_READ */
1520 msg_head.opcode = RX_STATUS;
1521 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1525 /* we need at least one can_frame */
1526 if (msg_head.nframes < 1)
1529 ret = bcm_tx_send(msg, ifindex, sk);
1533 DBG("Unknown opcode %d\n", msg_head.opcode);
1544 * notification handler for netdevice status changes
1546 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1549 struct net_device *dev = (struct net_device *)data;
1550 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1551 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1552 struct sock *sk = &bo->sk;
1554 struct sock *sk = bo->sk;
1557 int notify_enodev = 0;
1559 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1560 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1562 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1563 if (dev->nd_net != &init_net)
1567 if (dev->type != ARPHRD_CAN)
1572 case NETDEV_UNREGISTER:
1575 /* remove device specific receive entries */
1576 list_for_each_entry(op, &bo->rx_ops, list)
1577 if (op->rx_reg_dev == dev)
1578 bcm_rx_unreg(dev, op);
1580 /* remove device reference, if this is our bound device */
1581 if (bo->bound && bo->ifindex == dev->ifindex) {
1589 if (notify_enodev) {
1590 sk->sk_err = ENODEV;
1591 if (!sock_flag(sk, SOCK_DEAD))
1592 sk->sk_error_report(sk);
1597 if (bo->bound && bo->ifindex == dev->ifindex) {
1598 sk->sk_err = ENETDOWN;
1599 if (!sock_flag(sk, SOCK_DEAD))
1600 sk->sk_error_report(sk);
1608 * initial settings for all BCM sockets to be set at socket creation time
1610 static int bcm_init(struct sock *sk)
1612 struct bcm_sock *bo = bcm_sk(sk);
1614 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1619 bo->dropped_usr_msgs = 0;
1620 bo->bcm_proc_read = NULL;
1622 INIT_LIST_HEAD(&bo->tx_ops);
1623 INIT_LIST_HEAD(&bo->rx_ops);
1626 bo->notifier.notifier_call = bcm_notifier;
1628 register_netdevice_notifier(&bo->notifier);
1634 * standard socket functions
1636 static int bcm_release(struct socket *sock)
1638 struct sock *sk = sock->sk;
1639 struct bcm_sock *bo = bcm_sk(sk);
1640 struct bcm_op *op, *next;
1642 DBG("socket %p, sk %p\n", sock, sk);
1644 /* remove bcm_ops, timer, rx_unregister(), etc. */
1646 unregister_netdevice_notifier(&bo->notifier);
1650 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1651 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1655 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1656 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1659 * Don't care if we're bound or not (due to netdev problems)
1660 * can_rx_unregister() is always a save thing to do here.
1664 * Only remove subscriptions that had not
1665 * been removed due to NETDEV_UNREGISTER
1668 if (op->rx_reg_dev) {
1669 struct net_device *dev;
1671 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1672 dev = dev_get_by_index(&init_net, op->ifindex);
1674 dev = dev_get_by_index(op->ifindex);
1677 bcm_rx_unreg(dev, op);
1682 can_rx_unregister(NULL, op->can_id,
1683 REGMASK(op->can_id),
1684 bcm_rx_handler, op);
1689 /* remove procfs entry */
1690 if (proc_dir && bo->bcm_proc_read)
1691 remove_proc_entry(bo->procname, proc_dir);
1693 /* remove device reference */
1705 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1708 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1709 struct sock *sk = sock->sk;
1710 struct bcm_sock *bo = bcm_sk(sk);
1715 /* bind a device to this socket */
1716 if (addr->can_ifindex) {
1717 struct net_device *dev;
1719 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1720 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1722 dev = dev_get_by_index(addr->can_ifindex);
1725 DBG("could not find device index %d\n",
1730 if (dev->type != ARPHRD_CAN) {
1731 DBG("device %d no CAN device\n", addr->can_ifindex);
1736 bo->ifindex = dev->ifindex;
1739 DBG("socket %p bound to device %s (idx %d)\n",
1740 sock, dev->name, dev->ifindex);
1743 /* no interface reference for ifindex = 0 ('any' CAN device) */
1750 /* unique socket address as filename */
1751 sprintf(bo->procname, "%p", sock);
1752 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1760 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1761 struct msghdr *msg, size_t size, int flags)
1763 struct sock *sk = sock->sk;
1764 struct sk_buff *skb;
1769 DBG("socket %p, sk %p\n", sock, sk);
1771 noblock = flags & MSG_DONTWAIT;
1772 flags &= ~MSG_DONTWAIT;
1773 skb = skb_recv_datagram(sk, flags, noblock, &error);
1777 DBG("delivering skbuff %p\n", skb);
1780 if (skb->len < size)
1783 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1785 skb_free_datagram(sk, skb);
1789 sock_recv_timestamp(msg, sk, skb);
1791 if (msg->msg_name) {
1792 msg->msg_namelen = sizeof(struct sockaddr_can);
1793 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1796 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1797 skb_free_datagram(sk, skb);
1802 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1805 unsigned int mask = 0;
1807 DBG("socket %p\n", sock);
1809 mask = datagram_poll(file, sock, wait);
1813 static struct proto_ops bcm_ops __read_mostly = {
1815 .release = bcm_release,
1816 .bind = sock_no_bind,
1817 .connect = bcm_connect,
1818 .socketpair = sock_no_socketpair,
1819 .accept = sock_no_accept,
1820 .getname = sock_no_getname,
1822 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1823 .listen = sock_no_listen,
1824 .shutdown = sock_no_shutdown,
1825 .setsockopt = sock_no_setsockopt,
1826 .getsockopt = sock_no_getsockopt,
1827 .sendmsg = bcm_sendmsg,
1828 .recvmsg = bcm_recvmsg,
1829 .mmap = sock_no_mmap,
1830 .sendpage = sock_no_sendpage,
1833 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1834 static struct proto bcm_proto __read_mostly = {
1836 .owner = THIS_MODULE,
1837 .obj_size = sizeof(struct bcm_sock),
1841 static struct can_proto bcm_can_proto __read_mostly = {
1843 .protocol = CAN_BCM,
1844 .capability = BCM_CAP,
1849 static struct can_proto bcm_can_proto __read_mostly = {
1851 .protocol = CAN_BCM,
1852 .capability = BCM_CAP,
1854 .owner = THIS_MODULE,
1855 .obj_size = sizeof(struct bcm_sock),
1860 static int __init bcm_module_init(void)
1866 err = can_proto_register(&bcm_can_proto);
1868 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1872 /* create /proc/net/can-bcm directory */
1873 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1874 proc_dir = proc_mkdir("can-"IDENT, init_net.proc_net);
1876 proc_dir = proc_mkdir("can-"IDENT, proc_net);
1880 proc_dir->owner = THIS_MODULE;
1885 static void __exit bcm_module_exit(void)
1887 can_proto_unregister(&bcm_can_proto);
1890 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1891 proc_net_remove(&init_net, "can-"IDENT);
1893 proc_net_remove("can-"IDENT);
1897 module_init(bcm_module_init);
1898 module_exit(bcm_module_exit);