| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content | 
|  | 3 | * | 
|  | 4 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research | 
|  | 5 | * All rights reserved. | 
|  | 6 | * | 
|  | 7 | * Redistribution and use in source and binary forms, with or without | 
|  | 8 | * modification, are permitted provided that the following conditions | 
|  | 9 | * are met: | 
|  | 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. | 
|  | 18 | * | 
|  | 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. | 
|  | 23 | * | 
|  | 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. | 
|  | 26 | * | 
|  | 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 | 
|  | 38 | * DAMAGE. | 
|  | 39 | * | 
|  | 40 | * Send feedback to <socketcan-users@lists.berlios.de> | 
|  | 41 | * | 
|  | 42 | */ | 
|  | 43 |  | 
|  | 44 | #include <linux/module.h> | 
|  | 45 | #include <linux/init.h> | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 46 | #include <linux/hrtimer.h> | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 47 | #include <linux/list.h> | 
|  | 48 | #include <linux/proc_fs.h> | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 49 | #include <linux/seq_file.h> | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 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> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 59 | #include <linux/slab.h> | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 60 | #include <net/sock.h> | 
|  | 61 | #include <net/net_namespace.h> | 
|  | 62 |  | 
|  | 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 */ | 
|  | 67 |  | 
|  | 68 | /* get best masking value for can_rx_register() for a given single can_id */ | 
| Oliver Hartkopp | d253eee | 2008-12-03 15:52:35 -0800 | [diff] [blame] | 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)) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 72 |  | 
| Oliver Hartkopp | d253eee | 2008-12-03 15:52:35 -0800 | [diff] [blame] | 73 | #define CAN_BCM_VERSION CAN_VERSION | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 74 | static __initdata const char banner[] = KERN_INFO | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 75 | "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n"; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 76 |  | 
|  | 77 | MODULE_DESCRIPTION("PF_CAN broadcast manager protocol"); | 
|  | 78 | MODULE_LICENSE("Dual BSD/GPL"); | 
|  | 79 | MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); | 
| Lothar Waßmann | b13bb2e | 2009-07-14 23:12:25 +0000 | [diff] [blame] | 80 | MODULE_ALIAS("can-proto-2"); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 81 |  | 
|  | 82 | /* easy access to can_frame payload */ | 
|  | 83 | static inline u64 GET_U64(const struct can_frame *cp) | 
|  | 84 | { | 
|  | 85 | return *(u64 *)cp->data; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | struct bcm_op { | 
|  | 89 | struct list_head list; | 
|  | 90 | int ifindex; | 
|  | 91 | canid_t can_id; | 
|  | 92 | int flags; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 93 | unsigned long frames_abs, frames_filtered; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 94 | struct timeval ival1, ival2; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 95 | struct hrtimer timer, thrtimer; | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 96 | struct tasklet_struct tsklet, thrtsklet; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 97 | ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 98 | int rx_ifindex; | 
|  | 99 | int count; | 
|  | 100 | int nframes; | 
|  | 101 | int currframe; | 
|  | 102 | struct can_frame *frames; | 
|  | 103 | struct can_frame *last_frames; | 
|  | 104 | struct can_frame sframe; | 
|  | 105 | struct can_frame last_sframe; | 
|  | 106 | struct sock *sk; | 
|  | 107 | struct net_device *rx_reg_dev; | 
|  | 108 | }; | 
|  | 109 |  | 
|  | 110 | static struct proc_dir_entry *proc_dir; | 
|  | 111 |  | 
|  | 112 | struct bcm_sock { | 
|  | 113 | struct sock sk; | 
|  | 114 | int bound; | 
|  | 115 | int ifindex; | 
|  | 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 */ | 
|  | 122 | }; | 
|  | 123 |  | 
|  | 124 | static inline struct bcm_sock *bcm_sk(const struct sock *sk) | 
|  | 125 | { | 
|  | 126 | return (struct bcm_sock *)sk; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | #define CFSIZ sizeof(struct can_frame) | 
|  | 130 | #define OPSIZ sizeof(struct bcm_op) | 
|  | 131 | #define MHSIZ sizeof(struct bcm_msg_head) | 
|  | 132 |  | 
|  | 133 | /* | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 134 | * procfs functions | 
|  | 135 | */ | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 136 | static char *bcm_proc_getifname(char *result, int ifindex) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 137 | { | 
|  | 138 | struct net_device *dev; | 
|  | 139 |  | 
|  | 140 | if (!ifindex) | 
|  | 141 | return "any"; | 
|  | 142 |  | 
| stephen hemminger | ff879eb | 2009-11-10 07:54:56 +0000 | [diff] [blame] | 143 | rcu_read_lock(); | 
|  | 144 | dev = dev_get_by_index_rcu(&init_net, ifindex); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 145 | if (dev) | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 146 | strcpy(result, dev->name); | 
|  | 147 | else | 
|  | 148 | strcpy(result, "???"); | 
| stephen hemminger | ff879eb | 2009-11-10 07:54:56 +0000 | [diff] [blame] | 149 | rcu_read_unlock(); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 150 |  | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 151 | return result; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 154 | static int bcm_proc_show(struct seq_file *m, void *v) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 155 | { | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 156 | char ifname[IFNAMSIZ]; | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 157 | struct sock *sk = (struct sock *)m->private; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 158 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 159 | struct bcm_op *op; | 
|  | 160 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 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); | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 165 | seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex)); | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 166 | seq_printf(m, " <<<\n"); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 167 |  | 
|  | 168 | list_for_each_entry(op, &bo->rx_ops, list) { | 
|  | 169 |  | 
|  | 170 | unsigned long reduction; | 
|  | 171 |  | 
|  | 172 | /* print only active entries & prevent division by zero */ | 
|  | 173 | if (!op->frames_abs) | 
|  | 174 | continue; | 
|  | 175 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 176 | seq_printf(m, "rx_op: %03X %-5s ", | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 177 | op->can_id, bcm_proc_getifname(ifname, op->ifindex)); | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 178 | seq_printf(m, "[%d]%c ", op->nframes, | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 179 | (op->flags & RX_CHECK_DLC)?'d':' '); | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 180 | if (op->kt_ival1.tv64) | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 181 | seq_printf(m, "timeo=%lld ", | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 182 | (long long) | 
|  | 183 | ktime_to_us(op->kt_ival1)); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 184 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 185 | if (op->kt_ival2.tv64) | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 186 | seq_printf(m, "thr=%lld ", | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 187 | (long long) | 
|  | 188 | ktime_to_us(op->kt_ival2)); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 189 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 190 | seq_printf(m, "# recv %ld (%ld) => reduction: ", | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 191 | op->frames_filtered, op->frames_abs); | 
|  | 192 |  | 
|  | 193 | reduction = 100 - (op->frames_filtered * 100) / op->frames_abs; | 
|  | 194 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 195 | seq_printf(m, "%s%ld%%\n", | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 196 | (reduction == 100)?"near ":"", reduction); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 | list_for_each_entry(op, &bo->tx_ops, list) { | 
|  | 200 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 201 | seq_printf(m, "tx_op: %03X %s [%d] ", | 
| Eric Dumazet | 6755aeb | 2009-11-06 00:23:01 +0000 | [diff] [blame] | 202 | op->can_id, | 
|  | 203 | bcm_proc_getifname(ifname, op->ifindex), | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 204 | op->nframes); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 205 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 206 | if (op->kt_ival1.tv64) | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 207 | seq_printf(m, "t1=%lld ", | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 208 | (long long) ktime_to_us(op->kt_ival1)); | 
|  | 209 |  | 
|  | 210 | if (op->kt_ival2.tv64) | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 211 | seq_printf(m, "t2=%lld ", | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 212 | (long long) ktime_to_us(op->kt_ival2)); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 213 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 214 | seq_printf(m, "# sent %ld\n", op->frames_abs); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 215 | } | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 216 | seq_putc(m, '\n'); | 
|  | 217 | return 0; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 220 | static int bcm_proc_open(struct inode *inode, struct file *file) | 
|  | 221 | { | 
|  | 222 | return single_open(file, bcm_proc_show, PDE(inode)->data); | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | static const struct file_operations bcm_proc_fops = { | 
|  | 226 | .owner		= THIS_MODULE, | 
|  | 227 | .open		= bcm_proc_open, | 
|  | 228 | .read		= seq_read, | 
|  | 229 | .llseek		= seq_lseek, | 
|  | 230 | .release	= single_release, | 
|  | 231 | }; | 
|  | 232 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 233 | /* | 
|  | 234 | * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface | 
|  | 235 | *              of the given bcm tx op | 
|  | 236 | */ | 
|  | 237 | static void bcm_can_tx(struct bcm_op *op) | 
|  | 238 | { | 
|  | 239 | struct sk_buff *skb; | 
|  | 240 | struct net_device *dev; | 
|  | 241 | struct can_frame *cf = &op->frames[op->currframe]; | 
|  | 242 |  | 
|  | 243 | /* no target device? => exit */ | 
|  | 244 | if (!op->ifindex) | 
|  | 245 | return; | 
|  | 246 |  | 
|  | 247 | dev = dev_get_by_index(&init_net, op->ifindex); | 
|  | 248 | if (!dev) { | 
|  | 249 | /* RFC: should this bcm_op remove itself here? */ | 
|  | 250 | return; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | skb = alloc_skb(CFSIZ, gfp_any()); | 
|  | 254 | if (!skb) | 
|  | 255 | goto out; | 
|  | 256 |  | 
|  | 257 | memcpy(skb_put(skb, CFSIZ), cf, CFSIZ); | 
|  | 258 |  | 
|  | 259 | /* send with loopback */ | 
|  | 260 | skb->dev = dev; | 
|  | 261 | skb->sk = op->sk; | 
|  | 262 | can_send(skb, 1); | 
|  | 263 |  | 
|  | 264 | /* update statistics */ | 
|  | 265 | op->currframe++; | 
|  | 266 | op->frames_abs++; | 
|  | 267 |  | 
|  | 268 | /* reached last frame? */ | 
|  | 269 | if (op->currframe >= op->nframes) | 
|  | 270 | op->currframe = 0; | 
|  | 271 | out: | 
|  | 272 | dev_put(dev); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | /* | 
|  | 276 | * bcm_send_to_user - send a BCM message to the userspace | 
|  | 277 | *                    (consisting of bcm_msg_head + x CAN frames) | 
|  | 278 | */ | 
|  | 279 | static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head, | 
|  | 280 | struct can_frame *frames, int has_timestamp) | 
|  | 281 | { | 
|  | 282 | struct sk_buff *skb; | 
|  | 283 | struct can_frame *firstframe; | 
|  | 284 | struct sockaddr_can *addr; | 
|  | 285 | struct sock *sk = op->sk; | 
|  | 286 | int datalen = head->nframes * CFSIZ; | 
|  | 287 | int err; | 
|  | 288 |  | 
|  | 289 | skb = alloc_skb(sizeof(*head) + datalen, gfp_any()); | 
|  | 290 | if (!skb) | 
|  | 291 | return; | 
|  | 292 |  | 
|  | 293 | memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head)); | 
|  | 294 |  | 
|  | 295 | if (head->nframes) { | 
|  | 296 | /* can_frames starting here */ | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 297 | firstframe = (struct can_frame *)skb_tail_pointer(skb); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 298 |  | 
|  | 299 | memcpy(skb_put(skb, datalen), frames, datalen); | 
|  | 300 |  | 
|  | 301 | /* | 
|  | 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 | 
|  | 306 | */ | 
|  | 307 | if (head->nframes == 1) | 
|  | 308 | firstframe->can_dlc &= BCM_CAN_DLC_MASK; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | if (has_timestamp) { | 
|  | 312 | /* restore rx timestamp */ | 
|  | 313 | skb->tstamp = op->rx_stamp; | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | /* | 
|  | 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. | 
|  | 321 | */ | 
|  | 322 |  | 
|  | 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; | 
|  | 328 |  | 
|  | 329 | err = sock_queue_rcv_skb(sk, skb); | 
|  | 330 | if (err < 0) { | 
|  | 331 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 332 |  | 
|  | 333 | kfree_skb(skb); | 
|  | 334 | /* don't care about overflows in this statistic */ | 
|  | 335 | bo->dropped_usr_msgs++; | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 339 | static void bcm_tx_timeout_tsklet(unsigned long data) | 
|  | 340 | { | 
|  | 341 | struct bcm_op *op = (struct bcm_op *)data; | 
|  | 342 | struct bcm_msg_head msg_head; | 
|  | 343 |  | 
| Oliver Hartkopp | c53a6ee | 2009-01-14 21:06:55 -0800 | [diff] [blame] | 344 | if (op->kt_ival1.tv64 && (op->count > 0)) { | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 345 |  | 
| Oliver Hartkopp | c53a6ee | 2009-01-14 21:06:55 -0800 | [diff] [blame] | 346 | op->count--; | 
|  | 347 | if (!op->count && (op->flags & TX_COUNTEVT)) { | 
|  | 348 |  | 
|  | 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; | 
|  | 357 |  | 
|  | 358 | bcm_send_to_user(op, &msg_head, NULL, 0); | 
|  | 359 | } | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | if (op->kt_ival1.tv64 && (op->count > 0)) { | 
|  | 363 |  | 
|  | 364 | /* send (next) frame */ | 
|  | 365 | bcm_can_tx(op); | 
|  | 366 | hrtimer_start(&op->timer, | 
|  | 367 | ktime_add(ktime_get(), op->kt_ival1), | 
|  | 368 | HRTIMER_MODE_ABS); | 
|  | 369 |  | 
|  | 370 | } else { | 
|  | 371 | if (op->kt_ival2.tv64) { | 
|  | 372 |  | 
|  | 373 | /* send (next) frame */ | 
|  | 374 | bcm_can_tx(op); | 
|  | 375 | hrtimer_start(&op->timer, | 
|  | 376 | ktime_add(ktime_get(), op->kt_ival2), | 
|  | 377 | HRTIMER_MODE_ABS); | 
|  | 378 | } | 
|  | 379 | } | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 382 | /* | 
|  | 383 | * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions | 
|  | 384 | */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 385 | static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 386 | { | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 387 | struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 388 |  | 
| Oliver Hartkopp | c53a6ee | 2009-01-14 21:06:55 -0800 | [diff] [blame] | 389 | tasklet_schedule(&op->tsklet); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 390 |  | 
| Oliver Hartkopp | c53a6ee | 2009-01-14 21:06:55 -0800 | [diff] [blame] | 391 | return HRTIMER_NORESTART; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
|  | 394 | /* | 
|  | 395 | * bcm_rx_changed - create a RX_CHANGED notification due to changed content | 
|  | 396 | */ | 
|  | 397 | static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data) | 
|  | 398 | { | 
|  | 399 | struct bcm_msg_head head; | 
|  | 400 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 401 | /* update statistics */ | 
|  | 402 | op->frames_filtered++; | 
|  | 403 |  | 
|  | 404 | /* prevent statistics overflow */ | 
|  | 405 | if (op->frames_filtered > ULONG_MAX/100) | 
|  | 406 | op->frames_filtered = op->frames_abs = 0; | 
|  | 407 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 408 | /* this element is not throttled anymore */ | 
|  | 409 | data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV); | 
|  | 410 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 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; | 
|  | 417 | head.nframes = 1; | 
|  | 418 |  | 
|  | 419 | bcm_send_to_user(op, &head, data, 1); | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | /* | 
|  | 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) | 
|  | 426 | */ | 
|  | 427 | static void bcm_rx_update_and_send(struct bcm_op *op, | 
|  | 428 | struct can_frame *lastdata, | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 429 | const struct can_frame *rxdata) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 430 | { | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 431 | memcpy(lastdata, rxdata, CFSIZ); | 
|  | 432 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 433 | /* mark as used and throttled by default */ | 
|  | 434 | lastdata->can_dlc |= (RX_RECV|RX_THR); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 435 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 436 | /* throtteling mode inactive ? */ | 
|  | 437 | if (!op->kt_ival2.tv64) { | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 438 | /* send RX_CHANGED to the user immediately */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 439 | bcm_rx_changed(op, lastdata); | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 440 | return; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 441 | } | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 442 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 443 | /* with active throttling timer we are just done here */ | 
|  | 444 | if (hrtimer_active(&op->thrtimer)) | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 445 | return; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 446 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 447 | /* first receiption with enabled throttling mode */ | 
|  | 448 | if (!op->kt_lastmsg.tv64) | 
|  | 449 | goto rx_changed_settime; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 450 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 451 | /* got a second frame inside a potential throttle period? */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 452 | if (ktime_us_delta(ktime_get(), op->kt_lastmsg) < | 
|  | 453 | ktime_to_us(op->kt_ival2)) { | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 454 | /* do not send the saved data - only start throttle timer */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 455 | hrtimer_start(&op->thrtimer, | 
|  | 456 | ktime_add(op->kt_lastmsg, op->kt_ival2), | 
|  | 457 | HRTIMER_MODE_ABS); | 
|  | 458 | return; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | /* the gap was that big, that throttling was not needed here */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 462 | rx_changed_settime: | 
|  | 463 | bcm_rx_changed(op, lastdata); | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 464 | op->kt_lastmsg = ktime_get(); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
|  | 467 | /* | 
|  | 468 | * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly | 
|  | 469 | *                       received data stored in op->last_frames[] | 
|  | 470 | */ | 
|  | 471 | static void bcm_rx_cmp_to_index(struct bcm_op *op, int index, | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 472 | const struct can_frame *rxdata) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 473 | { | 
|  | 474 | /* | 
|  | 475 | * no one uses the MSBs of can_dlc for comparation, | 
|  | 476 | * so we use it here to detect the first time of reception | 
|  | 477 | */ | 
|  | 478 |  | 
|  | 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); | 
|  | 482 | return; | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | /* do a real check in can_frame data section */ | 
|  | 486 |  | 
|  | 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); | 
|  | 490 | return; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 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 & | 
|  | 496 | BCM_CAN_DLC_MASK)) { | 
|  | 497 | bcm_rx_update_and_send(op, &op->last_frames[index], | 
|  | 498 | rxdata); | 
|  | 499 | return; | 
|  | 500 | } | 
|  | 501 | } | 
|  | 502 | } | 
|  | 503 |  | 
|  | 504 | /* | 
|  | 505 | * bcm_rx_starttimer - enable timeout monitoring for CAN frame receiption | 
|  | 506 | */ | 
|  | 507 | static void bcm_rx_starttimer(struct bcm_op *op) | 
|  | 508 | { | 
|  | 509 | if (op->flags & RX_NO_AUTOTIMER) | 
|  | 510 | return; | 
|  | 511 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 512 | if (op->kt_ival1.tv64) | 
|  | 513 | hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 514 | } | 
|  | 515 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 516 | static void bcm_rx_timeout_tsklet(unsigned long data) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 517 | { | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 518 | struct bcm_op *op = (struct bcm_op *)data; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 519 | struct bcm_msg_head msg_head; | 
|  | 520 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 521 | /* create notification to user */ | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 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; | 
|  | 529 |  | 
|  | 530 | bcm_send_to_user(op, &msg_head, NULL, 0); | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 531 | } | 
|  | 532 |  | 
|  | 533 | /* | 
|  | 534 | * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out | 
|  | 535 | */ | 
|  | 536 | static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer) | 
|  | 537 | { | 
|  | 538 | struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer); | 
|  | 539 |  | 
|  | 540 | /* schedule before NET_RX_SOFTIRQ */ | 
|  | 541 | tasklet_hi_schedule(&op->tsklet); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 542 |  | 
|  | 543 | /* no restart of the timer is done here! */ | 
|  | 544 |  | 
|  | 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); | 
|  | 549 | } | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 550 |  | 
|  | 551 | return HRTIMER_NORESTART; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 552 | } | 
|  | 553 |  | 
|  | 554 | /* | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 555 | * bcm_rx_do_flush - helper for bcm_rx_thr_flush | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 556 | */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 557 | static inline int bcm_rx_do_flush(struct bcm_op *op, int update, int index) | 
|  | 558 | { | 
|  | 559 | if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) { | 
|  | 560 | if (update) | 
|  | 561 | bcm_rx_changed(op, &op->last_frames[index]); | 
|  | 562 | return 1; | 
|  | 563 | } | 
|  | 564 | return 0; | 
|  | 565 | } | 
|  | 566 |  | 
|  | 567 | /* | 
|  | 568 | * bcm_rx_thr_flush - Check for throttled data and send it to the userspace | 
|  | 569 | * | 
|  | 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) | 
|  | 572 | */ | 
|  | 573 | static int bcm_rx_thr_flush(struct bcm_op *op, int update) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 574 | { | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 575 | int updated = 0; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 576 |  | 
|  | 577 | if (op->nframes > 1) { | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 578 | int i; | 
|  | 579 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 580 | /* for MUX filter we start at index 1 */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 581 | for (i = 1; i < op->nframes; i++) | 
|  | 582 | updated += bcm_rx_do_flush(op, update, i); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 583 |  | 
|  | 584 | } else { | 
|  | 585 | /* for RX_FILTER_ID and simple filter */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 586 | updated += bcm_rx_do_flush(op, update, 0); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 587 | } | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 588 |  | 
|  | 589 | return updated; | 
|  | 590 | } | 
|  | 591 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 592 | static void bcm_rx_thr_tsklet(unsigned long data) | 
|  | 593 | { | 
|  | 594 | struct bcm_op *op = (struct bcm_op *)data; | 
|  | 595 |  | 
|  | 596 | /* push the changed data to the userspace */ | 
|  | 597 | bcm_rx_thr_flush(op, 1); | 
|  | 598 | } | 
|  | 599 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 600 | /* | 
|  | 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 | 
|  | 603 | */ | 
|  | 604 | static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer) | 
|  | 605 | { | 
|  | 606 | struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer); | 
|  | 607 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 608 | tasklet_schedule(&op->thrtsklet); | 
|  | 609 |  | 
|  | 610 | if (bcm_rx_thr_flush(op, 0)) { | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 611 | hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2); | 
|  | 612 | return HRTIMER_RESTART; | 
|  | 613 | } else { | 
|  | 614 | /* rearm throttle handling */ | 
|  | 615 | op->kt_lastmsg = ktime_set(0, 0); | 
|  | 616 | return HRTIMER_NORESTART; | 
|  | 617 | } | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 618 | } | 
|  | 619 |  | 
|  | 620 | /* | 
|  | 621 | * bcm_rx_handler - handle a CAN frame receiption | 
|  | 622 | */ | 
|  | 623 | static void bcm_rx_handler(struct sk_buff *skb, void *data) | 
|  | 624 | { | 
|  | 625 | struct bcm_op *op = (struct bcm_op *)data; | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 626 | const struct can_frame *rxframe = (struct can_frame *)skb->data; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 627 | int i; | 
|  | 628 |  | 
|  | 629 | /* disable timeout */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 630 | hrtimer_cancel(&op->timer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 631 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 632 | if (op->can_id != rxframe->can_id) | 
| Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 633 | return; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 634 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 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 */ | 
|  | 640 | op->frames_abs++; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 641 |  | 
|  | 642 | if (op->flags & RX_RTR_FRAME) { | 
|  | 643 | /* send reply for RTR-request (placed in op->frames[0]) */ | 
|  | 644 | bcm_can_tx(op); | 
| Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 645 | return; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
|  | 648 | if (op->flags & RX_FILTER_ID) { | 
|  | 649 | /* the easiest case */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 650 | bcm_rx_update_and_send(op, &op->last_frames[0], rxframe); | 
| Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 651 | goto rx_starttimer; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 652 | } | 
|  | 653 |  | 
|  | 654 | if (op->nframes == 1) { | 
|  | 655 | /* simple compare with index 0 */ | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 656 | bcm_rx_cmp_to_index(op, 0, rxframe); | 
| Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 657 | goto rx_starttimer; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 658 | } | 
|  | 659 |  | 
|  | 660 | if (op->nframes > 1) { | 
|  | 661 | /* | 
|  | 662 | * multiplex compare | 
|  | 663 | * | 
|  | 664 | * find the first multiplex mask that fits. | 
|  | 665 | * Remark: The MUX-mask is stored in index 0 | 
|  | 666 | */ | 
|  | 667 |  | 
|  | 668 | for (i = 1; i < op->nframes; i++) { | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 669 | if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) == | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 670 | (GET_U64(&op->frames[0]) & | 
|  | 671 | GET_U64(&op->frames[i]))) { | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 672 | bcm_rx_cmp_to_index(op, i, rxframe); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 673 | break; | 
|  | 674 | } | 
|  | 675 | } | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 676 | } | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 677 |  | 
| Oliver Hartkopp | 1fa17d4 | 2009-01-06 11:07:54 -0800 | [diff] [blame] | 678 | rx_starttimer: | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 679 | bcm_rx_starttimer(op); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 680 | } | 
|  | 681 |  | 
|  | 682 | /* | 
|  | 683 | * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements | 
|  | 684 | */ | 
|  | 685 | static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, | 
|  | 686 | int ifindex) | 
|  | 687 | { | 
|  | 688 | struct bcm_op *op; | 
|  | 689 |  | 
|  | 690 | list_for_each_entry(op, ops, list) { | 
|  | 691 | if ((op->can_id == can_id) && (op->ifindex == ifindex)) | 
|  | 692 | return op; | 
|  | 693 | } | 
|  | 694 |  | 
|  | 695 | return NULL; | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | static void bcm_remove_op(struct bcm_op *op) | 
|  | 699 | { | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 700 | hrtimer_cancel(&op->timer); | 
|  | 701 | hrtimer_cancel(&op->thrtimer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 702 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 703 | if (op->tsklet.func) | 
|  | 704 | tasklet_kill(&op->tsklet); | 
|  | 705 |  | 
|  | 706 | if (op->thrtsklet.func) | 
|  | 707 | tasklet_kill(&op->thrtsklet); | 
|  | 708 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 709 | if ((op->frames) && (op->frames != &op->sframe)) | 
|  | 710 | kfree(op->frames); | 
|  | 711 |  | 
|  | 712 | if ((op->last_frames) && (op->last_frames != &op->last_sframe)) | 
|  | 713 | kfree(op->last_frames); | 
|  | 714 |  | 
|  | 715 | kfree(op); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 716 | } | 
|  | 717 |  | 
|  | 718 | static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op) | 
|  | 719 | { | 
|  | 720 | if (op->rx_reg_dev == dev) { | 
|  | 721 | can_rx_unregister(dev, op->can_id, REGMASK(op->can_id), | 
|  | 722 | bcm_rx_handler, op); | 
|  | 723 |  | 
|  | 724 | /* mark as removed subscription */ | 
|  | 725 | op->rx_reg_dev = NULL; | 
|  | 726 | } else | 
|  | 727 | printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device " | 
|  | 728 | "mismatch %p %p\n", op->rx_reg_dev, dev); | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | /* | 
|  | 732 | * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops) | 
|  | 733 | */ | 
|  | 734 | static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex) | 
|  | 735 | { | 
|  | 736 | struct bcm_op *op, *n; | 
|  | 737 |  | 
|  | 738 | list_for_each_entry_safe(op, n, ops, list) { | 
|  | 739 | if ((op->can_id == can_id) && (op->ifindex == ifindex)) { | 
|  | 740 |  | 
|  | 741 | /* | 
|  | 742 | * Don't care if we're bound or not (due to netdev | 
|  | 743 | * problems) can_rx_unregister() is always a save | 
|  | 744 | * thing to do here. | 
|  | 745 | */ | 
|  | 746 | if (op->ifindex) { | 
|  | 747 | /* | 
|  | 748 | * Only remove subscriptions that had not | 
|  | 749 | * been removed due to NETDEV_UNREGISTER | 
|  | 750 | * in bcm_notifier() | 
|  | 751 | */ | 
|  | 752 | if (op->rx_reg_dev) { | 
|  | 753 | struct net_device *dev; | 
|  | 754 |  | 
|  | 755 | dev = dev_get_by_index(&init_net, | 
|  | 756 | op->ifindex); | 
|  | 757 | if (dev) { | 
|  | 758 | bcm_rx_unreg(dev, op); | 
|  | 759 | dev_put(dev); | 
|  | 760 | } | 
|  | 761 | } | 
|  | 762 | } else | 
|  | 763 | can_rx_unregister(NULL, op->can_id, | 
|  | 764 | REGMASK(op->can_id), | 
|  | 765 | bcm_rx_handler, op); | 
|  | 766 |  | 
|  | 767 | list_del(&op->list); | 
|  | 768 | bcm_remove_op(op); | 
|  | 769 | return 1; /* done */ | 
|  | 770 | } | 
|  | 771 | } | 
|  | 772 |  | 
|  | 773 | return 0; /* not found */ | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | /* | 
|  | 777 | * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops) | 
|  | 778 | */ | 
|  | 779 | static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex) | 
|  | 780 | { | 
|  | 781 | struct bcm_op *op, *n; | 
|  | 782 |  | 
|  | 783 | list_for_each_entry_safe(op, n, ops, list) { | 
|  | 784 | if ((op->can_id == can_id) && (op->ifindex == ifindex)) { | 
|  | 785 | list_del(&op->list); | 
|  | 786 | bcm_remove_op(op); | 
|  | 787 | return 1; /* done */ | 
|  | 788 | } | 
|  | 789 | } | 
|  | 790 |  | 
|  | 791 | return 0; /* not found */ | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | /* | 
|  | 795 | * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg) | 
|  | 796 | */ | 
|  | 797 | static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, | 
|  | 798 | int ifindex) | 
|  | 799 | { | 
|  | 800 | struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex); | 
|  | 801 |  | 
|  | 802 | if (!op) | 
|  | 803 | return -EINVAL; | 
|  | 804 |  | 
|  | 805 | /* put current values into msg_head */ | 
|  | 806 | msg_head->flags   = op->flags; | 
|  | 807 | msg_head->count   = op->count; | 
|  | 808 | msg_head->ival1   = op->ival1; | 
|  | 809 | msg_head->ival2   = op->ival2; | 
|  | 810 | msg_head->nframes = op->nframes; | 
|  | 811 |  | 
|  | 812 | bcm_send_to_user(op, msg_head, op->frames, 0); | 
|  | 813 |  | 
|  | 814 | return MHSIZ; | 
|  | 815 | } | 
|  | 816 |  | 
|  | 817 | /* | 
|  | 818 | * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg) | 
|  | 819 | */ | 
|  | 820 | static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, | 
|  | 821 | int ifindex, struct sock *sk) | 
|  | 822 | { | 
|  | 823 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 824 | struct bcm_op *op; | 
|  | 825 | int i, err; | 
|  | 826 |  | 
|  | 827 | /* we need a real device to send frames */ | 
|  | 828 | if (!ifindex) | 
|  | 829 | return -ENODEV; | 
|  | 830 |  | 
|  | 831 | /* we need at least one can_frame */ | 
|  | 832 | if (msg_head->nframes < 1) | 
|  | 833 | return -EINVAL; | 
|  | 834 |  | 
|  | 835 | /* check the given can_id */ | 
|  | 836 | op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex); | 
|  | 837 |  | 
|  | 838 | if (op) { | 
|  | 839 | /* update existing BCM operation */ | 
|  | 840 |  | 
|  | 841 | /* | 
|  | 842 | * Do we need more space for the can_frames than currently | 
|  | 843 | * allocated? -> This is a _really_ unusual use-case and | 
|  | 844 | * therefore (complexity / locking) it is not supported. | 
|  | 845 | */ | 
|  | 846 | if (msg_head->nframes > op->nframes) | 
|  | 847 | return -E2BIG; | 
|  | 848 |  | 
|  | 849 | /* update can_frames content */ | 
|  | 850 | for (i = 0; i < msg_head->nframes; i++) { | 
|  | 851 | err = memcpy_fromiovec((u8 *)&op->frames[i], | 
|  | 852 | msg->msg_iov, CFSIZ); | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 853 |  | 
|  | 854 | if (op->frames[i].can_dlc > 8) | 
|  | 855 | err = -EINVAL; | 
|  | 856 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 857 | if (err < 0) | 
|  | 858 | return err; | 
|  | 859 |  | 
|  | 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; | 
|  | 863 | } | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | } else { | 
|  | 867 | /* insert new BCM operation for the given can_id */ | 
|  | 868 |  | 
|  | 869 | op = kzalloc(OPSIZ, GFP_KERNEL); | 
|  | 870 | if (!op) | 
|  | 871 | return -ENOMEM; | 
|  | 872 |  | 
|  | 873 | op->can_id    = msg_head->can_id; | 
|  | 874 |  | 
|  | 875 | /* create array for can_frames and copy the data */ | 
|  | 876 | if (msg_head->nframes > 1) { | 
|  | 877 | op->frames = kmalloc(msg_head->nframes * CFSIZ, | 
|  | 878 | GFP_KERNEL); | 
|  | 879 | if (!op->frames) { | 
|  | 880 | kfree(op); | 
|  | 881 | return -ENOMEM; | 
|  | 882 | } | 
|  | 883 | } else | 
|  | 884 | op->frames = &op->sframe; | 
|  | 885 |  | 
|  | 886 | for (i = 0; i < msg_head->nframes; i++) { | 
|  | 887 | err = memcpy_fromiovec((u8 *)&op->frames[i], | 
|  | 888 | msg->msg_iov, CFSIZ); | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 889 |  | 
|  | 890 | if (op->frames[i].can_dlc > 8) | 
|  | 891 | err = -EINVAL; | 
|  | 892 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 893 | if (err < 0) { | 
|  | 894 | if (op->frames != &op->sframe) | 
|  | 895 | kfree(op->frames); | 
|  | 896 | kfree(op); | 
|  | 897 | return err; | 
|  | 898 | } | 
|  | 899 |  | 
|  | 900 | if (msg_head->flags & TX_CP_CAN_ID) { | 
|  | 901 | /* copy can_id into frame */ | 
|  | 902 | op->frames[i].can_id = msg_head->can_id; | 
|  | 903 | } | 
|  | 904 | } | 
|  | 905 |  | 
|  | 906 | /* tx_ops never compare with previous received messages */ | 
|  | 907 | op->last_frames = NULL; | 
|  | 908 |  | 
|  | 909 | /* bcm_can_tx / bcm_tx_timeout_handler needs this */ | 
|  | 910 | op->sk = sk; | 
|  | 911 | op->ifindex = ifindex; | 
|  | 912 |  | 
|  | 913 | /* initialize uninitialized (kzalloc) structure */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 914 | hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
|  | 915 | op->timer.function = bcm_tx_timeout_handler; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 916 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 917 | /* initialize tasklet for tx countevent notification */ | 
|  | 918 | tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet, | 
|  | 919 | (unsigned long) op); | 
|  | 920 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 921 | /* currently unused in tx_ops */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 922 | hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 923 |  | 
|  | 924 | /* add this bcm_op to the list of the tx_ops */ | 
|  | 925 | list_add(&op->list, &bo->tx_ops); | 
|  | 926 |  | 
|  | 927 | } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */ | 
|  | 928 |  | 
|  | 929 | if (op->nframes != msg_head->nframes) { | 
|  | 930 | op->nframes   = msg_head->nframes; | 
|  | 931 | /* start multiple frame transmission with index 0 */ | 
|  | 932 | op->currframe = 0; | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | /* check flags */ | 
|  | 936 |  | 
|  | 937 | op->flags = msg_head->flags; | 
|  | 938 |  | 
|  | 939 | if (op->flags & TX_RESET_MULTI_IDX) { | 
|  | 940 | /* start multiple frame transmission with index 0 */ | 
|  | 941 | op->currframe = 0; | 
|  | 942 | } | 
|  | 943 |  | 
|  | 944 | if (op->flags & SETTIMER) { | 
|  | 945 | /* set timer values */ | 
|  | 946 | op->count = msg_head->count; | 
|  | 947 | op->ival1 = msg_head->ival1; | 
|  | 948 | op->ival2 = msg_head->ival2; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 949 | op->kt_ival1 = timeval_to_ktime(msg_head->ival1); | 
|  | 950 | op->kt_ival2 = timeval_to_ktime(msg_head->ival2); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 951 |  | 
|  | 952 | /* disable an active timer due to zero values? */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 953 | if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64) | 
|  | 954 | hrtimer_cancel(&op->timer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 955 | } | 
|  | 956 |  | 
|  | 957 | if ((op->flags & STARTTIMER) && | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 958 | ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) { | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 959 |  | 
|  | 960 | /* spec: send can_frame when starting timer */ | 
|  | 961 | op->flags |= TX_ANNOUNCE; | 
|  | 962 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 963 | if (op->kt_ival1.tv64 && (op->count > 0)) { | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 964 | /* op->count-- is done in bcm_tx_timeout_handler */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 965 | hrtimer_start(&op->timer, op->kt_ival1, | 
|  | 966 | HRTIMER_MODE_REL); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 967 | } else | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 968 | hrtimer_start(&op->timer, op->kt_ival2, | 
|  | 969 | HRTIMER_MODE_REL); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 970 | } | 
|  | 971 |  | 
|  | 972 | if (op->flags & TX_ANNOUNCE) | 
|  | 973 | bcm_can_tx(op); | 
|  | 974 |  | 
|  | 975 | return msg_head->nframes * CFSIZ + MHSIZ; | 
|  | 976 | } | 
|  | 977 |  | 
|  | 978 | /* | 
|  | 979 | * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg) | 
|  | 980 | */ | 
|  | 981 | static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, | 
|  | 982 | int ifindex, struct sock *sk) | 
|  | 983 | { | 
|  | 984 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 985 | struct bcm_op *op; | 
|  | 986 | int do_rx_register; | 
|  | 987 | int err = 0; | 
|  | 988 |  | 
|  | 989 | if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) { | 
|  | 990 | /* be robust against wrong usage ... */ | 
|  | 991 | msg_head->flags |= RX_FILTER_ID; | 
|  | 992 | /* ignore trailing garbage */ | 
|  | 993 | msg_head->nframes = 0; | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | if ((msg_head->flags & RX_RTR_FRAME) && | 
|  | 997 | ((msg_head->nframes != 1) || | 
|  | 998 | (!(msg_head->can_id & CAN_RTR_FLAG)))) | 
|  | 999 | return -EINVAL; | 
|  | 1000 |  | 
|  | 1001 | /* check the given can_id */ | 
|  | 1002 | op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex); | 
|  | 1003 | if (op) { | 
|  | 1004 | /* update existing BCM operation */ | 
|  | 1005 |  | 
|  | 1006 | /* | 
|  | 1007 | * Do we need more space for the can_frames than currently | 
|  | 1008 | * allocated? -> This is a _really_ unusual use-case and | 
|  | 1009 | * therefore (complexity / locking) it is not supported. | 
|  | 1010 | */ | 
|  | 1011 | if (msg_head->nframes > op->nframes) | 
|  | 1012 | return -E2BIG; | 
|  | 1013 |  | 
|  | 1014 | if (msg_head->nframes) { | 
|  | 1015 | /* update can_frames content */ | 
|  | 1016 | err = memcpy_fromiovec((u8 *)op->frames, | 
|  | 1017 | msg->msg_iov, | 
|  | 1018 | msg_head->nframes * CFSIZ); | 
|  | 1019 | if (err < 0) | 
|  | 1020 | return err; | 
|  | 1021 |  | 
|  | 1022 | /* clear last_frames to indicate 'nothing received' */ | 
|  | 1023 | memset(op->last_frames, 0, msg_head->nframes * CFSIZ); | 
|  | 1024 | } | 
|  | 1025 |  | 
|  | 1026 | op->nframes = msg_head->nframes; | 
|  | 1027 |  | 
|  | 1028 | /* Only an update -> do not call can_rx_register() */ | 
|  | 1029 | do_rx_register = 0; | 
|  | 1030 |  | 
|  | 1031 | } else { | 
|  | 1032 | /* insert new BCM operation for the given can_id */ | 
|  | 1033 | op = kzalloc(OPSIZ, GFP_KERNEL); | 
|  | 1034 | if (!op) | 
|  | 1035 | return -ENOMEM; | 
|  | 1036 |  | 
|  | 1037 | op->can_id    = msg_head->can_id; | 
|  | 1038 | op->nframes   = msg_head->nframes; | 
|  | 1039 |  | 
|  | 1040 | if (msg_head->nframes > 1) { | 
|  | 1041 | /* create array for can_frames and copy the data */ | 
|  | 1042 | op->frames = kmalloc(msg_head->nframes * CFSIZ, | 
|  | 1043 | GFP_KERNEL); | 
|  | 1044 | if (!op->frames) { | 
|  | 1045 | kfree(op); | 
|  | 1046 | return -ENOMEM; | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | /* create and init array for received can_frames */ | 
|  | 1050 | op->last_frames = kzalloc(msg_head->nframes * CFSIZ, | 
|  | 1051 | GFP_KERNEL); | 
|  | 1052 | if (!op->last_frames) { | 
|  | 1053 | kfree(op->frames); | 
|  | 1054 | kfree(op); | 
|  | 1055 | return -ENOMEM; | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | } else { | 
|  | 1059 | op->frames = &op->sframe; | 
|  | 1060 | op->last_frames = &op->last_sframe; | 
|  | 1061 | } | 
|  | 1062 |  | 
|  | 1063 | if (msg_head->nframes) { | 
|  | 1064 | err = memcpy_fromiovec((u8 *)op->frames, msg->msg_iov, | 
|  | 1065 | msg_head->nframes * CFSIZ); | 
|  | 1066 | if (err < 0) { | 
|  | 1067 | if (op->frames != &op->sframe) | 
|  | 1068 | kfree(op->frames); | 
|  | 1069 | if (op->last_frames != &op->last_sframe) | 
|  | 1070 | kfree(op->last_frames); | 
|  | 1071 | kfree(op); | 
|  | 1072 | return err; | 
|  | 1073 | } | 
|  | 1074 | } | 
|  | 1075 |  | 
|  | 1076 | /* bcm_can_tx / bcm_tx_timeout_handler needs this */ | 
|  | 1077 | op->sk = sk; | 
|  | 1078 | op->ifindex = ifindex; | 
|  | 1079 |  | 
|  | 1080 | /* initialize uninitialized (kzalloc) structure */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1081 | hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
|  | 1082 | op->timer.function = bcm_rx_timeout_handler; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1083 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 1084 | /* initialize tasklet for rx timeout notification */ | 
|  | 1085 | tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet, | 
|  | 1086 | (unsigned long) op); | 
|  | 1087 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1088 | hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
|  | 1089 | op->thrtimer.function = bcm_rx_thr_handler; | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1090 |  | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 1091 | /* initialize tasklet for rx throttle handling */ | 
|  | 1092 | tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet, | 
|  | 1093 | (unsigned long) op); | 
|  | 1094 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1095 | /* add this bcm_op to the list of the rx_ops */ | 
|  | 1096 | list_add(&op->list, &bo->rx_ops); | 
|  | 1097 |  | 
|  | 1098 | /* call can_rx_register() */ | 
|  | 1099 | do_rx_register = 1; | 
|  | 1100 |  | 
|  | 1101 | } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */ | 
|  | 1102 |  | 
|  | 1103 | /* check flags */ | 
|  | 1104 | op->flags = msg_head->flags; | 
|  | 1105 |  | 
|  | 1106 | if (op->flags & RX_RTR_FRAME) { | 
|  | 1107 |  | 
|  | 1108 | /* no timers in RTR-mode */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1109 | hrtimer_cancel(&op->thrtimer); | 
|  | 1110 | hrtimer_cancel(&op->timer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1111 |  | 
|  | 1112 | /* | 
|  | 1113 | * funny feature in RX(!)_SETUP only for RTR-mode: | 
|  | 1114 | * copy can_id into frame BUT without RTR-flag to | 
|  | 1115 | * prevent a full-load-loopback-test ... ;-] | 
|  | 1116 | */ | 
|  | 1117 | if ((op->flags & TX_CP_CAN_ID) || | 
|  | 1118 | (op->frames[0].can_id == op->can_id)) | 
|  | 1119 | op->frames[0].can_id = op->can_id & ~CAN_RTR_FLAG; | 
|  | 1120 |  | 
|  | 1121 | } else { | 
|  | 1122 | if (op->flags & SETTIMER) { | 
|  | 1123 |  | 
|  | 1124 | /* set timer value */ | 
|  | 1125 | op->ival1 = msg_head->ival1; | 
|  | 1126 | op->ival2 = msg_head->ival2; | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1127 | op->kt_ival1 = timeval_to_ktime(msg_head->ival1); | 
|  | 1128 | op->kt_ival2 = timeval_to_ktime(msg_head->ival2); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1129 |  | 
|  | 1130 | /* disable an active timer due to zero value? */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1131 | if (!op->kt_ival1.tv64) | 
|  | 1132 | hrtimer_cancel(&op->timer); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1133 |  | 
|  | 1134 | /* | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1135 | * In any case cancel the throttle timer, flush | 
|  | 1136 | * potentially blocked msgs and reset throttle handling | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1137 | */ | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1138 | op->kt_lastmsg = ktime_set(0, 0); | 
|  | 1139 | hrtimer_cancel(&op->thrtimer); | 
| Oliver Hartkopp | 6e5c172 | 2009-01-04 17:31:18 -0800 | [diff] [blame] | 1140 | bcm_rx_thr_flush(op, 1); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1141 | } | 
|  | 1142 |  | 
| Oliver Hartkopp | 73e87e0 | 2008-04-15 19:29:14 -0700 | [diff] [blame] | 1143 | if ((op->flags & STARTTIMER) && op->kt_ival1.tv64) | 
|  | 1144 | hrtimer_start(&op->timer, op->kt_ival1, | 
|  | 1145 | HRTIMER_MODE_REL); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | /* now we can register for can_ids, if we added a new bcm_op */ | 
|  | 1149 | if (do_rx_register) { | 
|  | 1150 | if (ifindex) { | 
|  | 1151 | struct net_device *dev; | 
|  | 1152 |  | 
|  | 1153 | dev = dev_get_by_index(&init_net, ifindex); | 
|  | 1154 | if (dev) { | 
|  | 1155 | err = can_rx_register(dev, op->can_id, | 
|  | 1156 | REGMASK(op->can_id), | 
|  | 1157 | bcm_rx_handler, op, | 
|  | 1158 | "bcm"); | 
|  | 1159 |  | 
|  | 1160 | op->rx_reg_dev = dev; | 
|  | 1161 | dev_put(dev); | 
|  | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | } else | 
|  | 1165 | err = can_rx_register(NULL, op->can_id, | 
|  | 1166 | REGMASK(op->can_id), | 
|  | 1167 | bcm_rx_handler, op, "bcm"); | 
|  | 1168 | if (err) { | 
|  | 1169 | /* this bcm rx op is broken -> remove it */ | 
|  | 1170 | list_del(&op->list); | 
|  | 1171 | bcm_remove_op(op); | 
|  | 1172 | return err; | 
|  | 1173 | } | 
|  | 1174 | } | 
|  | 1175 |  | 
|  | 1176 | return msg_head->nframes * CFSIZ + MHSIZ; | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | /* | 
|  | 1180 | * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) | 
|  | 1181 | */ | 
|  | 1182 | static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk) | 
|  | 1183 | { | 
|  | 1184 | struct sk_buff *skb; | 
|  | 1185 | struct net_device *dev; | 
|  | 1186 | int err; | 
|  | 1187 |  | 
|  | 1188 | /* we need a real device to send frames */ | 
|  | 1189 | if (!ifindex) | 
|  | 1190 | return -ENODEV; | 
|  | 1191 |  | 
|  | 1192 | skb = alloc_skb(CFSIZ, GFP_KERNEL); | 
|  | 1193 |  | 
|  | 1194 | if (!skb) | 
|  | 1195 | return -ENOMEM; | 
|  | 1196 |  | 
|  | 1197 | err = memcpy_fromiovec(skb_put(skb, CFSIZ), msg->msg_iov, CFSIZ); | 
|  | 1198 | if (err < 0) { | 
|  | 1199 | kfree_skb(skb); | 
|  | 1200 | return err; | 
|  | 1201 | } | 
|  | 1202 |  | 
|  | 1203 | dev = dev_get_by_index(&init_net, ifindex); | 
|  | 1204 | if (!dev) { | 
|  | 1205 | kfree_skb(skb); | 
|  | 1206 | return -ENODEV; | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | skb->dev = dev; | 
|  | 1210 | skb->sk  = sk; | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 1211 | err = can_send(skb, 1); /* send with loopback */ | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1212 | dev_put(dev); | 
|  | 1213 |  | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 1214 | if (err) | 
|  | 1215 | return err; | 
|  | 1216 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1217 | return CFSIZ + MHSIZ; | 
|  | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | /* | 
|  | 1221 | * bcm_sendmsg - process BCM commands (opcodes) from the userspace | 
|  | 1222 | */ | 
|  | 1223 | static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock, | 
|  | 1224 | struct msghdr *msg, size_t size) | 
|  | 1225 | { | 
|  | 1226 | struct sock *sk = sock->sk; | 
|  | 1227 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 1228 | int ifindex = bo->ifindex; /* default ifindex for this bcm_op */ | 
|  | 1229 | struct bcm_msg_head msg_head; | 
|  | 1230 | int ret; /* read bytes or error codes as return value */ | 
|  | 1231 |  | 
|  | 1232 | if (!bo->bound) | 
|  | 1233 | return -ENOTCONN; | 
|  | 1234 |  | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 1235 | /* check for valid message length from userspace */ | 
|  | 1236 | if (size < MHSIZ || (size - MHSIZ) % CFSIZ) | 
|  | 1237 | return -EINVAL; | 
|  | 1238 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1239 | /* check for alternative ifindex for this bcm_op */ | 
|  | 1240 |  | 
|  | 1241 | if (!ifindex && msg->msg_name) { | 
|  | 1242 | /* no bound device as default => check msg_name */ | 
|  | 1243 | struct sockaddr_can *addr = | 
|  | 1244 | (struct sockaddr_can *)msg->msg_name; | 
|  | 1245 |  | 
|  | 1246 | if (addr->can_family != AF_CAN) | 
|  | 1247 | return -EINVAL; | 
|  | 1248 |  | 
|  | 1249 | /* ifindex from sendto() */ | 
|  | 1250 | ifindex = addr->can_ifindex; | 
|  | 1251 |  | 
|  | 1252 | if (ifindex) { | 
|  | 1253 | struct net_device *dev; | 
|  | 1254 |  | 
|  | 1255 | dev = dev_get_by_index(&init_net, ifindex); | 
|  | 1256 | if (!dev) | 
|  | 1257 | return -ENODEV; | 
|  | 1258 |  | 
|  | 1259 | if (dev->type != ARPHRD_CAN) { | 
|  | 1260 | dev_put(dev); | 
|  | 1261 | return -ENODEV; | 
|  | 1262 | } | 
|  | 1263 |  | 
|  | 1264 | dev_put(dev); | 
|  | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | /* read message head information */ | 
|  | 1269 |  | 
|  | 1270 | ret = memcpy_fromiovec((u8 *)&msg_head, msg->msg_iov, MHSIZ); | 
|  | 1271 | if (ret < 0) | 
|  | 1272 | return ret; | 
|  | 1273 |  | 
|  | 1274 | lock_sock(sk); | 
|  | 1275 |  | 
|  | 1276 | switch (msg_head.opcode) { | 
|  | 1277 |  | 
|  | 1278 | case TX_SETUP: | 
|  | 1279 | ret = bcm_tx_setup(&msg_head, msg, ifindex, sk); | 
|  | 1280 | break; | 
|  | 1281 |  | 
|  | 1282 | case RX_SETUP: | 
|  | 1283 | ret = bcm_rx_setup(&msg_head, msg, ifindex, sk); | 
|  | 1284 | break; | 
|  | 1285 |  | 
|  | 1286 | case TX_DELETE: | 
|  | 1287 | if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex)) | 
|  | 1288 | ret = MHSIZ; | 
|  | 1289 | else | 
|  | 1290 | ret = -EINVAL; | 
|  | 1291 | break; | 
|  | 1292 |  | 
|  | 1293 | case RX_DELETE: | 
|  | 1294 | if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex)) | 
|  | 1295 | ret = MHSIZ; | 
|  | 1296 | else | 
|  | 1297 | ret = -EINVAL; | 
|  | 1298 | break; | 
|  | 1299 |  | 
|  | 1300 | case TX_READ: | 
|  | 1301 | /* reuse msg_head for the reply to TX_READ */ | 
|  | 1302 | msg_head.opcode  = TX_STATUS; | 
|  | 1303 | ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex); | 
|  | 1304 | break; | 
|  | 1305 |  | 
|  | 1306 | case RX_READ: | 
|  | 1307 | /* reuse msg_head for the reply to RX_READ */ | 
|  | 1308 | msg_head.opcode  = RX_STATUS; | 
|  | 1309 | ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex); | 
|  | 1310 | break; | 
|  | 1311 |  | 
|  | 1312 | case TX_SEND: | 
| Oliver Hartkopp | 7f2d38e | 2008-07-05 23:38:43 -0700 | [diff] [blame] | 1313 | /* we need exactly one can_frame behind the msg head */ | 
|  | 1314 | if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ)) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1315 | ret = -EINVAL; | 
|  | 1316 | else | 
|  | 1317 | ret = bcm_tx_send(msg, ifindex, sk); | 
|  | 1318 | break; | 
|  | 1319 |  | 
|  | 1320 | default: | 
|  | 1321 | ret = -EINVAL; | 
|  | 1322 | break; | 
|  | 1323 | } | 
|  | 1324 |  | 
|  | 1325 | release_sock(sk); | 
|  | 1326 |  | 
|  | 1327 | return ret; | 
|  | 1328 | } | 
|  | 1329 |  | 
|  | 1330 | /* | 
|  | 1331 | * notification handler for netdevice status changes | 
|  | 1332 | */ | 
|  | 1333 | static int bcm_notifier(struct notifier_block *nb, unsigned long msg, | 
|  | 1334 | void *data) | 
|  | 1335 | { | 
|  | 1336 | struct net_device *dev = (struct net_device *)data; | 
|  | 1337 | struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier); | 
|  | 1338 | struct sock *sk = &bo->sk; | 
|  | 1339 | struct bcm_op *op; | 
|  | 1340 | int notify_enodev = 0; | 
|  | 1341 |  | 
| YOSHIFUJI Hideaki | 721499e | 2008-07-19 22:34:43 -0700 | [diff] [blame] | 1342 | if (!net_eq(dev_net(dev), &init_net)) | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1343 | return NOTIFY_DONE; | 
|  | 1344 |  | 
|  | 1345 | if (dev->type != ARPHRD_CAN) | 
|  | 1346 | return NOTIFY_DONE; | 
|  | 1347 |  | 
|  | 1348 | switch (msg) { | 
|  | 1349 |  | 
|  | 1350 | case NETDEV_UNREGISTER: | 
|  | 1351 | lock_sock(sk); | 
|  | 1352 |  | 
|  | 1353 | /* remove device specific receive entries */ | 
|  | 1354 | list_for_each_entry(op, &bo->rx_ops, list) | 
|  | 1355 | if (op->rx_reg_dev == dev) | 
|  | 1356 | bcm_rx_unreg(dev, op); | 
|  | 1357 |  | 
|  | 1358 | /* remove device reference, if this is our bound device */ | 
|  | 1359 | if (bo->bound && bo->ifindex == dev->ifindex) { | 
|  | 1360 | bo->bound   = 0; | 
|  | 1361 | bo->ifindex = 0; | 
|  | 1362 | notify_enodev = 1; | 
|  | 1363 | } | 
|  | 1364 |  | 
|  | 1365 | release_sock(sk); | 
|  | 1366 |  | 
|  | 1367 | if (notify_enodev) { | 
|  | 1368 | sk->sk_err = ENODEV; | 
|  | 1369 | if (!sock_flag(sk, SOCK_DEAD)) | 
|  | 1370 | sk->sk_error_report(sk); | 
|  | 1371 | } | 
|  | 1372 | break; | 
|  | 1373 |  | 
|  | 1374 | case NETDEV_DOWN: | 
|  | 1375 | if (bo->bound && bo->ifindex == dev->ifindex) { | 
|  | 1376 | sk->sk_err = ENETDOWN; | 
|  | 1377 | if (!sock_flag(sk, SOCK_DEAD)) | 
|  | 1378 | sk->sk_error_report(sk); | 
|  | 1379 | } | 
|  | 1380 | } | 
|  | 1381 |  | 
|  | 1382 | return NOTIFY_DONE; | 
|  | 1383 | } | 
|  | 1384 |  | 
|  | 1385 | /* | 
|  | 1386 | * initial settings for all BCM sockets to be set at socket creation time | 
|  | 1387 | */ | 
|  | 1388 | static int bcm_init(struct sock *sk) | 
|  | 1389 | { | 
|  | 1390 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 1391 |  | 
|  | 1392 | bo->bound            = 0; | 
|  | 1393 | bo->ifindex          = 0; | 
|  | 1394 | bo->dropped_usr_msgs = 0; | 
|  | 1395 | bo->bcm_proc_read    = NULL; | 
|  | 1396 |  | 
|  | 1397 | INIT_LIST_HEAD(&bo->tx_ops); | 
|  | 1398 | INIT_LIST_HEAD(&bo->rx_ops); | 
|  | 1399 |  | 
|  | 1400 | /* set notifier */ | 
|  | 1401 | bo->notifier.notifier_call = bcm_notifier; | 
|  | 1402 |  | 
|  | 1403 | register_netdevice_notifier(&bo->notifier); | 
|  | 1404 |  | 
|  | 1405 | return 0; | 
|  | 1406 | } | 
|  | 1407 |  | 
|  | 1408 | /* | 
|  | 1409 | * standard socket functions | 
|  | 1410 | */ | 
|  | 1411 | static int bcm_release(struct socket *sock) | 
|  | 1412 | { | 
|  | 1413 | struct sock *sk = sock->sk; | 
|  | 1414 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 1415 | struct bcm_op *op, *next; | 
|  | 1416 |  | 
|  | 1417 | /* remove bcm_ops, timer, rx_unregister(), etc. */ | 
|  | 1418 |  | 
|  | 1419 | unregister_netdevice_notifier(&bo->notifier); | 
|  | 1420 |  | 
|  | 1421 | lock_sock(sk); | 
|  | 1422 |  | 
|  | 1423 | list_for_each_entry_safe(op, next, &bo->tx_ops, list) | 
|  | 1424 | bcm_remove_op(op); | 
|  | 1425 |  | 
|  | 1426 | list_for_each_entry_safe(op, next, &bo->rx_ops, list) { | 
|  | 1427 | /* | 
|  | 1428 | * Don't care if we're bound or not (due to netdev problems) | 
|  | 1429 | * can_rx_unregister() is always a save thing to do here. | 
|  | 1430 | */ | 
|  | 1431 | if (op->ifindex) { | 
|  | 1432 | /* | 
|  | 1433 | * Only remove subscriptions that had not | 
|  | 1434 | * been removed due to NETDEV_UNREGISTER | 
|  | 1435 | * in bcm_notifier() | 
|  | 1436 | */ | 
|  | 1437 | if (op->rx_reg_dev) { | 
|  | 1438 | struct net_device *dev; | 
|  | 1439 |  | 
|  | 1440 | dev = dev_get_by_index(&init_net, op->ifindex); | 
|  | 1441 | if (dev) { | 
|  | 1442 | bcm_rx_unreg(dev, op); | 
|  | 1443 | dev_put(dev); | 
|  | 1444 | } | 
|  | 1445 | } | 
|  | 1446 | } else | 
|  | 1447 | can_rx_unregister(NULL, op->can_id, | 
|  | 1448 | REGMASK(op->can_id), | 
|  | 1449 | bcm_rx_handler, op); | 
|  | 1450 |  | 
|  | 1451 | bcm_remove_op(op); | 
|  | 1452 | } | 
|  | 1453 |  | 
|  | 1454 | /* remove procfs entry */ | 
|  | 1455 | if (proc_dir && bo->bcm_proc_read) | 
|  | 1456 | remove_proc_entry(bo->procname, proc_dir); | 
|  | 1457 |  | 
|  | 1458 | /* remove device reference */ | 
|  | 1459 | if (bo->bound) { | 
|  | 1460 | bo->bound   = 0; | 
|  | 1461 | bo->ifindex = 0; | 
|  | 1462 | } | 
|  | 1463 |  | 
| Lothar Waßmann | f7e5cc0 | 2009-07-14 23:10:21 +0000 | [diff] [blame] | 1464 | sock_orphan(sk); | 
|  | 1465 | sock->sk = NULL; | 
|  | 1466 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1467 | release_sock(sk); | 
|  | 1468 | sock_put(sk); | 
|  | 1469 |  | 
|  | 1470 | return 0; | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len, | 
|  | 1474 | int flags) | 
|  | 1475 | { | 
|  | 1476 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; | 
|  | 1477 | struct sock *sk = sock->sk; | 
|  | 1478 | struct bcm_sock *bo = bcm_sk(sk); | 
|  | 1479 |  | 
| Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 1480 | if (len < sizeof(*addr)) | 
|  | 1481 | return -EINVAL; | 
|  | 1482 |  | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1483 | if (bo->bound) | 
|  | 1484 | return -EISCONN; | 
|  | 1485 |  | 
|  | 1486 | /* bind a device to this socket */ | 
|  | 1487 | if (addr->can_ifindex) { | 
|  | 1488 | struct net_device *dev; | 
|  | 1489 |  | 
|  | 1490 | dev = dev_get_by_index(&init_net, addr->can_ifindex); | 
|  | 1491 | if (!dev) | 
|  | 1492 | return -ENODEV; | 
|  | 1493 |  | 
|  | 1494 | if (dev->type != ARPHRD_CAN) { | 
|  | 1495 | dev_put(dev); | 
|  | 1496 | return -ENODEV; | 
|  | 1497 | } | 
|  | 1498 |  | 
|  | 1499 | bo->ifindex = dev->ifindex; | 
|  | 1500 | dev_put(dev); | 
|  | 1501 |  | 
|  | 1502 | } else { | 
|  | 1503 | /* no interface reference for ifindex = 0 ('any' CAN device) */ | 
|  | 1504 | bo->ifindex = 0; | 
|  | 1505 | } | 
|  | 1506 |  | 
|  | 1507 | bo->bound = 1; | 
|  | 1508 |  | 
|  | 1509 | if (proc_dir) { | 
|  | 1510 | /* unique socket address as filename */ | 
|  | 1511 | sprintf(bo->procname, "%p", sock); | 
| Alexey Dobriyan | ea00b8e | 2009-08-28 09:57:21 +0000 | [diff] [blame] | 1512 | bo->bcm_proc_read = proc_create_data(bo->procname, 0644, | 
|  | 1513 | proc_dir, | 
|  | 1514 | &bcm_proc_fops, sk); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1515 | } | 
|  | 1516 |  | 
|  | 1517 | return 0; | 
|  | 1518 | } | 
|  | 1519 |  | 
|  | 1520 | static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock, | 
|  | 1521 | struct msghdr *msg, size_t size, int flags) | 
|  | 1522 | { | 
|  | 1523 | struct sock *sk = sock->sk; | 
|  | 1524 | struct sk_buff *skb; | 
|  | 1525 | int error = 0; | 
|  | 1526 | int noblock; | 
|  | 1527 | int err; | 
|  | 1528 |  | 
|  | 1529 | noblock =  flags & MSG_DONTWAIT; | 
|  | 1530 | flags   &= ~MSG_DONTWAIT; | 
|  | 1531 | skb = skb_recv_datagram(sk, flags, noblock, &error); | 
|  | 1532 | if (!skb) | 
|  | 1533 | return error; | 
|  | 1534 |  | 
|  | 1535 | if (skb->len < size) | 
|  | 1536 | size = skb->len; | 
|  | 1537 |  | 
|  | 1538 | err = memcpy_toiovec(msg->msg_iov, skb->data, size); | 
|  | 1539 | if (err < 0) { | 
|  | 1540 | skb_free_datagram(sk, skb); | 
|  | 1541 | return err; | 
|  | 1542 | } | 
|  | 1543 |  | 
| Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 1544 | sock_recv_ts_and_drops(msg, sk, skb); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1545 |  | 
|  | 1546 | if (msg->msg_name) { | 
|  | 1547 | msg->msg_namelen = sizeof(struct sockaddr_can); | 
|  | 1548 | memcpy(msg->msg_name, skb->cb, msg->msg_namelen); | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | skb_free_datagram(sk, skb); | 
|  | 1552 |  | 
|  | 1553 | return size; | 
|  | 1554 | } | 
|  | 1555 |  | 
|  | 1556 | static struct proto_ops bcm_ops __read_mostly = { | 
|  | 1557 | .family        = PF_CAN, | 
|  | 1558 | .release       = bcm_release, | 
|  | 1559 | .bind          = sock_no_bind, | 
|  | 1560 | .connect       = bcm_connect, | 
|  | 1561 | .socketpair    = sock_no_socketpair, | 
|  | 1562 | .accept        = sock_no_accept, | 
|  | 1563 | .getname       = sock_no_getname, | 
|  | 1564 | .poll          = datagram_poll, | 
|  | 1565 | .ioctl         = NULL,		/* use can_ioctl() from af_can.c */ | 
|  | 1566 | .listen        = sock_no_listen, | 
|  | 1567 | .shutdown      = sock_no_shutdown, | 
|  | 1568 | .setsockopt    = sock_no_setsockopt, | 
|  | 1569 | .getsockopt    = sock_no_getsockopt, | 
|  | 1570 | .sendmsg       = bcm_sendmsg, | 
|  | 1571 | .recvmsg       = bcm_recvmsg, | 
|  | 1572 | .mmap          = sock_no_mmap, | 
|  | 1573 | .sendpage      = sock_no_sendpage, | 
|  | 1574 | }; | 
|  | 1575 |  | 
|  | 1576 | static struct proto bcm_proto __read_mostly = { | 
|  | 1577 | .name       = "CAN_BCM", | 
|  | 1578 | .owner      = THIS_MODULE, | 
|  | 1579 | .obj_size   = sizeof(struct bcm_sock), | 
|  | 1580 | .init       = bcm_init, | 
|  | 1581 | }; | 
|  | 1582 |  | 
|  | 1583 | static struct can_proto bcm_can_proto __read_mostly = { | 
|  | 1584 | .type       = SOCK_DGRAM, | 
|  | 1585 | .protocol   = CAN_BCM, | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1586 | .ops        = &bcm_ops, | 
|  | 1587 | .prot       = &bcm_proto, | 
|  | 1588 | }; | 
|  | 1589 |  | 
|  | 1590 | static int __init bcm_module_init(void) | 
|  | 1591 | { | 
|  | 1592 | int err; | 
|  | 1593 |  | 
|  | 1594 | printk(banner); | 
|  | 1595 |  | 
|  | 1596 | err = can_proto_register(&bcm_can_proto); | 
|  | 1597 | if (err < 0) { | 
|  | 1598 | printk(KERN_ERR "can: registration of bcm protocol failed\n"); | 
|  | 1599 | return err; | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | /* create /proc/net/can-bcm directory */ | 
|  | 1603 | proc_dir = proc_mkdir("can-bcm", init_net.proc_net); | 
| Oliver Hartkopp | ffd980f | 2007-11-16 15:53:52 -0800 | [diff] [blame] | 1604 | return 0; | 
|  | 1605 | } | 
|  | 1606 |  | 
|  | 1607 | static void __exit bcm_module_exit(void) | 
|  | 1608 | { | 
|  | 1609 | can_proto_unregister(&bcm_can_proto); | 
|  | 1610 |  | 
|  | 1611 | if (proc_dir) | 
|  | 1612 | proc_net_remove(&init_net, "can-bcm"); | 
|  | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | module_init(bcm_module_init); | 
|  | 1616 | module_exit(bcm_module_exit); |