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/init.h>
46 #include <linux/list.h>
47 #include <linux/proc_fs.h>
48 #include <linux/uio.h>
49 #include <linux/net.h>
50 #include <linux/netdevice.h>
51 #include <linux/socket.h>
52 #include <linux/if_arp.h>
53 #include <linux/skbuff.h>
54 #include <linux/can.h>
55 #include <linux/can/core.h>
56 #include <linux/can/bcm.h>
60 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
63 /* use of last_frames[index].can_dlc */
64 #define RX_RECV 0x40 /* received data for this element */
65 #define RX_THR 0x80 /* element not been sent due to throttle feature */
66 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
68 /* get best masking value for can_rx_register() for a given single can_id */
69 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
70 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
71 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
73 #define CAN_BCM_VERSION CAN_VERSION
74 static __initdata const char banner[] = KERN_INFO
75 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
77 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
78 MODULE_LICENSE("Dual BSD/GPL");
79 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
81 /* easy access to can_frame payload */
82 static inline u64 GET_U64(const struct can_frame *cp)
84 return *(u64 *)cp->data;
88 struct list_head list;
92 unsigned long frames_abs, frames_filtered;
93 struct timeval ival1, ival2;
94 struct timer_list timer, thrtimer;
95 struct timeval rx_stamp;
96 unsigned long j_ival1, j_ival2, j_lastmsg;
101 struct can_frame *frames;
102 struct can_frame *last_frames;
103 struct can_frame sframe;
104 struct can_frame last_sframe;
106 struct net_device *rx_reg_dev;
109 static struct proc_dir_entry *proc_dir;
115 struct notifier_block notifier;
116 struct list_head rx_ops;
117 struct list_head tx_ops;
118 unsigned long dropped_usr_msgs;
119 struct proc_dir_entry *bcm_proc_read;
120 char procname [9]; /* pointer printed in ASCII with \0 */
123 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
125 return (struct bcm_sock *)&(sk)->tp_pinfo;
128 #define CFSIZ sizeof(struct can_frame)
129 #define OPSIZ sizeof(struct bcm_op)
130 #define MHSIZ sizeof(struct bcm_msg_head)
133 * rounded_tv2jif - calculate jiffies from timeval including optional up
134 * @tv: pointer to timeval
137 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
138 * function is intentionally more relaxed on precise timer ticks to get
139 * exact one jiffy for requested 1000us on a 1000HZ machine.
140 * This code is to be removed when upgrading to kernel hrtimer.
143 * calculated jiffies (max: ULONG_MAX)
145 static unsigned long rounded_tv2jif(const struct timeval *tv)
147 unsigned long sec = tv->tv_sec;
148 unsigned long usec = tv->tv_usec;
151 if (sec > ULONG_MAX / HZ)
154 /* round up to get at least the requested time */
155 usec += 1000000 / HZ - 1;
157 jif = usec / (1000000 / HZ);
159 if (sec * HZ > ULONG_MAX - jif)
162 return jif + sec * HZ;
168 static char *bcm_proc_getifname(int ifindex)
170 struct net_device *dev;
175 /* no usage counting */
176 dev = __dev_get_by_index(&init_net, ifindex);
183 static int bcm_read_proc(char *page, char **start, off_t off,
184 int count, int *eof, void *data)
187 struct sock *sk = (struct sock *)data;
188 struct bcm_sock *bo = bcm_sk(sk);
193 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
195 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
196 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
197 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
198 bo->dropped_usr_msgs);
199 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
200 bcm_proc_getifname(bo->ifindex));
201 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
203 list_for_each_entry(op, &bo->rx_ops, list) {
205 unsigned long reduction;
207 /* print only active entries & prevent division by zero */
211 len += snprintf(page + len, PAGE_SIZE - len,
213 op->can_id, bcm_proc_getifname(op->ifindex));
214 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
216 (op->flags & RX_CHECK_DLC)?'d':' ');
218 len += snprintf(page + len, PAGE_SIZE - len,
219 "timeo=%ld ", op->j_ival1);
222 len += snprintf(page + len, PAGE_SIZE - len,
223 "thr=%ld ", op->j_ival2);
225 len += snprintf(page + len, PAGE_SIZE - len,
226 "# recv %ld (%ld) => reduction: ",
227 op->frames_filtered, op->frames_abs);
229 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
231 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
232 (reduction == 100)?"near ":"", reduction);
234 if (len > PAGE_SIZE - 200) {
235 /* mark output cut off */
236 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
241 list_for_each_entry(op, &bo->tx_ops, list) {
243 len += snprintf(page + len, PAGE_SIZE - len,
244 "tx_op: %03X %s [%d] ",
245 op->can_id, bcm_proc_getifname(op->ifindex),
249 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
253 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
256 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
259 if (len > PAGE_SIZE - 100) {
260 /* mark output cut off */
261 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
266 len += snprintf(page + len, PAGE_SIZE - len, "\n");
275 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
276 * of the given bcm tx op
278 static void bcm_can_tx(struct bcm_op *op)
281 struct net_device *dev;
282 struct can_frame *cf = &op->frames[op->currframe];
284 /* no target device? => exit */
288 dev = dev_get_by_index(&init_net, op->ifindex);
290 /* RFC: should this bcm_op remove itself here? */
294 skb = alloc_skb(CFSIZ, gfp_any());
298 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
300 /* send with loopback */
305 /* update statistics */
309 /* reached last frame? */
310 if (op->currframe >= op->nframes)
317 * bcm_send_to_user - send a BCM message to the userspace
318 * (consisting of bcm_msg_head + x CAN frames)
320 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
321 struct can_frame *frames, int has_timestamp)
324 struct can_frame *firstframe;
325 struct sockaddr_can *addr;
326 struct sock *sk = op->sk;
327 int datalen = head->nframes * CFSIZ;
330 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
334 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
337 /* can_frames starting here */
338 firstframe = (struct can_frame *)skb->tail;
340 memcpy(skb_put(skb, datalen), frames, datalen);
343 * the BCM uses the can_dlc-element of the can_frame
344 * structure for internal purposes. This is only
345 * relevant for updates that are generated by the
346 * BCM, where nframes is 1
348 if (head->nframes == 1)
349 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
353 /* restore rx timestamp */
354 skb->stamp = op->rx_stamp;
358 * Put the datagram to the queue so that bcm_recvmsg() can
359 * get it from there. We need to pass the interface index to
360 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
361 * containing the interface index.
364 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
365 addr = (struct sockaddr_can *)skb->cb;
366 memset(addr, 0, sizeof(*addr));
367 addr->can_family = AF_CAN;
368 addr->can_ifindex = op->rx_ifindex;
370 err = sock_queue_rcv_skb(sk, skb);
372 struct bcm_sock *bo = bcm_sk(sk);
375 /* don't care about overflows in this statistic */
376 bo->dropped_usr_msgs++;
381 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
383 static void bcm_tx_timeout_handler(unsigned long data)
385 struct bcm_op *op = (struct bcm_op *)data;
386 struct bcm_msg_head msg_head;
388 if (op->j_ival1 && (op->count > 0)) {
391 if (!op->count && (op->flags & TX_COUNTEVT)) {
393 /* create notification to user */
394 msg_head.opcode = TX_EXPIRED;
395 msg_head.flags = op->flags;
396 msg_head.count = op->count;
397 msg_head.ival1 = op->ival1;
398 msg_head.ival2 = op->ival2;
399 msg_head.can_id = op->can_id;
400 msg_head.nframes = 0;
402 bcm_send_to_user(op, &msg_head, NULL, 0);
406 if (op->j_ival1 && (op->count > 0)) {
408 /* send (next) frame */
410 mod_timer(&op->timer, jiffies + op->j_ival1);
415 /* send (next) frame */
417 mod_timer(&op->timer, jiffies + op->j_ival2);
423 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
425 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
427 struct bcm_msg_head head;
429 op->j_lastmsg = jiffies;
431 /* update statistics */
432 op->frames_filtered++;
434 /* prevent statistics overflow */
435 if (op->frames_filtered > ULONG_MAX/100)
436 op->frames_filtered = op->frames_abs = 0;
438 /* this element is not throttled anymore */
439 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
441 head.opcode = RX_CHANGED;
442 head.flags = op->flags;
443 head.count = op->count;
444 head.ival1 = op->ival1;
445 head.ival2 = op->ival2;
446 head.can_id = op->can_id;
449 bcm_send_to_user(op, &head, data, 1);
453 * bcm_rx_update_and_send - process a detected relevant receive content change
454 * 1. update the last received data
455 * 2. send a notification to the user (if possible)
457 static void bcm_rx_update_and_send(struct bcm_op *op,
458 struct can_frame *lastdata,
459 const struct can_frame *rxdata)
461 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
463 memcpy(lastdata, rxdata, CFSIZ);
465 /* mark as used and throttled by default */
466 lastdata->can_dlc |= (RX_RECV|RX_THR);
468 /* throttle bcm_rx_changed ? */
469 if ((op->thrtimer.expires) ||
470 ((op->j_ival2) && (nexttx > jiffies))) {
471 /* we are already waiting OR we have to start waiting */
473 if (!(op->thrtimer.expires)) {
474 /* start the timer only the first time */
475 mod_timer(&op->thrtimer, nexttx);
479 /* send RX_CHANGED to the user immediately */
480 bcm_rx_changed(op, lastdata);
485 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
486 * received data stored in op->last_frames[]
488 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
489 const struct can_frame *rxdata)
492 * no one uses the MSBs of can_dlc for comparation,
493 * so we use it here to detect the first time of reception
496 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
497 /* received data for the first time => send update to user */
498 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
502 /* do a real check in can_frame data section */
504 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
505 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
506 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
510 if (op->flags & RX_CHECK_DLC) {
511 /* do a real check in can_frame dlc */
512 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
514 bcm_rx_update_and_send(op, &op->last_frames[index],
522 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
524 static void bcm_rx_starttimer(struct bcm_op *op)
526 if (op->flags & RX_NO_AUTOTIMER)
530 mod_timer(&op->timer, jiffies + op->j_ival1);
534 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
536 static void bcm_rx_timeout_handler(unsigned long data)
538 struct bcm_op *op = (struct bcm_op *)data;
539 struct bcm_msg_head msg_head;
541 /* create notification to user */
542 msg_head.opcode = RX_TIMEOUT;
543 msg_head.flags = op->flags;
544 msg_head.count = op->count;
545 msg_head.ival1 = op->ival1;
546 msg_head.ival2 = op->ival2;
547 msg_head.can_id = op->can_id;
548 msg_head.nframes = 0;
550 bcm_send_to_user(op, &msg_head, NULL, 0);
552 /* no restart of the timer is done here! */
554 /* if user wants to be informed, when cyclic CAN-Messages come back */
555 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
556 /* clear received can_frames to indicate 'nothing received' */
557 memset(op->last_frames, 0, op->nframes * CFSIZ);
562 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
564 static inline int bcm_rx_do_flush(struct bcm_op *op, int index)
566 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
567 bcm_rx_changed(op, &op->last_frames[index]);
574 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
576 static int bcm_rx_thr_flush(struct bcm_op *op)
580 if (op->nframes > 1) {
583 /* for MUX filter we start at index 1 */
584 for (i = 1; i < op->nframes; i++)
585 updated += bcm_rx_do_flush(op, i);
588 /* for RX_FILTER_ID and simple filter */
589 updated += bcm_rx_do_flush(op, 0);
596 * bcm_rx_thr_handler - the time for blocked content updates is over now:
597 * Check for throttled data and send it to the userspace
599 static void bcm_rx_thr_handler(unsigned long data)
601 struct bcm_op *op = (struct bcm_op *)data;
603 if (bcm_rx_thr_flush(op))
604 mod_timer(&op->thrtimer, jiffies + op->j_ival2);
606 /* mark disabled / consumed timer */
607 op->thrtimer.expires = 0;
612 * bcm_rx_handler - handle a CAN frame receiption
614 static void bcm_rx_handler(struct sk_buff *skb, void *data)
616 struct bcm_op *op = (struct bcm_op *)data;
617 const struct can_frame *rxframe = (struct can_frame *)skb->data;
620 /* disable timeout */
621 del_timer(&op->timer);
623 if (op->can_id != rxframe->can_id)
626 /* save rx timestamp */
627 op->rx_stamp = skb->stamp;
628 /* save originator for recvfrom() */
629 op->rx_ifindex = skb->dev->ifindex;
630 /* update statistics */
633 if (op->flags & RX_RTR_FRAME) {
634 /* send reply for RTR-request (placed in op->frames[0]) */
639 if (op->flags & RX_FILTER_ID) {
640 /* the easiest case */
641 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
645 if (op->nframes == 1) {
646 /* simple compare with index 0 */
647 bcm_rx_cmp_to_index(op, 0, rxframe);
651 if (op->nframes > 1) {
655 * find the first multiplex mask that fits.
656 * Remark: The MUX-mask is stored in index 0
659 for (i = 1; i < op->nframes; i++) {
660 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
661 (GET_U64(&op->frames[0]) &
662 GET_U64(&op->frames[i]))) {
663 bcm_rx_cmp_to_index(op, i, rxframe);
670 bcm_rx_starttimer(op);
674 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
676 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
681 list_for_each_entry(op, ops, list) {
682 if ((op->can_id == can_id) && (op->ifindex == ifindex))
689 static void bcm_remove_op(struct bcm_op *op)
691 del_timer(&op->timer);
692 del_timer(&op->thrtimer);
694 if ((op->frames) && (op->frames != &op->sframe))
697 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
698 kfree(op->last_frames);
705 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
707 if (op->rx_reg_dev == dev) {
708 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
711 /* mark as removed subscription */
712 op->rx_reg_dev = NULL;
714 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
715 "mismatch %p %p\n", op->rx_reg_dev, dev);
719 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
721 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
723 struct bcm_op *op, *n;
725 list_for_each_entry_safe(op, n, ops, list) {
726 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
729 * Don't care if we're bound or not (due to netdev
730 * problems) can_rx_unregister() is always a save
735 * Only remove subscriptions that had not
736 * been removed due to NETDEV_UNREGISTER
739 if (op->rx_reg_dev) {
740 struct net_device *dev;
742 dev = dev_get_by_index(&init_net,
745 bcm_rx_unreg(dev, op);
750 can_rx_unregister(NULL, op->can_id,
760 return 0; /* not found */
764 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
766 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
768 struct bcm_op *op, *n;
770 list_for_each_entry_safe(op, n, ops, list) {
771 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
778 return 0; /* not found */
782 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
784 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
787 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
792 /* put current values into msg_head */
793 msg_head->flags = op->flags;
794 msg_head->count = op->count;
795 msg_head->ival1 = op->ival1;
796 msg_head->ival2 = op->ival2;
797 msg_head->nframes = op->nframes;
799 bcm_send_to_user(op, msg_head, op->frames, 0);
805 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
807 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
808 int ifindex, struct sock *sk)
810 struct bcm_sock *bo = bcm_sk(sk);
814 /* we need a real device to send frames */
818 /* we need at least one can_frame */
819 if (msg_head->nframes < 1)
822 /* check the given can_id */
823 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
826 /* update existing BCM operation */
829 * Do we need more space for the can_frames than currently
830 * allocated? -> This is a _really_ unusual use-case and
831 * therefore (complexity / locking) it is not supported.
833 if (msg_head->nframes > op->nframes)
836 /* update can_frames content */
837 for (i = 0; i < msg_head->nframes; i++) {
838 err = memcpy_fromiovec((u8 *)&op->frames[i],
839 msg->msg_iov, CFSIZ);
841 if (op->frames[i].can_dlc > 8)
847 if (msg_head->flags & TX_CP_CAN_ID) {
848 /* copy can_id into frame */
849 op->frames[i].can_id = msg_head->can_id;
854 /* insert new BCM operation for the given can_id */
856 op = kzalloc(OPSIZ, GFP_KERNEL);
860 op->can_id = msg_head->can_id;
862 /* create array for can_frames and copy the data */
863 if (msg_head->nframes > 1) {
864 op->frames = kmalloc(msg_head->nframes * CFSIZ,
871 op->frames = &op->sframe;
873 for (i = 0; i < msg_head->nframes; i++) {
874 err = memcpy_fromiovec((u8 *)&op->frames[i],
875 msg->msg_iov, CFSIZ);
877 if (op->frames[i].can_dlc > 8)
881 if (op->frames != &op->sframe)
887 if (msg_head->flags & TX_CP_CAN_ID) {
888 /* copy can_id into frame */
889 op->frames[i].can_id = msg_head->can_id;
893 /* tx_ops never compare with previous received messages */
894 op->last_frames = NULL;
896 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
898 op->ifindex = ifindex;
900 /* initialize uninitialized (kzalloc) structure */
901 setup_timer(&op->timer, bcm_tx_timeout_handler,
904 /* currently unused in tx_ops */
905 init_timer(&op->thrtimer);
907 /* add this bcm_op to the list of the tx_ops */
908 list_add(&op->list, &bo->tx_ops);
910 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
912 if (op->nframes != msg_head->nframes) {
913 op->nframes = msg_head->nframes;
914 /* start multiple frame transmission with index 0 */
920 op->flags = msg_head->flags;
922 if (op->flags & TX_RESET_MULTI_IDX) {
923 /* start multiple frame transmission with index 0 */
927 if (op->flags & SETTIMER) {
928 /* set timer values */
929 op->count = msg_head->count;
930 op->ival1 = msg_head->ival1;
931 op->ival2 = msg_head->ival2;
932 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
933 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
935 /* disable an active timer due to zero values? */
936 if (!op->j_ival1 && !op->j_ival2)
937 del_timer(&op->timer);
940 if ((op->flags & STARTTIMER) &&
941 ((op->j_ival1 && op->count) || op->j_ival2)) {
943 /* spec: send can_frame when starting timer */
944 op->flags |= TX_ANNOUNCE;
946 if (op->j_ival1 && (op->count > 0)) {
947 /* op->count-- is done in bcm_tx_timeout_handler */
948 mod_timer(&op->timer, jiffies + op->j_ival1);
950 mod_timer(&op->timer, jiffies + op->j_ival2);
953 if (op->flags & TX_ANNOUNCE)
956 return msg_head->nframes * CFSIZ + MHSIZ;
960 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
962 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
963 int ifindex, struct sock *sk)
965 struct bcm_sock *bo = bcm_sk(sk);
970 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
971 /* be robust against wrong usage ... */
972 msg_head->flags |= RX_FILTER_ID;
973 /* ignore trailing garbage */
974 msg_head->nframes = 0;
977 if ((msg_head->flags & RX_RTR_FRAME) &&
978 ((msg_head->nframes != 1) ||
979 (!(msg_head->can_id & CAN_RTR_FLAG))))
982 /* check the given can_id */
983 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
985 /* update existing BCM operation */
988 * Do we need more space for the can_frames than currently
989 * allocated? -> This is a _really_ unusual use-case and
990 * therefore (complexity / locking) it is not supported.
992 if (msg_head->nframes > op->nframes)
995 if (msg_head->nframes) {
996 /* update can_frames content */
997 err = memcpy_fromiovec((u8 *)op->frames,
999 msg_head->nframes * CFSIZ);
1003 /* clear last_frames to indicate 'nothing received' */
1004 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1007 op->nframes = msg_head->nframes;
1009 /* Only an update -> do not call can_rx_register() */
1013 /* insert new BCM operation for the given can_id */
1014 op = kzalloc(OPSIZ, GFP_KERNEL);
1018 op->can_id = msg_head->can_id;
1019 op->nframes = msg_head->nframes;
1021 if (msg_head->nframes > 1) {
1022 /* create array for can_frames and copy the data */
1023 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1030 /* create and init array for received can_frames */
1031 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1033 if (!op->last_frames) {
1040 op->frames = &op->sframe;
1041 op->last_frames = &op->last_sframe;
1044 if (msg_head->nframes) {
1045 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1046 msg_head->nframes * CFSIZ);
1048 if (op->frames != &op->sframe)
1050 if (op->last_frames != &op->last_sframe)
1051 kfree(op->last_frames);
1057 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1059 op->ifindex = ifindex;
1061 /* initialize uninitialized (kzalloc) structure */
1062 setup_timer(&op->timer, bcm_rx_timeout_handler,
1065 /* init throttle timer for RX_CHANGED */
1066 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1069 /* mark disabled timer */
1070 op->thrtimer.expires = 0;
1072 /* add this bcm_op to the list of the rx_ops */
1073 list_add(&op->list, &bo->rx_ops);
1075 /* call can_rx_register() */
1078 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1081 op->flags = msg_head->flags;
1083 if (op->flags & RX_RTR_FRAME) {
1085 /* no timers in RTR-mode */
1086 del_timer(&op->thrtimer);
1087 del_timer(&op->timer);
1090 * funny feature in RX(!)_SETUP only for RTR-mode:
1091 * copy can_id into frame BUT without RTR-flag to
1092 * prevent a full-load-loopback-test ... ;-]
1094 if ((op->flags & TX_CP_CAN_ID) ||
1095 (op->frames[0].can_id == op->can_id))
1096 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1099 if (op->flags & SETTIMER) {
1101 /* set timer value */
1102 op->ival1 = msg_head->ival1;
1103 op->ival2 = msg_head->ival2;
1104 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1105 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1107 /* disable an active timer due to zero value? */
1109 del_timer(&op->timer);
1112 * In any case cancel the throttle timer, flush
1113 * potentially blocked msgs and reset throttle handling
1115 del_timer(&op->thrtimer);
1116 bcm_rx_thr_flush(op);
1117 op->thrtimer.expires = 0;
1120 if ((op->flags & STARTTIMER) && op->j_ival1)
1121 mod_timer(&op->timer, jiffies + op->j_ival1);
1124 /* now we can register for can_ids, if we added a new bcm_op */
1125 if (do_rx_register) {
1127 struct net_device *dev;
1129 dev = dev_get_by_index(&init_net, ifindex);
1131 err = can_rx_register(dev, op->can_id,
1132 REGMASK(op->can_id),
1136 op->rx_reg_dev = dev;
1141 err = can_rx_register(NULL, op->can_id,
1142 REGMASK(op->can_id),
1143 bcm_rx_handler, op, "bcm");
1145 /* this bcm rx op is broken -> remove it */
1146 list_del(&op->list);
1152 return msg_head->nframes * CFSIZ + MHSIZ;
1156 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1158 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1160 struct sk_buff *skb;
1161 struct net_device *dev;
1164 /* we need a real device to send frames */
1168 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1173 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1179 dev = dev_get_by_index(&init_net, ifindex);
1187 err = can_send(skb, 1); /* send with loopback */
1193 return CFSIZ + MHSIZ;
1197 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1199 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, int size,
1200 struct scm_cookie *scm)
1202 struct sock *sk = sock->sk;
1203 struct bcm_sock *bo = bcm_sk(sk);
1204 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1205 struct bcm_msg_head msg_head;
1206 int ret; /* read bytes or error codes as return value */
1211 /* check for valid message length from userspace */
1212 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1215 /* check for alternative ifindex for this bcm_op */
1217 if (!ifindex && msg->msg_name) {
1218 /* no bound device as default => check msg_name */
1219 struct sockaddr_can *addr =
1220 (struct sockaddr_can *)msg->msg_name;
1222 if (addr->can_family != AF_CAN)
1225 /* ifindex from sendto() */
1226 ifindex = addr->can_ifindex;
1229 struct net_device *dev;
1231 dev = dev_get_by_index(&init_net, ifindex);
1235 if (dev->type != ARPHRD_CAN) {
1244 /* read message head information */
1246 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1252 switch (msg_head.opcode) {
1255 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1259 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1263 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1270 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1277 /* reuse msg_head for the reply to TX_READ */
1278 msg_head.opcode = TX_STATUS;
1279 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1283 /* reuse msg_head for the reply to RX_READ */
1284 msg_head.opcode = RX_STATUS;
1285 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1289 /* we need exactly one can_frame behind the msg head */
1290 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1293 ret = bcm_tx_send(msg, ifindex, sk);
1307 * notification handler for netdevice status changes
1309 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1312 struct net_device *dev = (struct net_device *)data;
1313 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1314 struct sock *sk = bo->sk;
1316 int notify_enodev = 0;
1318 if (dev->type != ARPHRD_CAN)
1323 case NETDEV_UNREGISTER:
1326 /* remove device specific receive entries */
1327 list_for_each_entry(op, &bo->rx_ops, list)
1328 if (op->rx_reg_dev == dev)
1329 bcm_rx_unreg(dev, op);
1331 /* remove device reference, if this is our bound device */
1332 if (bo->bound && bo->ifindex == dev->ifindex) {
1340 if (notify_enodev) {
1341 sk->sk_err = ENODEV;
1343 sk->sk_error_report(sk);
1348 if (bo->bound && bo->ifindex == dev->ifindex) {
1349 sk->sk_err = ENETDOWN;
1351 sk->sk_error_report(sk);
1359 * initial settings for all BCM sockets to be set at socket creation time
1361 static int bcm_init(struct sock *sk)
1363 struct bcm_sock *bo = bcm_sk(sk);
1368 bo->dropped_usr_msgs = 0;
1369 bo->bcm_proc_read = NULL;
1371 INIT_LIST_HEAD(&bo->tx_ops);
1372 INIT_LIST_HEAD(&bo->rx_ops);
1375 bo->notifier.notifier_call = bcm_notifier;
1377 register_netdevice_notifier(&bo->notifier);
1383 * standard socket functions
1385 static int bcm_release(struct socket *sock)
1387 struct sock *sk = sock->sk;
1388 struct bcm_sock *bo = bcm_sk(sk);
1389 struct bcm_op *op, *next;
1391 /* remove bcm_ops, timer, rx_unregister(), etc. */
1393 unregister_netdevice_notifier(&bo->notifier);
1397 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1400 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1402 * Don't care if we're bound or not (due to netdev problems)
1403 * can_rx_unregister() is always a save thing to do here.
1407 * Only remove subscriptions that had not
1408 * been removed due to NETDEV_UNREGISTER
1411 if (op->rx_reg_dev) {
1412 struct net_device *dev;
1414 dev = dev_get_by_index(&init_net, op->ifindex);
1416 bcm_rx_unreg(dev, op);
1421 can_rx_unregister(NULL, op->can_id,
1422 REGMASK(op->can_id),
1423 bcm_rx_handler, op);
1428 /* remove procfs entry */
1429 if (proc_dir && bo->bcm_proc_read)
1430 remove_proc_entry(bo->procname, proc_dir);
1432 /* remove device reference */
1447 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1450 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1451 struct sock *sk = sock->sk;
1452 struct bcm_sock *bo = bcm_sk(sk);
1457 /* bind a device to this socket */
1458 if (addr->can_ifindex) {
1459 struct net_device *dev;
1461 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1465 if (dev->type != ARPHRD_CAN) {
1470 bo->ifindex = dev->ifindex;
1474 /* no interface reference for ifindex = 0 ('any' CAN device) */
1481 /* unique socket address as filename */
1482 sprintf(bo->procname, "%p", sock);
1483 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1491 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1492 int flags, struct scm_cookie *scm)
1494 struct sock *sk = sock->sk;
1495 struct sk_buff *skb;
1500 noblock = flags & MSG_DONTWAIT;
1501 flags &= ~MSG_DONTWAIT;
1502 skb = skb_recv_datagram(sk, flags, noblock, &error);
1506 if (skb->len < size)
1509 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1511 skb_free_datagram(sk, skb);
1515 sock_recv_timestamp(msg, sk, skb);
1517 if (msg->msg_name) {
1518 msg->msg_namelen = sizeof(struct sockaddr_can);
1519 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1522 skb_free_datagram(sk, skb);
1527 static struct proto_ops bcm_ops __read_mostly = {
1529 .release = bcm_release,
1530 .bind = sock_no_bind,
1531 .connect = bcm_connect,
1532 .socketpair = sock_no_socketpair,
1533 .accept = sock_no_accept,
1534 .getname = sock_no_getname,
1535 .poll = datagram_poll,
1536 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1537 .listen = sock_no_listen,
1538 .shutdown = sock_no_shutdown,
1539 .setsockopt = sock_no_setsockopt,
1540 .getsockopt = sock_no_getsockopt,
1541 .sendmsg = bcm_sendmsg,
1542 .recvmsg = bcm_recvmsg,
1543 .mmap = sock_no_mmap,
1544 .sendpage = sock_no_sendpage,
1547 static struct can_proto bcm_can_proto __read_mostly = {
1549 .protocol = CAN_BCM,
1552 .obj_size = sizeof(struct bcm_sock),
1556 static int __init bcm_module_init(void)
1562 err = can_proto_register(&bcm_can_proto);
1564 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1568 /* create /proc/net/can-bcm directory */
1569 proc_dir = proc_mkdir("can-bcm", proc_net);
1572 proc_dir->owner = THIS_MODULE;
1577 static void __exit bcm_module_exit(void)
1579 can_proto_unregister(&bcm_can_proto);
1582 proc_net_remove("can-bcm");
1585 module_init(bcm_module_init);
1586 module_exit(bcm_module_exit);