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/hrtimer.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.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>
59 #include <linux/slab.h>
61 #include <net/net_namespace.h>
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 " t)\n";
77 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
78 MODULE_LICENSE("Dual BSD/GPL");
79 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
80 MODULE_ALIAS("can-proto-2");
82 /* easy access to can_frame payload */
83 static inline u64 GET_U64(const struct can_frame *cp)
85 return *(u64 *)cp->data;
89 struct list_head list;
93 unsigned long frames_abs, frames_filtered;
94 struct timeval ival1, ival2;
95 struct hrtimer timer, thrtimer;
96 struct tasklet_struct tsklet, thrtsklet;
97 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
102 struct can_frame *frames;
103 struct can_frame *last_frames;
104 struct can_frame sframe;
105 struct can_frame last_sframe;
107 struct net_device *rx_reg_dev;
110 static struct proc_dir_entry *proc_dir;
116 struct notifier_block notifier;
117 struct list_head rx_ops;
118 struct list_head tx_ops;
119 unsigned long dropped_usr_msgs;
120 struct proc_dir_entry *bcm_proc_read;
121 char procname [9]; /* pointer printed in ASCII with \0 */
124 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
126 return (struct bcm_sock *)sk;
129 #define CFSIZ sizeof(struct can_frame)
130 #define OPSIZ sizeof(struct bcm_op)
131 #define MHSIZ sizeof(struct bcm_msg_head)
136 static char *bcm_proc_getifname(char *result, int ifindex)
138 struct net_device *dev;
144 dev = dev_get_by_index_rcu(&init_net, ifindex);
146 strcpy(result, dev->name);
148 strcpy(result, "???");
154 static int bcm_proc_show(struct seq_file *m, void *v)
156 char ifname[IFNAMSIZ];
157 struct sock *sk = (struct sock *)m->private;
158 struct bcm_sock *bo = bcm_sk(sk);
161 seq_printf(m, ">>> socket %p", sk->sk_socket);
162 seq_printf(m, " / sk %p", sk);
163 seq_printf(m, " / bo %p", bo);
164 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
165 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
166 seq_printf(m, " <<<\n");
168 list_for_each_entry(op, &bo->rx_ops, list) {
170 unsigned long reduction;
172 /* print only active entries & prevent division by zero */
176 seq_printf(m, "rx_op: %03X %-5s ",
177 op->can_id, bcm_proc_getifname(ifname, op->ifindex));
178 seq_printf(m, "[%d]%c ", op->nframes,
179 (op->flags & RX_CHECK_DLC)?'d':' ');
180 if (op->kt_ival1.tv64)
181 seq_printf(m, "timeo=%lld ",
183 ktime_to_us(op->kt_ival1));
185 if (op->kt_ival2.tv64)
186 seq_printf(m, "thr=%lld ",
188 ktime_to_us(op->kt_ival2));
190 seq_printf(m, "# recv %ld (%ld) => reduction: ",
191 op->frames_filtered, op->frames_abs);
193 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
195 seq_printf(m, "%s%ld%%\n",
196 (reduction == 100)?"near ":"", reduction);
199 list_for_each_entry(op, &bo->tx_ops, list) {
201 seq_printf(m, "tx_op: %03X %s [%d] ",
203 bcm_proc_getifname(ifname, op->ifindex),
206 if (op->kt_ival1.tv64)
207 seq_printf(m, "t1=%lld ",
208 (long long) ktime_to_us(op->kt_ival1));
210 if (op->kt_ival2.tv64)
211 seq_printf(m, "t2=%lld ",
212 (long long) ktime_to_us(op->kt_ival2));
214 seq_printf(m, "# sent %ld\n", op->frames_abs);
220 static int bcm_proc_open(struct inode *inode, struct file *file)
222 return single_open(file, bcm_proc_show, PDE(inode)->data);
225 static const struct file_operations bcm_proc_fops = {
226 .owner = THIS_MODULE,
227 .open = bcm_proc_open,
230 .release = single_release,
234 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
235 * of the given bcm tx op
237 static void bcm_can_tx(struct bcm_op *op)
240 struct net_device *dev;
241 struct can_frame *cf = &op->frames[op->currframe];
243 /* no target device? => exit */
247 dev = dev_get_by_index(&init_net, op->ifindex);
249 /* RFC: should this bcm_op remove itself here? */
253 skb = alloc_skb(CFSIZ, gfp_any());
257 memcpy(skb_put(skb, CFSIZ), cf, CFSIZ);
259 /* send with loopback */
264 /* update statistics */
268 /* reached last frame? */
269 if (op->currframe >= op->nframes)
276 * bcm_send_to_user - send a BCM message to the userspace
277 * (consisting of bcm_msg_head + x CAN frames)
279 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
280 struct can_frame *frames, int has_timestamp)
283 struct can_frame *firstframe;
284 struct sockaddr_can *addr;
285 struct sock *sk = op->sk;
286 int datalen = head->nframes * CFSIZ;
289 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
293 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
296 /* can_frames starting here */
297 firstframe = (struct can_frame *)skb_tail_pointer(skb);
299 memcpy(skb_put(skb, datalen), frames, datalen);
302 * the BCM uses the can_dlc-element of the can_frame
303 * structure for internal purposes. This is only
304 * relevant for updates that are generated by the
305 * BCM, where nframes is 1
307 if (head->nframes == 1)
308 firstframe->can_dlc &= BCM_CAN_DLC_MASK;
312 /* restore rx timestamp */
313 skb->tstamp = op->rx_stamp;
317 * Put the datagram to the queue so that bcm_recvmsg() can
318 * get it from there. We need to pass the interface index to
319 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
320 * containing the interface index.
323 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
324 addr = (struct sockaddr_can *)skb->cb;
325 memset(addr, 0, sizeof(*addr));
326 addr->can_family = AF_CAN;
327 addr->can_ifindex = op->rx_ifindex;
329 err = sock_queue_rcv_skb(sk, skb);
331 struct bcm_sock *bo = bcm_sk(sk);
334 /* don't care about overflows in this statistic */
335 bo->dropped_usr_msgs++;
339 static void bcm_tx_timeout_tsklet(unsigned long data)
341 struct bcm_op *op = (struct bcm_op *)data;
342 struct bcm_msg_head msg_head;
344 if (op->kt_ival1.tv64 && (op->count > 0)) {
347 if (!op->count && (op->flags & TX_COUNTEVT)) {
349 /* create notification to user */
350 msg_head.opcode = TX_EXPIRED;
351 msg_head.flags = op->flags;
352 msg_head.count = op->count;
353 msg_head.ival1 = op->ival1;
354 msg_head.ival2 = op->ival2;
355 msg_head.can_id = op->can_id;
356 msg_head.nframes = 0;
358 bcm_send_to_user(op, &msg_head, NULL, 0);
362 if (op->kt_ival1.tv64 && (op->count > 0)) {
364 /* send (next) frame */
366 hrtimer_start(&op->timer,
367 ktime_add(ktime_get(), op->kt_ival1),
371 if (op->kt_ival2.tv64) {
373 /* send (next) frame */
375 hrtimer_start(&op->timer,
376 ktime_add(ktime_get(), op->kt_ival2),
383 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
385 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
387 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
389 tasklet_schedule(&op->tsklet);
391 return HRTIMER_NORESTART;
395 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
397 static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
399 struct bcm_msg_head head;
401 /* update statistics */
402 op->frames_filtered++;
404 /* prevent statistics overflow */
405 if (op->frames_filtered > ULONG_MAX/100)
406 op->frames_filtered = op->frames_abs = 0;
408 /* this element is not throttled anymore */
409 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
411 head.opcode = RX_CHANGED;
412 head.flags = op->flags;
413 head.count = op->count;
414 head.ival1 = op->ival1;
415 head.ival2 = op->ival2;
416 head.can_id = op->can_id;
419 bcm_send_to_user(op, &head, data, 1);
423 * bcm_rx_update_and_send - process a detected relevant receive content change
424 * 1. update the last received data
425 * 2. send a notification to the user (if possible)
427 static void bcm_rx_update_and_send(struct bcm_op *op,
428 struct can_frame *lastdata,
429 const struct can_frame *rxdata)
431 memcpy(lastdata, rxdata, CFSIZ);
433 /* mark as used and throttled by default */
434 lastdata->can_dlc |= (RX_RECV|RX_THR);
436 /* throtteling mode inactive ? */
437 if (!op->kt_ival2.tv64) {
438 /* send RX_CHANGED to the user immediately */
439 bcm_rx_changed(op, lastdata);
443 /* with active throttling timer we are just done here */
444 if (hrtimer_active(&op->thrtimer))
447 /* first receiption with enabled throttling mode */
448 if (!op->kt_lastmsg.tv64)
449 goto rx_changed_settime;
451 /* got a second frame inside a potential throttle period? */
452 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
453 ktime_to_us(op->kt_ival2)) {
454 /* do not send the saved data - only start throttle timer */
455 hrtimer_start(&op->thrtimer,
456 ktime_add(op->kt_lastmsg, op->kt_ival2),
461 /* the gap was that big, that throttling was not needed here */
463 bcm_rx_changed(op, lastdata);
464 op->kt_lastmsg = ktime_get();
468 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
469 * received data stored in op->last_frames[]
471 static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
472 const struct can_frame *rxdata)
475 * no one uses the MSBs of can_dlc for comparation,
476 * so we use it here to detect the first time of reception
479 if (!(op->last_frames[index].can_dlc & RX_RECV)) {
480 /* received data for the first time => send update to user */
481 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
485 /* do a real check in can_frame data section */
487 if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) !=
488 (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) {
489 bcm_rx_update_and_send(op, &op->last_frames[index], rxdata);
493 if (op->flags & RX_CHECK_DLC) {
494 /* do a real check in can_frame dlc */
495 if (rxdata->can_dlc != (op->last_frames[index].can_dlc &
497 bcm_rx_update_and_send(op, &op->last_frames[index],
505 * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption
507 static void bcm_rx_starttimer(struct bcm_op *op)
509 if (op->flags & RX_NO_AUTOTIMER)
512 if (op->kt_ival1.tv64)
513 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
516 static void bcm_rx_timeout_tsklet(unsigned long data)
518 struct bcm_op *op = (struct bcm_op *)data;
519 struct bcm_msg_head msg_head;
521 /* create notification to user */
522 msg_head.opcode = RX_TIMEOUT;
523 msg_head.flags = op->flags;
524 msg_head.count = op->count;
525 msg_head.ival1 = op->ival1;
526 msg_head.ival2 = op->ival2;
527 msg_head.can_id = op->can_id;
528 msg_head.nframes = 0;
530 bcm_send_to_user(op, &msg_head, NULL, 0);
534 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
536 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
538 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
540 /* schedule before NET_RX_SOFTIRQ */
541 tasklet_hi_schedule(&op->tsklet);
543 /* no restart of the timer is done here! */
545 /* if user wants to be informed, when cyclic CAN-Messages come back */
546 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
547 /* clear received can_frames to indicate 'nothing received' */
548 memset(op->last_frames, 0, op->nframes * CFSIZ);
551 return HRTIMER_NORESTART;
555 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
557 static inline int bcm_rx_do_flush(struct bcm_op *op, int update, int index)
559 if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) {
561 bcm_rx_changed(op, &op->last_frames[index]);
568 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
570 * update == 0 : just check if throttled data is available (any irq context)
571 * update == 1 : check and send throttled data to userspace (soft_irq context)
573 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
577 if (op->nframes > 1) {
580 /* for MUX filter we start at index 1 */
581 for (i = 1; i < op->nframes; i++)
582 updated += bcm_rx_do_flush(op, update, i);
585 /* for RX_FILTER_ID and simple filter */
586 updated += bcm_rx_do_flush(op, update, 0);
592 static void bcm_rx_thr_tsklet(unsigned long data)
594 struct bcm_op *op = (struct bcm_op *)data;
596 /* push the changed data to the userspace */
597 bcm_rx_thr_flush(op, 1);
601 * bcm_rx_thr_handler - the time for blocked content updates is over now:
602 * Check for throttled data and send it to the userspace
604 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
606 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
608 tasklet_schedule(&op->thrtsklet);
610 if (bcm_rx_thr_flush(op, 0)) {
611 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
612 return HRTIMER_RESTART;
614 /* rearm throttle handling */
615 op->kt_lastmsg = ktime_set(0, 0);
616 return HRTIMER_NORESTART;
621 * bcm_rx_handler - handle a CAN frame receiption
623 static void bcm_rx_handler(struct sk_buff *skb, void *data)
625 struct bcm_op *op = (struct bcm_op *)data;
626 const struct can_frame *rxframe = (struct can_frame *)skb->data;
629 /* disable timeout */
630 hrtimer_cancel(&op->timer);
632 if (op->can_id != rxframe->can_id)
635 /* save rx timestamp */
636 op->rx_stamp = skb->tstamp;
637 /* save originator for recvfrom() */
638 op->rx_ifindex = skb->dev->ifindex;
639 /* update statistics */
642 if (op->flags & RX_RTR_FRAME) {
643 /* send reply for RTR-request (placed in op->frames[0]) */
648 if (op->flags & RX_FILTER_ID) {
649 /* the easiest case */
650 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
654 if (op->nframes == 1) {
655 /* simple compare with index 0 */
656 bcm_rx_cmp_to_index(op, 0, rxframe);
660 if (op->nframes > 1) {
664 * find the first multiplex mask that fits.
665 * Remark: The MUX-mask is stored in index 0
668 for (i = 1; i < op->nframes; i++) {
669 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
670 (GET_U64(&op->frames[0]) &
671 GET_U64(&op->frames[i]))) {
672 bcm_rx_cmp_to_index(op, i, rxframe);
679 bcm_rx_starttimer(op);
683 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
685 static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id,
690 list_for_each_entry(op, ops, list) {
691 if ((op->can_id == can_id) && (op->ifindex == ifindex))
698 static void bcm_remove_op(struct bcm_op *op)
700 hrtimer_cancel(&op->timer);
701 hrtimer_cancel(&op->thrtimer);
704 tasklet_kill(&op->tsklet);
706 if (op->thrtsklet.func)
707 tasklet_kill(&op->thrtsklet);
709 if ((op->frames) && (op->frames != &op->sframe))
712 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
713 kfree(op->last_frames);
720 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
722 if (op->rx_reg_dev == dev) {
723 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
726 /* mark as removed subscription */
727 op->rx_reg_dev = NULL;
729 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
730 "mismatch %p %p\n", op->rx_reg_dev, dev);
734 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
736 static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
738 struct bcm_op *op, *n;
740 list_for_each_entry_safe(op, n, ops, list) {
741 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
744 * Don't care if we're bound or not (due to netdev
745 * problems) can_rx_unregister() is always a save
750 * Only remove subscriptions that had not
751 * been removed due to NETDEV_UNREGISTER
754 if (op->rx_reg_dev) {
755 struct net_device *dev;
757 dev = dev_get_by_index(&init_net,
760 bcm_rx_unreg(dev, op);
765 can_rx_unregister(NULL, op->can_id,
775 return 0; /* not found */
779 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
781 static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
783 struct bcm_op *op, *n;
785 list_for_each_entry_safe(op, n, ops, list) {
786 if ((op->can_id == can_id) && (op->ifindex == ifindex)) {
793 return 0; /* not found */
797 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
799 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
802 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex);
807 /* put current values into msg_head */
808 msg_head->flags = op->flags;
809 msg_head->count = op->count;
810 msg_head->ival1 = op->ival1;
811 msg_head->ival2 = op->ival2;
812 msg_head->nframes = op->nframes;
814 bcm_send_to_user(op, msg_head, op->frames, 0);
820 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
822 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
823 int ifindex, struct sock *sk)
825 struct bcm_sock *bo = bcm_sk(sk);
829 /* we need a real device to send frames */
833 /* we need at least one can_frame */
834 if (msg_head->nframes < 1)
837 /* check the given can_id */
838 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex);
841 /* update existing BCM operation */
844 * Do we need more space for the can_frames than currently
845 * allocated? -> This is a _really_ unusual use-case and
846 * therefore (complexity / locking) it is not supported.
848 if (msg_head->nframes > op->nframes)
851 /* update can_frames content */
852 for (i = 0; i < msg_head->nframes; i++) {
853 err = memcpy_fromiovec((u8 *)&op->frames[i],
854 msg->msg_iov, CFSIZ);
856 if (op->frames[i].can_dlc > 8)
862 if (msg_head->flags & TX_CP_CAN_ID) {
863 /* copy can_id into frame */
864 op->frames[i].can_id = msg_head->can_id;
869 /* insert new BCM operation for the given can_id */
871 op = kzalloc(OPSIZ, GFP_KERNEL);
875 op->can_id = msg_head->can_id;
877 /* create array for can_frames and copy the data */
878 if (msg_head->nframes > 1) {
879 op->frames = kmalloc(msg_head->nframes * CFSIZ,
886 op->frames = &op->sframe;
888 for (i = 0; i < msg_head->nframes; i++) {
889 err = memcpy_fromiovec((u8 *)&op->frames[i],
890 msg->msg_iov, CFSIZ);
892 if (op->frames[i].can_dlc > 8)
896 if (op->frames != &op->sframe)
902 if (msg_head->flags & TX_CP_CAN_ID) {
903 /* copy can_id into frame */
904 op->frames[i].can_id = msg_head->can_id;
908 /* tx_ops never compare with previous received messages */
909 op->last_frames = NULL;
911 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
913 op->ifindex = ifindex;
915 /* initialize uninitialized (kzalloc) structure */
916 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
917 op->timer.function = bcm_tx_timeout_handler;
919 /* initialize tasklet for tx countevent notification */
920 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
923 /* currently unused in tx_ops */
924 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
926 /* add this bcm_op to the list of the tx_ops */
927 list_add(&op->list, &bo->tx_ops);
929 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
931 if (op->nframes != msg_head->nframes) {
932 op->nframes = msg_head->nframes;
933 /* start multiple frame transmission with index 0 */
939 op->flags = msg_head->flags;
941 if (op->flags & TX_RESET_MULTI_IDX) {
942 /* start multiple frame transmission with index 0 */
946 if (op->flags & SETTIMER) {
947 /* set timer values */
948 op->count = msg_head->count;
949 op->ival1 = msg_head->ival1;
950 op->ival2 = msg_head->ival2;
951 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
952 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
954 /* disable an active timer due to zero values? */
955 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
956 hrtimer_cancel(&op->timer);
959 if ((op->flags & STARTTIMER) &&
960 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
962 /* spec: send can_frame when starting timer */
963 op->flags |= TX_ANNOUNCE;
965 if (op->kt_ival1.tv64 && (op->count > 0)) {
966 /* op->count-- is done in bcm_tx_timeout_handler */
967 hrtimer_start(&op->timer, op->kt_ival1,
970 hrtimer_start(&op->timer, op->kt_ival2,
974 if (op->flags & TX_ANNOUNCE)
977 return msg_head->nframes * CFSIZ + MHSIZ;
981 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
983 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
984 int ifindex, struct sock *sk)
986 struct bcm_sock *bo = bcm_sk(sk);
991 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
992 /* be robust against wrong usage ... */
993 msg_head->flags |= RX_FILTER_ID;
994 /* ignore trailing garbage */
995 msg_head->nframes = 0;
998 if ((msg_head->flags & RX_RTR_FRAME) &&
999 ((msg_head->nframes != 1) ||
1000 (!(msg_head->can_id & CAN_RTR_FLAG))))
1003 /* check the given can_id */
1004 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex);
1006 /* update existing BCM operation */
1009 * Do we need more space for the can_frames than currently
1010 * allocated? -> This is a _really_ unusual use-case and
1011 * therefore (complexity / locking) it is not supported.
1013 if (msg_head->nframes > op->nframes)
1016 if (msg_head->nframes) {
1017 /* update can_frames content */
1018 err = memcpy_fromiovec((u8 *)op->frames,
1020 msg_head->nframes * CFSIZ);
1024 /* clear last_frames to indicate 'nothing received' */
1025 memset(op->last_frames, 0, msg_head->nframes * CFSIZ);
1028 op->nframes = msg_head->nframes;
1030 /* Only an update -> do not call can_rx_register() */
1034 /* insert new BCM operation for the given can_id */
1035 op = kzalloc(OPSIZ, GFP_KERNEL);
1039 op->can_id = msg_head->can_id;
1040 op->nframes = msg_head->nframes;
1042 if (msg_head->nframes > 1) {
1043 /* create array for can_frames and copy the data */
1044 op->frames = kmalloc(msg_head->nframes * CFSIZ,
1051 /* create and init array for received can_frames */
1052 op->last_frames = kzalloc(msg_head->nframes * CFSIZ,
1054 if (!op->last_frames) {
1061 op->frames = &op->sframe;
1062 op->last_frames = &op->last_sframe;
1065 if (msg_head->nframes) {
1066 err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov,
1067 msg_head->nframes * CFSIZ);
1069 if (op->frames != &op->sframe)
1071 if (op->last_frames != &op->last_sframe)
1072 kfree(op->last_frames);
1078 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1080 op->ifindex = ifindex;
1082 /* initialize uninitialized (kzalloc) structure */
1083 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1084 op->timer.function = bcm_rx_timeout_handler;
1086 /* initialize tasklet for rx timeout notification */
1087 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1088 (unsigned long) op);
1090 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1091 op->thrtimer.function = bcm_rx_thr_handler;
1093 /* initialize tasklet for rx throttle handling */
1094 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1095 (unsigned long) op);
1097 /* add this bcm_op to the list of the rx_ops */
1098 list_add(&op->list, &bo->rx_ops);
1100 /* call can_rx_register() */
1103 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1106 op->flags = msg_head->flags;
1108 if (op->flags & RX_RTR_FRAME) {
1110 /* no timers in RTR-mode */
1111 hrtimer_cancel(&op->thrtimer);
1112 hrtimer_cancel(&op->timer);
1115 * funny feature in RX(!)_SETUP only for RTR-mode:
1116 * copy can_id into frame BUT without RTR-flag to
1117 * prevent a full-load-loopback-test ... ;-]
1119 if ((op->flags & TX_CP_CAN_ID) ||
1120 (op->frames[0].can_id == op->can_id))
1121 op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG;
1124 if (op->flags & SETTIMER) {
1126 /* set timer value */
1127 op->ival1 = msg_head->ival1;
1128 op->ival2 = msg_head->ival2;
1129 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1130 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
1132 /* disable an active timer due to zero value? */
1133 if (!op->kt_ival1.tv64)
1134 hrtimer_cancel(&op->timer);
1137 * In any case cancel the throttle timer, flush
1138 * potentially blocked msgs and reset throttle handling
1140 op->kt_lastmsg = ktime_set(0, 0);
1141 hrtimer_cancel(&op->thrtimer);
1142 bcm_rx_thr_flush(op, 1);
1145 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1146 hrtimer_start(&op->timer, op->kt_ival1,
1150 /* now we can register for can_ids, if we added a new bcm_op */
1151 if (do_rx_register) {
1153 struct net_device *dev;
1155 dev = dev_get_by_index(&init_net, ifindex);
1157 err = can_rx_register(dev, op->can_id,
1158 REGMASK(op->can_id),
1162 op->rx_reg_dev = dev;
1167 err = can_rx_register(NULL, op->can_id,
1168 REGMASK(op->can_id),
1169 bcm_rx_handler, op, "bcm");
1171 /* this bcm rx op is broken -> remove it */
1172 list_del(&op->list);
1178 return msg_head->nframes * CFSIZ + MHSIZ;
1182 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1184 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1186 struct sk_buff *skb;
1187 struct net_device *dev;
1190 /* we need a real device to send frames */
1194 skb = alloc_skb(CFSIZ, GFP_KERNEL);
1199 err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ);
1205 dev = dev_get_by_index(&init_net, ifindex);
1213 err = can_send(skb, 1); /* send with loopback */
1219 return CFSIZ + MHSIZ;
1223 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1225 static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock,
1226 struct msghdr *msg, size_t size)
1228 struct sock *sk = sock->sk;
1229 struct bcm_sock *bo = bcm_sk(sk);
1230 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1231 struct bcm_msg_head msg_head;
1232 int ret; /* read bytes or error codes as return value */
1237 /* check for valid message length from userspace */
1238 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1241 /* check for alternative ifindex for this bcm_op */
1243 if (!ifindex && msg->msg_name) {
1244 /* no bound device as default => check msg_name */
1245 struct sockaddr_can *addr =
1246 (struct sockaddr_can *)msg->msg_name;
1248 if (addr->can_family != AF_CAN)
1251 /* ifindex from sendto() */
1252 ifindex = addr->can_ifindex;
1255 struct net_device *dev;
1257 dev = dev_get_by_index(&init_net, ifindex);
1261 if (dev->type != ARPHRD_CAN) {
1270 /* read message head information */
1272 ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ);
1278 switch (msg_head.opcode) {
1281 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1285 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1289 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex))
1296 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex))
1303 /* reuse msg_head for the reply to TX_READ */
1304 msg_head.opcode = TX_STATUS;
1305 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1309 /* reuse msg_head for the reply to RX_READ */
1310 msg_head.opcode = RX_STATUS;
1311 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1315 /* we need exactly one can_frame behind the msg head */
1316 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1319 ret = bcm_tx_send(msg, ifindex, sk);
1333 * notification handler for netdevice status changes
1335 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1338 struct net_device *dev = (struct net_device *)data;
1339 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1340 struct sock *sk = &bo->sk;
1342 int notify_enodev = 0;
1344 if (!net_eq(dev_net(dev), &init_net))
1347 if (dev->type != ARPHRD_CAN)
1352 case NETDEV_UNREGISTER:
1355 /* remove device specific receive entries */
1356 list_for_each_entry(op, &bo->rx_ops, list)
1357 if (op->rx_reg_dev == dev)
1358 bcm_rx_unreg(dev, op);
1360 /* remove device reference, if this is our bound device */
1361 if (bo->bound && bo->ifindex == dev->ifindex) {
1369 if (notify_enodev) {
1370 sk->sk_err = ENODEV;
1371 if (!sock_flag(sk, SOCK_DEAD))
1372 sk->sk_error_report(sk);
1377 if (bo->bound && bo->ifindex == dev->ifindex) {
1378 sk->sk_err = ENETDOWN;
1379 if (!sock_flag(sk, SOCK_DEAD))
1380 sk->sk_error_report(sk);
1388 * initial settings for all BCM sockets to be set at socket creation time
1390 static int bcm_init(struct sock *sk)
1392 struct bcm_sock *bo = bcm_sk(sk);
1396 bo->dropped_usr_msgs = 0;
1397 bo->bcm_proc_read = NULL;
1399 INIT_LIST_HEAD(&bo->tx_ops);
1400 INIT_LIST_HEAD(&bo->rx_ops);
1403 bo->notifier.notifier_call = bcm_notifier;
1405 register_netdevice_notifier(&bo->notifier);
1411 * standard socket functions
1413 static int bcm_release(struct socket *sock)
1415 struct sock *sk = sock->sk;
1416 struct bcm_sock *bo = bcm_sk(sk);
1417 struct bcm_op *op, *next;
1419 /* remove bcm_ops, timer, rx_unregister(), etc. */
1421 unregister_netdevice_notifier(&bo->notifier);
1425 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1428 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1430 * Don't care if we're bound or not (due to netdev problems)
1431 * can_rx_unregister() is always a save thing to do here.
1435 * Only remove subscriptions that had not
1436 * been removed due to NETDEV_UNREGISTER
1439 if (op->rx_reg_dev) {
1440 struct net_device *dev;
1442 dev = dev_get_by_index(&init_net, op->ifindex);
1444 bcm_rx_unreg(dev, op);
1449 can_rx_unregister(NULL, op->can_id,
1450 REGMASK(op->can_id),
1451 bcm_rx_handler, op);
1456 /* remove procfs entry */
1457 if (proc_dir && bo->bcm_proc_read)
1458 remove_proc_entry(bo->procname, proc_dir);
1460 /* remove device reference */
1475 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1478 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1479 struct sock *sk = sock->sk;
1480 struct bcm_sock *bo = bcm_sk(sk);
1482 if (len < sizeof(*addr))
1488 /* bind a device to this socket */
1489 if (addr->can_ifindex) {
1490 struct net_device *dev;
1492 dev = dev_get_by_index(&init_net, addr->can_ifindex);
1496 if (dev->type != ARPHRD_CAN) {
1501 bo->ifindex = dev->ifindex;
1505 /* no interface reference for ifindex = 0 ('any' CAN device) */
1512 /* unique socket address as filename */
1513 sprintf(bo->procname, "%p", sock);
1514 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1516 &bcm_proc_fops, sk);
1522 static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock,
1523 struct msghdr *msg, size_t size, int flags)
1525 struct sock *sk = sock->sk;
1526 struct sk_buff *skb;
1531 noblock = flags & MSG_DONTWAIT;
1532 flags &= ~MSG_DONTWAIT;
1533 skb = skb_recv_datagram(sk, flags, noblock, &error);
1537 if (skb->len < size)
1540 err = memcpy_toiovec(msg->msg_iov, skb->data, size);
1542 skb_free_datagram(sk, skb);
1546 sock_recv_ts_and_drops(msg, sk, skb);
1548 if (msg->msg_name) {
1549 msg->msg_namelen = sizeof(struct sockaddr_can);
1550 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1553 skb_free_datagram(sk, skb);
1558 static struct proto_ops bcm_ops __read_mostly = {
1560 .release = bcm_release,
1561 .bind = sock_no_bind,
1562 .connect = bcm_connect,
1563 .socketpair = sock_no_socketpair,
1564 .accept = sock_no_accept,
1565 .getname = sock_no_getname,
1566 .poll = datagram_poll,
1567 .ioctl = NULL, /* use can_ioctl() from af_can.c */
1568 .listen = sock_no_listen,
1569 .shutdown = sock_no_shutdown,
1570 .setsockopt = sock_no_setsockopt,
1571 .getsockopt = sock_no_getsockopt,
1572 .sendmsg = bcm_sendmsg,
1573 .recvmsg = bcm_recvmsg,
1574 .mmap = sock_no_mmap,
1575 .sendpage = sock_no_sendpage,
1578 static struct proto bcm_proto __read_mostly = {
1580 .owner = THIS_MODULE,
1581 .obj_size = sizeof(struct bcm_sock),
1585 static struct can_proto bcm_can_proto __read_mostly = {
1587 .protocol = CAN_BCM,
1592 static int __init bcm_module_init(void)
1598 err = can_proto_register(&bcm_can_proto);
1600 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1604 /* create /proc/net/can-bcm directory */
1605 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1609 static void __exit bcm_module_exit(void)
1611 can_proto_unregister(&bcm_can_proto);
1614 proc_net_remove(&init_net, "can-bcm");
1617 module_init(bcm_module_init);
1618 module_exit(bcm_module_exit);