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>
58 #include <net/net_namespace.h>
60 /* use of last_frames[index].can_dlc */
61 #define RX_RECV 0x40 /* received data for this element */
62 #define RX_THR 0x80 /* element not been sent due to throttle feature */
63 #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */
65 /* get best masking value for can_rx_register() for a given single can_id */
66 #define REGMASK(id) ((id & CAN_RTR_FLAG) | ((id & CAN_EFF_FLAG) ? \
67 (CAN_EFF_MASK | CAN_EFF_FLAG) : CAN_SFF_MASK))
69 #define CAN_BCM_VERSION CAN_VERSION
70 static __initdata const char banner[] = KERN_INFO
71 "can: broadcast manager protocol (rev " CAN_BCM_VERSION ")\n";
73 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
74 MODULE_LICENSE("Dual BSD/GPL");
75 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
77 /* easy access to can_frame payload */
78 static inline u64 GET_U64(const struct can_frame *cp)
80 return *(u64 *)cp->data;
84 struct list_head list;
88 unsigned long j_ival1, j_ival2, j_lastmsg;
89 unsigned long frames_abs, frames_filtered;
90 struct timer_list timer, thrtimer;
91 struct timeval ival1, ival2;
97 struct can_frame *frames;
98 struct can_frame *last_frames;
99 struct can_frame sframe;
100 struct can_frame last_sframe;
102 struct net_device *rx_reg_dev;
105 static struct proc_dir_entry *proc_dir;
111 struct notifier_block notifier;
112 struct list_head rx_ops;
113 struct list_head tx_ops;
114 unsigned long dropped_usr_msgs;
115 struct proc_dir_entry *bcm_proc_read;
116 char procname [9]; /* pointer printed in ASCII with \0 */
119 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
121 return (struct bcm_sock *)sk;
124 #define CFSIZ sizeof(struct can_frame)
125 #define OPSIZ sizeof(struct bcm_op)
126 #define MHSIZ sizeof(struct bcm_msg_head)
129 * rounded_tv2jif - calculate jiffies from timeval including optional up
130 * @tv: pointer to timeval
133 * Unlike timeval_to_jiffies() provided in include/linux/jiffies.h, this
134 * function is intentionally more relaxed on precise timer ticks to get
135 * exact one jiffy for requested 1000us on a 1000HZ machine.
136 * This code is to be removed when upgrading to kernel hrtimer.
139 * calculated jiffies (max: ULONG_MAX)
141 static unsigned long rounded_tv2jif(const struct timeval *tv)
143 unsigned long sec = tv->tv_sec;
144 unsigned long usec = tv->tv_usec;
147 if (sec > ULONG_MAX / HZ)
150 /* round up to get at least the requested time */
151 usec += 1000000 / HZ - 1;
153 jif = usec / (1000000 / HZ);
155 if (sec * HZ > ULONG_MAX - jif)
158 return jif + sec * HZ;
164 static char *bcm_proc_getifname(int ifindex)
166 struct net_device *dev;
171 /* no usage counting */
172 dev = __dev_get_by_index(&init_net, ifindex);
179 static int bcm_read_proc(char *page, char **start, off_t off,
180 int count, int *eof, void *data)
183 struct sock *sk = (struct sock *)data;
184 struct bcm_sock *bo = bcm_sk(sk);
187 len += snprintf(page + len, PAGE_SIZE - len, ">>> socket %p",
189 len += snprintf(page + len, PAGE_SIZE - len, " / sk %p", sk);
190 len += snprintf(page + len, PAGE_SIZE - len, " / bo %p", bo);
191 len += snprintf(page + len, PAGE_SIZE - len, " / dropped %lu",
192 bo->dropped_usr_msgs);
193 len += snprintf(page + len, PAGE_SIZE - len, " / bound %s",
194 bcm_proc_getifname(bo->ifindex));
195 len += snprintf(page + len, PAGE_SIZE - len, " <<<\n");
197 list_for_each_entry(op, &bo->rx_ops, list) {
199 unsigned long reduction;
201 /* print only active entries & prevent division by zero */
205 len += snprintf(page + len, PAGE_SIZE - len,
207 op->can_id, bcm_proc_getifname(op->ifindex));
208 len += snprintf(page + len, PAGE_SIZE - len, "[%d]%c ",
210 (op->flags & RX_CHECK_DLC)?'d':' ');
212 len += snprintf(page + len, PAGE_SIZE - len,
213 "timeo=%ld ", op->j_ival1);
216 len += snprintf(page + len, PAGE_SIZE - len,
217 "thr=%ld ", op->j_ival2);
219 len += snprintf(page + len, PAGE_SIZE - len,
220 "# recv %ld (%ld) => reduction: ",
221 op->frames_filtered, op->frames_abs);
223 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
225 len += snprintf(page + len, PAGE_SIZE - len, "%s%ld%%\n",
226 (reduction == 100)?"near ":"", reduction);
228 if (len > PAGE_SIZE - 200) {
229 /* mark output cut off */
230 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
235 list_for_each_entry(op, &bo->tx_ops, list) {
237 len += snprintf(page + len, PAGE_SIZE - len,
238 "tx_op: %03X %s [%d] ",
239 op->can_id, bcm_proc_getifname(op->ifindex),
242 len += snprintf(page + len, PAGE_SIZE - len, "t1=%ld ",
246 len += snprintf(page + len, PAGE_SIZE - len, "t2=%ld ",
249 len += snprintf(page + len, PAGE_SIZE - len, "# sent %ld\n",
252 if (len > PAGE_SIZE - 100) {
253 /* mark output cut off */
254 len += snprintf(page + len, PAGE_SIZE - len, "(..)\n");
259 len += snprintf(page + len, PAGE_SIZE - len, "\n");
266 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
267 * of the given bcm tx op
269 static void bcm_can_tx(struct bcm_op *op)
272 struct net_device *dev;
273 struct can_frame *cf = &op->frames[op->currframe];
275 /* no target device? => exit */
279 dev = dev_get_by_index(&init_net, op->ifindex);
281 /* RFC: should this bcm_op remove itself here? */
285 skb = alloc_skb(CFSIZ, gfp_any());
289 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
291 /* send with loopback */
296 /* update statistics */
300 /* reached last frame? */
301 if (op->currframe >= op->nframes)
308 * bcm_send_to_user - send a BCM message to the userspace
309 * (consisting of bcm_msg_head + x CAN frames)
311 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
312 struct can_frame *frames, int has_timestamp)
315 struct can_frame *firstframe;
316 struct sockaddr_can *addr;
317 struct sock *sk = op->sk;
318 int datalen = head->nframes * CFSIZ;
321 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
325 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
328 /* can_frames starting here */
329 firstframe = (struct can_frame *) skb_tail_pointer(skb);
331 memcpy(skb_put(skb, datalen), frames, datalen);
334 * the BCM uses the can_dlc-element of the can_frame
335 * structure for internal purposes. This is only
336 * relevant for updates that are generated by the
337 * BCM, where nframes is 1
339 if (head->nframes == 1)
340 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
344 /* restore rx timestamp */
345 skb->tstamp = op->rx_stamp;
349 * Put the datagram to the queue so that bcm_recvmsg() can
350 * get it from there. We need to pass the interface index to
351 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
352 * containing the interface index.
355 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
356 addr = (struct sockaddr_can *)skb->cb;
357 memset(addr, 0, sizeof(*addr));
358 addr->can_family = AF_CAN;
359 addr->can_ifindex = op->rx_ifindex;
361 err = sock_queue_rcv_skb(sk, skb);
363 struct bcm_sock *bo = bcm_sk(sk);
366 /* don't care about overflows in this statistic */
367 bo->dropped_usr_msgs++;
372 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
374 static void bcm_tx_timeout_handler(unsigned long data)
376 struct bcm_op *op = (struct bcm_op *)data;
378 if (op->j_ival1 && (op->count > 0)) {
381 if (!op->count && (op->flags & TX_COUNTEVT)) {
382 struct bcm_msg_head msg_head;
384 /* create notification to user */
385 msg_head.opcode = TX_EXPIRED;
386 msg_head.flags = op->flags;
387 msg_head.count = op->count;
388 msg_head.ival1 = op->ival1;
389 msg_head.ival2 = op->ival2;
390 msg_head.can_id = op->can_id;
391 msg_head.nframes = 0;
393 bcm_send_to_user(op, &msg_head, NULL, 0);
397 if (op->j_ival1 && (op->count > 0)) {
399 /* send (next) frame */
401 mod_timer(&op->timer, jiffies + op->j_ival1);
406 /* send (next) frame */
408 mod_timer(&op->timer, jiffies + op->j_ival2);
416 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
418 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
420 struct bcm_msg_head head;
422 op->j_lastmsg = jiffies;
424 /* update statistics */
425 op->frames_filtered++;
427 /* prevent statistics overflow */
428 if (op->frames_filtered > ULONG_MAX/100)
429 op->frames_filtered = op->frames_abs = 0;
431 head.opcode = RX_CHANGED;
432 head.flags = op->flags;
433 head.count = op->count;
434 head.ival1 = op->ival1;
435 head.ival2 = op->ival2;
436 head.can_id = op->can_id;
439 bcm_send_to_user(op, &head, data, 1);
443 * bcm_rx_update_and_send - process a detected relevant receive content change
444 * 1. update the last received data
445 * 2. send a notification to the user (if possible)
447 static void bcm_rx_update_and_send(struct bcm_op *op,
448 struct can_frame *lastdata,
449 struct can_frame *rxdata)
451 unsigned long nexttx = op->j_lastmsg + op->j_ival2;
453 memcpy(lastdata, rxdata, CFSIZ);
456 lastdata->can_dlc |= RX_RECV;
458 /* throttle bcm_rx_changed ? */
459 if ((op->thrtimer.expires) ||
460 ((op->j_ival2) && (nexttx > jiffies))) {
461 /* we are already waiting OR we have to start waiting */
463 /* mark as 'throttled' */
464 lastdata->can_dlc |= RX_THR;
466 if (!(op->thrtimer.expires)) {
467 /* start the timer only the first time */
468 mod_timer(&op->thrtimer, nexttx);
472 /* send RX_CHANGED to the user immediately */
473 bcm_rx_changed(op, rxdata);
478 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
479 * received data stored in op->last_frames[]
481 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
482 struct can_frame *rxdata)
485 * no one uses the MSBs of can_dlc for comparation,
486 * so we use it here to detect the first time of reception
489 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
490 /* received data for the first time => send update to user */
491 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
495 /* do a real check in can_frame data section */
497 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
498 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
499 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
503 if (op->flags & RX_CHECK_DLC) {
504 /* do a real check in can_frame dlc */
505 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
507 bcm_rx_update_and_send(op, &op->last_frames[index],
515 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
517 static void bcm_rx_starttimer(struct bcm_op *op)
519 if (op->flags & RX_NO_AUTOTIMER)
523 mod_timer(&op->timer, jiffies + op->j_ival1);
527 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
529 static void bcm_rx_timeout_handler(unsigned long data)
531 struct bcm_op *op = (struct bcm_op *)data;
532 struct bcm_msg_head msg_head;
534 msg_head.opcode = RX_TIMEOUT;
535 msg_head.flags = op->flags;
536 msg_head.count = op->count;
537 msg_head.ival1 = op->ival1;
538 msg_head.ival2 = op->ival2;
539 msg_head.can_id = op->can_id;
540 msg_head.nframes = 0;
542 bcm_send_to_user(op, &msg_head, NULL, 0);
544 /* no restart of the timer is done here! */
546 /* if user wants to be informed, when cyclic CAN-Messages come back */
547 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
548 /* clear received can_frames to indicate 'nothing received' */
549 memset(op->last_frames, 0, op->nframes * CFSIZ);
554 * bcm_rx_thr_handler - the time for blocked content updates is over now:
555 * Check for throttled data and send it to the userspace
557 static void bcm_rx_thr_handler(unsigned long data)
559 struct bcm_op *op = (struct bcm_op *)data;
562 /* mark disabled / consumed timer */
563 op->thrtimer.expires = 0;
565 if (op->nframes > 1) {
566 /* for MUX filter we start at index 1 */
567 for (i = 1; i < op->nframes; i++) {
568 if ((op->last_frames) &&
569 (op->last_frames[i].can_dlc & RX_THR)) {
570 op->last_frames[i].can_dlc &= ~RX_THR;
571 bcm_rx_changed(op, &op->last_frames[i]);
576 /* for RX_FILTER_ID and simple filter */
577 if (op->last_frames && (op->last_frames[0].can_dlc & RX_THR)) {
578 op->last_frames[0].can_dlc &= ~RX_THR;
579 bcm_rx_changed(op, &op->last_frames[0]);
585 * bcm_rx_handler - handle a CAN frame receiption
587 static void bcm_rx_handler(struct sk_buff *skb, void *data)
589 struct bcm_op *op = (struct bcm_op *)data;
590 struct can_frame rxframe;
593 /* disable timeout */
594 del_timer(&op->timer);
596 if (skb->len == sizeof(rxframe)) {
597 memcpy(&rxframe, skb->data, sizeof(rxframe));
598 /* save rx timestamp */
599 op->rx_stamp = skb->tstamp;
600 /* save originator for recvfrom() */
601 op->rx_ifindex = skb->dev->ifindex;
602 /* update statistics */
611 if (op->can_id != rxframe.can_id)
614 if (op->flags & RX_RTR_FRAME) {
615 /* send reply for RTR-request (placed in op->frames[0]) */
620 if (op->flags & RX_FILTER_ID) {
621 /* the easiest case */
622 bcm_rx_update_and_send(op, &op->last_frames[0], &rxframe);
623 bcm_rx_starttimer(op);
627 if (op->nframes == 1) {
628 /* simple compare with index 0 */
629 bcm_rx_cmp_to_index(op, 0, &rxframe);
630 bcm_rx_starttimer(op);
634 if (op->nframes > 1) {
638 * find the first multiplex mask that fits.
639 * Remark: The MUX-mask is stored in index 0
642 for (i = 1; i < op->nframes; i++) {
643 if ((GET_U64(&op->frames[0]) & GET_U64(&rxframe)) ==
644 (GET_U64(&op->frames[0]) &
645 GET_U64(&op->frames[i]))) {
646 bcm_rx_cmp_to_index(op, i, &rxframe);
650 bcm_rx_starttimer(op);
655 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
657 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
662 list_for_each_entry(op, ops, list) {
663 if ((op->can_id == can_id) && (op->ifindex == ifindex))
670 static void bcm_remove_op(struct bcm_op *op)
672 del_timer(&op->timer);
673 del_timer(&op->thrtimer);
675 if ((op->frames) && (op->frames != &op->sframe))
678 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
679 kfree(op->last_frames);
686 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
688 if (op->rx_reg_dev == dev) {
689 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
692 /* mark as removed subscription */
693 op->rx_reg_dev = NULL;
695 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
696 "mismatch %p %p\n", op->rx_reg_dev, dev);
700 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
702 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
704 struct bcm_op *op, *n;
706 list_for_each_entry_safe(op, n, ops, list) {
707 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
710 * Don't care if we're bound or not (due to netdev
711 * problems) can_rx_unregister() is always a save
716 * Only remove subscriptions that had not
717 * been removed due to NETDEV_UNREGISTER
720 if (op->rx_reg_dev) {
721 struct net_device *dev;
723 dev = dev_get_by_index(&init_net,
726 bcm_rx_unreg(dev, op);
731 can_rx_unregister(NULL, op->can_id,
741 return 0; /* not found */
745 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
747 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
749 struct bcm_op *op, *n;
751 list_for_each_entry_safe(op, n, ops, list) {
752 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
759 return 0; /* not found */
763 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
765 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
768 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
773 /* put current values into msg_head */
774 msg_head->flags = op->flags;
775 msg_head->count = op->count;
776 msg_head->ival1 = op->ival1;
777 msg_head->ival2 = op->ival2;
778 msg_head->nframes = op->nframes;
780 bcm_send_to_user(op, msg_head, op->frames, 0);
786 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
788 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
789 int ifindex, struct sock *sk)
791 struct bcm_sock *bo = bcm_sk(sk);
795 /* we need a real device to send frames */
799 /* we need at least one can_frame */
800 if (msg_head->nframes < 1)
803 /* check the given can_id */
804 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
807 /* update existing BCM operation */
810 * Do we need more space for the can_frames than currently
811 * allocated? -> This is a _really_ unusual use-case and
812 * therefore (complexity / locking) it is not supported.
814 if (msg_head->nframes > op->nframes)
817 /* update can_frames content */
818 for (i = 0; i < msg_head->nframes; i++) {
819 err = memcpy_fromiovec((u8 *)&op->frames[i],
820 msg->msg_iov, CFSIZ);
824 if (msg_head->flags & TX_CP_CAN_ID) {
825 /* copy can_id into frame */
826 op->frames[i].can_id = msg_head->can_id;
831 /* insert new BCM operation for the given can_id */
833 op = kzalloc(OPSIZ, GFP_KERNEL);
837 op->can_id = msg_head->can_id;
839 /* create array for can_frames and copy the data */
840 if (msg_head->nframes > 1) {
841 op->frames = kmalloc(msg_head->nframes * CFSIZ,
848 op->frames = &op->sframe;
850 for (i = 0; i < msg_head->nframes; i++) {
851 err = memcpy_fromiovec((u8 *)&op->frames[i],
852 msg->msg_iov, CFSIZ);
854 if (op->frames != &op->sframe)
860 if (msg_head->flags & TX_CP_CAN_ID) {
861 /* copy can_id into frame */
862 op->frames[i].can_id = msg_head->can_id;
866 /* tx_ops never compare with previous received messages */
867 op->last_frames = NULL;
869 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
871 op->ifindex = ifindex;
873 /* initialize uninitialized (kzalloc) structure */
874 setup_timer(&op->timer, bcm_tx_timeout_handler,
877 /* currently unused in tx_ops */
878 init_timer(&op->thrtimer);
880 /* add this bcm_op to the list of the tx_ops */
881 list_add(&op->list, &bo->tx_ops);
883 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
885 if (op->nframes != msg_head->nframes) {
886 op->nframes = msg_head->nframes;
887 /* start multiple frame transmission with index 0 */
893 op->flags = msg_head->flags;
895 if (op->flags & TX_RESET_MULTI_IDX) {
896 /* start multiple frame transmission with index 0 */
900 if (op->flags & SETTIMER) {
901 /* set timer values */
902 op->count = msg_head->count;
903 op->ival1 = msg_head->ival1;
904 op->ival2 = msg_head->ival2;
905 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
906 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
908 /* disable an active timer due to zero values? */
909 if (!op->j_ival1 && !op->j_ival2)
910 del_timer(&op->timer);
913 if ((op->flags & STARTTIMER) &&
914 ((op->j_ival1 && op->count) || op->j_ival2)) {
916 /* spec: send can_frame when starting timer */
917 op->flags |= TX_ANNOUNCE;
919 if (op->j_ival1 && (op->count > 0)) {
920 /* op->count-- is done in bcm_tx_timeout_handler */
921 mod_timer(&op->timer, jiffies + op->j_ival1);
923 mod_timer(&op->timer, jiffies + op->j_ival2);
926 if (op->flags & TX_ANNOUNCE)
929 return msg_head->nframes * CFSIZ + MHSIZ;
933 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
935 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
936 int ifindex, struct sock *sk)
938 struct bcm_sock *bo = bcm_sk(sk);
943 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
944 /* be robust against wrong usage ... */
945 msg_head->flags |= RX_FILTER_ID;
946 /* ignore trailing garbage */
947 msg_head->nframes = 0;
950 if ((msg_head->flags & RX_RTR_FRAME) &&
951 ((msg_head->nframes != 1) ||
952 (!(msg_head->can_id & CAN_RTR_FLAG))))
955 /* check the given can_id */
956 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
958 /* update existing BCM operation */
961 * Do we need more space for the can_frames than currently
962 * allocated? -> This is a _really_ unusual use-case and
963 * therefore (complexity / locking) it is not supported.
965 if (msg_head->nframes > op->nframes)
968 if (msg_head->nframes) {
969 /* update can_frames content */
970 err = memcpy_fromiovec((u8 *)op->frames,
972 msg_head->nframes * CFSIZ);
976 /* clear last_frames to indicate 'nothing received' */
977 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
980 op->nframes = msg_head->nframes;
982 /* Only an update -> do not call can_rx_register() */
986 /* insert new BCM operation for the given can_id */
987 op = kzalloc(OPSIZ, GFP_KERNEL);
991 op->can_id = msg_head->can_id;
992 op->nframes = msg_head->nframes;
994 if (msg_head->nframes > 1) {
995 /* create array for can_frames and copy the data */
996 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1003 /* create and init array for received can_frames */
1004 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1006 if (!op->last_frames) {
1013 op->frames = &op->sframe;
1014 op->last_frames = &op->last_sframe;
1017 if (msg_head->nframes) {
1018 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1019 msg_head->nframes * CFSIZ);
1021 if (op->frames != &op->sframe)
1023 if (op->last_frames != &op->last_sframe)
1024 kfree(op->last_frames);
1030 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1032 op->ifindex = ifindex;
1034 /* initialize uninitialized (kzalloc) structure */
1035 setup_timer(&op->timer, bcm_rx_timeout_handler,
1038 /* init throttle timer for RX_CHANGED */
1039 setup_timer(&op->thrtimer, bcm_rx_thr_handler,
1042 /* mark disabled timer */
1043 op->thrtimer.expires = 0;
1045 /* add this bcm_op to the list of the rx_ops */
1046 list_add(&op->list, &bo->rx_ops);
1048 /* call can_rx_register() */
1051 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1054 op->flags = msg_head->flags;
1056 if (op->flags & RX_RTR_FRAME) {
1058 /* no timers in RTR-mode */
1059 del_timer(&op->thrtimer);
1060 del_timer(&op->timer);
1063 * funny feature in RX(!)_SETUP only for RTR-mode:
1064 * copy can_id into frame BUT without RTR-flag to
1065 * prevent a full-load-loopback-test ... ;-]
1067 if ((op->flags & TX_CP_CAN_ID) ||
1068 (op->frames[0].can_id == op->can_id))
1069 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1072 if (op->flags & SETTIMER) {
1074 /* set timer value */
1075 op->ival1 = msg_head->ival1;
1076 op->ival2 = msg_head->ival2;
1077 op->j_ival1 = rounded_tv2jif(&msg_head->ival1);
1078 op->j_ival2 = rounded_tv2jif(&msg_head->ival2);
1080 /* disable an active timer due to zero value? */
1082 del_timer(&op->timer);
1084 /* free currently blocked msgs ? */
1085 if (op->thrtimer.expires) {
1086 /* send blocked msgs hereafter */
1087 mod_timer(&op->thrtimer, jiffies + 2);
1091 * if (op->j_ival2) is zero, no (new) throttling
1092 * will happen. For details see functions
1093 * bcm_rx_update_and_send() and bcm_rx_thr_handler()
1097 if ((op->flags & STARTTIMER) && op->j_ival1)
1098 mod_timer(&op->timer, jiffies + op->j_ival1);
1101 /* now we can register for can_ids, if we added a new bcm_op */
1102 if (do_rx_register) {
1104 struct net_device *dev;
1106 dev = dev_get_by_index(&init_net, ifindex);
1108 err = can_rx_register(dev, op->can_id,
1109 REGMASK(op->can_id),
1113 op->rx_reg_dev = dev;
1118 err = can_rx_register(NULL, op->can_id,
1119 REGMASK(op->can_id),
1120 bcm_rx_handler, op, "bcm");
1122 /* this bcm rx op is broken -> remove it */
1123 list_del(&op->list);
1129 return msg_head->nframes * CFSIZ + MHSIZ;
1133 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1135 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1137 struct sk_buff *skb;
1138 struct net_device *dev;
1141 /* we need a real device to send frames */
1145 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1150 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1156 dev = dev_get_by_index(&init_net, ifindex);
1164 can_send(skb, 1); /* send with loopback */
1167 return CFSIZ + MHSIZ;
1171 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1173 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1174 struct msghdr *msg, size_t size)
1176 struct sock *sk = sock->sk;
1177 struct bcm_sock *bo = bcm_sk(sk);
1178 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1179 struct bcm_msg_head msg_head;
1180 int ret; /* read bytes or error codes as return value */
1185 /* check for alternative ifindex for this bcm_op */
1187 if (!ifindex && msg->msg_name) {
1188 /* no bound device as default => check msg_name */
1189 struct sockaddr_can *addr =
1190 (struct sockaddr_can *)msg->msg_name;
1192 if (addr->can_family != AF_CAN)
1195 /* ifindex from sendto() */
1196 ifindex = addr->can_ifindex;
1199 struct net_device *dev;
1201 dev = dev_get_by_index(&init_net, ifindex);
1205 if (dev->type != ARPHRD_CAN) {
1214 /* read message head information */
1216 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1222 switch (msg_head.opcode) {
1225 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1229 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1233 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1240 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1247 /* reuse msg_head for the reply to TX_READ */
1248 msg_head.opcode = TX_STATUS;
1249 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1253 /* reuse msg_head for the reply to RX_READ */
1254 msg_head.opcode = RX_STATUS;
1255 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1259 /* we need at least one can_frame */
1260 if (msg_head.nframes < 1)
1263 ret = bcm_tx_send(msg, ifindex, sk);
1277 * notification handler for netdevice status changes
1279 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1282 struct net_device *dev = (struct net_device *)data;
1283 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1284 struct sock *sk = &bo->sk;
1286 int notify_enodev = 0;
1288 if (dev->nd_net != &init_net)
1291 if (dev->type != ARPHRD_CAN)
1296 case NETDEV_UNREGISTER:
1299 /* remove device specific receive entries */
1300 list_for_each_entry(op, &bo->rx_ops, list)
1301 if (op->rx_reg_dev == dev)
1302 bcm_rx_unreg(dev, op);
1304 /* remove device reference, if this is our bound device */
1305 if (bo->bound && bo->ifindex == dev->ifindex) {
1313 if (notify_enodev) {
1314 sk->sk_err = ENODEV;
1315 if (!sock_flag(sk, SOCK_DEAD))
1316 sk->sk_error_report(sk);
1321 if (bo->bound && bo->ifindex == dev->ifindex) {
1322 sk->sk_err = ENETDOWN;
1323 if (!sock_flag(sk, SOCK_DEAD))
1324 sk->sk_error_report(sk);
1332 * initial settings for all BCM sockets to be set at socket creation time
1334 static int bcm_init(struct sock *sk)
1336 struct bcm_sock *bo = bcm_sk(sk);
1340 bo->dropped_usr_msgs = 0;
1341 bo->bcm_proc_read = NULL;
1343 INIT_LIST_HEAD(&bo->tx_ops);
1344 INIT_LIST_HEAD(&bo->rx_ops);
1347 bo->notifier.notifier_call = bcm_notifier;
1349 register_netdevice_notifier(&bo->notifier);
1355 * standard socket functions
1357 static int bcm_release(struct socket *sock)
1359 struct sock *sk = sock->sk;
1360 struct bcm_sock *bo = bcm_sk(sk);
1361 struct bcm_op *op, *next;
1363 /* remove bcm_ops, timer, rx_unregister(), etc. */
1365 unregister_netdevice_notifier(&bo->notifier);
1369 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1372 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1374 * Don't care if we're bound or not (due to netdev problems)
1375 * can_rx_unregister() is always a save thing to do here.
1379 * Only remove subscriptions that had not
1380 * been removed due to NETDEV_UNREGISTER
1383 if (op->rx_reg_dev) {
1384 struct net_device *dev;
1386 dev = dev_get_by_index(&init_net, op->ifindex);
1388 bcm_rx_unreg(dev, op);
1393 can_rx_unregister(NULL, op->can_id,
1394 REGMASK(op->can_id),
1395 bcm_rx_handler, op);
1400 /* remove procfs entry */
1401 if (proc_dir && bo->bcm_proc_read)
1402 remove_proc_entry(bo->procname, proc_dir);
1404 /* remove device reference */
1416 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1419 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1420 struct sock *sk = sock->sk;
1421 struct bcm_sock *bo = bcm_sk(sk);
1426 /* bind a device to this socket */
1427 if (addr->can_ifindex) {
1428 struct net_device *dev;
1430 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1434 if (dev->type != ARPHRD_CAN) {
1439 bo->ifindex = dev->ifindex;
1443 /* no interface reference for ifindex = 0 ('any' CAN device) */
1450 /* unique socket address as filename */
1451 sprintf(bo->procname, "%p", sock);
1452 bo->bcm_proc_read = create_proc_read_entry(bo->procname, 0644,
1460 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1461 struct msghdr *msg, size_t size, int flags)
1463 struct sock *sk = sock->sk;
1464 struct sk_buff *skb;
1469 noblock = flags & MSG_DONTWAIT;
1470 flags &= ~MSG_DONTWAIT;
1471 skb = skb_recv_datagram(sk, flags, noblock, &error);
1475 if (skb->len < size)
1478 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1480 skb_free_datagram(sk, skb);
1484 sock_recv_timestamp(msg, sk, skb);
1486 if (msg->msg_name) {
1487 msg->msg_namelen = sizeof(struct sockaddr_can);
1488 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1491 skb_free_datagram(sk, skb);
1496 static struct proto_ops bcm_ops __read_mostly = {
1498 .release = bcm_release,
1499 .bind = sock_no_bind,
1500 .connect = bcm_connect,
1501 .socketpair = sock_no_socketpair,
1502 .accept = sock_no_accept,
1503 .getname = sock_no_getname,
1504 .poll = datagram_poll,
1505 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1506 .listen = sock_no_listen,
1507 .shutdown = sock_no_shutdown,
1508 .setsockopt = sock_no_setsockopt,
1509 .getsockopt = sock_no_getsockopt,
1510 .sendmsg = bcm_sendmsg,
1511 .recvmsg = bcm_recvmsg,
1512 .mmap = sock_no_mmap,
1513 .sendpage = sock_no_sendpage,
1516 static struct proto bcm_proto __read_mostly = {
1518 .owner = THIS_MODULE,
1519 .obj_size = sizeof(struct bcm_sock),
1523 static struct can_proto bcm_can_proto __read_mostly = {
1525 .protocol = CAN_BCM,
1531 static int __init bcm_module_init(void)
1537 err = can_proto_register(&bcm_can_proto);
1539 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1543 /* create /proc/net/can-bcm directory */
1544 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1547 proc_dir->owner = THIS_MODULE;
1552 static void __exit bcm_module_exit(void)
1554 can_proto_unregister(&bcm_can_proto);
1557 proc_net_remove(&init_net, "can-bcm");
1560 module_init(bcm_module_init);
1561 module_exit(bcm_module_exit);