blob: 2f47039c79dd1f604f890fff01e1d40e7e84e369 [file] [log] [blame]
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001/*
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 Hartkopp73e87e02008-04-15 19:29:14 -070046#include <linux/hrtimer.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080047#include <linux/list.h>
48#include <linux/proc_fs.h>
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +000049#include <linux/seq_file.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080050#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 Hartkoppd253eee2008-12-03 15:52:35 -080068#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 Hartkoppffd980f2007-11-16 15:53:52 -080071
Oliver Hartkoppd253eee2008-12-03 15:52:35 -080072#define CAN_BCM_VERSION CAN_VERSION
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080073static __initdata const char banner[] = KERN_INFO
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -080074 "can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n";
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080075
76MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
77MODULE_LICENSE("Dual BSD/GPL");
78MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
Lothar Waßmannb13bb2e2009-07-14 23:12:25 +000079MODULE_ALIAS("can-proto-2");
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080080
81/* easy access to can_frame payload */
82static inline u64 GET_U64(const struct can_frame *cp)
83{
84 return *(u64 *)cp->data;
85}
86
87struct bcm_op {
88 struct list_head list;
89 int ifindex;
90 canid_t can_id;
91 int flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080092 unsigned long frames_abs, frames_filtered;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080093 struct timeval ival1, ival2;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070094 struct hrtimer timer, thrtimer;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -080095 struct tasklet_struct tsklet, thrtsklet;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070096 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080097 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
109static struct proc_dir_entry *proc_dir;
110
111struct 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
123static 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 Hartkoppffd980f2007-11-16 15:53:52 -0800133 * procfs functions
134 */
135static 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 Dobriyanea00b8e2009-08-28 09:57:21 +0000150static int bcm_proc_show(struct seq_file *m, void *v)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800151{
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000152 struct sock *sk = (struct sock *)m->private;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800153 struct bcm_sock *bo = bcm_sk(sk);
154 struct bcm_op *op;
155
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000156 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 Hartkoppffd980f2007-11-16 15:53:52 -0800162
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 Dobriyanea00b8e2009-08-28 09:57:21 +0000171 seq_printf(m, "rx_op: %03X %-5s ",
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800172 op->can_id, bcm_proc_getifname(op->ifindex));
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000173 seq_printf(m, "[%d]%c ", op->nframes,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800174 (op->flags & RX_CHECK_DLC)?'d':' ');
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700175 if (op->kt_ival1.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000176 seq_printf(m, "timeo=%lld ",
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700177 (long long)
178 ktime_to_us(op->kt_ival1));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800179
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700180 if (op->kt_ival2.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000181 seq_printf(m, "thr=%lld ",
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700182 (long long)
183 ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800184
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000185 seq_printf(m, "# recv %ld (%ld) => reduction: ",
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800186 op->frames_filtered, op->frames_abs);
187
188 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
189
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000190 seq_printf(m, "%s%ld%%\n",
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800191 (reduction == 100)?"near ":"", reduction);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800192 }
193
194 list_for_each_entry(op, &bo->tx_ops, list) {
195
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000196 seq_printf(m, "tx_op: %03X %s [%d] ",
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800197 op->can_id, bcm_proc_getifname(op->ifindex),
198 op->nframes);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800199
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700200 if (op->kt_ival1.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000201 seq_printf(m, "t1=%lld ",
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700202 (long long) ktime_to_us(op->kt_ival1));
203
204 if (op->kt_ival2.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000205 seq_printf(m, "t2=%lld ",
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700206 (long long) ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800207
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000208 seq_printf(m, "# sent %ld\n", op->frames_abs);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800209 }
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000210 seq_putc(m, '\n');
211 return 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800212}
213
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000214static int bcm_proc_open(struct inode *inode, struct file *file)
215{
216 return single_open(file, bcm_proc_show, PDE(inode)->data);
217}
218
219static 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 Hartkoppffd980f2007-11-16 15:53:52 -0800227/*
228 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
229 * of the given bcm tx op
230 */
231static 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 */
273static 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 Hartkopp7f2d38e2008-07-05 23:38:43 -0700291 firstframe = (struct can_frame *)skb_tail_pointer(skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800292
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 Hartkopp6e5c1722009-01-04 17:31:18 -0800333static 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 Hartkoppc53a6ee2009-01-14 21:06:55 -0800338 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800339
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800340 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 Hartkopp6e5c1722009-01-04 17:31:18 -0800374}
375
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800376/*
377 * bcm_tx_timeout_handler - performes cyclic CAN frame transmissions
378 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700379static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800380{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700381 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800382
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800383 tasklet_schedule(&op->tsklet);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800384
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800385 return HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800386}
387
388/*
389 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
390 */
391static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data)
392{
393 struct bcm_msg_head head;
394
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800395 /* 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 Hartkopp6e5c1722009-01-04 17:31:18 -0800402 /* this element is not throttled anymore */
403 data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV);
404
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800405 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 */
421static void bcm_rx_update_and_send(struct bcm_op *op,
422 struct can_frame *lastdata,
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800423 const struct can_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800424{
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800425 memcpy(lastdata, rxdata, CFSIZ);
426
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800427 /* mark as used and throttled by default */
428 lastdata->can_dlc |= (RX_RECV|RX_THR);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800429
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800430 /* throtteling mode inactive ? */
431 if (!op->kt_ival2.tv64) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800432 /* send RX_CHANGED to the user immediately */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800433 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700434 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800435 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700436
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800437 /* with active throttling timer we are just done here */
438 if (hrtimer_active(&op->thrtimer))
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700439 return;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700440
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800441 /* first receiption with enabled throttling mode */
442 if (!op->kt_lastmsg.tv64)
443 goto rx_changed_settime;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700444
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800445 /* got a second frame inside a potential throttle period? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700446 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
447 ktime_to_us(op->kt_ival2)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800448 /* do not send the saved data - only start throttle timer */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700449 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 Hartkopp6e5c1722009-01-04 17:31:18 -0800456rx_changed_settime:
457 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700458 op->kt_lastmsg = ktime_get();
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800459}
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 */
465static void bcm_rx_cmp_to_index(struct bcm_op *op, int index,
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800466 const struct can_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800467{
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 */
501static void bcm_rx_starttimer(struct bcm_op *op)
502{
503 if (op->flags & RX_NO_AUTOTIMER)
504 return;
505
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700506 if (op->kt_ival1.tv64)
507 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800508}
509
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800510static void bcm_rx_timeout_tsklet(unsigned long data)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800511{
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800512 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800513 struct bcm_msg_head msg_head;
514
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800515 /* create notification to user */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800516 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 Hartkopp6e5c1722009-01-04 17:31:18 -0800525}
526
527/*
528 * bcm_rx_timeout_handler - when the (cyclic) CAN frame receiption timed out
529 */
530static 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 Hartkoppffd980f2007-11-16 15:53:52 -0800536
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 Hartkopp73e87e02008-04-15 19:29:14 -0700544
545 return HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800546}
547
548/*
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800549 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800550 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800551static 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 */
567static int bcm_rx_thr_flush(struct bcm_op *op, int update)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800568{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700569 int updated = 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800570
571 if (op->nframes > 1) {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700572 int i;
573
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800574 /* for MUX filter we start at index 1 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800575 for (i = 1; i < op->nframes; i++)
576 updated += bcm_rx_do_flush(op, update, i);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800577
578 } else {
579 /* for RX_FILTER_ID and simple filter */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800580 updated += bcm_rx_do_flush(op, update, 0);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800581 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700582
583 return updated;
584}
585
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800586static 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 Hartkopp73e87e02008-04-15 19:29:14 -0700594/*
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 */
598static 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 Hartkopp6e5c1722009-01-04 17:31:18 -0800602 tasklet_schedule(&op->thrtsklet);
603
604 if (bcm_rx_thr_flush(op, 0)) {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700605 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 Hartkoppffd980f2007-11-16 15:53:52 -0800612}
613
614/*
615 * bcm_rx_handler - handle a CAN frame receiption
616 */
617static void bcm_rx_handler(struct sk_buff *skb, void *data)
618{
619 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800620 const struct can_frame *rxframe = (struct can_frame *)skb->data;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800621 int i;
622
623 /* disable timeout */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700624 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800625
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800626 if (op->can_id != rxframe->can_id)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800627 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800628
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800629 /* 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 Hartkoppffd980f2007-11-16 15:53:52 -0800635
636 if (op->flags & RX_RTR_FRAME) {
637 /* send reply for RTR-request (placed in op->frames[0]) */
638 bcm_can_tx(op);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800639 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800640 }
641
642 if (op->flags & RX_FILTER_ID) {
643 /* the easiest case */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800644 bcm_rx_update_and_send(op, &op->last_frames[0], rxframe);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800645 goto rx_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800646 }
647
648 if (op->nframes == 1) {
649 /* simple compare with index 0 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800650 bcm_rx_cmp_to_index(op, 0, rxframe);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800651 goto rx_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800652 }
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 Hartkopp6e5c1722009-01-04 17:31:18 -0800663 if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) ==
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800664 (GET_U64(&op->frames[0]) &
665 GET_U64(&op->frames[i]))) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800666 bcm_rx_cmp_to_index(op, i, rxframe);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800667 break;
668 }
669 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800670 }
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800671
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800672rx_starttimer:
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800673 bcm_rx_starttimer(op);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800674}
675
676/*
677 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
678 */
679static 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
692static void bcm_remove_op(struct bcm_op *op)
693{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700694 hrtimer_cancel(&op->timer);
695 hrtimer_cancel(&op->thrtimer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800696
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800697 if (op->tsklet.func)
698 tasklet_kill(&op->tsklet);
699
700 if (op->thrtsklet.func)
701 tasklet_kill(&op->thrtsklet);
702
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800703 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
714static 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 */
730static 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 */
775static 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 */
793static 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 */
816static 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 Hartkopp7f2d38e2008-07-05 23:38:43 -0700849
850 if (op->frames[i].can_dlc > 8)
851 err = -EINVAL;
852
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800853 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 Hartkopp7f2d38e2008-07-05 23:38:43 -0700885
886 if (op->frames[i].can_dlc > 8)
887 err = -EINVAL;
888
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800889 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 Hartkopp73e87e02008-04-15 19:29:14 -0700910 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
911 op->timer.function = bcm_tx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800912
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800913 /* initialize tasklet for tx countevent notification */
914 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
915 (unsigned long) op);
916
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800917 /* currently unused in tx_ops */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700918 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800919
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 Hartkopp73e87e02008-04-15 19:29:14 -0700945 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
946 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800947
948 /* disable an active timer due to zero values? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700949 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
950 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800951 }
952
953 if ((op->flags & STARTTIMER) &&
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700954 ((op->kt_ival1.tv64 && op->count) || op->kt_ival2.tv64)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800955
956 /* spec: send can_frame when starting timer */
957 op->flags |= TX_ANNOUNCE;
958
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700959 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800960 /* op->count-- is done in bcm_tx_timeout_handler */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700961 hrtimer_start(&op->timer, op->kt_ival1,
962 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800963 } else
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700964 hrtimer_start(&op->timer, op->kt_ival2,
965 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800966 }
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 */
977static 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 Hartkopp73e87e02008-04-15 19:29:14 -07001077 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1078 op->timer.function = bcm_rx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001079
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001080 /* initialize tasklet for rx timeout notification */
1081 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1082 (unsigned long) op);
1083
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001084 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1085 op->thrtimer.function = bcm_rx_thr_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001086
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001087 /* initialize tasklet for rx throttle handling */
1088 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1089 (unsigned long) op);
1090
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001091 /* 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 Hartkopp73e87e02008-04-15 19:29:14 -07001105 hrtimer_cancel(&op->thrtimer);
1106 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001107
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 Hartkopp73e87e02008-04-15 19:29:14 -07001123 op->kt_ival1 = timeval_to_ktime(msg_head->ival1);
1124 op->kt_ival2 = timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001125
1126 /* disable an active timer due to zero value? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001127 if (!op->kt_ival1.tv64)
1128 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001129
1130 /*
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001131 * In any case cancel the throttle timer, flush
1132 * potentially blocked msgs and reset throttle handling
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001133 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001134 op->kt_lastmsg = ktime_set(0, 0);
1135 hrtimer_cancel(&op->thrtimer);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001136 bcm_rx_thr_flush(op, 1);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001137 }
1138
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001139 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1140 hrtimer_start(&op->timer, op->kt_ival1,
1141 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001142 }
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 */
1178static 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 Hartkopp7f2d38e2008-07-05 23:38:43 -07001207 err = can_send(skb, 1); /* send with loopback */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001208 dev_put(dev);
1209
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001210 if (err)
1211 return err;
1212
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001213 return CFSIZ + MHSIZ;
1214}
1215
1216/*
1217 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1218 */
1219static 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 Hartkopp7f2d38e2008-07-05 23:38:43 -07001231 /* check for valid message length from userspace */
1232 if (size < MHSIZ || (size - MHSIZ) % CFSIZ)
1233 return -EINVAL;
1234
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001235 /* 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 Hartkopp7f2d38e2008-07-05 23:38:43 -07001309 /* we need exactly one can_frame behind the msg head */
1310 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001311 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 */
1329static 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 Hideaki721499e2008-07-19 22:34:43 -07001338 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001339 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 */
1384static 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 */
1407static 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ßmannf7e5cc02009-07-14 23:10:21 +00001460 sock_orphan(sk);
1461 sock->sk = NULL;
1462
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001463 release_sock(sk);
1464 sock_put(sk);
1465
1466 return 0;
1467}
1468
1469static 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 Dobriyanea00b8e2009-08-28 09:57:21 +00001505 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1506 proc_dir,
1507 &bcm_proc_fops, sk);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001508 }
1509
1510 return 0;
1511}
1512
1513static 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
Neil Horman3b885782009-10-12 13:26:31 -07001537 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001538
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
1549static 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
1569static 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
1576static 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
1584static 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 Hartkoppffd980f2007-11-16 15:53:52 -08001598 return 0;
1599}
1600
1601static 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
1609module_init(bcm_module_init);
1610module_exit(bcm_module_exit);