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;
127 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
134 struct notifier_block notifier;
135 struct list_head rx_ops;
136 struct list_head tx_ops;
137 unsigned long dropped_usr_msgs;
138 struct proc_dir_entry *bcm_proc_read;
139 char procname [9]; /* pointer printed in ASCII with \0 */
142 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
144 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
145 return (struct bcm_sock *)sk;
147 return (struct bcm_sock *)sk->sk_protinfo;
151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
152 static void *kzalloc(size_t size, unsigned int __nocast flags)
154 void *ret = kmalloc(size, flags);
157 memset(ret, 0, size);
162 static inline void skb_get_timestamp(const struct sk_buff *skb,
163 struct timeval *stamp)
165 stamp->tv_sec = skb->stamp.tv_sec;
166 stamp->tv_usec = skb->stamp.tv_usec;
169 static inline void skb_set_timestamp(struct sk_buff *skb,
170 const struct timeval *stamp)
172 skb->stamp.tv_sec = stamp->tv_sec;
173 skb->stamp.tv_usec = stamp->tv_usec;
177 #define CFSIZ sizeof(struct can_frame)
178 #define OPSIZ sizeof(struct bcm_op)
179 #define MHSIZ sizeof(struct bcm_msg_head)
182 * rounded_tv2jif - calculate jiffies from timeval including optional up
183 * @tv: pointer to timeval
186 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
187 * function is intentionally more relaxed on precise timer ticks to get
188 * exact one jiffy for requested 1000us on a 1000HZ machine.
189 * This code is to be removed when upgrading to kernel hrtimer.
192 * calculated jiffies (max: ULONG_MAX)
194 static unsigned long rounded_tv2jif(const struct timeval *tv)
196 unsigned long sec = tv->tv_sec;
197 unsigned long usec = tv->tv_usec;
200 if (sec > ULONG_MAX / HZ)
203 /* round up to get at least the requested time */
204 usec += 1000000 / HZ - 1;
206 jif = usec / (1000000 / HZ);
208 if (sec * HZ > ULONG_MAX - jif)
211 return jif + sec * HZ;
217 static char *bcm_proc_getifname(int ifindex)
219 struct net_device *dev;
224 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
225 dev = __dev_get_by_index(&init_net, ifindex); /* no usage counting */
227 dev = __dev_get_by_index(ifindex); /* no usage counting */
235 static int bcm_read_proc(char *page, char **start, off_t off,
236 int count, int *eof, void *data)
239 struct sock *sk = (struct sock *)data;
240 struct bcm_sock *bo = bcm_sk(sk);
243 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
245 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
246 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
247 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
248 bo->dropped_usr_msgs);
249 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
250 bcm_proc_getifname(bo->ifindex));
251 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
253 list_for_each_entry(op, &bo->rx_ops, list) {
255 unsigned long reduction;
257 /* print only active entries & prevent division by zero */
261 len += snprintf(page + len, PAGE_SIZE - len,
263 op->can_id, bcm_proc_getifname(op->ifindex));
264 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
266 (op->flags & RX_CHECK_DLC)?'d':' ');
268 len += snprintf(page + len, PAGE_SIZE - len,
269 "timeo=%ld ", op->j_ival1);
272 len += snprintf(page + len, PAGE_SIZE - len,
273 "thr=%ld ", op->j_ival2);
275 len += snprintf(page + len, PAGE_SIZE - len,
276 "# recv %ld (%ld) => reduction: ",
277 op->frames_filtered, op->frames_abs);
279 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
281 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
282 (reduction == 100)?"near ":"", reduction);
284 if (len > PAGE_SIZE - 200) {
285 /* mark output cut off */
286 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
291 list_for_each_entry(op, &bo->tx_ops, list) {
293 len += snprintf(page + len, PAGE_SIZE - len,
294 "tx_op: %03X %s [%d] ",
295 op->can_id, bcm_proc_getifname(op->ifindex),
298 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
302 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
305 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
308 if (len > PAGE_SIZE - 100) {
309 /* mark output cut off */
310 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
315 len += snprintf(page + len, PAGE_SIZE - len, "\n");
322 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
323 * of the given bcm tx op
325 static void bcm_can_tx(struct bcm_op *op)
328 struct net_device *dev;
329 struct can_frame *cf = &op->frames[op->currframe];
331 DBG_FRAME("BCM: bcm_can_tx: sending frame", cf);
333 /* no target device? => exit */
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
338 dev = dev_get_by_index(&init_net, op->ifindex);
340 dev = dev_get_by_index(op->ifindex);
343 /* RFC: should this bcm_op remove itself here? */
347 skb = alloc_skb(CFSIZ,
348 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
353 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
355 /* send with loopback */
360 /* update statistics */
364 /* reached last frame? */
365 if (op->currframe >= op->nframes)
372 * bcm_send_to_user - send a BCM message to the userspace
373 * (consisting of bcm_msg_head + x CAN frames)
375 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
376 struct can_frame *frames, int has_timestamp)
379 struct can_frame *firstframe;
380 struct sockaddr_can *addr;
381 struct sock *sk = op->sk;
382 int datalen = head->nframes * CFSIZ;
385 skb = alloc_skb(sizeof(*head) + datalen,
386 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
390 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
393 /* can_frames starting here */
394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
395 firstframe = (struct can_frame *) skb_tail_pointer(skb);
397 firstframe = (struct can_frame *) skb->tail;
400 memcpy(skb_put(skb, datalen), frames, datalen);
403 * the BCM uses the can_dlc-element of the can_frame
404 * structure for internal purposes. This is only
405 * relevant for updates that are generated by the
406 * BCM, where nframes is 1
408 if (head->nframes == 1)
409 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
413 /* restore rx timestamp */
414 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
415 skb->tstamp = op->rx_stamp;
417 skb_set_timestamp(skb, &op->rx_stamp);
422 * Put the datagram to the queue so that bcm_recvmsg() can
423 * get it from there. We need to pass the interface index to
424 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
425 * containing the interface index.
428 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
429 addr = (struct sockaddr_can *)skb->cb;
430 memset(addr, 0, sizeof(*addr));
431 addr->can_family = AF_CAN;
432 addr->can_ifindex = op->rx_ifindex;
434 err = sock_queue_rcv_skb(sk, skb);
436 struct bcm_sock *bo = bcm_sk(sk);
438 DBG("sock_queue_rcv_skb failed: %d\n", err);
440 /* don't care about overflows in this statistic */
441 bo->dropped_usr_msgs++;
446 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
448 static void bcm_tx_timeout_handler(unsigned long data)
450 struct bcm_op *op = (struct bcm_op *)data;
452 DBG("Called with bcm_op %p\n", op);
454 if (op->j_ival1 && (op->count > 0)) {
457 if (!op->count && (op->flags & TX_COUNTEVT)) {
458 struct bcm_msg_head msg_head;
460 /* create notification to user */
461 DBG("sending TX_EXPIRED for can_id %03X\n",
464 msg_head.opcode = TX_EXPIRED;
465 msg_head.flags = op->flags;
466 msg_head.count = op->count;
467 msg_head.ival1 = op->ival1;
468 msg_head.ival2 = op->ival2;
469 msg_head.can_id = op->can_id;
470 msg_head.nframes = 0;
472 bcm_send_to_user(op, &msg_head, NULL, 0);
476 DBG("count=%d j_ival1=%ld j_ival2=%ld\n",
477 op->count, op->j_ival1, op->j_ival2);
479 if (op->j_ival1 && (op->count > 0)) {
481 op->timer.expires = jiffies + op->j_ival1;
482 add_timer(&op->timer);
484 DBG("adding timer ival1. func=%p data=%p exp=0x%08X\n",
486 (char *) op->timer.data,
487 (unsigned int) op->timer.expires);
489 /* send (next) frame */
494 op->timer.expires = jiffies + op->j_ival2;
495 add_timer(&op->timer);
497 DBG("adding timer ival2. func=%p data=%p exp=0x%08X\n",
499 (char *) op->timer.data,
500 (unsigned int) op->timer.expires);
502 /* send (next) frame */
506 DBG("no timer restart\n");
513 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
515 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
517 struct bcm_msg_head head;
519 op->j_lastmsg = jiffies;
521 /* update statistics */
522 op->frames_filtered++;
524 /* prevent statistics overflow */
525 if (op->frames_filtered > ULONG_MAX/100)
526 op->frames_filtered = op->frames_abs = 0;
528 DBG("setting j_lastmsg to 0x%08X for rx_op %p\n",
529 (unsigned int) op->j_lastmsg, op);
530 DBG("sending notification\n");
532 head.opcode = RX_CHANGED;
533 head.flags = op->flags;
534 head.count = op->count;
535 head.ival1 = op->ival1;
536 head.ival2 = op->ival2;
537 head.can_id = op->can_id;
540 bcm_send_to_user(op, &head, data, 1);
544 * bcm_rx_update_and_send - process a detected relevant receive content change
545 * 1. update the last received data
546 * 2. send a notification to the user (if possible)
548 static void bcm_rx_update_and_send(struct bcm_op *op,
549 struct can_frame *lastdata,
550 struct can_frame *rxdata)
552 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
554 memcpy(lastdata, rxdata, CFSIZ);
557 lastdata->can_dlc |= RX_RECV;
559 /* throttle bcm_rx_changed ? */
560 if ((op->thrtimer.expires) ||
561 ((op->j_ival2) && (nexttx > jiffies))) {
562 /* we are already waiting OR we have to start waiting */
564 /* mark as 'throttled' */
565 lastdata->can_dlc |= RX_THR;
567 if (!(op->thrtimer.expires)) {
568 /* start the timer only the first time */
569 op->thrtimer.expires = nexttx;
570 add_timer(&op->thrtimer);
572 DBG("adding thrtimer. func=%p data=%p exp=0x%08X\n",
573 op->thrtimer.function,
574 (char *) op->thrtimer.data,
575 (unsigned int) op->thrtimer.expires);
579 /* send RX_CHANGED to the user immediately */
580 bcm_rx_changed(op, rxdata);
585 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
586 * received data stored in op->last_frames[]
588 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
589 struct can_frame *rxdata)
592 * no one uses the MSBs of can_dlc for comparation,
593 * so we use it here to detect the first time of reception
596 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
597 /* received data for the first time => send update to user */
598 DBG("first time :)\n");
599 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
603 /* do a real check in can_frame data section */
605 DBG("op->frames[index].data = 0x%016llx\n",
606 GET_U64(&op->frames[index]));
607 DBG("op->last_frames[index].data = 0x%016llx\n",
608 GET_U64(&op->last_frames[index]));
609 DBG("rxdata->data = 0x%016llx\n", GET_U64(rxdata));
611 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
612 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
613 DBG("relevant data change :)\n");
614 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
618 if (op->flags & RX_CHECK_DLC) {
619 /* do a real check in can_frame dlc */
620 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
622 DBG("dlc change :)\n");
623 bcm_rx_update_and_send(op, &op->last_frames[index],
628 DBG("no relevant change :(\n");
632 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
634 static void bcm_rx_starttimer(struct bcm_op *op)
636 if (op->flags & RX_NO_AUTOTIMER)
640 op->timer.expires = jiffies + op->j_ival1;
642 DBG("adding rx timeout timer ival1. func=%p data=%p "
645 (char *) op->timer.data,
646 (unsigned int) op->timer.expires);
648 add_timer(&op->timer);
653 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
655 static void bcm_rx_timeout_handler(unsigned long data)
657 struct bcm_op *op = (struct bcm_op *)data;
658 struct bcm_msg_head msg_head;
660 DBG("sending RX_TIMEOUT for can_id %03X. op is %p\n", op->can_id, op);
662 msg_head.opcode = RX_TIMEOUT;
663 msg_head.flags = op->flags;
664 msg_head.count = op->count;
665 msg_head.ival1 = op->ival1;
666 msg_head.ival2 = op->ival2;
667 msg_head.can_id = op->can_id;
668 msg_head.nframes = 0;
670 bcm_send_to_user(op, &msg_head, NULL, 0);
672 /* no restart of the timer is done here! */
674 /* if user wants to be informed, when cyclic CAN-Messages come back */
675 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
676 /* clear received can_frames to indicate 'nothing received' */
677 memset(op->last_frames, 0, op->nframes * CFSIZ);
678 DBG("RX_ANNOUNCE_RESTART\n");
683 * bcm_rx_thr_handler - the time for blocked content updates is over now:
684 * Check for throttled data and send it to the userspace
686 static void bcm_rx_thr_handler(unsigned long data)
688 struct bcm_op *op = (struct bcm_op *)data;
691 /* mark disabled / consumed timer */
692 op->thrtimer.expires = 0;
694 if (op->nframes > 1) {
695 DBG("sending MUX RX_CHANGED for can_id %03X. op is %p\n",
697 /* for MUX filter we start at index 1 */
698 for (i = 1; i < op->nframes; i++) {
699 if ((op->last_frames) &&
700 (op->last_frames[i].can_dlc & RX_THR)) {
701 op->last_frames[i].can_dlc &= ~RX_THR;
702 bcm_rx_changed(op, &op->last_frames[i]);
707 DBG("sending simple RX_CHANGED for can_id %03X. op is %p\n",
709 /* for RX_FILTER_ID and simple filter */
710 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
711 op->last_frames[0].can_dlc &= ~RX_THR;
712 bcm_rx_changed(op, &op->last_frames[0]);
718 * bcm_rx_handler - handle a CAN frame receiption
720 static void bcm_rx_handler(struct sk_buff *skb, void *data)
722 struct bcm_op *op = (struct bcm_op *)data;
723 struct can_frame rxframe;
726 /* disable timeout */
727 del_timer(&op->timer);
729 DBG("Called with bcm_op %p\n", op);
731 if (skb->len == sizeof(rxframe)) {
732 memcpy(&rxframe, skb->data, sizeof(rxframe));
733 /* save rx timestamp */
734 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
735 op->rx_stamp = skb->tstamp;
737 skb_get_timestamp(skb, &op->rx_stamp);
739 /* save originator for recvfrom() */
740 op->rx_ifindex = skb->dev->ifindex;
741 /* update statistics */
744 DBG("got can_frame with can_id %03X\n", rxframe.can_id);
747 DBG("Wrong skb->len = %d\n", skb->len);
752 DBG_FRAME("BCM: bcm_rx_handler: CAN frame", &rxframe);
754 if (op->can_id != rxframe.can_id) {
755 DBG("ERROR! Got wrong can_id %03X! Expected %03X.\n",
756 rxframe.can_id, op->can_id);
760 if (op->flags & RX_RTR_FRAME) {
761 /* send reply for RTR-request */
762 DBG("RTR-request\n");
764 /* send op->frames[0] to CAN device */
769 if (op->flags & RX_FILTER_ID) {
770 /* the easiest case */
771 DBG("Easy does it with RX_FILTER_ID\n");
773 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
774 bcm_rx_starttimer(op);
778 if (op->nframes == 1) {
779 /* simple compare with index 0 */
780 DBG("Simple compare\n");
782 bcm_rx_cmp_to_index(op, 0, &rxframe);
783 bcm_rx_starttimer(op);
787 if (op->nframes > 1) {
788 /* multiplex compare */
789 DBG("Multiplex compare\n");
792 * find the first multiplex mask that fits.
793 * Remark: The MUX-mask is stored in index 0
796 for (i = 1; i < op->nframes; i++) {
797 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
798 (GET_U64(&op->frames[0]) &
799 GET_U64(&op->frames[i]))) {
800 DBG("found MUX index %d\n", i);
801 bcm_rx_cmp_to_index(op, i, &rxframe);
805 bcm_rx_starttimer(op);
810 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
812 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
817 list_for_each_entry(op, ops, list) {
818 if ((op->can_id == can_id) && (op->ifindex == ifindex))
825 static void bcm_remove_op(struct bcm_op *op)
827 del_timer(&op->timer);
828 del_timer(&op->thrtimer);
830 if ((op->frames) && (op->frames != &op->sframe))
833 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
834 kfree(op->last_frames);
841 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
843 if (op->rx_reg_dev == dev) {
844 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
847 /* mark as removed subscription */
848 op->rx_reg_dev = NULL;
850 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
851 "mismatch %p %p\n", op->rx_reg_dev, dev);
855 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
857 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
859 struct bcm_op *op, *n;
861 list_for_each_entry_safe(op, n, ops, list) {
862 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
863 DBG("removing rx_op %p for can_id %03X\n",
867 * Don't care if we're bound or not (due to netdev
868 * problems) can_rx_unregister() is always a save
873 * Only remove subscriptions that had not
874 * been removed due to NETDEV_UNREGISTER
877 if (op->rx_reg_dev) {
878 struct net_device *dev;
880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
881 dev = dev_get_by_index(&init_net,
884 dev = dev_get_by_index(op->ifindex);
887 bcm_rx_unreg(dev, op);
892 can_rx_unregister(NULL, op->can_id,
902 return 0; /* not found */
906 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
908 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
910 struct bcm_op *op, *n;
912 list_for_each_entry_safe(op, n, ops, list) {
913 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
914 DBG("removing rx_op %p for can_id %03X\n",
922 return 0; /* not found */
926 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
928 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
931 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
934 DBG("TRX_READ: did not find op for can_id %03X\n",
939 DBG("TRX_READ: sending status for can_id %03X\n",
941 /* put current values into msg_head */
942 msg_head->flags = op->flags;
943 msg_head->count = op->count;
944 msg_head->ival1 = op->ival1;
945 msg_head->ival2 = op->ival2;
946 msg_head->nframes = op->nframes;
948 bcm_send_to_user(op, msg_head, op->frames, 0);
954 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
956 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
957 int ifindex, struct sock *sk)
959 struct bcm_sock *bo = bcm_sk(sk);
963 /* we need a real device to send frames */
967 /* we need at least one can_frame */
968 if (msg_head->nframes < 1)
971 /* check the given can_id */
972 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
975 /* update existing BCM operation */
977 DBG("TX_SETUP: modifying existing tx_op %p for can_id %03X\n",
978 op, msg_head->can_id);
981 * Do we need more space for the can_frames than currently
982 * allocated? -> This is a _really_ unusual use-case and
983 * therefore (complexity / locking) it is not supported.
985 if (msg_head->nframes > op->nframes)
988 /* update can_frames content */
989 for (i = 0; i < msg_head->nframes; i++) {
990 err = memcpy_fromiovec((u8 *)&op->frames[i],
991 msg->msg_iov, CFSIZ);
995 if (msg_head->flags & TX_CP_CAN_ID) {
996 /* copy can_id into frame */
997 op->frames[i].can_id = msg_head->can_id;
1002 /* insert new BCM operation for the given can_id */
1004 op = kzalloc(OPSIZ, GFP_KERNEL);
1008 DBG("TX_SETUP: creating new tx_op %p for can_id %03X\n",
1009 op, msg_head->can_id);
1011 op->can_id = msg_head->can_id;
1013 /* create array for can_frames and copy the data */
1014 if (msg_head->nframes > 1) {
1015 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1022 op->frames = &op->sframe;
1024 for (i = 0; i < msg_head->nframes; i++) {
1025 err = memcpy_fromiovec((u8 *)&op->frames[i],
1026 msg->msg_iov, CFSIZ);
1028 if (op->frames != &op->sframe)
1034 if (msg_head->flags & TX_CP_CAN_ID) {
1035 /* copy can_id into frame */
1036 op->frames[i].can_id = msg_head->can_id;
1040 /* tx_ops never compare with previous received messages */
1041 op->last_frames = NULL;
1043 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1046 op->ifindex = ifindex;
1048 /* initialize uninitialized (kmalloc) structure */
1049 init_timer(&op->timer);
1051 /* currently unused in tx_ops */
1052 init_timer(&op->thrtimer);
1054 /* handler for tx_ops */
1055 op->timer.function = bcm_tx_timeout_handler;
1057 /* timer.data points to this op-structure */
1058 op->timer.data = (unsigned long)op;
1060 /* add this bcm_op to the list of the tx_ops */
1061 list_add(&op->list, &bo->tx_ops);
1063 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1065 if (op->nframes != msg_head->nframes) {
1066 op->nframes = msg_head->nframes;
1067 /* start multiple frame transmission with index 0 */
1073 op->flags = msg_head->flags;
1075 if (op->flags & TX_RESET_MULTI_IDX) {
1076 /* start multiple frame transmission with index 0 */
1080 if (op->flags & SETTIMER) {
1081 /* set timer values */
1083 op->count = msg_head->count;
1084 op->ival1 = msg_head->ival1;
1085 op->ival2 = msg_head->ival2;
1086 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1087 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1089 DBG("TX_SETUP: SETTIMER count=%d j_ival1=%ld j_ival2=%ld\n",
1090 op->count, op->j_ival1, op->j_ival2);
1092 /* disable an active timer due to zero values? */
1093 if (!op->j_ival1 && !op->j_ival2) {
1094 del_timer(&op->timer);
1095 DBG("TX_SETUP: SETTIMER disabled timer.\n");
1099 if ((op->flags & STARTTIMER) &&
1100 ((op->j_ival1 && op->count) || op->j_ival2)) {
1102 del_timer(&op->timer);
1104 /* spec: send can_frame when starting timer */
1105 op->flags |= TX_ANNOUNCE;
1107 if (op->j_ival1 && (op->count > 0)) {
1108 op->timer.expires = jiffies + op->j_ival1;
1109 /* op->count-- is done in bcm_tx_timeout_handler */
1110 DBG("TX_SETUP: adding timer ival1. func=%p data=%p "
1113 (char *) op->timer.data,
1114 (unsigned int) op->timer.expires);
1117 op->timer.expires = jiffies + op->j_ival2;
1118 DBG("TX_SETUP: adding timer ival2. func=%p data=%p "
1121 (char *) op->timer.data,
1122 (unsigned int) op->timer.expires);
1125 add_timer(&op->timer);
1128 if (op->flags & TX_ANNOUNCE)
1131 return msg_head->nframes * CFSIZ + MHSIZ;
1135 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1137 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1138 int ifindex, struct sock *sk)
1140 struct bcm_sock *bo = bcm_sk(sk);
1145 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1146 /* be robust against wrong usage ... */
1147 msg_head->flags |= RX_FILTER_ID;
1148 msg_head->nframes = 0; /* ignore trailing garbage */
1151 if ((msg_head->flags & RX_RTR_FRAME) &&
1152 ((msg_head->nframes != 1) ||
1153 (!(msg_head->can_id & CAN_RTR_FLAG)))) {
1155 DBG("RX_SETUP: bad RX_RTR_FRAME setup!\n");
1159 /* check the given can_id */
1160 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1162 /* update existing BCM operation */
1164 DBG("RX_SETUP: modifying existing rx_op %p for can_id %03X\n",
1165 op, msg_head->can_id);
1168 * Do we need more space for the can_frames than currently
1169 * allocated? -> This is a _really_ unusual use-case and
1170 * therefore (complexity / locking) it is not supported.
1172 if (msg_head->nframes > op->nframes)
1175 if (msg_head->nframes) {
1176 /* update can_frames content */
1177 err = memcpy_fromiovec((u8 *)op->frames,
1179 msg_head->nframes * CFSIZ);
1183 /* clear last_frames to indicate 'nothing received' */
1184 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1187 op->nframes = msg_head->nframes;
1189 /* Only an update -> do not call can_rx_register() */
1193 /* insert new BCM operation for the given can_id */
1195 op = kzalloc(OPSIZ, GFP_KERNEL);
1199 DBG("RX_SETUP: creating new rx_op %p for can_id %03X\n",
1200 op, msg_head->can_id);
1202 op->can_id = msg_head->can_id;
1203 op->nframes = msg_head->nframes;
1205 if (msg_head->nframes > 1) {
1206 /* create array for can_frames and copy the data */
1207 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1214 /* create and init array for received can_frames */
1215 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1217 if (!op->last_frames) {
1224 op->frames = &op->sframe;
1225 op->last_frames = &op->last_sframe;
1228 if (msg_head->nframes) {
1229 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1230 msg_head->nframes * CFSIZ);
1232 if (op->frames != &op->sframe)
1234 if (op->last_frames != &op->last_sframe)
1235 kfree(op->last_frames);
1242 op->ifindex = ifindex;
1244 /* initialize uninitialized (kzalloc) structure */
1245 init_timer(&op->timer);
1247 /* init throttle timer for RX_CHANGED */
1248 init_timer(&op->thrtimer);
1250 /* handler for rx timeouts */
1251 op->timer.function = bcm_rx_timeout_handler;
1253 /* timer.data points to this op-structure */
1254 op->timer.data = (unsigned long)op;
1256 /* handler for RX_CHANGED throttle timeouts */
1257 op->thrtimer.function = bcm_rx_thr_handler;
1259 /* timer.data points to this op-structure */
1260 op->thrtimer.data = (unsigned long)op;
1262 /* mark disabled timer */
1263 op->thrtimer.expires = 0;
1265 /* add this bcm_op to the list of the rx_ops */
1266 list_add(&op->list, &bo->rx_ops);
1268 /* call can_rx_register() */
1271 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1274 op->flags = msg_head->flags;
1276 if (op->flags & RX_RTR_FRAME) {
1278 /* no timers in RTR-mode */
1279 del_timer(&op->thrtimer);
1280 del_timer(&op->timer);
1283 * funny feature in RX(!)_SETUP only for RTR-mode:
1284 * copy can_id into frame BUT without RTR-flag to
1285 * prevent a full-load-loopback-test ... ;-]
1287 if ((op->flags & TX_CP_CAN_ID) ||
1288 (op->frames[0].can_id == op->can_id))
1289 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1292 if (op->flags & SETTIMER) {
1294 /* set timer value */
1295 op->ival1 = msg_head->ival1;
1296 op->ival2 = msg_head->ival2;
1297 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1298 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1300 DBG("RX_SETUP: SETTIMER j_ival1=%ld j_ival2=%ld\n",
1301 op->j_ival1, op->j_ival2);
1303 /* disable an active timer due to zero value? */
1305 del_timer(&op->timer);
1306 DBG("RX_SETUP: disabled timer rx timeouts.\n");
1309 /* free currently blocked msgs ? */
1310 if (op->thrtimer.expires) {
1311 DBG("RX_SETUP: unblocking throttled msgs.\n");
1312 del_timer(&op->thrtimer);
1313 /* send blocked msgs hereafter */
1314 op->thrtimer.expires = jiffies + 2;
1315 add_timer(&op->thrtimer);
1318 * if (op->j_ival2) is zero, no (new) throttling
1319 * will happen. For details see functions
1320 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1324 if ((op->flags & STARTTIMER) && op->j_ival1) {
1326 del_timer(&op->timer);
1327 op->timer.expires = jiffies + op->j_ival1;
1329 DBG("RX_SETUP: adding timer ival1. func=%p data=%p"
1331 (char *) op->timer.function,
1332 (char *) op->timer.data,
1333 (unsigned int) op->timer.expires);
1335 add_timer(&op->timer);
1339 /* now we can register for can_ids, if we added a new bcm_op */
1340 if (do_rx_register) {
1341 DBG("RX_SETUP: can_rx_register() for can_id %03X. "
1342 "rx_op is %p\n", op->can_id, op);
1345 struct net_device *dev;
1347 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1348 dev = dev_get_by_index(&init_net, ifindex);
1350 dev = dev_get_by_index(ifindex);
1353 err = can_rx_register(dev, op->can_id,
1354 REGMASK(op->can_id),
1358 op->rx_reg_dev = dev;
1363 err = can_rx_register(NULL, op->can_id,
1364 REGMASK(op->can_id),
1365 bcm_rx_handler, op, IDENT);
1367 /* this bcm rx op is broken -> remove it */
1368 list_del(&op->list);
1374 return msg_head->nframes * CFSIZ + MHSIZ;
1378 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1380 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1382 struct sk_buff *skb;
1383 struct net_device *dev;
1386 /* just copy and send one can_frame */
1388 if (!ifindex) /* we need a real device to send frames */
1391 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1396 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1402 DBG_FRAME("BCM: TX_SEND: sending frame",
1403 (struct can_frame *)skb->data);
1405 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1406 dev = dev_get_by_index(&init_net, ifindex);
1408 dev = dev_get_by_index(ifindex);
1417 can_send(skb, 1); /* send with loopback */
1420 return CFSIZ + MHSIZ;
1424 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1426 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1427 struct msghdr *msg, size_t size)
1429 struct sock *sk = sock->sk;
1430 struct bcm_sock *bo = bcm_sk(sk);
1431 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1432 struct bcm_msg_head msg_head;
1433 int ret; /* read bytes or error codes as return value */
1436 DBG("sock %p not bound\n", sk);
1440 /* check for alternative ifindex for this bcm_op */
1442 if (!ifindex && msg->msg_name) {
1443 /* no bound device as default => check msg_name */
1444 struct sockaddr_can *addr =
1445 (struct sockaddr_can *)msg->msg_name;
1447 if (addr->can_family != AF_CAN)
1450 ifindex = addr->can_ifindex; /* ifindex from sendto() */
1453 struct net_device *dev;
1455 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1456 dev = dev_get_by_index(&init_net, ifindex);
1458 dev = dev_get_by_index(ifindex);
1461 DBG("device %d not found\n", ifindex);
1465 if (dev->type != ARPHRD_CAN) {
1466 DBG("device %d no CAN device\n", ifindex);
1475 /* read message head information */
1477 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1481 DBG("opcode %d for can_id %03X\n", msg_head.opcode, msg_head.can_id);
1485 switch (msg_head.opcode) {
1488 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1492 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1496 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1503 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1510 /* reuse msg_head for the reply to TX_READ */
1511 msg_head.opcode = TX_STATUS;
1512 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1516 /* reuse msg_head for the reply to RX_READ */
1517 msg_head.opcode = RX_STATUS;
1518 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1522 /* we need at least one can_frame */
1523 if (msg_head.nframes < 1)
1526 ret = bcm_tx_send(msg, ifindex, sk);
1530 DBG("Unknown opcode %d\n", msg_head.opcode);
1541 * notification handler for netdevice status changes
1543 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1546 struct net_device *dev = (struct net_device *)data;
1547 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1548 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1549 struct sock *sk = &bo->sk;
1551 struct sock *sk = bo->sk;
1554 int notify_enodev = 0;
1556 DBG("msg %ld for dev %p (%s idx %d) sk %p bo->ifindex %d\n",
1557 msg, dev, dev->name, dev->ifindex, sk, bo->ifindex);
1559 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1560 if (dev->nd_net != &init_net)
1564 if (dev->type != ARPHRD_CAN)
1569 case NETDEV_UNREGISTER:
1572 /* remove device specific receive entries */
1573 list_for_each_entry(op, &bo->rx_ops, list)
1574 if (op->rx_reg_dev == dev)
1575 bcm_rx_unreg(dev, op);
1577 /* remove device reference, if this is our bound device */
1578 if (bo->bound && bo->ifindex == dev->ifindex) {
1586 if (notify_enodev) {
1587 sk->sk_err = ENODEV;
1588 if (!sock_flag(sk, SOCK_DEAD))
1589 sk->sk_error_report(sk);
1594 if (bo->bound && bo->ifindex == dev->ifindex) {
1595 sk->sk_err = ENETDOWN;
1596 if (!sock_flag(sk, SOCK_DEAD))
1597 sk->sk_error_report(sk);
1605 * initial settings for all BCM sockets to be set at socket creation time
1607 static int bcm_init(struct sock *sk)
1609 struct bcm_sock *bo = bcm_sk(sk);
1611 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
1616 bo->dropped_usr_msgs = 0;
1617 bo->bcm_proc_read = NULL;
1619 INIT_LIST_HEAD(&bo->tx_ops);
1620 INIT_LIST_HEAD(&bo->rx_ops);
1623 bo->notifier.notifier_call = bcm_notifier;
1625 register_netdevice_notifier(&bo->notifier);
1631 * standard socket functions
1633 static int bcm_release(struct socket *sock)
1635 struct sock *sk = sock->sk;
1636 struct bcm_sock *bo = bcm_sk(sk);
1637 struct bcm_op *op, *next;
1639 DBG("socket %p, sk %p\n", sock, sk);
1641 /* remove bcm_ops, timer, rx_unregister(), etc. */
1643 unregister_netdevice_notifier(&bo->notifier);
1647 list_for_each_entry_safe(op, next, &bo->tx_ops, list) {
1648 DBG("removing tx_op %p for can_id %03X\n", op, op->can_id);
1652 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1653 DBG("removing rx_op %p for can_id %03X\n", op, op->can_id);
1656 * Don't care if we're bound or not (due to netdev problems)
1657 * can_rx_unregister() is always a save thing to do here.
1661 * Only remove subscriptions that had not
1662 * been removed due to NETDEV_UNREGISTER
1665 if (op->rx_reg_dev) {
1666 struct net_device *dev;
1668 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1669 dev = dev_get_by_index(&init_net, op->ifindex);
1671 dev = dev_get_by_index(op->ifindex);
1674 bcm_rx_unreg(dev, op);
1679 can_rx_unregister(NULL, op->can_id,
1680 REGMASK(op->can_id),
1681 bcm_rx_handler, op);
1686 /* remove procfs entry */
1687 if (proc_dir && bo->bcm_proc_read)
1688 remove_proc_entry(bo->procname, proc_dir);
1690 /* remove device reference */
1702 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1705 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1706 struct sock *sk = sock->sk;
1707 struct bcm_sock *bo = bcm_sk(sk);
1712 /* bind a device to this socket */
1713 if (addr->can_ifindex) {
1714 struct net_device *dev;
1716 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1717 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1719 dev = dev_get_by_index(addr->can_ifindex);
1722 DBG("could not find device index %d\n",
1727 if (dev->type != ARPHRD_CAN) {
1728 DBG("device %d no CAN device\n", addr->can_ifindex);
1733 bo->ifindex = dev->ifindex;
1736 DBG("socket %p bound to device %s (idx %d)\n",
1737 sock, dev->name, dev->ifindex);
1740 /* no interface reference for ifindex = 0 ('any' CAN device) */
1747 /* unique socket address as filename */
1748 sprintf(bo->procname, "%p", sock);
1749 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1757 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1758 struct msghdr *msg, size_t size, int flags)
1760 struct sock *sk = sock->sk;
1761 struct sk_buff *skb;
1766 DBG("socket %p, sk %p\n", sock, sk);
1768 noblock = flags & MSG_DONTWAIT;
1769 flags &= ~MSG_DONTWAIT;
1770 skb = skb_recv_datagram(sk, flags, noblock, &error);
1774 DBG("delivering skbuff %p\n", skb);
1777 if (skb->len < size)
1780 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1782 skb_free_datagram(sk, skb);
1786 sock_recv_timestamp(msg, sk, skb);
1788 if (msg->msg_name) {
1789 msg->msg_namelen = sizeof(struct sockaddr_can);
1790 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1793 DBG("freeing sock %p, skbuff %p\n", sk, skb);
1794 skb_free_datagram(sk, skb);
1799 static unsigned int bcm_poll(struct file *file, struct socket *sock,
1802 unsigned int mask = 0;
1804 DBG("socket %p\n", sock);
1806 mask = datagram_poll(file, sock, wait);
1810 static struct proto_ops bcm_ops __read_mostly = {
1812 .release = bcm_release,
1813 .bind = sock_no_bind,
1814 .connect = bcm_connect,
1815 .socketpair = sock_no_socketpair,
1816 .accept = sock_no_accept,
1817 .getname = sock_no_getname,
1819 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1820 .listen = sock_no_listen,
1821 .shutdown = sock_no_shutdown,
1822 .setsockopt = sock_no_setsockopt,
1823 .getsockopt = sock_no_getsockopt,
1824 .sendmsg = bcm_sendmsg,
1825 .recvmsg = bcm_recvmsg,
1826 .mmap = sock_no_mmap,
1827 .sendpage = sock_no_sendpage,
1830 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
1831 static struct proto bcm_proto __read_mostly = {
1833 .owner = THIS_MODULE,
1834 .obj_size = sizeof(struct bcm_sock),
1838 static struct can_proto bcm_can_proto __read_mostly = {
1840 .protocol = CAN_BCM,
1846 static struct can_proto bcm_can_proto __read_mostly = {
1848 .protocol = CAN_BCM,
1851 .owner = THIS_MODULE,
1852 .obj_size = sizeof(struct bcm_sock),
1857 static int __init bcm_module_init(void)
1863 err = can_proto_register(&bcm_can_proto);
1865 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1869 /* create /proc/net/can-bcm directory */
1870 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1871 proc_dir = proc_mkdir("can-"IDENT, init_net.proc_net);
1873 proc_dir = proc_mkdir("can-"IDENT, proc_net);
1877 proc_dir->owner = THIS_MODULE;
1882 static void __exit bcm_module_exit(void)
1884 can_proto_unregister(&bcm_can_proto);
1887 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1888 proc_net_remove(&init_net, "can-"IDENT);
1890 proc_net_remove("can-"IDENT);
1894 module_init(bcm_module_init);
1895 module_exit(bcm_module_exit);