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