2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <socketcan-users@lists.berlios.de>
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/hrtimer.h>
48 #include <linux/list.h>
49 #include <linux/proc_fs.h>
50 #include <linux/uio.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/socket.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <linux/can.h>
57 #include <linux/can/core.h>
58 #include <linux/can/bcm.h>
60 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
61 #include <net/net_namespace.h>
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
67 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
70 /* use of last_frames[index].can_dlc */
71 #define RX_RECV 0x40 /* received data for this element */
72 #define RX_THR 0x80 /* element not been sent due to throttle feature */
73 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
75 /* get best masking value for can_rx_register() for a given single can_id */
76 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
77 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
78 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
80 #define CAN_BCM_VERSION CAN_VERSION
81 static __initdata const char banner[] = KERN_INFO
82 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
84 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
85 MODULE_LICENSE("Dual BSD/GPL");
86 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
88 /* easy access to can_frame payload */
89 static inline u64 GET_U64(const struct can_frame *cp)
91 return *(u64 *)cp->data;
95 struct list_head list;
99 unsigned long frames_abs, frames_filtered;
100 struct timeval ival1, ival2;
101 struct hrtimer timer, thrtimer;
102 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
107 struct can_frame *frames;
108 struct can_frame *last_frames;
109 struct can_frame sframe;
110 struct can_frame last_sframe;
112 struct net_device *rx_reg_dev;
115 static struct proc_dir_entry *proc_dir;
121 struct notifier_block notifier;
122 struct list_head rx_ops;
123 struct list_head tx_ops;
124 unsigned long dropped_usr_msgs;
125 struct proc_dir_entry *bcm_proc_read;
126 char procname [9]; /* pointer printed in ASCII with \0 */
129 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
131 return (struct bcm_sock *)sk;
134 #define CFSIZ sizeof(struct can_frame)
135 #define OPSIZ sizeof(struct bcm_op)
136 #define MHSIZ sizeof(struct bcm_msg_head)
141 static char *bcm_proc_getifname(int ifindex)
143 struct net_device *dev;
148 /* no usage counting */
149 dev = __dev_get_by_index(&init_net, ifindex);
156 static int bcm_read_proc(char *page, char **start, off_t off,
157 int count, int *eof, void *data)
160 struct sock *sk = (struct sock *)data;
161 struct bcm_sock *bo = bcm_sk(sk);
164 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
166 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
167 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
168 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
169 bo->dropped_usr_msgs);
170 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
171 bcm_proc_getifname(bo->ifindex));
172 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
174 list_for_each_entry(op, &bo->rx_ops, list) {
176 unsigned long reduction;
178 /* print only active entries & prevent division by zero */
182 len += snprintf(page + len, PAGE_SIZE - len,
184 op->can_id, bcm_proc_getifname(op->ifindex));
185 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
187 (op->flags & RX_CHECK_DLC)?'d':' ');
188 if (op->kt_ival1.tv64)
189 len += snprintf(page + len, PAGE_SIZE - len,
192 ktime_to_us(op->kt_ival1));
194 if (op->kt_ival2.tv64)
195 len += snprintf(page + len, PAGE_SIZE - len,
198 ktime_to_us(op->kt_ival2));
200 len += snprintf(page + len, PAGE_SIZE - len,
201 "# recv %ld (%ld) => reduction: ",
202 op->frames_filtered, op->frames_abs);
204 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
206 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
207 (reduction == 100)?"near ":"", reduction);
209 if (len > PAGE_SIZE - 200) {
210 /* mark output cut off */
211 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
216 list_for_each_entry(op, &bo->tx_ops, list) {
218 len += snprintf(page + len, PAGE_SIZE - len,
219 "tx_op: %03X %s [%d] ",
220 op->can_id, bcm_proc_getifname(op->ifindex),
223 if (op->kt_ival1.tv64)
224 len += snprintf(page + len, PAGE_SIZE - len, "t1=%lld ",
225 (long long) ktime_to_us(op->kt_ival1));
227 if (op->kt_ival2.tv64)
228 len += snprintf(page + len, PAGE_SIZE - len, "t2=%lld ",
229 (long long) ktime_to_us(op->kt_ival2));
231 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
234 if (len > PAGE_SIZE - 100) {
235 /* mark output cut off */
236 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
241 len += snprintf(page + len, PAGE_SIZE - len, "\n");
248 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
249 * of the given bcm tx op
251 static void bcm_can_tx(struct bcm_op *op)
254 struct net_device *dev;
255 struct can_frame *cf = &op->frames[op->currframe];
257 /* no target device? => exit */
261 dev = dev_get_by_index(&init_net, op->ifindex);
263 /* RFC: should this bcm_op remove itself here? */
267 skb = alloc_skb(CFSIZ, gfp_any());
271 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
273 /* send with loopback */
278 /* update statistics */
282 /* reached last frame? */
283 if (op->currframe >= op->nframes)
290 * bcm_send_to_user - send a BCM message to the userspace
291 * (consisting of bcm_msg_head + x CAN frames)
293 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
294 struct can_frame *frames, int has_timestamp)
297 struct can_frame *firstframe;
298 struct sockaddr_can *addr;
299 struct sock *sk = op->sk;
300 int datalen = head->nframes * CFSIZ;
303 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
307 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
310 /* can_frames starting here */
311 firstframe = (struct can_frame *)skb_tail_pointer(skb);
313 memcpy(skb_put(skb, datalen), frames, datalen);
316 * the BCM uses the can_dlc-element of the can_frame
317 * structure for internal purposes. This is only
318 * relevant for updates that are generated by the
319 * BCM, where nframes is 1
321 if (head->nframes == 1)
322 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
326 /* restore rx timestamp */
327 skb->tstamp = op->rx_stamp;
331 * Put the datagram to the queue so that bcm_recvmsg() can
332 * get it from there. We need to pass the interface index to
333 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
334 * containing the interface index.
337 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
338 addr = (struct sockaddr_can *)skb->cb;
339 memset(addr, 0, sizeof(*addr));
340 addr->can_family = AF_CAN;
341 addr->can_ifindex = op->rx_ifindex;
343 err = sock_queue_rcv_skb(sk, skb);
345 struct bcm_sock *bo = bcm_sk(sk);
348 /* don't care about overflows in this statistic */
349 bo->dropped_usr_msgs++;
354 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
356 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
358 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
359 enum hrtimer_restart ret = HRTIMER_NORESTART;
361 if (op->kt_ival1.tv64 && (op->count > 0)) {
364 if (!op->count && (op->flags & TX_COUNTEVT)) {
365 struct bcm_msg_head msg_head;
367 /* create notification to user */
368 msg_head.opcode = TX_EXPIRED;
369 msg_head.flags = op->flags;
370 msg_head.count = op->count;
371 msg_head.ival1 = op->ival1;
372 msg_head.ival2 = op->ival2;
373 msg_head.can_id = op->can_id;
374 msg_head.nframes = 0;
376 bcm_send_to_user(op, &msg_head, NULL, 0);
380 if (op->kt_ival1.tv64 && (op->count > 0)) {
382 /* send (next) frame */
384 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival1);
385 ret = HRTIMER_RESTART;
388 if (op->kt_ival2.tv64) {
390 /* send (next) frame */
392 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
393 ret = HRTIMER_RESTART;
401 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
403 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
405 struct bcm_msg_head head;
407 /* update statistics */
408 op->frames_filtered++;
410 /* prevent statistics overflow */
411 if (op->frames_filtered > ULONG_MAX/100)
412 op->frames_filtered = op->frames_abs = 0;
414 head.opcode = RX_CHANGED;
415 head.flags = op->flags;
416 head.count = op->count;
417 head.ival1 = op->ival1;
418 head.ival2 = op->ival2;
419 head.can_id = op->can_id;
422 bcm_send_to_user(op, &head, data, 1);
425 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,25)
426 /* is part of linux/hrtimer.h since 2.6.26 */
427 static inline int hrtimer_callback_running(struct hrtimer *timer)
429 return timer->state & HRTIMER_STATE_CALLBACK;
432 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,22)
433 static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
435 return ktime_to_us(ktime_sub(later, earlier));
439 * bcm_rx_update_and_send - process a detected relevant receive content change
440 * 1. update the last received data
441 * 2. send a notification to the user (if possible)
443 static void bcm_rx_update_and_send(struct bcm_op *op,
444 struct can_frame *lastdata,
445 struct can_frame *rxdata)
447 memcpy(lastdata, rxdata, CFSIZ);
450 lastdata->can_dlc |= RX_RECV;
452 /* throtteling mode inactive OR data update already on the run ? */
453 if (!op->kt_ival2.tv64 || hrtimer_callback_running(&op->thrtimer)) {
454 /* send RX_CHANGED to the user immediately */
455 bcm_rx_changed(op, rxdata);
459 if (hrtimer_active(&op->thrtimer)) {
460 /* mark as 'throttled' */
461 lastdata->can_dlc |= RX_THR;
465 if (!op->kt_lastmsg.tv64) {
466 /* send first RX_CHANGED to the user immediately */
467 bcm_rx_changed(op, rxdata);
468 op->kt_lastmsg = ktime_get();
472 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
473 ktime_to_us(op->kt_ival2)) {
474 /* mark as 'throttled' and start timer */
475 lastdata->can_dlc |= RX_THR;
476 hrtimer_start(&op->thrtimer,
477 ktime_add(op->kt_lastmsg, op->kt_ival2),
482 /* the gap was that big, that throttling was not needed here */
483 bcm_rx_changed(op, rxdata);
484 op->kt_lastmsg = ktime_get();
488 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
489 * received data stored in op->last_frames[]
491 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
492 struct can_frame *rxdata)
495 * no one uses the MSBs of can_dlc for comparation,
496 * so we use it here to detect the first time of reception
499 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
500 /* received data for the first time => send update to user */
501 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
505 /* do a real check in can_frame data section */
507 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
508 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
509 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
513 if (op->flags & RX_CHECK_DLC) {
514 /* do a real check in can_frame dlc */
515 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
517 bcm_rx_update_and_send(op, &op->last_frames[index],
525 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
527 static void bcm_rx_starttimer(struct bcm_op *op)
529 if (op->flags & RX_NO_AUTOTIMER)
532 if (op->kt_ival1.tv64)
533 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
537 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
539 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
541 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
542 struct bcm_msg_head msg_head;
544 msg_head.opcode = RX_TIMEOUT;
545 msg_head.flags = op->flags;
546 msg_head.count = op->count;
547 msg_head.ival1 = op->ival1;
548 msg_head.ival2 = op->ival2;
549 msg_head.can_id = op->can_id;
550 msg_head.nframes = 0;
552 bcm_send_to_user(op, &msg_head, NULL, 0);
554 /* no restart of the timer is done here! */
556 /* if user wants to be informed, when cyclic CAN-Messages come back */
557 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
558 /* clear received can_frames to indicate 'nothing received' */
559 memset(op->last_frames, 0, op->nframes * CFSIZ);
562 return HRTIMER_NORESTART;
566 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
568 static int bcm_rx_thr_flush(struct bcm_op *op)
572 if (op->nframes > 1) {
575 /* for MUX filter we start at index 1 */
576 for (i = 1; i < op->nframes; i++) {
577 if ((op->last_frames) &&
578 (op->last_frames[i].can_dlc & RX_THR)) {
579 op->last_frames[i].can_dlc &= ~RX_THR;
580 bcm_rx_changed(op, &op->last_frames[i]);
586 /* for RX_FILTER_ID and simple filter */
587 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
588 op->last_frames[0].can_dlc &= ~RX_THR;
589 bcm_rx_changed(op, &op->last_frames[0]);
598 * bcm_rx_thr_handler - the time for blocked content updates is over now:
599 * Check for throttled data and send it to the userspace
601 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
603 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
605 if (bcm_rx_thr_flush(op)) {
606 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
607 return HRTIMER_RESTART;
609 /* rearm throttle handling */
610 op->kt_lastmsg = ktime_set(0, 0);
611 return HRTIMER_NORESTART;
616 * bcm_rx_handler - handle a CAN frame receiption
618 static void bcm_rx_handler(struct sk_buff *skb, void *data)
620 struct bcm_op *op = (struct bcm_op *)data;
621 struct can_frame rxframe;
624 /* disable timeout */
625 hrtimer_cancel(&op->timer);
627 if (skb->len == sizeof(rxframe)) {
628 memcpy(&rxframe, skb->data, sizeof(rxframe));
629 /* save rx timestamp */
630 op->rx_stamp = skb->tstamp;
631 /* save originator for recvfrom() */
632 op->rx_ifindex = skb->dev->ifindex;
633 /* update statistics */
642 if (op->can_id != rxframe.can_id)
645 if (op->flags & RX_RTR_FRAME) {
646 /* send reply for RTR-request (placed in op->frames[0]) */
651 if (op->flags & RX_FILTER_ID) {
652 /* the easiest case */
653 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
654 bcm_rx_starttimer(op);
658 if (op->nframes == 1) {
659 /* simple compare with index 0 */
660 bcm_rx_cmp_to_index(op, 0, &rxframe);
661 bcm_rx_starttimer(op);
665 if (op->nframes > 1) {
669 * find the first multiplex mask that fits.
670 * Remark: The MUX-mask is stored in index 0
673 for (i = 1; i < op->nframes; i++) {
674 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
675 (GET_U64(&op->frames[0]) &
676 GET_U64(&op->frames[i]))) {
677 bcm_rx_cmp_to_index(op, i, &rxframe);
681 bcm_rx_starttimer(op);
686 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
688 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
693 list_for_each_entry(op, ops, list) {
694 if ((op->can_id == can_id) && (op->ifindex == ifindex))
701 static void bcm_remove_op(struct bcm_op *op)
703 hrtimer_cancel(&op->timer);
704 hrtimer_cancel(&op->thrtimer);
706 if ((op->frames) && (op->frames != &op->sframe))
709 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
710 kfree(op->last_frames);
717 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
719 if (op->rx_reg_dev == dev) {
720 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
723 /* mark as removed subscription */
724 op->rx_reg_dev = NULL;
726 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
727 "mismatch %p %p\n", op->rx_reg_dev, dev);
731 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
733 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
735 struct bcm_op *op, *n;
737 list_for_each_entry_safe(op, n, ops, list) {
738 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
741 * Don't care if we're bound or not (due to netdev
742 * problems) can_rx_unregister() is always a save
747 * Only remove subscriptions that had not
748 * been removed due to NETDEV_UNREGISTER
751 if (op->rx_reg_dev) {
752 struct net_device *dev;
754 dev = dev_get_by_index(&init_net,
757 bcm_rx_unreg(dev, op);
762 can_rx_unregister(NULL, op->can_id,
772 return 0; /* not found */
776 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
778 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
780 struct bcm_op *op, *n;
782 list_for_each_entry_safe(op, n, ops, list) {
783 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
790 return 0; /* not found */
794 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
796 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
799 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
804 /* put current values into msg_head */
805 msg_head->flags = op->flags;
806 msg_head->count = op->count;
807 msg_head->ival1 = op->ival1;
808 msg_head->ival2 = op->ival2;
809 msg_head->nframes = op->nframes;
811 bcm_send_to_user(op, msg_head, op->frames, 0);
817 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
819 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
820 int ifindex, struct sock *sk)
822 struct bcm_sock *bo = bcm_sk(sk);
826 /* we need a real device to send frames */
830 /* we need at least one can_frame */
831 if (msg_head->nframes < 1)
834 /* check the given can_id */
835 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
838 /* update existing BCM operation */
841 * Do we need more space for the can_frames than currently
842 * allocated? -> This is a _really_ unusual use-case and
843 * therefore (complexity / locking) it is not supported.
845 if (msg_head->nframes > op->nframes)
848 /* update can_frames content */
849 for (i = 0; i < msg_head->nframes; i++) {
850 err = memcpy_fromiovec((u8 *)&op->frames[i],
851 msg->msg_iov, CFSIZ);
853 if (op->frames[i].can_dlc > 8)
859 if (msg_head->flags & TX_CP_CAN_ID) {
860 /* copy can_id into frame */
861 op->frames[i].can_id = msg_head->can_id;
866 /* insert new BCM operation for the given can_id */
868 op = kzalloc(OPSIZ, GFP_KERNEL);
872 op->can_id = msg_head->can_id;
874 /* create array for can_frames and copy the data */
875 if (msg_head->nframes > 1) {
876 op->frames = kmalloc(msg_head->nframes * CFSIZ,
883 op->frames = &op->sframe;
885 for (i = 0; i < msg_head->nframes; i++) {
886 err = memcpy_fromiovec((u8 *)&op->frames[i],
887 msg->msg_iov, CFSIZ);
889 if (op->frames[i].can_dlc > 8)
893 if (op->frames != &op->sframe)
899 if (msg_head->flags & TX_CP_CAN_ID) {
900 /* copy can_id into frame */
901 op->frames[i].can_id = msg_head->can_id;
905 /* tx_ops never compare with previous received messages */
906 op->last_frames = NULL;
908 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
910 op->ifindex = ifindex;
912 /* initialize uninitialized (kzalloc) structure */
913 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
914 op->timer.function = bcm_tx_timeout_handler;
916 /* currently unused in tx_ops */
917 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
919 /* add this bcm_op to the list of the tx_ops */
920 list_add(&op->list, &bo->tx_ops);
922 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
924 if (op->nframes != msg_head->nframes) {
925 op->nframes = msg_head->nframes;
926 /* start multiple frame transmission with index 0 */
932 op->flags = msg_head->flags;
934 if (op->flags & TX_RESET_MULTI_IDX) {
935 /* start multiple frame transmission with index 0 */
939 if (op->flags & SETTIMER) {
940 /* set timer values */
941 op->count = msg_head->count;
942 op->ival1 = msg_head->ival1;
943 op->ival2 = msg_head->ival2;
944 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
945 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
947 /* disable an active timer due to zero values? */
948 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
949 hrtimer_cancel(&op->timer);
952 if ((op->flags & STARTTIMER) &&
953 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
955 /* spec: send can_frame when starting timer */
956 op->flags |= TX_ANNOUNCE;
958 if (op->kt_ival1.tv64 && (op->count > 0)) {
959 /* op->count-- is done in bcm_tx_timeout_handler */
960 hrtimer_start(&op->timer, op->kt_ival1,
963 hrtimer_start(&op->timer, op->kt_ival2,
967 if (op->flags & TX_ANNOUNCE)
970 return msg_head->nframes * CFSIZ + MHSIZ;
974 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
976 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
977 int ifindex, struct sock *sk)
979 struct bcm_sock *bo = bcm_sk(sk);
984 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
985 /* be robust against wrong usage ... */
986 msg_head->flags |= RX_FILTER_ID;
987 /* ignore trailing garbage */
988 msg_head->nframes = 0;
991 if ((msg_head->flags & RX_RTR_FRAME) &&
992 ((msg_head->nframes != 1) ||
993 (!(msg_head->can_id & CAN_RTR_FLAG))))
996 /* check the given can_id */
997 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
999 /* update existing BCM operation */
1002 * Do we need more space for the can_frames than currently
1003 * allocated? -> This is a _really_ unusual use-case and
1004 * therefore (complexity / locking) it is not supported.
1006 if (msg_head->nframes > op->nframes)
1009 if (msg_head->nframes) {
1010 /* update can_frames content */
1011 err = memcpy_fromiovec((u8 *)op->frames,
1013 msg_head->nframes * CFSIZ);
1017 /* clear last_frames to indicate 'nothing received' */
1018 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1021 op->nframes = msg_head->nframes;
1023 /* Only an update -> do not call can_rx_register() */
1027 /* insert new BCM operation for the given can_id */
1028 op = kzalloc(OPSIZ, GFP_KERNEL);
1032 op->can_id = msg_head->can_id;
1033 op->nframes = msg_head->nframes;
1035 if (msg_head->nframes > 1) {
1036 /* create array for can_frames and copy the data */
1037 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1044 /* create and init array for received can_frames */
1045 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1047 if (!op->last_frames) {
1054 op->frames = &op->sframe;
1055 op->last_frames = &op->last_sframe;
1058 if (msg_head->nframes) {
1059 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1060 msg_head->nframes * CFSIZ);
1062 if (op->frames != &op->sframe)
1064 if (op->last_frames != &op->last_sframe)
1065 kfree(op->last_frames);
1071 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1073 op->ifindex = ifindex;
1075 /* initialize uninitialized (kzalloc) structure */
1076 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1077 op->timer.function = bcm_rx_timeout_handler;
1079 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1080 op->thrtimer.function = bcm_rx_thr_handler;
1082 /* add this bcm_op to the list of the rx_ops */
1083 list_add(&op->list, &bo->rx_ops);
1085 /* call can_rx_register() */
1088 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1091 op->flags = msg_head->flags;
1093 if (op->flags & RX_RTR_FRAME) {
1095 /* no timers in RTR-mode */
1096 hrtimer_cancel(&op->thrtimer);
1097 hrtimer_cancel(&op->timer);
1100 * funny feature in RX(!)_SETUP only for RTR-mode:
1101 * copy can_id into frame BUT without RTR-flag to
1102 * prevent a full-load-loopback-test ... ;-]
1104 if ((op->flags & TX_CP_CAN_ID) ||
1105 (op->frames[0].can_id == op->can_id))
1106 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1109 if (op->flags & SETTIMER) {
1111 /* set timer value */
1112 op->ival1 = msg_head->ival1;
1113 op->ival2 = msg_head->ival2;
1114 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1115 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1117 /* disable an active timer due to zero value? */
1118 if (!op->kt_ival1.tv64)
1119 hrtimer_cancel(&op->timer);
1122 * In any case cancel the throttle timer, flush
1123 * potentially blocked msgs and reset throttle handling
1125 op->kt_lastmsg = ktime_set(0, 0);
1126 hrtimer_cancel(&op->thrtimer);
1127 bcm_rx_thr_flush(op);
1130 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1131 hrtimer_start(&op->timer, op->kt_ival1,
1135 /* now we can register for can_ids, if we added a new bcm_op */
1136 if (do_rx_register) {
1138 struct net_device *dev;
1140 dev = dev_get_by_index(&init_net, ifindex);
1142 err = can_rx_register(dev, op->can_id,
1143 REGMASK(op->can_id),
1147 op->rx_reg_dev = dev;
1152 err = can_rx_register(NULL, op->can_id,
1153 REGMASK(op->can_id),
1154 bcm_rx_handler, op, "bcm");
1156 /* this bcm rx op is broken -> remove it */
1157 list_del(&op->list);
1163 return msg_head->nframes * CFSIZ + MHSIZ;
1167 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1169 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1171 struct sk_buff *skb;
1172 struct net_device *dev;
1175 /* we need a real device to send frames */
1179 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1184 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1190 dev = dev_get_by_index(&init_net, ifindex);
1198 err = can_send(skb, 1); /* send with loopback */
1204 return CFSIZ + MHSIZ;
1208 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1210 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1211 struct msghdr *msg, size_t size)
1213 struct sock *sk = sock->sk;
1214 struct bcm_sock *bo = bcm_sk(sk);
1215 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1216 struct bcm_msg_head msg_head;
1217 int ret; /* read bytes or error codes as return value */
1222 /* check for valid message length from userspace */
1223 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1226 /* check for alternative ifindex for this bcm_op */
1228 if (!ifindex && msg->msg_name) {
1229 /* no bound device as default => check msg_name */
1230 struct sockaddr_can *addr =
1231 (struct sockaddr_can *)msg->msg_name;
1233 if (addr->can_family != AF_CAN)
1236 /* ifindex from sendto() */
1237 ifindex = addr->can_ifindex;
1240 struct net_device *dev;
1242 dev = dev_get_by_index(&init_net, ifindex);
1246 if (dev->type != ARPHRD_CAN) {
1255 /* read message head information */
1257 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1263 switch (msg_head.opcode) {
1266 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1270 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1274 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1281 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1288 /* reuse msg_head for the reply to TX_READ */
1289 msg_head.opcode = TX_STATUS;
1290 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1294 /* reuse msg_head for the reply to RX_READ */
1295 msg_head.opcode = RX_STATUS;
1296 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1300 /* we need exactly one can_frame behind the msg head */
1301 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1304 ret = bcm_tx_send(msg, ifindex, sk);
1318 * notification handler for netdevice status changes
1320 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1323 struct net_device *dev = (struct net_device *)data;
1324 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1325 struct sock *sk = &bo->sk;
1327 int notify_enodev = 0;
1329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
1330 if (!net_eq(dev_net(dev), &init_net))
1332 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1333 if (dev->nd_net != &init_net)
1337 if (dev->type != ARPHRD_CAN)
1342 case NETDEV_UNREGISTER:
1345 /* remove device specific receive entries */
1346 list_for_each_entry(op, &bo->rx_ops, list)
1347 if (op->rx_reg_dev == dev)
1348 bcm_rx_unreg(dev, op);
1350 /* remove device reference, if this is our bound device */
1351 if (bo->bound && bo->ifindex == dev->ifindex) {
1359 if (notify_enodev) {
1360 sk->sk_err = ENODEV;
1361 if (!sock_flag(sk, SOCK_DEAD))
1362 sk->sk_error_report(sk);
1367 if (bo->bound && bo->ifindex == dev->ifindex) {
1368 sk->sk_err = ENETDOWN;
1369 if (!sock_flag(sk, SOCK_DEAD))
1370 sk->sk_error_report(sk);
1378 * initial settings for all BCM sockets to be set at socket creation time
1380 static int bcm_init(struct sock *sk)
1382 struct bcm_sock *bo = bcm_sk(sk);
1386 bo->dropped_usr_msgs = 0;
1387 bo->bcm_proc_read = NULL;
1389 INIT_LIST_HEAD(&bo->tx_ops);
1390 INIT_LIST_HEAD(&bo->rx_ops);
1393 bo->notifier.notifier_call = bcm_notifier;
1395 register_netdevice_notifier(&bo->notifier);
1401 * standard socket functions
1403 static int bcm_release(struct socket *sock)
1405 struct sock *sk = sock->sk;
1406 struct bcm_sock *bo = bcm_sk(sk);
1407 struct bcm_op *op, *next;
1409 /* remove bcm_ops, timer, rx_unregister(), etc. */
1411 unregister_netdevice_notifier(&bo->notifier);
1415 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1418 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1420 * Don't care if we're bound or not (due to netdev problems)
1421 * can_rx_unregister() is always a save thing to do here.
1425 * Only remove subscriptions that had not
1426 * been removed due to NETDEV_UNREGISTER
1429 if (op->rx_reg_dev) {
1430 struct net_device *dev;
1432 dev = dev_get_by_index(&init_net, op->ifindex);
1434 bcm_rx_unreg(dev, op);
1439 can_rx_unregister(NULL, op->can_id,
1440 REGMASK(op->can_id),
1441 bcm_rx_handler, op);
1446 /* remove procfs entry */
1447 if (proc_dir && bo->bcm_proc_read)
1448 remove_proc_entry(bo->procname, proc_dir);
1450 /* remove device reference */
1462 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1465 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1466 struct sock *sk = sock->sk;
1467 struct bcm_sock *bo = bcm_sk(sk);
1472 /* bind a device to this socket */
1473 if (addr->can_ifindex) {
1474 struct net_device *dev;
1476 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1480 if (dev->type != ARPHRD_CAN) {
1485 bo->ifindex = dev->ifindex;
1489 /* no interface reference for ifindex = 0 ('any' CAN device) */
1496 /* unique socket address as filename */
1497 sprintf(bo->procname, "%p", sock);
1498 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1506 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1507 struct msghdr *msg, size_t size, int flags)
1509 struct sock *sk = sock->sk;
1510 struct sk_buff *skb;
1515 noblock = flags & MSG_DONTWAIT;
1516 flags &= ~MSG_DONTWAIT;
1517 skb = skb_recv_datagram(sk, flags, noblock, &error);
1521 if (skb->len < size)
1524 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1526 skb_free_datagram(sk, skb);
1530 sock_recv_timestamp(msg, sk, skb);
1532 if (msg->msg_name) {
1533 msg->msg_namelen = sizeof(struct sockaddr_can);
1534 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1537 skb_free_datagram(sk, skb);
1542 static struct proto_ops bcm_ops __read_mostly = {
1544 .release = bcm_release,
1545 .bind = sock_no_bind,
1546 .connect = bcm_connect,
1547 .socketpair = sock_no_socketpair,
1548 .accept = sock_no_accept,
1549 .getname = sock_no_getname,
1550 .poll = datagram_poll,
1551 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1552 .listen = sock_no_listen,
1553 .shutdown = sock_no_shutdown,
1554 .setsockopt = sock_no_setsockopt,
1555 .getsockopt = sock_no_getsockopt,
1556 .sendmsg = bcm_sendmsg,
1557 .recvmsg = bcm_recvmsg,
1558 .mmap = sock_no_mmap,
1559 .sendpage = sock_no_sendpage,
1562 static struct proto bcm_proto __read_mostly = {
1564 .owner = THIS_MODULE,
1565 .obj_size = sizeof(struct bcm_sock),
1569 static struct can_proto bcm_can_proto __read_mostly = {
1571 .protocol = CAN_BCM,
1577 static int __init bcm_module_init(void)
1583 err = can_proto_register(&bcm_can_proto);
1585 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1589 /* create /proc/net/can-bcm directory */
1590 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1591 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1593 proc_dir = proc_mkdir("can-bcm", proc_net);
1597 proc_dir->owner = THIS_MODULE;
1602 static void __exit bcm_module_exit(void)
1604 can_proto_unregister(&bcm_can_proto);
1607 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
1608 proc_net_remove(&init_net, "can-bcm");
1610 proc_net_remove("can-bcm");
1614 module_init(bcm_module_init);
1615 module_exit(bcm_module_exit);